Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 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 | * |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 10 | * Write support based on the fsl_otp driver, |
| 11 | * Copyright (C) 2010-2013 Freescale Semiconductor, Inc |
| 12 | * |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 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 | |
Peng Fan | deb3197 | 2016-06-02 12:05:11 +0100 | [diff] [blame] | 21 | #include <linux/clk.h> |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 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> |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 29 | #include <linux/slab.h> |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 30 | #include <linux/delay.h> |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 31 | |
Richard Leitner | 9b66587 | 2017-03-31 13:44:54 +0100 | [diff] [blame] | 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 | */ |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 38 | |
Richard Leitner | 9b66587 | 2017-03-31 13:44:54 +0100 | [diff] [blame] | 39 | #define IMX_OCOTP_ADDR_CTRL 0x0000 |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 40 | #define IMX_OCOTP_ADDR_CTRL_SET 0x0004 |
Richard Leitner | 9b66587 | 2017-03-31 13:44:54 +0100 | [diff] [blame] | 41 | #define IMX_OCOTP_ADDR_CTRL_CLR 0x0008 |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 42 | #define IMX_OCOTP_ADDR_TIMING 0x0010 |
Bryan O'Donoghue | ffd9115 | 2017-10-24 10:54:29 +0100 | [diff] [blame^] | 43 | #define IMX_OCOTP_ADDR_DATA0 0x0020 |
| 44 | #define IMX_OCOTP_ADDR_DATA1 0x0030 |
| 45 | #define IMX_OCOTP_ADDR_DATA2 0x0040 |
| 46 | #define IMX_OCOTP_ADDR_DATA3 0x0050 |
Richard Leitner | 9b66587 | 2017-03-31 13:44:54 +0100 | [diff] [blame] | 47 | |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 48 | #define IMX_OCOTP_BM_CTRL_ADDR 0x0000007F |
| 49 | #define IMX_OCOTP_BM_CTRL_BUSY 0x00000100 |
Richard Leitner | 9b66587 | 2017-03-31 13:44:54 +0100 | [diff] [blame] | 50 | #define IMX_OCOTP_BM_CTRL_ERROR 0x00000200 |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 51 | #define IMX_OCOTP_BM_CTRL_REL_SHADOWS 0x00000400 |
Richard Leitner | 9b66587 | 2017-03-31 13:44:54 +0100 | [diff] [blame] | 52 | |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 53 | #define DEF_RELAX 20 /* > 16.5ns */ |
| 54 | #define IMX_OCOTP_WR_UNLOCK 0x3E770000 |
Richard Leitner | 9b66587 | 2017-03-31 13:44:54 +0100 | [diff] [blame] | 55 | #define IMX_OCOTP_READ_LOCKED_VAL 0xBADABADA |
| 56 | |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 57 | static DEFINE_MUTEX(ocotp_mutex); |
| 58 | |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 59 | struct ocotp_params { |
| 60 | unsigned int nregs; |
Bryan O'Donoghue | ffd9115 | 2017-10-24 10:54:29 +0100 | [diff] [blame^] | 61 | unsigned int bank_address_words; |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 64 | struct ocotp_priv { |
| 65 | struct device *dev; |
Peng Fan | deb3197 | 2016-06-02 12:05:11 +0100 | [diff] [blame] | 66 | struct clk *clk; |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 67 | void __iomem *base; |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 68 | const struct ocotp_params *params; |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 69 | struct nvmem_config *config; |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 70 | }; |
| 71 | |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 72 | static int imx_ocotp_wait_for_busy(void __iomem *base, u32 flags) |
| 73 | { |
| 74 | int count; |
| 75 | u32 c, mask; |
| 76 | |
| 77 | mask = IMX_OCOTP_BM_CTRL_BUSY | IMX_OCOTP_BM_CTRL_ERROR | flags; |
| 78 | |
| 79 | for (count = 10000; count >= 0; count--) { |
| 80 | c = readl(base + IMX_OCOTP_ADDR_CTRL); |
| 81 | if (!(c & mask)) |
| 82 | break; |
| 83 | cpu_relax(); |
| 84 | } |
| 85 | |
| 86 | if (count < 0) { |
| 87 | /* HW_OCOTP_CTRL[ERROR] will be set under the following |
| 88 | * conditions: |
| 89 | * - A write is performed to a shadow register during a shadow |
| 90 | * reload (essentially, while HW_OCOTP_CTRL[RELOAD_SHADOWS] is |
| 91 | * set. In addition, the contents of the shadow register shall |
| 92 | * not be updated. |
| 93 | * - A write is performed to a shadow register which has been |
| 94 | * locked. |
| 95 | * - A read is performed to from a shadow register which has |
| 96 | * been read locked. |
| 97 | * - A program is performed to a fuse word which has been locked |
| 98 | * - A read is performed to from a fuse word which has been read |
| 99 | * locked. |
| 100 | */ |
| 101 | if (c & IMX_OCOTP_BM_CTRL_ERROR) |
| 102 | return -EPERM; |
| 103 | return -ETIMEDOUT; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Richard Leitner | 9b66587 | 2017-03-31 13:44:54 +0100 | [diff] [blame] | 109 | static void imx_ocotp_clr_err_if_set(void __iomem *base) |
| 110 | { |
| 111 | u32 c; |
| 112 | |
| 113 | c = readl(base + IMX_OCOTP_ADDR_CTRL); |
| 114 | if (!(c & IMX_OCOTP_BM_CTRL_ERROR)) |
| 115 | return; |
| 116 | |
| 117 | writel(IMX_OCOTP_BM_CTRL_ERROR, base + IMX_OCOTP_ADDR_CTRL_CLR); |
| 118 | } |
| 119 | |
Srinivas Kandagatla | 33e5e29 | 2016-04-24 20:28:13 +0100 | [diff] [blame] | 120 | static int imx_ocotp_read(void *context, unsigned int offset, |
| 121 | void *val, size_t bytes) |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 122 | { |
| 123 | struct ocotp_priv *priv = context; |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 124 | unsigned int count; |
Srinivas Kandagatla | 33e5e29 | 2016-04-24 20:28:13 +0100 | [diff] [blame] | 125 | u32 *buf = val; |
Peng Fan | deb3197 | 2016-06-02 12:05:11 +0100 | [diff] [blame] | 126 | int i, ret; |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 127 | u32 index; |
| 128 | |
| 129 | index = offset >> 2; |
Srinivas Kandagatla | 33e5e29 | 2016-04-24 20:28:13 +0100 | [diff] [blame] | 130 | count = bytes >> 2; |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 131 | |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 132 | if (count > (priv->params->nregs - index)) |
| 133 | count = priv->params->nregs - index; |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 134 | |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 135 | mutex_lock(&ocotp_mutex); |
| 136 | |
Peng Fan | deb3197 | 2016-06-02 12:05:11 +0100 | [diff] [blame] | 137 | ret = clk_prepare_enable(priv->clk); |
| 138 | if (ret < 0) { |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 139 | mutex_unlock(&ocotp_mutex); |
Peng Fan | deb3197 | 2016-06-02 12:05:11 +0100 | [diff] [blame] | 140 | dev_err(priv->dev, "failed to prepare/enable ocotp clk\n"); |
| 141 | return ret; |
| 142 | } |
Richard Leitner | 9b66587 | 2017-03-31 13:44:54 +0100 | [diff] [blame] | 143 | |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 144 | ret = imx_ocotp_wait_for_busy(priv->base, 0); |
| 145 | if (ret < 0) { |
| 146 | dev_err(priv->dev, "timeout during read setup\n"); |
| 147 | goto read_end; |
| 148 | } |
| 149 | |
Richard Leitner | 9b66587 | 2017-03-31 13:44:54 +0100 | [diff] [blame] | 150 | for (i = index; i < (index + count); i++) { |
| 151 | *buf++ = readl(priv->base + IMX_OCOTP_OFFSET_B0W0 + |
| 152 | i * IMX_OCOTP_OFFSET_PER_WORD); |
| 153 | |
| 154 | /* 47.3.1.2 |
| 155 | * For "read locked" registers 0xBADABADA will be returned and |
| 156 | * HW_OCOTP_CTRL[ERROR] will be set. It must be cleared by |
| 157 | * software before any new write, read or reload access can be |
| 158 | * issued |
| 159 | */ |
| 160 | if (*(buf - 1) == IMX_OCOTP_READ_LOCKED_VAL) |
| 161 | imx_ocotp_clr_err_if_set(priv->base); |
| 162 | } |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 163 | ret = 0; |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 164 | |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 165 | read_end: |
Peng Fan | deb3197 | 2016-06-02 12:05:11 +0100 | [diff] [blame] | 166 | clk_disable_unprepare(priv->clk); |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 167 | mutex_unlock(&ocotp_mutex); |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | static int imx_ocotp_write(void *context, unsigned int offset, void *val, |
| 172 | size_t bytes) |
| 173 | { |
| 174 | struct ocotp_priv *priv = context; |
| 175 | u32 *buf = val; |
| 176 | int ret; |
| 177 | |
| 178 | unsigned long clk_rate = 0; |
| 179 | unsigned long strobe_read, relax, strobe_prog; |
| 180 | u32 timing = 0; |
| 181 | u32 ctrl; |
| 182 | u8 waddr; |
Bryan O'Donoghue | ffd9115 | 2017-10-24 10:54:29 +0100 | [diff] [blame^] | 183 | u8 word = 0; |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 184 | |
| 185 | /* allow only writing one complete OTP word at a time */ |
| 186 | if ((bytes != priv->config->word_size) || |
| 187 | (offset % priv->config->word_size)) |
| 188 | return -EINVAL; |
| 189 | |
| 190 | mutex_lock(&ocotp_mutex); |
| 191 | |
| 192 | ret = clk_prepare_enable(priv->clk); |
| 193 | if (ret < 0) { |
| 194 | mutex_unlock(&ocotp_mutex); |
| 195 | dev_err(priv->dev, "failed to prepare/enable ocotp clk\n"); |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | /* 47.3.1.3.1 |
| 200 | * Program HW_OCOTP_TIMING[STROBE_PROG] and HW_OCOTP_TIMING[RELAX] |
| 201 | * fields with timing values to match the current frequency of the |
| 202 | * ipg_clk. OTP writes will work at maximum bus frequencies as long |
| 203 | * as the HW_OCOTP_TIMING parameters are set correctly. |
| 204 | */ |
| 205 | clk_rate = clk_get_rate(priv->clk); |
| 206 | |
| 207 | relax = clk_rate / (1000000000 / DEF_RELAX) - 1; |
| 208 | strobe_prog = clk_rate / (1000000000 / 10000) + 2 * (DEF_RELAX + 1) - 1; |
| 209 | strobe_read = clk_rate / (1000000000 / 40) + 2 * (DEF_RELAX + 1) - 1; |
| 210 | |
| 211 | timing = strobe_prog & 0x00000FFF; |
| 212 | timing |= (relax << 12) & 0x0000F000; |
| 213 | timing |= (strobe_read << 16) & 0x003F0000; |
| 214 | |
| 215 | writel(timing, priv->base + IMX_OCOTP_ADDR_TIMING); |
| 216 | |
| 217 | /* 47.3.1.3.2 |
| 218 | * Check that HW_OCOTP_CTRL[BUSY] and HW_OCOTP_CTRL[ERROR] are clear. |
| 219 | * Overlapped accesses are not supported by the controller. Any pending |
| 220 | * write or reload must be completed before a write access can be |
| 221 | * requested. |
| 222 | */ |
| 223 | ret = imx_ocotp_wait_for_busy(priv->base, 0); |
| 224 | if (ret < 0) { |
| 225 | dev_err(priv->dev, "timeout during timing setup\n"); |
| 226 | goto write_end; |
| 227 | } |
| 228 | |
| 229 | /* 47.3.1.3.3 |
| 230 | * Write the requested address to HW_OCOTP_CTRL[ADDR] and program the |
| 231 | * unlock code into HW_OCOTP_CTRL[WR_UNLOCK]. This must be programmed |
| 232 | * for each write access. The lock code is documented in the register |
| 233 | * description. Both the unlock code and address can be written in the |
| 234 | * same operation. |
| 235 | */ |
Bryan O'Donoghue | ffd9115 | 2017-10-24 10:54:29 +0100 | [diff] [blame^] | 236 | if (priv->params->bank_address_words != 0) { |
| 237 | /* |
| 238 | * In banked/i.MX7 mode the OTP register bank goes into waddr |
| 239 | * see i.MX 7Solo Applications Processor Reference Manual, Rev. |
| 240 | * 0.1 section 6.4.3.1 |
| 241 | */ |
| 242 | offset = offset / priv->config->word_size; |
| 243 | waddr = offset / priv->params->bank_address_words; |
| 244 | word = offset & (priv->params->bank_address_words - 1); |
| 245 | } else { |
| 246 | /* |
| 247 | * Non-banked i.MX6 mode. |
| 248 | * OTP write/read address specifies one of 128 word address |
| 249 | * locations |
| 250 | */ |
| 251 | waddr = offset / 4; |
| 252 | } |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 253 | |
| 254 | ctrl = readl(priv->base + IMX_OCOTP_ADDR_CTRL); |
| 255 | ctrl &= ~IMX_OCOTP_BM_CTRL_ADDR; |
| 256 | ctrl |= waddr & IMX_OCOTP_BM_CTRL_ADDR; |
| 257 | ctrl |= IMX_OCOTP_WR_UNLOCK; |
| 258 | |
| 259 | writel(ctrl, priv->base + IMX_OCOTP_ADDR_CTRL); |
| 260 | |
| 261 | /* 47.3.1.3.4 |
| 262 | * Write the data to the HW_OCOTP_DATA register. This will automatically |
| 263 | * set HW_OCOTP_CTRL[BUSY] and clear HW_OCOTP_CTRL[WR_UNLOCK]. To |
| 264 | * protect programming same OTP bit twice, before program OCOTP will |
| 265 | * automatically read fuse value in OTP and use read value to mask |
| 266 | * program data. The controller will use masked program data to program |
| 267 | * a 32-bit word in the OTP per the address in HW_OCOTP_CTRL[ADDR]. Bit |
| 268 | * fields with 1's will result in that OTP bit being programmed. Bit |
| 269 | * fields with 0's will be ignored. At the same time that the write is |
| 270 | * accepted, the controller makes an internal copy of |
| 271 | * HW_OCOTP_CTRL[ADDR] which cannot be updated until the next write |
| 272 | * sequence is initiated. This copy guarantees that erroneous writes to |
| 273 | * HW_OCOTP_CTRL[ADDR] will not affect an active write operation. It |
| 274 | * should also be noted that during the programming HW_OCOTP_DATA will |
| 275 | * shift right (with zero fill). This shifting is required to program |
| 276 | * the OTP serially. During the write operation, HW_OCOTP_DATA cannot be |
| 277 | * modified. |
Bryan O'Donoghue | ffd9115 | 2017-10-24 10:54:29 +0100 | [diff] [blame^] | 278 | * Note: on i.MX7 there are four data fields to write for banked write |
| 279 | * with the fuse blowing operation only taking place after data0 |
| 280 | * has been written. This is why data0 must always be the last |
| 281 | * register written. |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 282 | */ |
Bryan O'Donoghue | ffd9115 | 2017-10-24 10:54:29 +0100 | [diff] [blame^] | 283 | if (priv->params->bank_address_words != 0) { |
| 284 | /* Banked/i.MX7 mode */ |
| 285 | switch (word) { |
| 286 | case 0: |
| 287 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA1); |
| 288 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA2); |
| 289 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA3); |
| 290 | writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0); |
| 291 | break; |
| 292 | case 1: |
| 293 | writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA1); |
| 294 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA2); |
| 295 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA3); |
| 296 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA0); |
| 297 | break; |
| 298 | case 2: |
| 299 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA1); |
| 300 | writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA2); |
| 301 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA3); |
| 302 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA0); |
| 303 | break; |
| 304 | case 3: |
| 305 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA1); |
| 306 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA2); |
| 307 | writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA3); |
| 308 | writel(0, priv->base + IMX_OCOTP_ADDR_DATA0); |
| 309 | break; |
| 310 | } |
| 311 | } else { |
| 312 | /* Non-banked i.MX6 mode */ |
| 313 | writel(*buf, priv->base + IMX_OCOTP_ADDR_DATA0); |
| 314 | } |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 315 | |
| 316 | /* 47.4.1.4.5 |
| 317 | * Once complete, the controller will clear BUSY. A write request to a |
| 318 | * protected or locked region will result in no OTP access and no |
| 319 | * setting of HW_OCOTP_CTRL[BUSY]. In addition HW_OCOTP_CTRL[ERROR] will |
| 320 | * be set. It must be cleared by software before any new write access |
| 321 | * can be issued. |
| 322 | */ |
| 323 | ret = imx_ocotp_wait_for_busy(priv->base, 0); |
| 324 | if (ret < 0) { |
| 325 | if (ret == -EPERM) { |
| 326 | dev_err(priv->dev, "failed write to locked region"); |
| 327 | imx_ocotp_clr_err_if_set(priv->base); |
| 328 | } else { |
| 329 | dev_err(priv->dev, "timeout during data write\n"); |
| 330 | } |
| 331 | goto write_end; |
| 332 | } |
| 333 | |
| 334 | /* 47.3.1.4 |
| 335 | * Write Postamble: Due to internal electrical characteristics of the |
| 336 | * OTP during writes, all OTP operations following a write must be |
| 337 | * separated by 2 us after the clearing of HW_OCOTP_CTRL_BUSY following |
| 338 | * the write. |
| 339 | */ |
| 340 | udelay(2); |
| 341 | |
| 342 | /* reload all shadow registers */ |
| 343 | writel(IMX_OCOTP_BM_CTRL_REL_SHADOWS, |
| 344 | priv->base + IMX_OCOTP_ADDR_CTRL_SET); |
| 345 | ret = imx_ocotp_wait_for_busy(priv->base, |
| 346 | IMX_OCOTP_BM_CTRL_REL_SHADOWS); |
| 347 | if (ret < 0) { |
| 348 | dev_err(priv->dev, "timeout during shadow register reload\n"); |
| 349 | goto write_end; |
| 350 | } |
| 351 | |
| 352 | write_end: |
| 353 | clk_disable_unprepare(priv->clk); |
| 354 | mutex_unlock(&ocotp_mutex); |
| 355 | if (ret < 0) |
| 356 | return ret; |
| 357 | return bytes; |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 360 | static struct nvmem_config imx_ocotp_nvmem_config = { |
| 361 | .name = "imx-ocotp", |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 362 | .read_only = false, |
Srinivas Kandagatla | 33e5e29 | 2016-04-24 20:28:13 +0100 | [diff] [blame] | 363 | .word_size = 4, |
| 364 | .stride = 4, |
Srinivas Kandagatla | 33e5e29 | 2016-04-24 20:28:13 +0100 | [diff] [blame] | 365 | .reg_read = imx_ocotp_read, |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 366 | .reg_write = imx_ocotp_write, |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 367 | }; |
| 368 | |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 369 | static const struct ocotp_params imx6q_params = { |
| 370 | .nregs = 128, |
Bryan O'Donoghue | ffd9115 | 2017-10-24 10:54:29 +0100 | [diff] [blame^] | 371 | .bank_address_words = 0, |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | static const struct ocotp_params imx6sl_params = { |
| 375 | .nregs = 64, |
Bryan O'Donoghue | ffd9115 | 2017-10-24 10:54:29 +0100 | [diff] [blame^] | 376 | .bank_address_words = 0, |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | static const struct ocotp_params imx6sx_params = { |
| 380 | .nregs = 128, |
Bryan O'Donoghue | ffd9115 | 2017-10-24 10:54:29 +0100 | [diff] [blame^] | 381 | .bank_address_words = 0, |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | static const struct ocotp_params imx6ul_params = { |
| 385 | .nregs = 128, |
Bryan O'Donoghue | ffd9115 | 2017-10-24 10:54:29 +0100 | [diff] [blame^] | 386 | .bank_address_words = 0, |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | static const struct ocotp_params imx7d_params = { |
| 390 | .nregs = 64, |
Bryan O'Donoghue | ffd9115 | 2017-10-24 10:54:29 +0100 | [diff] [blame^] | 391 | .bank_address_words = 4, |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 392 | }; |
| 393 | |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 394 | static const struct of_device_id imx_ocotp_dt_ids[] = { |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 395 | { .compatible = "fsl,imx6q-ocotp", .data = &imx6q_params }, |
| 396 | { .compatible = "fsl,imx6sl-ocotp", .data = &imx6sl_params }, |
| 397 | { .compatible = "fsl,imx6sx-ocotp", .data = &imx6sx_params }, |
| 398 | { .compatible = "fsl,imx6ul-ocotp", .data = &imx6ul_params }, |
| 399 | { .compatible = "fsl,imx7d-ocotp", .data = &imx7d_params }, |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 400 | { }, |
| 401 | }; |
| 402 | MODULE_DEVICE_TABLE(of, imx_ocotp_dt_ids); |
| 403 | |
| 404 | static int imx_ocotp_probe(struct platform_device *pdev) |
| 405 | { |
| 406 | const struct of_device_id *of_id; |
| 407 | struct device *dev = &pdev->dev; |
| 408 | struct resource *res; |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 409 | struct ocotp_priv *priv; |
| 410 | struct nvmem_device *nvmem; |
| 411 | |
| 412 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 413 | if (!priv) |
| 414 | return -ENOMEM; |
| 415 | |
Richard Leitner | 4cefb74 | 2017-03-31 13:44:49 +0100 | [diff] [blame] | 416 | priv->dev = dev; |
| 417 | |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 418 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 419 | priv->base = devm_ioremap_resource(dev, res); |
| 420 | if (IS_ERR(priv->base)) |
| 421 | return PTR_ERR(priv->base); |
| 422 | |
Richard Leitner | 4cefb74 | 2017-03-31 13:44:49 +0100 | [diff] [blame] | 423 | priv->clk = devm_clk_get(dev, NULL); |
Peng Fan | deb3197 | 2016-06-02 12:05:11 +0100 | [diff] [blame] | 424 | if (IS_ERR(priv->clk)) |
| 425 | return PTR_ERR(priv->clk); |
| 426 | |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 427 | of_id = of_match_device(imx_ocotp_dt_ids, dev); |
Bryan O'Donoghue | e20d2b2 | 2017-10-24 10:54:28 +0100 | [diff] [blame] | 428 | priv->params = of_device_get_match_data(&pdev->dev); |
| 429 | imx_ocotp_nvmem_config.size = 4 * priv->params->nregs; |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 430 | imx_ocotp_nvmem_config.dev = dev; |
Srinivas Kandagatla | 33e5e29 | 2016-04-24 20:28:13 +0100 | [diff] [blame] | 431 | imx_ocotp_nvmem_config.priv = priv; |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 432 | priv->config = &imx_ocotp_nvmem_config; |
Bryan O'Donoghue | 9d6a8da | 2017-10-24 10:54:27 +0100 | [diff] [blame] | 433 | if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx7d-ocotp")) |
| 434 | imx_ocotp_nvmem_config.read_only = true; |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 435 | nvmem = nvmem_register(&imx_ocotp_nvmem_config); |
Richard Leitner | 0642bac | 2017-03-31 13:44:55 +0100 | [diff] [blame] | 436 | |
Philipp Zabel | 3edba6b | 2015-09-30 13:55:47 +0100 | [diff] [blame] | 437 | if (IS_ERR(nvmem)) |
| 438 | return PTR_ERR(nvmem); |
| 439 | |
| 440 | platform_set_drvdata(pdev, nvmem); |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static int imx_ocotp_remove(struct platform_device *pdev) |
| 446 | { |
| 447 | struct nvmem_device *nvmem = platform_get_drvdata(pdev); |
| 448 | |
| 449 | return nvmem_unregister(nvmem); |
| 450 | } |
| 451 | |
| 452 | static struct platform_driver imx_ocotp_driver = { |
| 453 | .probe = imx_ocotp_probe, |
| 454 | .remove = imx_ocotp_remove, |
| 455 | .driver = { |
| 456 | .name = "imx_ocotp", |
| 457 | .of_match_table = imx_ocotp_dt_ids, |
| 458 | }, |
| 459 | }; |
| 460 | module_platform_driver(imx_ocotp_driver); |
| 461 | |
| 462 | MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>"); |
| 463 | MODULE_DESCRIPTION("i.MX6 OCOTP fuse box driver"); |
| 464 | MODULE_LICENSE("GPL v2"); |