pruss.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PRU-ICSS platform driver for various TI SoCs
  4. *
  5. * Copyright (C) 2014-2020 Texas Instruments Incorporated - http://www.ti.com/
  6. * Author(s):
  7. * Suman Anna <[email protected]>
  8. * Andrew F. Davis <[email protected]>
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/io.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/pruss_driver.h>
  19. #include <linux/regmap.h>
  20. #include <linux/slab.h>
  21. /**
  22. * struct pruss_private_data - PRUSS driver private data
  23. * @has_no_sharedram: flag to indicate the absence of PRUSS Shared Data RAM
  24. * @has_core_mux_clock: flag to indicate the presence of PRUSS core clock
  25. */
  26. struct pruss_private_data {
  27. bool has_no_sharedram;
  28. bool has_core_mux_clock;
  29. };
  30. static void pruss_of_free_clk_provider(void *data)
  31. {
  32. struct device_node *clk_mux_np = data;
  33. of_clk_del_provider(clk_mux_np);
  34. of_node_put(clk_mux_np);
  35. }
  36. static int pruss_clk_mux_setup(struct pruss *pruss, struct clk *clk_mux,
  37. char *mux_name, struct device_node *clks_np)
  38. {
  39. struct device_node *clk_mux_np;
  40. struct device *dev = pruss->dev;
  41. char *clk_mux_name;
  42. unsigned int num_parents;
  43. const char **parent_names;
  44. void __iomem *reg;
  45. u32 reg_offset;
  46. int ret;
  47. clk_mux_np = of_get_child_by_name(clks_np, mux_name);
  48. if (!clk_mux_np) {
  49. dev_err(dev, "%pOF is missing its '%s' node\n", clks_np,
  50. mux_name);
  51. return -ENODEV;
  52. }
  53. num_parents = of_clk_get_parent_count(clk_mux_np);
  54. if (num_parents < 1) {
  55. dev_err(dev, "mux-clock %pOF must have parents\n", clk_mux_np);
  56. ret = -EINVAL;
  57. goto put_clk_mux_np;
  58. }
  59. parent_names = devm_kcalloc(dev, sizeof(*parent_names), num_parents,
  60. GFP_KERNEL);
  61. if (!parent_names) {
  62. ret = -ENOMEM;
  63. goto put_clk_mux_np;
  64. }
  65. of_clk_parent_fill(clk_mux_np, parent_names, num_parents);
  66. clk_mux_name = devm_kasprintf(dev, GFP_KERNEL, "%s.%pOFn",
  67. dev_name(dev), clk_mux_np);
  68. if (!clk_mux_name) {
  69. ret = -ENOMEM;
  70. goto put_clk_mux_np;
  71. }
  72. ret = of_property_read_u32(clk_mux_np, "reg", &reg_offset);
  73. if (ret)
  74. goto put_clk_mux_np;
  75. reg = pruss->cfg_base + reg_offset;
  76. clk_mux = clk_register_mux(NULL, clk_mux_name, parent_names,
  77. num_parents, 0, reg, 0, 1, 0, NULL);
  78. if (IS_ERR(clk_mux)) {
  79. ret = PTR_ERR(clk_mux);
  80. goto put_clk_mux_np;
  81. }
  82. ret = devm_add_action_or_reset(dev, (void(*)(void *))clk_unregister_mux,
  83. clk_mux);
  84. if (ret) {
  85. dev_err(dev, "failed to add clkmux unregister action %d", ret);
  86. goto put_clk_mux_np;
  87. }
  88. ret = of_clk_add_provider(clk_mux_np, of_clk_src_simple_get, clk_mux);
  89. if (ret)
  90. goto put_clk_mux_np;
  91. ret = devm_add_action_or_reset(dev, pruss_of_free_clk_provider,
  92. clk_mux_np);
  93. if (ret) {
  94. dev_err(dev, "failed to add clkmux free action %d", ret);
  95. goto put_clk_mux_np;
  96. }
  97. return 0;
  98. put_clk_mux_np:
  99. of_node_put(clk_mux_np);
  100. return ret;
  101. }
  102. static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
  103. {
  104. const struct pruss_private_data *data;
  105. struct device_node *clks_np;
  106. struct device *dev = pruss->dev;
  107. int ret = 0;
  108. data = of_device_get_match_data(dev);
  109. clks_np = of_get_child_by_name(cfg_node, "clocks");
  110. if (!clks_np) {
  111. dev_err(dev, "%pOF is missing its 'clocks' node\n", cfg_node);
  112. return -ENODEV;
  113. }
  114. if (data && data->has_core_mux_clock) {
  115. ret = pruss_clk_mux_setup(pruss, pruss->core_clk_mux,
  116. "coreclk-mux", clks_np);
  117. if (ret) {
  118. dev_err(dev, "failed to setup coreclk-mux\n");
  119. goto put_clks_node;
  120. }
  121. }
  122. ret = pruss_clk_mux_setup(pruss, pruss->iep_clk_mux, "iepclk-mux",
  123. clks_np);
  124. if (ret) {
  125. dev_err(dev, "failed to setup iepclk-mux\n");
  126. goto put_clks_node;
  127. }
  128. put_clks_node:
  129. of_node_put(clks_np);
  130. return ret;
  131. }
  132. static struct regmap_config regmap_conf = {
  133. .reg_bits = 32,
  134. .val_bits = 32,
  135. .reg_stride = 4,
  136. };
  137. static int pruss_cfg_of_init(struct device *dev, struct pruss *pruss)
  138. {
  139. struct device_node *np = dev_of_node(dev);
  140. struct device_node *child;
  141. struct resource res;
  142. int ret;
  143. child = of_get_child_by_name(np, "cfg");
  144. if (!child) {
  145. dev_err(dev, "%pOF is missing its 'cfg' node\n", child);
  146. return -ENODEV;
  147. }
  148. if (of_address_to_resource(child, 0, &res)) {
  149. ret = -ENOMEM;
  150. goto node_put;
  151. }
  152. pruss->cfg_base = devm_ioremap(dev, res.start, resource_size(&res));
  153. if (!pruss->cfg_base) {
  154. ret = -ENOMEM;
  155. goto node_put;
  156. }
  157. regmap_conf.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", child,
  158. (u64)res.start);
  159. regmap_conf.max_register = resource_size(&res) - 4;
  160. pruss->cfg_regmap = devm_regmap_init_mmio(dev, pruss->cfg_base,
  161. &regmap_conf);
  162. kfree(regmap_conf.name);
  163. if (IS_ERR(pruss->cfg_regmap)) {
  164. dev_err(dev, "regmap_init_mmio failed for cfg, ret = %ld\n",
  165. PTR_ERR(pruss->cfg_regmap));
  166. ret = PTR_ERR(pruss->cfg_regmap);
  167. goto node_put;
  168. }
  169. ret = pruss_clk_init(pruss, child);
  170. if (ret)
  171. dev_err(dev, "pruss_clk_init failed, ret = %d\n", ret);
  172. node_put:
  173. of_node_put(child);
  174. return ret;
  175. }
  176. static int pruss_probe(struct platform_device *pdev)
  177. {
  178. struct device *dev = &pdev->dev;
  179. struct device_node *np = dev_of_node(dev);
  180. struct device_node *child;
  181. struct pruss *pruss;
  182. struct resource res;
  183. int ret, i, index;
  184. const struct pruss_private_data *data;
  185. const char *mem_names[PRUSS_MEM_MAX] = { "dram0", "dram1", "shrdram2" };
  186. data = of_device_get_match_data(&pdev->dev);
  187. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  188. if (ret) {
  189. dev_err(dev, "failed to set the DMA coherent mask");
  190. return ret;
  191. }
  192. pruss = devm_kzalloc(dev, sizeof(*pruss), GFP_KERNEL);
  193. if (!pruss)
  194. return -ENOMEM;
  195. pruss->dev = dev;
  196. child = of_get_child_by_name(np, "memories");
  197. if (!child) {
  198. dev_err(dev, "%pOF is missing its 'memories' node\n", child);
  199. return -ENODEV;
  200. }
  201. for (i = 0; i < PRUSS_MEM_MAX; i++) {
  202. /*
  203. * On AM437x one of two PRUSS units don't contain Shared RAM,
  204. * skip it
  205. */
  206. if (data && data->has_no_sharedram && i == PRUSS_MEM_SHRD_RAM2)
  207. continue;
  208. index = of_property_match_string(child, "reg-names",
  209. mem_names[i]);
  210. if (index < 0) {
  211. of_node_put(child);
  212. return index;
  213. }
  214. if (of_address_to_resource(child, index, &res)) {
  215. of_node_put(child);
  216. return -EINVAL;
  217. }
  218. pruss->mem_regions[i].va = devm_ioremap(dev, res.start,
  219. resource_size(&res));
  220. if (!pruss->mem_regions[i].va) {
  221. dev_err(dev, "failed to parse and map memory resource %d %s\n",
  222. i, mem_names[i]);
  223. of_node_put(child);
  224. return -ENOMEM;
  225. }
  226. pruss->mem_regions[i].pa = res.start;
  227. pruss->mem_regions[i].size = resource_size(&res);
  228. dev_dbg(dev, "memory %8s: pa %pa size 0x%zx va %pK\n",
  229. mem_names[i], &pruss->mem_regions[i].pa,
  230. pruss->mem_regions[i].size, pruss->mem_regions[i].va);
  231. }
  232. of_node_put(child);
  233. platform_set_drvdata(pdev, pruss);
  234. pm_runtime_enable(dev);
  235. ret = pm_runtime_resume_and_get(dev);
  236. if (ret < 0) {
  237. dev_err(dev, "couldn't enable module\n");
  238. goto rpm_disable;
  239. }
  240. ret = pruss_cfg_of_init(dev, pruss);
  241. if (ret < 0)
  242. goto rpm_put;
  243. ret = devm_of_platform_populate(dev);
  244. if (ret) {
  245. dev_err(dev, "failed to register child devices\n");
  246. goto rpm_put;
  247. }
  248. return 0;
  249. rpm_put:
  250. pm_runtime_put_sync(dev);
  251. rpm_disable:
  252. pm_runtime_disable(dev);
  253. return ret;
  254. }
  255. static int pruss_remove(struct platform_device *pdev)
  256. {
  257. struct device *dev = &pdev->dev;
  258. devm_of_platform_depopulate(dev);
  259. pm_runtime_put_sync(dev);
  260. pm_runtime_disable(dev);
  261. return 0;
  262. }
  263. /* instance-specific driver private data */
  264. static const struct pruss_private_data am437x_pruss1_data = {
  265. .has_no_sharedram = false,
  266. };
  267. static const struct pruss_private_data am437x_pruss0_data = {
  268. .has_no_sharedram = true,
  269. };
  270. static const struct pruss_private_data am65x_j721e_pruss_data = {
  271. .has_core_mux_clock = true,
  272. };
  273. static const struct of_device_id pruss_of_match[] = {
  274. { .compatible = "ti,am3356-pruss" },
  275. { .compatible = "ti,am4376-pruss0", .data = &am437x_pruss0_data, },
  276. { .compatible = "ti,am4376-pruss1", .data = &am437x_pruss1_data, },
  277. { .compatible = "ti,am5728-pruss" },
  278. { .compatible = "ti,k2g-pruss" },
  279. { .compatible = "ti,am654-icssg", .data = &am65x_j721e_pruss_data, },
  280. { .compatible = "ti,j721e-icssg", .data = &am65x_j721e_pruss_data, },
  281. { .compatible = "ti,am642-icssg", .data = &am65x_j721e_pruss_data, },
  282. { .compatible = "ti,am625-pruss", .data = &am65x_j721e_pruss_data, },
  283. {},
  284. };
  285. MODULE_DEVICE_TABLE(of, pruss_of_match);
  286. static struct platform_driver pruss_driver = {
  287. .driver = {
  288. .name = "pruss",
  289. .of_match_table = pruss_of_match,
  290. },
  291. .probe = pruss_probe,
  292. .remove = pruss_remove,
  293. };
  294. module_platform_driver(pruss_driver);
  295. MODULE_AUTHOR("Suman Anna <[email protected]>");
  296. MODULE_DESCRIPTION("PRU-ICSS Subsystem Driver");
  297. MODULE_LICENSE("GPL v2");