st_slim_rproc.c 8.2 KB

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