of_device.c 12 KB

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