blob: 6f933355176296ac2541d0b45960dfbef87bb27d [file] [log] [blame]
Mike Frysingerd0b01a22008-02-04 19:26:57 -05001/*
2 * cmd_otp.c - interface to Blackfin on-chip One-Time-Programmable memory
3 *
4 * Copyright (c) 2007-2008 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
Mike Frysinger8b35e3a2008-10-11 22:05:42 -04009/* There are 512 128-bit "pages" (0x000 through 0x1FF).
Mike Frysingerd0b01a22008-02-04 19:26:57 -050010 * The pages are accessable as 64-bit "halfpages" (an upper and lower half).
11 * The pages are not part of the memory map. There is an OTP controller which
12 * handles scanning in/out of bits. While access is done through OTP MMRs,
13 * the bootrom provides C-callable helper functions to handle the interaction.
14 */
15
16#include <config.h>
17#include <common.h>
18#include <command.h>
19
Mike Frysingerd0b01a22008-02-04 19:26:57 -050020#include <asm/blackfin.h>
21#include <asm/mach-common/bits/otp.h>
22
23static const char *otp_strerror(uint32_t err)
24{
25 switch (err) {
26 case 0: return "no error";
27 case OTP_WRITE_ERROR: return "OTP fuse write error";
28 case OTP_READ_ERROR: return "OTP fuse read error";
29 case OTP_ACC_VIO_ERROR: return "invalid OTP address";
30 case OTP_DATA_MULT_ERROR: return "multiple bad bits detected";
31 case OTP_ECC_MULT_ERROR: return "error in ECC bits";
32 case OTP_PREV_WR_ERROR: return "space already written";
33 case OTP_DATA_SB_WARN: return "single bad bit in half page";
34 case OTP_ECC_SB_WARN: return "single bad bit in ECC";
35 default: return "unknown error";
36 }
37}
38
39#define lowup(x) ((x) % 2 ? "upper" : "lower")
40
Mike Frysinger8b35e3a2008-10-11 22:05:42 -040041static int check_voltage(void)
Mike Frysingerd0b01a22008-02-04 19:26:57 -050042{
Mike Frysinger8b35e3a2008-10-11 22:05:42 -040043 /* Make sure voltage limits are within datasheet spec */
44 uint16_t vr_ctl = bfin_read_VR_CTL();
45
46#ifdef __ADSPBF54x__
47 /* 0.9V <= VDDINT <= 1.1V */
48 if ((vr_ctl & 0xc) && (vr_ctl & 0xc0) == 0xc0)
49 return 1;
50#else
51 /* for the parts w/out qualification yet */
52 (void)vr_ctl;
53#endif
54
55 return 0;
56}
57
58static void set_otp_timing(bool write)
59{
60 static uint32_t timing;
61 if (!timing) {
62 uint32_t tp1, tp2, tp3;
63 /* OTP_TP1 = 1000 / sclk_period (in nanoseconds)
64 * OTP_TP1 = 1000 / (1 / get_sclk() * 10^9)
65 * OTP_TP1 = (1000 * get_sclk()) / 10^9
66 * OTP_TP1 = get_sclk() / 10^6
67 */
68 tp1 = get_sclk() / 1000000;
69 /* OTP_TP2 = 400 / (2 * sclk_period)
70 * OTP_TP2 = 400 / (2 * 1 / get_sclk() * 10^9)
71 * OTP_TP2 = (400 * get_sclk()) / (2 * 10^9)
72 * OTP_TP2 = (2 * get_sclk()) / 10^7
73 */
74 tp2 = (2 * get_sclk() / 10000000) << 8;
75 /* OTP_TP3 = magic constant */
76 tp3 = (0x1401) << 15;
77 timing = tp1 | tp2 | tp3;
Mike Frysingerd0b01a22008-02-04 19:26:57 -050078 }
79
Mike Frysinger8b35e3a2008-10-11 22:05:42 -040080 bfrom_OtpCommand(OTP_INIT, write ? timing : timing & ~(-1 << 15));
81}
82
Wolfgang Denk54841ab2010-06-28 22:00:46 +020083int do_otp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Mike Frysinger8b35e3a2008-10-11 22:05:42 -040084{
Mike Frysingerfcbd5b72010-10-02 14:31:32 -040085 char *cmd;
Mike Frysinger8b35e3a2008-10-11 22:05:42 -040086 uint32_t ret, base_flags;
87 bool prompt_user, force_read;
Mike Frysingerd0b01a22008-02-04 19:26:57 -050088 uint32_t (*otp_func)(uint32_t page, uint32_t flags, uint64_t *page_content);
Mike Frysinger8b35e3a2008-10-11 22:05:42 -040089
90 if (argc < 4) {
Mike Frysingerd0b01a22008-02-04 19:26:57 -050091 usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +000092 return CMD_RET_USAGE;
Mike Frysingerd0b01a22008-02-04 19:26:57 -050093 }
94
Mike Frysinger8b35e3a2008-10-11 22:05:42 -040095 prompt_user = false;
96 base_flags = 0;
Mike Frysingerfcbd5b72010-10-02 14:31:32 -040097 cmd = argv[1];
98 if (!strcmp(cmd, "read"))
Mike Frysinger8b35e3a2008-10-11 22:05:42 -040099 otp_func = bfrom_OtpRead;
Mike Frysingerfcbd5b72010-10-02 14:31:32 -0400100 else if (!strcmp(cmd, "dump")) {
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400101 otp_func = bfrom_OtpRead;
102 force_read = true;
Mike Frysingerfcbd5b72010-10-02 14:31:32 -0400103 } else if (!strcmp(cmd, "write")) {
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400104 otp_func = bfrom_OtpWrite;
105 base_flags = OTP_CHECK_FOR_PREV_WRITE;
106 if (!strcmp(argv[2], "--force")) {
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400107 argv++;
108 --argc;
109 } else
110 prompt_user = false;
Mike Frysingerfcbd5b72010-10-02 14:31:32 -0400111 } else if (!strcmp(cmd, "lock")) {
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400112 if (argc != 4)
113 goto usage;
114 otp_func = bfrom_OtpWrite;
115 base_flags = OTP_LOCK;
116 } else
117 goto usage;
118
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500119 uint64_t *addr = (uint64_t *)simple_strtoul(argv[2], NULL, 16);
120 uint32_t page = simple_strtoul(argv[3], NULL, 16);
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400121 uint32_t flags;
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500122 size_t i, count;
123 ulong half;
124
125 if (argc > 4)
126 count = simple_strtoul(argv[4], NULL, 16);
127 else
128 count = 2;
129
130 if (argc > 5) {
131 half = simple_strtoul(argv[5], NULL, 16);
132 if (half != 0 && half != 1) {
133 puts("Error: 'half' can only be '0' or '1'\n");
134 goto usage;
135 }
136 } else
137 half = 0;
138
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400139 /* "otp lock" has slightly different semantics */
140 if (base_flags & OTP_LOCK) {
141 count = page;
142 page = (uint32_t)addr;
143 addr = NULL;
144 }
145
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500146 /* do to the nature of OTP, make sure users are sure */
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400147 if (prompt_user) {
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500148 printf(
149 "Writing one time programmable memory\n"
150 "Make sure your operating voltages and temperature are within spec\n"
151 " source address: 0x%p\n"
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400152 " OTP destination: %s page 0x%03X - %s page 0x%03lX\n"
153 " number to write: %lu halfpages\n"
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500154 " type \"YES\" (no quotes) to confirm: ",
155 addr,
156 lowup(half), page,
157 lowup(half + count - 1), page + (half + count - 1) / 2,
158 half + count
159 );
160
161 i = 0;
162 while (1) {
163 if (tstc()) {
164 const char exp_ans[] = "YES\r";
165 char c;
166 putc(c = getc());
167 if (exp_ans[i++] != c) {
168 printf(" Aborting\n");
169 return 1;
170 } else if (!exp_ans[i]) {
171 puts("\n");
172 break;
173 }
174 }
175 }
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500176 }
177
Mike Frysingerfe033ad2008-10-12 06:02:55 -0400178 printf("OTP memory %s: addr 0x%p page 0x%03X count %zu ... ",
Mike Frysingerfcbd5b72010-10-02 14:31:32 -0400179 cmd, addr, page, count);
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500180
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400181 set_otp_timing(otp_func == bfrom_OtpWrite);
182 if (otp_func == bfrom_OtpWrite && check_voltage()) {
183 puts("ERROR: VDDINT voltage is out of spec for writing\n");
184 return -1;
185 }
186
187 /* Do the actual reading/writing stuff */
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500188 ret = 0;
189 for (i = half; i < count + half; ++i) {
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400190 flags = base_flags | (i % 2 ? OTP_UPPER_HALF : OTP_LOWER_HALF);
191 try_again:
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500192 ret = otp_func(page, flags, addr);
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400193 if (ret & OTP_MASTER_ERROR) {
194 if (force_read) {
195 if (flags & OTP_NO_ECC)
196 break;
197 else
198 flags |= OTP_NO_ECC;
199 puts("E");
200 goto try_again;
201 } else
202 break;
203 } else if (ret)
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500204 puts("W");
205 else
206 puts(".");
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400207 if (!(base_flags & OTP_LOCK)) {
208 ++addr;
209 if (i % 2)
210 ++page;
211 } else
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500212 ++page;
213 }
214 if (ret & 0x1)
215 printf("\nERROR at page 0x%03X (%s-halfpage): 0x%03X: %s\n",
216 page, lowup(i), ret, otp_strerror(ret));
217 else
218 puts(" done\n");
219
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400220 /* Make sure we disable writing */
221 set_otp_timing(false);
222 bfrom_OtpCommand(OTP_CLOSE, 0);
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500223
224 return ret;
225}
226
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200227U_BOOT_CMD(
228 otp, 7, 0, do_otp,
Mike Frysingerdbc6ab92009-03-23 22:17:27 -0400229 "One-Time-Programmable sub-system",
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500230 "read <addr> <page> [count] [half]\n"
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400231 " - read 'count' half-pages starting at 'page' (offset 'half') to 'addr'\n"
232 "otp dump <addr> <page> [count] [half]\n"
233 " - like 'otp read', but skip read errors\n"
Mike Frysingerd0b01a22008-02-04 19:26:57 -0500234 "otp write [--force] <addr> <page> [count] [half]\n"
Mike Frysinger8b35e3a2008-10-11 22:05:42 -0400235 " - write 'count' half-pages starting at 'page' (offset 'half') from 'addr'\n"
236 "otp lock <page> <count>\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200237 " - lock 'count' pages starting at 'page'"
238);