pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org),
  3. * IBM Corp.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. */
  10. #define DEBUG
  11. #include <linux/kernel.h>
  12. #include <linux/pci.h>
  13. #include <linux/delay.h>
  14. #include <linux/string.h>
  15. #include <linux/init.h>
  16. #include <linux/bootmem.h>
  17. #include <asm/sections.h>
  18. #include <asm/io.h>
  19. #include <asm/prom.h>
  20. #include <asm/pci-bridge.h>
  21. #include <asm/machdep.h>
  22. #include <asm/iommu.h>
  23. #include <asm/ppc-pci.h>
  24. #include "maple.h"
  25. #ifdef DEBUG
  26. #define DBG(x...) printk(x)
  27. #else
  28. #define DBG(x...)
  29. #endif
  30. static struct pci_controller *u3_agp, *u3_ht;
  31. static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
  32. {
  33. for (; node != 0;node = node->sibling) {
  34. int * bus_range;
  35. unsigned int *class_code;
  36. int len;
  37. /* For PCI<->PCI bridges or CardBus bridges, we go down */
  38. class_code = (unsigned int *) get_property(node, "class-code", NULL);
  39. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  40. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  41. continue;
  42. bus_range = (int *) get_property(node, "bus-range", &len);
  43. if (bus_range != NULL && len > 2 * sizeof(int)) {
  44. if (bus_range[1] > higher)
  45. higher = bus_range[1];
  46. }
  47. higher = fixup_one_level_bus_range(node->child, higher);
  48. }
  49. return higher;
  50. }
  51. /* This routine fixes the "bus-range" property of all bridges in the
  52. * system since they tend to have their "last" member wrong on macs
  53. *
  54. * Note that the bus numbers manipulated here are OF bus numbers, they
  55. * are not Linux bus numbers.
  56. */
  57. static void __init fixup_bus_range(struct device_node *bridge)
  58. {
  59. int * bus_range;
  60. int len;
  61. /* Lookup the "bus-range" property for the hose */
  62. bus_range = (int *) get_property(bridge, "bus-range", &len);
  63. if (bus_range == NULL || len < 2 * sizeof(int)) {
  64. printk(KERN_WARNING "Can't get bus-range for %s\n",
  65. bridge->full_name);
  66. return;
  67. }
  68. bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
  69. }
  70. #define U3_AGP_CFA0(devfn, off) \
  71. ((1 << (unsigned long)PCI_SLOT(dev_fn)) \
  72. | (((unsigned long)PCI_FUNC(dev_fn)) << 8) \
  73. | (((unsigned long)(off)) & 0xFCUL))
  74. #define U3_AGP_CFA1(bus, devfn, off) \
  75. ((((unsigned long)(bus)) << 16) \
  76. |(((unsigned long)(devfn)) << 8) \
  77. |(((unsigned long)(off)) & 0xFCUL) \
  78. |1UL)
  79. static unsigned long u3_agp_cfg_access(struct pci_controller* hose,
  80. u8 bus, u8 dev_fn, u8 offset)
  81. {
  82. unsigned int caddr;
  83. if (bus == hose->first_busno) {
  84. if (dev_fn < (11 << 3))
  85. return 0;
  86. caddr = U3_AGP_CFA0(dev_fn, offset);
  87. } else
  88. caddr = U3_AGP_CFA1(bus, dev_fn, offset);
  89. /* Uninorth will return garbage if we don't read back the value ! */
  90. do {
  91. out_le32(hose->cfg_addr, caddr);
  92. } while (in_le32(hose->cfg_addr) != caddr);
  93. offset &= 0x07;
  94. return ((unsigned long)hose->cfg_data) + offset;
  95. }
  96. static int u3_agp_read_config(struct pci_bus *bus, unsigned int devfn,
  97. int offset, int len, u32 *val)
  98. {
  99. struct pci_controller *hose;
  100. unsigned long addr;
  101. hose = pci_bus_to_host(bus);
  102. if (hose == NULL)
  103. return PCIBIOS_DEVICE_NOT_FOUND;
  104. addr = u3_agp_cfg_access(hose, bus->number, devfn, offset);
  105. if (!addr)
  106. return PCIBIOS_DEVICE_NOT_FOUND;
  107. /*
  108. * Note: the caller has already checked that offset is
  109. * suitably aligned and that len is 1, 2 or 4.
  110. */
  111. switch (len) {
  112. case 1:
  113. *val = in_8((u8 *)addr);
  114. break;
  115. case 2:
  116. *val = in_le16((u16 *)addr);
  117. break;
  118. default:
  119. *val = in_le32((u32 *)addr);
  120. break;
  121. }
  122. return PCIBIOS_SUCCESSFUL;
  123. }
  124. static int u3_agp_write_config(struct pci_bus *bus, unsigned int devfn,
  125. int offset, int len, u32 val)
  126. {
  127. struct pci_controller *hose;
  128. unsigned long addr;
  129. hose = pci_bus_to_host(bus);
  130. if (hose == NULL)
  131. return PCIBIOS_DEVICE_NOT_FOUND;
  132. addr = u3_agp_cfg_access(hose, bus->number, devfn, offset);
  133. if (!addr)
  134. return PCIBIOS_DEVICE_NOT_FOUND;
  135. /*
  136. * Note: the caller has already checked that offset is
  137. * suitably aligned and that len is 1, 2 or 4.
  138. */
  139. switch (len) {
  140. case 1:
  141. out_8((u8 *)addr, val);
  142. (void) in_8((u8 *)addr);
  143. break;
  144. case 2:
  145. out_le16((u16 *)addr, val);
  146. (void) in_le16((u16 *)addr);
  147. break;
  148. default:
  149. out_le32((u32 *)addr, val);
  150. (void) in_le32((u32 *)addr);
  151. break;
  152. }
  153. return PCIBIOS_SUCCESSFUL;
  154. }
  155. static struct pci_ops u3_agp_pci_ops =
  156. {
  157. u3_agp_read_config,
  158. u3_agp_write_config
  159. };
  160. #define U3_HT_CFA0(devfn, off) \
  161. ((((unsigned long)devfn) << 8) | offset)
  162. #define U3_HT_CFA1(bus, devfn, off) \
  163. (U3_HT_CFA0(devfn, off) \
  164. + (((unsigned long)bus) << 16) \
  165. + 0x01000000UL)
  166. static unsigned long u3_ht_cfg_access(struct pci_controller* hose,
  167. u8 bus, u8 devfn, u8 offset)
  168. {
  169. if (bus == hose->first_busno) {
  170. if (PCI_SLOT(devfn) == 0)
  171. return 0;
  172. return ((unsigned long)hose->cfg_data) + U3_HT_CFA0(devfn, offset);
  173. } else
  174. return ((unsigned long)hose->cfg_data) + U3_HT_CFA1(bus, devfn, offset);
  175. }
  176. static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
  177. int offset, int len, u32 *val)
  178. {
  179. struct pci_controller *hose;
  180. unsigned long addr;
  181. hose = pci_bus_to_host(bus);
  182. if (hose == NULL)
  183. return PCIBIOS_DEVICE_NOT_FOUND;
  184. addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
  185. if (!addr)
  186. return PCIBIOS_DEVICE_NOT_FOUND;
  187. /*
  188. * Note: the caller has already checked that offset is
  189. * suitably aligned and that len is 1, 2 or 4.
  190. */
  191. switch (len) {
  192. case 1:
  193. *val = in_8((u8 *)addr);
  194. break;
  195. case 2:
  196. *val = in_le16((u16 *)addr);
  197. break;
  198. default:
  199. *val = in_le32((u32 *)addr);
  200. break;
  201. }
  202. return PCIBIOS_SUCCESSFUL;
  203. }
  204. static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
  205. int offset, int len, u32 val)
  206. {
  207. struct pci_controller *hose;
  208. unsigned long addr;
  209. hose = pci_bus_to_host(bus);
  210. if (hose == NULL)
  211. return PCIBIOS_DEVICE_NOT_FOUND;
  212. addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
  213. if (!addr)
  214. return PCIBIOS_DEVICE_NOT_FOUND;
  215. /*
  216. * Note: the caller has already checked that offset is
  217. * suitably aligned and that len is 1, 2 or 4.
  218. */
  219. switch (len) {
  220. case 1:
  221. out_8((u8 *)addr, val);
  222. (void) in_8((u8 *)addr);
  223. break;
  224. case 2:
  225. out_le16((u16 *)addr, val);
  226. (void) in_le16((u16 *)addr);
  227. break;
  228. default:
  229. out_le32((u32 *)addr, val);
  230. (void) in_le32((u32 *)addr);
  231. break;
  232. }
  233. return PCIBIOS_SUCCESSFUL;
  234. }
  235. static struct pci_ops u3_ht_pci_ops =
  236. {
  237. u3_ht_read_config,
  238. u3_ht_write_config
  239. };
  240. static void __init setup_u3_agp(struct pci_controller* hose)
  241. {
  242. /* On G5, we move AGP up to high bus number so we don't need
  243. * to reassign bus numbers for HT. If we ever have P2P bridges
  244. * on AGP, we'll have to move pci_assign_all_buses to the
  245. * pci_controller structure so we enable it for AGP and not for
  246. * HT childs.
  247. * We hard code the address because of the different size of
  248. * the reg address cell, we shall fix that by killing struct
  249. * reg_property and using some accessor functions instead
  250. */
  251. hose->first_busno = 0xf0;
  252. hose->last_busno = 0xff;
  253. hose->ops = &u3_agp_pci_ops;
  254. hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
  255. hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
  256. u3_agp = hose;
  257. }
  258. static void __init setup_u3_ht(struct pci_controller* hose)
  259. {
  260. hose->ops = &u3_ht_pci_ops;
  261. /* We hard code the address because of the different size of
  262. * the reg address cell, we shall fix that by killing struct
  263. * reg_property and using some accessor functions instead
  264. */
  265. hose->cfg_data = (volatile unsigned char *)ioremap(0xf2000000, 0x02000000);
  266. hose->first_busno = 0;
  267. hose->last_busno = 0xef;
  268. u3_ht = hose;
  269. }
  270. static int __init add_bridge(struct device_node *dev)
  271. {
  272. int len;
  273. struct pci_controller *hose;
  274. char* disp_name;
  275. int *bus_range;
  276. int primary = 1;
  277. DBG("Adding PCI host bridge %s\n", dev->full_name);
  278. bus_range = (int *) get_property(dev, "bus-range", &len);
  279. if (bus_range == NULL || len < 2 * sizeof(int)) {
  280. printk(KERN_WARNING "Can't get bus-range for %s, assume bus 0\n",
  281. dev->full_name);
  282. }
  283. hose = pcibios_alloc_controller(dev);
  284. if (hose == NULL)
  285. return -ENOMEM;
  286. hose->first_busno = bus_range ? bus_range[0] : 0;
  287. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  288. disp_name = NULL;
  289. if (device_is_compatible(dev, "u3-agp")) {
  290. setup_u3_agp(hose);
  291. disp_name = "U3-AGP";
  292. primary = 0;
  293. } else if (device_is_compatible(dev, "u3-ht")) {
  294. setup_u3_ht(hose);
  295. disp_name = "U3-HT";
  296. primary = 1;
  297. }
  298. printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number: %d->%d\n",
  299. disp_name, hose->first_busno, hose->last_busno);
  300. /* Interpret the "ranges" property */
  301. /* This also maps the I/O region and sets isa_io/mem_base */
  302. pci_process_bridge_OF_ranges(hose, dev, primary);
  303. pci_setup_phb_io(hose, primary);
  304. /* Fixup "bus-range" OF property */
  305. fixup_bus_range(dev);
  306. return 0;
  307. }
  308. void __init maple_pcibios_fixup(void)
  309. {
  310. struct pci_dev *dev = NULL;
  311. DBG(" -> maple_pcibios_fixup\n");
  312. for_each_pci_dev(dev)
  313. pci_read_irq_line(dev);
  314. DBG(" <- maple_pcibios_fixup\n");
  315. }
  316. static void __init maple_fixup_phb_resources(void)
  317. {
  318. struct pci_controller *hose, *tmp;
  319. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  320. unsigned long offset = (unsigned long)hose->io_base_virt - pci_io_base;
  321. hose->io_resource.start += offset;
  322. hose->io_resource.end += offset;
  323. printk(KERN_INFO "PCI Host %d, io start: %lx; io end: %lx\n",
  324. hose->global_number,
  325. hose->io_resource.start, hose->io_resource.end);
  326. }
  327. }
  328. void __init maple_pci_init(void)
  329. {
  330. struct device_node *np, *root;
  331. struct device_node *ht = NULL;
  332. /* Probe root PCI hosts, that is on U3 the AGP host and the
  333. * HyperTransport host. That one is actually "kept" around
  334. * and actually added last as it's resource management relies
  335. * on the AGP resources to have been setup first
  336. */
  337. root = of_find_node_by_path("/");
  338. if (root == NULL) {
  339. printk(KERN_CRIT "maple_find_bridges: can't find root of device tree\n");
  340. return;
  341. }
  342. for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
  343. if (np->name == NULL)
  344. continue;
  345. if (strcmp(np->name, "pci") == 0) {
  346. if (add_bridge(np) == 0)
  347. of_node_get(np);
  348. }
  349. if (strcmp(np->name, "ht") == 0) {
  350. of_node_get(np);
  351. ht = np;
  352. }
  353. }
  354. of_node_put(root);
  355. /* Now setup the HyperTransport host if we found any
  356. */
  357. if (ht && add_bridge(ht) != 0)
  358. of_node_put(ht);
  359. /* Fixup the IO resources on our host bridges as the common code
  360. * does it only for childs of the host bridges
  361. */
  362. maple_fixup_phb_resources();
  363. /* Setup the linkage between OF nodes and PHBs */
  364. pci_devs_phb_init();
  365. /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
  366. * assume there is no P2P bridge on the AGP bus, which should be a
  367. * safe assumptions hopefully.
  368. */
  369. if (u3_agp) {
  370. struct device_node *np = u3_agp->arch_data;
  371. PCI_DN(np)->busno = 0xf0;
  372. for (np = np->child; np; np = np->sibling)
  373. PCI_DN(np)->busno = 0xf0;
  374. }
  375. /* Tell pci.c to not change any resource allocations. */
  376. pci_probe_only = 1;
  377. /* Allow all IO */
  378. io_page_mask = -1;
  379. }
  380. int maple_pci_get_legacy_ide_irq(struct pci_dev *pdev, int channel)
  381. {
  382. struct device_node *np;
  383. int irq = channel ? 15 : 14;
  384. if (pdev->vendor != PCI_VENDOR_ID_AMD ||
  385. pdev->device != PCI_DEVICE_ID_AMD_8111_IDE)
  386. return irq;
  387. np = pci_device_to_OF_node(pdev);
  388. if (np == NULL)
  389. return irq;
  390. if (np->n_intrs < 2)
  391. return irq;
  392. return np->intrs[channel & 0x1].line;
  393. }
  394. /* XXX: To remove once all firmwares are ok */
  395. static void fixup_maple_ide(struct pci_dev* dev)
  396. {
  397. #if 0 /* Enable this to enable IDE port 0 */
  398. {
  399. u8 v;
  400. pci_read_config_byte(dev, 0x40, &v);
  401. v |= 2;
  402. pci_write_config_byte(dev, 0x40, v);
  403. }
  404. #endif
  405. #if 0 /* fix bus master base */
  406. pci_write_config_dword(dev, 0x20, 0xcc01);
  407. printk("old ide resource: %lx -> %lx \n",
  408. dev->resource[4].start, dev->resource[4].end);
  409. dev->resource[4].start = 0xcc00;
  410. dev->resource[4].end = 0xcc10;
  411. #endif
  412. #if 1 /* Enable this to fixup IDE sense/polarity of irqs in IO-APICs */
  413. {
  414. struct pci_dev *apicdev;
  415. u32 v;
  416. apicdev = pci_get_slot (dev->bus, PCI_DEVFN(5,0));
  417. if (apicdev == NULL)
  418. printk("IDE Fixup IRQ: Can't find IO-APIC !\n");
  419. else {
  420. pci_write_config_byte(apicdev, 0xf2, 0x10 + 2*14);
  421. pci_read_config_dword(apicdev, 0xf4, &v);
  422. v &= ~0x00000022;
  423. pci_write_config_dword(apicdev, 0xf4, v);
  424. pci_write_config_byte(apicdev, 0xf2, 0x10 + 2*15);
  425. pci_read_config_dword(apicdev, 0xf4, &v);
  426. v &= ~0x00000022;
  427. pci_write_config_dword(apicdev, 0xf4, v);
  428. pci_dev_put(apicdev);
  429. }
  430. }
  431. #endif
  432. }
  433. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_IDE,
  434. fixup_maple_ide);