of_device.c 12 KB

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