prom_parse.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. #undef DEBUG
  2. #include <linux/kernel.h>
  3. #include <linux/string.h>
  4. #include <linux/pci_regs.h>
  5. #include <linux/module.h>
  6. #include <linux/ioport.h>
  7. #include <linux/etherdevice.h>
  8. #include <asm/prom.h>
  9. #include <asm/pci-bridge.h>
  10. #ifdef DEBUG
  11. #define DBG(fmt...) do { printk(fmt); } while(0)
  12. #else
  13. #define DBG(fmt...) do { } while(0)
  14. #endif
  15. #ifdef CONFIG_PPC64
  16. #define PRu64 "%lx"
  17. #else
  18. #define PRu64 "%llx"
  19. #endif
  20. /* Max address size we deal with */
  21. #define OF_MAX_ADDR_CELLS 4
  22. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  23. (ns) > 0)
  24. static struct of_bus *of_match_bus(struct device_node *np);
  25. static int __of_address_to_resource(struct device_node *dev,
  26. const u32 *addrp, u64 size, unsigned int flags,
  27. struct resource *r);
  28. /* Debug utility */
  29. #ifdef DEBUG
  30. static void of_dump_addr(const char *s, const u32 *addr, int na)
  31. {
  32. printk("%s", s);
  33. while(na--)
  34. printk(" %08x", *(addr++));
  35. printk("\n");
  36. }
  37. #else
  38. static void of_dump_addr(const char *s, const u32 *addr, int na) { }
  39. #endif
  40. /* Callbacks for bus specific translators */
  41. struct of_bus {
  42. const char *name;
  43. const char *addresses;
  44. int (*match)(struct device_node *parent);
  45. void (*count_cells)(struct device_node *child,
  46. int *addrc, int *sizec);
  47. u64 (*map)(u32 *addr, const u32 *range,
  48. int na, int ns, int pna);
  49. int (*translate)(u32 *addr, u64 offset, int na);
  50. unsigned int (*get_flags)(const u32 *addr);
  51. };
  52. /*
  53. * Default translator (generic bus)
  54. */
  55. static void of_bus_default_count_cells(struct device_node *dev,
  56. int *addrc, int *sizec)
  57. {
  58. if (addrc)
  59. *addrc = of_n_addr_cells(dev);
  60. if (sizec)
  61. *sizec = of_n_size_cells(dev);
  62. }
  63. static u64 of_bus_default_map(u32 *addr, const u32 *range,
  64. int na, int ns, int pna)
  65. {
  66. u64 cp, s, da;
  67. cp = of_read_number(range, na);
  68. s = of_read_number(range + na + pna, ns);
  69. da = of_read_number(addr, na);
  70. DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
  71. cp, s, da);
  72. if (da < cp || da >= (cp + s))
  73. return OF_BAD_ADDR;
  74. return da - cp;
  75. }
  76. static int of_bus_default_translate(u32 *addr, u64 offset, int na)
  77. {
  78. u64 a = of_read_number(addr, na);
  79. memset(addr, 0, na * 4);
  80. a += offset;
  81. if (na > 1)
  82. addr[na - 2] = a >> 32;
  83. addr[na - 1] = a & 0xffffffffu;
  84. return 0;
  85. }
  86. static unsigned int of_bus_default_get_flags(const u32 *addr)
  87. {
  88. return IORESOURCE_MEM;
  89. }
  90. #ifdef CONFIG_PCI
  91. /*
  92. * PCI bus specific translator
  93. */
  94. static int of_bus_pci_match(struct device_node *np)
  95. {
  96. /* "vci" is for the /chaos bridge on 1st-gen PCI powermacs */
  97. return !strcmp(np->type, "pci") || !strcmp(np->type, "vci");
  98. }
  99. static void of_bus_pci_count_cells(struct device_node *np,
  100. int *addrc, int *sizec)
  101. {
  102. if (addrc)
  103. *addrc = 3;
  104. if (sizec)
  105. *sizec = 2;
  106. }
  107. static unsigned int of_bus_pci_get_flags(const u32 *addr)
  108. {
  109. unsigned int flags = 0;
  110. u32 w = addr[0];
  111. switch((w >> 24) & 0x03) {
  112. case 0x01:
  113. flags |= IORESOURCE_IO;
  114. break;
  115. case 0x02: /* 32 bits */
  116. case 0x03: /* 64 bits */
  117. flags |= IORESOURCE_MEM;
  118. break;
  119. }
  120. if (w & 0x40000000)
  121. flags |= IORESOURCE_PREFETCH;
  122. return flags;
  123. }
  124. static u64 of_bus_pci_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  125. {
  126. u64 cp, s, da;
  127. unsigned int af, rf;
  128. af = of_bus_pci_get_flags(addr);
  129. rf = of_bus_pci_get_flags(range);
  130. /* Check address type match */
  131. if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
  132. return OF_BAD_ADDR;
  133. /* Read address values, skipping high cell */
  134. cp = of_read_number(range + 1, na - 1);
  135. s = of_read_number(range + na + pna, ns);
  136. da = of_read_number(addr + 1, na - 1);
  137. DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
  138. if (da < cp || da >= (cp + s))
  139. return OF_BAD_ADDR;
  140. return da - cp;
  141. }
  142. static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
  143. {
  144. return of_bus_default_translate(addr + 1, offset, na - 1);
  145. }
  146. const u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size,
  147. unsigned int *flags)
  148. {
  149. const u32 *prop;
  150. unsigned int psize;
  151. struct device_node *parent;
  152. struct of_bus *bus;
  153. int onesize, i, na, ns;
  154. /* Get parent & match bus type */
  155. parent = of_get_parent(dev);
  156. if (parent == NULL)
  157. return NULL;
  158. bus = of_match_bus(parent);
  159. if (strcmp(bus->name, "pci")) {
  160. of_node_put(parent);
  161. return NULL;
  162. }
  163. bus->count_cells(dev, &na, &ns);
  164. of_node_put(parent);
  165. if (!OF_CHECK_COUNTS(na, ns))
  166. return NULL;
  167. /* Get "reg" or "assigned-addresses" property */
  168. prop = of_get_property(dev, bus->addresses, &psize);
  169. if (prop == NULL)
  170. return NULL;
  171. psize /= 4;
  172. onesize = na + ns;
  173. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  174. if ((prop[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) {
  175. if (size)
  176. *size = of_read_number(prop + na, ns);
  177. if (flags)
  178. *flags = bus->get_flags(prop);
  179. return prop;
  180. }
  181. return NULL;
  182. }
  183. EXPORT_SYMBOL(of_get_pci_address);
  184. int of_pci_address_to_resource(struct device_node *dev, int bar,
  185. struct resource *r)
  186. {
  187. const u32 *addrp;
  188. u64 size;
  189. unsigned int flags;
  190. addrp = of_get_pci_address(dev, bar, &size, &flags);
  191. if (addrp == NULL)
  192. return -EINVAL;
  193. return __of_address_to_resource(dev, addrp, size, flags, r);
  194. }
  195. EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
  196. static u8 of_irq_pci_swizzle(u8 slot, u8 pin)
  197. {
  198. return (((pin - 1) + slot) % 4) + 1;
  199. }
  200. int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
  201. {
  202. struct device_node *dn, *ppnode;
  203. struct pci_dev *ppdev;
  204. u32 lspec;
  205. u32 laddr[3];
  206. u8 pin;
  207. int rc;
  208. /* Check if we have a device node, if yes, fallback to standard OF
  209. * parsing
  210. */
  211. dn = pci_device_to_OF_node(pdev);
  212. if (dn) {
  213. rc = of_irq_map_one(dn, 0, out_irq);
  214. if (!rc)
  215. return rc;
  216. }
  217. /* Ok, we don't, time to have fun. Let's start by building up an
  218. * interrupt spec. we assume #interrupt-cells is 1, which is standard
  219. * for PCI. If you do different, then don't use that routine.
  220. */
  221. rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  222. if (rc != 0)
  223. return rc;
  224. /* No pin, exit */
  225. if (pin == 0)
  226. return -ENODEV;
  227. /* Now we walk up the PCI tree */
  228. lspec = pin;
  229. for (;;) {
  230. /* Get the pci_dev of our parent */
  231. ppdev = pdev->bus->self;
  232. /* Ouch, it's a host bridge... */
  233. if (ppdev == NULL) {
  234. #ifdef CONFIG_PPC64
  235. ppnode = pci_bus_to_OF_node(pdev->bus);
  236. #else
  237. struct pci_controller *host;
  238. host = pci_bus_to_host(pdev->bus);
  239. ppnode = host ? host->dn : NULL;
  240. #endif
  241. /* No node for host bridge ? give up */
  242. if (ppnode == NULL)
  243. return -EINVAL;
  244. } else
  245. /* We found a P2P bridge, check if it has a node */
  246. ppnode = pci_device_to_OF_node(ppdev);
  247. /* Ok, we have found a parent with a device-node, hand over to
  248. * the OF parsing code.
  249. * We build a unit address from the linux device to be used for
  250. * resolution. Note that we use the linux bus number which may
  251. * not match your firmware bus numbering.
  252. * Fortunately, in most cases, interrupt-map-mask doesn't include
  253. * the bus number as part of the matching.
  254. * You should still be careful about that though if you intend
  255. * to rely on this function (you ship a firmware that doesn't
  256. * create device nodes for all PCI devices).
  257. */
  258. if (ppnode)
  259. break;
  260. /* We can only get here if we hit a P2P bridge with no node,
  261. * let's do standard swizzling and try again
  262. */
  263. lspec = of_irq_pci_swizzle(PCI_SLOT(pdev->devfn), lspec);
  264. pdev = ppdev;
  265. }
  266. laddr[0] = (pdev->bus->number << 16)
  267. | (pdev->devfn << 8);
  268. laddr[1] = laddr[2] = 0;
  269. return of_irq_map_raw(ppnode, &lspec, 1, laddr, out_irq);
  270. }
  271. EXPORT_SYMBOL_GPL(of_irq_map_pci);
  272. #endif /* CONFIG_PCI */
  273. /*
  274. * ISA bus specific translator
  275. */
  276. static int of_bus_isa_match(struct device_node *np)
  277. {
  278. return !strcmp(np->name, "isa");
  279. }
  280. static void of_bus_isa_count_cells(struct device_node *child,
  281. int *addrc, int *sizec)
  282. {
  283. if (addrc)
  284. *addrc = 2;
  285. if (sizec)
  286. *sizec = 1;
  287. }
  288. static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  289. {
  290. u64 cp, s, da;
  291. /* Check address type match */
  292. if ((addr[0] ^ range[0]) & 0x00000001)
  293. return OF_BAD_ADDR;
  294. /* Read address values, skipping high cell */
  295. cp = of_read_number(range + 1, na - 1);
  296. s = of_read_number(range + na + pna, ns);
  297. da = of_read_number(addr + 1, na - 1);
  298. DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
  299. if (da < cp || da >= (cp + s))
  300. return OF_BAD_ADDR;
  301. return da - cp;
  302. }
  303. static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
  304. {
  305. return of_bus_default_translate(addr + 1, offset, na - 1);
  306. }
  307. static unsigned int of_bus_isa_get_flags(const u32 *addr)
  308. {
  309. unsigned int flags = 0;
  310. u32 w = addr[0];
  311. if (w & 1)
  312. flags |= IORESOURCE_IO;
  313. else
  314. flags |= IORESOURCE_MEM;
  315. return flags;
  316. }
  317. /*
  318. * Array of bus specific translators
  319. */
  320. static struct of_bus of_busses[] = {
  321. #ifdef CONFIG_PCI
  322. /* PCI */
  323. {
  324. .name = "pci",
  325. .addresses = "assigned-addresses",
  326. .match = of_bus_pci_match,
  327. .count_cells = of_bus_pci_count_cells,
  328. .map = of_bus_pci_map,
  329. .translate = of_bus_pci_translate,
  330. .get_flags = of_bus_pci_get_flags,
  331. },
  332. #endif /* CONFIG_PCI */
  333. /* ISA */
  334. {
  335. .name = "isa",
  336. .addresses = "reg",
  337. .match = of_bus_isa_match,
  338. .count_cells = of_bus_isa_count_cells,
  339. .map = of_bus_isa_map,
  340. .translate = of_bus_isa_translate,
  341. .get_flags = of_bus_isa_get_flags,
  342. },
  343. /* Default */
  344. {
  345. .name = "default",
  346. .addresses = "reg",
  347. .match = NULL,
  348. .count_cells = of_bus_default_count_cells,
  349. .map = of_bus_default_map,
  350. .translate = of_bus_default_translate,
  351. .get_flags = of_bus_default_get_flags,
  352. },
  353. };
  354. static struct of_bus *of_match_bus(struct device_node *np)
  355. {
  356. int i;
  357. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  358. if (!of_busses[i].match || of_busses[i].match(np))
  359. return &of_busses[i];
  360. BUG();
  361. return NULL;
  362. }
  363. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  364. struct of_bus *pbus, u32 *addr,
  365. int na, int ns, int pna, const char *rprop)
  366. {
  367. const u32 *ranges;
  368. unsigned int rlen;
  369. int rone;
  370. u64 offset = OF_BAD_ADDR;
  371. /* Normally, an absence of a "ranges" property means we are
  372. * crossing a non-translatable boundary, and thus the addresses
  373. * below the current not cannot be converted to CPU physical ones.
  374. * Unfortunately, while this is very clear in the spec, it's not
  375. * what Apple understood, and they do have things like /uni-n or
  376. * /ht nodes with no "ranges" property and a lot of perfectly
  377. * useable mapped devices below them. Thus we treat the absence of
  378. * "ranges" as equivalent to an empty "ranges" property which means
  379. * a 1:1 translation at that level. It's up to the caller not to try
  380. * to translate addresses that aren't supposed to be translated in
  381. * the first place. --BenH.
  382. */
  383. ranges = of_get_property(parent, rprop, &rlen);
  384. if (ranges == NULL || rlen == 0) {
  385. offset = of_read_number(addr, na);
  386. memset(addr, 0, pna * 4);
  387. DBG("OF: no ranges, 1:1 translation\n");
  388. goto finish;
  389. }
  390. DBG("OF: walking ranges...\n");
  391. /* Now walk through the ranges */
  392. rlen /= 4;
  393. rone = na + pna + ns;
  394. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  395. offset = bus->map(addr, ranges, na, ns, pna);
  396. if (offset != OF_BAD_ADDR)
  397. break;
  398. }
  399. if (offset == OF_BAD_ADDR) {
  400. DBG("OF: not found !\n");
  401. return 1;
  402. }
  403. memcpy(addr, ranges + na, 4 * pna);
  404. finish:
  405. of_dump_addr("OF: parent translation for:", addr, pna);
  406. DBG("OF: with offset: "PRu64"\n", offset);
  407. /* Translate it into parent bus space */
  408. return pbus->translate(addr, offset, pna);
  409. }
  410. /*
  411. * Translate an address from the device-tree into a CPU physical address,
  412. * this walks up the tree and applies the various bus mappings on the
  413. * way.
  414. *
  415. * Note: We consider that crossing any level with #size-cells == 0 to mean
  416. * that translation is impossible (that is we are not dealing with a value
  417. * that can be mapped to a cpu physical address). This is not really specified
  418. * that way, but this is traditionally the way IBM at least do things
  419. */
  420. u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
  421. const char *rprop)
  422. {
  423. struct device_node *parent = NULL;
  424. struct of_bus *bus, *pbus;
  425. u32 addr[OF_MAX_ADDR_CELLS];
  426. int na, ns, pna, pns;
  427. u64 result = OF_BAD_ADDR;
  428. DBG("OF: ** translation for device %s **\n", dev->full_name);
  429. /* Increase refcount at current level */
  430. of_node_get(dev);
  431. /* Get parent & match bus type */
  432. parent = of_get_parent(dev);
  433. if (parent == NULL)
  434. goto bail;
  435. bus = of_match_bus(parent);
  436. /* Cound address cells & copy address locally */
  437. bus->count_cells(dev, &na, &ns);
  438. if (!OF_CHECK_COUNTS(na, ns)) {
  439. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  440. dev->full_name);
  441. goto bail;
  442. }
  443. memcpy(addr, in_addr, na * 4);
  444. DBG("OF: bus is %s (na=%d, ns=%d) on %s\n",
  445. bus->name, na, ns, parent->full_name);
  446. of_dump_addr("OF: translating address:", addr, na);
  447. /* Translate */
  448. for (;;) {
  449. /* Switch to parent bus */
  450. of_node_put(dev);
  451. dev = parent;
  452. parent = of_get_parent(dev);
  453. /* If root, we have finished */
  454. if (parent == NULL) {
  455. DBG("OF: reached root node\n");
  456. result = of_read_number(addr, na);
  457. break;
  458. }
  459. /* Get new parent bus and counts */
  460. pbus = of_match_bus(parent);
  461. pbus->count_cells(dev, &pna, &pns);
  462. if (!OF_CHECK_COUNTS(pna, pns)) {
  463. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  464. dev->full_name);
  465. break;
  466. }
  467. DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  468. pbus->name, pna, pns, parent->full_name);
  469. /* Apply bus translation */
  470. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  471. break;
  472. /* Complete the move up one level */
  473. na = pna;
  474. ns = pns;
  475. bus = pbus;
  476. of_dump_addr("OF: one level translation:", addr, na);
  477. }
  478. bail:
  479. of_node_put(parent);
  480. of_node_put(dev);
  481. return result;
  482. }
  483. u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
  484. {
  485. return __of_translate_address(dev, in_addr, "ranges");
  486. }
  487. EXPORT_SYMBOL(of_translate_address);
  488. u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
  489. {
  490. return __of_translate_address(dev, in_addr, "dma-ranges");
  491. }
  492. EXPORT_SYMBOL(of_translate_dma_address);
  493. const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
  494. unsigned int *flags)
  495. {
  496. const u32 *prop;
  497. unsigned int psize;
  498. struct device_node *parent;
  499. struct of_bus *bus;
  500. int onesize, i, na, ns;
  501. /* Get parent & match bus type */
  502. parent = of_get_parent(dev);
  503. if (parent == NULL)
  504. return NULL;
  505. bus = of_match_bus(parent);
  506. bus->count_cells(dev, &na, &ns);
  507. of_node_put(parent);
  508. if (!OF_CHECK_COUNTS(na, ns))
  509. return NULL;
  510. /* Get "reg" or "assigned-addresses" property */
  511. prop = of_get_property(dev, bus->addresses, &psize);
  512. if (prop == NULL)
  513. return NULL;
  514. psize /= 4;
  515. onesize = na + ns;
  516. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  517. if (i == index) {
  518. if (size)
  519. *size = of_read_number(prop + na, ns);
  520. if (flags)
  521. *flags = bus->get_flags(prop);
  522. return prop;
  523. }
  524. return NULL;
  525. }
  526. EXPORT_SYMBOL(of_get_address);
  527. static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
  528. u64 size, unsigned int flags,
  529. struct resource *r)
  530. {
  531. u64 taddr;
  532. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  533. return -EINVAL;
  534. taddr = of_translate_address(dev, addrp);
  535. if (taddr == OF_BAD_ADDR)
  536. return -EINVAL;
  537. memset(r, 0, sizeof(struct resource));
  538. if (flags & IORESOURCE_IO) {
  539. unsigned long port;
  540. port = pci_address_to_pio(taddr);
  541. if (port == (unsigned long)-1)
  542. return -EINVAL;
  543. r->start = port;
  544. r->end = port + size - 1;
  545. } else {
  546. r->start = taddr;
  547. r->end = taddr + size - 1;
  548. }
  549. r->flags = flags;
  550. r->name = dev->name;
  551. return 0;
  552. }
  553. int of_address_to_resource(struct device_node *dev, int index,
  554. struct resource *r)
  555. {
  556. const u32 *addrp;
  557. u64 size;
  558. unsigned int flags;
  559. addrp = of_get_address(dev, index, &size, &flags);
  560. if (addrp == NULL)
  561. return -EINVAL;
  562. return __of_address_to_resource(dev, addrp, size, flags, r);
  563. }
  564. EXPORT_SYMBOL_GPL(of_address_to_resource);
  565. void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop,
  566. unsigned long *busno, unsigned long *phys, unsigned long *size)
  567. {
  568. const u32 *dma_window;
  569. u32 cells;
  570. const unsigned char *prop;
  571. dma_window = dma_window_prop;
  572. /* busno is always one cell */
  573. *busno = *(dma_window++);
  574. prop = of_get_property(dn, "ibm,#dma-address-cells", NULL);
  575. if (!prop)
  576. prop = of_get_property(dn, "#address-cells", NULL);
  577. cells = prop ? *(u32 *)prop : of_n_addr_cells(dn);
  578. *phys = of_read_number(dma_window, cells);
  579. dma_window += cells;
  580. prop = of_get_property(dn, "ibm,#dma-size-cells", NULL);
  581. cells = prop ? *(u32 *)prop : of_n_size_cells(dn);
  582. *size = of_read_number(dma_window, cells);
  583. }
  584. /*
  585. * Interrupt remapper
  586. */
  587. static unsigned int of_irq_workarounds;
  588. static struct device_node *of_irq_dflt_pic;
  589. static struct device_node *of_irq_find_parent(struct device_node *child)
  590. {
  591. struct device_node *p;
  592. const phandle *parp;
  593. if (!of_node_get(child))
  594. return NULL;
  595. do {
  596. parp = of_get_property(child, "interrupt-parent", NULL);
  597. if (parp == NULL)
  598. p = of_get_parent(child);
  599. else {
  600. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  601. p = of_node_get(of_irq_dflt_pic);
  602. else
  603. p = of_find_node_by_phandle(*parp);
  604. }
  605. of_node_put(child);
  606. child = p;
  607. } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
  608. return p;
  609. }
  610. /* This doesn't need to be called if you don't have any special workaround
  611. * flags to pass
  612. */
  613. void of_irq_map_init(unsigned int flags)
  614. {
  615. of_irq_workarounds = flags;
  616. /* OldWorld, don't bother looking at other things */
  617. if (flags & OF_IMAP_OLDWORLD_MAC)
  618. return;
  619. /* If we don't have phandles, let's try to locate a default interrupt
  620. * controller (happens when booting with BootX). We do a first match
  621. * here, hopefully, that only ever happens on machines with one
  622. * controller.
  623. */
  624. if (flags & OF_IMAP_NO_PHANDLE) {
  625. struct device_node *np;
  626. for_each_node_with_property(np, "interrupt-controller") {
  627. /* Skip /chosen/interrupt-controller */
  628. if (strcmp(np->name, "chosen") == 0)
  629. continue;
  630. /* It seems like at least one person on this planet wants
  631. * to use BootX on a machine with an AppleKiwi controller
  632. * which happens to pretend to be an interrupt
  633. * controller too.
  634. */
  635. if (strcmp(np->name, "AppleKiwi") == 0)
  636. continue;
  637. /* I think we found one ! */
  638. of_irq_dflt_pic = np;
  639. break;
  640. }
  641. }
  642. }
  643. int of_irq_map_raw(struct device_node *parent, const u32 *intspec, u32 ointsize,
  644. const u32 *addr, struct of_irq *out_irq)
  645. {
  646. struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
  647. const u32 *tmp, *imap, *imask;
  648. u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
  649. int imaplen, match, i;
  650. DBG("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
  651. parent->full_name, intspec[0], intspec[1], ointsize);
  652. ipar = of_node_get(parent);
  653. /* First get the #interrupt-cells property of the current cursor
  654. * that tells us how to interpret the passed-in intspec. If there
  655. * is none, we are nice and just walk up the tree
  656. */
  657. do {
  658. tmp = of_get_property(ipar, "#interrupt-cells", NULL);
  659. if (tmp != NULL) {
  660. intsize = *tmp;
  661. break;
  662. }
  663. tnode = ipar;
  664. ipar = of_irq_find_parent(ipar);
  665. of_node_put(tnode);
  666. } while (ipar);
  667. if (ipar == NULL) {
  668. DBG(" -> no parent found !\n");
  669. goto fail;
  670. }
  671. DBG("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
  672. if (ointsize != intsize)
  673. return -EINVAL;
  674. /* Look for this #address-cells. We have to implement the old linux
  675. * trick of looking for the parent here as some device-trees rely on it
  676. */
  677. old = of_node_get(ipar);
  678. do {
  679. tmp = of_get_property(old, "#address-cells", NULL);
  680. tnode = of_get_parent(old);
  681. of_node_put(old);
  682. old = tnode;
  683. } while(old && tmp == NULL);
  684. of_node_put(old);
  685. old = NULL;
  686. addrsize = (tmp == NULL) ? 2 : *tmp;
  687. DBG(" -> addrsize=%d\n", addrsize);
  688. /* Now start the actual "proper" walk of the interrupt tree */
  689. while (ipar != NULL) {
  690. /* Now check if cursor is an interrupt-controller and if it is
  691. * then we are done
  692. */
  693. if (of_get_property(ipar, "interrupt-controller", NULL) !=
  694. NULL) {
  695. DBG(" -> got it !\n");
  696. memcpy(out_irq->specifier, intspec,
  697. intsize * sizeof(u32));
  698. out_irq->size = intsize;
  699. out_irq->controller = ipar;
  700. of_node_put(old);
  701. return 0;
  702. }
  703. /* Now look for an interrupt-map */
  704. imap = of_get_property(ipar, "interrupt-map", &imaplen);
  705. /* No interrupt map, check for an interrupt parent */
  706. if (imap == NULL) {
  707. DBG(" -> no map, getting parent\n");
  708. newpar = of_irq_find_parent(ipar);
  709. goto skiplevel;
  710. }
  711. imaplen /= sizeof(u32);
  712. /* Look for a mask */
  713. imask = of_get_property(ipar, "interrupt-map-mask", NULL);
  714. /* If we were passed no "reg" property and we attempt to parse
  715. * an interrupt-map, then #address-cells must be 0.
  716. * Fail if it's not.
  717. */
  718. if (addr == NULL && addrsize != 0) {
  719. DBG(" -> no reg passed in when needed !\n");
  720. goto fail;
  721. }
  722. /* Parse interrupt-map */
  723. match = 0;
  724. while (imaplen > (addrsize + intsize + 1) && !match) {
  725. /* Compare specifiers */
  726. match = 1;
  727. for (i = 0; i < addrsize && match; ++i) {
  728. u32 mask = imask ? imask[i] : 0xffffffffu;
  729. match = ((addr[i] ^ imap[i]) & mask) == 0;
  730. }
  731. for (; i < (addrsize + intsize) && match; ++i) {
  732. u32 mask = imask ? imask[i] : 0xffffffffu;
  733. match =
  734. ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
  735. }
  736. imap += addrsize + intsize;
  737. imaplen -= addrsize + intsize;
  738. DBG(" -> match=%d (imaplen=%d)\n", match, imaplen);
  739. /* Get the interrupt parent */
  740. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  741. newpar = of_node_get(of_irq_dflt_pic);
  742. else
  743. newpar = of_find_node_by_phandle((phandle)*imap);
  744. imap++;
  745. --imaplen;
  746. /* Check if not found */
  747. if (newpar == NULL) {
  748. DBG(" -> imap parent not found !\n");
  749. goto fail;
  750. }
  751. /* Get #interrupt-cells and #address-cells of new
  752. * parent
  753. */
  754. tmp = of_get_property(newpar, "#interrupt-cells", NULL);
  755. if (tmp == NULL) {
  756. DBG(" -> parent lacks #interrupt-cells !\n");
  757. goto fail;
  758. }
  759. newintsize = *tmp;
  760. tmp = of_get_property(newpar, "#address-cells", NULL);
  761. newaddrsize = (tmp == NULL) ? 0 : *tmp;
  762. DBG(" -> newintsize=%d, newaddrsize=%d\n",
  763. newintsize, newaddrsize);
  764. /* Check for malformed properties */
  765. if (imaplen < (newaddrsize + newintsize))
  766. goto fail;
  767. imap += newaddrsize + newintsize;
  768. imaplen -= newaddrsize + newintsize;
  769. DBG(" -> imaplen=%d\n", imaplen);
  770. }
  771. if (!match)
  772. goto fail;
  773. of_node_put(old);
  774. old = of_node_get(newpar);
  775. addrsize = newaddrsize;
  776. intsize = newintsize;
  777. intspec = imap - intsize;
  778. addr = intspec - addrsize;
  779. skiplevel:
  780. /* Iterate again with new parent */
  781. DBG(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
  782. of_node_put(ipar);
  783. ipar = newpar;
  784. newpar = NULL;
  785. }
  786. fail:
  787. of_node_put(ipar);
  788. of_node_put(old);
  789. of_node_put(newpar);
  790. return -EINVAL;
  791. }
  792. EXPORT_SYMBOL_GPL(of_irq_map_raw);
  793. #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
  794. static int of_irq_map_oldworld(struct device_node *device, int index,
  795. struct of_irq *out_irq)
  796. {
  797. const u32 *ints = NULL;
  798. int intlen;
  799. /*
  800. * Old machines just have a list of interrupt numbers
  801. * and no interrupt-controller nodes. We also have dodgy
  802. * cases where the APPL,interrupts property is completely
  803. * missing behind pci-pci bridges and we have to get it
  804. * from the parent (the bridge itself, as apple just wired
  805. * everything together on these)
  806. */
  807. while (device) {
  808. ints = of_get_property(device, "AAPL,interrupts", &intlen);
  809. if (ints != NULL)
  810. break;
  811. device = device->parent;
  812. if (device && strcmp(device->type, "pci") != 0)
  813. break;
  814. }
  815. if (ints == NULL)
  816. return -EINVAL;
  817. intlen /= sizeof(u32);
  818. if (index >= intlen)
  819. return -EINVAL;
  820. out_irq->controller = NULL;
  821. out_irq->specifier[0] = ints[index];
  822. out_irq->size = 1;
  823. return 0;
  824. }
  825. #else /* defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32) */
  826. static int of_irq_map_oldworld(struct device_node *device, int index,
  827. struct of_irq *out_irq)
  828. {
  829. return -EINVAL;
  830. }
  831. #endif /* !(defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)) */
  832. int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
  833. {
  834. struct device_node *p;
  835. const u32 *intspec, *tmp, *addr;
  836. u32 intsize, intlen;
  837. int res;
  838. DBG("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);
  839. /* OldWorld mac stuff is "special", handle out of line */
  840. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  841. return of_irq_map_oldworld(device, index, out_irq);
  842. /* Get the interrupts property */
  843. intspec = of_get_property(device, "interrupts", &intlen);
  844. if (intspec == NULL)
  845. return -EINVAL;
  846. intlen /= sizeof(u32);
  847. /* Get the reg property (if any) */
  848. addr = of_get_property(device, "reg", NULL);
  849. /* Look for the interrupt parent. */
  850. p = of_irq_find_parent(device);
  851. if (p == NULL)
  852. return -EINVAL;
  853. /* Get size of interrupt specifier */
  854. tmp = of_get_property(p, "#interrupt-cells", NULL);
  855. if (tmp == NULL) {
  856. of_node_put(p);
  857. return -EINVAL;
  858. }
  859. intsize = *tmp;
  860. DBG(" intsize=%d intlen=%d\n", intsize, intlen);
  861. /* Check index */
  862. if ((index + 1) * intsize > intlen)
  863. return -EINVAL;
  864. /* Get new specifier and map it */
  865. res = of_irq_map_raw(p, intspec + index * intsize, intsize,
  866. addr, out_irq);
  867. of_node_put(p);
  868. return res;
  869. }
  870. EXPORT_SYMBOL_GPL(of_irq_map_one);
  871. /**
  872. * Search the device tree for the best MAC address to use. 'mac-address' is
  873. * checked first, because that is supposed to contain to "most recent" MAC
  874. * address. If that isn't set, then 'local-mac-address' is checked next,
  875. * because that is the default address. If that isn't set, then the obsolete
  876. * 'address' is checked, just in case we're using an old device tree.
  877. *
  878. * Note that the 'address' property is supposed to contain a virtual address of
  879. * the register set, but some DTS files have redefined that property to be the
  880. * MAC address.
  881. *
  882. * All-zero MAC addresses are rejected, because those could be properties that
  883. * exist in the device tree, but were not set by U-Boot. For example, the
  884. * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
  885. * addresses. Some older U-Boots only initialized 'local-mac-address'. In
  886. * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
  887. * but is all zeros.
  888. */
  889. const void *of_get_mac_address(struct device_node *np)
  890. {
  891. struct property *pp;
  892. pp = of_find_property(np, "mac-address", NULL);
  893. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  894. return pp->value;
  895. pp = of_find_property(np, "local-mac-address", NULL);
  896. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  897. return pp->value;
  898. pp = of_find_property(np, "address", NULL);
  899. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  900. return pp->value;
  901. return NULL;
  902. }
  903. EXPORT_SYMBOL(of_get_mac_address);
  904. int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
  905. {
  906. int irq = irq_of_parse_and_map(dev, index);
  907. /* Only dereference the resource if both the
  908. * resource and the irq are valid. */
  909. if (r && irq != NO_IRQ) {
  910. r->start = r->end = irq;
  911. r->flags = IORESOURCE_IRQ;
  912. }
  913. return irq;
  914. }
  915. EXPORT_SYMBOL_GPL(of_irq_to_resource);
  916. void __iomem *of_iomap(struct device_node *np, int index)
  917. {
  918. struct resource res;
  919. if (of_address_to_resource(np, index, &res))
  920. return NULL;
  921. return ioremap(res.start, 1 + res.end - res.start);
  922. }
  923. EXPORT_SYMBOL(of_iomap);