]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_otp.c
Merge branch 'master' of git://git.denx.de/u-boot-samsung
[people/ms/u-boot.git] / common / cmd_otp.c
CommitLineData
d0b01a24
MF
1/*
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
8b35e3ae 9/* There are 512 128-bit "pages" (0x000 through 0x1FF).
d0b01a24
MF
10 * 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
d0b01a24
MF
20#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
8b35e3ae
MF
41static int check_voltage(void)
42{
43 /* 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)
d0b01a24 59{
8b35e3ae
MF
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;
d0b01a24
MF
78 }
79
8b35e3ae
MF
80 bfrom_OtpCommand(OTP_INIT, write ? timing : timing & ~(-1 << 15));
81}
82
54841ab5 83int do_otp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
8b35e3ae
MF
84{
85 uint32_t ret, base_flags;
86 bool prompt_user, force_read;
d0b01a24 87 uint32_t (*otp_func)(uint32_t page, uint32_t flags, uint64_t *page_content);
8b35e3ae
MF
88
89 if (argc < 4) {
d0b01a24 90 usage:
47e26b1b 91 return cmd_usage(cmdtp);
d0b01a24
MF
92 }
93
8b35e3ae
MF
94 prompt_user = false;
95 base_flags = 0;
96 if (!strcmp(argv[1], "read"))
97 otp_func = bfrom_OtpRead;
98 else if (!strcmp(argv[1], "dump")) {
99 otp_func = bfrom_OtpRead;
100 force_read = true;
101 } else if (!strcmp(argv[1], "write")) {
102 otp_func = bfrom_OtpWrite;
103 base_flags = OTP_CHECK_FOR_PREV_WRITE;
104 if (!strcmp(argv[2], "--force")) {
105 argv[2] = argv[1];
106 argv++;
107 --argc;
108 } else
109 prompt_user = false;
110 } else if (!strcmp(argv[1], "lock")) {
111 if (argc != 4)
112 goto usage;
113 otp_func = bfrom_OtpWrite;
114 base_flags = OTP_LOCK;
115 } else
116 goto usage;
117
d0b01a24
MF
118 uint64_t *addr = (uint64_t *)simple_strtoul(argv[2], NULL, 16);
119 uint32_t page = simple_strtoul(argv[3], NULL, 16);
8b35e3ae 120 uint32_t flags;
d0b01a24
MF
121 size_t i, count;
122 ulong half;
123
124 if (argc > 4)
125 count = simple_strtoul(argv[4], NULL, 16);
126 else
127 count = 2;
128
129 if (argc > 5) {
130 half = simple_strtoul(argv[5], NULL, 16);
131 if (half != 0 && half != 1) {
132 puts("Error: 'half' can only be '0' or '1'\n");
133 goto usage;
134 }
135 } else
136 half = 0;
137
8b35e3ae
MF
138 /* "otp lock" has slightly different semantics */
139 if (base_flags & OTP_LOCK) {
140 count = page;
141 page = (uint32_t)addr;
142 addr = NULL;
143 }
144
d0b01a24 145 /* do to the nature of OTP, make sure users are sure */
8b35e3ae 146 if (prompt_user) {
d0b01a24
MF
147 printf(
148 "Writing one time programmable memory\n"
149 "Make sure your operating voltages and temperature are within spec\n"
150 " source address: 0x%p\n"
fe033ad6
MF
151 " OTP destination: %s page 0x%03X - %s page 0x%03lX\n"
152 " number to write: %lu halfpages\n"
d0b01a24
MF
153 " type \"YES\" (no quotes) to confirm: ",
154 addr,
155 lowup(half), page,
156 lowup(half + count - 1), page + (half + count - 1) / 2,
157 half + count
158 );
159
160 i = 0;
161 while (1) {
162 if (tstc()) {
163 const char exp_ans[] = "YES\r";
164 char c;
165 putc(c = getc());
166 if (exp_ans[i++] != c) {
167 printf(" Aborting\n");
168 return 1;
169 } else if (!exp_ans[i]) {
170 puts("\n");
171 break;
172 }
173 }
174 }
d0b01a24
MF
175 }
176
fe033ad6 177 printf("OTP memory %s: addr 0x%p page 0x%03X count %zu ... ",
d0b01a24
MF
178 argv[1], addr, page, count);
179
8b35e3ae
MF
180 set_otp_timing(otp_func == bfrom_OtpWrite);
181 if (otp_func == bfrom_OtpWrite && check_voltage()) {
182 puts("ERROR: VDDINT voltage is out of spec for writing\n");
183 return -1;
184 }
185
186 /* Do the actual reading/writing stuff */
d0b01a24
MF
187 ret = 0;
188 for (i = half; i < count + half; ++i) {
8b35e3ae
MF
189 flags = base_flags | (i % 2 ? OTP_UPPER_HALF : OTP_LOWER_HALF);
190 try_again:
d0b01a24 191 ret = otp_func(page, flags, addr);
8b35e3ae
MF
192 if (ret & OTP_MASTER_ERROR) {
193 if (force_read) {
194 if (flags & OTP_NO_ECC)
195 break;
196 else
197 flags |= OTP_NO_ECC;
198 puts("E");
199 goto try_again;
200 } else
201 break;
202 } else if (ret)
d0b01a24
MF
203 puts("W");
204 else
205 puts(".");
8b35e3ae
MF
206 if (!(base_flags & OTP_LOCK)) {
207 ++addr;
208 if (i % 2)
209 ++page;
210 } else
d0b01a24
MF
211 ++page;
212 }
213 if (ret & 0x1)
214 printf("\nERROR at page 0x%03X (%s-halfpage): 0x%03X: %s\n",
215 page, lowup(i), ret, otp_strerror(ret));
216 else
217 puts(" done\n");
218
8b35e3ae
MF
219 /* Make sure we disable writing */
220 set_otp_timing(false);
221 bfrom_OtpCommand(OTP_CLOSE, 0);
d0b01a24
MF
222
223 return ret;
224}
225
8b35e3ae 226U_BOOT_CMD(otp, 7, 0, do_otp,
dbc6ab9f 227 "One-Time-Programmable sub-system",
d0b01a24 228 "read <addr> <page> [count] [half]\n"
8b35e3ae
MF
229 " - read 'count' half-pages starting at 'page' (offset 'half') to 'addr'\n"
230 "otp dump <addr> <page> [count] [half]\n"
231 " - like 'otp read', but skip read errors\n"
d0b01a24 232 "otp write [--force] <addr> <page> [count] [half]\n"
8b35e3ae
MF
233 " - write 'count' half-pages starting at 'page' (offset 'half') from 'addr'\n"
234 "otp lock <page> <count>\n"
a89c33db
WD
235 " - lock 'count' pages starting at 'page'"
236);