of_device.c 21 KB

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