1 // SPDX-License-Identifier: GPL-2.0+
3 * i.MX8 OCOTP fusebox driver
7 * Peng Fan <peng.fan@nxp.com>
10 #include <linux/firmware/imx/sci.h>
11 #include <linux/module.h>
12 #include <linux/nvmem-provider.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
21 struct ocotp_devtype_data {
28 const struct ocotp_devtype_data *data;
29 struct imx_sc_ipc *nvmem_ipc;
32 struct imx_sc_msg_misc_fuse_read {
33 struct imx_sc_rpc_msg hdr;
37 static struct ocotp_devtype_data imx8qxp_data = {
42 static int imx_sc_misc_otp_fuse_read(struct imx_sc_ipc *ipc, u32 word,
45 struct imx_sc_msg_misc_fuse_read msg;
46 struct imx_sc_rpc_msg *hdr = &msg.hdr;
49 hdr->ver = IMX_SC_RPC_VERSION;
50 hdr->svc = IMX_SC_RPC_SVC_MISC;
51 hdr->func = IMX_SC_MISC_FUNC_OTP_FUSE_READ;
56 ret = imx_scu_call_rpc(ipc, &msg, true);
65 static int imx_scu_ocotp_read(void *context, unsigned int offset,
66 void *val, size_t bytes)
68 struct ocotp_priv *priv = context;
69 u32 count, index, num_bytes;
75 num_bytes = round_up((offset % 4) + bytes, 4);
76 count = num_bytes >> 2;
78 if (count > (priv->data->nregs - index))
79 count = priv->data->nregs - index;
81 p = kzalloc(num_bytes, GFP_KERNEL);
87 for (i = index; i < (index + count); i++) {
88 if (priv->data->devtype == IMX8QXP) {
89 if ((i > 271) && (i < 544)) {
95 ret = imx_sc_misc_otp_fuse_read(priv->nvmem_ipc, i, buf);
103 memcpy(val, (u8 *)p + offset % 4, bytes);
110 static struct nvmem_config imx_scu_ocotp_nvmem_config = {
111 .name = "imx-scu-ocotp",
115 .owner = THIS_MODULE,
116 .reg_read = imx_scu_ocotp_read,
119 static const struct of_device_id imx_scu_ocotp_dt_ids[] = {
120 { .compatible = "fsl,imx8qxp-scu-ocotp", (void *)&imx8qxp_data },
123 MODULE_DEVICE_TABLE(of, imx_scu_ocotp_dt_ids);
125 static int imx_scu_ocotp_probe(struct platform_device *pdev)
127 struct device *dev = &pdev->dev;
128 struct ocotp_priv *priv;
129 struct nvmem_device *nvmem;
132 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
136 ret = imx_scu_get_handle(&priv->nvmem_ipc);
140 priv->data = of_device_get_match_data(dev);
142 imx_scu_ocotp_nvmem_config.size = 4 * priv->data->nregs;
143 imx_scu_ocotp_nvmem_config.dev = dev;
144 imx_scu_ocotp_nvmem_config.priv = priv;
145 nvmem = devm_nvmem_register(dev, &imx_scu_ocotp_nvmem_config);
147 return PTR_ERR_OR_ZERO(nvmem);
150 static struct platform_driver imx_scu_ocotp_driver = {
151 .probe = imx_scu_ocotp_probe,
153 .name = "imx_scu_ocotp",
154 .of_match_table = imx_scu_ocotp_dt_ids,
157 module_platform_driver(imx_scu_ocotp_driver);
159 MODULE_AUTHOR("Peng Fan <peng.fan@nxp.com>");
160 MODULE_DESCRIPTION("i.MX8 SCU OCOTP fuse box driver");
161 MODULE_LICENSE("GPL v2");