blob: 662707c8d80d1a8834ab10b0c938816d8e321bef [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
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000072#define BIT(x) (1<<x)
Stefan Tauner8d21ff12015-01-10 09:33:06 +000073#define EE_PAGE_MASK 0x3f
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000074
75static uint8_t *nicintel_eebar;
76static struct pci_dev *nicintel_pci;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010077static bool done_i20_write = false;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000078
79#define UNPROG_DEVICE 0x1509
80
Nico Huber4343e7d2017-10-10 17:38:07 +020081/*
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 Heijligencc853d82021-05-04 15:32:17 +020085static const struct dev_entry nics_intel_ee[] = {
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000086 {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 Pons771bb792021-05-02 15:09:20 +020092 {PCI_VENDOR_ID_INTEL, 0x1531, OK, "Intel", "I210 Gigabit Network Connection Unprogrammed"},
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010093 {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 Delgado2a41f0a2014-07-28 20:35:21 +000099 {0},
100};
101
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100102static inline bool is_i210(uint16_t device_id)
103{
Nico Huber4343e7d2017-10-10 17:38:07 +0200104 return (device_id & 0xfff0) == 0x1530;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100105}
106
107static 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
120static int nicintel_ee_probe_82580(struct flashctx *flash)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000121{
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 Tauner8d21ff12015-01-10 09:33:06 +0000140 flash->chip->page_size = EE_PAGE_MASK + 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000141 flash->chip->tested = TEST_OK_PREW;
142 flash->chip->gran = write_gran_1byte_implicit_erase;
Stefan Tauner8d21ff12015-01-10 09:33:06 +0000143 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 Delgado2a41f0a2014-07-28 20:35:21 +0000145
146 return 1;
147}
148
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100149#define MAX_ATTEMPTS 10000000
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000150static int nicintel_ee_read_word(unsigned int addr, uint16_t *data)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000151{
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 Delgado9fe1fb72017-03-23 15:11:22 +0100157 for (i = 0; i < MAX_ATTEMPTS; i++) {
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000158 tmp = pci_mmio_readl(nicintel_eebar + EERD);
159 if (tmp & BIT(EERD_DONE)) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000160 *data = (tmp >> EERD_DATA) & 0xffff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000161 return 0;
162 }
163 }
164
165 return -1;
166}
167
168static int nicintel_ee_read(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len)
169{
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000170 uint16_t data;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000171
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 Tauner3e6b7bb2015-01-25 23:45:14 +0000175 if (nicintel_ee_read_word(addr / 2, &data))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000176 return -1;
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000177 *buf++ = data & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000178 addr++;
179 len--;
180 }
181
182 while (len > 0) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000183 if (nicintel_ee_read_word(addr / 2, &data))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000184 return -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) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000189 *buf++ = (data >> 8) & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000190 addr++;
191 len--;
192 }
193 }
194
195 return 0;
196}
197
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100198static 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 Hendricks79d838d2017-09-27 09:25:34 -0700208 int i;
209 for (i = 0; i < MAX_ATTEMPTS; i++)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100210 if (pci_mmio_readl(nicintel_eebar + EEWR) & BIT(EEWR_DONE))
211 return 0;
212 return -1;
213}
214
Nico Huber4343e7d2017-10-10 17:38:07 +0200215static int nicintel_ee_write_i210(struct flashctx *flash, const uint8_t *buf,
216 unsigned int addr, unsigned int len)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100217{
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 Huber89622672017-10-10 18:05:55 +0200277static 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 Delgado2a41f0a2014-07-28 20:35:21 +0000282static 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). */
297static 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. */
320static 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. */
340static 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 Delgado9fe1fb72017-03-23 15:11:22 +0100355static 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 +0000356{
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 Tauner8d21ff12015-01-10 09:33:06 +0000380 if (!(addr & EE_PAGE_MASK))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000381 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;
389out:
390 nicintel_ee_bitset(EEC, EE_REQ, 0); /* Give up direct access. */
391 return ret;
392}
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100393
Nico Huber89622672017-10-10 18:05:55 +0200394static 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 Delgado9fe1fb72017-03-23 15:11:22 +0100397}
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000398
Nico Huber72c02ff2023-01-08 02:00:06 +0100399static int nicintel_ee_shutdown_82580(void *eecp);
400
Nico Huber89622672017-10-10 18:05:55 +0200401static const struct opaque_master opaque_master_nicintel_ee_82580 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200402 .probe = nicintel_ee_probe_82580,
403 .read = nicintel_ee_read,
404 .write = nicintel_ee_write_82580,
405 .erase = nicintel_ee_erase_82580,
Nico Huber72c02ff2023-01-08 02:00:06 +0100406 .shutdown = nicintel_ee_shutdown_82580,
Nico Huber89622672017-10-10 18:05:55 +0200407};
408
Nico Huber72c02ff2023-01-08 02:00:06 +0100409static int nicintel_ee_shutdown_i210(void *arg);
410
Nico Huber89622672017-10-10 18:05:55 +0200411static const struct opaque_master opaque_master_nicintel_ee_i210 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200412 .probe = nicintel_ee_probe_i210,
413 .read = nicintel_ee_read,
414 .write = nicintel_ee_write_i210,
415 .erase = nicintel_ee_erase_i210,
Nico Huber72c02ff2023-01-08 02:00:06 +0100416 .shutdown = nicintel_ee_shutdown_i210,
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000417};
418
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100419static int nicintel_ee_shutdown_i210(void *arg)
420{
Alexander Goncharov316ef012022-08-07 12:08:49 +0300421 int ret = 0;
422
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100423 if (!done_i20_write)
Alexander Goncharov316ef012022-08-07 12:08:49 +0300424 goto out;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100425
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 Hendricks79d838d2017-09-27 09:25:34 -0700431 int i;
432 for (i = 0; i < MAX_ATTEMPTS; i++)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100433 if (pci_mmio_readl(nicintel_eebar + EEC) & BIT(EE_FLUDONE))
Alexander Goncharov316ef012022-08-07 12:08:49 +0300434 goto out;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100435
Alexander Goncharov316ef012022-08-07 12:08:49 +0300436 ret = -1;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100437 msg_perr("Flash update failed\n");
438
Alexander Goncharov316ef012022-08-07 12:08:49 +0300439out:
440 return ret;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100441}
442
Nico Huber89622672017-10-10 18:05:55 +0200443static int nicintel_ee_shutdown_82580(void *eecp)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000444{
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000445 int ret = 0;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000446
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000447 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 Delgado2a41f0a2014-07-28 20:35:21 +0000454
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000455 /* 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
464out:
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000465 free(eecp);
466 return ret;
467}
468
Thomas Heijligencc853d82021-05-04 15:32:17 +0200469static int nicintel_ee_init(void)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000470{
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000471 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 Huber89622672017-10-10 18:05:55 +0200479 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 Delgado2a41f0a2014-07-28 20:35:21 +0000482 return 1;
Nico Huber89622672017-10-10 18:05:55 +0200483
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000484 uint32_t *eecp = NULL;
485
Nico Huber89622672017-10-10 18:05:55 +0200486 nicintel_pci = dev;
Anastasia Klimchuk27fdfd72021-08-03 10:41:50 +1000487 if (dev->device_id != UNPROG_DEVICE) {
Nico Huber89622672017-10-10 18:05:55 +0200488 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 Klimchuk7a7b25d2021-08-03 11:54:56 +1000498 eecp = malloc(sizeof(uint32_t));
Nico Huber89622672017-10-10 18:05:55 +0200499 if (eecp == NULL)
500 return 1;
501 *eecp = eec;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000502 }
503
Nico Huber72c02ff2023-01-08 02:00:06 +0100504 return register_opaque_master(&opaque_master_nicintel_ee_82580, eecp);
Nico Huber89622672017-10-10 18:05:55 +0200505 } else {
506 nicintel_eebar = rphysmap("Intel i210 NIC w/ emulated EEPROM",
507 io_base_addr + 0x12000, MEMMAP_SIZE);
508 if (!nicintel_eebar)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000509 return 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000510
Anastasia Klimchuk21b20212021-05-13 12:28:47 +1000511 return register_opaque_master(&opaque_master_nicintel_ee_i210, NULL);
Nico Huber89622672017-10-10 18:05:55 +0200512 }
513
514 return 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000515}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200516
517const 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};