1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * IO workarounds for PCI on Celleb/Cell platform
4  *
5  * (C) Copyright 2006-2007 TOSHIBA CORPORATION
6  */
7 
8 #undef DEBUG
9 
10 #include <linux/kernel.h>
11 #include <linux/of_address.h>
12 #include <linux/of_platform.h>
13 #include <linux/slab.h>
14 #include <linux/io.h>
15 
16 #include <asm/ppc-pci.h>
17 #include <asm/pci-bridge.h>
18 #include <asm/io-workarounds.h>
19 
20 #define SPIDER_PCI_DISABLE_PREFETCH
21 
22 struct spiderpci_iowa_private {
23 	void __iomem *regs;
24 };
25 
spiderpci_io_flush(struct iowa_bus * bus)26 static void spiderpci_io_flush(struct iowa_bus *bus)
27 {
28 	struct spiderpci_iowa_private *priv;
29 
30 	priv = bus->private;
31 	in_be32(priv->regs + SPIDER_PCI_DUMMY_READ);
32 	iosync();
33 }
34 
35 #define SPIDER_PCI_MMIO_READ(name, ret)					\
36 static ret spiderpci_##name(const PCI_IO_ADDR addr)			\
37 {									\
38 	ret val = __do_##name(addr);					\
39 	spiderpci_io_flush(iowa_mem_find_bus(addr));			\
40 	return val;							\
41 }
42 
43 #define SPIDER_PCI_MMIO_READ_STR(name)					\
44 static void spiderpci_##name(const PCI_IO_ADDR addr, void *buf, 	\
45 			     unsigned long count)			\
46 {									\
47 	__do_##name(addr, buf, count);					\
48 	spiderpci_io_flush(iowa_mem_find_bus(addr));			\
49 }
50 
SPIDER_PCI_MMIO_READ(readb,u8)51 SPIDER_PCI_MMIO_READ(readb, u8)
52 SPIDER_PCI_MMIO_READ(readw, u16)
53 SPIDER_PCI_MMIO_READ(readl, u32)
54 SPIDER_PCI_MMIO_READ(readq, u64)
55 SPIDER_PCI_MMIO_READ(readw_be, u16)
56 SPIDER_PCI_MMIO_READ(readl_be, u32)
57 SPIDER_PCI_MMIO_READ(readq_be, u64)
58 SPIDER_PCI_MMIO_READ_STR(readsb)
59 SPIDER_PCI_MMIO_READ_STR(readsw)
60 SPIDER_PCI_MMIO_READ_STR(readsl)
61 
62 static void spiderpci_memcpy_fromio(void *dest, const PCI_IO_ADDR src,
63 				    unsigned long n)
64 {
65 	__do_memcpy_fromio(dest, src, n);
66 	spiderpci_io_flush(iowa_mem_find_bus(src));
67 }
68 
spiderpci_pci_setup_chip(struct pci_controller * phb,void __iomem * regs)69 static int __init spiderpci_pci_setup_chip(struct pci_controller *phb,
70 					   void __iomem *regs)
71 {
72 	void *dummy_page_va;
73 	dma_addr_t dummy_page_da;
74 
75 #ifdef SPIDER_PCI_DISABLE_PREFETCH
76 	u32 val = in_be32(regs + SPIDER_PCI_VCI_CNTL_STAT);
77 	pr_debug("SPIDER_IOWA:PVCI_Control_Status was 0x%08x\n", val);
78 	out_be32(regs + SPIDER_PCI_VCI_CNTL_STAT, val | 0x8);
79 #endif /* SPIDER_PCI_DISABLE_PREFETCH */
80 
81 	/* setup dummy read */
82 	/*
83 	 * On CellBlade, we can't know that which XDR memory is used by
84 	 * kmalloc() to allocate dummy_page_va.
85 	 * In order to improve the performance, the XDR which is used to
86 	 * allocate dummy_page_va is the nearest the spider-pci.
87 	 * We have to select the CBE which is the nearest the spider-pci
88 	 * to allocate memory from the best XDR, but I don't know that
89 	 * how to do.
90 	 *
91 	 * Celleb does not have this problem, because it has only one XDR.
92 	 */
93 	dummy_page_va = kmalloc(PAGE_SIZE, GFP_KERNEL);
94 	if (!dummy_page_va) {
95 		pr_err("SPIDERPCI-IOWA:Alloc dummy_page_va failed.\n");
96 		return -1;
97 	}
98 
99 	dummy_page_da = dma_map_single(phb->parent, dummy_page_va,
100 				       PAGE_SIZE, DMA_FROM_DEVICE);
101 	if (dma_mapping_error(phb->parent, dummy_page_da)) {
102 		pr_err("SPIDER-IOWA:Map dummy page filed.\n");
103 		kfree(dummy_page_va);
104 		return -1;
105 	}
106 
107 	out_be32(regs + SPIDER_PCI_DUMMY_READ_BASE, dummy_page_da);
108 
109 	return 0;
110 }
111 
spiderpci_iowa_init(struct iowa_bus * bus,void * data)112 int __init spiderpci_iowa_init(struct iowa_bus *bus, void *data)
113 {
114 	void __iomem *regs = NULL;
115 	struct spiderpci_iowa_private *priv;
116 	struct device_node *np = bus->phb->dn;
117 	struct resource r;
118 	unsigned long offset = (unsigned long)data;
119 
120 	pr_debug("SPIDERPCI-IOWA:Bus initialize for spider(%pOF)\n",
121 		 np);
122 
123 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
124 	if (!priv) {
125 		pr_err("SPIDERPCI-IOWA:"
126 		       "Can't allocate struct spiderpci_iowa_private");
127 		return -1;
128 	}
129 
130 	if (of_address_to_resource(np, 0, &r)) {
131 		pr_err("SPIDERPCI-IOWA:Can't get resource.\n");
132 		goto error;
133 	}
134 
135 	regs = ioremap(r.start + offset, SPIDER_PCI_REG_SIZE);
136 	if (!regs) {
137 		pr_err("SPIDERPCI-IOWA:ioremap failed.\n");
138 		goto error;
139 	}
140 	priv->regs = regs;
141 	bus->private = priv;
142 
143 	if (spiderpci_pci_setup_chip(bus->phb, regs))
144 		goto error;
145 
146 	return 0;
147 
148 error:
149 	kfree(priv);
150 	bus->private = NULL;
151 
152 	if (regs)
153 		iounmap(regs);
154 
155 	return -1;
156 }
157 
158 struct ppc_pci_io spiderpci_ops = {
159 	.readb = spiderpci_readb,
160 	.readw = spiderpci_readw,
161 	.readl = spiderpci_readl,
162 	.readq = spiderpci_readq,
163 	.readw_be = spiderpci_readw_be,
164 	.readl_be = spiderpci_readl_be,
165 	.readq_be = spiderpci_readq_be,
166 	.readsb = spiderpci_readsb,
167 	.readsw = spiderpci_readsw,
168 	.readsl = spiderpci_readsl,
169 	.memcpy_fromio = spiderpci_memcpy_fromio,
170 };
171 
172