1 // SPDX-License-Identifier: GPL-2.0+
3 * i.MX8 OCOTP fusebox driver
7 * Peng Fan <peng.fan@nxp.com>
10 #include <linux/arm-smccc.h>
11 #include <linux/firmware/imx/sci.h>
12 #include <linux/module.h>
13 #include <linux/nvmem-provider.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
18 #define IMX_SIP_OTP 0xC200000A
19 #define IMX_SIP_OTP_WRITE 0x2
26 #define ECC_REGION BIT(0)
27 #define HOLE_REGION BIT(1)
35 struct ocotp_devtype_data
{
39 struct ocotp_region region
[];
44 const struct ocotp_devtype_data
*data
;
45 struct imx_sc_ipc
*nvmem_ipc
;
48 struct imx_sc_msg_misc_fuse_read
{
49 struct imx_sc_rpc_msg hdr
;
53 static DEFINE_MUTEX(scu_ocotp_mutex
);
55 static struct ocotp_devtype_data imx8qxp_data
= {
60 {0x10, 0x10f, ECC_REGION
},
61 {0x110, 0x21F, HOLE_REGION
},
62 {0x220, 0x31F, ECC_REGION
},
66 static struct ocotp_devtype_data imx8qm_data
= {
71 {0x10, 0x10f, ECC_REGION
},
72 {0x1a0, 0x1ff, ECC_REGION
},
76 static bool in_hole(void *context
, u32 index
)
78 struct ocotp_priv
*priv
= context
;
79 const struct ocotp_devtype_data
*data
= priv
->data
;
82 for (i
= 0; i
< data
->num_region
; i
++) {
83 if (data
->region
[i
].flag
& HOLE_REGION
) {
84 if ((index
>= data
->region
[i
].start
) &&
85 (index
<= data
->region
[i
].end
))
93 static bool in_ecc(void *context
, u32 index
)
95 struct ocotp_priv
*priv
= context
;
96 const struct ocotp_devtype_data
*data
= priv
->data
;
99 for (i
= 0; i
< data
->num_region
; i
++) {
100 if (data
->region
[i
].flag
& ECC_REGION
) {
101 if ((index
>= data
->region
[i
].start
) &&
102 (index
<= data
->region
[i
].end
))
110 static int imx_sc_misc_otp_fuse_read(struct imx_sc_ipc
*ipc
, u32 word
,
113 struct imx_sc_msg_misc_fuse_read msg
;
114 struct imx_sc_rpc_msg
*hdr
= &msg
.hdr
;
117 hdr
->ver
= IMX_SC_RPC_VERSION
;
118 hdr
->svc
= IMX_SC_RPC_SVC_MISC
;
119 hdr
->func
= IMX_SC_MISC_FUNC_OTP_FUSE_READ
;
124 ret
= imx_scu_call_rpc(ipc
, &msg
, true);
133 static int imx_scu_ocotp_read(void *context
, unsigned int offset
,
134 void *val
, size_t bytes
)
136 struct ocotp_priv
*priv
= context
;
137 u32 count
, index
, num_bytes
;
143 num_bytes
= round_up((offset
% 4) + bytes
, 4);
144 count
= num_bytes
>> 2;
146 if (count
> (priv
->data
->nregs
- index
))
147 count
= priv
->data
->nregs
- index
;
149 p
= kzalloc(num_bytes
, GFP_KERNEL
);
153 mutex_lock(&scu_ocotp_mutex
);
157 for (i
= index
; i
< (index
+ count
); i
++) {
158 if (in_hole(context
, i
)) {
163 ret
= imx_sc_misc_otp_fuse_read(priv
->nvmem_ipc
, i
, buf
);
165 mutex_unlock(&scu_ocotp_mutex
);
172 memcpy(val
, (u8
*)p
+ offset
% 4, bytes
);
174 mutex_unlock(&scu_ocotp_mutex
);
181 static int imx_scu_ocotp_write(void *context
, unsigned int offset
,
182 void *val
, size_t bytes
)
184 struct ocotp_priv
*priv
= context
;
185 struct arm_smccc_res res
;
191 /* allow only writing one complete OTP word at a time */
192 if ((bytes
!= 4) || (offset
% 4))
197 if (in_hole(context
, index
))
200 if (in_ecc(context
, index
)) {
201 pr_warn("ECC region, only program once\n");
202 mutex_lock(&scu_ocotp_mutex
);
203 ret
= imx_sc_misc_otp_fuse_read(priv
->nvmem_ipc
, index
, &tmp
);
204 mutex_unlock(&scu_ocotp_mutex
);
208 pr_warn("ECC region, already has value: %x\n", tmp
);
213 mutex_lock(&scu_ocotp_mutex
);
215 arm_smccc_smc(IMX_SIP_OTP
, IMX_SIP_OTP_WRITE
, index
, *buf
,
218 mutex_unlock(&scu_ocotp_mutex
);
223 static struct nvmem_config imx_scu_ocotp_nvmem_config
= {
224 .name
= "imx-scu-ocotp",
228 .owner
= THIS_MODULE
,
229 .reg_read
= imx_scu_ocotp_read
,
230 .reg_write
= imx_scu_ocotp_write
,
233 static const struct of_device_id imx_scu_ocotp_dt_ids
[] = {
234 { .compatible
= "fsl,imx8qxp-scu-ocotp", (void *)&imx8qxp_data
},
235 { .compatible
= "fsl,imx8qm-scu-ocotp", (void *)&imx8qm_data
},
238 MODULE_DEVICE_TABLE(of
, imx_scu_ocotp_dt_ids
);
240 static int imx_scu_ocotp_probe(struct platform_device
*pdev
)
242 struct device
*dev
= &pdev
->dev
;
243 struct ocotp_priv
*priv
;
244 struct nvmem_device
*nvmem
;
247 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
251 ret
= imx_scu_get_handle(&priv
->nvmem_ipc
);
255 priv
->data
= of_device_get_match_data(dev
);
257 imx_scu_ocotp_nvmem_config
.size
= 4 * priv
->data
->nregs
;
258 imx_scu_ocotp_nvmem_config
.dev
= dev
;
259 imx_scu_ocotp_nvmem_config
.priv
= priv
;
260 nvmem
= devm_nvmem_register(dev
, &imx_scu_ocotp_nvmem_config
);
262 return PTR_ERR_OR_ZERO(nvmem
);
265 static struct platform_driver imx_scu_ocotp_driver
= {
266 .probe
= imx_scu_ocotp_probe
,
268 .name
= "imx_scu_ocotp",
269 .of_match_table
= imx_scu_ocotp_dt_ids
,
272 module_platform_driver(imx_scu_ocotp_driver
);
274 MODULE_AUTHOR("Peng Fan <peng.fan@nxp.com>");
275 MODULE_DESCRIPTION("i.MX8 SCU OCOTP fuse box driver");
276 MODULE_LICENSE("GPL v2");