2 * Freescale MXS On-Chip OTP driver
4 * Copyright (C) 2015 Stefan Wahren <stefan.wahren@i2se.com>
6 * Based on the driver from Huang Shijie and Christoph G. Baumann
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/clk.h>
20 #include <linux/delay.h>
21 #include <linux/device.h>
22 #include <linux/err.h>
24 #include <linux/module.h>
25 #include <linux/nvmem-provider.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/stmp_device.h>
31 /* OCOTP registers and bits */
33 #define BM_OCOTP_CTRL_RD_BANK_OPEN BIT(12)
34 #define BM_OCOTP_CTRL_ERROR BIT(9)
35 #define BM_OCOTP_CTRL_BUSY BIT(8)
37 #define OCOTP_TIMEOUT 10000
38 #define OCOTP_DATA_OFFSET 0x20
43 struct nvmem_device
*nvmem
;
46 static int mxs_ocotp_wait(struct mxs_ocotp
*otp
)
48 int timeout
= OCOTP_TIMEOUT
;
49 unsigned int status
= 0;
52 status
= readl(otp
->base
);
54 if (!(status
& (BM_OCOTP_CTRL_BUSY
| BM_OCOTP_CTRL_ERROR
)))
60 if (status
& BM_OCOTP_CTRL_BUSY
)
62 else if (status
& BM_OCOTP_CTRL_ERROR
)
68 static int mxs_ocotp_read(void *context
, unsigned int offset
,
69 void *val
, size_t bytes
)
71 struct mxs_ocotp
*otp
= context
;
75 ret
= clk_enable(otp
->clk
);
79 writel(BM_OCOTP_CTRL_ERROR
, otp
->base
+ STMP_OFFSET_REG_CLR
);
81 ret
= mxs_ocotp_wait(otp
);
85 /* open OCOTP banks for read */
86 writel(BM_OCOTP_CTRL_RD_BANK_OPEN
, otp
->base
+ STMP_OFFSET_REG_SET
);
88 /* approximately wait 33 hclk cycles */
91 ret
= mxs_ocotp_wait(otp
);
96 if ((offset
< OCOTP_DATA_OFFSET
) || (offset
% 16)) {
97 /* fill up non-data register */
100 *buf
++ = readl(otp
->base
+ offset
);
108 /* close banks for power saving */
109 writel(BM_OCOTP_CTRL_RD_BANK_OPEN
, otp
->base
+ STMP_OFFSET_REG_CLR
);
112 clk_disable(otp
->clk
);
117 static struct nvmem_config ocotp_config
= {
121 .owner
= THIS_MODULE
,
122 .reg_read
= mxs_ocotp_read
,
129 static const struct mxs_data imx23_data
= {
133 static const struct mxs_data imx28_data
= {
137 static const struct of_device_id mxs_ocotp_match
[] = {
138 { .compatible
= "fsl,imx23-ocotp", .data
= &imx23_data
},
139 { .compatible
= "fsl,imx28-ocotp", .data
= &imx28_data
},
142 MODULE_DEVICE_TABLE(of
, mxs_ocotp_match
);
144 static int mxs_ocotp_probe(struct platform_device
*pdev
)
146 struct device
*dev
= &pdev
->dev
;
147 const struct mxs_data
*data
;
148 struct mxs_ocotp
*otp
;
149 struct resource
*res
;
150 const struct of_device_id
*match
;
153 match
= of_match_device(dev
->driver
->of_match_table
, dev
);
154 if (!match
|| !match
->data
)
157 otp
= devm_kzalloc(dev
, sizeof(*otp
), GFP_KERNEL
);
161 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
162 otp
->base
= devm_ioremap_resource(dev
, res
);
163 if (IS_ERR(otp
->base
))
164 return PTR_ERR(otp
->base
);
166 otp
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
167 if (IS_ERR(otp
->clk
))
168 return PTR_ERR(otp
->clk
);
170 ret
= clk_prepare(otp
->clk
);
172 dev_err(dev
, "failed to prepare clk: %d\n", ret
);
178 ocotp_config
.size
= data
->size
;
179 ocotp_config
.priv
= otp
;
180 ocotp_config
.dev
= dev
;
181 otp
->nvmem
= nvmem_register(&ocotp_config
);
182 if (IS_ERR(otp
->nvmem
)) {
183 ret
= PTR_ERR(otp
->nvmem
);
187 platform_set_drvdata(pdev
, otp
);
192 clk_unprepare(otp
->clk
);
197 static int mxs_ocotp_remove(struct platform_device
*pdev
)
199 struct mxs_ocotp
*otp
= platform_get_drvdata(pdev
);
201 clk_unprepare(otp
->clk
);
203 return nvmem_unregister(otp
->nvmem
);
206 static struct platform_driver mxs_ocotp_driver
= {
207 .probe
= mxs_ocotp_probe
,
208 .remove
= mxs_ocotp_remove
,
211 .of_match_table
= mxs_ocotp_match
,
215 module_platform_driver(mxs_ocotp_driver
);
216 MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
217 MODULE_DESCRIPTION("driver for OCOTP in i.MX23/i.MX28");
218 MODULE_LICENSE("GPL v2");