st_slim_rproc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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%pK\n",
  154. da, len, va);
  155. return va;
  156. }
  157. static const struct rproc_ops slim_rproc_ops = {
  158. .start = slim_rproc_start,
  159. .stop = slim_rproc_stop,
  160. .da_to_va = slim_rproc_da_to_va,
  161. .get_boot_addr = rproc_elf_get_boot_addr,
  162. .load = rproc_elf_load_segments,
  163. .sanity_check = rproc_elf_sanity_check,
  164. };
  165. /**
  166. * st_slim_rproc_alloc() - allocate and initialise slim rproc
  167. * @pdev: Pointer to the platform_device struct
  168. * @fw_name: Name of firmware for rproc to use
  169. *
  170. * Function for allocating and initialising a slim rproc for use by
  171. * device drivers whose IP is based around the SLIM core. It
  172. * obtains and enables any clocks required by the SLIM core and also
  173. * ioremaps the various IO.
  174. *
  175. * Returns st_slim_rproc pointer or PTR_ERR() on error.
  176. */
  177. struct st_slim_rproc *st_slim_rproc_alloc(struct platform_device *pdev,
  178. char *fw_name)
  179. {
  180. struct device *dev = &pdev->dev;
  181. struct st_slim_rproc *slim_rproc;
  182. struct device_node *np = dev->of_node;
  183. struct rproc *rproc;
  184. struct resource *res;
  185. int err, i;
  186. if (!fw_name)
  187. return ERR_PTR(-EINVAL);
  188. if (!of_device_is_compatible(np, "st,slim-rproc"))
  189. return ERR_PTR(-EINVAL);
  190. rproc = rproc_alloc(dev, np->name, &slim_rproc_ops,
  191. fw_name, sizeof(*slim_rproc));
  192. if (!rproc)
  193. return ERR_PTR(-ENOMEM);
  194. rproc->has_iommu = false;
  195. slim_rproc = rproc->priv;
  196. slim_rproc->rproc = rproc;
  197. /* get imem and dmem */
  198. for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
  199. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  200. mem_names[i]);
  201. slim_rproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
  202. if (IS_ERR(slim_rproc->mem[i].cpu_addr)) {
  203. dev_err(&pdev->dev, "devm_ioremap_resource failed\n");
  204. err = PTR_ERR(slim_rproc->mem[i].cpu_addr);
  205. goto err;
  206. }
  207. slim_rproc->mem[i].bus_addr = res->start;
  208. slim_rproc->mem[i].size = resource_size(res);
  209. }
  210. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "slimcore");
  211. slim_rproc->slimcore = devm_ioremap_resource(dev, res);
  212. if (IS_ERR(slim_rproc->slimcore)) {
  213. dev_err(&pdev->dev, "failed to ioremap slimcore IO\n");
  214. err = PTR_ERR(slim_rproc->slimcore);
  215. goto err;
  216. }
  217. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "peripherals");
  218. slim_rproc->peri = devm_ioremap_resource(dev, res);
  219. if (IS_ERR(slim_rproc->peri)) {
  220. dev_err(&pdev->dev, "failed to ioremap peripherals IO\n");
  221. err = PTR_ERR(slim_rproc->peri);
  222. goto err;
  223. }
  224. err = slim_clk_get(slim_rproc, dev);
  225. if (err)
  226. goto err;
  227. err = slim_clk_enable(slim_rproc);
  228. if (err) {
  229. dev_err(dev, "Failed to enable clocks\n");
  230. goto err_clk_put;
  231. }
  232. /* Register as a remoteproc device */
  233. err = rproc_add(rproc);
  234. if (err) {
  235. dev_err(dev, "registration of slim remoteproc failed\n");
  236. goto err_clk_dis;
  237. }
  238. return slim_rproc;
  239. err_clk_dis:
  240. slim_clk_disable(slim_rproc);
  241. err_clk_put:
  242. for (i = 0; i < ST_SLIM_MAX_CLK && slim_rproc->clks[i]; i++)
  243. clk_put(slim_rproc->clks[i]);
  244. err:
  245. rproc_free(rproc);
  246. return ERR_PTR(err);
  247. }
  248. EXPORT_SYMBOL(st_slim_rproc_alloc);
  249. /**
  250. * st_slim_rproc_put() - put slim rproc resources
  251. * @slim_rproc: Pointer to the st_slim_rproc struct
  252. *
  253. * Function for calling respective _put() functions on slim_rproc resources.
  254. *
  255. */
  256. void st_slim_rproc_put(struct st_slim_rproc *slim_rproc)
  257. {
  258. int clk;
  259. if (!slim_rproc)
  260. return;
  261. slim_clk_disable(slim_rproc);
  262. for (clk = 0; clk < ST_SLIM_MAX_CLK && slim_rproc->clks[clk]; clk++)
  263. clk_put(slim_rproc->clks[clk]);
  264. rproc_del(slim_rproc->rproc);
  265. rproc_free(slim_rproc->rproc);
  266. }
  267. EXPORT_SYMBOL(st_slim_rproc_put);
  268. MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
  269. MODULE_DESCRIPTION("STMicroelectronics SLIM core rproc driver");
  270. MODULE_LICENSE("GPL v2");