of_device.c 13 KB

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