of_device.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/module.h>
  5. #include <linux/mod_devicetable.h>
  6. #include <linux/slab.h>
  7. #include <asm/errno.h>
  8. #include <asm/of_device.h>
  9. /**
  10. * of_match_device - Tell if an of_device structure has a matching
  11. * of_match structure
  12. * @ids: array of of device match structures to search in
  13. * @dev: the of device structure to match against
  14. *
  15. * Used by a driver to check whether an of_device present in the
  16. * system is in its list of supported devices.
  17. */
  18. const struct of_device_id *of_match_device(const struct of_device_id *matches,
  19. const struct of_device *dev)
  20. {
  21. if (!dev->node)
  22. return NULL;
  23. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  24. int match = 1;
  25. if (matches->name[0])
  26. match &= dev->node->name
  27. && !strcmp(matches->name, dev->node->name);
  28. if (matches->type[0])
  29. match &= dev->node->type
  30. && !strcmp(matches->type, dev->node->type);
  31. if (matches->compatible[0])
  32. match &= of_device_is_compatible(dev->node,
  33. matches->compatible);
  34. if (match)
  35. return matches;
  36. matches++;
  37. }
  38. return NULL;
  39. }
  40. static int of_platform_bus_match(struct device *dev, struct device_driver *drv)
  41. {
  42. struct of_device * of_dev = to_of_device(dev);
  43. struct of_platform_driver * of_drv = to_of_platform_driver(drv);
  44. const struct of_device_id * matches = of_drv->match_table;
  45. if (!matches)
  46. return 0;
  47. return of_match_device(matches, of_dev) != NULL;
  48. }
  49. struct of_device *of_dev_get(struct of_device *dev)
  50. {
  51. struct device *tmp;
  52. if (!dev)
  53. return NULL;
  54. tmp = get_device(&dev->dev);
  55. if (tmp)
  56. return to_of_device(tmp);
  57. else
  58. return NULL;
  59. }
  60. void of_dev_put(struct of_device *dev)
  61. {
  62. if (dev)
  63. put_device(&dev->dev);
  64. }
  65. static int of_device_probe(struct device *dev)
  66. {
  67. int error = -ENODEV;
  68. struct of_platform_driver *drv;
  69. struct of_device *of_dev;
  70. const struct of_device_id *match;
  71. drv = to_of_platform_driver(dev->driver);
  72. of_dev = to_of_device(dev);
  73. if (!drv->probe)
  74. return error;
  75. of_dev_get(of_dev);
  76. match = of_match_device(drv->match_table, of_dev);
  77. if (match)
  78. error = drv->probe(of_dev, match);
  79. if (error)
  80. of_dev_put(of_dev);
  81. return error;
  82. }
  83. static int of_device_remove(struct device *dev)
  84. {
  85. struct of_device * of_dev = to_of_device(dev);
  86. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  87. if (dev->driver && drv->remove)
  88. drv->remove(of_dev);
  89. return 0;
  90. }
  91. static int of_device_suspend(struct device *dev, pm_message_t state)
  92. {
  93. struct of_device * of_dev = to_of_device(dev);
  94. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  95. int error = 0;
  96. if (dev->driver && drv->suspend)
  97. error = drv->suspend(of_dev, state);
  98. return error;
  99. }
  100. static int of_device_resume(struct device * dev)
  101. {
  102. struct of_device * of_dev = to_of_device(dev);
  103. struct of_platform_driver * drv = to_of_platform_driver(dev->driver);
  104. int error = 0;
  105. if (dev->driver && drv->resume)
  106. error = drv->resume(of_dev);
  107. return error;
  108. }
  109. void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name)
  110. {
  111. unsigned long ret = res->start + offset;
  112. struct resource *r;
  113. if (res->flags & IORESOURCE_MEM)
  114. r = request_mem_region(ret, size, name);
  115. else
  116. r = request_region(ret, size, name);
  117. if (!r)
  118. ret = 0;
  119. return (void __iomem *) ret;
  120. }
  121. EXPORT_SYMBOL(of_ioremap);
  122. void of_iounmap(void __iomem *base, unsigned long size)
  123. {
  124. release_region((unsigned long) base, size);
  125. }
  126. EXPORT_SYMBOL(of_iounmap);
  127. static int node_match(struct device *dev, void *data)
  128. {
  129. struct of_device *op = to_of_device(dev);
  130. struct device_node *dp = data;
  131. return (op->node == dp);
  132. }
  133. struct of_device *of_find_device_by_node(struct device_node *dp)
  134. {
  135. struct device *dev = bus_find_device(&of_bus_type, NULL,
  136. dp, node_match);
  137. if (dev)
  138. return to_of_device(dev);
  139. return NULL;
  140. }
  141. EXPORT_SYMBOL(of_find_device_by_node);
  142. #ifdef CONFIG_PCI
  143. struct bus_type isa_bus_type = {
  144. .name = "isa",
  145. .match = of_platform_bus_match,
  146. .probe = of_device_probe,
  147. .remove = of_device_remove,
  148. .suspend = of_device_suspend,
  149. .resume = of_device_resume,
  150. };
  151. EXPORT_SYMBOL(isa_bus_type);
  152. struct bus_type ebus_bus_type = {
  153. .name = "ebus",
  154. .match = of_platform_bus_match,
  155. .probe = of_device_probe,
  156. .remove = of_device_remove,
  157. .suspend = of_device_suspend,
  158. .resume = of_device_resume,
  159. };
  160. EXPORT_SYMBOL(ebus_bus_type);
  161. #endif
  162. #ifdef CONFIG_SBUS
  163. struct bus_type sbus_bus_type = {
  164. .name = "sbus",
  165. .match = of_platform_bus_match,
  166. .probe = of_device_probe,
  167. .remove = of_device_remove,
  168. .suspend = of_device_suspend,
  169. .resume = of_device_resume,
  170. };
  171. EXPORT_SYMBOL(sbus_bus_type);
  172. #endif
  173. struct bus_type of_bus_type = {
  174. .name = "of",
  175. .match = of_platform_bus_match,
  176. .probe = of_device_probe,
  177. .remove = of_device_remove,
  178. .suspend = of_device_suspend,
  179. .resume = of_device_resume,
  180. };
  181. EXPORT_SYMBOL(of_bus_type);
  182. static inline u64 of_read_addr(const u32 *cell, int size)
  183. {
  184. u64 r = 0;
  185. while (size--)
  186. r = (r << 32) | *(cell++);
  187. return r;
  188. }
  189. static void __init get_cells(struct device_node *dp,
  190. int *addrc, int *sizec)
  191. {
  192. if (addrc)
  193. *addrc = of_n_addr_cells(dp);
  194. if (sizec)
  195. *sizec = of_n_size_cells(dp);
  196. }
  197. /* Max address size we deal with */
  198. #define OF_MAX_ADDR_CELLS 4
  199. struct of_bus {
  200. const char *name;
  201. const char *addr_prop_name;
  202. int (*match)(struct device_node *parent);
  203. void (*count_cells)(struct device_node *child,
  204. int *addrc, int *sizec);
  205. int (*map)(u32 *addr, const u32 *range,
  206. int na, int ns, int pna);
  207. unsigned int (*get_flags)(u32 *addr);
  208. };
  209. /*
  210. * Default translator (generic bus)
  211. */
  212. static void of_bus_default_count_cells(struct device_node *dev,
  213. int *addrc, int *sizec)
  214. {
  215. get_cells(dev, addrc, sizec);
  216. }
  217. /* Make sure the least significant 64-bits are in-range. Even
  218. * for 3 or 4 cell values it is a good enough approximation.
  219. */
  220. static int of_out_of_range(const u32 *addr, const u32 *base,
  221. const u32 *size, int na, int ns)
  222. {
  223. u64 a = of_read_addr(addr, na);
  224. u64 b = of_read_addr(base, na);
  225. if (a < b)
  226. return 1;
  227. b += of_read_addr(size, ns);
  228. if (a >= b)
  229. return 1;
  230. return 0;
  231. }
  232. static int of_bus_default_map(u32 *addr, const u32 *range,
  233. int na, int ns, int pna)
  234. {
  235. u32 result[OF_MAX_ADDR_CELLS];
  236. int i;
  237. if (ns > 2) {
  238. printk("of_device: Cannot handle size cells (%d) > 2.", ns);
  239. return -EINVAL;
  240. }
  241. if (of_out_of_range(addr, range, range + na + pna, na, ns))
  242. return -EINVAL;
  243. /* Start with the parent range base. */
  244. memcpy(result, range + na, pna * 4);
  245. /* Add in the child address offset. */
  246. for (i = 0; i < na; i++)
  247. result[pna - 1 - i] +=
  248. (addr[na - 1 - i] -
  249. range[na - 1 - i]);
  250. memcpy(addr, result, pna * 4);
  251. return 0;
  252. }
  253. static unsigned int of_bus_default_get_flags(u32 *addr)
  254. {
  255. return IORESOURCE_MEM;
  256. }
  257. /*
  258. * PCI bus specific translator
  259. */
  260. static int of_bus_pci_match(struct device_node *np)
  261. {
  262. if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
  263. /* Do not do PCI specific frobbing if the
  264. * PCI bridge lacks a ranges property. We
  265. * want to pass it through up to the next
  266. * parent as-is, not with the PCI translate
  267. * method which chops off the top address cell.
  268. */
  269. if (!of_find_property(np, "ranges", NULL))
  270. return 0;
  271. return 1;
  272. }
  273. return 0;
  274. }
  275. static void of_bus_pci_count_cells(struct device_node *np,
  276. int *addrc, int *sizec)
  277. {
  278. if (addrc)
  279. *addrc = 3;
  280. if (sizec)
  281. *sizec = 2;
  282. }
  283. static int of_bus_pci_map(u32 *addr, const u32 *range,
  284. int na, int ns, int pna)
  285. {
  286. u32 result[OF_MAX_ADDR_CELLS];
  287. int i;
  288. /* Check address type match */
  289. if ((addr[0] ^ range[0]) & 0x03000000)
  290. return -EINVAL;
  291. if (of_out_of_range(addr + 1, range + 1, range + na + pna,
  292. na - 1, ns))
  293. return -EINVAL;
  294. /* Start with the parent range base. */
  295. memcpy(result, range + na, pna * 4);
  296. /* Add in the child address offset, skipping high cell. */
  297. for (i = 0; i < na - 1; i++)
  298. result[pna - 1 - i] +=
  299. (addr[na - 1 - i] -
  300. range[na - 1 - i]);
  301. memcpy(addr, result, pna * 4);
  302. return 0;
  303. }
  304. static unsigned int of_bus_pci_get_flags(u32 *addr)
  305. {
  306. unsigned int flags = 0;
  307. u32 w = addr[0];
  308. switch((w >> 24) & 0x03) {
  309. case 0x01:
  310. flags |= IORESOURCE_IO;
  311. case 0x02: /* 32 bits */
  312. case 0x03: /* 64 bits */
  313. flags |= IORESOURCE_MEM;
  314. }
  315. if (w & 0x40000000)
  316. flags |= IORESOURCE_PREFETCH;
  317. return flags;
  318. }
  319. /*
  320. * SBUS bus specific translator
  321. */
  322. static int of_bus_sbus_match(struct device_node *np)
  323. {
  324. return !strcmp(np->name, "sbus") ||
  325. !strcmp(np->name, "sbi");
  326. }
  327. static void of_bus_sbus_count_cells(struct device_node *child,
  328. int *addrc, int *sizec)
  329. {
  330. if (addrc)
  331. *addrc = 2;
  332. if (sizec)
  333. *sizec = 1;
  334. }
  335. /*
  336. * FHC/Central bus specific translator.
  337. *
  338. * This is just needed to hard-code the address and size cell
  339. * counts. 'fhc' and 'central' nodes lack the #address-cells and
  340. * #size-cells properties, and if you walk to the root on such
  341. * Enterprise boxes all you'll get is a #size-cells of 2 which is
  342. * not what we want to use.
  343. */
  344. static int of_bus_fhc_match(struct device_node *np)
  345. {
  346. return !strcmp(np->name, "fhc") ||
  347. !strcmp(np->name, "central");
  348. }
  349. #define of_bus_fhc_count_cells of_bus_sbus_count_cells
  350. /*
  351. * Array of bus specific translators
  352. */
  353. static struct of_bus of_busses[] = {
  354. /* PCI */
  355. {
  356. .name = "pci",
  357. .addr_prop_name = "assigned-addresses",
  358. .match = of_bus_pci_match,
  359. .count_cells = of_bus_pci_count_cells,
  360. .map = of_bus_pci_map,
  361. .get_flags = of_bus_pci_get_flags,
  362. },
  363. /* SBUS */
  364. {
  365. .name = "sbus",
  366. .addr_prop_name = "reg",
  367. .match = of_bus_sbus_match,
  368. .count_cells = of_bus_sbus_count_cells,
  369. .map = of_bus_default_map,
  370. .get_flags = of_bus_default_get_flags,
  371. },
  372. /* FHC */
  373. {
  374. .name = "fhc",
  375. .addr_prop_name = "reg",
  376. .match = of_bus_fhc_match,
  377. .count_cells = of_bus_fhc_count_cells,
  378. .map = of_bus_default_map,
  379. .get_flags = of_bus_default_get_flags,
  380. },
  381. /* Default */
  382. {
  383. .name = "default",
  384. .addr_prop_name = "reg",
  385. .match = NULL,
  386. .count_cells = of_bus_default_count_cells,
  387. .map = of_bus_default_map,
  388. .get_flags = of_bus_default_get_flags,
  389. },
  390. };
  391. static struct of_bus *of_match_bus(struct device_node *np)
  392. {
  393. int i;
  394. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  395. if (!of_busses[i].match || of_busses[i].match(np))
  396. return &of_busses[i];
  397. BUG();
  398. return NULL;
  399. }
  400. static int __init build_one_resource(struct device_node *parent,
  401. struct of_bus *bus,
  402. struct of_bus *pbus,
  403. u32 *addr,
  404. int na, int ns, int pna)
  405. {
  406. u32 *ranges;
  407. unsigned int rlen;
  408. int rone;
  409. ranges = of_get_property(parent, "ranges", &rlen);
  410. if (ranges == NULL || rlen == 0) {
  411. u32 result[OF_MAX_ADDR_CELLS];
  412. int i;
  413. memset(result, 0, pna * 4);
  414. for (i = 0; i < na; i++)
  415. result[pna - 1 - i] =
  416. addr[na - 1 - i];
  417. memcpy(addr, result, pna * 4);
  418. return 0;
  419. }
  420. /* Now walk through the ranges */
  421. rlen /= 4;
  422. rone = na + pna + ns;
  423. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  424. if (!bus->map(addr, ranges, na, ns, pna))
  425. return 0;
  426. }
  427. return 1;
  428. }
  429. static int __init use_1to1_mapping(struct device_node *pp)
  430. {
  431. char *model;
  432. /* If this is on the PMU bus, don't try to translate it even
  433. * if a ranges property exists.
  434. */
  435. if (!strcmp(pp->name, "pmu"))
  436. return 1;
  437. /* If we have a ranges property in the parent, use it. */
  438. if (of_find_property(pp, "ranges", NULL) != NULL)
  439. return 0;
  440. /* If the parent is the dma node of an ISA bus, pass
  441. * the translation up to the root.
  442. */
  443. if (!strcmp(pp->name, "dma"))
  444. return 0;
  445. /* Similarly for Simba PCI bridges. */
  446. model = of_get_property(pp, "model", NULL);
  447. if (model && !strcmp(model, "SUNW,simba"))
  448. return 0;
  449. return 1;
  450. }
  451. static int of_resource_verbose;
  452. static void __init build_device_resources(struct of_device *op,
  453. struct device *parent)
  454. {
  455. struct of_device *p_op;
  456. struct of_bus *bus;
  457. int na, ns;
  458. int index, num_reg;
  459. void *preg;
  460. if (!parent)
  461. return;
  462. p_op = to_of_device(parent);
  463. bus = of_match_bus(p_op->node);
  464. bus->count_cells(op->node, &na, &ns);
  465. preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
  466. if (!preg || num_reg == 0)
  467. return;
  468. /* Convert to num-cells. */
  469. num_reg /= 4;
  470. /* Convert to num-entries. */
  471. num_reg /= na + ns;
  472. /* Prevent overruning the op->resources[] array. */
  473. if (num_reg > PROMREG_MAX) {
  474. printk(KERN_WARNING "%s: Too many regs (%d), "
  475. "limiting to %d.\n",
  476. op->node->full_name, num_reg, PROMREG_MAX);
  477. num_reg = PROMREG_MAX;
  478. }
  479. for (index = 0; index < num_reg; index++) {
  480. struct resource *r = &op->resource[index];
  481. u32 addr[OF_MAX_ADDR_CELLS];
  482. u32 *reg = (preg + (index * ((na + ns) * 4)));
  483. struct device_node *dp = op->node;
  484. struct device_node *pp = p_op->node;
  485. struct of_bus *pbus;
  486. u64 size, result = OF_BAD_ADDR;
  487. unsigned long flags;
  488. int dna, dns;
  489. int pna, pns;
  490. size = of_read_addr(reg + na, ns);
  491. flags = bus->get_flags(reg);
  492. memcpy(addr, reg, na * 4);
  493. if (use_1to1_mapping(pp)) {
  494. result = of_read_addr(addr, na);
  495. goto build_res;
  496. }
  497. dna = na;
  498. dns = ns;
  499. while (1) {
  500. dp = pp;
  501. pp = dp->parent;
  502. if (!pp) {
  503. result = of_read_addr(addr, dna);
  504. break;
  505. }
  506. pbus = of_match_bus(pp);
  507. pbus->count_cells(dp, &pna, &pns);
  508. if (build_one_resource(dp, bus, pbus, addr,
  509. dna, dns, pna))
  510. break;
  511. dna = pna;
  512. dns = pns;
  513. bus = pbus;
  514. }
  515. build_res:
  516. memset(r, 0, sizeof(*r));
  517. if (of_resource_verbose)
  518. printk("%s reg[%d] -> %lx\n",
  519. op->node->full_name, index,
  520. result);
  521. if (result != OF_BAD_ADDR) {
  522. if (tlb_type == hypervisor)
  523. result &= 0x0fffffffffffffffUL;
  524. r->start = result;
  525. r->end = result + size - 1;
  526. r->flags = flags;
  527. } else {
  528. r->start = ~0UL;
  529. r->end = ~0UL;
  530. }
  531. r->name = op->node->name;
  532. }
  533. }
  534. static struct device_node * __init
  535. apply_interrupt_map(struct device_node *dp, struct device_node *pp,
  536. u32 *imap, int imlen, u32 *imask,
  537. unsigned int *irq_p)
  538. {
  539. struct device_node *cp;
  540. unsigned int irq = *irq_p;
  541. struct of_bus *bus;
  542. phandle handle;
  543. u32 *reg;
  544. int na, num_reg, i;
  545. bus = of_match_bus(pp);
  546. bus->count_cells(dp, &na, NULL);
  547. reg = of_get_property(dp, "reg", &num_reg);
  548. if (!reg || !num_reg)
  549. return NULL;
  550. imlen /= ((na + 3) * 4);
  551. handle = 0;
  552. for (i = 0; i < imlen; i++) {
  553. int j;
  554. for (j = 0; j < na; j++) {
  555. if ((reg[j] & imask[j]) != imap[j])
  556. goto next;
  557. }
  558. if (imap[na] == irq) {
  559. handle = imap[na + 1];
  560. irq = imap[na + 2];
  561. break;
  562. }
  563. next:
  564. imap += (na + 3);
  565. }
  566. if (i == imlen) {
  567. /* Psycho and Sabre PCI controllers can have 'interrupt-map'
  568. * properties that do not include the on-board device
  569. * interrupts. Instead, the device's 'interrupts' property
  570. * is already a fully specified INO value.
  571. *
  572. * Handle this by deciding that, if we didn't get a
  573. * match in the parent's 'interrupt-map', and the
  574. * parent is an IRQ translater, then use the parent as
  575. * our IRQ controller.
  576. */
  577. if (pp->irq_trans)
  578. return pp;
  579. return NULL;
  580. }
  581. *irq_p = irq;
  582. cp = of_find_node_by_phandle(handle);
  583. return cp;
  584. }
  585. static unsigned int __init pci_irq_swizzle(struct device_node *dp,
  586. struct device_node *pp,
  587. unsigned int irq)
  588. {
  589. struct linux_prom_pci_registers *regs;
  590. unsigned int devfn, slot, ret;
  591. if (irq < 1 || irq > 4)
  592. return irq;
  593. regs = of_get_property(dp, "reg", NULL);
  594. if (!regs)
  595. return irq;
  596. devfn = (regs->phys_hi >> 8) & 0xff;
  597. slot = (devfn >> 3) & 0x1f;
  598. ret = ((irq - 1 + (slot & 3)) & 3) + 1;
  599. return ret;
  600. }
  601. static int of_irq_verbose;
  602. static unsigned int __init build_one_device_irq(struct of_device *op,
  603. struct device *parent,
  604. unsigned int irq)
  605. {
  606. struct device_node *dp = op->node;
  607. struct device_node *pp, *ip;
  608. unsigned int orig_irq = irq;
  609. if (irq == 0xffffffff)
  610. return irq;
  611. if (dp->irq_trans) {
  612. irq = dp->irq_trans->irq_build(dp, irq,
  613. dp->irq_trans->data);
  614. if (of_irq_verbose)
  615. printk("%s: direct translate %x --> %x\n",
  616. dp->full_name, orig_irq, irq);
  617. return irq;
  618. }
  619. /* Something more complicated. Walk up to the root, applying
  620. * interrupt-map or bus specific translations, until we hit
  621. * an IRQ translator.
  622. *
  623. * If we hit a bus type or situation we cannot handle, we
  624. * stop and assume that the original IRQ number was in a
  625. * format which has special meaning to it's immediate parent.
  626. */
  627. pp = dp->parent;
  628. ip = NULL;
  629. while (pp) {
  630. void *imap, *imsk;
  631. int imlen;
  632. imap = of_get_property(pp, "interrupt-map", &imlen);
  633. imsk = of_get_property(pp, "interrupt-map-mask", NULL);
  634. if (imap && imsk) {
  635. struct device_node *iret;
  636. int this_orig_irq = irq;
  637. iret = apply_interrupt_map(dp, pp,
  638. imap, imlen, imsk,
  639. &irq);
  640. if (of_irq_verbose)
  641. printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
  642. op->node->full_name,
  643. pp->full_name, this_orig_irq,
  644. (iret ? iret->full_name : "NULL"), irq);
  645. if (!iret)
  646. break;
  647. if (iret->irq_trans) {
  648. ip = iret;
  649. break;
  650. }
  651. } else {
  652. if (!strcmp(pp->type, "pci") ||
  653. !strcmp(pp->type, "pciex")) {
  654. unsigned int this_orig_irq = irq;
  655. irq = pci_irq_swizzle(dp, pp, irq);
  656. if (of_irq_verbose)
  657. printk("%s: PCI swizzle [%s] "
  658. "%x --> %x\n",
  659. op->node->full_name,
  660. pp->full_name, this_orig_irq,
  661. irq);
  662. }
  663. if (pp->irq_trans) {
  664. ip = pp;
  665. break;
  666. }
  667. }
  668. dp = pp;
  669. pp = pp->parent;
  670. }
  671. if (!ip)
  672. return orig_irq;
  673. irq = ip->irq_trans->irq_build(op->node, irq,
  674. ip->irq_trans->data);
  675. if (of_irq_verbose)
  676. printk("%s: Apply IRQ trans [%s] %x --> %x\n",
  677. op->node->full_name, ip->full_name, orig_irq, irq);
  678. return irq;
  679. }
  680. static struct of_device * __init scan_one_device(struct device_node *dp,
  681. struct device *parent)
  682. {
  683. struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
  684. unsigned int *irq;
  685. int len, i;
  686. if (!op)
  687. return NULL;
  688. op->node = dp;
  689. op->clock_freq = of_getintprop_default(dp, "clock-frequency",
  690. (25*1000*1000));
  691. op->portid = of_getintprop_default(dp, "upa-portid", -1);
  692. if (op->portid == -1)
  693. op->portid = of_getintprop_default(dp, "portid", -1);
  694. irq = of_get_property(dp, "interrupts", &len);
  695. if (irq) {
  696. memcpy(op->irqs, irq, len);
  697. op->num_irqs = len / 4;
  698. } else {
  699. op->num_irqs = 0;
  700. }
  701. /* Prevent overruning the op->irqs[] array. */
  702. if (op->num_irqs > PROMINTR_MAX) {
  703. printk(KERN_WARNING "%s: Too many irqs (%d), "
  704. "limiting to %d.\n",
  705. dp->full_name, op->num_irqs, PROMINTR_MAX);
  706. op->num_irqs = PROMINTR_MAX;
  707. }
  708. build_device_resources(op, parent);
  709. for (i = 0; i < op->num_irqs; i++)
  710. op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
  711. op->dev.parent = parent;
  712. op->dev.bus = &of_bus_type;
  713. if (!parent)
  714. strcpy(op->dev.bus_id, "root");
  715. else
  716. sprintf(op->dev.bus_id, "%08x", dp->node);
  717. if (of_device_register(op)) {
  718. printk("%s: Could not register of device.\n",
  719. dp->full_name);
  720. kfree(op);
  721. op = NULL;
  722. }
  723. return op;
  724. }
  725. static void __init scan_tree(struct device_node *dp, struct device *parent)
  726. {
  727. while (dp) {
  728. struct of_device *op = scan_one_device(dp, parent);
  729. if (op)
  730. scan_tree(dp->child, &op->dev);
  731. dp = dp->sibling;
  732. }
  733. }
  734. static void __init scan_of_devices(void)
  735. {
  736. struct device_node *root = of_find_node_by_path("/");
  737. struct of_device *parent;
  738. parent = scan_one_device(root, NULL);
  739. if (!parent)
  740. return;
  741. scan_tree(root->child, &parent->dev);
  742. }
  743. static int __init of_bus_driver_init(void)
  744. {
  745. int err;
  746. err = bus_register(&of_bus_type);
  747. #ifdef CONFIG_PCI
  748. if (!err)
  749. err = bus_register(&isa_bus_type);
  750. if (!err)
  751. err = bus_register(&ebus_bus_type);
  752. #endif
  753. #ifdef CONFIG_SBUS
  754. if (!err)
  755. err = bus_register(&sbus_bus_type);
  756. #endif
  757. if (!err)
  758. scan_of_devices();
  759. return err;
  760. }
  761. postcore_initcall(of_bus_driver_init);
  762. static int __init of_debug(char *str)
  763. {
  764. int val = 0;
  765. get_option(&str, &val);
  766. if (val & 1)
  767. of_resource_verbose = 1;
  768. if (val & 2)
  769. of_irq_verbose = 1;
  770. return 1;
  771. }
  772. __setup("of_debug=", of_debug);
  773. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  774. {
  775. /* initialize common driver fields */
  776. drv->driver.name = drv->name;
  777. drv->driver.bus = bus;
  778. /* register with core */
  779. return driver_register(&drv->driver);
  780. }
  781. void of_unregister_driver(struct of_platform_driver *drv)
  782. {
  783. driver_unregister(&drv->driver);
  784. }
  785. static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  786. {
  787. struct of_device *ofdev;
  788. ofdev = to_of_device(dev);
  789. return sprintf(buf, "%s", ofdev->node->full_name);
  790. }
  791. static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
  792. /**
  793. * of_release_dev - free an of device structure when all users of it are finished.
  794. * @dev: device that's been disconnected
  795. *
  796. * Will be called only by the device core when all users of this of device are
  797. * done.
  798. */
  799. void of_release_dev(struct device *dev)
  800. {
  801. struct of_device *ofdev;
  802. ofdev = to_of_device(dev);
  803. kfree(ofdev);
  804. }
  805. int of_device_register(struct of_device *ofdev)
  806. {
  807. int rc;
  808. BUG_ON(ofdev->node == NULL);
  809. rc = device_register(&ofdev->dev);
  810. if (rc)
  811. return rc;
  812. rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
  813. if (rc)
  814. device_unregister(&ofdev->dev);
  815. return rc;
  816. }
  817. void of_device_unregister(struct of_device *ofdev)
  818. {
  819. device_remove_file(&ofdev->dev, &dev_attr_devspec);
  820. device_unregister(&ofdev->dev);
  821. }
  822. struct of_device* of_platform_device_create(struct device_node *np,
  823. const char *bus_id,
  824. struct device *parent,
  825. struct bus_type *bus)
  826. {
  827. struct of_device *dev;
  828. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  829. if (!dev)
  830. return NULL;
  831. memset(dev, 0, sizeof(*dev));
  832. dev->dev.parent = parent;
  833. dev->dev.bus = bus;
  834. dev->dev.release = of_release_dev;
  835. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  836. if (of_device_register(dev) != 0) {
  837. kfree(dev);
  838. return NULL;
  839. }
  840. return dev;
  841. }
  842. EXPORT_SYMBOL(of_match_device);
  843. EXPORT_SYMBOL(of_register_driver);
  844. EXPORT_SYMBOL(of_unregister_driver);
  845. EXPORT_SYMBOL(of_device_register);
  846. EXPORT_SYMBOL(of_device_unregister);
  847. EXPORT_SYMBOL(of_dev_get);
  848. EXPORT_SYMBOL(of_dev_put);
  849. EXPORT_SYMBOL(of_platform_device_create);
  850. EXPORT_SYMBOL(of_release_dev);