st_slim_rproc.c 8.5 KB

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