blob: e4a91efc394d398434094fb0fd23a2d93b738650 [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.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see http://www.gnu.org/licenses/.
20 */
21
22/*
23 * Datasheet: Intel 82580 Quad/Dual Gigabit Ethernet LAN Controller Datasheet
24 * 3.3.1.4: General EEPROM Software Access
25 * 4.7: Access to shared resources (FIXME: we should probably use this semaphore interface)
26 * 7.4: Register Descriptions
27 */
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010028/*
29 * Datasheet: Intel Ethernet Controller I210: Datasheet
30 * 8.4.3: EEPROM-Mode Read Register
31 * 8.4.6: EEPROM-Mode Write Register
32 * Write process inspired on kernel e1000_i210.c
33 */
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000034
35#include <stdlib.h>
36#include <unistd.h>
37#include "flash.h"
38#include "spi.h"
39#include "programmer.h"
40#include "hwaccess.h"
41
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
83const struct dev_entry nics_intel_ee[] = {
84 {PCI_VENDOR_ID_INTEL, 0x150e, OK, "Intel", "82580 Quad Gigabit Ethernet Controller (Copper)"},
85 {PCI_VENDOR_ID_INTEL, 0x150f, NT , "Intel", "82580 Quad Gigabit Ethernet Controller (Fiber)"},
86 {PCI_VENDOR_ID_INTEL, 0x1510, NT , "Intel", "82580 Quad Gigabit Ethernet Controller (Backplane)"},
87 {PCI_VENDOR_ID_INTEL, 0x1511, NT , "Intel", "82580 Quad Gigabit Ethernet Controller (Ext. PHY)"},
88 {PCI_VENDOR_ID_INTEL, 0x1511, NT , "Intel", "82580 Dual Gigabit Ethernet Controller (Copper)"},
89 {PCI_VENDOR_ID_INTEL, UNPROG_DEVICE, OK, "Intel", "Unprogrammed 82580 Quad/Dual Gigabit Ethernet Controller"},
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +010090 {PCI_VENDOR_ID_INTEL, 0x1531, NT, "Intel", "I210 Gigabit Network Connection Unprogrammed"},
91 {PCI_VENDOR_ID_INTEL, 0x1532, NT, "Intel", "I211 Gigabit Network Connection Unprogrammed"},
92 {PCI_VENDOR_ID_INTEL, 0x1533, OK, "Intel", "I210 Gigabit Network Connection"},
93 {PCI_VENDOR_ID_INTEL, 0x1536, NT, "Intel", "I210 Gigabit Network Connection SERDES Fiber"},
94 {PCI_VENDOR_ID_INTEL, 0x1537, NT, "Intel", "I210 Gigabit Network Connection SERDES Backplane"},
95 {PCI_VENDOR_ID_INTEL, 0x1538, NT, "Intel", "I210 Gigabit Network Connection SGMII"},
96 {PCI_VENDOR_ID_INTEL, 0x1539, NT, "Intel", "I211 Gigabit Network Connection"},
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +000097 {0},
98};
99
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100100static inline bool is_i210(uint16_t device_id)
101{
102 return (device_id & 0xff00) == 0x1500;
103}
104
105static int nicintel_ee_probe_i210(struct flashctx *flash)
106{
107 /* Emulated eeprom has a fixed size of 4 KB */
108 flash->chip->total_size = 4;
109 flash->chip->page_size = flash->chip->total_size * 1024;
110 flash->chip->tested = TEST_OK_PREW;
111 flash->chip->gran = write_gran_1byte_implicit_erase;
112 flash->chip->block_erasers->eraseblocks[0].size = flash->chip->page_size;
113 flash->chip->block_erasers->eraseblocks[0].count = 1;
114
115 return 1;
116}
117
118static int nicintel_ee_probe_82580(struct flashctx *flash)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000119{
120 if (nicintel_pci->device_id == UNPROG_DEVICE)
121 flash->chip->total_size = 16; /* Fall back to minimum supported size. */
122 else {
123 uint32_t tmp = pci_mmio_readl(nicintel_eebar + EEC);
124 tmp = ((tmp >> EE_SIZE) & EE_SIZE_MASK);
125 switch (tmp) {
126 case 7:
127 flash->chip->total_size = 16;
128 break;
129 case 8:
130 flash->chip->total_size = 32;
131 break;
132 default:
133 msg_cerr("Unsupported chip size 0x%x\n", tmp);
134 return 0;
135 }
136 }
137
Stefan Tauner8d21ff12015-01-10 09:33:06 +0000138 flash->chip->page_size = EE_PAGE_MASK + 1;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000139 flash->chip->tested = TEST_OK_PREW;
140 flash->chip->gran = write_gran_1byte_implicit_erase;
Stefan Tauner8d21ff12015-01-10 09:33:06 +0000141 flash->chip->block_erasers->eraseblocks[0].size = (EE_PAGE_MASK + 1);
142 flash->chip->block_erasers->eraseblocks[0].count = (flash->chip->total_size * 1024) / (EE_PAGE_MASK + 1);
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000143
144 return 1;
145}
146
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100147static int nicintel_ee_probe(struct flashctx *flash)
148{
149 if (is_i210(nicintel_pci->device_id))
150 return nicintel_ee_probe_i210(flash);
151
152 return nicintel_ee_probe_82580(flash);
153}
154
155#define MAX_ATTEMPTS 10000000
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000156static int nicintel_ee_read_word(unsigned int addr, uint16_t *data)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000157{
158 uint32_t tmp = BIT(EERD_START) | (addr << EERD_ADDR);
159 pci_mmio_writel(tmp, nicintel_eebar + EERD);
160
161 /* Poll done flag. 10.000.000 cycles seem to be enough. */
162 uint32_t i;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100163 for (i = 0; i < MAX_ATTEMPTS; i++) {
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000164 tmp = pci_mmio_readl(nicintel_eebar + EERD);
165 if (tmp & BIT(EERD_DONE)) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000166 *data = (tmp >> EERD_DATA) & 0xffff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000167 return 0;
168 }
169 }
170
171 return -1;
172}
173
174static int nicintel_ee_read(struct flashctx *flash, uint8_t *buf, unsigned int addr, unsigned int len)
175{
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000176 uint16_t data;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000177
178 /* The NIC interface always reads 16 b words so we need to convert the address and handle odd address
179 * explicitly at the start (and also at the end in the loop below). */
180 if (addr & 1) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000181 if (nicintel_ee_read_word(addr / 2, &data))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000182 return -1;
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000183 *buf++ = data & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000184 addr++;
185 len--;
186 }
187
188 while (len > 0) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000189 if (nicintel_ee_read_word(addr / 2, &data))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000190 return -1;
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000191 *buf++ = data & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000192 addr++;
193 len--;
194 if (len > 0) {
Stefan Tauner3e6b7bb2015-01-25 23:45:14 +0000195 *buf++ = (data >> 8) & 0xff;
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000196 addr++;
197 len--;
198 }
199 }
200
201 return 0;
202}
203
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100204static int nicintel_ee_write_word_i210(unsigned int addr, uint16_t data)
205{
206 uint32_t eewr;
207
208 eewr = addr << EEWR_ADDR;
209 eewr |= data << EEWR_DATA;
210 eewr |= BIT(EEWR_CMDV);
211 pci_mmio_writel(eewr, nicintel_eebar + EEWR);
212
213 programmer_delay(5);
David Hendricks79d838d2017-09-27 09:25:34 -0700214 int i;
215 for (i = 0; i < MAX_ATTEMPTS; i++)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100216 if (pci_mmio_readl(nicintel_eebar + EEWR) & BIT(EEWR_DONE))
217 return 0;
218 return -1;
219}
220
221static int nicintel_ee_write_i210(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len)
222{
223 done_i20_write = true;
224
225 if (addr & 1) {
226 uint16_t data;
227
228 if (nicintel_ee_read_word(addr / 2, &data)) {
229 msg_perr("Timeout reading heading byte\n");
230 return -1;
231 }
232
233 data &= 0xff;
234 data |= (buf ? (buf[0]) : 0xff) << 8;
235
236 if (nicintel_ee_write_word_i210(addr / 2, data)) {
237 msg_perr("Timeout writing heading word\n");
238 return -1;
239 }
240
241 if (buf)
242 buf ++;
243 addr ++;
244 len --;
245 }
246
247 while (len > 0) {
248 uint16_t data;
249
250 if (len == 1) {
251 if (nicintel_ee_read_word(addr / 2, &data)) {
252 msg_perr("Timeout reading tail byte\n");
253 return -1;
254 }
255
256 data &= 0xff00;
257 data |= buf ? (buf[0]) : 0xff;
258 } else {
259 if (buf)
260 data = buf[0] | (buf[1] << 8);
261 else
262 data = 0xffff;
263 }
264
265 if (nicintel_ee_write_word_i210(addr / 2, data)) {
266 msg_perr("Timeout writing Shadow RAM\n");
267 return -1;
268 }
269
270 if (buf)
271 buf += 2;
272 if (len > 2)
273 len -= 2;
274 else
275 len = 0;
276 addr += 2;
277 }
278
279 return 0;
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 +0100393static int nicintel_ee_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len)
394{
395 if (is_i210(nicintel_pci->device_id))
396 return nicintel_ee_write_i210(flash, buf, addr, len);
397
398 return nicintel_ee_write_82580(flash, buf, addr, len);
399}
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000400
401static int nicintel_ee_erase(struct flashctx *flash, unsigned int addr, unsigned int len)
402{
403 return nicintel_ee_write(flash, NULL, addr, len);
404}
405
406static const struct opaque_master opaque_master_nicintel_ee = {
407 .probe = nicintel_ee_probe,
408 .read = nicintel_ee_read,
409 .write = nicintel_ee_write,
410 .erase = nicintel_ee_erase,
411};
412
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100413static int nicintel_ee_shutdown_i210(void *arg)
414{
415 if (!done_i20_write)
416 return 0;
417
418 uint32_t flup = pci_mmio_readl(nicintel_eebar + EEC);
419
420 flup |= BIT(EE_FLUPD);
421 pci_mmio_writel(flup, nicintel_eebar + EEC);
422
David Hendricks79d838d2017-09-27 09:25:34 -0700423 int i;
424 for (i = 0; i < MAX_ATTEMPTS; i++)
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100425 if (pci_mmio_readl(nicintel_eebar + EEC) & BIT(EE_FLUDONE))
426 return 0;
427
428 msg_perr("Flash update failed\n");
429
430 return -1;
431}
432
Stefan Tauner5c316f92015-02-08 21:57:52 +0000433static int nicintel_ee_shutdown(void *eecp)
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000434{
435 uint32_t old_eec = *(uint32_t *)eecp;
436 /* Request bitbanging and unselect the chip first to be safe. */
437 if (nicintel_ee_req() || nicintel_ee_bitset(EEC, EE_CS, 1))
438 return -1;
439
440 /* Try to restore individual bits we care about. */
441 int ret = nicintel_ee_bitset(EEC, EE_SCK, old_eec & BIT(EE_SCK));
442 ret |= nicintel_ee_bitset(EEC, EE_SI, old_eec & BIT(EE_SI));
443 ret |= nicintel_ee_bitset(EEC, EE_CS, old_eec & BIT(EE_CS));
444 /* REQ will be cleared by hardware anyway after 2 seconds of inactivity on the SPI pins (3.3.2.1). */
445 ret |= nicintel_ee_bitset(EEC, EE_REQ, old_eec & BIT(EE_REQ));
446
447 free(eecp);
448 return ret;
449}
450
451int nicintel_ee_init(void)
452{
453 if (rget_io_perms())
454 return 1;
455
456 struct pci_dev *dev = pcidev_init(nics_intel_ee, PCI_BASE_ADDRESS_0);
457 if (!dev)
458 return 1;
459
460 uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
461 if (!io_base_addr)
462 return 1;
463
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100464 nicintel_eebar = rphysmap("Intel Gigabit NIC w/ SPI EEPROM",
465 io_base_addr + (is_i210(dev->device_id) ? 0x12000 : 0), MEMMAP_SIZE);
466 if (!nicintel_eebar)
467 return 1;
468
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000469 nicintel_pci = dev;
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100470 if ((dev->device_id != UNPROG_DEVICE) && ! is_i210(dev->device_id))
471 {
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000472 uint32_t eec = pci_mmio_readl(nicintel_eebar + EEC);
473
474 /* C.f. 3.3.1.5 for the detection mechanism (maybe? contradicting the EE_PRES definition),
475 * and 3.3.1.7 for possible recovery. */
476 if (!(eec & BIT(EE_PRES))) {
477 msg_perr("Controller reports no EEPROM is present.\n");
478 return 1;
479 }
480
481 uint32_t *eecp = malloc(sizeof(uint32_t));
482 if (eecp == NULL)
483 return 1;
484 *eecp = eec;
485
Stefan Tauner5c316f92015-02-08 21:57:52 +0000486 if (register_shutdown(nicintel_ee_shutdown, eecp))
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000487 return 1;
488 }
489
Ricardo Ribalda Delgado9fe1fb72017-03-23 15:11:22 +0100490 if (is_i210(dev->device_id))
491 if (register_shutdown(nicintel_ee_shutdown_i210, NULL))
492 return 1;
493
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000494 return register_opaque_master(&opaque_master_nicintel_ee);
495}