Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Ricardo Ribalda - Qtechnology A/S |
| 5 | * Copyright (C) 2011, 2014 Stefan Tauner |
| 6 | * |
| 7 | * Based on nicinctel_spi.c and ichspi.c |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * Datasheet: Intel 82580 Quad/Dual Gigabit Ethernet LAN Controller Datasheet |
| 21 | * 3.3.1.4: General EEPROM Software Access |
| 22 | * 4.7: Access to shared resources (FIXME: we should probably use this semaphore interface) |
| 23 | * 7.4: Register Descriptions |
| 24 | */ |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 25 | /* |
| 26 | * Datasheet: Intel Ethernet Controller I210: Datasheet |
| 27 | * 8.4.3: EEPROM-Mode Read Register |
| 28 | * 8.4.6: EEPROM-Mode Write Register |
| 29 | * Write process inspired on kernel e1000_i210.c |
| 30 | */ |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 31 | |
| 32 | #include <stdlib.h> |
| 33 | #include <unistd.h> |
| 34 | #include "flash.h" |
| 35 | #include "spi.h" |
| 36 | #include "programmer.h" |
| 37 | #include "hwaccess.h" |
| 38 | |
| 39 | #define PCI_VENDOR_ID_INTEL 0x8086 |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 40 | #define MEMMAP_SIZE 0x1c /* Only EEC, EERD and EEWR are needed. */ |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 41 | |
| 42 | #define EEC 0x10 /* EEPROM/Flash Control Register */ |
| 43 | #define EERD 0x14 /* EEPROM Read Register */ |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 44 | #define EEWR 0x18 /* EEPROM Write Register */ |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 45 | |
| 46 | /* EPROM/Flash Control Register bits */ |
| 47 | #define EE_SCK 0 |
| 48 | #define EE_CS 1 |
| 49 | #define EE_SI 2 |
| 50 | #define EE_SO 3 |
| 51 | #define EE_REQ 6 |
| 52 | #define EE_GNT 7 |
| 53 | #define EE_PRES 8 |
| 54 | #define EE_SIZE 11 |
| 55 | #define EE_SIZE_MASK 0xf |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 56 | #define EE_FLUPD 23 |
| 57 | #define EE_FLUDONE 26 |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 58 | |
| 59 | /* EEPROM Read Register bits */ |
| 60 | #define EERD_START 0 |
| 61 | #define EERD_DONE 1 |
| 62 | #define EERD_ADDR 2 |
| 63 | #define EERD_DATA 16 |
| 64 | |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 65 | /* EEPROM Write Register bits */ |
| 66 | #define EEWR_CMDV 0 |
| 67 | #define EEWR_DONE 1 |
| 68 | #define EEWR_ADDR 2 |
| 69 | #define EEWR_DATA 16 |
| 70 | |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 71 | #define BIT(x) (1<<x) |
Stefan Tauner | 8d21ff1 | 2015-01-10 09:33:06 +0000 | [diff] [blame] | 72 | #define EE_PAGE_MASK 0x3f |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 73 | |
| 74 | static uint8_t *nicintel_eebar; |
| 75 | static struct pci_dev *nicintel_pci; |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 76 | static bool done_i20_write = false; |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 77 | |
| 78 | #define UNPROG_DEVICE 0x1509 |
| 79 | |
Nico Huber | 4343e7d | 2017-10-10 17:38:07 +0200 | [diff] [blame] | 80 | /* |
| 81 | * Warning: is_i210() below makes assumptions on these PCI ids. |
| 82 | * It may have to be updated when this list is extended. |
| 83 | */ |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 84 | const struct dev_entry nics_intel_ee[] = { |
| 85 | {PCI_VENDOR_ID_INTEL, 0x150e, OK, "Intel", "82580 Quad Gigabit Ethernet Controller (Copper)"}, |
| 86 | {PCI_VENDOR_ID_INTEL, 0x150f, NT , "Intel", "82580 Quad Gigabit Ethernet Controller (Fiber)"}, |
| 87 | {PCI_VENDOR_ID_INTEL, 0x1510, NT , "Intel", "82580 Quad Gigabit Ethernet Controller (Backplane)"}, |
| 88 | {PCI_VENDOR_ID_INTEL, 0x1511, NT , "Intel", "82580 Quad Gigabit Ethernet Controller (Ext. PHY)"}, |
| 89 | {PCI_VENDOR_ID_INTEL, 0x1511, NT , "Intel", "82580 Dual Gigabit Ethernet Controller (Copper)"}, |
| 90 | {PCI_VENDOR_ID_INTEL, UNPROG_DEVICE, OK, "Intel", "Unprogrammed 82580 Quad/Dual Gigabit Ethernet Controller"}, |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 91 | {PCI_VENDOR_ID_INTEL, 0x1531, NT, "Intel", "I210 Gigabit Network Connection Unprogrammed"}, |
| 92 | {PCI_VENDOR_ID_INTEL, 0x1532, NT, "Intel", "I211 Gigabit Network Connection Unprogrammed"}, |
| 93 | {PCI_VENDOR_ID_INTEL, 0x1533, OK, "Intel", "I210 Gigabit Network Connection"}, |
| 94 | {PCI_VENDOR_ID_INTEL, 0x1536, NT, "Intel", "I210 Gigabit Network Connection SERDES Fiber"}, |
| 95 | {PCI_VENDOR_ID_INTEL, 0x1537, NT, "Intel", "I210 Gigabit Network Connection SERDES Backplane"}, |
| 96 | {PCI_VENDOR_ID_INTEL, 0x1538, NT, "Intel", "I210 Gigabit Network Connection SGMII"}, |
| 97 | {PCI_VENDOR_ID_INTEL, 0x1539, NT, "Intel", "I211 Gigabit Network Connection"}, |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 98 | {0}, |
| 99 | }; |
| 100 | |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 101 | static inline bool is_i210(uint16_t device_id) |
| 102 | { |
Nico Huber | 4343e7d | 2017-10-10 17:38:07 +0200 | [diff] [blame] | 103 | return (device_id & 0xfff0) == 0x1530; |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static int nicintel_ee_probe_i210(struct flashctx *flash) |
| 107 | { |
| 108 | /* Emulated eeprom has a fixed size of 4 KB */ |
| 109 | flash->chip->total_size = 4; |
| 110 | flash->chip->page_size = flash->chip->total_size * 1024; |
| 111 | flash->chip->tested = TEST_OK_PREW; |
| 112 | flash->chip->gran = write_gran_1byte_implicit_erase; |
| 113 | flash->chip->block_erasers->eraseblocks[0].size = flash->chip->page_size; |
| 114 | flash->chip->block_erasers->eraseblocks[0].count = 1; |
| 115 | |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | static int nicintel_ee_probe_82580(struct flashctx *flash) |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 120 | { |
| 121 | if (nicintel_pci->device_id == UNPROG_DEVICE) |
| 122 | flash->chip->total_size = 16; /* Fall back to minimum supported size. */ |
| 123 | else { |
| 124 | uint32_t tmp = pci_mmio_readl(nicintel_eebar + EEC); |
| 125 | tmp = ((tmp >> EE_SIZE) & EE_SIZE_MASK); |
| 126 | switch (tmp) { |
| 127 | case 7: |
| 128 | flash->chip->total_size = 16; |
| 129 | break; |
| 130 | case 8: |
| 131 | flash->chip->total_size = 32; |
| 132 | break; |
| 133 | default: |
| 134 | msg_cerr("Unsupported chip size 0x%x\n", tmp); |
| 135 | return 0; |
| 136 | } |
| 137 | } |
| 138 | |
Stefan Tauner | 8d21ff1 | 2015-01-10 09:33:06 +0000 | [diff] [blame] | 139 | flash->chip->page_size = EE_PAGE_MASK + 1; |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 140 | flash->chip->tested = TEST_OK_PREW; |
| 141 | flash->chip->gran = write_gran_1byte_implicit_erase; |
Stefan Tauner | 8d21ff1 | 2015-01-10 09:33:06 +0000 | [diff] [blame] | 142 | flash->chip->block_erasers->eraseblocks[0].size = (EE_PAGE_MASK + 1); |
| 143 | flash->chip->block_erasers->eraseblocks[0].count = (flash->chip->total_size * 1024) / (EE_PAGE_MASK + 1); |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 144 | |
| 145 | return 1; |
| 146 | } |
| 147 | |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 148 | static int nicintel_ee_probe(struct flashctx *flash) |
| 149 | { |
| 150 | if (is_i210(nicintel_pci->device_id)) |
| 151 | return nicintel_ee_probe_i210(flash); |
| 152 | |
| 153 | return nicintel_ee_probe_82580(flash); |
| 154 | } |
| 155 | |
| 156 | #define MAX_ATTEMPTS 10000000 |
Stefan Tauner | 3e6b7bb | 2015-01-25 23:45:14 +0000 | [diff] [blame] | 157 | static int nicintel_ee_read_word(unsigned int addr, uint16_t *data) |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 158 | { |
| 159 | uint32_t tmp = BIT(EERD_START) | (addr << EERD_ADDR); |
| 160 | pci_mmio_writel(tmp, nicintel_eebar + EERD); |
| 161 | |
| 162 | /* Poll done flag. 10.000.000 cycles seem to be enough. */ |
| 163 | uint32_t i; |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 164 | for (i = 0; i < MAX_ATTEMPTS; i++) { |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 165 | tmp = pci_mmio_readl(nicintel_eebar + EERD); |
| 166 | if (tmp & BIT(EERD_DONE)) { |
Stefan Tauner | 3e6b7bb | 2015-01-25 23:45:14 +0000 | [diff] [blame] | 167 | *data = (tmp >> EERD_DATA) & 0xffff; |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | static int nicintel_ee_read(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len) |
| 176 | { |
Stefan Tauner | 3e6b7bb | 2015-01-25 23:45:14 +0000 | [diff] [blame] | 177 | uint16_t data; |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 178 | |
| 179 | /* The NIC interface always reads 16 b words so we need to convert the address and handle odd address |
| 180 | * explicitly at the start (and also at the end in the loop below). */ |
| 181 | if (addr & 1) { |
Stefan Tauner | 3e6b7bb | 2015-01-25 23:45:14 +0000 | [diff] [blame] | 182 | if (nicintel_ee_read_word(addr / 2, &data)) |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 183 | return -1; |
Stefan Tauner | 3e6b7bb | 2015-01-25 23:45:14 +0000 | [diff] [blame] | 184 | *buf++ = data & 0xff; |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 185 | addr++; |
| 186 | len--; |
| 187 | } |
| 188 | |
| 189 | while (len > 0) { |
Stefan Tauner | 3e6b7bb | 2015-01-25 23:45:14 +0000 | [diff] [blame] | 190 | if (nicintel_ee_read_word(addr / 2, &data)) |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 191 | return -1; |
Stefan Tauner | 3e6b7bb | 2015-01-25 23:45:14 +0000 | [diff] [blame] | 192 | *buf++ = data & 0xff; |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 193 | addr++; |
| 194 | len--; |
| 195 | if (len > 0) { |
Stefan Tauner | 3e6b7bb | 2015-01-25 23:45:14 +0000 | [diff] [blame] | 196 | *buf++ = (data >> 8) & 0xff; |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 197 | addr++; |
| 198 | len--; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 205 | static int nicintel_ee_write_word_i210(unsigned int addr, uint16_t data) |
| 206 | { |
| 207 | uint32_t eewr; |
| 208 | |
| 209 | eewr = addr << EEWR_ADDR; |
| 210 | eewr |= data << EEWR_DATA; |
| 211 | eewr |= BIT(EEWR_CMDV); |
| 212 | pci_mmio_writel(eewr, nicintel_eebar + EEWR); |
| 213 | |
| 214 | programmer_delay(5); |
David Hendricks | 79d838d | 2017-09-27 09:25:34 -0700 | [diff] [blame] | 215 | int i; |
| 216 | for (i = 0; i < MAX_ATTEMPTS; i++) |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 217 | if (pci_mmio_readl(nicintel_eebar + EEWR) & BIT(EEWR_DONE)) |
| 218 | return 0; |
| 219 | return -1; |
| 220 | } |
| 221 | |
Nico Huber | 4343e7d | 2017-10-10 17:38:07 +0200 | [diff] [blame] | 222 | static int nicintel_ee_write_i210(struct flashctx *flash, const uint8_t *buf, |
| 223 | unsigned int addr, unsigned int len) |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 224 | { |
| 225 | done_i20_write = true; |
| 226 | |
| 227 | if (addr & 1) { |
| 228 | uint16_t data; |
| 229 | |
| 230 | if (nicintel_ee_read_word(addr / 2, &data)) { |
| 231 | msg_perr("Timeout reading heading byte\n"); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | data &= 0xff; |
| 236 | data |= (buf ? (buf[0]) : 0xff) << 8; |
| 237 | |
| 238 | if (nicintel_ee_write_word_i210(addr / 2, data)) { |
| 239 | msg_perr("Timeout writing heading word\n"); |
| 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | if (buf) |
| 244 | buf ++; |
| 245 | addr ++; |
| 246 | len --; |
| 247 | } |
| 248 | |
| 249 | while (len > 0) { |
| 250 | uint16_t data; |
| 251 | |
| 252 | if (len == 1) { |
| 253 | if (nicintel_ee_read_word(addr / 2, &data)) { |
| 254 | msg_perr("Timeout reading tail byte\n"); |
| 255 | return -1; |
| 256 | } |
| 257 | |
| 258 | data &= 0xff00; |
| 259 | data |= buf ? (buf[0]) : 0xff; |
| 260 | } else { |
| 261 | if (buf) |
| 262 | data = buf[0] | (buf[1] << 8); |
| 263 | else |
| 264 | data = 0xffff; |
| 265 | } |
| 266 | |
| 267 | if (nicintel_ee_write_word_i210(addr / 2, data)) { |
| 268 | msg_perr("Timeout writing Shadow RAM\n"); |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | if (buf) |
| 273 | buf += 2; |
| 274 | if (len > 2) |
| 275 | len -= 2; |
| 276 | else |
| 277 | len = 0; |
| 278 | addr += 2; |
| 279 | } |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 284 | static int nicintel_ee_bitset(int reg, int bit, bool val) |
| 285 | { |
| 286 | uint32_t tmp; |
| 287 | |
| 288 | tmp = pci_mmio_readl(nicintel_eebar + reg); |
| 289 | if (val) |
| 290 | tmp |= BIT(bit); |
| 291 | else |
| 292 | tmp &= ~BIT(bit); |
| 293 | pci_mmio_writel(tmp, nicintel_eebar + reg); |
| 294 | |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | /* Shifts one byte out while receiving another one by bitbanging (denoted "direct access" in the datasheet). */ |
| 299 | static int nicintel_ee_bitbang(uint8_t mosi, uint8_t *miso) |
| 300 | { |
| 301 | uint8_t out = 0x0; |
| 302 | |
| 303 | int i; |
| 304 | for (i = 7; i >= 0; i--) { |
| 305 | nicintel_ee_bitset(EEC, EE_SI, mosi & BIT(i)); |
| 306 | nicintel_ee_bitset(EEC, EE_SCK, 1); |
| 307 | if (miso != NULL) { |
| 308 | uint32_t tmp = pci_mmio_readl(nicintel_eebar + EEC); |
| 309 | if (tmp & BIT(EE_SO)) |
| 310 | out |= BIT(i); |
| 311 | } |
| 312 | nicintel_ee_bitset(EEC, EE_SCK, 0); |
| 313 | } |
| 314 | |
| 315 | if (miso != NULL) |
| 316 | *miso = out; |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | /* Polls the WIP bit of the status register of the attached EEPROM via bitbanging. */ |
| 322 | static int nicintel_ee_ready(void) |
| 323 | { |
| 324 | unsigned int i; |
| 325 | for (i = 0; i < 1000; i++) { |
| 326 | nicintel_ee_bitset(EEC, EE_CS, 0); |
| 327 | |
| 328 | nicintel_ee_bitbang(JEDEC_RDSR, NULL); |
| 329 | uint8_t rdsr; |
| 330 | nicintel_ee_bitbang(0x00, &rdsr); |
| 331 | |
| 332 | nicintel_ee_bitset(EEC, EE_CS, 1); |
| 333 | programmer_delay(1); |
| 334 | if (!(rdsr & SPI_SR_WIP)) { |
| 335 | return 0; |
| 336 | } |
| 337 | } |
| 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | /* Requests direct access to the SPI pins. */ |
| 342 | static int nicintel_ee_req(void) |
| 343 | { |
| 344 | uint32_t tmp; |
| 345 | nicintel_ee_bitset(EEC, EE_REQ, 1); |
| 346 | |
| 347 | tmp = pci_mmio_readl(nicintel_eebar + EEC); |
| 348 | if (!(tmp & BIT(EE_GNT))) { |
| 349 | msg_perr("Enabling eeprom access failed.\n"); |
| 350 | return 1; |
| 351 | } |
| 352 | |
| 353 | nicintel_ee_bitset(EEC, EE_SCK, 0); |
| 354 | return 0; |
| 355 | } |
| 356 | |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 357 | static int nicintel_ee_write_82580(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len) |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 358 | { |
| 359 | if (nicintel_ee_req()) |
| 360 | return -1; |
| 361 | |
| 362 | int ret = -1; |
| 363 | if (nicintel_ee_ready()) |
| 364 | goto out; |
| 365 | |
| 366 | while (len > 0) { |
| 367 | /* WREN */ |
| 368 | nicintel_ee_bitset(EEC, EE_CS, 0); |
| 369 | nicintel_ee_bitbang(JEDEC_WREN, NULL); |
| 370 | nicintel_ee_bitset(EEC, EE_CS, 1); |
| 371 | programmer_delay(1); |
| 372 | |
| 373 | /* data */ |
| 374 | nicintel_ee_bitset(EEC, EE_CS, 0); |
| 375 | nicintel_ee_bitbang(JEDEC_BYTE_PROGRAM, NULL); |
| 376 | nicintel_ee_bitbang((addr >> 8) & 0xff, NULL); |
| 377 | nicintel_ee_bitbang(addr & 0xff, NULL); |
| 378 | while (len > 0) { |
| 379 | nicintel_ee_bitbang((buf) ? *buf++ : 0xff, NULL); |
| 380 | len--; |
| 381 | addr++; |
Stefan Tauner | 8d21ff1 | 2015-01-10 09:33:06 +0000 | [diff] [blame] | 382 | if (!(addr & EE_PAGE_MASK)) |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 383 | break; |
| 384 | } |
| 385 | nicintel_ee_bitset(EEC, EE_CS, 1); |
| 386 | programmer_delay(1); |
| 387 | if (nicintel_ee_ready()) |
| 388 | goto out; |
| 389 | } |
| 390 | ret = 0; |
| 391 | out: |
| 392 | nicintel_ee_bitset(EEC, EE_REQ, 0); /* Give up direct access. */ |
| 393 | return ret; |
| 394 | } |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 395 | static int nicintel_ee_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len) |
| 396 | { |
| 397 | if (is_i210(nicintel_pci->device_id)) |
| 398 | return nicintel_ee_write_i210(flash, buf, addr, len); |
| 399 | |
| 400 | return nicintel_ee_write_82580(flash, buf, addr, len); |
| 401 | } |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 402 | |
| 403 | static int nicintel_ee_erase(struct flashctx *flash, unsigned int addr, unsigned int len) |
| 404 | { |
| 405 | return nicintel_ee_write(flash, NULL, addr, len); |
| 406 | } |
| 407 | |
| 408 | static const struct opaque_master opaque_master_nicintel_ee = { |
| 409 | .probe = nicintel_ee_probe, |
| 410 | .read = nicintel_ee_read, |
| 411 | .write = nicintel_ee_write, |
| 412 | .erase = nicintel_ee_erase, |
| 413 | }; |
| 414 | |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 415 | static int nicintel_ee_shutdown_i210(void *arg) |
| 416 | { |
| 417 | if (!done_i20_write) |
| 418 | return 0; |
| 419 | |
| 420 | uint32_t flup = pci_mmio_readl(nicintel_eebar + EEC); |
| 421 | |
| 422 | flup |= BIT(EE_FLUPD); |
| 423 | pci_mmio_writel(flup, nicintel_eebar + EEC); |
| 424 | |
David Hendricks | 79d838d | 2017-09-27 09:25:34 -0700 | [diff] [blame] | 425 | int i; |
| 426 | for (i = 0; i < MAX_ATTEMPTS; i++) |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 427 | if (pci_mmio_readl(nicintel_eebar + EEC) & BIT(EE_FLUDONE)) |
| 428 | return 0; |
| 429 | |
| 430 | msg_perr("Flash update failed\n"); |
| 431 | |
| 432 | return -1; |
| 433 | } |
| 434 | |
Stefan Tauner | 5c316f9 | 2015-02-08 21:57:52 +0000 | [diff] [blame] | 435 | static int nicintel_ee_shutdown(void *eecp) |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 436 | { |
| 437 | uint32_t old_eec = *(uint32_t *)eecp; |
| 438 | /* Request bitbanging and unselect the chip first to be safe. */ |
| 439 | if (nicintel_ee_req() || nicintel_ee_bitset(EEC, EE_CS, 1)) |
| 440 | return -1; |
| 441 | |
| 442 | /* Try to restore individual bits we care about. */ |
| 443 | int ret = nicintel_ee_bitset(EEC, EE_SCK, old_eec & BIT(EE_SCK)); |
| 444 | ret |= nicintel_ee_bitset(EEC, EE_SI, old_eec & BIT(EE_SI)); |
| 445 | ret |= nicintel_ee_bitset(EEC, EE_CS, old_eec & BIT(EE_CS)); |
| 446 | /* REQ will be cleared by hardware anyway after 2 seconds of inactivity on the SPI pins (3.3.2.1). */ |
| 447 | ret |= nicintel_ee_bitset(EEC, EE_REQ, old_eec & BIT(EE_REQ)); |
| 448 | |
| 449 | free(eecp); |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | int nicintel_ee_init(void) |
| 454 | { |
| 455 | if (rget_io_perms()) |
| 456 | return 1; |
| 457 | |
| 458 | struct pci_dev *dev = pcidev_init(nics_intel_ee, PCI_BASE_ADDRESS_0); |
| 459 | if (!dev) |
| 460 | return 1; |
| 461 | |
| 462 | uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0); |
| 463 | if (!io_base_addr) |
| 464 | return 1; |
| 465 | |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 466 | nicintel_eebar = rphysmap("Intel Gigabit NIC w/ SPI EEPROM", |
| 467 | io_base_addr + (is_i210(dev->device_id) ? 0x12000 : 0), MEMMAP_SIZE); |
| 468 | if (!nicintel_eebar) |
| 469 | return 1; |
| 470 | |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 471 | nicintel_pci = dev; |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 472 | if ((dev->device_id != UNPROG_DEVICE) && ! is_i210(dev->device_id)) |
| 473 | { |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 474 | uint32_t eec = pci_mmio_readl(nicintel_eebar + EEC); |
| 475 | |
| 476 | /* C.f. 3.3.1.5 for the detection mechanism (maybe? contradicting the EE_PRES definition), |
| 477 | * and 3.3.1.7 for possible recovery. */ |
| 478 | if (!(eec & BIT(EE_PRES))) { |
| 479 | msg_perr("Controller reports no EEPROM is present.\n"); |
| 480 | return 1; |
| 481 | } |
| 482 | |
| 483 | uint32_t *eecp = malloc(sizeof(uint32_t)); |
| 484 | if (eecp == NULL) |
| 485 | return 1; |
| 486 | *eecp = eec; |
| 487 | |
Stefan Tauner | 5c316f9 | 2015-02-08 21:57:52 +0000 | [diff] [blame] | 488 | if (register_shutdown(nicintel_ee_shutdown, eecp)) |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 489 | return 1; |
| 490 | } |
| 491 | |
Ricardo Ribalda Delgado | 9fe1fb7 | 2017-03-23 15:11:22 +0100 | [diff] [blame] | 492 | if (is_i210(dev->device_id)) |
| 493 | if (register_shutdown(nicintel_ee_shutdown_i210, NULL)) |
| 494 | return 1; |
| 495 | |
Ricardo Ribalda Delgado | 2a41f0a | 2014-07-28 20:35:21 +0000 | [diff] [blame] | 496 | return register_opaque_master(&opaque_master_nicintel_ee); |
| 497 | } |