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 resource *addr)
  252. {
  253. /* assume a `chaos' bridge */
  254. hose->ops = &chaos_pci_ops;
  255. hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
  256. hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
  257. }
  258. #endif /* CONFIG_PPC32 */
  259. #ifdef CONFIG_PPC64
  260. /*
  261. * These versions of U3 HyperTransport config space access ops do not
  262. * implement self-view of the HT host yet
  263. */
  264. /*
  265. * This function deals with some "special cases" devices.
  266. *
  267. * 0 -> No special case
  268. * 1 -> Skip the device but act as if the access was successfull
  269. * (return 0xff's on reads, eventually, cache config space
  270. * accesses in a later version)
  271. * -1 -> Hide the device (unsuccessful acess)
  272. */
  273. static int u3_ht_skip_device(struct pci_controller *hose,
  274. struct pci_bus *bus, unsigned int devfn)
  275. {
  276. struct device_node *busdn, *dn;
  277. int i;
  278. /* We only allow config cycles to devices that are in OF device-tree
  279. * as we are apparently having some weird things going on with some
  280. * revs of K2 on recent G5s
  281. */
  282. if (bus->self)
  283. busdn = pci_device_to_OF_node(bus->self);
  284. else
  285. busdn = hose->arch_data;
  286. for (dn = busdn->child; dn; dn = dn->sibling)
  287. if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)
  288. break;
  289. if (dn == NULL)
  290. return -1;
  291. /*
  292. * When a device in K2 is powered down, we die on config
  293. * cycle accesses. Fix that here.
  294. */
  295. for (i=0; i<2; i++)
  296. if (k2_skiplist[i] == dn)
  297. return 1;
  298. return 0;
  299. }
  300. #define U3_HT_CFA0(devfn, off) \
  301. ((((unsigned long)devfn) << 8) | offset)
  302. #define U3_HT_CFA1(bus, devfn, off) \
  303. (U3_HT_CFA0(devfn, off) \
  304. + (((unsigned long)bus) << 16) \
  305. + 0x01000000UL)
  306. static unsigned long u3_ht_cfg_access(struct pci_controller* hose,
  307. u8 bus, u8 devfn, u8 offset)
  308. {
  309. if (bus == hose->first_busno) {
  310. /* For now, we don't self probe U3 HT bridge */
  311. if (PCI_SLOT(devfn) == 0)
  312. return 0;
  313. return ((unsigned long)hose->cfg_data) +
  314. U3_HT_CFA0(devfn, offset);
  315. } else
  316. return ((unsigned long)hose->cfg_data) +
  317. 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"
  476. " control\n");
  477. return;
  478. }
  479. val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;
  480. early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);
  481. }
  482. /*
  483. * Some Apple desktop machines have a NEC PD720100A USB2 controller
  484. * on the motherboard. Open Firmware, on these, will disable the
  485. * EHCI part of it so it behaves like a pair of OHCI's. This fixup
  486. * code re-enables it ;)
  487. */
  488. static void __init fixup_nec_usb2(void)
  489. {
  490. struct device_node *nec;
  491. for (nec = NULL; (nec = of_find_node_by_name(nec, "usb")) != NULL;) {
  492. struct pci_controller *hose;
  493. u32 data, *prop;
  494. u8 bus, devfn;
  495. prop = (u32 *)get_property(nec, "vendor-id", NULL);
  496. if (prop == NULL)
  497. continue;
  498. if (0x1033 != *prop)
  499. continue;
  500. prop = (u32 *)get_property(nec, "device-id", NULL);
  501. if (prop == NULL)
  502. continue;
  503. if (0x0035 != *prop)
  504. continue;
  505. prop = (u32 *)get_property(nec, "reg", NULL);
  506. if (prop == NULL)
  507. continue;
  508. devfn = (prop[0] >> 8) & 0xff;
  509. bus = (prop[0] >> 16) & 0xff;
  510. if (PCI_FUNC(devfn) != 0)
  511. continue;
  512. hose = pci_find_hose_for_OF_device(nec);
  513. if (!hose)
  514. continue;
  515. early_read_config_dword(hose, bus, devfn, 0xe4, &data);
  516. if (data & 1UL) {
  517. printk("Found NEC PD720100A USB2 chip with disabled"
  518. " EHCI, fixing up...\n");
  519. data &= ~1UL;
  520. early_write_config_dword(hose, bus, devfn, 0xe4, data);
  521. early_write_config_byte(hose, bus,
  522. devfn | 2, PCI_INTERRUPT_LINE,
  523. nec->intrs[0].line);
  524. }
  525. }
  526. }
  527. static void __init setup_bandit(struct pci_controller *hose,
  528. struct resource *addr)
  529. {
  530. hose->ops = &macrisc_pci_ops;
  531. hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
  532. hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
  533. init_bandit(hose);
  534. }
  535. static int __init setup_uninorth(struct pci_controller *hose,
  536. struct resource *addr)
  537. {
  538. pci_assign_all_buses = 1;
  539. has_uninorth = 1;
  540. hose->ops = &macrisc_pci_ops;
  541. hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
  542. hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
  543. /* We "know" that the bridge at f2000000 has the PCI slots. */
  544. return addr->start == 0xf2000000;
  545. }
  546. #endif /* CONFIG_PPC32 */
  547. #ifdef CONFIG_PPC64
  548. static void __init setup_u3_agp(struct pci_controller* hose)
  549. {
  550. /* On G5, we move AGP up to high bus number so we don't need
  551. * to reassign bus numbers for HT. If we ever have P2P bridges
  552. * on AGP, we'll have to move pci_assign_all_busses to the
  553. * pci_controller structure so we enable it for AGP and not for
  554. * HT childs.
  555. * We hard code the address because of the different size of
  556. * the reg address cell, we shall fix that by killing struct
  557. * reg_property and using some accessor functions instead
  558. */
  559. hose->first_busno = 0xf0;
  560. hose->last_busno = 0xff;
  561. has_uninorth = 1;
  562. hose->ops = &macrisc_pci_ops;
  563. hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
  564. hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
  565. u3_agp = hose;
  566. }
  567. static void __init setup_u3_ht(struct pci_controller* hose)
  568. {
  569. struct device_node *np = (struct device_node *)hose->arch_data;
  570. int i, cur;
  571. hose->ops = &u3_ht_pci_ops;
  572. /* We hard code the address because of the different size of
  573. * the reg address cell, we shall fix that by killing struct
  574. * reg_property and using some accessor functions instead
  575. */
  576. hose->cfg_data = (volatile unsigned char *)ioremap(0xf2000000,
  577. 0x02000000);
  578. /*
  579. * /ht node doesn't expose a "ranges" property, so we "remove"
  580. * regions that have been allocated to AGP. So far, this version of
  581. * the code doesn't assign any of the 0xfxxxxxxx "fine" memory regions
  582. * to /ht. We need to fix that sooner or later by either parsing all
  583. * child "ranges" properties or figuring out the U3 address space
  584. * decoding logic and then read its configuration register (if any).
  585. */
  586. hose->io_base_phys = 0xf4000000;
  587. hose->pci_io_size = 0x00400000;
  588. hose->io_resource.name = np->full_name;
  589. hose->io_resource.start = 0;
  590. hose->io_resource.end = 0x003fffff;
  591. hose->io_resource.flags = IORESOURCE_IO;
  592. hose->pci_mem_offset = 0;
  593. hose->first_busno = 0;
  594. hose->last_busno = 0xef;
  595. hose->mem_resources[0].name = np->full_name;
  596. hose->mem_resources[0].start = 0x80000000;
  597. hose->mem_resources[0].end = 0xefffffff;
  598. hose->mem_resources[0].flags = IORESOURCE_MEM;
  599. u3_ht = hose;
  600. if (u3_agp == NULL) {
  601. DBG("U3 has no AGP, using full resource range\n");
  602. return;
  603. }
  604. /* We "remove" the AGP resources from the resources allocated to HT,
  605. * that is we create "holes". However, that code does assumptions
  606. * that so far happen to be true (cross fingers...), typically that
  607. * resources in the AGP node are properly ordered
  608. */
  609. cur = 0;
  610. for (i=0; i<3; i++) {
  611. struct resource *res = &u3_agp->mem_resources[i];
  612. if (res->flags != IORESOURCE_MEM)
  613. continue;
  614. /* We don't care about "fine" resources */
  615. if (res->start >= 0xf0000000)
  616. continue;
  617. /* Check if it's just a matter of "shrinking" us in one
  618. * direction
  619. */
  620. if (hose->mem_resources[cur].start == res->start) {
  621. DBG("U3/HT: shrink start of %d, %08lx -> %08lx\n",
  622. cur, hose->mem_resources[cur].start,
  623. res->end + 1);
  624. hose->mem_resources[cur].start = res->end + 1;
  625. continue;
  626. }
  627. if (hose->mem_resources[cur].end == res->end) {
  628. DBG("U3/HT: shrink end of %d, %08lx -> %08lx\n",
  629. cur, hose->mem_resources[cur].end,
  630. res->start - 1);
  631. hose->mem_resources[cur].end = res->start - 1;
  632. continue;
  633. }
  634. /* No, it's not the case, we need a hole */
  635. if (cur == 2) {
  636. /* not enough resources for a hole, we drop part
  637. * of the range
  638. */
  639. printk(KERN_WARNING "Running out of resources"
  640. " for /ht host !\n");
  641. hose->mem_resources[cur].end = res->start - 1;
  642. continue;
  643. }
  644. cur++;
  645. DBG("U3/HT: hole, %d end at %08lx, %d start at %08lx\n",
  646. cur-1, res->start - 1, cur, res->end + 1);
  647. hose->mem_resources[cur].name = np->full_name;
  648. hose->mem_resources[cur].flags = IORESOURCE_MEM;
  649. hose->mem_resources[cur].start = res->end + 1;
  650. hose->mem_resources[cur].end = hose->mem_resources[cur-1].end;
  651. hose->mem_resources[cur-1].end = res->start - 1;
  652. }
  653. }
  654. #endif /* CONFIG_PPC64 */
  655. /*
  656. * We assume that if we have a G3 powermac, we have one bridge called
  657. * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
  658. * if we have one or more bandit or chaos bridges, we don't have a MPC106.
  659. */
  660. static int __init add_bridge(struct device_node *dev)
  661. {
  662. int len;
  663. struct pci_controller *hose;
  664. struct resource rsrc;
  665. char *disp_name;
  666. int *bus_range;
  667. int primary = 1, has_address = 0;
  668. DBG("Adding PCI host bridge %s\n", dev->full_name);
  669. /* Fetch host bridge registers address */
  670. has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
  671. /* Get bus range if any */
  672. bus_range = (int *) get_property(dev, "bus-range", &len);
  673. if (bus_range == NULL || len < 2 * sizeof(int)) {
  674. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  675. " bus 0\n", dev->full_name);
  676. }
  677. /* XXX Different prototypes, to be merged */
  678. #ifdef CONFIG_PPC64
  679. hose = pcibios_alloc_controller(dev);
  680. #else
  681. hose = pcibios_alloc_controller();
  682. #endif
  683. if (!hose)
  684. return -ENOMEM;
  685. hose->arch_data = dev;
  686. hose->first_busno = bus_range ? bus_range[0] : 0;
  687. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  688. disp_name = NULL;
  689. /* 64 bits only bridges */
  690. #ifdef CONFIG_PPC64
  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. #endif /* CONFIG_PPC64 */
  703. /* 32 bits only bridges */
  704. #ifdef CONFIG_PPC32
  705. if (device_is_compatible(dev, "uni-north")) {
  706. primary = setup_uninorth(hose, &rsrc);
  707. disp_name = "UniNorth";
  708. } else if (strcmp(dev->name, "pci") == 0) {
  709. /* XXX assume this is a mpc106 (grackle) */
  710. setup_grackle(hose);
  711. disp_name = "Grackle (MPC106)";
  712. } else if (strcmp(dev->name, "bandit") == 0) {
  713. setup_bandit(hose, &rsrc);
  714. disp_name = "Bandit";
  715. } else if (strcmp(dev->name, "chaos") == 0) {
  716. setup_chaos(hose, &rsrc);
  717. disp_name = "Chaos";
  718. primary = 0;
  719. }
  720. printk(KERN_INFO "Found %s PCI host bridge at 0x%08lx. "
  721. "Firmware bus number: %d->%d\n",
  722. disp_name, rsrc.start, hose->first_busno, hose->last_busno);
  723. #endif /* CONFIG_PPC32 */
  724. DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  725. hose, hose->cfg_addr, hose->cfg_data);
  726. /* Interpret the "ranges" property */
  727. /* This also maps the I/O region and sets isa_io/mem_base */
  728. pci_process_bridge_OF_ranges(hose, dev, primary);
  729. /* Fixup "bus-range" OF property */
  730. fixup_bus_range(dev);
  731. return 0;
  732. }
  733. static void __init pcibios_fixup_OF_interrupts(void)
  734. {
  735. struct pci_dev* dev = NULL;
  736. /*
  737. * Open Firmware often doesn't initialize the
  738. * PCI_INTERRUPT_LINE config register properly, so we
  739. * should find the device node and apply the interrupt
  740. * obtained from the OF device-tree
  741. */
  742. for_each_pci_dev(dev) {
  743. struct device_node *node;
  744. node = pci_device_to_OF_node(dev);
  745. /* this is the node, see if it has interrupts */
  746. if (node && node->n_intrs > 0)
  747. dev->irq = node->intrs[0].line;
  748. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
  749. }
  750. }
  751. void __init pmac_pcibios_fixup(void)
  752. {
  753. /* Fixup interrupts according to OF tree */
  754. pcibios_fixup_OF_interrupts();
  755. }
  756. #ifdef CONFIG_PPC64
  757. static void __init pmac_fixup_phb_resources(void)
  758. {
  759. struct pci_controller *hose, *tmp;
  760. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  761. printk(KERN_INFO "PCI Host %d, io start: %lx; io end: %lx\n",
  762. hose->global_number,
  763. hose->io_resource.start, hose->io_resource.end);
  764. }
  765. }
  766. #endif
  767. void __init pmac_pci_init(void)
  768. {
  769. struct device_node *np, *root;
  770. struct device_node *ht = NULL;
  771. root = of_find_node_by_path("/");
  772. if (root == NULL) {
  773. printk(KERN_CRIT "pmac_pci_init: can't find root "
  774. "of device tree\n");
  775. return;
  776. }
  777. for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
  778. if (np->name == NULL)
  779. continue;
  780. if (strcmp(np->name, "bandit") == 0
  781. || strcmp(np->name, "chaos") == 0
  782. || strcmp(np->name, "pci") == 0) {
  783. if (add_bridge(np) == 0)
  784. of_node_get(np);
  785. }
  786. if (strcmp(np->name, "ht") == 0) {
  787. of_node_get(np);
  788. ht = np;
  789. }
  790. }
  791. of_node_put(root);
  792. #ifdef CONFIG_PPC64
  793. /* Probe HT last as it relies on the agp resources to be already
  794. * setup
  795. */
  796. if (ht && add_bridge(ht) != 0)
  797. of_node_put(ht);
  798. /*
  799. * We need to call pci_setup_phb_io for the HT bridge first
  800. * so it gets the I/O port numbers starting at 0, and we
  801. * need to call it for the AGP bridge after that so it gets
  802. * small positive I/O port numbers.
  803. */
  804. if (u3_ht)
  805. pci_setup_phb_io(u3_ht, 1);
  806. if (u3_agp)
  807. pci_setup_phb_io(u3_agp, 0);
  808. /*
  809. * On ppc64, fixup the IO resources on our host bridges as
  810. * the common code does it only for children of the host bridges
  811. */
  812. pmac_fixup_phb_resources();
  813. /* Setup the linkage between OF nodes and PHBs */
  814. pci_devs_phb_init();
  815. /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
  816. * assume there is no P2P bridge on the AGP bus, which should be a
  817. * safe assumptions hopefully.
  818. */
  819. if (u3_agp) {
  820. struct device_node *np = u3_agp->arch_data;
  821. PCI_DN(np)->busno = 0xf0;
  822. for (np = np->child; np; np = np->sibling)
  823. PCI_DN(np)->busno = 0xf0;
  824. }
  825. /* pmac_check_ht_link(); */
  826. /* Tell pci.c to not use the common resource allocation mechanism */
  827. pci_probe_only = 1;
  828. /* Allow all IO */
  829. io_page_mask = -1;
  830. #else /* CONFIG_PPC64 */
  831. init_p2pbridge();
  832. fixup_nec_usb2();
  833. /* We are still having some issues with the Xserve G4, enabling
  834. * some offset between bus number and domains for now when we
  835. * assign all busses should help for now
  836. */
  837. if (pci_assign_all_buses)
  838. pcibios_assign_bus_offset = 0x10;
  839. #endif
  840. }
  841. int
  842. pmac_pci_enable_device_hook(struct pci_dev *dev, int initial)
  843. {
  844. struct device_node* node;
  845. int updatecfg = 0;
  846. int uninorth_child;
  847. node = pci_device_to_OF_node(dev);
  848. /* We don't want to enable USB controllers absent from the OF tree
  849. * (iBook second controller)
  850. */
  851. if (dev->vendor == PCI_VENDOR_ID_APPLE
  852. && (dev->class == ((PCI_CLASS_SERIAL_USB << 8) | 0x10))
  853. && !node) {
  854. printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
  855. pci_name(dev));
  856. return -EINVAL;
  857. }
  858. if (!node)
  859. return 0;
  860. uninorth_child = node->parent &&
  861. device_is_compatible(node->parent, "uni-north");
  862. /* Firewire & GMAC were disabled after PCI probe, the driver is
  863. * claiming them, we must re-enable them now.
  864. */
  865. if (uninorth_child && !strcmp(node->name, "firewire") &&
  866. (device_is_compatible(node, "pci106b,18") ||
  867. device_is_compatible(node, "pci106b,30") ||
  868. device_is_compatible(node, "pci11c1,5811"))) {
  869. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
  870. pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
  871. updatecfg = 1;
  872. }
  873. if (uninorth_child && !strcmp(node->name, "ethernet") &&
  874. device_is_compatible(node, "gmac")) {
  875. pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
  876. updatecfg = 1;
  877. }
  878. if (updatecfg) {
  879. u16 cmd;
  880. /*
  881. * Make sure PCI is correctly configured
  882. *
  883. * We use old pci_bios versions of the function since, by
  884. * default, gmac is not powered up, and so will be absent
  885. * from the kernel initial PCI lookup.
  886. *
  887. * Should be replaced by 2.4 new PCI mechanisms and really
  888. * register the device.
  889. */
  890. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  891. cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
  892. | PCI_COMMAND_INVALIDATE;
  893. pci_write_config_word(dev, PCI_COMMAND, cmd);
  894. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
  895. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  896. L1_CACHE_BYTES >> 2);
  897. }
  898. return 0;
  899. }
  900. /* We power down some devices after they have been probed. They'll
  901. * be powered back on later on
  902. */
  903. void __init pmac_pcibios_after_init(void)
  904. {
  905. struct device_node* nd;
  906. #ifdef CONFIG_BLK_DEV_IDE
  907. struct pci_dev *dev = NULL;
  908. /* OF fails to initialize IDE controllers on macs
  909. * (and maybe other machines)
  910. *
  911. * Ideally, this should be moved to the IDE layer, but we need
  912. * to check specifically with Andre Hedrick how to do it cleanly
  913. * since the common IDE code seem to care about the fact that the
  914. * BIOS may have disabled a controller.
  915. *
  916. * -- BenH
  917. */
  918. for_each_pci_dev(dev) {
  919. if ((dev->class >> 16) == PCI_BASE_CLASS_STORAGE)
  920. pci_enable_device(dev);
  921. }
  922. #endif /* CONFIG_BLK_DEV_IDE */
  923. nd = find_devices("firewire");
  924. while (nd) {
  925. if (nd->parent && (device_is_compatible(nd, "pci106b,18") ||
  926. device_is_compatible(nd, "pci106b,30") ||
  927. device_is_compatible(nd, "pci11c1,5811"))
  928. && device_is_compatible(nd->parent, "uni-north")) {
  929. pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
  930. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
  931. }
  932. nd = nd->next;
  933. }
  934. nd = find_devices("ethernet");
  935. while (nd) {
  936. if (nd->parent && device_is_compatible(nd, "gmac")
  937. && device_is_compatible(nd->parent, "uni-north"))
  938. pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
  939. nd = nd->next;
  940. }
  941. }
  942. #ifdef CONFIG_PPC32
  943. void pmac_pci_fixup_cardbus(struct pci_dev* dev)
  944. {
  945. if (_machine != _MACH_Pmac)
  946. return;
  947. /*
  948. * Fix the interrupt routing on the various cardbus bridges
  949. * used on powerbooks
  950. */
  951. if (dev->vendor != PCI_VENDOR_ID_TI)
  952. return;
  953. if (dev->device == PCI_DEVICE_ID_TI_1130 ||
  954. dev->device == PCI_DEVICE_ID_TI_1131) {
  955. u8 val;
  956. /* Enable PCI interrupt */
  957. if (pci_read_config_byte(dev, 0x91, &val) == 0)
  958. pci_write_config_byte(dev, 0x91, val | 0x30);
  959. /* Disable ISA interrupt mode */
  960. if (pci_read_config_byte(dev, 0x92, &val) == 0)
  961. pci_write_config_byte(dev, 0x92, val & ~0x06);
  962. }
  963. if (dev->device == PCI_DEVICE_ID_TI_1210 ||
  964. dev->device == PCI_DEVICE_ID_TI_1211 ||
  965. dev->device == PCI_DEVICE_ID_TI_1410 ||
  966. dev->device == PCI_DEVICE_ID_TI_1510) {
  967. u8 val;
  968. /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA
  969. signal out the MFUNC0 pin */
  970. if (pci_read_config_byte(dev, 0x8c, &val) == 0)
  971. pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
  972. /* Disable ISA interrupt mode */
  973. if (pci_read_config_byte(dev, 0x92, &val) == 0)
  974. pci_write_config_byte(dev, 0x92, val & ~0x06);
  975. }
  976. }
  977. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
  978. void pmac_pci_fixup_pciata(struct pci_dev* dev)
  979. {
  980. u8 progif = 0;
  981. /*
  982. * On PowerMacs, we try to switch any PCI ATA controller to
  983. * fully native mode
  984. */
  985. if (_machine != _MACH_Pmac)
  986. return;
  987. /* Some controllers don't have the class IDE */
  988. if (dev->vendor == PCI_VENDOR_ID_PROMISE)
  989. switch(dev->device) {
  990. case PCI_DEVICE_ID_PROMISE_20246:
  991. case PCI_DEVICE_ID_PROMISE_20262:
  992. case PCI_DEVICE_ID_PROMISE_20263:
  993. case PCI_DEVICE_ID_PROMISE_20265:
  994. case PCI_DEVICE_ID_PROMISE_20267:
  995. case PCI_DEVICE_ID_PROMISE_20268:
  996. case PCI_DEVICE_ID_PROMISE_20269:
  997. case PCI_DEVICE_ID_PROMISE_20270:
  998. case PCI_DEVICE_ID_PROMISE_20271:
  999. case PCI_DEVICE_ID_PROMISE_20275:
  1000. case PCI_DEVICE_ID_PROMISE_20276:
  1001. case PCI_DEVICE_ID_PROMISE_20277:
  1002. goto good;
  1003. }
  1004. /* Others, check PCI class */
  1005. if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
  1006. return;
  1007. good:
  1008. pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  1009. if ((progif & 5) != 5) {
  1010. printk(KERN_INFO "Forcing PCI IDE into native mode: %s\n", pci_name(dev));
  1011. (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
  1012. if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
  1013. (progif & 5) != 5)
  1014. printk(KERN_ERR "Rewrite of PROGIF failed !\n");
  1015. }
  1016. }
  1017. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
  1018. #endif
  1019. /*
  1020. * Disable second function on K2-SATA, it's broken
  1021. * and disable IO BARs on first one
  1022. */
  1023. static void fixup_k2_sata(struct pci_dev* dev)
  1024. {
  1025. int i;
  1026. u16 cmd;
  1027. if (PCI_FUNC(dev->devfn) > 0) {
  1028. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1029. cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  1030. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1031. for (i = 0; i < 6; i++) {
  1032. dev->resource[i].start = dev->resource[i].end = 0;
  1033. dev->resource[i].flags = 0;
  1034. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
  1035. }
  1036. } else {
  1037. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1038. cmd &= ~PCI_COMMAND_IO;
  1039. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1040. for (i = 0; i < 5; i++) {
  1041. dev->resource[i].start = dev->resource[i].end = 0;
  1042. dev->resource[i].flags = 0;
  1043. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
  1044. }
  1045. }
  1046. }
  1047. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);