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