1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2009-2013 ADVANSEE
4 * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
6 * Based on the mpc512x iim code:
7 * Copyright 2008 Silicon Turnkey Express, Inc.
8 * Martha Marx <mmarx@silicontkx.com>
13 #include <linux/errno.h>
15 #include <asm/arch/imx-regs.h>
16 #if defined(CONFIG_MX51) || defined(CONFIG_MX53)
17 #include <asm/arch/clock.h>
20 /* FSL IIM-specific constants */
21 #define STAT_BUSY 0x80
22 #define STAT_PRGD 0x02
23 #define STAT_SNSD 0x01
25 #define STATM_PRGD_M 0x02
26 #define STATM_SNSD_M 0x01
34 #define ERR_PARITYE 0x02
36 #define EMASK_PRGE_M 0x80
37 #define EMASK_WPE_M 0x40
38 #define EMASK_OPE_M 0x20
39 #define EMASK_RPE_M 0x10
40 #define EMASK_WLRE_M 0x08
41 #define EMASK_SNSE_M 0x04
42 #define EMASK_PARITYE_M 0x02
45 #define FCTL_PRG_LENGTH_MASK 0x70
46 #define FCTL_ESNS_N 0x08
47 #define FCTL_ESNS_0 0x04
48 #define FCTL_ESNS_1 0x02
51 #define UA_A_BANK_MASK 0x38
52 #define UA_A_ROWH_MASK 0x07
54 #define LA_A_ROWL_MASK 0xf8
55 #define LA_A_BIT_MASK 0x07
57 #define PREV_PROD_REV_MASK 0xf8
58 #define PREV_PROD_VT_MASK 0x07
60 /* Select the correct accessors depending on endianness */
61 #if __BYTE_ORDER == __LITTLE_ENDIAN
62 #define iim_read32 in_le32
63 #define iim_write32 out_le32
64 #define iim_clrsetbits32 clrsetbits_le32
65 #define iim_clrbits32 clrbits_le32
66 #define iim_setbits32 setbits_le32
67 #elif __BYTE_ORDER == __BIG_ENDIAN
68 #define iim_read32 in_be32
69 #define iim_write32 out_be32
70 #define iim_clrsetbits32 clrsetbits_be32
71 #define iim_clrbits32 clrbits_be32
72 #define iim_setbits32 setbits_be32
74 #error Endianess is not defined: please fix to continue
77 /* IIM control registers */
96 #if !defined(CONFIG_MX51) && !defined(CONFIG_MX53)
97 #define enable_efuse_prog_supply(enable)
100 static int prepare_access(struct fsl_iim **regs, u32 bank, u32 word, int assert,
103 *regs = (struct fsl_iim *)IIM_BASE_ADDR;
105 if (bank >= ARRAY_SIZE((*regs)->bank) ||
106 word >= ARRAY_SIZE((*regs)->bank[0].word) ||
108 printf("fsl_iim %s(): Invalid argument\n", caller);
115 static void clear_status(struct fsl_iim *regs)
117 iim_setbits32(®s->stat, 0);
118 iim_setbits32(®s->err, 0);
121 static void finish_access(struct fsl_iim *regs, u32 *stat, u32 *err)
123 *stat = iim_read32(®s->stat);
124 *err = iim_read32(®s->err);
128 static int prepare_read(struct fsl_iim **regs, u32 bank, u32 word, u32 *val,
133 ret = prepare_access(regs, bank, word, val != NULL, caller);
142 int fuse_read(u32 bank, u32 word, u32 *val)
144 struct fsl_iim *regs;
148 ret = prepare_read(®s, bank, word, val, __func__);
152 *val = iim_read32(®s->bank[bank].word[word]);
153 finish_access(regs, &stat, &err);
156 puts("fsl_iim fuse_read(): Read protect error\n");
163 static void direct_access(struct fsl_iim *regs, u32 bank, u32 word, u32 bit,
164 u32 fctl, u32 *stat, u32 *err)
166 iim_write32(®s->ua, bank << 3 | word >> 5);
167 iim_write32(®s->la, (word << 3 | bit) & 0xff);
168 if (fctl == FCTL_PRG)
169 iim_write32(®s->prg_p, 0xaa);
170 iim_setbits32(®s->fctl, fctl);
171 while (iim_read32(®s->stat) & STAT_BUSY)
173 finish_access(regs, stat, err);
176 int fuse_sense(u32 bank, u32 word, u32 *val)
178 struct fsl_iim *regs;
182 ret = prepare_read(®s, bank, word, val, __func__);
186 direct_access(regs, bank, word, 0, FCTL_ESNS_N, &stat, &err);
188 if (err & ERR_SNSE) {
189 puts("fsl_iim fuse_sense(): Explicit sense cycle error\n");
193 if (!(stat & STAT_SNSD)) {
194 puts("fsl_iim fuse_sense(): Explicit sense cycle did not complete\n");
198 *val = iim_read32(®s->sdat);
202 static int prog_bit(struct fsl_iim *regs, u32 bank, u32 word, u32 bit)
207 direct_access(regs, bank, word, bit, FCTL_PRG, &stat, &err);
208 iim_write32(®s->prg_p, 0x00);
210 if (err & ERR_PRGE) {
211 puts("fsl_iim fuse_prog(): Program error\n");
216 puts("fsl_iim fuse_prog(): Write protect error\n");
220 if (!(stat & STAT_PRGD)) {
221 puts("fsl_iim fuse_prog(): Program did not complete\n");
228 static int prepare_write(struct fsl_iim **regs, u32 bank, u32 word, u32 val,
231 return prepare_access(regs, bank, word, !(val & ~0xff), caller);
234 int fuse_prog(u32 bank, u32 word, u32 val)
236 struct fsl_iim *regs;
240 ret = prepare_write(®s, bank, word, val, __func__);
244 enable_efuse_prog_supply(1);
245 for (bit = 0; val; bit++, val >>= 1)
247 ret = prog_bit(regs, bank, word, bit);
249 enable_efuse_prog_supply(0);
253 enable_efuse_prog_supply(0);
258 int fuse_override(u32 bank, u32 word, u32 val)
260 struct fsl_iim *regs;
264 ret = prepare_write(®s, bank, word, val, __func__);
269 iim_write32(®s->bank[bank].word[word], val);
270 finish_access(regs, &stat, &err);
273 puts("fsl_iim fuse_override(): Override protect error\n");