Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 ADVANSEE |
| 4 | * Benoît Thébaudeau <benoit.thebaudeau@advansee.com> |
| 5 | * |
| 6 | * Based on Dirk Behme's |
| 7 | * https://github.com/dirkbehme/u-boot-imx6/blob/28b17e9/drivers/misc/imx_otp.c, |
| 8 | * which is based on Freescale's |
| 9 | * http://git.freescale.com/git/cgit.cgi/imx/uboot-imx.git/tree/drivers/misc/imx_otp.c?h=imx_v2009.08_1.1.0&id=9aa74e6, |
| 10 | * which is: |
| 11 | * Copyright (C) 2011 Freescale Semiconductor, Inc. |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <common.h> |
| 15 | #include <fuse.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 16 | #include <linux/delay.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 17 | #include <linux/errno.h> |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 18 | #include <asm/io.h> |
| 19 | #include <asm/arch/clock.h> |
| 20 | #include <asm/arch/imx-regs.h> |
Stefano Babic | 33731bc | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 21 | #include <asm/mach-imx/sys_proto.h> |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 22 | |
| 23 | #define BO_CTRL_WR_UNLOCK 16 |
| 24 | #define BM_CTRL_WR_UNLOCK 0xffff0000 |
| 25 | #define BV_CTRL_WR_UNLOCK_KEY 0x3e77 |
| 26 | #define BM_CTRL_ERROR 0x00000200 |
| 27 | #define BM_CTRL_BUSY 0x00000100 |
| 28 | #define BO_CTRL_ADDR 0 |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 29 | #ifdef CONFIG_MX7 |
| 30 | #define BM_CTRL_ADDR 0x0000000f |
| 31 | #define BM_CTRL_RELOAD 0x00000400 |
Peng Fan | 98c5782 | 2017-02-22 16:21:46 +0800 | [diff] [blame] | 32 | #elif defined(CONFIG_MX7ULP) |
| 33 | #define BM_CTRL_ADDR 0x000000FF |
| 34 | #define BM_CTRL_RELOAD 0x00000400 |
| 35 | #define BM_OUT_STATUS_DED 0x00000400 |
| 36 | #define BM_OUT_STATUS_LOCKED 0x00000800 |
| 37 | #define BM_OUT_STATUS_PROGFAIL 0x00001000 |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 38 | #elif defined(CONFIG_IMX8M) |
Peng Fan | dc3accc | 2019-12-24 11:26:41 +0800 | [diff] [blame] | 39 | #ifdef CONFIG_IMX8MP |
| 40 | #undef BM_CTRL_ADDR |
| 41 | #undef BM_CTRL_ERROR |
| 42 | #undef BM_CTRL_BUSY |
| 43 | #define BM_CTRL_ADDR 0x000001ff |
| 44 | #define BM_CTRL_ERROR 0x00000400 |
| 45 | #define BM_CTRL_BUSY 0x00000200 |
| 46 | #else |
Peng Fan | 716d496 | 2018-01-10 13:20:39 +0800 | [diff] [blame] | 47 | #define BM_CTRL_ADDR 0x000000ff |
Peng Fan | dc3accc | 2019-12-24 11:26:41 +0800 | [diff] [blame] | 48 | #endif |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 49 | #else |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 50 | #define BM_CTRL_ADDR 0x0000007f |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 51 | #endif |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 52 | |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 53 | #ifdef CONFIG_MX7 |
| 54 | #define BO_TIMING_FSOURCE 12 |
| 55 | #define BM_TIMING_FSOURCE 0x0007f000 |
| 56 | #define BV_TIMING_FSOURCE_NS 1001 |
| 57 | #define BO_TIMING_PROG 0 |
| 58 | #define BM_TIMING_PROG 0x00000fff |
| 59 | #define BV_TIMING_PROG_US 10 |
| 60 | #else |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 61 | #define BO_TIMING_STROBE_READ 16 |
| 62 | #define BM_TIMING_STROBE_READ 0x003f0000 |
| 63 | #define BV_TIMING_STROBE_READ_NS 37 |
| 64 | #define BO_TIMING_RELAX 12 |
| 65 | #define BM_TIMING_RELAX 0x0000f000 |
| 66 | #define BV_TIMING_RELAX_NS 17 |
| 67 | #define BO_TIMING_STROBE_PROG 0 |
| 68 | #define BM_TIMING_STROBE_PROG 0x00000fff |
| 69 | #define BV_TIMING_STROBE_PROG_US 10 |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 70 | #endif |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 71 | |
| 72 | #define BM_READ_CTRL_READ_FUSE 0x00000001 |
| 73 | |
| 74 | #define BF(value, field) (((value) << BO_##field) & BM_##field) |
| 75 | |
| 76 | #define WRITE_POSTAMBLE_US 2 |
| 77 | |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 78 | #if defined(CONFIG_MX6) || defined(CONFIG_VF610) |
| 79 | #define FUSE_BANK_SIZE 0x80 |
| 80 | #ifdef CONFIG_MX6SL |
| 81 | #define FUSE_BANKS 8 |
Peng Fan | a2c2f6c | 2016-12-11 19:24:33 +0800 | [diff] [blame] | 82 | #elif defined(CONFIG_MX6ULL) || defined(CONFIG_MX6SLL) |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 83 | #define FUSE_BANKS 9 |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 84 | #else |
| 85 | #define FUSE_BANKS 16 |
| 86 | #endif |
| 87 | #elif defined CONFIG_MX7 |
| 88 | #define FUSE_BANK_SIZE 0x40 |
| 89 | #define FUSE_BANKS 16 |
Peng Fan | 98c5782 | 2017-02-22 16:21:46 +0800 | [diff] [blame] | 90 | #elif defined(CONFIG_MX7ULP) |
| 91 | #define FUSE_BANK_SIZE 0x80 |
| 92 | #define FUSE_BANKS 31 |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 93 | #elif defined(CONFIG_IMX8M) |
Peng Fan | 716d496 | 2018-01-10 13:20:39 +0800 | [diff] [blame] | 94 | #define FUSE_BANK_SIZE 0x40 |
Peng Fan | dc3accc | 2019-12-24 11:26:41 +0800 | [diff] [blame] | 95 | #ifdef CONFIG_IMX8MP |
| 96 | #define FUSE_BANKS 96 |
| 97 | #else |
Peng Fan | 716d496 | 2018-01-10 13:20:39 +0800 | [diff] [blame] | 98 | #define FUSE_BANKS 64 |
Peng Fan | dc3accc | 2019-12-24 11:26:41 +0800 | [diff] [blame] | 99 | #endif |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 100 | #else |
| 101 | #error "Unsupported architecture\n" |
| 102 | #endif |
| 103 | |
| 104 | #if defined(CONFIG_MX6) |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * There is a hole in shadow registers address map of size 0x100 |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 108 | * between bank 5 and bank 6 on iMX6QP, iMX6DQ, iMX6SDL, iMX6SX, |
Peng Fan | a2c2f6c | 2016-12-11 19:24:33 +0800 | [diff] [blame] | 109 | * iMX6UL, i.MX6ULL and i.MX6SLL. |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 110 | * Bank 5 ends at 0x6F0 and Bank 6 starts at 0x800. When reading the fuses, |
| 111 | * we should account for this hole in address space. |
| 112 | * |
| 113 | * Similar hole exists between bank 14 and bank 15 of size |
| 114 | * 0x80 on iMX6QP, iMX6DQ, iMX6SDL and iMX6SX. |
| 115 | * Note: iMX6SL has only 0-7 banks and there is no hole. |
| 116 | * Note: iMX6UL doesn't have this one. |
| 117 | * |
| 118 | * This function is to covert user input to physical bank index. |
| 119 | * Only needed when read fuse, because we use register offset, so |
| 120 | * need to calculate real register offset. |
| 121 | * When write, no need to consider hole, always use the bank/word |
| 122 | * index from fuse map. |
| 123 | */ |
| 124 | u32 fuse_bank_physical(int index) |
| 125 | { |
| 126 | u32 phy_index; |
| 127 | |
Peng Fan | 98c5782 | 2017-02-22 16:21:46 +0800 | [diff] [blame] | 128 | if (is_mx6sl() || is_mx7ulp()) { |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 129 | phy_index = index; |
Peng Fan | a2c2f6c | 2016-12-11 19:24:33 +0800 | [diff] [blame] | 130 | } else if (is_mx6ul() || is_mx6ull() || is_mx6sll()) { |
| 131 | if ((is_mx6ull() || is_mx6sll()) && index == 8) |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 132 | index = 7; |
| 133 | |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 134 | if (index >= 6) |
| 135 | phy_index = fuse_bank_physical(5) + (index - 6) + 3; |
| 136 | else |
| 137 | phy_index = index; |
| 138 | } else { |
| 139 | if (index >= 15) |
| 140 | phy_index = fuse_bank_physical(14) + (index - 15) + 2; |
| 141 | else if (index >= 6) |
| 142 | phy_index = fuse_bank_physical(5) + (index - 6) + 3; |
| 143 | else |
| 144 | phy_index = index; |
| 145 | } |
| 146 | return phy_index; |
| 147 | } |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 148 | |
| 149 | u32 fuse_word_physical(u32 bank, u32 word_index) |
| 150 | { |
Peng Fan | a2c2f6c | 2016-12-11 19:24:33 +0800 | [diff] [blame] | 151 | if (is_mx6ull() || is_mx6sll()) { |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 152 | if (bank == 8) |
| 153 | word_index = word_index + 4; |
| 154 | } |
| 155 | |
| 156 | return word_index; |
| 157 | } |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 158 | #else |
| 159 | u32 fuse_bank_physical(int index) |
| 160 | { |
| 161 | return index; |
| 162 | } |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 163 | |
| 164 | u32 fuse_word_physical(u32 bank, u32 word_index) |
| 165 | { |
| 166 | return word_index; |
| 167 | } |
| 168 | |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 169 | #endif |
| 170 | |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 171 | static void wait_busy(struct ocotp_regs *regs, unsigned int delay_us) |
| 172 | { |
| 173 | while (readl(®s->ctrl) & BM_CTRL_BUSY) |
| 174 | udelay(delay_us); |
| 175 | } |
| 176 | |
| 177 | static void clear_error(struct ocotp_regs *regs) |
| 178 | { |
| 179 | writel(BM_CTRL_ERROR, ®s->ctrl_clr); |
| 180 | } |
| 181 | |
| 182 | static int prepare_access(struct ocotp_regs **regs, u32 bank, u32 word, |
| 183 | int assert, const char *caller) |
| 184 | { |
| 185 | *regs = (struct ocotp_regs *)OCOTP_BASE_ADDR; |
| 186 | |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 187 | if (bank >= FUSE_BANKS || |
| 188 | word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 2 || |
| 189 | !assert) { |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 190 | printf("mxc_ocotp %s(): Invalid argument\n", caller); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
Peng Fan | a2c2f6c | 2016-12-11 19:24:33 +0800 | [diff] [blame] | 194 | if (is_mx6ull() || is_mx6sll()) { |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 195 | if ((bank == 7 || bank == 8) && |
| 196 | word >= ARRAY_SIZE((*regs)->bank[0].fuse_regs) >> 3) { |
Peng Fan | a2c2f6c | 2016-12-11 19:24:33 +0800 | [diff] [blame] | 197 | printf("mxc_ocotp %s(): Invalid argument\n", caller); |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 198 | return -EINVAL; |
| 199 | } |
| 200 | } |
| 201 | |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 202 | enable_ocotp_clk(1); |
| 203 | |
| 204 | wait_busy(*regs, 1); |
| 205 | clear_error(*regs); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int finish_access(struct ocotp_regs *regs, const char *caller) |
| 211 | { |
| 212 | u32 err; |
| 213 | |
| 214 | err = !!(readl(®s->ctrl) & BM_CTRL_ERROR); |
| 215 | clear_error(regs); |
| 216 | |
Peng Fan | 98c5782 | 2017-02-22 16:21:46 +0800 | [diff] [blame] | 217 | #ifdef CONFIG_MX7ULP |
| 218 | /* Need to power down the OTP memory */ |
| 219 | writel(1, ®s->pdn); |
| 220 | #endif |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 221 | if (err) { |
| 222 | printf("mxc_ocotp %s(): Access protect error\n", caller); |
| 223 | return -EIO; |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int prepare_read(struct ocotp_regs **regs, u32 bank, u32 word, u32 *val, |
| 230 | const char *caller) |
| 231 | { |
| 232 | return prepare_access(regs, bank, word, val != NULL, caller); |
| 233 | } |
| 234 | |
| 235 | int fuse_read(u32 bank, u32 word, u32 *val) |
| 236 | { |
| 237 | struct ocotp_regs *regs; |
| 238 | int ret; |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 239 | u32 phy_bank; |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 240 | u32 phy_word; |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 241 | |
| 242 | ret = prepare_read(®s, bank, word, val, __func__); |
| 243 | if (ret) |
| 244 | return ret; |
| 245 | |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 246 | phy_bank = fuse_bank_physical(bank); |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 247 | phy_word = fuse_word_physical(bank, word); |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 248 | |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 249 | *val = readl(®s->bank[phy_bank].fuse_regs[phy_word << 2]); |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 250 | |
Peng Fan | 98c5782 | 2017-02-22 16:21:46 +0800 | [diff] [blame] | 251 | #ifdef CONFIG_MX7ULP |
| 252 | if (readl(®s->out_status) & BM_OUT_STATUS_DED) { |
| 253 | writel(BM_OUT_STATUS_DED, ®s->out_status_clr); |
| 254 | printf("mxc_ocotp %s(): fuse read wrong\n", __func__); |
| 255 | return -EIO; |
| 256 | } |
| 257 | #endif |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 258 | return finish_access(regs, __func__); |
| 259 | } |
| 260 | |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 261 | #ifdef CONFIG_MX7 |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 262 | static void set_timing(struct ocotp_regs *regs) |
| 263 | { |
| 264 | u32 ipg_clk; |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 265 | u32 fsource, prog; |
| 266 | u32 timing; |
| 267 | |
| 268 | ipg_clk = mxc_get_clock(MXC_IPG_CLK); |
| 269 | |
| 270 | fsource = DIV_ROUND_UP((ipg_clk / 1000) * BV_TIMING_FSOURCE_NS, |
| 271 | + 1000000) + 1; |
| 272 | prog = DIV_ROUND_CLOSEST(ipg_clk * BV_TIMING_PROG_US, 1000000) + 1; |
| 273 | |
| 274 | timing = BF(fsource, TIMING_FSOURCE) | BF(prog, TIMING_PROG); |
| 275 | |
| 276 | clrsetbits_le32(®s->timing, BM_TIMING_FSOURCE | BM_TIMING_PROG, |
| 277 | timing); |
| 278 | } |
Peng Fan | 98c5782 | 2017-02-22 16:21:46 +0800 | [diff] [blame] | 279 | #elif defined(CONFIG_MX7ULP) |
| 280 | static void set_timing(struct ocotp_regs *regs) |
| 281 | { |
| 282 | /* No timing set for MX7ULP */ |
| 283 | } |
| 284 | |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 285 | #else |
| 286 | static void set_timing(struct ocotp_regs *regs) |
| 287 | { |
| 288 | u32 ipg_clk; |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 289 | u32 relax, strobe_read, strobe_prog; |
| 290 | u32 timing; |
| 291 | |
| 292 | ipg_clk = mxc_get_clock(MXC_IPG_CLK); |
| 293 | |
| 294 | relax = DIV_ROUND_UP(ipg_clk * BV_TIMING_RELAX_NS, 1000000000) - 1; |
| 295 | strobe_read = DIV_ROUND_UP(ipg_clk * BV_TIMING_STROBE_READ_NS, |
| 296 | 1000000000) + 2 * (relax + 1) - 1; |
Masahiro Yamada | eff8e68 | 2014-11-07 03:03:26 +0900 | [diff] [blame] | 297 | strobe_prog = DIV_ROUND_CLOSEST(ipg_clk * BV_TIMING_STROBE_PROG_US, |
| 298 | 1000000) + 2 * (relax + 1) - 1; |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 299 | |
| 300 | timing = BF(strobe_read, TIMING_STROBE_READ) | |
| 301 | BF(relax, TIMING_RELAX) | |
| 302 | BF(strobe_prog, TIMING_STROBE_PROG); |
| 303 | |
| 304 | clrsetbits_le32(®s->timing, BM_TIMING_STROBE_READ | BM_TIMING_RELAX | |
| 305 | BM_TIMING_STROBE_PROG, timing); |
| 306 | } |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 307 | #endif |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 308 | |
| 309 | static void setup_direct_access(struct ocotp_regs *regs, u32 bank, u32 word, |
| 310 | int write) |
| 311 | { |
| 312 | u32 wr_unlock = write ? BV_CTRL_WR_UNLOCK_KEY : 0; |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 313 | #ifdef CONFIG_MX7 |
| 314 | u32 addr = bank; |
Peng Fan | 39945c1 | 2018-11-20 10:19:25 +0000 | [diff] [blame] | 315 | #elif defined CONFIG_IMX8M |
Peng Fan | 716d496 | 2018-01-10 13:20:39 +0800 | [diff] [blame] | 316 | u32 addr = bank << 2 | word; |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 317 | #else |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 318 | u32 addr; |
| 319 | /* Bank 7 and Bank 8 only supports 4 words each for i.MX6ULL */ |
Peng Fan | a2c2f6c | 2016-12-11 19:24:33 +0800 | [diff] [blame] | 320 | if ((is_mx6ull() || is_mx6sll()) && (bank > 7)) { |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 321 | bank = bank - 1; |
| 322 | word += 4; |
| 323 | } |
| 324 | addr = bank << 3 | word; |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 325 | #endif |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 326 | |
| 327 | set_timing(regs); |
| 328 | clrsetbits_le32(®s->ctrl, BM_CTRL_WR_UNLOCK | BM_CTRL_ADDR, |
| 329 | BF(wr_unlock, CTRL_WR_UNLOCK) | |
| 330 | BF(addr, CTRL_ADDR)); |
| 331 | } |
| 332 | |
| 333 | int fuse_sense(u32 bank, u32 word, u32 *val) |
| 334 | { |
| 335 | struct ocotp_regs *regs; |
| 336 | int ret; |
| 337 | |
Ye Li | c29b7c7 | 2021-03-19 15:57:17 +0800 | [diff] [blame] | 338 | if (is_imx8mq() && (soc_rev() >= CHIP_REV_2_1)) { |
Ye Li | 4d5e0a2 | 2019-04-17 09:41:23 +0000 | [diff] [blame] | 339 | printf("mxc_ocotp %s(): fuse sense is disabled\n", __func__); |
| 340 | return -EPERM; |
| 341 | } |
| 342 | |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 343 | ret = prepare_read(®s, bank, word, val, __func__); |
| 344 | if (ret) |
| 345 | return ret; |
| 346 | |
| 347 | setup_direct_access(regs, bank, word, false); |
| 348 | writel(BM_READ_CTRL_READ_FUSE, ®s->read_ctrl); |
| 349 | wait_busy(regs, 1); |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 350 | #ifdef CONFIG_MX7 |
| 351 | *val = readl((®s->read_fuse_data0) + (word << 2)); |
| 352 | #else |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 353 | *val = readl(®s->read_fuse_data); |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 354 | #endif |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 355 | |
Peng Fan | 98c5782 | 2017-02-22 16:21:46 +0800 | [diff] [blame] | 356 | #ifdef CONFIG_MX7ULP |
| 357 | if (readl(®s->out_status) & BM_OUT_STATUS_DED) { |
| 358 | writel(BM_OUT_STATUS_DED, ®s->out_status_clr); |
| 359 | printf("mxc_ocotp %s(): fuse read wrong\n", __func__); |
| 360 | return -EIO; |
| 361 | } |
| 362 | #endif |
| 363 | |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 364 | return finish_access(regs, __func__); |
| 365 | } |
| 366 | |
| 367 | static int prepare_write(struct ocotp_regs **regs, u32 bank, u32 word, |
| 368 | const char *caller) |
| 369 | { |
Peng Fan | 235ba33 | 2018-01-02 15:51:20 +0800 | [diff] [blame] | 370 | #ifdef CONFIG_MX7ULP |
| 371 | u32 val; |
| 372 | int ret; |
| 373 | |
| 374 | /* Only bank 0 and 1 are redundancy mode, others are ECC mode */ |
| 375 | if (bank != 0 && bank != 1) { |
Ye Li | 2cb89e3 | 2019-04-17 09:41:20 +0000 | [diff] [blame] | 376 | if ((soc_rev() < CHIP_REV_2_0) || |
| 377 | ((soc_rev() >= CHIP_REV_2_0) && |
| 378 | bank != 9 && bank != 10 && bank != 28)) { |
| 379 | ret = fuse_sense(bank, word, &val); |
| 380 | if (ret) |
| 381 | return ret; |
Peng Fan | 235ba33 | 2018-01-02 15:51:20 +0800 | [diff] [blame] | 382 | |
Ye Li | 2cb89e3 | 2019-04-17 09:41:20 +0000 | [diff] [blame] | 383 | if (val != 0) { |
| 384 | printf("mxc_ocotp: The word has been programmed, no more write\n"); |
| 385 | return -EPERM; |
| 386 | } |
Peng Fan | 235ba33 | 2018-01-02 15:51:20 +0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | #endif |
| 390 | |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 391 | return prepare_access(regs, bank, word, true, caller); |
| 392 | } |
| 393 | |
| 394 | int fuse_prog(u32 bank, u32 word, u32 val) |
| 395 | { |
| 396 | struct ocotp_regs *regs; |
| 397 | int ret; |
| 398 | |
| 399 | ret = prepare_write(®s, bank, word, __func__); |
| 400 | if (ret) |
| 401 | return ret; |
| 402 | |
| 403 | setup_direct_access(regs, bank, word, true); |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 404 | #ifdef CONFIG_MX7 |
| 405 | switch (word) { |
| 406 | case 0: |
| 407 | writel(0, ®s->data1); |
| 408 | writel(0, ®s->data2); |
| 409 | writel(0, ®s->data3); |
| 410 | writel(val, ®s->data0); |
| 411 | break; |
| 412 | case 1: |
| 413 | writel(val, ®s->data1); |
| 414 | writel(0, ®s->data2); |
| 415 | writel(0, ®s->data3); |
| 416 | writel(0, ®s->data0); |
| 417 | break; |
| 418 | case 2: |
| 419 | writel(0, ®s->data1); |
| 420 | writel(val, ®s->data2); |
| 421 | writel(0, ®s->data3); |
| 422 | writel(0, ®s->data0); |
| 423 | break; |
| 424 | case 3: |
| 425 | writel(0, ®s->data1); |
| 426 | writel(0, ®s->data2); |
| 427 | writel(val, ®s->data3); |
| 428 | writel(0, ®s->data0); |
| 429 | break; |
| 430 | } |
| 431 | wait_busy(regs, BV_TIMING_PROG_US); |
| 432 | #else |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 433 | writel(val, ®s->data); |
| 434 | wait_busy(regs, BV_TIMING_STROBE_PROG_US); |
Adrian Alonso | 57b0013 | 2015-08-11 11:19:52 -0500 | [diff] [blame] | 435 | #endif |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 436 | udelay(WRITE_POSTAMBLE_US); |
| 437 | |
Peng Fan | 98c5782 | 2017-02-22 16:21:46 +0800 | [diff] [blame] | 438 | #ifdef CONFIG_MX7ULP |
| 439 | if (readl(®s->out_status) & (BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED)) { |
| 440 | writel((BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED), ®s->out_status_clr); |
| 441 | printf("mxc_ocotp %s(): fuse write is failed\n", __func__); |
| 442 | return -EIO; |
| 443 | } |
| 444 | #endif |
| 445 | |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 446 | return finish_access(regs, __func__); |
| 447 | } |
| 448 | |
| 449 | int fuse_override(u32 bank, u32 word, u32 val) |
| 450 | { |
| 451 | struct ocotp_regs *regs; |
| 452 | int ret; |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 453 | u32 phy_bank; |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 454 | u32 phy_word; |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 455 | |
| 456 | ret = prepare_write(®s, bank, word, __func__); |
| 457 | if (ret) |
| 458 | return ret; |
| 459 | |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 460 | phy_bank = fuse_bank_physical(bank); |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 461 | phy_word = fuse_word_physical(bank, word); |
Peng Fan | 52bae46 | 2015-08-26 15:40:47 +0800 | [diff] [blame] | 462 | |
Peng Fan | cb356f9 | 2016-08-11 14:02:41 +0800 | [diff] [blame] | 463 | writel(val, ®s->bank[phy_bank].fuse_regs[phy_word << 2]); |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 464 | |
Peng Fan | 98c5782 | 2017-02-22 16:21:46 +0800 | [diff] [blame] | 465 | #ifdef CONFIG_MX7ULP |
| 466 | if (readl(®s->out_status) & (BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED)) { |
| 467 | writel((BM_OUT_STATUS_PROGFAIL | BM_OUT_STATUS_LOCKED), ®s->out_status_clr); |
| 468 | printf("mxc_ocotp %s(): fuse write is failed\n", __func__); |
| 469 | return -EIO; |
| 470 | } |
| 471 | #endif |
| 472 | |
Benoît Thébaudeau | 20db631 | 2013-04-23 10:17:44 +0000 | [diff] [blame] | 473 | return finish_access(regs, __func__); |
| 474 | } |