of_device.c 19 KB

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