blob: 86d4b12f0354b7817bea9bdbc8bef2d684a44e96 [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.
uwe6764e922010-09-03 18:21:21 +000015 */
16
17/*
Patrick Georgie39d6442017-03-22 21:23:35 +010018 * Datasheets:
uwe6764e922010-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
Patrick Georgie39d6442017-03-22 21:23:35 +010021 * 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
28 *
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
uwe6764e922010-09-03 18:21:21 +000031 */
32
33#include <stdlib.h>
Patrick Georgi5dc3d882017-03-31 16:57:08 +020034#include <unistd.h>
uwe6764e922010-09-03 18:21:21 +000035#include "flash.h"
36#include "programmer.h"
Patrick Georgi5dc3d882017-03-31 16:57:08 +020037#include "hwaccess.h"
uwe6764e922010-09-03 18:21:21 +000038
39#define PCI_VENDOR_ID_INTEL 0x8086
Patrick Georgi5dc3d882017-03-31 16:57:08 +020040#define MEMMAP_SIZE getpagesize()
uwe6764e922010-09-03 18:21:21 +000041
Patrick Georgie39d6442017-03-22 21:23:35 +010042/* EEPROM/Flash Control & Data Register */
uwe6764e922010-09-03 18:21:21 +000043#define EECD 0x10
Patrick Georgie39d6442017-03-22 21:23:35 +010044/* Flash Access Register */
uwe6764e922010-09-03 18:21:21 +000045#define FLA 0x1c
46
47/*
48 * Register bits of EECD.
Patrick Georgie39d6442017-03-22 21:23:35 +010049 * Table 13-6
50 *
uwe6764e922010-09-03 18:21:21 +000051 * Bit 04, 05: FWE (Flash Write Enable Control)
Patrick Georgie39d6442017-03-22 21:23:35 +010052 * 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 +000053 * 01b = flash writes disabled
54 * 10b = flash writes enabled
55 * 11b = not allowed
56 */
57#define FLASH_WRITES_DISABLED 0x10 /* FWE: 10000b */
58#define FLASH_WRITES_ENABLED 0x20 /* FWE: 100000b */
59
Patrick Georgie39d6442017-03-22 21:23:35 +010060/* Flash Access register bits
61 * Table 13-9
62 */
uwe6764e922010-09-03 18:21:21 +000063#define FL_SCK 0
64#define FL_CS 1
65#define FL_SI 2
66#define FL_SO 3
67#define FL_REQ 4
68#define FL_GNT 5
69/* Currently unused */
70// #define FL_BUSY 30
71// #define FL_ER 31
72
Edward O'Callaghanef4e28b2019-06-28 13:18:41 +100073static uint8_t *nicintel_spibar;
uwe6764e922010-09-03 18:21:21 +000074
Patrick Georgi8ae16572017-03-09 15:59:25 +010075const struct dev_entry nics_intel_spi[] = {
hailfinger14ef3112010-10-05 11:16:14 +000076 {PCI_VENDOR_ID_INTEL, 0x105e, OK, "Intel", "82571EB Gigabit Ethernet Controller"},
stefanctc79b8b12011-05-18 01:31:24 +000077 {PCI_VENDOR_ID_INTEL, 0x1076, OK, "Intel", "82541GI Gigabit Ethernet Controller"},
uwe6764e922010-09-03 18:21:21 +000078 {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"},
hailfinger14ef3112010-10-05 11:16:14 +000079 {PCI_VENDOR_ID_INTEL, 0x10b9, OK, "Intel", "82572EI Gigabit Ethernet Controller"},
Patrick Georgi5dc3d882017-03-31 16:57:08 +020080 {PCI_VENDOR_ID_INTEL, 0x10d3, OK, "Intel", "82574L Gigabit Ethernet Controller"},
81
82 {PCI_VENDOR_ID_INTEL, 0x10d8, NT, "Intel", "82599 10 Gigabit Unprogrammed Network Controller"},
83 {PCI_VENDOR_ID_INTEL, 0x10f7, NT, "Intel", "82599 10 Gigabit KX4 Dual Port Network Controller"},
84 {PCI_VENDOR_ID_INTEL, 0x10f8, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller"},
85 {PCI_VENDOR_ID_INTEL, 0x10f9, NT, "Intel", "82599 10 Gigabit CX4 Dual Port Network Controller"},
86 {PCI_VENDOR_ID_INTEL, 0x10fb, NT, "Intel", "82599 10-Gigabit SFI/SFP+ Network Controller"},
87 {PCI_VENDOR_ID_INTEL, 0x10fc, OK, "Intel", "82599 10 Gigabit XAUI/BX4 Dual Port Network Controller"},
88 {PCI_VENDOR_ID_INTEL, 0x1517, NT, "Intel", "82599 10 Gigabit KR Network Controller"},
89 {PCI_VENDOR_ID_INTEL, 0x151c, NT, "Intel", "82599 10 Gigabit TN Network Controller"},
90 {PCI_VENDOR_ID_INTEL, 0x1529, NT, "Intel", "82599 10 Gigabit Dual Port Network Controller with FCoE"},
91 {PCI_VENDOR_ID_INTEL, 0x152a, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller with FCoE"},
92 {PCI_VENDOR_ID_INTEL, 0x1557, NT, "Intel", "82599 10 Gigabit SFI Network Controller"},
uwe6764e922010-09-03 18:21:21 +000093
Patrick Georgi8ddfee92017-03-20 14:54:28 +010094 {0},
uwe6764e922010-09-03 18:21:21 +000095};
96
97static void nicintel_request_spibus(void)
98{
99 uint32_t tmp;
100
101 tmp = pci_mmio_readl(nicintel_spibar + FLA);
102 tmp |= 1 << FL_REQ;
103 pci_mmio_writel(tmp, nicintel_spibar + FLA);
104
105 /* Wait until we are allowed to use the SPI bus. */
106 while (!(pci_mmio_readl(nicintel_spibar + FLA) & (1 << FL_GNT))) ;
107}
108
109static void nicintel_release_spibus(void)
110{
111 uint32_t tmp;
112
113 tmp = pci_mmio_readl(nicintel_spibar + FLA);
114 tmp &= ~(1 << FL_REQ);
115 pci_mmio_writel(tmp, nicintel_spibar + FLA);
116}
117
118static void nicintel_bitbang_set_cs(int val)
119{
120 uint32_t tmp;
121
uwe6764e922010-09-03 18:21:21 +0000122 tmp = pci_mmio_readl(nicintel_spibar + FLA);
123 tmp &= ~(1 << FL_CS);
124 tmp |= (val << FL_CS);
125 pci_mmio_writel(tmp, nicintel_spibar + FLA);
uwe6764e922010-09-03 18:21:21 +0000126}
127
128static void nicintel_bitbang_set_sck(int val)
129{
130 uint32_t tmp;
131
132 tmp = pci_mmio_readl(nicintel_spibar + FLA);
133 tmp &= ~(1 << FL_SCK);
134 tmp |= (val << FL_SCK);
135 pci_mmio_writel(tmp, nicintel_spibar + FLA);
136}
137
138static void nicintel_bitbang_set_mosi(int val)
139{
140 uint32_t tmp;
141
142 tmp = pci_mmio_readl(nicintel_spibar + FLA);
143 tmp &= ~(1 << FL_SI);
144 tmp |= (val << FL_SI);
145 pci_mmio_writel(tmp, nicintel_spibar + FLA);
146}
147
148static int nicintel_bitbang_get_miso(void)
149{
150 uint32_t tmp;
151
152 tmp = pci_mmio_readl(nicintel_spibar + FLA);
153 tmp = (tmp >> FL_SO) & 0x1;
154 return tmp;
155}
156
157static const struct bitbang_spi_master bitbang_spi_master_nicintel = {
158 .type = BITBANG_SPI_MASTER_NICINTEL,
159 .set_cs = nicintel_bitbang_set_cs,
160 .set_sck = nicintel_bitbang_set_sck,
161 .set_mosi = nicintel_bitbang_set_mosi,
162 .get_miso = nicintel_bitbang_get_miso,
hailfinger12cba9a2010-09-15 00:17:37 +0000163 .request_bus = nicintel_request_spibus,
164 .release_bus = nicintel_release_spibus,
Patrick Georgie081d5d2017-03-22 21:18:18 +0100165 .half_period = 1,
uwe6764e922010-09-03 18:21:21 +0000166};
167
David Hendricks93784b42016-08-09 17:00:38 -0700168static int nicintel_spi_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +0000169{
170 uint32_t tmp;
171
Patrick Georgie39d6442017-03-22 21:23:35 +0100172 /* Disable writes manually. See the comment about EECD in nicintel_spi_init() for details. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000173 tmp = pci_mmio_readl(nicintel_spibar + EECD);
174 tmp &= ~FLASH_WRITES_ENABLED;
175 tmp |= FLASH_WRITES_DISABLED;
176 pci_mmio_writel(tmp, nicintel_spibar + EECD);
177
dhendrix0ffc2eb2011-06-14 01:35:36 +0000178 return 0;
179}
180
David Hendricksac1d25c2016-08-09 17:00:58 -0700181int nicintel_spi_init(void)
uwe6764e922010-09-03 18:21:21 +0000182{
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200183 struct pci_dev *dev = NULL;
uwe6764e922010-09-03 18:21:21 +0000184 uint32_t tmp;
185
Patrick Georgi2a2d67f2017-03-09 10:15:39 +0100186 if (rget_io_perms())
187 return 1;
uwe6764e922010-09-03 18:21:21 +0000188
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200189 dev = pcidev_init(nics_intel_spi, PCI_BASE_ADDRESS_0);
190 if (!dev)
191 return 1;
192
193 uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
194 if (!io_base_addr)
195 return 1;
uwe6764e922010-09-03 18:21:21 +0000196
Patrick Georgi5dc3d882017-03-31 16:57:08 +0200197 if (dev->device_id < 0x10d8) {
198 nicintel_spibar = rphysmap("Intel Gigabit NIC w/ SPI flash", io_base_addr,
199 MEMMAP_SIZE);
200 } else {
201 nicintel_spibar = rphysmap("Intel 10 Gigabit NIC w/ SPI flash", io_base_addr + 0x10000,
202 MEMMAP_SIZE);
203 }
204 if (nicintel_spibar == ERROR_PTR)
205 return 1;
206
hailfinger1e2e3442011-05-03 21:49:41 +0000207 /* Automatic restore of EECD on shutdown is not possible because EECD
208 * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED,
209 * but other bits with side effects as well. Those other bits must be
210 * left untouched.
211 */
uwe6764e922010-09-03 18:21:21 +0000212 tmp = pci_mmio_readl(nicintel_spibar + EECD);
213 tmp &= ~FLASH_WRITES_DISABLED;
214 tmp |= FLASH_WRITES_ENABLED;
215 pci_mmio_writel(tmp, nicintel_spibar + EECD);
216
Patrick Georgi5dc3d882017-03-31 16:57:08 +0200217 /* test if FWE is really set to allow writes */
218 tmp = pci_mmio_readl(nicintel_spibar + EECD);
219 if ( (tmp & FLASH_WRITES_DISABLED) || !(tmp & FLASH_WRITES_ENABLED) ) {
220 msg_perr("Enabling flash write access failed.\n");
221 return 1;
222 }
223
dhendrix0ffc2eb2011-06-14 01:35:36 +0000224 if (register_shutdown(nicintel_spi_shutdown, NULL))
225 return 1;
226
Craig Hesling65eb8812019-08-01 09:33:56 -0700227 if (register_spi_bitbang_master(&bitbang_spi_master_nicintel))
uwe6764e922010-09-03 18:21:21 +0000228 return 1;
229
uwe6764e922010-09-03 18:21:21 +0000230 return 0;
231}