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/vmalloc.h>
23 #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 (ioremap_page_range(ISA_IO_BASE
, ISA_IO_BASE
+ size
, pa
,
50 pgprot_noncached(PAGE_KERNEL
)))
51 unmap_kernel_range(ISA_IO_BASE
, size
);
53 early_ioremap_range(ISA_IO_BASE
, pa
, size
,
54 pgprot_noncached(PAGE_KERNEL
));
58 static void pci_process_ISA_OF_ranges(struct device_node
*isa_node
,
59 unsigned long phb_io_base_phys
)
61 /* We should get some saner parsing here and remove these structs */
74 struct isa_address isa_addr
;
75 struct pci_address pci_addr
;
79 const struct isa_range
*range
;
80 unsigned long pci_addr
;
81 unsigned int isa_addr
;
85 range
= of_get_property(isa_node
, "ranges", &rlen
);
86 if (range
== NULL
|| (rlen
< sizeof(struct isa_range
)))
89 /* From "ISA Binding to 1275"
90 * The ranges property is laid out as an array of elements,
91 * each of which comprises:
92 * cells 0 - 1: an ISA address
93 * cells 2 - 4: a PCI address
94 * (size depending on dev->n_addr_cells)
95 * cell 5: the size of the range
97 if ((range
->isa_addr
.a_hi
& ISA_SPACE_MASK
) != ISA_SPACE_IO
) {
99 rlen
-= sizeof(struct isa_range
);
100 if (rlen
< sizeof(struct isa_range
))
103 if ((range
->isa_addr
.a_hi
& ISA_SPACE_MASK
) != ISA_SPACE_IO
)
106 isa_addr
= range
->isa_addr
.a_lo
;
107 pci_addr
= (unsigned long) range
->pci_addr
.a_mid
<< 32 |
108 range
->pci_addr
.a_lo
;
110 /* Assume these are both zero. Note: We could fix that and
111 * do a proper parsing instead ... oh well, that will do for
112 * now as nobody uses fancy mappings for ISA bridges
114 if ((pci_addr
!= 0) || (isa_addr
!= 0)) {
115 printk(KERN_ERR
"unexpected isa to pci mapping: %s\n",
120 /* Align size and make sure it's cropped to 64K */
121 size
= PAGE_ALIGN(range
->size
);
125 remap_isa_base(phb_io_base_phys
, size
);
129 printk(KERN_ERR
"no ISA IO ranges or unexpected isa range, "
131 remap_isa_base(phb_io_base_phys
, 0x10000);
136 * isa_bridge_find_early - Find and map the ISA IO space early before
137 * main PCI discovery. This is optionally called by
138 * the arch code when adding PCI PHBs to get early
139 * access to ISA IO ports
141 void __init
isa_bridge_find_early(struct pci_controller
*hose
)
143 struct device_node
*np
, *parent
= NULL
, *tmp
;
145 /* If we already have an ISA bridge, bail off */
146 if (isa_bridge_devnode
!= NULL
)
149 /* For each "isa" node in the system. Note : we do a search by
150 * type and not by name. It might be better to do by name but that's
151 * what the code used to do and I don't want to break too much at
152 * once. We can look into changing that separately
154 for_each_node_by_type(np
, "isa") {
155 /* Look for our hose being a parent */
156 for (parent
= of_get_parent(np
); parent
;) {
157 if (parent
== hose
->dn
) {
162 parent
= of_get_parent(parent
);
170 isa_bridge_devnode
= np
;
172 /* Now parse the "ranges" property and setup the ISA mapping */
173 pci_process_ISA_OF_ranges(np
, hose
->io_base_phys
);
175 /* Set the global ISA io base to indicate we have an ISA bridge */
176 isa_io_base
= ISA_IO_BASE
;
178 pr_debug("ISA bridge (early) is %pOF\n", np
);
182 * isa_bridge_find_early - Find and map the ISA IO space early before
183 * main PCI discovery. This is optionally called by
184 * the arch code when adding PCI PHBs to get early
185 * access to ISA IO ports
187 void __init
isa_bridge_init_non_pci(struct device_node
*np
)
189 const __be32
*ranges
, *pbasep
= NULL
;
192 u64 cbase
, pbase
, size
= 0;
194 /* If we already have an ISA bridge, bail off */
195 if (isa_bridge_devnode
!= NULL
)
198 pna
= of_n_addr_cells(np
);
199 if (of_property_read_u32(np
, "#address-cells", &na
) ||
200 of_property_read_u32(np
, "#size-cells", &ns
)) {
201 pr_warn("ISA: Non-PCI bridge %pOF is missing address format\n",
206 /* Check it's a supported address format */
207 if (na
!= 2 || ns
!= 1) {
208 pr_warn("ISA: Non-PCI bridge %pOF has unsupported address format\n",
214 /* Grab the ranges property */
215 ranges
= of_get_property(np
, "ranges", &rlen
);
216 if (ranges
== NULL
|| rlen
< rs
) {
217 pr_warn("ISA: Non-PCI bridge %pOF has absent or invalid ranges\n",
222 /* Parse it. We are only looking for IO space */
223 for (i
= 0; (i
+ rs
- 1) < rlen
; i
+= rs
) {
224 if (be32_to_cpup(ranges
+ i
) != 1)
226 cbase
= be32_to_cpup(ranges
+ i
+ 1);
227 size
= of_read_number(ranges
+ i
+ na
+ pna
, ns
);
228 pbasep
= ranges
+ i
+ na
;
232 /* Got something ? */
233 if (!size
|| !pbasep
) {
234 pr_warn("ISA: Non-PCI bridge %pOF has no usable IO range\n",
239 /* Align size and make sure it's cropped to 64K */
240 size
= PAGE_ALIGN(size
);
245 pbase
= of_translate_address(np
, pbasep
);
246 if (pbase
== OF_BAD_ADDR
) {
247 pr_warn("ISA: Non-PCI bridge %pOF failed to translate IO base\n",
252 /* We need page alignment */
253 if ((cbase
& ~PAGE_MASK
) || (pbase
& ~PAGE_MASK
)) {
254 pr_warn("ISA: Non-PCI bridge %pOF has non aligned IO range\n",
260 isa_bridge_devnode
= np
;
262 /* Set the global ISA io base to indicate we have an ISA bridge
265 isa_io_base
= ISA_IO_BASE
;
266 remap_isa_base(pbase
, size
);
268 pr_debug("ISA: Non-PCI bridge is %pOF\n", np
);
272 * isa_bridge_find_late - Find and map the ISA IO space upon discovery of
275 static void isa_bridge_find_late(struct pci_dev
*pdev
,
276 struct device_node
*devnode
)
278 struct pci_controller
*hose
= pci_bus_to_host(pdev
->bus
);
280 /* Store ISA device node and PCI device */
281 isa_bridge_devnode
= of_node_get(devnode
);
282 isa_bridge_pcidev
= pdev
;
284 /* Now parse the "ranges" property and setup the ISA mapping */
285 pci_process_ISA_OF_ranges(devnode
, hose
->io_base_phys
);
287 /* Set the global ISA io base to indicate we have an ISA bridge */
288 isa_io_base
= ISA_IO_BASE
;
290 pr_debug("ISA bridge (late) is %pOF on %s\n",
291 devnode
, pci_name(pdev
));
295 * isa_bridge_remove - Remove/unmap an ISA bridge
297 static void isa_bridge_remove(void)
299 pr_debug("ISA bridge removed !\n");
301 /* Clear the global ISA io base to indicate that we have no more
302 * ISA bridge. Note that drivers don't quite handle that, though
303 * we should probably do something about it. But do we ever really
304 * have ISA bridges being removed on machines using legacy devices ?
306 isa_io_base
= ISA_IO_BASE
;
308 /* Clear references to the bridge */
309 of_node_put(isa_bridge_devnode
);
310 isa_bridge_devnode
= NULL
;
311 isa_bridge_pcidev
= NULL
;
313 /* Unmap the ISA area */
314 unmap_kernel_range(ISA_IO_BASE
, 0x10000);
318 * isa_bridge_notify - Get notified of PCI devices addition/removal
320 static int isa_bridge_notify(struct notifier_block
*nb
, unsigned long action
,
323 struct device
*dev
= data
;
324 struct pci_dev
*pdev
= to_pci_dev(dev
);
325 struct device_node
*devnode
= pci_device_to_OF_node(pdev
);
328 case BUS_NOTIFY_ADD_DEVICE
:
329 /* Check if we have an early ISA device, without PCI dev */
330 if (isa_bridge_devnode
&& isa_bridge_devnode
== devnode
&&
331 !isa_bridge_pcidev
) {
332 pr_debug("ISA bridge PCI attached: %s\n",
334 isa_bridge_pcidev
= pdev
;
337 /* Check if we have no ISA device, and this happens to be one,
338 * register it as such if it has an OF device
340 if (!isa_bridge_devnode
&& of_node_is_type(devnode
, "isa"))
341 isa_bridge_find_late(pdev
, devnode
);
344 case BUS_NOTIFY_DEL_DEVICE
:
345 /* Check if this our existing ISA device */
346 if (pdev
== isa_bridge_pcidev
||
347 (devnode
&& devnode
== isa_bridge_devnode
))
354 static struct notifier_block isa_bridge_notifier
= {
355 .notifier_call
= isa_bridge_notify
359 * isa_bridge_init - register to be notified of ISA bridge addition/removal
362 static int __init
isa_bridge_init(void)
364 bus_register_notifier(&pci_bus_type
, &isa_bridge_notifier
);
367 arch_initcall(isa_bridge_init
);