isa-bridge.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Routines for tracking a legacy ISA bridge
  3. *
  4. * Copyrigh 2007 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
  5. *
  6. * Some bits and pieces moved over from pci_64.c
  7. *
  8. * Copyrigh 2003 Anton Blanchard <anton@au.ibm.com>, IBM Corp.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #define DEBUG
  16. #include <linux/kernel.h>
  17. #include <linux/pci.h>
  18. #include <linux/string.h>
  19. #include <linux/export.h>
  20. #include <linux/init.h>
  21. #include <linux/mm.h>
  22. #include <linux/notifier.h>
  23. #include <asm/processor.h>
  24. #include <asm/io.h>
  25. #include <asm/prom.h>
  26. #include <asm/pci-bridge.h>
  27. #include <asm/machdep.h>
  28. #include <asm/ppc-pci.h>
  29. #include <asm/isa-bridge.h>
  30. unsigned long isa_io_base; /* NULL if no ISA bus */
  31. EXPORT_SYMBOL(isa_io_base);
  32. /* Cached ISA bridge dev. */
  33. static struct device_node *isa_bridge_devnode;
  34. struct pci_dev *isa_bridge_pcidev;
  35. EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
  36. #define ISA_SPACE_MASK 0x1
  37. #define ISA_SPACE_IO 0x1
  38. static void pci_process_ISA_OF_ranges(struct device_node *isa_node,
  39. unsigned long phb_io_base_phys)
  40. {
  41. /* We should get some saner parsing here and remove these structs */
  42. struct pci_address {
  43. u32 a_hi;
  44. u32 a_mid;
  45. u32 a_lo;
  46. };
  47. struct isa_address {
  48. u32 a_hi;
  49. u32 a_lo;
  50. };
  51. struct isa_range {
  52. struct isa_address isa_addr;
  53. struct pci_address pci_addr;
  54. unsigned int size;
  55. };
  56. const struct isa_range *range;
  57. unsigned long pci_addr;
  58. unsigned int isa_addr;
  59. unsigned int size;
  60. int rlen = 0;
  61. range = of_get_property(isa_node, "ranges", &rlen);
  62. if (range == NULL || (rlen < sizeof(struct isa_range)))
  63. goto inval_range;
  64. /* From "ISA Binding to 1275"
  65. * The ranges property is laid out as an array of elements,
  66. * each of which comprises:
  67. * cells 0 - 1: an ISA address
  68. * cells 2 - 4: a PCI address
  69. * (size depending on dev->n_addr_cells)
  70. * cell 5: the size of the range
  71. */
  72. if ((range->isa_addr.a_hi & ISA_SPACE_MASK) != ISA_SPACE_IO) {
  73. range++;
  74. rlen -= sizeof(struct isa_range);
  75. if (rlen < sizeof(struct isa_range))
  76. goto inval_range;
  77. }
  78. if ((range->isa_addr.a_hi & ISA_SPACE_MASK) != ISA_SPACE_IO)
  79. goto inval_range;
  80. isa_addr = range->isa_addr.a_lo;
  81. pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
  82. range->pci_addr.a_lo;
  83. /* Assume these are both zero. Note: We could fix that and
  84. * do a proper parsing instead ... oh well, that will do for
  85. * now as nobody uses fancy mappings for ISA bridges
  86. */
  87. if ((pci_addr != 0) || (isa_addr != 0)) {
  88. printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
  89. __func__);
  90. return;
  91. }
  92. /* Align size and make sure it's cropped to 64K */
  93. size = PAGE_ALIGN(range->size);
  94. if (size > 0x10000)
  95. size = 0x10000;
  96. __ioremap_at(phb_io_base_phys, (void *)ISA_IO_BASE,
  97. size, pgprot_val(pgprot_noncached(__pgprot(0))));
  98. return;
  99. inval_range:
  100. printk(KERN_ERR "no ISA IO ranges or unexpected isa range, "
  101. "mapping 64k\n");
  102. __ioremap_at(phb_io_base_phys, (void *)ISA_IO_BASE,
  103. 0x10000, pgprot_val(pgprot_noncached(__pgprot(0))));
  104. }
  105. /**
  106. * isa_bridge_find_early - Find and map the ISA IO space early before
  107. * main PCI discovery. This is optionally called by
  108. * the arch code when adding PCI PHBs to get early
  109. * access to ISA IO ports
  110. */
  111. void __init isa_bridge_find_early(struct pci_controller *hose)
  112. {
  113. struct device_node *np, *parent = NULL, *tmp;
  114. /* If we already have an ISA bridge, bail off */
  115. if (isa_bridge_devnode != NULL)
  116. return;
  117. /* For each "isa" node in the system. Note : we do a search by
  118. * type and not by name. It might be better to do by name but that's
  119. * what the code used to do and I don't want to break too much at
  120. * once. We can look into changing that separately
  121. */
  122. for_each_node_by_type(np, "isa") {
  123. /* Look for our hose being a parent */
  124. for (parent = of_get_parent(np); parent;) {
  125. if (parent == hose->dn) {
  126. of_node_put(parent);
  127. break;
  128. }
  129. tmp = parent;
  130. parent = of_get_parent(parent);
  131. of_node_put(tmp);
  132. }
  133. if (parent != NULL)
  134. break;
  135. }
  136. if (np == NULL)
  137. return;
  138. isa_bridge_devnode = np;
  139. /* Now parse the "ranges" property and setup the ISA mapping */
  140. pci_process_ISA_OF_ranges(np, hose->io_base_phys);
  141. /* Set the global ISA io base to indicate we have an ISA bridge */
  142. isa_io_base = ISA_IO_BASE;
  143. pr_debug("ISA bridge (early) is %pOF\n", np);
  144. }
  145. /**
  146. * isa_bridge_find_early - Find and map the ISA IO space early before
  147. * main PCI discovery. This is optionally called by
  148. * the arch code when adding PCI PHBs to get early
  149. * access to ISA IO ports
  150. */
  151. void __init isa_bridge_init_non_pci(struct device_node *np)
  152. {
  153. const __be32 *ranges, *pbasep = NULL;
  154. int rlen, i, rs;
  155. u32 na, ns, pna;
  156. u64 cbase, pbase, size = 0;
  157. /* If we already have an ISA bridge, bail off */
  158. if (isa_bridge_devnode != NULL)
  159. return;
  160. pna = of_n_addr_cells(np);
  161. if (of_property_read_u32(np, "#address-cells", &na) ||
  162. of_property_read_u32(np, "#size-cells", &ns)) {
  163. pr_warn("ISA: Non-PCI bridge %pOF is missing address format\n",
  164. np);
  165. return;
  166. }
  167. /* Check it's a supported address format */
  168. if (na != 2 || ns != 1) {
  169. pr_warn("ISA: Non-PCI bridge %pOF has unsupported address format\n",
  170. np);
  171. return;
  172. }
  173. rs = na + ns + pna;
  174. /* Grab the ranges property */
  175. ranges = of_get_property(np, "ranges", &rlen);
  176. if (ranges == NULL || rlen < rs) {
  177. pr_warn("ISA: Non-PCI bridge %pOF has absent or invalid ranges\n",
  178. np);
  179. return;
  180. }
  181. /* Parse it. We are only looking for IO space */
  182. for (i = 0; (i + rs - 1) < rlen; i += rs) {
  183. if (be32_to_cpup(ranges + i) != 1)
  184. continue;
  185. cbase = be32_to_cpup(ranges + i + 1);
  186. size = of_read_number(ranges + i + na + pna, ns);
  187. pbasep = ranges + i + na;
  188. break;
  189. }
  190. /* Got something ? */
  191. if (!size || !pbasep) {
  192. pr_warn("ISA: Non-PCI bridge %pOF has no usable IO range\n",
  193. np);
  194. return;
  195. }
  196. /* Align size and make sure it's cropped to 64K */
  197. size = PAGE_ALIGN(size);
  198. if (size > 0x10000)
  199. size = 0x10000;
  200. /* Map pbase */
  201. pbase = of_translate_address(np, pbasep);
  202. if (pbase == OF_BAD_ADDR) {
  203. pr_warn("ISA: Non-PCI bridge %pOF failed to translate IO base\n",
  204. np);
  205. return;
  206. }
  207. /* We need page alignment */
  208. if ((cbase & ~PAGE_MASK) || (pbase & ~PAGE_MASK)) {
  209. pr_warn("ISA: Non-PCI bridge %pOF has non aligned IO range\n",
  210. np);
  211. return;
  212. }
  213. /* Got it */
  214. isa_bridge_devnode = np;
  215. /* Set the global ISA io base to indicate we have an ISA bridge
  216. * and map it
  217. */
  218. isa_io_base = ISA_IO_BASE;
  219. __ioremap_at(pbase, (void *)ISA_IO_BASE,
  220. size, pgprot_val(pgprot_noncached(__pgprot(0))));
  221. pr_debug("ISA: Non-PCI bridge is %pOF\n", np);
  222. }
  223. /**
  224. * isa_bridge_find_late - Find and map the ISA IO space upon discovery of
  225. * a new ISA bridge
  226. */
  227. static void isa_bridge_find_late(struct pci_dev *pdev,
  228. struct device_node *devnode)
  229. {
  230. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  231. /* Store ISA device node and PCI device */
  232. isa_bridge_devnode = of_node_get(devnode);
  233. isa_bridge_pcidev = pdev;
  234. /* Now parse the "ranges" property and setup the ISA mapping */
  235. pci_process_ISA_OF_ranges(devnode, hose->io_base_phys);
  236. /* Set the global ISA io base to indicate we have an ISA bridge */
  237. isa_io_base = ISA_IO_BASE;
  238. pr_debug("ISA bridge (late) is %pOF on %s\n",
  239. devnode, pci_name(pdev));
  240. }
  241. /**
  242. * isa_bridge_remove - Remove/unmap an ISA bridge
  243. */
  244. static void isa_bridge_remove(void)
  245. {
  246. pr_debug("ISA bridge removed !\n");
  247. /* Clear the global ISA io base to indicate that we have no more
  248. * ISA bridge. Note that drivers don't quite handle that, though
  249. * we should probably do something about it. But do we ever really
  250. * have ISA bridges being removed on machines using legacy devices ?
  251. */
  252. isa_io_base = ISA_IO_BASE;
  253. /* Clear references to the bridge */
  254. of_node_put(isa_bridge_devnode);
  255. isa_bridge_devnode = NULL;
  256. isa_bridge_pcidev = NULL;
  257. /* Unmap the ISA area */
  258. __iounmap_at((void *)ISA_IO_BASE, 0x10000);
  259. }
  260. /**
  261. * isa_bridge_notify - Get notified of PCI devices addition/removal
  262. */
  263. static int isa_bridge_notify(struct notifier_block *nb, unsigned long action,
  264. void *data)
  265. {
  266. struct device *dev = data;
  267. struct pci_dev *pdev = to_pci_dev(dev);
  268. struct device_node *devnode = pci_device_to_OF_node(pdev);
  269. switch(action) {
  270. case BUS_NOTIFY_ADD_DEVICE:
  271. /* Check if we have an early ISA device, without PCI dev */
  272. if (isa_bridge_devnode && isa_bridge_devnode == devnode &&
  273. !isa_bridge_pcidev) {
  274. pr_debug("ISA bridge PCI attached: %s\n",
  275. pci_name(pdev));
  276. isa_bridge_pcidev = pdev;
  277. }
  278. /* Check if we have no ISA device, and this happens to be one,
  279. * register it as such if it has an OF device
  280. */
  281. if (!isa_bridge_devnode && devnode && devnode->type &&
  282. !strcmp(devnode->type, "isa"))
  283. isa_bridge_find_late(pdev, devnode);
  284. return 0;
  285. case BUS_NOTIFY_DEL_DEVICE:
  286. /* Check if this our existing ISA device */
  287. if (pdev == isa_bridge_pcidev ||
  288. (devnode && devnode == isa_bridge_devnode))
  289. isa_bridge_remove();
  290. return 0;
  291. }
  292. return 0;
  293. }
  294. static struct notifier_block isa_bridge_notifier = {
  295. .notifier_call = isa_bridge_notify
  296. };
  297. /**
  298. * isa_bridge_init - register to be notified of ISA bridge addition/removal
  299. *
  300. */
  301. static int __init isa_bridge_init(void)
  302. {
  303. bus_register_notifier(&pci_bus_type, &isa_bridge_notifier);
  304. return 0;
  305. }
  306. arch_initcall(isa_bridge_init);