Blame view
common/cmd_otp.c
5.78 KB
d0b01a246 interface to Blac... |
1 2 3 4 5 6 7 |
/* * cmd_otp.c - interface to Blackfin on-chip One-Time-Programmable memory * * Copyright (c) 2007-2008 Analog Devices Inc. * * Licensed under the GPL-2 or later. */ |
8b35e3aef Blackfin: impleme... |
8 |
/* There are 512 128-bit "pages" (0x000 through 0x1FF). |
d0b01a246 interface to Blac... |
9 10 11 12 13 14 15 16 17 |
* The pages are accessable as 64-bit "halfpages" (an upper and lower half). * The pages are not part of the memory map. There is an OTP controller which * handles scanning in/out of bits. While access is done through OTP MMRs, * the bootrom provides C-callable helper functions to handle the interaction. */ #include <config.h> #include <common.h> #include <command.h> |
d0b01a246 interface to Blac... |
18 |
#include <asm/blackfin.h> |
130fbeb1c blackfin: Add <as... |
19 |
#include <asm/clock.h> |
d0b01a246 interface to Blac... |
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <asm/mach-common/bits/otp.h> static const char *otp_strerror(uint32_t err) { switch (err) { case 0: return "no error"; case OTP_WRITE_ERROR: return "OTP fuse write error"; case OTP_READ_ERROR: return "OTP fuse read error"; case OTP_ACC_VIO_ERROR: return "invalid OTP address"; case OTP_DATA_MULT_ERROR: return "multiple bad bits detected"; case OTP_ECC_MULT_ERROR: return "error in ECC bits"; case OTP_PREV_WR_ERROR: return "space already written"; case OTP_DATA_SB_WARN: return "single bad bit in half page"; case OTP_ECC_SB_WARN: return "single bad bit in ECC"; default: return "unknown error"; } } #define lowup(x) ((x) % 2 ? "upper" : "lower") |
8b35e3aef Blackfin: impleme... |
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
static int check_voltage(void) { /* Make sure voltage limits are within datasheet spec */ uint16_t vr_ctl = bfin_read_VR_CTL(); #ifdef __ADSPBF54x__ /* 0.9V <= VDDINT <= 1.1V */ if ((vr_ctl & 0xc) && (vr_ctl & 0xc0) == 0xc0) return 1; #else /* for the parts w/out qualification yet */ (void)vr_ctl; #endif return 0; } static void set_otp_timing(bool write) |
d0b01a246 interface to Blac... |
57 |
{ |
8b35e3aef Blackfin: impleme... |
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
static uint32_t timing; if (!timing) { uint32_t tp1, tp2, tp3; /* OTP_TP1 = 1000 / sclk_period (in nanoseconds) * OTP_TP1 = 1000 / (1 / get_sclk() * 10^9) * OTP_TP1 = (1000 * get_sclk()) / 10^9 * OTP_TP1 = get_sclk() / 10^6 */ tp1 = get_sclk() / 1000000; /* OTP_TP2 = 400 / (2 * sclk_period) * OTP_TP2 = 400 / (2 * 1 / get_sclk() * 10^9) * OTP_TP2 = (400 * get_sclk()) / (2 * 10^9) * OTP_TP2 = (2 * get_sclk()) / 10^7 */ tp2 = (2 * get_sclk() / 10000000) << 8; /* OTP_TP3 = magic constant */ tp3 = (0x1401) << 15; timing = tp1 | tp2 | tp3; |
d0b01a246 interface to Blac... |
76 |
} |
8b35e3aef Blackfin: impleme... |
77 78 |
bfrom_OtpCommand(OTP_INIT, write ? timing : timing & ~(-1 << 15)); } |
54841ab50 Make sure that ar... |
79 |
int do_otp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
8b35e3aef Blackfin: impleme... |
80 |
{ |
fcbd5b73d Blackfin: otp: fi... |
81 |
char *cmd; |
8b35e3aef Blackfin: impleme... |
82 83 |
uint32_t ret, base_flags; bool prompt_user, force_read; |
d0b01a246 interface to Blac... |
84 |
uint32_t (*otp_func)(uint32_t page, uint32_t flags, uint64_t *page_content); |
8b35e3aef Blackfin: impleme... |
85 86 |
if (argc < 4) { |
d0b01a246 interface to Blac... |
87 |
usage: |
4c12eeb8b Convert cmd_usage... |
88 |
return CMD_RET_USAGE; |
d0b01a246 interface to Blac... |
89 |
} |
8b35e3aef Blackfin: impleme... |
90 91 |
prompt_user = false; base_flags = 0; |
fcbd5b73d Blackfin: otp: fi... |
92 93 |
cmd = argv[1]; if (!strcmp(cmd, "read")) |
8b35e3aef Blackfin: impleme... |
94 |
otp_func = bfrom_OtpRead; |
fcbd5b73d Blackfin: otp: fi... |
95 |
else if (!strcmp(cmd, "dump")) { |
8b35e3aef Blackfin: impleme... |
96 97 |
otp_func = bfrom_OtpRead; force_read = true; |
fcbd5b73d Blackfin: otp: fi... |
98 |
} else if (!strcmp(cmd, "write")) { |
8b35e3aef Blackfin: impleme... |
99 100 101 |
otp_func = bfrom_OtpWrite; base_flags = OTP_CHECK_FOR_PREV_WRITE; if (!strcmp(argv[2], "--force")) { |
8b35e3aef Blackfin: impleme... |
102 103 104 105 |
argv++; --argc; } else prompt_user = false; |
fcbd5b73d Blackfin: otp: fi... |
106 |
} else if (!strcmp(cmd, "lock")) { |
8b35e3aef Blackfin: impleme... |
107 108 109 110 111 112 |
if (argc != 4) goto usage; otp_func = bfrom_OtpWrite; base_flags = OTP_LOCK; } else goto usage; |
d0b01a246 interface to Blac... |
113 114 |
uint64_t *addr = (uint64_t *)simple_strtoul(argv[2], NULL, 16); uint32_t page = simple_strtoul(argv[3], NULL, 16); |
8b35e3aef Blackfin: impleme... |
115 |
uint32_t flags; |
d0b01a246 interface to Blac... |
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
size_t i, count; ulong half; if (argc > 4) count = simple_strtoul(argv[4], NULL, 16); else count = 2; if (argc > 5) { half = simple_strtoul(argv[5], NULL, 16); if (half != 0 && half != 1) { puts("Error: 'half' can only be '0' or '1' "); goto usage; } } else half = 0; |
8b35e3aef Blackfin: impleme... |
133 134 135 136 137 138 |
/* "otp lock" has slightly different semantics */ if (base_flags & OTP_LOCK) { count = page; page = (uint32_t)addr; addr = NULL; } |
d0b01a246 interface to Blac... |
139 |
/* do to the nature of OTP, make sure users are sure */ |
8b35e3aef Blackfin: impleme... |
140 |
if (prompt_user) { |
d0b01a246 interface to Blac... |
141 142 143 144 145 146 147 |
printf( "Writing one time programmable memory " "Make sure your operating voltages and temperature are within spec " " source address: 0x%p " |
fe033ad6d Blackfin: fixup m... |
148 149 150 151 |
" OTP destination: %s page 0x%03X - %s page 0x%03lX " " number to write: %lu halfpages " |
d0b01a246 interface to Blac... |
152 153 154 155 156 157 |
" type \"YES\" (no quotes) to confirm: ", addr, lowup(half), page, lowup(half + count - 1), page + (half + count - 1) / 2, half + count ); |
a5dffa4b6 Add the function ... |
158 159 160 161 |
if (!confirm_yesno()) { printf(" Aborting "); return 1; |
d0b01a246 interface to Blac... |
162 |
} |
d0b01a246 interface to Blac... |
163 |
} |
fe033ad6d Blackfin: fixup m... |
164 |
printf("OTP memory %s: addr 0x%p page 0x%03X count %zu ... ", |
fcbd5b73d Blackfin: otp: fi... |
165 |
cmd, addr, page, count); |
d0b01a246 interface to Blac... |
166 |
|
8b35e3aef Blackfin: impleme... |
167 168 169 170 171 172 173 174 |
set_otp_timing(otp_func == bfrom_OtpWrite); if (otp_func == bfrom_OtpWrite && check_voltage()) { puts("ERROR: VDDINT voltage is out of spec for writing "); return -1; } /* Do the actual reading/writing stuff */ |
d0b01a246 interface to Blac... |
175 176 |
ret = 0; for (i = half; i < count + half; ++i) { |
8b35e3aef Blackfin: impleme... |
177 178 |
flags = base_flags | (i % 2 ? OTP_UPPER_HALF : OTP_LOWER_HALF); try_again: |
d0b01a246 interface to Blac... |
179 |
ret = otp_func(page, flags, addr); |
8b35e3aef Blackfin: impleme... |
180 181 182 183 184 185 186 187 188 189 190 |
if (ret & OTP_MASTER_ERROR) { if (force_read) { if (flags & OTP_NO_ECC) break; else flags |= OTP_NO_ECC; puts("E"); goto try_again; } else break; } else if (ret) |
d0b01a246 interface to Blac... |
191 192 193 |
puts("W"); else puts("."); |
8b35e3aef Blackfin: impleme... |
194 195 196 197 198 |
if (!(base_flags & OTP_LOCK)) { ++addr; if (i % 2) ++page; } else |
d0b01a246 interface to Blac... |
199 200 201 202 203 204 205 206 207 208 |
++page; } if (ret & 0x1) printf(" ERROR at page 0x%03X (%s-halfpage): 0x%03X: %s ", page, lowup(i), ret, otp_strerror(ret)); else puts(" done "); |
8b35e3aef Blackfin: impleme... |
209 210 211 |
/* Make sure we disable writing */ set_otp_timing(false); bfrom_OtpCommand(OTP_CLOSE, 0); |
d0b01a246 interface to Blac... |
212 213 214 |
return ret; } |
388a29d02 various cmd_* fil... |
215 216 |
U_BOOT_CMD( otp, 7, 0, do_otp, |
dbc6ab9f7 Blackfin: drop ne... |
217 |
"One-Time-Programmable sub-system", |
d0b01a246 interface to Blac... |
218 219 |
"read <addr> <page> [count] [half] " |
8b35e3aef Blackfin: impleme... |
220 221 222 223 224 225 |
" - read 'count' half-pages starting at 'page' (offset 'half') to 'addr' " "otp dump <addr> <page> [count] [half] " " - like 'otp read', but skip read errors " |
d0b01a246 interface to Blac... |
226 227 |
"otp write [--force] <addr> <page> [count] [half] " |
8b35e3aef Blackfin: impleme... |
228 229 230 231 |
" - write 'count' half-pages starting at 'page' (offset 'half') from 'addr' " "otp lock <page> <count> " |
a89c33db9 General help mess... |
232 233 |
" - lock 'count' pages starting at 'page'" ); |