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. return of_irq_map_one(dn, 0, out_irq);
  214. /* Ok, we don't, time to have fun. Let's start by building up an
  215. * interrupt spec. we assume #interrupt-cells is 1, which is standard
  216. * for PCI. If you do different, then don't use that routine.
  217. */
  218. rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  219. if (rc != 0)
  220. return rc;
  221. /* No pin, exit */
  222. if (pin == 0)
  223. return -ENODEV;
  224. /* Now we walk up the PCI tree */
  225. lspec = pin;
  226. for (;;) {
  227. /* Get the pci_dev of our parent */
  228. ppdev = pdev->bus->self;
  229. /* Ouch, it's a host bridge... */
  230. if (ppdev == NULL) {
  231. #ifdef CONFIG_PPC64
  232. ppnode = pci_bus_to_OF_node(pdev->bus);
  233. #else
  234. struct pci_controller *host;
  235. host = pci_bus_to_host(pdev->bus);
  236. ppnode = host ? host->dn : NULL;
  237. #endif
  238. /* No node for host bridge ? give up */
  239. if (ppnode == NULL)
  240. return -EINVAL;
  241. } else
  242. /* We found a P2P bridge, check if it has a node */
  243. ppnode = pci_device_to_OF_node(ppdev);
  244. /* Ok, we have found a parent with a device-node, hand over to
  245. * the OF parsing code.
  246. * We build a unit address from the linux device to be used for
  247. * resolution. Note that we use the linux bus number which may
  248. * not match your firmware bus numbering.
  249. * Fortunately, in most cases, interrupt-map-mask doesn't include
  250. * the bus number as part of the matching.
  251. * You should still be careful about that though if you intend
  252. * to rely on this function (you ship a firmware that doesn't
  253. * create device nodes for all PCI devices).
  254. */
  255. if (ppnode)
  256. break;
  257. /* We can only get here if we hit a P2P bridge with no node,
  258. * let's do standard swizzling and try again
  259. */
  260. lspec = of_irq_pci_swizzle(PCI_SLOT(pdev->devfn), lspec);
  261. pdev = ppdev;
  262. }
  263. laddr[0] = (pdev->bus->number << 16)
  264. | (pdev->devfn << 8);
  265. laddr[1] = laddr[2] = 0;
  266. return of_irq_map_raw(ppnode, &lspec, 1, laddr, out_irq);
  267. }
  268. EXPORT_SYMBOL_GPL(of_irq_map_pci);
  269. #endif /* CONFIG_PCI */
  270. /*
  271. * ISA bus specific translator
  272. */
  273. static int of_bus_isa_match(struct device_node *np)
  274. {
  275. return !strcmp(np->name, "isa");
  276. }
  277. static void of_bus_isa_count_cells(struct device_node *child,
  278. int *addrc, int *sizec)
  279. {
  280. if (addrc)
  281. *addrc = 2;
  282. if (sizec)
  283. *sizec = 1;
  284. }
  285. static u64 of_bus_isa_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  286. {
  287. u64 cp, s, da;
  288. /* Check address type match */
  289. if ((addr[0] ^ range[0]) & 0x00000001)
  290. return OF_BAD_ADDR;
  291. /* Read address values, skipping high cell */
  292. cp = of_read_number(range + 1, na - 1);
  293. s = of_read_number(range + na + pna, ns);
  294. da = of_read_number(addr + 1, na - 1);
  295. DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
  296. if (da < cp || da >= (cp + s))
  297. return OF_BAD_ADDR;
  298. return da - cp;
  299. }
  300. static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
  301. {
  302. return of_bus_default_translate(addr + 1, offset, na - 1);
  303. }
  304. static unsigned int of_bus_isa_get_flags(const u32 *addr)
  305. {
  306. unsigned int flags = 0;
  307. u32 w = addr[0];
  308. if (w & 1)
  309. flags |= IORESOURCE_IO;
  310. else
  311. flags |= IORESOURCE_MEM;
  312. return flags;
  313. }
  314. /*
  315. * Array of bus specific translators
  316. */
  317. static struct of_bus of_busses[] = {
  318. #ifdef CONFIG_PCI
  319. /* PCI */
  320. {
  321. .name = "pci",
  322. .addresses = "assigned-addresses",
  323. .match = of_bus_pci_match,
  324. .count_cells = of_bus_pci_count_cells,
  325. .map = of_bus_pci_map,
  326. .translate = of_bus_pci_translate,
  327. .get_flags = of_bus_pci_get_flags,
  328. },
  329. #endif /* CONFIG_PCI */
  330. /* ISA */
  331. {
  332. .name = "isa",
  333. .addresses = "reg",
  334. .match = of_bus_isa_match,
  335. .count_cells = of_bus_isa_count_cells,
  336. .map = of_bus_isa_map,
  337. .translate = of_bus_isa_translate,
  338. .get_flags = of_bus_isa_get_flags,
  339. },
  340. /* Default */
  341. {
  342. .name = "default",
  343. .addresses = "reg",
  344. .match = NULL,
  345. .count_cells = of_bus_default_count_cells,
  346. .map = of_bus_default_map,
  347. .translate = of_bus_default_translate,
  348. .get_flags = of_bus_default_get_flags,
  349. },
  350. };
  351. static struct of_bus *of_match_bus(struct device_node *np)
  352. {
  353. int i;
  354. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  355. if (!of_busses[i].match || of_busses[i].match(np))
  356. return &of_busses[i];
  357. BUG();
  358. return NULL;
  359. }
  360. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  361. struct of_bus *pbus, u32 *addr,
  362. int na, int ns, int pna, const char *rprop)
  363. {
  364. const u32 *ranges;
  365. unsigned int rlen;
  366. int rone;
  367. u64 offset = OF_BAD_ADDR;
  368. /* Normally, an absence of a "ranges" property means we are
  369. * crossing a non-translatable boundary, and thus the addresses
  370. * below the current not cannot be converted to CPU physical ones.
  371. * Unfortunately, while this is very clear in the spec, it's not
  372. * what Apple understood, and they do have things like /uni-n or
  373. * /ht nodes with no "ranges" property and a lot of perfectly
  374. * useable mapped devices below them. Thus we treat the absence of
  375. * "ranges" as equivalent to an empty "ranges" property which means
  376. * a 1:1 translation at that level. It's up to the caller not to try
  377. * to translate addresses that aren't supposed to be translated in
  378. * the first place. --BenH.
  379. */
  380. ranges = of_get_property(parent, rprop, &rlen);
  381. if (ranges == NULL || rlen == 0) {
  382. offset = of_read_number(addr, na);
  383. memset(addr, 0, pna * 4);
  384. DBG("OF: no ranges, 1:1 translation\n");
  385. goto finish;
  386. }
  387. DBG("OF: walking ranges...\n");
  388. /* Now walk through the ranges */
  389. rlen /= 4;
  390. rone = na + pna + ns;
  391. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  392. offset = bus->map(addr, ranges, na, ns, pna);
  393. if (offset != OF_BAD_ADDR)
  394. break;
  395. }
  396. if (offset == OF_BAD_ADDR) {
  397. DBG("OF: not found !\n");
  398. return 1;
  399. }
  400. memcpy(addr, ranges + na, 4 * pna);
  401. finish:
  402. of_dump_addr("OF: parent translation for:", addr, pna);
  403. DBG("OF: with offset: "PRu64"\n", offset);
  404. /* Translate it into parent bus space */
  405. return pbus->translate(addr, offset, pna);
  406. }
  407. /*
  408. * Translate an address from the device-tree into a CPU physical address,
  409. * this walks up the tree and applies the various bus mappings on the
  410. * way.
  411. *
  412. * Note: We consider that crossing any level with #size-cells == 0 to mean
  413. * that translation is impossible (that is we are not dealing with a value
  414. * that can be mapped to a cpu physical address). This is not really specified
  415. * that way, but this is traditionally the way IBM at least do things
  416. */
  417. u64 __of_translate_address(struct device_node *dev, const u32 *in_addr,
  418. const char *rprop)
  419. {
  420. struct device_node *parent = NULL;
  421. struct of_bus *bus, *pbus;
  422. u32 addr[OF_MAX_ADDR_CELLS];
  423. int na, ns, pna, pns;
  424. u64 result = OF_BAD_ADDR;
  425. DBG("OF: ** translation for device %s **\n", dev->full_name);
  426. /* Increase refcount at current level */
  427. of_node_get(dev);
  428. /* Get parent & match bus type */
  429. parent = of_get_parent(dev);
  430. if (parent == NULL)
  431. goto bail;
  432. bus = of_match_bus(parent);
  433. /* Cound address cells & copy address locally */
  434. bus->count_cells(dev, &na, &ns);
  435. if (!OF_CHECK_COUNTS(na, ns)) {
  436. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  437. dev->full_name);
  438. goto bail;
  439. }
  440. memcpy(addr, in_addr, na * 4);
  441. DBG("OF: bus is %s (na=%d, ns=%d) on %s\n",
  442. bus->name, na, ns, parent->full_name);
  443. of_dump_addr("OF: translating address:", addr, na);
  444. /* Translate */
  445. for (;;) {
  446. /* Switch to parent bus */
  447. of_node_put(dev);
  448. dev = parent;
  449. parent = of_get_parent(dev);
  450. /* If root, we have finished */
  451. if (parent == NULL) {
  452. DBG("OF: reached root node\n");
  453. result = of_read_number(addr, na);
  454. break;
  455. }
  456. /* Get new parent bus and counts */
  457. pbus = of_match_bus(parent);
  458. pbus->count_cells(dev, &pna, &pns);
  459. if (!OF_CHECK_COUNTS(pna, pns)) {
  460. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  461. dev->full_name);
  462. break;
  463. }
  464. DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  465. pbus->name, pna, pns, parent->full_name);
  466. /* Apply bus translation */
  467. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
  468. break;
  469. /* Complete the move up one level */
  470. na = pna;
  471. ns = pns;
  472. bus = pbus;
  473. of_dump_addr("OF: one level translation:", addr, na);
  474. }
  475. bail:
  476. of_node_put(parent);
  477. of_node_put(dev);
  478. return result;
  479. }
  480. u64 of_translate_address(struct device_node *dev, const u32 *in_addr)
  481. {
  482. return __of_translate_address(dev, in_addr, "ranges");
  483. }
  484. EXPORT_SYMBOL(of_translate_address);
  485. u64 of_translate_dma_address(struct device_node *dev, const u32 *in_addr)
  486. {
  487. return __of_translate_address(dev, in_addr, "dma-ranges");
  488. }
  489. EXPORT_SYMBOL(of_translate_dma_address);
  490. const u32 *of_get_address(struct device_node *dev, int index, u64 *size,
  491. unsigned int *flags)
  492. {
  493. const u32 *prop;
  494. unsigned int psize;
  495. struct device_node *parent;
  496. struct of_bus *bus;
  497. int onesize, i, na, ns;
  498. /* Get parent & match bus type */
  499. parent = of_get_parent(dev);
  500. if (parent == NULL)
  501. return NULL;
  502. bus = of_match_bus(parent);
  503. bus->count_cells(dev, &na, &ns);
  504. of_node_put(parent);
  505. if (!OF_CHECK_COUNTS(na, ns))
  506. return NULL;
  507. /* Get "reg" or "assigned-addresses" property */
  508. prop = of_get_property(dev, bus->addresses, &psize);
  509. if (prop == NULL)
  510. return NULL;
  511. psize /= 4;
  512. onesize = na + ns;
  513. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  514. if (i == index) {
  515. if (size)
  516. *size = of_read_number(prop + na, ns);
  517. if (flags)
  518. *flags = bus->get_flags(prop);
  519. return prop;
  520. }
  521. return NULL;
  522. }
  523. EXPORT_SYMBOL(of_get_address);
  524. static int __of_address_to_resource(struct device_node *dev, const u32 *addrp,
  525. u64 size, unsigned int flags,
  526. struct resource *r)
  527. {
  528. u64 taddr;
  529. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  530. return -EINVAL;
  531. taddr = of_translate_address(dev, addrp);
  532. if (taddr == OF_BAD_ADDR)
  533. return -EINVAL;
  534. memset(r, 0, sizeof(struct resource));
  535. if (flags & IORESOURCE_IO) {
  536. unsigned long port;
  537. port = pci_address_to_pio(taddr);
  538. if (port == (unsigned long)-1)
  539. return -EINVAL;
  540. r->start = port;
  541. r->end = port + size - 1;
  542. } else {
  543. r->start = taddr;
  544. r->end = taddr + size - 1;
  545. }
  546. r->flags = flags;
  547. r->name = dev->name;
  548. return 0;
  549. }
  550. int of_address_to_resource(struct device_node *dev, int index,
  551. struct resource *r)
  552. {
  553. const u32 *addrp;
  554. u64 size;
  555. unsigned int flags;
  556. addrp = of_get_address(dev, index, &size, &flags);
  557. if (addrp == NULL)
  558. return -EINVAL;
  559. return __of_address_to_resource(dev, addrp, size, flags, r);
  560. }
  561. EXPORT_SYMBOL_GPL(of_address_to_resource);
  562. void of_parse_dma_window(struct device_node *dn, const void *dma_window_prop,
  563. unsigned long *busno, unsigned long *phys, unsigned long *size)
  564. {
  565. const u32 *dma_window;
  566. u32 cells;
  567. const unsigned char *prop;
  568. dma_window = dma_window_prop;
  569. /* busno is always one cell */
  570. *busno = *(dma_window++);
  571. prop = of_get_property(dn, "ibm,#dma-address-cells", NULL);
  572. if (!prop)
  573. prop = of_get_property(dn, "#address-cells", NULL);
  574. cells = prop ? *(u32 *)prop : of_n_addr_cells(dn);
  575. *phys = of_read_number(dma_window, cells);
  576. dma_window += cells;
  577. prop = of_get_property(dn, "ibm,#dma-size-cells", NULL);
  578. cells = prop ? *(u32 *)prop : of_n_size_cells(dn);
  579. *size = of_read_number(dma_window, cells);
  580. }
  581. /*
  582. * Interrupt remapper
  583. */
  584. static unsigned int of_irq_workarounds;
  585. static struct device_node *of_irq_dflt_pic;
  586. static struct device_node *of_irq_find_parent(struct device_node *child)
  587. {
  588. struct device_node *p;
  589. const phandle *parp;
  590. if (!of_node_get(child))
  591. return NULL;
  592. do {
  593. parp = of_get_property(child, "interrupt-parent", NULL);
  594. if (parp == NULL)
  595. p = of_get_parent(child);
  596. else {
  597. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  598. p = of_node_get(of_irq_dflt_pic);
  599. else
  600. p = of_find_node_by_phandle(*parp);
  601. }
  602. of_node_put(child);
  603. child = p;
  604. } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
  605. return p;
  606. }
  607. /* This doesn't need to be called if you don't have any special workaround
  608. * flags to pass
  609. */
  610. void of_irq_map_init(unsigned int flags)
  611. {
  612. of_irq_workarounds = flags;
  613. /* OldWorld, don't bother looking at other things */
  614. if (flags & OF_IMAP_OLDWORLD_MAC)
  615. return;
  616. /* If we don't have phandles, let's try to locate a default interrupt
  617. * controller (happens when booting with BootX). We do a first match
  618. * here, hopefully, that only ever happens on machines with one
  619. * controller.
  620. */
  621. if (flags & OF_IMAP_NO_PHANDLE) {
  622. struct device_node *np;
  623. for(np = NULL; (np = of_find_all_nodes(np)) != NULL;) {
  624. if (of_get_property(np, "interrupt-controller", NULL)
  625. == NULL)
  626. continue;
  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);