of_device_32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/irq.h>
  10. #include <linux/of_device.h>
  11. #include <linux/of_platform.h>
  12. #include <asm/leon.h>
  13. #include <asm/leon_amba.h>
  14. #include "of_device_common.h"
  15. /*
  16. * PCI bus specific translator
  17. */
  18. static int of_bus_pci_match(struct device_node *np)
  19. {
  20. if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) {
  21. /* Do not do PCI specific frobbing if the
  22. * PCI bridge lacks a ranges property. We
  23. * want to pass it through up to the next
  24. * parent as-is, not with the PCI translate
  25. * method which chops off the top address cell.
  26. */
  27. if (!of_find_property(np, "ranges", NULL))
  28. return 0;
  29. return 1;
  30. }
  31. return 0;
  32. }
  33. static void of_bus_pci_count_cells(struct device_node *np,
  34. int *addrc, int *sizec)
  35. {
  36. if (addrc)
  37. *addrc = 3;
  38. if (sizec)
  39. *sizec = 2;
  40. }
  41. static int of_bus_pci_map(u32 *addr, const u32 *range,
  42. int na, int ns, int pna)
  43. {
  44. u32 result[OF_MAX_ADDR_CELLS];
  45. int i;
  46. /* Check address type match */
  47. if ((addr[0] ^ range[0]) & 0x03000000)
  48. return -EINVAL;
  49. if (of_out_of_range(addr + 1, range + 1, range + na + pna,
  50. na - 1, ns))
  51. return -EINVAL;
  52. /* Start with the parent range base. */
  53. memcpy(result, range + na, pna * 4);
  54. /* Add in the child address offset, skipping high cell. */
  55. for (i = 0; i < na - 1; i++)
  56. result[pna - 1 - i] +=
  57. (addr[na - 1 - i] -
  58. range[na - 1 - i]);
  59. memcpy(addr, result, pna * 4);
  60. return 0;
  61. }
  62. static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags)
  63. {
  64. u32 w = addr[0];
  65. /* For PCI, we override whatever child busses may have used. */
  66. flags = 0;
  67. switch((w >> 24) & 0x03) {
  68. case 0x01:
  69. flags |= IORESOURCE_IO;
  70. break;
  71. case 0x02: /* 32 bits */
  72. case 0x03: /* 64 bits */
  73. flags |= IORESOURCE_MEM;
  74. break;
  75. }
  76. if (w & 0x40000000)
  77. flags |= IORESOURCE_PREFETCH;
  78. return flags;
  79. }
  80. static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags)
  81. {
  82. return IORESOURCE_MEM;
  83. }
  84. /*
  85. * AMBAPP bus specific translator
  86. */
  87. static int of_bus_ambapp_match(struct device_node *np)
  88. {
  89. return !strcmp(np->type, "ambapp");
  90. }
  91. static void of_bus_ambapp_count_cells(struct device_node *child,
  92. int *addrc, int *sizec)
  93. {
  94. if (addrc)
  95. *addrc = 1;
  96. if (sizec)
  97. *sizec = 1;
  98. }
  99. static int of_bus_ambapp_map(u32 *addr, const u32 *range,
  100. int na, int ns, int pna)
  101. {
  102. return of_bus_default_map(addr, range, na, ns, pna);
  103. }
  104. static unsigned long of_bus_ambapp_get_flags(const u32 *addr,
  105. unsigned long flags)
  106. {
  107. return IORESOURCE_MEM;
  108. }
  109. /*
  110. * Array of bus specific translators
  111. */
  112. static struct of_bus of_busses[] = {
  113. /* PCI */
  114. {
  115. .name = "pci",
  116. .addr_prop_name = "assigned-addresses",
  117. .match = of_bus_pci_match,
  118. .count_cells = of_bus_pci_count_cells,
  119. .map = of_bus_pci_map,
  120. .get_flags = of_bus_pci_get_flags,
  121. },
  122. /* SBUS */
  123. {
  124. .name = "sbus",
  125. .addr_prop_name = "reg",
  126. .match = of_bus_sbus_match,
  127. .count_cells = of_bus_sbus_count_cells,
  128. .map = of_bus_default_map,
  129. .get_flags = of_bus_sbus_get_flags,
  130. },
  131. /* AMBA */
  132. {
  133. .name = "ambapp",
  134. .addr_prop_name = "reg",
  135. .match = of_bus_ambapp_match,
  136. .count_cells = of_bus_ambapp_count_cells,
  137. .map = of_bus_ambapp_map,
  138. .get_flags = of_bus_ambapp_get_flags,
  139. },
  140. /* Default */
  141. {
  142. .name = "default",
  143. .addr_prop_name = "reg",
  144. .match = NULL,
  145. .count_cells = of_bus_default_count_cells,
  146. .map = of_bus_default_map,
  147. .get_flags = of_bus_default_get_flags,
  148. },
  149. };
  150. static struct of_bus *of_match_bus(struct device_node *np)
  151. {
  152. int i;
  153. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  154. if (!of_busses[i].match || of_busses[i].match(np))
  155. return &of_busses[i];
  156. BUG();
  157. return NULL;
  158. }
  159. static int __init build_one_resource(struct device_node *parent,
  160. struct of_bus *bus,
  161. struct of_bus *pbus,
  162. u32 *addr,
  163. int na, int ns, int pna)
  164. {
  165. const u32 *ranges;
  166. unsigned int rlen;
  167. int rone;
  168. ranges = of_get_property(parent, "ranges", &rlen);
  169. if (ranges == NULL || rlen == 0) {
  170. u32 result[OF_MAX_ADDR_CELLS];
  171. int i;
  172. memset(result, 0, pna * 4);
  173. for (i = 0; i < na; i++)
  174. result[pna - 1 - i] =
  175. addr[na - 1 - i];
  176. memcpy(addr, result, pna * 4);
  177. return 0;
  178. }
  179. /* Now walk through the ranges */
  180. rlen /= 4;
  181. rone = na + pna + ns;
  182. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  183. if (!bus->map(addr, ranges, na, ns, pna))
  184. return 0;
  185. }
  186. return 1;
  187. }
  188. static int __init use_1to1_mapping(struct device_node *pp)
  189. {
  190. /* If we have a ranges property in the parent, use it. */
  191. if (of_find_property(pp, "ranges", NULL) != NULL)
  192. return 0;
  193. /* Some SBUS devices use intermediate nodes to express
  194. * hierarchy within the device itself. These aren't
  195. * real bus nodes, and don't have a 'ranges' property.
  196. * But, we should still pass the translation work up
  197. * to the SBUS itself.
  198. */
  199. if (!strcmp(pp->name, "dma") ||
  200. !strcmp(pp->name, "espdma") ||
  201. !strcmp(pp->name, "ledma") ||
  202. !strcmp(pp->name, "lebuffer"))
  203. return 0;
  204. return 1;
  205. }
  206. static int of_resource_verbose;
  207. static void __init build_device_resources(struct of_device *op,
  208. struct device *parent)
  209. {
  210. struct of_device *p_op;
  211. struct of_bus *bus;
  212. int na, ns;
  213. int index, num_reg;
  214. const void *preg;
  215. if (!parent)
  216. return;
  217. p_op = to_of_device(parent);
  218. bus = of_match_bus(p_op->dev.of_node);
  219. bus->count_cells(op->dev.of_node, &na, &ns);
  220. preg = of_get_property(op->dev.of_node, bus->addr_prop_name, &num_reg);
  221. if (!preg || num_reg == 0)
  222. return;
  223. /* Convert to num-cells. */
  224. num_reg /= 4;
  225. /* Conver to num-entries. */
  226. num_reg /= na + ns;
  227. for (index = 0; index < num_reg; index++) {
  228. struct resource *r = &op->resource[index];
  229. u32 addr[OF_MAX_ADDR_CELLS];
  230. const u32 *reg = (preg + (index * ((na + ns) * 4)));
  231. struct device_node *dp = op->dev.of_node;
  232. struct device_node *pp = p_op->dev.of_node;
  233. struct of_bus *pbus, *dbus;
  234. u64 size, result = OF_BAD_ADDR;
  235. unsigned long flags;
  236. int dna, dns;
  237. int pna, pns;
  238. size = of_read_addr(reg + na, ns);
  239. memcpy(addr, reg, na * 4);
  240. flags = bus->get_flags(reg, 0);
  241. if (use_1to1_mapping(pp)) {
  242. result = of_read_addr(addr, na);
  243. goto build_res;
  244. }
  245. dna = na;
  246. dns = ns;
  247. dbus = bus;
  248. while (1) {
  249. dp = pp;
  250. pp = dp->parent;
  251. if (!pp) {
  252. result = of_read_addr(addr, dna);
  253. break;
  254. }
  255. pbus = of_match_bus(pp);
  256. pbus->count_cells(dp, &pna, &pns);
  257. if (build_one_resource(dp, dbus, pbus, addr,
  258. dna, dns, pna))
  259. break;
  260. flags = pbus->get_flags(addr, flags);
  261. dna = pna;
  262. dns = pns;
  263. dbus = pbus;
  264. }
  265. build_res:
  266. memset(r, 0, sizeof(*r));
  267. if (of_resource_verbose)
  268. printk("%s reg[%d] -> %llx\n",
  269. op->dev.of_node->full_name, index,
  270. result);
  271. if (result != OF_BAD_ADDR) {
  272. r->start = result & 0xffffffff;
  273. r->end = result + size - 1;
  274. r->flags = flags | ((result >> 32ULL) & 0xffUL);
  275. }
  276. r->name = op->dev.of_node->name;
  277. }
  278. }
  279. static struct of_device * __init scan_one_device(struct device_node *dp,
  280. struct device *parent)
  281. {
  282. struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
  283. const struct linux_prom_irqs *intr;
  284. struct dev_archdata *sd;
  285. int len, i;
  286. if (!op)
  287. return NULL;
  288. sd = &op->dev.archdata;
  289. sd->op = op;
  290. op->dev.of_node = dp;
  291. op->clock_freq = of_getintprop_default(dp, "clock-frequency",
  292. (25*1000*1000));
  293. op->portid = of_getintprop_default(dp, "upa-portid", -1);
  294. if (op->portid == -1)
  295. op->portid = of_getintprop_default(dp, "portid", -1);
  296. intr = of_get_property(dp, "intr", &len);
  297. if (intr) {
  298. op->num_irqs = len / sizeof(struct linux_prom_irqs);
  299. for (i = 0; i < op->num_irqs; i++)
  300. op->irqs[i] = intr[i].pri;
  301. } else {
  302. const unsigned int *irq =
  303. of_get_property(dp, "interrupts", &len);
  304. if (irq) {
  305. op->num_irqs = len / sizeof(unsigned int);
  306. for (i = 0; i < op->num_irqs; i++)
  307. op->irqs[i] = irq[i];
  308. } else {
  309. op->num_irqs = 0;
  310. }
  311. }
  312. if (sparc_cpu_model == sun4d) {
  313. static int pil_to_sbus[] = {
  314. 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0,
  315. };
  316. struct device_node *io_unit, *sbi = dp->parent;
  317. const struct linux_prom_registers *regs;
  318. int board, slot;
  319. while (sbi) {
  320. if (!strcmp(sbi->name, "sbi"))
  321. break;
  322. sbi = sbi->parent;
  323. }
  324. if (!sbi)
  325. goto build_resources;
  326. regs = of_get_property(dp, "reg", NULL);
  327. if (!regs)
  328. goto build_resources;
  329. slot = regs->which_io;
  330. /* If SBI's parent is not io-unit or the io-unit lacks
  331. * a "board#" property, something is very wrong.
  332. */
  333. if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
  334. printk("%s: Error, parent is not io-unit.\n",
  335. sbi->full_name);
  336. goto build_resources;
  337. }
  338. io_unit = sbi->parent;
  339. board = of_getintprop_default(io_unit, "board#", -1);
  340. if (board == -1) {
  341. printk("%s: Error, lacks board# property.\n",
  342. io_unit->full_name);
  343. goto build_resources;
  344. }
  345. for (i = 0; i < op->num_irqs; i++) {
  346. int this_irq = op->irqs[i];
  347. int sbusl = pil_to_sbus[this_irq];
  348. if (sbusl)
  349. this_irq = (((board + 1) << 5) +
  350. (sbusl << 2) +
  351. slot);
  352. op->irqs[i] = this_irq;
  353. }
  354. }
  355. build_resources:
  356. build_device_resources(op, parent);
  357. op->dev.parent = parent;
  358. op->dev.bus = &of_platform_bus_type;
  359. if (!parent)
  360. dev_set_name(&op->dev, "root");
  361. else
  362. dev_set_name(&op->dev, "%08x", dp->phandle);
  363. if (of_device_register(op)) {
  364. printk("%s: Could not register of device.\n",
  365. dp->full_name);
  366. kfree(op);
  367. op = NULL;
  368. }
  369. return op;
  370. }
  371. static void __init scan_tree(struct device_node *dp, struct device *parent)
  372. {
  373. while (dp) {
  374. struct of_device *op = scan_one_device(dp, parent);
  375. if (op)
  376. scan_tree(dp->child, &op->dev);
  377. dp = dp->sibling;
  378. }
  379. }
  380. static void __init scan_of_devices(void)
  381. {
  382. struct device_node *root = of_find_node_by_path("/");
  383. struct of_device *parent;
  384. parent = scan_one_device(root, NULL);
  385. if (!parent)
  386. return;
  387. scan_tree(root->child, &parent->dev);
  388. }
  389. static int __init of_bus_driver_init(void)
  390. {
  391. int err;
  392. err = of_bus_type_init(&of_platform_bus_type, "of");
  393. if (!err)
  394. scan_of_devices();
  395. return err;
  396. }
  397. postcore_initcall(of_bus_driver_init);
  398. static int __init of_debug(char *str)
  399. {
  400. int val = 0;
  401. get_option(&str, &val);
  402. if (val & 1)
  403. of_resource_verbose = 1;
  404. return 1;
  405. }
  406. __setup("of_debug=", of_debug);