pci.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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,
  574. 0x02000000);
  575. /*
  576. * /ht node doesn't expose a "ranges" property, so we "remove"
  577. * regions that have been allocated to AGP. So far, this version of
  578. * the code doesn't assign any of the 0xfxxxxxxx "fine" memory regions
  579. * to /ht. We need to fix that sooner or later by either parsing all
  580. * child "ranges" properties or figuring out the U3 address space
  581. * decoding logic and then read its configuration register (if any).
  582. */
  583. hose->io_base_phys = 0xf4000000;
  584. hose->pci_io_size = 0x00400000;
  585. hose->io_resource.name = np->full_name;
  586. hose->io_resource.start = 0;
  587. hose->io_resource.end = 0x003fffff;
  588. hose->io_resource.flags = IORESOURCE_IO;
  589. hose->pci_mem_offset = 0;
  590. hose->first_busno = 0;
  591. hose->last_busno = 0xef;
  592. hose->mem_resources[0].name = np->full_name;
  593. hose->mem_resources[0].start = 0x80000000;
  594. hose->mem_resources[0].end = 0xefffffff;
  595. hose->mem_resources[0].flags = IORESOURCE_MEM;
  596. u3_ht = hose;
  597. if (u3_agp == NULL) {
  598. DBG("U3 has no AGP, using full resource range\n");
  599. return;
  600. }
  601. /* We "remove" the AGP resources from the resources allocated to HT,
  602. * that is we create "holes". However, that code does assumptions
  603. * that so far happen to be true (cross fingers...), typically that
  604. * resources in the AGP node are properly ordered
  605. */
  606. cur = 0;
  607. for (i=0; i<3; i++) {
  608. struct resource *res = &u3_agp->mem_resources[i];
  609. if (res->flags != IORESOURCE_MEM)
  610. continue;
  611. /* We don't care about "fine" resources */
  612. if (res->start >= 0xf0000000)
  613. continue;
  614. /* Check if it's just a matter of "shrinking" us in one
  615. * direction
  616. */
  617. if (hose->mem_resources[cur].start == res->start) {
  618. DBG("U3/HT: shrink start of %d, %08lx -> %08lx\n",
  619. cur, hose->mem_resources[cur].start,
  620. res->end + 1);
  621. hose->mem_resources[cur].start = res->end + 1;
  622. continue;
  623. }
  624. if (hose->mem_resources[cur].end == res->end) {
  625. DBG("U3/HT: shrink end of %d, %08lx -> %08lx\n",
  626. cur, hose->mem_resources[cur].end,
  627. res->start - 1);
  628. hose->mem_resources[cur].end = res->start - 1;
  629. continue;
  630. }
  631. /* No, it's not the case, we need a hole */
  632. if (cur == 2) {
  633. /* not enough resources for a hole, we drop part
  634. * of the range
  635. */
  636. printk(KERN_WARNING "Running out of resources"
  637. " for /ht host !\n");
  638. hose->mem_resources[cur].end = res->start - 1;
  639. continue;
  640. }
  641. cur++;
  642. DBG("U3/HT: hole, %d end at %08lx, %d start at %08lx\n",
  643. cur-1, res->start - 1, cur, res->end + 1);
  644. hose->mem_resources[cur].name = np->full_name;
  645. hose->mem_resources[cur].flags = IORESOURCE_MEM;
  646. hose->mem_resources[cur].start = res->end + 1;
  647. hose->mem_resources[cur].end = hose->mem_resources[cur-1].end;
  648. hose->mem_resources[cur-1].end = res->start - 1;
  649. }
  650. }
  651. #endif
  652. /*
  653. * We assume that if we have a G3 powermac, we have one bridge called
  654. * "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,
  655. * if we have one or more bandit or chaos bridges, we don't have a MPC106.
  656. */
  657. static int __init add_bridge(struct device_node *dev)
  658. {
  659. int len;
  660. struct pci_controller *hose;
  661. #ifdef CONFIG_PPC32
  662. struct reg_property *addr;
  663. #endif
  664. char *disp_name;
  665. int *bus_range;
  666. int primary = 1;
  667. DBG("Adding PCI host bridge %s\n", dev->full_name);
  668. #ifdef CONFIG_PPC32
  669. /* XXX fix this */
  670. addr = (struct reg_property *) get_property(dev, "reg", &len);
  671. if (addr == NULL || len < sizeof(*addr)) {
  672. printk(KERN_WARNING "Can't use %s: no address\n",
  673. dev->full_name);
  674. return -ENODEV;
  675. }
  676. #endif
  677. bus_range = (int *) get_property(dev, "bus-range", &len);
  678. if (bus_range == NULL || len < 2 * sizeof(int)) {
  679. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  680. " bus 0\n", dev->full_name);
  681. }
  682. /* XXX Different prototypes, to be merged */
  683. #ifdef CONFIG_PPC64
  684. hose = pcibios_alloc_controller(dev);
  685. #else
  686. hose = pcibios_alloc_controller();
  687. #endif
  688. if (!hose)
  689. return -ENOMEM;
  690. hose->arch_data = dev;
  691. hose->first_busno = bus_range ? bus_range[0] : 0;
  692. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  693. disp_name = NULL;
  694. #ifdef CONFIG_PPC64
  695. if (device_is_compatible(dev, "u3-agp")) {
  696. setup_u3_agp(hose);
  697. disp_name = "U3-AGP";
  698. primary = 0;
  699. } else if (device_is_compatible(dev, "u3-ht")) {
  700. setup_u3_ht(hose);
  701. disp_name = "U3-HT";
  702. primary = 1;
  703. }
  704. printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number: %d->%d\n",
  705. disp_name, hose->first_busno, hose->last_busno);
  706. #else
  707. if (device_is_compatible(dev, "uni-north")) {
  708. primary = setup_uninorth(hose, addr);
  709. disp_name = "UniNorth";
  710. } else if (strcmp(dev->name, "pci") == 0) {
  711. /* XXX assume this is a mpc106 (grackle) */
  712. setup_grackle(hose);
  713. disp_name = "Grackle (MPC106)";
  714. } else if (strcmp(dev->name, "bandit") == 0) {
  715. setup_bandit(hose, addr);
  716. disp_name = "Bandit";
  717. } else if (strcmp(dev->name, "chaos") == 0) {
  718. setup_chaos(hose, addr);
  719. disp_name = "Chaos";
  720. primary = 0;
  721. }
  722. printk(KERN_INFO "Found %s PCI host bridge at 0x%08lx. Firmware bus number: %d->%d\n",
  723. disp_name, addr->address, hose->first_busno, hose->last_busno);
  724. #endif
  725. DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
  726. hose, hose->cfg_addr, hose->cfg_data);
  727. /* Interpret the "ranges" property */
  728. /* This also maps the I/O region and sets isa_io/mem_base */
  729. pci_process_bridge_OF_ranges(hose, dev, primary);
  730. /* Fixup "bus-range" OF property */
  731. fixup_bus_range(dev);
  732. return 0;
  733. }
  734. static void __init
  735. pcibios_fixup_OF_interrupts(void)
  736. {
  737. struct pci_dev* dev = NULL;
  738. /*
  739. * Open Firmware often doesn't initialize the
  740. * PCI_INTERRUPT_LINE config register properly, so we
  741. * should find the device node and apply the interrupt
  742. * obtained from the OF device-tree
  743. */
  744. for_each_pci_dev(dev) {
  745. struct device_node *node;
  746. node = pci_device_to_OF_node(dev);
  747. /* this is the node, see if it has interrupts */
  748. if (node && node->n_intrs > 0)
  749. dev->irq = node->intrs[0].line;
  750. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
  751. }
  752. }
  753. void __init
  754. pmac_pcibios_fixup(void)
  755. {
  756. /* Fixup interrupts according to OF tree */
  757. pcibios_fixup_OF_interrupts();
  758. }
  759. #ifdef CONFIG_PPC64
  760. static void __init pmac_fixup_phb_resources(void)
  761. {
  762. struct pci_controller *hose, *tmp;
  763. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  764. printk(KERN_INFO "PCI Host %d, io start: %lx; io end: %lx\n",
  765. hose->global_number,
  766. hose->io_resource.start, hose->io_resource.end);
  767. }
  768. }
  769. #endif
  770. void __init pmac_pci_init(void)
  771. {
  772. struct device_node *np, *root;
  773. struct device_node *ht = NULL;
  774. root = of_find_node_by_path("/");
  775. if (root == NULL) {
  776. printk(KERN_CRIT "pmac_pci_init: can't find root "
  777. "of device tree\n");
  778. return;
  779. }
  780. for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
  781. if (np->name == NULL)
  782. continue;
  783. if (strcmp(np->name, "bandit") == 0
  784. || strcmp(np->name, "chaos") == 0
  785. || strcmp(np->name, "pci") == 0) {
  786. if (add_bridge(np) == 0)
  787. of_node_get(np);
  788. }
  789. if (strcmp(np->name, "ht") == 0) {
  790. of_node_get(np);
  791. ht = np;
  792. }
  793. }
  794. of_node_put(root);
  795. #ifdef CONFIG_PPC64
  796. /* Probe HT last as it relies on the agp resources to be already
  797. * setup
  798. */
  799. if (ht && add_bridge(ht) != 0)
  800. of_node_put(ht);
  801. /*
  802. * We need to call pci_setup_phb_io for the HT bridge first
  803. * so it gets the I/O port numbers starting at 0, and we
  804. * need to call it for the AGP bridge after that so it gets
  805. * small positive I/O port numbers.
  806. */
  807. if (u3_ht)
  808. pci_setup_phb_io(u3_ht, 1);
  809. if (u3_agp)
  810. pci_setup_phb_io(u3_agp, 0);
  811. /*
  812. * On ppc64, fixup the IO resources on our host bridges as
  813. * the common code does it only for children of the host bridges
  814. */
  815. pmac_fixup_phb_resources();
  816. /* Setup the linkage between OF nodes and PHBs */
  817. pci_devs_phb_init();
  818. /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
  819. * assume there is no P2P bridge on the AGP bus, which should be a
  820. * safe assumptions hopefully.
  821. */
  822. if (u3_agp) {
  823. struct device_node *np = u3_agp->arch_data;
  824. PCI_DN(np)->busno = 0xf0;
  825. for (np = np->child; np; np = np->sibling)
  826. PCI_DN(np)->busno = 0xf0;
  827. }
  828. /* pmac_check_ht_link(); */
  829. /* Tell pci.c to not use the common resource allocation mechanism */
  830. pci_probe_only = 1;
  831. /* Allow all IO */
  832. io_page_mask = -1;
  833. #else /* CONFIG_PPC64 */
  834. init_p2pbridge();
  835. fixup_nec_usb2();
  836. /* We are still having some issues with the Xserve G4, enabling
  837. * some offset between bus number and domains for now when we
  838. * assign all busses should help for now
  839. */
  840. if (pci_assign_all_buses)
  841. pcibios_assign_bus_offset = 0x10;
  842. #endif
  843. }
  844. int
  845. pmac_pci_enable_device_hook(struct pci_dev *dev, int initial)
  846. {
  847. struct device_node* node;
  848. int updatecfg = 0;
  849. int uninorth_child;
  850. node = pci_device_to_OF_node(dev);
  851. /* We don't want to enable USB controllers absent from the OF tree
  852. * (iBook second controller)
  853. */
  854. if (dev->vendor == PCI_VENDOR_ID_APPLE
  855. && (dev->class == ((PCI_CLASS_SERIAL_USB << 8) | 0x10))
  856. && !node) {
  857. printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
  858. pci_name(dev));
  859. return -EINVAL;
  860. }
  861. if (!node)
  862. return 0;
  863. uninorth_child = node->parent &&
  864. device_is_compatible(node->parent, "uni-north");
  865. /* Firewire & GMAC were disabled after PCI probe, the driver is
  866. * claiming them, we must re-enable them now.
  867. */
  868. if (uninorth_child && !strcmp(node->name, "firewire") &&
  869. (device_is_compatible(node, "pci106b,18") ||
  870. device_is_compatible(node, "pci106b,30") ||
  871. device_is_compatible(node, "pci11c1,5811"))) {
  872. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
  873. pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
  874. updatecfg = 1;
  875. }
  876. if (uninorth_child && !strcmp(node->name, "ethernet") &&
  877. device_is_compatible(node, "gmac")) {
  878. pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
  879. updatecfg = 1;
  880. }
  881. if (updatecfg) {
  882. u16 cmd;
  883. /*
  884. * Make sure PCI is correctly configured
  885. *
  886. * We use old pci_bios versions of the function since, by
  887. * default, gmac is not powered up, and so will be absent
  888. * from the kernel initial PCI lookup.
  889. *
  890. * Should be replaced by 2.4 new PCI mechanisms and really
  891. * register the device.
  892. */
  893. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  894. cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
  895. | PCI_COMMAND_INVALIDATE;
  896. pci_write_config_word(dev, PCI_COMMAND, cmd);
  897. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
  898. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  899. L1_CACHE_BYTES >> 2);
  900. }
  901. return 0;
  902. }
  903. /* We power down some devices after they have been probed. They'll
  904. * be powered back on later on
  905. */
  906. void __init pmac_pcibios_after_init(void)
  907. {
  908. struct device_node* nd;
  909. #ifdef CONFIG_BLK_DEV_IDE
  910. struct pci_dev *dev = NULL;
  911. /* OF fails to initialize IDE controllers on macs
  912. * (and maybe other machines)
  913. *
  914. * Ideally, this should be moved to the IDE layer, but we need
  915. * to check specifically with Andre Hedrick how to do it cleanly
  916. * since the common IDE code seem to care about the fact that the
  917. * BIOS may have disabled a controller.
  918. *
  919. * -- BenH
  920. */
  921. for_each_pci_dev(dev) {
  922. if ((dev->class >> 16) == PCI_BASE_CLASS_STORAGE)
  923. pci_enable_device(dev);
  924. }
  925. #endif /* CONFIG_BLK_DEV_IDE */
  926. nd = find_devices("firewire");
  927. while (nd) {
  928. if (nd->parent && (device_is_compatible(nd, "pci106b,18") ||
  929. device_is_compatible(nd, "pci106b,30") ||
  930. device_is_compatible(nd, "pci11c1,5811"))
  931. && device_is_compatible(nd->parent, "uni-north")) {
  932. pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
  933. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
  934. }
  935. nd = nd->next;
  936. }
  937. nd = find_devices("ethernet");
  938. while (nd) {
  939. if (nd->parent && device_is_compatible(nd, "gmac")
  940. && device_is_compatible(nd->parent, "uni-north"))
  941. pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
  942. nd = nd->next;
  943. }
  944. }
  945. #ifdef CONFIG_PPC32
  946. void pmac_pci_fixup_cardbus(struct pci_dev* dev)
  947. {
  948. if (_machine != _MACH_Pmac)
  949. return;
  950. /*
  951. * Fix the interrupt routing on the various cardbus bridges
  952. * used on powerbooks
  953. */
  954. if (dev->vendor != PCI_VENDOR_ID_TI)
  955. return;
  956. if (dev->device == PCI_DEVICE_ID_TI_1130 ||
  957. dev->device == PCI_DEVICE_ID_TI_1131) {
  958. u8 val;
  959. /* Enable PCI interrupt */
  960. if (pci_read_config_byte(dev, 0x91, &val) == 0)
  961. pci_write_config_byte(dev, 0x91, val | 0x30);
  962. /* Disable ISA interrupt mode */
  963. if (pci_read_config_byte(dev, 0x92, &val) == 0)
  964. pci_write_config_byte(dev, 0x92, val & ~0x06);
  965. }
  966. if (dev->device == PCI_DEVICE_ID_TI_1210 ||
  967. dev->device == PCI_DEVICE_ID_TI_1211 ||
  968. dev->device == PCI_DEVICE_ID_TI_1410 ||
  969. dev->device == PCI_DEVICE_ID_TI_1510) {
  970. u8 val;
  971. /* 0x8c == TI122X_IRQMUX, 2 says to route the INTA
  972. signal out the MFUNC0 pin */
  973. if (pci_read_config_byte(dev, 0x8c, &val) == 0)
  974. pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
  975. /* Disable ISA interrupt mode */
  976. if (pci_read_config_byte(dev, 0x92, &val) == 0)
  977. pci_write_config_byte(dev, 0x92, val & ~0x06);
  978. }
  979. }
  980. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
  981. void pmac_pci_fixup_pciata(struct pci_dev* dev)
  982. {
  983. u8 progif = 0;
  984. /*
  985. * On PowerMacs, we try to switch any PCI ATA controller to
  986. * fully native mode
  987. */
  988. if (_machine != _MACH_Pmac)
  989. return;
  990. /* Some controllers don't have the class IDE */
  991. if (dev->vendor == PCI_VENDOR_ID_PROMISE)
  992. switch(dev->device) {
  993. case PCI_DEVICE_ID_PROMISE_20246:
  994. case PCI_DEVICE_ID_PROMISE_20262:
  995. case PCI_DEVICE_ID_PROMISE_20263:
  996. case PCI_DEVICE_ID_PROMISE_20265:
  997. case PCI_DEVICE_ID_PROMISE_20267:
  998. case PCI_DEVICE_ID_PROMISE_20268:
  999. case PCI_DEVICE_ID_PROMISE_20269:
  1000. case PCI_DEVICE_ID_PROMISE_20270:
  1001. case PCI_DEVICE_ID_PROMISE_20271:
  1002. case PCI_DEVICE_ID_PROMISE_20275:
  1003. case PCI_DEVICE_ID_PROMISE_20276:
  1004. case PCI_DEVICE_ID_PROMISE_20277:
  1005. goto good;
  1006. }
  1007. /* Others, check PCI class */
  1008. if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
  1009. return;
  1010. good:
  1011. pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  1012. if ((progif & 5) != 5) {
  1013. printk(KERN_INFO "Forcing PCI IDE into native mode: %s\n", pci_name(dev));
  1014. (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
  1015. if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
  1016. (progif & 5) != 5)
  1017. printk(KERN_ERR "Rewrite of PROGIF failed !\n");
  1018. }
  1019. }
  1020. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
  1021. #endif
  1022. /*
  1023. * Disable second function on K2-SATA, it's broken
  1024. * and disable IO BARs on first one
  1025. */
  1026. static void fixup_k2_sata(struct pci_dev* dev)
  1027. {
  1028. int i;
  1029. u16 cmd;
  1030. if (PCI_FUNC(dev->devfn) > 0) {
  1031. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1032. cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  1033. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1034. for (i = 0; i < 6; i++) {
  1035. dev->resource[i].start = dev->resource[i].end = 0;
  1036. dev->resource[i].flags = 0;
  1037. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
  1038. }
  1039. } else {
  1040. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  1041. cmd &= ~PCI_COMMAND_IO;
  1042. pci_write_config_word(dev, PCI_COMMAND, cmd);
  1043. for (i = 0; i < 5; i++) {
  1044. dev->resource[i].start = dev->resource[i].end = 0;
  1045. dev->resource[i].flags = 0;
  1046. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i, 0);
  1047. }
  1048. }
  1049. }
  1050. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);