of_device.c 22 KB

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