Simon Glass | cece904 | 2015-08-19 09:33:38 -0600 | [diff] [blame] | 1 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 2 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 3 | #include <console.h> |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 4 | #include "e1000.h" |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 5 | #include <malloc.h> |
Anatolij Gustschin | 6711ee5 | 2011-12-20 02:29:03 +0000 | [diff] [blame] | 6 | #include <linux/compiler.h> |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 7 | |
| 8 | /*----------------------------------------------------------------------- |
| 9 | * SPI transfer |
| 10 | * |
| 11 | * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks |
| 12 | * "bitlen" bits in the SPI MISO port. That's just the way SPI works. |
| 13 | * |
| 14 | * The source of the outgoing bits is the "dout" parameter and the |
| 15 | * destination of the input bits is the "din" parameter. Note that "dout" |
| 16 | * and "din" can point to the same memory location, in which case the |
| 17 | * input data overwrites the output data (since both are buffered by |
| 18 | * temporary variables, this is OK). |
| 19 | * |
| 20 | * This may be interrupted with Ctrl-C if "intr" is true, otherwise it will |
| 21 | * never return an error. |
| 22 | */ |
| 23 | static int e1000_spi_xfer(struct e1000_hw *hw, unsigned int bitlen, |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 24 | const void *dout_mem, void *din_mem, bool intr) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 25 | { |
| 26 | const uint8_t *dout = dout_mem; |
| 27 | uint8_t *din = din_mem; |
| 28 | |
| 29 | uint8_t mask = 0; |
| 30 | uint32_t eecd; |
| 31 | unsigned long i; |
| 32 | |
| 33 | /* Pre-read the control register */ |
| 34 | eecd = E1000_READ_REG(hw, EECD); |
| 35 | |
| 36 | /* Iterate over each bit */ |
| 37 | for (i = 0, mask = 0x80; i < bitlen; i++, mask = (mask >> 1)?:0x80) { |
| 38 | /* Check for interrupt */ |
| 39 | if (intr && ctrlc()) |
| 40 | return -1; |
| 41 | |
| 42 | /* Determine the output bit */ |
| 43 | if (dout && dout[i >> 3] & mask) |
| 44 | eecd |= E1000_EECD_DI; |
| 45 | else |
| 46 | eecd &= ~E1000_EECD_DI; |
| 47 | |
| 48 | /* Write the output bit and wait 50us */ |
| 49 | E1000_WRITE_REG(hw, EECD, eecd); |
| 50 | E1000_WRITE_FLUSH(hw); |
| 51 | udelay(50); |
| 52 | |
| 53 | /* Poke the clock (waits 50us) */ |
| 54 | e1000_raise_ee_clk(hw, &eecd); |
| 55 | |
| 56 | /* Now read the input bit */ |
| 57 | eecd = E1000_READ_REG(hw, EECD); |
| 58 | if (din) { |
| 59 | if (eecd & E1000_EECD_DO) |
| 60 | din[i >> 3] |= mask; |
| 61 | else |
| 62 | din[i >> 3] &= ~mask; |
| 63 | } |
| 64 | |
| 65 | /* Poke the clock again (waits 50us) */ |
| 66 | e1000_lower_ee_clk(hw, &eecd); |
| 67 | } |
| 68 | |
| 69 | /* Now clear any remaining bits of the input */ |
| 70 | if (din && (i & 7)) |
| 71 | din[i >> 3] &= ~((mask << 1) - 1); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | #ifdef CONFIG_E1000_SPI_GENERIC |
| 77 | static inline struct e1000_hw *e1000_hw_from_spi(struct spi_slave *spi) |
| 78 | { |
| 79 | return container_of(spi, struct e1000_hw, spi); |
| 80 | } |
| 81 | |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 82 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 83 | unsigned int max_hz, unsigned int mode) |
| 84 | { |
| 85 | /* Find the right PCI device */ |
| 86 | struct e1000_hw *hw = e1000_find_card(bus); |
| 87 | if (!hw) { |
| 88 | printf("ERROR: No such e1000 device: e1000#%u\n", bus); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | /* Make sure it has an SPI chip */ |
| 93 | if (hw->eeprom.type != e1000_eeprom_spi) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 94 | E1000_ERR(hw, "No attached SPI EEPROM found!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | /* Argument sanity checks */ |
| 99 | if (cs != 0) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 100 | E1000_ERR(hw, "No such SPI chip: %u\n", cs); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
| 103 | if (mode != SPI_MODE_0) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 104 | E1000_ERR(hw, "Only SPI MODE-0 is supported!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | /* TODO: Use max_hz somehow */ |
| 109 | E1000_DBG(hw->nic, "EEPROM SPI access requested\n"); |
| 110 | return &hw->spi; |
| 111 | } |
| 112 | |
| 113 | void spi_free_slave(struct spi_slave *spi) |
| 114 | { |
Anatolij Gustschin | 6711ee5 | 2011-12-20 02:29:03 +0000 | [diff] [blame] | 115 | __maybe_unused struct e1000_hw *hw = e1000_hw_from_spi(spi); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 116 | E1000_DBG(hw->nic, "EEPROM SPI access released\n"); |
| 117 | } |
| 118 | |
| 119 | int spi_claim_bus(struct spi_slave *spi) |
| 120 | { |
| 121 | struct e1000_hw *hw = e1000_hw_from_spi(spi); |
| 122 | |
| 123 | if (e1000_acquire_eeprom(hw)) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 124 | E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | void spi_release_bus(struct spi_slave *spi) |
| 132 | { |
| 133 | struct e1000_hw *hw = e1000_hw_from_spi(spi); |
| 134 | e1000_release_eeprom(hw); |
| 135 | } |
| 136 | |
| 137 | /* Skinny wrapper around e1000_spi_xfer */ |
| 138 | int spi_xfer(struct spi_slave *spi, unsigned int bitlen, |
| 139 | const void *dout_mem, void *din_mem, unsigned long flags) |
| 140 | { |
| 141 | struct e1000_hw *hw = e1000_hw_from_spi(spi); |
| 142 | int ret; |
| 143 | |
| 144 | if (flags & SPI_XFER_BEGIN) |
| 145 | e1000_standby_eeprom(hw); |
| 146 | |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 147 | ret = e1000_spi_xfer(hw, bitlen, dout_mem, din_mem, true); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 148 | |
| 149 | if (flags & SPI_XFER_END) |
| 150 | e1000_standby_eeprom(hw); |
| 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | #endif /* not CONFIG_E1000_SPI_GENERIC */ |
| 156 | |
| 157 | #ifdef CONFIG_CMD_E1000 |
| 158 | |
| 159 | /* The EEPROM opcodes */ |
| 160 | #define SPI_EEPROM_ENABLE_WR 0x06 |
| 161 | #define SPI_EEPROM_DISABLE_WR 0x04 |
| 162 | #define SPI_EEPROM_WRITE_STATUS 0x01 |
| 163 | #define SPI_EEPROM_READ_STATUS 0x05 |
| 164 | #define SPI_EEPROM_WRITE_PAGE 0x02 |
| 165 | #define SPI_EEPROM_READ_PAGE 0x03 |
| 166 | |
| 167 | /* The EEPROM status bits */ |
| 168 | #define SPI_EEPROM_STATUS_BUSY 0x01 |
| 169 | #define SPI_EEPROM_STATUS_WREN 0x02 |
| 170 | |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 171 | static int e1000_spi_eeprom_enable_wr(struct e1000_hw *hw, bool intr) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 172 | { |
| 173 | u8 op[] = { SPI_EEPROM_ENABLE_WR }; |
| 174 | e1000_standby_eeprom(hw); |
| 175 | return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr); |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * These have been tested to perform correctly, but they are not used by any |
| 180 | * of the EEPROM commands at this time. |
| 181 | */ |
Bin Meng | d0d175f | 2015-11-16 01:19:18 -0800 | [diff] [blame] | 182 | static __maybe_unused int e1000_spi_eeprom_disable_wr(struct e1000_hw *hw, |
| 183 | bool intr) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 184 | { |
| 185 | u8 op[] = { SPI_EEPROM_DISABLE_WR }; |
| 186 | e1000_standby_eeprom(hw); |
| 187 | return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr); |
| 188 | } |
| 189 | |
Bin Meng | d0d175f | 2015-11-16 01:19:18 -0800 | [diff] [blame] | 190 | static __maybe_unused int e1000_spi_eeprom_write_status(struct e1000_hw *hw, |
| 191 | u8 status, bool intr) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 192 | { |
| 193 | u8 op[] = { SPI_EEPROM_WRITE_STATUS, status }; |
| 194 | e1000_standby_eeprom(hw); |
| 195 | return e1000_spi_xfer(hw, 8*sizeof(op), op, NULL, intr); |
| 196 | } |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 197 | |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 198 | static int e1000_spi_eeprom_read_status(struct e1000_hw *hw, bool intr) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 199 | { |
| 200 | u8 op[] = { SPI_EEPROM_READ_STATUS, 0 }; |
| 201 | e1000_standby_eeprom(hw); |
| 202 | if (e1000_spi_xfer(hw, 8*sizeof(op), op, op, intr)) |
| 203 | return -1; |
| 204 | return op[1]; |
| 205 | } |
| 206 | |
| 207 | static int e1000_spi_eeprom_write_page(struct e1000_hw *hw, |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 208 | const void *data, u16 off, u16 len, bool intr) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 209 | { |
| 210 | u8 op[] = { |
| 211 | SPI_EEPROM_WRITE_PAGE, |
| 212 | (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff |
| 213 | }; |
| 214 | |
| 215 | e1000_standby_eeprom(hw); |
| 216 | |
| 217 | if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr)) |
| 218 | return -1; |
| 219 | if (e1000_spi_xfer(hw, len << 3, data, NULL, intr)) |
| 220 | return -1; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int e1000_spi_eeprom_read_page(struct e1000_hw *hw, |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 226 | void *data, u16 off, u16 len, bool intr) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 227 | { |
| 228 | u8 op[] = { |
| 229 | SPI_EEPROM_READ_PAGE, |
| 230 | (off >> (hw->eeprom.address_bits - 8)) & 0xff, off & 0xff |
| 231 | }; |
| 232 | |
| 233 | e1000_standby_eeprom(hw); |
| 234 | |
| 235 | if (e1000_spi_xfer(hw, 8 + hw->eeprom.address_bits, op, NULL, intr)) |
| 236 | return -1; |
| 237 | if (e1000_spi_xfer(hw, len << 3, NULL, data, intr)) |
| 238 | return -1; |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 243 | static int e1000_spi_eeprom_poll_ready(struct e1000_hw *hw, bool intr) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 244 | { |
| 245 | int status; |
| 246 | while ((status = e1000_spi_eeprom_read_status(hw, intr)) >= 0) { |
| 247 | if (!(status & SPI_EEPROM_STATUS_BUSY)) |
| 248 | return 0; |
| 249 | } |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | static int e1000_spi_eeprom_dump(struct e1000_hw *hw, |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 254 | void *data, u16 off, unsigned int len, bool intr) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 255 | { |
| 256 | /* Interruptibly wait for the EEPROM to be ready */ |
| 257 | if (e1000_spi_eeprom_poll_ready(hw, intr)) |
| 258 | return -1; |
| 259 | |
| 260 | /* Dump each page in sequence */ |
| 261 | while (len) { |
| 262 | /* Calculate the data bytes on this page */ |
| 263 | u16 pg_off = off & (hw->eeprom.page_size - 1); |
| 264 | u16 pg_len = hw->eeprom.page_size - pg_off; |
| 265 | if (pg_len > len) |
| 266 | pg_len = len; |
| 267 | |
| 268 | /* Now dump the page */ |
| 269 | if (e1000_spi_eeprom_read_page(hw, data, off, pg_len, intr)) |
| 270 | return -1; |
| 271 | |
| 272 | /* Otherwise go on to the next page */ |
| 273 | len -= pg_len; |
| 274 | off += pg_len; |
| 275 | data += pg_len; |
| 276 | } |
| 277 | |
| 278 | /* We're done! */ |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int e1000_spi_eeprom_program(struct e1000_hw *hw, |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 283 | const void *data, u16 off, u16 len, bool intr) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 284 | { |
| 285 | /* Program each page in sequence */ |
| 286 | while (len) { |
| 287 | /* Calculate the data bytes on this page */ |
| 288 | u16 pg_off = off & (hw->eeprom.page_size - 1); |
| 289 | u16 pg_len = hw->eeprom.page_size - pg_off; |
| 290 | if (pg_len > len) |
| 291 | pg_len = len; |
| 292 | |
| 293 | /* Interruptibly wait for the EEPROM to be ready */ |
| 294 | if (e1000_spi_eeprom_poll_ready(hw, intr)) |
| 295 | return -1; |
| 296 | |
| 297 | /* Enable write access */ |
| 298 | if (e1000_spi_eeprom_enable_wr(hw, intr)) |
| 299 | return -1; |
| 300 | |
| 301 | /* Now program the page */ |
| 302 | if (e1000_spi_eeprom_write_page(hw, data, off, pg_len, intr)) |
| 303 | return -1; |
| 304 | |
| 305 | /* Otherwise go on to the next page */ |
| 306 | len -= pg_len; |
| 307 | off += pg_len; |
| 308 | data += pg_len; |
| 309 | } |
| 310 | |
| 311 | /* Wait for the last write to complete */ |
| 312 | if (e1000_spi_eeprom_poll_ready(hw, intr)) |
| 313 | return -1; |
| 314 | |
| 315 | /* We're done! */ |
| 316 | return 0; |
| 317 | } |
| 318 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 319 | static int do_e1000_spi_show(struct cmd_tbl *cmdtp, struct e1000_hw *hw, |
| 320 | int argc, char *const argv[]) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 321 | { |
| 322 | unsigned int length = 0; |
| 323 | u16 i, offset = 0; |
| 324 | u8 *buffer; |
| 325 | int err; |
| 326 | |
| 327 | if (argc > 2) { |
| 328 | cmd_usage(cmdtp); |
| 329 | return 1; |
| 330 | } |
| 331 | |
| 332 | /* Parse the offset and length */ |
| 333 | if (argc >= 1) |
| 334 | offset = simple_strtoul(argv[0], NULL, 0); |
| 335 | if (argc == 2) |
| 336 | length = simple_strtoul(argv[1], NULL, 0); |
| 337 | else if (offset < (hw->eeprom.word_size << 1)) |
| 338 | length = (hw->eeprom.word_size << 1) - offset; |
| 339 | |
| 340 | /* Extra sanity checks */ |
| 341 | if (!length) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 342 | E1000_ERR(hw, "Requested zero-sized dump!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 343 | return 1; |
| 344 | } |
| 345 | if ((0x10000 < length) || (0x10000 - length < offset)) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 346 | E1000_ERR(hw, "Can't dump past 0xFFFF!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 347 | return 1; |
| 348 | } |
| 349 | |
| 350 | /* Allocate a buffer to hold stuff */ |
| 351 | buffer = malloc(length); |
| 352 | if (!buffer) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 353 | E1000_ERR(hw, "Out of Memory!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 354 | return 1; |
| 355 | } |
| 356 | |
| 357 | /* Acquire the EEPROM and perform the dump */ |
| 358 | if (e1000_acquire_eeprom(hw)) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 359 | E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 360 | free(buffer); |
| 361 | return 1; |
| 362 | } |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 363 | err = e1000_spi_eeprom_dump(hw, buffer, offset, length, true); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 364 | e1000_release_eeprom(hw); |
| 365 | if (err) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 366 | E1000_ERR(hw, "Interrupted!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 367 | free(buffer); |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | /* Now hexdump the result */ |
| 372 | printf("%s: ===== Intel e1000 EEPROM (0x%04hX - 0x%04hX) =====", |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 373 | hw->name, offset, offset + length - 1); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 374 | for (i = 0; i < length; i++) { |
| 375 | if ((i & 0xF) == 0) |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 376 | printf("\n%s: %04hX: ", hw->name, offset + i); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 377 | else if ((i & 0xF) == 0x8) |
| 378 | printf(" "); |
| 379 | printf(" %02hx", buffer[i]); |
| 380 | } |
| 381 | printf("\n"); |
| 382 | |
| 383 | /* Success! */ |
| 384 | free(buffer); |
| 385 | return 0; |
| 386 | } |
| 387 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 388 | static int do_e1000_spi_dump(struct cmd_tbl *cmdtp, struct e1000_hw *hw, |
| 389 | int argc, char *const argv[]) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 390 | { |
| 391 | unsigned int length; |
| 392 | u16 offset; |
| 393 | void *dest; |
| 394 | |
| 395 | if (argc != 3) { |
| 396 | cmd_usage(cmdtp); |
| 397 | return 1; |
| 398 | } |
| 399 | |
| 400 | /* Parse the arguments */ |
| 401 | dest = (void *)simple_strtoul(argv[0], NULL, 16); |
| 402 | offset = simple_strtoul(argv[1], NULL, 0); |
| 403 | length = simple_strtoul(argv[2], NULL, 0); |
| 404 | |
| 405 | /* Extra sanity checks */ |
| 406 | if (!length) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 407 | E1000_ERR(hw, "Requested zero-sized dump!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 408 | return 1; |
| 409 | } |
| 410 | if ((0x10000 < length) || (0x10000 - length < offset)) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 411 | E1000_ERR(hw, "Can't dump past 0xFFFF!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 412 | return 1; |
| 413 | } |
| 414 | |
| 415 | /* Acquire the EEPROM */ |
| 416 | if (e1000_acquire_eeprom(hw)) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 417 | E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | /* Perform the programming operation */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 422 | if (e1000_spi_eeprom_dump(hw, dest, offset, length, true) < 0) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 423 | E1000_ERR(hw, "Interrupted!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 424 | e1000_release_eeprom(hw); |
| 425 | return 1; |
| 426 | } |
| 427 | |
| 428 | e1000_release_eeprom(hw); |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 429 | printf("%s: ===== EEPROM DUMP COMPLETE =====\n", hw->name); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 433 | static int do_e1000_spi_program(struct cmd_tbl *cmdtp, struct e1000_hw *hw, |
| 434 | int argc, char *const argv[]) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 435 | { |
| 436 | unsigned int length; |
| 437 | const void *source; |
| 438 | u16 offset; |
| 439 | |
| 440 | if (argc != 3) { |
| 441 | cmd_usage(cmdtp); |
| 442 | return 1; |
| 443 | } |
| 444 | |
| 445 | /* Parse the arguments */ |
| 446 | source = (const void *)simple_strtoul(argv[0], NULL, 16); |
| 447 | offset = simple_strtoul(argv[1], NULL, 0); |
| 448 | length = simple_strtoul(argv[2], NULL, 0); |
| 449 | |
| 450 | /* Acquire the EEPROM */ |
| 451 | if (e1000_acquire_eeprom(hw)) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 452 | E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 453 | return 1; |
| 454 | } |
| 455 | |
| 456 | /* Perform the programming operation */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 457 | if (e1000_spi_eeprom_program(hw, source, offset, length, true) < 0) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 458 | E1000_ERR(hw, "Interrupted!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 459 | e1000_release_eeprom(hw); |
| 460 | return 1; |
| 461 | } |
| 462 | |
| 463 | e1000_release_eeprom(hw); |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 464 | printf("%s: ===== EEPROM PROGRAMMED =====\n", hw->name); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 468 | static int do_e1000_spi_checksum(struct cmd_tbl *cmdtp, struct e1000_hw *hw, |
| 469 | int argc, char *const argv[]) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 470 | { |
Anatolij Gustschin | 6711ee5 | 2011-12-20 02:29:03 +0000 | [diff] [blame] | 471 | uint16_t i, length, checksum = 0, checksum_reg; |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 472 | uint16_t *buffer; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 473 | bool upd; |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 474 | |
| 475 | if (argc == 0) |
| 476 | upd = 0; |
| 477 | else if ((argc == 1) && !strcmp(argv[0], "update")) |
| 478 | upd = 1; |
| 479 | else { |
| 480 | cmd_usage(cmdtp); |
| 481 | return 1; |
| 482 | } |
| 483 | |
| 484 | /* Allocate a temporary buffer */ |
| 485 | length = sizeof(uint16_t) * (EEPROM_CHECKSUM_REG + 1); |
| 486 | buffer = malloc(length); |
| 487 | if (!buffer) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 488 | E1000_ERR(hw, "Unable to allocate EEPROM buffer!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | /* Acquire the EEPROM */ |
| 493 | if (e1000_acquire_eeprom(hw)) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 494 | E1000_ERR(hw, "EEPROM SPI cannot be acquired!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 495 | return 1; |
| 496 | } |
| 497 | |
| 498 | /* Read the EEPROM */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 499 | if (e1000_spi_eeprom_dump(hw, buffer, 0, length, true) < 0) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 500 | E1000_ERR(hw, "Interrupted!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 501 | e1000_release_eeprom(hw); |
| 502 | return 1; |
| 503 | } |
| 504 | |
| 505 | /* Compute the checksum and read the expected value */ |
| 506 | for (i = 0; i < EEPROM_CHECKSUM_REG; i++) |
| 507 | checksum += le16_to_cpu(buffer[i]); |
| 508 | checksum = ((uint16_t)EEPROM_SUM) - checksum; |
| 509 | checksum_reg = le16_to_cpu(buffer[i]); |
| 510 | |
| 511 | /* Verify it! */ |
| 512 | if (checksum_reg == checksum) { |
| 513 | printf("%s: INFO: EEPROM checksum is correct! (0x%04hx)\n", |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 514 | hw->name, checksum); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 515 | e1000_release_eeprom(hw); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /* Hrm, verification failed, print an error */ |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 520 | E1000_ERR(hw, "EEPROM checksum is incorrect!\n"); |
| 521 | E1000_ERR(hw, " ...register was 0x%04hx, calculated 0x%04hx\n", |
| 522 | checksum_reg, checksum); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 523 | |
| 524 | /* If they didn't ask us to update it, just return an error */ |
| 525 | if (!upd) { |
| 526 | e1000_release_eeprom(hw); |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | /* Ok, correct it! */ |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 531 | printf("%s: Reprogramming the EEPROM checksum...\n", hw->name); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 532 | buffer[i] = cpu_to_le16(checksum); |
| 533 | if (e1000_spi_eeprom_program(hw, &buffer[i], i * sizeof(uint16_t), |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 534 | sizeof(uint16_t), true)) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 535 | E1000_ERR(hw, "Interrupted!\n"); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 536 | e1000_release_eeprom(hw); |
| 537 | return 1; |
| 538 | } |
| 539 | |
| 540 | e1000_release_eeprom(hw); |
| 541 | return 0; |
| 542 | } |
| 543 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame^] | 544 | int do_e1000_spi(struct cmd_tbl *cmdtp, struct e1000_hw *hw, |
| 545 | int argc, char *const argv[]) |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 546 | { |
| 547 | if (argc < 1) { |
| 548 | cmd_usage(cmdtp); |
| 549 | return 1; |
| 550 | } |
| 551 | |
| 552 | /* Make sure it has an SPI chip */ |
| 553 | if (hw->eeprom.type != e1000_eeprom_spi) { |
Alban Bedel | c1255dd | 2016-08-03 11:31:03 +0200 | [diff] [blame] | 554 | E1000_ERR(hw, "No attached SPI EEPROM found (%d)!\n", |
| 555 | hw->eeprom.type); |
Kyle Moffett | 64b94dd | 2011-10-18 11:05:29 +0000 | [diff] [blame] | 556 | return 1; |
| 557 | } |
| 558 | |
| 559 | /* Check the eeprom sub-sub-command arguments */ |
| 560 | if (!strcmp(argv[0], "show")) |
| 561 | return do_e1000_spi_show(cmdtp, hw, argc - 1, argv + 1); |
| 562 | |
| 563 | if (!strcmp(argv[0], "dump")) |
| 564 | return do_e1000_spi_dump(cmdtp, hw, argc - 1, argv + 1); |
| 565 | |
| 566 | if (!strcmp(argv[0], "program")) |
| 567 | return do_e1000_spi_program(cmdtp, hw, argc - 1, argv + 1); |
| 568 | |
| 569 | if (!strcmp(argv[0], "checksum")) |
| 570 | return do_e1000_spi_checksum(cmdtp, hw, argc - 1, argv + 1); |
| 571 | |
| 572 | cmd_usage(cmdtp); |
| 573 | return 1; |
| 574 | } |
| 575 | |
| 576 | #endif /* not CONFIG_CMD_E1000 */ |