Merge tag 'iio-fixes-for-5.2a' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23...
[linux-2.6-block.git] / drivers / nvmem / mxs-ocotp.c
CommitLineData
c01e9a11
SW
1/*
2 * Freescale MXS On-Chip OTP driver
3 *
4 * Copyright (C) 2015 Stefan Wahren <stefan.wahren@i2se.com>
5 *
6 * Based on the driver from Huang Shijie and Christoph G. Baumann
7 *
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.
12 *
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.
17 *
18 */
19#include <linux/clk.h>
20#include <linux/delay.h>
21#include <linux/device.h>
22#include <linux/err.h>
23#include <linux/io.h>
24#include <linux/module.h>
25#include <linux/nvmem-provider.h>
26#include <linux/of_device.h>
27#include <linux/platform_device.h>
c01e9a11
SW
28#include <linux/slab.h>
29#include <linux/stmp_device.h>
30
31/* OCOTP registers and bits */
32
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)
36
37#define OCOTP_TIMEOUT 10000
38#define OCOTP_DATA_OFFSET 0x20
39
40struct mxs_ocotp {
41 struct clk *clk;
42 void __iomem *base;
43 struct nvmem_device *nvmem;
44};
45
46static int mxs_ocotp_wait(struct mxs_ocotp *otp)
47{
48 int timeout = OCOTP_TIMEOUT;
49 unsigned int status = 0;
50
51 while (timeout--) {
52 status = readl(otp->base);
53
54 if (!(status & (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)))
55 break;
56
57 cpu_relax();
58 }
59
60 if (status & BM_OCOTP_CTRL_BUSY)
61 return -EBUSY;
62 else if (status & BM_OCOTP_CTRL_ERROR)
63 return -EIO;
64
65 return 0;
66}
67
7d8867d7
SK
68static int mxs_ocotp_read(void *context, unsigned int offset,
69 void *val, size_t bytes)
c01e9a11
SW
70{
71 struct mxs_ocotp *otp = context;
c01e9a11
SW
72 u32 *buf = val;
73 int ret;
74
75 ret = clk_enable(otp->clk);
76 if (ret)
77 return ret;
78
79 writel(BM_OCOTP_CTRL_ERROR, otp->base + STMP_OFFSET_REG_CLR);
80
81 ret = mxs_ocotp_wait(otp);
82 if (ret)
83 goto disable_clk;
84
85 /* open OCOTP banks for read */
86 writel(BM_OCOTP_CTRL_RD_BANK_OPEN, otp->base + STMP_OFFSET_REG_SET);
87
88 /* approximately wait 33 hclk cycles */
89 udelay(1);
90
91 ret = mxs_ocotp_wait(otp);
92 if (ret)
93 goto close_banks;
94
7d8867d7 95 while (bytes) {
c01e9a11
SW
96 if ((offset < OCOTP_DATA_OFFSET) || (offset % 16)) {
97 /* fill up non-data register */
7d8867d7 98 *buf++ = 0;
c01e9a11 99 } else {
7d8867d7 100 *buf++ = readl(otp->base + offset);
c01e9a11
SW
101 }
102
7d8867d7
SK
103 bytes -= 4;
104 offset += 4;
c01e9a11
SW
105 }
106
107close_banks:
108 /* close banks for power saving */
109 writel(BM_OCOTP_CTRL_RD_BANK_OPEN, otp->base + STMP_OFFSET_REG_CLR);
110
111disable_clk:
112 clk_disable(otp->clk);
113
114 return ret;
115}
116
c01e9a11
SW
117static struct nvmem_config ocotp_config = {
118 .name = "mxs-ocotp",
7d8867d7
SK
119 .stride = 16,
120 .word_size = 4,
7d8867d7 121 .reg_read = mxs_ocotp_read,
c01e9a11
SW
122};
123
7d8867d7
SK
124struct mxs_data {
125 int size;
c01e9a11
SW
126};
127
7d8867d7
SK
128static const struct mxs_data imx23_data = {
129 .size = 0x220,
c01e9a11
SW
130};
131
7d8867d7
SK
132static const struct mxs_data imx28_data = {
133 .size = 0x2a0,
c01e9a11
SW
134};
135
136static const struct of_device_id mxs_ocotp_match[] = {
7d8867d7
SK
137 { .compatible = "fsl,imx23-ocotp", .data = &imx23_data },
138 { .compatible = "fsl,imx28-ocotp", .data = &imx28_data },
c01e9a11
SW
139 { /* sentinel */},
140};
141MODULE_DEVICE_TABLE(of, mxs_ocotp_match);
142
143static int mxs_ocotp_probe(struct platform_device *pdev)
144{
145 struct device *dev = &pdev->dev;
7d8867d7 146 const struct mxs_data *data;
c01e9a11 147 struct mxs_ocotp *otp;
c01e9a11 148 const struct of_device_id *match;
c01e9a11
SW
149 int ret;
150
151 match = of_match_device(dev->driver->of_match_table, dev);
152 if (!match || !match->data)
153 return -EINVAL;
154
155 otp = devm_kzalloc(dev, sizeof(*otp), GFP_KERNEL);
156 if (!otp)
157 return -ENOMEM;
158
794a1e22 159 otp->base = devm_platform_ioremap_resource(pdev, 0);
c01e9a11
SW
160 if (IS_ERR(otp->base))
161 return PTR_ERR(otp->base);
162
163 otp->clk = devm_clk_get(&pdev->dev, NULL);
164 if (IS_ERR(otp->clk))
165 return PTR_ERR(otp->clk);
166
167 ret = clk_prepare(otp->clk);
168 if (ret < 0) {
169 dev_err(dev, "failed to prepare clk: %d\n", ret);
170 return ret;
171 }
172
7d8867d7 173 data = match->data;
c01e9a11 174
7d8867d7
SK
175 ocotp_config.size = data->size;
176 ocotp_config.priv = otp;
c01e9a11 177 ocotp_config.dev = dev;
7d9f9f24 178 otp->nvmem = devm_nvmem_register(dev, &ocotp_config);
c01e9a11
SW
179 if (IS_ERR(otp->nvmem)) {
180 ret = PTR_ERR(otp->nvmem);
181 goto err_clk;
182 }
183
184 platform_set_drvdata(pdev, otp);
185
186 return 0;
187
188err_clk:
189 clk_unprepare(otp->clk);
190
191 return ret;
192}
193
194static int mxs_ocotp_remove(struct platform_device *pdev)
195{
196 struct mxs_ocotp *otp = platform_get_drvdata(pdev);
197
198 clk_unprepare(otp->clk);
199
7d9f9f24 200 return 0;
c01e9a11
SW
201}
202
203static struct platform_driver mxs_ocotp_driver = {
204 .probe = mxs_ocotp_probe,
205 .remove = mxs_ocotp_remove,
206 .driver = {
207 .name = "mxs-ocotp",
208 .of_match_table = mxs_ocotp_match,
209 },
210};
211
212module_platform_driver(mxs_ocotp_driver);
213MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
214MODULE_DESCRIPTION("driver for OCOTP in i.MX23/i.MX28");
215MODULE_LICENSE("GPL v2");