of_device.c 21 KB

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