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