blob: 9f69ece79616203b6bdc6088e9abf7f416fe3f30 [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 Heijligena0655202021-12-14 16:36:05 +010038#include "hwaccess_x86_io.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010039#include "hwaccess_physmap.h"
Thomas Heijligend96c97c2021-11-02 21:03:00 +010040#include "platform/pci.h"
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000041
42#define PCI_VENDOR_ID_INTEL 0x8086
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010043#define MEMMAP_SIZE 0x1c /* Only EEC, EERD and EEWR are needed. */
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000044
45#define EEC 0x10 /* EEPROM/Flash Control Register */
46#define EERD 0x14 /* EEPROM Read Register */
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010047#define EEWR 0x18 /* EEPROM Write Register */
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000048
49/* EPROM/Flash Control Register bits */
50#define EE_SCK 0
51#define EE_CS 1
52#define EE_SI 2
53#define EE_SO 3
54#define EE_REQ 6
55#define EE_GNT 7
56#define EE_PRES 8
57#define EE_SIZE 11
58#define EE_SIZE_MASK 0xf
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010059#define EE_FLUPD 23
60#define EE_FLUDONE 26
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000061
62/* EEPROM Read Register bits */
63#define EERD_START 0
64#define EERD_DONE 1
65#define EERD_ADDR 2
66#define EERD_DATA 16
67
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010068/* EEPROM Write Register bits */
69#define EEWR_CMDV 0
70#define EEWR_DONE 1
71#define EEWR_ADDR 2
72#define EEWR_DATA 16
73
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000074#define BIT(x) (1<<x)
Stefan Tauner8d21ff12015-01-10 09:33:06 +000075#define EE_PAGE_MASK 0x3f
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000076
77static uint8_t *nicintel_eebar;
78static struct pci_dev *nicintel_pci;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010079static bool done_i20_write = false;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000080
81#define UNPROG_DEVICE 0x1509
82
Nico Huber4343e7d2017-10-10 17:38:07 +020083/*
84 * Warning: is_i210() below makes assumptions on these PCI ids.
85 * It may have to be updated when this list is extended.
86 */
Thomas Heijligencc853d82021-05-04 15:32:17 +020087static const struct dev_entry nics_intel_ee[] = {
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000088 {PCI_VENDOR_ID_INTEL, 0x150e, OK, "Intel", "82580 Quad Gigabit Ethernet Controller (Copper)"},
89 {PCI_VENDOR_ID_INTEL, 0x150f, NT , "Intel", "82580 Quad Gigabit Ethernet Controller (Fiber)"},
90 {PCI_VENDOR_ID_INTEL, 0x1510, NT , "Intel", "82580 Quad Gigabit Ethernet Controller (Backplane)"},
91 {PCI_VENDOR_ID_INTEL, 0x1511, NT , "Intel", "82580 Quad Gigabit Ethernet Controller (Ext. PHY)"},
92 {PCI_VENDOR_ID_INTEL, 0x1511, NT , "Intel", "82580 Dual Gigabit Ethernet Controller (Copper)"},
93 {PCI_VENDOR_ID_INTEL, UNPROG_DEVICE, OK, "Intel", "Unprogrammed 82580 Quad/Dual Gigabit Ethernet Controller"},
Angel Pons771bb792021-05-02 15:09:20 +020094 {PCI_VENDOR_ID_INTEL, 0x1531, OK, "Intel", "I210 Gigabit Network Connection Unprogrammed"},
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010095 {PCI_VENDOR_ID_INTEL, 0x1532, NT, "Intel", "I211 Gigabit Network Connection Unprogrammed"},
96 {PCI_VENDOR_ID_INTEL, 0x1533, OK, "Intel", "I210 Gigabit Network Connection"},
97 {PCI_VENDOR_ID_INTEL, 0x1536, NT, "Intel", "I210 Gigabit Network Connection SERDES Fiber"},
98 {PCI_VENDOR_ID_INTEL, 0x1537, NT, "Intel", "I210 Gigabit Network Connection SERDES Backplane"},
99 {PCI_VENDOR_ID_INTEL, 0x1538, NT, "Intel", "I210 Gigabit Network Connection SGMII"},
100 {PCI_VENDOR_ID_INTEL, 0x1539, NT, "Intel", "I211 Gigabit Network Connection"},
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000101 {0},
102};
103
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100104static inline bool is_i210(uint16_t device_id)
105{
Nico Huber4343e7d2017-10-10 17:38:07 +0200106 return (device_id & 0xfff0) == 0x1530;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100107}
108
109static int nicintel_ee_probe_i210(struct flashctx *flash)
110{
111 /* Emulated eeprom has a fixed size of 4 KB */
112 flash->chip->total_size = 4;
113 flash->chip->page_size = flash->chip->total_size * 1024;
114 flash->chip->tested = TEST_OK_PREW;
115 flash->chip->gran = write_gran_1byte_implicit_erase;
116 flash->chip->block_erasers->eraseblocks[0].size = flash->chip->page_size;
117 flash->chip->block_erasers->eraseblocks[0].count = 1;
118
119 return 1;
120}
121
122static int nicintel_ee_probe_82580(struct flashctx *flash)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000123{
124 if (nicintel_pci->device_id == UNPROG_DEVICE)
125 flash->chip->total_size = 16; /* Fall back to minimum supported size. */
126 else {
127 uint32_t tmp = pci_mmio_readl(nicintel_eebar + EEC);
128 tmp = ((tmp >> EE_SIZE) & EE_SIZE_MASK);
129 switch (tmp) {
130 case 7:
131 flash->chip->total_size = 16;
132 break;
133 case 8:
134 flash->chip->total_size = 32;
135 break;
136 default:
137 msg_cerr("Unsupported chip size 0x%x\n", tmp);
138 return 0;
139 }
140 }
141
Stefan Tauner8d21ff12015-01-10 09:33:06 +0000142 flash->chip->page_size = EE_PAGE_MASK + 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000143 flash->chip->tested = TEST_OK_PREW;
144 flash->chip->gran = write_gran_1byte_implicit_erase;
Stefan Tauner8d21ff12015-01-10 09:33:06 +0000145 flash->chip->block_erasers->eraseblocks[0].size = (EE_PAGE_MASK + 1);
146 flash->chip->block_erasers->eraseblocks[0].count = (flash->chip->total_size * 1024) / (EE_PAGE_MASK + 1);
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000147
148 return 1;
149}
150
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100151#define MAX_ATTEMPTS 10000000
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000152static int nicintel_ee_read_word(unsigned int addr, uint16_t *data)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000153{
154 uint32_t tmp = BIT(EERD_START) | (addr << EERD_ADDR);
155 pci_mmio_writel(tmp, nicintel_eebar + EERD);
156
157 /* Poll done flag. 10.000.000 cycles seem to be enough. */
158 uint32_t i;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100159 for (i = 0; i < MAX_ATTEMPTS; i++) {
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000160 tmp = pci_mmio_readl(nicintel_eebar + EERD);
161 if (tmp & BIT(EERD_DONE)) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000162 *data = (tmp >> EERD_DATA) & 0xffff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000163 return 0;
164 }
165 }
166
167 return -1;
168}
169
170static int nicintel_ee_read(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len)
171{
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000172 uint16_t data;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000173
174 /* The NIC interface always reads 16 b words so we need to convert the address and handle odd address
175 * explicitly at the start (and also at the end in the loop below). */
176 if (addr & 1) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000177 if (nicintel_ee_read_word(addr / 2, &data))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000178 return -1;
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000179 *buf++ = data & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000180 addr++;
181 len--;
182 }
183
184 while (len > 0) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000185 if (nicintel_ee_read_word(addr / 2, &data))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000186 return -1;
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000187 *buf++ = data & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000188 addr++;
189 len--;
190 if (len > 0) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000191 *buf++ = (data >> 8) & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000192 addr++;
193 len--;
194 }
195 }
196
197 return 0;
198}
199
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100200static int nicintel_ee_write_word_i210(unsigned int addr, uint16_t data)
201{
202 uint32_t eewr;
203
204 eewr = addr << EEWR_ADDR;
205 eewr |= data << EEWR_DATA;
206 eewr |= BIT(EEWR_CMDV);
207 pci_mmio_writel(eewr, nicintel_eebar + EEWR);
208
209 programmer_delay(5);
David Hendricks79d838d2017-09-27 09:25:34 -0700210 int i;
211 for (i = 0; i < MAX_ATTEMPTS; i++)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100212 if (pci_mmio_readl(nicintel_eebar + EEWR) & BIT(EEWR_DONE))
213 return 0;
214 return -1;
215}
216
Nico Huber4343e7d2017-10-10 17:38:07 +0200217static int nicintel_ee_write_i210(struct flashctx *flash, const uint8_t *buf,
218 unsigned int addr, unsigned int len)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100219{
220 done_i20_write = true;
221
222 if (addr & 1) {
223 uint16_t data;
224
225 if (nicintel_ee_read_word(addr / 2, &data)) {
226 msg_perr("Timeout reading heading byte\n");
227 return -1;
228 }
229
230 data &= 0xff;
231 data |= (buf ? (buf[0]) : 0xff) << 8;
232
233 if (nicintel_ee_write_word_i210(addr / 2, data)) {
234 msg_perr("Timeout writing heading word\n");
235 return -1;
236 }
237
238 if (buf)
239 buf ++;
240 addr ++;
241 len --;
242 }
243
244 while (len > 0) {
245 uint16_t data;
246
247 if (len == 1) {
248 if (nicintel_ee_read_word(addr / 2, &data)) {
249 msg_perr("Timeout reading tail byte\n");
250 return -1;
251 }
252
253 data &= 0xff00;
254 data |= buf ? (buf[0]) : 0xff;
255 } else {
256 if (buf)
257 data = buf[0] | (buf[1] << 8);
258 else
259 data = 0xffff;
260 }
261
262 if (nicintel_ee_write_word_i210(addr / 2, data)) {
263 msg_perr("Timeout writing Shadow RAM\n");
264 return -1;
265 }
266
267 if (buf)
268 buf += 2;
269 if (len > 2)
270 len -= 2;
271 else
272 len = 0;
273 addr += 2;
274 }
275
276 return 0;
277}
278
Nico Huber89622672017-10-10 18:05:55 +0200279static int nicintel_ee_erase_i210(struct flashctx *flash, unsigned int addr, unsigned int len)
280{
281 return nicintel_ee_write_i210(flash, NULL, addr, len);
282}
283
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000284static int nicintel_ee_bitset(int reg, int bit, bool val)
285{
286 uint32_t tmp;
287
288 tmp = pci_mmio_readl(nicintel_eebar + reg);
289 if (val)
290 tmp |= BIT(bit);
291 else
292 tmp &= ~BIT(bit);
293 pci_mmio_writel(tmp, nicintel_eebar + reg);
294
295 return -1;
296}
297
298/* Shifts one byte out while receiving another one by bitbanging (denoted "direct access" in the datasheet). */
299static int nicintel_ee_bitbang(uint8_t mosi, uint8_t *miso)
300{
301 uint8_t out = 0x0;
302
303 int i;
304 for (i = 7; i >= 0; i--) {
305 nicintel_ee_bitset(EEC, EE_SI, mosi & BIT(i));
306 nicintel_ee_bitset(EEC, EE_SCK, 1);
307 if (miso != NULL) {
308 uint32_t tmp = pci_mmio_readl(nicintel_eebar + EEC);
309 if (tmp & BIT(EE_SO))
310 out |= BIT(i);
311 }
312 nicintel_ee_bitset(EEC, EE_SCK, 0);
313 }
314
315 if (miso != NULL)
316 *miso = out;
317
318 return 0;
319}
320
321/* Polls the WIP bit of the status register of the attached EEPROM via bitbanging. */
322static int nicintel_ee_ready(void)
323{
324 unsigned int i;
325 for (i = 0; i < 1000; i++) {
326 nicintel_ee_bitset(EEC, EE_CS, 0);
327
328 nicintel_ee_bitbang(JEDEC_RDSR, NULL);
329 uint8_t rdsr;
330 nicintel_ee_bitbang(0x00, &rdsr);
331
332 nicintel_ee_bitset(EEC, EE_CS, 1);
333 programmer_delay(1);
334 if (!(rdsr & SPI_SR_WIP)) {
335 return 0;
336 }
337 }
338 return -1;
339}
340
341/* Requests direct access to the SPI pins. */
342static int nicintel_ee_req(void)
343{
344 uint32_t tmp;
345 nicintel_ee_bitset(EEC, EE_REQ, 1);
346
347 tmp = pci_mmio_readl(nicintel_eebar + EEC);
348 if (!(tmp & BIT(EE_GNT))) {
349 msg_perr("Enabling eeprom access failed.\n");
350 return 1;
351 }
352
353 nicintel_ee_bitset(EEC, EE_SCK, 0);
354 return 0;
355}
356
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100357static 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 +0000358{
359 if (nicintel_ee_req())
360 return -1;
361
362 int ret = -1;
363 if (nicintel_ee_ready())
364 goto out;
365
366 while (len > 0) {
367 /* WREN */
368 nicintel_ee_bitset(EEC, EE_CS, 0);
369 nicintel_ee_bitbang(JEDEC_WREN, NULL);
370 nicintel_ee_bitset(EEC, EE_CS, 1);
371 programmer_delay(1);
372
373 /* data */
374 nicintel_ee_bitset(EEC, EE_CS, 0);
375 nicintel_ee_bitbang(JEDEC_BYTE_PROGRAM, NULL);
376 nicintel_ee_bitbang((addr >> 8) & 0xff, NULL);
377 nicintel_ee_bitbang(addr & 0xff, NULL);
378 while (len > 0) {
379 nicintel_ee_bitbang((buf) ? *buf++ : 0xff, NULL);
380 len--;
381 addr++;
Stefan Tauner8d21ff12015-01-10 09:33:06 +0000382 if (!(addr & EE_PAGE_MASK))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000383 break;
384 }
385 nicintel_ee_bitset(EEC, EE_CS, 1);
386 programmer_delay(1);
387 if (nicintel_ee_ready())
388 goto out;
389 }
390 ret = 0;
391out:
392 nicintel_ee_bitset(EEC, EE_REQ, 0); /* Give up direct access. */
393 return ret;
394}
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100395
Nico Huber89622672017-10-10 18:05:55 +0200396static int nicintel_ee_erase_82580(struct flashctx *flash, unsigned int addr, unsigned int len)
397{
398 return nicintel_ee_write_82580(flash, NULL, addr, len);
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100399}
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000400
Nico Huber72c02ff2023-01-08 02:00:06 +0100401static int nicintel_ee_shutdown_82580(void *eecp);
402
Nico Huber89622672017-10-10 18:05:55 +0200403static const struct opaque_master opaque_master_nicintel_ee_82580 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200404 .probe = nicintel_ee_probe_82580,
405 .read = nicintel_ee_read,
406 .write = nicintel_ee_write_82580,
407 .erase = nicintel_ee_erase_82580,
Nico Huber72c02ff2023-01-08 02:00:06 +0100408 .shutdown = nicintel_ee_shutdown_82580,
Nico Huber89622672017-10-10 18:05:55 +0200409};
410
Nico Huber72c02ff2023-01-08 02:00:06 +0100411static int nicintel_ee_shutdown_i210(void *arg);
412
Nico Huber89622672017-10-10 18:05:55 +0200413static const struct opaque_master opaque_master_nicintel_ee_i210 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200414 .probe = nicintel_ee_probe_i210,
415 .read = nicintel_ee_read,
416 .write = nicintel_ee_write_i210,
417 .erase = nicintel_ee_erase_i210,
Nico Huber72c02ff2023-01-08 02:00:06 +0100418 .shutdown = nicintel_ee_shutdown_i210,
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000419};
420
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100421static int nicintel_ee_shutdown_i210(void *arg)
422{
Alexander Goncharov316ef012022-08-07 12:08:49 +0300423 int ret = 0;
424
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100425 if (!done_i20_write)
Alexander Goncharov316ef012022-08-07 12:08:49 +0300426 goto out;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100427
428 uint32_t flup = pci_mmio_readl(nicintel_eebar + EEC);
429
430 flup |= BIT(EE_FLUPD);
431 pci_mmio_writel(flup, nicintel_eebar + EEC);
432
David Hendricks79d838d2017-09-27 09:25:34 -0700433 int i;
434 for (i = 0; i < MAX_ATTEMPTS; i++)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100435 if (pci_mmio_readl(nicintel_eebar + EEC) & BIT(EE_FLUDONE))
Alexander Goncharov316ef012022-08-07 12:08:49 +0300436 goto out;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100437
Alexander Goncharov316ef012022-08-07 12:08:49 +0300438 ret = -1;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100439 msg_perr("Flash update failed\n");
440
Alexander Goncharov316ef012022-08-07 12:08:49 +0300441out:
442 return ret;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100443}
444
Nico Huber89622672017-10-10 18:05:55 +0200445static int nicintel_ee_shutdown_82580(void *eecp)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000446{
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000447 int ret = 0;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000448
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000449 if (nicintel_pci->device_id != UNPROG_DEVICE) {
450 uint32_t old_eec = *(uint32_t *)eecp;
451 /* Request bitbanging and unselect the chip first to be safe. */
452 if (nicintel_ee_req() || nicintel_ee_bitset(EEC, EE_CS, 1)) {
453 ret = -1;
454 goto out;
455 }
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000456
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000457 /* Try to restore individual bits we care about. */
458 ret = nicintel_ee_bitset(EEC, EE_SCK, old_eec & BIT(EE_SCK));
459 ret |= nicintel_ee_bitset(EEC, EE_SI, old_eec & BIT(EE_SI));
460 ret |= nicintel_ee_bitset(EEC, EE_CS, old_eec & BIT(EE_CS));
461 /* REQ will be cleared by hardware anyway after 2 seconds of inactivity
462 * on the SPI pins (3.3.2.1). */
463 ret |= nicintel_ee_bitset(EEC, EE_REQ, old_eec & BIT(EE_REQ));
464 }
465
466out:
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000467 free(eecp);
468 return ret;
469}
470
Thomas Heijligencc853d82021-05-04 15:32:17 +0200471static int nicintel_ee_init(void)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000472{
473 if (rget_io_perms())
474 return 1;
475
476 struct pci_dev *dev = pcidev_init(nics_intel_ee, PCI_BASE_ADDRESS_0);
477 if (!dev)
478 return 1;
479
480 uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
481 if (!io_base_addr)
482 return 1;
483
Nico Huber89622672017-10-10 18:05:55 +0200484 if (!is_i210(dev->device_id)) {
485 nicintel_eebar = rphysmap("Intel Gigabit NIC w/ SPI EEPROM", io_base_addr, MEMMAP_SIZE);
486 if (!nicintel_eebar)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000487 return 1;
Nico Huber89622672017-10-10 18:05:55 +0200488
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000489 uint32_t *eecp = NULL;
490
Nico Huber89622672017-10-10 18:05:55 +0200491 nicintel_pci = dev;
Anastasia Klimchuk27fdfd72021-08-03 10:41:50 +1000492 if (dev->device_id != UNPROG_DEVICE) {
Nico Huber89622672017-10-10 18:05:55 +0200493 uint32_t eec = pci_mmio_readl(nicintel_eebar + EEC);
494
495 /* C.f. 3.3.1.5 for the detection mechanism (maybe? contradicting
496 the EE_PRES definition),
497 and 3.3.1.7 for possible recovery. */
498 if (!(eec & BIT(EE_PRES))) {
499 msg_perr("Controller reports no EEPROM is present.\n");
500 return 1;
501 }
502
Anastasia Klimchuk7a7b25d2021-08-03 11:54:56 +1000503 eecp = malloc(sizeof(uint32_t));
Nico Huber89622672017-10-10 18:05:55 +0200504 if (eecp == NULL)
505 return 1;
506 *eecp = eec;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000507 }
508
Nico Huber72c02ff2023-01-08 02:00:06 +0100509 return register_opaque_master(&opaque_master_nicintel_ee_82580, eecp);
Nico Huber89622672017-10-10 18:05:55 +0200510 } else {
511 nicintel_eebar = rphysmap("Intel i210 NIC w/ emulated EEPROM",
512 io_base_addr + 0x12000, MEMMAP_SIZE);
513 if (!nicintel_eebar)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000514 return 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000515
Anastasia Klimchuk21b20212021-05-13 12:28:47 +1000516 return register_opaque_master(&opaque_master_nicintel_ee_i210, NULL);
Nico Huber89622672017-10-10 18:05:55 +0200517 }
518
519 return 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000520}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200521
522const struct programmer_entry programmer_nicintel_eeprom = {
523 .name = "nicintel_eeprom",
524 .type = PCI,
525 .devs.dev = nics_intel_ee,
526 .init = nicintel_ee_init,
527 .map_flash_region = fallback_map,
528 .unmap_flash_region = fallback_unmap,
529 .delay = internal_delay,
530};