of_device.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  336. {
  337. return of_bus_default_map(addr, range, na, ns, pna);
  338. }
  339. static unsigned int of_bus_sbus_get_flags(u32 *addr)
  340. {
  341. return IORESOURCE_MEM;
  342. }
  343. /*
  344. * Array of bus specific translators
  345. */
  346. static struct of_bus of_busses[] = {
  347. /* PCI */
  348. {
  349. .name = "pci",
  350. .addr_prop_name = "assigned-addresses",
  351. .match = of_bus_pci_match,
  352. .count_cells = of_bus_pci_count_cells,
  353. .map = of_bus_pci_map,
  354. .get_flags = of_bus_pci_get_flags,
  355. },
  356. /* SBUS */
  357. {
  358. .name = "sbus",
  359. .addr_prop_name = "reg",
  360. .match = of_bus_sbus_match,
  361. .count_cells = of_bus_sbus_count_cells,
  362. .map = of_bus_sbus_map,
  363. .get_flags = of_bus_sbus_get_flags,
  364. },
  365. /* Default */
  366. {
  367. .name = "default",
  368. .addr_prop_name = "reg",
  369. .match = NULL,
  370. .count_cells = of_bus_default_count_cells,
  371. .map = of_bus_default_map,
  372. .get_flags = of_bus_default_get_flags,
  373. },
  374. };
  375. static struct of_bus *of_match_bus(struct device_node *np)
  376. {
  377. int i;
  378. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  379. if (!of_busses[i].match || of_busses[i].match(np))
  380. return &of_busses[i];
  381. BUG();
  382. return NULL;
  383. }
  384. static int __init build_one_resource(struct device_node *parent,
  385. struct of_bus *bus,
  386. struct of_bus *pbus,
  387. u32 *addr,
  388. int na, int ns, int pna)
  389. {
  390. u32 *ranges;
  391. unsigned int rlen;
  392. int rone;
  393. ranges = of_get_property(parent, "ranges", &rlen);
  394. if (ranges == NULL || rlen == 0) {
  395. u32 result[OF_MAX_ADDR_CELLS];
  396. int i;
  397. memset(result, 0, pna * 4);
  398. for (i = 0; i < na; i++)
  399. result[pna - 1 - i] =
  400. addr[na - 1 - i];
  401. memcpy(addr, result, pna * 4);
  402. return 0;
  403. }
  404. /* Now walk through the ranges */
  405. rlen /= 4;
  406. rone = na + pna + ns;
  407. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  408. if (!bus->map(addr, ranges, na, ns, pna))
  409. return 0;
  410. }
  411. return 1;
  412. }
  413. static int __init use_1to1_mapping(struct device_node *pp)
  414. {
  415. char *model;
  416. /* If this is on the PMU bus, don't try to translate it even
  417. * if a ranges property exists.
  418. */
  419. if (!strcmp(pp->name, "pmu"))
  420. return 1;
  421. /* If we have a ranges property in the parent, use it. */
  422. if (of_find_property(pp, "ranges", NULL) != NULL)
  423. return 0;
  424. /* If the parent is the dma node of an ISA bus, pass
  425. * the translation up to the root.
  426. */
  427. if (!strcmp(pp->name, "dma"))
  428. return 0;
  429. /* Similarly for Simba PCI bridges. */
  430. model = of_get_property(pp, "model", NULL);
  431. if (model && !strcmp(model, "SUNW,simba"))
  432. return 0;
  433. return 1;
  434. }
  435. static int of_resource_verbose;
  436. static void __init build_device_resources(struct of_device *op,
  437. struct device *parent)
  438. {
  439. struct of_device *p_op;
  440. struct of_bus *bus;
  441. int na, ns;
  442. int index, num_reg;
  443. void *preg;
  444. if (!parent)
  445. return;
  446. p_op = to_of_device(parent);
  447. bus = of_match_bus(p_op->node);
  448. bus->count_cells(op->node, &na, &ns);
  449. preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
  450. if (!preg || num_reg == 0)
  451. return;
  452. /* Convert to num-cells. */
  453. num_reg /= 4;
  454. /* Convert to num-entries. */
  455. num_reg /= na + ns;
  456. /* Prevent overruning the op->resources[] array. */
  457. if (num_reg > PROMREG_MAX) {
  458. printk(KERN_WARNING "%s: Too many regs (%d), "
  459. "limiting to %d.\n",
  460. op->node->full_name, num_reg, PROMREG_MAX);
  461. num_reg = PROMREG_MAX;
  462. }
  463. for (index = 0; index < num_reg; index++) {
  464. struct resource *r = &op->resource[index];
  465. u32 addr[OF_MAX_ADDR_CELLS];
  466. u32 *reg = (preg + (index * ((na + ns) * 4)));
  467. struct device_node *dp = op->node;
  468. struct device_node *pp = p_op->node;
  469. struct of_bus *pbus;
  470. u64 size, result = OF_BAD_ADDR;
  471. unsigned long flags;
  472. int dna, dns;
  473. int pna, pns;
  474. size = of_read_addr(reg + na, ns);
  475. flags = bus->get_flags(reg);
  476. memcpy(addr, reg, na * 4);
  477. if (use_1to1_mapping(pp)) {
  478. result = of_read_addr(addr, na);
  479. goto build_res;
  480. }
  481. dna = na;
  482. dns = ns;
  483. while (1) {
  484. dp = pp;
  485. pp = dp->parent;
  486. if (!pp) {
  487. result = of_read_addr(addr, dna);
  488. break;
  489. }
  490. pbus = of_match_bus(pp);
  491. pbus->count_cells(dp, &pna, &pns);
  492. if (build_one_resource(dp, bus, pbus, addr,
  493. dna, dns, pna))
  494. break;
  495. dna = pna;
  496. dns = pns;
  497. bus = pbus;
  498. }
  499. build_res:
  500. memset(r, 0, sizeof(*r));
  501. if (of_resource_verbose)
  502. printk("%s reg[%d] -> %lx\n",
  503. op->node->full_name, index,
  504. result);
  505. if (result != OF_BAD_ADDR) {
  506. if (tlb_type == hypervisor)
  507. result &= 0x0fffffffffffffffUL;
  508. r->start = result;
  509. r->end = result + size - 1;
  510. r->flags = flags;
  511. } else {
  512. r->start = ~0UL;
  513. r->end = ~0UL;
  514. }
  515. r->name = op->node->name;
  516. }
  517. }
  518. static struct device_node * __init
  519. apply_interrupt_map(struct device_node *dp, struct device_node *pp,
  520. u32 *imap, int imlen, u32 *imask,
  521. unsigned int *irq_p)
  522. {
  523. struct device_node *cp;
  524. unsigned int irq = *irq_p;
  525. struct of_bus *bus;
  526. phandle handle;
  527. u32 *reg;
  528. int na, num_reg, i;
  529. bus = of_match_bus(pp);
  530. bus->count_cells(dp, &na, NULL);
  531. reg = of_get_property(dp, "reg", &num_reg);
  532. if (!reg || !num_reg)
  533. return NULL;
  534. imlen /= ((na + 3) * 4);
  535. handle = 0;
  536. for (i = 0; i < imlen; i++) {
  537. int j;
  538. for (j = 0; j < na; j++) {
  539. if ((reg[j] & imask[j]) != imap[j])
  540. goto next;
  541. }
  542. if (imap[na] == irq) {
  543. handle = imap[na + 1];
  544. irq = imap[na + 2];
  545. break;
  546. }
  547. next:
  548. imap += (na + 3);
  549. }
  550. if (i == imlen) {
  551. /* Psycho and Sabre PCI controllers can have 'interrupt-map'
  552. * properties that do not include the on-board device
  553. * interrupts. Instead, the device's 'interrupts' property
  554. * is already a fully specified INO value.
  555. *
  556. * Handle this by deciding that, if we didn't get a
  557. * match in the parent's 'interrupt-map', and the
  558. * parent is an IRQ translater, then use the parent as
  559. * our IRQ controller.
  560. */
  561. if (pp->irq_trans)
  562. return pp;
  563. return NULL;
  564. }
  565. *irq_p = irq;
  566. cp = of_find_node_by_phandle(handle);
  567. return cp;
  568. }
  569. static unsigned int __init pci_irq_swizzle(struct device_node *dp,
  570. struct device_node *pp,
  571. unsigned int irq)
  572. {
  573. struct linux_prom_pci_registers *regs;
  574. unsigned int devfn, slot, ret;
  575. if (irq < 1 || irq > 4)
  576. return irq;
  577. regs = of_get_property(dp, "reg", NULL);
  578. if (!regs)
  579. return irq;
  580. devfn = (regs->phys_hi >> 8) & 0xff;
  581. slot = (devfn >> 3) & 0x1f;
  582. ret = ((irq - 1 + (slot & 3)) & 3) + 1;
  583. return ret;
  584. }
  585. static int of_irq_verbose;
  586. static unsigned int __init build_one_device_irq(struct of_device *op,
  587. struct device *parent,
  588. unsigned int irq)
  589. {
  590. struct device_node *dp = op->node;
  591. struct device_node *pp, *ip;
  592. unsigned int orig_irq = irq;
  593. if (irq == 0xffffffff)
  594. return irq;
  595. if (dp->irq_trans) {
  596. irq = dp->irq_trans->irq_build(dp, irq,
  597. dp->irq_trans->data);
  598. if (of_irq_verbose)
  599. printk("%s: direct translate %x --> %x\n",
  600. dp->full_name, orig_irq, irq);
  601. return irq;
  602. }
  603. /* Something more complicated. Walk up to the root, applying
  604. * interrupt-map or bus specific translations, until we hit
  605. * an IRQ translator.
  606. *
  607. * If we hit a bus type or situation we cannot handle, we
  608. * stop and assume that the original IRQ number was in a
  609. * format which has special meaning to it's immediate parent.
  610. */
  611. pp = dp->parent;
  612. ip = NULL;
  613. while (pp) {
  614. void *imap, *imsk;
  615. int imlen;
  616. imap = of_get_property(pp, "interrupt-map", &imlen);
  617. imsk = of_get_property(pp, "interrupt-map-mask", NULL);
  618. if (imap && imsk) {
  619. struct device_node *iret;
  620. int this_orig_irq = irq;
  621. iret = apply_interrupt_map(dp, pp,
  622. imap, imlen, imsk,
  623. &irq);
  624. if (of_irq_verbose)
  625. printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
  626. op->node->full_name,
  627. pp->full_name, this_orig_irq,
  628. (iret ? iret->full_name : "NULL"), irq);
  629. if (!iret)
  630. break;
  631. if (iret->irq_trans) {
  632. ip = iret;
  633. break;
  634. }
  635. } else {
  636. if (!strcmp(pp->type, "pci") ||
  637. !strcmp(pp->type, "pciex")) {
  638. unsigned int this_orig_irq = irq;
  639. irq = pci_irq_swizzle(dp, pp, irq);
  640. if (of_irq_verbose)
  641. printk("%s: PCI swizzle [%s] "
  642. "%x --> %x\n",
  643. op->node->full_name,
  644. pp->full_name, this_orig_irq,
  645. irq);
  646. }
  647. if (pp->irq_trans) {
  648. ip = pp;
  649. break;
  650. }
  651. }
  652. dp = pp;
  653. pp = pp->parent;
  654. }
  655. if (!ip)
  656. return orig_irq;
  657. irq = ip->irq_trans->irq_build(op->node, irq,
  658. ip->irq_trans->data);
  659. if (of_irq_verbose)
  660. printk("%s: Apply IRQ trans [%s] %x --> %x\n",
  661. op->node->full_name, ip->full_name, orig_irq, irq);
  662. return irq;
  663. }
  664. static struct of_device * __init scan_one_device(struct device_node *dp,
  665. struct device *parent)
  666. {
  667. struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
  668. unsigned int *irq;
  669. int len, i;
  670. if (!op)
  671. return NULL;
  672. op->node = dp;
  673. op->clock_freq = of_getintprop_default(dp, "clock-frequency",
  674. (25*1000*1000));
  675. op->portid = of_getintprop_default(dp, "upa-portid", -1);
  676. if (op->portid == -1)
  677. op->portid = of_getintprop_default(dp, "portid", -1);
  678. irq = of_get_property(dp, "interrupts", &len);
  679. if (irq) {
  680. memcpy(op->irqs, irq, len);
  681. op->num_irqs = len / 4;
  682. } else {
  683. op->num_irqs = 0;
  684. }
  685. /* Prevent overruning the op->irqs[] array. */
  686. if (op->num_irqs > PROMINTR_MAX) {
  687. printk(KERN_WARNING "%s: Too many irqs (%d), "
  688. "limiting to %d.\n",
  689. dp->full_name, op->num_irqs, PROMINTR_MAX);
  690. op->num_irqs = PROMINTR_MAX;
  691. }
  692. build_device_resources(op, parent);
  693. for (i = 0; i < op->num_irqs; i++)
  694. op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
  695. op->dev.parent = parent;
  696. op->dev.bus = &of_bus_type;
  697. if (!parent)
  698. strcpy(op->dev.bus_id, "root");
  699. else
  700. sprintf(op->dev.bus_id, "%s@%08x", dp->name, dp->node);
  701. if (of_device_register(op)) {
  702. printk("%s: Could not register of device.\n",
  703. dp->full_name);
  704. kfree(op);
  705. op = NULL;
  706. }
  707. return op;
  708. }
  709. static void __init scan_tree(struct device_node *dp, struct device *parent)
  710. {
  711. while (dp) {
  712. struct of_device *op = scan_one_device(dp, parent);
  713. if (op)
  714. scan_tree(dp->child, &op->dev);
  715. dp = dp->sibling;
  716. }
  717. }
  718. static void __init scan_of_devices(void)
  719. {
  720. struct device_node *root = of_find_node_by_path("/");
  721. struct of_device *parent;
  722. parent = scan_one_device(root, NULL);
  723. if (!parent)
  724. return;
  725. scan_tree(root->child, &parent->dev);
  726. }
  727. static int __init of_bus_driver_init(void)
  728. {
  729. int err;
  730. err = bus_register(&of_bus_type);
  731. #ifdef CONFIG_PCI
  732. if (!err)
  733. err = bus_register(&isa_bus_type);
  734. if (!err)
  735. err = bus_register(&ebus_bus_type);
  736. #endif
  737. #ifdef CONFIG_SBUS
  738. if (!err)
  739. err = bus_register(&sbus_bus_type);
  740. #endif
  741. if (!err)
  742. scan_of_devices();
  743. return err;
  744. }
  745. postcore_initcall(of_bus_driver_init);
  746. static int __init of_debug(char *str)
  747. {
  748. int val = 0;
  749. get_option(&str, &val);
  750. if (val & 1)
  751. of_resource_verbose = 1;
  752. if (val & 2)
  753. of_irq_verbose = 1;
  754. return 1;
  755. }
  756. __setup("of_debug=", of_debug);
  757. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  758. {
  759. /* initialize common driver fields */
  760. drv->driver.name = drv->name;
  761. drv->driver.bus = bus;
  762. /* register with core */
  763. return driver_register(&drv->driver);
  764. }
  765. void of_unregister_driver(struct of_platform_driver *drv)
  766. {
  767. driver_unregister(&drv->driver);
  768. }
  769. static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  770. {
  771. struct of_device *ofdev;
  772. ofdev = to_of_device(dev);
  773. return sprintf(buf, "%s", ofdev->node->full_name);
  774. }
  775. static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
  776. /**
  777. * of_release_dev - free an of device structure when all users of it are finished.
  778. * @dev: device that's been disconnected
  779. *
  780. * Will be called only by the device core when all users of this of device are
  781. * done.
  782. */
  783. void of_release_dev(struct device *dev)
  784. {
  785. struct of_device *ofdev;
  786. ofdev = to_of_device(dev);
  787. kfree(ofdev);
  788. }
  789. int of_device_register(struct of_device *ofdev)
  790. {
  791. int rc;
  792. BUG_ON(ofdev->node == NULL);
  793. rc = device_register(&ofdev->dev);
  794. if (rc)
  795. return rc;
  796. rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
  797. if (rc)
  798. device_unregister(&ofdev->dev);
  799. return rc;
  800. }
  801. void of_device_unregister(struct of_device *ofdev)
  802. {
  803. device_remove_file(&ofdev->dev, &dev_attr_devspec);
  804. device_unregister(&ofdev->dev);
  805. }
  806. struct of_device* of_platform_device_create(struct device_node *np,
  807. const char *bus_id,
  808. struct device *parent,
  809. struct bus_type *bus)
  810. {
  811. struct of_device *dev;
  812. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  813. if (!dev)
  814. return NULL;
  815. memset(dev, 0, sizeof(*dev));
  816. dev->dev.parent = parent;
  817. dev->dev.bus = bus;
  818. dev->dev.release = of_release_dev;
  819. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  820. if (of_device_register(dev) != 0) {
  821. kfree(dev);
  822. return NULL;
  823. }
  824. return dev;
  825. }
  826. EXPORT_SYMBOL(of_match_device);
  827. EXPORT_SYMBOL(of_register_driver);
  828. EXPORT_SYMBOL(of_unregister_driver);
  829. EXPORT_SYMBOL(of_device_register);
  830. EXPORT_SYMBOL(of_device_unregister);
  831. EXPORT_SYMBOL(of_dev_get);
  832. EXPORT_SYMBOL(of_dev_put);
  833. EXPORT_SYMBOL(of_platform_device_create);
  834. EXPORT_SYMBOL(of_release_dev);