of_device.c 24 KB

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