pmac_pci.c 32 KB

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