1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Routines for tracking a legacy ISA bridge
5 * Copyrigh 2007 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
7 * Some bits and pieces moved over from pci_64.c
9 * Copyrigh 2003 Anton Blanchard <anton@au.ibm.com>, IBM Corp.
14 #include <linux/kernel.h>
15 #include <linux/pci.h>
16 #include <linux/string.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
20 #include <linux/notifier.h>
21 #include <linux/of_address.h>
22 #include <linux/vmalloc.h>
24 #include <asm/processor.h>
26 #include <asm/pci-bridge.h>
27 #include <asm/machdep.h>
28 #include <asm/ppc-pci.h>
29 #include <asm/isa-bridge.h>
31 unsigned long isa_io_base
; /* NULL if no ISA bus */
32 EXPORT_SYMBOL(isa_io_base
);
34 /* Cached ISA bridge dev. */
35 static struct device_node
*isa_bridge_devnode
;
36 struct pci_dev
*isa_bridge_pcidev
;
37 EXPORT_SYMBOL_GPL(isa_bridge_pcidev
);
39 #define ISA_SPACE_MASK 0x1
40 #define ISA_SPACE_IO 0x1
42 static void remap_isa_base(phys_addr_t pa
, unsigned long size
)
44 WARN_ON_ONCE(ISA_IO_BASE
& ~PAGE_MASK
);
45 WARN_ON_ONCE(pa
& ~PAGE_MASK
);
46 WARN_ON_ONCE(size
& ~PAGE_MASK
);
48 if (slab_is_available()) {
49 if (vmap_page_range(ISA_IO_BASE
, ISA_IO_BASE
+ size
, pa
,
50 pgprot_noncached(PAGE_KERNEL
)))
51 vunmap_range(ISA_IO_BASE
, ISA_IO_BASE
+ size
);
53 early_ioremap_range(ISA_IO_BASE
, pa
, size
,
54 pgprot_noncached(PAGE_KERNEL
));
58 static int process_ISA_OF_ranges(struct device_node
*isa_node
,
59 unsigned long phb_io_base_phys
)
62 struct of_range_parser parser
;
63 struct of_range range
;
65 if (of_range_parser_init(&parser
, isa_node
))
68 for_each_of_range(&parser
, &range
) {
69 if ((range
.flags
& ISA_SPACE_MASK
) != ISA_SPACE_IO
)
72 if (range
.cpu_addr
== OF_BAD_ADDR
) {
73 pr_err("ISA: Bad CPU mapping: %s\n", __func__
);
77 /* We need page alignment */
78 if ((range
.bus_addr
& ~PAGE_MASK
) || (range
.cpu_addr
& ~PAGE_MASK
)) {
79 pr_warn("ISA: bridge %pOF has non aligned IO range\n", isa_node
);
83 /* Align size and make sure it's cropped to 64K */
84 size
= PAGE_ALIGN(range
.size
);
88 if (!phb_io_base_phys
)
89 phb_io_base_phys
= range
.cpu_addr
;
91 remap_isa_base(phb_io_base_phys
, size
);
96 if (phb_io_base_phys
) {
97 pr_err("no ISA IO ranges or unexpected isa range, mapping 64k\n");
98 remap_isa_base(phb_io_base_phys
, 0x10000);
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
111 void __init
isa_bridge_find_early(struct pci_controller
*hose
)
113 struct device_node
*np
, *parent
= NULL
, *tmp
;
115 /* If we already have an ISA bridge, bail off */
116 if (isa_bridge_devnode
!= NULL
)
119 /* For each "isa" node in the system. Note : we do a search by
120 * type and not by name. It might be better to do by name but that's
121 * what the code used to do and I don't want to break too much at
122 * once. We can look into changing that separately
124 for_each_node_by_type(np
, "isa") {
125 /* Look for our hose being a parent */
126 for (parent
= of_get_parent(np
); parent
;) {
127 if (parent
== hose
->dn
) {
132 parent
= of_get_parent(parent
);
140 isa_bridge_devnode
= np
;
142 /* Now parse the "ranges" property and setup the ISA mapping */
143 process_ISA_OF_ranges(np
, hose
->io_base_phys
);
145 /* Set the global ISA io base to indicate we have an ISA bridge */
146 isa_io_base
= ISA_IO_BASE
;
148 pr_debug("ISA bridge (early) is %pOF\n", np
);
152 * isa_bridge_find_early - Find and map the ISA IO space early before
153 * main PCI discovery. This is optionally called by
154 * the arch code when adding PCI PHBs to get early
155 * access to ISA IO ports
157 void __init
isa_bridge_init_non_pci(struct device_node
*np
)
161 /* If we already have an ISA bridge, bail off */
162 if (isa_bridge_devnode
!= NULL
)
165 ret
= process_ISA_OF_ranges(np
, 0);
170 isa_bridge_devnode
= np
;
172 /* Set the global ISA io base to indicate we have an ISA bridge
175 isa_io_base
= ISA_IO_BASE
;
177 pr_debug("ISA: Non-PCI bridge is %pOF\n", np
);
181 * isa_bridge_find_late - Find and map the ISA IO space upon discovery of
184 static void isa_bridge_find_late(struct pci_dev
*pdev
,
185 struct device_node
*devnode
)
187 struct pci_controller
*hose
= pci_bus_to_host(pdev
->bus
);
189 /* Store ISA device node and PCI device */
190 isa_bridge_devnode
= of_node_get(devnode
);
191 isa_bridge_pcidev
= pdev
;
193 /* Now parse the "ranges" property and setup the ISA mapping */
194 process_ISA_OF_ranges(devnode
, hose
->io_base_phys
);
196 /* Set the global ISA io base to indicate we have an ISA bridge */
197 isa_io_base
= ISA_IO_BASE
;
199 pr_debug("ISA bridge (late) is %pOF on %s\n",
200 devnode
, pci_name(pdev
));
204 * isa_bridge_remove - Remove/unmap an ISA bridge
206 static void isa_bridge_remove(void)
208 pr_debug("ISA bridge removed !\n");
210 /* Clear the global ISA io base to indicate that we have no more
211 * ISA bridge. Note that drivers don't quite handle that, though
212 * we should probably do something about it. But do we ever really
213 * have ISA bridges being removed on machines using legacy devices ?
215 isa_io_base
= ISA_IO_BASE
;
217 /* Clear references to the bridge */
218 of_node_put(isa_bridge_devnode
);
219 isa_bridge_devnode
= NULL
;
220 isa_bridge_pcidev
= NULL
;
222 /* Unmap the ISA area */
223 vunmap_range(ISA_IO_BASE
, ISA_IO_BASE
+ 0x10000);
227 * isa_bridge_notify - Get notified of PCI devices addition/removal
229 static int isa_bridge_notify(struct notifier_block
*nb
, unsigned long action
,
232 struct device
*dev
= data
;
233 struct pci_dev
*pdev
= to_pci_dev(dev
);
234 struct device_node
*devnode
= pci_device_to_OF_node(pdev
);
237 case BUS_NOTIFY_ADD_DEVICE
:
238 /* Check if we have an early ISA device, without PCI dev */
239 if (isa_bridge_devnode
&& isa_bridge_devnode
== devnode
&&
240 !isa_bridge_pcidev
) {
241 pr_debug("ISA bridge PCI attached: %s\n",
243 isa_bridge_pcidev
= pdev
;
246 /* Check if we have no ISA device, and this happens to be one,
247 * register it as such if it has an OF device
249 if (!isa_bridge_devnode
&& of_node_is_type(devnode
, "isa"))
250 isa_bridge_find_late(pdev
, devnode
);
253 case BUS_NOTIFY_DEL_DEVICE
:
254 /* Check if this our existing ISA device */
255 if (pdev
== isa_bridge_pcidev
||
256 (devnode
&& devnode
== isa_bridge_devnode
))
263 static struct notifier_block isa_bridge_notifier
= {
264 .notifier_call
= isa_bridge_notify
268 * isa_bridge_init - register to be notified of ISA bridge addition/removal
271 static int __init
isa_bridge_init(void)
273 bus_register_notifier(&pci_bus_type
, &isa_bridge_notifier
);
276 arch_initcall(isa_bridge_init
);