blob: 616c161be3f2825ea353a0805344fa33f7c9305e [file] [log] [blame]
Idwer Vollering004f4b72010-09-03 18:21:21 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Carl-Daniel Hailfinger
5 * Copyright (C) 2010 Idwer Vollering
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Idwer Vollering004f4b72010-09-03 18:21:21 +000015 */
16
17/*
Bill Paulbf8ea492014-03-17 22:07:29 +000018 * Datasheets:
Idwer Vollering004f4b72010-09-03 18:21:21 +000019 * PCI/PCI-X Family of Gigabit Ethernet Controllers Software Developer's Manual
20 * 82540EP/EM, 82541xx, 82544GC/EI, 82545GM/EM, 82546GB/EB, and 82547xx
Bill Paulbf8ea492014-03-17 22:07:29 +000021 * http://www.intel.com/content/www/us/en/ethernet-controllers/pci-pci-x-family-gbe-controllers-software-dev-manual.html
22 *
23 * PCIe GbE Controllers Open Source Software Developer's Manual
24 * http://www.intel.com/content/www/us/en/ethernet-controllers/pcie-gbe-controllers-open-source-manual.html
25 *
26 * Intel 82574 Gigabit Ethernet Controller Family Datasheet
27 * http://www.intel.com/content/www/us/en/ethernet-controllers/82574l-gbe-controller-datasheet.html
Ed Swierk33180df2014-12-05 22:56:13 +000028 *
29 * Intel 82599 10 GbE Controller Datasheet (331520)
30 * http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/82599-10-gbe-controller-datasheet.pdf
Idwer Vollering004f4b72010-09-03 18:21:21 +000031 */
32
33#include <stdlib.h>
Stefan Tauner6745d6f2012-08-26 21:50:36 +000034#include <unistd.h>
Idwer Vollering004f4b72010-09-03 18:21:21 +000035#include "flash.h"
36#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000037#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"
Idwer Vollering004f4b72010-09-03 18:21:21 +000041
42#define PCI_VENDOR_ID_INTEL 0x8086
Stefan Tauner6745d6f2012-08-26 21:50:36 +000043#define MEMMAP_SIZE getpagesize()
Idwer Vollering004f4b72010-09-03 18:21:21 +000044
Stefan Tauner8ee180d2012-02-27 19:44:16 +000045/* EEPROM/Flash Control & Data Register */
Idwer Vollering004f4b72010-09-03 18:21:21 +000046#define EECD 0x10
Stefan Tauner8ee180d2012-02-27 19:44:16 +000047/* Flash Access Register */
Idwer Vollering004f4b72010-09-03 18:21:21 +000048#define FLA 0x1c
49
50/*
51 * Register bits of EECD.
Stefan Tauner8ee180d2012-02-27 19:44:16 +000052 * Table 13-6
53 *
Idwer Vollering004f4b72010-09-03 18:21:21 +000054 * Bit 04, 05: FWE (Flash Write Enable Control)
Ed Swierk33180df2014-12-05 22:56:13 +000055 * 00b = not allowed (on some cards this sends an erase command if bit 31 (FL_ER) of FLA is set)
Idwer Vollering004f4b72010-09-03 18:21:21 +000056 * 01b = flash writes disabled
57 * 10b = flash writes enabled
58 * 11b = not allowed
59 */
60#define FLASH_WRITES_DISABLED 0x10 /* FWE: 10000b */
61#define FLASH_WRITES_ENABLED 0x20 /* FWE: 100000b */
62
Stefan Tauner8ee180d2012-02-27 19:44:16 +000063/* Flash Access register bits
64 * Table 13-9
65 */
Idwer Vollering004f4b72010-09-03 18:21:21 +000066#define FL_SCK 0
67#define FL_CS 1
68#define FL_SI 2
69#define FL_SO 3
70#define FL_REQ 4
71#define FL_GNT 5
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +010072#define FL_LOCKED 6
73#define FL_ABORT 7
74#define FL_CLR_ERR 8
Idwer Vollering004f4b72010-09-03 18:21:21 +000075/* Currently unused */
76// #define FL_BUSY 30
77// #define FL_ER 31
78
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +010079#define BIT(x) (1<<(x))
80
Jacob Garberafc3ad62019-06-24 16:05:28 -060081static uint8_t *nicintel_spibar;
Idwer Vollering004f4b72010-09-03 18:21:21 +000082
Thomas Heijligencc853d82021-05-04 15:32:17 +020083static const struct dev_entry nics_intel_spi[] = {
Idwer Volleringbdc48272010-10-05 11:16:14 +000084 {PCI_VENDOR_ID_INTEL, 0x105e, OK, "Intel", "82571EB Gigabit Ethernet Controller"},
Stefan Tauner4b90e6b2011-05-18 01:31:24 +000085 {PCI_VENDOR_ID_INTEL, 0x1076, OK, "Intel", "82541GI Gigabit Ethernet Controller"},
Idwer Vollering004f4b72010-09-03 18:21:21 +000086 {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"},
Idwer Volleringbdc48272010-10-05 11:16:14 +000087 {PCI_VENDOR_ID_INTEL, 0x10b9, OK, "Intel", "82572EI Gigabit Ethernet Controller"},
Bill Paulbf8ea492014-03-17 22:07:29 +000088 {PCI_VENDOR_ID_INTEL, 0x10d3, OK, "Intel", "82574L Gigabit Ethernet Controller"},
Idwer Vollering004f4b72010-09-03 18:21:21 +000089
Ed Swierk33180df2014-12-05 22:56:13 +000090 {PCI_VENDOR_ID_INTEL, 0x10d8, NT, "Intel", "82599 10 Gigabit Unprogrammed Network Controller"},
91 {PCI_VENDOR_ID_INTEL, 0x10f7, NT, "Intel", "82599 10 Gigabit KX4 Dual Port Network Controller"},
92 {PCI_VENDOR_ID_INTEL, 0x10f8, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller"},
93 {PCI_VENDOR_ID_INTEL, 0x10f9, NT, "Intel", "82599 10 Gigabit CX4 Dual Port Network Controller"},
94 {PCI_VENDOR_ID_INTEL, 0x10fb, NT, "Intel", "82599 10-Gigabit SFI/SFP+ Network Controller"},
95 {PCI_VENDOR_ID_INTEL, 0x10fc, OK, "Intel", "82599 10 Gigabit XAUI/BX4 Dual Port Network Controller"},
96 {PCI_VENDOR_ID_INTEL, 0x1517, NT, "Intel", "82599 10 Gigabit KR Network Controller"},
97 {PCI_VENDOR_ID_INTEL, 0x151c, NT, "Intel", "82599 10 Gigabit TN Network Controller"},
98 {PCI_VENDOR_ID_INTEL, 0x1529, NT, "Intel", "82599 10 Gigabit Dual Port Network Controller with FCoE"},
99 {PCI_VENDOR_ID_INTEL, 0x152a, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller with FCoE"},
100 {PCI_VENDOR_ID_INTEL, 0x1557, NT, "Intel", "82599 10 Gigabit SFI Network Controller"},
101
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100102 {PCI_VENDOR_ID_INTEL, 0x1531, OK, "Intel", "I210 Gigabit Network Connection Unprogrammed"},
103 {PCI_VENDOR_ID_INTEL, 0x1532, NT, "Intel", "I211 Gigabit Network Connection Unprogrammed"},
104 {PCI_VENDOR_ID_INTEL, 0x1533, NT, "Intel", "I210 Gigabit Network Connection"},
105 {PCI_VENDOR_ID_INTEL, 0x1536, NT, "Intel", "I210 Gigabit Network Connection SERDES Fiber"},
106 {PCI_VENDOR_ID_INTEL, 0x1537, NT, "Intel", "I210 Gigabit Network Connection SERDES Backplane"},
107 {PCI_VENDOR_ID_INTEL, 0x1538, NT, "Intel", "I210 Gigabit Network Connection SGMII"},
108 {PCI_VENDOR_ID_INTEL, 0x1539, NT, "Intel", "I211 Gigabit Network Connection"},
109
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000110 {0},
Idwer Vollering004f4b72010-09-03 18:21:21 +0000111};
112
113static void nicintel_request_spibus(void)
114{
115 uint32_t tmp;
116
117 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100118 tmp |= BIT(FL_REQ);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000119 pci_mmio_writel(tmp, nicintel_spibar + FLA);
120
121 /* Wait until we are allowed to use the SPI bus. */
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100122 while (!(pci_mmio_readl(nicintel_spibar + FLA) & BIT(FL_GNT))) ;
Idwer Vollering004f4b72010-09-03 18:21:21 +0000123}
124
125static void nicintel_release_spibus(void)
126{
127 uint32_t tmp;
128
129 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100130 tmp &= ~BIT(FL_REQ);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000131 pci_mmio_writel(tmp, nicintel_spibar + FLA);
132}
133
134static void nicintel_bitbang_set_cs(int val)
135{
136 uint32_t tmp;
137
Idwer Vollering004f4b72010-09-03 18:21:21 +0000138 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100139 tmp &= ~BIT(FL_CS);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000140 tmp |= (val << FL_CS);
141 pci_mmio_writel(tmp, nicintel_spibar + FLA);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000142}
143
144static void nicintel_bitbang_set_sck(int val)
145{
146 uint32_t tmp;
147
148 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100149 tmp &= ~BIT(FL_SCK);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000150 tmp |= (val << FL_SCK);
151 pci_mmio_writel(tmp, nicintel_spibar + FLA);
152}
153
154static void nicintel_bitbang_set_mosi(int val)
155{
156 uint32_t tmp;
157
158 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100159 tmp &= ~BIT(FL_SI);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000160 tmp |= (val << FL_SI);
161 pci_mmio_writel(tmp, nicintel_spibar + FLA);
162}
163
Angel Ponsf0c03fb2021-06-21 13:01:45 +0200164static void nicintel_bitbang_set_sck_set_mosi(int sck, int mosi)
165{
166 uint32_t tmp;
167
168 tmp = pci_mmio_readl(nicintel_spibar + FLA);
169 tmp &= ~BIT(FL_SCK);
170 tmp &= ~BIT(FL_SI);
171 tmp |= (sck << FL_SCK);
172 tmp |= (mosi << FL_SI);
173 pci_mmio_writel(tmp, nicintel_spibar + FLA);
174}
175
Idwer Vollering004f4b72010-09-03 18:21:21 +0000176static int nicintel_bitbang_get_miso(void)
177{
178 uint32_t tmp;
179
180 tmp = pci_mmio_readl(nicintel_spibar + FLA);
181 tmp = (tmp >> FL_SO) & 0x1;
182 return tmp;
183}
184
Angel Ponsf0c03fb2021-06-21 13:01:45 +0200185static int nicintel_bitbang_set_sck_get_miso(int sck)
186{
187 uint32_t tmp;
188
189 tmp = pci_mmio_readl(nicintel_spibar + FLA);
190 tmp &= ~BIT(FL_SCK);
191 tmp |= (sck << FL_SCK);
192 pci_mmio_writel(tmp, nicintel_spibar + FLA);
193 return (tmp >> FL_SO) & 0x1;
194}
195
Idwer Vollering004f4b72010-09-03 18:21:21 +0000196static const struct bitbang_spi_master bitbang_spi_master_nicintel = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200197 .set_cs = nicintel_bitbang_set_cs,
198 .set_sck = nicintel_bitbang_set_sck,
199 .set_mosi = nicintel_bitbang_set_mosi,
200 .set_sck_set_mosi = nicintel_bitbang_set_sck_set_mosi,
201 .set_sck_get_miso = nicintel_bitbang_set_sck_get_miso,
202 .get_miso = nicintel_bitbang_get_miso,
203 .request_bus = nicintel_request_spibus,
204 .release_bus = nicintel_release_spibus,
205 .half_period = 1,
Idwer Vollering004f4b72010-09-03 18:21:21 +0000206};
207
David Hendricks8bb20212011-06-14 01:35:36 +0000208static int nicintel_spi_shutdown(void *data)
209{
210 uint32_t tmp;
211
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000212 /* Disable writes manually. See the comment about EECD in nicintel_spi_init() for details. */
David Hendricks8bb20212011-06-14 01:35:36 +0000213 tmp = pci_mmio_readl(nicintel_spibar + EECD);
214 tmp &= ~FLASH_WRITES_ENABLED;
215 tmp |= FLASH_WRITES_DISABLED;
216 pci_mmio_writel(tmp, nicintel_spibar + EECD);
217
David Hendricks8bb20212011-06-14 01:35:36 +0000218 return 0;
219}
220
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100221static int nicintel_spi_82599_enable_flash(void)
Idwer Vollering004f4b72010-09-03 18:21:21 +0000222{
223 uint32_t tmp;
224
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000225 /* Automatic restore of EECD on shutdown is not possible because EECD
226 * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED,
227 * but other bits with side effects as well. Those other bits must be
228 * left untouched.
229 */
Idwer Vollering004f4b72010-09-03 18:21:21 +0000230 tmp = pci_mmio_readl(nicintel_spibar + EECD);
231 tmp &= ~FLASH_WRITES_DISABLED;
232 tmp |= FLASH_WRITES_ENABLED;
233 pci_mmio_writel(tmp, nicintel_spibar + EECD);
234
Stefan Tauner8ee180d2012-02-27 19:44:16 +0000235 /* test if FWE is really set to allow writes */
236 tmp = pci_mmio_readl(nicintel_spibar + EECD);
237 if ( (tmp & FLASH_WRITES_DISABLED) || !(tmp & FLASH_WRITES_ENABLED) ) {
238 msg_perr("Enabling flash write access failed.\n");
239 return 1;
240 }
241
David Hendricks8bb20212011-06-14 01:35:36 +0000242 if (register_shutdown(nicintel_spi_shutdown, NULL))
243 return 1;
244
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100245 return 0;
246}
247
Richard Hughes93e16252018-12-19 11:54:47 +0000248static int nicintel_spi_i210_enable_flash(void)
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100249{
250 uint32_t tmp;
251
252 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100253 if (tmp & BIT(FL_LOCKED)) {
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100254 msg_perr("Flash is in Secure Mode. Abort.\n");
255 return 1;
256 }
257
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100258 if (!(tmp & BIT(FL_ABORT)))
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100259 return 0;
260
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100261 tmp |= BIT(FL_CLR_ERR);
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100262 pci_mmio_writel(tmp, nicintel_spibar + FLA);
263 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100264 if (!(tmp & BIT(FL_ABORT))) {
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100265 msg_perr("Unable to clear Flash Access Error. Abort\n");
266 return 1;
267 }
268
269 return 0;
270}
271
Thomas Heijligencc853d82021-05-04 15:32:17 +0200272static int nicintel_spi_init(void)
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100273{
274 struct pci_dev *dev = NULL;
275
276 if (rget_io_perms())
277 return 1;
278
279 dev = pcidev_init(nics_intel_spi, PCI_BASE_ADDRESS_0);
280 if (!dev)
281 return 1;
282
283 uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
284 if (!io_base_addr)
285 return 1;
286
287 if ((dev->device_id & 0xfff0) == 0x1530) {
288 nicintel_spibar = rphysmap("Intel I210 Gigabit w/ SPI flash", io_base_addr + 0x12000,
289 MEMMAP_SIZE);
290 if (!nicintel_spibar || nicintel_spi_i210_enable_flash())
291 return 1;
292 } else if (dev->device_id < 0x10d8) {
293 nicintel_spibar = rphysmap("Intel Gigabit NIC w/ SPI flash", io_base_addr,
294 MEMMAP_SIZE);
295 if (!nicintel_spibar || nicintel_spi_82599_enable_flash())
296 return 1;
297 } else {
298 nicintel_spibar = rphysmap("Intel 10 Gigabit NIC w/ SPI flash", io_base_addr + 0x10000,
299 MEMMAP_SIZE);
300 if (!nicintel_spibar || nicintel_spi_82599_enable_flash())
301 return 1;
302 }
303
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000304 if (register_spi_bitbang_master(&bitbang_spi_master_nicintel))
Idwer Vollering004f4b72010-09-03 18:21:21 +0000305 return 1;
306
Idwer Vollering004f4b72010-09-03 18:21:21 +0000307 return 0;
308}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200309
310const struct programmer_entry programmer_nicintel_spi = {
311 .name = "nicintel_spi",
312 .type = PCI,
313 .devs.dev = nics_intel_spi,
314 .init = nicintel_spi_init,
315 .map_flash_region = fallback_map,
316 .unmap_flash_region = fallback_unmap,
317 .delay = internal_delay,
318};