pci.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /*
  2. * Support for PCI bridges found on Power Macintoshes.
  3. *
  4. * Copyright (C) 2003 Benjamin Herrenschmuidt (benh@kernel.crashing.org)
  5. * 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. #include <linux/kernel.h>
  13. #include <linux/pci.h>
  14. #include <linux/delay.h>
  15. #include <linux/string.h>
  16. #include <linux/init.h>
  17. #include <linux/bootmem.h>
  18. #include <asm/sections.h>
  19. #include <asm/io.h>
  20. #include <asm/prom.h>
  21. #include <asm/pci-bridge.h>
  22. #include <asm/machdep.h>
  23. #include <asm/pmac_feature.h>
  24. #include <asm/grackle.h>
  25. #ifdef CONFIG_PPC64
  26. #include <asm/iommu.h>
  27. #include <asm/ppc-pci.h>
  28. #endif
  29. #undef DEBUG
  30. #ifdef DEBUG
  31. #define DBG(x...) printk(x)
  32. #else
  33. #define DBG(x...)
  34. #endif
  35. static int add_bridge(struct device_node *dev);
  36. /* XXX Could be per-controller, but I don't think we risk anything by
  37. * assuming we won't have both UniNorth and Bandit */
  38. static int has_uninorth;
  39. #ifdef CONFIG_PPC64
  40. static struct pci_controller *u3_agp;
  41. static struct pci_controller *u3_ht;
  42. #endif /* CONFIG_PPC64 */
  43. extern u8 pci_cache_line_size;
  44. extern int pcibios_assign_bus_offset;
  45. struct device_node *k2_skiplist[2];
  46. /*
  47. * Magic constants for enabling cache coherency in the bandit/PSX bridge.
  48. */
  49. #define BANDIT_DEVID_2 8
  50. #define BANDIT_REVID 3
  51. #define BANDIT_DEVNUM 11
  52. #define BANDIT_MAGIC 0x50
  53. #define BANDIT_COHERENT 0x40
  54. static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
  55. {
  56. for (; node != 0;node = node->sibling) {
  57. int * bus_range;
  58. unsigned int *class_code;
  59. int len;
  60. /* For PCI<->PCI bridges or CardBus bridges, we go down */
  61. class_code = (unsigned int *) get_property(node, "class-code", NULL);
  62. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  63. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  64. continue;
  65. bus_range = (int *) get_property(node, "bus-range", &len);
  66. if (bus_range != NULL && len > 2 * sizeof(int)) {
  67. if (bus_range[1] > higher)
  68. higher = bus_range[1];
  69. }
  70. higher = fixup_one_level_bus_range(node->child, higher);
  71. }
  72. return higher;
  73. }
  74. /* This routine fixes the "bus-range" property of all bridges in the
  75. * system since they tend to have their "last" member wrong on macs
  76. *
  77. * Note that the bus numbers manipulated here are OF bus numbers, they
  78. * are not Linux bus numbers.
  79. */
  80. static void __init fixup_bus_range(struct device_node *bridge)
  81. {
  82. int * bus_range;
  83. int len;
  84. /* Lookup the "bus-range" property for the hose */
  85. bus_range = (int *) get_property(bridge, "bus-range", &len);
  86. if (bus_range == NULL || len < 2 * sizeof(int)) {
  87. printk(KERN_WARNING "Can't get bus-range for %s\n",
  88. bridge->full_name);
  89. return;
  90. }
  91. bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
  92. }
  93. /*
  94. * Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.
  95. *
  96. * The "Bandit" version is present in all early PCI PowerMacs,
  97. * and up to the first ones using Grackle. Some machines may
  98. * have 2 bandit controllers (2 PCI busses).
  99. *
  100. * "Chaos" is used in some "Bandit"-type machines as a bridge
  101. * for the separate display bus. It is accessed the same
  102. * way as bandit, but cannot be probed for devices. It therefore
  103. * has its own config access functions.
  104. *
  105. * The "UniNorth" version is present in all Core99 machines
  106. * (iBook, G4, new IMacs, and all the recent Apple machines).
  107. * It contains 3 controllers in one ASIC.
  108. *
  109. * The U3 is the bridge used on G5 machines. It contains an
  110. * AGP bus which is dealt with the old UniNorth access routines
  111. * and a HyperTransport bus which uses its own set of access
  112. * functions.
  113. */
  114. #define MACRISC_CFA0(devfn, off) \
  115. ((1 << (unsigned long)PCI_SLOT(dev_fn)) \
  116. | (((unsigned long)PCI_FUNC(dev_fn)) << 8) \
  117. | (((unsigned long)(off)) & 0xFCUL))
  118. #define MACRISC_CFA1(bus, devfn, off) \
  119. ((((unsigned long)(bus)) << 16) \
  120. |(((unsigned long)(devfn)) << 8) \
  121. |(((unsigned long)(off)) & 0xFCUL) \
  122. |1UL)
  123. static unsigned long macrisc_cfg_access(struct pci_controller* hose,
  124. u8 bus, u8 dev_fn, u8 offset)
  125. {
  126. unsigned int caddr;
  127. if (bus == hose->first_busno) {
  128. if (dev_fn < (11 << 3))
  129. return 0;
  130. caddr = MACRISC_CFA0(dev_fn, offset);
  131. } else
  132. caddr = MACRISC_CFA1(bus, dev_fn, offset);
  133. /* Uninorth will return garbage if we don't read back the value ! */
  134. do {
  135. out_le32(hose->cfg_addr, caddr);
  136. } while (in_le32(hose->cfg_addr) != caddr);
  137. offset &= has_uninorth ? 0x07 : 0x03;
  138. return ((unsigned long)hose->cfg_data) + offset;
  139. }
  140. static int macrisc_read_config(struct pci_bus *bus, unsigned int devfn,
  141. int offset, int len, u32 *val)
  142. {
  143. struct pci_controller *hose;
  144. unsigned long addr;
  145. hose = pci_bus_to_host(bus);
  146. if (hose == NULL)
  147. return PCIBIOS_DEVICE_NOT_FOUND;
  148. addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
  149. if (!addr)
  150. return PCIBIOS_DEVICE_NOT_FOUND;
  151. /*
  152. * Note: the caller has already checked that offset is
  153. * suitably aligned and that len is 1, 2 or 4.
  154. */
  155. switch (len) {
  156. case 1:
  157. *val = in_8((u8 *)addr);
  158. break;
  159. case 2:
  160. *val = in_le16((u16 *)addr);
  161. break;
  162. default:
  163. *val = in_le32((u32 *)addr);
  164. break;
  165. }
  166. return PCIBIOS_SUCCESSFUL;
  167. }
  168. static int macrisc_write_config(struct pci_bus *bus, unsigned int devfn,
  169. int offset, int len, u32 val)
  170. {
  171. struct pci_controller *hose;
  172. unsigned long addr;
  173. hose = pci_bus_to_host(bus);
  174. if (hose == NULL)
  175. return PCIBIOS_DEVICE_NOT_FOUND;
  176. addr = macrisc_cfg_access(hose, bus->number, devfn, offset);
  177. if (!addr)
  178. return PCIBIOS_DEVICE_NOT_FOUND;
  179. /*
  180. * Note: the caller has already checked that offset is
  181. * suitably aligned and that len is 1, 2 or 4.
  182. */
  183. switch (len) {
  184. case 1:
  185. out_8((u8 *)addr, val);
  186. (void) in_8((u8 *)addr);
  187. break;
  188. case 2:
  189. out_le16((u16 *)addr, val);
  190. (void) in_le16((u16 *)addr);
  191. break;
  192. default:
  193. out_le32((u32 *)addr, val);
  194. (void) in_le32((u32 *)addr);
  195. break;
  196. }
  197. return PCIBIOS_SUCCESSFUL;
  198. }
  199. static struct pci_ops macrisc_pci_ops =
  200. {
  201. macrisc_read_config,
  202. macrisc_write_config
  203. };
  204. #ifdef CONFIG_PPC32
  205. /*
  206. * Verify that a specific (bus, dev_fn) exists on chaos
  207. */
  208. static int
  209. chaos_validate_dev(struct pci_bus *bus, int devfn, int offset)
  210. {
  211. struct device_node *np;
  212. u32 *vendor, *device;
  213. np = pci_busdev_to_OF_node(bus, devfn);
  214. if (np == NULL)
  215. return PCIBIOS_DEVICE_NOT_FOUND;
  216. vendor = (u32 *)get_property(np, "vendor-id", NULL);
  217. device = (u32 *)get_property(np, "device-id", NULL);
  218. if (vendor == NULL || device == NULL)
  219. return PCIBIOS_DEVICE_NOT_FOUND;
  220. if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)
  221. && (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))
  222. return PCIBIOS_BAD_REGISTER_NUMBER;
  223. return PCIBIOS_SUCCESSFUL;
  224. }
  225. static int
  226. chaos_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  227. int len, u32 *val)
  228. {
  229. int result = chaos_validate_dev(bus, devfn, offset);
  230. if (result == PCIBIOS_BAD_REGISTER_NUMBER)
  231. *val = ~0U;
  232. if (result != PCIBIOS_SUCCESSFUL)
  233. return result;
  234. return macrisc_read_config(bus, devfn, offset, len, val);
  235. }
  236. static int
  237. chaos_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  238. int len, u32 val)
  239. {
  240. int result = chaos_validate_dev(bus, devfn, offset);
  241. if (result != PCIBIOS_SUCCESSFUL)
  242. return result;
  243. return macrisc_write_config(bus, devfn, offset, len, val);
  244. }
  245. static struct pci_ops chaos_pci_ops =
  246. {
  247. chaos_read_config,
  248. chaos_write_config
  249. };
  250. static void __init setup_chaos(struct pci_controller *hose,
  251. struct reg_property *addr)
  252. {
  253. /* assume a `chaos' bridge */
  254. hose->ops = &chaos_pci_ops;
  255. hose->cfg_addr = ioremap(addr->address + 0x800000, 0x1000);
  256. hose->cfg_data = ioremap(addr->address + 0xc00000, 0x1000);
  257. }
  258. #else
  259. #define setup_chaos(hose, addr)
  260. #endif /* CONFIG_PPC32 */
  261. #ifdef CONFIG_PPC64
  262. /*
  263. * These versions of U3 HyperTransport config space access ops do not
  264. * implement self-view of the HT host yet
  265. */
  266. /*
  267. * This function deals with some "special cases" devices.
  268. *
  269. * 0 -> No special case
  270. * 1 -> Skip the device but act as if the access was successfull
  271. * (return 0xff's on reads, eventually, cache config space
  272. * accesses in a later version)
  273. * -1 -> Hide the device (unsuccessful acess)
  274. */
  275. static int u3_ht_skip_device(struct pci_controller *hose,
  276. struct pci_bus *bus, unsigned int devfn)
  277. {
  278. struct device_node *busdn, *dn;
  279. int i;
  280. /* We only allow config cycles to devices that are in OF device-tree
  281. * as we are apparently having some weird things going on with some
  282. * revs of K2 on recent G5s
  283. */
  284. if (bus->self)
  285. busdn = pci_device_to_OF_node(bus->self);
  286. else
  287. busdn = hose->arch_data;
  288. for (dn = busdn->child; dn; dn = dn->sibling)
  289. if (dn->data && PCI_DN(dn)->devfn == devfn)
  290. break;
  291. if (dn == NULL)
  292. return -1;
  293. /*
  294. * When a device in K2 is powered down, we die on config
  295. * cycle accesses. Fix that here.
  296. */
  297. for (i=0; i<2; i++)
  298. if (k2_skiplist[i] == dn)
  299. return 1;
  300. return 0;
  301. }
  302. #define U3_HT_CFA0(devfn, off) \
  303. ((((unsigned long)devfn) << 8) | offset)
  304. #define U3_HT_CFA1(bus, devfn, off) \
  305. (U3_HT_CFA0(devfn, off) \
  306. + (((unsigned long)bus) << 16) \
  307. + 0x01000000UL)
  308. static unsigned long u3_ht_cfg_access(struct pci_controller* hose,
  309. u8 bus, u8 devfn, u8 offset)
  310. {
  311. if (bus == hose->first_busno) {
  312. /* For now, we don't self probe U3 HT bridge */
  313. if (PCI_SLOT(devfn) == 0)
  314. return 0;
  315. return ((unsigned long)hose->cfg_data) + U3_HT_CFA0(devfn, offset);
  316. } else
  317. return ((unsigned long)hose->cfg_data) + U3_HT_CFA1(bus, devfn, offset);
  318. }
  319. static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
  320. int offset, int len, u32 *val)
  321. {
  322. struct pci_controller *hose;
  323. unsigned long addr;
  324. hose = pci_bus_to_host(bus);
  325. if (hose == NULL)
  326. return PCIBIOS_DEVICE_NOT_FOUND;
  327. addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
  328. if (!addr)
  329. return PCIBIOS_DEVICE_NOT_FOUND;
  330. switch (u3_ht_skip_device(hose, bus, devfn)) {
  331. case 0:
  332. break;
  333. case 1:
  334. switch (len) {
  335. case 1:
  336. *val = 0xff; break;
  337. case 2:
  338. *val = 0xffff; break;
  339. default:
  340. *val = 0xfffffffful; break;
  341. }
  342. return PCIBIOS_SUCCESSFUL;
  343. default:
  344. return PCIBIOS_DEVICE_NOT_FOUND;
  345. }
  346. /*
  347. * Note: the caller has already checked that offset is
  348. * suitably aligned and that len is 1, 2 or 4.
  349. */
  350. switch (len) {
  351. case 1:
  352. *val = in_8((u8 *)addr);
  353. break;
  354. case 2:
  355. *val = in_le16((u16 *)addr);
  356. break;
  357. default:
  358. *val = in_le32((u32 *)addr);
  359. break;
  360. }
  361. return PCIBIOS_SUCCESSFUL;
  362. }
  363. static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
  364. int offset, int len, u32 val)
  365. {
  366. struct pci_controller *hose;
  367. unsigned long addr;
  368. hose = pci_bus_to_host(bus);
  369. if (hose == NULL)
  370. return PCIBIOS_DEVICE_NOT_FOUND;
  371. addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
  372. if (!addr)
  373. return PCIBIOS_DEVICE_NOT_FOUND;
  374. switch (u3_ht_skip_device(hose, bus, devfn)) {
  375. case 0:
  376. break;
  377. case 1:
  378. return PCIBIOS_SUCCESSFUL;
  379. default:
  380. return PCIBIOS_DEVICE_NOT_FOUND;
  381. }
  382. /*
  383. * Note: the caller has already checked that offset is
  384. * suitably aligned and that len is 1, 2 or 4.
  385. */
  386. switch (len) {
  387. case 1:
  388. out_8((u8 *)addr, val);
  389. (void) in_8((u8 *)addr);
  390. break;
  391. case 2:
  392. out_le16((u16 *)addr, val);
  393. (void) in_le16((u16 *)addr);
  394. break;
  395. default:
  396. out_le32((u32 *)addr, val);
  397. (void) in_le32((u32 *)addr);
  398. break;
  399. }
  400. return PCIBIOS_SUCCESSFUL;
  401. }
  402. static struct pci_ops u3_ht_pci_ops =
  403. {
  404. u3_ht_read_config,
  405. u3_ht_write_config
  406. };
  407. #endif /* CONFIG_PPC64 */
  408. #ifdef CONFIG_PPC32
  409. /*
  410. * For a bandit bridge, turn on cache coherency if necessary.
  411. * N.B. we could clean this up using the hose ops directly.
  412. */
  413. static void __init init_bandit(struct pci_controller *bp)
  414. {
  415. unsigned int vendev, magic;
  416. int rev;
  417. /* read the word at offset 0 in config space for device 11 */
  418. out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);
  419. udelay(2);
  420. vendev = in_le32(bp->cfg_data);
  421. if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +
  422. PCI_VENDOR_ID_APPLE) {
  423. /* read the revision id */
  424. out_le32(bp->cfg_addr,
  425. (1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);
  426. udelay(2);
  427. rev = in_8(bp->cfg_data);
  428. if (rev != BANDIT_REVID)
  429. printk(KERN_WARNING
  430. "Unknown revision %d for bandit\n", rev);
  431. } else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {
  432. printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);
  433. return;
  434. }
  435. /* read the word at offset 0x50 */
  436. out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);
  437. udelay(2);
  438. magic = in_le32(bp->cfg_data);
  439. if ((magic & BANDIT_COHERENT) != 0)
  440. return;
  441. magic |= BANDIT_COHERENT;
  442. udelay(2);
  443. out_le32(bp->cfg_data, magic);
  444. printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");
  445. }
  446. /*
  447. * Tweak the PCI-PCI bridge chip on the blue & white G3s.
  448. */
  449. static void __init init_p2pbridge(void)
  450. {
  451. struct device_node *p2pbridge;
  452. struct pci_controller* hose;
  453. u8 bus, devfn;
  454. u16 val;
  455. /* XXX it would be better here to identify the specific
  456. PCI-PCI bridge chip we have. */
  457. if ((p2pbridge = find_devices("pci-bridge")) == 0
  458. || p2pbridge->parent == NULL
  459. || strcmp(p2pbridge->parent->name, "pci") != 0)
  460. return;
  461. if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {
  462. DBG("Can't find PCI infos for PCI<->PCI bridge\n");
  463. return;
  464. }
  465. /* Warning: At this point, we have not yet renumbered all busses.
  466. * So we must use OF walking to find out hose
  467. */
  468. hose = pci_find_hose_for_OF_device(p2pbridge);
  469. if (!hose) {
  470. DBG("Can't find hose for PCI<->PCI bridge\n");
  471. return;
  472. }
  473. if (early_read_config_word(hose, bus, devfn,
  474. PCI_BRIDGE_CONTROL, &val) < 0) {
  475. printk(KERN_ERR "init_p2pbridge: couldn't read bridge control\n");
  476. return;
  477. }
  478. val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;
  479. early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);
  480. }
  481. /*
  482. * Some Apple desktop machines have a NEC PD720100A USB2 controller
  483. * on the motherboard. Open Firmware, on these, will disable the
  484. * EHCI part of it so it behaves like a pair of OHCI's. This fixup
  485. * code re-enables it ;)
  486. */
  487. static void __init fixup_nec_usb2(void)
  488. {
  489. struct device_node *nec;
  490. for (nec = NULL; (nec = of_find_node_by_name(nec, "usb")) != NULL;) {
  491. struct pci_controller *hose;
  492. u32 data, *prop;
  493. u8 bus, devfn;
  494. prop = (u32 *)get_property(nec, "vendor-id", NULL);
  495. if (prop == NULL)
  496. continue;
  497. if (0x1033 != *prop)
  498. continue;
  499. prop = (u32 *)get_property(nec, "device-id", NULL);
  500. if (prop == NULL)
  501. continue;
  502. if (0x0035 != *prop)
  503. continue;
  504. prop = (u32 *)get_property(nec, "reg", NULL);
  505. if (prop == NULL)
  506. continue;
  507. devfn = (prop[0] >> 8) & 0xff;
  508. bus = (prop[0] >> 16) & 0xff;
  509. if (PCI_FUNC(devfn) != 0)
  510. continue;
  511. hose = pci_find_hose_for_OF_device(nec);
  512. if (!hose)
  513. continue;
  514. early_read_config_dword(hose, bus, devfn, 0xe4, &data);
  515. if (data & 1UL) {
  516. printk("Found NEC PD720100A USB2 chip with disabled EHCI, fixing up...\n");
  517. data &= ~1UL;
  518. early_write_config_dword(hose, bus, devfn, 0xe4, data);
  519. early_write_config_byte(hose, bus, devfn | 2, PCI_INTERRUPT_LINE,
  520. nec->intrs[0].line);
  521. }
  522. }
  523. }
  524. static void __init setup_bandit(struct pci_controller *hose,
  525. struct reg_property *addr)
  526. {
  527. hose->ops = &macrisc_pci_ops;
  528. hose->cfg_addr = ioremap(addr->address + 0x800000, 0x1000);
  529. hose->cfg_data = ioremap(addr->address + 0xc00000, 0x1000);
  530. init_bandit(hose);
  531. }
  532. static int __init setup_uninorth(struct pci_controller *hose,
  533. struct reg_property *addr)
  534. {
  535. pci_assign_all_buses = 1;
  536. has_uninorth = 1;
  537. hose->ops = &macrisc_pci_ops;
  538. hose->cfg_addr = ioremap(addr->address + 0x800000, 0x1000);
  539. hose->cfg_data = ioremap(addr->address + 0xc00000, 0x1000);
  540. /* We "know" that the bridge at f2000000 has the PCI slots. */
  541. return addr->address == 0xf2000000;
  542. }
  543. #endif
  544. #ifdef CONFIG_PPC64
  545. static void __init setup_u3_agp(struct pci_controller* hose)
  546. {
  547. /* On G5, we move AGP up to high bus number so we don't need
  548. * to reassign bus numbers for HT. If we ever have P2P bridges
  549. * on AGP, we'll have to move pci_assign_all_busses to the
  550. * pci_controller structure so we enable it for AGP and not for
  551. * HT childs.
  552. * We hard code the address because of the different size of
  553. * the reg address cell, we shall fix that by killing struct
  554. * reg_property and using some accessor functions instead
  555. */
  556. hose->first_busno = 0xf0;
  557. hose->last_busno = 0xff;
  558. has_uninorth = 1;
  559. hose->ops = &macrisc_pci_ops;
  560. hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
  561. hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
  562. u3_agp = hose;
  563. }
  564. static void __init setup_u3_ht(struct pci_controller* hose)
  565. {
  566. struct device_node *np = (struct device_node *)hose->arch_data;
  567. int i, cur;
  568. hose->ops = &u3_ht_pci_ops;
  569. /* We hard code the address because of the different size of
  570. * the reg address cell, we shall fix that by killing struct
  571. * reg_property and using some accessor functions instead
  572. */
  573. hose->cfg_data = (volatile unsigned char *)ioremap(0xf2000000, 0x02000000);
  574. /*
  575. * /ht node doesn't expose a "ranges" property, so we "remove" regions that
  576. * have been allocated to AGP. So far, this version of the code doesn't assign
  577. * any of the 0xfxxxxxxx "fine" memory regions to /ht.
  578. * We need to fix that sooner or later by either parsing all child "ranges"
  579. * properties or figuring out the U3 address space decoding logic and
  580. * then read its configuration register (if any).
  581. */
  582. hose->io_base_phys = 0xf4000000;
  583. hose->pci_io_size = 0x00400000;
  584. hose->io_resource.name = np->full_name;
  585. hose->io_resource.start = 0;
  586. hose->io_resource.end = 0x003fffff;
  587. hose->io_resource.flags = IORESOURCE_IO;
  588. hose->pci_mem_offset = 0;
  589. hose->first_busno = 0;
  590. hose->last_busno = 0xef;
  591. hose->mem_resources[0].name = np->full_name;
  592. hose->mem_resources[0].start = 0x80000000;
  593. hose->mem_resources[0].end = 0xefffffff;
  594. hose->mem_resources[0].flags = IORESOURCE_MEM;
  595. u3_ht = hose;
  596. if (u3_agp == NULL) {
  597. DBG("U3 has no AGP, using full resource range\n");
  598. return;
  599. }
  600. /* We "remove" the AGP resources from the resources allocated to HT, that
  601. * is we create "holes". However, that code does assumptions that so far
  602. * happen to be true (cross fingers...), typically that resources in the
  603. * AGP node are properly ordered
  604. */
  605. cur = 0;
  606. for (i=0; i<3; i++) {
  607. struct resource *res = &u3_agp->mem_resources[i];
  608. if (res->flags != IORESOURCE_MEM)
  609. continue;
  610. /* We don't care about "fine" resources */
  611. if (res->start >= 0xf0000000)
  612. continue;
  613. /* Check if it's just a matter of "shrinking" us in one direction */
  614. if (hose->mem_resources[cur].start == res->start) {
  615. DBG("U3/HT: shrink start of %d, %08lx -> %08lx\n",
  616. cur, hose->mem_resources[cur].start, res->end + 1);
  617. hose->mem_resources[cur].start = res->end + 1;
  618. continue;
  619. }
  620. if (hose->mem_resources[cur].end == res->end) {
  621. DBG("U3/HT: shrink end of %d, %08lx -> %08lx\n",
  622. cur, hose->mem_resources[cur].end, res->start - 1);
  623. hose->mem_resources[cur].end = res->start - 1;
  624. continue;
  625. }
  626. /* No, it's not the case, we need a hole */
  627. if (cur == 2) {
  628. /* not enough resources for a hole, we drop part of the range */
  629. printk(KERN_WARNING "Running out of resources for /ht host !\n");
  630. hose->mem_resources[cur].end = res->start - 1;
  631. continue;
  632. }
  633. cur++;
  634. DBG("U3/HT: hole, %d end at %08lx, %d start at %08lx\n",
  635. cur-1, res->start - 1, cur, res->end + 1);
  636. hose->mem_resources[cur].name = np->full_name;
  637. hose->mem_resources[cur].flags = IORESOURCE_MEM;
  638. hose->mem_resources[cur].start = res->end + 1;
  639. hose->mem_resources[cur].end = hose->mem_resources[cur-1].end;
  640. hose->mem_resources[cur-1].end = res->start - 1;
  641. }
  642. }
  643. /* XXX this needs to be converged between ppc32 and ppc64... */
  644. static struct pci_controller * __init pcibios_alloc_controller(void)
  645. {
  646. struct pci_controller *hose;
  647. hose = alloc_bootmem(sizeof(struct pci_controller));
  648. if (hose)
  649. pci_setup_pci_controller(hose);
  650. return hose;
  651. }
  652. #endif
  653. /*
  654. * We assume that if we have a G3 powermac, we have one bridge called
  655. * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
  656. * if we have one or more bandit or chaos bridges, we don't have a MPC106.
  657. */
  658. static int __init add_bridge(struct device_node *dev)
  659. {
  660. int len;
  661. struct pci_controller *hose;
  662. #ifdef CONFIG_PPC32
  663. struct reg_property *addr;
  664. #endif
  665. char *disp_name;
  666. int *bus_range;
  667. int primary = 1;
  668. DBG("Adding PCI host bridge %s\n", dev->full_name);
  669. #ifdef CONFIG_PPC32
  670. /* XXX fix this */
  671. addr = (struct reg_property *) get_property(dev, "reg", &len);
  672. if (addr == NULL || len < sizeof(*addr)) {
  673. printk(KERN_WARNING "Can't use %s: no address\n",
  674. dev->full_name);
  675. return -ENODEV;
  676. }
  677. #endif
  678. bus_range = (int *) get_property(dev, "bus-range", &len);
  679. if (bus_range == NULL || len < 2 * sizeof(int)) {
  680. printk(KERN_WARNING "Can't get bus-range for %s, assume bus 0\n",
  681. dev->full_name);
  682. }
  683. hose = pcibios_alloc_controller();
  684. if (!hose)
  685. return -ENOMEM;
  686. hose->arch_data = dev;
  687. hose->first_busno = bus_range ? bus_range[0] : 0;
  688. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  689. disp_name = NULL;
  690. #ifdef CONFIG_POWER4
  691. if (device_is_compatible(dev, "u3-agp")) {
  692. setup_u3_agp(hose);
  693. disp_name = "U3-AGP";
  694. primary = 0;
  695. } else if (device_is_compatible(dev, "u3-ht")) {
  696. setup_u3_ht(hose);
  697. disp_name = "U3-HT";
  698. primary = 1;
  699. }
  700. printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number: %d->%d\n",
  701. disp_name, hose->first_busno, hose->last_busno);
  702. #else
  703. if (device_is_compatible(dev, "uni-north")) {
  704. primary = setup_uninorth(hose, addr);
  705. disp_name = "UniNorth";
  706. } else if (strcmp(dev->name, "pci") == 0) {
  707. /* XXX assume this is a mpc106 (grackle) */
  708. setup_grackle(hose);
  709. disp_name = "Grackle (MPC106)";
  710. } else if (strcmp(dev->name, "bandit") == 0) {
  711. setup_bandit(hose, addr);
  712. disp_name = "Bandit";
  713. } else if (strcmp(dev->name, "chaos") == 0) {
  714. setup_chaos(hose, addr);
  715. disp_name = "Chaos";
  716. primary = 0;
  717. }
  718. printk(KERN_INFO "Found %s PCI host bridge at 0x%08lx. Firmware bus number: %d->%d\n",
  719. disp_name, addr->address, hose->first_busno, hose->last_busno);
  720. #endif
  721. DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  722. hose, hose->cfg_addr, hose->cfg_data);
  723. /* Interpret the "ranges" property */
  724. /* This also maps the I/O region and sets isa_io/mem_base */
  725. pci_process_bridge_OF_ranges(hose, dev, primary);
  726. /* Fixup "bus-range" OF property */
  727. fixup_bus_range(dev);
  728. return 0;
  729. }
  730. static void __init
  731. pcibios_fixup_OF_interrupts(void)
  732. {
  733. struct pci_dev* dev = NULL;
  734. /*
  735. * Open Firmware often doesn't initialize the
  736. * PCI_INTERRUPT_LINE config register properly, so we
  737. * should find the device node and apply the interrupt
  738. * obtained from the OF device-tree
  739. */
  740. for_each_pci_dev(dev) {
  741. struct device_node *node;
  742. node = pci_device_to_OF_node(dev);
  743. /* this is the node, see if it has interrupts */
  744. if (node && node->n_intrs > 0)
  745. dev->irq = node->intrs[0].line;
  746. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
  747. }
  748. }
  749. void __init
  750. pmac_pcibios_fixup(void)
  751. {
  752. /* Fixup interrupts according to OF tree */
  753. pcibios_fixup_OF_interrupts();
  754. }
  755. #ifdef CONFIG_PPC64
  756. static void __init pmac_fixup_phb_resources(void)
  757. {
  758. struct pci_controller *hose, *tmp;
  759. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  760. printk(KERN_INFO "PCI Host %d, io start: %lx; io end: %lx\n",
  761. hose->global_number,
  762. hose->io_resource.start, hose->io_resource.end);
  763. }
  764. }
  765. #endif
  766. void __init pmac_pci_init(void)
  767. {
  768. struct device_node *np, *root;
  769. struct device_node *ht = NULL;
  770. root = of_find_node_by_path("/");
  771. if (root == NULL) {
  772. printk(KERN_CRIT "pmac_pci_init: can't find root "
  773. "of device tree\n");
  774. return;
  775. }
  776. for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
  777. if (np->name == NULL)
  778. continue;
  779. if (strcmp(np->name, "bandit") == 0
  780. || strcmp(np->name, "chaos") == 0
  781. || strcmp(np->name, "pci") == 0) {
  782. if (add_bridge(np) == 0)
  783. of_node_get(np);
  784. }
  785. if (strcmp(np->name, "ht") == 0) {
  786. of_node_get(np);
  787. ht = np;
  788. }
  789. }
  790. of_node_put(root);
  791. #ifdef CONFIG_PPC64
  792. /* Probe HT last as it relies on the agp resources to be already
  793. * setup
  794. */
  795. if (ht && add_bridge(ht) != 0)
  796. of_node_put(ht);
  797. /*
  798. * We need to call pci_setup_phb_io for the HT bridge first
  799. * so it gets the I/O port numbers starting at 0, and we
  800. * need to call it for the AGP bridge after that so it gets
  801. * small positive I/O port numbers.
  802. */
  803. if (u3_ht)
  804. pci_setup_phb_io(u3_ht, 1);
  805. if (u3_agp)
  806. pci_setup_phb_io(u3_agp, 0);
  807. /*
  808. * On ppc64, fixup the IO resources on our host bridges as
  809. * the common code does it only for children of the host bridges
  810. */
  811. pmac_fixup_phb_resources();
  812. /* Setup the linkage between OF nodes and PHBs */
  813. pci_devs_phb_init();
  814. /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
  815. * assume there is no P2P bridge on the AGP bus, which should be a
  816. * safe assumptions hopefully.
  817. */
  818. if (u3_agp) {
  819. struct device_node *np = u3_agp->arch_data;
  820. PCI_DN(np)->busno = 0xf0;
  821. for (np = np->child; np; np = np->sibling)
  822. PCI_DN(np)->busno = 0xf0;
  823. }
  824. /* map in PCI I/O space */
  825. phbs_remap_io();
  826. /* pmac_check_ht_link(); */
  827. /* Tell pci.c to not use the common resource allocation mechanism */
  828. pci_probe_only = 1;
  829. /* Allow all IO */
  830. io_page_mask = -1;
  831. #else /* CONFIG_PPC64 */
  832. init_p2pbridge();
  833. fixup_nec_usb2();
  834. /* We are still having some issues with the Xserve G4, enabling
  835. * some offset between bus number and domains for now when we
  836. * assign all busses should help for now
  837. */
  838. if (pci_assign_all_buses)
  839. pcibios_assign_bus_offset = 0x10;
  840. #endif
  841. }
  842. int
  843. pmac_pci_enable_device_hook(struct pci_dev *dev, int initial)
  844. {
  845. struct device_node* node;
  846. int updatecfg = 0;
  847. int uninorth_child;
  848. node = pci_device_to_OF_node(dev);
  849. /* We don't want to enable USB controllers absent from the OF tree
  850. * (iBook second controller)
  851. */
  852. if (dev->vendor == PCI_VENDOR_ID_APPLE
  853. && (dev->class == ((PCI_CLASS_SERIAL_USB << 8) | 0x10))
  854. && !node) {
  855. printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
  856. pci_name(dev));
  857. return -EINVAL;
  858. }
  859. if (!node)
  860. return 0;
  861. uninorth_child = node->parent &&
  862. device_is_compatible(node->parent, "uni-north");
  863. /* Firewire & GMAC were disabled after PCI probe, the driver is
  864. * claiming them, we must re-enable them now.
  865. */
  866. if (uninorth_child && !strcmp(node->name, "firewire") &&
  867. (device_is_compatible(node, "pci106b,18") ||
  868. device_is_compatible(node, "pci106b,30") ||
  869. device_is_compatible(node, "pci11c1,5811"))) {
  870. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
  871. pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
  872. updatecfg = 1;
  873. }
  874. if (uninorth_child && !strcmp(node->name, "ethernet") &&
  875. device_is_compatible(node, "gmac")) {
  876. pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
  877. updatecfg = 1;
  878. }
  879. if (updatecfg) {
  880. u16 cmd;
  881. /*
  882. * Make sure PCI is correctly configured
  883. *
  884. * We use old pci_bios versions of the function since, by
  885. * default, gmac is not powered up, and so will be absent
  886. * from the kernel initial PCI lookup.
  887. *
  888. * Should be replaced by 2.4 new PCI mechanisms and really
  889. * register the device.
  890. */
  891. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  892. cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
  893. | PCI_COMMAND_INVALIDATE;
  894. pci_write_config_word(dev, PCI_COMMAND, cmd);
  895. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
  896. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  897. L1_CACHE_BYTES >> 2);
  898. }
  899. return 0;
  900. }
  901. /* We power down some devices after they have been probed. They'll
  902. * be powered back on later on
  903. */
  904. void __init pmac_pcibios_after_init(void)
  905. {
  906. struct device_node* nd;
  907. #ifdef CONFIG_BLK_DEV_IDE
  908. struct pci_dev *dev = NULL;
  909. /* OF fails to initialize IDE controllers on macs
  910. * (and maybe other machines)
  911. *
  912. * Ideally, this should be moved to the IDE layer, but we need
  913. * to check specifically with Andre Hedrick how to do it cleanly
  914. * since the common IDE code seem to care about the fact that the
  915. * BIOS may have disabled a controller.
  916. *
  917. * -- BenH
  918. */
  919. for_each_pci_dev(dev) {
  920. if ((dev->class >> 16) == PCI_BASE_CLASS_STORAGE)
  921. pci_enable_device(dev);
  922. }
  923. #endif /* CONFIG_BLK_DEV_IDE */
  924. nd = find_devices("firewire");
  925. while (nd) {
  926. if (nd->parent && (device_is_compatible(nd, "pci106b,18") ||
  927. device_is_compatible(nd, "pci106b,30") ||
  928. device_is_compatible(nd, "pci11c1,5811"))
  929. && device_is_compatible(nd->parent, "uni-north")) {
  930. pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
  931. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
  932. }
  933. nd = nd->next;
  934. }
  935. nd = find_devices("ethernet");
  936. while (nd) {
  937. if (nd->parent && device_is_compatible(nd, "gmac")
  938. && device_is_compatible(nd->parent, "uni-north"))
  939. pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
  940. nd = nd->next;
  941. }
  942. }
  943. #ifdef CONFIG_PPC32
  944. void pmac_pci_fixup_cardbus(struct pci_dev* dev)
  945. {
  946. if (_machine != _MACH_Pmac)
  947. return;
  948. /*
  949. * Fix the interrupt routing on the various cardbus bridges
  950. * used on powerbooks
  951. */
  952. if (dev->vendor != PCI_VENDOR_ID_TI)
  953. return;
  954. if (dev->device == PCI_DEVICE_ID_TI_1130 ||
  955. dev->device == PCI_DEVICE_ID_TI_1131) {
  956. u8 val;
  957. /* Enable PCI interrupt */
  958. if (pci_read_config_byte(dev, 0x91, &val) == 0)
  959. pci_write_config_byte(dev, 0x91, val | 0x30);
  960. /* Disable ISA interrupt mode */
  961. if (pci_read_config_byte(dev, 0x92, &val) == 0)
  962. pci_write_config_byte(dev, 0x92, val & ~0x06);
  963. }
  964. if (dev->device == PCI_DEVICE_ID_TI_1210 ||
  965. dev->device == PCI_DEVICE_ID_TI_1211 ||
  966. dev->device == PCI_DEVICE_ID_TI_1410 ||
  967. dev->device == PCI_DEVICE_ID_TI_1510) {
  968. u8 val;
  969. /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA
  970. signal out the MFUNC0 pin */
  971. if (pci_read_config_byte(dev, 0x8c, &val) == 0)
  972. pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
  973. /* Disable ISA interrupt mode */
  974. if (pci_read_config_byte(dev, 0x92, &val) == 0)
  975. pci_write_config_byte(dev, 0x92, val & ~0x06);
  976. }
  977. }
  978. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
  979. void pmac_pci_fixup_pciata(struct pci_dev* dev)
  980. {
  981. u8 progif = 0;
  982. /*
  983. * On PowerMacs, we try to switch any PCI ATA controller to
  984. * fully native mode
  985. */
  986. if (_machine != _MACH_Pmac)
  987. return;
  988. /* Some controllers don't have the class IDE */
  989. if (dev->vendor == PCI_VENDOR_ID_PROMISE)
  990. switch(dev->device) {
  991. case PCI_DEVICE_ID_PROMISE_20246:
  992. case PCI_DEVICE_ID_PROMISE_20262:
  993. case PCI_DEVICE_ID_PROMISE_20263:
  994. case PCI_DEVICE_ID_PROMISE_20265:
  995. case PCI_DEVICE_ID_PROMISE_20267:
  996. case PCI_DEVICE_ID_PROMISE_20268:
  997. case PCI_DEVICE_ID_PROMISE_20269:
  998. case PCI_DEVICE_ID_PROMISE_20270:
  999. case PCI_DEVICE_ID_PROMISE_20271:
  1000. case PCI_DEVICE_ID_PROMISE_20275:
  1001. case PCI_DEVICE_ID_PROMISE_20276:
  1002. case PCI_DEVICE_ID_PROMISE_20277:
  1003. goto good;
  1004. }
  1005. /* Others, check PCI class */
  1006. if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
  1007. return;
  1008. good:
  1009. pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  1010. if ((progif & 5) != 5) {
  1011. printk(KERN_INFO "Forcing PCI IDE into native mode: %s\n", pci_name(dev));
  1012. (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
  1013. if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
  1014. (progif & 5) != 5)
  1015. printk(KERN_ERR "Rewrite of PROGIF failed !\n");
  1016. }
  1017. }
  1018. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
  1019. #endif
  1020. /*
  1021. * Disable second function on K2-SATA, it's broken
  1022. * and disable IO BARs on first one
  1023. */
  1024. static void fixup_k2_sata(struct pci_dev* dev)
  1025. {
  1026. int i;
  1027. u16 cmd;
  1028. if (PCI_FUNC(dev->devfn) > 0) {
  1029. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1030. cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  1031. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1032. for (i = 0; i < 6; i++) {
  1033. dev->resource[i].start = dev->resource[i].end = 0;
  1034. dev->resource[i].flags = 0;
  1035. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
  1036. }
  1037. } else {
  1038. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1039. cmd &= ~PCI_COMMAND_IO;
  1040. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1041. for (i = 0; i < 5; i++) {
  1042. dev->resource[i].start = dev->resource[i].end = 0;
  1043. dev->resource[i].flags = 0;
  1044. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
  1045. }
  1046. }
  1047. }
  1048. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);