stm32-romem.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * STM32 Factory-programmed memory read access driver
  4. *
  5. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  6. * Author: Fabrice Gasnier <fabrice.gasnier@st.com> for STMicroelectronics.
  7. */
  8. #include <linux/arm-smccc.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/nvmem-provider.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/tee_drv.h>
  16. #include "stm32-bsec-optee-ta.h"
  17. /* BSEC secure service access from non-secure */
  18. #define STM32_SMC_BSEC 0x82001003
  19. #define STM32_SMC_READ_SHADOW 0x01
  20. #define STM32_SMC_PROG_OTP 0x02
  21. #define STM32_SMC_WRITE_SHADOW 0x03
  22. #define STM32_SMC_READ_OTP 0x04
  23. /* shadow registers offset */
  24. #define STM32MP15_BSEC_DATA0 0x200
  25. struct stm32_romem_cfg {
  26. int size;
  27. u8 lower;
  28. bool ta;
  29. };
  30. struct stm32_romem_priv {
  31. void __iomem *base;
  32. struct nvmem_config cfg;
  33. u8 lower;
  34. struct tee_context *ctx;
  35. };
  36. static int stm32_romem_read(void *context, unsigned int offset, void *buf,
  37. size_t bytes)
  38. {
  39. struct stm32_romem_priv *priv = context;
  40. u8 *buf8 = buf;
  41. int i;
  42. for (i = offset; i < offset + bytes; i++)
  43. *buf8++ = readb_relaxed(priv->base + i);
  44. return 0;
  45. }
  46. static int stm32_bsec_smc(u8 op, u32 otp, u32 data, u32 *result)
  47. {
  48. #if IS_ENABLED(CONFIG_HAVE_ARM_SMCCC)
  49. struct arm_smccc_res res;
  50. arm_smccc_smc(STM32_SMC_BSEC, op, otp, data, 0, 0, 0, 0, &res);
  51. if (res.a0)
  52. return -EIO;
  53. if (result)
  54. *result = (u32)res.a1;
  55. return 0;
  56. #else
  57. return -ENXIO;
  58. #endif
  59. }
  60. static int stm32_bsec_read(void *context, unsigned int offset, void *buf,
  61. size_t bytes)
  62. {
  63. struct stm32_romem_priv *priv = context;
  64. struct device *dev = priv->cfg.dev;
  65. u32 roffset, rbytes, val;
  66. u8 *buf8 = buf, *val8 = (u8 *)&val;
  67. int i, j = 0, ret, skip_bytes, size;
  68. /* Round unaligned access to 32-bits */
  69. roffset = rounddown(offset, 4);
  70. skip_bytes = offset & 0x3;
  71. rbytes = roundup(bytes + skip_bytes, 4);
  72. if (roffset + rbytes > priv->cfg.size)
  73. return -EINVAL;
  74. for (i = roffset; (i < roffset + rbytes); i += 4) {
  75. u32 otp = i >> 2;
  76. if (otp < priv->lower) {
  77. /* read lower data from shadow registers */
  78. val = readl_relaxed(
  79. priv->base + STM32MP15_BSEC_DATA0 + i);
  80. } else {
  81. ret = stm32_bsec_smc(STM32_SMC_READ_SHADOW, otp, 0,
  82. &val);
  83. if (ret) {
  84. dev_err(dev, "Can't read data%d (%d)\n", otp,
  85. ret);
  86. return ret;
  87. }
  88. }
  89. /* skip first bytes in case of unaligned read */
  90. if (skip_bytes)
  91. size = min(bytes, (size_t)(4 - skip_bytes));
  92. else
  93. size = min(bytes, (size_t)4);
  94. memcpy(&buf8[j], &val8[skip_bytes], size);
  95. bytes -= size;
  96. j += size;
  97. skip_bytes = 0;
  98. }
  99. return 0;
  100. }
  101. static int stm32_bsec_write(void *context, unsigned int offset, void *buf,
  102. size_t bytes)
  103. {
  104. struct stm32_romem_priv *priv = context;
  105. struct device *dev = priv->cfg.dev;
  106. u32 *buf32 = buf;
  107. int ret, i;
  108. /* Allow only writing complete 32-bits aligned words */
  109. if ((bytes % 4) || (offset % 4))
  110. return -EINVAL;
  111. for (i = offset; i < offset + bytes; i += 4) {
  112. ret = stm32_bsec_smc(STM32_SMC_PROG_OTP, i >> 2, *buf32++,
  113. NULL);
  114. if (ret) {
  115. dev_err(dev, "Can't write data%d (%d)\n", i >> 2, ret);
  116. return ret;
  117. }
  118. }
  119. if (offset + bytes >= priv->lower * 4)
  120. dev_warn(dev, "Update of upper OTPs with ECC protection (word programming, only once)\n");
  121. return 0;
  122. }
  123. static int stm32_bsec_pta_read(void *context, unsigned int offset, void *buf,
  124. size_t bytes)
  125. {
  126. struct stm32_romem_priv *priv = context;
  127. return stm32_bsec_optee_ta_read(priv->ctx, offset, buf, bytes);
  128. }
  129. static int stm32_bsec_pta_write(void *context, unsigned int offset, void *buf,
  130. size_t bytes)
  131. {
  132. struct stm32_romem_priv *priv = context;
  133. return stm32_bsec_optee_ta_write(priv->ctx, priv->lower, offset, buf, bytes);
  134. }
  135. static bool stm32_bsec_smc_check(void)
  136. {
  137. u32 val;
  138. int ret;
  139. /* check that the OP-TEE support the BSEC SMC (legacy mode) */
  140. ret = stm32_bsec_smc(STM32_SMC_READ_SHADOW, 0, 0, &val);
  141. return !ret;
  142. }
  143. static bool optee_presence_check(void)
  144. {
  145. struct device_node *np;
  146. bool tee_detected = false;
  147. /* check that the OP-TEE node is present and available. */
  148. np = of_find_compatible_node(NULL, NULL, "linaro,optee-tz");
  149. if (np && of_device_is_available(np))
  150. tee_detected = true;
  151. of_node_put(np);
  152. return tee_detected;
  153. }
  154. static int stm32_romem_probe(struct platform_device *pdev)
  155. {
  156. const struct stm32_romem_cfg *cfg;
  157. struct device *dev = &pdev->dev;
  158. struct stm32_romem_priv *priv;
  159. struct resource *res;
  160. int rc;
  161. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  162. if (!priv)
  163. return -ENOMEM;
  164. priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  165. if (IS_ERR(priv->base))
  166. return PTR_ERR(priv->base);
  167. priv->cfg.name = "stm32-romem";
  168. priv->cfg.word_size = 1;
  169. priv->cfg.stride = 1;
  170. priv->cfg.dev = dev;
  171. priv->cfg.priv = priv;
  172. priv->cfg.owner = THIS_MODULE;
  173. priv->cfg.type = NVMEM_TYPE_OTP;
  174. priv->cfg.add_legacy_fixed_of_cells = true;
  175. priv->lower = 0;
  176. cfg = device_get_match_data(dev);
  177. if (!cfg) {
  178. priv->cfg.read_only = true;
  179. priv->cfg.size = resource_size(res);
  180. priv->cfg.reg_read = stm32_romem_read;
  181. } else {
  182. priv->cfg.size = cfg->size;
  183. priv->lower = cfg->lower;
  184. if (cfg->ta || optee_presence_check()) {
  185. rc = stm32_bsec_optee_ta_open(&priv->ctx);
  186. if (rc) {
  187. /* wait for OP-TEE client driver to be up and ready */
  188. if (rc == -EPROBE_DEFER)
  189. return -EPROBE_DEFER;
  190. /* BSEC PTA is required or SMC not supported */
  191. if (cfg->ta || !stm32_bsec_smc_check())
  192. return rc;
  193. }
  194. }
  195. if (priv->ctx) {
  196. rc = devm_add_action_or_reset(dev, stm32_bsec_optee_ta_close, priv->ctx);
  197. if (rc) {
  198. dev_err(dev, "devm_add_action_or_reset() failed (%d)\n", rc);
  199. return rc;
  200. }
  201. priv->cfg.reg_read = stm32_bsec_pta_read;
  202. priv->cfg.reg_write = stm32_bsec_pta_write;
  203. } else {
  204. priv->cfg.reg_read = stm32_bsec_read;
  205. priv->cfg.reg_write = stm32_bsec_write;
  206. }
  207. }
  208. return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &priv->cfg));
  209. }
  210. /*
  211. * STM32MP15/13 BSEC OTP regions: 4096 OTP bits (with 3072 effective bits)
  212. * => 96 x 32-bits data words
  213. * - Lower: 1K bits, 2:1 redundancy, incremental bit programming
  214. * => 32 (x 32-bits) lower shadow registers = words 0 to 31
  215. * - Upper: 2K bits, ECC protection, word programming only
  216. * => 64 (x 32-bits) = words 32 to 95
  217. */
  218. static const struct stm32_romem_cfg stm32mp15_bsec_cfg = {
  219. .size = 384,
  220. .lower = 32,
  221. .ta = false,
  222. };
  223. static const struct stm32_romem_cfg stm32mp13_bsec_cfg = {
  224. .size = 384,
  225. .lower = 32,
  226. .ta = true,
  227. };
  228. /*
  229. * STM32MP25 BSEC OTP: 3 regions of 32-bits data words
  230. * lower OTP (OTP0 to OTP127), bitwise (1-bit) programmable
  231. * mid OTP (OTP128 to OTP255), bulk (32-bit) programmable
  232. * upper OTP (OTP256 to OTP383), bulk (32-bit) programmable
  233. * but no access to HWKEY and ECIES key: limited at OTP367
  234. */
  235. static const struct stm32_romem_cfg stm32mp25_bsec_cfg = {
  236. .size = 368 * 4,
  237. .lower = 127,
  238. .ta = true,
  239. };
  240. static const struct of_device_id stm32_romem_of_match[] __maybe_unused = {
  241. { .compatible = "st,stm32f4-otp", }, {
  242. .compatible = "st,stm32mp15-bsec",
  243. .data = (void *)&stm32mp15_bsec_cfg,
  244. }, {
  245. .compatible = "st,stm32mp13-bsec",
  246. .data = (void *)&stm32mp13_bsec_cfg,
  247. }, {
  248. .compatible = "st,stm32mp25-bsec",
  249. .data = (void *)&stm32mp25_bsec_cfg,
  250. },
  251. { /* sentinel */ },
  252. };
  253. MODULE_DEVICE_TABLE(of, stm32_romem_of_match);
  254. static struct platform_driver stm32_romem_driver = {
  255. .probe = stm32_romem_probe,
  256. .driver = {
  257. .name = "stm32-romem",
  258. .of_match_table = of_match_ptr(stm32_romem_of_match),
  259. },
  260. };
  261. module_platform_driver(stm32_romem_driver);
  262. MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
  263. MODULE_DESCRIPTION("STMicroelectronics STM32 RO-MEM");
  264. MODULE_ALIAS("platform:nvmem-stm32-romem");
  265. MODULE_LICENSE("GPL v2");