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