of_device.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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. /* When we miss an I/O space match on PCI, just pass it up
  455. * to the next PCI bridge and/or controller.
  456. */
  457. if (!strcmp(bus->name, "pci") &&
  458. (addr[0] & 0x03000000) == 0x01000000)
  459. return 0;
  460. return 1;
  461. }
  462. static int __init use_1to1_mapping(struct device_node *pp)
  463. {
  464. const char *model;
  465. /* If this is on the PMU bus, don't try to translate it even
  466. * if a ranges property exists.
  467. */
  468. if (!strcmp(pp->name, "pmu"))
  469. return 1;
  470. /* If we have a ranges property in the parent, use it. */
  471. if (of_find_property(pp, "ranges", NULL) != NULL)
  472. return 0;
  473. /* If the parent is the dma node of an ISA bus, pass
  474. * the translation up to the root.
  475. */
  476. if (!strcmp(pp->name, "dma"))
  477. return 0;
  478. /* Similarly for Simba PCI bridges. */
  479. model = of_get_property(pp, "model", NULL);
  480. if (model && !strcmp(model, "SUNW,simba"))
  481. return 0;
  482. return 1;
  483. }
  484. static int of_resource_verbose;
  485. static void __init build_device_resources(struct of_device *op,
  486. struct device *parent)
  487. {
  488. struct of_device *p_op;
  489. struct of_bus *bus;
  490. int na, ns;
  491. int index, num_reg;
  492. const void *preg;
  493. if (!parent)
  494. return;
  495. p_op = to_of_device(parent);
  496. bus = of_match_bus(p_op->node);
  497. bus->count_cells(op->node, &na, &ns);
  498. preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
  499. if (!preg || num_reg == 0)
  500. return;
  501. /* Convert to num-cells. */
  502. num_reg /= 4;
  503. /* Convert to num-entries. */
  504. num_reg /= na + ns;
  505. /* Prevent overrunning the op->resources[] array. */
  506. if (num_reg > PROMREG_MAX) {
  507. printk(KERN_WARNING "%s: Too many regs (%d), "
  508. "limiting to %d.\n",
  509. op->node->full_name, num_reg, PROMREG_MAX);
  510. num_reg = PROMREG_MAX;
  511. }
  512. for (index = 0; index < num_reg; index++) {
  513. struct resource *r = &op->resource[index];
  514. u32 addr[OF_MAX_ADDR_CELLS];
  515. const u32 *reg = (preg + (index * ((na + ns) * 4)));
  516. struct device_node *dp = op->node;
  517. struct device_node *pp = p_op->node;
  518. struct of_bus *pbus, *dbus;
  519. u64 size, result = OF_BAD_ADDR;
  520. unsigned long flags;
  521. int dna, dns;
  522. int pna, pns;
  523. size = of_read_addr(reg + na, ns);
  524. flags = bus->get_flags(reg);
  525. memcpy(addr, reg, na * 4);
  526. if (use_1to1_mapping(pp)) {
  527. result = of_read_addr(addr, na);
  528. goto build_res;
  529. }
  530. dna = na;
  531. dns = ns;
  532. dbus = bus;
  533. while (1) {
  534. dp = pp;
  535. pp = dp->parent;
  536. if (!pp) {
  537. result = of_read_addr(addr, dna);
  538. break;
  539. }
  540. pbus = of_match_bus(pp);
  541. pbus->count_cells(dp, &pna, &pns);
  542. if (build_one_resource(dp, dbus, pbus, addr,
  543. dna, dns, pna))
  544. break;
  545. dna = pna;
  546. dns = pns;
  547. dbus = pbus;
  548. }
  549. build_res:
  550. memset(r, 0, sizeof(*r));
  551. if (of_resource_verbose)
  552. printk("%s reg[%d] -> %lx\n",
  553. op->node->full_name, index,
  554. result);
  555. if (result != OF_BAD_ADDR) {
  556. if (tlb_type == hypervisor)
  557. result &= 0x0fffffffffffffffUL;
  558. r->start = result;
  559. r->end = result + size - 1;
  560. r->flags = flags;
  561. }
  562. r->name = op->node->name;
  563. }
  564. }
  565. static struct device_node * __init
  566. apply_interrupt_map(struct device_node *dp, struct device_node *pp,
  567. const u32 *imap, int imlen, const u32 *imask,
  568. unsigned int *irq_p)
  569. {
  570. struct device_node *cp;
  571. unsigned int irq = *irq_p;
  572. struct of_bus *bus;
  573. phandle handle;
  574. const u32 *reg;
  575. int na, num_reg, i;
  576. bus = of_match_bus(pp);
  577. bus->count_cells(dp, &na, NULL);
  578. reg = of_get_property(dp, "reg", &num_reg);
  579. if (!reg || !num_reg)
  580. return NULL;
  581. imlen /= ((na + 3) * 4);
  582. handle = 0;
  583. for (i = 0; i < imlen; i++) {
  584. int j;
  585. for (j = 0; j < na; j++) {
  586. if ((reg[j] & imask[j]) != imap[j])
  587. goto next;
  588. }
  589. if (imap[na] == irq) {
  590. handle = imap[na + 1];
  591. irq = imap[na + 2];
  592. break;
  593. }
  594. next:
  595. imap += (na + 3);
  596. }
  597. if (i == imlen) {
  598. /* Psycho and Sabre PCI controllers can have 'interrupt-map'
  599. * properties that do not include the on-board device
  600. * interrupts. Instead, the device's 'interrupts' property
  601. * is already a fully specified INO value.
  602. *
  603. * Handle this by deciding that, if we didn't get a
  604. * match in the parent's 'interrupt-map', and the
  605. * parent is an IRQ translater, then use the parent as
  606. * our IRQ controller.
  607. */
  608. if (pp->irq_trans)
  609. return pp;
  610. return NULL;
  611. }
  612. *irq_p = irq;
  613. cp = of_find_node_by_phandle(handle);
  614. return cp;
  615. }
  616. static unsigned int __init pci_irq_swizzle(struct device_node *dp,
  617. struct device_node *pp,
  618. unsigned int irq)
  619. {
  620. const struct linux_prom_pci_registers *regs;
  621. unsigned int bus, devfn, slot, ret;
  622. if (irq < 1 || irq > 4)
  623. return irq;
  624. regs = of_get_property(dp, "reg", NULL);
  625. if (!regs)
  626. return irq;
  627. bus = (regs->phys_hi >> 16) & 0xff;
  628. devfn = (regs->phys_hi >> 8) & 0xff;
  629. slot = (devfn >> 3) & 0x1f;
  630. if (pp->irq_trans) {
  631. /* Derived from Table 8-3, U2P User's Manual. This branch
  632. * is handling a PCI controller that lacks a proper set of
  633. * interrupt-map and interrupt-map-mask properties. The
  634. * Ultra-E450 is one example.
  635. *
  636. * The bit layout is BSSLL, where:
  637. * B: 0 on bus A, 1 on bus B
  638. * D: 2-bit slot number, derived from PCI device number as
  639. * (dev - 1) for bus A, or (dev - 2) for bus B
  640. * L: 2-bit line number
  641. */
  642. if (bus & 0x80) {
  643. /* PBM-A */
  644. bus = 0x00;
  645. slot = (slot - 1) << 2;
  646. } else {
  647. /* PBM-B */
  648. bus = 0x10;
  649. slot = (slot - 2) << 2;
  650. }
  651. irq -= 1;
  652. ret = (bus | slot | irq);
  653. } else {
  654. /* Going through a PCI-PCI bridge that lacks a set of
  655. * interrupt-map and interrupt-map-mask properties.
  656. */
  657. ret = ((irq - 1 + (slot & 3)) & 3) + 1;
  658. }
  659. return ret;
  660. }
  661. static int of_irq_verbose;
  662. static unsigned int __init build_one_device_irq(struct of_device *op,
  663. struct device *parent,
  664. unsigned int irq)
  665. {
  666. struct device_node *dp = op->node;
  667. struct device_node *pp, *ip;
  668. unsigned int orig_irq = irq;
  669. if (irq == 0xffffffff)
  670. return irq;
  671. if (dp->irq_trans) {
  672. irq = dp->irq_trans->irq_build(dp, irq,
  673. dp->irq_trans->data);
  674. if (of_irq_verbose)
  675. printk("%s: direct translate %x --> %x\n",
  676. dp->full_name, orig_irq, irq);
  677. return irq;
  678. }
  679. /* Something more complicated. Walk up to the root, applying
  680. * interrupt-map or bus specific translations, until we hit
  681. * an IRQ translator.
  682. *
  683. * If we hit a bus type or situation we cannot handle, we
  684. * stop and assume that the original IRQ number was in a
  685. * format which has special meaning to it's immediate parent.
  686. */
  687. pp = dp->parent;
  688. ip = NULL;
  689. while (pp) {
  690. const void *imap, *imsk;
  691. int imlen;
  692. imap = of_get_property(pp, "interrupt-map", &imlen);
  693. imsk = of_get_property(pp, "interrupt-map-mask", NULL);
  694. if (imap && imsk) {
  695. struct device_node *iret;
  696. int this_orig_irq = irq;
  697. iret = apply_interrupt_map(dp, pp,
  698. imap, imlen, imsk,
  699. &irq);
  700. if (of_irq_verbose)
  701. printk("%s: Apply [%s:%x] imap --> [%s:%x]\n",
  702. op->node->full_name,
  703. pp->full_name, this_orig_irq,
  704. (iret ? iret->full_name : "NULL"), irq);
  705. if (!iret)
  706. break;
  707. if (iret->irq_trans) {
  708. ip = iret;
  709. break;
  710. }
  711. } else {
  712. if (!strcmp(pp->type, "pci") ||
  713. !strcmp(pp->type, "pciex")) {
  714. unsigned int this_orig_irq = irq;
  715. irq = pci_irq_swizzle(dp, pp, irq);
  716. if (of_irq_verbose)
  717. printk("%s: PCI swizzle [%s] "
  718. "%x --> %x\n",
  719. op->node->full_name,
  720. pp->full_name, this_orig_irq,
  721. irq);
  722. }
  723. if (pp->irq_trans) {
  724. ip = pp;
  725. break;
  726. }
  727. }
  728. dp = pp;
  729. pp = pp->parent;
  730. }
  731. if (!ip)
  732. return orig_irq;
  733. irq = ip->irq_trans->irq_build(op->node, irq,
  734. ip->irq_trans->data);
  735. if (of_irq_verbose)
  736. printk("%s: Apply IRQ trans [%s] %x --> %x\n",
  737. op->node->full_name, ip->full_name, orig_irq, irq);
  738. return irq;
  739. }
  740. static struct of_device * __init scan_one_device(struct device_node *dp,
  741. struct device *parent)
  742. {
  743. struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
  744. const unsigned int *irq;
  745. int len, i;
  746. if (!op)
  747. return NULL;
  748. op->node = dp;
  749. op->clock_freq = of_getintprop_default(dp, "clock-frequency",
  750. (25*1000*1000));
  751. op->portid = of_getintprop_default(dp, "upa-portid", -1);
  752. if (op->portid == -1)
  753. op->portid = of_getintprop_default(dp, "portid", -1);
  754. irq = of_get_property(dp, "interrupts", &len);
  755. if (irq) {
  756. memcpy(op->irqs, irq, len);
  757. op->num_irqs = len / 4;
  758. } else {
  759. op->num_irqs = 0;
  760. }
  761. /* Prevent overrunning the op->irqs[] array. */
  762. if (op->num_irqs > PROMINTR_MAX) {
  763. printk(KERN_WARNING "%s: Too many irqs (%d), "
  764. "limiting to %d.\n",
  765. dp->full_name, op->num_irqs, PROMINTR_MAX);
  766. op->num_irqs = PROMINTR_MAX;
  767. }
  768. build_device_resources(op, parent);
  769. for (i = 0; i < op->num_irqs; i++)
  770. op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]);
  771. op->dev.parent = parent;
  772. op->dev.bus = &of_bus_type;
  773. if (!parent)
  774. strcpy(op->dev.bus_id, "root");
  775. else
  776. sprintf(op->dev.bus_id, "%08x", dp->node);
  777. if (of_device_register(op)) {
  778. printk("%s: Could not register of device.\n",
  779. dp->full_name);
  780. kfree(op);
  781. op = NULL;
  782. }
  783. return op;
  784. }
  785. static void __init scan_tree(struct device_node *dp, struct device *parent)
  786. {
  787. while (dp) {
  788. struct of_device *op = scan_one_device(dp, parent);
  789. if (op)
  790. scan_tree(dp->child, &op->dev);
  791. dp = dp->sibling;
  792. }
  793. }
  794. static void __init scan_of_devices(void)
  795. {
  796. struct device_node *root = of_find_node_by_path("/");
  797. struct of_device *parent;
  798. parent = scan_one_device(root, NULL);
  799. if (!parent)
  800. return;
  801. scan_tree(root->child, &parent->dev);
  802. }
  803. static int __init of_bus_driver_init(void)
  804. {
  805. int err;
  806. err = bus_register(&of_bus_type);
  807. #ifdef CONFIG_PCI
  808. if (!err)
  809. err = bus_register(&isa_bus_type);
  810. if (!err)
  811. err = bus_register(&ebus_bus_type);
  812. #endif
  813. #ifdef CONFIG_SBUS
  814. if (!err)
  815. err = bus_register(&sbus_bus_type);
  816. #endif
  817. if (!err)
  818. scan_of_devices();
  819. return err;
  820. }
  821. postcore_initcall(of_bus_driver_init);
  822. static int __init of_debug(char *str)
  823. {
  824. int val = 0;
  825. get_option(&str, &val);
  826. if (val & 1)
  827. of_resource_verbose = 1;
  828. if (val & 2)
  829. of_irq_verbose = 1;
  830. return 1;
  831. }
  832. __setup("of_debug=", of_debug);
  833. int of_register_driver(struct of_platform_driver *drv, struct bus_type *bus)
  834. {
  835. /* initialize common driver fields */
  836. drv->driver.name = drv->name;
  837. drv->driver.bus = bus;
  838. /* register with core */
  839. return driver_register(&drv->driver);
  840. }
  841. void of_unregister_driver(struct of_platform_driver *drv)
  842. {
  843. driver_unregister(&drv->driver);
  844. }
  845. static ssize_t dev_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  846. {
  847. struct of_device *ofdev;
  848. ofdev = to_of_device(dev);
  849. return sprintf(buf, "%s", ofdev->node->full_name);
  850. }
  851. static DEVICE_ATTR(devspec, S_IRUGO, dev_show_devspec, NULL);
  852. /**
  853. * of_release_dev - free an of device structure when all users of it are finished.
  854. * @dev: device that's been disconnected
  855. *
  856. * Will be called only by the device core when all users of this of device are
  857. * done.
  858. */
  859. void of_release_dev(struct device *dev)
  860. {
  861. struct of_device *ofdev;
  862. ofdev = to_of_device(dev);
  863. kfree(ofdev);
  864. }
  865. int of_device_register(struct of_device *ofdev)
  866. {
  867. int rc;
  868. BUG_ON(ofdev->node == NULL);
  869. rc = device_register(&ofdev->dev);
  870. if (rc)
  871. return rc;
  872. rc = device_create_file(&ofdev->dev, &dev_attr_devspec);
  873. if (rc)
  874. device_unregister(&ofdev->dev);
  875. return rc;
  876. }
  877. void of_device_unregister(struct of_device *ofdev)
  878. {
  879. device_remove_file(&ofdev->dev, &dev_attr_devspec);
  880. device_unregister(&ofdev->dev);
  881. }
  882. struct of_device* of_platform_device_create(struct device_node *np,
  883. const char *bus_id,
  884. struct device *parent,
  885. struct bus_type *bus)
  886. {
  887. struct of_device *dev;
  888. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  889. if (!dev)
  890. return NULL;
  891. dev->dev.parent = parent;
  892. dev->dev.bus = bus;
  893. dev->dev.release = of_release_dev;
  894. strlcpy(dev->dev.bus_id, bus_id, BUS_ID_SIZE);
  895. if (of_device_register(dev) != 0) {
  896. kfree(dev);
  897. return NULL;
  898. }
  899. return dev;
  900. }
  901. EXPORT_SYMBOL(of_match_device);
  902. EXPORT_SYMBOL(of_register_driver);
  903. EXPORT_SYMBOL(of_unregister_driver);
  904. EXPORT_SYMBOL(of_device_register);
  905. EXPORT_SYMBOL(of_device_unregister);
  906. EXPORT_SYMBOL(of_dev_get);
  907. EXPORT_SYMBOL(of_dev_put);
  908. EXPORT_SYMBOL(of_platform_device_create);
  909. EXPORT_SYMBOL(of_release_dev);