/arch/powerpc/kernel/isa-bridge.c

http://github.com/mirrors/linux · C · 353 lines · 214 code · 52 blank · 87 comment · 52 complexity · 96e4549e1ae2b374766d520aa0920182 MD5 · raw file

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