of_device.c 19 KB

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