of_device_64.c 16 KB

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