blob: c73aa13125448df83c0c856263dd2fa244aa146d [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 Heijligend96c97c2021-11-02 21:03:00 +010039#include "platform/pci.h"
Idwer Vollering004f4b72010-09-03 18:21:21 +000040
41#define PCI_VENDOR_ID_INTEL 0x8086
Stefan Tauner6745d6f2012-08-26 21:50:36 +000042#define MEMMAP_SIZE getpagesize()
Idwer Vollering004f4b72010-09-03 18:21:21 +000043
Stefan Tauner8ee180d2012-02-27 19:44:16 +000044/* EEPROM/Flash Control & Data Register */
Idwer Vollering004f4b72010-09-03 18:21:21 +000045#define EECD 0x10
Stefan Tauner8ee180d2012-02-27 19:44:16 +000046/* Flash Access Register */
Idwer Vollering004f4b72010-09-03 18:21:21 +000047#define FLA 0x1c
48
49/*
50 * Register bits of EECD.
Stefan Tauner8ee180d2012-02-27 19:44:16 +000051 * Table 13-6
52 *
Idwer Vollering004f4b72010-09-03 18:21:21 +000053 * Bit 04, 05: FWE (Flash Write Enable Control)
Ed Swierk33180df2014-12-05 22:56:13 +000054 * 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 +000055 * 01b = flash writes disabled
56 * 10b = flash writes enabled
57 * 11b = not allowed
58 */
59#define FLASH_WRITES_DISABLED 0x10 /* FWE: 10000b */
60#define FLASH_WRITES_ENABLED 0x20 /* FWE: 100000b */
61
Stefan Tauner8ee180d2012-02-27 19:44:16 +000062/* Flash Access register bits
63 * Table 13-9
64 */
Idwer Vollering004f4b72010-09-03 18:21:21 +000065#define FL_SCK 0
66#define FL_CS 1
67#define FL_SI 2
68#define FL_SO 3
69#define FL_REQ 4
70#define FL_GNT 5
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +010071#define FL_LOCKED 6
72#define FL_ABORT 7
73#define FL_CLR_ERR 8
Idwer Vollering004f4b72010-09-03 18:21:21 +000074/* Currently unused */
75// #define FL_BUSY 30
76// #define FL_ER 31
77
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +010078#define BIT(x) (1<<(x))
79
Jacob Garberafc3ad62019-06-24 16:05:28 -060080static uint8_t *nicintel_spibar;
Idwer Vollering004f4b72010-09-03 18:21:21 +000081
Thomas Heijligencc853d82021-05-04 15:32:17 +020082static const struct dev_entry nics_intel_spi[] = {
Idwer Volleringbdc48272010-10-05 11:16:14 +000083 {PCI_VENDOR_ID_INTEL, 0x105e, OK, "Intel", "82571EB Gigabit Ethernet Controller"},
Stefan Tauner4b90e6b2011-05-18 01:31:24 +000084 {PCI_VENDOR_ID_INTEL, 0x1076, OK, "Intel", "82541GI Gigabit Ethernet Controller"},
Idwer Vollering004f4b72010-09-03 18:21:21 +000085 {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"},
Idwer Volleringbdc48272010-10-05 11:16:14 +000086 {PCI_VENDOR_ID_INTEL, 0x10b9, OK, "Intel", "82572EI Gigabit Ethernet Controller"},
Bill Paulbf8ea492014-03-17 22:07:29 +000087 {PCI_VENDOR_ID_INTEL, 0x10d3, OK, "Intel", "82574L Gigabit Ethernet Controller"},
Idwer Vollering004f4b72010-09-03 18:21:21 +000088
Ed Swierk33180df2014-12-05 22:56:13 +000089 {PCI_VENDOR_ID_INTEL, 0x10d8, NT, "Intel", "82599 10 Gigabit Unprogrammed Network Controller"},
90 {PCI_VENDOR_ID_INTEL, 0x10f7, NT, "Intel", "82599 10 Gigabit KX4 Dual Port Network Controller"},
91 {PCI_VENDOR_ID_INTEL, 0x10f8, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller"},
92 {PCI_VENDOR_ID_INTEL, 0x10f9, NT, "Intel", "82599 10 Gigabit CX4 Dual Port Network Controller"},
93 {PCI_VENDOR_ID_INTEL, 0x10fb, NT, "Intel", "82599 10-Gigabit SFI/SFP+ Network Controller"},
94 {PCI_VENDOR_ID_INTEL, 0x10fc, OK, "Intel", "82599 10 Gigabit XAUI/BX4 Dual Port Network Controller"},
95 {PCI_VENDOR_ID_INTEL, 0x1517, NT, "Intel", "82599 10 Gigabit KR Network Controller"},
96 {PCI_VENDOR_ID_INTEL, 0x151c, NT, "Intel", "82599 10 Gigabit TN Network Controller"},
97 {PCI_VENDOR_ID_INTEL, 0x1529, NT, "Intel", "82599 10 Gigabit Dual Port Network Controller with FCoE"},
98 {PCI_VENDOR_ID_INTEL, 0x152a, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller with FCoE"},
99 {PCI_VENDOR_ID_INTEL, 0x1557, NT, "Intel", "82599 10 Gigabit SFI Network Controller"},
100
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100101 {PCI_VENDOR_ID_INTEL, 0x1531, OK, "Intel", "I210 Gigabit Network Connection Unprogrammed"},
102 {PCI_VENDOR_ID_INTEL, 0x1532, NT, "Intel", "I211 Gigabit Network Connection Unprogrammed"},
103 {PCI_VENDOR_ID_INTEL, 0x1533, NT, "Intel", "I210 Gigabit Network Connection"},
104 {PCI_VENDOR_ID_INTEL, 0x1536, NT, "Intel", "I210 Gigabit Network Connection SERDES Fiber"},
105 {PCI_VENDOR_ID_INTEL, 0x1537, NT, "Intel", "I210 Gigabit Network Connection SERDES Backplane"},
106 {PCI_VENDOR_ID_INTEL, 0x1538, NT, "Intel", "I210 Gigabit Network Connection SGMII"},
107 {PCI_VENDOR_ID_INTEL, 0x1539, NT, "Intel", "I211 Gigabit Network Connection"},
108
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000109 {0},
Idwer Vollering004f4b72010-09-03 18:21:21 +0000110};
111
112static void nicintel_request_spibus(void)
113{
114 uint32_t tmp;
115
116 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100117 tmp |= BIT(FL_REQ);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000118 pci_mmio_writel(tmp, nicintel_spibar + FLA);
119
120 /* Wait until we are allowed to use the SPI bus. */
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100121 while (!(pci_mmio_readl(nicintel_spibar + FLA) & BIT(FL_GNT))) ;
Idwer Vollering004f4b72010-09-03 18:21:21 +0000122}
123
124static void nicintel_release_spibus(void)
125{
126 uint32_t tmp;
127
128 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100129 tmp &= ~BIT(FL_REQ);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000130 pci_mmio_writel(tmp, nicintel_spibar + FLA);
131}
132
133static void nicintel_bitbang_set_cs(int val)
134{
135 uint32_t tmp;
136
Idwer Vollering004f4b72010-09-03 18:21:21 +0000137 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100138 tmp &= ~BIT(FL_CS);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000139 tmp |= (val << FL_CS);
140 pci_mmio_writel(tmp, nicintel_spibar + FLA);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000141}
142
143static void nicintel_bitbang_set_sck(int val)
144{
145 uint32_t tmp;
146
147 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100148 tmp &= ~BIT(FL_SCK);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000149 tmp |= (val << FL_SCK);
150 pci_mmio_writel(tmp, nicintel_spibar + FLA);
151}
152
153static void nicintel_bitbang_set_mosi(int val)
154{
155 uint32_t tmp;
156
157 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100158 tmp &= ~BIT(FL_SI);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000159 tmp |= (val << FL_SI);
160 pci_mmio_writel(tmp, nicintel_spibar + FLA);
161}
162
Angel Ponsf0c03fb2021-06-21 13:01:45 +0200163static void nicintel_bitbang_set_sck_set_mosi(int sck, int mosi)
164{
165 uint32_t tmp;
166
167 tmp = pci_mmio_readl(nicintel_spibar + FLA);
168 tmp &= ~BIT(FL_SCK);
169 tmp &= ~BIT(FL_SI);
170 tmp |= (sck << FL_SCK);
171 tmp |= (mosi << FL_SI);
172 pci_mmio_writel(tmp, nicintel_spibar + FLA);
173}
174
Idwer Vollering004f4b72010-09-03 18:21:21 +0000175static int nicintel_bitbang_get_miso(void)
176{
177 uint32_t tmp;
178
179 tmp = pci_mmio_readl(nicintel_spibar + FLA);
180 tmp = (tmp >> FL_SO) & 0x1;
181 return tmp;
182}
183
Angel Ponsf0c03fb2021-06-21 13:01:45 +0200184static int nicintel_bitbang_set_sck_get_miso(int sck)
185{
186 uint32_t tmp;
187
188 tmp = pci_mmio_readl(nicintel_spibar + FLA);
189 tmp &= ~BIT(FL_SCK);
190 tmp |= (sck << FL_SCK);
191 pci_mmio_writel(tmp, nicintel_spibar + FLA);
192 return (tmp >> FL_SO) & 0x1;
193}
194
Idwer Vollering004f4b72010-09-03 18:21:21 +0000195static const struct bitbang_spi_master bitbang_spi_master_nicintel = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200196 .set_cs = nicintel_bitbang_set_cs,
197 .set_sck = nicintel_bitbang_set_sck,
198 .set_mosi = nicintel_bitbang_set_mosi,
199 .set_sck_set_mosi = nicintel_bitbang_set_sck_set_mosi,
200 .set_sck_get_miso = nicintel_bitbang_set_sck_get_miso,
201 .get_miso = nicintel_bitbang_get_miso,
202 .request_bus = nicintel_request_spibus,
203 .release_bus = nicintel_release_spibus,
204 .half_period = 1,
Idwer Vollering004f4b72010-09-03 18:21:21 +0000205};
206
David Hendricks8bb20212011-06-14 01:35:36 +0000207static int nicintel_spi_shutdown(void *data)
208{
209 uint32_t tmp;
210
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000211 /* Disable writes manually. See the comment about EECD in nicintel_spi_init() for details. */
David Hendricks8bb20212011-06-14 01:35:36 +0000212 tmp = pci_mmio_readl(nicintel_spibar + EECD);
213 tmp &= ~FLASH_WRITES_ENABLED;
214 tmp |= FLASH_WRITES_DISABLED;
215 pci_mmio_writel(tmp, nicintel_spibar + EECD);
216
David Hendricks8bb20212011-06-14 01:35:36 +0000217 return 0;
218}
219
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100220static int nicintel_spi_82599_enable_flash(void)
Idwer Vollering004f4b72010-09-03 18:21:21 +0000221{
222 uint32_t tmp;
223
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000224 /* Automatic restore of EECD on shutdown is not possible because EECD
225 * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED,
226 * but other bits with side effects as well. Those other bits must be
227 * left untouched.
228 */
Idwer Vollering004f4b72010-09-03 18:21:21 +0000229 tmp = pci_mmio_readl(nicintel_spibar + EECD);
230 tmp &= ~FLASH_WRITES_DISABLED;
231 tmp |= FLASH_WRITES_ENABLED;
232 pci_mmio_writel(tmp, nicintel_spibar + EECD);
233
Stefan Tauner8ee180d2012-02-27 19:44:16 +0000234 /* test if FWE is really set to allow writes */
235 tmp = pci_mmio_readl(nicintel_spibar + EECD);
236 if ( (tmp & FLASH_WRITES_DISABLED) || !(tmp & FLASH_WRITES_ENABLED) ) {
237 msg_perr("Enabling flash write access failed.\n");
238 return 1;
239 }
240
David Hendricks8bb20212011-06-14 01:35:36 +0000241 if (register_shutdown(nicintel_spi_shutdown, NULL))
242 return 1;
243
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100244 return 0;
245}
246
Richard Hughes93e16252018-12-19 11:54:47 +0000247static int nicintel_spi_i210_enable_flash(void)
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100248{
249 uint32_t tmp;
250
251 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100252 if (tmp & BIT(FL_LOCKED)) {
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100253 msg_perr("Flash is in Secure Mode. Abort.\n");
254 return 1;
255 }
256
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100257 if (!(tmp & BIT(FL_ABORT)))
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100258 return 0;
259
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100260 tmp |= BIT(FL_CLR_ERR);
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100261 pci_mmio_writel(tmp, nicintel_spibar + FLA);
262 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Ricardo Ribalda Delgado75a2a792017-03-23 23:38:04 +0100263 if (!(tmp & BIT(FL_ABORT))) {
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100264 msg_perr("Unable to clear Flash Access Error. Abort\n");
265 return 1;
266 }
267
268 return 0;
269}
270
Thomas Heijligencc853d82021-05-04 15:32:17 +0200271static int nicintel_spi_init(void)
Ricardo Ribalda Delgado26d33d22017-03-22 14:30:52 +0100272{
273 struct pci_dev *dev = NULL;
274
275 if (rget_io_perms())
276 return 1;
277
278 dev = pcidev_init(nics_intel_spi, PCI_BASE_ADDRESS_0);
279 if (!dev)
280 return 1;
281
282 uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
283 if (!io_base_addr)
284 return 1;
285
286 if ((dev->device_id & 0xfff0) == 0x1530) {
287 nicintel_spibar = rphysmap("Intel I210 Gigabit w/ SPI flash", io_base_addr + 0x12000,
288 MEMMAP_SIZE);
289 if (!nicintel_spibar || nicintel_spi_i210_enable_flash())
290 return 1;
291 } else if (dev->device_id < 0x10d8) {
292 nicintel_spibar = rphysmap("Intel Gigabit NIC w/ SPI flash", io_base_addr,
293 MEMMAP_SIZE);
294 if (!nicintel_spibar || nicintel_spi_82599_enable_flash())
295 return 1;
296 } else {
297 nicintel_spibar = rphysmap("Intel 10 Gigabit NIC w/ SPI flash", io_base_addr + 0x10000,
298 MEMMAP_SIZE);
299 if (!nicintel_spibar || nicintel_spi_82599_enable_flash())
300 return 1;
301 }
302
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000303 if (register_spi_bitbang_master(&bitbang_spi_master_nicintel))
Idwer Vollering004f4b72010-09-03 18:21:21 +0000304 return 1;
305
Idwer Vollering004f4b72010-09-03 18:21:21 +0000306 return 0;
307}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200308
309const struct programmer_entry programmer_nicintel_spi = {
310 .name = "nicintel_spi",
311 .type = PCI,
312 .devs.dev = nics_intel_spi,
313 .init = nicintel_spi_init,
314 .map_flash_region = fallback_map,
315 .unmap_flash_region = fallback_unmap,
316 .delay = internal_delay,
317};