isa-bridge.c 9.5 KB

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