1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/kernel.h>
17#include <linux/pci.h>
18#include <linux/delay.h>
19#include <linux/string.h>
20#include <linux/init.h>
21#include <linux/bootmem.h>
22
23#include <asm/sections.h>
24#include <asm/io.h>
25#include <asm/prom.h>
26#include <asm/pci-bridge.h>
27#include <asm/machdep.h>
28#include <asm/pmac_feature.h>
29#include <asm/iommu.h>
30
31#include "pci.h"
32#include "pmac.h"
33
34#define DEBUG
35
36#ifdef DEBUG
37#define DBG(x...) printk(x)
38#else
39#define DBG(x...)
40#endif
41
42extern int pci_probe_only;
43extern int pci_read_irq_line(struct pci_dev *pci_dev);
44
45
46
47static int has_uninorth;
48static struct pci_controller *u3_agp;
49u8 pci_cache_line_size;
50struct pci_dev *k2_skiplist[2];
51
52static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
53{
54 for (; node != 0;node = node->sibling) {
55 int * bus_range;
56 unsigned int *class_code;
57 int len;
58
59
60 class_code = (unsigned int *) get_property(node, "class-code", NULL);
61 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
62 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
63 continue;
64 bus_range = (int *) get_property(node, "bus-range", &len);
65 if (bus_range != NULL && len > 2 * sizeof(int)) {
66 if (bus_range[1] > higher)
67 higher = bus_range[1];
68 }
69 higher = fixup_one_level_bus_range(node->child, higher);
70 }
71 return higher;
72}
73
74
75
76
77
78
79
80static void __init fixup_bus_range(struct device_node *bridge)
81{
82 int * bus_range;
83 int len;
84
85
86 bus_range = (int *) get_property(bridge, "bus-range", &len);
87 if (bus_range == NULL || len < 2 * sizeof(int)) {
88 printk(KERN_WARNING "Can't get bus-range for %s\n",
89 bridge->full_name);
90 return;
91 }
92 bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
93}
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117#define MACRISC_CFA0(devfn, off) \
118 ((1 << (unsigned long)PCI_SLOT(dev_fn)) \
119 | (((unsigned long)PCI_FUNC(dev_fn)) << 8) \
120 | (((unsigned long)(off)) & 0xFCUL))
121
122#define MACRISC_CFA1(bus, devfn, off) \
123 ((((unsigned long)(bus)) << 16) \
124 |(((unsigned long)(devfn)) << 8) \
125 |(((unsigned long)(off)) & 0xFCUL) \
126 |1UL)
127
128static unsigned long __pmac macrisc_cfg_access(struct pci_controller* hose,
129 u8 bus, u8 dev_fn, u8 offset)
130{
131 unsigned int caddr;
132
133 if (bus == hose->first_busno) {
134 if (dev_fn < (11 << 3))
135 return 0;
136 caddr = MACRISC_CFA0(dev_fn, offset);
137 } else
138 caddr = MACRISC_CFA1(bus, dev_fn, offset);
139
140
141 do {
142 out_le32(hose->cfg_addr, caddr);
143 } while (in_le32(hose->cfg_addr) != caddr);
144
145 offset &= has_uninorth ? 0x07 : 0x03;
146 return ((unsigned long)hose->cfg_data) + offset;
147}
148
149static int __pmac macrisc_read_config(struct pci_bus *bus, unsigned int devfn,
150 int offset, int len, u32 *val)
151{
152 struct pci_controller *hose;
153 struct device_node *busdn;
154 unsigned long addr;
155
156 if (bus->self)
157 busdn = pci_device_to_OF_node(bus->self);
158 else
159 busdn = bus->sysdata;
160 if (busdn == NULL)
161 return PCIBIOS_DEVICE_NOT_FOUND;
162 hose = busdn->phb;
163 if (hose == NULL)
164 return PCIBIOS_DEVICE_NOT_FOUND;
165
166 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
167 if (!addr)
168 return PCIBIOS_DEVICE_NOT_FOUND;
169
170
171
172
173 switch (len) {
174 case 1:
175 *val = in_8((u8 *)addr);
176 break;
177 case 2:
178 *val = in_le16((u16 *)addr);
179 break;
180 default:
181 *val = in_le32((u32 *)addr);
182 break;
183 }
184 return PCIBIOS_SUCCESSFUL;
185}
186
187static int __pmac macrisc_write_config(struct pci_bus *bus, unsigned int devfn,
188 int offset, int len, u32 val)
189{
190 struct pci_controller *hose;
191 struct device_node *busdn;
192 unsigned long addr;
193
194 if (bus->self)
195 busdn = pci_device_to_OF_node(bus->self);
196 else
197 busdn = bus->sysdata;
198 if (busdn == NULL)
199 return PCIBIOS_DEVICE_NOT_FOUND;
200 hose = busdn->phb;
201 if (hose == NULL)
202 return PCIBIOS_DEVICE_NOT_FOUND;
203
204 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
205 if (!addr)
206 return PCIBIOS_DEVICE_NOT_FOUND;
207
208
209
210
211 switch (len) {
212 case 1:
213 out_8((u8 *)addr, val);
214 (void) in_8((u8 *)addr);
215 break;
216 case 2:
217 out_le16((u16 *)addr, val);
218 (void) in_le16((u16 *)addr);
219 break;
220 default:
221 out_le32((u32 *)addr, val);
222 (void) in_le32((u32 *)addr);
223 break;
224 }
225 return PCIBIOS_SUCCESSFUL;
226}
227
228static struct pci_ops macrisc_pci_ops =
229{
230 macrisc_read_config,
231 macrisc_write_config
232};
233
234
235
236
237
238
239static int skip_k2_device(struct pci_bus *bus, unsigned int devfn)
240{
241 int i;
242
243 for (i=0; i<2; i++)
244 if (k2_skiplist[i] && k2_skiplist[i]->bus == bus &&
245 k2_skiplist[i]->devfn == devfn)
246 return 1;
247 return 0;
248}
249
250#define U3_HT_CFA0(devfn, off) \
251 ((((unsigned long)devfn) << 8) | offset)
252#define U3_HT_CFA1(bus, devfn, off) \
253 (U3_HT_CFA0(devfn, off) \
254 + (((unsigned long)bus) << 16) \
255 + 0x01000000UL)
256
257static unsigned long __pmac u3_ht_cfg_access(struct pci_controller* hose,
258 u8 bus, u8 devfn, u8 offset)
259{
260 if (bus == hose->first_busno) {
261
262 if (PCI_FUNC(devfn) != 0 || PCI_SLOT(devfn) > 7 ||
263 PCI_SLOT(devfn) < 1)
264 return 0;
265 return ((unsigned long)hose->cfg_data) + U3_HT_CFA0(devfn, offset);
266 } else
267 return ((unsigned long)hose->cfg_data) + U3_HT_CFA1(bus, devfn, offset);
268}
269
270static int __pmac u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
271 int offset, int len, u32 *val)
272{
273 struct pci_controller *hose;
274 struct device_node *busdn;
275 unsigned long addr;
276
277 if (bus->self)
278 busdn = pci_device_to_OF_node(bus->self);
279 else
280 busdn = bus->sysdata;
281 if (busdn == NULL)
282 return PCIBIOS_DEVICE_NOT_FOUND;
283 hose = busdn->phb;
284 if (hose == NULL)
285 return PCIBIOS_DEVICE_NOT_FOUND;
286
287 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
288 if (!addr)
289 return PCIBIOS_DEVICE_NOT_FOUND;
290
291
292
293
294
295
296 if (skip_k2_device(bus, devfn)) {
297 switch (len) {
298 case 1:
299 *val = 0xff; break;
300 case 2:
301 *val = 0xffff; break;
302 default:
303 *val = 0xfffffffful; break;
304 }
305 return PCIBIOS_SUCCESSFUL;
306 }
307
308
309
310
311
312 switch (len) {
313 case 1:
314 *val = in_8((u8 *)addr);
315 break;
316 case 2:
317 *val = in_le16((u16 *)addr);
318 break;
319 default:
320 *val = in_le32((u32 *)addr);
321 break;
322 }
323 return PCIBIOS_SUCCESSFUL;
324}
325
326static int __pmac u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
327 int offset, int len, u32 val)
328{
329 struct pci_controller *hose;
330 struct device_node *busdn;
331 unsigned long addr;
332
333 if (bus->self)
334 busdn = pci_device_to_OF_node(bus->self);
335 else
336 busdn = bus->sysdata;
337 if (busdn == NULL)
338 return PCIBIOS_DEVICE_NOT_FOUND;
339 hose = busdn->phb;
340 if (hose == NULL)
341 return PCIBIOS_DEVICE_NOT_FOUND;
342
343 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
344 if (!addr)
345 return PCIBIOS_DEVICE_NOT_FOUND;
346
347
348
349
350 if (skip_k2_device(bus, devfn))
351 return PCIBIOS_SUCCESSFUL;
352
353
354
355
356
357 switch (len) {
358 case 1:
359 out_8((u8 *)addr, val);
360 (void) in_8((u8 *)addr);
361 break;
362 case 2:
363 out_le16((u16 *)addr, val);
364 (void) in_le16((u16 *)addr);
365 break;
366 default:
367 out_le32((u32 *)addr, val);
368 (void) in_le32((u32 *)addr);
369 break;
370 }
371 return PCIBIOS_SUCCESSFUL;
372}
373
374static struct pci_ops u3_ht_pci_ops =
375{
376 u3_ht_read_config,
377 u3_ht_write_config
378};
379
380static void __init setup_u3_agp(struct pci_controller* hose)
381{
382
383
384
385
386
387
388
389
390
391 hose->first_busno = 0xf0;
392 hose->last_busno = 0xff;
393 has_uninorth = 1;
394 hose->ops = ¯isc_pci_ops;
395 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
396 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
397
398 u3_agp = hose;
399}
400
401static void __init setup_u3_ht(struct pci_controller* hose)
402{
403 struct device_node *np = (struct device_node *)hose->arch_data;
404 int i, cur;
405
406 hose->ops = &u3_ht_pci_ops;
407
408
409
410
411
412 hose->cfg_data = (volatile unsigned char *)ioremap(0xf2000000, 0x02000000);
413
414
415
416
417
418
419
420
421
422 hose->io_base_phys = 0xf4000000 + 0x00400000;
423 hose->io_base_virt = ioremap(hose->io_base_phys, 0x00400000);
424 isa_io_base = pci_io_base = (unsigned long) hose->io_base_virt;
425 hose->io_resource.name = np->full_name;
426 hose->io_resource.start = 0;
427 hose->io_resource.end = 0x003fffff;
428 hose->io_resource.flags = IORESOURCE_IO;
429 hose->pci_mem_offset = 0;
430 hose->first_busno = 0;
431 hose->last_busno = 0xef;
432 hose->mem_resources[0].name = np->full_name;
433 hose->mem_resources[0].start = 0x80000000;
434 hose->mem_resources[0].end = 0xefffffff;
435 hose->mem_resources[0].flags = IORESOURCE_MEM;
436
437 if (u3_agp == NULL) {
438 DBG("U3 has no AGP, using full resource range\n");
439 return;
440 }
441
442
443
444
445
446
447 cur = 0;
448 for (i=0; i<3; i++) {
449 struct resource *res = &u3_agp->mem_resources[i];
450 if (res->flags != IORESOURCE_MEM)
451 continue;
452
453 if (res->start >= 0xf0000000)
454 continue;
455
456 if (hose->mem_resources[cur].start == res->start) {
457 DBG("U3/HT: shrink start of %d, %08lx -> %08lx\n",
458 cur, hose->mem_resources[cur].start, res->end + 1);
459 hose->mem_resources[cur].start = res->end + 1;
460 continue;
461 }
462 if (hose->mem_resources[cur].end == res->end) {
463 DBG("U3/HT: shrink end of %d, %08lx -> %08lx\n",
464 cur, hose->mem_resources[cur].end, res->start - 1);
465 hose->mem_resources[cur].end = res->start - 1;
466 continue;
467 }
468
469 if (cur == 2) {
470
471 printk(KERN_WARNING "Running out of resources for /ht host !\n");
472 hose->mem_resources[cur].end = res->start - 1;
473 continue;
474 }
475 cur++;
476 DBG("U3/HT: hole, %d end at %08lx, %d start at %08lx\n",
477 cur-1, res->start - 1, cur, res->end + 1);
478 hose->mem_resources[cur].name = np->full_name;
479 hose->mem_resources[cur].flags = IORESOURCE_MEM;
480 hose->mem_resources[cur].start = res->end + 1;
481 hose->mem_resources[cur].end = hose->mem_resources[cur-1].end;
482 hose->mem_resources[cur-1].end = res->start - 1;
483 }
484}
485
486static void __init pmac_process_bridge_OF_ranges(struct pci_controller *hose,
487 struct device_node *dev, int primary)
488{
489 static unsigned int static_lc_ranges[2024];
490 unsigned int *dt_ranges, *lc_ranges, *ranges, *prev;
491 unsigned int size;
492 int rlen = 0, orig_rlen;
493 int memno = 0;
494 struct resource *res;
495 int np, na = prom_n_addr_cells(dev);
496
497 np = na + 5;
498
499
500
501
502
503 dt_ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
504 if (!dt_ranges)
505 return;
506
507 lc_ranges = static_lc_ranges;
508 if (!lc_ranges)
509 return;
510 memcpy(lc_ranges, dt_ranges, rlen);
511 orig_rlen = rlen;
512
513
514
515
516 ranges = lc_ranges;
517 prev = NULL;
518 while ((rlen -= np * sizeof(unsigned int)) >= 0) {
519 if (prev) {
520 if (prev[0] == ranges[0] && prev[1] == ranges[1] &&
521 (prev[2] + prev[na+4]) == ranges[2] &&
522 (prev[na+2] + prev[na+4]) == ranges[na+2]) {
523 prev[na+4] += ranges[na+4];
524 ranges[0] = 0;
525 ranges += np;
526 continue;
527 }
528 }
529 prev = ranges;
530 ranges += np;
531 }
532
533
534
535
536
537
538
539
540
541 ranges = lc_ranges;
542 rlen = orig_rlen;
543 while (ranges && (rlen -= np * sizeof(unsigned int)) >= 0) {
544 res = NULL;
545 size = ranges[na+4];
546 switch (ranges[0] >> 24) {
547 case 1:
548 if (ranges[2] != 0)
549 break;
550 hose->io_base_phys = ranges[na+2];
551
552 if (size > 0x01000000)
553 size = 0x01000000;
554 hose->io_base_virt = ioremap(ranges[na+2], size);
555 if (primary)
556 isa_io_base = (unsigned long) hose->io_base_virt;
557 res = &hose->io_resource;
558 res->flags = IORESOURCE_IO;
559 res->start = ranges[2];
560 break;
561 case 2:
562 memno = 0;
563 if (ranges[1] == 0 && ranges[2] == 0
564 && ranges[na+4] <= (16 << 20)) {
565
566#if 0
567 if (primary)
568 isa_mem_base = ranges[na+2];
569#endif
570 memno = 1;
571 }
572 while (memno < 3 && hose->mem_resources[memno].flags)
573 ++memno;
574 if (memno == 0)
575 hose->pci_mem_offset = ranges[na+2] - ranges[2];
576 if (memno < 3) {
577 res = &hose->mem_resources[memno];
578 res->flags = IORESOURCE_MEM;
579 res->start = ranges[na+2];
580 }
581 break;
582 }
583 if (res != NULL) {
584 res->name = dev->full_name;
585 res->end = res->start + size - 1;
586 res->parent = NULL;
587 res->sibling = NULL;
588 res->child = NULL;
589 }
590 ranges += np;
591 }
592}
593
594
595
596
597
598
599static int __init add_bridge(struct device_node *dev)
600{
601 int len;
602 struct pci_controller *hose;
603 char* disp_name;
604 int *bus_range;
605 int primary = 1;
606 struct property *of_prop;
607
608 DBG("Adding PCI host bridge %s\n", dev->full_name);
609
610 bus_range = (int *) get_property(dev, "bus-range", &len);
611 if (bus_range == NULL || len < 2 * sizeof(int)) {
612 printk(KERN_WARNING "Can't get bus-range for %s, assume bus 0\n",
613 dev->full_name);
614 }
615
616 hose = pci_alloc_pci_controller(phb_type_apple);
617 if (!hose)
618 return -ENOMEM;
619 hose->arch_data = dev;
620 hose->first_busno = bus_range ? bus_range[0] : 0;
621 hose->last_busno = bus_range ? bus_range[1] : 0xff;
622
623 of_prop = (struct property *)alloc_bootmem(sizeof(struct property) +
624 sizeof(hose->global_number));
625 if (of_prop) {
626 memset(of_prop, 0, sizeof(struct property));
627 of_prop->name = "linux,pci-domain";
628 of_prop->length = sizeof(hose->global_number);
629 of_prop->value = (unsigned char *)&of_prop[1];
630 memcpy(of_prop->value, &hose->global_number, sizeof(hose->global_number));
631 prom_add_property(dev, of_prop);
632 }
633
634 disp_name = NULL;
635 if (device_is_compatible(dev, "u3-agp")) {
636 setup_u3_agp(hose);
637 disp_name = "U3-AGP";
638 primary = 0;
639 } else if (device_is_compatible(dev, "u3-ht")) {
640 setup_u3_ht(hose);
641 disp_name = "U3-HT";
642 primary = 1;
643 }
644 printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number: %d->%d\n",
645 disp_name, hose->first_busno, hose->last_busno);
646
647
648
649 pmac_process_bridge_OF_ranges(hose, dev, primary);
650
651
652 fixup_bus_range(dev);
653
654 return 0;
655}
656
657
658void __init pmac_pcibios_fixup(void)
659{
660 struct pci_dev *dev = NULL;
661
662 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL)
663 pci_read_irq_line(dev);
664
665 pci_fix_bus_sysdata();
666
667#ifdef CONFIG_PMAC_DART
668 iommu_setup_pmac();
669#endif
670
671}
672
673static void __init pmac_fixup_phb_resources(void)
674{
675 struct pci_controller *hose;
676
677 for (hose = hose_head; hose; hose = hose->next) {
678 unsigned long offset = (unsigned long)hose->io_base_virt - pci_io_base;
679 hose->io_resource.start += offset;
680 hose->io_resource.end += offset;
681 printk(KERN_INFO "PCI Host %d, io start: %lx; io end: %lx\n",
682 hose->global_number,
683 hose->io_resource.start, hose->io_resource.end);
684 }
685}
686
687void __init pmac_pci_init(void)
688{
689 struct device_node *np, *root;
690 struct device_node *ht = NULL;
691
692
693
694
695
696
697 root = of_find_node_by_path("/");
698 if (root == NULL) {
699 printk(KERN_CRIT "pmac_find_bridges: can't find root of device tree\n");
700 return;
701 }
702 for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
703 if (np->name == NULL)
704 continue;
705 if (strcmp(np->name, "pci") == 0) {
706 if (add_bridge(np) == 0)
707 of_node_get(np);
708 }
709 if (strcmp(np->name, "ht") == 0) {
710 of_node_get(np);
711 ht = np;
712 }
713 }
714 of_node_put(root);
715
716
717
718 if (ht && add_bridge(ht) != 0)
719 of_node_put(ht);
720
721
722
723
724 pmac_fixup_phb_resources();
725
726
727 pci_devs_phb_init();
728
729
730
731
732
733 if (u3_agp) {
734 struct device_node *np = u3_agp->arch_data;
735 np->busno = 0xf0;
736 for (np = np->child; np; np = np->sibling)
737 np->busno = 0xf0;
738 }
739
740 pmac_check_ht_link();
741
742
743 pci_probe_only = 0;
744
745
746
747
748 pci_cache_line_size = 16;
749}
750
751
752
753
754
755void fixup_k2_sata(struct pci_dev* dev)
756{
757 int i;
758 u16 cmd;
759
760 if (PCI_FUNC(dev->devfn) > 0) {
761 pci_read_config_word(dev, PCI_COMMAND, &cmd);
762 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
763 pci_write_config_word(dev, PCI_COMMAND, cmd);
764 for (i = 0; i < 6; i++) {
765 dev->resource[i].start = dev->resource[i].end = 0;
766 dev->resource[i].flags = 0;
767 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
768 }
769 } else {
770 pci_read_config_word(dev, PCI_COMMAND, &cmd);
771 cmd &= ~PCI_COMMAND_IO;
772 pci_write_config_word(dev, PCI_COMMAND, cmd);
773 for (i = 0; i < 5; i++) {
774 dev->resource[i].start = dev->resource[i].end = 0;
775 dev->resource[i].flags = 0;
776 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
777 }
778 }
779}
780