blob: 787ffd999bc2efc6ecc99b6232f2a39e4f905346 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
2 * Support for PCI bridges found on Power Macintoshes.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10003 *
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11004 * Copyright (C) 2003-2005 Benjamin Herrenschmuidt (benh@kernel.crashing.org)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10005 * Copyright (C) 1997 Paul Mackerras (paulus@samba.org)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/kernel.h>
14#include <linux/pci.h>
15#include <linux/delay.h>
16#include <linux/string.h>
17#include <linux/init.h>
18#include <linux/bootmem.h>
19
20#include <asm/sections.h>
21#include <asm/io.h>
22#include <asm/prom.h>
23#include <asm/pci-bridge.h>
24#include <asm/machdep.h>
25#include <asm/pmac_feature.h>
Paul Mackerras830825d2005-10-26 17:16:38 +100026#include <asm/grackle.h>
Paul Mackerras3c3f42d2005-10-10 22:58:41 +100027#ifdef CONFIG_PPC64
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +110028//#include <asm/iommu.h>
Paul Mackerras3c3f42d2005-10-10 22:58:41 +100029#include <asm/ppc-pci.h>
30#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +100031
32#undef DEBUG
33
34#ifdef DEBUG
35#define DBG(x...) printk(x)
36#else
37#define DBG(x...)
38#endif
39
40static int add_bridge(struct device_node *dev);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100041
42/* XXX Could be per-controller, but I don't think we risk anything by
43 * assuming we won't have both UniNorth and Bandit */
44static int has_uninorth;
Paul Mackerras35499c02005-10-22 16:02:39 +100045#ifdef CONFIG_PPC64
Paul Mackerras14cf11a2005-09-26 16:04:21 +100046static struct pci_controller *u3_agp;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +110047static struct pci_controller *u4_pcie;
Paul Mackerras35499c02005-10-22 16:02:39 +100048static struct pci_controller *u3_ht;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100049#define has_second_ohare 0
50#else
51static int has_second_ohare;
Paul Mackerras35499c02005-10-22 16:02:39 +100052#endif /* CONFIG_PPC64 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100053
54extern u8 pci_cache_line_size;
55extern int pcibios_assign_bus_offset;
56
57struct device_node *k2_skiplist[2];
58
59/*
60 * Magic constants for enabling cache coherency in the bandit/PSX bridge.
61 */
62#define BANDIT_DEVID_2 8
63#define BANDIT_REVID 3
64
65#define BANDIT_DEVNUM 11
66#define BANDIT_MAGIC 0x50
67#define BANDIT_COHERENT 0x40
68
69static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
70{
71 for (; node != 0;node = node->sibling) {
Jeremy Kerr018a3d12006-07-12 15:40:29 +100072 const int * bus_range;
73 const unsigned int *class_code;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100074 int len;
75
76 /* For PCI<->PCI bridges or CardBus bridges, we go down */
Jeremy Kerr018a3d12006-07-12 15:40:29 +100077 class_code = get_property(node, "class-code", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100078 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
79 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
80 continue;
Jeremy Kerr018a3d12006-07-12 15:40:29 +100081 bus_range = get_property(node, "bus-range", &len);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100082 if (bus_range != NULL && len > 2 * sizeof(int)) {
83 if (bus_range[1] > higher)
84 higher = bus_range[1];
85 }
86 higher = fixup_one_level_bus_range(node->child, higher);
87 }
88 return higher;
89}
90
91/* This routine fixes the "bus-range" property of all bridges in the
92 * system since they tend to have their "last" member wrong on macs
93 *
94 * Note that the bus numbers manipulated here are OF bus numbers, they
95 * are not Linux bus numbers.
96 */
97static void __init fixup_bus_range(struct device_node *bridge)
98{
Jeremy Kerr018a3d12006-07-12 15:40:29 +100099 int *bus_range, len;
100 struct property *prop;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000101
102 /* Lookup the "bus-range" property for the hose */
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000103 prop = of_find_property(bridge, "bus-range", &len);
104 if (prop == NULL || prop->length < 2 * sizeof(int))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000105 return;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000106
107 bus_range = (int *)prop->value;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000108 bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
109}
110
111/*
112 * Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.
113 *
114 * The "Bandit" version is present in all early PCI PowerMacs,
115 * and up to the first ones using Grackle. Some machines may
116 * have 2 bandit controllers (2 PCI busses).
117 *
118 * "Chaos" is used in some "Bandit"-type machines as a bridge
119 * for the separate display bus. It is accessed the same
120 * way as bandit, but cannot be probed for devices. It therefore
121 * has its own config access functions.
122 *
123 * The "UniNorth" version is present in all Core99 machines
124 * (iBook, G4, new IMacs, and all the recent Apple machines).
125 * It contains 3 controllers in one ASIC.
126 *
127 * The U3 is the bridge used on G5 machines. It contains an
128 * AGP bus which is dealt with the old UniNorth access routines
129 * and a HyperTransport bus which uses its own set of access
130 * functions.
131 */
132
133#define MACRISC_CFA0(devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100134 ((1 << (unsigned int)PCI_SLOT(dev_fn)) \
135 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
136 | (((unsigned int)(off)) & 0xFCUL))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000137
138#define MACRISC_CFA1(bus, devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100139 ((((unsigned int)(bus)) << 16) \
140 |(((unsigned int)(devfn)) << 8) \
141 |(((unsigned int)(off)) & 0xFCUL) \
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000142 |1UL)
143
Al Virode125bf2006-02-01 05:18:43 -0500144static volatile void __iomem *macrisc_cfg_access(struct pci_controller* hose,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000145 u8 bus, u8 dev_fn, u8 offset)
146{
147 unsigned int caddr;
148
149 if (bus == hose->first_busno) {
150 if (dev_fn < (11 << 3))
Al Virode125bf2006-02-01 05:18:43 -0500151 return NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000152 caddr = MACRISC_CFA0(dev_fn, offset);
153 } else
154 caddr = MACRISC_CFA1(bus, dev_fn, offset);
155
156 /* Uninorth will return garbage if we don't read back the value ! */
157 do {
158 out_le32(hose->cfg_addr, caddr);
159 } while (in_le32(hose->cfg_addr) != caddr);
160
161 offset &= has_uninorth ? 0x07 : 0x03;
Al Virode125bf2006-02-01 05:18:43 -0500162 return hose->cfg_data + offset;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000163}
164
165static int macrisc_read_config(struct pci_bus *bus, unsigned int devfn,
166 int offset, int len, u32 *val)
167{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000168 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500169 volatile void __iomem *addr;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000170
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000171 hose = pci_bus_to_host(bus);
172 if (hose == NULL)
173 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100174 if (offset >= 0x100)
175 return PCIBIOS_BAD_REGISTER_NUMBER;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000176 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
177 if (!addr)
178 return PCIBIOS_DEVICE_NOT_FOUND;
179 /*
180 * Note: the caller has already checked that offset is
181 * suitably aligned and that len is 1, 2 or 4.
182 */
183 switch (len) {
184 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500185 *val = in_8(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000186 break;
187 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500188 *val = in_le16(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000189 break;
190 default:
Al Virode125bf2006-02-01 05:18:43 -0500191 *val = in_le32(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000192 break;
193 }
194 return PCIBIOS_SUCCESSFUL;
195}
196
197static int macrisc_write_config(struct pci_bus *bus, unsigned int devfn,
198 int offset, int len, u32 val)
199{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000200 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500201 volatile void __iomem *addr;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000202
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000203 hose = pci_bus_to_host(bus);
204 if (hose == NULL)
205 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100206 if (offset >= 0x100)
207 return PCIBIOS_BAD_REGISTER_NUMBER;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000208 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
209 if (!addr)
210 return PCIBIOS_DEVICE_NOT_FOUND;
211 /*
212 * Note: the caller has already checked that offset is
213 * suitably aligned and that len is 1, 2 or 4.
214 */
215 switch (len) {
216 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500217 out_8(addr, val);
218 (void) in_8(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000219 break;
220 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500221 out_le16(addr, val);
222 (void) in_le16(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000223 break;
224 default:
Al Virode125bf2006-02-01 05:18:43 -0500225 out_le32(addr, val);
226 (void) in_le32(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000227 break;
228 }
229 return PCIBIOS_SUCCESSFUL;
230}
231
232static struct pci_ops macrisc_pci_ops =
233{
234 macrisc_read_config,
235 macrisc_write_config
236};
237
Paul Mackerras35499c02005-10-22 16:02:39 +1000238#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000239/*
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000240 * Verify that a specific (bus, dev_fn) exists on chaos
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000241 */
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100242static int chaos_validate_dev(struct pci_bus *bus, int devfn, int offset)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000243{
244 struct device_node *np;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000245 const u32 *vendor, *device;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000246
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100247 if (offset >= 0x100)
248 return PCIBIOS_BAD_REGISTER_NUMBER;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000249 np = pci_busdev_to_OF_node(bus, devfn);
250 if (np == NULL)
251 return PCIBIOS_DEVICE_NOT_FOUND;
252
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000253 vendor = get_property(np, "vendor-id", NULL);
254 device = get_property(np, "device-id", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000255 if (vendor == NULL || device == NULL)
256 return PCIBIOS_DEVICE_NOT_FOUND;
257
258 if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)
259 && (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))
260 return PCIBIOS_BAD_REGISTER_NUMBER;
261
262 return PCIBIOS_SUCCESSFUL;
263}
264
265static int
266chaos_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
267 int len, u32 *val)
268{
269 int result = chaos_validate_dev(bus, devfn, offset);
270 if (result == PCIBIOS_BAD_REGISTER_NUMBER)
271 *val = ~0U;
272 if (result != PCIBIOS_SUCCESSFUL)
273 return result;
274 return macrisc_read_config(bus, devfn, offset, len, val);
275}
276
277static int
278chaos_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
279 int len, u32 val)
280{
281 int result = chaos_validate_dev(bus, devfn, offset);
282 if (result != PCIBIOS_SUCCESSFUL)
283 return result;
284 return macrisc_write_config(bus, devfn, offset, len, val);
285}
286
287static struct pci_ops chaos_pci_ops =
288{
289 chaos_read_config,
290 chaos_write_config
291};
292
Paul Mackerras35499c02005-10-22 16:02:39 +1000293static void __init setup_chaos(struct pci_controller *hose,
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100294 struct resource *addr)
Paul Mackerras35499c02005-10-22 16:02:39 +1000295{
296 /* assume a `chaos' bridge */
297 hose->ops = &chaos_pci_ops;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100298 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
299 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
Paul Mackerras35499c02005-10-22 16:02:39 +1000300}
Paul Mackerras35499c02005-10-22 16:02:39 +1000301#endif /* CONFIG_PPC32 */
302
303#ifdef CONFIG_PPC64
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000304/*
305 * These versions of U3 HyperTransport config space access ops do not
306 * implement self-view of the HT host yet
307 */
308
309/*
310 * This function deals with some "special cases" devices.
311 *
312 * 0 -> No special case
313 * 1 -> Skip the device but act as if the access was successfull
314 * (return 0xff's on reads, eventually, cache config space
315 * accesses in a later version)
316 * -1 -> Hide the device (unsuccessful acess)
317 */
318static int u3_ht_skip_device(struct pci_controller *hose,
319 struct pci_bus *bus, unsigned int devfn)
320{
321 struct device_node *busdn, *dn;
322 int i;
323
324 /* We only allow config cycles to devices that are in OF device-tree
325 * as we are apparently having some weird things going on with some
326 * revs of K2 on recent G5s
327 */
328 if (bus->self)
329 busdn = pci_device_to_OF_node(bus->self);
330 else
331 busdn = hose->arch_data;
332 for (dn = busdn->child; dn; dn = dn->sibling)
linase07102d2005-12-05 19:37:35 -0600333 if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000334 break;
335 if (dn == NULL)
336 return -1;
337
338 /*
339 * When a device in K2 is powered down, we die on config
340 * cycle accesses. Fix that here.
341 */
342 for (i=0; i<2; i++)
343 if (k2_skiplist[i] == dn)
344 return 1;
345
346 return 0;
347}
348
349#define U3_HT_CFA0(devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100350 ((((unsigned int)devfn) << 8) | offset)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000351#define U3_HT_CFA1(bus, devfn, off) \
352 (U3_HT_CFA0(devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100353 + (((unsigned int)bus) << 16) \
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000354 + 0x01000000UL)
355
Al Virode125bf2006-02-01 05:18:43 -0500356static volatile void __iomem *u3_ht_cfg_access(struct pci_controller* hose,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000357 u8 bus, u8 devfn, u8 offset)
358{
359 if (bus == hose->first_busno) {
360 /* For now, we don't self probe U3 HT bridge */
361 if (PCI_SLOT(devfn) == 0)
Al Virode125bf2006-02-01 05:18:43 -0500362 return NULL;
363 return hose->cfg_data + U3_HT_CFA0(devfn, offset);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000364 } else
Al Virode125bf2006-02-01 05:18:43 -0500365 return hose->cfg_data + U3_HT_CFA1(bus, devfn, offset);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000366}
367
368static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
369 int offset, int len, u32 *val)
370{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000371 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500372 volatile void __iomem *addr;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000373
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000374 hose = pci_bus_to_host(bus);
375 if (hose == NULL)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000376 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100377 if (offset >= 0x100)
378 return PCIBIOS_BAD_REGISTER_NUMBER;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000379 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
380 if (!addr)
381 return PCIBIOS_DEVICE_NOT_FOUND;
382
383 switch (u3_ht_skip_device(hose, bus, devfn)) {
384 case 0:
385 break;
386 case 1:
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000387 switch (len) {
388 case 1:
389 *val = 0xff; break;
390 case 2:
391 *val = 0xffff; break;
392 default:
393 *val = 0xfffffffful; break;
394 }
395 return PCIBIOS_SUCCESSFUL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000396 default:
397 return PCIBIOS_DEVICE_NOT_FOUND;
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000398 }
399
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000400 /*
401 * Note: the caller has already checked that offset is
402 * suitably aligned and that len is 1, 2 or 4.
403 */
404 switch (len) {
405 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500406 *val = in_8(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000407 break;
408 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500409 *val = in_le16(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000410 break;
411 default:
Al Virode125bf2006-02-01 05:18:43 -0500412 *val = in_le32(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000413 break;
414 }
415 return PCIBIOS_SUCCESSFUL;
416}
417
418static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
419 int offset, int len, u32 val)
420{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000421 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500422 volatile void __iomem *addr;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000423
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000424 hose = pci_bus_to_host(bus);
425 if (hose == NULL)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000426 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100427 if (offset >= 0x100)
428 return PCIBIOS_BAD_REGISTER_NUMBER;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000429 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
430 if (!addr)
431 return PCIBIOS_DEVICE_NOT_FOUND;
432
433 switch (u3_ht_skip_device(hose, bus, devfn)) {
434 case 0:
435 break;
436 case 1:
437 return PCIBIOS_SUCCESSFUL;
438 default:
439 return PCIBIOS_DEVICE_NOT_FOUND;
440 }
441
442 /*
443 * Note: the caller has already checked that offset is
444 * suitably aligned and that len is 1, 2 or 4.
445 */
446 switch (len) {
447 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500448 out_8(addr, val);
449 (void) in_8(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000450 break;
451 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500452 out_le16(addr, val);
453 (void) in_le16(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000454 break;
455 default:
Al Virode125bf2006-02-01 05:18:43 -0500456 out_le32((u32 __iomem *)addr, val);
457 (void) in_le32(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000458 break;
459 }
460 return PCIBIOS_SUCCESSFUL;
461}
462
463static struct pci_ops u3_ht_pci_ops =
464{
465 u3_ht_read_config,
466 u3_ht_write_config
467};
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100468
469#define U4_PCIE_CFA0(devfn, off) \
470 ((1 << ((unsigned int)PCI_SLOT(dev_fn))) \
471 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
472 | ((((unsigned int)(off)) >> 8) << 28) \
473 | (((unsigned int)(off)) & 0xfcU))
474
475#define U4_PCIE_CFA1(bus, devfn, off) \
476 ((((unsigned int)(bus)) << 16) \
477 |(((unsigned int)(devfn)) << 8) \
478 | ((((unsigned int)(off)) >> 8) << 28) \
479 |(((unsigned int)(off)) & 0xfcU) \
480 |1UL)
481
Al Virode125bf2006-02-01 05:18:43 -0500482static volatile void __iomem *u4_pcie_cfg_access(struct pci_controller* hose,
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100483 u8 bus, u8 dev_fn, int offset)
484{
485 unsigned int caddr;
486
487 if (bus == hose->first_busno) {
488 caddr = U4_PCIE_CFA0(dev_fn, offset);
489 } else
490 caddr = U4_PCIE_CFA1(bus, dev_fn, offset);
491
492 /* Uninorth will return garbage if we don't read back the value ! */
493 do {
494 out_le32(hose->cfg_addr, caddr);
495 } while (in_le32(hose->cfg_addr) != caddr);
496
497 offset &= 0x03;
Al Virode125bf2006-02-01 05:18:43 -0500498 return hose->cfg_data + offset;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100499}
500
501static int u4_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
502 int offset, int len, u32 *val)
503{
504 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500505 volatile void __iomem *addr;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100506
507 hose = pci_bus_to_host(bus);
508 if (hose == NULL)
509 return PCIBIOS_DEVICE_NOT_FOUND;
510 if (offset >= 0x1000)
511 return PCIBIOS_BAD_REGISTER_NUMBER;
512 addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
513 if (!addr)
514 return PCIBIOS_DEVICE_NOT_FOUND;
515 /*
516 * Note: the caller has already checked that offset is
517 * suitably aligned and that len is 1, 2 or 4.
518 */
519 switch (len) {
520 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500521 *val = in_8(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100522 break;
523 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500524 *val = in_le16(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100525 break;
526 default:
Al Virode125bf2006-02-01 05:18:43 -0500527 *val = in_le32(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100528 break;
529 }
530 return PCIBIOS_SUCCESSFUL;
531}
532
533static int u4_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
534 int offset, int len, u32 val)
535{
536 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500537 volatile void __iomem *addr;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100538
539 hose = pci_bus_to_host(bus);
540 if (hose == NULL)
541 return PCIBIOS_DEVICE_NOT_FOUND;
542 if (offset >= 0x1000)
543 return PCIBIOS_BAD_REGISTER_NUMBER;
544 addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
545 if (!addr)
546 return PCIBIOS_DEVICE_NOT_FOUND;
547 /*
548 * Note: the caller has already checked that offset is
549 * suitably aligned and that len is 1, 2 or 4.
550 */
551 switch (len) {
552 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500553 out_8(addr, val);
554 (void) in_8(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100555 break;
556 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500557 out_le16(addr, val);
558 (void) in_le16(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100559 break;
560 default:
Al Virode125bf2006-02-01 05:18:43 -0500561 out_le32(addr, val);
562 (void) in_le32(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100563 break;
564 }
565 return PCIBIOS_SUCCESSFUL;
566}
567
568static struct pci_ops u4_pcie_pci_ops =
569{
570 u4_pcie_read_config,
571 u4_pcie_write_config
572};
573
Paul Mackerras35499c02005-10-22 16:02:39 +1000574#endif /* CONFIG_PPC64 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000575
Paul Mackerras35499c02005-10-22 16:02:39 +1000576#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000577/*
578 * For a bandit bridge, turn on cache coherency if necessary.
579 * N.B. we could clean this up using the hose ops directly.
580 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000581static void __init init_bandit(struct pci_controller *bp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000582{
583 unsigned int vendev, magic;
584 int rev;
585
586 /* read the word at offset 0 in config space for device 11 */
587 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);
588 udelay(2);
589 vendev = in_le32(bp->cfg_data);
590 if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +
591 PCI_VENDOR_ID_APPLE) {
592 /* read the revision id */
593 out_le32(bp->cfg_addr,
594 (1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);
595 udelay(2);
596 rev = in_8(bp->cfg_data);
597 if (rev != BANDIT_REVID)
598 printk(KERN_WARNING
599 "Unknown revision %d for bandit\n", rev);
600 } else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {
601 printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);
602 return;
603 }
604
605 /* read the word at offset 0x50 */
606 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);
607 udelay(2);
608 magic = in_le32(bp->cfg_data);
609 if ((magic & BANDIT_COHERENT) != 0)
610 return;
611 magic |= BANDIT_COHERENT;
612 udelay(2);
613 out_le32(bp->cfg_data, magic);
614 printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");
615}
616
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000617/*
618 * Tweak the PCI-PCI bridge chip on the blue & white G3s.
619 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000620static void __init init_p2pbridge(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000621{
622 struct device_node *p2pbridge;
623 struct pci_controller* hose;
624 u8 bus, devfn;
625 u16 val;
626
627 /* XXX it would be better here to identify the specific
628 PCI-PCI bridge chip we have. */
629 if ((p2pbridge = find_devices("pci-bridge")) == 0
630 || p2pbridge->parent == NULL
631 || strcmp(p2pbridge->parent->name, "pci") != 0)
632 return;
633 if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {
634 DBG("Can't find PCI infos for PCI<->PCI bridge\n");
635 return;
636 }
637 /* Warning: At this point, we have not yet renumbered all busses.
638 * So we must use OF walking to find out hose
639 */
640 hose = pci_find_hose_for_OF_device(p2pbridge);
641 if (!hose) {
642 DBG("Can't find hose for PCI<->PCI bridge\n");
643 return;
644 }
645 if (early_read_config_word(hose, bus, devfn,
646 PCI_BRIDGE_CONTROL, &val) < 0) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100647 printk(KERN_ERR "init_p2pbridge: couldn't read bridge"
648 " control\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000649 return;
650 }
651 val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;
652 early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);
653}
654
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000655static void __init init_second_ohare(void)
656{
657 struct device_node *np = of_find_node_by_name(NULL, "pci106b,7");
658 unsigned char bus, devfn;
659 unsigned short cmd;
660
661 if (np == NULL)
662 return;
663
664 /* This must run before we initialize the PICs since the second
665 * ohare hosts a PIC that will be accessed there.
666 */
667 if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
668 struct pci_controller* hose =
669 pci_find_hose_for_OF_device(np);
670 if (!hose) {
671 printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
672 return;
673 }
674 early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
675 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
676 cmd &= ~PCI_COMMAND_IO;
677 early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
678 }
679 has_second_ohare = 1;
680}
681
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000682/*
683 * Some Apple desktop machines have a NEC PD720100A USB2 controller
684 * on the motherboard. Open Firmware, on these, will disable the
685 * EHCI part of it so it behaves like a pair of OHCI's. This fixup
686 * code re-enables it ;)
687 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000688static void __init fixup_nec_usb2(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000689{
690 struct device_node *nec;
691
692 for (nec = NULL; (nec = of_find_node_by_name(nec, "usb")) != NULL;) {
693 struct pci_controller *hose;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000694 u32 data;
695 const u32 *prop;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000696 u8 bus, devfn;
Paul Mackerras35499c02005-10-22 16:02:39 +1000697
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000698 prop = get_property(nec, "vendor-id", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000699 if (prop == NULL)
700 continue;
701 if (0x1033 != *prop)
702 continue;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000703 prop = get_property(nec, "device-id", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000704 if (prop == NULL)
705 continue;
706 if (0x0035 != *prop)
707 continue;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000708 prop = get_property(nec, "reg", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000709 if (prop == NULL)
710 continue;
711 devfn = (prop[0] >> 8) & 0xff;
712 bus = (prop[0] >> 16) & 0xff;
713 if (PCI_FUNC(devfn) != 0)
714 continue;
715 hose = pci_find_hose_for_OF_device(nec);
716 if (!hose)
717 continue;
718 early_read_config_dword(hose, bus, devfn, 0xe4, &data);
719 if (data & 1UL) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100720 printk("Found NEC PD720100A USB2 chip with disabled"
721 " EHCI, fixing up...\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000722 data &= ~1UL;
723 early_write_config_dword(hose, bus, devfn, 0xe4, data);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000724 }
725 }
726}
727
Paul Mackerras35499c02005-10-22 16:02:39 +1000728static void __init setup_bandit(struct pci_controller *hose,
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100729 struct resource *addr)
Paul Mackerras35499c02005-10-22 16:02:39 +1000730{
731 hose->ops = &macrisc_pci_ops;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100732 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
733 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
Paul Mackerras35499c02005-10-22 16:02:39 +1000734 init_bandit(hose);
735}
736
737static int __init setup_uninorth(struct pci_controller *hose,
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100738 struct resource *addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000739{
Paul Mackerras399fe2b2005-10-20 20:57:05 +1000740 pci_assign_all_buses = 1;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000741 has_uninorth = 1;
742 hose->ops = &macrisc_pci_ops;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100743 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
744 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000745 /* We "know" that the bridge at f2000000 has the PCI slots. */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100746 return addr->start == 0xf2000000;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000747}
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100748#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000749
Paul Mackerras35499c02005-10-22 16:02:39 +1000750#ifdef CONFIG_PPC64
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000751static void __init setup_u3_agp(struct pci_controller* hose)
752{
753 /* On G5, we move AGP up to high bus number so we don't need
754 * to reassign bus numbers for HT. If we ever have P2P bridges
Paul Mackerras35499c02005-10-22 16:02:39 +1000755 * on AGP, we'll have to move pci_assign_all_busses to the
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000756 * pci_controller structure so we enable it for AGP and not for
757 * HT childs.
758 * We hard code the address because of the different size of
759 * the reg address cell, we shall fix that by killing struct
760 * reg_property and using some accessor functions instead
761 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000762 hose->first_busno = 0xf0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000763 hose->last_busno = 0xff;
764 has_uninorth = 1;
765 hose->ops = &macrisc_pci_ops;
766 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
767 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000768 u3_agp = hose;
769}
770
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100771static void __init setup_u4_pcie(struct pci_controller* hose)
772{
773 /* We currently only implement the "non-atomic" config space, to
774 * be optimised later.
775 */
776 hose->ops = &u4_pcie_pci_ops;
777 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
778 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
779
780 /* The bus contains a bridge from root -> device, we need to
781 * make it visible on bus 0 so that we pick the right type
782 * of config cycles. If we didn't, we would have to force all
783 * config cycles to be type 1. So we override the "bus-range"
784 * property here
785 */
786 hose->first_busno = 0x00;
787 hose->last_busno = 0xff;
788 u4_pcie = hose;
789}
790
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000791static void __init setup_u3_ht(struct pci_controller* hose)
792{
793 struct device_node *np = (struct device_node *)hose->arch_data;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100794 struct pci_controller *other = NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000795 int i, cur;
796
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100797
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000798 hose->ops = &u3_ht_pci_ops;
799
800 /* We hard code the address because of the different size of
801 * the reg address cell, we shall fix that by killing struct
802 * reg_property and using some accessor functions instead
803 */
Al Virode125bf2006-02-01 05:18:43 -0500804 hose->cfg_data = ioremap(0xf2000000, 0x02000000);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000805
806 /*
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100807 * /ht node doesn't expose a "ranges" property, so we "remove"
808 * regions that have been allocated to AGP. So far, this version of
809 * the code doesn't assign any of the 0xfxxxxxxx "fine" memory regions
810 * to /ht. We need to fix that sooner or later by either parsing all
811 * child "ranges" properties or figuring out the U3 address space
812 * decoding logic and then read its configuration register (if any).
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000813 */
814 hose->io_base_phys = 0xf4000000;
Paul Mackerras35499c02005-10-22 16:02:39 +1000815 hose->pci_io_size = 0x00400000;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000816 hose->io_resource.name = np->full_name;
817 hose->io_resource.start = 0;
818 hose->io_resource.end = 0x003fffff;
819 hose->io_resource.flags = IORESOURCE_IO;
820 hose->pci_mem_offset = 0;
821 hose->first_busno = 0;
822 hose->last_busno = 0xef;
823 hose->mem_resources[0].name = np->full_name;
824 hose->mem_resources[0].start = 0x80000000;
825 hose->mem_resources[0].end = 0xefffffff;
826 hose->mem_resources[0].flags = IORESOURCE_MEM;
827
Paul Mackerras35499c02005-10-22 16:02:39 +1000828 u3_ht = hose;
829
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100830 if (u3_agp != NULL)
831 other = u3_agp;
832 else if (u4_pcie != NULL)
833 other = u4_pcie;
834
835 if (other == NULL) {
836 DBG("U3/4 has no AGP/PCIE, using full resource range\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000837 return;
838 }
839
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100840 /* Fixup bus range vs. PCIE */
841 if (u4_pcie)
842 hose->last_busno = u4_pcie->first_busno - 1;
843
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100844 /* We "remove" the AGP resources from the resources allocated to HT,
845 * that is we create "holes". However, that code does assumptions
846 * that so far happen to be true (cross fingers...), typically that
847 * resources in the AGP node are properly ordered
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000848 */
849 cur = 0;
850 for (i=0; i<3; i++) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100851 struct resource *res = &other->mem_resources[i];
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000852 if (res->flags != IORESOURCE_MEM)
853 continue;
854 /* We don't care about "fine" resources */
855 if (res->start >= 0xf0000000)
856 continue;
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100857 /* Check if it's just a matter of "shrinking" us in one
858 * direction
859 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000860 if (hose->mem_resources[cur].start == res->start) {
861 DBG("U3/HT: shrink start of %d, %08lx -> %08lx\n",
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100862 cur, hose->mem_resources[cur].start,
863 res->end + 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000864 hose->mem_resources[cur].start = res->end + 1;
865 continue;
866 }
867 if (hose->mem_resources[cur].end == res->end) {
868 DBG("U3/HT: shrink end of %d, %08lx -> %08lx\n",
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100869 cur, hose->mem_resources[cur].end,
870 res->start - 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000871 hose->mem_resources[cur].end = res->start - 1;
872 continue;
873 }
874 /* No, it's not the case, we need a hole */
875 if (cur == 2) {
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100876 /* not enough resources for a hole, we drop part
877 * of the range
878 */
879 printk(KERN_WARNING "Running out of resources"
880 " for /ht host !\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000881 hose->mem_resources[cur].end = res->start - 1;
882 continue;
Paul Mackerras35499c02005-10-22 16:02:39 +1000883 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000884 cur++;
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000885 DBG("U3/HT: hole, %d end at %08lx, %d start at %08lx\n",
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000886 cur-1, res->start - 1, cur, res->end + 1);
887 hose->mem_resources[cur].name = np->full_name;
888 hose->mem_resources[cur].flags = IORESOURCE_MEM;
889 hose->mem_resources[cur].start = res->end + 1;
890 hose->mem_resources[cur].end = hose->mem_resources[cur-1].end;
891 hose->mem_resources[cur-1].end = res->start - 1;
892 }
893}
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100894#endif /* CONFIG_PPC64 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000895
896/*
897 * We assume that if we have a G3 powermac, we have one bridge called
898 * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
899 * if we have one or more bandit or chaos bridges, we don't have a MPC106.
900 */
901static int __init add_bridge(struct device_node *dev)
902{
903 int len;
904 struct pci_controller *hose;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100905 struct resource rsrc;
Paul Mackerras35499c02005-10-22 16:02:39 +1000906 char *disp_name;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000907 const int *bus_range;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100908 int primary = 1, has_address = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000909
910 DBG("Adding PCI host bridge %s\n", dev->full_name);
911
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100912 /* Fetch host bridge registers address */
913 has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
914
915 /* Get bus range if any */
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000916 bus_range = get_property(dev, "bus-range", &len);
Paul Mackerras35499c02005-10-22 16:02:39 +1000917 if (bus_range == NULL || len < 2 * sizeof(int)) {
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100918 printk(KERN_WARNING "Can't get bus-range for %s, assume"
919 " bus 0\n", dev->full_name);
Paul Mackerras35499c02005-10-22 16:02:39 +1000920 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000921
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100922 /* XXX Different prototypes, to be merged */
923#ifdef CONFIG_PPC64
924 hose = pcibios_alloc_controller(dev);
925#else
Paul Mackerras35499c02005-10-22 16:02:39 +1000926 hose = pcibios_alloc_controller();
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100927#endif
Paul Mackerras35499c02005-10-22 16:02:39 +1000928 if (!hose)
929 return -ENOMEM;
930 hose->arch_data = dev;
931 hose->first_busno = bus_range ? bus_range[0] : 0;
932 hose->last_busno = bus_range ? bus_range[1] : 0xff;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000933
934 disp_name = NULL;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100935
936 /* 64 bits only bridges */
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100937#ifdef CONFIG_PPC64
Paul Mackerras35499c02005-10-22 16:02:39 +1000938 if (device_is_compatible(dev, "u3-agp")) {
939 setup_u3_agp(hose);
940 disp_name = "U3-AGP";
941 primary = 0;
942 } else if (device_is_compatible(dev, "u3-ht")) {
943 setup_u3_ht(hose);
944 disp_name = "U3-HT";
945 primary = 1;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100946 } else if (device_is_compatible(dev, "u4-pcie")) {
947 setup_u4_pcie(hose);
948 disp_name = "U4-PCIE";
949 primary = 0;
Paul Mackerras35499c02005-10-22 16:02:39 +1000950 }
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100951 printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number:"
952 " %d->%d\n", disp_name, hose->first_busno, hose->last_busno);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100953#endif /* CONFIG_PPC64 */
954
955 /* 32 bits only bridges */
956#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000957 if (device_is_compatible(dev, "uni-north")) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100958 primary = setup_uninorth(hose, &rsrc);
Paul Mackerras35499c02005-10-22 16:02:39 +1000959 disp_name = "UniNorth";
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000960 } else if (strcmp(dev->name, "pci") == 0) {
Paul Mackerras35499c02005-10-22 16:02:39 +1000961 /* XXX assume this is a mpc106 (grackle) */
962 setup_grackle(hose);
963 disp_name = "Grackle (MPC106)";
964 } else if (strcmp(dev->name, "bandit") == 0) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100965 setup_bandit(hose, &rsrc);
Paul Mackerras35499c02005-10-22 16:02:39 +1000966 disp_name = "Bandit";
967 } else if (strcmp(dev->name, "chaos") == 0) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100968 setup_chaos(hose, &rsrc);
Paul Mackerras35499c02005-10-22 16:02:39 +1000969 disp_name = "Chaos";
970 primary = 0;
971 }
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700972 printk(KERN_INFO "Found %s PCI host bridge at 0x%016llx. "
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100973 "Firmware bus number: %d->%d\n",
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700974 disp_name, (unsigned long long)rsrc.start, hose->first_busno,
975 hose->last_busno);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100976#endif /* CONFIG_PPC32 */
977
Paul Mackerras35499c02005-10-22 16:02:39 +1000978 DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
979 hose, hose->cfg_addr, hose->cfg_data);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000980
Paul Mackerras35499c02005-10-22 16:02:39 +1000981 /* Interpret the "ranges" property */
982 /* This also maps the I/O region and sets isa_io/mem_base */
983 pci_process_bridge_OF_ranges(hose, dev, primary);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000984
Paul Mackerras35499c02005-10-22 16:02:39 +1000985 /* Fixup "bus-range" OF property */
986 fixup_bus_range(dev);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000987
988 return 0;
989}
990
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000991void __init pmac_pcibios_fixup(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000992{
993 struct pci_dev* dev = NULL;
994
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000995 for_each_pci_dev(dev) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000996 /* Read interrupt from the device-tree */
997 pci_read_irq_line(dev);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000998
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000999 /* Fixup interrupt for the modem/ethernet combo controller.
1000 * on machines with a second ohare chip.
1001 * The number in the device tree (27) is bogus (correct for
1002 * the ethernet-only board but not the combo ethernet/modem
1003 * board). The real interrupt is 28 on the second controller
1004 * -> 28+32 = 60.
1005 */
1006 if (has_second_ohare &&
1007 dev->vendor == PCI_VENDOR_ID_DEC &&
1008 dev->device == PCI_DEVICE_ID_DEC_TULIP_PLUS)
1009 dev->irq = irq_create_mapping(NULL, 60, 0);
1010 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001011}
1012
Paul Mackerras35499c02005-10-22 16:02:39 +10001013#ifdef CONFIG_PPC64
1014static void __init pmac_fixup_phb_resources(void)
1015{
1016 struct pci_controller *hose, *tmp;
1017
1018 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1019 printk(KERN_INFO "PCI Host %d, io start: %lx; io end: %lx\n",
1020 hose->global_number,
1021 hose->io_resource.start, hose->io_resource.end);
1022 }
1023}
1024#endif
1025
1026void __init pmac_pci_init(void)
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001027{
1028 struct device_node *np, *root;
1029 struct device_node *ht = NULL;
1030
1031 root = of_find_node_by_path("/");
1032 if (root == NULL) {
Paul Mackerras35499c02005-10-22 16:02:39 +10001033 printk(KERN_CRIT "pmac_pci_init: can't find root "
1034 "of device tree\n");
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001035 return;
1036 }
1037 for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
1038 if (np->name == NULL)
1039 continue;
1040 if (strcmp(np->name, "bandit") == 0
1041 || strcmp(np->name, "chaos") == 0
1042 || strcmp(np->name, "pci") == 0) {
1043 if (add_bridge(np) == 0)
1044 of_node_get(np);
1045 }
1046 if (strcmp(np->name, "ht") == 0) {
1047 of_node_get(np);
1048 ht = np;
1049 }
1050 }
1051 of_node_put(root);
1052
Paul Mackerras35499c02005-10-22 16:02:39 +10001053#ifdef CONFIG_PPC64
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001054 /* Probe HT last as it relies on the agp resources to be already
1055 * setup
1056 */
1057 if (ht && add_bridge(ht) != 0)
1058 of_node_put(ht);
1059
Paul Mackerras35499c02005-10-22 16:02:39 +10001060 /*
1061 * We need to call pci_setup_phb_io for the HT bridge first
1062 * so it gets the I/O port numbers starting at 0, and we
1063 * need to call it for the AGP bridge after that so it gets
1064 * small positive I/O port numbers.
1065 */
1066 if (u3_ht)
1067 pci_setup_phb_io(u3_ht, 1);
1068 if (u3_agp)
1069 pci_setup_phb_io(u3_agp, 0);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001070 if (u4_pcie)
1071 pci_setup_phb_io(u4_pcie, 0);
Paul Mackerras35499c02005-10-22 16:02:39 +10001072
1073 /*
1074 * On ppc64, fixup the IO resources on our host bridges as
1075 * the common code does it only for children of the host bridges
1076 */
1077 pmac_fixup_phb_resources();
1078
1079 /* Setup the linkage between OF nodes and PHBs */
1080 pci_devs_phb_init();
1081
1082 /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
1083 * assume there is no P2P bridge on the AGP bus, which should be a
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001084 * safe assumptions for now. We should do something better in the
1085 * future though
Paul Mackerras35499c02005-10-22 16:02:39 +10001086 */
1087 if (u3_agp) {
1088 struct device_node *np = u3_agp->arch_data;
1089 PCI_DN(np)->busno = 0xf0;
1090 for (np = np->child; np; np = np->sibling)
1091 PCI_DN(np)->busno = 0xf0;
1092 }
Paul Mackerras35499c02005-10-22 16:02:39 +10001093 /* pmac_check_ht_link(); */
1094
1095 /* Tell pci.c to not use the common resource allocation mechanism */
1096 pci_probe_only = 1;
1097
Paul Mackerras35499c02005-10-22 16:02:39 +10001098#else /* CONFIG_PPC64 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001099 init_p2pbridge();
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001100 init_second_ohare();
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001101 fixup_nec_usb2();
Paul Mackerras35499c02005-10-22 16:02:39 +10001102
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001103 /* We are still having some issues with the Xserve G4, enabling
1104 * some offset between bus number and domains for now when we
1105 * assign all busses should help for now
1106 */
Paul Mackerras399fe2b2005-10-20 20:57:05 +10001107 if (pci_assign_all_buses)
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001108 pcibios_assign_bus_offset = 0x10;
Paul Mackerras35499c02005-10-22 16:02:39 +10001109#endif
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001110}
1111
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001112int
1113pmac_pci_enable_device_hook(struct pci_dev *dev, int initial)
1114{
1115 struct device_node* node;
1116 int updatecfg = 0;
1117 int uninorth_child;
1118
1119 node = pci_device_to_OF_node(dev);
1120
1121 /* We don't want to enable USB controllers absent from the OF tree
1122 * (iBook second controller)
1123 */
1124 if (dev->vendor == PCI_VENDOR_ID_APPLE
Jean Delvarec67808e2006-04-09 20:07:35 +02001125 && dev->class == PCI_CLASS_SERIAL_USB_OHCI
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001126 && !node) {
1127 printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
1128 pci_name(dev));
1129 return -EINVAL;
1130 }
1131
1132 if (!node)
1133 return 0;
1134
1135 uninorth_child = node->parent &&
1136 device_is_compatible(node->parent, "uni-north");
Paul Mackerras35499c02005-10-22 16:02:39 +10001137
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001138 /* Firewire & GMAC were disabled after PCI probe, the driver is
1139 * claiming them, we must re-enable them now.
1140 */
1141 if (uninorth_child && !strcmp(node->name, "firewire") &&
1142 (device_is_compatible(node, "pci106b,18") ||
1143 device_is_compatible(node, "pci106b,30") ||
1144 device_is_compatible(node, "pci11c1,5811"))) {
1145 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
1146 pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
1147 updatecfg = 1;
1148 }
1149 if (uninorth_child && !strcmp(node->name, "ethernet") &&
1150 device_is_compatible(node, "gmac")) {
1151 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
1152 updatecfg = 1;
1153 }
1154
1155 if (updatecfg) {
1156 u16 cmd;
Paul Mackerras35499c02005-10-22 16:02:39 +10001157
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001158 /*
1159 * Make sure PCI is correctly configured
1160 *
1161 * We use old pci_bios versions of the function since, by
1162 * default, gmac is not powered up, and so will be absent
1163 * from the kernel initial PCI lookup.
1164 *
1165 * Should be replaced by 2.4 new PCI mechanisms and really
1166 * register the device.
1167 */
1168 pci_read_config_word(dev, PCI_COMMAND, &cmd);
Paul Mackerras35499c02005-10-22 16:02:39 +10001169 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1170 | PCI_COMMAND_INVALIDATE;
1171 pci_write_config_word(dev, PCI_COMMAND, cmd);
1172 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
1173 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1174 L1_CACHE_BYTES >> 2);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001175 }
1176
1177 return 0;
1178}
1179
1180/* We power down some devices after they have been probed. They'll
1181 * be powered back on later on
1182 */
Paul Mackerras35499c02005-10-22 16:02:39 +10001183void __init pmac_pcibios_after_init(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001184{
1185 struct device_node* nd;
1186
1187#ifdef CONFIG_BLK_DEV_IDE
1188 struct pci_dev *dev = NULL;
1189
1190 /* OF fails to initialize IDE controllers on macs
1191 * (and maybe other machines)
1192 *
1193 * Ideally, this should be moved to the IDE layer, but we need
1194 * to check specifically with Andre Hedrick how to do it cleanly
1195 * since the common IDE code seem to care about the fact that the
1196 * BIOS may have disabled a controller.
1197 *
1198 * -- BenH
1199 */
1200 for_each_pci_dev(dev) {
1201 if ((dev->class >> 16) == PCI_BASE_CLASS_STORAGE)
1202 pci_enable_device(dev);
1203 }
1204#endif /* CONFIG_BLK_DEV_IDE */
1205
1206 nd = find_devices("firewire");
1207 while (nd) {
1208 if (nd->parent && (device_is_compatible(nd, "pci106b,18") ||
1209 device_is_compatible(nd, "pci106b,30") ||
1210 device_is_compatible(nd, "pci11c1,5811"))
1211 && device_is_compatible(nd->parent, "uni-north")) {
1212 pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
1213 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
1214 }
1215 nd = nd->next;
1216 }
1217 nd = find_devices("ethernet");
1218 while (nd) {
1219 if (nd->parent && device_is_compatible(nd, "gmac")
1220 && device_is_compatible(nd->parent, "uni-north"))
1221 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
1222 nd = nd->next;
1223 }
1224}
1225
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001226#ifdef CONFIG_PPC32
1227void pmac_pci_fixup_cardbus(struct pci_dev* dev)
1228{
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001229 if (!machine_is(powermac))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001230 return;
1231 /*
1232 * Fix the interrupt routing on the various cardbus bridges
1233 * used on powerbooks
1234 */
1235 if (dev->vendor != PCI_VENDOR_ID_TI)
1236 return;
1237 if (dev->device == PCI_DEVICE_ID_TI_1130 ||
1238 dev->device == PCI_DEVICE_ID_TI_1131) {
1239 u8 val;
Paul Mackerras35499c02005-10-22 16:02:39 +10001240 /* Enable PCI interrupt */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001241 if (pci_read_config_byte(dev, 0x91, &val) == 0)
1242 pci_write_config_byte(dev, 0x91, val | 0x30);
1243 /* Disable ISA interrupt mode */
1244 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1245 pci_write_config_byte(dev, 0x92, val & ~0x06);
1246 }
1247 if (dev->device == PCI_DEVICE_ID_TI_1210 ||
1248 dev->device == PCI_DEVICE_ID_TI_1211 ||
1249 dev->device == PCI_DEVICE_ID_TI_1410 ||
1250 dev->device == PCI_DEVICE_ID_TI_1510) {
1251 u8 val;
1252 /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA
1253 signal out the MFUNC0 pin */
1254 if (pci_read_config_byte(dev, 0x8c, &val) == 0)
1255 pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
1256 /* Disable ISA interrupt mode */
1257 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1258 pci_write_config_byte(dev, 0x92, val & ~0x06);
1259 }
1260}
1261
1262DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
1263
1264void pmac_pci_fixup_pciata(struct pci_dev* dev)
1265{
1266 u8 progif = 0;
1267
1268 /*
1269 * On PowerMacs, we try to switch any PCI ATA controller to
1270 * fully native mode
1271 */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001272 if (!machine_is(powermac))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001273 return;
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001274
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001275 /* Some controllers don't have the class IDE */
1276 if (dev->vendor == PCI_VENDOR_ID_PROMISE)
1277 switch(dev->device) {
1278 case PCI_DEVICE_ID_PROMISE_20246:
1279 case PCI_DEVICE_ID_PROMISE_20262:
1280 case PCI_DEVICE_ID_PROMISE_20263:
1281 case PCI_DEVICE_ID_PROMISE_20265:
1282 case PCI_DEVICE_ID_PROMISE_20267:
1283 case PCI_DEVICE_ID_PROMISE_20268:
1284 case PCI_DEVICE_ID_PROMISE_20269:
1285 case PCI_DEVICE_ID_PROMISE_20270:
1286 case PCI_DEVICE_ID_PROMISE_20271:
1287 case PCI_DEVICE_ID_PROMISE_20275:
1288 case PCI_DEVICE_ID_PROMISE_20276:
1289 case PCI_DEVICE_ID_PROMISE_20277:
1290 goto good;
1291 }
1292 /* Others, check PCI class */
1293 if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1294 return;
1295 good:
1296 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1297 if ((progif & 5) != 5) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001298 printk(KERN_INFO "Forcing PCI IDE into native mode: %s\n",
1299 pci_name(dev));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001300 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
1301 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
1302 (progif & 5) != 5)
1303 printk(KERN_ERR "Rewrite of PROGIF failed !\n");
1304 }
1305}
1306DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
1307#endif
1308
1309/*
1310 * Disable second function on K2-SATA, it's broken
1311 * and disable IO BARs on first one
1312 */
1313static void fixup_k2_sata(struct pci_dev* dev)
1314{
1315 int i;
1316 u16 cmd;
1317
1318 if (PCI_FUNC(dev->devfn) > 0) {
1319 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1320 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1321 pci_write_config_word(dev, PCI_COMMAND, cmd);
1322 for (i = 0; i < 6; i++) {
1323 dev->resource[i].start = dev->resource[i].end = 0;
1324 dev->resource[i].flags = 0;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001325 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1326 0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001327 }
1328 } else {
1329 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1330 cmd &= ~PCI_COMMAND_IO;
1331 pci_write_config_word(dev, PCI_COMMAND, cmd);
1332 for (i = 0; i < 5; i++) {
1333 dev->resource[i].start = dev->resource[i].end = 0;
1334 dev->resource[i].flags = 0;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001335 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1336 0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001337 }
1338 }
1339}
1340DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);
1341