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