blob: 6c93e7c0da152d0678a1ba26c53c809caa65c14d [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 Herrenschmidt1beb6a72005-12-14 13:10:10 +110043static struct pci_controller *u4_pcie;
Paul Mackerras35499c02005-10-22 16:02:39 +100044static struct pci_controller *u3_ht;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100045#else
46static int has_second_ohare;
Paul Mackerras35499c02005-10-22 16:02:39 +100047#endif /* CONFIG_PPC64 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048
Paul Mackerras14cf11a2005-09-26 16:04:21 +100049extern int pcibios_assign_bus_offset;
50
51struct device_node *k2_skiplist[2];
52
53/*
54 * Magic constants for enabling cache coherency in the bandit/PSX bridge.
55 */
56#define BANDIT_DEVID_2 8
57#define BANDIT_REVID 3
58
59#define BANDIT_DEVNUM 11
60#define BANDIT_MAGIC 0x50
61#define BANDIT_COHERENT 0x40
62
63static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
64{
65 for (; node != 0;node = node->sibling) {
Jeremy Kerr018a3d12006-07-12 15:40:29 +100066 const int * bus_range;
67 const unsigned int *class_code;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100068 int len;
69
70 /* For PCI<->PCI bridges or CardBus bridges, we go down */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100071 class_code = of_get_property(node, "class-code", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100072 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
73 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
74 continue;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100075 bus_range = of_get_property(node, "bus-range", &len);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100076 if (bus_range != NULL && len > 2 * sizeof(int)) {
77 if (bus_range[1] > higher)
78 higher = bus_range[1];
79 }
80 higher = fixup_one_level_bus_range(node->child, higher);
81 }
82 return higher;
83}
84
85/* This routine fixes the "bus-range" property of all bridges in the
86 * system since they tend to have their "last" member wrong on macs
87 *
88 * Note that the bus numbers manipulated here are OF bus numbers, they
89 * are not Linux bus numbers.
90 */
91static void __init fixup_bus_range(struct device_node *bridge)
92{
Jeremy Kerr018a3d12006-07-12 15:40:29 +100093 int *bus_range, len;
94 struct property *prop;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100095
96 /* Lookup the "bus-range" property for the hose */
Jeremy Kerr018a3d12006-07-12 15:40:29 +100097 prop = of_find_property(bridge, "bus-range", &len);
98 if (prop == NULL || prop->length < 2 * sizeof(int))
Paul Mackerras14cf11a2005-09-26 16:04:21 +100099 return;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000100
Stephen Rothwell1a381472007-04-03 10:58:52 +1000101 bus_range = prop->value;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000102 bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
103}
104
105/*
106 * Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.
107 *
108 * The "Bandit" version is present in all early PCI PowerMacs,
109 * and up to the first ones using Grackle. Some machines may
110 * have 2 bandit controllers (2 PCI busses).
111 *
112 * "Chaos" is used in some "Bandit"-type machines as a bridge
113 * for the separate display bus. It is accessed the same
114 * way as bandit, but cannot be probed for devices. It therefore
115 * has its own config access functions.
116 *
117 * The "UniNorth" version is present in all Core99 machines
118 * (iBook, G4, new IMacs, and all the recent Apple machines).
119 * It contains 3 controllers in one ASIC.
120 *
121 * The U3 is the bridge used on G5 machines. It contains an
122 * AGP bus which is dealt with the old UniNorth access routines
123 * and a HyperTransport bus which uses its own set of access
124 * functions.
125 */
126
127#define MACRISC_CFA0(devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100128 ((1 << (unsigned int)PCI_SLOT(dev_fn)) \
129 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
130 | (((unsigned int)(off)) & 0xFCUL))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000131
132#define MACRISC_CFA1(bus, devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100133 ((((unsigned int)(bus)) << 16) \
134 |(((unsigned int)(devfn)) << 8) \
135 |(((unsigned int)(off)) & 0xFCUL) \
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000136 |1UL)
137
Al Virode125bf2006-02-01 05:18:43 -0500138static volatile void __iomem *macrisc_cfg_access(struct pci_controller* hose,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000139 u8 bus, u8 dev_fn, u8 offset)
140{
141 unsigned int caddr;
142
143 if (bus == hose->first_busno) {
144 if (dev_fn < (11 << 3))
Al Virode125bf2006-02-01 05:18:43 -0500145 return NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000146 caddr = MACRISC_CFA0(dev_fn, offset);
147 } else
148 caddr = MACRISC_CFA1(bus, dev_fn, offset);
149
150 /* Uninorth will return garbage if we don't read back the value ! */
151 do {
152 out_le32(hose->cfg_addr, caddr);
153 } while (in_le32(hose->cfg_addr) != caddr);
154
155 offset &= has_uninorth ? 0x07 : 0x03;
Al Virode125bf2006-02-01 05:18:43 -0500156 return hose->cfg_data + offset;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000157}
158
159static int macrisc_read_config(struct pci_bus *bus, unsigned int devfn,
160 int offset, int len, u32 *val)
161{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000162 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500163 volatile void __iomem *addr;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000164
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000165 hose = pci_bus_to_host(bus);
166 if (hose == NULL)
167 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100168 if (offset >= 0x100)
169 return PCIBIOS_BAD_REGISTER_NUMBER;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000170 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
171 if (!addr)
172 return PCIBIOS_DEVICE_NOT_FOUND;
173 /*
174 * Note: the caller has already checked that offset is
175 * suitably aligned and that len is 1, 2 or 4.
176 */
177 switch (len) {
178 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500179 *val = in_8(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000180 break;
181 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500182 *val = in_le16(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000183 break;
184 default:
Al Virode125bf2006-02-01 05:18:43 -0500185 *val = in_le32(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000186 break;
187 }
188 return PCIBIOS_SUCCESSFUL;
189}
190
191static int macrisc_write_config(struct pci_bus *bus, unsigned int devfn,
192 int offset, int len, u32 val)
193{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000194 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500195 volatile void __iomem *addr;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000196
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000197 hose = pci_bus_to_host(bus);
198 if (hose == NULL)
199 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100200 if (offset >= 0x100)
201 return PCIBIOS_BAD_REGISTER_NUMBER;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000202 addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
203 if (!addr)
204 return PCIBIOS_DEVICE_NOT_FOUND;
205 /*
206 * Note: the caller has already checked that offset is
207 * suitably aligned and that len is 1, 2 or 4.
208 */
209 switch (len) {
210 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500211 out_8(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000212 break;
213 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500214 out_le16(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000215 break;
216 default:
Al Virode125bf2006-02-01 05:18:43 -0500217 out_le32(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000218 break;
219 }
220 return PCIBIOS_SUCCESSFUL;
221}
222
223static struct pci_ops macrisc_pci_ops =
224{
Nathan Lynch3fac10e2007-08-10 05:18:41 +1000225 .read = macrisc_read_config,
226 .write = macrisc_write_config,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000227};
228
Paul Mackerras35499c02005-10-22 16:02:39 +1000229#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000230/*
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000231 * Verify that a specific (bus, dev_fn) exists on chaos
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000232 */
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100233static int chaos_validate_dev(struct pci_bus *bus, int devfn, int offset)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000234{
235 struct device_node *np;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000236 const u32 *vendor, *device;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000237
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100238 if (offset >= 0x100)
239 return PCIBIOS_BAD_REGISTER_NUMBER;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000240 np = pci_busdev_to_OF_node(bus, devfn);
241 if (np == NULL)
242 return PCIBIOS_DEVICE_NOT_FOUND;
243
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000244 vendor = of_get_property(np, "vendor-id", NULL);
245 device = of_get_property(np, "device-id", NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000246 if (vendor == NULL || device == NULL)
247 return PCIBIOS_DEVICE_NOT_FOUND;
248
249 if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)
250 && (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))
251 return PCIBIOS_BAD_REGISTER_NUMBER;
252
253 return PCIBIOS_SUCCESSFUL;
254}
255
256static int
257chaos_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
258 int len, u32 *val)
259{
260 int result = chaos_validate_dev(bus, devfn, offset);
261 if (result == PCIBIOS_BAD_REGISTER_NUMBER)
262 *val = ~0U;
263 if (result != PCIBIOS_SUCCESSFUL)
264 return result;
265 return macrisc_read_config(bus, devfn, offset, len, val);
266}
267
268static int
269chaos_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
270 int len, u32 val)
271{
272 int result = chaos_validate_dev(bus, devfn, offset);
273 if (result != PCIBIOS_SUCCESSFUL)
274 return result;
275 return macrisc_write_config(bus, devfn, offset, len, val);
276}
277
278static struct pci_ops chaos_pci_ops =
279{
Nathan Lynch3fac10e2007-08-10 05:18:41 +1000280 .read = chaos_read_config,
281 .write = chaos_write_config,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000282};
283
Paul Mackerras35499c02005-10-22 16:02:39 +1000284static void __init setup_chaos(struct pci_controller *hose,
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100285 struct resource *addr)
Paul Mackerras35499c02005-10-22 16:02:39 +1000286{
287 /* assume a `chaos' bridge */
288 hose->ops = &chaos_pci_ops;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100289 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
290 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
Paul Mackerras35499c02005-10-22 16:02:39 +1000291}
Paul Mackerras35499c02005-10-22 16:02:39 +1000292#endif /* CONFIG_PPC32 */
293
294#ifdef CONFIG_PPC64
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000295/*
296 * These versions of U3 HyperTransport config space access ops do not
297 * implement self-view of the HT host yet
298 */
299
300/*
301 * This function deals with some "special cases" devices.
302 *
303 * 0 -> No special case
304 * 1 -> Skip the device but act as if the access was successfull
305 * (return 0xff's on reads, eventually, cache config space
306 * accesses in a later version)
307 * -1 -> Hide the device (unsuccessful acess)
308 */
309static int u3_ht_skip_device(struct pci_controller *hose,
310 struct pci_bus *bus, unsigned int devfn)
311{
312 struct device_node *busdn, *dn;
313 int i;
314
315 /* We only allow config cycles to devices that are in OF device-tree
316 * as we are apparently having some weird things going on with some
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100317 * revs of K2 on recent G5s, except for the host bridge itself, which
318 * is missing from the tree but we know we can probe.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000319 */
320 if (bus->self)
321 busdn = pci_device_to_OF_node(bus->self);
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100322 else if (devfn == 0)
323 return 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000324 else
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100325 busdn = hose->dn;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000326 for (dn = busdn->child; dn; dn = dn->sibling)
linase07102d2005-12-05 19:37:35 -0600327 if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000328 break;
329 if (dn == NULL)
330 return -1;
331
332 /*
333 * When a device in K2 is powered down, we die on config
334 * cycle accesses. Fix that here.
335 */
336 for (i=0; i<2; i++)
337 if (k2_skiplist[i] == dn)
338 return 1;
339
340 return 0;
341}
342
343#define U3_HT_CFA0(devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100344 ((((unsigned int)devfn) << 8) | offset)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000345#define U3_HT_CFA1(bus, devfn, off) \
346 (U3_HT_CFA0(devfn, off) \
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100347 + (((unsigned int)bus) << 16) \
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000348 + 0x01000000UL)
349
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100350static void __iomem *u3_ht_cfg_access(struct pci_controller *hose, u8 bus,
351 u8 devfn, u8 offset, int *swap)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000352{
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100353 *swap = 1;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000354 if (bus == hose->first_busno) {
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100355 if (devfn != 0)
356 return hose->cfg_data + U3_HT_CFA0(devfn, offset);
357 *swap = 0;
358 return ((void __iomem *)hose->cfg_addr) + (offset << 2);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000359 } else
Al Virode125bf2006-02-01 05:18:43 -0500360 return hose->cfg_data + U3_HT_CFA1(bus, devfn, offset);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000361}
362
363static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
364 int offset, int len, u32 *val)
365{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000366 struct pci_controller *hose;
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100367 void __iomem *addr;
368 int swap;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000369
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000370 hose = pci_bus_to_host(bus);
371 if (hose == NULL)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000372 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100373 if (offset >= 0x100)
374 return PCIBIOS_BAD_REGISTER_NUMBER;
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100375 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000376 if (!addr)
377 return PCIBIOS_DEVICE_NOT_FOUND;
378
379 switch (u3_ht_skip_device(hose, bus, devfn)) {
380 case 0:
381 break;
382 case 1:
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000383 switch (len) {
384 case 1:
385 *val = 0xff; break;
386 case 2:
387 *val = 0xffff; break;
388 default:
389 *val = 0xfffffffful; break;
390 }
391 return PCIBIOS_SUCCESSFUL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000392 default:
393 return PCIBIOS_DEVICE_NOT_FOUND;
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000394 }
395
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000396 /*
397 * Note: the caller has already checked that offset is
398 * suitably aligned and that len is 1, 2 or 4.
399 */
400 switch (len) {
401 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500402 *val = in_8(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000403 break;
404 case 2:
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100405 *val = swap ? in_le16(addr) : in_be16(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000406 break;
407 default:
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100408 *val = swap ? in_le32(addr) : in_be32(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000409 break;
410 }
411 return PCIBIOS_SUCCESSFUL;
412}
413
414static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
415 int offset, int len, u32 val)
416{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000417 struct pci_controller *hose;
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100418 void __iomem *addr;
419 int swap;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000420
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000421 hose = pci_bus_to_host(bus);
422 if (hose == NULL)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000423 return PCIBIOS_DEVICE_NOT_FOUND;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100424 if (offset >= 0x100)
425 return PCIBIOS_BAD_REGISTER_NUMBER;
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100426 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000427 if (!addr)
428 return PCIBIOS_DEVICE_NOT_FOUND;
429
430 switch (u3_ht_skip_device(hose, bus, devfn)) {
431 case 0:
432 break;
433 case 1:
434 return PCIBIOS_SUCCESSFUL;
435 default:
436 return PCIBIOS_DEVICE_NOT_FOUND;
437 }
438
439 /*
440 * Note: the caller has already checked that offset is
441 * suitably aligned and that len is 1, 2 or 4.
442 */
443 switch (len) {
444 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500445 out_8(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000446 break;
447 case 2:
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100448 swap ? out_le16(addr, val) : out_be16(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000449 break;
450 default:
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100451 swap ? out_le32(addr, val) : out_be32(addr, val);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000452 break;
453 }
454 return PCIBIOS_SUCCESSFUL;
455}
456
457static struct pci_ops u3_ht_pci_ops =
458{
Nathan Lynch3fac10e2007-08-10 05:18:41 +1000459 .read = u3_ht_read_config,
460 .write = u3_ht_write_config,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000461};
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100462
463#define U4_PCIE_CFA0(devfn, off) \
464 ((1 << ((unsigned int)PCI_SLOT(dev_fn))) \
465 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
466 | ((((unsigned int)(off)) >> 8) << 28) \
467 | (((unsigned int)(off)) & 0xfcU))
468
469#define U4_PCIE_CFA1(bus, devfn, off) \
470 ((((unsigned int)(bus)) << 16) \
471 |(((unsigned int)(devfn)) << 8) \
472 | ((((unsigned int)(off)) >> 8) << 28) \
473 |(((unsigned int)(off)) & 0xfcU) \
474 |1UL)
475
Al Virode125bf2006-02-01 05:18:43 -0500476static volatile void __iomem *u4_pcie_cfg_access(struct pci_controller* hose,
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100477 u8 bus, u8 dev_fn, int offset)
478{
479 unsigned int caddr;
480
481 if (bus == hose->first_busno) {
482 caddr = U4_PCIE_CFA0(dev_fn, offset);
483 } else
484 caddr = U4_PCIE_CFA1(bus, dev_fn, offset);
485
486 /* Uninorth will return garbage if we don't read back the value ! */
487 do {
488 out_le32(hose->cfg_addr, caddr);
489 } while (in_le32(hose->cfg_addr) != caddr);
490
491 offset &= 0x03;
Al Virode125bf2006-02-01 05:18:43 -0500492 return hose->cfg_data + offset;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100493}
494
495static int u4_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
496 int offset, int len, u32 *val)
497{
498 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500499 volatile void __iomem *addr;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100500
501 hose = pci_bus_to_host(bus);
502 if (hose == NULL)
503 return PCIBIOS_DEVICE_NOT_FOUND;
504 if (offset >= 0x1000)
505 return PCIBIOS_BAD_REGISTER_NUMBER;
506 addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
507 if (!addr)
508 return PCIBIOS_DEVICE_NOT_FOUND;
509 /*
510 * Note: the caller has already checked that offset is
511 * suitably aligned and that len is 1, 2 or 4.
512 */
513 switch (len) {
514 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500515 *val = in_8(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100516 break;
517 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500518 *val = in_le16(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100519 break;
520 default:
Al Virode125bf2006-02-01 05:18:43 -0500521 *val = in_le32(addr);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100522 break;
523 }
524 return PCIBIOS_SUCCESSFUL;
525}
526
527static int u4_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
528 int offset, int len, u32 val)
529{
530 struct pci_controller *hose;
Al Virode125bf2006-02-01 05:18:43 -0500531 volatile void __iomem *addr;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100532
533 hose = pci_bus_to_host(bus);
534 if (hose == NULL)
535 return PCIBIOS_DEVICE_NOT_FOUND;
536 if (offset >= 0x1000)
537 return PCIBIOS_BAD_REGISTER_NUMBER;
538 addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
539 if (!addr)
540 return PCIBIOS_DEVICE_NOT_FOUND;
541 /*
542 * Note: the caller has already checked that offset is
543 * suitably aligned and that len is 1, 2 or 4.
544 */
545 switch (len) {
546 case 1:
Al Virode125bf2006-02-01 05:18:43 -0500547 out_8(addr, val);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100548 break;
549 case 2:
Al Virode125bf2006-02-01 05:18:43 -0500550 out_le16(addr, val);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100551 break;
552 default:
Al Virode125bf2006-02-01 05:18:43 -0500553 out_le32(addr, val);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100554 break;
555 }
556 return PCIBIOS_SUCCESSFUL;
557}
558
559static struct pci_ops u4_pcie_pci_ops =
560{
Nathan Lynch3fac10e2007-08-10 05:18:41 +1000561 .read = u4_pcie_read_config,
562 .write = u4_pcie_write_config,
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100563};
564
Paul Mackerras35499c02005-10-22 16:02:39 +1000565#endif /* CONFIG_PPC64 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000566
Paul Mackerras35499c02005-10-22 16:02:39 +1000567#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000568/*
569 * For a bandit bridge, turn on cache coherency if necessary.
570 * N.B. we could clean this up using the hose ops directly.
571 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000572static void __init init_bandit(struct pci_controller *bp)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000573{
574 unsigned int vendev, magic;
575 int rev;
576
577 /* read the word at offset 0 in config space for device 11 */
578 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);
579 udelay(2);
580 vendev = in_le32(bp->cfg_data);
581 if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +
582 PCI_VENDOR_ID_APPLE) {
583 /* read the revision id */
584 out_le32(bp->cfg_addr,
585 (1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);
586 udelay(2);
587 rev = in_8(bp->cfg_data);
588 if (rev != BANDIT_REVID)
589 printk(KERN_WARNING
590 "Unknown revision %d for bandit\n", rev);
591 } else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {
592 printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);
593 return;
594 }
595
596 /* read the word at offset 0x50 */
597 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);
598 udelay(2);
599 magic = in_le32(bp->cfg_data);
600 if ((magic & BANDIT_COHERENT) != 0)
601 return;
602 magic |= BANDIT_COHERENT;
603 udelay(2);
604 out_le32(bp->cfg_data, magic);
605 printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");
606}
607
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000608/*
609 * Tweak the PCI-PCI bridge chip on the blue & white G3s.
610 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000611static void __init init_p2pbridge(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000612{
613 struct device_node *p2pbridge;
614 struct pci_controller* hose;
615 u8 bus, devfn;
616 u16 val;
617
618 /* XXX it would be better here to identify the specific
619 PCI-PCI bridge chip we have. */
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000620 p2pbridge = of_find_node_by_name(NULL, "pci-bridge");
621 if (p2pbridge == NULL
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000622 || p2pbridge->parent == NULL
623 || strcmp(p2pbridge->parent->name, "pci") != 0)
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000624 goto done;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000625 if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {
626 DBG("Can't find PCI infos for PCI<->PCI bridge\n");
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000627 goto done;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000628 }
629 /* Warning: At this point, we have not yet renumbered all busses.
630 * So we must use OF walking to find out hose
631 */
632 hose = pci_find_hose_for_OF_device(p2pbridge);
633 if (!hose) {
634 DBG("Can't find hose for PCI<->PCI bridge\n");
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000635 goto done;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000636 }
637 if (early_read_config_word(hose, bus, devfn,
638 PCI_BRIDGE_CONTROL, &val) < 0) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100639 printk(KERN_ERR "init_p2pbridge: couldn't read bridge"
640 " control\n");
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000641 goto done;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000642 }
643 val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;
644 early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000645done:
646 of_node_put(p2pbridge);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000647}
648
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000649static void __init init_second_ohare(void)
650{
651 struct device_node *np = of_find_node_by_name(NULL, "pci106b,7");
652 unsigned char bus, devfn;
653 unsigned short cmd;
654
655 if (np == NULL)
656 return;
657
658 /* This must run before we initialize the PICs since the second
659 * ohare hosts a PIC that will be accessed there.
660 */
661 if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
662 struct pci_controller* hose =
663 pci_find_hose_for_OF_device(np);
664 if (!hose) {
665 printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
666 return;
667 }
668 early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
669 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
670 cmd &= ~PCI_COMMAND_IO;
671 early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
672 }
673 has_second_ohare = 1;
674}
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{
Benjamin Herrenschmidtfc3fb712007-12-20 14:54:46 +1100734 ppc_pci_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;
782 u4_pcie = hose;
783}
784
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000785static void __init setup_u3_ht(struct pci_controller* hose)
786{
Stephen Rothwell44ef3392007-12-10 14:33:21 +1100787 struct device_node *np = hose->dn;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100788 struct pci_controller *other = NULL;
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100789 struct resource cfg_res, self_res;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000790 int i, cur;
791
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100792
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000793 hose->ops = &u3_ht_pci_ops;
794
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100795 /* Get base addresses from OF tree
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000796 */
Benjamin Herrenschmidt444532d2007-12-20 14:55:01 +1100797 if (of_address_to_resource(np, 0, &cfg_res) ||
798 of_address_to_resource(np, 1, &self_res)) {
799 printk(KERN_ERR "PCI: Failed to get U3/U4 HT resources !\n");
800 return;
801 }
802
803 /* Map external cfg space access into cfg_data and self registers
804 * into cfg_addr
805 */
806 hose->cfg_data = ioremap(cfg_res.start, 0x02000000);
807 hose->cfg_addr = ioremap(self_res.start,
808 self_res.end - self_res.start + 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000809
810 /*
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100811 * /ht node doesn't expose a "ranges" property, so we "remove"
812 * regions that have been allocated to AGP. So far, this version of
813 * the code doesn't assign any of the 0xfxxxxxxx "fine" memory regions
814 * to /ht. We need to fix that sooner or later by either parsing all
815 * child "ranges" properties or figuring out the U3 address space
816 * decoding logic and then read its configuration register (if any).
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000817 */
818 hose->io_base_phys = 0xf4000000;
Paul Mackerras35499c02005-10-22 16:02:39 +1000819 hose->pci_io_size = 0x00400000;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000820 hose->io_resource.name = np->full_name;
821 hose->io_resource.start = 0;
822 hose->io_resource.end = 0x003fffff;
823 hose->io_resource.flags = IORESOURCE_IO;
824 hose->pci_mem_offset = 0;
825 hose->first_busno = 0;
826 hose->last_busno = 0xef;
827 hose->mem_resources[0].name = np->full_name;
828 hose->mem_resources[0].start = 0x80000000;
829 hose->mem_resources[0].end = 0xefffffff;
830 hose->mem_resources[0].flags = IORESOURCE_MEM;
831
Paul Mackerras35499c02005-10-22 16:02:39 +1000832 u3_ht = hose;
833
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100834 if (u3_agp != NULL)
835 other = u3_agp;
836 else if (u4_pcie != NULL)
837 other = u4_pcie;
838
839 if (other == NULL) {
840 DBG("U3/4 has no AGP/PCIE, using full resource range\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000841 return;
842 }
843
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100844 /* Fixup bus range vs. PCIE */
845 if (u4_pcie)
846 hose->last_busno = u4_pcie->first_busno - 1;
847
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100848 /* We "remove" the AGP resources from the resources allocated to HT,
849 * that is we create "holes". However, that code does assumptions
850 * that so far happen to be true (cross fingers...), typically that
851 * resources in the AGP node are properly ordered
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000852 */
853 cur = 0;
854 for (i=0; i<3; i++) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100855 struct resource *res = &other->mem_resources[i];
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000856 if (res->flags != IORESOURCE_MEM)
857 continue;
858 /* We don't care about "fine" resources */
859 if (res->start >= 0xf0000000)
860 continue;
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100861 /* Check if it's just a matter of "shrinking" us in one
862 * direction
863 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000864 if (hose->mem_resources[cur].start == res->start) {
865 DBG("U3/HT: shrink start of %d, %08lx -> %08lx\n",
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100866 cur, hose->mem_resources[cur].start,
867 res->end + 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000868 hose->mem_resources[cur].start = res->end + 1;
869 continue;
870 }
871 if (hose->mem_resources[cur].end == res->end) {
872 DBG("U3/HT: shrink end of %d, %08lx -> %08lx\n",
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100873 cur, hose->mem_resources[cur].end,
874 res->start - 1);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000875 hose->mem_resources[cur].end = res->start - 1;
876 continue;
877 }
878 /* No, it's not the case, we need a hole */
879 if (cur == 2) {
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100880 /* not enough resources for a hole, we drop part
881 * of the range
882 */
883 printk(KERN_WARNING "Running out of resources"
884 " for /ht host !\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000885 hose->mem_resources[cur].end = res->start - 1;
886 continue;
Paul Mackerras35499c02005-10-22 16:02:39 +1000887 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000888 cur++;
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000889 DBG("U3/HT: hole, %d end at %08lx, %d start at %08lx\n",
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000890 cur-1, res->start - 1, cur, res->end + 1);
891 hose->mem_resources[cur].name = np->full_name;
892 hose->mem_resources[cur].flags = IORESOURCE_MEM;
893 hose->mem_resources[cur].start = res->end + 1;
894 hose->mem_resources[cur].end = hose->mem_resources[cur-1].end;
895 hose->mem_resources[cur-1].end = res->start - 1;
896 }
897}
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100898#endif /* CONFIG_PPC64 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000899
900/*
901 * We assume that if we have a G3 powermac, we have one bridge called
902 * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
903 * if we have one or more bandit or chaos bridges, we don't have a MPC106.
904 */
Arnd Bergmann09b55f72007-06-18 01:06:54 +0200905static int __init pmac_add_bridge(struct device_node *dev)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000906{
907 int len;
908 struct pci_controller *hose;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100909 struct resource rsrc;
Paul Mackerras35499c02005-10-22 16:02:39 +1000910 char *disp_name;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000911 const int *bus_range;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100912 int primary = 1, has_address = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000913
914 DBG("Adding PCI host bridge %s\n", dev->full_name);
915
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100916 /* Fetch host bridge registers address */
917 has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
918
919 /* Get bus range if any */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000920 bus_range = of_get_property(dev, "bus-range", &len);
Paul Mackerras35499c02005-10-22 16:02:39 +1000921 if (bus_range == NULL || len < 2 * sizeof(int)) {
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100922 printk(KERN_WARNING "Can't get bus-range for %s, assume"
923 " bus 0\n", dev->full_name);
Paul Mackerras35499c02005-10-22 16:02:39 +1000924 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000925
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100926 hose = pcibios_alloc_controller(dev);
Paul Mackerras35499c02005-10-22 16:02:39 +1000927 if (!hose)
928 return -ENOMEM;
Paul Mackerras35499c02005-10-22 16:02:39 +1000929 hose->first_busno = bus_range ? bus_range[0] : 0;
930 hose->last_busno = bus_range ? bus_range[1] : 0xff;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000931
932 disp_name = NULL;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100933
934 /* 64 bits only bridges */
Benjamin Herrenschmidtb5166cc2005-11-15 16:05:33 +1100935#ifdef CONFIG_PPC64
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000936 if (of_device_is_compatible(dev, "u3-agp")) {
Paul Mackerras35499c02005-10-22 16:02:39 +1000937 setup_u3_agp(hose);
938 disp_name = "U3-AGP";
939 primary = 0;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000940 } else if (of_device_is_compatible(dev, "u3-ht")) {
Paul Mackerras35499c02005-10-22 16:02:39 +1000941 setup_u3_ht(hose);
942 disp_name = "U3-HT";
943 primary = 1;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000944 } else if (of_device_is_compatible(dev, "u4-pcie")) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100945 setup_u4_pcie(hose);
946 disp_name = "U4-PCIE";
947 primary = 0;
Paul Mackerras35499c02005-10-22 16:02:39 +1000948 }
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100949 printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number:"
950 " %d->%d\n", disp_name, hose->first_busno, hose->last_busno);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100951#endif /* CONFIG_PPC64 */
952
953 /* 32 bits only bridges */
954#ifdef CONFIG_PPC32
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000955 if (of_device_is_compatible(dev, "uni-north")) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100956 primary = setup_uninorth(hose, &rsrc);
Paul Mackerras35499c02005-10-22 16:02:39 +1000957 disp_name = "UniNorth";
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000958 } else if (strcmp(dev->name, "pci") == 0) {
Paul Mackerras35499c02005-10-22 16:02:39 +1000959 /* XXX assume this is a mpc106 (grackle) */
960 setup_grackle(hose);
961 disp_name = "Grackle (MPC106)";
962 } else if (strcmp(dev->name, "bandit") == 0) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100963 setup_bandit(hose, &rsrc);
Paul Mackerras35499c02005-10-22 16:02:39 +1000964 disp_name = "Bandit";
965 } else if (strcmp(dev->name, "chaos") == 0) {
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100966 setup_chaos(hose, &rsrc);
Paul Mackerras35499c02005-10-22 16:02:39 +1000967 disp_name = "Chaos";
968 primary = 0;
969 }
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700970 printk(KERN_INFO "Found %s PCI host bridge at 0x%016llx. "
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100971 "Firmware bus number: %d->%d\n",
Greg Kroah-Hartman685143a2006-06-12 15:18:31 -0700972 disp_name, (unsigned long long)rsrc.start, hose->first_busno,
973 hose->last_busno);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100974#endif /* CONFIG_PPC32 */
975
Paul Mackerras35499c02005-10-22 16:02:39 +1000976 DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
977 hose, hose->cfg_addr, hose->cfg_data);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000978
Paul Mackerras35499c02005-10-22 16:02:39 +1000979 /* Interpret the "ranges" property */
980 /* This also maps the I/O region and sets isa_io/mem_base */
981 pci_process_bridge_OF_ranges(hose, dev, primary);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000982
Paul Mackerras35499c02005-10-22 16:02:39 +1000983 /* Fixup "bus-range" OF property */
984 fixup_bus_range(dev);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000985
986 return 0;
987}
988
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +1100989void __devinit pmac_pci_irq_fixup(struct pci_dev *dev)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000990{
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700991#ifdef CONFIG_PPC32
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +1100992 /* Fixup interrupt for the modem/ethernet combo controller.
993 * on machines with a second ohare chip.
994 * The number in the device tree (27) is bogus (correct for
995 * the ethernet-only board but not the combo ethernet/modem
996 * board). The real interrupt is 28 on the second controller
997 * -> 28+32 = 60.
998 */
999 if (has_second_ohare &&
1000 dev->vendor == PCI_VENDOR_ID_DEC &&
1001 dev->device == PCI_DEVICE_ID_DEC_TULIP_PLUS) {
1002 dev->irq = irq_create_mapping(NULL, 60);
1003 set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001004 }
Benjamin Herrenschmidtf90bb152006-11-11 17:24:51 +11001005#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001006}
1007
Paul Mackerras35499c02005-10-22 16:02:39 +10001008void __init pmac_pci_init(void)
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001009{
1010 struct device_node *np, *root;
1011 struct device_node *ht = NULL;
1012
Benjamin Herrenschmidtfc3fb712007-12-20 14:54:46 +11001013 ppc_pci_flags = PPC_PCI_CAN_SKIP_ISA_ALIGN;
Benjamin Herrenschmidt3fd94c62007-12-20 14:54:53 +11001014
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001015 root = of_find_node_by_path("/");
1016 if (root == NULL) {
Paul Mackerras35499c02005-10-22 16:02:39 +10001017 printk(KERN_CRIT "pmac_pci_init: can't find root "
1018 "of device tree\n");
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001019 return;
1020 }
1021 for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
1022 if (np->name == NULL)
1023 continue;
1024 if (strcmp(np->name, "bandit") == 0
1025 || strcmp(np->name, "chaos") == 0
1026 || strcmp(np->name, "pci") == 0) {
Arnd Bergmann09b55f72007-06-18 01:06:54 +02001027 if (pmac_add_bridge(np) == 0)
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001028 of_node_get(np);
1029 }
1030 if (strcmp(np->name, "ht") == 0) {
1031 of_node_get(np);
1032 ht = np;
1033 }
1034 }
1035 of_node_put(root);
1036
Paul Mackerras35499c02005-10-22 16:02:39 +10001037#ifdef CONFIG_PPC64
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001038 /* Probe HT last as it relies on the agp resources to be already
1039 * setup
1040 */
Arnd Bergmann09b55f72007-06-18 01:06:54 +02001041 if (ht && pmac_add_bridge(ht) != 0)
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001042 of_node_put(ht);
1043
Paul Mackerras35499c02005-10-22 16:02:39 +10001044 /* Setup the linkage between OF nodes and PHBs */
1045 pci_devs_phb_init();
1046
1047 /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
1048 * assume there is no P2P bridge on the AGP bus, which should be a
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001049 * safe assumptions for now. We should do something better in the
1050 * future though
Paul Mackerras35499c02005-10-22 16:02:39 +10001051 */
1052 if (u3_agp) {
Stephen Rothwell44ef3392007-12-10 14:33:21 +11001053 struct device_node *np = u3_agp->dn;
Paul Mackerras35499c02005-10-22 16:02:39 +10001054 PCI_DN(np)->busno = 0xf0;
1055 for (np = np->child; np; np = np->sibling)
1056 PCI_DN(np)->busno = 0xf0;
1057 }
Paul Mackerras35499c02005-10-22 16:02:39 +10001058 /* pmac_check_ht_link(); */
1059
Benjamin Herrenschmidt295f83e2007-12-20 14:54:58 +11001060 /* We can allocate missing resources if any */
1061 pci_probe_only = 0;
Paul Mackerras35499c02005-10-22 16:02:39 +10001062
Paul Mackerras35499c02005-10-22 16:02:39 +10001063#else /* CONFIG_PPC64 */
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001064 init_p2pbridge();
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001065 init_second_ohare();
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001066 fixup_nec_usb2();
Paul Mackerras35499c02005-10-22 16:02:39 +10001067
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001068 /* We are still having some issues with the Xserve G4, enabling
1069 * some offset between bus number and domains for now when we
1070 * assign all busses should help for now
1071 */
Benjamin Herrenschmidtfc3fb712007-12-20 14:54:46 +11001072 if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001073 pcibios_assign_bus_offset = 0x10;
Paul Mackerras35499c02005-10-22 16:02:39 +10001074#endif
Paul Mackerras3c3f42d2005-10-10 22:58:41 +10001075}
1076
Benjamin Herrenschmidtbc0826c2007-12-20 14:54:57 +11001077#ifdef CONFIG_PPC32
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001078int pmac_pci_enable_device_hook(struct pci_dev *dev)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001079{
1080 struct device_node* node;
1081 int updatecfg = 0;
1082 int uninorth_child;
1083
1084 node = pci_device_to_OF_node(dev);
1085
1086 /* We don't want to enable USB controllers absent from the OF tree
1087 * (iBook second controller)
1088 */
1089 if (dev->vendor == PCI_VENDOR_ID_APPLE
Jean Delvarec67808e2006-04-09 20:07:35 +02001090 && dev->class == PCI_CLASS_SERIAL_USB_OHCI
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001091 && !node) {
1092 printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
1093 pci_name(dev));
1094 return -EINVAL;
1095 }
1096
1097 if (!node)
1098 return 0;
1099
1100 uninorth_child = node->parent &&
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001101 of_device_is_compatible(node->parent, "uni-north");
Paul Mackerras35499c02005-10-22 16:02:39 +10001102
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001103 /* Firewire & GMAC were disabled after PCI probe, the driver is
1104 * claiming them, we must re-enable them now.
1105 */
1106 if (uninorth_child && !strcmp(node->name, "firewire") &&
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001107 (of_device_is_compatible(node, "pci106b,18") ||
1108 of_device_is_compatible(node, "pci106b,30") ||
1109 of_device_is_compatible(node, "pci11c1,5811"))) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001110 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
1111 pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
1112 updatecfg = 1;
1113 }
1114 if (uninorth_child && !strcmp(node->name, "ethernet") &&
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001115 of_device_is_compatible(node, "gmac")) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001116 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
1117 updatecfg = 1;
1118 }
1119
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001120 /*
1121 * Fixup various header fields on 32 bits. We don't do that on
1122 * 64 bits as some of these have strange values behind the HT
1123 * bridge and we must not, for example, enable MWI or set the
1124 * cache line size on them.
1125 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001126 if (updatecfg) {
1127 u16 cmd;
Paul Mackerras35499c02005-10-22 16:02:39 +10001128
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001129 pci_read_config_word(dev, PCI_COMMAND, &cmd);
Paul Mackerras35499c02005-10-22 16:02:39 +10001130 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1131 | PCI_COMMAND_INVALIDATE;
1132 pci_write_config_word(dev, PCI_COMMAND, cmd);
1133 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
Benjamin Herrenschmidt549beb92007-12-20 14:54:57 +11001134
Paul Mackerras35499c02005-10-22 16:02:39 +10001135 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1136 L1_CACHE_BYTES >> 2);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001137 }
1138
1139 return 0;
1140}
1141
Benjamin Herrenschmidtbc0826c2007-12-20 14:54:57 +11001142void __devinit pmac_pci_fixup_ohci(struct pci_dev *dev)
1143{
1144 struct device_node *node = pci_device_to_OF_node(dev);
1145
1146 /* We don't want to assign resources to USB controllers
1147 * absent from the OF tree (iBook second controller)
1148 */
1149 if (dev->class == PCI_CLASS_SERIAL_USB_OHCI && !node)
1150 dev->resource[0].flags = 0;
1151}
1152DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_ANY_ID, pmac_pci_fixup_ohci);
1153
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001154/* We power down some devices after they have been probed. They'll
1155 * be powered back on later on
1156 */
Paul Mackerras35499c02005-10-22 16:02:39 +10001157void __init pmac_pcibios_after_init(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001158{
1159 struct device_node* nd;
1160
1161#ifdef CONFIG_BLK_DEV_IDE
1162 struct pci_dev *dev = NULL;
1163
1164 /* OF fails to initialize IDE controllers on macs
1165 * (and maybe other machines)
1166 *
1167 * Ideally, this should be moved to the IDE layer, but we need
1168 * to check specifically with Andre Hedrick how to do it cleanly
1169 * since the common IDE code seem to care about the fact that the
1170 * BIOS may have disabled a controller.
1171 *
1172 * -- BenH
1173 */
1174 for_each_pci_dev(dev) {
Benjamin Herrenschmidtb5d99e62007-03-07 11:27:55 +01001175 if ((dev->class >> 16) != PCI_BASE_CLASS_STORAGE)
1176 continue;
1177 if (pci_enable_device(dev))
1178 printk(KERN_WARNING
1179 "pci: Failed to enable %s\n", pci_name(dev));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001180 }
1181#endif /* CONFIG_BLK_DEV_IDE */
1182
Stephen Rothwell30686ba2007-04-24 13:53:04 +10001183 for_each_node_by_name(nd, "firewire") {
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001184 if (nd->parent && (of_device_is_compatible(nd, "pci106b,18") ||
1185 of_device_is_compatible(nd, "pci106b,30") ||
1186 of_device_is_compatible(nd, "pci11c1,5811"))
1187 && of_device_is_compatible(nd->parent, "uni-north")) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001188 pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
1189 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
1190 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001191 }
Stephen Rothwell30686ba2007-04-24 13:53:04 +10001192 of_node_put(nd);
1193 for_each_node_by_name(nd, "ethernet") {
Stephen Rothwell55b61fe2007-05-03 17:26:52 +10001194 if (nd->parent && of_device_is_compatible(nd, "gmac")
1195 && of_device_is_compatible(nd->parent, "uni-north"))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001196 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001197 }
Stephen Rothwell30686ba2007-04-24 13:53:04 +10001198 of_node_put(nd);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001199}
1200
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001201void pmac_pci_fixup_cardbus(struct pci_dev* dev)
1202{
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001203 if (!machine_is(powermac))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001204 return;
1205 /*
1206 * Fix the interrupt routing on the various cardbus bridges
1207 * used on powerbooks
1208 */
1209 if (dev->vendor != PCI_VENDOR_ID_TI)
1210 return;
1211 if (dev->device == PCI_DEVICE_ID_TI_1130 ||
1212 dev->device == PCI_DEVICE_ID_TI_1131) {
1213 u8 val;
Paul Mackerras35499c02005-10-22 16:02:39 +10001214 /* Enable PCI interrupt */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001215 if (pci_read_config_byte(dev, 0x91, &val) == 0)
1216 pci_write_config_byte(dev, 0x91, val | 0x30);
1217 /* Disable ISA interrupt mode */
1218 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1219 pci_write_config_byte(dev, 0x92, val & ~0x06);
1220 }
1221 if (dev->device == PCI_DEVICE_ID_TI_1210 ||
1222 dev->device == PCI_DEVICE_ID_TI_1211 ||
1223 dev->device == PCI_DEVICE_ID_TI_1410 ||
1224 dev->device == PCI_DEVICE_ID_TI_1510) {
1225 u8 val;
1226 /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA
1227 signal out the MFUNC0 pin */
1228 if (pci_read_config_byte(dev, 0x8c, &val) == 0)
1229 pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
1230 /* Disable ISA interrupt mode */
1231 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1232 pci_write_config_byte(dev, 0x92, val & ~0x06);
1233 }
1234}
1235
1236DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
1237
1238void pmac_pci_fixup_pciata(struct pci_dev* dev)
1239{
1240 u8 progif = 0;
1241
1242 /*
1243 * On PowerMacs, we try to switch any PCI ATA controller to
1244 * fully native mode
1245 */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001246 if (!machine_is(powermac))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001247 return;
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001248
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001249 /* Some controllers don't have the class IDE */
1250 if (dev->vendor == PCI_VENDOR_ID_PROMISE)
1251 switch(dev->device) {
1252 case PCI_DEVICE_ID_PROMISE_20246:
1253 case PCI_DEVICE_ID_PROMISE_20262:
1254 case PCI_DEVICE_ID_PROMISE_20263:
1255 case PCI_DEVICE_ID_PROMISE_20265:
1256 case PCI_DEVICE_ID_PROMISE_20267:
1257 case PCI_DEVICE_ID_PROMISE_20268:
1258 case PCI_DEVICE_ID_PROMISE_20269:
1259 case PCI_DEVICE_ID_PROMISE_20270:
1260 case PCI_DEVICE_ID_PROMISE_20271:
1261 case PCI_DEVICE_ID_PROMISE_20275:
1262 case PCI_DEVICE_ID_PROMISE_20276:
1263 case PCI_DEVICE_ID_PROMISE_20277:
1264 goto good;
1265 }
1266 /* Others, check PCI class */
1267 if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1268 return;
1269 good:
1270 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1271 if ((progif & 5) != 5) {
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +11001272 printk(KERN_INFO "PCI: %s Forcing PCI IDE into native mode\n",
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001273 pci_name(dev));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001274 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
1275 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
1276 (progif & 5) != 5)
1277 printk(KERN_ERR "Rewrite of PROGIF failed !\n");
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +11001278 else {
1279 /* Clear IO BARs, they will be reassigned */
1280 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0);
1281 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, 0);
1282 pci_write_config_dword(dev, PCI_BASE_ADDRESS_2, 0);
1283 pci_write_config_dword(dev, PCI_BASE_ADDRESS_3, 0);
1284 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001285 }
1286}
Benjamin Herrenschmidt6d98bda2007-12-10 15:29:22 +11001287DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
Benjamin Herrenschmidtbc0826c2007-12-20 14:54:57 +11001288#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001289
1290/*
1291 * Disable second function on K2-SATA, it's broken
1292 * and disable IO BARs on first one
1293 */
1294static void fixup_k2_sata(struct pci_dev* dev)
1295{
1296 int i;
1297 u16 cmd;
1298
1299 if (PCI_FUNC(dev->devfn) > 0) {
1300 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1301 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1302 pci_write_config_word(dev, PCI_COMMAND, cmd);
1303 for (i = 0; i < 6; i++) {
1304 dev->resource[i].start = dev->resource[i].end = 0;
1305 dev->resource[i].flags = 0;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001306 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1307 0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001308 }
1309 } else {
1310 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1311 cmd &= ~PCI_COMMAND_IO;
1312 pci_write_config_word(dev, PCI_COMMAND, cmd);
1313 for (i = 0; i < 5; i++) {
1314 dev->resource[i].start = dev->resource[i].end = 0;
1315 dev->resource[i].flags = 0;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001316 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1317 0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001318 }
1319 }
1320}
1321DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);
1322