GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / nvmem / imx-ocotp.c
1 /*
2  * i.MX6 OCOTP fusebox driver
3  *
4  * Copyright (c) 2015 Pengutronix, Philipp Zabel <p.zabel@pengutronix.de>
5  *
6  * Based on the barebox ocotp driver,
7  * Copyright (c) 2010 Baruch Siach <baruch@tkos.co.il>,
8  *      Orex Computed Radiography
9  *
10  * Write support based on the fsl_otp driver,
11  * Copyright (C) 2010-2013 Freescale Semiconductor, Inc
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2
15  * as published by the Free Software Foundation.
16  *
17  * http://www.opensource.org/licenses/gpl-license.html
18  * http://www.gnu.org/copyleft/gpl.html
19  */
20
21 #include <linux/clk.h>
22 #include <linux/device.h>
23 #include <linux/io.h>
24 #include <linux/module.h>
25 #include <linux/nvmem-provider.h>
26 #include <linux/of.h>
27 #include <linux/of_device.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31
32 #define IMX_OCOTP_OFFSET_B0W0           0x400 /* Offset from base address of the
33                                                * OTP Bank0 Word0
34                                                */
35 #define IMX_OCOTP_OFFSET_PER_WORD       0x10  /* Offset between the start addr
36                                                * of two consecutive OTP words.
37                                                */
38
39 #define IMX_OCOTP_ADDR_CTRL             0x0000
40 #define IMX_OCOTP_ADDR_CTRL_SET         0x0004
41 #define IMX_OCOTP_ADDR_CTRL_CLR         0x0008
42 #define IMX_OCOTP_ADDR_TIMING           0x0010
43 #define IMX_OCOTP_ADDR_DATA             0x0020
44
45 #define IMX_OCOTP_BM_CTRL_ADDR          0x0000007F
46 #define IMX_OCOTP_BM_CTRL_BUSY          0x00000100
47 #define IMX_OCOTP_BM_CTRL_ERROR         0x00000200
48 #define IMX_OCOTP_BM_CTRL_REL_SHADOWS   0x00000400
49
50 #define DEF_RELAX                       20 /* > 16.5ns */
51 #define IMX_OCOTP_WR_UNLOCK             0x3E770000
52 #define IMX_OCOTP_READ_LOCKED_VAL       0xBADABADA
53
54 static DEFINE_MUTEX(ocotp_mutex);
55
56 struct ocotp_priv {
57         struct device *dev;
58         struct clk *clk;
59         void __iomem *base;
60         unsigned int nregs;
61         struct nvmem_config *config;
62 };
63
64 static int imx_ocotp_wait_for_busy(void __iomem *base, u32 flags)
65 {
66         int count;
67         u32 c, mask;
68
69         mask = IMX_OCOTP_BM_CTRL_BUSY | IMX_OCOTP_BM_CTRL_ERROR | flags;
70
71         for (count = 10000; count >= 0; count--) {
72                 c = readl(base + IMX_OCOTP_ADDR_CTRL);
73                 if (!(c & mask))
74                         break;
75                 cpu_relax();
76         }
77
78         if (count < 0) {
79                 /* HW_OCOTP_CTRL[ERROR] will be set under the following
80                  * conditions:
81                  * - A write is performed to a shadow register during a shadow
82                  *   reload (essentially, while HW_OCOTP_CTRL[RELOAD_SHADOWS] is
83                  *   set. In addition, the contents of the shadow register shall
84                  *   not be updated.
85                  * - A write is performed to a shadow register which has been
86                  *   locked.
87                  * - A read is performed to from a shadow register which has
88                  *   been read locked.
89                  * - A program is performed to a fuse word which has been locked
90                  * - A read is performed to from a fuse word which has been read
91                  *   locked.
92                  */
93                 if (c & IMX_OCOTP_BM_CTRL_ERROR)
94                         return -EPERM;
95                 return -ETIMEDOUT;
96         }
97
98         return 0;
99 }
100
101 static void imx_ocotp_clr_err_if_set(void __iomem *base)
102 {
103         u32 c;
104
105         c = readl(base + IMX_OCOTP_ADDR_CTRL);
106         if (!(c & IMX_OCOTP_BM_CTRL_ERROR))
107                 return;
108
109         writel(IMX_OCOTP_BM_CTRL_ERROR, base + IMX_OCOTP_ADDR_CTRL_CLR);
110 }
111
112 static int imx_ocotp_read(void *context, unsigned int offset,
113                           void *val, size_t bytes)
114 {
115         struct ocotp_priv *priv = context;
116         unsigned int count;
117         u32 *buf = val;
118         int i, ret;
119         u32 index;
120
121         index = offset >> 2;
122         count = bytes >> 2;
123
124         if (count > (priv->nregs - index))
125                 count = priv->nregs - index;
126
127         mutex_lock(&ocotp_mutex);
128
129         ret = clk_prepare_enable(priv->clk);
130         if (ret < 0) {
131                 mutex_unlock(&ocotp_mutex);
132                 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
133                 return ret;
134         }
135
136         ret = imx_ocotp_wait_for_busy(priv->base, 0);
137         if (ret < 0) {
138                 dev_err(priv->dev, "timeout during read setup\n");
139                 goto read_end;
140         }
141
142         for (i = index; i < (index + count); i++) {
143                 *buf++ = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 +
144                                i * IMX_OCOTP_OFFSET_PER_WORD);
145
146                 /* 47.3.1.2
147                  * For "read locked" registers 0xBADABADA will be returned and
148                  * HW_OCOTP_CTRL[ERROR] will be set. It must be cleared by
149                  * software before any new write, read or reload access can be
150                  * issued
151                  */
152                 if (*(buf - 1) == IMX_OCOTP_READ_LOCKED_VAL)
153                         imx_ocotp_clr_err_if_set(priv->base);
154         }
155         ret = 0;
156
157 read_end:
158         clk_disable_unprepare(priv->clk);
159         mutex_unlock(&ocotp_mutex);
160         return ret;
161 }
162
163 static int imx_ocotp_write(void *context, unsigned int offset, void *val,
164                            size_t bytes)
165 {
166         struct ocotp_priv *priv = context;
167         u32 *buf = val;
168         int ret;
169
170         unsigned long clk_rate = 0;
171         unsigned long strobe_read, relax, strobe_prog;
172         u32 timing = 0;
173         u32 ctrl;
174         u8 waddr;
175
176         /* allow only writing one complete OTP word at a time */
177         if ((bytes != priv->config->word_size) ||
178             (offset % priv->config->word_size))
179                 return -EINVAL;
180
181         mutex_lock(&ocotp_mutex);
182
183         ret = clk_prepare_enable(priv->clk);
184         if (ret < 0) {
185                 mutex_unlock(&ocotp_mutex);
186                 dev_err(priv->dev, "failed to prepare/enable ocotp clk\n");
187                 return ret;
188         }
189
190         /* 47.3.1.3.1
191          * Program HW_OCOTP_TIMING[STROBE_PROG] and HW_OCOTP_TIMING[RELAX]
192          * fields with timing values to match the current frequency of the
193          * ipg_clk. OTP writes will work at maximum bus frequencies as long
194          * as the HW_OCOTP_TIMING parameters are set correctly.
195          */
196         clk_rate = clk_get_rate(priv->clk);
197
198         relax = clk_rate / (1000000000 / DEF_RELAX) - 1;
199         strobe_prog = clk_rate / (1000000000 / 10000) + 2 * (DEF_RELAX + 1) - 1;
200         strobe_read = clk_rate / (1000000000 / 40) + 2 * (DEF_RELAX + 1) - 1;
201
202         timing = readl(priv->base + IMX_OCOTP_ADDR_TIMING) & 0x0FC00000;
203         timing |= strobe_prog & 0x00000FFF;
204         timing |= (relax       << 12) & 0x0000F000;
205         timing |= (strobe_read << 16) & 0x003F0000;
206
207         writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING);
208
209         /* 47.3.1.3.2
210          * Check that HW_OCOTP_CTRL[BUSY] and HW_OCOTP_CTRL[ERROR] are clear.
211          * Overlapped accesses are not supported by the controller. Any pending
212          * write or reload must be completed before a write access can be
213          * requested.
214          */
215         ret = imx_ocotp_wait_for_busy(priv->base, 0);
216         if (ret < 0) {
217                 dev_err(priv->dev, "timeout during timing setup\n");
218                 goto write_end;
219         }
220
221         /* 47.3.1.3.3
222          * Write the requested address to HW_OCOTP_CTRL[ADDR] and program the
223          * unlock code into HW_OCOTP_CTRL[WR_UNLOCK]. This must be programmed
224          * for each write access. The lock code is documented in the register
225          * description. Both the unlock code and address can be written in the
226          * same operation.
227          */
228         /* OTP write/read address specifies one of 128 word address locations */
229         waddr = offset / 4;
230
231         ctrl = readl(priv->base + IMX_OCOTP_ADDR_CTRL);
232         ctrl &= ~IMX_OCOTP_BM_CTRL_ADDR;
233         ctrl |= waddr & IMX_OCOTP_BM_CTRL_ADDR;
234         ctrl |= IMX_OCOTP_WR_UNLOCK;
235
236         writel(ctrl, priv->base + IMX_OCOTP_ADDR_CTRL);
237
238         /* 47.3.1.3.4
239          * Write the data to the HW_OCOTP_DATA register. This will automatically
240          * set HW_OCOTP_CTRL[BUSY] and clear HW_OCOTP_CTRL[WR_UNLOCK]. To
241          * protect programming same OTP bit twice, before program OCOTP will
242          * automatically read fuse value in OTP and use read value to mask
243          * program data. The controller will use masked program data to program
244          * a 32-bit word in the OTP per the address in HW_OCOTP_CTRL[ADDR]. Bit
245          * fields with 1's will result in that OTP bit being programmed. Bit
246          * fields with 0's will be ignored. At the same time that the write is
247          * accepted, the controller makes an internal copy of
248          * HW_OCOTP_CTRL[ADDR] which cannot be updated until the next write
249          * sequence is initiated. This copy guarantees that erroneous writes to
250          * HW_OCOTP_CTRL[ADDR] will not affect an active write operation. It
251          * should also be noted that during the programming HW_OCOTP_DATA will
252          * shift right (with zero fill). This shifting is required to program
253          * the OTP serially. During the write operation, HW_OCOTP_DATA cannot be
254          * modified.
255          */
256         writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA);
257
258         /* 47.4.1.4.5
259          * Once complete, the controller will clear BUSY. A write request to a
260          * protected or locked region will result in no OTP access and no
261          * setting of HW_OCOTP_CTRL[BUSY]. In addition HW_OCOTP_CTRL[ERROR] will
262          * be set. It must be cleared by software before any new write access
263          * can be issued.
264          */
265         ret = imx_ocotp_wait_for_busy(priv->base, 0);
266         if (ret < 0) {
267                 if (ret == -EPERM) {
268                         dev_err(priv->dev, "failed write to locked region");
269                         imx_ocotp_clr_err_if_set(priv->base);
270                 } else {
271                         dev_err(priv->dev, "timeout during data write\n");
272                 }
273                 goto write_end;
274         }
275
276         /* 47.3.1.4
277          * Write Postamble: Due to internal electrical characteristics of the
278          * OTP during writes, all OTP operations following a write must be
279          * separated by 2 us after the clearing of HW_OCOTP_CTRL_BUSY following
280          * the write.
281          */
282         udelay(2);
283
284         /* reload all shadow registers */
285         writel(IMX_OCOTP_BM_CTRL_REL_SHADOWS,
286                priv->base + IMX_OCOTP_ADDR_CTRL_SET);
287         ret = imx_ocotp_wait_for_busy(priv->base,
288                                       IMX_OCOTP_BM_CTRL_REL_SHADOWS);
289         if (ret < 0) {
290                 dev_err(priv->dev, "timeout during shadow register reload\n");
291                 goto write_end;
292         }
293
294 write_end:
295         clk_disable_unprepare(priv->clk);
296         mutex_unlock(&ocotp_mutex);
297         if (ret < 0)
298                 return ret;
299         return bytes;
300 }
301
302 static struct nvmem_config imx_ocotp_nvmem_config = {
303         .name = "imx-ocotp",
304         .read_only = false,
305         .word_size = 4,
306         .stride = 4,
307         .owner = THIS_MODULE,
308         .reg_read = imx_ocotp_read,
309         .reg_write = imx_ocotp_write,
310 };
311
312 static const struct of_device_id imx_ocotp_dt_ids[] = {
313         { .compatible = "fsl,imx6q-ocotp",  (void *)128 },
314         { .compatible = "fsl,imx6sl-ocotp", (void *)64 },
315         { .compatible = "fsl,imx6sx-ocotp", (void *)128 },
316         { .compatible = "fsl,imx6ul-ocotp", (void *)128 },
317         { .compatible = "fsl,imx7d-ocotp", (void *)64 },
318         { },
319 };
320 MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids);
321
322 static int imx_ocotp_probe(struct platform_device *pdev)
323 {
324         const struct of_device_id *of_id;
325         struct device *dev = &pdev->dev;
326         struct resource *res;
327         struct ocotp_priv *priv;
328         struct nvmem_device *nvmem;
329
330         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
331         if (!priv)
332                 return -ENOMEM;
333
334         priv->dev = dev;
335
336         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
337         priv->base = devm_ioremap_resource(dev, res);
338         if (IS_ERR(priv->base))
339                 return PTR_ERR(priv->base);
340
341         priv->clk = devm_clk_get(dev, NULL);
342         if (IS_ERR(priv->clk))
343                 return PTR_ERR(priv->clk);
344
345         of_id = of_match_device(imx_ocotp_dt_ids, dev);
346         priv->nregs = (unsigned long)of_id->data;
347         imx_ocotp_nvmem_config.size = 4 * priv->nregs;
348         imx_ocotp_nvmem_config.dev = dev;
349         imx_ocotp_nvmem_config.priv = priv;
350         priv->config = &imx_ocotp_nvmem_config;
351         nvmem = nvmem_register(&imx_ocotp_nvmem_config);
352
353         if (IS_ERR(nvmem))
354                 return PTR_ERR(nvmem);
355
356         platform_set_drvdata(pdev, nvmem);
357
358         return 0;
359 }
360
361 static int imx_ocotp_remove(struct platform_device *pdev)
362 {
363         struct nvmem_device *nvmem = platform_get_drvdata(pdev);
364
365         return nvmem_unregister(nvmem);
366 }
367
368 static struct platform_driver imx_ocotp_driver = {
369         .probe  = imx_ocotp_probe,
370         .remove = imx_ocotp_remove,
371         .driver = {
372                 .name   = "imx_ocotp",
373                 .of_match_table = imx_ocotp_dt_ids,
374         },
375 };
376 module_platform_driver(imx_ocotp_driver);
377
378 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
379 MODULE_DESCRIPTION("i.MX6 OCOTP fuse box driver");
380 MODULE_LICENSE("GPL v2");