of_device_64.c 19 KB

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