sparc32: Convert PC RTC probing to pure OF driver.
[linux-2.6-block.git] / arch / sparc64 / kernel / pci.c
CommitLineData
a2fb23af 1/* pci.c: UltraSparc PCI controller support.
1da177e4
LT
2 *
3 * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
4 * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz)
a2fb23af
DM
6 *
7 * OF tree based PCI bus probing taken from the PowerPC port
8 * with minor modifications, see there for credits.
1da177e4
LT
9 */
10
1da177e4
LT
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/string.h>
14#include <linux/sched.h>
15#include <linux/capability.h>
16#include <linux/errno.h>
c57c2ffb 17#include <linux/pci.h>
35a17eb6
DM
18#include <linux/msi.h>
19#include <linux/irq.h>
1da177e4
LT
20#include <linux/init.h>
21
22#include <asm/uaccess.h>
1da177e4
LT
23#include <asm/pgtable.h>
24#include <asm/irq.h>
25#include <asm/ebus.h>
e87dc350 26#include <asm/prom.h>
01f94c4a 27#include <asm/apb.h>
1da177e4 28
1e8a8cc5
DM
29#include "pci_impl.h"
30
1da177e4 31/* List of all PCI controllers found in the system. */
34768bc8 32struct pci_pbm_info *pci_pbm_root = NULL;
1da177e4 33
6c108f12
DM
34/* Each PBM found gets a unique index. */
35int pci_num_pbms = 0;
1da177e4 36
1da177e4
LT
37volatile int pci_poke_in_progress;
38volatile int pci_poke_cpu = -1;
39volatile int pci_poke_faulted;
40
41static DEFINE_SPINLOCK(pci_poke_lock);
42
43void pci_config_read8(u8 *addr, u8 *ret)
44{
45 unsigned long flags;
46 u8 byte;
47
48 spin_lock_irqsave(&pci_poke_lock, flags);
49 pci_poke_cpu = smp_processor_id();
50 pci_poke_in_progress = 1;
51 pci_poke_faulted = 0;
52 __asm__ __volatile__("membar #Sync\n\t"
53 "lduba [%1] %2, %0\n\t"
54 "membar #Sync"
55 : "=r" (byte)
56 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
57 : "memory");
58 pci_poke_in_progress = 0;
59 pci_poke_cpu = -1;
60 if (!pci_poke_faulted)
61 *ret = byte;
62 spin_unlock_irqrestore(&pci_poke_lock, flags);
63}
64
65void pci_config_read16(u16 *addr, u16 *ret)
66{
67 unsigned long flags;
68 u16 word;
69
70 spin_lock_irqsave(&pci_poke_lock, flags);
71 pci_poke_cpu = smp_processor_id();
72 pci_poke_in_progress = 1;
73 pci_poke_faulted = 0;
74 __asm__ __volatile__("membar #Sync\n\t"
75 "lduha [%1] %2, %0\n\t"
76 "membar #Sync"
77 : "=r" (word)
78 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
79 : "memory");
80 pci_poke_in_progress = 0;
81 pci_poke_cpu = -1;
82 if (!pci_poke_faulted)
83 *ret = word;
84 spin_unlock_irqrestore(&pci_poke_lock, flags);
85}
86
87void pci_config_read32(u32 *addr, u32 *ret)
88{
89 unsigned long flags;
90 u32 dword;
91
92 spin_lock_irqsave(&pci_poke_lock, flags);
93 pci_poke_cpu = smp_processor_id();
94 pci_poke_in_progress = 1;
95 pci_poke_faulted = 0;
96 __asm__ __volatile__("membar #Sync\n\t"
97 "lduwa [%1] %2, %0\n\t"
98 "membar #Sync"
99 : "=r" (dword)
100 : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
101 : "memory");
102 pci_poke_in_progress = 0;
103 pci_poke_cpu = -1;
104 if (!pci_poke_faulted)
105 *ret = dword;
106 spin_unlock_irqrestore(&pci_poke_lock, flags);
107}
108
109void pci_config_write8(u8 *addr, u8 val)
110{
111 unsigned long flags;
112
113 spin_lock_irqsave(&pci_poke_lock, flags);
114 pci_poke_cpu = smp_processor_id();
115 pci_poke_in_progress = 1;
116 pci_poke_faulted = 0;
117 __asm__ __volatile__("membar #Sync\n\t"
118 "stba %0, [%1] %2\n\t"
119 "membar #Sync"
120 : /* no outputs */
121 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
122 : "memory");
123 pci_poke_in_progress = 0;
124 pci_poke_cpu = -1;
125 spin_unlock_irqrestore(&pci_poke_lock, flags);
126}
127
128void pci_config_write16(u16 *addr, u16 val)
129{
130 unsigned long flags;
131
132 spin_lock_irqsave(&pci_poke_lock, flags);
133 pci_poke_cpu = smp_processor_id();
134 pci_poke_in_progress = 1;
135 pci_poke_faulted = 0;
136 __asm__ __volatile__("membar #Sync\n\t"
137 "stha %0, [%1] %2\n\t"
138 "membar #Sync"
139 : /* no outputs */
140 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
141 : "memory");
142 pci_poke_in_progress = 0;
143 pci_poke_cpu = -1;
144 spin_unlock_irqrestore(&pci_poke_lock, flags);
145}
146
147void pci_config_write32(u32 *addr, u32 val)
148{
149 unsigned long flags;
150
151 spin_lock_irqsave(&pci_poke_lock, flags);
152 pci_poke_cpu = smp_processor_id();
153 pci_poke_in_progress = 1;
154 pci_poke_faulted = 0;
155 __asm__ __volatile__("membar #Sync\n\t"
156 "stwa %0, [%1] %2\n\t"
157 "membar #Sync"
158 : /* no outputs */
159 : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
160 : "memory");
161 pci_poke_in_progress = 0;
162 pci_poke_cpu = -1;
163 spin_unlock_irqrestore(&pci_poke_lock, flags);
164}
165
166/* Probe for all PCI controllers in the system. */
e87dc350
DM
167extern void sabre_init(struct device_node *, const char *);
168extern void psycho_init(struct device_node *, const char *);
169extern void schizo_init(struct device_node *, const char *);
170extern void schizo_plus_init(struct device_node *, const char *);
171extern void tomatillo_init(struct device_node *, const char *);
172extern void sun4v_pci_init(struct device_node *, const char *);
861fe906 173extern void fire_pci_init(struct device_node *, const char *);
1da177e4
LT
174
175static struct {
176 char *model_name;
e87dc350 177 void (*init)(struct device_node *, const char *);
1da177e4
LT
178} pci_controller_table[] __initdata = {
179 { "SUNW,sabre", sabre_init },
180 { "pci108e,a000", sabre_init },
181 { "pci108e,a001", sabre_init },
182 { "SUNW,psycho", psycho_init },
183 { "pci108e,8000", psycho_init },
184 { "SUNW,schizo", schizo_init },
185 { "pci108e,8001", schizo_init },
186 { "SUNW,schizo+", schizo_plus_init },
187 { "pci108e,8002", schizo_plus_init },
188 { "SUNW,tomatillo", tomatillo_init },
189 { "pci108e,a801", tomatillo_init },
8f6a93a1 190 { "SUNW,sun4v-pci", sun4v_pci_init },
861fe906 191 { "pciex108e,80f0", fire_pci_init },
1da177e4 192};
29f139ce 193#define PCI_NUM_CONTROLLER_TYPES ARRAY_SIZE(pci_controller_table)
1da177e4 194
e87dc350 195static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
1da177e4
LT
196{
197 int i;
198
199 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
200 if (!strncmp(model_name,
201 pci_controller_table[i].model_name,
202 namelen)) {
e87dc350 203 pci_controller_table[i].init(dp, model_name);
1da177e4
LT
204 return 1;
205 }
206 }
1da177e4
LT
207
208 return 0;
209}
210
e87dc350 211static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
1da177e4 212{
e87dc350 213 struct device_node *dp;
1da177e4
LT
214 int count = 0;
215
e87dc350
DM
216 for_each_node_by_name(dp, "pci") {
217 struct property *prop;
1da177e4
LT
218 int len;
219
e87dc350
DM
220 prop = of_find_property(dp, "model", &len);
221 if (!prop)
222 prop = of_find_property(dp, "compatible", &len);
223
224 if (prop) {
225 const char *model = prop->value;
1da177e4
LT
226 int item_len = 0;
227
228 /* Our value may be a multi-valued string in the
229 * case of some compatible properties. For sanity,
e87dc350
DM
230 * only try the first one.
231 */
232 while (model[item_len] && len) {
1da177e4
LT
233 len--;
234 item_len++;
235 }
236
e87dc350 237 if (handler(model, item_len, dp))
1da177e4
LT
238 count++;
239 }
1da177e4
LT
240 }
241
242 return count;
243}
244
1da177e4
LT
245/* Find each controller in the system, attach and initialize
246 * software state structure for each and link into the
34768bc8 247 * pci_pbm_root. Setup the controller enough such
1da177e4
LT
248 * that bus scanning can be done.
249 */
250static void __init pci_controller_probe(void)
251{
252 printk("PCI: Probing for controllers.\n");
253
254 pci_controller_scan(pci_controller_init);
255}
256
5840fc66
DM
257static int ofpci_verbose;
258
259static int __init ofpci_debug(char *str)
260{
261 int val = 0;
262
263 get_option(&str, &val);
264 if (val)
265 ofpci_verbose = 1;
266 return 1;
267}
268
269__setup("ofpci_debug=", ofpci_debug);
270
a2fb23af
DM
271static unsigned long pci_parse_of_flags(u32 addr0)
272{
273 unsigned long flags = 0;
274
275 if (addr0 & 0x02000000) {
276 flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
277 flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
278 flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
279 if (addr0 & 0x40000000)
280 flags |= IORESOURCE_PREFETCH
281 | PCI_BASE_ADDRESS_MEM_PREFETCH;
282 } else if (addr0 & 0x01000000)
283 flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
284 return flags;
285}
286
287/* The of_device layer has translated all of the assigned-address properties
288 * into physical address resources, we only have to figure out the register
289 * mapping.
290 */
291static void pci_parse_of_addrs(struct of_device *op,
292 struct device_node *node,
293 struct pci_dev *dev)
294{
295 struct resource *op_res;
296 const u32 *addrs;
297 int proplen;
298
299 addrs = of_get_property(node, "assigned-addresses", &proplen);
300 if (!addrs)
301 return;
5840fc66
DM
302 if (ofpci_verbose)
303 printk(" parse addresses (%d bytes) @ %p\n",
304 proplen, addrs);
a2fb23af
DM
305 op_res = &op->resource[0];
306 for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
307 struct resource *res;
308 unsigned long flags;
309 int i;
310
311 flags = pci_parse_of_flags(addrs[0]);
312 if (!flags)
313 continue;
314 i = addrs[0] & 0xff;
5840fc66
DM
315 if (ofpci_verbose)
316 printk(" start: %lx, end: %lx, i: %x\n",
317 op_res->start, op_res->end, i);
a2fb23af
DM
318
319 if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
320 res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
321 } else if (i == dev->rom_base_reg) {
322 res = &dev->resource[PCI_ROM_RESOURCE];
323 flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
324 } else {
325 printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
326 continue;
327 }
328 res->start = op_res->start;
329 res->end = op_res->end;
330 res->flags = flags;
331 res->name = pci_name(dev);
332 }
333}
334
335struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
336 struct device_node *node,
c26d3c01 337 struct pci_bus *bus, int devfn)
a2fb23af
DM
338{
339 struct dev_archdata *sd;
ae05f87e 340 struct of_device *op;
a2fb23af
DM
341 struct pci_dev *dev;
342 const char *type;
01f94c4a 343 u32 class;
a2fb23af 344
26e6385f 345 dev = alloc_pci_dev();
a2fb23af
DM
346 if (!dev)
347 return NULL;
348
349 sd = &dev->dev.archdata;
350 sd->iommu = pbm->iommu;
351 sd->stc = &pbm->stc;
352 sd->host_controller = pbm;
353 sd->prom_node = node;
ae05f87e 354 sd->op = op = of_find_device_by_node(node);
c1b1a5f1 355 sd->numa_node = pbm->numa_node;
a2fb23af 356
ae05f87e 357 sd = &op->dev.archdata;
ad7ad57c
DM
358 sd->iommu = pbm->iommu;
359 sd->stc = &pbm->stc;
c1b1a5f1 360 sd->numa_node = pbm->numa_node;
ad7ad57c 361
ae05f87e
DM
362 if (!strcmp(node->name, "ebus"))
363 of_propagate_archdata(op);
364
a2fb23af
DM
365 type = of_get_property(node, "device_type", NULL);
366 if (type == NULL)
367 type = "";
368
5840fc66
DM
369 if (ofpci_verbose)
370 printk(" create device, devfn: %x, type: %s\n",
371 devfn, type);
a2fb23af
DM
372
373 dev->bus = bus;
374 dev->sysdata = node;
375 dev->dev.parent = bus->bridge;
376 dev->dev.bus = &pci_bus_type;
377 dev->devfn = devfn;
378 dev->multifunction = 0; /* maybe a lie? */
379
c26d3c01
DM
380 dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
381 dev->device = of_getintprop_default(node, "device-id", 0xffff);
382 dev->subsystem_vendor =
383 of_getintprop_default(node, "subsystem-vendor-id", 0);
384 dev->subsystem_device =
385 of_getintprop_default(node, "subsystem-id", 0);
386
387 dev->cfg_size = pci_cfg_space_size(dev);
388
389 /* We can't actually use the firmware value, we have
390 * to read what is in the register right now. One
391 * reason is that in the case of IDE interfaces the
392 * firmware can sample the value before the the IDE
393 * interface is programmed into native mode.
394 */
395 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
396 dev->class = class >> 8;
397 dev->revision = class & 0xff;
398
2222c313 399 dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
c26d3c01 400 dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
28f57e77 401
5840fc66
DM
402 if (ofpci_verbose)
403 printk(" class: 0x%x device name: %s\n",
404 dev->class, pci_name(dev));
a2fb23af 405
861fe906
DM
406 /* I have seen IDE devices which will not respond to
407 * the bmdma simplex check reads if bus mastering is
408 * disabled.
409 */
410 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
411 pci_set_master(dev);
412
a2fb23af
DM
413 dev->current_state = 4; /* unknown power state */
414 dev->error_state = pci_channel_io_normal;
415
c26d3c01
DM
416 if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
417 /* a PCI-PCI bridge */
a2fb23af
DM
418 dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
419 dev->rom_base_reg = PCI_ROM_ADDRESS1;
c26d3c01
DM
420 } else if (!strcmp(type, "cardbus")) {
421 dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
a2fb23af 422 } else {
c26d3c01
DM
423 dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
424 dev->rom_base_reg = PCI_ROM_ADDRESS;
a2fb23af 425
c26d3c01
DM
426 dev->irq = sd->op->irqs[0];
427 if (dev->irq == 0xffffffff)
428 dev->irq = PCI_IRQ_NONE;
a2fb23af 429 }
c26d3c01 430
a2fb23af
DM
431 pci_parse_of_addrs(sd->op, node, dev);
432
5840fc66
DM
433 if (ofpci_verbose)
434 printk(" adding to system ...\n");
a2fb23af
DM
435
436 pci_device_add(dev, bus);
437
438 return dev;
439}
440
a6009dda 441static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
01f94c4a
DM
442{
443 u32 idx, first, last;
444
445 first = 8;
446 last = 0;
447 for (idx = 0; idx < 8; idx++) {
448 if ((map & (1 << idx)) != 0) {
449 if (first > idx)
450 first = idx;
451 if (last < idx)
452 last = idx;
453 }
454 }
455
456 *first_p = first;
457 *last_p = last;
458}
459
f16537ba
DM
460static void pci_resource_adjust(struct resource *res,
461 struct resource *root)
0bae5f81
DM
462{
463 res->start += root->start;
464 res->end += root->start;
465}
466
8c2786cf
DM
467/* For PCI bus devices which lack a 'ranges' property we interrogate
468 * the config space values to set the resources, just like the generic
469 * Linux PCI probing code does.
470 */
471static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev,
472 struct pci_bus *bus,
473 struct pci_pbm_info *pbm)
474{
475 struct resource *res;
476 u8 io_base_lo, io_limit_lo;
477 u16 mem_base_lo, mem_limit_lo;
478 unsigned long base, limit;
479
480 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
481 pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
482 base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
483 limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
484
485 if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
486 u16 io_base_hi, io_limit_hi;
487
488 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
489 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
490 base |= (io_base_hi << 16);
491 limit |= (io_limit_hi << 16);
492 }
493
494 res = bus->resource[0];
495 if (base <= limit) {
496 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
497 if (!res->start)
498 res->start = base;
499 if (!res->end)
500 res->end = limit + 0xfff;
501 pci_resource_adjust(res, &pbm->io_space);
502 }
503
504 pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
505 pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
506 base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
507 limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
508
509 res = bus->resource[1];
510 if (base <= limit) {
511 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
512 IORESOURCE_MEM);
513 res->start = base;
514 res->end = limit + 0xfffff;
515 pci_resource_adjust(res, &pbm->mem_space);
516 }
517
518 pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
519 pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
520 base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
521 limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
522
523 if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
524 u32 mem_base_hi, mem_limit_hi;
525
526 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
527 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
528
529 /*
530 * Some bridges set the base > limit by default, and some
531 * (broken) BIOSes do not initialize them. If we find
532 * this, just assume they are not being used.
533 */
534 if (mem_base_hi <= mem_limit_hi) {
535 base |= ((long) mem_base_hi) << 32;
536 limit |= ((long) mem_limit_hi) << 32;
537 }
538 }
539
540 res = bus->resource[2];
541 if (base <= limit) {
542 res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
543 IORESOURCE_MEM | IORESOURCE_PREFETCH);
544 res->start = base;
545 res->end = limit + 0xfffff;
546 pci_resource_adjust(res, &pbm->mem_space);
547 }
548}
549
01f94c4a
DM
550/* Cook up fake bus resources for SUNW,simba PCI bridges which lack
551 * a proper 'ranges' property.
552 */
a6009dda
DM
553static void __devinit apb_fake_ranges(struct pci_dev *dev,
554 struct pci_bus *bus,
555 struct pci_pbm_info *pbm)
01f94c4a
DM
556{
557 struct resource *res;
558 u32 first, last;
559 u8 map;
560
561 pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
562 apb_calc_first_last(map, &first, &last);
563 res = bus->resource[0];
564 res->start = (first << 21);
565 res->end = (last << 21) + ((1 << 21) - 1);
566 res->flags = IORESOURCE_IO;
0bae5f81 567 pci_resource_adjust(res, &pbm->io_space);
01f94c4a
DM
568
569 pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
570 apb_calc_first_last(map, &first, &last);
571 res = bus->resource[1];
572 res->start = (first << 21);
573 res->end = (last << 21) + ((1 << 21) - 1);
574 res->flags = IORESOURCE_MEM;
0bae5f81 575 pci_resource_adjust(res, &pbm->mem_space);
01f94c4a
DM
576}
577
a6009dda
DM
578static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
579 struct device_node *node,
580 struct pci_bus *bus);
a2fb23af
DM
581
582#define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
583
a6009dda
DM
584static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
585 struct device_node *node,
586 struct pci_dev *dev)
a2fb23af
DM
587{
588 struct pci_bus *bus;
589 const u32 *busrange, *ranges;
01f94c4a 590 int len, i, simba;
a2fb23af
DM
591 struct resource *res;
592 unsigned int flags;
593 u64 size;
594
5840fc66
DM
595 if (ofpci_verbose)
596 printk("of_scan_pci_bridge(%s)\n", node->full_name);
a2fb23af
DM
597
598 /* parse bus-range property */
599 busrange = of_get_property(node, "bus-range", &len);
600 if (busrange == NULL || len != 8) {
601 printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
602 node->full_name);
603 return;
604 }
605 ranges = of_get_property(node, "ranges", &len);
01f94c4a 606 simba = 0;
a2fb23af 607 if (ranges == NULL) {
a165b420 608 const char *model = of_get_property(node, "model", NULL);
8c2786cf 609 if (model && !strcmp(model, "SUNW,simba"))
01f94c4a 610 simba = 1;
a2fb23af
DM
611 }
612
613 bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
614 if (!bus) {
615 printk(KERN_ERR "Failed to create pci bus for %s\n",
616 node->full_name);
617 return;
618 }
619
620 bus->primary = dev->bus->number;
621 bus->subordinate = busrange[1];
622 bus->bridge_ctl = 0;
623
01f94c4a 624 /* parse ranges property, or cook one up by hand for Simba */
a2fb23af
DM
625 /* PCI #address-cells == 3 and #size-cells == 2 always */
626 res = &dev->resource[PCI_BRIDGE_RESOURCES];
627 for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
628 res->flags = 0;
629 bus->resource[i] = res;
630 ++res;
631 }
01f94c4a
DM
632 if (simba) {
633 apb_fake_ranges(dev, bus, pbm);
8c2786cf
DM
634 goto after_ranges;
635 } else if (ranges == NULL) {
636 pci_cfg_fake_ranges(dev, bus, pbm);
637 goto after_ranges;
01f94c4a 638 }
a2fb23af
DM
639 i = 1;
640 for (; len >= 32; len -= 32, ranges += 8) {
641 struct resource *root;
642
643 flags = pci_parse_of_flags(ranges[0]);
644 size = GET_64BIT(ranges, 6);
645 if (flags == 0 || size == 0)
646 continue;
647 if (flags & IORESOURCE_IO) {
648 res = bus->resource[0];
649 if (res->flags) {
650 printk(KERN_ERR "PCI: ignoring extra I/O range"
651 " for bridge %s\n", node->full_name);
652 continue;
653 }
654 root = &pbm->io_space;
655 } else {
656 if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
657 printk(KERN_ERR "PCI: too many memory ranges"
658 " for bridge %s\n", node->full_name);
659 continue;
660 }
661 res = bus->resource[i];
662 ++i;
663 root = &pbm->mem_space;
664 }
665
666 res->start = GET_64BIT(ranges, 1);
667 res->end = res->start + size - 1;
668 res->flags = flags;
669
670 /* Another way to implement this would be to add an of_device
671 * layer routine that can calculate a resource for a given
672 * range property value in a PCI device.
673 */
0bae5f81 674 pci_resource_adjust(res, root);
a2fb23af 675 }
8c2786cf 676after_ranges:
a2fb23af
DM
677 sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
678 bus->number);
5840fc66
DM
679 if (ofpci_verbose)
680 printk(" bus name: %s\n", bus->name);
a2fb23af
DM
681
682 pci_of_scan_bus(pbm, node, bus);
683}
684
a6009dda
DM
685static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
686 struct device_node *node,
687 struct pci_bus *bus)
a2fb23af
DM
688{
689 struct device_node *child;
690 const u32 *reg;
2cc7345f 691 int reglen, devfn, prev_devfn;
a2fb23af
DM
692 struct pci_dev *dev;
693
5840fc66
DM
694 if (ofpci_verbose)
695 printk("PCI: scan_bus[%s] bus no %d\n",
696 node->full_name, bus->number);
a2fb23af
DM
697
698 child = NULL;
2cc7345f 699 prev_devfn = -1;
a2fb23af 700 while ((child = of_get_next_child(node, child)) != NULL) {
5840fc66
DM
701 if (ofpci_verbose)
702 printk(" * %s\n", child->full_name);
a2fb23af
DM
703 reg = of_get_property(child, "reg", &reglen);
704 if (reg == NULL || reglen < 20)
705 continue;
2cc7345f 706
a2fb23af
DM
707 devfn = (reg[0] >> 8) & 0xff;
708
2cc7345f
DM
709 /* This is a workaround for some device trees
710 * which list PCI devices twice. On the V100
711 * for example, device number 3 is listed twice.
712 * Once as "pm" and once again as "lomp".
713 */
714 if (devfn == prev_devfn)
715 continue;
716 prev_devfn = devfn;
717
a2fb23af 718 /* create a new pci_dev for this device */
c26d3c01 719 dev = of_create_pci_dev(pbm, child, bus, devfn);
a2fb23af
DM
720 if (!dev)
721 continue;
5840fc66
DM
722 if (ofpci_verbose)
723 printk("PCI: dev header type: %x\n",
724 dev->hdr_type);
a2fb23af
DM
725
726 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
727 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
728 of_scan_pci_bridge(pbm, child, dev);
729 }
730}
731
732static ssize_t
733show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
734{
735 struct pci_dev *pdev;
736 struct device_node *dp;
737
738 pdev = to_pci_dev(dev);
739 dp = pdev->dev.archdata.prom_node;
740
741 return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
742}
743
744static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
745
746static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
747{
748 struct pci_dev *dev;
a378fd0e 749 struct pci_bus *child_bus;
a2fb23af
DM
750 int err;
751
752 list_for_each_entry(dev, &bus->devices, bus_list) {
753 /* we don't really care if we can create this file or
754 * not, but we need to assign the result of the call
755 * or the world will fall under alien invasion and
756 * everybody will be frozen on a spaceship ready to be
757 * eaten on alpha centauri by some green and jelly
758 * humanoid.
759 */
760 err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
761 }
a378fd0e
DM
762 list_for_each_entry(child_bus, &bus->children, node)
763 pci_bus_register_of_sysfs(child_bus);
a2fb23af
DM
764}
765
a6009dda 766struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm)
a2fb23af 767{
a2fb23af
DM
768 struct device_node *node = pbm->prom_node;
769 struct pci_bus *bus;
770
771 printk("PCI: Scanning PBM %s\n", node->full_name);
772
773 /* XXX parent device? XXX */
f1cd8de2 774 bus = pci_create_bus(NULL, pbm->pci_first_busno, pbm->pci_ops, pbm);
a2fb23af
DM
775 if (!bus) {
776 printk(KERN_ERR "Failed to create bus for %s\n",
777 node->full_name);
778 return NULL;
779 }
780 bus->secondary = pbm->pci_first_busno;
781 bus->subordinate = pbm->pci_last_busno;
782
783 bus->resource[0] = &pbm->io_space;
784 bus->resource[1] = &pbm->mem_space;
785
786 pci_of_scan_bus(pbm, node, bus);
787 pci_bus_add_devices(bus);
788 pci_bus_register_of_sysfs(bus);
789
790 return bus;
791}
792
1da177e4
LT
793static void __init pci_scan_each_controller_bus(void)
794{
34768bc8 795 struct pci_pbm_info *pbm;
1da177e4 796
34768bc8
DM
797 for (pbm = pci_pbm_root; pbm; pbm = pbm->next)
798 pbm->scan_bus(pbm);
1da177e4
LT
799}
800
1da177e4
LT
801extern void power_init(void);
802
803static int __init pcibios_init(void)
804{
805 pci_controller_probe();
34768bc8 806 if (pci_pbm_root == NULL)
1da177e4
LT
807 return 0;
808
809 pci_scan_each_controller_bus();
810
1da177e4 811 ebus_init();
1da177e4
LT
812 power_init();
813
814 return 0;
815}
816
817subsys_initcall(pcibios_init);
818
f6b45da1 819void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
1da177e4
LT
820{
821 struct pci_pbm_info *pbm = pbus->sysdata;
822
823 /* Generic PCI bus probing sets these to point at
824 * &io{port,mem}_resouce which is wrong for us.
825 */
826 pbus->resource[0] = &pbm->io_space;
827 pbus->resource[1] = &pbm->mem_space;
828}
829
085ae41f 830struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
1da177e4
LT
831{
832 struct pci_pbm_info *pbm = pdev->bus->sysdata;
085ae41f 833 struct resource *root = NULL;
1da177e4 834
085ae41f 835 if (r->flags & IORESOURCE_IO)
1da177e4 836 root = &pbm->io_space;
085ae41f 837 if (r->flags & IORESOURCE_MEM)
1da177e4
LT
838 root = &pbm->mem_space;
839
085ae41f 840 return root;
1da177e4
LT
841}
842
843void pcibios_update_irq(struct pci_dev *pdev, int irq)
844{
845}
846
847void pcibios_align_resource(void *data, struct resource *res,
e31dd6e4 848 resource_size_t size, resource_size_t align)
1da177e4
LT
849{
850}
851
a2fb23af 852int pcibios_enable_device(struct pci_dev *dev, int mask)
1da177e4 853{
a2fb23af
DM
854 u16 cmd, oldcmd;
855 int i;
856
857 pci_read_config_word(dev, PCI_COMMAND, &cmd);
858 oldcmd = cmd;
859
860 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
861 struct resource *res = &dev->resource[i];
862
863 /* Only set up the requested stuff */
864 if (!(mask & (1<<i)))
865 continue;
866
867 if (res->flags & IORESOURCE_IO)
868 cmd |= PCI_COMMAND_IO;
869 if (res->flags & IORESOURCE_MEM)
870 cmd |= PCI_COMMAND_MEMORY;
871 }
872
873 if (cmd != oldcmd) {
874 printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
875 pci_name(dev), cmd);
876 /* Enable the appropriate bits in the PCI command register. */
877 pci_write_config_word(dev, PCI_COMMAND, cmd);
878 }
1da177e4
LT
879 return 0;
880}
881
882void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
883 struct resource *res)
884{
885 struct pci_pbm_info *pbm = pdev->bus->sysdata;
886 struct resource zero_res, *root;
887
888 zero_res.start = 0;
889 zero_res.end = 0;
890 zero_res.flags = res->flags;
891
892 if (res->flags & IORESOURCE_IO)
893 root = &pbm->io_space;
894 else
895 root = &pbm->mem_space;
896
0bae5f81 897 pci_resource_adjust(&zero_res, root);
1da177e4
LT
898
899 region->start = res->start - zero_res.start;
900 region->end = res->end - zero_res.start;
901}
5fdfd42e 902EXPORT_SYMBOL(pcibios_resource_to_bus);
1da177e4
LT
903
904void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
905 struct pci_bus_region *region)
906{
907 struct pci_pbm_info *pbm = pdev->bus->sysdata;
908 struct resource *root;
909
910 res->start = region->start;
911 res->end = region->end;
912
913 if (res->flags & IORESOURCE_IO)
914 root = &pbm->io_space;
915 else
916 root = &pbm->mem_space;
917
0bae5f81 918 pci_resource_adjust(res, root);
1da177e4 919}
41290c14 920EXPORT_SYMBOL(pcibios_bus_to_resource);
1da177e4 921
f6b45da1 922char * __devinit pcibios_setup(char *str)
1da177e4 923{
1da177e4
LT
924 return str;
925}
926
927/* Platform support for /proc/bus/pci/X/Y mmap()s. */
928
929/* If the user uses a host-bridge as the PCI device, he may use
930 * this to perform a raw mmap() of the I/O or MEM space behind
931 * that controller.
932 *
933 * This can be useful for execution of x86 PCI bios initialization code
934 * on a PCI card, like the xfree86 int10 stuff does.
935 */
936static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
937 enum pci_mmap_state mmap_state)
938{
a2fb23af 939 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1da177e4
LT
940 unsigned long space_size, user_offset, user_size;
941
3875c5c0
DM
942 if (mmap_state == pci_mmap_io) {
943 space_size = (pbm->io_space.end -
944 pbm->io_space.start) + 1;
1da177e4 945 } else {
3875c5c0
DM
946 space_size = (pbm->mem_space.end -
947 pbm->mem_space.start) + 1;
1da177e4
LT
948 }
949
950 /* Make sure the request is in range. */
951 user_offset = vma->vm_pgoff << PAGE_SHIFT;
952 user_size = vma->vm_end - vma->vm_start;
953
954 if (user_offset >= space_size ||
955 (user_offset + user_size) > space_size)
956 return -EINVAL;
957
3875c5c0
DM
958 if (mmap_state == pci_mmap_io) {
959 vma->vm_pgoff = (pbm->io_space.start +
960 user_offset) >> PAGE_SHIFT;
1da177e4 961 } else {
3875c5c0
DM
962 vma->vm_pgoff = (pbm->mem_space.start +
963 user_offset) >> PAGE_SHIFT;
1da177e4
LT
964 }
965
966 return 0;
967}
968
bbe0b5eb
DM
969/* Adjust vm_pgoff of VMA such that it is the physical page offset
970 * corresponding to the 32-bit pci bus offset for DEV requested by the user.
1da177e4
LT
971 *
972 * Basically, the user finds the base address for his device which he wishes
973 * to mmap. They read the 32-bit value from the config space base register,
974 * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
975 * offset parameter of mmap on /proc/bus/pci/XXX for that device.
976 *
977 * Returns negative error code on failure, zero on success.
978 */
bbe0b5eb
DM
979static int __pci_mmap_make_offset(struct pci_dev *pdev,
980 struct vm_area_struct *vma,
1da177e4
LT
981 enum pci_mmap_state mmap_state)
982{
bbe0b5eb
DM
983 unsigned long user_paddr, user_size;
984 int i, err;
1da177e4 985
bbe0b5eb
DM
986 /* First compute the physical address in vma->vm_pgoff,
987 * making sure the user offset is within range in the
988 * appropriate PCI space.
989 */
990 err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
991 if (err)
992 return err;
993
994 /* If this is a mapping on a host bridge, any address
995 * is OK.
996 */
997 if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
998 return err;
999
1000 /* Otherwise make sure it's in the range for one of the
1001 * device's resources.
1002 */
1003 user_paddr = vma->vm_pgoff << PAGE_SHIFT;
1004 user_size = vma->vm_end - vma->vm_start;
1da177e4 1005
1da177e4 1006 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
bbe0b5eb 1007 struct resource *rp = &pdev->resource[i];
1da177e4
LT
1008
1009 /* Active? */
1010 if (!rp->flags)
1011 continue;
1012
1013 /* Same type? */
1014 if (i == PCI_ROM_RESOURCE) {
1015 if (mmap_state != pci_mmap_mem)
1016 continue;
1017 } else {
1018 if ((mmap_state == pci_mmap_io &&
1019 (rp->flags & IORESOURCE_IO) == 0) ||
1020 (mmap_state == pci_mmap_mem &&
1021 (rp->flags & IORESOURCE_MEM) == 0))
1022 continue;
1023 }
1024
bbe0b5eb
DM
1025 if ((rp->start <= user_paddr) &&
1026 (user_paddr + user_size) <= (rp->end + 1UL))
1027 break;
1da177e4
LT
1028 }
1029
bbe0b5eb 1030 if (i > PCI_ROM_RESOURCE)
1da177e4
LT
1031 return -EINVAL;
1032
1da177e4
LT
1033 return 0;
1034}
1035
1036/* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
1037 * mapping.
1038 */
1039static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
1040 enum pci_mmap_state mmap_state)
1041{
1042 vma->vm_flags |= (VM_IO | VM_RESERVED);
1043}
1044
1045/* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
1046 * device mapping.
1047 */
1048static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
1049 enum pci_mmap_state mmap_state)
1050{
a7a6cac2 1051 /* Our io_remap_pfn_range takes care of this, do nothing. */
1da177e4
LT
1052}
1053
1054/* Perform the actual remap of the pages for a PCI device mapping, as appropriate
1055 * for this architecture. The region in the process to map is described by vm_start
1056 * and vm_end members of VMA, the base physical address is found in vm_pgoff.
1057 * The pci device structure is provided so that architectures may make mapping
1058 * decisions on a per-device or per-bus basis.
1059 *
1060 * Returns a negative error code on failure, zero on success.
1061 */
1062int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
1063 enum pci_mmap_state mmap_state,
1064 int write_combine)
1065{
1066 int ret;
1067
1068 ret = __pci_mmap_make_offset(dev, vma, mmap_state);
1069 if (ret < 0)
1070 return ret;
1071
1072 __pci_mmap_set_flags(dev, vma, mmap_state);
1073 __pci_mmap_set_pgprot(dev, vma, mmap_state);
1074
14778d90 1075 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1da177e4
LT
1076 ret = io_remap_pfn_range(vma, vma->vm_start,
1077 vma->vm_pgoff,
1078 vma->vm_end - vma->vm_start,
1079 vma->vm_page_prot);
1080 if (ret)
1081 return ret;
1082
1da177e4
LT
1083 return 0;
1084}
1085
c1b1a5f1
DM
1086#ifdef CONFIG_NUMA
1087int pcibus_to_node(struct pci_bus *pbus)
1088{
1089 struct pci_pbm_info *pbm = pbus->sysdata;
1090
1091 return pbm->numa_node;
1092}
1093EXPORT_SYMBOL(pcibus_to_node);
1094#endif
1095
1da177e4
LT
1096/* Return the domain nuber for this pci bus */
1097
1098int pci_domain_nr(struct pci_bus *pbus)
1099{
1100 struct pci_pbm_info *pbm = pbus->sysdata;
1101 int ret;
1102
1103 if (pbm == NULL || pbm->parent == NULL) {
1104 ret = -ENXIO;
1105 } else {
6c108f12 1106 ret = pbm->index;
1da177e4
LT
1107 }
1108
1109 return ret;
1110}
1111EXPORT_SYMBOL(pci_domain_nr);
1112
35a17eb6
DM
1113#ifdef CONFIG_PCI_MSI
1114int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
1115{
a2fb23af 1116 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
e9870c4c 1117 int virt_irq;
35a17eb6 1118
e9870c4c 1119 if (!pbm->setup_msi_irq)
35a17eb6
DM
1120 return -EINVAL;
1121
e9870c4c 1122 return pbm->setup_msi_irq(&virt_irq, pdev, desc);
35a17eb6
DM
1123}
1124
1125void arch_teardown_msi_irq(unsigned int virt_irq)
1126{
abfd336c 1127 struct msi_desc *entry = get_irq_msi(virt_irq);
35a17eb6 1128 struct pci_dev *pdev = entry->dev;
a2fb23af 1129 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
35a17eb6 1130
e9870c4c 1131 if (!pbm->teardown_msi_irq)
35a17eb6
DM
1132 return;
1133
e9870c4c 1134 return pbm->teardown_msi_irq(virt_irq, pdev);
35a17eb6
DM
1135}
1136#endif /* !(CONFIG_PCI_MSI) */
1137
f6d0f9ea
DM
1138struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
1139{
a2fb23af 1140 return pdev->dev.archdata.prom_node;
f6d0f9ea
DM
1141}
1142EXPORT_SYMBOL(pci_device_to_OF_node);
1143
ad7ad57c
DM
1144static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
1145{
1146 struct pci_dev *ali_isa_bridge;
1147 u8 val;
1148
1149 /* ALI sound chips generate 31-bits of DMA, a special register
1150 * determines what bit 31 is emitted as.
1151 */
1152 ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
1153 PCI_DEVICE_ID_AL_M1533,
1154 NULL);
1155
1156 pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
1157 if (set_bit)
1158 val |= 0x01;
1159 else
1160 val &= ~0x01;
1161 pci_write_config_byte(ali_isa_bridge, 0x7e, val);
1162 pci_dev_put(ali_isa_bridge);
1163}
1164
1165int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
1166{
1167 u64 dma_addr_mask;
1168
1169 if (pdev == NULL) {
1170 dma_addr_mask = 0xffffffff;
1171 } else {
1172 struct iommu *iommu = pdev->dev.archdata.iommu;
1173
1174 dma_addr_mask = iommu->dma_addr_mask;
1175
1176 if (pdev->vendor == PCI_VENDOR_ID_AL &&
1177 pdev->device == PCI_DEVICE_ID_AL_M5451 &&
1178 device_mask == 0x7fffffff) {
1179 ali_sound_dma_hack(pdev,
1180 (dma_addr_mask & 0x80000000) != 0);
1181 return 1;
1182 }
1183 }
1184
1185 if (device_mask >= (1UL << 32UL))
1186 return 0;
1187
1188 return (device_mask & dma_addr_mask) == dma_addr_mask;
1189}
1190
bcea1db1
DM
1191void pci_resource_to_user(const struct pci_dev *pdev, int bar,
1192 const struct resource *rp, resource_size_t *start,
1193 resource_size_t *end)
1194{
1195 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
1196 unsigned long offset;
1197
1198 if (rp->flags & IORESOURCE_IO)
1199 offset = pbm->io_space.start;
1200 else
1201 offset = pbm->mem_space.start;
1202
1203 *start = rp->start - offset;
1204 *end = rp->end - offset;
1205}