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