1 // SPDX-License-Identifier: GPL-2.0
3 * STM32 Factory-programmed memory read access driver
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
6 * Author: Fabrice Gasnier <fabrice.gasnier@st.com> for STMicroelectronics.
9 #include <linux/arm-smccc.h>
11 #include <linux/module.h>
12 #include <linux/nvmem-provider.h>
13 #include <linux/of_device.h>
14 #include <linux/tee_drv.h>
16 #include "stm32-bsec-optee-ta.h"
18 /* BSEC secure service access from non-secure */
19 #define STM32_SMC_BSEC 0x82001003
20 #define STM32_SMC_READ_SHADOW 0x01
21 #define STM32_SMC_PROG_OTP 0x02
22 #define STM32_SMC_WRITE_SHADOW 0x03
23 #define STM32_SMC_READ_OTP 0x04
25 /* shadow registers offset */
26 #define STM32MP15_BSEC_DATA0 0x200
28 struct stm32_romem_cfg {
34 struct stm32_romem_priv {
36 struct nvmem_config cfg;
38 struct tee_context *ctx;
41 static int stm32_romem_read(void *context, unsigned int offset, void *buf,
44 struct stm32_romem_priv *priv = context;
48 for (i = offset; i < offset + bytes; i++)
49 *buf8++ = readb_relaxed(priv->base + i);
54 static int stm32_bsec_smc(u8 op, u32 otp, u32 data, u32 *result)
56 #if IS_ENABLED(CONFIG_HAVE_ARM_SMCCC)
57 struct arm_smccc_res res;
59 arm_smccc_smc(STM32_SMC_BSEC, op, otp, data, 0, 0, 0, 0, &res);
64 *result = (u32)res.a1;
72 static int stm32_bsec_read(void *context, unsigned int offset, void *buf,
75 struct stm32_romem_priv *priv = context;
76 struct device *dev = priv->cfg.dev;
77 u32 roffset, rbytes, val;
78 u8 *buf8 = buf, *val8 = (u8 *)&val;
79 int i, j = 0, ret, skip_bytes, size;
81 /* Round unaligned access to 32-bits */
82 roffset = rounddown(offset, 4);
83 skip_bytes = offset & 0x3;
84 rbytes = roundup(bytes + skip_bytes, 4);
86 if (roffset + rbytes > priv->cfg.size)
89 for (i = roffset; (i < roffset + rbytes); i += 4) {
92 if (otp < priv->lower) {
93 /* read lower data from shadow registers */
95 priv->base + STM32MP15_BSEC_DATA0 + i);
97 ret = stm32_bsec_smc(STM32_SMC_READ_SHADOW, otp, 0,
100 dev_err(dev, "Can't read data%d (%d)\n", otp,
105 /* skip first bytes in case of unaligned read */
107 size = min(bytes, (size_t)(4 - skip_bytes));
109 size = min(bytes, (size_t)4);
110 memcpy(&buf8[j], &val8[skip_bytes], size);
119 static int stm32_bsec_write(void *context, unsigned int offset, void *buf,
122 struct stm32_romem_priv *priv = context;
123 struct device *dev = priv->cfg.dev;
127 /* Allow only writing complete 32-bits aligned words */
128 if ((bytes % 4) || (offset % 4))
131 for (i = offset; i < offset + bytes; i += 4) {
132 ret = stm32_bsec_smc(STM32_SMC_PROG_OTP, i >> 2, *buf32++,
135 dev_err(dev, "Can't write data%d (%d)\n", i >> 2, ret);
140 if (offset + bytes >= priv->lower * 4)
141 dev_warn(dev, "Update of upper OTPs with ECC protection (word programming, only once)\n");
146 static int stm32_bsec_pta_read(void *context, unsigned int offset, void *buf,
149 struct stm32_romem_priv *priv = context;
151 return stm32_bsec_optee_ta_read(priv->ctx, offset, buf, bytes);
154 static int stm32_bsec_pta_write(void *context, unsigned int offset, void *buf,
157 struct stm32_romem_priv *priv = context;
159 return stm32_bsec_optee_ta_write(priv->ctx, priv->lower, offset, buf, bytes);
162 static bool stm32_bsec_smc_check(void)
167 /* check that the OP-TEE support the BSEC SMC (legacy mode) */
168 ret = stm32_bsec_smc(STM32_SMC_READ_SHADOW, 0, 0, &val);
173 static bool optee_presence_check(void)
175 struct device_node *np;
176 bool tee_detected = false;
178 /* check that the OP-TEE node is present and available. */
179 np = of_find_compatible_node(NULL, NULL, "linaro,optee-tz");
180 if (np && of_device_is_available(np))
187 static int stm32_romem_probe(struct platform_device *pdev)
189 const struct stm32_romem_cfg *cfg;
190 struct device *dev = &pdev->dev;
191 struct stm32_romem_priv *priv;
192 struct resource *res;
195 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
199 priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
200 if (IS_ERR(priv->base))
201 return PTR_ERR(priv->base);
203 priv->cfg.name = "stm32-romem";
204 priv->cfg.word_size = 1;
205 priv->cfg.stride = 1;
207 priv->cfg.priv = priv;
208 priv->cfg.owner = THIS_MODULE;
209 priv->cfg.type = NVMEM_TYPE_OTP;
213 cfg = (const struct stm32_romem_cfg *)
214 of_match_device(dev->driver->of_match_table, dev)->data;
216 priv->cfg.read_only = true;
217 priv->cfg.size = resource_size(res);
218 priv->cfg.reg_read = stm32_romem_read;
220 priv->cfg.size = cfg->size;
221 priv->lower = cfg->lower;
222 if (cfg->ta || optee_presence_check()) {
223 rc = stm32_bsec_optee_ta_open(&priv->ctx);
225 /* wait for OP-TEE client driver to be up and ready */
226 if (rc == -EPROBE_DEFER)
227 return -EPROBE_DEFER;
228 /* BSEC PTA is required or SMC not supported */
229 if (cfg->ta || !stm32_bsec_smc_check())
234 rc = devm_add_action_or_reset(dev, stm32_bsec_optee_ta_close, priv->ctx);
236 dev_err(dev, "devm_add_action_or_reset() failed (%d)\n", rc);
239 priv->cfg.reg_read = stm32_bsec_pta_read;
240 priv->cfg.reg_write = stm32_bsec_pta_write;
242 priv->cfg.reg_read = stm32_bsec_read;
243 priv->cfg.reg_write = stm32_bsec_write;
247 return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &priv->cfg));
251 * STM32MP15/13 BSEC OTP regions: 4096 OTP bits (with 3072 effective bits)
252 * => 96 x 32-bits data words
253 * - Lower: 1K bits, 2:1 redundancy, incremental bit programming
254 * => 32 (x 32-bits) lower shadow registers = words 0 to 31
255 * - Upper: 2K bits, ECC protection, word programming only
256 * => 64 (x 32-bits) = words 32 to 95
258 static const struct stm32_romem_cfg stm32mp15_bsec_cfg = {
264 static const struct stm32_romem_cfg stm32mp13_bsec_cfg = {
270 static const struct of_device_id stm32_romem_of_match[] __maybe_unused = {
271 { .compatible = "st,stm32f4-otp", }, {
272 .compatible = "st,stm32mp15-bsec",
273 .data = (void *)&stm32mp15_bsec_cfg,
275 .compatible = "st,stm32mp13-bsec",
276 .data = (void *)&stm32mp13_bsec_cfg,
280 MODULE_DEVICE_TABLE(of, stm32_romem_of_match);
282 static struct platform_driver stm32_romem_driver = {
283 .probe = stm32_romem_probe,
285 .name = "stm32-romem",
286 .of_match_table = of_match_ptr(stm32_romem_of_match),
289 module_platform_driver(stm32_romem_driver);
291 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@st.com>");
292 MODULE_DESCRIPTION("STMicroelectronics STM32 RO-MEM");
293 MODULE_ALIAS("platform:nvmem-stm32-romem");
294 MODULE_LICENSE("GPL v2");