of_device_32.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include <linux/string.h>
  2. #include <linux/kernel.h>
  3. #include <linux/of.h>
  4. #include <linux/init.h>
  5. #include <linux/mod_devicetable.h>
  6. #include <linux/slab.h>
  7. #include <linux/errno.h>
  8. #include <linux/irq.h>
  9. #include <linux/of_device.h>
  10. #include <linux/of_platform.h>
  11. #include <asm/leon.h>
  12. #include <asm/leon_amba.h>
  13. #include "of_device_common.h"
  14. #include "irq.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 platform_device *op,
  208. struct device *parent)
  209. {
  210. struct platform_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_platform_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. op->resource = op->archdata.resource;
  228. op->num_resources = num_reg;
  229. for (index = 0; index < num_reg; index++) {
  230. struct resource *r = &op->resource[index];
  231. u32 addr[OF_MAX_ADDR_CELLS];
  232. const u32 *reg = (preg + (index * ((na + ns) * 4)));
  233. struct device_node *dp = op->dev.of_node;
  234. struct device_node *pp = p_op->dev.of_node;
  235. struct of_bus *pbus, *dbus;
  236. u64 size, result = OF_BAD_ADDR;
  237. unsigned long flags;
  238. int dna, dns;
  239. int pna, pns;
  240. size = of_read_addr(reg + na, ns);
  241. memcpy(addr, reg, na * 4);
  242. flags = bus->get_flags(reg, 0);
  243. if (use_1to1_mapping(pp)) {
  244. result = of_read_addr(addr, na);
  245. goto build_res;
  246. }
  247. dna = na;
  248. dns = ns;
  249. dbus = bus;
  250. while (1) {
  251. dp = pp;
  252. pp = dp->parent;
  253. if (!pp) {
  254. result = of_read_addr(addr, dna);
  255. break;
  256. }
  257. pbus = of_match_bus(pp);
  258. pbus->count_cells(dp, &pna, &pns);
  259. if (build_one_resource(dp, dbus, pbus, addr,
  260. dna, dns, pna))
  261. break;
  262. flags = pbus->get_flags(addr, flags);
  263. dna = pna;
  264. dns = pns;
  265. dbus = pbus;
  266. }
  267. build_res:
  268. memset(r, 0, sizeof(*r));
  269. if (of_resource_verbose)
  270. printk("%s reg[%d] -> %llx\n",
  271. op->dev.of_node->full_name, index,
  272. result);
  273. if (result != OF_BAD_ADDR) {
  274. r->start = result & 0xffffffff;
  275. r->end = result + size - 1;
  276. r->flags = flags | ((result >> 32ULL) & 0xffUL);
  277. }
  278. r->name = op->dev.of_node->name;
  279. }
  280. }
  281. static struct platform_device * __init scan_one_device(struct device_node *dp,
  282. struct device *parent)
  283. {
  284. struct platform_device *op = kzalloc(sizeof(*op), GFP_KERNEL);
  285. const struct linux_prom_irqs *intr;
  286. struct dev_archdata *sd;
  287. int len, i;
  288. if (!op)
  289. return NULL;
  290. sd = &op->dev.archdata;
  291. sd->op = op;
  292. op->dev.of_node = dp;
  293. intr = of_get_property(dp, "intr", &len);
  294. if (intr) {
  295. op->archdata.num_irqs = len / sizeof(struct linux_prom_irqs);
  296. for (i = 0; i < op->archdata.num_irqs; i++)
  297. op->archdata.irqs[i] =
  298. sparc_config.build_device_irq(op, intr[i].pri);
  299. } else {
  300. const unsigned int *irq =
  301. of_get_property(dp, "interrupts", &len);
  302. if (irq) {
  303. op->archdata.num_irqs = len / sizeof(unsigned int);
  304. for (i = 0; i < op->archdata.num_irqs; i++)
  305. op->archdata.irqs[i] =
  306. sparc_config.build_device_irq(op, irq[i]);
  307. } else {
  308. op->archdata.num_irqs = 0;
  309. }
  310. }
  311. build_device_resources(op, parent);
  312. op->dev.parent = parent;
  313. op->dev.bus = &platform_bus_type;
  314. if (!parent)
  315. dev_set_name(&op->dev, "root");
  316. else
  317. dev_set_name(&op->dev, "%08x", dp->phandle);
  318. if (of_device_register(op)) {
  319. printk("%s: Could not register of device.\n",
  320. dp->full_name);
  321. kfree(op);
  322. op = NULL;
  323. }
  324. return op;
  325. }
  326. static void __init scan_tree(struct device_node *dp, struct device *parent)
  327. {
  328. while (dp) {
  329. struct platform_device *op = scan_one_device(dp, parent);
  330. if (op)
  331. scan_tree(dp->child, &op->dev);
  332. dp = dp->sibling;
  333. }
  334. }
  335. static int __init scan_of_devices(void)
  336. {
  337. struct device_node *root = of_find_node_by_path("/");
  338. struct platform_device *parent;
  339. parent = scan_one_device(root, NULL);
  340. if (!parent)
  341. return 0;
  342. scan_tree(root->child, &parent->dev);
  343. return 0;
  344. }
  345. postcore_initcall(scan_of_devices);
  346. static int __init of_debug(char *str)
  347. {
  348. int val = 0;
  349. get_option(&str, &val);
  350. if (val & 1)
  351. of_resource_verbose = 1;
  352. return 1;
  353. }
  354. __setup("of_debug=", of_debug);