1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Freescale MXS On-Chip OTP driver
5 * Copyright (C) 2015 Stefan Wahren <stefan.wahren@i2se.com>
7 * Based on the driver from Huang Shijie and Christoph G. Baumann
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
14 #include <linux/module.h>
15 #include <linux/nvmem-provider.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/stmp_device.h>
21 /* OCOTP registers and bits */
23 #define BM_OCOTP_CTRL_RD_BANK_OPEN BIT(12)
24 #define BM_OCOTP_CTRL_ERROR BIT(9)
25 #define BM_OCOTP_CTRL_BUSY BIT(8)
27 #define OCOTP_TIMEOUT 10000
28 #define OCOTP_DATA_OFFSET 0x20
33 struct nvmem_device
*nvmem
;
36 static int mxs_ocotp_wait(struct mxs_ocotp
*otp
)
38 int timeout
= OCOTP_TIMEOUT
;
39 unsigned int status
= 0;
42 status
= readl(otp
->base
);
44 if (!(status
& (BM_OCOTP_CTRL_BUSY
| BM_OCOTP_CTRL_ERROR
)))
50 if (status
& BM_OCOTP_CTRL_BUSY
)
52 else if (status
& BM_OCOTP_CTRL_ERROR
)
58 static int mxs_ocotp_read(void *context
, unsigned int offset
,
59 void *val
, size_t bytes
)
61 struct mxs_ocotp
*otp
= context
;
65 ret
= clk_enable(otp
->clk
);
69 writel(BM_OCOTP_CTRL_ERROR
, otp
->base
+ STMP_OFFSET_REG_CLR
);
71 ret
= mxs_ocotp_wait(otp
);
75 /* open OCOTP banks for read */
76 writel(BM_OCOTP_CTRL_RD_BANK_OPEN
, otp
->base
+ STMP_OFFSET_REG_SET
);
78 /* approximately wait 33 hclk cycles */
81 ret
= mxs_ocotp_wait(otp
);
86 if ((offset
< OCOTP_DATA_OFFSET
) || (offset
% 16)) {
87 /* fill up non-data register */
90 *buf
++ = readl(otp
->base
+ offset
);
98 /* close banks for power saving */
99 writel(BM_OCOTP_CTRL_RD_BANK_OPEN
, otp
->base
+ STMP_OFFSET_REG_CLR
);
102 clk_disable(otp
->clk
);
107 static struct nvmem_config ocotp_config
= {
111 .reg_read
= mxs_ocotp_read
,
118 static const struct mxs_data imx23_data
= {
122 static const struct mxs_data imx28_data
= {
126 static const struct of_device_id mxs_ocotp_match
[] = {
127 { .compatible
= "fsl,imx23-ocotp", .data
= &imx23_data
},
128 { .compatible
= "fsl,imx28-ocotp", .data
= &imx28_data
},
131 MODULE_DEVICE_TABLE(of
, mxs_ocotp_match
);
133 static int mxs_ocotp_probe(struct platform_device
*pdev
)
135 struct device
*dev
= &pdev
->dev
;
136 const struct mxs_data
*data
;
137 struct mxs_ocotp
*otp
;
138 const struct of_device_id
*match
;
141 match
= of_match_device(dev
->driver
->of_match_table
, dev
);
142 if (!match
|| !match
->data
)
145 otp
= devm_kzalloc(dev
, sizeof(*otp
), GFP_KERNEL
);
149 otp
->base
= devm_platform_ioremap_resource(pdev
, 0);
150 if (IS_ERR(otp
->base
))
151 return PTR_ERR(otp
->base
);
153 otp
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
154 if (IS_ERR(otp
->clk
))
155 return PTR_ERR(otp
->clk
);
157 ret
= clk_prepare(otp
->clk
);
159 dev_err(dev
, "failed to prepare clk: %d\n", ret
);
165 ocotp_config
.size
= data
->size
;
166 ocotp_config
.priv
= otp
;
167 ocotp_config
.dev
= dev
;
168 otp
->nvmem
= devm_nvmem_register(dev
, &ocotp_config
);
169 if (IS_ERR(otp
->nvmem
)) {
170 ret
= PTR_ERR(otp
->nvmem
);
174 platform_set_drvdata(pdev
, otp
);
179 clk_unprepare(otp
->clk
);
184 static int mxs_ocotp_remove(struct platform_device
*pdev
)
186 struct mxs_ocotp
*otp
= platform_get_drvdata(pdev
);
188 clk_unprepare(otp
->clk
);
193 static struct platform_driver mxs_ocotp_driver
= {
194 .probe
= mxs_ocotp_probe
,
195 .remove
= mxs_ocotp_remove
,
198 .of_match_table
= mxs_ocotp_match
,
202 module_platform_driver(mxs_ocotp_driver
);
203 MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
204 MODULE_DESCRIPTION("driver for OCOTP in i.MX23/i.MX28");
205 MODULE_LICENSE("GPL v2");