st_slim_rproc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SLIM core rproc driver
  4. *
  5. * Copyright (C) 2016 STMicroelectronics
  6. *
  7. * Author: Peter Griffin <peter.griffin@linaro.org>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/err.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/remoteproc.h>
  17. #include <linux/remoteproc/st_slim_rproc.h>
  18. #include "remoteproc_internal.h"
  19. /* SLIM core registers */
  20. #define SLIM_ID_OFST 0x0
  21. #define SLIM_VER_OFST 0x4
  22. #define SLIM_EN_OFST 0x8
  23. #define SLIM_EN_RUN BIT(0)
  24. #define SLIM_CLK_GATE_OFST 0xC
  25. #define SLIM_CLK_GATE_DIS BIT(0)
  26. #define SLIM_CLK_GATE_RESET BIT(2)
  27. #define SLIM_SLIM_PC_OFST 0x20
  28. /* DMEM registers */
  29. #define SLIM_REV_ID_OFST 0x0
  30. #define SLIM_REV_ID_MIN_MASK GENMASK(15, 8)
  31. #define SLIM_REV_ID_MIN(id) ((id & SLIM_REV_ID_MIN_MASK) >> 8)
  32. #define SLIM_REV_ID_MAJ_MASK GENMASK(23, 16)
  33. #define SLIM_REV_ID_MAJ(id) ((id & SLIM_REV_ID_MAJ_MASK) >> 16)
  34. /* peripherals registers */
  35. #define SLIM_STBUS_SYNC_OFST 0xF88
  36. #define SLIM_STBUS_SYNC_DIS BIT(0)
  37. #define SLIM_INT_SET_OFST 0xFD4
  38. #define SLIM_INT_CLR_OFST 0xFD8
  39. #define SLIM_INT_MASK_OFST 0xFDC
  40. #define SLIM_CMD_CLR_OFST 0xFC8
  41. #define SLIM_CMD_MASK_OFST 0xFCC
  42. static const char *mem_names[ST_SLIM_MEM_MAX] = {
  43. [ST_SLIM_DMEM] = "dmem",
  44. [ST_SLIM_IMEM] = "imem",
  45. };
  46. static int slim_clk_get(struct st_slim_rproc *slim_rproc, struct device *dev)
  47. {
  48. int clk, err;
  49. for (clk = 0; clk < ST_SLIM_MAX_CLK; clk++) {
  50. slim_rproc->clks[clk] = of_clk_get(dev->of_node, clk);
  51. if (IS_ERR(slim_rproc->clks[clk])) {
  52. err = PTR_ERR(slim_rproc->clks[clk]);
  53. if (err == -EPROBE_DEFER)
  54. goto err_put_clks;
  55. slim_rproc->clks[clk] = NULL;
  56. break;
  57. }
  58. }
  59. return 0;
  60. err_put_clks:
  61. while (--clk >= 0)
  62. clk_put(slim_rproc->clks[clk]);
  63. return err;
  64. }
  65. static void slim_clk_disable(struct st_slim_rproc *slim_rproc)
  66. {
  67. int clk;
  68. for (clk = 0; clk < ST_SLIM_MAX_CLK && slim_rproc->clks[clk]; clk++)
  69. clk_disable_unprepare(slim_rproc->clks[clk]);
  70. }
  71. static int slim_clk_enable(struct st_slim_rproc *slim_rproc)
  72. {
  73. int clk, ret;
  74. for (clk = 0; clk < ST_SLIM_MAX_CLK && slim_rproc->clks[clk]; clk++) {
  75. ret = clk_prepare_enable(slim_rproc->clks[clk]);
  76. if (ret)
  77. goto err_disable_clks;
  78. }
  79. return 0;
  80. err_disable_clks:
  81. while (--clk >= 0)
  82. clk_disable_unprepare(slim_rproc->clks[clk]);
  83. return ret;
  84. }
  85. /*
  86. * Remoteproc slim specific device handlers
  87. */
  88. static int slim_rproc_start(struct rproc *rproc)
  89. {
  90. struct device *dev = &rproc->dev;
  91. struct st_slim_rproc *slim_rproc = rproc->priv;
  92. unsigned long hw_id, hw_ver, fw_rev;
  93. u32 val;
  94. /* disable CPU pipeline clock & reset CPU pipeline */
  95. val = SLIM_CLK_GATE_DIS | SLIM_CLK_GATE_RESET;
  96. writel(val, slim_rproc->slimcore + SLIM_CLK_GATE_OFST);
  97. /* disable SLIM core STBus sync */
  98. writel(SLIM_STBUS_SYNC_DIS, slim_rproc->peri + SLIM_STBUS_SYNC_OFST);
  99. /* enable cpu pipeline clock */
  100. writel(!SLIM_CLK_GATE_DIS,
  101. slim_rproc->slimcore + SLIM_CLK_GATE_OFST);
  102. /* clear int & cmd mailbox */
  103. writel(~0U, slim_rproc->peri + SLIM_INT_CLR_OFST);
  104. writel(~0U, slim_rproc->peri + SLIM_CMD_CLR_OFST);
  105. /* enable all channels cmd & int */
  106. writel(~0U, slim_rproc->peri + SLIM_INT_MASK_OFST);
  107. writel(~0U, slim_rproc->peri + SLIM_CMD_MASK_OFST);
  108. /* enable cpu */
  109. writel(SLIM_EN_RUN, slim_rproc->slimcore + SLIM_EN_OFST);
  110. hw_id = readl_relaxed(slim_rproc->slimcore + SLIM_ID_OFST);
  111. hw_ver = readl_relaxed(slim_rproc->slimcore + SLIM_VER_OFST);
  112. fw_rev = readl(slim_rproc->mem[ST_SLIM_DMEM].cpu_addr +
  113. SLIM_REV_ID_OFST);
  114. dev_info(dev, "fw rev:%ld.%ld on SLIM %ld.%ld\n",
  115. SLIM_REV_ID_MAJ(fw_rev), SLIM_REV_ID_MIN(fw_rev),
  116. hw_id, hw_ver);
  117. return 0;
  118. }
  119. static int slim_rproc_stop(struct rproc *rproc)
  120. {
  121. struct st_slim_rproc *slim_rproc = rproc->priv;
  122. u32 val;
  123. /* mask all (cmd & int) channels */
  124. writel(0UL, slim_rproc->peri + SLIM_INT_MASK_OFST);
  125. writel(0UL, slim_rproc->peri + SLIM_CMD_MASK_OFST);
  126. /* disable cpu pipeline clock */
  127. writel(SLIM_CLK_GATE_DIS, slim_rproc->slimcore + SLIM_CLK_GATE_OFST);
  128. writel(!SLIM_EN_RUN, slim_rproc->slimcore + SLIM_EN_OFST);
  129. val = readl(slim_rproc->slimcore + SLIM_EN_OFST);
  130. if (val & SLIM_EN_RUN)
  131. dev_warn(&rproc->dev, "Failed to disable SLIM");
  132. dev_dbg(&rproc->dev, "slim stopped\n");
  133. return 0;
  134. }
  135. static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
  136. {
  137. struct st_slim_rproc *slim_rproc = rproc->priv;
  138. void *va = NULL;
  139. int i;
  140. for (i = 0; i < ST_SLIM_MEM_MAX; i++) {
  141. if (da != slim_rproc->mem[i].bus_addr)
  142. continue;
  143. if (len <= slim_rproc->mem[i].size) {
  144. /* __force to make sparse happy with type conversion */
  145. va = (__force void *)slim_rproc->mem[i].cpu_addr;
  146. break;
  147. }
  148. }
  149. dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%zx va = 0x%pK\n",
  150. da, len, va);
  151. return va;
  152. }
  153. static const struct rproc_ops slim_rproc_ops = {
  154. .start = slim_rproc_start,
  155. .stop = slim_rproc_stop,
  156. .da_to_va = slim_rproc_da_to_va,
  157. .get_boot_addr = rproc_elf_get_boot_addr,
  158. .load = rproc_elf_load_segments,
  159. .sanity_check = rproc_elf_sanity_check,
  160. };
  161. /**
  162. * st_slim_rproc_alloc() - allocate and initialise slim rproc
  163. * @pdev: Pointer to the platform_device struct
  164. * @fw_name: Name of firmware for rproc to use
  165. *
  166. * Function for allocating and initialising a slim rproc for use by
  167. * device drivers whose IP is based around the SLIM core. It
  168. * obtains and enables any clocks required by the SLIM core and also
  169. * ioremaps the various IO.
  170. *
  171. * Returns st_slim_rproc pointer or PTR_ERR() on error.
  172. */
  173. struct st_slim_rproc *st_slim_rproc_alloc(struct platform_device *pdev,
  174. char *fw_name)
  175. {
  176. struct device *dev = &pdev->dev;
  177. struct st_slim_rproc *slim_rproc;
  178. struct device_node *np = dev->of_node;
  179. struct rproc *rproc;
  180. struct resource *res;
  181. int err, i;
  182. if (!fw_name)
  183. return ERR_PTR(-EINVAL);
  184. if (!of_device_is_compatible(np, "st,slim-rproc"))
  185. return ERR_PTR(-EINVAL);
  186. rproc = rproc_alloc(dev, np->name, &slim_rproc_ops,
  187. fw_name, sizeof(*slim_rproc));
  188. if (!rproc)
  189. return ERR_PTR(-ENOMEM);
  190. rproc->has_iommu = false;
  191. slim_rproc = rproc->priv;
  192. slim_rproc->rproc = rproc;
  193. /* get imem and dmem */
  194. for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
  195. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  196. mem_names[i]);
  197. slim_rproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
  198. if (IS_ERR(slim_rproc->mem[i].cpu_addr)) {
  199. dev_err(&pdev->dev, "devm_ioremap_resource failed\n");
  200. err = PTR_ERR(slim_rproc->mem[i].cpu_addr);
  201. goto err;
  202. }
  203. slim_rproc->mem[i].bus_addr = res->start;
  204. slim_rproc->mem[i].size = resource_size(res);
  205. }
  206. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "slimcore");
  207. slim_rproc->slimcore = devm_ioremap_resource(dev, res);
  208. if (IS_ERR(slim_rproc->slimcore)) {
  209. dev_err(&pdev->dev, "failed to ioremap slimcore IO\n");
  210. err = PTR_ERR(slim_rproc->slimcore);
  211. goto err;
  212. }
  213. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "peripherals");
  214. slim_rproc->peri = devm_ioremap_resource(dev, res);
  215. if (IS_ERR(slim_rproc->peri)) {
  216. dev_err(&pdev->dev, "failed to ioremap peripherals IO\n");
  217. err = PTR_ERR(slim_rproc->peri);
  218. goto err;
  219. }
  220. err = slim_clk_get(slim_rproc, dev);
  221. if (err)
  222. goto err;
  223. err = slim_clk_enable(slim_rproc);
  224. if (err) {
  225. dev_err(dev, "Failed to enable clocks\n");
  226. goto err_clk_put;
  227. }
  228. /* Register as a remoteproc device */
  229. err = rproc_add(rproc);
  230. if (err) {
  231. dev_err(dev, "registration of slim remoteproc failed\n");
  232. goto err_clk_dis;
  233. }
  234. return slim_rproc;
  235. err_clk_dis:
  236. slim_clk_disable(slim_rproc);
  237. err_clk_put:
  238. for (i = 0; i < ST_SLIM_MAX_CLK && slim_rproc->clks[i]; i++)
  239. clk_put(slim_rproc->clks[i]);
  240. err:
  241. rproc_free(rproc);
  242. return ERR_PTR(err);
  243. }
  244. EXPORT_SYMBOL(st_slim_rproc_alloc);
  245. /**
  246. * st_slim_rproc_put() - put slim rproc resources
  247. * @slim_rproc: Pointer to the st_slim_rproc struct
  248. *
  249. * Function for calling respective _put() functions on slim_rproc resources.
  250. *
  251. */
  252. void st_slim_rproc_put(struct st_slim_rproc *slim_rproc)
  253. {
  254. int clk;
  255. if (!slim_rproc)
  256. return;
  257. slim_clk_disable(slim_rproc);
  258. for (clk = 0; clk < ST_SLIM_MAX_CLK && slim_rproc->clks[clk]; clk++)
  259. clk_put(slim_rproc->clks[clk]);
  260. rproc_del(slim_rproc->rproc);
  261. rproc_free(slim_rproc->rproc);
  262. }
  263. EXPORT_SYMBOL(st_slim_rproc_put);
  264. MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
  265. MODULE_DESCRIPTION("STMicroelectronics SLIM core rproc driver");
  266. MODULE_LICENSE("GPL v2");