blob: 2839e7b38ccd525185c4e169fb826744c8865e1f [file] [log] [blame]
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +00001/*
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 Delgado2a41f0a2014-07-28 20:35:21 +000017 */
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 Delgado9fe1fb72017-03-23 15:11:22 +010025/*
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 Delgado2a41f0a2014-07-28 20:35:21 +000031
32#include <stdlib.h>
33#include <unistd.h>
34#include "flash.h"
35#include "spi.h"
36#include "programmer.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010037#include "hwaccess_physmap.h"
Thomas Heijligend96c97c2021-11-02 21:03:00 +010038#include "platform/pci.h"
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000039
40#define PCI_VENDOR_ID_INTEL 0x8086
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010041#define MEMMAP_SIZE 0x1c /* Only EEC, EERD and EEWR are needed. */
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000042
43#define EEC 0x10 /* EEPROM/Flash Control Register */
44#define EERD 0x14 /* EEPROM Read Register */
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010045#define EEWR 0x18 /* EEPROM Write Register */
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000046
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 Delgado9fe1fb72017-03-23 15:11:22 +010057#define EE_FLUPD 23
58#define EE_FLUDONE 26
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000059
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 Delgado9fe1fb72017-03-23 15:11:22 +010066/* 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
Stefan Tauner8d21ff12015-01-10 09:33:06 +000072#define EE_PAGE_MASK 0x3f
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000073
74static uint8_t *nicintel_eebar;
75static struct pci_dev *nicintel_pci;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010076static bool done_i20_write = false;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000077
78#define UNPROG_DEVICE 0x1509
79
Nico Huber4343e7d2017-10-10 17:38:07 +020080/*
81 * Warning: is_i210() below makes assumptions on these PCI ids.
82 * It may have to be updated when this list is extended.
83 */
Thomas Heijligencc853d82021-05-04 15:32:17 +020084static const struct dev_entry nics_intel_ee[] = {
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000085 {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"},
Angel Pons771bb792021-05-02 15:09:20 +020091 {PCI_VENDOR_ID_INTEL, 0x1531, OK, "Intel", "I210 Gigabit Network Connection Unprogrammed"},
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010092 {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 Delgado2a41f0a2014-07-28 20:35:21 +000098 {0},
99};
100
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100101static inline bool is_i210(uint16_t device_id)
102{
Nico Huber4343e7d2017-10-10 17:38:07 +0200103 return (device_id & 0xfff0) == 0x1530;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100104}
105
106static 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
119static int nicintel_ee_probe_82580(struct flashctx *flash)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000120{
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 Tauner8d21ff12015-01-10 09:33:06 +0000139 flash->chip->page_size = EE_PAGE_MASK + 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000140 flash->chip->tested = TEST_OK_PREW;
141 flash->chip->gran = write_gran_1byte_implicit_erase;
Stefan Tauner8d21ff12015-01-10 09:33:06 +0000142 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 Delgado2a41f0a2014-07-28 20:35:21 +0000144
145 return 1;
146}
147
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100148#define MAX_ATTEMPTS 10000000
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000149static int nicintel_ee_read_word(unsigned int addr, uint16_t *data)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000150{
151 uint32_t tmp = BIT(EERD_START) | (addr << EERD_ADDR);
152 pci_mmio_writel(tmp, nicintel_eebar + EERD);
153
154 /* Poll done flag. 10.000.000 cycles seem to be enough. */
155 uint32_t i;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100156 for (i = 0; i < MAX_ATTEMPTS; i++) {
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000157 tmp = pci_mmio_readl(nicintel_eebar + EERD);
158 if (tmp & BIT(EERD_DONE)) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000159 *data = (tmp >> EERD_DATA) & 0xffff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000160 return 0;
161 }
162 }
163
164 return -1;
165}
166
167static int nicintel_ee_read(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len)
168{
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000169 uint16_t data;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000170
171 /* The NIC interface always reads 16 b words so we need to convert the address and handle odd address
172 * explicitly at the start (and also at the end in the loop below). */
173 if (addr & 1) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000174 if (nicintel_ee_read_word(addr / 2, &data))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000175 return -1;
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000176 *buf++ = data & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000177 addr++;
178 len--;
179 }
180
181 while (len > 0) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000182 if (nicintel_ee_read_word(addr / 2, &data))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000183 return -1;
Richard Hughes842d6782021-01-15 09:48:12 +0000184 flashprog_progress_add(flash, 1);
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000185 *buf++ = data & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000186 addr++;
187 len--;
188 if (len > 0) {
Richard Hughes842d6782021-01-15 09:48:12 +0000189 flashprog_progress_add(flash, 1);
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000190 *buf++ = (data >> 8) & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000191 addr++;
192 len--;
193 }
194 }
195
196 return 0;
197}
198
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100199static 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 Hendricks79d838d2017-09-27 09:25:34 -0700209 int i;
210 for (i = 0; i < MAX_ATTEMPTS; i++)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100211 if (pci_mmio_readl(nicintel_eebar + EEWR) & BIT(EEWR_DONE))
212 return 0;
213 return -1;
214}
215
Nico Huber4343e7d2017-10-10 17:38:07 +0200216static int nicintel_ee_write_i210(struct flashctx *flash, const uint8_t *buf,
217 unsigned int addr, unsigned int len)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100218{
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
Richard Hughes842d6782021-01-15 09:48:12 +0000237 if (buf) {
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100238 buf ++;
Richard Hughes842d6782021-01-15 09:48:12 +0000239 flashprog_progress_add(flash, 1);
240 }
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100241 addr ++;
242 len --;
243 }
244
245 while (len > 0) {
246 uint16_t data;
247
248 if (len == 1) {
249 if (nicintel_ee_read_word(addr / 2, &data)) {
250 msg_perr("Timeout reading tail byte\n");
251 return -1;
252 }
253
254 data &= 0xff00;
255 data |= buf ? (buf[0]) : 0xff;
256 } else {
257 if (buf)
258 data = buf[0] | (buf[1] << 8);
259 else
260 data = 0xffff;
261 }
262
263 if (nicintel_ee_write_word_i210(addr / 2, data)) {
264 msg_perr("Timeout writing Shadow RAM\n");
265 return -1;
266 }
267
Richard Hughes842d6782021-01-15 09:48:12 +0000268 if (buf) {
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100269 buf += 2;
Richard Hughes842d6782021-01-15 09:48:12 +0000270 flashprog_progress_add(flash, min(len, 2));
271 }
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100272 if (len > 2)
273 len -= 2;
274 else
275 len = 0;
276 addr += 2;
277 }
278
279 return 0;
280}
281
Nico Huber89622672017-10-10 18:05:55 +0200282static int nicintel_ee_erase_i210(struct flashctx *flash, unsigned int addr, unsigned int len)
283{
284 return nicintel_ee_write_i210(flash, NULL, addr, len);
285}
286
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000287static int nicintel_ee_bitset(int reg, int bit, bool val)
288{
289 uint32_t tmp;
290
291 tmp = pci_mmio_readl(nicintel_eebar + reg);
292 if (val)
293 tmp |= BIT(bit);
294 else
295 tmp &= ~BIT(bit);
296 pci_mmio_writel(tmp, nicintel_eebar + reg);
297
298 return -1;
299}
300
301/* Shifts one byte out while receiving another one by bitbanging (denoted "direct access" in the datasheet). */
302static int nicintel_ee_bitbang(uint8_t mosi, uint8_t *miso)
303{
304 uint8_t out = 0x0;
305
306 int i;
307 for (i = 7; i >= 0; i--) {
308 nicintel_ee_bitset(EEC, EE_SI, mosi & BIT(i));
309 nicintel_ee_bitset(EEC, EE_SCK, 1);
310 if (miso != NULL) {
311 uint32_t tmp = pci_mmio_readl(nicintel_eebar + EEC);
312 if (tmp & BIT(EE_SO))
313 out |= BIT(i);
314 }
315 nicintel_ee_bitset(EEC, EE_SCK, 0);
316 }
317
318 if (miso != NULL)
319 *miso = out;
320
321 return 0;
322}
323
324/* Polls the WIP bit of the status register of the attached EEPROM via bitbanging. */
325static int nicintel_ee_ready(void)
326{
327 unsigned int i;
328 for (i = 0; i < 1000; i++) {
329 nicintel_ee_bitset(EEC, EE_CS, 0);
330
331 nicintel_ee_bitbang(JEDEC_RDSR, NULL);
332 uint8_t rdsr;
333 nicintel_ee_bitbang(0x00, &rdsr);
334
335 nicintel_ee_bitset(EEC, EE_CS, 1);
336 programmer_delay(1);
337 if (!(rdsr & SPI_SR_WIP)) {
338 return 0;
339 }
340 }
341 return -1;
342}
343
344/* Requests direct access to the SPI pins. */
345static int nicintel_ee_req(void)
346{
347 uint32_t tmp;
348 nicintel_ee_bitset(EEC, EE_REQ, 1);
349
350 tmp = pci_mmio_readl(nicintel_eebar + EEC);
351 if (!(tmp & BIT(EE_GNT))) {
352 msg_perr("Enabling eeprom access failed.\n");
353 return 1;
354 }
355
356 nicintel_ee_bitset(EEC, EE_SCK, 0);
357 return 0;
358}
359
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100360static int nicintel_ee_write_82580(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000361{
362 if (nicintel_ee_req())
363 return -1;
364
365 int ret = -1;
366 if (nicintel_ee_ready())
367 goto out;
368
369 while (len > 0) {
370 /* WREN */
371 nicintel_ee_bitset(EEC, EE_CS, 0);
372 nicintel_ee_bitbang(JEDEC_WREN, NULL);
373 nicintel_ee_bitset(EEC, EE_CS, 1);
374 programmer_delay(1);
375
376 /* data */
377 nicintel_ee_bitset(EEC, EE_CS, 0);
378 nicintel_ee_bitbang(JEDEC_BYTE_PROGRAM, NULL);
379 nicintel_ee_bitbang((addr >> 8) & 0xff, NULL);
380 nicintel_ee_bitbang(addr & 0xff, NULL);
381 while (len > 0) {
382 nicintel_ee_bitbang((buf) ? *buf++ : 0xff, NULL);
383 len--;
384 addr++;
Richard Hughes842d6782021-01-15 09:48:12 +0000385 if (buf)
386 flashprog_progress_add(flash, 1);
Stefan Tauner8d21ff12015-01-10 09:33:06 +0000387 if (!(addr & EE_PAGE_MASK))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000388 break;
389 }
390 nicintel_ee_bitset(EEC, EE_CS, 1);
391 programmer_delay(1);
392 if (nicintel_ee_ready())
393 goto out;
394 }
395 ret = 0;
396out:
397 nicintel_ee_bitset(EEC, EE_REQ, 0); /* Give up direct access. */
398 return ret;
399}
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100400
Nico Huber89622672017-10-10 18:05:55 +0200401static int nicintel_ee_erase_82580(struct flashctx *flash, unsigned int addr, unsigned int len)
402{
403 return nicintel_ee_write_82580(flash, NULL, addr, len);
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100404}
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000405
Nico Huber72c02ff2023-01-08 02:00:06 +0100406static int nicintel_ee_shutdown_82580(void *eecp);
407
Nico Huber89622672017-10-10 18:05:55 +0200408static const struct opaque_master opaque_master_nicintel_ee_82580 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200409 .probe = nicintel_ee_probe_82580,
410 .read = nicintel_ee_read,
411 .write = nicintel_ee_write_82580,
412 .erase = nicintel_ee_erase_82580,
Nico Huber72c02ff2023-01-08 02:00:06 +0100413 .shutdown = nicintel_ee_shutdown_82580,
Nico Huber89622672017-10-10 18:05:55 +0200414};
415
Nico Huber72c02ff2023-01-08 02:00:06 +0100416static int nicintel_ee_shutdown_i210(void *arg);
417
Nico Huber89622672017-10-10 18:05:55 +0200418static const struct opaque_master opaque_master_nicintel_ee_i210 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200419 .probe = nicintel_ee_probe_i210,
420 .read = nicintel_ee_read,
421 .write = nicintel_ee_write_i210,
422 .erase = nicintel_ee_erase_i210,
Nico Huber72c02ff2023-01-08 02:00:06 +0100423 .shutdown = nicintel_ee_shutdown_i210,
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000424};
425
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100426static int nicintel_ee_shutdown_i210(void *arg)
427{
Alexander Goncharov316ef012022-08-07 12:08:49 +0300428 int ret = 0;
429
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100430 if (!done_i20_write)
Alexander Goncharov316ef012022-08-07 12:08:49 +0300431 goto out;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100432
433 uint32_t flup = pci_mmio_readl(nicintel_eebar + EEC);
434
435 flup |= BIT(EE_FLUPD);
436 pci_mmio_writel(flup, nicintel_eebar + EEC);
437
David Hendricks79d838d2017-09-27 09:25:34 -0700438 int i;
439 for (i = 0; i < MAX_ATTEMPTS; i++)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100440 if (pci_mmio_readl(nicintel_eebar + EEC) & BIT(EE_FLUDONE))
Alexander Goncharov316ef012022-08-07 12:08:49 +0300441 goto out;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100442
Alexander Goncharov316ef012022-08-07 12:08:49 +0300443 ret = -1;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100444 msg_perr("Flash update failed\n");
445
Alexander Goncharov316ef012022-08-07 12:08:49 +0300446out:
447 return ret;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100448}
449
Nico Huber89622672017-10-10 18:05:55 +0200450static int nicintel_ee_shutdown_82580(void *eecp)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000451{
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000452 int ret = 0;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000453
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000454 if (nicintel_pci->device_id != UNPROG_DEVICE) {
455 uint32_t old_eec = *(uint32_t *)eecp;
456 /* Request bitbanging and unselect the chip first to be safe. */
457 if (nicintel_ee_req() || nicintel_ee_bitset(EEC, EE_CS, 1)) {
458 ret = -1;
459 goto out;
460 }
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000461
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000462 /* Try to restore individual bits we care about. */
463 ret = nicintel_ee_bitset(EEC, EE_SCK, old_eec & BIT(EE_SCK));
464 ret |= nicintel_ee_bitset(EEC, EE_SI, old_eec & BIT(EE_SI));
465 ret |= nicintel_ee_bitset(EEC, EE_CS, old_eec & BIT(EE_CS));
466 /* REQ will be cleared by hardware anyway after 2 seconds of inactivity
467 * on the SPI pins (3.3.2.1). */
468 ret |= nicintel_ee_bitset(EEC, EE_REQ, old_eec & BIT(EE_REQ));
469 }
470
471out:
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000472 free(eecp);
473 return ret;
474}
475
Nico Hubere3a26882023-01-11 21:45:51 +0100476static int nicintel_ee_init(struct flashprog_programmer *const prog)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000477{
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000478 struct pci_dev *dev = pcidev_init(nics_intel_ee, PCI_BASE_ADDRESS_0);
479 if (!dev)
480 return 1;
481
482 uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
483 if (!io_base_addr)
484 return 1;
485
Nico Huber89622672017-10-10 18:05:55 +0200486 if (!is_i210(dev->device_id)) {
487 nicintel_eebar = rphysmap("Intel Gigabit NIC w/ SPI EEPROM", io_base_addr, MEMMAP_SIZE);
488 if (!nicintel_eebar)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000489 return 1;
Nico Huber89622672017-10-10 18:05:55 +0200490
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000491 uint32_t *eecp = NULL;
492
Nico Huber89622672017-10-10 18:05:55 +0200493 nicintel_pci = dev;
Anastasia Klimchuk27fdfd72021-08-03 10:41:50 +1000494 if (dev->device_id != UNPROG_DEVICE) {
Nico Huber89622672017-10-10 18:05:55 +0200495 uint32_t eec = pci_mmio_readl(nicintel_eebar + EEC);
496
497 /* C.f. 3.3.1.5 for the detection mechanism (maybe? contradicting
498 the EE_PRES definition),
499 and 3.3.1.7 for possible recovery. */
500 if (!(eec & BIT(EE_PRES))) {
501 msg_perr("Controller reports no EEPROM is present.\n");
502 return 1;
503 }
504
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000505 eecp = malloc(sizeof(uint32_t));
Nico Huber89622672017-10-10 18:05:55 +0200506 if (eecp == NULL)
507 return 1;
508 *eecp = eec;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000509 }
510
Nico Huber72c02ff2023-01-08 02:00:06 +0100511 return register_opaque_master(&opaque_master_nicintel_ee_82580, eecp);
Nico Huber89622672017-10-10 18:05:55 +0200512 } else {
513 nicintel_eebar = rphysmap("Intel i210 NIC w/ emulated EEPROM",
514 io_base_addr + 0x12000, MEMMAP_SIZE);
515 if (!nicintel_eebar)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000516 return 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000517
Anastasia Klimchuk21b20212021-05-13 12:28:47 +1000518 return register_opaque_master(&opaque_master_nicintel_ee_i210, NULL);
Nico Huber89622672017-10-10 18:05:55 +0200519 }
520
521 return 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000522}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200523
524const struct programmer_entry programmer_nicintel_eeprom = {
525 .name = "nicintel_eeprom",
526 .type = PCI,
527 .devs.dev = nics_intel_ee,
528 .init = nicintel_ee_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +0200529};