of_device_32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/of.h>
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/mod_devicetable.h>
  7. #include <linux/slab.h>
  8. #include <linux/errno.h>
  9. #include <linux/of_device.h>
  10. #include <linux/of_platform.h>
  11. static int node_match(struct device *dev, void *data)
  12. {
  13. struct of_device *op = to_of_device(dev);
  14. struct device_node *dp = data;
  15. return (op->node == dp);
  16. }
  17. struct of_device *of_find_device_by_node(struct device_node *dp)
  18. {
  19. struct device *dev = bus_find_device(&of_platform_bus_type, NULL,
  20. dp, node_match);
  21. if (dev)
  22. return to_of_device(dev);
  23. return NULL;
  24. }
  25. EXPORT_SYMBOL(of_find_device_by_node);
  26. unsigned int irq_of_parse_and_map(struct device_node *node, int index)
  27. {
  28. struct of_device *op = of_find_device_by_node(node);
  29. if (!op || index >= op->num_irqs)
  30. return 0;
  31. return op->irqs[index];
  32. }
  33. EXPORT_SYMBOL(irq_of_parse_and_map);
  34. /* Take the archdata values for IOMMU, STC, and HOSTDATA found in
  35. * BUS and propagate to all child of_device objects.
  36. */
  37. void of_propagate_archdata(struct of_device *bus)
  38. {
  39. struct dev_archdata *bus_sd = &bus->dev.archdata;
  40. struct device_node *bus_dp = bus->node;
  41. struct device_node *dp;
  42. for (dp = bus_dp->child; dp; dp = dp->sibling) {
  43. struct of_device *op = of_find_device_by_node(dp);
  44. op->dev.archdata.iommu = bus_sd->iommu;
  45. op->dev.archdata.stc = bus_sd->stc;
  46. op->dev.archdata.host_controller = bus_sd->host_controller;
  47. op->dev.archdata.numa_node = bus_sd->numa_node;
  48. if (dp->child)
  49. of_propagate_archdata(op);
  50. }
  51. }
  52. struct bus_type of_platform_bus_type;
  53. EXPORT_SYMBOL(of_platform_bus_type);
  54. static inline u64 of_read_addr(const u32 *cell, int size)
  55. {
  56. u64 r = 0;
  57. while (size--)
  58. r = (r << 32) | *(cell++);
  59. return r;
  60. }
  61. static void __init get_cells(struct device_node *dp,
  62. int *addrc, int *sizec)
  63. {
  64. if (addrc)
  65. *addrc = of_n_addr_cells(dp);
  66. if (sizec)
  67. *sizec = of_n_size_cells(dp);
  68. }
  69. /* Max address size we deal with */
  70. #define OF_MAX_ADDR_CELLS 4
  71. struct of_bus {
  72. const char *name;
  73. const char *addr_prop_name;
  74. int (*match)(struct device_node *parent);
  75. void (*count_cells)(struct device_node *child,
  76. int *addrc, int *sizec);
  77. int (*map)(u32 *addr, const u32 *range,
  78. int na, int ns, int pna);
  79. unsigned long (*get_flags)(const u32 *addr, unsigned long);
  80. };
  81. /*
  82. * Default translator (generic bus)
  83. */
  84. static void of_bus_default_count_cells(struct device_node *dev,
  85. int *addrc, int *sizec)
  86. {
  87. get_cells(dev, addrc, sizec);
  88. }
  89. /* Make sure the least significant 64-bits are in-range. Even
  90. * for 3 or 4 cell values it is a good enough approximation.
  91. */
  92. static int of_out_of_range(const u32 *addr, const u32 *base,
  93. const u32 *size, int na, int ns)
  94. {
  95. u64 a = of_read_addr(addr, na);
  96. u64 b = of_read_addr(base, na);
  97. if (a < b)
  98. return 1;
  99. b += of_read_addr(size, ns);
  100. if (a >= b)
  101. return 1;
  102. return 0;
  103. }
  104. static int of_bus_default_map(u32 *addr, const u32 *range,
  105. int na, int ns, int pna)
  106. {
  107. u32 result[OF_MAX_ADDR_CELLS];
  108. int i;
  109. if (ns > 2) {
  110. printk("of_device: Cannot handle size cells (%d) > 2.", ns);
  111. return -EINVAL;
  112. }
  113. if (of_out_of_range(addr, range, range + na + pna, na, ns))
  114. return -EINVAL;
  115. /* Start with the parent range base. */
  116. memcpy(result, range + na, pna * 4);
  117. /* Add in the child address offset. */
  118. for (i = 0; i < na; i++)
  119. result[pna - 1 - i] +=
  120. (addr[na - 1 - i] -
  121. range[na - 1 - i]);
  122. memcpy(addr, result, pna * 4);
  123. return 0;
  124. }
  125. static unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags)
  126. {
  127. if (flags)
  128. return flags;
  129. return IORESOURCE_MEM;
  130. }
  131. /*
  132. * PCI bus specific translator
  133. */
  134. static int of_bus_pci_match(struct device_node *np)
  135. {
  136. if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
  137. /* Do not do PCI specific frobbing if the
  138. * PCI bridge lacks a ranges property. We
  139. * want to pass it through up to the next
  140. * parent as-is, not with the PCI translate
  141. * method which chops off the top address cell.
  142. */
  143. if (!of_find_property(np, "ranges", NULL))
  144. return 0;
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. static void of_bus_pci_count_cells(struct device_node *np,
  150. int *addrc, int *sizec)
  151. {
  152. if (addrc)
  153. *addrc = 3;
  154. if (sizec)
  155. *sizec = 2;
  156. }
  157. static int of_bus_pci_map(u32 *addr, const u32 *range,
  158. int na, int ns, int pna)
  159. {
  160. u32 result[OF_MAX_ADDR_CELLS];
  161. int i;
  162. /* Check address type match */
  163. if ((addr[0] ^ range[0]) & 0x03000000)
  164. return -EINVAL;
  165. if (of_out_of_range(addr + 1, range + 1, range + na + pna,
  166. na - 1, ns))
  167. return -EINVAL;
  168. /* Start with the parent range base. */
  169. memcpy(result, range + na, pna * 4);
  170. /* Add in the child address offset, skipping high cell. */
  171. for (i = 0; i < na - 1; i++)
  172. result[pna - 1 - i] +=
  173. (addr[na - 1 - i] -
  174. range[na - 1 - i]);
  175. memcpy(addr, result, pna * 4);
  176. return 0;
  177. }
  178. static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
  179. {
  180. u32 w = addr[0];
  181. /* For PCI, we override whatever child busses may have used. */
  182. flags = 0;
  183. switch((w >> 24) & 0x03) {
  184. case 0x01:
  185. flags |= IORESOURCE_IO;
  186. break;
  187. case 0x02: /* 32 bits */
  188. case 0x03: /* 64 bits */
  189. flags |= IORESOURCE_MEM;
  190. break;
  191. }
  192. if (w & 0x40000000)
  193. flags |= IORESOURCE_PREFETCH;
  194. return flags;
  195. }
  196. /*
  197. * SBUS bus specific translator
  198. */
  199. static int of_bus_sbus_match(struct device_node *np)
  200. {
  201. struct device_node *dp = np;
  202. while (dp) {
  203. if (!strcmp(dp->name, "sbus") ||
  204. !strcmp(dp->name, "sbi"))
  205. return 1;
  206. /* Have a look at use_1to1_mapping(). We're trying
  207. * to match SBUS if that's the top-level bus and we
  208. * don't have some intervening real bus that provides
  209. * ranges based translations.
  210. */
  211. if (of_find_property(dp, "ranges", NULL) != NULL)
  212. break;
  213. dp = dp->parent;
  214. }
  215. return 0;
  216. }
  217. static void of_bus_sbus_count_cells(struct device_node *child,
  218. int *addrc, int *sizec)
  219. {
  220. if (addrc)
  221. *addrc = 2;
  222. if (sizec)
  223. *sizec = 1;
  224. }
  225. static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  226. {
  227. return of_bus_default_map(addr, range, na, ns, pna);
  228. }
  229. static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
  230. {
  231. return IORESOURCE_MEM;
  232. }
  233. /*
  234. * Array of bus specific translators
  235. */
  236. static struct of_bus of_busses[] = {
  237. /* PCI */
  238. {
  239. .name = "pci",
  240. .addr_prop_name = "assigned-addresses",
  241. .match = of_bus_pci_match,
  242. .count_cells = of_bus_pci_count_cells,
  243. .map = of_bus_pci_map,
  244. .get_flags = of_bus_pci_get_flags,
  245. },
  246. /* SBUS */
  247. {
  248. .name = "sbus",
  249. .addr_prop_name = "reg",
  250. .match = of_bus_sbus_match,
  251. .count_cells = of_bus_sbus_count_cells,
  252. .map = of_bus_sbus_map,
  253. .get_flags = of_bus_sbus_get_flags,
  254. },
  255. /* Default */
  256. {
  257. .name = "default",
  258. .addr_prop_name = "reg",
  259. .match = NULL,
  260. .count_cells = of_bus_default_count_cells,
  261. .map = of_bus_default_map,
  262. .get_flags = of_bus_default_get_flags,
  263. },
  264. };
  265. static struct of_bus *of_match_bus(struct device_node *np)
  266. {
  267. int i;
  268. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  269. if (!of_busses[i].match || of_busses[i].match(np))
  270. return &of_busses[i];
  271. BUG();
  272. return NULL;
  273. }
  274. static int __init build_one_resource(struct device_node *parent,
  275. struct of_bus *bus,
  276. struct of_bus *pbus,
  277. u32 *addr,
  278. int na, int ns, int pna)
  279. {
  280. const u32 *ranges;
  281. unsigned int rlen;
  282. int rone;
  283. ranges = of_get_property(parent, "ranges", &rlen);
  284. if (ranges == NULL || rlen == 0) {
  285. u32 result[OF_MAX_ADDR_CELLS];
  286. int i;
  287. memset(result, 0, pna * 4);
  288. for (i = 0; i < na; i++)
  289. result[pna - 1 - i] =
  290. addr[na - 1 - i];
  291. memcpy(addr, result, pna * 4);
  292. return 0;
  293. }
  294. /* Now walk through the ranges */
  295. rlen /= 4;
  296. rone = na + pna + ns;
  297. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  298. if (!bus->map(addr, ranges, na, ns, pna))
  299. return 0;
  300. }
  301. return 1;
  302. }
  303. static int __init use_1to1_mapping(struct device_node *pp)
  304. {
  305. /* If we have a ranges property in the parent, use it. */
  306. if (of_find_property(pp, "ranges", NULL) != NULL)
  307. return 0;
  308. /* Some SBUS devices use intermediate nodes to express
  309. * hierarchy within the device itself. These aren't
  310. * real bus nodes, and don't have a 'ranges' property.
  311. * But, we should still pass the translation work up
  312. * to the SBUS itself.
  313. */
  314. if (!strcmp(pp->name, "dma") ||
  315. !strcmp(pp->name, "espdma") ||
  316. !strcmp(pp->name, "ledma") ||
  317. !strcmp(pp->name, "lebuffer"))
  318. return 0;
  319. return 1;
  320. }
  321. static int of_resource_verbose;
  322. static void __init build_device_resources(struct of_device *op,
  323. struct device *parent)
  324. {
  325. struct of_device *p_op;
  326. struct of_bus *bus;
  327. int na, ns;
  328. int index, num_reg;
  329. const void *preg;
  330. if (!parent)
  331. return;
  332. p_op = to_of_device(parent);
  333. bus = of_match_bus(p_op->node);
  334. bus->count_cells(op->node, &na, &ns);
  335. preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
  336. if (!preg || num_reg == 0)
  337. return;
  338. /* Convert to num-cells. */
  339. num_reg /= 4;
  340. /* Conver to num-entries. */
  341. num_reg /= na + ns;
  342. for (index = 0; index < num_reg; index++) {
  343. struct resource *r = &op->resource[index];
  344. u32 addr[OF_MAX_ADDR_CELLS];
  345. const u32 *reg = (preg + (index * ((na + ns) * 4)));
  346. struct device_node *dp = op->node;
  347. struct device_node *pp = p_op->node;
  348. struct of_bus *pbus, *dbus;
  349. u64 size, result = OF_BAD_ADDR;
  350. unsigned long flags;
  351. int dna, dns;
  352. int pna, pns;
  353. size = of_read_addr(reg + na, ns);
  354. memcpy(addr, reg, na * 4);
  355. flags = bus->get_flags(reg, 0);
  356. if (use_1to1_mapping(pp)) {
  357. result = of_read_addr(addr, na);
  358. goto build_res;
  359. }
  360. dna = na;
  361. dns = ns;
  362. dbus = bus;
  363. while (1) {
  364. dp = pp;
  365. pp = dp->parent;
  366. if (!pp) {
  367. result = of_read_addr(addr, dna);
  368. break;
  369. }
  370. pbus = of_match_bus(pp);
  371. pbus->count_cells(dp, &pna, &pns);
  372. if (build_one_resource(dp, dbus, pbus, addr,
  373. dna, dns, pna))
  374. break;
  375. flags = pbus->get_flags(addr, flags);
  376. dna = pna;
  377. dns = pns;
  378. dbus = pbus;
  379. }
  380. build_res:
  381. memset(r, 0, sizeof(*r));
  382. if (of_resource_verbose)
  383. printk("%s reg[%d] -> %llx\n",
  384. op->node->full_name, index,
  385. result);
  386. if (result != OF_BAD_ADDR) {
  387. r->start = result & 0xffffffff;
  388. r->end = result + size - 1;
  389. r->flags = flags | ((result >> 32ULL) & 0xffUL);
  390. }
  391. r->name = op->node->name;
  392. }
  393. }
  394. static struct of_device * __init scan_one_device(struct device_node *dp,
  395. struct device *parent)
  396. {
  397. struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
  398. const struct linux_prom_irqs *intr;
  399. struct dev_archdata *sd;
  400. int len, i;
  401. if (!op)
  402. return NULL;
  403. sd = &op->dev.archdata;
  404. sd->prom_node = dp;
  405. sd->op = op;
  406. op->node = dp;
  407. op->clock_freq = of_getintprop_default(dp, "clock-frequency",
  408. (25*1000*1000));
  409. op->portid = of_getintprop_default(dp, "upa-portid", -1);
  410. if (op->portid == -1)
  411. op->portid = of_getintprop_default(dp, "portid", -1);
  412. intr = of_get_property(dp, "intr", &len);
  413. if (intr) {
  414. op->num_irqs = len / sizeof(struct linux_prom_irqs);
  415. for (i = 0; i < op->num_irqs; i++)
  416. op->irqs[i] = intr[i].pri;
  417. } else {
  418. const unsigned int *irq =
  419. of_get_property(dp, "interrupts", &len);
  420. if (irq) {
  421. op->num_irqs = len / sizeof(unsigned int);
  422. for (i = 0; i < op->num_irqs; i++)
  423. op->irqs[i] = irq[i];
  424. } else {
  425. op->num_irqs = 0;
  426. }
  427. }
  428. if (sparc_cpu_model == sun4d) {
  429. static int pil_to_sbus[] = {
  430. 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
  431. };
  432. struct device_node *io_unit, *sbi = dp->parent;
  433. const struct linux_prom_registers *regs;
  434. int board, slot;
  435. while (sbi) {
  436. if (!strcmp(sbi->name, "sbi"))
  437. break;
  438. sbi = sbi->parent;
  439. }
  440. if (!sbi)
  441. goto build_resources;
  442. regs = of_get_property(dp, "reg", NULL);
  443. if (!regs)
  444. goto build_resources;
  445. slot = regs->which_io;
  446. /* If SBI's parent is not io-unit or the io-unit lacks
  447. * a "board#" property, something is very wrong.
  448. */
  449. if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
  450. printk("%s: Error, parent is not io-unit.\n",
  451. sbi->full_name);
  452. goto build_resources;
  453. }
  454. io_unit = sbi->parent;
  455. board = of_getintprop_default(io_unit, "board#", -1);
  456. if (board == -1) {
  457. printk("%s: Error, lacks board# property.\n",
  458. io_unit->full_name);
  459. goto build_resources;
  460. }
  461. for (i = 0; i < op->num_irqs; i++) {
  462. int this_irq = op->irqs[i];
  463. int sbusl = pil_to_sbus[this_irq];
  464. if (sbusl)
  465. this_irq = (((board + 1) << 5) +
  466. (sbusl << 2) +
  467. slot);
  468. op->irqs[i] = this_irq;
  469. }
  470. }
  471. build_resources:
  472. build_device_resources(op, parent);
  473. op->dev.parent = parent;
  474. op->dev.bus = &of_platform_bus_type;
  475. if (!parent)
  476. dev_set_name(&op->dev, "root");
  477. else
  478. dev_set_name(&op->dev, "%08x", dp->node);
  479. if (of_device_register(op)) {
  480. printk("%s: Could not register of device.\n",
  481. dp->full_name);
  482. kfree(op);
  483. op = NULL;
  484. }
  485. return op;
  486. }
  487. static void __init scan_tree(struct device_node *dp, struct device *parent)
  488. {
  489. while (dp) {
  490. struct of_device *op = scan_one_device(dp, parent);
  491. if (op)
  492. scan_tree(dp->child, &op->dev);
  493. dp = dp->sibling;
  494. }
  495. }
  496. static void __init scan_of_devices(void)
  497. {
  498. struct device_node *root = of_find_node_by_path("/");
  499. struct of_device *parent;
  500. parent = scan_one_device(root, NULL);
  501. if (!parent)
  502. return;
  503. scan_tree(root->child, &parent->dev);
  504. }
  505. static int __init of_bus_driver_init(void)
  506. {
  507. int err;
  508. err = of_bus_type_init(&of_platform_bus_type, "of");
  509. if (!err)
  510. scan_of_devices();
  511. return err;
  512. }
  513. postcore_initcall(of_bus_driver_init);
  514. static int __init of_debug(char *str)
  515. {
  516. int val = 0;
  517. get_option(&str, &val);
  518. if (val & 1)
  519. of_resource_verbose = 1;
  520. return 1;
  521. }
  522. __setup("of_debug=", of_debug);