pci.c 13 KB

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