st_slim_rproc.c 9.0 KB

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