pci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. #undef 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 <linux/irq.h>
  18. #include <asm/sections.h>
  19. #include <asm/io.h>
  20. #include <asm/prom.h>
  21. #include <asm/pci-bridge.h>
  22. #include <asm/machdep.h>
  23. #include <asm/iommu.h>
  24. #include <asm/ppc-pci.h>
  25. #include "maple.h"
  26. #ifdef DEBUG
  27. #define DBG(x...) printk(x)
  28. #else
  29. #define DBG(x...)
  30. #endif
  31. static struct pci_controller *u3_agp, *u3_ht, *u4_pcie;
  32. static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
  33. {
  34. for (; node != 0;node = node->sibling) {
  35. const int *bus_range;
  36. const unsigned int *class_code;
  37. int len;
  38. /* For PCI<->PCI bridges or CardBus bridges, we go down */
  39. class_code = of_get_property(node, "class-code", NULL);
  40. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  41. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  42. continue;
  43. bus_range = of_get_property(node, "bus-range", &len);
  44. if (bus_range != NULL && len > 2 * sizeof(int)) {
  45. if (bus_range[1] > higher)
  46. higher = bus_range[1];
  47. }
  48. higher = fixup_one_level_bus_range(node->child, higher);
  49. }
  50. return higher;
  51. }
  52. /* This routine fixes the "bus-range" property of all bridges in the
  53. * system since they tend to have their "last" member wrong on macs
  54. *
  55. * Note that the bus numbers manipulated here are OF bus numbers, they
  56. * are not Linux bus numbers.
  57. */
  58. static void __init fixup_bus_range(struct device_node *bridge)
  59. {
  60. int *bus_range;
  61. struct property *prop;
  62. int len;
  63. /* Lookup the "bus-range" property for the hose */
  64. prop = of_find_property(bridge, "bus-range", &len);
  65. if (prop == NULL || prop->value == NULL || len < 2 * sizeof(int)) {
  66. printk(KERN_WARNING "Can't get bus-range for %s\n",
  67. bridge->full_name);
  68. return;
  69. }
  70. bus_range = prop->value;
  71. bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
  72. }
  73. static unsigned long u3_agp_cfa0(u8 devfn, u8 off)
  74. {
  75. return (1 << (unsigned long)PCI_SLOT(devfn)) |
  76. ((unsigned long)PCI_FUNC(devfn) << 8) |
  77. ((unsigned long)off & 0xFCUL);
  78. }
  79. static unsigned long u3_agp_cfa1(u8 bus, u8 devfn, u8 off)
  80. {
  81. return ((unsigned long)bus << 16) |
  82. ((unsigned long)devfn << 8) |
  83. ((unsigned long)off & 0xFCUL) |
  84. 1UL;
  85. }
  86. static volatile void __iomem *u3_agp_cfg_access(struct pci_controller* hose,
  87. u8 bus, u8 dev_fn, u8 offset)
  88. {
  89. unsigned int caddr;
  90. if (bus == hose->first_busno) {
  91. if (dev_fn < (11 << 3))
  92. return NULL;
  93. caddr = u3_agp_cfa0(dev_fn, offset);
  94. } else
  95. caddr = u3_agp_cfa1(bus, dev_fn, offset);
  96. /* Uninorth will return garbage if we don't read back the value ! */
  97. do {
  98. out_le32(hose->cfg_addr, caddr);
  99. } while (in_le32(hose->cfg_addr) != caddr);
  100. offset &= 0x07;
  101. return hose->cfg_data + offset;
  102. }
  103. static int u3_agp_read_config(struct pci_bus *bus, unsigned int devfn,
  104. int offset, int len, u32 *val)
  105. {
  106. struct pci_controller *hose;
  107. volatile void __iomem *addr;
  108. hose = pci_bus_to_host(bus);
  109. if (hose == NULL)
  110. return PCIBIOS_DEVICE_NOT_FOUND;
  111. addr = u3_agp_cfg_access(hose, bus->number, devfn, offset);
  112. if (!addr)
  113. return PCIBIOS_DEVICE_NOT_FOUND;
  114. /*
  115. * Note: the caller has already checked that offset is
  116. * suitably aligned and that len is 1, 2 or 4.
  117. */
  118. switch (len) {
  119. case 1:
  120. *val = in_8(addr);
  121. break;
  122. case 2:
  123. *val = in_le16(addr);
  124. break;
  125. default:
  126. *val = in_le32(addr);
  127. break;
  128. }
  129. return PCIBIOS_SUCCESSFUL;
  130. }
  131. static int u3_agp_write_config(struct pci_bus *bus, unsigned int devfn,
  132. int offset, int len, u32 val)
  133. {
  134. struct pci_controller *hose;
  135. volatile void __iomem *addr;
  136. hose = pci_bus_to_host(bus);
  137. if (hose == NULL)
  138. return PCIBIOS_DEVICE_NOT_FOUND;
  139. addr = u3_agp_cfg_access(hose, bus->number, devfn, offset);
  140. if (!addr)
  141. return PCIBIOS_DEVICE_NOT_FOUND;
  142. /*
  143. * Note: the caller has already checked that offset is
  144. * suitably aligned and that len is 1, 2 or 4.
  145. */
  146. switch (len) {
  147. case 1:
  148. out_8(addr, val);
  149. break;
  150. case 2:
  151. out_le16(addr, val);
  152. break;
  153. default:
  154. out_le32(addr, val);
  155. break;
  156. }
  157. return PCIBIOS_SUCCESSFUL;
  158. }
  159. static struct pci_ops u3_agp_pci_ops =
  160. {
  161. .read = u3_agp_read_config,
  162. .write = u3_agp_write_config,
  163. };
  164. static unsigned long u3_ht_cfa0(u8 devfn, u8 off)
  165. {
  166. return (devfn << 8) | off;
  167. }
  168. static unsigned long u3_ht_cfa1(u8 bus, u8 devfn, u8 off)
  169. {
  170. return u3_ht_cfa0(devfn, off) + (bus << 16) + 0x01000000UL;
  171. }
  172. static volatile void __iomem *u3_ht_cfg_access(struct pci_controller* hose,
  173. u8 bus, u8 devfn, u8 offset)
  174. {
  175. if (bus == hose->first_busno) {
  176. if (PCI_SLOT(devfn) == 0)
  177. return NULL;
  178. return hose->cfg_data + u3_ht_cfa0(devfn, offset);
  179. } else
  180. return hose->cfg_data + u3_ht_cfa1(bus, devfn, offset);
  181. }
  182. static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
  183. int offset, int len, u32 *val)
  184. {
  185. struct pci_controller *hose;
  186. volatile void __iomem *addr;
  187. hose = pci_bus_to_host(bus);
  188. if (hose == NULL)
  189. return PCIBIOS_DEVICE_NOT_FOUND;
  190. if (offset > 0xff)
  191. return PCIBIOS_BAD_REGISTER_NUMBER;
  192. addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
  193. if (!addr)
  194. return PCIBIOS_DEVICE_NOT_FOUND;
  195. /*
  196. * Note: the caller has already checked that offset is
  197. * suitably aligned and that len is 1, 2 or 4.
  198. */
  199. switch (len) {
  200. case 1:
  201. *val = in_8(addr);
  202. break;
  203. case 2:
  204. *val = in_le16(addr);
  205. break;
  206. default:
  207. *val = in_le32(addr);
  208. break;
  209. }
  210. return PCIBIOS_SUCCESSFUL;
  211. }
  212. static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
  213. int offset, int len, u32 val)
  214. {
  215. struct pci_controller *hose;
  216. volatile void __iomem *addr;
  217. hose = pci_bus_to_host(bus);
  218. if (hose == NULL)
  219. return PCIBIOS_DEVICE_NOT_FOUND;
  220. if (offset > 0xff)
  221. return PCIBIOS_BAD_REGISTER_NUMBER;
  222. addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);
  223. if (!addr)
  224. return PCIBIOS_DEVICE_NOT_FOUND;
  225. /*
  226. * Note: the caller has already checked that offset is
  227. * suitably aligned and that len is 1, 2 or 4.
  228. */
  229. switch (len) {
  230. case 1:
  231. out_8(addr, val);
  232. break;
  233. case 2:
  234. out_le16(addr, val);
  235. break;
  236. default:
  237. out_le32(addr, val);
  238. break;
  239. }
  240. return PCIBIOS_SUCCESSFUL;
  241. }
  242. static struct pci_ops u3_ht_pci_ops =
  243. {
  244. .read = u3_ht_read_config,
  245. .write = u3_ht_write_config,
  246. };
  247. static unsigned int u4_pcie_cfa0(unsigned int devfn, unsigned int off)
  248. {
  249. return (1 << PCI_SLOT(devfn)) |
  250. (PCI_FUNC(devfn) << 8) |
  251. ((off >> 8) << 28) |
  252. (off & 0xfcu);
  253. }
  254. static unsigned int u4_pcie_cfa1(unsigned int bus, unsigned int devfn,
  255. unsigned int off)
  256. {
  257. return (bus << 16) |
  258. (devfn << 8) |
  259. ((off >> 8) << 28) |
  260. (off & 0xfcu) | 1u;
  261. }
  262. static volatile void __iomem *u4_pcie_cfg_access(struct pci_controller* hose,
  263. u8 bus, u8 dev_fn, int offset)
  264. {
  265. unsigned int caddr;
  266. if (bus == hose->first_busno)
  267. caddr = u4_pcie_cfa0(dev_fn, offset);
  268. else
  269. caddr = u4_pcie_cfa1(bus, dev_fn, offset);
  270. /* Uninorth will return garbage if we don't read back the value ! */
  271. do {
  272. out_le32(hose->cfg_addr, caddr);
  273. } while (in_le32(hose->cfg_addr) != caddr);
  274. offset &= 0x03;
  275. return hose->cfg_data + offset;
  276. }
  277. static int u4_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  278. int offset, int len, u32 *val)
  279. {
  280. struct pci_controller *hose;
  281. volatile void __iomem *addr;
  282. hose = pci_bus_to_host(bus);
  283. if (hose == NULL)
  284. return PCIBIOS_DEVICE_NOT_FOUND;
  285. if (offset >= 0x1000)
  286. return PCIBIOS_BAD_REGISTER_NUMBER;
  287. addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
  288. if (!addr)
  289. return PCIBIOS_DEVICE_NOT_FOUND;
  290. /*
  291. * Note: the caller has already checked that offset is
  292. * suitably aligned and that len is 1, 2 or 4.
  293. */
  294. switch (len) {
  295. case 1:
  296. *val = in_8(addr);
  297. break;
  298. case 2:
  299. *val = in_le16(addr);
  300. break;
  301. default:
  302. *val = in_le32(addr);
  303. break;
  304. }
  305. return PCIBIOS_SUCCESSFUL;
  306. }
  307. static int u4_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  308. int offset, int len, u32 val)
  309. {
  310. struct pci_controller *hose;
  311. volatile void __iomem *addr;
  312. hose = pci_bus_to_host(bus);
  313. if (hose == NULL)
  314. return PCIBIOS_DEVICE_NOT_FOUND;
  315. if (offset >= 0x1000)
  316. return PCIBIOS_BAD_REGISTER_NUMBER;
  317. addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);
  318. if (!addr)
  319. return PCIBIOS_DEVICE_NOT_FOUND;
  320. /*
  321. * Note: the caller has already checked that offset is
  322. * suitably aligned and that len is 1, 2 or 4.
  323. */
  324. switch (len) {
  325. case 1:
  326. out_8(addr, val);
  327. break;
  328. case 2:
  329. out_le16(addr, val);
  330. break;
  331. default:
  332. out_le32(addr, val);
  333. break;
  334. }
  335. return PCIBIOS_SUCCESSFUL;
  336. }
  337. static struct pci_ops u4_pcie_pci_ops =
  338. {
  339. .read = u4_pcie_read_config,
  340. .write = u4_pcie_write_config,
  341. };
  342. static void __init setup_u3_agp(struct pci_controller* hose)
  343. {
  344. /* On G5, we move AGP up to high bus number so we don't need
  345. * to reassign bus numbers for HT. If we ever have P2P bridges
  346. * on AGP, we'll have to move pci_assign_all_buses to the
  347. * pci_controller structure so we enable it for AGP and not for
  348. * HT childs.
  349. * We hard code the address because of the different size of
  350. * the reg address cell, we shall fix that by killing struct
  351. * reg_property and using some accessor functions instead
  352. */
  353. hose->first_busno = 0xf0;
  354. hose->last_busno = 0xff;
  355. hose->ops = &u3_agp_pci_ops;
  356. hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
  357. hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
  358. u3_agp = hose;
  359. }
  360. static void __init setup_u4_pcie(struct pci_controller* hose)
  361. {
  362. /* We currently only implement the "non-atomic" config space, to
  363. * be optimised later.
  364. */
  365. hose->ops = &u4_pcie_pci_ops;
  366. hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
  367. hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
  368. u4_pcie = hose;
  369. }
  370. static void __init setup_u3_ht(struct pci_controller* hose)
  371. {
  372. hose->ops = &u3_ht_pci_ops;
  373. /* We hard code the address because of the different size of
  374. * the reg address cell, we shall fix that by killing struct
  375. * reg_property and using some accessor functions instead
  376. */
  377. hose->cfg_data = ioremap(0xf2000000, 0x02000000);
  378. hose->first_busno = 0;
  379. hose->last_busno = 0xef;
  380. u3_ht = hose;
  381. }
  382. static int __init maple_add_bridge(struct device_node *dev)
  383. {
  384. int len;
  385. struct pci_controller *hose;
  386. char* disp_name;
  387. const int *bus_range;
  388. int primary = 1;
  389. DBG("Adding PCI host bridge %s\n", dev->full_name);
  390. bus_range = of_get_property(dev, "bus-range", &len);
  391. if (bus_range == NULL || len < 2 * sizeof(int)) {
  392. printk(KERN_WARNING "Can't get bus-range for %s, assume bus 0\n",
  393. dev->full_name);
  394. }
  395. hose = pcibios_alloc_controller(dev);
  396. if (hose == NULL)
  397. return -ENOMEM;
  398. hose->first_busno = bus_range ? bus_range[0] : 0;
  399. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  400. disp_name = NULL;
  401. if (of_device_is_compatible(dev, "u3-agp")) {
  402. setup_u3_agp(hose);
  403. disp_name = "U3-AGP";
  404. primary = 0;
  405. } else if (of_device_is_compatible(dev, "u3-ht")) {
  406. setup_u3_ht(hose);
  407. disp_name = "U3-HT";
  408. primary = 1;
  409. } else if (of_device_is_compatible(dev, "u4-pcie")) {
  410. setup_u4_pcie(hose);
  411. disp_name = "U4-PCIE";
  412. primary = 0;
  413. }
  414. printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number: %d->%d\n",
  415. disp_name, hose->first_busno, hose->last_busno);
  416. /* Interpret the "ranges" property */
  417. /* This also maps the I/O region and sets isa_io/mem_base */
  418. pci_process_bridge_OF_ranges(hose, dev, primary);
  419. /* Fixup "bus-range" OF property */
  420. fixup_bus_range(dev);
  421. /* Check for legacy IOs */
  422. isa_bridge_find_early(hose);
  423. return 0;
  424. }
  425. void __devinit maple_pci_irq_fixup(struct pci_dev *dev)
  426. {
  427. DBG(" -> maple_pci_irq_fixup\n");
  428. /* Fixup IRQ for PCIe host */
  429. if (u4_pcie != NULL && dev->bus->number == 0 &&
  430. pci_bus_to_host(dev->bus) == u4_pcie) {
  431. printk(KERN_DEBUG "Fixup U4 PCIe IRQ\n");
  432. dev->irq = irq_create_mapping(NULL, 1);
  433. if (dev->irq != NO_IRQ)
  434. set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);
  435. }
  436. /* Hide AMD8111 IDE interrupt when in legacy mode so
  437. * the driver calls pci_get_legacy_ide_irq()
  438. */
  439. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  440. dev->device == PCI_DEVICE_ID_AMD_8111_IDE &&
  441. (dev->class & 5) != 5) {
  442. dev->irq = NO_IRQ;
  443. }
  444. DBG(" <- maple_pci_irq_fixup\n");
  445. }
  446. void __init maple_pci_init(void)
  447. {
  448. struct device_node *np, *root;
  449. struct device_node *ht = NULL;
  450. /* Probe root PCI hosts, that is on U3 the AGP host and the
  451. * HyperTransport host. That one is actually "kept" around
  452. * and actually added last as it's resource management relies
  453. * on the AGP resources to have been setup first
  454. */
  455. root = of_find_node_by_path("/");
  456. if (root == NULL) {
  457. printk(KERN_CRIT "maple_find_bridges: can't find root of device tree\n");
  458. return;
  459. }
  460. for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
  461. if (!np->type)
  462. continue;
  463. if (strcmp(np->type, "pci") && strcmp(np->type, "ht"))
  464. continue;
  465. if ((of_device_is_compatible(np, "u4-pcie") ||
  466. of_device_is_compatible(np, "u3-agp")) &&
  467. maple_add_bridge(np) == 0)
  468. of_node_get(np);
  469. if (of_device_is_compatible(np, "u3-ht")) {
  470. of_node_get(np);
  471. ht = np;
  472. }
  473. }
  474. of_node_put(root);
  475. /* Now setup the HyperTransport host if we found any
  476. */
  477. if (ht && maple_add_bridge(ht) != 0)
  478. of_node_put(ht);
  479. /* Setup the linkage between OF nodes and PHBs */
  480. pci_devs_phb_init();
  481. /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We
  482. * assume there is no P2P bridge on the AGP bus, which should be a
  483. * safe assumptions hopefully.
  484. */
  485. if (u3_agp) {
  486. struct device_node *np = u3_agp->dn;
  487. PCI_DN(np)->busno = 0xf0;
  488. for (np = np->child; np; np = np->sibling)
  489. PCI_DN(np)->busno = 0xf0;
  490. }
  491. /* Tell pci.c to not change any resource allocations. */
  492. pci_probe_only = 1;
  493. }
  494. int maple_pci_get_legacy_ide_irq(struct pci_dev *pdev, int channel)
  495. {
  496. struct device_node *np;
  497. unsigned int defirq = channel ? 15 : 14;
  498. unsigned int irq;
  499. if (pdev->vendor != PCI_VENDOR_ID_AMD ||
  500. pdev->device != PCI_DEVICE_ID_AMD_8111_IDE)
  501. return defirq;
  502. np = pci_device_to_OF_node(pdev);
  503. if (np == NULL) {
  504. printk("Failed to locate OF node for IDE %s\n",
  505. pci_name(pdev));
  506. return defirq;
  507. }
  508. irq = irq_of_parse_and_map(np, channel & 0x1);
  509. if (irq == NO_IRQ) {
  510. printk("Failed to map onboard IDE interrupt for channel %d\n",
  511. channel);
  512. return defirq;
  513. }
  514. return irq;
  515. }