cea8e93858fc7db899a2c28eea27e2a763a15a42
[openwrt/staging/xback.git] /
1 From df2f34ef1d924125ffaf29dfdaf7cdbd3183c321 Mon Sep 17 00:00:00 2001
2 From: Patrick Delaunay <patrick.delaunay@foss.st.com>
3 Date: Mon, 6 Feb 2023 13:43:52 +0000
4 Subject: [PATCH] nvmem: stm32: detect bsec pta presence for STM32MP15x
5
6 On STM32MP15x SoC, the SMC backend is optional when OP-TEE is used;
7 the PTA BSEC should be used as it is done on STM32MP13x platform,
8 but the BSEC SMC can be also used: it is a legacy mode in OP-TEE,
9 not recommended but used in previous OP-TEE firmware.
10
11 The presence of OP-TEE is dynamically detected in STM32MP15x device tree
12 and the supported NVMEM backend is dynamically detected:
13 - PTA with stm32_bsec_pta_find
14 - SMC with stm32_bsec_check
15
16 With OP-TEE but without PTA and SMC detection, the probe is deferred for
17 STM32MP15x devices.
18
19 On STM32MP13x platform, only the PTA is supported with cfg->ta = true
20 and this detection is skipped.
21
22 Signed-off-by: Patrick Delaunay <patrick.delaunay@foss.st.com>
23 Reviewed-by: Etienne Carriere <etienne.carriere@linaro.org>
24 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
25 Link: https://lore.kernel.org/r/20230206134356.839737-19-srinivas.kandagatla@linaro.org
26 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
27 ---
28 drivers/nvmem/stm32-romem.c | 38 +++++++++++++++++++++++++++++++++----
29 1 file changed, 34 insertions(+), 4 deletions(-)
30
31 --- a/drivers/nvmem/stm32-romem.c
32 +++ b/drivers/nvmem/stm32-romem.c
33 @@ -159,6 +159,31 @@ static int stm32_bsec_pta_write(void *co
34 return stm32_bsec_optee_ta_write(priv->ctx, priv->lower, offset, buf, bytes);
35 }
36
37 +static bool stm32_bsec_smc_check(void)
38 +{
39 + u32 val;
40 + int ret;
41 +
42 + /* check that the OP-TEE support the BSEC SMC (legacy mode) */
43 + ret = stm32_bsec_smc(STM32_SMC_READ_SHADOW, 0, 0, &val);
44 +
45 + return !ret;
46 +}
47 +
48 +static bool optee_presence_check(void)
49 +{
50 + struct device_node *np;
51 + bool tee_detected = false;
52 +
53 + /* check that the OP-TEE node is present and available. */
54 + np = of_find_compatible_node(NULL, NULL, "linaro,optee-tz");
55 + if (np && of_device_is_available(np))
56 + tee_detected = true;
57 + of_node_put(np);
58 +
59 + return tee_detected;
60 +}
61 +
62 static int stm32_romem_probe(struct platform_device *pdev)
63 {
64 const struct stm32_romem_cfg *cfg;
65 @@ -195,11 +220,16 @@ static int stm32_romem_probe(struct plat
66 } else {
67 priv->cfg.size = cfg->size;
68 priv->lower = cfg->lower;
69 - if (cfg->ta) {
70 + if (cfg->ta || optee_presence_check()) {
71 rc = stm32_bsec_optee_ta_open(&priv->ctx);
72 - /* wait for OP-TEE client driver to be up and ready */
73 - if (rc)
74 - return rc;
75 + if (rc) {
76 + /* wait for OP-TEE client driver to be up and ready */
77 + if (rc == -EPROBE_DEFER)
78 + return -EPROBE_DEFER;
79 + /* BSEC PTA is required or SMC not supported */
80 + if (cfg->ta || !stm32_bsec_smc_check())
81 + return rc;
82 + }
83 }
84 if (priv->ctx) {
85 rc = devm_add_action_or_reset(dev, stm32_bsec_optee_ta_close, priv->ctx);