Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 1 | #undef DEBUG |
| 2 | |
| 3 | #include <linux/kernel.h> |
| 4 | #include <linux/string.h> |
| 5 | #include <linux/pci_regs.h> |
| 6 | #include <linux/module.h> |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 7 | #include <linux/ioport.h> |
Timur Tabi | 29cfe6f | 2007-02-16 12:01:29 -0600 | [diff] [blame] | 8 | #include <linux/etherdevice.h> |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame^] | 9 | #include <linux/of_address.h> |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 10 | #include <asm/prom.h> |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 11 | #include <asm/pci-bridge.h> |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 12 | |
| 13 | #ifdef DEBUG |
| 14 | #define DBG(fmt...) do { printk(fmt); } while(0) |
| 15 | #else |
| 16 | #define DBG(fmt...) do { } while(0) |
| 17 | #endif |
| 18 | |
| 19 | #ifdef CONFIG_PPC64 |
| 20 | #define PRu64 "%lx" |
| 21 | #else |
| 22 | #define PRu64 "%llx" |
| 23 | #endif |
| 24 | |
| 25 | /* Max address size we deal with */ |
| 26 | #define OF_MAX_ADDR_CELLS 4 |
| 27 | #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \ |
Paul Mackerras | 7792682 | 2007-07-26 13:44:36 +1000 | [diff] [blame] | 28 | (ns) > 0) |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 29 | |
Andy Fleming | 83efafb | 2006-10-16 16:03:33 -0500 | [diff] [blame] | 30 | static struct of_bus *of_match_bus(struct device_node *np); |
Andy Fleming | 83efafb | 2006-10-16 16:03:33 -0500 | [diff] [blame] | 31 | |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 32 | /* Debug utility */ |
| 33 | #ifdef DEBUG |
Jeremy Kerr | b5a1a9a | 2006-07-04 16:46:44 +1000 | [diff] [blame] | 34 | static void of_dump_addr(const char *s, const u32 *addr, int na) |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 35 | { |
| 36 | printk("%s", s); |
| 37 | while(na--) |
| 38 | printk(" %08x", *(addr++)); |
| 39 | printk("\n"); |
| 40 | } |
| 41 | #else |
Jeremy Kerr | b5a1a9a | 2006-07-04 16:46:44 +1000 | [diff] [blame] | 42 | static void of_dump_addr(const char *s, const u32 *addr, int na) { } |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 43 | #endif |
| 44 | |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 45 | |
| 46 | /* Callbacks for bus specific translators */ |
| 47 | struct of_bus { |
| 48 | const char *name; |
| 49 | const char *addresses; |
| 50 | int (*match)(struct device_node *parent); |
| 51 | void (*count_cells)(struct device_node *child, |
| 52 | int *addrc, int *sizec); |
Jeremy Kerr | b5a1a9a | 2006-07-04 16:46:44 +1000 | [diff] [blame] | 53 | u64 (*map)(u32 *addr, const u32 *range, |
| 54 | int na, int ns, int pna); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 55 | int (*translate)(u32 *addr, u64 offset, int na); |
Jeremy Kerr | b5a1a9a | 2006-07-04 16:46:44 +1000 | [diff] [blame] | 56 | unsigned int (*get_flags)(const u32 *addr); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | |
| 60 | /* |
| 61 | * Default translator (generic bus) |
| 62 | */ |
| 63 | |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 64 | static void of_bus_default_count_cells(struct device_node *dev, |
| 65 | int *addrc, int *sizec) |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 66 | { |
| 67 | if (addrc) |
Stephen Rothwell | a8bda5d | 2007-04-03 10:56:50 +1000 | [diff] [blame] | 68 | *addrc = of_n_addr_cells(dev); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 69 | if (sizec) |
Stephen Rothwell | 9213fee | 2007-04-03 10:57:48 +1000 | [diff] [blame] | 70 | *sizec = of_n_size_cells(dev); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 71 | } |
| 72 | |
Jeremy Kerr | b5a1a9a | 2006-07-04 16:46:44 +1000 | [diff] [blame] | 73 | static u64 of_bus_default_map(u32 *addr, const u32 *range, |
| 74 | int na, int ns, int pna) |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 75 | { |
| 76 | u64 cp, s, da; |
| 77 | |
Benjamin Herrenschmidt | cc9fd71 | 2006-07-03 19:35:17 +1000 | [diff] [blame] | 78 | cp = of_read_number(range, na); |
| 79 | s = of_read_number(range + na + pna, ns); |
| 80 | da = of_read_number(addr, na); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 81 | |
| 82 | DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n", |
| 83 | cp, s, da); |
| 84 | |
| 85 | if (da < cp || da >= (cp + s)) |
| 86 | return OF_BAD_ADDR; |
| 87 | return da - cp; |
| 88 | } |
| 89 | |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 90 | static int of_bus_default_translate(u32 *addr, u64 offset, int na) |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 91 | { |
Benjamin Herrenschmidt | cc9fd71 | 2006-07-03 19:35:17 +1000 | [diff] [blame] | 92 | u64 a = of_read_number(addr, na); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 93 | memset(addr, 0, na * 4); |
| 94 | a += offset; |
| 95 | if (na > 1) |
| 96 | addr[na - 2] = a >> 32; |
| 97 | addr[na - 1] = a & 0xffffffffu; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
Jeremy Kerr | b5a1a9a | 2006-07-04 16:46:44 +1000 | [diff] [blame] | 102 | static unsigned int of_bus_default_get_flags(const u32 *addr) |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 103 | { |
| 104 | return IORESOURCE_MEM; |
| 105 | } |
| 106 | |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 107 | |
Andy Fleming | 83efafb | 2006-10-16 16:03:33 -0500 | [diff] [blame] | 108 | #ifdef CONFIG_PCI |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 109 | /* |
| 110 | * PCI bus specific translator |
| 111 | */ |
| 112 | |
| 113 | static int of_bus_pci_match(struct device_node *np) |
| 114 | { |
Paul Mackerras | d5f0790 | 2006-01-14 15:08:50 +1100 | [diff] [blame] | 115 | /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */ |
| 116 | return !strcmp(np->type, "pci") || !strcmp(np->type, "vci"); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static void of_bus_pci_count_cells(struct device_node *np, |
| 120 | int *addrc, int *sizec) |
| 121 | { |
| 122 | if (addrc) |
| 123 | *addrc = 3; |
| 124 | if (sizec) |
| 125 | *sizec = 2; |
| 126 | } |
| 127 | |
Jeremy Kerr | b5a1a9a | 2006-07-04 16:46:44 +1000 | [diff] [blame] | 128 | static unsigned int of_bus_pci_get_flags(const u32 *addr) |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 129 | { |
| 130 | unsigned int flags = 0; |
| 131 | u32 w = addr[0]; |
| 132 | |
| 133 | switch((w >> 24) & 0x03) { |
| 134 | case 0x01: |
| 135 | flags |= IORESOURCE_IO; |
Olof Johansson | 4468f01 | 2006-11-27 21:21:29 -0600 | [diff] [blame] | 136 | break; |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 137 | case 0x02: /* 32 bits */ |
| 138 | case 0x03: /* 64 bits */ |
| 139 | flags |= IORESOURCE_MEM; |
Olof Johansson | 4468f01 | 2006-11-27 21:21:29 -0600 | [diff] [blame] | 140 | break; |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 141 | } |
| 142 | if (w & 0x40000000) |
| 143 | flags |= IORESOURCE_PREFETCH; |
| 144 | return flags; |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 145 | } |
| 146 | |
Benjamin Herrenschmidt | 67260ac | 2008-07-17 15:53:31 +1000 | [diff] [blame] | 147 | static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna) |
| 148 | { |
| 149 | u64 cp, s, da; |
| 150 | unsigned int af, rf; |
| 151 | |
| 152 | af = of_bus_pci_get_flags(addr); |
| 153 | rf = of_bus_pci_get_flags(range); |
| 154 | |
| 155 | /* Check address type match */ |
| 156 | if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO)) |
| 157 | return OF_BAD_ADDR; |
| 158 | |
| 159 | /* Read address values, skipping high cell */ |
| 160 | cp = of_read_number(range + 1, na - 1); |
| 161 | s = of_read_number(range + na + pna, ns); |
| 162 | da = of_read_number(addr + 1, na - 1); |
| 163 | |
| 164 | DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da); |
| 165 | |
| 166 | if (da < cp || da >= (cp + s)) |
| 167 | return OF_BAD_ADDR; |
| 168 | return da - cp; |
| 169 | } |
| 170 | |
| 171 | static int of_bus_pci_translate(u32 *addr, u64 offset, int na) |
| 172 | { |
| 173 | return of_bus_default_translate(addr + 1, offset, na - 1); |
| 174 | } |
| 175 | |
Andy Fleming | 83efafb | 2006-10-16 16:03:33 -0500 | [diff] [blame] | 176 | const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size, |
| 177 | unsigned int *flags) |
| 178 | { |
| 179 | const u32 *prop; |
| 180 | unsigned int psize; |
| 181 | struct device_node *parent; |
| 182 | struct of_bus *bus; |
| 183 | int onesize, i, na, ns; |
| 184 | |
| 185 | /* Get parent & match bus type */ |
| 186 | parent = of_get_parent(dev); |
| 187 | if (parent == NULL) |
| 188 | return NULL; |
| 189 | bus = of_match_bus(parent); |
| 190 | if (strcmp(bus->name, "pci")) { |
| 191 | of_node_put(parent); |
| 192 | return NULL; |
| 193 | } |
| 194 | bus->count_cells(dev, &na, &ns); |
| 195 | of_node_put(parent); |
| 196 | if (!OF_CHECK_COUNTS(na, ns)) |
| 197 | return NULL; |
| 198 | |
| 199 | /* Get "reg" or "assigned-addresses" property */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 200 | prop = of_get_property(dev, bus->addresses, &psize); |
Andy Fleming | 83efafb | 2006-10-16 16:03:33 -0500 | [diff] [blame] | 201 | if (prop == NULL) |
| 202 | return NULL; |
| 203 | psize /= 4; |
| 204 | |
| 205 | onesize = na + ns; |
| 206 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) |
| 207 | if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) { |
| 208 | if (size) |
| 209 | *size = of_read_number(prop + na, ns); |
| 210 | if (flags) |
| 211 | *flags = bus->get_flags(prop); |
| 212 | return prop; |
| 213 | } |
| 214 | return NULL; |
| 215 | } |
| 216 | EXPORT_SYMBOL(of_get_pci_address); |
| 217 | |
| 218 | int of_pci_address_to_resource(struct device_node *dev, int bar, |
| 219 | struct resource *r) |
| 220 | { |
| 221 | const u32 *addrp; |
| 222 | u64 size; |
| 223 | unsigned int flags; |
| 224 | |
| 225 | addrp = of_get_pci_address(dev, bar, &size, &flags); |
| 226 | if (addrp == NULL) |
| 227 | return -EINVAL; |
| 228 | return __of_address_to_resource(dev, addrp, size, flags, r); |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(of_pci_address_to_resource); |
| 231 | |
Andy Fleming | 83efafb | 2006-10-16 16:03:33 -0500 | [diff] [blame] | 232 | int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq) |
| 233 | { |
| 234 | struct device_node *dn, *ppnode; |
| 235 | struct pci_dev *ppdev; |
| 236 | u32 lspec; |
| 237 | u32 laddr[3]; |
| 238 | u8 pin; |
| 239 | int rc; |
| 240 | |
| 241 | /* Check if we have a device node, if yes, fallback to standard OF |
| 242 | * parsing |
| 243 | */ |
| 244 | dn = pci_device_to_OF_node(pdev); |
Adhemerval Zanella | 4b824de | 2008-11-19 03:55:35 +0000 | [diff] [blame] | 245 | if (dn) { |
| 246 | rc = of_irq_map_one(dn, 0, out_irq); |
| 247 | if (!rc) |
| 248 | return rc; |
| 249 | } |
Andy Fleming | 83efafb | 2006-10-16 16:03:33 -0500 | [diff] [blame] | 250 | |
| 251 | /* Ok, we don't, time to have fun. Let's start by building up an |
| 252 | * interrupt spec. we assume #interrupt-cells is 1, which is standard |
| 253 | * for PCI. If you do different, then don't use that routine. |
| 254 | */ |
| 255 | rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin); |
| 256 | if (rc != 0) |
| 257 | return rc; |
| 258 | /* No pin, exit */ |
| 259 | if (pin == 0) |
| 260 | return -ENODEV; |
| 261 | |
| 262 | /* Now we walk up the PCI tree */ |
| 263 | lspec = pin; |
| 264 | for (;;) { |
| 265 | /* Get the pci_dev of our parent */ |
| 266 | ppdev = pdev->bus->self; |
| 267 | |
| 268 | /* Ouch, it's a host bridge... */ |
| 269 | if (ppdev == NULL) { |
| 270 | #ifdef CONFIG_PPC64 |
| 271 | ppnode = pci_bus_to_OF_node(pdev->bus); |
| 272 | #else |
| 273 | struct pci_controller *host; |
| 274 | host = pci_bus_to_host(pdev->bus); |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 275 | ppnode = host ? host->dn : NULL; |
Andy Fleming | 83efafb | 2006-10-16 16:03:33 -0500 | [diff] [blame] | 276 | #endif |
| 277 | /* No node for host bridge ? give up */ |
| 278 | if (ppnode == NULL) |
| 279 | return -EINVAL; |
| 280 | } else |
| 281 | /* We found a P2P bridge, check if it has a node */ |
| 282 | ppnode = pci_device_to_OF_node(ppdev); |
| 283 | |
| 284 | /* Ok, we have found a parent with a device-node, hand over to |
| 285 | * the OF parsing code. |
| 286 | * We build a unit address from the linux device to be used for |
| 287 | * resolution. Note that we use the linux bus number which may |
| 288 | * not match your firmware bus numbering. |
| 289 | * Fortunately, in most cases, interrupt-map-mask doesn't include |
| 290 | * the bus number as part of the matching. |
| 291 | * You should still be careful about that though if you intend |
| 292 | * to rely on this function (you ship a firmware that doesn't |
| 293 | * create device nodes for all PCI devices). |
| 294 | */ |
| 295 | if (ppnode) |
| 296 | break; |
| 297 | |
| 298 | /* We can only get here if we hit a P2P bridge with no node, |
| 299 | * let's do standard swizzling and try again |
| 300 | */ |
Bjorn Helgaas | 3f9455d4 | 2008-12-09 16:12:27 -0700 | [diff] [blame] | 301 | lspec = pci_swizzle_interrupt_pin(pdev, lspec); |
Andy Fleming | 83efafb | 2006-10-16 16:03:33 -0500 | [diff] [blame] | 302 | pdev = ppdev; |
| 303 | } |
| 304 | |
| 305 | laddr[0] = (pdev->bus->number << 16) |
| 306 | | (pdev->devfn << 8); |
| 307 | laddr[1] = laddr[2] = 0; |
| 308 | return of_irq_map_raw(ppnode, &lspec, 1, laddr, out_irq); |
| 309 | } |
| 310 | EXPORT_SYMBOL_GPL(of_irq_map_pci); |
| 311 | #endif /* CONFIG_PCI */ |
| 312 | |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 313 | /* |
| 314 | * ISA bus specific translator |
| 315 | */ |
| 316 | |
| 317 | static int of_bus_isa_match(struct device_node *np) |
| 318 | { |
| 319 | return !strcmp(np->name, "isa"); |
| 320 | } |
| 321 | |
| 322 | static void of_bus_isa_count_cells(struct device_node *child, |
| 323 | int *addrc, int *sizec) |
| 324 | { |
| 325 | if (addrc) |
| 326 | *addrc = 2; |
| 327 | if (sizec) |
| 328 | *sizec = 1; |
| 329 | } |
| 330 | |
Jeremy Kerr | b5a1a9a | 2006-07-04 16:46:44 +1000 | [diff] [blame] | 331 | static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna) |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 332 | { |
| 333 | u64 cp, s, da; |
| 334 | |
| 335 | /* Check address type match */ |
| 336 | if ((addr[0] ^ range[0]) & 0x00000001) |
| 337 | return OF_BAD_ADDR; |
| 338 | |
| 339 | /* Read address values, skipping high cell */ |
Benjamin Herrenschmidt | cc9fd71 | 2006-07-03 19:35:17 +1000 | [diff] [blame] | 340 | cp = of_read_number(range + 1, na - 1); |
| 341 | s = of_read_number(range + na + pna, ns); |
| 342 | da = of_read_number(addr + 1, na - 1); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 343 | |
| 344 | DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da); |
| 345 | |
| 346 | if (da < cp || da >= (cp + s)) |
| 347 | return OF_BAD_ADDR; |
| 348 | return da - cp; |
| 349 | } |
| 350 | |
| 351 | static int of_bus_isa_translate(u32 *addr, u64 offset, int na) |
| 352 | { |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 353 | return of_bus_default_translate(addr + 1, offset, na - 1); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 354 | } |
| 355 | |
Jeremy Kerr | b5a1a9a | 2006-07-04 16:46:44 +1000 | [diff] [blame] | 356 | static unsigned int of_bus_isa_get_flags(const u32 *addr) |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 357 | { |
| 358 | unsigned int flags = 0; |
| 359 | u32 w = addr[0]; |
| 360 | |
| 361 | if (w & 1) |
| 362 | flags |= IORESOURCE_IO; |
| 363 | else |
| 364 | flags |= IORESOURCE_MEM; |
| 365 | return flags; |
| 366 | } |
| 367 | |
| 368 | |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 369 | /* |
| 370 | * Array of bus specific translators |
| 371 | */ |
| 372 | |
| 373 | static struct of_bus of_busses[] = { |
Andy Fleming | 83efafb | 2006-10-16 16:03:33 -0500 | [diff] [blame] | 374 | #ifdef CONFIG_PCI |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 375 | /* PCI */ |
| 376 | { |
| 377 | .name = "pci", |
| 378 | .addresses = "assigned-addresses", |
| 379 | .match = of_bus_pci_match, |
| 380 | .count_cells = of_bus_pci_count_cells, |
| 381 | .map = of_bus_pci_map, |
| 382 | .translate = of_bus_pci_translate, |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 383 | .get_flags = of_bus_pci_get_flags, |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 384 | }, |
Andy Fleming | 83efafb | 2006-10-16 16:03:33 -0500 | [diff] [blame] | 385 | #endif /* CONFIG_PCI */ |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 386 | /* ISA */ |
| 387 | { |
| 388 | .name = "isa", |
| 389 | .addresses = "reg", |
| 390 | .match = of_bus_isa_match, |
| 391 | .count_cells = of_bus_isa_count_cells, |
| 392 | .map = of_bus_isa_map, |
| 393 | .translate = of_bus_isa_translate, |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 394 | .get_flags = of_bus_isa_get_flags, |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 395 | }, |
| 396 | /* Default */ |
| 397 | { |
| 398 | .name = "default", |
| 399 | .addresses = "reg", |
| 400 | .match = NULL, |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 401 | .count_cells = of_bus_default_count_cells, |
| 402 | .map = of_bus_default_map, |
| 403 | .translate = of_bus_default_translate, |
| 404 | .get_flags = of_bus_default_get_flags, |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 405 | }, |
| 406 | }; |
| 407 | |
| 408 | static struct of_bus *of_match_bus(struct device_node *np) |
| 409 | { |
| 410 | int i; |
| 411 | |
| 412 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) |
| 413 | if (!of_busses[i].match || of_busses[i].match(np)) |
| 414 | return &of_busses[i]; |
| 415 | BUG(); |
| 416 | return NULL; |
| 417 | } |
| 418 | |
| 419 | static int of_translate_one(struct device_node *parent, struct of_bus *bus, |
| 420 | struct of_bus *pbus, u32 *addr, |
Benjamin Herrenschmidt | 837c54d | 2007-12-11 14:48:22 +1100 | [diff] [blame] | 421 | int na, int ns, int pna, const char *rprop) |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 422 | { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 423 | const u32 *ranges; |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 424 | unsigned int rlen; |
| 425 | int rone; |
| 426 | u64 offset = OF_BAD_ADDR; |
| 427 | |
| 428 | /* Normally, an absence of a "ranges" property means we are |
| 429 | * crossing a non-translatable boundary, and thus the addresses |
| 430 | * below the current not cannot be converted to CPU physical ones. |
| 431 | * Unfortunately, while this is very clear in the spec, it's not |
| 432 | * what Apple understood, and they do have things like /uni-n or |
| 433 | * /ht nodes with no "ranges" property and a lot of perfectly |
| 434 | * useable mapped devices below them. Thus we treat the absence of |
| 435 | * "ranges" as equivalent to an empty "ranges" property which means |
| 436 | * a 1:1 translation at that level. It's up to the caller not to try |
| 437 | * to translate addresses that aren't supposed to be translated in |
| 438 | * the first place. --BenH. |
| 439 | */ |
Benjamin Herrenschmidt | 837c54d | 2007-12-11 14:48:22 +1100 | [diff] [blame] | 440 | ranges = of_get_property(parent, rprop, &rlen); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 441 | if (ranges == NULL || rlen == 0) { |
Benjamin Herrenschmidt | cc9fd71 | 2006-07-03 19:35:17 +1000 | [diff] [blame] | 442 | offset = of_read_number(addr, na); |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 443 | memset(addr, 0, pna * 4); |
| 444 | DBG("OF: no ranges, 1:1 translation\n"); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 445 | goto finish; |
| 446 | } |
| 447 | |
| 448 | DBG("OF: walking ranges...\n"); |
| 449 | |
| 450 | /* Now walk through the ranges */ |
| 451 | rlen /= 4; |
| 452 | rone = na + pna + ns; |
| 453 | for (; rlen >= rone; rlen -= rone, ranges += rone) { |
| 454 | offset = bus->map(addr, ranges, na, ns, pna); |
| 455 | if (offset != OF_BAD_ADDR) |
| 456 | break; |
| 457 | } |
| 458 | if (offset == OF_BAD_ADDR) { |
| 459 | DBG("OF: not found !\n"); |
| 460 | return 1; |
| 461 | } |
| 462 | memcpy(addr, ranges + na, 4 * pna); |
| 463 | |
| 464 | finish: |
| 465 | of_dump_addr("OF: parent translation for:", addr, pna); |
Benjamin Herrenschmidt | bb6b9b2 | 2005-11-30 16:54:12 +1100 | [diff] [blame] | 466 | DBG("OF: with offset: "PRu64"\n", offset); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 467 | |
| 468 | /* Translate it into parent bus space */ |
| 469 | return pbus->translate(addr, offset, pna); |
| 470 | } |
| 471 | |
| 472 | |
| 473 | /* |
| 474 | * Translate an address from the device-tree into a CPU physical address, |
| 475 | * this walks up the tree and applies the various bus mappings on the |
| 476 | * way. |
| 477 | * |
| 478 | * Note: We consider that crossing any level with #size-cells == 0 to mean |
| 479 | * that translation is impossible (that is we are not dealing with a value |
| 480 | * that can be mapped to a cpu physical address). This is not really specified |
| 481 | * that way, but this is traditionally the way IBM at least do things |
| 482 | */ |
Benjamin Herrenschmidt | 837c54d | 2007-12-11 14:48:22 +1100 | [diff] [blame] | 483 | u64 __of_translate_address(struct device_node *dev, const u32 *in_addr, |
| 484 | const char *rprop) |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 485 | { |
| 486 | struct device_node *parent = NULL; |
| 487 | struct of_bus *bus, *pbus; |
| 488 | u32 addr[OF_MAX_ADDR_CELLS]; |
| 489 | int na, ns, pna, pns; |
| 490 | u64 result = OF_BAD_ADDR; |
| 491 | |
| 492 | DBG("OF: ** translation for device %s **\n", dev->full_name); |
| 493 | |
| 494 | /* Increase refcount at current level */ |
| 495 | of_node_get(dev); |
| 496 | |
| 497 | /* Get parent & match bus type */ |
| 498 | parent = of_get_parent(dev); |
| 499 | if (parent == NULL) |
| 500 | goto bail; |
| 501 | bus = of_match_bus(parent); |
| 502 | |
| 503 | /* Cound address cells & copy address locally */ |
| 504 | bus->count_cells(dev, &na, &ns); |
| 505 | if (!OF_CHECK_COUNTS(na, ns)) { |
| 506 | printk(KERN_ERR "prom_parse: Bad cell count for %s\n", |
| 507 | dev->full_name); |
| 508 | goto bail; |
| 509 | } |
| 510 | memcpy(addr, in_addr, na * 4); |
| 511 | |
| 512 | DBG("OF: bus is %s (na=%d, ns=%d) on %s\n", |
| 513 | bus->name, na, ns, parent->full_name); |
| 514 | of_dump_addr("OF: translating address:", addr, na); |
| 515 | |
| 516 | /* Translate */ |
| 517 | for (;;) { |
| 518 | /* Switch to parent bus */ |
| 519 | of_node_put(dev); |
| 520 | dev = parent; |
| 521 | parent = of_get_parent(dev); |
| 522 | |
| 523 | /* If root, we have finished */ |
| 524 | if (parent == NULL) { |
| 525 | DBG("OF: reached root node\n"); |
Benjamin Herrenschmidt | cc9fd71 | 2006-07-03 19:35:17 +1000 | [diff] [blame] | 526 | result = of_read_number(addr, na); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 527 | break; |
| 528 | } |
| 529 | |
| 530 | /* Get new parent bus and counts */ |
| 531 | pbus = of_match_bus(parent); |
| 532 | pbus->count_cells(dev, &pna, &pns); |
| 533 | if (!OF_CHECK_COUNTS(pna, pns)) { |
| 534 | printk(KERN_ERR "prom_parse: Bad cell count for %s\n", |
| 535 | dev->full_name); |
| 536 | break; |
| 537 | } |
| 538 | |
| 539 | DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n", |
| 540 | pbus->name, pna, pns, parent->full_name); |
| 541 | |
| 542 | /* Apply bus translation */ |
Benjamin Herrenschmidt | 837c54d | 2007-12-11 14:48:22 +1100 | [diff] [blame] | 543 | if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop)) |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 544 | break; |
| 545 | |
| 546 | /* Complete the move up one level */ |
| 547 | na = pna; |
| 548 | ns = pns; |
| 549 | bus = pbus; |
| 550 | |
| 551 | of_dump_addr("OF: one level translation:", addr, na); |
| 552 | } |
| 553 | bail: |
| 554 | of_node_put(parent); |
| 555 | of_node_put(dev); |
| 556 | |
| 557 | return result; |
| 558 | } |
Benjamin Herrenschmidt | 837c54d | 2007-12-11 14:48:22 +1100 | [diff] [blame] | 559 | |
| 560 | u64 of_translate_address(struct device_node *dev, const u32 *in_addr) |
| 561 | { |
| 562 | return __of_translate_address(dev, in_addr, "ranges"); |
| 563 | } |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 564 | EXPORT_SYMBOL(of_translate_address); |
| 565 | |
Benjamin Herrenschmidt | 837c54d | 2007-12-11 14:48:22 +1100 | [diff] [blame] | 566 | u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr) |
| 567 | { |
| 568 | return __of_translate_address(dev, in_addr, "dma-ranges"); |
| 569 | } |
| 570 | EXPORT_SYMBOL(of_translate_dma_address); |
| 571 | |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 572 | const u32 *of_get_address(struct device_node *dev, int index, u64 *size, |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 573 | unsigned int *flags) |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 574 | { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 575 | const u32 *prop; |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 576 | unsigned int psize; |
| 577 | struct device_node *parent; |
| 578 | struct of_bus *bus; |
| 579 | int onesize, i, na, ns; |
| 580 | |
| 581 | /* Get parent & match bus type */ |
| 582 | parent = of_get_parent(dev); |
| 583 | if (parent == NULL) |
| 584 | return NULL; |
| 585 | bus = of_match_bus(parent); |
| 586 | bus->count_cells(dev, &na, &ns); |
| 587 | of_node_put(parent); |
| 588 | if (!OF_CHECK_COUNTS(na, ns)) |
| 589 | return NULL; |
| 590 | |
| 591 | /* Get "reg" or "assigned-addresses" property */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 592 | prop = of_get_property(dev, bus->addresses, &psize); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 593 | if (prop == NULL) |
| 594 | return NULL; |
| 595 | psize /= 4; |
| 596 | |
| 597 | onesize = na + ns; |
| 598 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) |
| 599 | if (i == index) { |
| 600 | if (size) |
Benjamin Herrenschmidt | cc9fd71 | 2006-07-03 19:35:17 +1000 | [diff] [blame] | 601 | *size = of_read_number(prop + na, ns); |
Benjamin Herrenschmidt | d2dd482 | 2005-11-30 16:57:28 +1100 | [diff] [blame] | 602 | if (flags) |
| 603 | *flags = bus->get_flags(prop); |
Benjamin Herrenschmidt | d1405b8 | 2005-11-23 17:53:42 +1100 | [diff] [blame] | 604 | return prop; |
| 605 | } |
| 606 | return NULL; |
| 607 | } |
| 608 | EXPORT_SYMBOL(of_get_address); |
| 609 | |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 610 | void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop, |
Jeremy Kerr | d4ad66f | 2006-05-18 18:05:15 +1000 | [diff] [blame] | 611 | unsigned long *busno, unsigned long *phys, unsigned long *size) |
| 612 | { |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 613 | const u32 *dma_window; |
| 614 | u32 cells; |
| 615 | const unsigned char *prop; |
Jeremy Kerr | d4ad66f | 2006-05-18 18:05:15 +1000 | [diff] [blame] | 616 | |
Jeremy Kerr | a7f67bd | 2006-07-12 15:35:54 +1000 | [diff] [blame] | 617 | dma_window = dma_window_prop; |
Jeremy Kerr | d4ad66f | 2006-05-18 18:05:15 +1000 | [diff] [blame] | 618 | |
| 619 | /* busno is always one cell */ |
| 620 | *busno = *(dma_window++); |
| 621 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 622 | prop = of_get_property(dn, "ibm,#dma-address-cells", NULL); |
Will Schmidt | 03ac829 | 2006-05-30 13:38:40 -0500 | [diff] [blame] | 623 | if (!prop) |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 624 | prop = of_get_property(dn, "#address-cells", NULL); |
Will Schmidt | 03ac829 | 2006-05-30 13:38:40 -0500 | [diff] [blame] | 625 | |
Stephen Rothwell | a8bda5d | 2007-04-03 10:56:50 +1000 | [diff] [blame] | 626 | cells = prop ? *(u32 *)prop : of_n_addr_cells(dn); |
Benjamin Herrenschmidt | cc9fd71 | 2006-07-03 19:35:17 +1000 | [diff] [blame] | 627 | *phys = of_read_number(dma_window, cells); |
Jeremy Kerr | d4ad66f | 2006-05-18 18:05:15 +1000 | [diff] [blame] | 628 | |
| 629 | dma_window += cells; |
| 630 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 631 | prop = of_get_property(dn, "ibm,#dma-size-cells", NULL); |
Stephen Rothwell | 9213fee | 2007-04-03 10:57:48 +1000 | [diff] [blame] | 632 | cells = prop ? *(u32 *)prop : of_n_size_cells(dn); |
Benjamin Herrenschmidt | cc9fd71 | 2006-07-03 19:35:17 +1000 | [diff] [blame] | 633 | *size = of_read_number(dma_window, cells); |
Jeremy Kerr | d4ad66f | 2006-05-18 18:05:15 +1000 | [diff] [blame] | 634 | } |
Benjamin Herrenschmidt | cc9fd71 | 2006-07-03 19:35:17 +1000 | [diff] [blame] | 635 | |
Timur Tabi | 29cfe6f | 2007-02-16 12:01:29 -0600 | [diff] [blame] | 636 | /** |
| 637 | * Search the device tree for the best MAC address to use. 'mac-address' is |
| 638 | * checked first, because that is supposed to contain to "most recent" MAC |
| 639 | * address. If that isn't set, then 'local-mac-address' is checked next, |
| 640 | * because that is the default address. If that isn't set, then the obsolete |
| 641 | * 'address' is checked, just in case we're using an old device tree. |
| 642 | * |
| 643 | * Note that the 'address' property is supposed to contain a virtual address of |
| 644 | * the register set, but some DTS files have redefined that property to be the |
| 645 | * MAC address. |
| 646 | * |
| 647 | * All-zero MAC addresses are rejected, because those could be properties that |
| 648 | * exist in the device tree, but were not set by U-Boot. For example, the |
| 649 | * DTS could define 'mac-address' and 'local-mac-address', with zero MAC |
| 650 | * addresses. Some older U-Boots only initialized 'local-mac-address'. In |
| 651 | * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists |
| 652 | * but is all zeros. |
| 653 | */ |
| 654 | const void *of_get_mac_address(struct device_node *np) |
| 655 | { |
| 656 | struct property *pp; |
| 657 | |
| 658 | pp = of_find_property(np, "mac-address", NULL); |
| 659 | if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value)) |
| 660 | return pp->value; |
| 661 | |
| 662 | pp = of_find_property(np, "local-mac-address", NULL); |
| 663 | if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value)) |
| 664 | return pp->value; |
| 665 | |
| 666 | pp = of_find_property(np, "address", NULL); |
| 667 | if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value)) |
| 668 | return pp->value; |
| 669 | |
| 670 | return NULL; |
| 671 | } |
| 672 | EXPORT_SYMBOL(of_get_mac_address); |