pci.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Support for PCI bridges found on Power Macintoshes.
  4. *
  5. * Copyright (C) 2003-2005 Benjamin Herrenschmuidt ([email protected])
  6. * Copyright (C) 1997 Paul Mackerras ([email protected])
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/pci.h>
  10. #include <linux/delay.h>
  11. #include <linux/string.h>
  12. #include <linux/init.h>
  13. #include <linux/irq.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/of_pci.h>
  17. #include <asm/sections.h>
  18. #include <asm/io.h>
  19. #include <asm/pci-bridge.h>
  20. #include <asm/machdep.h>
  21. #include <asm/pmac_feature.h>
  22. #include <asm/grackle.h>
  23. #include <asm/ppc-pci.h>
  24. #include "pmac.h"
  25. #undef DEBUG
  26. #ifdef DEBUG
  27. #define DBG(x...) printk(x)
  28. #else
  29. #define DBG(x...)
  30. #endif
  31. /* XXX Could be per-controller, but I don't think we risk anything by
  32. * assuming we won't have both UniNorth and Bandit */
  33. static int has_uninorth;
  34. #ifdef CONFIG_PPC64
  35. static struct pci_controller *u3_agp;
  36. #else
  37. static int has_second_ohare;
  38. #endif /* CONFIG_PPC64 */
  39. extern int pcibios_assign_bus_offset;
  40. struct device_node *k2_skiplist[2];
  41. /*
  42. * Magic constants for enabling cache coherency in the bandit/PSX bridge.
  43. */
  44. #define BANDIT_DEVID_2 8
  45. #define BANDIT_REVID 3
  46. #define BANDIT_DEVNUM 11
  47. #define BANDIT_MAGIC 0x50
  48. #define BANDIT_COHERENT 0x40
  49. static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
  50. {
  51. for (; node; node = node->sibling) {
  52. const int * bus_range;
  53. const unsigned int *class_code;
  54. int len;
  55. /* For PCI<->PCI bridges or CardBus bridges, we go down */
  56. class_code = of_get_property(node, "class-code", NULL);
  57. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  58. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  59. continue;
  60. bus_range = of_get_property(node, "bus-range", &len);
  61. if (bus_range != NULL && len > 2 * sizeof(int)) {
  62. if (bus_range[1] > higher)
  63. higher = bus_range[1];
  64. }
  65. higher = fixup_one_level_bus_range(node->child, higher);
  66. }
  67. return higher;
  68. }
  69. /* This routine fixes the "bus-range" property of all bridges in the
  70. * system since they tend to have their "last" member wrong on macs
  71. *
  72. * Note that the bus numbers manipulated here are OF bus numbers, they
  73. * are not Linux bus numbers.
  74. */
  75. static void __init fixup_bus_range(struct device_node *bridge)
  76. {
  77. int *bus_range, len;
  78. struct property *prop;
  79. /* Lookup the "bus-range" property for the hose */
  80. prop = of_find_property(bridge, "bus-range", &len);
  81. if (prop == NULL || prop->length < 2 * sizeof(int))
  82. return;
  83. bus_range = prop->value;
  84. bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
  85. }
  86. /*
  87. * Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.
  88. *
  89. * The "Bandit" version is present in all early PCI PowerMacs,
  90. * and up to the first ones using Grackle. Some machines may
  91. * have 2 bandit controllers (2 PCI busses).
  92. *
  93. * "Chaos" is used in some "Bandit"-type machines as a bridge
  94. * for the separate display bus. It is accessed the same
  95. * way as bandit, but cannot be probed for devices. It therefore
  96. * has its own config access functions.
  97. *
  98. * The "UniNorth" version is present in all Core99 machines
  99. * (iBook, G4, new IMacs, and all the recent Apple machines).
  100. * It contains 3 controllers in one ASIC.
  101. *
  102. * The U3 is the bridge used on G5 machines. It contains an
  103. * AGP bus which is dealt with the old UniNorth access routines
  104. * and a HyperTransport bus which uses its own set of access
  105. * functions.
  106. */
  107. #define MACRISC_CFA0(devfn, off) \
  108. ((1 << (unsigned int)PCI_SLOT(dev_fn)) \
  109. | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
  110. | (((unsigned int)(off)) & 0xFCUL))
  111. #define MACRISC_CFA1(bus, devfn, off) \
  112. ((((unsigned int)(bus)) << 16) \
  113. |(((unsigned int)(devfn)) << 8) \
  114. |(((unsigned int)(off)) & 0xFCUL) \
  115. |1UL)
  116. static void __iomem *macrisc_cfg_map_bus(struct pci_bus *bus,
  117. unsigned int dev_fn,
  118. int offset)
  119. {
  120. unsigned int caddr;
  121. struct pci_controller *hose;
  122. hose = pci_bus_to_host(bus);
  123. if (hose == NULL)
  124. return NULL;
  125. if (bus->number == hose->first_busno) {
  126. if (dev_fn < (11 << 3))
  127. return NULL;
  128. caddr = MACRISC_CFA0(dev_fn, offset);
  129. } else
  130. caddr = MACRISC_CFA1(bus->number, dev_fn, offset);
  131. /* Uninorth will return garbage if we don't read back the value ! */
  132. do {
  133. out_le32(hose->cfg_addr, caddr);
  134. } while (in_le32(hose->cfg_addr) != caddr);
  135. offset &= has_uninorth ? 0x07 : 0x03;
  136. return hose->cfg_data + offset;
  137. }
  138. static struct pci_ops macrisc_pci_ops =
  139. {
  140. .map_bus = macrisc_cfg_map_bus,
  141. .read = pci_generic_config_read,
  142. .write = pci_generic_config_write,
  143. };
  144. #ifdef CONFIG_PPC32
  145. /*
  146. * Verify that a specific (bus, dev_fn) exists on chaos
  147. */
  148. static void __iomem *chaos_map_bus(struct pci_bus *bus, unsigned int devfn,
  149. int offset)
  150. {
  151. struct device_node *np;
  152. const u32 *vendor, *device;
  153. if (offset >= 0x100)
  154. return NULL;
  155. np = of_pci_find_child_device(bus->dev.of_node, devfn);
  156. if (np == NULL)
  157. return NULL;
  158. vendor = of_get_property(np, "vendor-id", NULL);
  159. device = of_get_property(np, "device-id", NULL);
  160. if (vendor == NULL || device == NULL)
  161. return NULL;
  162. if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)
  163. && (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))
  164. return NULL;
  165. return macrisc_cfg_map_bus(bus, devfn, offset);
  166. }
  167. static struct pci_ops chaos_pci_ops =
  168. {
  169. .map_bus = chaos_map_bus,
  170. .read = pci_generic_config_read,
  171. .write = pci_generic_config_write,
  172. };
  173. static void __init setup_chaos(struct pci_controller *hose,
  174. struct resource *addr)
  175. {
  176. /* assume a `chaos' bridge */
  177. hose->ops = &chaos_pci_ops;
  178. hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
  179. hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
  180. }
  181. #endif /* CONFIG_PPC32 */
  182. #ifdef CONFIG_PPC64
  183. /*
  184. * These versions of U3 HyperTransport config space access ops do not
  185. * implement self-view of the HT host yet
  186. */
  187. /*
  188. * This function deals with some "special cases" devices.
  189. *
  190. * 0 -> No special case
  191. * 1 -> Skip the device but act as if the access was successful
  192. * (return 0xff's on reads, eventually, cache config space
  193. * accesses in a later version)
  194. * -1 -> Hide the device (unsuccessful access)
  195. */
  196. static int u3_ht_skip_device(struct pci_controller *hose,
  197. struct pci_bus *bus, unsigned int devfn)
  198. {
  199. struct device_node *busdn, *dn;
  200. int i;
  201. /* We only allow config cycles to devices that are in OF device-tree
  202. * as we are apparently having some weird things going on with some
  203. * revs of K2 on recent G5s, except for the host bridge itself, which
  204. * is missing from the tree but we know we can probe.
  205. */
  206. if (bus->self)
  207. busdn = pci_device_to_OF_node(bus->self);
  208. else if (devfn == 0)
  209. return 0;
  210. else
  211. busdn = hose->dn;
  212. for (dn = busdn->child; dn; dn = dn->sibling)
  213. if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)
  214. break;
  215. if (dn == NULL)
  216. return -1;
  217. /*
  218. * When a device in K2 is powered down, we die on config
  219. * cycle accesses. Fix that here.
  220. */
  221. for (i=0; i<2; i++)
  222. if (k2_skiplist[i] == dn)
  223. return 1;
  224. return 0;
  225. }
  226. #define U3_HT_CFA0(devfn, off) \
  227. ((((unsigned int)devfn) << 8) | offset)
  228. #define U3_HT_CFA1(bus, devfn, off) \
  229. (U3_HT_CFA0(devfn, off) \
  230. + (((unsigned int)bus) << 16) \
  231. + 0x01000000UL)
  232. static void __iomem *u3_ht_cfg_access(struct pci_controller *hose, u8 bus,
  233. u8 devfn, u8 offset, int *swap)
  234. {
  235. *swap = 1;
  236. if (bus == hose->first_busno) {
  237. if (devfn != 0)
  238. return hose->cfg_data + U3_HT_CFA0(devfn, offset);
  239. *swap = 0;
  240. return ((void __iomem *)hose->cfg_addr) + (offset << 2);
  241. } else
  242. return hose->cfg_data + U3_HT_CFA1(bus, devfn, offset);
  243. }
  244. static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
  245. int offset, int len, u32 *val)
  246. {
  247. struct pci_controller *hose;
  248. void __iomem *addr;
  249. int swap;
  250. hose = pci_bus_to_host(bus);
  251. if (hose == NULL)
  252. return PCIBIOS_DEVICE_NOT_FOUND;
  253. if (offset >= 0x100)
  254. return PCIBIOS_BAD_REGISTER_NUMBER;
  255. addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
  256. if (!addr)
  257. return PCIBIOS_DEVICE_NOT_FOUND;
  258. switch (u3_ht_skip_device(hose, bus, devfn)) {
  259. case 0:
  260. break;
  261. case 1:
  262. switch (len) {
  263. case 1:
  264. *val = 0xff; break;
  265. case 2:
  266. *val = 0xffff; break;
  267. default:
  268. *val = 0xfffffffful; break;
  269. }
  270. return PCIBIOS_SUCCESSFUL;
  271. default:
  272. return PCIBIOS_DEVICE_NOT_FOUND;
  273. }
  274. /*
  275. * Note: the caller has already checked that offset is
  276. * suitably aligned and that len is 1, 2 or 4.
  277. */
  278. switch (len) {
  279. case 1:
  280. *val = in_8(addr);
  281. break;
  282. case 2:
  283. *val = swap ? in_le16(addr) : in_be16(addr);
  284. break;
  285. default:
  286. *val = swap ? in_le32(addr) : in_be32(addr);
  287. break;
  288. }
  289. return PCIBIOS_SUCCESSFUL;
  290. }
  291. static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
  292. int offset, int len, u32 val)
  293. {
  294. struct pci_controller *hose;
  295. void __iomem *addr;
  296. int swap;
  297. hose = pci_bus_to_host(bus);
  298. if (hose == NULL)
  299. return PCIBIOS_DEVICE_NOT_FOUND;
  300. if (offset >= 0x100)
  301. return PCIBIOS_BAD_REGISTER_NUMBER;
  302. addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
  303. if (!addr)
  304. return PCIBIOS_DEVICE_NOT_FOUND;
  305. switch (u3_ht_skip_device(hose, bus, devfn)) {
  306. case 0:
  307. break;
  308. case 1:
  309. return PCIBIOS_SUCCESSFUL;
  310. default:
  311. return PCIBIOS_DEVICE_NOT_FOUND;
  312. }
  313. /*
  314. * Note: the caller has already checked that offset is
  315. * suitably aligned and that len is 1, 2 or 4.
  316. */
  317. switch (len) {
  318. case 1:
  319. out_8(addr, val);
  320. break;
  321. case 2:
  322. swap ? out_le16(addr, val) : out_be16(addr, val);
  323. break;
  324. default:
  325. swap ? out_le32(addr, val) : out_be32(addr, val);
  326. break;
  327. }
  328. return PCIBIOS_SUCCESSFUL;
  329. }
  330. static struct pci_ops u3_ht_pci_ops =
  331. {
  332. .read = u3_ht_read_config,
  333. .write = u3_ht_write_config,
  334. };
  335. #define U4_PCIE_CFA0(devfn, off) \
  336. ((1 << ((unsigned int)PCI_SLOT(dev_fn))) \
  337. | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
  338. | ((((unsigned int)(off)) >> 8) << 28) \
  339. | (((unsigned int)(off)) & 0xfcU))
  340. #define U4_PCIE_CFA1(bus, devfn, off) \
  341. ((((unsigned int)(bus)) << 16) \
  342. |(((unsigned int)(devfn)) << 8) \
  343. | ((((unsigned int)(off)) >> 8) << 28) \
  344. |(((unsigned int)(off)) & 0xfcU) \
  345. |1UL)
  346. static void __iomem *u4_pcie_cfg_map_bus(struct pci_bus *bus,
  347. unsigned int dev_fn,
  348. int offset)
  349. {
  350. struct pci_controller *hose;
  351. unsigned int caddr;
  352. if (offset >= 0x1000)
  353. return NULL;
  354. hose = pci_bus_to_host(bus);
  355. if (!hose)
  356. return NULL;
  357. if (bus->number == hose->first_busno) {
  358. caddr = U4_PCIE_CFA0(dev_fn, offset);
  359. } else
  360. caddr = U4_PCIE_CFA1(bus->number, dev_fn, offset);
  361. /* Uninorth will return garbage if we don't read back the value ! */
  362. do {
  363. out_le32(hose->cfg_addr, caddr);
  364. } while (in_le32(hose->cfg_addr) != caddr);
  365. offset &= 0x03;
  366. return hose->cfg_data + offset;
  367. }
  368. static struct pci_ops u4_pcie_pci_ops =
  369. {
  370. .map_bus = u4_pcie_cfg_map_bus,
  371. .read = pci_generic_config_read,
  372. .write = pci_generic_config_write,
  373. };
  374. static void pmac_pci_fixup_u4_of_node(struct pci_dev *dev)
  375. {
  376. /* Apple's device-tree "hides" the root complex virtual P2P bridge
  377. * on U4. However, Linux sees it, causing the PCI <-> OF matching
  378. * code to fail to properly match devices below it. This works around
  379. * it by setting the node of the bridge to point to the PHB node,
  380. * which is not entirely correct but fixes the matching code and
  381. * doesn't break anything else. It's also the simplest possible fix.
  382. */
  383. if (dev->dev.of_node == NULL)
  384. dev->dev.of_node = pcibios_get_phb_of_node(dev->bus);
  385. }
  386. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, 0x5b, pmac_pci_fixup_u4_of_node);
  387. #endif /* CONFIG_PPC64 */
  388. #ifdef CONFIG_PPC32
  389. /*
  390. * For a bandit bridge, turn on cache coherency if necessary.
  391. * N.B. we could clean this up using the hose ops directly.
  392. */
  393. static void __init init_bandit(struct pci_controller *bp)
  394. {
  395. unsigned int vendev, magic;
  396. int rev;
  397. /* read the word at offset 0 in config space for device 11 */
  398. out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);
  399. udelay(2);
  400. vendev = in_le32(bp->cfg_data);
  401. if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +
  402. PCI_VENDOR_ID_APPLE) {
  403. /* read the revision id */
  404. out_le32(bp->cfg_addr,
  405. (1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);
  406. udelay(2);
  407. rev = in_8(bp->cfg_data);
  408. if (rev != BANDIT_REVID)
  409. printk(KERN_WARNING
  410. "Unknown revision %d for bandit\n", rev);
  411. } else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {
  412. printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);
  413. return;
  414. }
  415. /* read the word at offset 0x50 */
  416. out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);
  417. udelay(2);
  418. magic = in_le32(bp->cfg_data);
  419. if ((magic & BANDIT_COHERENT) != 0)
  420. return;
  421. magic |= BANDIT_COHERENT;
  422. udelay(2);
  423. out_le32(bp->cfg_data, magic);
  424. printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");
  425. }
  426. /*
  427. * Tweak the PCI-PCI bridge chip on the blue & white G3s.
  428. */
  429. static void __init init_p2pbridge(void)
  430. {
  431. struct device_node *p2pbridge;
  432. struct pci_controller* hose;
  433. u8 bus, devfn;
  434. u16 val;
  435. /* XXX it would be better here to identify the specific
  436. PCI-PCI bridge chip we have. */
  437. p2pbridge = of_find_node_by_name(NULL, "pci-bridge");
  438. if (p2pbridge == NULL || !of_node_name_eq(p2pbridge->parent, "pci"))
  439. goto done;
  440. if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {
  441. DBG("Can't find PCI infos for PCI<->PCI bridge\n");
  442. goto done;
  443. }
  444. /* Warning: At this point, we have not yet renumbered all busses.
  445. * So we must use OF walking to find out hose
  446. */
  447. hose = pci_find_hose_for_OF_device(p2pbridge);
  448. if (!hose) {
  449. DBG("Can't find hose for PCI<->PCI bridge\n");
  450. goto done;
  451. }
  452. if (early_read_config_word(hose, bus, devfn,
  453. PCI_BRIDGE_CONTROL, &val) < 0) {
  454. printk(KERN_ERR "init_p2pbridge: couldn't read bridge"
  455. " control\n");
  456. goto done;
  457. }
  458. val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;
  459. early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);
  460. done:
  461. of_node_put(p2pbridge);
  462. }
  463. static void __init init_second_ohare(void)
  464. {
  465. struct device_node *np = of_find_node_by_name(NULL, "pci106b,7");
  466. unsigned char bus, devfn;
  467. unsigned short cmd;
  468. if (np == NULL)
  469. return;
  470. /* This must run before we initialize the PICs since the second
  471. * ohare hosts a PIC that will be accessed there.
  472. */
  473. if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
  474. struct pci_controller* hose =
  475. pci_find_hose_for_OF_device(np);
  476. if (!hose) {
  477. printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
  478. of_node_put(np);
  479. return;
  480. }
  481. early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
  482. cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  483. cmd &= ~PCI_COMMAND_IO;
  484. early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
  485. }
  486. has_second_ohare = 1;
  487. of_node_put(np);
  488. }
  489. /*
  490. * Some Apple desktop machines have a NEC PD720100A USB2 controller
  491. * on the motherboard. Open Firmware, on these, will disable the
  492. * EHCI part of it so it behaves like a pair of OHCI's. This fixup
  493. * code re-enables it ;)
  494. */
  495. static void __init fixup_nec_usb2(void)
  496. {
  497. struct device_node *nec;
  498. for_each_node_by_name(nec, "usb") {
  499. struct pci_controller *hose;
  500. u32 data;
  501. const u32 *prop;
  502. u8 bus, devfn;
  503. prop = of_get_property(nec, "vendor-id", NULL);
  504. if (prop == NULL)
  505. continue;
  506. if (0x1033 != *prop)
  507. continue;
  508. prop = of_get_property(nec, "device-id", NULL);
  509. if (prop == NULL)
  510. continue;
  511. if (0x0035 != *prop)
  512. continue;
  513. prop = of_get_property(nec, "reg", NULL);
  514. if (prop == NULL)
  515. continue;
  516. devfn = (prop[0] >> 8) & 0xff;
  517. bus = (prop[0] >> 16) & 0xff;
  518. if (PCI_FUNC(devfn) != 0)
  519. continue;
  520. hose = pci_find_hose_for_OF_device(nec);
  521. if (!hose)
  522. continue;
  523. early_read_config_dword(hose, bus, devfn, 0xe4, &data);
  524. if (data & 1UL) {
  525. printk("Found NEC PD720100A USB2 chip with disabled"
  526. " EHCI, fixing up...\n");
  527. data &= ~1UL;
  528. early_write_config_dword(hose, bus, devfn, 0xe4, data);
  529. }
  530. }
  531. }
  532. static void __init setup_bandit(struct pci_controller *hose,
  533. struct resource *addr)
  534. {
  535. hose->ops = &macrisc_pci_ops;
  536. hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
  537. hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
  538. init_bandit(hose);
  539. }
  540. static int __init setup_uninorth(struct pci_controller *hose,
  541. struct resource *addr)
  542. {
  543. pci_add_flags(PCI_REASSIGN_ALL_BUS);
  544. has_uninorth = 1;
  545. hose->ops = &macrisc_pci_ops;
  546. hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
  547. hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
  548. /* We "know" that the bridge at f2000000 has the PCI slots. */
  549. return addr->start == 0xf2000000;
  550. }
  551. #endif /* CONFIG_PPC32 */
  552. #ifdef CONFIG_PPC64
  553. static void __init setup_u3_agp(struct pci_controller* hose)
  554. {
  555. /* On G5, we move AGP up to high bus number so we don't need
  556. * to reassign bus numbers for HT. If we ever have P2P bridges
  557. * on AGP, we'll have to move pci_assign_all_busses to the
  558. * pci_controller structure so we enable it for AGP and not for
  559. * HT childs.
  560. * We hard code the address because of the different size of
  561. * the reg address cell, we shall fix that by killing struct
  562. * reg_property and using some accessor functions instead
  563. */
  564. hose->first_busno = 0xf0;
  565. hose->last_busno = 0xff;
  566. has_uninorth = 1;
  567. hose->ops = &macrisc_pci_ops;
  568. hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
  569. hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
  570. u3_agp = hose;
  571. }
  572. static void __init setup_u4_pcie(struct pci_controller* hose)
  573. {
  574. /* We currently only implement the "non-atomic" config space, to
  575. * be optimised later.
  576. */
  577. hose->ops = &u4_pcie_pci_ops;
  578. hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
  579. hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
  580. /* The bus contains a bridge from root -> device, we need to
  581. * make it visible on bus 0 so that we pick the right type
  582. * of config cycles. If we didn't, we would have to force all
  583. * config cycles to be type 1. So we override the "bus-range"
  584. * property here
  585. */
  586. hose->first_busno = 0x00;
  587. hose->last_busno = 0xff;
  588. }
  589. static void __init parse_region_decode(struct pci_controller *hose,
  590. u32 decode)
  591. {
  592. unsigned long base, end, next = -1;
  593. int i, cur = -1;
  594. /* Iterate through all bits. We ignore the last bit as this region is
  595. * reserved for the ROM among other niceties
  596. */
  597. for (i = 0; i < 31; i++) {
  598. if ((decode & (0x80000000 >> i)) == 0)
  599. continue;
  600. if (i < 16) {
  601. base = 0xf0000000 | (((u32)i) << 24);
  602. end = base + 0x00ffffff;
  603. } else {
  604. base = ((u32)i-16) << 28;
  605. end = base + 0x0fffffff;
  606. }
  607. if (base != next) {
  608. if (++cur >= 3) {
  609. printk(KERN_WARNING "PCI: Too many ranges !\n");
  610. break;
  611. }
  612. hose->mem_resources[cur].flags = IORESOURCE_MEM;
  613. hose->mem_resources[cur].name = hose->dn->full_name;
  614. hose->mem_resources[cur].start = base;
  615. hose->mem_resources[cur].end = end;
  616. hose->mem_offset[cur] = 0;
  617. DBG(" %d: 0x%08lx-0x%08lx\n", cur, base, end);
  618. } else {
  619. DBG(" : -0x%08lx\n", end);
  620. hose->mem_resources[cur].end = end;
  621. }
  622. next = end + 1;
  623. }
  624. }
  625. static void __init setup_u3_ht(struct pci_controller* hose)
  626. {
  627. struct device_node *np = hose->dn;
  628. struct resource cfg_res, self_res;
  629. u32 decode;
  630. hose->ops = &u3_ht_pci_ops;
  631. /* Get base addresses from OF tree
  632. */
  633. if (of_address_to_resource(np, 0, &cfg_res) ||
  634. of_address_to_resource(np, 1, &self_res)) {
  635. printk(KERN_ERR "PCI: Failed to get U3/U4 HT resources !\n");
  636. return;
  637. }
  638. /* Map external cfg space access into cfg_data and self registers
  639. * into cfg_addr
  640. */
  641. hose->cfg_data = ioremap(cfg_res.start, 0x02000000);
  642. hose->cfg_addr = ioremap(self_res.start, resource_size(&self_res));
  643. /*
  644. * /ht node doesn't expose a "ranges" property, we read the register
  645. * that controls the decoding logic and use that for memory regions.
  646. * The IO region is hard coded since it is fixed in HW as well.
  647. */
  648. hose->io_base_phys = 0xf4000000;
  649. hose->pci_io_size = 0x00400000;
  650. hose->io_resource.name = np->full_name;
  651. hose->io_resource.start = 0;
  652. hose->io_resource.end = 0x003fffff;
  653. hose->io_resource.flags = IORESOURCE_IO;
  654. hose->first_busno = 0;
  655. hose->last_busno = 0xef;
  656. /* Note: fix offset when cfg_addr becomes a void * */
  657. decode = in_be32(hose->cfg_addr + 0x80);
  658. DBG("PCI: Apple HT bridge decode register: 0x%08x\n", decode);
  659. /* NOTE: The decode register setup is a bit weird... region
  660. * 0xf8000000 for example is marked as enabled in there while it's
  661. & actually the memory controller registers.
  662. * That means that we are incorrectly attributing it to HT.
  663. *
  664. * In a similar vein, region 0xf4000000 is actually the HT IO space but
  665. * also marked as enabled in here and 0xf9000000 is used by some other
  666. * internal bits of the northbridge.
  667. *
  668. * Unfortunately, we can't just mask out those bit as we would end
  669. * up with more regions than we can cope (linux can only cope with
  670. * 3 memory regions for a PHB at this stage).
  671. *
  672. * So for now, we just do a little hack. We happen to -know- that
  673. * Apple firmware doesn't assign things below 0xfa000000 for that
  674. * bridge anyway so we mask out all bits we don't want.
  675. */
  676. decode &= 0x003fffff;
  677. /* Now parse the resulting bits and build resources */
  678. parse_region_decode(hose, decode);
  679. }
  680. #endif /* CONFIG_PPC64 */
  681. /*
  682. * We assume that if we have a G3 powermac, we have one bridge called
  683. * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
  684. * if we have one or more bandit or chaos bridges, we don't have a MPC106.
  685. */
  686. static int __init pmac_add_bridge(struct device_node *dev)
  687. {
  688. int len;
  689. struct pci_controller *hose;
  690. struct resource rsrc;
  691. char *disp_name;
  692. const int *bus_range;
  693. int primary = 1;
  694. DBG("Adding PCI host bridge %pOF\n", dev);
  695. /* Fetch host bridge registers address */
  696. of_address_to_resource(dev, 0, &rsrc);
  697. /* Get bus range if any */
  698. bus_range = of_get_property(dev, "bus-range", &len);
  699. if (bus_range == NULL || len < 2 * sizeof(int)) {
  700. printk(KERN_WARNING "Can't get bus-range for %pOF, assume"
  701. " bus 0\n", dev);
  702. }
  703. hose = pcibios_alloc_controller(dev);
  704. if (!hose)
  705. return -ENOMEM;
  706. hose->first_busno = bus_range ? bus_range[0] : 0;
  707. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  708. hose->controller_ops = pmac_pci_controller_ops;
  709. disp_name = NULL;
  710. /* 64 bits only bridges */
  711. #ifdef CONFIG_PPC64
  712. if (of_device_is_compatible(dev, "u3-agp")) {
  713. setup_u3_agp(hose);
  714. disp_name = "U3-AGP";
  715. primary = 0;
  716. } else if (of_device_is_compatible(dev, "u3-ht")) {
  717. setup_u3_ht(hose);
  718. disp_name = "U3-HT";
  719. primary = 1;
  720. } else if (of_device_is_compatible(dev, "u4-pcie")) {
  721. setup_u4_pcie(hose);
  722. disp_name = "U4-PCIE";
  723. primary = 0;
  724. }
  725. printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number:"
  726. " %d->%d\n", disp_name, hose->first_busno, hose->last_busno);
  727. #endif /* CONFIG_PPC64 */
  728. /* 32 bits only bridges */
  729. #ifdef CONFIG_PPC32
  730. if (of_device_is_compatible(dev, "uni-north")) {
  731. primary = setup_uninorth(hose, &rsrc);
  732. disp_name = "UniNorth";
  733. } else if (of_node_name_eq(dev, "pci")) {
  734. /* XXX assume this is a mpc106 (grackle) */
  735. setup_grackle(hose);
  736. disp_name = "Grackle (MPC106)";
  737. } else if (of_node_name_eq(dev, "bandit")) {
  738. setup_bandit(hose, &rsrc);
  739. disp_name = "Bandit";
  740. } else if (of_node_name_eq(dev, "chaos")) {
  741. setup_chaos(hose, &rsrc);
  742. disp_name = "Chaos";
  743. primary = 0;
  744. }
  745. printk(KERN_INFO "Found %s PCI host bridge at 0x%016llx. "
  746. "Firmware bus number: %d->%d\n",
  747. disp_name, (unsigned long long)rsrc.start, hose->first_busno,
  748. hose->last_busno);
  749. #endif /* CONFIG_PPC32 */
  750. DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  751. hose, hose->cfg_addr, hose->cfg_data);
  752. /* Interpret the "ranges" property */
  753. /* This also maps the I/O region and sets isa_io/mem_base */
  754. pci_process_bridge_OF_ranges(hose, dev, primary);
  755. /* Fixup "bus-range" OF property */
  756. fixup_bus_range(dev);
  757. /* create pci_dn's for DT nodes under this PHB */
  758. if (IS_ENABLED(CONFIG_PPC64))
  759. pci_devs_phb_init_dynamic(hose);
  760. return 0;
  761. }
  762. void pmac_pci_irq_fixup(struct pci_dev *dev)
  763. {
  764. #ifdef CONFIG_PPC32
  765. /* Fixup interrupt for the modem/ethernet combo controller.
  766. * on machines with a second ohare chip.
  767. * The number in the device tree (27) is bogus (correct for
  768. * the ethernet-only board but not the combo ethernet/modem
  769. * board). The real interrupt is 28 on the second controller
  770. * -> 28+32 = 60.
  771. */
  772. if (has_second_ohare &&
  773. dev->vendor == PCI_VENDOR_ID_DEC &&
  774. dev->device == PCI_DEVICE_ID_DEC_TULIP_PLUS) {
  775. dev->irq = irq_create_mapping(NULL, 60);
  776. irq_set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);
  777. }
  778. #endif /* CONFIG_PPC32 */
  779. }
  780. #ifdef CONFIG_PPC64
  781. static int pmac_pci_root_bridge_prepare(struct pci_host_bridge *bridge)
  782. {
  783. struct pci_controller *hose = pci_bus_to_host(bridge->bus);
  784. struct device_node *np, *child;
  785. if (hose != u3_agp)
  786. return 0;
  787. /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
  788. * assume there is no P2P bridge on the AGP bus, which should be a
  789. * safe assumptions for now. We should do something better in the
  790. * future though
  791. */
  792. np = hose->dn;
  793. PCI_DN(np)->busno = 0xf0;
  794. for_each_child_of_node(np, child)
  795. PCI_DN(child)->busno = 0xf0;
  796. return 0;
  797. }
  798. #endif /* CONFIG_PPC64 */
  799. void __init pmac_pci_init(void)
  800. {
  801. struct device_node *np, *root;
  802. struct device_node *ht __maybe_unused = NULL;
  803. pci_set_flags(PCI_CAN_SKIP_ISA_ALIGN);
  804. root = of_find_node_by_path("/");
  805. if (root == NULL) {
  806. printk(KERN_CRIT "pmac_pci_init: can't find root "
  807. "of device tree\n");
  808. return;
  809. }
  810. for_each_child_of_node(root, np) {
  811. if (of_node_name_eq(np, "bandit")
  812. || of_node_name_eq(np, "chaos")
  813. || of_node_name_eq(np, "pci")) {
  814. if (pmac_add_bridge(np) == 0)
  815. of_node_get(np);
  816. }
  817. if (of_node_name_eq(np, "ht")) {
  818. of_node_get(np);
  819. ht = np;
  820. }
  821. }
  822. of_node_put(root);
  823. #ifdef CONFIG_PPC64
  824. /* Probe HT last as it relies on the agp resources to be already
  825. * setup
  826. */
  827. if (ht && pmac_add_bridge(ht) != 0)
  828. of_node_put(ht);
  829. ppc_md.pcibios_root_bridge_prepare = pmac_pci_root_bridge_prepare;
  830. /* pmac_check_ht_link(); */
  831. #else /* CONFIG_PPC64 */
  832. init_p2pbridge();
  833. init_second_ohare();
  834. fixup_nec_usb2();
  835. /* We are still having some issues with the Xserve G4, enabling
  836. * some offset between bus number and domains for now when we
  837. * assign all busses should help for now
  838. */
  839. if (pci_has_flag(PCI_REASSIGN_ALL_BUS))
  840. pcibios_assign_bus_offset = 0x10;
  841. #endif
  842. }
  843. #ifdef CONFIG_PPC32
  844. static bool pmac_pci_enable_device_hook(struct pci_dev *dev)
  845. {
  846. struct device_node* node;
  847. int updatecfg = 0;
  848. int uninorth_child;
  849. node = pci_device_to_OF_node(dev);
  850. /* We don't want to enable USB controllers absent from the OF tree
  851. * (iBook second controller)
  852. */
  853. if (dev->vendor == PCI_VENDOR_ID_APPLE
  854. && dev->class == PCI_CLASS_SERIAL_USB_OHCI
  855. && !node) {
  856. printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
  857. pci_name(dev));
  858. return false;
  859. }
  860. if (!node)
  861. return true;
  862. uninorth_child = node->parent &&
  863. of_device_is_compatible(node->parent, "uni-north");
  864. /* Firewire & GMAC were disabled after PCI probe, the driver is
  865. * claiming them, we must re-enable them now.
  866. */
  867. if (uninorth_child && of_node_name_eq(node, "firewire") &&
  868. (of_device_is_compatible(node, "pci106b,18") ||
  869. of_device_is_compatible(node, "pci106b,30") ||
  870. of_device_is_compatible(node, "pci11c1,5811"))) {
  871. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
  872. pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
  873. updatecfg = 1;
  874. }
  875. if (uninorth_child && of_node_name_eq(node, "ethernet") &&
  876. of_device_is_compatible(node, "gmac")) {
  877. pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
  878. updatecfg = 1;
  879. }
  880. /*
  881. * Fixup various header fields on 32 bits. We don't do that on
  882. * 64 bits as some of these have strange values behind the HT
  883. * bridge and we must not, for example, enable MWI or set the
  884. * cache line size on them.
  885. */
  886. if (updatecfg) {
  887. u16 cmd;
  888. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  889. cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
  890. | PCI_COMMAND_INVALIDATE;
  891. pci_write_config_word(dev, PCI_COMMAND, cmd);
  892. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
  893. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  894. L1_CACHE_BYTES >> 2);
  895. }
  896. return true;
  897. }
  898. static void pmac_pci_fixup_ohci(struct pci_dev *dev)
  899. {
  900. struct device_node *node = pci_device_to_OF_node(dev);
  901. /* We don't want to assign resources to USB controllers
  902. * absent from the OF tree (iBook second controller)
  903. */
  904. if (dev->class == PCI_CLASS_SERIAL_USB_OHCI && !node)
  905. dev->resource[0].flags = 0;
  906. }
  907. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_ANY_ID, pmac_pci_fixup_ohci);
  908. /* We power down some devices after they have been probed. They'll
  909. * be powered back on later on
  910. */
  911. void __init pmac_pcibios_after_init(void)
  912. {
  913. struct device_node* nd;
  914. for_each_node_by_name(nd, "firewire") {
  915. if (nd->parent && (of_device_is_compatible(nd, "pci106b,18") ||
  916. of_device_is_compatible(nd, "pci106b,30") ||
  917. of_device_is_compatible(nd, "pci11c1,5811"))
  918. && of_device_is_compatible(nd->parent, "uni-north")) {
  919. pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
  920. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
  921. }
  922. }
  923. for_each_node_by_name(nd, "ethernet") {
  924. if (nd->parent && of_device_is_compatible(nd, "gmac")
  925. && of_device_is_compatible(nd->parent, "uni-north"))
  926. pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
  927. }
  928. }
  929. static void pmac_pci_fixup_cardbus(struct pci_dev *dev)
  930. {
  931. if (!machine_is(powermac))
  932. return;
  933. /*
  934. * Fix the interrupt routing on the various cardbus bridges
  935. * used on powerbooks
  936. */
  937. if (dev->vendor != PCI_VENDOR_ID_TI)
  938. return;
  939. if (dev->device == PCI_DEVICE_ID_TI_1130 ||
  940. dev->device == PCI_DEVICE_ID_TI_1131) {
  941. u8 val;
  942. /* Enable PCI interrupt */
  943. if (pci_read_config_byte(dev, 0x91, &val) == 0)
  944. pci_write_config_byte(dev, 0x91, val | 0x30);
  945. /* Disable ISA interrupt mode */
  946. if (pci_read_config_byte(dev, 0x92, &val) == 0)
  947. pci_write_config_byte(dev, 0x92, val & ~0x06);
  948. }
  949. if (dev->device == PCI_DEVICE_ID_TI_1210 ||
  950. dev->device == PCI_DEVICE_ID_TI_1211 ||
  951. dev->device == PCI_DEVICE_ID_TI_1410 ||
  952. dev->device == PCI_DEVICE_ID_TI_1510) {
  953. u8 val;
  954. /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA
  955. signal out the MFUNC0 pin */
  956. if (pci_read_config_byte(dev, 0x8c, &val) == 0)
  957. pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
  958. /* Disable ISA interrupt mode */
  959. if (pci_read_config_byte(dev, 0x92, &val) == 0)
  960. pci_write_config_byte(dev, 0x92, val & ~0x06);
  961. }
  962. }
  963. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
  964. static void pmac_pci_fixup_pciata(struct pci_dev *dev)
  965. {
  966. u8 progif = 0;
  967. /*
  968. * On PowerMacs, we try to switch any PCI ATA controller to
  969. * fully native mode
  970. */
  971. if (!machine_is(powermac))
  972. return;
  973. /* Some controllers don't have the class IDE */
  974. if (dev->vendor == PCI_VENDOR_ID_PROMISE)
  975. switch(dev->device) {
  976. case PCI_DEVICE_ID_PROMISE_20246:
  977. case PCI_DEVICE_ID_PROMISE_20262:
  978. case PCI_DEVICE_ID_PROMISE_20263:
  979. case PCI_DEVICE_ID_PROMISE_20265:
  980. case PCI_DEVICE_ID_PROMISE_20267:
  981. case PCI_DEVICE_ID_PROMISE_20268:
  982. case PCI_DEVICE_ID_PROMISE_20269:
  983. case PCI_DEVICE_ID_PROMISE_20270:
  984. case PCI_DEVICE_ID_PROMISE_20271:
  985. case PCI_DEVICE_ID_PROMISE_20275:
  986. case PCI_DEVICE_ID_PROMISE_20276:
  987. case PCI_DEVICE_ID_PROMISE_20277:
  988. goto good;
  989. }
  990. /* Others, check PCI class */
  991. if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
  992. return;
  993. good:
  994. pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  995. if ((progif & 5) != 5) {
  996. printk(KERN_INFO "PCI: %s Forcing PCI IDE into native mode\n",
  997. pci_name(dev));
  998. (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
  999. if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
  1000. (progif & 5) != 5)
  1001. printk(KERN_ERR "Rewrite of PROGIF failed !\n");
  1002. else {
  1003. /* Clear IO BARs, they will be reassigned */
  1004. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0);
  1005. pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, 0);
  1006. pci_write_config_dword(dev, PCI_BASE_ADDRESS_2, 0);
  1007. pci_write_config_dword(dev, PCI_BASE_ADDRESS_3, 0);
  1008. }
  1009. }
  1010. }
  1011. DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
  1012. #endif /* CONFIG_PPC32 */
  1013. /*
  1014. * Disable second function on K2-SATA, it's broken
  1015. * and disable IO BARs on first one
  1016. */
  1017. static void fixup_k2_sata(struct pci_dev* dev)
  1018. {
  1019. int i;
  1020. u16 cmd;
  1021. if (PCI_FUNC(dev->devfn) > 0) {
  1022. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1023. cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  1024. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1025. for (i = 0; i < 6; i++) {
  1026. dev->resource[i].start = dev->resource[i].end = 0;
  1027. dev->resource[i].flags = 0;
  1028. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
  1029. 0);
  1030. }
  1031. } else {
  1032. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1033. cmd &= ~PCI_COMMAND_IO;
  1034. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1035. for (i = 0; i < 5; i++) {
  1036. dev->resource[i].start = dev->resource[i].end = 0;
  1037. dev->resource[i].flags = 0;
  1038. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
  1039. 0);
  1040. }
  1041. }
  1042. }
  1043. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);
  1044. /*
  1045. * On U4 (aka CPC945) the PCIe root complex "P2P" bridge resource ranges aren't
  1046. * configured by the firmware. The bridge itself seems to ignore them but it
  1047. * causes problems with Linux which then re-assigns devices below the bridge,
  1048. * thus changing addresses of those devices from what was in the device-tree,
  1049. * which sucks when those are video cards using offb
  1050. *
  1051. * We could just mark it transparent but I prefer fixing up the resources to
  1052. * properly show what's going on here, as I have some doubts about having them
  1053. * badly configured potentially being an issue for DMA.
  1054. *
  1055. * We leave PIO alone, it seems to be fine
  1056. *
  1057. * Oh and there's another funny bug. The OF properties advertize the region
  1058. * 0xf1000000..0xf1ffffff as being forwarded as memory space. But that's
  1059. * actually not true, this region is the memory mapped config space. So we
  1060. * also need to filter it out or we'll map things in the wrong place.
  1061. */
  1062. static void fixup_u4_pcie(struct pci_dev* dev)
  1063. {
  1064. struct pci_controller *host = pci_bus_to_host(dev->bus);
  1065. struct resource *region = NULL;
  1066. u32 reg;
  1067. int i;
  1068. /* Only do that on PowerMac */
  1069. if (!machine_is(powermac))
  1070. return;
  1071. /* Find the largest MMIO region */
  1072. for (i = 0; i < 3; i++) {
  1073. struct resource *r = &host->mem_resources[i];
  1074. if (!(r->flags & IORESOURCE_MEM))
  1075. continue;
  1076. /* Skip the 0xf0xxxxxx..f2xxxxxx regions, we know they
  1077. * are reserved by HW for other things
  1078. */
  1079. if (r->start >= 0xf0000000 && r->start < 0xf3000000)
  1080. continue;
  1081. if (!region || resource_size(r) > resource_size(region))
  1082. region = r;
  1083. }
  1084. /* Nothing found, bail */
  1085. if (!region)
  1086. return;
  1087. /* Print things out */
  1088. printk(KERN_INFO "PCI: Fixup U4 PCIe bridge range: %pR\n", region);
  1089. /* Fixup bridge config space. We know it's a Mac, resource aren't
  1090. * offset so let's just blast them as-is. We also know that they
  1091. * fit in 32 bits
  1092. */
  1093. reg = ((region->start >> 16) & 0xfff0) | (region->end & 0xfff00000);
  1094. pci_write_config_dword(dev, PCI_MEMORY_BASE, reg);
  1095. pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0);
  1096. pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
  1097. pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0);
  1098. }
  1099. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_U4_PCIE, fixup_u4_pcie);
  1100. #ifdef CONFIG_PPC64
  1101. static int pmac_pci_probe_mode(struct pci_bus *bus)
  1102. {
  1103. struct device_node *node = pci_bus_to_OF_node(bus);
  1104. /* We need to use normal PCI probing for the AGP bus,
  1105. * since the device for the AGP bridge isn't in the tree.
  1106. * Same for the PCIe host on U4 and the HT host bridge.
  1107. */
  1108. if (bus->self == NULL && (of_device_is_compatible(node, "u3-agp") ||
  1109. of_device_is_compatible(node, "u4-pcie") ||
  1110. of_device_is_compatible(node, "u3-ht")))
  1111. return PCI_PROBE_NORMAL;
  1112. return PCI_PROBE_DEVTREE;
  1113. }
  1114. #endif /* CONFIG_PPC64 */
  1115. struct pci_controller_ops pmac_pci_controller_ops = {
  1116. #ifdef CONFIG_PPC64
  1117. .probe_mode = pmac_pci_probe_mode,
  1118. #endif
  1119. #ifdef CONFIG_PPC32
  1120. .enable_device_hook = pmac_pci_enable_device_hook,
  1121. #endif
  1122. };