blob: 539d2cc595428ecdecf34fb76ea582e456e705da [file] [log] [blame]
Ishizaki Kou551a3d82007-01-12 10:03:28 +09001/*
2 * Support for PCI on Celleb platform.
3 *
4 * (C) Copyright 2006-2007 TOSHIBA CORPORATION
5 *
6 * This code is based on arch/powerpc/kernel/rtas_pci.c:
7 * Copyright (C) 2001 Dave Engebretsen, IBM Corporation
8 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
25#undef DEBUG
26
27#include <linux/kernel.h>
28#include <linux/threads.h>
29#include <linux/pci.h>
30#include <linux/string.h>
31#include <linux/init.h>
32#include <linux/bootmem.h>
33#include <linux/pci_regs.h>
Grant Likely283029d2008-01-09 06:20:40 +110034#include <linux/of.h>
Ishizaki Kouda0bd342007-10-02 18:26:53 +100035#include <linux/of_device.h>
Ishizaki Kou551a3d82007-01-12 10:03:28 +090036
37#include <asm/io.h>
38#include <asm/irq.h>
39#include <asm/prom.h>
40#include <asm/machdep.h>
41#include <asm/pci-bridge.h>
42#include <asm/ppc-pci.h>
43
Ishizaki Kou6ec859e2008-04-24 19:24:13 +100044#include "../cell/io-workarounds.h"
Ishizaki Kou551a3d82007-01-12 10:03:28 +090045#include "pci.h"
46#include "interrupt.h"
47
48#define MAX_PCI_DEVICES 32
49#define MAX_PCI_FUNCTIONS 8
50#define MAX_PCI_BASE_ADDRS 3 /* use 64 bit address */
51
52/* definition for fake pci configuration area for GbE, .... ,and etc. */
53
54struct celleb_pci_resource {
55 struct resource r[MAX_PCI_BASE_ADDRS];
56};
57
58struct celleb_pci_private {
59 unsigned char *fake_config[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
60 struct celleb_pci_resource *res[MAX_PCI_DEVICES][MAX_PCI_FUNCTIONS];
61};
62
63static inline u8 celleb_fake_config_readb(void *addr)
64{
65 u8 *p = addr;
66 return *p;
67}
68
69static inline u16 celleb_fake_config_readw(void *addr)
70{
Al Virof1fda892007-02-09 16:39:50 +000071 __le16 *p = addr;
Ishizaki Kou551a3d82007-01-12 10:03:28 +090072 return le16_to_cpu(*p);
73}
74
75static inline u32 celleb_fake_config_readl(void *addr)
76{
Al Virof1fda892007-02-09 16:39:50 +000077 __le32 *p = addr;
Ishizaki Kou551a3d82007-01-12 10:03:28 +090078 return le32_to_cpu(*p);
79}
80
81static inline void celleb_fake_config_writeb(u32 val, void *addr)
82{
83 u8 *p = addr;
84 *p = val;
85}
86
87static inline void celleb_fake_config_writew(u32 val, void *addr)
88{
Al Virof1fda892007-02-09 16:39:50 +000089 __le16 val16;
90 __le16 *p = addr;
Ishizaki Kou551a3d82007-01-12 10:03:28 +090091 val16 = cpu_to_le16(val);
92 *p = val16;
93}
94
95static inline void celleb_fake_config_writel(u32 val, void *addr)
96{
Al Virof1fda892007-02-09 16:39:50 +000097 __le32 val32;
98 __le32 *p = addr;
Ishizaki Kou551a3d82007-01-12 10:03:28 +090099 val32 = cpu_to_le32(val);
100 *p = val32;
101}
102
103static unsigned char *get_fake_config_start(struct pci_controller *hose,
104 int devno, int fn)
105{
106 struct celleb_pci_private *private = hose->private_data;
107
108 if (private == NULL)
109 return NULL;
110
111 return private->fake_config[devno][fn];
112}
113
114static struct celleb_pci_resource *get_resource_start(
115 struct pci_controller *hose,
116 int devno, int fn)
117{
118 struct celleb_pci_private *private = hose->private_data;
119
120 if (private == NULL)
121 return NULL;
122
123 return private->res[devno][fn];
124}
125
126
127static void celleb_config_read_fake(unsigned char *config, int where,
128 int size, u32 *val)
129{
130 char *p = config + where;
131
132 switch (size) {
133 case 1:
134 *val = celleb_fake_config_readb(p);
135 break;
136 case 2:
137 *val = celleb_fake_config_readw(p);
138 break;
139 case 4:
140 *val = celleb_fake_config_readl(p);
141 break;
142 }
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900143}
144
145static void celleb_config_write_fake(unsigned char *config, int where,
146 int size, u32 val)
147{
148 char *p = config + where;
149
150 switch (size) {
151 case 1:
152 celleb_fake_config_writeb(val, p);
153 break;
154 case 2:
155 celleb_fake_config_writew(val, p);
156 break;
157 case 4:
158 celleb_fake_config_writel(val, p);
159 break;
160 }
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900161}
162
163static int celleb_fake_pci_read_config(struct pci_bus *bus,
164 unsigned int devfn, int where, int size, u32 *val)
165{
166 char *config;
167 struct device_node *node;
168 struct pci_controller *hose;
169 unsigned int devno = devfn >> 3;
170 unsigned int fn = devfn & 0x7;
171
172 /* allignment check */
173 BUG_ON(where % size);
174
175 pr_debug(" fake read: bus=0x%x, ", bus->number);
176 node = (struct device_node *)bus->sysdata;
177 hose = pci_find_hose_for_OF_device(node);
178 config = get_fake_config_start(hose, devno, fn);
179
180 pr_debug("devno=0x%x, where=0x%x, size=0x%x, ", devno, where, size);
181 if (!config) {
182 pr_debug("failed\n");
183 return PCIBIOS_DEVICE_NOT_FOUND;
184 }
185
186 celleb_config_read_fake(config, where, size, val);
187 pr_debug("val=0x%x\n", *val);
188
189 return PCIBIOS_SUCCESSFUL;
190}
191
192
193static int celleb_fake_pci_write_config(struct pci_bus *bus,
194 unsigned int devfn, int where, int size, u32 val)
195{
196 char *config;
197 struct device_node *node;
198 struct pci_controller *hose;
199 struct celleb_pci_resource *res;
200 unsigned int devno = devfn >> 3;
201 unsigned int fn = devfn & 0x7;
202
203 /* allignment check */
204 BUG_ON(where % size);
205
206 node = (struct device_node *)bus->sysdata;
207 hose = pci_find_hose_for_OF_device(node);
208 config = get_fake_config_start(hose, devno, fn);
209
210 if (!config)
211 return PCIBIOS_DEVICE_NOT_FOUND;
212
213 if (val == ~0) {
214 int i = (where - PCI_BASE_ADDRESS_0) >> 3;
215
216 switch (where) {
217 case PCI_BASE_ADDRESS_0:
218 case PCI_BASE_ADDRESS_2:
219 if (size != 4)
220 return PCIBIOS_DEVICE_NOT_FOUND;
221 res = get_resource_start(hose, devno, fn);
222 if (!res)
223 return PCIBIOS_DEVICE_NOT_FOUND;
224 celleb_config_write_fake(config, where, size,
225 (res->r[i].end - res->r[i].start));
226 return PCIBIOS_SUCCESSFUL;
227 case PCI_BASE_ADDRESS_1:
228 case PCI_BASE_ADDRESS_3:
229 case PCI_BASE_ADDRESS_4:
230 case PCI_BASE_ADDRESS_5:
231 break;
232 default:
233 break;
234 }
235 }
236
237 celleb_config_write_fake(config, where, size, val);
238 pr_debug(" fake write: where=%x, size=%d, val=%x\n",
239 where, size, val);
240
241 return PCIBIOS_SUCCESSFUL;
242}
243
244static struct pci_ops celleb_fake_pci_ops = {
Nathan Lynchaec249b2007-08-10 05:18:37 +1000245 .read = celleb_fake_pci_read_config,
246 .write = celleb_fake_pci_write_config,
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900247};
248
249static inline void celleb_setup_pci_base_addrs(struct pci_controller *hose,
250 unsigned int devno, unsigned int fn,
251 unsigned int num_base_addr)
252{
253 u32 val;
254 unsigned char *config;
255 struct celleb_pci_resource *res;
256
257 config = get_fake_config_start(hose, devno, fn);
258 res = get_resource_start(hose, devno, fn);
259
260 if (!config || !res)
261 return;
262
263 switch (num_base_addr) {
264 case 3:
265 val = (res->r[2].start & 0xfffffff0)
266 | PCI_BASE_ADDRESS_MEM_TYPE_64;
267 celleb_config_write_fake(config, PCI_BASE_ADDRESS_4, 4, val);
268 val = res->r[2].start >> 32;
269 celleb_config_write_fake(config, PCI_BASE_ADDRESS_5, 4, val);
270 /* FALLTHROUGH */
271 case 2:
272 val = (res->r[1].start & 0xfffffff0)
273 | PCI_BASE_ADDRESS_MEM_TYPE_64;
274 celleb_config_write_fake(config, PCI_BASE_ADDRESS_2, 4, val);
275 val = res->r[1].start >> 32;
276 celleb_config_write_fake(config, PCI_BASE_ADDRESS_3, 4, val);
277 /* FALLTHROUGH */
278 case 1:
279 val = (res->r[0].start & 0xfffffff0)
280 | PCI_BASE_ADDRESS_MEM_TYPE_64;
281 celleb_config_write_fake(config, PCI_BASE_ADDRESS_0, 4, val);
282 val = res->r[0].start >> 32;
283 celleb_config_write_fake(config, PCI_BASE_ADDRESS_1, 4, val);
284 break;
285 }
286
287 val = PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
288 celleb_config_write_fake(config, PCI_COMMAND, 2, val);
289}
290
Ishizaki Koue5b91872007-07-26 19:59:17 +1000291static int __init celleb_setup_fake_pci_device(struct device_node *node,
292 struct pci_controller *hose)
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900293{
294 unsigned int rlen;
295 int num_base_addr = 0;
296 u32 val;
297 const u32 *wi0, *wi1, *wi2, *wi3, *wi4;
298 unsigned int devno, fn;
299 struct celleb_pci_private *private = hose->private_data;
300 unsigned char **config = NULL;
301 struct celleb_pci_resource **res = NULL;
302 const char *name;
303 const unsigned long *li;
304 int size, result;
305
306 if (private == NULL) {
307 printk(KERN_ERR "PCI: "
308 "memory space for pci controller is not assigned\n");
309 goto error;
310 }
311
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000312 name = of_get_property(node, "model", &rlen);
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900313 if (!name) {
314 printk(KERN_ERR "PCI: model property not found.\n");
315 goto error;
316 }
317
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000318 wi4 = of_get_property(node, "reg", &rlen);
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900319 if (wi4 == NULL)
320 goto error;
321
322 devno = ((wi4[0] >> 8) & 0xff) >> 3;
323 fn = (wi4[0] >> 8) & 0x7;
324
325 pr_debug("PCI: celleb_setup_fake_pci() %s devno=%x fn=%x\n", name,
326 devno, fn);
327
328 size = 256;
329 config = &private->fake_config[devno][fn];
Stephen Rothwell7b2c3c52007-09-17 14:08:06 +1000330 *config = alloc_maybe_bootmem(size, GFP_KERNEL);
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900331 if (*config == NULL) {
332 printk(KERN_ERR "PCI: "
333 "not enough memory for fake configuration space\n");
334 goto error;
335 }
336 pr_debug("PCI: fake config area assigned 0x%016lx\n",
337 (unsigned long)*config);
338
339 size = sizeof(struct celleb_pci_resource);
340 res = &private->res[devno][fn];
Stephen Rothwell7b2c3c52007-09-17 14:08:06 +1000341 *res = alloc_maybe_bootmem(size, GFP_KERNEL);
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900342 if (*res == NULL) {
343 printk(KERN_ERR
344 "PCI: not enough memory for resource data space\n");
345 goto error;
346 }
347 pr_debug("PCI: res assigned 0x%016lx\n", (unsigned long)*res);
348
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000349 wi0 = of_get_property(node, "device-id", NULL);
350 wi1 = of_get_property(node, "vendor-id", NULL);
351 wi2 = of_get_property(node, "class-code", NULL);
352 wi3 = of_get_property(node, "revision-id", NULL);
Cyrill Gorcunov3a1c81f2007-11-29 18:44:18 +1100353 if (!wi0 || !wi1 || !wi2 || !wi3) {
354 printk(KERN_ERR "PCI: Missing device tree properties.\n");
355 goto error;
356 }
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900357
358 celleb_config_write_fake(*config, PCI_DEVICE_ID, 2, wi0[0] & 0xffff);
359 celleb_config_write_fake(*config, PCI_VENDOR_ID, 2, wi1[0] & 0xffff);
360 pr_debug("class-code = 0x%08x\n", wi2[0]);
361
362 celleb_config_write_fake(*config, PCI_CLASS_PROG, 1, wi2[0] & 0xff);
363 celleb_config_write_fake(*config, PCI_CLASS_DEVICE, 2,
364 (wi2[0] >> 8) & 0xffff);
365 celleb_config_write_fake(*config, PCI_REVISION_ID, 1, wi3[0]);
366
367 while (num_base_addr < MAX_PCI_BASE_ADDRS) {
368 result = of_address_to_resource(node,
369 num_base_addr, &(*res)->r[num_base_addr]);
370 if (result)
371 break;
372 num_base_addr++;
373 }
374
375 celleb_setup_pci_base_addrs(hose, devno, fn, num_base_addr);
376
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000377 li = of_get_property(node, "interrupts", &rlen);
Cyrill Gorcunov3a1c81f2007-11-29 18:44:18 +1100378 if (!li) {
379 printk(KERN_ERR "PCI: interrupts not found.\n");
380 goto error;
381 }
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900382 val = li[0];
383 celleb_config_write_fake(*config, PCI_INTERRUPT_PIN, 1, 1);
384 celleb_config_write_fake(*config, PCI_INTERRUPT_LINE, 1, val);
385
386#ifdef DEBUG
387 pr_debug("PCI: %s irq=%ld\n", name, li[0]);
388 for (i = 0; i < 6; i++) {
389 celleb_config_read_fake(*config,
390 PCI_BASE_ADDRESS_0 + 0x4 * i, 4,
391 &val);
392 pr_debug("PCI: %s fn=%d base_address_%d=0x%x\n",
393 name, fn, i, val);
394 }
395#endif
396
397 celleb_config_write_fake(*config, PCI_HEADER_TYPE, 1,
398 PCI_HEADER_TYPE_NORMAL);
399
400 return 0;
401
402error:
403 if (mem_init_done) {
404 if (config && *config)
405 kfree(*config);
406 if (res && *res)
407 kfree(*res);
408
409 } else {
410 if (config && *config) {
411 size = 256;
412 free_bootmem((unsigned long)(*config), size);
413 }
414 if (res && *res) {
415 size = sizeof(struct celleb_pci_resource);
416 free_bootmem((unsigned long)(*res), size);
417 }
418 }
419
420 return 1;
421}
422
Ishizaki Koue5b91872007-07-26 19:59:17 +1000423static int __init phb_set_bus_ranges(struct device_node *dev,
424 struct pci_controller *phb)
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900425{
426 const int *bus_range;
427 unsigned int len;
428
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000429 bus_range = of_get_property(dev, "bus-range", &len);
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900430 if (bus_range == NULL || len < 2 * sizeof(int))
431 return 1;
432
433 phb->first_busno = bus_range[0];
434 phb->last_busno = bus_range[1];
435
436 return 0;
437}
438
Ishizaki Koue5b91872007-07-26 19:59:17 +1000439static void __init celleb_alloc_private_mem(struct pci_controller *hose)
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900440{
Stephen Rothwell7b2c3c52007-09-17 14:08:06 +1000441 hose->private_data =
442 alloc_maybe_bootmem(sizeof(struct celleb_pci_private),
443 GFP_KERNEL);
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900444}
445
Ishizaki Kouda0bd342007-10-02 18:26:53 +1000446static int __init celleb_setup_fake_pci(struct device_node *dev,
447 struct pci_controller *phb)
448{
449 struct device_node *node;
450
451 phb->ops = &celleb_fake_pci_ops;
452 celleb_alloc_private_mem(phb);
453
454 for (node = of_get_next_child(dev, NULL);
455 node != NULL; node = of_get_next_child(dev, node))
456 celleb_setup_fake_pci_device(node, phb);
457
458 return 0;
459}
460
Ishizaki Kou6ec859e2008-04-24 19:24:13 +1000461static struct celleb_phb_spec celleb_fake_pci_spec __initdata = {
462 .setup = celleb_setup_fake_pci,
463};
Ishizaki Kouda0bd342007-10-02 18:26:53 +1000464
465static struct of_device_id celleb_phb_match[] __initdata = {
466 {
467 .name = "pci-pseudo",
Ishizaki Kou6ec859e2008-04-24 19:24:13 +1000468 .data = &celleb_fake_pci_spec,
Ishizaki Kouda0bd342007-10-02 18:26:53 +1000469 }, {
470 .name = "epci",
Ishizaki Kou6ec859e2008-04-24 19:24:13 +1000471 .data = &celleb_epci_spec,
Ishizaki Kouda0bd342007-10-02 18:26:53 +1000472 }, {
473 },
474};
475
Ishizaki Kou6ec859e2008-04-24 19:24:13 +1000476static int __init celleb_io_workaround_init(struct pci_controller *phb,
477 struct celleb_phb_spec *phb_spec)
478{
479 if (phb_spec->ops) {
480 iowa_register_bus(phb, phb_spec->ops, phb_spec->iowa_init,
481 phb_spec->iowa_data);
482 io_workaround_init();
483 }
484
485 return 0;
486}
487
Ishizaki Koue5b91872007-07-26 19:59:17 +1000488int __init celleb_setup_phb(struct pci_controller *phb)
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900489{
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100490 struct device_node *dev = phb->dn;
Ishizaki Kouda0bd342007-10-02 18:26:53 +1000491 const struct of_device_id *match;
Ishizaki Kou6ec859e2008-04-24 19:24:13 +1000492 struct celleb_phb_spec *phb_spec;
493 int rc;
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900494
Ishizaki Kouda0bd342007-10-02 18:26:53 +1000495 match = of_match_node(celleb_phb_match, dev);
496 if (!match)
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900497 return 1;
498
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900499 phb_set_bus_ranges(dev, phb);
Ishizaki Koud1af5b42007-05-09 17:34:08 +1000500 phb->buid = 1;
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900501
Ishizaki Kou6ec859e2008-04-24 19:24:13 +1000502 phb_spec = match->data;
503 rc = (*phb_spec->setup)(dev, phb);
504 if (rc)
505 return 1;
506
507 return celleb_io_workaround_init(phb, phb_spec);
Ishizaki Kou551a3d82007-01-12 10:03:28 +0900508}
509
510int celleb_pci_probe_mode(struct pci_bus *bus)
511{
512 return PCI_PROBE_DEVTREE;
513}