of_device.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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 bus, 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. bus = (regs->phys_hi >> 16) & 0xff;
  600. devfn = (regs->phys_hi >> 8) & 0xff;
  601. slot = (devfn >> 3) & 0x1f;
  602. if (pp->irq_trans) {
  603. /* Derived from Table 8-3, U2P User's Manual. This branch
  604. * is handling a PCI controller that lacks a proper set of
  605. * interrupt-map and interrupt-map-mask properties. The
  606. * Ultra-E450 is one example.
  607. *
  608. * The bit layout is BSSLL, where:
  609. * B: 0 on bus A, 1 on bus B
  610. * D: 2-bit slot number, derived from PCI device number as
  611. * (dev - 1) for bus A, or (dev - 2) for bus B
  612. * L: 2-bit line number
  613. *
  614. * Actually, more "portable" way to calculate the funky
  615. * slot number is to subtract pbm->pci_first_slot from the
  616. * device number, and that's exactly what the pre-OF
  617. * sparc64 code did, but we're building this stuff generically
  618. * using the OBP tree, not in the PCI controller layer.
  619. */
  620. if (bus & 0x80) {
  621. /* PBM-A */
  622. bus = 0x00;
  623. slot = (slot - 1) << 2;
  624. } else {
  625. /* PBM-B */
  626. bus = 0x10;
  627. slot = (slot - 2) << 2;
  628. }
  629. irq -= 1;
  630. ret = (bus | slot | irq);
  631. } else {
  632. /* Going through a PCI-PCI bridge that lacks a set of
  633. * interrupt-map and interrupt-map-mask properties.
  634. */
  635. ret = ((irq - 1 + (slot & 3)) & 3) + 1;
  636. }
  637. return ret;
  638. }
  639. static int of_irq_verbose;
  640. static unsigned int __init build_one_device_irq(struct of_device *op,
  641. struct device *parent,
  642. unsigned int irq)
  643. {
  644. struct device_node *dp = op->node;
  645. struct device_node *pp, *ip;
  646. unsigned int orig_irq = irq;
  647. if (irq == 0xffffffff)
  648. return irq;
  649. if (dp->irq_trans) {
  650. irq = dp->irq_trans->irq_build(dp, irq,
  651. dp->irq_trans->data);
  652. if (of_irq_verbose)
  653. printk("%s: direct translate %x --> %x\n",
  654. dp->full_name, orig_irq, irq);
  655. return irq;
  656. }
  657. /* Something more complicated. Walk up to the root, applying
  658. * interrupt-map or bus specific translations, until we hit
  659. * an IRQ translator.
  660. *
  661. * If we hit a bus type or situation we cannot handle, we
  662. * stop and assume that the original IRQ number was in a
  663. * format which has special meaning to it's immediate parent.
  664. */
  665. pp = dp->parent;
  666. ip = NULL;
  667. while (pp) {
  668. void *imap, *imsk;
  669. int imlen;
  670. imap = of_get_property(pp, "interrupt-map", &imlen);
  671. imsk = of_get_property(pp, "interrupt-map-mask", NULL);
  672. if (imap && imsk) {
  673. struct device_node *iret;
  674. int this_orig_irq = irq;
  675. iret = apply_interrupt_map(dp, pp,
  676. imap, imlen, imsk,
  677. &irq);
  678. if (of_irq_verbose)
  679. printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
  680. op->node->full_name,
  681. pp->full_name, this_orig_irq,
  682. (iret ? iret->full_name : "NULL"), irq);
  683. if (!iret)
  684. break;
  685. if (iret->irq_trans) {
  686. ip = iret;
  687. break;
  688. }
  689. } else {
  690. if (!strcmp(pp->type, "pci") ||
  691. !strcmp(pp->type, "pciex")) {
  692. unsigned int this_orig_irq = irq;
  693. irq = pci_irq_swizzle(dp, pp, irq);
  694. if (of_irq_verbose)
  695. printk("%s: PCI swizzle [%s] "
  696. "%x --> %x\n",
  697. op->node->full_name,
  698. pp->full_name, this_orig_irq,
  699. irq);
  700. }
  701. if (pp->irq_trans) {
  702. ip = pp;
  703. break;
  704. }
  705. }
  706. dp = pp;
  707. pp = pp->parent;
  708. }
  709. if (!ip)
  710. return orig_irq;
  711. irq = ip->irq_trans->irq_build(op->node, irq,
  712. ip->irq_trans->data);
  713. if (of_irq_verbose)
  714. printk("%s: Apply IRQ trans [%s] %x --> %x\n",
  715. op->node->full_name, ip->full_name, orig_irq, irq);
  716. return irq;
  717. }
  718. static struct of_device * __init scan_one_device(struct device_node *dp,
  719. struct device *parent)
  720. {
  721. struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
  722. unsigned int *irq;
  723. int len, i;
  724. if (!op)
  725. return NULL;
  726. op->node = dp;
  727. op->clock_freq = of_getintprop_default(dp, "clock-frequency",
  728. (25*1000*1000));
  729. op->portid = of_getintprop_default(dp, "upa-portid", -1);
  730. if (op->portid == -1)
  731. op->portid = of_getintprop_default(dp, "portid", -1);
  732. irq = of_get_property(dp, "interrupts", &len);
  733. if (irq) {
  734. memcpy(op->irqs, irq, len);
  735. op->num_irqs = len / 4;
  736. } else {
  737. op->num_irqs = 0;
  738. }
  739. /* Prevent overruning the op->irqs[] array. */
  740. if (op->num_irqs > PROMINTR_MAX) {
  741. printk(KERN_WARNING "%s: Too many irqs (%d), "
  742. "limiting to %d.\n",
  743. dp->full_name, op->num_irqs, PROMINTR_MAX);
  744. op->num_irqs = PROMINTR_MAX;
  745. }
  746. build_device_resources(op, parent);
  747. for (i = 0; i < op->num_irqs; i++)
  748. op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
  749. op->dev.parent = parent;
  750. op->dev.bus = &of_bus_type;
  751. if (!parent)
  752. strcpy(op->dev.bus_id, "root");
  753. else
  754. sprintf(op->dev.bus_id, "%08x", dp->node);
  755. if (of_device_register(op)) {
  756. printk("%s: Could not register of device.\n",
  757. dp->full_name);
  758. kfree(op);
  759. op = NULL;
  760. }
  761. return op;
  762. }
  763. static void __init scan_tree(struct device_node *dp, struct device *parent)
  764. {
  765. while (dp) {
  766. struct of_device *op = scan_one_device(dp, parent);
  767. if (op)
  768. scan_tree(dp->child, &op->dev);
  769. dp = dp->sibling;
  770. }
  771. }
  772. static void __init scan_of_devices(void)
  773. {
  774. struct device_node *root = of_find_node_by_path("/");
  775. struct of_device *parent;
  776. parent = scan_one_device(root, NULL);
  777. if (!parent)
  778. return;
  779. scan_tree(root->child, &parent->dev);
  780. }
  781. static int __init of_bus_driver_init(void)
  782. {
  783. int err;
  784. err = bus_register(&of_bus_type);
  785. #ifdef CONFIG_PCI
  786. if (!err)
  787. err = bus_register(&isa_bus_type);
  788. if (!err)
  789. err = bus_register(&ebus_bus_type);
  790. #endif
  791. #ifdef CONFIG_SBUS
  792. if (!err)
  793. err = bus_register(&sbus_bus_type);
  794. #endif
  795. if (!err)
  796. scan_of_devices();
  797. return err;
  798. }
  799. postcore_initcall(of_bus_driver_init);
  800. static int __init of_debug(char *str)
  801. {
  802. int val = 0;
  803. get_option(&str, &val);
  804. if (val & 1)
  805. of_resource_verbose = 1;
  806. if (val & 2)
  807. of_irq_verbose = 1;
  808. return 1;
  809. }
  810. __setup("of_debug=", of_debug);
  811. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  812. {
  813. /* initialize common driver fields */
  814. drv->driver.name = drv->name;
  815. drv->driver.bus = bus;
  816. /* register with core */
  817. return driver_register(&drv->driver);
  818. }
  819. void of_unregister_driver(struct of_platform_driver *drv)
  820. {
  821. driver_unregister(&drv->driver);
  822. }
  823. static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  824. {
  825. struct of_device *ofdev;
  826. ofdev = to_of_device(dev);
  827. return sprintf(buf, "%s", ofdev->node->full_name);
  828. }
  829. static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
  830. /**
  831. * of_release_dev - free an of device structure when all users of it are finished.
  832. * @dev: device that's been disconnected
  833. *
  834. * Will be called only by the device core when all users of this of device are
  835. * done.
  836. */
  837. void of_release_dev(struct device *dev)
  838. {
  839. struct of_device *ofdev;
  840. ofdev = to_of_device(dev);
  841. kfree(ofdev);
  842. }
  843. int of_device_register(struct of_device *ofdev)
  844. {
  845. int rc;
  846. BUG_ON(ofdev->node == NULL);
  847. rc = device_register(&ofdev->dev);
  848. if (rc)
  849. return rc;
  850. rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
  851. if (rc)
  852. device_unregister(&ofdev->dev);
  853. return rc;
  854. }
  855. void of_device_unregister(struct of_device *ofdev)
  856. {
  857. device_remove_file(&ofdev->dev, &dev_attr_devspec);
  858. device_unregister(&ofdev->dev);
  859. }
  860. struct of_device* of_platform_device_create(struct device_node *np,
  861. const char *bus_id,
  862. struct device *parent,
  863. struct bus_type *bus)
  864. {
  865. struct of_device *dev;
  866. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  867. if (!dev)
  868. return NULL;
  869. dev->dev.parent = parent;
  870. dev->dev.bus = bus;
  871. dev->dev.release = of_release_dev;
  872. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  873. if (of_device_register(dev) != 0) {
  874. kfree(dev);
  875. return NULL;
  876. }
  877. return dev;
  878. }
  879. EXPORT_SYMBOL(of_match_device);
  880. EXPORT_SYMBOL(of_register_driver);
  881. EXPORT_SYMBOL(of_unregister_driver);
  882. EXPORT_SYMBOL(of_device_register);
  883. EXPORT_SYMBOL(of_device_unregister);
  884. EXPORT_SYMBOL(of_dev_get);
  885. EXPORT_SYMBOL(of_dev_put);
  886. EXPORT_SYMBOL(of_platform_device_create);
  887. EXPORT_SYMBOL(of_release_dev);