blob: 3bc075c788ef3af25bcf06223098cffee2808942 [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>
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -070019#include <linux/irq.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100020
21#include <asm/sections.h>
22#include <asm/io.h>
23#include <asm/prom.h>
24#include <asm/pci-bridge.h>
25#include <asm/machdep.h>
26#include <asm/pmac_feature.h>
Paul Mackerras830825d2005-10-26 17:16:38 +100027#include <asm/grackle.h>
Paul Mackerras3c3f42d2005-10-10 22:58:41 +100028#include <asm/ppc-pci.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100029
30#undef DEBUG
31
32#ifdef DEBUG
33#define DBG(x...) printk(x)
34#else
35#define DBG(x...)
36#endif
37
Paul Mackerras14cf11a2005-09-26 16:04:21 +100038/* XXX Could be per-controller, but I don't think we risk anything by
39 * assuming we won't have both UniNorth and Bandit */
40static int has_uninorth;
Paul Mackerras35499c02005-10-22 16:02:39 +100041#ifdef CONFIG_PPC64
Paul Mackerras14cf11a2005-09-26 16:04:21 +100042static struct pci_controller *u3_agp;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100043#else
44static int has_second_ohare;
Paul Mackerras35499c02005-10-22 16:02:39 +100045#endif /* CONFIG_PPC64 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100046
Paul Mackerras14cf11a2005-09-26 16:04:21 +100047extern int pcibios_assign_bus_offset;
48
49struct device_node *k2_skiplist[2];
50
51/*
52 * Magic constants for enabling cache coherency in the bandit/PSX bridge.
53 */
54#define BANDIT_DEVID_2 8
55#define BANDIT_REVID 3
56
57#define BANDIT_DEVNUM 11
58#define BANDIT_MAGIC 0x50
59#define BANDIT_COHERENT 0x40
60
61static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
62{
63 for (; node != 0;node = node->sibling) {
Jeremy Kerr018a3d12006-07-12 15:40:29 +100064 const int * bus_range;
65 const unsigned int *class_code;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100066 int len;
67
68 /* For PCI<->PCI bridges or CardBus bridges, we go down */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100069 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100070 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
71 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
72 continue;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100073 bus_range = of_get_property(node, "bus-range", &len);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100074 if (bus_range != NULL && len > 2 * sizeof(int)) {
75 if (bus_range[1] > higher)
76 higher = bus_range[1];
77 }
78 higher = fixup_one_level_bus_range(node->child, higher);
79 }
80 return higher;
81}
82
83/* This routine fixes the "bus-range" property of all bridges in the
84 * system since they tend to have their "last" member wrong on macs
85 *
86 * Note that the bus numbers manipulated here are OF bus numbers, they
87 * are not Linux bus numbers.
88 */
89static void __init fixup_bus_range(struct device_node *bridge)
90{
Jeremy Kerr018a3d12006-07-12 15:40:29 +100091 int *bus_range, len;
92 struct property *prop;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100093
94 /* Lookup the "bus-range" property for the hose */
Jeremy Kerr018a3d12006-07-12 15:40:29 +100095 prop = of_find_property(bridge, "bus-range", &len);
96 if (prop == NULL || prop->length < 2 * sizeof(int))
Paul Mackerras14cf11a2005-09-26 16:04:21 +100097 return;
Jeremy Kerr018a3d12006-07-12 15:40:29 +100098
Stephen Rothwell1a381472007-04-03 10:58:52 +100099 bus_range = prop->value;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000100 bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
101}
102
103/*
104 * Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.
105 *
106 * The "Bandit" version is present in all early PCI PowerMacs,
107 * and up to the first ones using Grackle. Some machines may
108 * have 2 bandit controllers (2 PCI busses).
109 *
110 * "Chaos" is used in some "Bandit"-type machines as a bridge
111 * for the separate display bus. It is accessed the same
112 * way as bandit, but cannot be probed for devices. It therefore
113 * has its own config access functions.
114 *
115 * The "UniNorth" version is present in all Core99 machines
116 * (iBook, G4, new IMacs, and all the recent Apple machines).
117 * It contains 3 controllers in one ASIC.
118 *
119 * The U3 is the bridge used on G5 machines. It contains an
120 * AGP bus which is dealt with the old UniNorth access routines
121 * and a HyperTransport bus which uses its own set of access
122 * functions.
123 */
124
125#define MACRISC_CFA0(devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100126 ((1 << (unsigned int)PCI_SLOT(dev_fn)) \
127 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
128 | (((unsigned int)(off)) & 0xFCUL))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000129
130#define MACRISC_CFA1(bus, devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100131 ((((unsigned int)(bus)) << 16) \
132 |(((unsigned int)(devfn)) << 8) \
133 |(((unsigned int)(off)) & 0xFCUL) \
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000134 |1UL)
135
Al Virode125bf2006-02-01 05:18:43 -0500136static volatile void __iomem *macrisc_cfg_access(struct pci_controller* hose,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000137 u8 bus, u8 dev_fn, u8 offset)
138{
139 unsigned int caddr;
140
141 if (bus == hose->first_busno) {
142 if (dev_fn < (11 << 3))
Al Virode125bf2006-02-01 05:18:43 -0500143 return NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000144 caddr = MACRISC_CFA0(dev_fn, offset);
145 } else
146 caddr = MACRISC_CFA1(bus, dev_fn, offset);
147
148 /* Uninorth will return garbage if we don't read back the value ! */
149 do {
150 out_le32(hose->cfg_addr, caddr);
151 } while (in_le32(hose->cfg_addr) != caddr);
152
153 offset &= has_uninorth ? 0x07 : 0x03;
Al Virode125bf2006-02-01 05:18:43 -0500154 return hose->cfg_data + offset;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000155}
156
157static int macrisc_read_config(struct pci_bus *bus, unsigned int devfn,
158 int offset, int len, u32 *val)
159{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000160 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500161 volatile void __iomem *addr;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000162
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000163 hose = pci_bus_to_host(bus);
164 if (hose == NULL)
165 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100166 if (offset >= 0x100)
167 return PCIBIOS_BAD_REGISTER_NUMBER;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000168 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
169 if (!addr)
170 return PCIBIOS_DEVICE_NOT_FOUND;
171 /*
172 * Note: the caller has already checked that offset is
173 * suitably aligned and that len is 1, 2 or 4.
174 */
175 switch (len) {
176 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500177 *val = in_8(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000178 break;
179 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500180 *val = in_le16(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000181 break;
182 default:
Al Virode125bf2006-02-01 05:18:43 -0500183 *val = in_le32(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000184 break;
185 }
186 return PCIBIOS_SUCCESSFUL;
187}
188
189static int macrisc_write_config(struct pci_bus *bus, unsigned int devfn,
190 int offset, int len, u32 val)
191{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000192 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500193 volatile void __iomem *addr;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000194
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000195 hose = pci_bus_to_host(bus);
196 if (hose == NULL)
197 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100198 if (offset >= 0x100)
199 return PCIBIOS_BAD_REGISTER_NUMBER;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000200 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
201 if (!addr)
202 return PCIBIOS_DEVICE_NOT_FOUND;
203 /*
204 * Note: the caller has already checked that offset is
205 * suitably aligned and that len is 1, 2 or 4.
206 */
207 switch (len) {
208 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500209 out_8(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000210 break;
211 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500212 out_le16(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000213 break;
214 default:
Al Virode125bf2006-02-01 05:18:43 -0500215 out_le32(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000216 break;
217 }
218 return PCIBIOS_SUCCESSFUL;
219}
220
221static struct pci_ops macrisc_pci_ops =
222{
Nathan Lynch3fac10e2007-08-10 05:18:41 +1000223 .read = macrisc_read_config,
224 .write = macrisc_write_config,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000225};
226
Paul Mackerras35499c02005-10-22 16:02:39 +1000227#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000228/*
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000229 * Verify that a specific (bus, dev_fn) exists on chaos
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000230 */
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100231static int chaos_validate_dev(struct pci_bus *bus, int devfn, int offset)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000232{
233 struct device_node *np;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000234 const u32 *vendor, *device;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000235
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100236 if (offset >= 0x100)
237 return PCIBIOS_BAD_REGISTER_NUMBER;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000238 np = pci_busdev_to_OF_node(bus, devfn);
239 if (np == NULL)
240 return PCIBIOS_DEVICE_NOT_FOUND;
241
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000242 vendor = of_get_property(np, "vendor-id", NULL);
243 device = of_get_property(np, "device-id", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000244 if (vendor == NULL || device == NULL)
245 return PCIBIOS_DEVICE_NOT_FOUND;
246
247 if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)
248 && (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))
249 return PCIBIOS_BAD_REGISTER_NUMBER;
250
251 return PCIBIOS_SUCCESSFUL;
252}
253
254static int
255chaos_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
256 int len, u32 *val)
257{
258 int result = chaos_validate_dev(bus, devfn, offset);
259 if (result == PCIBIOS_BAD_REGISTER_NUMBER)
260 *val = ~0U;
261 if (result != PCIBIOS_SUCCESSFUL)
262 return result;
263 return macrisc_read_config(bus, devfn, offset, len, val);
264}
265
266static int
267chaos_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
268 int len, u32 val)
269{
270 int result = chaos_validate_dev(bus, devfn, offset);
271 if (result != PCIBIOS_SUCCESSFUL)
272 return result;
273 return macrisc_write_config(bus, devfn, offset, len, val);
274}
275
276static struct pci_ops chaos_pci_ops =
277{
Nathan Lynch3fac10e2007-08-10 05:18:41 +1000278 .read = chaos_read_config,
279 .write = chaos_write_config,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000280};
281
Paul Mackerras35499c02005-10-22 16:02:39 +1000282static void __init setup_chaos(struct pci_controller *hose,
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100283 struct resource *addr)
Paul Mackerras35499c02005-10-22 16:02:39 +1000284{
285 /* assume a `chaos' bridge */
286 hose->ops = &chaos_pci_ops;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100287 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
288 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
Paul Mackerras35499c02005-10-22 16:02:39 +1000289}
Paul Mackerras35499c02005-10-22 16:02:39 +1000290#endif /* CONFIG_PPC32 */
291
292#ifdef CONFIG_PPC64
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000293/*
294 * These versions of U3 HyperTransport config space access ops do not
295 * implement self-view of the HT host yet
296 */
297
298/*
299 * This function deals with some "special cases" devices.
300 *
301 * 0 -> No special case
302 * 1 -> Skip the device but act as if the access was successfull
303 * (return 0xff's on reads, eventually, cache config space
304 * accesses in a later version)
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200305 * -1 -> Hide the device (unsuccessful access)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000306 */
307static int u3_ht_skip_device(struct pci_controller *hose,
308 struct pci_bus *bus, unsigned int devfn)
309{
310 struct device_node *busdn, *dn;
311 int i;
312
313 /* We only allow config cycles to devices that are in OF device-tree
314 * as we are apparently having some weird things going on with some
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100315 * revs of K2 on recent G5s, except for the host bridge itself, which
316 * is missing from the tree but we know we can probe.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000317 */
318 if (bus->self)
319 busdn = pci_device_to_OF_node(bus->self);
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100320 else if (devfn == 0)
321 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000322 else
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100323 busdn = hose->dn;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000324 for (dn = busdn->child; dn; dn = dn->sibling)
linase07102d2005-12-05 19:37:35 -0600325 if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000326 break;
327 if (dn == NULL)
328 return -1;
329
330 /*
331 * When a device in K2 is powered down, we die on config
332 * cycle accesses. Fix that here.
333 */
334 for (i=0; i<2; i++)
335 if (k2_skiplist[i] == dn)
336 return 1;
337
338 return 0;
339}
340
341#define U3_HT_CFA0(devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100342 ((((unsigned int)devfn) << 8) | offset)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000343#define U3_HT_CFA1(bus, devfn, off) \
344 (U3_HT_CFA0(devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100345 + (((unsigned int)bus) << 16) \
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000346 + 0x01000000UL)
347
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100348static void __iomem *u3_ht_cfg_access(struct pci_controller *hose, u8 bus,
349 u8 devfn, u8 offset, int *swap)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000350{
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100351 *swap = 1;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000352 if (bus == hose->first_busno) {
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100353 if (devfn != 0)
354 return hose->cfg_data + U3_HT_CFA0(devfn, offset);
355 *swap = 0;
356 return ((void __iomem *)hose->cfg_addr) + (offset << 2);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000357 } else
Al Virode125bf2006-02-01 05:18:43 -0500358 return hose->cfg_data + U3_HT_CFA1(bus, devfn, offset);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000359}
360
361static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
362 int offset, int len, u32 *val)
363{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000364 struct pci_controller *hose;
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100365 void __iomem *addr;
366 int swap;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000367
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000368 hose = pci_bus_to_host(bus);
369 if (hose == NULL)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000370 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100371 if (offset >= 0x100)
372 return PCIBIOS_BAD_REGISTER_NUMBER;
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100373 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000374 if (!addr)
375 return PCIBIOS_DEVICE_NOT_FOUND;
376
377 switch (u3_ht_skip_device(hose, bus, devfn)) {
378 case 0:
379 break;
380 case 1:
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000381 switch (len) {
382 case 1:
383 *val = 0xff; break;
384 case 2:
385 *val = 0xffff; break;
386 default:
387 *val = 0xfffffffful; break;
388 }
389 return PCIBIOS_SUCCESSFUL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000390 default:
391 return PCIBIOS_DEVICE_NOT_FOUND;
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000392 }
393
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000394 /*
395 * Note: the caller has already checked that offset is
396 * suitably aligned and that len is 1, 2 or 4.
397 */
398 switch (len) {
399 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500400 *val = in_8(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000401 break;
402 case 2:
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100403 *val = swap ? in_le16(addr) : in_be16(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000404 break;
405 default:
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100406 *val = swap ? in_le32(addr) : in_be32(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000407 break;
408 }
409 return PCIBIOS_SUCCESSFUL;
410}
411
412static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
413 int offset, int len, u32 val)
414{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000415 struct pci_controller *hose;
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100416 void __iomem *addr;
417 int swap;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000418
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000419 hose = pci_bus_to_host(bus);
420 if (hose == NULL)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000421 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100422 if (offset >= 0x100)
423 return PCIBIOS_BAD_REGISTER_NUMBER;
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100424 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000425 if (!addr)
426 return PCIBIOS_DEVICE_NOT_FOUND;
427
428 switch (u3_ht_skip_device(hose, bus, devfn)) {
429 case 0:
430 break;
431 case 1:
432 return PCIBIOS_SUCCESSFUL;
433 default:
434 return PCIBIOS_DEVICE_NOT_FOUND;
435 }
436
437 /*
438 * Note: the caller has already checked that offset is
439 * suitably aligned and that len is 1, 2 or 4.
440 */
441 switch (len) {
442 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500443 out_8(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000444 break;
445 case 2:
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100446 swap ? out_le16(addr, val) : out_be16(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000447 break;
448 default:
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100449 swap ? out_le32(addr, val) : out_be32(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000450 break;
451 }
452 return PCIBIOS_SUCCESSFUL;
453}
454
455static struct pci_ops u3_ht_pci_ops =
456{
Nathan Lynch3fac10e2007-08-10 05:18:41 +1000457 .read = u3_ht_read_config,
458 .write = u3_ht_write_config,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000459};
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100460
461#define U4_PCIE_CFA0(devfn, off) \
462 ((1 << ((unsigned int)PCI_SLOT(dev_fn))) \
463 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
464 | ((((unsigned int)(off)) >> 8) << 28) \
465 | (((unsigned int)(off)) & 0xfcU))
466
467#define U4_PCIE_CFA1(bus, devfn, off) \
468 ((((unsigned int)(bus)) << 16) \
469 |(((unsigned int)(devfn)) << 8) \
470 | ((((unsigned int)(off)) >> 8) << 28) \
471 |(((unsigned int)(off)) & 0xfcU) \
472 |1UL)
473
Al Virode125bf2006-02-01 05:18:43 -0500474static volatile void __iomem *u4_pcie_cfg_access(struct pci_controller* hose,
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100475 u8 bus, u8 dev_fn, int offset)
476{
477 unsigned int caddr;
478
479 if (bus == hose->first_busno) {
480 caddr = U4_PCIE_CFA0(dev_fn, offset);
481 } else
482 caddr = U4_PCIE_CFA1(bus, dev_fn, offset);
483
484 /* Uninorth will return garbage if we don't read back the value ! */
485 do {
486 out_le32(hose->cfg_addr, caddr);
487 } while (in_le32(hose->cfg_addr) != caddr);
488
489 offset &= 0x03;
Al Virode125bf2006-02-01 05:18:43 -0500490 return hose->cfg_data + offset;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100491}
492
493static int u4_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
494 int offset, int len, u32 *val)
495{
496 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500497 volatile void __iomem *addr;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100498
499 hose = pci_bus_to_host(bus);
500 if (hose == NULL)
501 return PCIBIOS_DEVICE_NOT_FOUND;
502 if (offset >= 0x1000)
503 return PCIBIOS_BAD_REGISTER_NUMBER;
504 addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
505 if (!addr)
506 return PCIBIOS_DEVICE_NOT_FOUND;
507 /*
508 * Note: the caller has already checked that offset is
509 * suitably aligned and that len is 1, 2 or 4.
510 */
511 switch (len) {
512 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500513 *val = in_8(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100514 break;
515 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500516 *val = in_le16(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100517 break;
518 default:
Al Virode125bf2006-02-01 05:18:43 -0500519 *val = in_le32(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100520 break;
521 }
522 return PCIBIOS_SUCCESSFUL;
523}
524
525static int u4_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
526 int offset, int len, u32 val)
527{
528 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500529 volatile void __iomem *addr;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100530
531 hose = pci_bus_to_host(bus);
532 if (hose == NULL)
533 return PCIBIOS_DEVICE_NOT_FOUND;
534 if (offset >= 0x1000)
535 return PCIBIOS_BAD_REGISTER_NUMBER;
536 addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
537 if (!addr)
538 return PCIBIOS_DEVICE_NOT_FOUND;
539 /*
540 * Note: the caller has already checked that offset is
541 * suitably aligned and that len is 1, 2 or 4.
542 */
543 switch (len) {
544 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500545 out_8(addr, val);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100546 break;
547 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500548 out_le16(addr, val);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100549 break;
550 default:
Al Virode125bf2006-02-01 05:18:43 -0500551 out_le32(addr, val);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100552 break;
553 }
554 return PCIBIOS_SUCCESSFUL;
555}
556
557static struct pci_ops u4_pcie_pci_ops =
558{
Nathan Lynch3fac10e2007-08-10 05:18:41 +1000559 .read = u4_pcie_read_config,
560 .write = u4_pcie_write_config,
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100561};
562
Paul Mackerras35499c02005-10-22 16:02:39 +1000563#endif /* CONFIG_PPC64 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000564
Paul Mackerras35499c02005-10-22 16:02:39 +1000565#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000566/*
567 * For a bandit bridge, turn on cache coherency if necessary.
568 * N.B. we could clean this up using the hose ops directly.
569 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000570static void __init init_bandit(struct pci_controller *bp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000571{
572 unsigned int vendev, magic;
573 int rev;
574
575 /* read the word at offset 0 in config space for device 11 */
576 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);
577 udelay(2);
578 vendev = in_le32(bp->cfg_data);
579 if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +
580 PCI_VENDOR_ID_APPLE) {
581 /* read the revision id */
582 out_le32(bp->cfg_addr,
583 (1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);
584 udelay(2);
585 rev = in_8(bp->cfg_data);
586 if (rev != BANDIT_REVID)
587 printk(KERN_WARNING
588 "Unknown revision %d for bandit\n", rev);
589 } else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {
590 printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);
591 return;
592 }
593
594 /* read the word at offset 0x50 */
595 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);
596 udelay(2);
597 magic = in_le32(bp->cfg_data);
598 if ((magic & BANDIT_COHERENT) != 0)
599 return;
600 magic |= BANDIT_COHERENT;
601 udelay(2);
602 out_le32(bp->cfg_data, magic);
603 printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");
604}
605
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000606/*
607 * Tweak the PCI-PCI bridge chip on the blue & white G3s.
608 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000609static void __init init_p2pbridge(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000610{
611 struct device_node *p2pbridge;
612 struct pci_controller* hose;
613 u8 bus, devfn;
614 u16 val;
615
616 /* XXX it would be better here to identify the specific
617 PCI-PCI bridge chip we have. */
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000618 p2pbridge = of_find_node_by_name(NULL, "pci-bridge");
619 if (p2pbridge == NULL
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000620 || p2pbridge->parent == NULL
621 || strcmp(p2pbridge->parent->name, "pci") != 0)
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000622 goto done;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000623 if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {
624 DBG("Can't find PCI infos for PCI<->PCI bridge\n");
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000625 goto done;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000626 }
627 /* Warning: At this point, we have not yet renumbered all busses.
628 * So we must use OF walking to find out hose
629 */
630 hose = pci_find_hose_for_OF_device(p2pbridge);
631 if (!hose) {
632 DBG("Can't find hose for PCI<->PCI bridge\n");
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000633 goto done;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000634 }
635 if (early_read_config_word(hose, bus, devfn,
636 PCI_BRIDGE_CONTROL, &val) < 0) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100637 printk(KERN_ERR "init_p2pbridge: couldn't read bridge"
638 " control\n");
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000639 goto done;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000640 }
641 val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;
642 early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000643done:
644 of_node_put(p2pbridge);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000645}
646
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000647static void __init init_second_ohare(void)
648{
649 struct device_node *np = of_find_node_by_name(NULL, "pci106b,7");
650 unsigned char bus, devfn;
651 unsigned short cmd;
652
653 if (np == NULL)
654 return;
655
656 /* This must run before we initialize the PICs since the second
657 * ohare hosts a PIC that will be accessed there.
658 */
659 if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
660 struct pci_controller* hose =
661 pci_find_hose_for_OF_device(np);
662 if (!hose) {
663 printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
Nicolas Palixafcb0652008-12-03 00:25:03 +0000664 of_node_put(np);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000665 return;
666 }
667 early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
668 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
669 cmd &= ~PCI_COMMAND_IO;
670 early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
671 }
672 has_second_ohare = 1;
Nicolas Palixafcb0652008-12-03 00:25:03 +0000673 of_node_put(np);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000674}
675
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000676/*
677 * Some Apple desktop machines have a NEC PD720100A USB2 controller
678 * on the motherboard. Open Firmware, on these, will disable the
679 * EHCI part of it so it behaves like a pair of OHCI's. This fixup
680 * code re-enables it ;)
681 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000682static void __init fixup_nec_usb2(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000683{
684 struct device_node *nec;
685
686 for (nec = NULL; (nec = of_find_node_by_name(nec, "usb")) != NULL;) {
687 struct pci_controller *hose;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000688 u32 data;
689 const u32 *prop;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000690 u8 bus, devfn;
Paul Mackerras35499c02005-10-22 16:02:39 +1000691
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000692 prop = of_get_property(nec, "vendor-id", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000693 if (prop == NULL)
694 continue;
695 if (0x1033 != *prop)
696 continue;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000697 prop = of_get_property(nec, "device-id", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000698 if (prop == NULL)
699 continue;
700 if (0x0035 != *prop)
701 continue;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000702 prop = of_get_property(nec, "reg", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000703 if (prop == NULL)
704 continue;
705 devfn = (prop[0] >> 8) & 0xff;
706 bus = (prop[0] >> 16) & 0xff;
707 if (PCI_FUNC(devfn) != 0)
708 continue;
709 hose = pci_find_hose_for_OF_device(nec);
710 if (!hose)
711 continue;
712 early_read_config_dword(hose, bus, devfn, 0xe4, &data);
713 if (data & 1UL) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100714 printk("Found NEC PD720100A USB2 chip with disabled"
715 " EHCI, fixing up...\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000716 data &= ~1UL;
717 early_write_config_dword(hose, bus, devfn, 0xe4, data);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000718 }
719 }
720}
721
Paul Mackerras35499c02005-10-22 16:02:39 +1000722static void __init setup_bandit(struct pci_controller *hose,
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100723 struct resource *addr)
Paul Mackerras35499c02005-10-22 16:02:39 +1000724{
725 hose->ops = &macrisc_pci_ops;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100726 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
727 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
Paul Mackerras35499c02005-10-22 16:02:39 +1000728 init_bandit(hose);
729}
730
731static int __init setup_uninorth(struct pci_controller *hose,
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100732 struct resource *addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000733{
Josh Boyer7fe519c2008-12-11 09:46:44 +0000734 ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000735 has_uninorth = 1;
736 hose->ops = &macrisc_pci_ops;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100737 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
738 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000739 /* We "know" that the bridge at f2000000 has the PCI slots. */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100740 return addr->start == 0xf2000000;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000741}
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100742#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000743
Paul Mackerras35499c02005-10-22 16:02:39 +1000744#ifdef CONFIG_PPC64
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000745static void __init setup_u3_agp(struct pci_controller* hose)
746{
747 /* On G5, we move AGP up to high bus number so we don't need
748 * to reassign bus numbers for HT. If we ever have P2P bridges
Paul Mackerras35499c02005-10-22 16:02:39 +1000749 * on AGP, we'll have to move pci_assign_all_busses to the
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000750 * pci_controller structure so we enable it for AGP and not for
751 * HT childs.
752 * We hard code the address because of the different size of
753 * the reg address cell, we shall fix that by killing struct
754 * reg_property and using some accessor functions instead
755 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000756 hose->first_busno = 0xf0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000757 hose->last_busno = 0xff;
758 has_uninorth = 1;
759 hose->ops = &macrisc_pci_ops;
760 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
761 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000762 u3_agp = hose;
763}
764
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100765static void __init setup_u4_pcie(struct pci_controller* hose)
766{
767 /* We currently only implement the "non-atomic" config space, to
768 * be optimised later.
769 */
770 hose->ops = &u4_pcie_pci_ops;
771 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
772 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
773
774 /* The bus contains a bridge from root -> device, we need to
775 * make it visible on bus 0 so that we pick the right type
776 * of config cycles. If we didn't, we would have to force all
777 * config cycles to be type 1. So we override the "bus-range"
778 * property here
779 */
780 hose->first_busno = 0x00;
781 hose->last_busno = 0xff;
Benjamin Herrenschmidtd0264ce2007-12-20 14:55:02 +1100782}
783
784static void __init parse_region_decode(struct pci_controller *hose,
785 u32 decode)
786{
787 unsigned long base, end, next = -1;
788 int i, cur = -1;
789
790 /* Iterate through all bits. We ignore the last bit as this region is
791 * reserved for the ROM among other niceties
792 */
793 for (i = 0; i < 31; i++) {
794 if ((decode & (0x80000000 >> i)) == 0)
795 continue;
796 if (i < 16) {
797 base = 0xf0000000 | (((u32)i) << 24);
798 end = base + 0x00ffffff;
799 } else {
800 base = ((u32)i-16) << 28;
801 end = base + 0x0fffffff;
802 }
803 if (base != next) {
804 if (++cur >= 3) {
805 printk(KERN_WARNING "PCI: Too many ranges !\n");
806 break;
807 }
808 hose->mem_resources[cur].flags = IORESOURCE_MEM;
809 hose->mem_resources[cur].name = hose->dn->full_name;
810 hose->mem_resources[cur].start = base;
811 hose->mem_resources[cur].end = end;
812 DBG(" %d: 0x%08lx-0x%08lx\n", cur, base, end);
813 } else {
814 DBG(" : -0x%08lx\n", end);
815 hose->mem_resources[cur].end = end;
816 }
817 next = end + 1;
818 }
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100819}
820
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000821static void __init setup_u3_ht(struct pci_controller* hose)
822{
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100823 struct device_node *np = hose->dn;
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100824 struct resource cfg_res, self_res;
Benjamin Herrenschmidtd0264ce2007-12-20 14:55:02 +1100825 u32 decode;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100826
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000827 hose->ops = &u3_ht_pci_ops;
828
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100829 /* Get base addresses from OF tree
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000830 */
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100831 if (of_address_to_resource(np, 0, &cfg_res) ||
832 of_address_to_resource(np, 1, &self_res)) {
833 printk(KERN_ERR "PCI: Failed to get U3/U4 HT resources !\n");
834 return;
835 }
836
837 /* Map external cfg space access into cfg_data and self registers
838 * into cfg_addr
839 */
840 hose->cfg_data = ioremap(cfg_res.start, 0x02000000);
841 hose->cfg_addr = ioremap(self_res.start,
842 self_res.end - self_res.start + 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000843
844 /*
Benjamin Herrenschmidtd0264ce2007-12-20 14:55:02 +1100845 * /ht node doesn't expose a "ranges" property, we read the register
846 * that controls the decoding logic and use that for memory regions.
847 * The IO region is hard coded since it is fixed in HW as well.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000848 */
849 hose->io_base_phys = 0xf4000000;
Paul Mackerras35499c02005-10-22 16:02:39 +1000850 hose->pci_io_size = 0x00400000;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000851 hose->io_resource.name = np->full_name;
852 hose->io_resource.start = 0;
853 hose->io_resource.end = 0x003fffff;
854 hose->io_resource.flags = IORESOURCE_IO;
855 hose->pci_mem_offset = 0;
856 hose->first_busno = 0;
857 hose->last_busno = 0xef;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000858
Benjamin Herrenschmidtd0264ce2007-12-20 14:55:02 +1100859 /* Note: fix offset when cfg_addr becomes a void * */
860 decode = in_be32(hose->cfg_addr + 0x80);
Paul Mackerras35499c02005-10-22 16:02:39 +1000861
Benjamin Herrenschmidtd0264ce2007-12-20 14:55:02 +1100862 DBG("PCI: Apple HT bridge decode register: 0x%08x\n", decode);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100863
Benjamin Herrenschmidtd0264ce2007-12-20 14:55:02 +1100864 /* NOTE: The decode register setup is a bit weird... region
865 * 0xf8000000 for example is marked as enabled in there while it's
866 & actually the memory controller registers.
867 * That means that we are incorrectly attributing it to HT.
868 *
869 * In a similar vein, region 0xf4000000 is actually the HT IO space but
870 * also marked as enabled in here and 0xf9000000 is used by some other
871 * internal bits of the northbridge.
872 *
873 * Unfortunately, we can't just mask out those bit as we would end
874 * up with more regions than we can cope (linux can only cope with
875 * 3 memory regions for a PHB at this stage).
876 *
877 * So for now, we just do a little hack. We happen to -know- that
878 * Apple firmware doesn't assign things below 0xfa000000 for that
879 * bridge anyway so we mask out all bits we don't want.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000880 */
Benjamin Herrenschmidtd0264ce2007-12-20 14:55:02 +1100881 decode &= 0x003fffff;
882
883 /* Now parse the resulting bits and build resources */
884 parse_region_decode(hose, decode);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000885}
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100886#endif /* CONFIG_PPC64 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000887
888/*
889 * We assume that if we have a G3 powermac, we have one bridge called
890 * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
891 * if we have one or more bandit or chaos bridges, we don't have a MPC106.
892 */
Arnd Bergmann09b55f72007-06-18 01:06:54 +0200893static int __init pmac_add_bridge(struct device_node *dev)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000894{
895 int len;
896 struct pci_controller *hose;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100897 struct resource rsrc;
Paul Mackerras35499c02005-10-22 16:02:39 +1000898 char *disp_name;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000899 const int *bus_range;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100900 int primary = 1, has_address = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000901
902 DBG("Adding PCI host bridge %s\n", dev->full_name);
903
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100904 /* Fetch host bridge registers address */
905 has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
906
907 /* Get bus range if any */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000908 bus_range = of_get_property(dev, "bus-range", &len);
Paul Mackerras35499c02005-10-22 16:02:39 +1000909 if (bus_range == NULL || len < 2 * sizeof(int)) {
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100910 printk(KERN_WARNING "Can't get bus-range for %s, assume"
911 " bus 0\n", dev->full_name);
Paul Mackerras35499c02005-10-22 16:02:39 +1000912 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000913
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100914 hose = pcibios_alloc_controller(dev);
Paul Mackerras35499c02005-10-22 16:02:39 +1000915 if (!hose)
916 return -ENOMEM;
Paul Mackerras35499c02005-10-22 16:02:39 +1000917 hose->first_busno = bus_range ? bus_range[0] : 0;
918 hose->last_busno = bus_range ? bus_range[1] : 0xff;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000919
920 disp_name = NULL;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100921
922 /* 64 bits only bridges */
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100923#ifdef CONFIG_PPC64
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000924 if (of_device_is_compatible(dev, "u3-agp")) {
Paul Mackerras35499c02005-10-22 16:02:39 +1000925 setup_u3_agp(hose);
926 disp_name = "U3-AGP";
927 primary = 0;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000928 } else if (of_device_is_compatible(dev, "u3-ht")) {
Paul Mackerras35499c02005-10-22 16:02:39 +1000929 setup_u3_ht(hose);
930 disp_name = "U3-HT";
931 primary = 1;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000932 } else if (of_device_is_compatible(dev, "u4-pcie")) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100933 setup_u4_pcie(hose);
934 disp_name = "U4-PCIE";
935 primary = 0;
Paul Mackerras35499c02005-10-22 16:02:39 +1000936 }
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100937 printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number:"
938 " %d->%d\n", disp_name, hose->first_busno, hose->last_busno);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100939#endif /* CONFIG_PPC64 */
940
941 /* 32 bits only bridges */
942#ifdef CONFIG_PPC32
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000943 if (of_device_is_compatible(dev, "uni-north")) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100944 primary = setup_uninorth(hose, &rsrc);
Paul Mackerras35499c02005-10-22 16:02:39 +1000945 disp_name = "UniNorth";
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000946 } else if (strcmp(dev->name, "pci") == 0) {
Paul Mackerras35499c02005-10-22 16:02:39 +1000947 /* XXX assume this is a mpc106 (grackle) */
948 setup_grackle(hose);
949 disp_name = "Grackle (MPC106)";
950 } else if (strcmp(dev->name, "bandit") == 0) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100951 setup_bandit(hose, &rsrc);
Paul Mackerras35499c02005-10-22 16:02:39 +1000952 disp_name = "Bandit";
953 } else if (strcmp(dev->name, "chaos") == 0) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100954 setup_chaos(hose, &rsrc);
Paul Mackerras35499c02005-10-22 16:02:39 +1000955 disp_name = "Chaos";
956 primary = 0;
957 }
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -0700958 printk(KERN_INFO "Found %s PCI host bridge at 0x%016llx. "
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100959 "Firmware bus number: %d->%d\n",
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -0700960 disp_name, (unsigned long long)rsrc.start, hose->first_busno,
961 hose->last_busno);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100962#endif /* CONFIG_PPC32 */
963
Paul Mackerras35499c02005-10-22 16:02:39 +1000964 DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
965 hose, hose->cfg_addr, hose->cfg_data);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000966
Paul Mackerras35499c02005-10-22 16:02:39 +1000967 /* Interpret the "ranges" property */
968 /* This also maps the I/O region and sets isa_io/mem_base */
969 pci_process_bridge_OF_ranges(hose, dev, primary);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000970
Paul Mackerras35499c02005-10-22 16:02:39 +1000971 /* Fixup "bus-range" OF property */
972 fixup_bus_range(dev);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000973
974 return 0;
975}
976
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +1100977void __devinit pmac_pci_irq_fixup(struct pci_dev *dev)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000978{
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700979#ifdef CONFIG_PPC32
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +1100980 /* Fixup interrupt for the modem/ethernet combo controller.
981 * on machines with a second ohare chip.
982 * The number in the device tree (27) is bogus (correct for
983 * the ethernet-only board but not the combo ethernet/modem
984 * board). The real interrupt is 28 on the second controller
985 * -> 28+32 = 60.
986 */
987 if (has_second_ohare &&
988 dev->vendor == PCI_VENDOR_ID_DEC &&
989 dev->device == PCI_DEVICE_ID_DEC_TULIP_PLUS) {
990 dev->irq = irq_create_mapping(NULL, 60);
991 set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000992 }
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +1100993#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000994}
995
Paul Mackerras35499c02005-10-22 16:02:39 +1000996void __init pmac_pci_init(void)
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000997{
998 struct device_node *np, *root;
999 struct device_node *ht = NULL;
1000
Josh Boyer7fe519c2008-12-11 09:46:44 +00001001 ppc_pci_set_flags(PPC_PCI_CAN_SKIP_ISA_ALIGN);
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001002
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001003 root = of_find_node_by_path("/");
1004 if (root == NULL) {
Paul Mackerras35499c02005-10-22 16:02:39 +10001005 printk(KERN_CRIT "pmac_pci_init: can't find root "
1006 "of device tree\n");
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001007 return;
1008 }
1009 for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
1010 if (np->name == NULL)
1011 continue;
1012 if (strcmp(np->name, "bandit") == 0
1013 || strcmp(np->name, "chaos") == 0
1014 || strcmp(np->name, "pci") == 0) {
Arnd Bergmann09b55f72007-06-18 01:06:54 +02001015 if (pmac_add_bridge(np) == 0)
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001016 of_node_get(np);
1017 }
1018 if (strcmp(np->name, "ht") == 0) {
1019 of_node_get(np);
1020 ht = np;
1021 }
1022 }
1023 of_node_put(root);
1024
Paul Mackerras35499c02005-10-22 16:02:39 +10001025#ifdef CONFIG_PPC64
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001026 /* Probe HT last as it relies on the agp resources to be already
1027 * setup
1028 */
Arnd Bergmann09b55f72007-06-18 01:06:54 +02001029 if (ht && pmac_add_bridge(ht) != 0)
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001030 of_node_put(ht);
1031
Paul Mackerras35499c02005-10-22 16:02:39 +10001032 /* Setup the linkage between OF nodes and PHBs */
1033 pci_devs_phb_init();
1034
1035 /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
1036 * assume there is no P2P bridge on the AGP bus, which should be a
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001037 * safe assumptions for now. We should do something better in the
1038 * future though
Paul Mackerras35499c02005-10-22 16:02:39 +10001039 */
1040 if (u3_agp) {
Stephen Rothwell44ef3392007-12-10 14:33:21 +11001041 struct device_node *np = u3_agp->dn;
Paul Mackerras35499c02005-10-22 16:02:39 +10001042 PCI_DN(np)->busno = 0xf0;
1043 for (np = np->child; np; np = np->sibling)
1044 PCI_DN(np)->busno = 0xf0;
1045 }
Paul Mackerras35499c02005-10-22 16:02:39 +10001046 /* pmac_check_ht_link(); */
1047
Benjamin Herrenschmidt295f83e2007-12-20 14:54:58 +11001048 /* We can allocate missing resources if any */
1049 pci_probe_only = 0;
Paul Mackerras35499c02005-10-22 16:02:39 +10001050
Paul Mackerras35499c02005-10-22 16:02:39 +10001051#else /* CONFIG_PPC64 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001052 init_p2pbridge();
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001053 init_second_ohare();
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001054 fixup_nec_usb2();
Paul Mackerras35499c02005-10-22 16:02:39 +10001055
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001056 /* We are still having some issues with the Xserve G4, enabling
1057 * some offset between bus number and domains for now when we
1058 * assign all busses should help for now
1059 */
Josh Boyer7fe519c2008-12-11 09:46:44 +00001060 if (ppc_pci_has_flag(PPC_PCI_REASSIGN_ALL_BUS))
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001061 pcibios_assign_bus_offset = 0x10;
Paul Mackerras35499c02005-10-22 16:02:39 +10001062#endif
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001063}
1064
Benjamin Herrenschmidtbc0826c2007-12-20 14:54:57 +11001065#ifdef CONFIG_PPC32
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001066int pmac_pci_enable_device_hook(struct pci_dev *dev)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001067{
1068 struct device_node* node;
1069 int updatecfg = 0;
1070 int uninorth_child;
1071
1072 node = pci_device_to_OF_node(dev);
1073
1074 /* We don't want to enable USB controllers absent from the OF tree
1075 * (iBook second controller)
1076 */
1077 if (dev->vendor == PCI_VENDOR_ID_APPLE
Jean Delvarec67808e2006-04-09 20:07:35 +02001078 && dev->class == PCI_CLASS_SERIAL_USB_OHCI
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001079 && !node) {
1080 printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
1081 pci_name(dev));
1082 return -EINVAL;
1083 }
1084
1085 if (!node)
1086 return 0;
1087
1088 uninorth_child = node->parent &&
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001089 of_device_is_compatible(node->parent, "uni-north");
Paul Mackerras35499c02005-10-22 16:02:39 +10001090
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001091 /* Firewire & GMAC were disabled after PCI probe, the driver is
1092 * claiming them, we must re-enable them now.
1093 */
1094 if (uninorth_child && !strcmp(node->name, "firewire") &&
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001095 (of_device_is_compatible(node, "pci106b,18") ||
1096 of_device_is_compatible(node, "pci106b,30") ||
1097 of_device_is_compatible(node, "pci11c1,5811"))) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001098 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
1099 pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
1100 updatecfg = 1;
1101 }
1102 if (uninorth_child && !strcmp(node->name, "ethernet") &&
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001103 of_device_is_compatible(node, "gmac")) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001104 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
1105 updatecfg = 1;
1106 }
1107
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001108 /*
1109 * Fixup various header fields on 32 bits. We don't do that on
1110 * 64 bits as some of these have strange values behind the HT
1111 * bridge and we must not, for example, enable MWI or set the
1112 * cache line size on them.
1113 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001114 if (updatecfg) {
1115 u16 cmd;
Paul Mackerras35499c02005-10-22 16:02:39 +10001116
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001117 pci_read_config_word(dev, PCI_COMMAND, &cmd);
Paul Mackerras35499c02005-10-22 16:02:39 +10001118 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1119 | PCI_COMMAND_INVALIDATE;
1120 pci_write_config_word(dev, PCI_COMMAND, cmd);
1121 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001122
Paul Mackerras35499c02005-10-22 16:02:39 +10001123 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1124 L1_CACHE_BYTES >> 2);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001125 }
1126
1127 return 0;
1128}
1129
Benjamin Herrenschmidtbc0826c2007-12-20 14:54:57 +11001130void __devinit pmac_pci_fixup_ohci(struct pci_dev *dev)
1131{
1132 struct device_node *node = pci_device_to_OF_node(dev);
1133
1134 /* We don't want to assign resources to USB controllers
1135 * absent from the OF tree (iBook second controller)
1136 */
1137 if (dev->class == PCI_CLASS_SERIAL_USB_OHCI && !node)
1138 dev->resource[0].flags = 0;
1139}
1140DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_ANY_ID, pmac_pci_fixup_ohci);
1141
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001142/* We power down some devices after they have been probed. They'll
1143 * be powered back on later on
1144 */
Paul Mackerras35499c02005-10-22 16:02:39 +10001145void __init pmac_pcibios_after_init(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001146{
1147 struct device_node* nd;
1148
Stephen Rothwell30686ba2007-04-24 13:53:04 +10001149 for_each_node_by_name(nd, "firewire") {
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001150 if (nd->parent && (of_device_is_compatible(nd, "pci106b,18") ||
1151 of_device_is_compatible(nd, "pci106b,30") ||
1152 of_device_is_compatible(nd, "pci11c1,5811"))
1153 && of_device_is_compatible(nd->parent, "uni-north")) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001154 pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
1155 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
1156 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001157 }
Stephen Rothwell30686ba2007-04-24 13:53:04 +10001158 for_each_node_by_name(nd, "ethernet") {
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001159 if (nd->parent && of_device_is_compatible(nd, "gmac")
1160 && of_device_is_compatible(nd->parent, "uni-north"))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001161 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001162 }
1163}
1164
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001165void pmac_pci_fixup_cardbus(struct pci_dev* dev)
1166{
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001167 if (!machine_is(powermac))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001168 return;
1169 /*
1170 * Fix the interrupt routing on the various cardbus bridges
1171 * used on powerbooks
1172 */
1173 if (dev->vendor != PCI_VENDOR_ID_TI)
1174 return;
1175 if (dev->device == PCI_DEVICE_ID_TI_1130 ||
1176 dev->device == PCI_DEVICE_ID_TI_1131) {
1177 u8 val;
Paul Mackerras35499c02005-10-22 16:02:39 +10001178 /* Enable PCI interrupt */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001179 if (pci_read_config_byte(dev, 0x91, &val) == 0)
1180 pci_write_config_byte(dev, 0x91, val | 0x30);
1181 /* Disable ISA interrupt mode */
1182 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1183 pci_write_config_byte(dev, 0x92, val & ~0x06);
1184 }
1185 if (dev->device == PCI_DEVICE_ID_TI_1210 ||
1186 dev->device == PCI_DEVICE_ID_TI_1211 ||
1187 dev->device == PCI_DEVICE_ID_TI_1410 ||
1188 dev->device == PCI_DEVICE_ID_TI_1510) {
1189 u8 val;
1190 /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA
1191 signal out the MFUNC0 pin */
1192 if (pci_read_config_byte(dev, 0x8c, &val) == 0)
1193 pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
1194 /* Disable ISA interrupt mode */
1195 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1196 pci_write_config_byte(dev, 0x92, val & ~0x06);
1197 }
1198}
1199
1200DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
1201
1202void pmac_pci_fixup_pciata(struct pci_dev* dev)
1203{
1204 u8 progif = 0;
1205
1206 /*
1207 * On PowerMacs, we try to switch any PCI ATA controller to
1208 * fully native mode
1209 */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001210 if (!machine_is(powermac))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001211 return;
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001212
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001213 /* Some controllers don't have the class IDE */
1214 if (dev->vendor == PCI_VENDOR_ID_PROMISE)
1215 switch(dev->device) {
1216 case PCI_DEVICE_ID_PROMISE_20246:
1217 case PCI_DEVICE_ID_PROMISE_20262:
1218 case PCI_DEVICE_ID_PROMISE_20263:
1219 case PCI_DEVICE_ID_PROMISE_20265:
1220 case PCI_DEVICE_ID_PROMISE_20267:
1221 case PCI_DEVICE_ID_PROMISE_20268:
1222 case PCI_DEVICE_ID_PROMISE_20269:
1223 case PCI_DEVICE_ID_PROMISE_20270:
1224 case PCI_DEVICE_ID_PROMISE_20271:
1225 case PCI_DEVICE_ID_PROMISE_20275:
1226 case PCI_DEVICE_ID_PROMISE_20276:
1227 case PCI_DEVICE_ID_PROMISE_20277:
1228 goto good;
1229 }
1230 /* Others, check PCI class */
1231 if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1232 return;
1233 good:
1234 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1235 if ((progif & 5) != 5) {
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +11001236 printk(KERN_INFO "PCI: %s Forcing PCI IDE into native mode\n",
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001237 pci_name(dev));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001238 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
1239 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
1240 (progif & 5) != 5)
1241 printk(KERN_ERR "Rewrite of PROGIF failed !\n");
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +11001242 else {
1243 /* Clear IO BARs, they will be reassigned */
1244 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0);
1245 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, 0);
1246 pci_write_config_dword(dev, PCI_BASE_ADDRESS_2, 0);
1247 pci_write_config_dword(dev, PCI_BASE_ADDRESS_3, 0);
1248 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001249 }
1250}
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +11001251DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
Benjamin Herrenschmidtbc0826c2007-12-20 14:54:57 +11001252#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001253
1254/*
1255 * Disable second function on K2-SATA, it's broken
1256 * and disable IO BARs on first one
1257 */
1258static void fixup_k2_sata(struct pci_dev* dev)
1259{
1260 int i;
1261 u16 cmd;
1262
1263 if (PCI_FUNC(dev->devfn) > 0) {
1264 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1265 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1266 pci_write_config_word(dev, PCI_COMMAND, cmd);
1267 for (i = 0; i < 6; i++) {
1268 dev->resource[i].start = dev->resource[i].end = 0;
1269 dev->resource[i].flags = 0;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001270 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1271 0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001272 }
1273 } else {
1274 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1275 cmd &= ~PCI_COMMAND_IO;
1276 pci_write_config_word(dev, PCI_COMMAND, cmd);
1277 for (i = 0; i < 5; i++) {
1278 dev->resource[i].start = dev->resource[i].end = 0;
1279 dev->resource[i].flags = 0;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001280 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1281 0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001282 }
1283 }
1284}
1285DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);
1286
Benjamin Herrenschmidtcede3932009-08-31 21:34:36 +00001287/*
1288 * On U4 (aka CPC945) the PCIe root complex "P2P" bridge resource ranges aren't
1289 * configured by the firmware. The bridge itself seems to ignore them but it
1290 * causes problems with Linux which then re-assigns devices below the bridge,
1291 * thus changing addresses of those devices from what was in the device-tree,
1292 * which sucks when those are video cards using offb
1293 *
1294 * We could just mark it transparent but I prefer fixing up the resources to
1295 * properly show what's going on here, as I have some doubts about having them
1296 * badly configured potentially being an issue for DMA.
1297 *
1298 * We leave PIO alone, it seems to be fine
1299 *
1300 * Oh and there's another funny bug. The OF properties advertize the region
1301 * 0xf1000000..0xf1ffffff as being forwarded as memory space. But that's
1302 * actually not true, this region is the memory mapped config space. So we
1303 * also need to filter it out or we'll map things in the wrong place.
1304 */
1305static void fixup_u4_pcie(struct pci_dev* dev)
1306{
1307 struct pci_controller *host = pci_bus_to_host(dev->bus);
1308 struct resource *region = NULL;
1309 u32 reg;
1310 int i;
1311
1312 /* Only do that on PowerMac */
1313 if (!machine_is(powermac))
1314 return;
1315
1316 /* Find the largest MMIO region */
1317 for (i = 0; i < 3; i++) {
1318 struct resource *r = &host->mem_resources[i];
1319 if (!(r->flags & IORESOURCE_MEM))
1320 continue;
1321 /* Skip the 0xf0xxxxxx..f2xxxxxx regions, we know they
1322 * are reserved by HW for other things
1323 */
1324 if (r->start >= 0xf0000000 && r->start < 0xf3000000)
1325 continue;
1326 if (!region || (r->end - r->start) >
1327 (region->end - region->start))
1328 region = r;
1329 }
1330 /* Nothing found, bail */
1331 if (region == 0)
1332 return;
1333
1334 /* Print things out */
1335 printk(KERN_INFO "PCI: Fixup U4 PCIe bridge range: %pR\n", region);
1336
1337 /* Fixup bridge config space. We know it's a Mac, resource aren't
1338 * offset so let's just blast them as-is. We also know that they
1339 * fit in 32 bits
1340 */
1341 reg = ((region->start >> 16) & 0xfff0) | (region->end & 0xfff00000);
1342 pci_write_config_dword(dev, PCI_MEMORY_BASE, reg);
1343 pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0);
1344 pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
1345 pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0);
1346}
1347DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_U4_PCIE, fixup_u4_pcie);