imx_rproc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (c) 2017 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/err.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/remoteproc.h>
  19. #define IMX7D_SRC_SCR 0x0C
  20. #define IMX7D_ENABLE_M4 BIT(3)
  21. #define IMX7D_SW_M4P_RST BIT(2)
  22. #define IMX7D_SW_M4C_RST BIT(1)
  23. #define IMX7D_SW_M4C_NON_SCLR_RST BIT(0)
  24. #define IMX7D_M4_RST_MASK (IMX7D_ENABLE_M4 | IMX7D_SW_M4P_RST \
  25. | IMX7D_SW_M4C_RST \
  26. | IMX7D_SW_M4C_NON_SCLR_RST)
  27. #define IMX7D_M4_START (IMX7D_ENABLE_M4 | IMX7D_SW_M4P_RST \
  28. | IMX7D_SW_M4C_RST)
  29. #define IMX7D_M4_STOP IMX7D_SW_M4C_NON_SCLR_RST
  30. /* Address: 0x020D8000 */
  31. #define IMX6SX_SRC_SCR 0x00
  32. #define IMX6SX_ENABLE_M4 BIT(22)
  33. #define IMX6SX_SW_M4P_RST BIT(12)
  34. #define IMX6SX_SW_M4C_NON_SCLR_RST BIT(4)
  35. #define IMX6SX_SW_M4C_RST BIT(3)
  36. #define IMX6SX_M4_START (IMX6SX_ENABLE_M4 | IMX6SX_SW_M4P_RST \
  37. | IMX6SX_SW_M4C_RST)
  38. #define IMX6SX_M4_STOP IMX6SX_SW_M4C_NON_SCLR_RST
  39. #define IMX6SX_M4_RST_MASK (IMX6SX_ENABLE_M4 | IMX6SX_SW_M4P_RST \
  40. | IMX6SX_SW_M4C_NON_SCLR_RST \
  41. | IMX6SX_SW_M4C_RST)
  42. #define IMX7D_RPROC_MEM_MAX 8
  43. /**
  44. * struct imx_rproc_mem - slim internal memory structure
  45. * @cpu_addr: MPU virtual address of the memory region
  46. * @sys_addr: Bus address used to access the memory region
  47. * @size: Size of the memory region
  48. */
  49. struct imx_rproc_mem {
  50. void __iomem *cpu_addr;
  51. phys_addr_t sys_addr;
  52. size_t size;
  53. };
  54. /* att flags */
  55. /* M4 own area. Can be mapped at probe */
  56. #define ATT_OWN BIT(1)
  57. /* address translation table */
  58. struct imx_rproc_att {
  59. u32 da; /* device address (From Cortex M4 view)*/
  60. u32 sa; /* system bus address */
  61. u32 size; /* size of reg range */
  62. int flags;
  63. };
  64. struct imx_rproc_dcfg {
  65. u32 src_reg;
  66. u32 src_mask;
  67. u32 src_start;
  68. u32 src_stop;
  69. const struct imx_rproc_att *att;
  70. size_t att_size;
  71. };
  72. struct imx_rproc {
  73. struct device *dev;
  74. struct regmap *regmap;
  75. struct rproc *rproc;
  76. const struct imx_rproc_dcfg *dcfg;
  77. struct imx_rproc_mem mem[IMX7D_RPROC_MEM_MAX];
  78. struct clk *clk;
  79. };
  80. static const struct imx_rproc_att imx_rproc_att_imx7d[] = {
  81. /* dev addr , sys addr , size , flags */
  82. /* OCRAM_S (M4 Boot code) - alias */
  83. { 0x00000000, 0x00180000, 0x00008000, 0 },
  84. /* OCRAM_S (Code) */
  85. { 0x00180000, 0x00180000, 0x00008000, ATT_OWN },
  86. /* OCRAM (Code) - alias */
  87. { 0x00900000, 0x00900000, 0x00020000, 0 },
  88. /* OCRAM_EPDC (Code) - alias */
  89. { 0x00920000, 0x00920000, 0x00020000, 0 },
  90. /* OCRAM_PXP (Code) - alias */
  91. { 0x00940000, 0x00940000, 0x00008000, 0 },
  92. /* TCML (Code) */
  93. { 0x1FFF8000, 0x007F8000, 0x00008000, ATT_OWN },
  94. /* DDR (Code) - alias, first part of DDR (Data) */
  95. { 0x10000000, 0x80000000, 0x0FFF0000, 0 },
  96. /* TCMU (Data) */
  97. { 0x20000000, 0x00800000, 0x00008000, ATT_OWN },
  98. /* OCRAM (Data) */
  99. { 0x20200000, 0x00900000, 0x00020000, 0 },
  100. /* OCRAM_EPDC (Data) */
  101. { 0x20220000, 0x00920000, 0x00020000, 0 },
  102. /* OCRAM_PXP (Data) */
  103. { 0x20240000, 0x00940000, 0x00008000, 0 },
  104. /* DDR (Data) */
  105. { 0x80000000, 0x80000000, 0x60000000, 0 },
  106. };
  107. static const struct imx_rproc_att imx_rproc_att_imx6sx[] = {
  108. /* dev addr , sys addr , size , flags */
  109. /* TCML (M4 Boot Code) - alias */
  110. { 0x00000000, 0x007F8000, 0x00008000, 0 },
  111. /* OCRAM_S (Code) */
  112. { 0x00180000, 0x008F8000, 0x00004000, 0 },
  113. /* OCRAM_S (Code) - alias */
  114. { 0x00180000, 0x008FC000, 0x00004000, 0 },
  115. /* TCML (Code) */
  116. { 0x1FFF8000, 0x007F8000, 0x00008000, ATT_OWN },
  117. /* DDR (Code) - alias, first part of DDR (Data) */
  118. { 0x10000000, 0x80000000, 0x0FFF8000, 0 },
  119. /* TCMU (Data) */
  120. { 0x20000000, 0x00800000, 0x00008000, ATT_OWN },
  121. /* OCRAM_S (Data) - alias? */
  122. { 0x208F8000, 0x008F8000, 0x00004000, 0 },
  123. /* DDR (Data) */
  124. { 0x80000000, 0x80000000, 0x60000000, 0 },
  125. };
  126. static const struct imx_rproc_dcfg imx_rproc_cfg_imx7d = {
  127. .src_reg = IMX7D_SRC_SCR,
  128. .src_mask = IMX7D_M4_RST_MASK,
  129. .src_start = IMX7D_M4_START,
  130. .src_stop = IMX7D_M4_STOP,
  131. .att = imx_rproc_att_imx7d,
  132. .att_size = ARRAY_SIZE(imx_rproc_att_imx7d),
  133. };
  134. static const struct imx_rproc_dcfg imx_rproc_cfg_imx6sx = {
  135. .src_reg = IMX6SX_SRC_SCR,
  136. .src_mask = IMX6SX_M4_RST_MASK,
  137. .src_start = IMX6SX_M4_START,
  138. .src_stop = IMX6SX_M4_STOP,
  139. .att = imx_rproc_att_imx6sx,
  140. .att_size = ARRAY_SIZE(imx_rproc_att_imx6sx),
  141. };
  142. static int imx_rproc_start(struct rproc *rproc)
  143. {
  144. struct imx_rproc *priv = rproc->priv;
  145. const struct imx_rproc_dcfg *dcfg = priv->dcfg;
  146. struct device *dev = priv->dev;
  147. int ret;
  148. ret = regmap_update_bits(priv->regmap, dcfg->src_reg,
  149. dcfg->src_mask, dcfg->src_start);
  150. if (ret)
  151. dev_err(dev, "Filed to enable M4!\n");
  152. return ret;
  153. }
  154. static int imx_rproc_stop(struct rproc *rproc)
  155. {
  156. struct imx_rproc *priv = rproc->priv;
  157. const struct imx_rproc_dcfg *dcfg = priv->dcfg;
  158. struct device *dev = priv->dev;
  159. int ret;
  160. ret = regmap_update_bits(priv->regmap, dcfg->src_reg,
  161. dcfg->src_mask, dcfg->src_stop);
  162. if (ret)
  163. dev_err(dev, "Filed to stop M4!\n");
  164. return ret;
  165. }
  166. static int imx_rproc_da_to_sys(struct imx_rproc *priv, u64 da,
  167. int len, u64 *sys)
  168. {
  169. const struct imx_rproc_dcfg *dcfg = priv->dcfg;
  170. int i;
  171. /* parse address translation table */
  172. for (i = 0; i < dcfg->att_size; i++) {
  173. const struct imx_rproc_att *att = &dcfg->att[i];
  174. if (da >= att->da && da + len < att->da + att->size) {
  175. unsigned int offset = da - att->da;
  176. *sys = att->sa + offset;
  177. return 0;
  178. }
  179. }
  180. dev_warn(priv->dev, "Translation filed: da = 0x%llx len = 0x%x\n",
  181. da, len);
  182. return -ENOENT;
  183. }
  184. static void *imx_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
  185. {
  186. struct imx_rproc *priv = rproc->priv;
  187. void *va = NULL;
  188. u64 sys;
  189. int i;
  190. if (len <= 0)
  191. return NULL;
  192. /*
  193. * On device side we have many aliases, so we need to convert device
  194. * address (M4) to system bus address first.
  195. */
  196. if (imx_rproc_da_to_sys(priv, da, len, &sys))
  197. return NULL;
  198. for (i = 0; i < IMX7D_RPROC_MEM_MAX; i++) {
  199. if (sys >= priv->mem[i].sys_addr && sys + len <
  200. priv->mem[i].sys_addr + priv->mem[i].size) {
  201. unsigned int offset = sys - priv->mem[i].sys_addr;
  202. /* __force to make sparse happy with type conversion */
  203. va = (__force void *)(priv->mem[i].cpu_addr + offset);
  204. break;
  205. }
  206. }
  207. dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%x va = 0x%p\n", da, len, va);
  208. return va;
  209. }
  210. static const struct rproc_ops imx_rproc_ops = {
  211. .start = imx_rproc_start,
  212. .stop = imx_rproc_stop,
  213. .da_to_va = imx_rproc_da_to_va,
  214. };
  215. static int imx_rproc_addr_init(struct imx_rproc *priv,
  216. struct platform_device *pdev)
  217. {
  218. const struct imx_rproc_dcfg *dcfg = priv->dcfg;
  219. struct device *dev = &pdev->dev;
  220. struct device_node *np = dev->of_node;
  221. int a, b = 0, err, nph;
  222. /* remap required addresses */
  223. for (a = 0; a < dcfg->att_size; a++) {
  224. const struct imx_rproc_att *att = &dcfg->att[a];
  225. if (!(att->flags & ATT_OWN))
  226. continue;
  227. if (b >= IMX7D_RPROC_MEM_MAX)
  228. break;
  229. priv->mem[b].cpu_addr = devm_ioremap(&pdev->dev,
  230. att->sa, att->size);
  231. if (!priv->mem[b].cpu_addr) {
  232. dev_err(dev, "devm_ioremap_resource failed\n");
  233. return -ENOMEM;
  234. }
  235. priv->mem[b].sys_addr = att->sa;
  236. priv->mem[b].size = att->size;
  237. b++;
  238. }
  239. /* memory-region is optional property */
  240. nph = of_count_phandle_with_args(np, "memory-region", NULL);
  241. if (nph <= 0)
  242. return 0;
  243. /* remap optional addresses */
  244. for (a = 0; a < nph; a++) {
  245. struct device_node *node;
  246. struct resource res;
  247. node = of_parse_phandle(np, "memory-region", a);
  248. err = of_address_to_resource(node, 0, &res);
  249. if (err) {
  250. dev_err(dev, "unable to resolve memory region\n");
  251. return err;
  252. }
  253. if (b >= IMX7D_RPROC_MEM_MAX)
  254. break;
  255. priv->mem[b].cpu_addr = devm_ioremap_resource(&pdev->dev, &res);
  256. if (IS_ERR(priv->mem[b].cpu_addr)) {
  257. dev_err(dev, "devm_ioremap_resource failed\n");
  258. err = PTR_ERR(priv->mem[b].cpu_addr);
  259. return err;
  260. }
  261. priv->mem[b].sys_addr = res.start;
  262. priv->mem[b].size = resource_size(&res);
  263. b++;
  264. }
  265. return 0;
  266. }
  267. static int imx_rproc_probe(struct platform_device *pdev)
  268. {
  269. struct device *dev = &pdev->dev;
  270. struct device_node *np = dev->of_node;
  271. struct imx_rproc *priv;
  272. struct rproc *rproc;
  273. struct regmap_config config = { .name = "imx-rproc" };
  274. const struct imx_rproc_dcfg *dcfg;
  275. struct regmap *regmap;
  276. int ret;
  277. regmap = syscon_regmap_lookup_by_phandle(np, "syscon");
  278. if (IS_ERR(regmap)) {
  279. dev_err(dev, "failed to find syscon\n");
  280. return PTR_ERR(regmap);
  281. }
  282. regmap_attach_dev(dev, regmap, &config);
  283. /* set some other name then imx */
  284. rproc = rproc_alloc(dev, "imx-rproc", &imx_rproc_ops,
  285. NULL, sizeof(*priv));
  286. if (!rproc)
  287. return -ENOMEM;
  288. dcfg = of_device_get_match_data(dev);
  289. if (!dcfg) {
  290. ret = -EINVAL;
  291. goto err_put_rproc;
  292. }
  293. priv = rproc->priv;
  294. priv->rproc = rproc;
  295. priv->regmap = regmap;
  296. priv->dcfg = dcfg;
  297. priv->dev = dev;
  298. dev_set_drvdata(dev, rproc);
  299. ret = imx_rproc_addr_init(priv, pdev);
  300. if (ret) {
  301. dev_err(dev, "filed on imx_rproc_addr_init\n");
  302. goto err_put_rproc;
  303. }
  304. priv->clk = devm_clk_get(dev, NULL);
  305. if (IS_ERR(priv->clk)) {
  306. dev_err(dev, "Failed to get clock\n");
  307. ret = PTR_ERR(priv->clk);
  308. goto err_put_rproc;
  309. }
  310. /*
  311. * clk for M4 block including memory. Should be
  312. * enabled before .start for FW transfer.
  313. */
  314. ret = clk_prepare_enable(priv->clk);
  315. if (ret) {
  316. dev_err(&rproc->dev, "Failed to enable clock\n");
  317. goto err_put_rproc;
  318. }
  319. ret = rproc_add(rproc);
  320. if (ret) {
  321. dev_err(dev, "rproc_add failed\n");
  322. goto err_put_clk;
  323. }
  324. return 0;
  325. err_put_clk:
  326. clk_disable_unprepare(priv->clk);
  327. err_put_rproc:
  328. rproc_free(rproc);
  329. return ret;
  330. }
  331. static int imx_rproc_remove(struct platform_device *pdev)
  332. {
  333. struct rproc *rproc = platform_get_drvdata(pdev);
  334. struct imx_rproc *priv = rproc->priv;
  335. clk_disable_unprepare(priv->clk);
  336. rproc_del(rproc);
  337. rproc_free(rproc);
  338. return 0;
  339. }
  340. static const struct of_device_id imx_rproc_of_match[] = {
  341. { .compatible = "fsl,imx7d-cm4", .data = &imx_rproc_cfg_imx7d },
  342. { .compatible = "fsl,imx6sx-cm4", .data = &imx_rproc_cfg_imx6sx },
  343. {},
  344. };
  345. MODULE_DEVICE_TABLE(of, imx_rproc_of_match);
  346. static struct platform_driver imx_rproc_driver = {
  347. .probe = imx_rproc_probe,
  348. .remove = imx_rproc_remove,
  349. .driver = {
  350. .name = "imx-rproc",
  351. .of_match_table = imx_rproc_of_match,
  352. },
  353. };
  354. module_platform_driver(imx_rproc_driver);
  355. MODULE_LICENSE("GPL v2");
  356. MODULE_DESCRIPTION("IMX6SX/7D remote processor control driver");
  357. MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");