of_device.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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(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. u64 (*map)(u32 *addr, u32 *range, int na, int ns, int pna);
  202. int (*translate)(u32 *addr, u64 offset, int na);
  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. static u64 of_bus_default_map(u32 *addr, u32 *range, int na, int ns, int pna)
  214. {
  215. u64 cp, s, da;
  216. cp = of_read_addr(range, na);
  217. s = of_read_addr(range + na + pna, ns);
  218. da = of_read_addr(addr, na);
  219. if (da < cp || da >= (cp + s))
  220. return OF_BAD_ADDR;
  221. return da - cp;
  222. }
  223. static int of_bus_default_translate(u32 *addr, u64 offset, int na)
  224. {
  225. u64 a = of_read_addr(addr, na);
  226. memset(addr, 0, na * 4);
  227. a += offset;
  228. if (na > 1)
  229. addr[na - 2] = a >> 32;
  230. addr[na - 1] = a & 0xffffffffu;
  231. return 0;
  232. }
  233. static unsigned int of_bus_default_get_flags(u32 *addr)
  234. {
  235. return IORESOURCE_MEM;
  236. }
  237. /*
  238. * PCI bus specific translator
  239. */
  240. static int of_bus_pci_match(struct device_node *np)
  241. {
  242. return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex");
  243. }
  244. static void of_bus_pci_count_cells(struct device_node *np,
  245. int *addrc, int *sizec)
  246. {
  247. if (addrc)
  248. *addrc = 3;
  249. if (sizec)
  250. *sizec = 2;
  251. }
  252. static u64 of_bus_pci_map(u32 *addr, u32 *range, int na, int ns, int pna)
  253. {
  254. u64 cp, s, da;
  255. /* Check address type match */
  256. if ((addr[0] ^ range[0]) & 0x03000000)
  257. return OF_BAD_ADDR;
  258. /* Read address values, skipping high cell */
  259. cp = of_read_addr(range + 1, na - 1);
  260. s = of_read_addr(range + na + pna, ns);
  261. da = of_read_addr(addr + 1, na - 1);
  262. if (da < cp || da >= (cp + s))
  263. return OF_BAD_ADDR;
  264. return da - cp;
  265. }
  266. static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
  267. {
  268. return of_bus_default_translate(addr + 1, offset, na - 1);
  269. }
  270. static unsigned int of_bus_pci_get_flags(u32 *addr)
  271. {
  272. unsigned int flags = 0;
  273. u32 w = addr[0];
  274. switch((w >> 24) & 0x03) {
  275. case 0x01:
  276. flags |= IORESOURCE_IO;
  277. case 0x02: /* 32 bits */
  278. case 0x03: /* 64 bits */
  279. flags |= IORESOURCE_MEM;
  280. }
  281. if (w & 0x40000000)
  282. flags |= IORESOURCE_PREFETCH;
  283. return flags;
  284. }
  285. /*
  286. * ISA bus specific translator
  287. */
  288. static int of_bus_isa_match(struct device_node *np)
  289. {
  290. return !strcmp(np->name, "isa");
  291. }
  292. static void of_bus_isa_count_cells(struct device_node *child,
  293. int *addrc, int *sizec)
  294. {
  295. if (addrc)
  296. *addrc = 2;
  297. if (sizec)
  298. *sizec = 1;
  299. }
  300. static u64 of_bus_isa_map(u32 *addr, u32 *range, int na, int ns, int pna)
  301. {
  302. u64 cp, s, da;
  303. /* Check address type match */
  304. if ((addr[0] ^ range[0]) & 0x00000001)
  305. return OF_BAD_ADDR;
  306. /* Read address values, skipping high cell */
  307. cp = of_read_addr(range + 1, na - 1);
  308. s = of_read_addr(range + na + pna, ns);
  309. da = of_read_addr(addr + 1, na - 1);
  310. if (da < cp || da >= (cp + s))
  311. return OF_BAD_ADDR;
  312. return da - cp;
  313. }
  314. static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
  315. {
  316. return of_bus_default_translate(addr + 1, offset, na - 1);
  317. }
  318. static unsigned int of_bus_isa_get_flags(u32 *addr)
  319. {
  320. unsigned int flags = 0;
  321. u32 w = addr[0];
  322. if (w & 1)
  323. flags |= IORESOURCE_IO;
  324. else
  325. flags |= IORESOURCE_MEM;
  326. return flags;
  327. }
  328. /*
  329. * SBUS bus specific translator
  330. */
  331. static int of_bus_sbus_match(struct device_node *np)
  332. {
  333. return !strcmp(np->name, "sbus") ||
  334. !strcmp(np->name, "sbi");
  335. }
  336. static void of_bus_sbus_count_cells(struct device_node *child,
  337. int *addrc, int *sizec)
  338. {
  339. if (addrc)
  340. *addrc = 2;
  341. if (sizec)
  342. *sizec = 1;
  343. }
  344. static u64 of_bus_sbus_map(u32 *addr, u32 *range, int na, int ns, int pna)
  345. {
  346. return of_bus_default_map(addr, range, na, ns, pna);
  347. }
  348. static int of_bus_sbus_translate(u32 *addr, u64 offset, int na)
  349. {
  350. return of_bus_default_translate(addr, offset, na);
  351. }
  352. static unsigned int of_bus_sbus_get_flags(u32 *addr)
  353. {
  354. return IORESOURCE_MEM;
  355. }
  356. /*
  357. * Array of bus specific translators
  358. */
  359. static struct of_bus of_busses[] = {
  360. /* PCI */
  361. {
  362. .name = "pci",
  363. .addr_prop_name = "assigned-addresses",
  364. .match = of_bus_pci_match,
  365. .count_cells = of_bus_pci_count_cells,
  366. .map = of_bus_pci_map,
  367. .translate = of_bus_pci_translate,
  368. .get_flags = of_bus_pci_get_flags,
  369. },
  370. /* ISA */
  371. {
  372. .name = "isa",
  373. .addr_prop_name = "reg",
  374. .match = of_bus_isa_match,
  375. .count_cells = of_bus_isa_count_cells,
  376. .map = of_bus_isa_map,
  377. .translate = of_bus_isa_translate,
  378. .get_flags = of_bus_isa_get_flags,
  379. },
  380. /* SBUS */
  381. {
  382. .name = "sbus",
  383. .addr_prop_name = "reg",
  384. .match = of_bus_sbus_match,
  385. .count_cells = of_bus_sbus_count_cells,
  386. .map = of_bus_sbus_map,
  387. .translate = of_bus_sbus_translate,
  388. .get_flags = of_bus_sbus_get_flags,
  389. },
  390. /* Default */
  391. {
  392. .name = "default",
  393. .addr_prop_name = "reg",
  394. .match = NULL,
  395. .count_cells = of_bus_default_count_cells,
  396. .map = of_bus_default_map,
  397. .translate = of_bus_default_translate,
  398. .get_flags = of_bus_default_get_flags,
  399. },
  400. };
  401. static struct of_bus *of_match_bus(struct device_node *np)
  402. {
  403. int i;
  404. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  405. if (!of_busses[i].match || of_busses[i].match(np))
  406. return &of_busses[i];
  407. BUG();
  408. return NULL;
  409. }
  410. static int __init build_one_resource(struct device_node *parent,
  411. struct of_bus *bus,
  412. struct of_bus *pbus,
  413. u32 *addr,
  414. int na, int ns, int pna)
  415. {
  416. u32 *ranges;
  417. unsigned int rlen;
  418. int rone;
  419. u64 offset = OF_BAD_ADDR;
  420. ranges = of_get_property(parent, "ranges", &rlen);
  421. if (ranges == NULL || rlen == 0) {
  422. offset = of_read_addr(addr, na);
  423. memset(addr, 0, pna * 4);
  424. goto finish;
  425. }
  426. /* Now walk through the ranges */
  427. rlen /= 4;
  428. rone = na + pna + ns;
  429. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  430. offset = bus->map(addr, ranges, na, ns, pna);
  431. if (offset != OF_BAD_ADDR)
  432. break;
  433. }
  434. if (offset == OF_BAD_ADDR)
  435. return 1;
  436. memcpy(addr, ranges + na, 4 * pna);
  437. finish:
  438. /* Translate it into parent bus space */
  439. return pbus->translate(addr, offset, pna);
  440. }
  441. static void __init build_device_resources(struct of_device *op,
  442. struct device *parent)
  443. {
  444. struct of_device *p_op;
  445. struct of_bus *bus;
  446. int na, ns;
  447. int index, num_reg;
  448. void *preg;
  449. if (!parent)
  450. return;
  451. p_op = to_of_device(parent);
  452. bus = of_match_bus(p_op->node);
  453. bus->count_cells(op->node, &na, &ns);
  454. preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
  455. if (!preg || num_reg == 0)
  456. return;
  457. /* Convert to num-cells. */
  458. num_reg /= 4;
  459. /* Conver to num-entries. */
  460. num_reg /= na + ns;
  461. for (index = 0; index < num_reg; index++) {
  462. struct resource *r = &op->resource[index];
  463. u32 addr[OF_MAX_ADDR_CELLS];
  464. u32 *reg = (preg + (index * ((na + ns) * 4)));
  465. struct device_node *dp = op->node;
  466. struct device_node *pp = p_op->node;
  467. struct of_bus *pbus;
  468. u64 size, result = OF_BAD_ADDR;
  469. unsigned long flags;
  470. int dna, dns;
  471. int pna, pns;
  472. size = of_read_addr(reg + na, ns);
  473. flags = bus->get_flags(reg);
  474. memcpy(addr, reg, na * 4);
  475. /* If the immediate parent has no ranges property to apply,
  476. * just use a 1<->1 mapping. Unless it is the 'dma' child
  477. * of an isa bus, which must be passed up towards the root.
  478. *
  479. * Also, don't try to translate PMU bus device registers.
  480. */
  481. if ((of_find_property(pp, "ranges", NULL) == NULL &&
  482. strcmp(pp->name, "dma") != 0) ||
  483. !strcmp(pp->name, "pmu")) {
  484. result = of_read_addr(addr, na);
  485. goto build_res;
  486. }
  487. dna = na;
  488. dns = ns;
  489. while (1) {
  490. dp = pp;
  491. pp = dp->parent;
  492. if (!pp) {
  493. result = of_read_addr(addr, dna);
  494. break;
  495. }
  496. pbus = of_match_bus(pp);
  497. pbus->count_cells(dp, &pna, &pns);
  498. if (build_one_resource(dp, bus, pbus, addr, dna, dns, pna))
  499. break;
  500. dna = pna;
  501. dns = pns;
  502. bus = pbus;
  503. }
  504. build_res:
  505. memset(r, 0, sizeof(*r));
  506. if (result != OF_BAD_ADDR) {
  507. if (tlb_type == hypervisor)
  508. result &= 0x0fffffffffffffffUL;
  509. r->start = result;
  510. r->end = result + size - 1;
  511. r->flags = flags;
  512. } else {
  513. r->start = ~0UL;
  514. r->end = ~0UL;
  515. }
  516. r->name = op->node->name;
  517. }
  518. }
  519. static struct device_node * __init
  520. apply_interrupt_map(struct device_node *dp, struct device_node *pp,
  521. u32 *imap, int imlen, u32 *imask,
  522. unsigned int *irq_p)
  523. {
  524. struct device_node *cp;
  525. unsigned int irq = *irq_p;
  526. struct of_bus *bus;
  527. phandle handle;
  528. u32 *reg;
  529. int na, num_reg, i;
  530. bus = of_match_bus(pp);
  531. bus->count_cells(dp, &na, NULL);
  532. reg = of_get_property(dp, "reg", &num_reg);
  533. if (!reg || !num_reg)
  534. return NULL;
  535. imlen /= ((na + 3) * 4);
  536. handle = 0;
  537. for (i = 0; i < imlen; i++) {
  538. int j;
  539. for (j = 0; j < na; j++) {
  540. if ((reg[j] & imask[j]) != imap[j])
  541. goto next;
  542. }
  543. if (imap[na] == irq) {
  544. handle = imap[na + 1];
  545. irq = imap[na + 2];
  546. break;
  547. }
  548. next:
  549. imap += (na + 3);
  550. }
  551. if (i == imlen)
  552. return NULL;
  553. *irq_p = irq;
  554. cp = of_find_node_by_phandle(handle);
  555. return cp;
  556. }
  557. static unsigned int __init pci_irq_swizzle(struct device_node *dp,
  558. struct device_node *pp,
  559. unsigned int irq)
  560. {
  561. struct linux_prom_pci_registers *regs;
  562. unsigned int devfn, slot, ret;
  563. if (irq < 1 || irq > 4)
  564. return irq;
  565. regs = of_get_property(dp, "reg", NULL);
  566. if (!regs)
  567. return irq;
  568. devfn = (regs->phys_hi >> 8) & 0xff;
  569. slot = (devfn >> 3) & 0x1f;
  570. ret = ((irq - 1 + (slot & 3)) & 3) + 1;
  571. return ret;
  572. }
  573. static unsigned int __init build_one_device_irq(struct of_device *op,
  574. struct device *parent,
  575. unsigned int irq)
  576. {
  577. struct device_node *dp = op->node;
  578. struct device_node *pp, *ip;
  579. unsigned int orig_irq = irq;
  580. if (irq == 0xffffffff)
  581. return irq;
  582. if (dp->irq_trans) {
  583. irq = dp->irq_trans->irq_build(dp, irq,
  584. dp->irq_trans->data);
  585. #if 1
  586. printk("%s: direct translate %x --> %x\n",
  587. dp->full_name, orig_irq, irq);
  588. #endif
  589. return irq;
  590. }
  591. /* Something more complicated. Walk up to the root, applying
  592. * interrupt-map or bus specific translations, until we hit
  593. * an IRQ translator.
  594. *
  595. * If we hit a bus type or situation we cannot handle, we
  596. * stop and assume that the original IRQ number was in a
  597. * format which has special meaning to it's immediate parent.
  598. */
  599. pp = dp->parent;
  600. ip = NULL;
  601. while (pp) {
  602. void *imap, *imsk;
  603. int imlen;
  604. imap = of_get_property(pp, "interrupt-map", &imlen);
  605. imsk = of_get_property(pp, "interrupt-map-mask", NULL);
  606. if (imap && imsk) {
  607. struct device_node *iret;
  608. int this_orig_irq = irq;
  609. iret = apply_interrupt_map(dp, pp,
  610. imap, imlen, imsk,
  611. &irq);
  612. #if 1
  613. printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
  614. op->node->full_name,
  615. pp->full_name, this_orig_irq,
  616. (iret ? iret->full_name : "NULL"), irq);
  617. #endif
  618. if (!iret)
  619. break;
  620. if (iret->irq_trans) {
  621. ip = iret;
  622. break;
  623. }
  624. } else {
  625. if (!strcmp(pp->type, "pci") ||
  626. !strcmp(pp->type, "pciex")) {
  627. unsigned int this_orig_irq = irq;
  628. irq = pci_irq_swizzle(dp, pp, irq);
  629. #if 1
  630. printk("%s: PCI swizzle [%s] %x --> %x\n",
  631. op->node->full_name,
  632. pp->full_name, this_orig_irq, irq);
  633. #endif
  634. }
  635. if (pp->irq_trans) {
  636. ip = pp;
  637. break;
  638. }
  639. }
  640. dp = pp;
  641. pp = pp->parent;
  642. }
  643. if (!ip)
  644. return orig_irq;
  645. irq = ip->irq_trans->irq_build(op->node, irq,
  646. ip->irq_trans->data);
  647. #if 1
  648. printk("%s: Apply IRQ trans [%s] %x --> %x\n",
  649. op->node->full_name, ip->full_name, orig_irq, irq);
  650. #endif
  651. return irq;
  652. }
  653. static struct of_device * __init scan_one_device(struct device_node *dp,
  654. struct device *parent)
  655. {
  656. struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
  657. unsigned int *irq;
  658. int len, i;
  659. if (!op)
  660. return NULL;
  661. op->node = dp;
  662. op->clock_freq = of_getintprop_default(dp, "clock-frequency",
  663. (25*1000*1000));
  664. op->portid = of_getintprop_default(dp, "upa-portid", -1);
  665. if (op->portid == -1)
  666. op->portid = of_getintprop_default(dp, "portid", -1);
  667. irq = of_get_property(dp, "interrupts", &len);
  668. if (irq) {
  669. memcpy(op->irqs, irq, len);
  670. op->num_irqs = len / 4;
  671. } else {
  672. op->num_irqs = 0;
  673. }
  674. build_device_resources(op, parent);
  675. for (i = 0; i < op->num_irqs; i++)
  676. op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
  677. op->dev.parent = parent;
  678. op->dev.bus = &of_bus_type;
  679. if (!parent)
  680. strcpy(op->dev.bus_id, "root");
  681. else
  682. strcpy(op->dev.bus_id, dp->path_component_name);
  683. if (of_device_register(op)) {
  684. printk("%s: Could not register of device.\n",
  685. dp->full_name);
  686. kfree(op);
  687. op = NULL;
  688. }
  689. return op;
  690. }
  691. static void __init scan_tree(struct device_node *dp, struct device *parent)
  692. {
  693. while (dp) {
  694. struct of_device *op = scan_one_device(dp, parent);
  695. if (op)
  696. scan_tree(dp->child, &op->dev);
  697. dp = dp->sibling;
  698. }
  699. }
  700. static void __init scan_of_devices(void)
  701. {
  702. struct device_node *root = of_find_node_by_path("/");
  703. struct of_device *parent;
  704. parent = scan_one_device(root, NULL);
  705. if (!parent)
  706. return;
  707. scan_tree(root->child, &parent->dev);
  708. }
  709. static int __init of_bus_driver_init(void)
  710. {
  711. int err;
  712. err = bus_register(&of_bus_type);
  713. #ifdef CONFIG_PCI
  714. if (!err)
  715. err = bus_register(&isa_bus_type);
  716. if (!err)
  717. err = bus_register(&ebus_bus_type);
  718. #endif
  719. #ifdef CONFIG_SBUS
  720. if (!err)
  721. err = bus_register(&sbus_bus_type);
  722. #endif
  723. if (!err)
  724. scan_of_devices();
  725. return err;
  726. }
  727. postcore_initcall(of_bus_driver_init);
  728. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  729. {
  730. /* initialize common driver fields */
  731. drv->driver.name = drv->name;
  732. drv->driver.bus = bus;
  733. /* register with core */
  734. return driver_register(&drv->driver);
  735. }
  736. void of_unregister_driver(struct of_platform_driver *drv)
  737. {
  738. driver_unregister(&drv->driver);
  739. }
  740. static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  741. {
  742. struct of_device *ofdev;
  743. ofdev = to_of_device(dev);
  744. return sprintf(buf, "%s", ofdev->node->full_name);
  745. }
  746. static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
  747. /**
  748. * of_release_dev - free an of device structure when all users of it are finished.
  749. * @dev: device that's been disconnected
  750. *
  751. * Will be called only by the device core when all users of this of device are
  752. * done.
  753. */
  754. void of_release_dev(struct device *dev)
  755. {
  756. struct of_device *ofdev;
  757. ofdev = to_of_device(dev);
  758. kfree(ofdev);
  759. }
  760. int of_device_register(struct of_device *ofdev)
  761. {
  762. int rc;
  763. BUG_ON(ofdev->node == NULL);
  764. rc = device_register(&ofdev->dev);
  765. if (rc)
  766. return rc;
  767. device_create_file(&ofdev->dev, &dev_attr_devspec);
  768. return 0;
  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);