of_device.c 21 KB

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