xref: /openbmc/linux/drivers/nvmem/imx-ocotp-ele.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
122e9e6fcSPeng Fan // SPDX-License-Identifier: GPL-2.0-only
222e9e6fcSPeng Fan /*
322e9e6fcSPeng Fan  * i.MX9 OCOTP fusebox driver
422e9e6fcSPeng Fan  *
522e9e6fcSPeng Fan  * Copyright 2023 NXP
622e9e6fcSPeng Fan  */
722e9e6fcSPeng Fan 
822e9e6fcSPeng Fan #include <linux/device.h>
922e9e6fcSPeng Fan #include <linux/io.h>
1022e9e6fcSPeng Fan #include <linux/module.h>
1122e9e6fcSPeng Fan #include <linux/nvmem-provider.h>
12*9bf75da0SRob Herring #include <linux/of.h>
1322e9e6fcSPeng Fan #include <linux/platform_device.h>
1422e9e6fcSPeng Fan #include <linux/slab.h>
1522e9e6fcSPeng Fan 
1622e9e6fcSPeng Fan enum fuse_type {
1722e9e6fcSPeng Fan 	FUSE_FSB = 1,
1822e9e6fcSPeng Fan 	FUSE_ELE = 2,
1922e9e6fcSPeng Fan 	FUSE_INVALID = -1
2022e9e6fcSPeng Fan };
2122e9e6fcSPeng Fan 
2222e9e6fcSPeng Fan struct ocotp_map_entry {
2322e9e6fcSPeng Fan 	u32 start; /* start word */
2422e9e6fcSPeng Fan 	u32 num; /* num words */
2522e9e6fcSPeng Fan 	enum fuse_type type;
2622e9e6fcSPeng Fan };
2722e9e6fcSPeng Fan 
2822e9e6fcSPeng Fan struct ocotp_devtype_data {
2922e9e6fcSPeng Fan 	u32 reg_off;
3022e9e6fcSPeng Fan 	char *name;
3122e9e6fcSPeng Fan 	u32 size;
3222e9e6fcSPeng Fan 	u32 num_entry;
3322e9e6fcSPeng Fan 	u32 flag;
3422e9e6fcSPeng Fan 	nvmem_reg_read_t reg_read;
3522e9e6fcSPeng Fan 	struct ocotp_map_entry entry[];
3622e9e6fcSPeng Fan };
3722e9e6fcSPeng Fan 
3822e9e6fcSPeng Fan struct imx_ocotp_priv {
3922e9e6fcSPeng Fan 	struct device *dev;
4022e9e6fcSPeng Fan 	void __iomem *base;
4122e9e6fcSPeng Fan 	struct nvmem_config config;
4222e9e6fcSPeng Fan 	struct mutex lock;
4322e9e6fcSPeng Fan 	const struct ocotp_devtype_data *data;
4422e9e6fcSPeng Fan };
4522e9e6fcSPeng Fan 
imx_ocotp_fuse_type(void * context,u32 index)4622e9e6fcSPeng Fan static enum fuse_type imx_ocotp_fuse_type(void *context, u32 index)
4722e9e6fcSPeng Fan {
4822e9e6fcSPeng Fan 	struct imx_ocotp_priv *priv = context;
4922e9e6fcSPeng Fan 	const struct ocotp_devtype_data *data = priv->data;
5022e9e6fcSPeng Fan 	u32 start, end;
5122e9e6fcSPeng Fan 	int i;
5222e9e6fcSPeng Fan 
5322e9e6fcSPeng Fan 	for (i = 0; i < data->num_entry; i++) {
5422e9e6fcSPeng Fan 		start = data->entry[i].start;
5522e9e6fcSPeng Fan 		end = data->entry[i].start + data->entry[i].num;
5622e9e6fcSPeng Fan 
5722e9e6fcSPeng Fan 		if (index >= start && index < end)
5822e9e6fcSPeng Fan 			return data->entry[i].type;
5922e9e6fcSPeng Fan 	}
6022e9e6fcSPeng Fan 
6122e9e6fcSPeng Fan 	return FUSE_INVALID;
6222e9e6fcSPeng Fan }
6322e9e6fcSPeng Fan 
imx_ocotp_reg_read(void * context,unsigned int offset,void * val,size_t bytes)6422e9e6fcSPeng Fan static int imx_ocotp_reg_read(void *context, unsigned int offset, void *val, size_t bytes)
6522e9e6fcSPeng Fan {
6622e9e6fcSPeng Fan 	struct imx_ocotp_priv *priv = context;
6722e9e6fcSPeng Fan 	void __iomem *reg = priv->base + priv->data->reg_off;
6822e9e6fcSPeng Fan 	u32 count, index, num_bytes;
6922e9e6fcSPeng Fan 	enum fuse_type type;
7022e9e6fcSPeng Fan 	u32 *buf;
7122e9e6fcSPeng Fan 	void *p;
7222e9e6fcSPeng Fan 	int i;
7322e9e6fcSPeng Fan 
7422e9e6fcSPeng Fan 	index = offset;
7522e9e6fcSPeng Fan 	num_bytes = round_up(bytes, 4);
7622e9e6fcSPeng Fan 	count = num_bytes >> 2;
7722e9e6fcSPeng Fan 
7822e9e6fcSPeng Fan 	if (count > ((priv->data->size >> 2) - index))
7922e9e6fcSPeng Fan 		count = (priv->data->size >> 2) - index;
8022e9e6fcSPeng Fan 
8122e9e6fcSPeng Fan 	p = kzalloc(num_bytes, GFP_KERNEL);
8222e9e6fcSPeng Fan 	if (!p)
8322e9e6fcSPeng Fan 		return -ENOMEM;
8422e9e6fcSPeng Fan 
8522e9e6fcSPeng Fan 	mutex_lock(&priv->lock);
8622e9e6fcSPeng Fan 
8722e9e6fcSPeng Fan 	buf = p;
8822e9e6fcSPeng Fan 
8922e9e6fcSPeng Fan 	for (i = index; i < (index + count); i++) {
9022e9e6fcSPeng Fan 		type = imx_ocotp_fuse_type(context, i);
9122e9e6fcSPeng Fan 		if (type == FUSE_INVALID || type == FUSE_ELE) {
9222e9e6fcSPeng Fan 			*buf++ = 0;
9322e9e6fcSPeng Fan 			continue;
9422e9e6fcSPeng Fan 		}
9522e9e6fcSPeng Fan 
9622e9e6fcSPeng Fan 		*buf++ = readl_relaxed(reg + (i << 2));
9722e9e6fcSPeng Fan 	}
9822e9e6fcSPeng Fan 
9922e9e6fcSPeng Fan 	memcpy(val, (u8 *)p, bytes);
10022e9e6fcSPeng Fan 
10122e9e6fcSPeng Fan 	mutex_unlock(&priv->lock);
10222e9e6fcSPeng Fan 
10322e9e6fcSPeng Fan 	kfree(p);
10422e9e6fcSPeng Fan 
10522e9e6fcSPeng Fan 	return 0;
10622e9e6fcSPeng Fan };
10722e9e6fcSPeng Fan 
imx_ele_ocotp_probe(struct platform_device * pdev)10822e9e6fcSPeng Fan static int imx_ele_ocotp_probe(struct platform_device *pdev)
10922e9e6fcSPeng Fan {
11022e9e6fcSPeng Fan 	struct device *dev = &pdev->dev;
11122e9e6fcSPeng Fan 	struct imx_ocotp_priv *priv;
11222e9e6fcSPeng Fan 	struct nvmem_device *nvmem;
11322e9e6fcSPeng Fan 
11422e9e6fcSPeng Fan 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
11522e9e6fcSPeng Fan 	if (!priv)
11622e9e6fcSPeng Fan 		return -ENOMEM;
11722e9e6fcSPeng Fan 
11822e9e6fcSPeng Fan 	priv->data = of_device_get_match_data(dev);
11922e9e6fcSPeng Fan 
12022e9e6fcSPeng Fan 	priv->base = devm_platform_ioremap_resource(pdev, 0);
12122e9e6fcSPeng Fan 	if (IS_ERR(priv->base))
12222e9e6fcSPeng Fan 		return PTR_ERR(priv->base);
12322e9e6fcSPeng Fan 
12422e9e6fcSPeng Fan 	priv->config.dev = dev;
12522e9e6fcSPeng Fan 	priv->config.name = "ELE-OCOTP";
12622e9e6fcSPeng Fan 	priv->config.id = NVMEM_DEVID_AUTO;
12722e9e6fcSPeng Fan 	priv->config.owner = THIS_MODULE;
12822e9e6fcSPeng Fan 	priv->config.size = priv->data->size;
12922e9e6fcSPeng Fan 	priv->config.reg_read = priv->data->reg_read;
13022e9e6fcSPeng Fan 	priv->config.word_size = 4;
13122e9e6fcSPeng Fan 	priv->config.stride = 1;
13222e9e6fcSPeng Fan 	priv->config.priv = priv;
13322e9e6fcSPeng Fan 	priv->config.read_only = true;
13422e9e6fcSPeng Fan 	mutex_init(&priv->lock);
13522e9e6fcSPeng Fan 
13622e9e6fcSPeng Fan 	nvmem = devm_nvmem_register(dev, &priv->config);
13722e9e6fcSPeng Fan 	if (IS_ERR(nvmem))
13822e9e6fcSPeng Fan 		return PTR_ERR(nvmem);
13922e9e6fcSPeng Fan 
14022e9e6fcSPeng Fan 	return 0;
14122e9e6fcSPeng Fan }
14222e9e6fcSPeng Fan 
14322e9e6fcSPeng Fan static const struct ocotp_devtype_data imx93_ocotp_data = {
14422e9e6fcSPeng Fan 	.reg_off = 0x8000,
14522e9e6fcSPeng Fan 	.reg_read = imx_ocotp_reg_read,
14622e9e6fcSPeng Fan 	.size = 2048,
14722e9e6fcSPeng Fan 	.num_entry = 6,
14822e9e6fcSPeng Fan 	.entry = {
14922e9e6fcSPeng Fan 		{ 0, 52, FUSE_FSB },
15022e9e6fcSPeng Fan 		{ 63, 1, FUSE_ELE},
15122e9e6fcSPeng Fan 		{ 128, 16, FUSE_ELE },
15222e9e6fcSPeng Fan 		{ 182, 1, FUSE_ELE },
15322e9e6fcSPeng Fan 		{ 188, 1, FUSE_ELE },
15422e9e6fcSPeng Fan 		{ 312, 200, FUSE_FSB }
15522e9e6fcSPeng Fan 	},
15622e9e6fcSPeng Fan };
15722e9e6fcSPeng Fan 
15822e9e6fcSPeng Fan static const struct of_device_id imx_ele_ocotp_dt_ids[] = {
15922e9e6fcSPeng Fan 	{ .compatible = "fsl,imx93-ocotp", .data = &imx93_ocotp_data, },
16022e9e6fcSPeng Fan 	{},
16122e9e6fcSPeng Fan };
16222e9e6fcSPeng Fan MODULE_DEVICE_TABLE(of, imx_ele_ocotp_dt_ids);
16322e9e6fcSPeng Fan 
16422e9e6fcSPeng Fan static struct platform_driver imx_ele_ocotp_driver = {
16522e9e6fcSPeng Fan 	.driver = {
16622e9e6fcSPeng Fan 		.name = "imx_ele_ocotp",
16722e9e6fcSPeng Fan 		.of_match_table = imx_ele_ocotp_dt_ids,
16822e9e6fcSPeng Fan 	},
16922e9e6fcSPeng Fan 	.probe = imx_ele_ocotp_probe,
17022e9e6fcSPeng Fan };
17122e9e6fcSPeng Fan module_platform_driver(imx_ele_ocotp_driver);
17222e9e6fcSPeng Fan 
17322e9e6fcSPeng Fan MODULE_DESCRIPTION("i.MX OCOTP/ELE driver");
17422e9e6fcSPeng Fan MODULE_AUTHOR("Peng Fan <peng.fan@nxp.com>");
17522e9e6fcSPeng Fan MODULE_LICENSE("GPL");
176