of_device.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. return !strcmp(np->name, "sbus") ||
  202. !strcmp(np->name, "sbi");
  203. }
  204. static void of_bus_sbus_count_cells(struct device_node *child,
  205. int *addrc, int *sizec)
  206. {
  207. if (addrc)
  208. *addrc = 2;
  209. if (sizec)
  210. *sizec = 1;
  211. }
  212. static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna)
  213. {
  214. return of_bus_default_map(addr, range, na, ns, pna);
  215. }
  216. static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
  217. {
  218. return IORESOURCE_MEM;
  219. }
  220. /*
  221. * Array of bus specific translators
  222. */
  223. static struct of_bus of_busses[] = {
  224. /* PCI */
  225. {
  226. .name = "pci",
  227. .addr_prop_name = "assigned-addresses",
  228. .match = of_bus_pci_match,
  229. .count_cells = of_bus_pci_count_cells,
  230. .map = of_bus_pci_map,
  231. .get_flags = of_bus_pci_get_flags,
  232. },
  233. /* SBUS */
  234. {
  235. .name = "sbus",
  236. .addr_prop_name = "reg",
  237. .match = of_bus_sbus_match,
  238. .count_cells = of_bus_sbus_count_cells,
  239. .map = of_bus_sbus_map,
  240. .get_flags = of_bus_sbus_get_flags,
  241. },
  242. /* Default */
  243. {
  244. .name = "default",
  245. .addr_prop_name = "reg",
  246. .match = NULL,
  247. .count_cells = of_bus_default_count_cells,
  248. .map = of_bus_default_map,
  249. .get_flags = of_bus_default_get_flags,
  250. },
  251. };
  252. static struct of_bus *of_match_bus(struct device_node *np)
  253. {
  254. int i;
  255. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  256. if (!of_busses[i].match || of_busses[i].match(np))
  257. return &of_busses[i];
  258. BUG();
  259. return NULL;
  260. }
  261. static int __init build_one_resource(struct device_node *parent,
  262. struct of_bus *bus,
  263. struct of_bus *pbus,
  264. u32 *addr,
  265. int na, int ns, int pna)
  266. {
  267. const u32 *ranges;
  268. unsigned int rlen;
  269. int rone;
  270. ranges = of_get_property(parent, "ranges", &rlen);
  271. if (ranges == NULL || rlen == 0) {
  272. u32 result[OF_MAX_ADDR_CELLS];
  273. int i;
  274. memset(result, 0, pna * 4);
  275. for (i = 0; i < na; i++)
  276. result[pna - 1 - i] =
  277. addr[na - 1 - i];
  278. memcpy(addr, result, pna * 4);
  279. return 0;
  280. }
  281. /* Now walk through the ranges */
  282. rlen /= 4;
  283. rone = na + pna + ns;
  284. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  285. if (!bus->map(addr, ranges, na, ns, pna))
  286. return 0;
  287. }
  288. return 1;
  289. }
  290. static int __init use_1to1_mapping(struct device_node *pp)
  291. {
  292. /* If we have a ranges property in the parent, use it. */
  293. if (of_find_property(pp, "ranges", NULL) != NULL)
  294. return 0;
  295. /* Some SBUS devices use intermediate nodes to express
  296. * hierarchy within the device itself. These aren't
  297. * real bus nodes, and don't have a 'ranges' property.
  298. * But, we should still pass the translation work up
  299. * to the SBUS itself.
  300. */
  301. if (!strcmp(pp->name, "dma") ||
  302. !strcmp(pp->name, "espdma") ||
  303. !strcmp(pp->name, "ledma") ||
  304. !strcmp(pp->name, "lebuffer"))
  305. return 0;
  306. return 1;
  307. }
  308. static int of_resource_verbose;
  309. static void __init build_device_resources(struct of_device *op,
  310. struct device *parent)
  311. {
  312. struct of_device *p_op;
  313. struct of_bus *bus;
  314. int na, ns;
  315. int index, num_reg;
  316. const void *preg;
  317. if (!parent)
  318. return;
  319. p_op = to_of_device(parent);
  320. bus = of_match_bus(p_op->node);
  321. bus->count_cells(op->node, &na, &ns);
  322. preg = of_get_property(op->node, bus->addr_prop_name, &num_reg);
  323. if (!preg || num_reg == 0)
  324. return;
  325. /* Convert to num-cells. */
  326. num_reg /= 4;
  327. /* Conver to num-entries. */
  328. num_reg /= na + ns;
  329. for (index = 0; index < num_reg; index++) {
  330. struct resource *r = &op->resource[index];
  331. u32 addr[OF_MAX_ADDR_CELLS];
  332. const u32 *reg = (preg + (index * ((na + ns) * 4)));
  333. struct device_node *dp = op->node;
  334. struct device_node *pp = p_op->node;
  335. struct of_bus *pbus, *dbus;
  336. u64 size, result = OF_BAD_ADDR;
  337. unsigned long flags;
  338. int dna, dns;
  339. int pna, pns;
  340. size = of_read_addr(reg + na, ns);
  341. memcpy(addr, reg, na * 4);
  342. flags = bus->get_flags(reg, 0);
  343. if (use_1to1_mapping(pp)) {
  344. result = of_read_addr(addr, na);
  345. goto build_res;
  346. }
  347. dna = na;
  348. dns = ns;
  349. dbus = bus;
  350. while (1) {
  351. dp = pp;
  352. pp = dp->parent;
  353. if (!pp) {
  354. result = of_read_addr(addr, dna);
  355. break;
  356. }
  357. pbus = of_match_bus(pp);
  358. pbus->count_cells(dp, &pna, &pns);
  359. if (build_one_resource(dp, dbus, pbus, addr,
  360. dna, dns, pna))
  361. break;
  362. flags = pbus->get_flags(addr, flags);
  363. dna = pna;
  364. dns = pns;
  365. dbus = pbus;
  366. }
  367. build_res:
  368. memset(r, 0, sizeof(*r));
  369. if (of_resource_verbose)
  370. printk("%s reg[%d] -> %llx\n",
  371. op->node->full_name, index,
  372. result);
  373. if (result != OF_BAD_ADDR) {
  374. r->start = result & 0xffffffff;
  375. r->end = result + size - 1;
  376. r->flags = flags | ((result >> 32ULL) & 0xffUL);
  377. }
  378. r->name = op->node->name;
  379. }
  380. }
  381. static struct of_device * __init scan_one_device(struct device_node *dp,
  382. struct device *parent)
  383. {
  384. struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
  385. const struct linux_prom_irqs *intr;
  386. struct dev_archdata *sd;
  387. int len, i;
  388. if (!op)
  389. return NULL;
  390. sd = &op->dev.archdata;
  391. sd->prom_node = dp;
  392. sd->op = op;
  393. op->node = dp;
  394. op->clock_freq = of_getintprop_default(dp, "clock-frequency",
  395. (25*1000*1000));
  396. op->portid = of_getintprop_default(dp, "upa-portid", -1);
  397. if (op->portid == -1)
  398. op->portid = of_getintprop_default(dp, "portid", -1);
  399. intr = of_get_property(dp, "intr", &len);
  400. if (intr) {
  401. op->num_irqs = len / sizeof(struct linux_prom_irqs);
  402. for (i = 0; i < op->num_irqs; i++)
  403. op->irqs[i] = intr[i].pri;
  404. } else {
  405. const unsigned int *irq =
  406. of_get_property(dp, "interrupts", &len);
  407. if (irq) {
  408. op->num_irqs = len / sizeof(unsigned int);
  409. for (i = 0; i < op->num_irqs; i++)
  410. op->irqs[i] = irq[i];
  411. } else {
  412. op->num_irqs = 0;
  413. }
  414. }
  415. if (sparc_cpu_model == sun4d) {
  416. static int pil_to_sbus[] = {
  417. 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
  418. };
  419. struct device_node *io_unit, *sbi = dp->parent;
  420. const struct linux_prom_registers *regs;
  421. int board, slot;
  422. while (sbi) {
  423. if (!strcmp(sbi->name, "sbi"))
  424. break;
  425. sbi = sbi->parent;
  426. }
  427. if (!sbi)
  428. goto build_resources;
  429. regs = of_get_property(dp, "reg", NULL);
  430. if (!regs)
  431. goto build_resources;
  432. slot = regs->which_io;
  433. /* If SBI's parent is not io-unit or the io-unit lacks
  434. * a "board#" property, something is very wrong.
  435. */
  436. if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
  437. printk("%s: Error, parent is not io-unit.\n",
  438. sbi->full_name);
  439. goto build_resources;
  440. }
  441. io_unit = sbi->parent;
  442. board = of_getintprop_default(io_unit, "board#", -1);
  443. if (board == -1) {
  444. printk("%s: Error, lacks board# property.\n",
  445. io_unit->full_name);
  446. goto build_resources;
  447. }
  448. for (i = 0; i < op->num_irqs; i++) {
  449. int this_irq = op->irqs[i];
  450. int sbusl = pil_to_sbus[this_irq];
  451. if (sbusl)
  452. this_irq = (((board + 1) << 5) +
  453. (sbusl << 2) +
  454. slot);
  455. op->irqs[i] = this_irq;
  456. }
  457. }
  458. build_resources:
  459. build_device_resources(op, parent);
  460. op->dev.parent = parent;
  461. op->dev.bus = &of_platform_bus_type;
  462. if (!parent)
  463. dev_set_name(&op->dev, "root");
  464. else
  465. dev_set_name(&op->dev, "%08x", dp->node);
  466. if (of_device_register(op)) {
  467. printk("%s: Could not register of device.\n",
  468. dp->full_name);
  469. kfree(op);
  470. op = NULL;
  471. }
  472. return op;
  473. }
  474. static void __init scan_tree(struct device_node *dp, struct device *parent)
  475. {
  476. while (dp) {
  477. struct of_device *op = scan_one_device(dp, parent);
  478. if (op)
  479. scan_tree(dp->child, &op->dev);
  480. dp = dp->sibling;
  481. }
  482. }
  483. static void __init scan_of_devices(void)
  484. {
  485. struct device_node *root = of_find_node_by_path("/");
  486. struct of_device *parent;
  487. parent = scan_one_device(root, NULL);
  488. if (!parent)
  489. return;
  490. scan_tree(root->child, &parent->dev);
  491. }
  492. static int __init of_bus_driver_init(void)
  493. {
  494. int err;
  495. err = of_bus_type_init(&of_platform_bus_type, "of");
  496. if (!err)
  497. scan_of_devices();
  498. return err;
  499. }
  500. postcore_initcall(of_bus_driver_init);
  501. static int __init of_debug(char *str)
  502. {
  503. int val = 0;
  504. get_option(&str, &val);
  505. if (val & 1)
  506. of_resource_verbose = 1;
  507. return 1;
  508. }
  509. __setup("of_debug=", of_debug);