of_device_64.c 16 KB

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