of_device.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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)(const 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(const 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. const char *model = of_get_property(np, "model", NULL);
  267. if (model && !strcmp(model, "SUNW,simba"))
  268. return 0;
  269. /* Do not do PCI specific frobbing if the
  270. * PCI bridge lacks a ranges property. We
  271. * want to pass it through up to the next
  272. * parent as-is, not with the PCI translate
  273. * method which chops off the top address cell.
  274. */
  275. if (!of_find_property(np, "ranges", NULL))
  276. return 0;
  277. return 1;
  278. }
  279. return 0;
  280. }
  281. static int of_bus_simba_match(struct device_node *np)
  282. {
  283. const char *model = of_get_property(np, "model", NULL);
  284. if (model && !strcmp(model, "SUNW,simba"))
  285. return 1;
  286. return 0;
  287. }
  288. static int of_bus_simba_map(u32 *addr, const u32 *range,
  289. int na, int ns, int pna)
  290. {
  291. return 0;
  292. }
  293. static void of_bus_pci_count_cells(struct device_node *np,
  294. int *addrc, int *sizec)
  295. {
  296. if (addrc)
  297. *addrc = 3;
  298. if (sizec)
  299. *sizec = 2;
  300. }
  301. static int of_bus_pci_map(u32 *addr, const u32 *range,
  302. int na, int ns, int pna)
  303. {
  304. u32 result[OF_MAX_ADDR_CELLS];
  305. int i;
  306. /* Check address type match */
  307. if ((addr[0] ^ range[0]) & 0x03000000)
  308. return -EINVAL;
  309. if (of_out_of_range(addr + 1, range + 1, range + na + pna,
  310. na - 1, ns))
  311. return -EINVAL;
  312. /* Start with the parent range base. */
  313. memcpy(result, range + na, pna * 4);
  314. /* Add in the child address offset, skipping high cell. */
  315. for (i = 0; i < na - 1; i++)
  316. result[pna - 1 - i] +=
  317. (addr[na - 1 - i] -
  318. range[na - 1 - i]);
  319. memcpy(addr, result, pna * 4);
  320. return 0;
  321. }
  322. static unsigned int of_bus_pci_get_flags(const u32 *addr)
  323. {
  324. unsigned int flags = 0;
  325. u32 w = addr[0];
  326. switch((w >> 24) & 0x03) {
  327. case 0x01:
  328. flags |= IORESOURCE_IO;
  329. case 0x02: /* 32 bits */
  330. case 0x03: /* 64 bits */
  331. flags |= IORESOURCE_MEM;
  332. }
  333. if (w & 0x40000000)
  334. flags |= IORESOURCE_PREFETCH;
  335. return flags;
  336. }
  337. /*
  338. * SBUS bus specific translator
  339. */
  340. static int of_bus_sbus_match(struct device_node *np)
  341. {
  342. return !strcmp(np->name, "sbus") ||
  343. !strcmp(np->name, "sbi");
  344. }
  345. static void of_bus_sbus_count_cells(struct device_node *child,
  346. int *addrc, int *sizec)
  347. {
  348. if (addrc)
  349. *addrc = 2;
  350. if (sizec)
  351. *sizec = 1;
  352. }
  353. /*
  354. * FHC/Central bus specific translator.
  355. *
  356. * This is just needed to hard-code the address and size cell
  357. * counts. 'fhc' and 'central' nodes lack the #address-cells and
  358. * #size-cells properties, and if you walk to the root on such
  359. * Enterprise boxes all you'll get is a #size-cells of 2 which is
  360. * not what we want to use.
  361. */
  362. static int of_bus_fhc_match(struct device_node *np)
  363. {
  364. return !strcmp(np->name, "fhc") ||
  365. !strcmp(np->name, "central");
  366. }
  367. #define of_bus_fhc_count_cells of_bus_sbus_count_cells
  368. /*
  369. * Array of bus specific translators
  370. */
  371. static struct of_bus of_busses[] = {
  372. /* PCI */
  373. {
  374. .name = "pci",
  375. .addr_prop_name = "assigned-addresses",
  376. .match = of_bus_pci_match,
  377. .count_cells = of_bus_pci_count_cells,
  378. .map = of_bus_pci_map,
  379. .get_flags = of_bus_pci_get_flags,
  380. },
  381. /* SIMBA */
  382. {
  383. .name = "simba",
  384. .addr_prop_name = "assigned-addresses",
  385. .match = of_bus_simba_match,
  386. .count_cells = of_bus_pci_count_cells,
  387. .map = of_bus_simba_map,
  388. .get_flags = of_bus_pci_get_flags,
  389. },
  390. /* SBUS */
  391. {
  392. .name = "sbus",
  393. .addr_prop_name = "reg",
  394. .match = of_bus_sbus_match,
  395. .count_cells = of_bus_sbus_count_cells,
  396. .map = of_bus_default_map,
  397. .get_flags = of_bus_default_get_flags,
  398. },
  399. /* FHC */
  400. {
  401. .name = "fhc",
  402. .addr_prop_name = "reg",
  403. .match = of_bus_fhc_match,
  404. .count_cells = of_bus_fhc_count_cells,
  405. .map = of_bus_default_map,
  406. .get_flags = of_bus_default_get_flags,
  407. },
  408. /* Default */
  409. {
  410. .name = "default",
  411. .addr_prop_name = "reg",
  412. .match = NULL,
  413. .count_cells = of_bus_default_count_cells,
  414. .map = of_bus_default_map,
  415. .get_flags = of_bus_default_get_flags,
  416. },
  417. };
  418. static struct of_bus *of_match_bus(struct device_node *np)
  419. {
  420. int i;
  421. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  422. if (!of_busses[i].match || of_busses[i].match(np))
  423. return &of_busses[i];
  424. BUG();
  425. return NULL;
  426. }
  427. static int __init build_one_resource(struct device_node *parent,
  428. struct of_bus *bus,
  429. struct of_bus *pbus,
  430. u32 *addr,
  431. int na, int ns, int pna)
  432. {
  433. const u32 *ranges;
  434. unsigned int rlen;
  435. int rone;
  436. ranges = of_get_property(parent, "ranges", &rlen);
  437. if (ranges == NULL || rlen == 0) {
  438. u32 result[OF_MAX_ADDR_CELLS];
  439. int i;
  440. memset(result, 0, pna * 4);
  441. for (i = 0; i < na; i++)
  442. result[pna - 1 - i] =
  443. addr[na - 1 - i];
  444. memcpy(addr, result, pna * 4);
  445. return 0;
  446. }
  447. /* Now walk through the ranges */
  448. rlen /= 4;
  449. rone = na + pna + ns;
  450. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  451. if (!bus->map(addr, ranges, na, ns, pna))
  452. return 0;
  453. }
  454. return 1;
  455. }
  456. static int __init use_1to1_mapping(struct device_node *pp)
  457. {
  458. const char *model;
  459. /* If this is on the PMU bus, don't try to translate it even
  460. * if a ranges property exists.
  461. */
  462. if (!strcmp(pp->name, "pmu"))
  463. return 1;
  464. /* If we have a ranges property in the parent, use it. */
  465. if (of_find_property(pp, "ranges", NULL) != NULL)
  466. return 0;
  467. /* If the parent is the dma node of an ISA bus, pass
  468. * the translation up to the root.
  469. */
  470. if (!strcmp(pp->name, "dma"))
  471. return 0;
  472. /* Similarly for Simba PCI bridges. */
  473. model = of_get_property(pp, "model", NULL);
  474. if (model && !strcmp(model, "SUNW,simba"))
  475. return 0;
  476. return 1;
  477. }
  478. static int of_resource_verbose;
  479. static void __init build_device_resources(struct of_device *op,
  480. struct device *parent)
  481. {
  482. struct of_device *p_op;
  483. struct of_bus *bus;
  484. int na, ns;
  485. int index, num_reg;
  486. const void *preg;
  487. if (!parent)
  488. return;
  489. p_op = to_of_device(parent);
  490. bus = of_match_bus(p_op->node);
  491. bus->count_cells(op->node, &na, &ns);
  492. preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
  493. if (!preg || num_reg == 0)
  494. return;
  495. /* Convert to num-cells. */
  496. num_reg /= 4;
  497. /* Convert to num-entries. */
  498. num_reg /= na + ns;
  499. /* Prevent overruning the op->resources[] array. */
  500. if (num_reg > PROMREG_MAX) {
  501. printk(KERN_WARNING "%s: Too many regs (%d), "
  502. "limiting to %d.\n",
  503. op->node->full_name, num_reg, PROMREG_MAX);
  504. num_reg = PROMREG_MAX;
  505. }
  506. for (index = 0; index < num_reg; index++) {
  507. struct resource *r = &op->resource[index];
  508. u32 addr[OF_MAX_ADDR_CELLS];
  509. const u32 *reg = (preg + (index * ((na + ns) * 4)));
  510. struct device_node *dp = op->node;
  511. struct device_node *pp = p_op->node;
  512. struct of_bus *pbus, *dbus;
  513. u64 size, result = OF_BAD_ADDR;
  514. unsigned long flags;
  515. int dna, dns;
  516. int pna, pns;
  517. size = of_read_addr(reg + na, ns);
  518. flags = bus->get_flags(reg);
  519. memcpy(addr, reg, na * 4);
  520. if (use_1to1_mapping(pp)) {
  521. result = of_read_addr(addr, na);
  522. goto build_res;
  523. }
  524. dna = na;
  525. dns = ns;
  526. dbus = bus;
  527. while (1) {
  528. dp = pp;
  529. pp = dp->parent;
  530. if (!pp) {
  531. result = of_read_addr(addr, dna);
  532. break;
  533. }
  534. pbus = of_match_bus(pp);
  535. pbus->count_cells(dp, &pna, &pns);
  536. if (build_one_resource(dp, dbus, pbus, addr,
  537. dna, dns, pna))
  538. break;
  539. dna = pna;
  540. dns = pns;
  541. dbus = pbus;
  542. }
  543. build_res:
  544. memset(r, 0, sizeof(*r));
  545. if (of_resource_verbose)
  546. printk("%s reg[%d] -> %lx\n",
  547. op->node->full_name, index,
  548. result);
  549. if (result != OF_BAD_ADDR) {
  550. if (tlb_type == hypervisor)
  551. result &= 0x0fffffffffffffffUL;
  552. r->start = result;
  553. r->end = result + size - 1;
  554. r->flags = flags;
  555. }
  556. r->name = op->node->name;
  557. }
  558. }
  559. static struct device_node * __init
  560. apply_interrupt_map(struct device_node *dp, struct device_node *pp,
  561. const u32 *imap, int imlen, const u32 *imask,
  562. unsigned int *irq_p)
  563. {
  564. struct device_node *cp;
  565. unsigned int irq = *irq_p;
  566. struct of_bus *bus;
  567. phandle handle;
  568. const u32 *reg;
  569. int na, num_reg, i;
  570. bus = of_match_bus(pp);
  571. bus->count_cells(dp, &na, NULL);
  572. reg = of_get_property(dp, "reg", &num_reg);
  573. if (!reg || !num_reg)
  574. return NULL;
  575. imlen /= ((na + 3) * 4);
  576. handle = 0;
  577. for (i = 0; i < imlen; i++) {
  578. int j;
  579. for (j = 0; j < na; j++) {
  580. if ((reg[j] & imask[j]) != imap[j])
  581. goto next;
  582. }
  583. if (imap[na] == irq) {
  584. handle = imap[na + 1];
  585. irq = imap[na + 2];
  586. break;
  587. }
  588. next:
  589. imap += (na + 3);
  590. }
  591. if (i == imlen) {
  592. /* Psycho and Sabre PCI controllers can have 'interrupt-map'
  593. * properties that do not include the on-board device
  594. * interrupts. Instead, the device's 'interrupts' property
  595. * is already a fully specified INO value.
  596. *
  597. * Handle this by deciding that, if we didn't get a
  598. * match in the parent's 'interrupt-map', and the
  599. * parent is an IRQ translater, then use the parent as
  600. * our IRQ controller.
  601. */
  602. if (pp->irq_trans)
  603. return pp;
  604. return NULL;
  605. }
  606. *irq_p = irq;
  607. cp = of_find_node_by_phandle(handle);
  608. return cp;
  609. }
  610. static unsigned int __init pci_irq_swizzle(struct device_node *dp,
  611. struct device_node *pp,
  612. unsigned int irq)
  613. {
  614. const struct linux_prom_pci_registers *regs;
  615. unsigned int bus, devfn, slot, ret;
  616. if (irq < 1 || irq > 4)
  617. return irq;
  618. regs = of_get_property(dp, "reg", NULL);
  619. if (!regs)
  620. return irq;
  621. bus = (regs->phys_hi >> 16) & 0xff;
  622. devfn = (regs->phys_hi >> 8) & 0xff;
  623. slot = (devfn >> 3) & 0x1f;
  624. if (pp->irq_trans) {
  625. /* Derived from Table 8-3, U2P User's Manual. This branch
  626. * is handling a PCI controller that lacks a proper set of
  627. * interrupt-map and interrupt-map-mask properties. The
  628. * Ultra-E450 is one example.
  629. *
  630. * The bit layout is BSSLL, where:
  631. * B: 0 on bus A, 1 on bus B
  632. * D: 2-bit slot number, derived from PCI device number as
  633. * (dev - 1) for bus A, or (dev - 2) for bus B
  634. * L: 2-bit line number
  635. */
  636. if (bus & 0x80) {
  637. /* PBM-A */
  638. bus = 0x00;
  639. slot = (slot - 1) << 2;
  640. } else {
  641. /* PBM-B */
  642. bus = 0x10;
  643. slot = (slot - 2) << 2;
  644. }
  645. irq -= 1;
  646. ret = (bus | slot | irq);
  647. } else {
  648. /* Going through a PCI-PCI bridge that lacks a set of
  649. * interrupt-map and interrupt-map-mask properties.
  650. */
  651. ret = ((irq - 1 + (slot & 3)) & 3) + 1;
  652. }
  653. return ret;
  654. }
  655. static int of_irq_verbose;
  656. static unsigned int __init build_one_device_irq(struct of_device *op,
  657. struct device *parent,
  658. unsigned int irq)
  659. {
  660. struct device_node *dp = op->node;
  661. struct device_node *pp, *ip;
  662. unsigned int orig_irq = irq;
  663. if (irq == 0xffffffff)
  664. return irq;
  665. if (dp->irq_trans) {
  666. irq = dp->irq_trans->irq_build(dp, irq,
  667. dp->irq_trans->data);
  668. if (of_irq_verbose)
  669. printk("%s: direct translate %x --> %x\n",
  670. dp->full_name, orig_irq, irq);
  671. return irq;
  672. }
  673. /* Something more complicated. Walk up to the root, applying
  674. * interrupt-map or bus specific translations, until we hit
  675. * an IRQ translator.
  676. *
  677. * If we hit a bus type or situation we cannot handle, we
  678. * stop and assume that the original IRQ number was in a
  679. * format which has special meaning to it's immediate parent.
  680. */
  681. pp = dp->parent;
  682. ip = NULL;
  683. while (pp) {
  684. const void *imap, *imsk;
  685. int imlen;
  686. imap = of_get_property(pp, "interrupt-map", &imlen);
  687. imsk = of_get_property(pp, "interrupt-map-mask", NULL);
  688. if (imap && imsk) {
  689. struct device_node *iret;
  690. int this_orig_irq = irq;
  691. iret = apply_interrupt_map(dp, pp,
  692. imap, imlen, imsk,
  693. &irq);
  694. if (of_irq_verbose)
  695. printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
  696. op->node->full_name,
  697. pp->full_name, this_orig_irq,
  698. (iret ? iret->full_name : "NULL"), irq);
  699. if (!iret)
  700. break;
  701. if (iret->irq_trans) {
  702. ip = iret;
  703. break;
  704. }
  705. } else {
  706. if (!strcmp(pp->type, "pci") ||
  707. !strcmp(pp->type, "pciex")) {
  708. unsigned int this_orig_irq = irq;
  709. irq = pci_irq_swizzle(dp, pp, irq);
  710. if (of_irq_verbose)
  711. printk("%s: PCI swizzle [%s] "
  712. "%x --> %x\n",
  713. op->node->full_name,
  714. pp->full_name, this_orig_irq,
  715. irq);
  716. }
  717. if (pp->irq_trans) {
  718. ip = pp;
  719. break;
  720. }
  721. }
  722. dp = pp;
  723. pp = pp->parent;
  724. }
  725. if (!ip)
  726. return orig_irq;
  727. irq = ip->irq_trans->irq_build(op->node, irq,
  728. ip->irq_trans->data);
  729. if (of_irq_verbose)
  730. printk("%s: Apply IRQ trans [%s] %x --> %x\n",
  731. op->node->full_name, ip->full_name, orig_irq, irq);
  732. return irq;
  733. }
  734. static struct of_device * __init scan_one_device(struct device_node *dp,
  735. struct device *parent)
  736. {
  737. struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
  738. const unsigned int *irq;
  739. int len, i;
  740. if (!op)
  741. return NULL;
  742. op->node = dp;
  743. op->clock_freq = of_getintprop_default(dp, "clock-frequency",
  744. (25*1000*1000));
  745. op->portid = of_getintprop_default(dp, "upa-portid", -1);
  746. if (op->portid == -1)
  747. op->portid = of_getintprop_default(dp, "portid", -1);
  748. irq = of_get_property(dp, "interrupts", &len);
  749. if (irq) {
  750. memcpy(op->irqs, irq, len);
  751. op->num_irqs = len / 4;
  752. } else {
  753. op->num_irqs = 0;
  754. }
  755. /* Prevent overruning the op->irqs[] array. */
  756. if (op->num_irqs > PROMINTR_MAX) {
  757. printk(KERN_WARNING "%s: Too many irqs (%d), "
  758. "limiting to %d.\n",
  759. dp->full_name, op->num_irqs, PROMINTR_MAX);
  760. op->num_irqs = PROMINTR_MAX;
  761. }
  762. build_device_resources(op, parent);
  763. for (i = 0; i < op->num_irqs; i++)
  764. op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
  765. op->dev.parent = parent;
  766. op->dev.bus = &of_bus_type;
  767. if (!parent)
  768. strcpy(op->dev.bus_id, "root");
  769. else
  770. sprintf(op->dev.bus_id, "%08x", dp->node);
  771. if (of_device_register(op)) {
  772. printk("%s: Could not register of device.\n",
  773. dp->full_name);
  774. kfree(op);
  775. op = NULL;
  776. }
  777. return op;
  778. }
  779. static void __init scan_tree(struct device_node *dp, struct device *parent)
  780. {
  781. while (dp) {
  782. struct of_device *op = scan_one_device(dp, parent);
  783. if (op)
  784. scan_tree(dp->child, &op->dev);
  785. dp = dp->sibling;
  786. }
  787. }
  788. static void __init scan_of_devices(void)
  789. {
  790. struct device_node *root = of_find_node_by_path("/");
  791. struct of_device *parent;
  792. parent = scan_one_device(root, NULL);
  793. if (!parent)
  794. return;
  795. scan_tree(root->child, &parent->dev);
  796. }
  797. static int __init of_bus_driver_init(void)
  798. {
  799. int err;
  800. err = bus_register(&of_bus_type);
  801. #ifdef CONFIG_PCI
  802. if (!err)
  803. err = bus_register(&isa_bus_type);
  804. if (!err)
  805. err = bus_register(&ebus_bus_type);
  806. #endif
  807. #ifdef CONFIG_SBUS
  808. if (!err)
  809. err = bus_register(&sbus_bus_type);
  810. #endif
  811. if (!err)
  812. scan_of_devices();
  813. return err;
  814. }
  815. postcore_initcall(of_bus_driver_init);
  816. static int __init of_debug(char *str)
  817. {
  818. int val = 0;
  819. get_option(&str, &val);
  820. if (val & 1)
  821. of_resource_verbose = 1;
  822. if (val & 2)
  823. of_irq_verbose = 1;
  824. return 1;
  825. }
  826. __setup("of_debug=", of_debug);
  827. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  828. {
  829. /* initialize common driver fields */
  830. drv->driver.name = drv->name;
  831. drv->driver.bus = bus;
  832. /* register with core */
  833. return driver_register(&drv->driver);
  834. }
  835. void of_unregister_driver(struct of_platform_driver *drv)
  836. {
  837. driver_unregister(&drv->driver);
  838. }
  839. static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  840. {
  841. struct of_device *ofdev;
  842. ofdev = to_of_device(dev);
  843. return sprintf(buf, "%s", ofdev->node->full_name);
  844. }
  845. static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
  846. /**
  847. * of_release_dev - free an of device structure when all users of it are finished.
  848. * @dev: device that's been disconnected
  849. *
  850. * Will be called only by the device core when all users of this of device are
  851. * done.
  852. */
  853. void of_release_dev(struct device *dev)
  854. {
  855. struct of_device *ofdev;
  856. ofdev = to_of_device(dev);
  857. kfree(ofdev);
  858. }
  859. int of_device_register(struct of_device *ofdev)
  860. {
  861. int rc;
  862. BUG_ON(ofdev->node == NULL);
  863. rc = device_register(&ofdev->dev);
  864. if (rc)
  865. return rc;
  866. rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
  867. if (rc)
  868. device_unregister(&ofdev->dev);
  869. return rc;
  870. }
  871. void of_device_unregister(struct of_device *ofdev)
  872. {
  873. device_remove_file(&ofdev->dev, &dev_attr_devspec);
  874. device_unregister(&ofdev->dev);
  875. }
  876. struct of_device* of_platform_device_create(struct device_node *np,
  877. const char *bus_id,
  878. struct device *parent,
  879. struct bus_type *bus)
  880. {
  881. struct of_device *dev;
  882. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  883. if (!dev)
  884. return NULL;
  885. dev->dev.parent = parent;
  886. dev->dev.bus = bus;
  887. dev->dev.release = of_release_dev;
  888. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  889. if (of_device_register(dev) != 0) {
  890. kfree(dev);
  891. return NULL;
  892. }
  893. return dev;
  894. }
  895. EXPORT_SYMBOL(of_match_device);
  896. EXPORT_SYMBOL(of_register_driver);
  897. EXPORT_SYMBOL(of_unregister_driver);
  898. EXPORT_SYMBOL(of_device_register);
  899. EXPORT_SYMBOL(of_device_unregister);
  900. EXPORT_SYMBOL(of_dev_get);
  901. EXPORT_SYMBOL(of_dev_put);
  902. EXPORT_SYMBOL(of_platform_device_create);
  903. EXPORT_SYMBOL(of_release_dev);