2 * (C) Copyright 2009-2013 ADVANSEE
3 * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
5 * Based on the mpc512x iim code:
6 * Copyright 2008 Silicon Turnkey Express, Inc.
7 * Martha Marx <mmarx@silicontkx.com>
9 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/errno.h>
16 #ifndef CONFIG_MPC512X
17 #include <asm/arch/imx-regs.h>
19 #if defined(CONFIG_MX51) || defined(CONFIG_MX53)
20 #include <asm/arch/clock.h>
23 /* FSL IIM-specific constants */
24 #define STAT_BUSY 0x80
25 #define STAT_PRGD 0x02
26 #define STAT_SNSD 0x01
28 #define STATM_PRGD_M 0x02
29 #define STATM_SNSD_M 0x01
37 #define ERR_PARITYE 0x02
39 #define EMASK_PRGE_M 0x80
40 #define EMASK_WPE_M 0x40
41 #define EMASK_OPE_M 0x20
42 #define EMASK_RPE_M 0x10
43 #define EMASK_WLRE_M 0x08
44 #define EMASK_SNSE_M 0x04
45 #define EMASK_PARITYE_M 0x02
48 #define FCTL_PRG_LENGTH_MASK 0x70
49 #define FCTL_ESNS_N 0x08
50 #define FCTL_ESNS_0 0x04
51 #define FCTL_ESNS_1 0x02
54 #define UA_A_BANK_MASK 0x38
55 #define UA_A_ROWH_MASK 0x07
57 #define LA_A_ROWL_MASK 0xf8
58 #define LA_A_BIT_MASK 0x07
60 #define PREV_PROD_REV_MASK 0xf8
61 #define PREV_PROD_VT_MASK 0x07
63 /* Select the correct accessors depending on endianness */
64 #if __BYTE_ORDER == __LITTLE_ENDIAN
65 #define iim_read32 in_le32
66 #define iim_write32 out_le32
67 #define iim_clrsetbits32 clrsetbits_le32
68 #define iim_clrbits32 clrbits_le32
69 #define iim_setbits32 setbits_le32
70 #elif __BYTE_ORDER == __BIG_ENDIAN
71 #define iim_read32 in_be32
72 #define iim_write32 out_be32
73 #define iim_clrsetbits32 clrsetbits_be32
74 #define iim_clrbits32 clrbits_be32
75 #define iim_setbits32 setbits_be32
77 #error Endianess is not defined: please fix to continue
80 /* IIM control registers */
99 #if !defined(CONFIG_MX51) && !defined(CONFIG_MX53)
100 #define enable_efuse_prog_supply(enable)
103 static int prepare_access(struct fsl_iim **regs, u32 bank, u32 word, int assert,
106 *regs = (struct fsl_iim *)IIM_BASE_ADDR;
108 if (bank >= ARRAY_SIZE((*regs)->bank) ||
109 word >= ARRAY_SIZE((*regs)->bank[0].word) ||
111 printf("fsl_iim %s(): Invalid argument\n", caller);
118 static void clear_status(struct fsl_iim *regs)
120 iim_setbits32(®s->stat, 0);
121 iim_setbits32(®s->err, 0);
124 static void finish_access(struct fsl_iim *regs, u32 *stat, u32 *err)
126 *stat = iim_read32(®s->stat);
127 *err = iim_read32(®s->err);
131 static int prepare_read(struct fsl_iim **regs, u32 bank, u32 word, u32 *val,
136 ret = prepare_access(regs, bank, word, val != NULL, caller);
145 int fuse_read(u32 bank, u32 word, u32 *val)
147 struct fsl_iim *regs;
151 ret = prepare_read(®s, bank, word, val, __func__);
155 *val = iim_read32(®s->bank[bank].word[word]);
156 finish_access(regs, &stat, &err);
159 puts("fsl_iim fuse_read(): Read protect error\n");
166 static void direct_access(struct fsl_iim *regs, u32 bank, u32 word, u32 bit,
167 u32 fctl, u32 *stat, u32 *err)
169 iim_write32(®s->ua, bank << 3 | word >> 5);
170 iim_write32(®s->la, (word << 3 | bit) & 0xff);
171 if (fctl == FCTL_PRG)
172 iim_write32(®s->prg_p, 0xaa);
173 iim_setbits32(®s->fctl, fctl);
174 while (iim_read32(®s->stat) & STAT_BUSY)
176 finish_access(regs, stat, err);
179 int fuse_sense(u32 bank, u32 word, u32 *val)
181 struct fsl_iim *regs;
185 ret = prepare_read(®s, bank, word, val, __func__);
189 direct_access(regs, bank, word, 0, FCTL_ESNS_N, &stat, &err);
191 if (err & ERR_SNSE) {
192 puts("fsl_iim fuse_sense(): Explicit sense cycle error\n");
196 if (!(stat & STAT_SNSD)) {
197 puts("fsl_iim fuse_sense(): Explicit sense cycle did not complete\n");
201 *val = iim_read32(®s->sdat);
205 static int prog_bit(struct fsl_iim *regs, u32 bank, u32 word, u32 bit)
210 direct_access(regs, bank, word, bit, FCTL_PRG, &stat, &err);
211 iim_write32(®s->prg_p, 0x00);
213 if (err & ERR_PRGE) {
214 puts("fsl_iim fuse_prog(): Program error\n");
219 puts("fsl_iim fuse_prog(): Write protect error\n");
223 if (!(stat & STAT_PRGD)) {
224 puts("fsl_iim fuse_prog(): Program did not complete\n");
231 static int prepare_write(struct fsl_iim **regs, u32 bank, u32 word, u32 val,
234 return prepare_access(regs, bank, word, !(val & ~0xff), caller);
237 int fuse_prog(u32 bank, u32 word, u32 val)
239 struct fsl_iim *regs;
243 ret = prepare_write(®s, bank, word, val, __func__);
247 enable_efuse_prog_supply(1);
248 for (bit = 0; val; bit++, val >>= 1)
250 ret = prog_bit(regs, bank, word, bit);
252 enable_efuse_prog_supply(0);
256 enable_efuse_prog_supply(0);
261 int fuse_override(u32 bank, u32 word, u32 val)
263 struct fsl_iim *regs;
267 ret = prepare_write(®s, bank, word, val, __func__);
272 iim_write32(®s->bank[bank].word[word], val);
273 finish_access(regs, &stat, &err);
276 puts("fsl_iim fuse_override(): Override protect error\n");