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