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