of_device.c 20 KB

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