stm32-romem.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <[email protected]> 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_device.h>
  13. /* BSEC secure service access from non-secure */
  14. #define STM32_SMC_BSEC 0x82001003
  15. #define STM32_SMC_READ_SHADOW 0x01
  16. #define STM32_SMC_PROG_OTP 0x02
  17. #define STM32_SMC_WRITE_SHADOW 0x03
  18. #define STM32_SMC_READ_OTP 0x04
  19. /* shadow registers offest */
  20. #define STM32MP15_BSEC_DATA0 0x200
  21. /* 32 (x 32-bits) lower shadow registers */
  22. #define STM32MP15_BSEC_NUM_LOWER 32
  23. struct stm32_romem_cfg {
  24. int size;
  25. };
  26. struct stm32_romem_priv {
  27. void __iomem *base;
  28. struct nvmem_config cfg;
  29. };
  30. static int stm32_romem_read(void *context, unsigned int offset, void *buf,
  31. size_t bytes)
  32. {
  33. struct stm32_romem_priv *priv = context;
  34. u8 *buf8 = buf;
  35. int i;
  36. for (i = offset; i < offset + bytes; i++)
  37. *buf8++ = readb_relaxed(priv->base + i);
  38. return 0;
  39. }
  40. static int stm32_bsec_smc(u8 op, u32 otp, u32 data, u32 *result)
  41. {
  42. #if IS_ENABLED(CONFIG_HAVE_ARM_SMCCC)
  43. struct arm_smccc_res res;
  44. arm_smccc_smc(STM32_SMC_BSEC, op, otp, data, 0, 0, 0, 0, &res);
  45. if (res.a0)
  46. return -EIO;
  47. if (result)
  48. *result = (u32)res.a1;
  49. return 0;
  50. #else
  51. return -ENXIO;
  52. #endif
  53. }
  54. static int stm32_bsec_read(void *context, unsigned int offset, void *buf,
  55. size_t bytes)
  56. {
  57. struct stm32_romem_priv *priv = context;
  58. struct device *dev = priv->cfg.dev;
  59. u32 roffset, rbytes, val;
  60. u8 *buf8 = buf, *val8 = (u8 *)&val;
  61. int i, j = 0, ret, skip_bytes, size;
  62. /* Round unaligned access to 32-bits */
  63. roffset = rounddown(offset, 4);
  64. skip_bytes = offset & 0x3;
  65. rbytes = roundup(bytes + skip_bytes, 4);
  66. if (roffset + rbytes > priv->cfg.size)
  67. return -EINVAL;
  68. for (i = roffset; (i < roffset + rbytes); i += 4) {
  69. u32 otp = i >> 2;
  70. if (otp < STM32MP15_BSEC_NUM_LOWER) {
  71. /* read lower data from shadow registers */
  72. val = readl_relaxed(
  73. priv->base + STM32MP15_BSEC_DATA0 + i);
  74. } else {
  75. ret = stm32_bsec_smc(STM32_SMC_READ_SHADOW, otp, 0,
  76. &val);
  77. if (ret) {
  78. dev_err(dev, "Can't read data%d (%d)\n", otp,
  79. ret);
  80. return ret;
  81. }
  82. }
  83. /* skip first bytes in case of unaligned read */
  84. if (skip_bytes)
  85. size = min(bytes, (size_t)(4 - skip_bytes));
  86. else
  87. size = min(bytes, (size_t)4);
  88. memcpy(&buf8[j], &val8[skip_bytes], size);
  89. bytes -= size;
  90. j += size;
  91. skip_bytes = 0;
  92. }
  93. return 0;
  94. }
  95. static int stm32_bsec_write(void *context, unsigned int offset, void *buf,
  96. size_t bytes)
  97. {
  98. struct stm32_romem_priv *priv = context;
  99. struct device *dev = priv->cfg.dev;
  100. u32 *buf32 = buf;
  101. int ret, i;
  102. /* Allow only writing complete 32-bits aligned words */
  103. if ((bytes % 4) || (offset % 4))
  104. return -EINVAL;
  105. for (i = offset; i < offset + bytes; i += 4) {
  106. ret = stm32_bsec_smc(STM32_SMC_PROG_OTP, i >> 2, *buf32++,
  107. NULL);
  108. if (ret) {
  109. dev_err(dev, "Can't write data%d (%d)\n", i >> 2, ret);
  110. return ret;
  111. }
  112. }
  113. return 0;
  114. }
  115. static int stm32_romem_probe(struct platform_device *pdev)
  116. {
  117. const struct stm32_romem_cfg *cfg;
  118. struct device *dev = &pdev->dev;
  119. struct stm32_romem_priv *priv;
  120. struct resource *res;
  121. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  122. if (!priv)
  123. return -ENOMEM;
  124. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  125. priv->base = devm_ioremap_resource(dev, res);
  126. if (IS_ERR(priv->base))
  127. return PTR_ERR(priv->base);
  128. priv->cfg.name = "stm32-romem";
  129. priv->cfg.word_size = 1;
  130. priv->cfg.stride = 1;
  131. priv->cfg.dev = dev;
  132. priv->cfg.priv = priv;
  133. priv->cfg.owner = THIS_MODULE;
  134. cfg = (const struct stm32_romem_cfg *)
  135. of_match_device(dev->driver->of_match_table, dev)->data;
  136. if (!cfg) {
  137. priv->cfg.read_only = true;
  138. priv->cfg.size = resource_size(res);
  139. priv->cfg.reg_read = stm32_romem_read;
  140. } else {
  141. priv->cfg.size = cfg->size;
  142. priv->cfg.reg_read = stm32_bsec_read;
  143. priv->cfg.reg_write = stm32_bsec_write;
  144. }
  145. return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &priv->cfg));
  146. }
  147. static const struct stm32_romem_cfg stm32mp15_bsec_cfg = {
  148. .size = 384, /* 96 x 32-bits data words */
  149. };
  150. static const struct of_device_id stm32_romem_of_match[] = {
  151. { .compatible = "st,stm32f4-otp", }, {
  152. .compatible = "st,stm32mp15-bsec",
  153. .data = (void *)&stm32mp15_bsec_cfg,
  154. }, {
  155. },
  156. };
  157. MODULE_DEVICE_TABLE(of, stm32_romem_of_match);
  158. static struct platform_driver stm32_romem_driver = {
  159. .probe = stm32_romem_probe,
  160. .driver = {
  161. .name = "stm32-romem",
  162. .of_match_table = of_match_ptr(stm32_romem_of_match),
  163. },
  164. };
  165. module_platform_driver(stm32_romem_driver);
  166. MODULE_AUTHOR("Fabrice Gasnier <[email protected]>");
  167. MODULE_DESCRIPTION("STMicroelectronics STM32 RO-MEM");
  168. MODULE_ALIAS("platform:nvmem-stm32-romem");
  169. MODULE_LICENSE("GPL v2");