of_device_32.c 9.0 KB

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