prom_parse.c 27 KB

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