address.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. #include <linux/io.h>
  2. #include <linux/ioport.h>
  3. #include <linux/module.h>
  4. #include <linux/of_address.h>
  5. #include <linux/pci_regs.h>
  6. #include <linux/string.h>
  7. /* Max address size we deal with */
  8. #define OF_MAX_ADDR_CELLS 4
  9. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  10. (ns) > 0)
  11. static struct of_bus *of_match_bus(struct device_node *np);
  12. static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
  13. u64 size, unsigned int flags,
  14. struct resource *r);
  15. /* Debug utility */
  16. #ifdef DEBUG
  17. static void of_dump_addr(const char *s, const u32 *addr, int na)
  18. {
  19. printk(KERN_DEBUG "%s", s);
  20. while (na--)
  21. printk(" %08x", be32_to_cpu(*(addr++)));
  22. printk("\n");
  23. }
  24. #else
  25. static void of_dump_addr(const char *s, const u32 *addr, int na) { }
  26. #endif
  27. /* Callbacks for bus specific translators */
  28. struct of_bus {
  29. const char *name;
  30. const char *addresses;
  31. int (*match)(struct device_node *parent);
  32. void (*count_cells)(struct device_node *child,
  33. int *addrc, int *sizec);
  34. u64 (*map)(u32 *addr, const u32 *range,
  35. int na, int ns, int pna);
  36. int (*translate)(u32 *addr, u64 offset, int na);
  37. unsigned int (*get_flags)(const u32 *addr);
  38. };
  39. /*
  40. * Default translator (generic bus)
  41. */
  42. static void of_bus_default_count_cells(struct device_node *dev,
  43. int *addrc, int *sizec)
  44. {
  45. if (addrc)
  46. *addrc = of_n_addr_cells(dev);
  47. if (sizec)
  48. *sizec = of_n_size_cells(dev);
  49. }
  50. static u64 of_bus_default_map(u32 *addr, const u32 *range,
  51. int na, int ns, int pna)
  52. {
  53. u64 cp, s, da;
  54. cp = of_read_number(range, na);
  55. s = of_read_number(range + na + pna, ns);
  56. da = of_read_number(addr, na);
  57. pr_debug("OF: default map, cp=%llx, s=%llx, da=%llx\n",
  58. (unsigned long long)cp, (unsigned long long)s,
  59. (unsigned long long)da);
  60. if (da < cp || da >= (cp + s))
  61. return OF_BAD_ADDR;
  62. return da - cp;
  63. }
  64. static int of_bus_default_translate(u32 *addr, u64 offset, int na)
  65. {
  66. u64 a = of_read_number(addr, na);
  67. memset(addr, 0, na * 4);
  68. a += offset;
  69. if (na > 1)
  70. addr[na - 2] = cpu_to_be32(a >> 32);
  71. addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
  72. return 0;
  73. }
  74. static unsigned int of_bus_default_get_flags(const u32 *addr)
  75. {
  76. return IORESOURCE_MEM;
  77. }
  78. #ifdef CONFIG_PCI
  79. /*
  80. * PCI bus specific translator
  81. */
  82. static int of_bus_pci_match(struct device_node *np)
  83. {
  84. /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
  85. return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
  86. }
  87. static void of_bus_pci_count_cells(struct device_node *np,
  88. int *addrc, int *sizec)
  89. {
  90. if (addrc)
  91. *addrc = 3;
  92. if (sizec)
  93. *sizec = 2;
  94. }
  95. static unsigned int of_bus_pci_get_flags(const u32 *addr)
  96. {
  97. unsigned int flags = 0;
  98. u32 w = addr[0];
  99. switch((w >> 24) & 0x03) {
  100. case 0x01:
  101. flags |= IORESOURCE_IO;
  102. break;
  103. case 0x02: /* 32 bits */
  104. case 0x03: /* 64 bits */
  105. flags |= IORESOURCE_MEM;
  106. break;
  107. }
  108. if (w & 0x40000000)
  109. flags |= IORESOURCE_PREFETCH;
  110. return flags;
  111. }
  112. static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  113. {
  114. u64 cp, s, da;
  115. unsigned int af, rf;
  116. af = of_bus_pci_get_flags(addr);
  117. rf = of_bus_pci_get_flags(range);
  118. /* Check address type match */
  119. if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
  120. return OF_BAD_ADDR;
  121. /* Read address values, skipping high cell */
  122. cp = of_read_number(range + 1, na - 1);
  123. s = of_read_number(range + na + pna, ns);
  124. da = of_read_number(addr + 1, na - 1);
  125. pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n",
  126. (unsigned long long)cp, (unsigned long long)s,
  127. (unsigned long long)da);
  128. if (da < cp || da >= (cp + s))
  129. return OF_BAD_ADDR;
  130. return da - cp;
  131. }
  132. static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
  133. {
  134. return of_bus_default_translate(addr + 1, offset, na - 1);
  135. }
  136. const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
  137. unsigned int *flags)
  138. {
  139. const u32 *prop;
  140. unsigned int psize;
  141. struct device_node *parent;
  142. struct of_bus *bus;
  143. int onesize, i, na, ns;
  144. /* Get parent & match bus type */
  145. parent = of_get_parent(dev);
  146. if (parent == NULL)
  147. return NULL;
  148. bus = of_match_bus(parent);
  149. if (strcmp(bus->name, "pci")) {
  150. of_node_put(parent);
  151. return NULL;
  152. }
  153. bus->count_cells(dev, &na, &ns);
  154. of_node_put(parent);
  155. if (!OF_CHECK_COUNTS(na, ns))
  156. return NULL;
  157. /* Get "reg" or "assigned-addresses" property */
  158. prop = of_get_property(dev, bus->addresses, &psize);
  159. if (prop == NULL)
  160. return NULL;
  161. psize /= 4;
  162. onesize = na + ns;
  163. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
  164. u32 val = be32_to_cpu(prop[0]);
  165. if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
  166. if (size)
  167. *size = of_read_number(prop + na, ns);
  168. if (flags)
  169. *flags = bus->get_flags(prop);
  170. return prop;
  171. }
  172. }
  173. return NULL;
  174. }
  175. EXPORT_SYMBOL(of_get_pci_address);
  176. int of_pci_address_to_resource(struct device_node *dev, int bar,
  177. struct resource *r)
  178. {
  179. const u32 *addrp;
  180. u64 size;
  181. unsigned int flags;
  182. addrp = of_get_pci_address(dev, bar, &size, &flags);
  183. if (addrp == NULL)
  184. return -EINVAL;
  185. return __of_address_to_resource(dev, addrp, size, flags, r);
  186. }
  187. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  188. #endif /* CONFIG_PCI */
  189. /*
  190. * ISA bus specific translator
  191. */
  192. static int of_bus_isa_match(struct device_node *np)
  193. {
  194. return !strcmp(np->name, "isa");
  195. }
  196. static void of_bus_isa_count_cells(struct device_node *child,
  197. int *addrc, int *sizec)
  198. {
  199. if (addrc)
  200. *addrc = 2;
  201. if (sizec)
  202. *sizec = 1;
  203. }
  204. static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  205. {
  206. u64 cp, s, da;
  207. /* Check address type match */
  208. if ((addr[0] ^ range[0]) & 0x00000001)
  209. return OF_BAD_ADDR;
  210. /* Read address values, skipping high cell */
  211. cp = of_read_number(range + 1, na - 1);
  212. s = of_read_number(range + na + pna, ns);
  213. da = of_read_number(addr + 1, na - 1);
  214. pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
  215. (unsigned long long)cp, (unsigned long long)s,
  216. (unsigned long long)da);
  217. if (da < cp || da >= (cp + s))
  218. return OF_BAD_ADDR;
  219. return da - cp;
  220. }
  221. static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
  222. {
  223. return of_bus_default_translate(addr + 1, offset, na - 1);
  224. }
  225. static unsigned int of_bus_isa_get_flags(const u32 *addr)
  226. {
  227. unsigned int flags = 0;
  228. u32 w = addr[0];
  229. if (w & 1)
  230. flags |= IORESOURCE_IO;
  231. else
  232. flags |= IORESOURCE_MEM;
  233. return flags;
  234. }
  235. /*
  236. * Array of bus specific translators
  237. */
  238. static struct of_bus of_busses[] = {
  239. #ifdef CONFIG_PCI
  240. /* PCI */
  241. {
  242. .name = "pci",
  243. .addresses = "assigned-addresses",
  244. .match = of_bus_pci_match,
  245. .count_cells = of_bus_pci_count_cells,
  246. .map = of_bus_pci_map,
  247. .translate = of_bus_pci_translate,
  248. .get_flags = of_bus_pci_get_flags,
  249. },
  250. #endif /* CONFIG_PCI */
  251. /* ISA */
  252. {
  253. .name = "isa",
  254. .addresses = "reg",
  255. .match = of_bus_isa_match,
  256. .count_cells = of_bus_isa_count_cells,
  257. .map = of_bus_isa_map,
  258. .translate = of_bus_isa_translate,
  259. .get_flags = of_bus_isa_get_flags,
  260. },
  261. /* Default */
  262. {
  263. .name = "default",
  264. .addresses = "reg",
  265. .match = NULL,
  266. .count_cells = of_bus_default_count_cells,
  267. .map = of_bus_default_map,
  268. .translate = of_bus_default_translate,
  269. .get_flags = of_bus_default_get_flags,
  270. },
  271. };
  272. static struct of_bus *of_match_bus(struct device_node *np)
  273. {
  274. int i;
  275. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  276. if (!of_busses[i].match || of_busses[i].match(np))
  277. return &of_busses[i];
  278. BUG();
  279. return NULL;
  280. }
  281. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  282. struct of_bus *pbus, u32 *addr,
  283. int na, int ns, int pna, const char *rprop)
  284. {
  285. const u32 *ranges;
  286. unsigned int rlen;
  287. int rone;
  288. u64 offset = OF_BAD_ADDR;
  289. /* Normally, an absence of a "ranges" property means we are
  290. * crossing a non-translatable boundary, and thus the addresses
  291. * below the current not cannot be converted to CPU physical ones.
  292. * Unfortunately, while this is very clear in the spec, it's not
  293. * what Apple understood, and they do have things like /uni-n or
  294. * /ht nodes with no "ranges" property and a lot of perfectly
  295. * useable mapped devices below them. Thus we treat the absence of
  296. * "ranges" as equivalent to an empty "ranges" property which means
  297. * a 1:1 translation at that level. It's up to the caller not to try
  298. * to translate addresses that aren't supposed to be translated in
  299. * the first place. --BenH.
  300. */
  301. ranges = of_get_property(parent, rprop, &rlen);
  302. if (ranges == NULL || rlen == 0) {
  303. offset = of_read_number(addr, na);
  304. memset(addr, 0, pna * 4);
  305. pr_debug("OF: no ranges, 1:1 translation\n");
  306. goto finish;
  307. }
  308. pr_debug("OF: walking ranges...\n");
  309. /* Now walk through the ranges */
  310. rlen /= 4;
  311. rone = na + pna + ns;
  312. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  313. offset = bus->map(addr, ranges, na, ns, pna);
  314. if (offset != OF_BAD_ADDR)
  315. break;
  316. }
  317. if (offset == OF_BAD_ADDR) {
  318. pr_debug("OF: not found !\n");
  319. return 1;
  320. }
  321. memcpy(addr, ranges + na, 4 * pna);
  322. finish:
  323. of_dump_addr("OF: parent translation for:", addr, pna);
  324. pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
  325. /* Translate it into parent bus space */
  326. return pbus->translate(addr, offset, pna);
  327. }
  328. /*
  329. * Translate an address from the device-tree into a CPU physical address,
  330. * this walks up the tree and applies the various bus mappings on the
  331. * way.
  332. *
  333. * Note: We consider that crossing any level with #size-cells == 0 to mean
  334. * that translation is impossible (that is we are not dealing with a value
  335. * that can be mapped to a cpu physical address). This is not really specified
  336. * that way, but this is traditionally the way IBM at least do things
  337. */
  338. u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
  339. const char *rprop)
  340. {
  341. struct device_node *parent = NULL;
  342. struct of_bus *bus, *pbus;
  343. u32 addr[OF_MAX_ADDR_CELLS];
  344. int na, ns, pna, pns;
  345. u64 result = OF_BAD_ADDR;
  346. pr_debug("OF: ** translation for device %s **\n", dev->full_name);
  347. /* Increase refcount at current level */
  348. of_node_get(dev);
  349. /* Get parent & match bus type */
  350. parent = of_get_parent(dev);
  351. if (parent == NULL)
  352. goto bail;
  353. bus = of_match_bus(parent);
  354. /* Cound address cells & copy address locally */
  355. bus->count_cells(dev, &na, &ns);
  356. if (!OF_CHECK_COUNTS(na, ns)) {
  357. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  358. dev->full_name);
  359. goto bail;
  360. }
  361. memcpy(addr, in_addr, na * 4);
  362. pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  363. bus->name, na, ns, parent->full_name);
  364. of_dump_addr("OF: translating address:", addr, na);
  365. /* Translate */
  366. for (;;) {
  367. /* Switch to parent bus */
  368. of_node_put(dev);
  369. dev = parent;
  370. parent = of_get_parent(dev);
  371. /* If root, we have finished */
  372. if (parent == NULL) {
  373. pr_debug("OF: reached root node\n");
  374. result = of_read_number(addr, na);
  375. break;
  376. }
  377. /* Get new parent bus and counts */
  378. pbus = of_match_bus(parent);
  379. pbus->count_cells(dev, &pna, &pns);
  380. if (!OF_CHECK_COUNTS(pna, pns)) {
  381. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  382. dev->full_name);
  383. break;
  384. }
  385. pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  386. pbus->name, pna, pns, parent->full_name);
  387. /* Apply bus translation */
  388. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  389. break;
  390. /* Complete the move up one level */
  391. na = pna;
  392. ns = pns;
  393. bus = pbus;
  394. of_dump_addr("OF: one level translation:", addr, na);
  395. }
  396. bail:
  397. of_node_put(parent);
  398. of_node_put(dev);
  399. return result;
  400. }
  401. u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
  402. {
  403. return __of_translate_address(dev, in_addr, "ranges");
  404. }
  405. EXPORT_SYMBOL(of_translate_address);
  406. u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
  407. {
  408. return __of_translate_address(dev, in_addr, "dma-ranges");
  409. }
  410. EXPORT_SYMBOL(of_translate_dma_address);
  411. const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
  412. unsigned int *flags)
  413. {
  414. const u32 *prop;
  415. unsigned int psize;
  416. struct device_node *parent;
  417. struct of_bus *bus;
  418. int onesize, i, na, ns;
  419. /* Get parent & match bus type */
  420. parent = of_get_parent(dev);
  421. if (parent == NULL)
  422. return NULL;
  423. bus = of_match_bus(parent);
  424. bus->count_cells(dev, &na, &ns);
  425. of_node_put(parent);
  426. if (!OF_CHECK_COUNTS(na, ns))
  427. return NULL;
  428. /* Get "reg" or "assigned-addresses" property */
  429. prop = of_get_property(dev, bus->addresses, &psize);
  430. if (prop == NULL)
  431. return NULL;
  432. psize /= 4;
  433. onesize = na + ns;
  434. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  435. if (i == index) {
  436. if (size)
  437. *size = of_read_number(prop + na, ns);
  438. if (flags)
  439. *flags = bus->get_flags(prop);
  440. return prop;
  441. }
  442. return NULL;
  443. }
  444. EXPORT_SYMBOL(of_get_address);
  445. static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
  446. u64 size, unsigned int flags,
  447. struct resource *r)
  448. {
  449. u64 taddr;
  450. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  451. return -EINVAL;
  452. taddr = of_translate_address(dev, addrp);
  453. if (taddr == OF_BAD_ADDR)
  454. return -EINVAL;
  455. memset(r, 0, sizeof(struct resource));
  456. if (flags & IORESOURCE_IO) {
  457. unsigned long port;
  458. port = pci_address_to_pio(taddr);
  459. if (port == (unsigned long)-1)
  460. return -EINVAL;
  461. r->start = port;
  462. r->end = port + size - 1;
  463. } else {
  464. r->start = taddr;
  465. r->end = taddr + size - 1;
  466. }
  467. r->flags = flags;
  468. r->name = dev->name;
  469. return 0;
  470. }
  471. /**
  472. * of_address_to_resource - Translate device tree address and return as resource
  473. *
  474. * Note that if your address is a PIO address, the conversion will fail if
  475. * the physical address can't be internally converted to an IO token with
  476. * pci_address_to_pio(), that is because it's either called to early or it
  477. * can't be matched to any host bridge IO space
  478. */
  479. int of_address_to_resource(struct device_node *dev, int index,
  480. struct resource *r)
  481. {
  482. const u32 *addrp;
  483. u64 size;
  484. unsigned int flags;
  485. addrp = of_get_address(dev, index, &size, &flags);
  486. if (addrp == NULL)
  487. return -EINVAL;
  488. return __of_address_to_resource(dev, addrp, size, flags, r);
  489. }
  490. EXPORT_SYMBOL_GPL(of_address_to_resource);
  491. /**
  492. * of_iomap - Maps the memory mapped IO for a given device_node
  493. * @device: the device whose io range will be mapped
  494. * @index: index of the io range
  495. *
  496. * Returns a pointer to the mapped memory
  497. */
  498. void __iomem *of_iomap(struct device_node *np, int index)
  499. {
  500. struct resource res;
  501. if (of_address_to_resource(np, index, &res))
  502. return NULL;
  503. return ioremap(res.start, 1 + res.end - res.start);
  504. }
  505. EXPORT_SYMBOL(of_iomap);