blob: 1a45229711d581f3cc2c49273d2164d447b9d1ca [file] [log] [blame]
uwe6764e922010-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.
15 *
uwe6764e922010-09-03 18:21:21 +000016 */
17
18/*
Patrick Georgie39d6442017-03-22 21:23:35 +010019 * Datasheets:
uwe6764e922010-09-03 18:21:21 +000020 * PCI/PCI-X Family of Gigabit Ethernet Controllers Software Developer's Manual
21 * 82540EP/EM, 82541xx, 82544GC/EI, 82545GM/EM, 82546GB/EB, and 82547xx
Patrick Georgie39d6442017-03-22 21:23:35 +010022 * http://www.intel.com/content/www/us/en/ethernet-controllers/pci-pci-x-family-gbe-controllers-software-dev-manual.html
23 *
24 * PCIe GbE Controllers Open Source Software Developer's Manual
25 * http://www.intel.com/content/www/us/en/ethernet-controllers/pcie-gbe-controllers-open-source-manual.html
26 *
27 * Intel 82574 Gigabit Ethernet Controller Family Datasheet
28 * http://www.intel.com/content/www/us/en/ethernet-controllers/82574l-gbe-controller-datasheet.html
29 *
30 * Intel 82599 10 GbE Controller Datasheet (331520)
31 * http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/82599-10-gbe-controller-datasheet.pdf
uwe6764e922010-09-03 18:21:21 +000032 */
33
34#include <stdlib.h>
Patrick Georgi5dc3d882017-03-31 16:57:08 +020035#include <unistd.h>
uwe6764e922010-09-03 18:21:21 +000036#include "flash.h"
37#include "programmer.h"
Patrick Georgi5dc3d882017-03-31 16:57:08 +020038#include "hwaccess.h"
uwe6764e922010-09-03 18:21:21 +000039
40#define PCI_VENDOR_ID_INTEL 0x8086
Patrick Georgi5dc3d882017-03-31 16:57:08 +020041#define MEMMAP_SIZE getpagesize()
uwe6764e922010-09-03 18:21:21 +000042
Patrick Georgie39d6442017-03-22 21:23:35 +010043/* EEPROM/Flash Control & Data Register */
uwe6764e922010-09-03 18:21:21 +000044#define EECD 0x10
Patrick Georgie39d6442017-03-22 21:23:35 +010045/* Flash Access Register */
uwe6764e922010-09-03 18:21:21 +000046#define FLA 0x1c
47
48/*
49 * Register bits of EECD.
Patrick Georgie39d6442017-03-22 21:23:35 +010050 * Table 13-6
51 *
uwe6764e922010-09-03 18:21:21 +000052 * Bit 04, 05: FWE (Flash Write Enable Control)
Patrick Georgie39d6442017-03-22 21:23:35 +010053 * 00b = not allowed (on some cards this sends an erase command if bit 31 (FL_ER) of FLA is set)
uwe6764e922010-09-03 18:21:21 +000054 * 01b = flash writes disabled
55 * 10b = flash writes enabled
56 * 11b = not allowed
57 */
58#define FLASH_WRITES_DISABLED 0x10 /* FWE: 10000b */
59#define FLASH_WRITES_ENABLED 0x20 /* FWE: 100000b */
60
Patrick Georgie39d6442017-03-22 21:23:35 +010061/* Flash Access register bits
62 * Table 13-9
63 */
uwe6764e922010-09-03 18:21:21 +000064#define FL_SCK 0
65#define FL_CS 1
66#define FL_SI 2
67#define FL_SO 3
68#define FL_REQ 4
69#define FL_GNT 5
70/* Currently unused */
71// #define FL_BUSY 30
72// #define FL_ER 31
73
Edward O'Callaghanef4e28b2019-06-28 13:18:41 +100074static uint8_t *nicintel_spibar;
uwe6764e922010-09-03 18:21:21 +000075
Patrick Georgi8ae16572017-03-09 15:59:25 +010076const struct dev_entry nics_intel_spi[] = {
hailfinger14ef3112010-10-05 11:16:14 +000077 {PCI_VENDOR_ID_INTEL, 0x105e, OK, "Intel", "82571EB Gigabit Ethernet Controller"},
stefanctc79b8b12011-05-18 01:31:24 +000078 {PCI_VENDOR_ID_INTEL, 0x1076, OK, "Intel", "82541GI Gigabit Ethernet Controller"},
uwe6764e922010-09-03 18:21:21 +000079 {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"},
hailfinger14ef3112010-10-05 11:16:14 +000080 {PCI_VENDOR_ID_INTEL, 0x10b9, OK, "Intel", "82572EI Gigabit Ethernet Controller"},
Patrick Georgi5dc3d882017-03-31 16:57:08 +020081 {PCI_VENDOR_ID_INTEL, 0x10d3, OK, "Intel", "82574L Gigabit Ethernet Controller"},
82
83 {PCI_VENDOR_ID_INTEL, 0x10d8, NT, "Intel", "82599 10 Gigabit Unprogrammed Network Controller"},
84 {PCI_VENDOR_ID_INTEL, 0x10f7, NT, "Intel", "82599 10 Gigabit KX4 Dual Port Network Controller"},
85 {PCI_VENDOR_ID_INTEL, 0x10f8, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller"},
86 {PCI_VENDOR_ID_INTEL, 0x10f9, NT, "Intel", "82599 10 Gigabit CX4 Dual Port Network Controller"},
87 {PCI_VENDOR_ID_INTEL, 0x10fb, NT, "Intel", "82599 10-Gigabit SFI/SFP+ Network Controller"},
88 {PCI_VENDOR_ID_INTEL, 0x10fc, OK, "Intel", "82599 10 Gigabit XAUI/BX4 Dual Port Network Controller"},
89 {PCI_VENDOR_ID_INTEL, 0x1517, NT, "Intel", "82599 10 Gigabit KR Network Controller"},
90 {PCI_VENDOR_ID_INTEL, 0x151c, NT, "Intel", "82599 10 Gigabit TN Network Controller"},
91 {PCI_VENDOR_ID_INTEL, 0x1529, NT, "Intel", "82599 10 Gigabit Dual Port Network Controller with FCoE"},
92 {PCI_VENDOR_ID_INTEL, 0x152a, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller with FCoE"},
93 {PCI_VENDOR_ID_INTEL, 0x1557, NT, "Intel", "82599 10 Gigabit SFI Network Controller"},
uwe6764e922010-09-03 18:21:21 +000094
Patrick Georgi8ddfee92017-03-20 14:54:28 +010095 {0},
uwe6764e922010-09-03 18:21:21 +000096};
97
98static void nicintel_request_spibus(void)
99{
100 uint32_t tmp;
101
102 tmp = pci_mmio_readl(nicintel_spibar + FLA);
103 tmp |= 1 << FL_REQ;
104 pci_mmio_writel(tmp, nicintel_spibar + FLA);
105
106 /* Wait until we are allowed to use the SPI bus. */
107 while (!(pci_mmio_readl(nicintel_spibar + FLA) & (1 << FL_GNT))) ;
108}
109
110static void nicintel_release_spibus(void)
111{
112 uint32_t tmp;
113
114 tmp = pci_mmio_readl(nicintel_spibar + FLA);
115 tmp &= ~(1 << FL_REQ);
116 pci_mmio_writel(tmp, nicintel_spibar + FLA);
117}
118
119static void nicintel_bitbang_set_cs(int val)
120{
121 uint32_t tmp;
122
uwe6764e922010-09-03 18:21:21 +0000123 tmp = pci_mmio_readl(nicintel_spibar + FLA);
124 tmp &= ~(1 << FL_CS);
125 tmp |= (val << FL_CS);
126 pci_mmio_writel(tmp, nicintel_spibar + FLA);
uwe6764e922010-09-03 18:21:21 +0000127}
128
129static void nicintel_bitbang_set_sck(int val)
130{
131 uint32_t tmp;
132
133 tmp = pci_mmio_readl(nicintel_spibar + FLA);
134 tmp &= ~(1 << FL_SCK);
135 tmp |= (val << FL_SCK);
136 pci_mmio_writel(tmp, nicintel_spibar + FLA);
137}
138
139static void nicintel_bitbang_set_mosi(int val)
140{
141 uint32_t tmp;
142
143 tmp = pci_mmio_readl(nicintel_spibar + FLA);
144 tmp &= ~(1 << FL_SI);
145 tmp |= (val << FL_SI);
146 pci_mmio_writel(tmp, nicintel_spibar + FLA);
147}
148
149static int nicintel_bitbang_get_miso(void)
150{
151 uint32_t tmp;
152
153 tmp = pci_mmio_readl(nicintel_spibar + FLA);
154 tmp = (tmp >> FL_SO) & 0x1;
155 return tmp;
156}
157
158static const struct bitbang_spi_master bitbang_spi_master_nicintel = {
159 .type = BITBANG_SPI_MASTER_NICINTEL,
160 .set_cs = nicintel_bitbang_set_cs,
161 .set_sck = nicintel_bitbang_set_sck,
162 .set_mosi = nicintel_bitbang_set_mosi,
163 .get_miso = nicintel_bitbang_get_miso,
hailfinger12cba9a2010-09-15 00:17:37 +0000164 .request_bus = nicintel_request_spibus,
165 .release_bus = nicintel_release_spibus,
Patrick Georgie081d5d2017-03-22 21:18:18 +0100166 .half_period = 1,
uwe6764e922010-09-03 18:21:21 +0000167};
168
David Hendricks93784b42016-08-09 17:00:38 -0700169static int nicintel_spi_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +0000170{
171 uint32_t tmp;
172
Patrick Georgie39d6442017-03-22 21:23:35 +0100173 /* Disable writes manually. See the comment about EECD in nicintel_spi_init() for details. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000174 tmp = pci_mmio_readl(nicintel_spibar + EECD);
175 tmp &= ~FLASH_WRITES_ENABLED;
176 tmp |= FLASH_WRITES_DISABLED;
177 pci_mmio_writel(tmp, nicintel_spibar + EECD);
178
dhendrix0ffc2eb2011-06-14 01:35:36 +0000179 return 0;
180}
181
David Hendricksac1d25c2016-08-09 17:00:58 -0700182int nicintel_spi_init(void)
uwe6764e922010-09-03 18:21:21 +0000183{
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200184 struct pci_dev *dev = NULL;
uwe6764e922010-09-03 18:21:21 +0000185 uint32_t tmp;
186
Patrick Georgi2a2d67f2017-03-09 10:15:39 +0100187 if (rget_io_perms())
188 return 1;
uwe6764e922010-09-03 18:21:21 +0000189
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200190 dev = pcidev_init(nics_intel_spi, PCI_BASE_ADDRESS_0);
191 if (!dev)
192 return 1;
193
194 uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
195 if (!io_base_addr)
196 return 1;
uwe6764e922010-09-03 18:21:21 +0000197
Patrick Georgi5dc3d882017-03-31 16:57:08 +0200198 if (dev->device_id < 0x10d8) {
199 nicintel_spibar = rphysmap("Intel Gigabit NIC w/ SPI flash", io_base_addr,
200 MEMMAP_SIZE);
201 } else {
202 nicintel_spibar = rphysmap("Intel 10 Gigabit NIC w/ SPI flash", io_base_addr + 0x10000,
203 MEMMAP_SIZE);
204 }
205 if (nicintel_spibar == ERROR_PTR)
206 return 1;
207
hailfinger1e2e3442011-05-03 21:49:41 +0000208 /* Automatic restore of EECD on shutdown is not possible because EECD
209 * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED,
210 * but other bits with side effects as well. Those other bits must be
211 * left untouched.
212 */
uwe6764e922010-09-03 18:21:21 +0000213 tmp = pci_mmio_readl(nicintel_spibar + EECD);
214 tmp &= ~FLASH_WRITES_DISABLED;
215 tmp |= FLASH_WRITES_ENABLED;
216 pci_mmio_writel(tmp, nicintel_spibar + EECD);
217
Patrick Georgi5dc3d882017-03-31 16:57:08 +0200218 /* test if FWE is really set to allow writes */
219 tmp = pci_mmio_readl(nicintel_spibar + EECD);
220 if ( (tmp & FLASH_WRITES_DISABLED) || !(tmp & FLASH_WRITES_ENABLED) ) {
221 msg_perr("Enabling flash write access failed.\n");
222 return 1;
223 }
224
dhendrix0ffc2eb2011-06-14 01:35:36 +0000225 if (register_shutdown(nicintel_spi_shutdown, NULL))
226 return 1;
227
Craig Hesling65eb8812019-08-01 09:33:56 -0700228 if (register_spi_bitbang_master(&bitbang_spi_master_nicintel))
uwe6764e922010-09-03 18:21:21 +0000229 return 1;
230
uwe6764e922010-09-03 18:21:21 +0000231 return 0;
232}