of_device_32.c 8.9 KB

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