blob: 91666052e0c377160058976df4f80c223563aeb8 [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"
37#include "hwaccess.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010038#include "hwaccess_physmap.h"
Thomas Heijligend96c97c2021-11-02 21:03:00 +010039#include "platform/pci.h"
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000040
41#define PCI_VENDOR_ID_INTEL 0x8086
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010042#define MEMMAP_SIZE 0x1c /* Only EEC, EERD and EEWR are needed. */
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000043
44#define EEC 0x10 /* EEPROM/Flash Control Register */
45#define EERD 0x14 /* EEPROM Read Register */
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010046#define EEWR 0x18 /* EEPROM Write Register */
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000047
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 Delgado9fe1fb72017-03-23 15:11:22 +010058#define EE_FLUPD 23
59#define EE_FLUDONE 26
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000060
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 Delgado9fe1fb72017-03-23 15:11:22 +010067/* 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 Delgado2a41f0a2014-07-28 20:35:21 +000073#define BIT(x) (1<<x)
Stefan Tauner8d21ff12015-01-10 09:33:06 +000074#define EE_PAGE_MASK 0x3f
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000075
76static uint8_t *nicintel_eebar;
77static struct pci_dev *nicintel_pci;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010078static bool done_i20_write = false;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000079
80#define UNPROG_DEVICE 0x1509
81
Nico Huber4343e7d2017-10-10 17:38:07 +020082/*
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 Heijligencc853d82021-05-04 15:32:17 +020086static const struct dev_entry nics_intel_ee[] = {
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000087 {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 Pons771bb792021-05-02 15:09:20 +020093 {PCI_VENDOR_ID_INTEL, 0x1531, OK, "Intel", "I210 Gigabit Network Connection Unprogrammed"},
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010094 {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 Delgado2a41f0a2014-07-28 20:35:21 +0000100 {0},
101};
102
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100103static inline bool is_i210(uint16_t device_id)
104{
Nico Huber4343e7d2017-10-10 17:38:07 +0200105 return (device_id & 0xfff0) == 0x1530;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100106}
107
108static 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
121static int nicintel_ee_probe_82580(struct flashctx *flash)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000122{
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 Tauner8d21ff12015-01-10 09:33:06 +0000141 flash->chip->page_size = EE_PAGE_MASK + 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000142 flash->chip->tested = TEST_OK_PREW;
143 flash->chip->gran = write_gran_1byte_implicit_erase;
Stefan Tauner8d21ff12015-01-10 09:33:06 +0000144 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 Delgado2a41f0a2014-07-28 20:35:21 +0000146
147 return 1;
148}
149
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100150#define MAX_ATTEMPTS 10000000
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000151static int nicintel_ee_read_word(unsigned int addr, uint16_t *data)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000152{
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 Delgado9fe1fb72017-03-23 15:11:22 +0100158 for (i = 0; i < MAX_ATTEMPTS; i++) {
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000159 tmp = pci_mmio_readl(nicintel_eebar + EERD);
160 if (tmp & BIT(EERD_DONE)) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000161 *data = (tmp >> EERD_DATA) & 0xffff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000162 return 0;
163 }
164 }
165
166 return -1;
167}
168
169static int nicintel_ee_read(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len)
170{
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000171 uint16_t data;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000172
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 Tauner3e6b7bb2015-01-25 23:45:14 +0000176 if (nicintel_ee_read_word(addr / 2, &data))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000177 return -1;
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000178 *buf++ = data & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000179 addr++;
180 len--;
181 }
182
183 while (len > 0) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000184 if (nicintel_ee_read_word(addr / 2, &data))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000185 return -1;
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000186 *buf++ = data & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000187 addr++;
188 len--;
189 if (len > 0) {
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
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 Huber89622672017-10-10 18:05:55 +0200278static 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 Delgado2a41f0a2014-07-28 20:35:21 +0000283static 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). */
298static 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. */
321static 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. */
341static 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 Delgado9fe1fb72017-03-23 15:11:22 +0100356static 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 +0000357{
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 Tauner8d21ff12015-01-10 09:33:06 +0000381 if (!(addr & EE_PAGE_MASK))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000382 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;
390out:
391 nicintel_ee_bitset(EEC, EE_REQ, 0); /* Give up direct access. */
392 return ret;
393}
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100394
Nico Huber89622672017-10-10 18:05:55 +0200395static 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 Delgado9fe1fb72017-03-23 15:11:22 +0100398}
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000399
Nico Huber72c02ff2023-01-08 02:00:06 +0100400static int nicintel_ee_shutdown_82580(void *eecp);
401
Nico Huber89622672017-10-10 18:05:55 +0200402static const struct opaque_master opaque_master_nicintel_ee_82580 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200403 .probe = nicintel_ee_probe_82580,
404 .read = nicintel_ee_read,
405 .write = nicintel_ee_write_82580,
406 .erase = nicintel_ee_erase_82580,
Nico Huber72c02ff2023-01-08 02:00:06 +0100407 .shutdown = nicintel_ee_shutdown_82580,
Nico Huber89622672017-10-10 18:05:55 +0200408};
409
Nico Huber72c02ff2023-01-08 02:00:06 +0100410static int nicintel_ee_shutdown_i210(void *arg);
411
Nico Huber89622672017-10-10 18:05:55 +0200412static const struct opaque_master opaque_master_nicintel_ee_i210 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200413 .probe = nicintel_ee_probe_i210,
414 .read = nicintel_ee_read,
415 .write = nicintel_ee_write_i210,
416 .erase = nicintel_ee_erase_i210,
Nico Huber72c02ff2023-01-08 02:00:06 +0100417 .shutdown = nicintel_ee_shutdown_i210,
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000418};
419
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100420static int nicintel_ee_shutdown_i210(void *arg)
421{
Alexander Goncharov316ef012022-08-07 12:08:49 +0300422 int ret = 0;
423
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100424 if (!done_i20_write)
Alexander Goncharov316ef012022-08-07 12:08:49 +0300425 goto out;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100426
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 Hendricks79d838d2017-09-27 09:25:34 -0700432 int i;
433 for (i = 0; i < MAX_ATTEMPTS; i++)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100434 if (pci_mmio_readl(nicintel_eebar + EEC) & BIT(EE_FLUDONE))
Alexander Goncharov316ef012022-08-07 12:08:49 +0300435 goto out;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100436
Alexander Goncharov316ef012022-08-07 12:08:49 +0300437 ret = -1;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100438 msg_perr("Flash update failed\n");
439
Alexander Goncharov316ef012022-08-07 12:08:49 +0300440out:
441 return ret;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100442}
443
Nico Huber89622672017-10-10 18:05:55 +0200444static int nicintel_ee_shutdown_82580(void *eecp)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000445{
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000446 int ret = 0;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000447
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000448 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 Delgado2a41f0a2014-07-28 20:35:21 +0000455
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000456 /* 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
465out:
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000466 free(eecp);
467 return ret;
468}
469
Thomas Heijligencc853d82021-05-04 15:32:17 +0200470static int nicintel_ee_init(void)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000471{
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000472 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 Huber89622672017-10-10 18:05:55 +0200480 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 Delgado2a41f0a2014-07-28 20:35:21 +0000483 return 1;
Nico Huber89622672017-10-10 18:05:55 +0200484
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000485 uint32_t *eecp = NULL;
486
Nico Huber89622672017-10-10 18:05:55 +0200487 nicintel_pci = dev;
Anastasia Klimchuk27fdfd72021-08-03 10:41:50 +1000488 if (dev->device_id != UNPROG_DEVICE) {
Nico Huber89622672017-10-10 18:05:55 +0200489 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 Klimchuk7a7b25d2021-08-03 11:54:56 +1000499 eecp = malloc(sizeof(uint32_t));
Nico Huber89622672017-10-10 18:05:55 +0200500 if (eecp == NULL)
501 return 1;
502 *eecp = eec;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000503 }
504
Nico Huber72c02ff2023-01-08 02:00:06 +0100505 return register_opaque_master(&opaque_master_nicintel_ee_82580, eecp);
Nico Huber89622672017-10-10 18:05:55 +0200506 } else {
507 nicintel_eebar = rphysmap("Intel i210 NIC w/ emulated EEPROM",
508 io_base_addr + 0x12000, MEMMAP_SIZE);
509 if (!nicintel_eebar)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000510 return 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000511
Anastasia Klimchuk21b20212021-05-13 12:28:47 +1000512 return register_opaque_master(&opaque_master_nicintel_ee_i210, NULL);
Nico Huber89622672017-10-10 18:05:55 +0200513 }
514
515 return 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000516}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200517
518const 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};