0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #define DEBUG
0013
0014 #include <linux/kernel.h>
0015 #include <linux/pci.h>
0016 #include <linux/string.h>
0017 #include <linux/export.h>
0018 #include <linux/init.h>
0019 #include <linux/mm.h>
0020 #include <linux/notifier.h>
0021 #include <linux/of_address.h>
0022 #include <linux/vmalloc.h>
0023
0024 #include <asm/processor.h>
0025 #include <asm/io.h>
0026 #include <asm/pci-bridge.h>
0027 #include <asm/machdep.h>
0028 #include <asm/ppc-pci.h>
0029 #include <asm/isa-bridge.h>
0030
0031 unsigned long isa_io_base;
0032 EXPORT_SYMBOL(isa_io_base);
0033
0034
0035 static struct device_node *isa_bridge_devnode;
0036 struct pci_dev *isa_bridge_pcidev;
0037 EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
0038
0039 #define ISA_SPACE_MASK 0x1
0040 #define ISA_SPACE_IO 0x1
0041
0042 static void remap_isa_base(phys_addr_t pa, unsigned long size)
0043 {
0044 WARN_ON_ONCE(ISA_IO_BASE & ~PAGE_MASK);
0045 WARN_ON_ONCE(pa & ~PAGE_MASK);
0046 WARN_ON_ONCE(size & ~PAGE_MASK);
0047
0048 if (slab_is_available()) {
0049 if (ioremap_page_range(ISA_IO_BASE, ISA_IO_BASE + size, pa,
0050 pgprot_noncached(PAGE_KERNEL)))
0051 vunmap_range(ISA_IO_BASE, ISA_IO_BASE + size);
0052 } else {
0053 early_ioremap_range(ISA_IO_BASE, pa, size,
0054 pgprot_noncached(PAGE_KERNEL));
0055 }
0056 }
0057
0058 static void pci_process_ISA_OF_ranges(struct device_node *isa_node,
0059 unsigned long phb_io_base_phys)
0060 {
0061
0062 struct pci_address {
0063 u32 a_hi;
0064 u32 a_mid;
0065 u32 a_lo;
0066 };
0067
0068 struct isa_address {
0069 u32 a_hi;
0070 u32 a_lo;
0071 };
0072
0073 struct isa_range {
0074 struct isa_address isa_addr;
0075 struct pci_address pci_addr;
0076 unsigned int size;
0077 };
0078
0079 const struct isa_range *range;
0080 unsigned long pci_addr;
0081 unsigned int isa_addr;
0082 unsigned int size;
0083 int rlen = 0;
0084
0085 range = of_get_property(isa_node, "ranges", &rlen);
0086 if (range == NULL || (rlen < sizeof(struct isa_range)))
0087 goto inval_range;
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 if ((range->isa_addr.a_hi & ISA_SPACE_MASK) != ISA_SPACE_IO) {
0098 range++;
0099 rlen -= sizeof(struct isa_range);
0100 if (rlen < sizeof(struct isa_range))
0101 goto inval_range;
0102 }
0103 if ((range->isa_addr.a_hi & ISA_SPACE_MASK) != ISA_SPACE_IO)
0104 goto inval_range;
0105
0106 isa_addr = range->isa_addr.a_lo;
0107 pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
0108 range->pci_addr.a_lo;
0109
0110
0111
0112
0113
0114 if ((pci_addr != 0) || (isa_addr != 0)) {
0115 printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
0116 __func__);
0117 return;
0118 }
0119
0120
0121 size = PAGE_ALIGN(range->size);
0122 if (size > 0x10000)
0123 size = 0x10000;
0124
0125 remap_isa_base(phb_io_base_phys, size);
0126 return;
0127
0128 inval_range:
0129 printk(KERN_ERR "no ISA IO ranges or unexpected isa range, "
0130 "mapping 64k\n");
0131 remap_isa_base(phb_io_base_phys, 0x10000);
0132 }
0133
0134
0135
0136
0137
0138
0139
0140
0141 void __init isa_bridge_find_early(struct pci_controller *hose)
0142 {
0143 struct device_node *np, *parent = NULL, *tmp;
0144
0145
0146 if (isa_bridge_devnode != NULL)
0147 return;
0148
0149
0150
0151
0152
0153
0154 for_each_node_by_type(np, "isa") {
0155
0156 for (parent = of_get_parent(np); parent;) {
0157 if (parent == hose->dn) {
0158 of_node_put(parent);
0159 break;
0160 }
0161 tmp = parent;
0162 parent = of_get_parent(parent);
0163 of_node_put(tmp);
0164 }
0165 if (parent != NULL)
0166 break;
0167 }
0168 if (np == NULL)
0169 return;
0170 isa_bridge_devnode = np;
0171
0172
0173 pci_process_ISA_OF_ranges(np, hose->io_base_phys);
0174
0175
0176 isa_io_base = ISA_IO_BASE;
0177
0178 pr_debug("ISA bridge (early) is %pOF\n", np);
0179 }
0180
0181
0182
0183
0184
0185
0186
0187 void __init isa_bridge_init_non_pci(struct device_node *np)
0188 {
0189 const __be32 *ranges, *pbasep = NULL;
0190 int rlen, i, rs;
0191 u32 na, ns, pna;
0192 u64 cbase, pbase, size = 0;
0193
0194
0195 if (isa_bridge_devnode != NULL)
0196 return;
0197
0198 pna = of_n_addr_cells(np);
0199 if (of_property_read_u32(np, "#address-cells", &na) ||
0200 of_property_read_u32(np, "#size-cells", &ns)) {
0201 pr_warn("ISA: Non-PCI bridge %pOF is missing address format\n",
0202 np);
0203 return;
0204 }
0205
0206
0207 if (na != 2 || ns != 1) {
0208 pr_warn("ISA: Non-PCI bridge %pOF has unsupported address format\n",
0209 np);
0210 return;
0211 }
0212 rs = na + ns + pna;
0213
0214
0215 ranges = of_get_property(np, "ranges", &rlen);
0216 if (ranges == NULL || rlen < rs) {
0217 pr_warn("ISA: Non-PCI bridge %pOF has absent or invalid ranges\n",
0218 np);
0219 return;
0220 }
0221
0222
0223 for (i = 0; (i + rs - 1) < rlen; i += rs) {
0224 if (be32_to_cpup(ranges + i) != 1)
0225 continue;
0226 cbase = be32_to_cpup(ranges + i + 1);
0227 size = of_read_number(ranges + i + na + pna, ns);
0228 pbasep = ranges + i + na;
0229 break;
0230 }
0231
0232
0233 if (!size || !pbasep) {
0234 pr_warn("ISA: Non-PCI bridge %pOF has no usable IO range\n",
0235 np);
0236 return;
0237 }
0238
0239
0240 size = PAGE_ALIGN(size);
0241 if (size > 0x10000)
0242 size = 0x10000;
0243
0244
0245 pbase = of_translate_address(np, pbasep);
0246 if (pbase == OF_BAD_ADDR) {
0247 pr_warn("ISA: Non-PCI bridge %pOF failed to translate IO base\n",
0248 np);
0249 return;
0250 }
0251
0252
0253 if ((cbase & ~PAGE_MASK) || (pbase & ~PAGE_MASK)) {
0254 pr_warn("ISA: Non-PCI bridge %pOF has non aligned IO range\n",
0255 np);
0256 return;
0257 }
0258
0259
0260 isa_bridge_devnode = np;
0261
0262
0263
0264
0265 isa_io_base = ISA_IO_BASE;
0266 remap_isa_base(pbase, size);
0267
0268 pr_debug("ISA: Non-PCI bridge is %pOF\n", np);
0269 }
0270
0271
0272
0273
0274
0275 static void isa_bridge_find_late(struct pci_dev *pdev,
0276 struct device_node *devnode)
0277 {
0278 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
0279
0280
0281 isa_bridge_devnode = of_node_get(devnode);
0282 isa_bridge_pcidev = pdev;
0283
0284
0285 pci_process_ISA_OF_ranges(devnode, hose->io_base_phys);
0286
0287
0288 isa_io_base = ISA_IO_BASE;
0289
0290 pr_debug("ISA bridge (late) is %pOF on %s\n",
0291 devnode, pci_name(pdev));
0292 }
0293
0294
0295
0296
0297 static void isa_bridge_remove(void)
0298 {
0299 pr_debug("ISA bridge removed !\n");
0300
0301
0302
0303
0304
0305
0306 isa_io_base = ISA_IO_BASE;
0307
0308
0309 of_node_put(isa_bridge_devnode);
0310 isa_bridge_devnode = NULL;
0311 isa_bridge_pcidev = NULL;
0312
0313
0314 vunmap_range(ISA_IO_BASE, ISA_IO_BASE + 0x10000);
0315 }
0316
0317
0318
0319
0320 static int isa_bridge_notify(struct notifier_block *nb, unsigned long action,
0321 void *data)
0322 {
0323 struct device *dev = data;
0324 struct pci_dev *pdev = to_pci_dev(dev);
0325 struct device_node *devnode = pci_device_to_OF_node(pdev);
0326
0327 switch(action) {
0328 case BUS_NOTIFY_ADD_DEVICE:
0329
0330 if (isa_bridge_devnode && isa_bridge_devnode == devnode &&
0331 !isa_bridge_pcidev) {
0332 pr_debug("ISA bridge PCI attached: %s\n",
0333 pci_name(pdev));
0334 isa_bridge_pcidev = pdev;
0335 }
0336
0337
0338
0339
0340 if (!isa_bridge_devnode && of_node_is_type(devnode, "isa"))
0341 isa_bridge_find_late(pdev, devnode);
0342
0343 return 0;
0344 case BUS_NOTIFY_DEL_DEVICE:
0345
0346 if (pdev == isa_bridge_pcidev ||
0347 (devnode && devnode == isa_bridge_devnode))
0348 isa_bridge_remove();
0349 return 0;
0350 }
0351 return 0;
0352 }
0353
0354 static struct notifier_block isa_bridge_notifier = {
0355 .notifier_call = isa_bridge_notify
0356 };
0357
0358
0359
0360
0361
0362 static int __init isa_bridge_init(void)
0363 {
0364 bus_register_notifier(&pci_bus_type, &isa_bridge_notifier);
0365 return 0;
0366 }
0367 arch_initcall(isa_bridge_init);