isa-bridge.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/init.h>
  20. #include <linux/mm.h>
  21. #include <linux/notifier.h>
  22. #include <asm/processor.h>
  23. #include <asm/io.h>
  24. #include <asm/prom.h>
  25. #include <asm/pci-bridge.h>
  26. #include <asm/machdep.h>
  27. #include <asm/ppc-pci.h>
  28. #include <asm/firmware.h>
  29. unsigned long isa_io_base; /* NULL if no ISA bus */
  30. EXPORT_SYMBOL(isa_io_base);
  31. /* Cached ISA bridge dev. */
  32. static struct device_node *isa_bridge_devnode;
  33. struct pci_dev *isa_bridge_pcidev;
  34. EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
  35. #define ISA_SPACE_MASK 0x1
  36. #define ISA_SPACE_IO 0x1
  37. static void __devinit pci_process_ISA_OF_ranges(struct device_node *isa_node,
  38. unsigned long phb_io_base_phys)
  39. {
  40. /* We should get some saner parsing here and remove these structs */
  41. struct pci_address {
  42. u32 a_hi;
  43. u32 a_mid;
  44. u32 a_lo;
  45. };
  46. struct isa_address {
  47. u32 a_hi;
  48. u32 a_lo;
  49. };
  50. struct isa_range {
  51. struct isa_address isa_addr;
  52. struct pci_address pci_addr;
  53. unsigned int size;
  54. };
  55. const struct isa_range *range;
  56. unsigned long pci_addr;
  57. unsigned int isa_addr;
  58. unsigned int size;
  59. int rlen = 0;
  60. range = of_get_property(isa_node, "ranges", &rlen);
  61. if (range == NULL || (rlen < sizeof(struct isa_range)))
  62. goto inval_range;
  63. /* From "ISA Binding to 1275"
  64. * The ranges property is laid out as an array of elements,
  65. * each of which comprises:
  66. * cells 0 - 1: an ISA address
  67. * cells 2 - 4: a PCI address
  68. * (size depending on dev->n_addr_cells)
  69. * cell 5: the size of the range
  70. */
  71. if ((range->isa_addr.a_hi && ISA_SPACE_MASK) != ISA_SPACE_IO) {
  72. range++;
  73. rlen -= sizeof(struct isa_range);
  74. if (rlen < sizeof(struct isa_range))
  75. goto inval_range;
  76. }
  77. if ((range->isa_addr.a_hi && ISA_SPACE_MASK) != ISA_SPACE_IO)
  78. goto inval_range;
  79. isa_addr = range->isa_addr.a_lo;
  80. pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
  81. range->pci_addr.a_lo;
  82. /* Assume these are both zero. Note: We could fix that and
  83. * do a proper parsing instead ... oh well, that will do for
  84. * now as nobody uses fancy mappings for ISA bridges
  85. */
  86. if ((pci_addr != 0) || (isa_addr != 0)) {
  87. printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
  88. __FUNCTION__);
  89. return;
  90. }
  91. /* Align size and make sure it's cropped to 64K */
  92. size = PAGE_ALIGN(range->size);
  93. if (size > 0x10000)
  94. size = 0x10000;
  95. printk(KERN_ERR "no ISA IO ranges or unexpected isa range,"
  96. "mapping 64k\n");
  97. __ioremap_at(phb_io_base_phys, (void *)ISA_IO_BASE,
  98. size, _PAGE_NO_CACHE|_PAGE_GUARDED);
  99. return;
  100. inval_range:
  101. printk(KERN_ERR "no ISA IO ranges or unexpected isa range,"
  102. "mapping 64k\n");
  103. __ioremap_at(phb_io_base_phys, (void *)ISA_IO_BASE,
  104. 0x10000, _PAGE_NO_CACHE|_PAGE_GUARDED);
  105. }
  106. /**
  107. * isa_bridge_find_early - Find and map the ISA IO space early before
  108. * main PCI discovery. This is optionally called by
  109. * the arch code when adding PCI PHBs to get early
  110. * access to ISA IO ports
  111. */
  112. void __init isa_bridge_find_early(struct pci_controller *hose)
  113. {
  114. struct device_node *np, *parent = NULL, *tmp;
  115. /* If we already have an ISA bridge, bail off */
  116. if (isa_bridge_devnode != NULL)
  117. return;
  118. /* For each "isa" node in the system. Note : we do a search by
  119. * type and not by name. It might be better to do by name but that's
  120. * what the code used to do and I don't want to break too much at
  121. * once. We can look into changing that separately
  122. */
  123. for_each_node_by_type(np, "isa") {
  124. /* Look for our hose being a parent */
  125. for (parent = of_get_parent(np); parent;) {
  126. if (parent == hose->arch_data) {
  127. of_node_put(parent);
  128. break;
  129. }
  130. tmp = parent;
  131. parent = of_get_parent(parent);
  132. of_node_put(tmp);
  133. }
  134. if (parent != NULL)
  135. break;
  136. }
  137. if (np == NULL)
  138. return;
  139. isa_bridge_devnode = np;
  140. /* Now parse the "ranges" property and setup the ISA mapping */
  141. pci_process_ISA_OF_ranges(np, hose->io_base_phys);
  142. /* Set the global ISA io base to indicate we have an ISA bridge */
  143. isa_io_base = ISA_IO_BASE;
  144. pr_debug("ISA bridge (early) is %s\n", np->full_name);
  145. }
  146. /**
  147. * isa_bridge_find_late - Find and map the ISA IO space upon discovery of
  148. * a new ISA bridge
  149. */
  150. static void __devinit isa_bridge_find_late(struct pci_dev *pdev,
  151. struct device_node *devnode)
  152. {
  153. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  154. /* Store ISA device node and PCI device */
  155. isa_bridge_devnode = of_node_get(devnode);
  156. isa_bridge_pcidev = pdev;
  157. /* Now parse the "ranges" property and setup the ISA mapping */
  158. pci_process_ISA_OF_ranges(devnode, hose->io_base_phys);
  159. /* Set the global ISA io base to indicate we have an ISA bridge */
  160. isa_io_base = ISA_IO_BASE;
  161. pr_debug("ISA bridge (late) is %s on %s\n",
  162. devnode->full_name, pci_name(pdev));
  163. }
  164. /**
  165. * isa_bridge_remove - Remove/unmap an ISA bridge
  166. */
  167. static void isa_bridge_remove(void)
  168. {
  169. pr_debug("ISA bridge removed !\n");
  170. /* Clear the global ISA io base to indicate that we have no more
  171. * ISA bridge. Note that drivers don't quite handle that, though
  172. * we should probably do something about it. But do we ever really
  173. * have ISA bridges being removed on machines using legacy devices ?
  174. */
  175. isa_io_base = ISA_IO_BASE;
  176. /* Clear references to the bridge */
  177. of_node_put(isa_bridge_devnode);
  178. isa_bridge_devnode = NULL;
  179. isa_bridge_pcidev = NULL;
  180. /* Unmap the ISA area */
  181. __iounmap_at((void *)ISA_IO_BASE, 0x10000);
  182. }
  183. /**
  184. * isa_bridge_notify - Get notified of PCI devices addition/removal
  185. */
  186. static int __devinit isa_bridge_notify(struct notifier_block *nb,
  187. unsigned long action, void *data)
  188. {
  189. struct device *dev = data;
  190. struct pci_dev *pdev = to_pci_dev(dev);
  191. struct device_node *devnode = pci_device_to_OF_node(pdev);
  192. switch(action) {
  193. case BUS_NOTIFY_ADD_DEVICE:
  194. /* Check if we have an early ISA device, without PCI dev */
  195. if (isa_bridge_devnode && isa_bridge_devnode == devnode &&
  196. !isa_bridge_pcidev) {
  197. pr_debug("ISA bridge PCI attached: %s\n",
  198. pci_name(pdev));
  199. isa_bridge_pcidev = pdev;
  200. }
  201. /* Check if we have no ISA device, and this happens to be one,
  202. * register it as such if it has an OF device
  203. */
  204. if (!isa_bridge_devnode && devnode && devnode->type &&
  205. !strcmp(devnode->type, "isa"))
  206. isa_bridge_find_late(pdev, devnode);
  207. return 0;
  208. case BUS_NOTIFY_DEL_DEVICE:
  209. /* Check if this our existing ISA device */
  210. if (pdev == isa_bridge_pcidev ||
  211. (devnode && devnode == isa_bridge_devnode))
  212. isa_bridge_remove();
  213. return 0;
  214. }
  215. return 0;
  216. }
  217. static struct notifier_block isa_bridge_notifier = {
  218. .notifier_call = isa_bridge_notify
  219. };
  220. /**
  221. * isa_bridge_init - register to be notified of ISA bridge addition/removal
  222. *
  223. */
  224. static int __init isa_bridge_init(void)
  225. {
  226. if (firmware_has_feature(FW_FEATURE_ISERIES))
  227. return 0;
  228. bus_register_notifier(&pci_bus_type, &isa_bridge_notifier);
  229. return 0;
  230. }
  231. arch_initcall(isa_bridge_init);