blob: 423339be1bac8f3c2a0ef2400fcf19ede0275682 [file] [log] [blame]
Ishizaki Kouda0bd342007-10-02 18:26:53 +10001/*
2 * Support for Celleb io workarounds
3 *
4 * (C) Copyright 2006-2007 TOSHIBA CORPORATION
5 *
6 * This file is based to arch/powerpc/platform/cell/io-workarounds.c
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#undef DEBUG
24
Grant Likely283029d2008-01-09 06:20:40 +110025#include <linux/of.h>
Ishizaki Kouda0bd342007-10-02 18:26:53 +100026#include <linux/of_device.h>
27#include <linux/irq.h>
28
29#include <asm/io.h>
30#include <asm/prom.h>
31#include <asm/machdep.h>
32#include <asm/pci-bridge.h>
33#include <asm/ppc-pci.h>
34
35#include "pci.h"
36
37#define MAX_CELLEB_PCI_BUS 4
38
39void *celleb_dummy_page_va;
40
41static struct celleb_pci_bus {
42 struct pci_controller *phb;
43 void (*dummy_read)(struct pci_controller *);
44} celleb_pci_busses[MAX_CELLEB_PCI_BUS];
45
46static int celleb_pci_count = 0;
47
48static struct celleb_pci_bus *celleb_pci_find(unsigned long vaddr,
49 unsigned long paddr)
50{
51 int i, j;
52 struct resource *res;
53
54 for (i = 0; i < celleb_pci_count; i++) {
55 struct celleb_pci_bus *bus = &celleb_pci_busses[i];
56 struct pci_controller *phb = bus->phb;
57 if (paddr)
58 for (j = 0; j < 3; j++) {
59 res = &phb->mem_resources[j];
60 if (paddr >= res->start && paddr <= res->end)
61 return bus;
62 }
63 res = &phb->io_resource;
64 if (vaddr && vaddr >= res->start && vaddr <= res->end)
65 return bus;
66 }
67 return NULL;
68}
69
70static void celleb_io_flush(const PCI_IO_ADDR addr)
71{
72 struct celleb_pci_bus *bus;
73 int token;
74
75 token = PCI_GET_ADDR_TOKEN(addr);
76
77 if (token && token <= celleb_pci_count)
78 bus = &celleb_pci_busses[token - 1];
79 else {
80 unsigned long vaddr, paddr;
81 pte_t *ptep;
82
83 vaddr = (unsigned long)PCI_FIX_ADDR(addr);
84 if (vaddr < PHB_IO_BASE || vaddr >= PHB_IO_END)
85 return;
86
87 ptep = find_linux_pte(init_mm.pgd, vaddr);
88 if (ptep == NULL)
89 paddr = 0;
90 else
91 paddr = pte_pfn(*ptep) << PAGE_SHIFT;
92 bus = celleb_pci_find(vaddr, paddr);
93
94 if (bus == NULL)
95 return;
96 }
97
98 if (bus->dummy_read)
99 bus->dummy_read(bus->phb);
100}
101
102static u8 celleb_readb(const PCI_IO_ADDR addr)
103{
104 u8 val;
105 val = __do_readb(addr);
106 celleb_io_flush(addr);
107 return val;
108}
109
110static u16 celleb_readw(const PCI_IO_ADDR addr)
111{
112 u16 val;
113 val = __do_readw(addr);
114 celleb_io_flush(addr);
115 return val;
116}
117
118static u32 celleb_readl(const PCI_IO_ADDR addr)
119{
120 u32 val;
121 val = __do_readl(addr);
122 celleb_io_flush(addr);
123 return val;
124}
125
126static u64 celleb_readq(const PCI_IO_ADDR addr)
127{
128 u64 val;
129 val = __do_readq(addr);
130 celleb_io_flush(addr);
131 return val;
132}
133
134static u16 celleb_readw_be(const PCI_IO_ADDR addr)
135{
136 u16 val;
137 val = __do_readw_be(addr);
138 celleb_io_flush(addr);
139 return val;
140}
141
142static u32 celleb_readl_be(const PCI_IO_ADDR addr)
143{
144 u32 val;
145 val = __do_readl_be(addr);
146 celleb_io_flush(addr);
147 return val;
148}
149
150static u64 celleb_readq_be(const PCI_IO_ADDR addr)
151{
152 u64 val;
153 val = __do_readq_be(addr);
154 celleb_io_flush(addr);
155 return val;
156}
157
158static void celleb_readsb(const PCI_IO_ADDR addr,
159 void *buf, unsigned long count)
160{
161 __do_readsb(addr, buf, count);
162 celleb_io_flush(addr);
163}
164
165static void celleb_readsw(const PCI_IO_ADDR addr,
166 void *buf, unsigned long count)
167{
168 __do_readsw(addr, buf, count);
169 celleb_io_flush(addr);
170}
171
172static void celleb_readsl(const PCI_IO_ADDR addr,
173 void *buf, unsigned long count)
174{
175 __do_readsl(addr, buf, count);
176 celleb_io_flush(addr);
177}
178
179static void celleb_memcpy_fromio(void *dest,
180 const PCI_IO_ADDR src,
181 unsigned long n)
182{
183 __do_memcpy_fromio(dest, src, n);
184 celleb_io_flush(src);
185}
186
187static void __iomem *celleb_ioremap(unsigned long addr,
188 unsigned long size,
189 unsigned long flags)
190{
191 struct celleb_pci_bus *bus;
192 void __iomem *res = __ioremap(addr, size, flags);
193 int busno;
194
195 bus = celleb_pci_find(0, addr);
196 if (bus != NULL) {
197 busno = bus - celleb_pci_busses;
198 PCI_SET_ADDR_TOKEN(res, busno + 1);
199 }
200 return res;
201}
202
203static void celleb_iounmap(volatile void __iomem *addr)
204{
205 return __iounmap(PCI_FIX_ADDR(addr));
206}
207
208static struct ppc_pci_io celleb_pci_io __initdata = {
209 .readb = celleb_readb,
210 .readw = celleb_readw,
211 .readl = celleb_readl,
212 .readq = celleb_readq,
213 .readw_be = celleb_readw_be,
214 .readl_be = celleb_readl_be,
215 .readq_be = celleb_readq_be,
216 .readsb = celleb_readsb,
217 .readsw = celleb_readsw,
218 .readsl = celleb_readsl,
219 .memcpy_fromio = celleb_memcpy_fromio,
220};
221
222void __init celleb_pci_add_one(struct pci_controller *phb,
223 void (*dummy_read)(struct pci_controller *))
224{
225 struct celleb_pci_bus *bus = &celleb_pci_busses[celleb_pci_count];
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100226 struct device_node *np = phb->dn;
Ishizaki Kouda0bd342007-10-02 18:26:53 +1000227
228 if (celleb_pci_count >= MAX_CELLEB_PCI_BUS) {
229 printk(KERN_ERR "Too many pci bridges, workarounds"
230 " disabled for %s\n", np->full_name);
231 return;
232 }
233
234 celleb_pci_count++;
235
236 bus->phb = phb;
237 bus->dummy_read = dummy_read;
238}
239
240static struct of_device_id celleb_pci_workaround_match[] __initdata = {
241 {
242 .name = "pci-pseudo",
243 .data = fake_pci_workaround_init,
244 }, {
245 .name = "epci",
246 .data = epci_workaround_init,
247 }, {
248 },
249};
250
251int __init celleb_pci_workaround_init(void)
252{
253 struct pci_controller *phb;
254 struct device_node *node;
255 const struct of_device_id *match;
256 void (*init_func)(struct pci_controller *);
257
258 celleb_dummy_page_va = kmalloc(PAGE_SIZE, GFP_KERNEL);
259 if (!celleb_dummy_page_va) {
joe@perches.comdf3c9012007-11-20 12:47:55 +1100260 printk(KERN_ERR "Celleb: dummy read disabled. "
Ishizaki Kouda0bd342007-10-02 18:26:53 +1000261 "Alloc celleb_dummy_page_va failed\n");
262 return 1;
263 }
264
265 list_for_each_entry(phb, &hose_list, list_node) {
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100266 node = phb->dn;
Ishizaki Kouda0bd342007-10-02 18:26:53 +1000267 match = of_match_node(celleb_pci_workaround_match, node);
268
269 if (match) {
270 init_func = match->data;
271 (*init_func)(phb);
272 }
273 }
274
275 ppc_pci_io = celleb_pci_io;
276 ppc_md.ioremap = celleb_ioremap;
277 ppc_md.iounmap = celleb_iounmap;
278
279 return 0;
280}