of_device.c 13 KB

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