Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 1 | |
Arnd Bergmann | 5019f0b | 2012-04-11 17:30:11 +0000 | [diff] [blame] | 2 | #include <linux/device.h> |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 3 | #include <linux/io.h> |
| 4 | #include <linux/ioport.h> |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 5 | #include <linux/module.h> |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 6 | #include <linux/of_address.h> |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 7 | #include <linux/pci_regs.h> |
| 8 | #include <linux/string.h> |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 9 | |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 10 | /* Max address size we deal with */ |
| 11 | #define OF_MAX_ADDR_CELLS 4 |
Stephen Warren | 5d61b16 | 2012-07-25 17:34:37 -0600 | [diff] [blame] | 12 | #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS) |
| 13 | #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 14 | |
| 15 | static struct of_bus *of_match_bus(struct device_node *np); |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 16 | static int __of_address_to_resource(struct device_node *dev, |
| 17 | const __be32 *addrp, u64 size, unsigned int flags, |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 18 | const char *name, struct resource *r); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 19 | |
| 20 | /* Debug utility */ |
| 21 | #ifdef DEBUG |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 22 | static void of_dump_addr(const char *s, const __be32 *addr, int na) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 23 | { |
| 24 | printk(KERN_DEBUG "%s", s); |
| 25 | while (na--) |
Grant Likely | 154063a9 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 26 | printk(" %08x", be32_to_cpu(*(addr++))); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 27 | printk("\n"); |
| 28 | } |
| 29 | #else |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 30 | static void of_dump_addr(const char *s, const __be32 *addr, int na) { } |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 31 | #endif |
| 32 | |
| 33 | /* Callbacks for bus specific translators */ |
| 34 | struct of_bus { |
| 35 | const char *name; |
| 36 | const char *addresses; |
| 37 | int (*match)(struct device_node *parent); |
| 38 | void (*count_cells)(struct device_node *child, |
| 39 | int *addrc, int *sizec); |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 40 | u64 (*map)(__be32 *addr, const __be32 *range, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 41 | int na, int ns, int pna); |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 42 | int (*translate)(__be32 *addr, u64 offset, int na); |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 43 | unsigned int (*get_flags)(const __be32 *addr); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * Default translator (generic bus) |
| 48 | */ |
| 49 | |
| 50 | static void of_bus_default_count_cells(struct device_node *dev, |
| 51 | int *addrc, int *sizec) |
| 52 | { |
| 53 | if (addrc) |
| 54 | *addrc = of_n_addr_cells(dev); |
| 55 | if (sizec) |
| 56 | *sizec = of_n_size_cells(dev); |
| 57 | } |
| 58 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 59 | static u64 of_bus_default_map(__be32 *addr, const __be32 *range, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 60 | int na, int ns, int pna) |
| 61 | { |
| 62 | u64 cp, s, da; |
| 63 | |
| 64 | cp = of_read_number(range, na); |
| 65 | s = of_read_number(range + na + pna, ns); |
| 66 | da = of_read_number(addr, na); |
| 67 | |
| 68 | pr_debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", |
| 69 | (unsigned long long)cp, (unsigned long long)s, |
| 70 | (unsigned long long)da); |
| 71 | |
| 72 | if (da < cp || da >= (cp + s)) |
| 73 | return OF_BAD_ADDR; |
| 74 | return da - cp; |
| 75 | } |
| 76 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 77 | static int of_bus_default_translate(__be32 *addr, u64 offset, int na) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 78 | { |
| 79 | u64 a = of_read_number(addr, na); |
| 80 | memset(addr, 0, na * 4); |
| 81 | a += offset; |
| 82 | if (na > 1) |
Grant Likely | 154063a9 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 83 | addr[na - 2] = cpu_to_be32(a >> 32); |
| 84 | addr[na - 1] = cpu_to_be32(a & 0xffffffffu); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 89 | static unsigned int of_bus_default_get_flags(const __be32 *addr) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 90 | { |
| 91 | return IORESOURCE_MEM; |
| 92 | } |
| 93 | |
Gregory CLEMENT | 25a3157 | 2014-02-19 23:14:55 +0100 | [diff] [blame^] | 94 | #ifdef CONFIG_OF_ADDRESS_PCI |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 95 | /* |
| 96 | * PCI bus specific translator |
| 97 | */ |
| 98 | |
| 99 | static int of_bus_pci_match(struct device_node *np) |
| 100 | { |
Benjamin Herrenschmidt | 6dd18e4 | 2013-07-03 16:01:10 +1000 | [diff] [blame] | 101 | /* |
| 102 | * "vci" is for the /chaos bridge on 1st-gen PCI powermacs |
| 103 | * "ht" is hypertransport |
| 104 | */ |
| 105 | return !strcmp(np->type, "pci") || !strcmp(np->type, "vci") || |
| 106 | !strcmp(np->type, "ht"); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void of_bus_pci_count_cells(struct device_node *np, |
| 110 | int *addrc, int *sizec) |
| 111 | { |
| 112 | if (addrc) |
| 113 | *addrc = 3; |
| 114 | if (sizec) |
| 115 | *sizec = 2; |
| 116 | } |
| 117 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 118 | static unsigned int of_bus_pci_get_flags(const __be32 *addr) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 119 | { |
| 120 | unsigned int flags = 0; |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 121 | u32 w = be32_to_cpup(addr); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 122 | |
| 123 | switch((w >> 24) & 0x03) { |
| 124 | case 0x01: |
| 125 | flags |= IORESOURCE_IO; |
| 126 | break; |
| 127 | case 0x02: /* 32 bits */ |
| 128 | case 0x03: /* 64 bits */ |
| 129 | flags |= IORESOURCE_MEM; |
| 130 | break; |
| 131 | } |
| 132 | if (w & 0x40000000) |
| 133 | flags |= IORESOURCE_PREFETCH; |
| 134 | return flags; |
| 135 | } |
| 136 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 137 | static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 138 | int pna) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 139 | { |
| 140 | u64 cp, s, da; |
| 141 | unsigned int af, rf; |
| 142 | |
| 143 | af = of_bus_pci_get_flags(addr); |
| 144 | rf = of_bus_pci_get_flags(range); |
| 145 | |
| 146 | /* Check address type match */ |
| 147 | if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO)) |
| 148 | return OF_BAD_ADDR; |
| 149 | |
| 150 | /* Read address values, skipping high cell */ |
| 151 | cp = of_read_number(range + 1, na - 1); |
| 152 | s = of_read_number(range + na + pna, ns); |
| 153 | da = of_read_number(addr + 1, na - 1); |
| 154 | |
| 155 | pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n", |
| 156 | (unsigned long long)cp, (unsigned long long)s, |
| 157 | (unsigned long long)da); |
| 158 | |
| 159 | if (da < cp || da >= (cp + s)) |
| 160 | return OF_BAD_ADDR; |
| 161 | return da - cp; |
| 162 | } |
| 163 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 164 | static int of_bus_pci_translate(__be32 *addr, u64 offset, int na) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 165 | { |
| 166 | return of_bus_default_translate(addr + 1, offset, na - 1); |
| 167 | } |
Gregory CLEMENT | 25a3157 | 2014-02-19 23:14:55 +0100 | [diff] [blame^] | 168 | #endif /* CONFIG_OF_ADDRESS_PCI */ |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 169 | |
Gregory CLEMENT | 25a3157 | 2014-02-19 23:14:55 +0100 | [diff] [blame^] | 170 | #ifdef CONFIG_PCI |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 171 | const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 172 | unsigned int *flags) |
| 173 | { |
Jeremy Kerr | a9fadee | 2010-10-10 21:24:10 -0600 | [diff] [blame] | 174 | const __be32 *prop; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 175 | unsigned int psize; |
| 176 | struct device_node *parent; |
| 177 | struct of_bus *bus; |
| 178 | int onesize, i, na, ns; |
| 179 | |
| 180 | /* Get parent & match bus type */ |
| 181 | parent = of_get_parent(dev); |
| 182 | if (parent == NULL) |
| 183 | return NULL; |
| 184 | bus = of_match_bus(parent); |
| 185 | if (strcmp(bus->name, "pci")) { |
| 186 | of_node_put(parent); |
| 187 | return NULL; |
| 188 | } |
| 189 | bus->count_cells(dev, &na, &ns); |
| 190 | of_node_put(parent); |
Stephen Warren | 5d61b16 | 2012-07-25 17:34:37 -0600 | [diff] [blame] | 191 | if (!OF_CHECK_ADDR_COUNT(na)) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 192 | return NULL; |
| 193 | |
| 194 | /* Get "reg" or "assigned-addresses" property */ |
| 195 | prop = of_get_property(dev, bus->addresses, &psize); |
| 196 | if (prop == NULL) |
| 197 | return NULL; |
| 198 | psize /= 4; |
| 199 | |
| 200 | onesize = na + ns; |
Grant Likely | 154063a9 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 201 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) { |
| 202 | u32 val = be32_to_cpu(prop[0]); |
| 203 | if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) { |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 204 | if (size) |
| 205 | *size = of_read_number(prop + na, ns); |
| 206 | if (flags) |
| 207 | *flags = bus->get_flags(prop); |
| 208 | return prop; |
| 209 | } |
Grant Likely | 154063a9 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 210 | } |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 211 | return NULL; |
| 212 | } |
| 213 | EXPORT_SYMBOL(of_get_pci_address); |
| 214 | |
| 215 | int of_pci_address_to_resource(struct device_node *dev, int bar, |
| 216 | struct resource *r) |
| 217 | { |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 218 | const __be32 *addrp; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 219 | u64 size; |
| 220 | unsigned int flags; |
| 221 | |
| 222 | addrp = of_get_pci_address(dev, bar, &size, &flags); |
| 223 | if (addrp == NULL) |
| 224 | return -EINVAL; |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 225 | return __of_address_to_resource(dev, addrp, size, flags, NULL, r); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 226 | } |
| 227 | EXPORT_SYMBOL_GPL(of_pci_address_to_resource); |
Andrew Murray | 29b635c | 2013-05-16 17:55:17 +0200 | [diff] [blame] | 228 | |
| 229 | int of_pci_range_parser_init(struct of_pci_range_parser *parser, |
| 230 | struct device_node *node) |
| 231 | { |
| 232 | const int na = 3, ns = 2; |
| 233 | int rlen; |
| 234 | |
| 235 | parser->node = node; |
| 236 | parser->pna = of_n_addr_cells(node); |
| 237 | parser->np = parser->pna + na + ns; |
| 238 | |
| 239 | parser->range = of_get_property(node, "ranges", &rlen); |
| 240 | if (parser->range == NULL) |
| 241 | return -ENOENT; |
| 242 | |
| 243 | parser->end = parser->range + rlen / sizeof(__be32); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | EXPORT_SYMBOL_GPL(of_pci_range_parser_init); |
| 248 | |
| 249 | struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser, |
| 250 | struct of_pci_range *range) |
| 251 | { |
| 252 | const int na = 3, ns = 2; |
| 253 | |
| 254 | if (!range) |
| 255 | return NULL; |
| 256 | |
| 257 | if (!parser->range || parser->range + parser->np > parser->end) |
| 258 | return NULL; |
| 259 | |
| 260 | range->pci_space = parser->range[0]; |
| 261 | range->flags = of_bus_pci_get_flags(parser->range); |
| 262 | range->pci_addr = of_read_number(parser->range + 1, ns); |
| 263 | range->cpu_addr = of_translate_address(parser->node, |
| 264 | parser->range + na); |
| 265 | range->size = of_read_number(parser->range + parser->pna + na, ns); |
| 266 | |
| 267 | parser->range += parser->np; |
| 268 | |
| 269 | /* Now consume following elements while they are contiguous */ |
| 270 | while (parser->range + parser->np <= parser->end) { |
| 271 | u32 flags, pci_space; |
| 272 | u64 pci_addr, cpu_addr, size; |
| 273 | |
| 274 | pci_space = be32_to_cpup(parser->range); |
| 275 | flags = of_bus_pci_get_flags(parser->range); |
| 276 | pci_addr = of_read_number(parser->range + 1, ns); |
| 277 | cpu_addr = of_translate_address(parser->node, |
| 278 | parser->range + na); |
| 279 | size = of_read_number(parser->range + parser->pna + na, ns); |
| 280 | |
| 281 | if (flags != range->flags) |
| 282 | break; |
| 283 | if (pci_addr != range->pci_addr + range->size || |
| 284 | cpu_addr != range->cpu_addr + range->size) |
| 285 | break; |
| 286 | |
| 287 | range->size += size; |
| 288 | parser->range += parser->np; |
| 289 | } |
| 290 | |
| 291 | return range; |
| 292 | } |
| 293 | EXPORT_SYMBOL_GPL(of_pci_range_parser_one); |
| 294 | |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 295 | #endif /* CONFIG_PCI */ |
| 296 | |
| 297 | /* |
| 298 | * ISA bus specific translator |
| 299 | */ |
| 300 | |
| 301 | static int of_bus_isa_match(struct device_node *np) |
| 302 | { |
| 303 | return !strcmp(np->name, "isa"); |
| 304 | } |
| 305 | |
| 306 | static void of_bus_isa_count_cells(struct device_node *child, |
| 307 | int *addrc, int *sizec) |
| 308 | { |
| 309 | if (addrc) |
| 310 | *addrc = 2; |
| 311 | if (sizec) |
| 312 | *sizec = 1; |
| 313 | } |
| 314 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 315 | static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns, |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 316 | int pna) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 317 | { |
| 318 | u64 cp, s, da; |
| 319 | |
| 320 | /* Check address type match */ |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 321 | if ((addr[0] ^ range[0]) & cpu_to_be32(1)) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 322 | return OF_BAD_ADDR; |
| 323 | |
| 324 | /* Read address values, skipping high cell */ |
| 325 | cp = of_read_number(range + 1, na - 1); |
| 326 | s = of_read_number(range + na + pna, ns); |
| 327 | da = of_read_number(addr + 1, na - 1); |
| 328 | |
| 329 | pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", |
| 330 | (unsigned long long)cp, (unsigned long long)s, |
| 331 | (unsigned long long)da); |
| 332 | |
| 333 | if (da < cp || da >= (cp + s)) |
| 334 | return OF_BAD_ADDR; |
| 335 | return da - cp; |
| 336 | } |
| 337 | |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 338 | static int of_bus_isa_translate(__be32 *addr, u64 offset, int na) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 339 | { |
| 340 | return of_bus_default_translate(addr + 1, offset, na - 1); |
| 341 | } |
| 342 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 343 | static unsigned int of_bus_isa_get_flags(const __be32 *addr) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 344 | { |
| 345 | unsigned int flags = 0; |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 346 | u32 w = be32_to_cpup(addr); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 347 | |
| 348 | if (w & 1) |
| 349 | flags |= IORESOURCE_IO; |
| 350 | else |
| 351 | flags |= IORESOURCE_MEM; |
| 352 | return flags; |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Array of bus specific translators |
| 357 | */ |
| 358 | |
| 359 | static struct of_bus of_busses[] = { |
Gregory CLEMENT | 25a3157 | 2014-02-19 23:14:55 +0100 | [diff] [blame^] | 360 | #ifdef CONFIG_OF_ADDRESS_PCI |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 361 | /* PCI */ |
| 362 | { |
| 363 | .name = "pci", |
| 364 | .addresses = "assigned-addresses", |
| 365 | .match = of_bus_pci_match, |
| 366 | .count_cells = of_bus_pci_count_cells, |
| 367 | .map = of_bus_pci_map, |
| 368 | .translate = of_bus_pci_translate, |
| 369 | .get_flags = of_bus_pci_get_flags, |
| 370 | }, |
Gregory CLEMENT | 25a3157 | 2014-02-19 23:14:55 +0100 | [diff] [blame^] | 371 | #endif /* CONFIG_OF_ADDRESS_PCI */ |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 372 | /* ISA */ |
| 373 | { |
| 374 | .name = "isa", |
| 375 | .addresses = "reg", |
| 376 | .match = of_bus_isa_match, |
| 377 | .count_cells = of_bus_isa_count_cells, |
| 378 | .map = of_bus_isa_map, |
| 379 | .translate = of_bus_isa_translate, |
| 380 | .get_flags = of_bus_isa_get_flags, |
| 381 | }, |
| 382 | /* Default */ |
| 383 | { |
| 384 | .name = "default", |
| 385 | .addresses = "reg", |
| 386 | .match = NULL, |
| 387 | .count_cells = of_bus_default_count_cells, |
| 388 | .map = of_bus_default_map, |
| 389 | .translate = of_bus_default_translate, |
| 390 | .get_flags = of_bus_default_get_flags, |
| 391 | }, |
| 392 | }; |
| 393 | |
| 394 | static struct of_bus *of_match_bus(struct device_node *np) |
| 395 | { |
| 396 | int i; |
| 397 | |
| 398 | for (i = 0; i < ARRAY_SIZE(of_busses); i++) |
| 399 | if (!of_busses[i].match || of_busses[i].match(np)) |
| 400 | return &of_busses[i]; |
| 401 | BUG(); |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | static int of_translate_one(struct device_node *parent, struct of_bus *bus, |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 406 | struct of_bus *pbus, __be32 *addr, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 407 | int na, int ns, int pna, const char *rprop) |
| 408 | { |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 409 | const __be32 *ranges; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 410 | unsigned int rlen; |
| 411 | int rone; |
| 412 | u64 offset = OF_BAD_ADDR; |
| 413 | |
| 414 | /* Normally, an absence of a "ranges" property means we are |
| 415 | * crossing a non-translatable boundary, and thus the addresses |
| 416 | * below the current not cannot be converted to CPU physical ones. |
| 417 | * Unfortunately, while this is very clear in the spec, it's not |
| 418 | * what Apple understood, and they do have things like /uni-n or |
| 419 | * /ht nodes with no "ranges" property and a lot of perfectly |
| 420 | * useable mapped devices below them. Thus we treat the absence of |
| 421 | * "ranges" as equivalent to an empty "ranges" property which means |
| 422 | * a 1:1 translation at that level. It's up to the caller not to try |
| 423 | * to translate addresses that aren't supposed to be translated in |
| 424 | * the first place. --BenH. |
Grant Likely | 3930f29 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 425 | * |
| 426 | * As far as we know, this damage only exists on Apple machines, so |
| 427 | * This code is only enabled on powerpc. --gcl |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 428 | */ |
| 429 | ranges = of_get_property(parent, rprop, &rlen); |
Grant Likely | 3930f29 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 430 | #if !defined(CONFIG_PPC) |
| 431 | if (ranges == NULL) { |
| 432 | pr_err("OF: no ranges; cannot translate\n"); |
| 433 | return 1; |
| 434 | } |
| 435 | #endif /* !defined(CONFIG_PPC) */ |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 436 | if (ranges == NULL || rlen == 0) { |
| 437 | offset = of_read_number(addr, na); |
| 438 | memset(addr, 0, pna * 4); |
Grant Likely | 3930f29 | 2010-06-08 07:48:11 -0600 | [diff] [blame] | 439 | pr_debug("OF: empty ranges; 1:1 translation\n"); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 440 | goto finish; |
| 441 | } |
| 442 | |
| 443 | pr_debug("OF: walking ranges...\n"); |
| 444 | |
| 445 | /* Now walk through the ranges */ |
| 446 | rlen /= 4; |
| 447 | rone = na + pna + ns; |
| 448 | for (; rlen >= rone; rlen -= rone, ranges += rone) { |
| 449 | offset = bus->map(addr, ranges, na, ns, pna); |
| 450 | if (offset != OF_BAD_ADDR) |
| 451 | break; |
| 452 | } |
| 453 | if (offset == OF_BAD_ADDR) { |
| 454 | pr_debug("OF: not found !\n"); |
| 455 | return 1; |
| 456 | } |
| 457 | memcpy(addr, ranges + na, 4 * pna); |
| 458 | |
| 459 | finish: |
| 460 | of_dump_addr("OF: parent translation for:", addr, pna); |
| 461 | pr_debug("OF: with offset: %llx\n", (unsigned long long)offset); |
| 462 | |
| 463 | /* Translate it into parent bus space */ |
| 464 | return pbus->translate(addr, offset, pna); |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Translate an address from the device-tree into a CPU physical address, |
| 469 | * this walks up the tree and applies the various bus mappings on the |
| 470 | * way. |
| 471 | * |
| 472 | * Note: We consider that crossing any level with #size-cells == 0 to mean |
| 473 | * that translation is impossible (that is we are not dealing with a value |
| 474 | * that can be mapped to a cpu physical address). This is not really specified |
| 475 | * that way, but this is traditionally the way IBM at least do things |
| 476 | */ |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 477 | static u64 __of_translate_address(struct device_node *dev, |
| 478 | const __be32 *in_addr, const char *rprop) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 479 | { |
| 480 | struct device_node *parent = NULL; |
| 481 | struct of_bus *bus, *pbus; |
Kim Phillips | 47b1e68 | 2012-10-08 19:41:58 -0500 | [diff] [blame] | 482 | __be32 addr[OF_MAX_ADDR_CELLS]; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 483 | int na, ns, pna, pns; |
| 484 | u64 result = OF_BAD_ADDR; |
| 485 | |
Grant Likely | 8804827 | 2013-09-19 11:01:52 -0500 | [diff] [blame] | 486 | pr_debug("OF: ** translation for device %s **\n", of_node_full_name(dev)); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 487 | |
| 488 | /* Increase refcount at current level */ |
| 489 | of_node_get(dev); |
| 490 | |
| 491 | /* Get parent & match bus type */ |
| 492 | parent = of_get_parent(dev); |
| 493 | if (parent == NULL) |
| 494 | goto bail; |
| 495 | bus = of_match_bus(parent); |
| 496 | |
Andrew Murray | 59f5ca4 | 2012-12-13 10:11:23 +0000 | [diff] [blame] | 497 | /* Count address cells & copy address locally */ |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 498 | bus->count_cells(dev, &na, &ns); |
| 499 | if (!OF_CHECK_COUNTS(na, ns)) { |
| 500 | printk(KERN_ERR "prom_parse: Bad cell count for %s\n", |
Grant Likely | 8804827 | 2013-09-19 11:01:52 -0500 | [diff] [blame] | 501 | of_node_full_name(dev)); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 502 | goto bail; |
| 503 | } |
| 504 | memcpy(addr, in_addr, na * 4); |
| 505 | |
| 506 | pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n", |
Grant Likely | 8804827 | 2013-09-19 11:01:52 -0500 | [diff] [blame] | 507 | bus->name, na, ns, of_node_full_name(parent)); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 508 | of_dump_addr("OF: translating address:", addr, na); |
| 509 | |
| 510 | /* Translate */ |
| 511 | for (;;) { |
| 512 | /* Switch to parent bus */ |
| 513 | of_node_put(dev); |
| 514 | dev = parent; |
| 515 | parent = of_get_parent(dev); |
| 516 | |
| 517 | /* If root, we have finished */ |
| 518 | if (parent == NULL) { |
| 519 | pr_debug("OF: reached root node\n"); |
| 520 | result = of_read_number(addr, na); |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | /* Get new parent bus and counts */ |
| 525 | pbus = of_match_bus(parent); |
| 526 | pbus->count_cells(dev, &pna, &pns); |
| 527 | if (!OF_CHECK_COUNTS(pna, pns)) { |
| 528 | printk(KERN_ERR "prom_parse: Bad cell count for %s\n", |
Grant Likely | 0c02c80 | 2013-09-19 11:22:36 -0500 | [diff] [blame] | 529 | of_node_full_name(dev)); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 530 | break; |
| 531 | } |
| 532 | |
| 533 | pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n", |
Grant Likely | 0c02c80 | 2013-09-19 11:22:36 -0500 | [diff] [blame] | 534 | pbus->name, pna, pns, of_node_full_name(parent)); |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 535 | |
| 536 | /* Apply bus translation */ |
| 537 | if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop)) |
| 538 | break; |
| 539 | |
| 540 | /* Complete the move up one level */ |
| 541 | na = pna; |
| 542 | ns = pns; |
| 543 | bus = pbus; |
| 544 | |
| 545 | of_dump_addr("OF: one level translation:", addr, na); |
| 546 | } |
| 547 | bail: |
| 548 | of_node_put(parent); |
| 549 | of_node_put(dev); |
| 550 | |
| 551 | return result; |
| 552 | } |
| 553 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 554 | u64 of_translate_address(struct device_node *dev, const __be32 *in_addr) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 555 | { |
| 556 | return __of_translate_address(dev, in_addr, "ranges"); |
| 557 | } |
| 558 | EXPORT_SYMBOL(of_translate_address); |
| 559 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 560 | u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 561 | { |
| 562 | return __of_translate_address(dev, in_addr, "dma-ranges"); |
| 563 | } |
| 564 | EXPORT_SYMBOL(of_translate_dma_address); |
| 565 | |
Stephen Warren | 5d61b16 | 2012-07-25 17:34:37 -0600 | [diff] [blame] | 566 | bool of_can_translate_address(struct device_node *dev) |
| 567 | { |
| 568 | struct device_node *parent; |
| 569 | struct of_bus *bus; |
| 570 | int na, ns; |
| 571 | |
| 572 | parent = of_get_parent(dev); |
| 573 | if (parent == NULL) |
| 574 | return false; |
| 575 | |
| 576 | bus = of_match_bus(parent); |
| 577 | bus->count_cells(dev, &na, &ns); |
| 578 | |
| 579 | of_node_put(parent); |
| 580 | |
| 581 | return OF_CHECK_COUNTS(na, ns); |
| 582 | } |
| 583 | EXPORT_SYMBOL(of_can_translate_address); |
| 584 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 585 | const __be32 *of_get_address(struct device_node *dev, int index, u64 *size, |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 586 | unsigned int *flags) |
| 587 | { |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 588 | const __be32 *prop; |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 589 | unsigned int psize; |
| 590 | struct device_node *parent; |
| 591 | struct of_bus *bus; |
| 592 | int onesize, i, na, ns; |
| 593 | |
| 594 | /* Get parent & match bus type */ |
| 595 | parent = of_get_parent(dev); |
| 596 | if (parent == NULL) |
| 597 | return NULL; |
| 598 | bus = of_match_bus(parent); |
| 599 | bus->count_cells(dev, &na, &ns); |
| 600 | of_node_put(parent); |
Stephen Warren | 5d61b16 | 2012-07-25 17:34:37 -0600 | [diff] [blame] | 601 | if (!OF_CHECK_ADDR_COUNT(na)) |
Grant Likely | dbbdee9 | 2010-06-08 07:48:10 -0600 | [diff] [blame] | 602 | return NULL; |
| 603 | |
| 604 | /* Get "reg" or "assigned-addresses" property */ |
| 605 | prop = of_get_property(dev, bus->addresses, &psize); |
| 606 | if (prop == NULL) |
| 607 | return NULL; |
| 608 | psize /= 4; |
| 609 | |
| 610 | onesize = na + ns; |
| 611 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) |
| 612 | if (i == index) { |
| 613 | if (size) |
| 614 | *size = of_read_number(prop + na, ns); |
| 615 | if (flags) |
| 616 | *flags = bus->get_flags(prop); |
| 617 | return prop; |
| 618 | } |
| 619 | return NULL; |
| 620 | } |
| 621 | EXPORT_SYMBOL(of_get_address); |
| 622 | |
Rob Herring | 25ff794 | 2013-09-07 14:07:11 -0500 | [diff] [blame] | 623 | unsigned long __weak pci_address_to_pio(phys_addr_t address) |
| 624 | { |
| 625 | if (address > IO_SPACE_LIMIT) |
| 626 | return (unsigned long)-1; |
| 627 | |
| 628 | return (unsigned long) address; |
| 629 | } |
| 630 | |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 631 | static int __of_address_to_resource(struct device_node *dev, |
| 632 | const __be32 *addrp, u64 size, unsigned int flags, |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 633 | const char *name, struct resource *r) |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 634 | { |
| 635 | u64 taddr; |
| 636 | |
| 637 | if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0) |
| 638 | return -EINVAL; |
| 639 | taddr = of_translate_address(dev, addrp); |
| 640 | if (taddr == OF_BAD_ADDR) |
| 641 | return -EINVAL; |
| 642 | memset(r, 0, sizeof(struct resource)); |
| 643 | if (flags & IORESOURCE_IO) { |
| 644 | unsigned long port; |
| 645 | port = pci_address_to_pio(taddr); |
| 646 | if (port == (unsigned long)-1) |
| 647 | return -EINVAL; |
| 648 | r->start = port; |
| 649 | r->end = port + size - 1; |
| 650 | } else { |
| 651 | r->start = taddr; |
| 652 | r->end = taddr + size - 1; |
| 653 | } |
| 654 | r->flags = flags; |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 655 | r->name = name ? name : dev->full_name; |
| 656 | |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * of_address_to_resource - Translate device tree address and return as resource |
| 662 | * |
| 663 | * Note that if your address is a PIO address, the conversion will fail if |
| 664 | * the physical address can't be internally converted to an IO token with |
| 665 | * pci_address_to_pio(), that is because it's either called to early or it |
| 666 | * can't be matched to any host bridge IO space |
| 667 | */ |
| 668 | int of_address_to_resource(struct device_node *dev, int index, |
| 669 | struct resource *r) |
| 670 | { |
Sebastian Andrzej Siewior | 0131d89 | 2010-12-01 10:54:46 +0100 | [diff] [blame] | 671 | const __be32 *addrp; |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 672 | u64 size; |
| 673 | unsigned int flags; |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 674 | const char *name = NULL; |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 675 | |
| 676 | addrp = of_get_address(dev, index, &size, &flags); |
| 677 | if (addrp == NULL) |
| 678 | return -EINVAL; |
Benoit Cousson | 35f3da3 | 2011-12-05 15:23:55 +0100 | [diff] [blame] | 679 | |
| 680 | /* Get optional "reg-names" property to add a name to a resource */ |
| 681 | of_property_read_string_index(dev, "reg-names", index, &name); |
| 682 | |
| 683 | return __of_address_to_resource(dev, addrp, size, flags, name, r); |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 684 | } |
| 685 | EXPORT_SYMBOL_GPL(of_address_to_resource); |
| 686 | |
Grant Likely | 90e33f6 | 2011-07-05 23:42:28 -0600 | [diff] [blame] | 687 | struct device_node *of_find_matching_node_by_address(struct device_node *from, |
| 688 | const struct of_device_id *matches, |
| 689 | u64 base_address) |
| 690 | { |
| 691 | struct device_node *dn = of_find_matching_node(from, matches); |
| 692 | struct resource res; |
| 693 | |
| 694 | while (dn) { |
| 695 | if (of_address_to_resource(dn, 0, &res)) |
| 696 | continue; |
| 697 | if (res.start == base_address) |
| 698 | return dn; |
| 699 | dn = of_find_matching_node(dn, matches); |
| 700 | } |
| 701 | |
| 702 | return NULL; |
| 703 | } |
| 704 | |
Grant Likely | 1f5bef3 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 705 | |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 706 | /** |
| 707 | * of_iomap - Maps the memory mapped IO for a given device_node |
| 708 | * @device: the device whose io range will be mapped |
| 709 | * @index: index of the io range |
| 710 | * |
| 711 | * Returns a pointer to the mapped memory |
| 712 | */ |
| 713 | void __iomem *of_iomap(struct device_node *np, int index) |
| 714 | { |
| 715 | struct resource res; |
| 716 | |
| 717 | if (of_address_to_resource(np, index, &res)) |
| 718 | return NULL; |
| 719 | |
Felipe Balbi | 28c1b6d | 2011-07-22 22:47:21 +0300 | [diff] [blame] | 720 | return ioremap(res.start, resource_size(&res)); |
Grant Likely | 6b884a8 | 2010-06-08 07:48:09 -0600 | [diff] [blame] | 721 | } |
| 722 | EXPORT_SYMBOL(of_iomap); |