address.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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", *(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] = a >> 32;
  71. addr[na - 1] = 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. if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
  165. if (size)
  166. *size = of_read_number(prop + na, ns);
  167. if (flags)
  168. *flags = bus->get_flags(prop);
  169. return prop;
  170. }
  171. return NULL;
  172. }
  173. EXPORT_SYMBOL(of_get_pci_address);
  174. int of_pci_address_to_resource(struct device_node *dev, int bar,
  175. struct resource *r)
  176. {
  177. const u32 *addrp;
  178. u64 size;
  179. unsigned int flags;
  180. addrp = of_get_pci_address(dev, bar, &size, &flags);
  181. if (addrp == NULL)
  182. return -EINVAL;
  183. return __of_address_to_resource(dev, addrp, size, flags, r);
  184. }
  185. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  186. #endif /* CONFIG_PCI */
  187. /*
  188. * ISA bus specific translator
  189. */
  190. static int of_bus_isa_match(struct device_node *np)
  191. {
  192. return !strcmp(np->name, "isa");
  193. }
  194. static void of_bus_isa_count_cells(struct device_node *child,
  195. int *addrc, int *sizec)
  196. {
  197. if (addrc)
  198. *addrc = 2;
  199. if (sizec)
  200. *sizec = 1;
  201. }
  202. static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  203. {
  204. u64 cp, s, da;
  205. /* Check address type match */
  206. if ((addr[0] ^ range[0]) & 0x00000001)
  207. return OF_BAD_ADDR;
  208. /* Read address values, skipping high cell */
  209. cp = of_read_number(range + 1, na - 1);
  210. s = of_read_number(range + na + pna, ns);
  211. da = of_read_number(addr + 1, na - 1);
  212. pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n",
  213. (unsigned long long)cp, (unsigned long long)s,
  214. (unsigned long long)da);
  215. if (da < cp || da >= (cp + s))
  216. return OF_BAD_ADDR;
  217. return da - cp;
  218. }
  219. static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
  220. {
  221. return of_bus_default_translate(addr + 1, offset, na - 1);
  222. }
  223. static unsigned int of_bus_isa_get_flags(const u32 *addr)
  224. {
  225. unsigned int flags = 0;
  226. u32 w = addr[0];
  227. if (w & 1)
  228. flags |= IORESOURCE_IO;
  229. else
  230. flags |= IORESOURCE_MEM;
  231. return flags;
  232. }
  233. /*
  234. * Array of bus specific translators
  235. */
  236. static struct of_bus of_busses[] = {
  237. #ifdef CONFIG_PCI
  238. /* PCI */
  239. {
  240. .name = "pci",
  241. .addresses = "assigned-addresses",
  242. .match = of_bus_pci_match,
  243. .count_cells = of_bus_pci_count_cells,
  244. .map = of_bus_pci_map,
  245. .translate = of_bus_pci_translate,
  246. .get_flags = of_bus_pci_get_flags,
  247. },
  248. #endif /* CONFIG_PCI */
  249. /* ISA */
  250. {
  251. .name = "isa",
  252. .addresses = "reg",
  253. .match = of_bus_isa_match,
  254. .count_cells = of_bus_isa_count_cells,
  255. .map = of_bus_isa_map,
  256. .translate = of_bus_isa_translate,
  257. .get_flags = of_bus_isa_get_flags,
  258. },
  259. /* Default */
  260. {
  261. .name = "default",
  262. .addresses = "reg",
  263. .match = NULL,
  264. .count_cells = of_bus_default_count_cells,
  265. .map = of_bus_default_map,
  266. .translate = of_bus_default_translate,
  267. .get_flags = of_bus_default_get_flags,
  268. },
  269. };
  270. static struct of_bus *of_match_bus(struct device_node *np)
  271. {
  272. int i;
  273. for (i = 0; i < ARRAY_SIZE(of_busses); i++)
  274. if (!of_busses[i].match || of_busses[i].match(np))
  275. return &of_busses[i];
  276. BUG();
  277. return NULL;
  278. }
  279. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  280. struct of_bus *pbus, u32 *addr,
  281. int na, int ns, int pna, const char *rprop)
  282. {
  283. const u32 *ranges;
  284. unsigned int rlen;
  285. int rone;
  286. u64 offset = OF_BAD_ADDR;
  287. /* Normally, an absence of a "ranges" property means we are
  288. * crossing a non-translatable boundary, and thus the addresses
  289. * below the current not cannot be converted to CPU physical ones.
  290. * Unfortunately, while this is very clear in the spec, it's not
  291. * what Apple understood, and they do have things like /uni-n or
  292. * /ht nodes with no "ranges" property and a lot of perfectly
  293. * useable mapped devices below them. Thus we treat the absence of
  294. * "ranges" as equivalent to an empty "ranges" property which means
  295. * a 1:1 translation at that level. It's up to the caller not to try
  296. * to translate addresses that aren't supposed to be translated in
  297. * the first place. --BenH.
  298. */
  299. ranges = of_get_property(parent, rprop, &rlen);
  300. if (ranges == NULL || rlen == 0) {
  301. offset = of_read_number(addr, na);
  302. memset(addr, 0, pna * 4);
  303. pr_debug("OF: no ranges, 1:1 translation\n");
  304. goto finish;
  305. }
  306. pr_debug("OF: walking ranges...\n");
  307. /* Now walk through the ranges */
  308. rlen /= 4;
  309. rone = na + pna + ns;
  310. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  311. offset = bus->map(addr, ranges, na, ns, pna);
  312. if (offset != OF_BAD_ADDR)
  313. break;
  314. }
  315. if (offset == OF_BAD_ADDR) {
  316. pr_debug("OF: not found !\n");
  317. return 1;
  318. }
  319. memcpy(addr, ranges + na, 4 * pna);
  320. finish:
  321. of_dump_addr("OF: parent translation for:", addr, pna);
  322. pr_debug("OF: with offset: %llx\n", (unsigned long long)offset);
  323. /* Translate it into parent bus space */
  324. return pbus->translate(addr, offset, pna);
  325. }
  326. /*
  327. * Translate an address from the device-tree into a CPU physical address,
  328. * this walks up the tree and applies the various bus mappings on the
  329. * way.
  330. *
  331. * Note: We consider that crossing any level with #size-cells == 0 to mean
  332. * that translation is impossible (that is we are not dealing with a value
  333. * that can be mapped to a cpu physical address). This is not really specified
  334. * that way, but this is traditionally the way IBM at least do things
  335. */
  336. u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
  337. const char *rprop)
  338. {
  339. struct device_node *parent = NULL;
  340. struct of_bus *bus, *pbus;
  341. u32 addr[OF_MAX_ADDR_CELLS];
  342. int na, ns, pna, pns;
  343. u64 result = OF_BAD_ADDR;
  344. pr_debug("OF: ** translation for device %s **\n", dev->full_name);
  345. /* Increase refcount at current level */
  346. of_node_get(dev);
  347. /* Get parent & match bus type */
  348. parent = of_get_parent(dev);
  349. if (parent == NULL)
  350. goto bail;
  351. bus = of_match_bus(parent);
  352. /* Cound address cells & copy address locally */
  353. bus->count_cells(dev, &na, &ns);
  354. if (!OF_CHECK_COUNTS(na, ns)) {
  355. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  356. dev->full_name);
  357. goto bail;
  358. }
  359. memcpy(addr, in_addr, na * 4);
  360. pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  361. bus->name, na, ns, parent->full_name);
  362. of_dump_addr("OF: translating address:", addr, na);
  363. /* Translate */
  364. for (;;) {
  365. /* Switch to parent bus */
  366. of_node_put(dev);
  367. dev = parent;
  368. parent = of_get_parent(dev);
  369. /* If root, we have finished */
  370. if (parent == NULL) {
  371. pr_debug("OF: reached root node\n");
  372. result = of_read_number(addr, na);
  373. break;
  374. }
  375. /* Get new parent bus and counts */
  376. pbus = of_match_bus(parent);
  377. pbus->count_cells(dev, &pna, &pns);
  378. if (!OF_CHECK_COUNTS(pna, pns)) {
  379. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  380. dev->full_name);
  381. break;
  382. }
  383. pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  384. pbus->name, pna, pns, parent->full_name);
  385. /* Apply bus translation */
  386. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  387. break;
  388. /* Complete the move up one level */
  389. na = pna;
  390. ns = pns;
  391. bus = pbus;
  392. of_dump_addr("OF: one level translation:", addr, na);
  393. }
  394. bail:
  395. of_node_put(parent);
  396. of_node_put(dev);
  397. return result;
  398. }
  399. u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
  400. {
  401. return __of_translate_address(dev, in_addr, "ranges");
  402. }
  403. EXPORT_SYMBOL(of_translate_address);
  404. u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
  405. {
  406. return __of_translate_address(dev, in_addr, "dma-ranges");
  407. }
  408. EXPORT_SYMBOL(of_translate_dma_address);
  409. const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
  410. unsigned int *flags)
  411. {
  412. const u32 *prop;
  413. unsigned int psize;
  414. struct device_node *parent;
  415. struct of_bus *bus;
  416. int onesize, i, na, ns;
  417. /* Get parent & match bus type */
  418. parent = of_get_parent(dev);
  419. if (parent == NULL)
  420. return NULL;
  421. bus = of_match_bus(parent);
  422. bus->count_cells(dev, &na, &ns);
  423. of_node_put(parent);
  424. if (!OF_CHECK_COUNTS(na, ns))
  425. return NULL;
  426. /* Get "reg" or "assigned-addresses" property */
  427. prop = of_get_property(dev, bus->addresses, &psize);
  428. if (prop == NULL)
  429. return NULL;
  430. psize /= 4;
  431. onesize = na + ns;
  432. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  433. if (i == index) {
  434. if (size)
  435. *size = of_read_number(prop + na, ns);
  436. if (flags)
  437. *flags = bus->get_flags(prop);
  438. return prop;
  439. }
  440. return NULL;
  441. }
  442. EXPORT_SYMBOL(of_get_address);
  443. static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
  444. u64 size, unsigned int flags,
  445. struct resource *r)
  446. {
  447. u64 taddr;
  448. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  449. return -EINVAL;
  450. taddr = of_translate_address(dev, addrp);
  451. if (taddr == OF_BAD_ADDR)
  452. return -EINVAL;
  453. memset(r, 0, sizeof(struct resource));
  454. if (flags & IORESOURCE_IO) {
  455. unsigned long port;
  456. port = pci_address_to_pio(taddr);
  457. if (port == (unsigned long)-1)
  458. return -EINVAL;
  459. r->start = port;
  460. r->end = port + size - 1;
  461. } else {
  462. r->start = taddr;
  463. r->end = taddr + size - 1;
  464. }
  465. r->flags = flags;
  466. r->name = dev->name;
  467. return 0;
  468. }
  469. /**
  470. * of_address_to_resource - Translate device tree address and return as resource
  471. *
  472. * Note that if your address is a PIO address, the conversion will fail if
  473. * the physical address can't be internally converted to an IO token with
  474. * pci_address_to_pio(), that is because it's either called to early or it
  475. * can't be matched to any host bridge IO space
  476. */
  477. int of_address_to_resource(struct device_node *dev, int index,
  478. struct resource *r)
  479. {
  480. const u32 *addrp;
  481. u64 size;
  482. unsigned int flags;
  483. addrp = of_get_address(dev, index, &size, &flags);
  484. if (addrp == NULL)
  485. return -EINVAL;
  486. return __of_address_to_resource(dev, addrp, size, flags, r);
  487. }
  488. EXPORT_SYMBOL_GPL(of_address_to_resource);
  489. /**
  490. * of_iomap - Maps the memory mapped IO for a given device_node
  491. * @device: the device whose io range will be mapped
  492. * @index: index of the io range
  493. *
  494. * Returns a pointer to the mapped memory
  495. */
  496. void __iomem *of_iomap(struct device_node *np, int index)
  497. {
  498. struct resource res;
  499. if (of_address_to_resource(np, index, &res))
  500. return NULL;
  501. return ioremap(res.start, 1 + res.end - res.start);
  502. }
  503. EXPORT_SYMBOL(of_iomap);