pmac_pci.c 32 KB

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