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