blob: 9195c79e56c9b45e734a46b71f581dd9f8839e5c [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
Bill Paulbf8ea492014-03-17 22:07:29 +000022 * Datasheets:
Idwer Vollering004f4b72010-09-03 18:21:21 +000023 * PCI/PCI-X Family of Gigabit Ethernet Controllers Software Developer's Manual
24 * 82540EP/EM, 82541xx, 82544GC/EI, 82545GM/EM, 82546GB/EB, and 82547xx
Bill Paulbf8ea492014-03-17 22:07:29 +000025 * http://www.intel.com/content/www/us/en/ethernet-controllers/pci-pci-x-family-gbe-controllers-software-dev-manual.html
26 *
27 * PCIe GbE Controllers Open Source Software Developer's Manual
28 * http://www.intel.com/content/www/us/en/ethernet-controllers/pcie-gbe-controllers-open-source-manual.html
29 *
30 * Intel 82574 Gigabit Ethernet Controller Family Datasheet
31 * http://www.intel.com/content/www/us/en/ethernet-controllers/82574l-gbe-controller-datasheet.html
Ed Swierk33180df2014-12-05 22:56:13 +000032 *
33 * Intel 82599 10 GbE Controller Datasheet (331520)
34 * 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 +000035 */
36
37#include <stdlib.h>
Stefan Tauner6745d6f2012-08-26 21:50:36 +000038#include <unistd.h>
Idwer Vollering004f4b72010-09-03 18:21:21 +000039#include "flash.h"
40#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000041#include "hwaccess.h"
Idwer Vollering004f4b72010-09-03 18:21:21 +000042
43#define PCI_VENDOR_ID_INTEL 0x8086
Stefan Tauner6745d6f2012-08-26 21:50:36 +000044#define MEMMAP_SIZE getpagesize()
Idwer Vollering004f4b72010-09-03 18:21:21 +000045
Stefan Tauner8ee180d2012-02-27 19:44:16 +000046/* EEPROM/Flash Control & Data Register */
Idwer Vollering004f4b72010-09-03 18:21:21 +000047#define EECD 0x10
Stefan Tauner8ee180d2012-02-27 19:44:16 +000048/* Flash Access Register */
Idwer Vollering004f4b72010-09-03 18:21:21 +000049#define FLA 0x1c
50
51/*
52 * Register bits of EECD.
Stefan Tauner8ee180d2012-02-27 19:44:16 +000053 * Table 13-6
54 *
Idwer Vollering004f4b72010-09-03 18:21:21 +000055 * Bit 04, 05: FWE (Flash Write Enable Control)
Ed Swierk33180df2014-12-05 22:56:13 +000056 * 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 +000057 * 01b = flash writes disabled
58 * 10b = flash writes enabled
59 * 11b = not allowed
60 */
61#define FLASH_WRITES_DISABLED 0x10 /* FWE: 10000b */
62#define FLASH_WRITES_ENABLED 0x20 /* FWE: 100000b */
63
Stefan Tauner8ee180d2012-02-27 19:44:16 +000064/* Flash Access register bits
65 * Table 13-9
66 */
Idwer Vollering004f4b72010-09-03 18:21:21 +000067#define FL_SCK 0
68#define FL_CS 1
69#define FL_SI 2
70#define FL_SO 3
71#define FL_REQ 4
72#define FL_GNT 5
73/* Currently unused */
74// #define FL_BUSY 30
75// #define FL_ER 31
76
77uint8_t *nicintel_spibar;
78
Stefan Tauner4b24a2d2012-12-27 18:40:36 +000079const struct dev_entry nics_intel_spi[] = {
Idwer Volleringbdc48272010-10-05 11:16:14 +000080 {PCI_VENDOR_ID_INTEL, 0x105e, OK, "Intel", "82571EB Gigabit Ethernet Controller"},
Stefan Tauner4b90e6b2011-05-18 01:31:24 +000081 {PCI_VENDOR_ID_INTEL, 0x1076, OK, "Intel", "82541GI Gigabit Ethernet Controller"},
Idwer Vollering004f4b72010-09-03 18:21:21 +000082 {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"},
Idwer Volleringbdc48272010-10-05 11:16:14 +000083 {PCI_VENDOR_ID_INTEL, 0x10b9, OK, "Intel", "82572EI Gigabit Ethernet Controller"},
Bill Paulbf8ea492014-03-17 22:07:29 +000084 {PCI_VENDOR_ID_INTEL, 0x10d3, OK, "Intel", "82574L Gigabit Ethernet Controller"},
Idwer Vollering004f4b72010-09-03 18:21:21 +000085
Ed Swierk33180df2014-12-05 22:56:13 +000086 {PCI_VENDOR_ID_INTEL, 0x10d8, NT, "Intel", "82599 10 Gigabit Unprogrammed Network Controller"},
87 {PCI_VENDOR_ID_INTEL, 0x10f7, NT, "Intel", "82599 10 Gigabit KX4 Dual Port Network Controller"},
88 {PCI_VENDOR_ID_INTEL, 0x10f8, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller"},
89 {PCI_VENDOR_ID_INTEL, 0x10f9, NT, "Intel", "82599 10 Gigabit CX4 Dual Port Network Controller"},
90 {PCI_VENDOR_ID_INTEL, 0x10fb, NT, "Intel", "82599 10-Gigabit SFI/SFP+ Network Controller"},
91 {PCI_VENDOR_ID_INTEL, 0x10fc, OK, "Intel", "82599 10 Gigabit XAUI/BX4 Dual Port Network Controller"},
92 {PCI_VENDOR_ID_INTEL, 0x1517, NT, "Intel", "82599 10 Gigabit KR Network Controller"},
93 {PCI_VENDOR_ID_INTEL, 0x151c, NT, "Intel", "82599 10 Gigabit TN Network Controller"},
94 {PCI_VENDOR_ID_INTEL, 0x1529, NT, "Intel", "82599 10 Gigabit Dual Port Network Controller with FCoE"},
95 {PCI_VENDOR_ID_INTEL, 0x152a, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller with FCoE"},
96 {PCI_VENDOR_ID_INTEL, 0x1557, NT, "Intel", "82599 10 Gigabit SFI Network Controller"},
97
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000098 {0},
Idwer Vollering004f4b72010-09-03 18:21:21 +000099};
100
101static void nicintel_request_spibus(void)
102{
103 uint32_t tmp;
104
105 tmp = pci_mmio_readl(nicintel_spibar + FLA);
106 tmp |= 1 << FL_REQ;
107 pci_mmio_writel(tmp, nicintel_spibar + FLA);
108
109 /* Wait until we are allowed to use the SPI bus. */
110 while (!(pci_mmio_readl(nicintel_spibar + FLA) & (1 << FL_GNT))) ;
111}
112
113static void nicintel_release_spibus(void)
114{
115 uint32_t tmp;
116
117 tmp = pci_mmio_readl(nicintel_spibar + FLA);
118 tmp &= ~(1 << FL_REQ);
119 pci_mmio_writel(tmp, nicintel_spibar + FLA);
120}
121
122static void nicintel_bitbang_set_cs(int val)
123{
124 uint32_t tmp;
125
Idwer Vollering004f4b72010-09-03 18:21:21 +0000126 tmp = pci_mmio_readl(nicintel_spibar + FLA);
127 tmp &= ~(1 << FL_CS);
128 tmp |= (val << FL_CS);
129 pci_mmio_writel(tmp, nicintel_spibar + FLA);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000130}
131
132static void nicintel_bitbang_set_sck(int val)
133{
134 uint32_t tmp;
135
136 tmp = pci_mmio_readl(nicintel_spibar + FLA);
137 tmp &= ~(1 << FL_SCK);
138 tmp |= (val << FL_SCK);
139 pci_mmio_writel(tmp, nicintel_spibar + FLA);
140}
141
142static void nicintel_bitbang_set_mosi(int val)
143{
144 uint32_t tmp;
145
146 tmp = pci_mmio_readl(nicintel_spibar + FLA);
147 tmp &= ~(1 << FL_SI);
148 tmp |= (val << FL_SI);
149 pci_mmio_writel(tmp, nicintel_spibar + FLA);
150}
151
152static int nicintel_bitbang_get_miso(void)
153{
154 uint32_t tmp;
155
156 tmp = pci_mmio_readl(nicintel_spibar + FLA);
157 tmp = (tmp >> FL_SO) & 0x1;
158 return tmp;
159}
160
161static const struct bitbang_spi_master bitbang_spi_master_nicintel = {
162 .type = BITBANG_SPI_MASTER_NICINTEL,
163 .set_cs = nicintel_bitbang_set_cs,
164 .set_sck = nicintel_bitbang_set_sck,
165 .set_mosi = nicintel_bitbang_set_mosi,
166 .get_miso = nicintel_bitbang_get_miso,
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000167 .request_bus = nicintel_request_spibus,
168 .release_bus = nicintel_release_spibus,
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000169 .half_period = 1,
Idwer Vollering004f4b72010-09-03 18:21:21 +0000170};
171
David Hendricks8bb20212011-06-14 01:35:36 +0000172static int nicintel_spi_shutdown(void *data)
173{
174 uint32_t tmp;
175
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000176 /* Disable writes manually. See the comment about EECD in nicintel_spi_init() for details. */
David Hendricks8bb20212011-06-14 01:35:36 +0000177 tmp = pci_mmio_readl(nicintel_spibar + EECD);
178 tmp &= ~FLASH_WRITES_ENABLED;
179 tmp |= FLASH_WRITES_DISABLED;
180 pci_mmio_writel(tmp, nicintel_spibar + EECD);
181
David Hendricks8bb20212011-06-14 01:35:36 +0000182 return 0;
183}
184
Idwer Vollering004f4b72010-09-03 18:21:21 +0000185int nicintel_spi_init(void)
186{
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +0000187 struct pci_dev *dev = NULL;
Idwer Vollering004f4b72010-09-03 18:21:21 +0000188 uint32_t tmp;
189
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000190 if (rget_io_perms())
191 return 1;
Idwer Vollering004f4b72010-09-03 18:21:21 +0000192
Carl-Daniel Hailfingera2faddf2013-01-05 23:52:45 +0000193 dev = pcidev_init(nics_intel_spi, PCI_BASE_ADDRESS_0);
194 if (!dev)
195 return 1;
Idwer Vollering004f4b72010-09-03 18:21:21 +0000196
Stefan Tauner0ccec8f2014-06-01 23:49:03 +0000197 uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
Niklas Söderlund89edf362013-08-23 23:29:23 +0000198 if (!io_base_addr)
199 return 1;
200
Ed Swierk33180df2014-12-05 22:56:13 +0000201 if (dev->device_id < 0x10d8) {
202 nicintel_spibar = rphysmap("Intel Gigabit NIC w/ SPI flash", io_base_addr,
203 MEMMAP_SIZE);
204 } else {
205 nicintel_spibar = rphysmap("Intel 10 Gigabit NIC w/ SPI flash", io_base_addr + 0x10000,
206 MEMMAP_SIZE);
207 }
Niklas Söderlund5d307202013-09-14 09:02:27 +0000208 if (nicintel_spibar == ERROR_PTR)
209 return 1;
210
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000211 /* Automatic restore of EECD on shutdown is not possible because EECD
212 * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED,
213 * but other bits with side effects as well. Those other bits must be
214 * left untouched.
215 */
Idwer Vollering004f4b72010-09-03 18:21:21 +0000216 tmp = pci_mmio_readl(nicintel_spibar + EECD);
217 tmp &= ~FLASH_WRITES_DISABLED;
218 tmp |= FLASH_WRITES_ENABLED;
219 pci_mmio_writel(tmp, nicintel_spibar + EECD);
220
Stefan Tauner8ee180d2012-02-27 19:44:16 +0000221 /* test if FWE is really set to allow writes */
222 tmp = pci_mmio_readl(nicintel_spibar + EECD);
223 if ( (tmp & FLASH_WRITES_DISABLED) || !(tmp & FLASH_WRITES_ENABLED) ) {
224 msg_perr("Enabling flash write access failed.\n");
225 return 1;
226 }
227
David Hendricks8bb20212011-06-14 01:35:36 +0000228 if (register_shutdown(nicintel_spi_shutdown, NULL))
229 return 1;
230
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000231 if (register_spi_bitbang_master(&bitbang_spi_master_nicintel))
Idwer Vollering004f4b72010-09-03 18:21:21 +0000232 return 1;
233
Idwer Vollering004f4b72010-09-03 18:21:21 +0000234 return 0;
235}