Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 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 | |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 9 | /* There are 512 128-bit "pages" (0x000 through 0x1FF). |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 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 | |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 20 | #include <asm/blackfin.h> |
Tom Rini | c68e851 | 2014-02-20 10:14:10 -0500 | [diff] [blame] | 21 | #include <asm/clock.h> |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 22 | #include <asm/mach-common/bits/otp.h> |
| 23 | |
| 24 | static const char *otp_strerror(uint32_t err) |
| 25 | { |
| 26 | switch (err) { |
| 27 | case 0: return "no error"; |
| 28 | case OTP_WRITE_ERROR: return "OTP fuse write error"; |
| 29 | case OTP_READ_ERROR: return "OTP fuse read error"; |
| 30 | case OTP_ACC_VIO_ERROR: return "invalid OTP address"; |
| 31 | case OTP_DATA_MULT_ERROR: return "multiple bad bits detected"; |
| 32 | case OTP_ECC_MULT_ERROR: return "error in ECC bits"; |
| 33 | case OTP_PREV_WR_ERROR: return "space already written"; |
| 34 | case OTP_DATA_SB_WARN: return "single bad bit in half page"; |
| 35 | case OTP_ECC_SB_WARN: return "single bad bit in ECC"; |
| 36 | default: return "unknown error"; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #define lowup(x) ((x) % 2 ? "upper" : "lower") |
| 41 | |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 42 | static int check_voltage(void) |
| 43 | { |
| 44 | /* Make sure voltage limits are within datasheet spec */ |
| 45 | uint16_t vr_ctl = bfin_read_VR_CTL(); |
| 46 | |
| 47 | #ifdef __ADSPBF54x__ |
| 48 | /* 0.9V <= VDDINT <= 1.1V */ |
| 49 | if ((vr_ctl & 0xc) && (vr_ctl & 0xc0) == 0xc0) |
| 50 | return 1; |
| 51 | #else |
| 52 | /* for the parts w/out qualification yet */ |
| 53 | (void)vr_ctl; |
| 54 | #endif |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static void set_otp_timing(bool write) |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 60 | { |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 61 | static uint32_t timing; |
| 62 | if (!timing) { |
| 63 | uint32_t tp1, tp2, tp3; |
| 64 | /* OTP_TP1 = 1000 / sclk_period (in nanoseconds) |
| 65 | * OTP_TP1 = 1000 / (1 / get_sclk() * 10^9) |
| 66 | * OTP_TP1 = (1000 * get_sclk()) / 10^9 |
| 67 | * OTP_TP1 = get_sclk() / 10^6 |
| 68 | */ |
| 69 | tp1 = get_sclk() / 1000000; |
| 70 | /* OTP_TP2 = 400 / (2 * sclk_period) |
| 71 | * OTP_TP2 = 400 / (2 * 1 / get_sclk() * 10^9) |
| 72 | * OTP_TP2 = (400 * get_sclk()) / (2 * 10^9) |
| 73 | * OTP_TP2 = (2 * get_sclk()) / 10^7 |
| 74 | */ |
| 75 | tp2 = (2 * get_sclk() / 10000000) << 8; |
| 76 | /* OTP_TP3 = magic constant */ |
| 77 | tp3 = (0x1401) << 15; |
| 78 | timing = tp1 | tp2 | tp3; |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 81 | bfrom_OtpCommand(OTP_INIT, write ? timing : timing & ~(-1 << 15)); |
| 82 | } |
| 83 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 84 | int do_otp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 85 | { |
Mike Frysinger | e6f07dc | 2010-10-02 14:31:32 -0400 | [diff] [blame] | 86 | char *cmd; |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 87 | uint32_t ret, base_flags; |
| 88 | bool prompt_user, force_read; |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 89 | uint32_t (*otp_func)(uint32_t page, uint32_t flags, uint64_t *page_content); |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 90 | |
| 91 | if (argc < 4) { |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 92 | usage: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 93 | return CMD_RET_USAGE; |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 96 | prompt_user = false; |
| 97 | base_flags = 0; |
Mike Frysinger | e6f07dc | 2010-10-02 14:31:32 -0400 | [diff] [blame] | 98 | cmd = argv[1]; |
| 99 | if (!strcmp(cmd, "read")) |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 100 | otp_func = bfrom_OtpRead; |
Mike Frysinger | e6f07dc | 2010-10-02 14:31:32 -0400 | [diff] [blame] | 101 | else if (!strcmp(cmd, "dump")) { |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 102 | otp_func = bfrom_OtpRead; |
| 103 | force_read = true; |
Mike Frysinger | e6f07dc | 2010-10-02 14:31:32 -0400 | [diff] [blame] | 104 | } else if (!strcmp(cmd, "write")) { |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 105 | otp_func = bfrom_OtpWrite; |
| 106 | base_flags = OTP_CHECK_FOR_PREV_WRITE; |
| 107 | if (!strcmp(argv[2], "--force")) { |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 108 | argv++; |
| 109 | --argc; |
| 110 | } else |
| 111 | prompt_user = false; |
Mike Frysinger | e6f07dc | 2010-10-02 14:31:32 -0400 | [diff] [blame] | 112 | } else if (!strcmp(cmd, "lock")) { |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 113 | if (argc != 4) |
| 114 | goto usage; |
| 115 | otp_func = bfrom_OtpWrite; |
| 116 | base_flags = OTP_LOCK; |
| 117 | } else |
| 118 | goto usage; |
| 119 | |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 120 | uint64_t *addr = (uint64_t *)simple_strtoul(argv[2], NULL, 16); |
| 121 | uint32_t page = simple_strtoul(argv[3], NULL, 16); |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 122 | uint32_t flags; |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 123 | size_t i, count; |
| 124 | ulong half; |
| 125 | |
| 126 | if (argc > 4) |
| 127 | count = simple_strtoul(argv[4], NULL, 16); |
| 128 | else |
| 129 | count = 2; |
| 130 | |
| 131 | if (argc > 5) { |
| 132 | half = simple_strtoul(argv[5], NULL, 16); |
| 133 | if (half != 0 && half != 1) { |
| 134 | puts("Error: 'half' can only be '0' or '1'\n"); |
| 135 | goto usage; |
| 136 | } |
| 137 | } else |
| 138 | half = 0; |
| 139 | |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 140 | /* "otp lock" has slightly different semantics */ |
| 141 | if (base_flags & OTP_LOCK) { |
| 142 | count = page; |
| 143 | page = (uint32_t)addr; |
| 144 | addr = NULL; |
| 145 | } |
| 146 | |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 147 | /* do to the nature of OTP, make sure users are sure */ |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 148 | if (prompt_user) { |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 149 | printf( |
| 150 | "Writing one time programmable memory\n" |
| 151 | "Make sure your operating voltages and temperature are within spec\n" |
| 152 | " source address: 0x%p\n" |
Mike Frysinger | 9d01614 | 2008-10-12 06:02:55 -0400 | [diff] [blame] | 153 | " OTP destination: %s page 0x%03X - %s page 0x%03lX\n" |
| 154 | " number to write: %lu halfpages\n" |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 155 | " type \"YES\" (no quotes) to confirm: ", |
| 156 | addr, |
| 157 | lowup(half), page, |
| 158 | lowup(half + count - 1), page + (half + count - 1) / 2, |
| 159 | half + count |
| 160 | ); |
Pierre Aubert | 5fef9d3 | 2014-04-24 10:30:07 +0200 | [diff] [blame] | 161 | if (!confirm_yesno()) { |
| 162 | printf(" Aborting\n"); |
| 163 | return 1; |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 164 | } |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Mike Frysinger | 9d01614 | 2008-10-12 06:02:55 -0400 | [diff] [blame] | 167 | printf("OTP memory %s: addr 0x%p page 0x%03X count %zu ... ", |
Mike Frysinger | e6f07dc | 2010-10-02 14:31:32 -0400 | [diff] [blame] | 168 | cmd, addr, page, count); |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 169 | |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 170 | set_otp_timing(otp_func == bfrom_OtpWrite); |
| 171 | if (otp_func == bfrom_OtpWrite && check_voltage()) { |
| 172 | puts("ERROR: VDDINT voltage is out of spec for writing\n"); |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | /* Do the actual reading/writing stuff */ |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 177 | ret = 0; |
| 178 | for (i = half; i < count + half; ++i) { |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 179 | flags = base_flags | (i % 2 ? OTP_UPPER_HALF : OTP_LOWER_HALF); |
| 180 | try_again: |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 181 | ret = otp_func(page, flags, addr); |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 182 | if (ret & OTP_MASTER_ERROR) { |
| 183 | if (force_read) { |
| 184 | if (flags & OTP_NO_ECC) |
| 185 | break; |
| 186 | else |
| 187 | flags |= OTP_NO_ECC; |
| 188 | puts("E"); |
| 189 | goto try_again; |
| 190 | } else |
| 191 | break; |
| 192 | } else if (ret) |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 193 | puts("W"); |
| 194 | else |
| 195 | puts("."); |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 196 | if (!(base_flags & OTP_LOCK)) { |
| 197 | ++addr; |
| 198 | if (i % 2) |
| 199 | ++page; |
| 200 | } else |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 201 | ++page; |
| 202 | } |
| 203 | if (ret & 0x1) |
| 204 | printf("\nERROR at page 0x%03X (%s-halfpage): 0x%03X: %s\n", |
| 205 | page, lowup(i), ret, otp_strerror(ret)); |
| 206 | else |
| 207 | puts(" done\n"); |
| 208 | |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 209 | /* Make sure we disable writing */ |
| 210 | set_otp_timing(false); |
| 211 | bfrom_OtpCommand(OTP_CLOSE, 0); |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 212 | |
| 213 | return ret; |
| 214 | } |
| 215 | |
Frans Meulenbroeks | 7675a09 | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 216 | U_BOOT_CMD( |
| 217 | otp, 7, 0, do_otp, |
Mike Frysinger | 8a3a8ac | 2009-03-23 22:17:27 -0400 | [diff] [blame] | 218 | "One-Time-Programmable sub-system", |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 219 | "read <addr> <page> [count] [half]\n" |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 220 | " - read 'count' half-pages starting at 'page' (offset 'half') to 'addr'\n" |
| 221 | "otp dump <addr> <page> [count] [half]\n" |
| 222 | " - like 'otp read', but skip read errors\n" |
Mike Frysinger | 13f5778 | 2008-02-04 19:26:57 -0500 | [diff] [blame] | 223 | "otp write [--force] <addr> <page> [count] [half]\n" |
Mike Frysinger | 0f68833 | 2008-10-11 22:05:42 -0400 | [diff] [blame] | 224 | " - write 'count' half-pages starting at 'page' (offset 'half') from 'addr'\n" |
| 225 | "otp lock <page> <count>\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 226 | " - lock 'count' pages starting at 'page'" |
| 227 | ); |