blob: 3e04001b6e5033ecf2c3cd3a08ed58e1ec149893 [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'Callaghan1aa8e9a2019-08-02 17:16:47 +100073#define BIT(x) (1<<(x))
74
Edward O'Callaghanef4e28b2019-06-28 13:18:41 +100075static uint8_t *nicintel_spibar;
uwe6764e922010-09-03 18:21:21 +000076
Patrick Georgi8ae16572017-03-09 15:59:25 +010077const struct dev_entry nics_intel_spi[] = {
hailfinger14ef3112010-10-05 11:16:14 +000078 {PCI_VENDOR_ID_INTEL, 0x105e, OK, "Intel", "82571EB Gigabit Ethernet Controller"},
stefanctc79b8b12011-05-18 01:31:24 +000079 {PCI_VENDOR_ID_INTEL, 0x1076, OK, "Intel", "82541GI Gigabit Ethernet Controller"},
uwe6764e922010-09-03 18:21:21 +000080 {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"},
hailfinger14ef3112010-10-05 11:16:14 +000081 {PCI_VENDOR_ID_INTEL, 0x10b9, OK, "Intel", "82572EI Gigabit Ethernet Controller"},
Patrick Georgi5dc3d882017-03-31 16:57:08 +020082 {PCI_VENDOR_ID_INTEL, 0x10d3, OK, "Intel", "82574L Gigabit Ethernet Controller"},
83
84 {PCI_VENDOR_ID_INTEL, 0x10d8, NT, "Intel", "82599 10 Gigabit Unprogrammed Network Controller"},
85 {PCI_VENDOR_ID_INTEL, 0x10f7, NT, "Intel", "82599 10 Gigabit KX4 Dual Port Network Controller"},
86 {PCI_VENDOR_ID_INTEL, 0x10f8, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller"},
87 {PCI_VENDOR_ID_INTEL, 0x10f9, NT, "Intel", "82599 10 Gigabit CX4 Dual Port Network Controller"},
88 {PCI_VENDOR_ID_INTEL, 0x10fb, NT, "Intel", "82599 10-Gigabit SFI/SFP+ Network Controller"},
89 {PCI_VENDOR_ID_INTEL, 0x10fc, OK, "Intel", "82599 10 Gigabit XAUI/BX4 Dual Port Network Controller"},
90 {PCI_VENDOR_ID_INTEL, 0x1517, NT, "Intel", "82599 10 Gigabit KR Network Controller"},
91 {PCI_VENDOR_ID_INTEL, 0x151c, NT, "Intel", "82599 10 Gigabit TN Network Controller"},
92 {PCI_VENDOR_ID_INTEL, 0x1529, NT, "Intel", "82599 10 Gigabit Dual Port Network Controller with FCoE"},
93 {PCI_VENDOR_ID_INTEL, 0x152a, NT, "Intel", "82599 10 Gigabit Dual Port Backplane Controller with FCoE"},
94 {PCI_VENDOR_ID_INTEL, 0x1557, NT, "Intel", "82599 10 Gigabit SFI Network Controller"},
uwe6764e922010-09-03 18:21:21 +000095
Patrick Georgi8ddfee92017-03-20 14:54:28 +010096 {0},
uwe6764e922010-09-03 18:21:21 +000097};
98
99static void nicintel_request_spibus(void)
100{
101 uint32_t tmp;
102
103 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Edward O'Callaghan1aa8e9a2019-08-02 17:16:47 +1000104 tmp |= BIT(FL_REQ);
uwe6764e922010-09-03 18:21:21 +0000105 pci_mmio_writel(tmp, nicintel_spibar + FLA);
106
107 /* Wait until we are allowed to use the SPI bus. */
Edward O'Callaghan1aa8e9a2019-08-02 17:16:47 +1000108 while (!(pci_mmio_readl(nicintel_spibar + FLA) & BIT(FL_GNT))) ;
uwe6764e922010-09-03 18:21:21 +0000109}
110
111static void nicintel_release_spibus(void)
112{
113 uint32_t tmp;
114
115 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Edward O'Callaghan1aa8e9a2019-08-02 17:16:47 +1000116 tmp &= ~BIT(FL_REQ);
uwe6764e922010-09-03 18:21:21 +0000117 pci_mmio_writel(tmp, nicintel_spibar + FLA);
118}
119
120static void nicintel_bitbang_set_cs(int val)
121{
122 uint32_t tmp;
123
uwe6764e922010-09-03 18:21:21 +0000124 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Edward O'Callaghan1aa8e9a2019-08-02 17:16:47 +1000125 tmp &= ~BIT(FL_CS);
uwe6764e922010-09-03 18:21:21 +0000126 tmp |= (val << FL_CS);
127 pci_mmio_writel(tmp, nicintel_spibar + FLA);
uwe6764e922010-09-03 18:21:21 +0000128}
129
130static void nicintel_bitbang_set_sck(int val)
131{
132 uint32_t tmp;
133
134 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Edward O'Callaghan1aa8e9a2019-08-02 17:16:47 +1000135 tmp &= ~BIT(FL_SCK);
uwe6764e922010-09-03 18:21:21 +0000136 tmp |= (val << FL_SCK);
137 pci_mmio_writel(tmp, nicintel_spibar + FLA);
138}
139
140static void nicintel_bitbang_set_mosi(int val)
141{
142 uint32_t tmp;
143
144 tmp = pci_mmio_readl(nicintel_spibar + FLA);
Edward O'Callaghan1aa8e9a2019-08-02 17:16:47 +1000145 tmp &= ~BIT(FL_SI);
uwe6764e922010-09-03 18:21:21 +0000146 tmp |= (val << FL_SI);
147 pci_mmio_writel(tmp, nicintel_spibar + FLA);
148}
149
150static int nicintel_bitbang_get_miso(void)
151{
152 uint32_t tmp;
153
154 tmp = pci_mmio_readl(nicintel_spibar + FLA);
155 tmp = (tmp >> FL_SO) & 0x1;
156 return tmp;
157}
158
159static const struct bitbang_spi_master bitbang_spi_master_nicintel = {
160 .type = BITBANG_SPI_MASTER_NICINTEL,
161 .set_cs = nicintel_bitbang_set_cs,
162 .set_sck = nicintel_bitbang_set_sck,
163 .set_mosi = nicintel_bitbang_set_mosi,
164 .get_miso = nicintel_bitbang_get_miso,
hailfinger12cba9a2010-09-15 00:17:37 +0000165 .request_bus = nicintel_request_spibus,
166 .release_bus = nicintel_release_spibus,
Patrick Georgie081d5d2017-03-22 21:18:18 +0100167 .half_period = 1,
uwe6764e922010-09-03 18:21:21 +0000168};
169
David Hendricks93784b42016-08-09 17:00:38 -0700170static int nicintel_spi_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +0000171{
172 uint32_t tmp;
173
Patrick Georgie39d6442017-03-22 21:23:35 +0100174 /* Disable writes manually. See the comment about EECD in nicintel_spi_init() for details. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000175 tmp = pci_mmio_readl(nicintel_spibar + EECD);
176 tmp &= ~FLASH_WRITES_ENABLED;
177 tmp |= FLASH_WRITES_DISABLED;
178 pci_mmio_writel(tmp, nicintel_spibar + EECD);
179
dhendrix0ffc2eb2011-06-14 01:35:36 +0000180 return 0;
181}
182
David Hendricksac1d25c2016-08-09 17:00:58 -0700183int nicintel_spi_init(void)
uwe6764e922010-09-03 18:21:21 +0000184{
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200185 struct pci_dev *dev = NULL;
uwe6764e922010-09-03 18:21:21 +0000186 uint32_t tmp;
187
Patrick Georgi2a2d67f2017-03-09 10:15:39 +0100188 if (rget_io_perms())
189 return 1;
uwe6764e922010-09-03 18:21:21 +0000190
Patrick Georgi7c30fa92017-03-28 22:47:12 +0200191 dev = pcidev_init(nics_intel_spi, PCI_BASE_ADDRESS_0);
192 if (!dev)
193 return 1;
194
195 uint32_t io_base_addr = pcidev_readbar(dev, PCI_BASE_ADDRESS_0);
196 if (!io_base_addr)
197 return 1;
uwe6764e922010-09-03 18:21:21 +0000198
Patrick Georgi5dc3d882017-03-31 16:57:08 +0200199 if (dev->device_id < 0x10d8) {
200 nicintel_spibar = rphysmap("Intel Gigabit NIC w/ SPI flash", io_base_addr,
201 MEMMAP_SIZE);
202 } else {
203 nicintel_spibar = rphysmap("Intel 10 Gigabit NIC w/ SPI flash", io_base_addr + 0x10000,
204 MEMMAP_SIZE);
205 }
206 if (nicintel_spibar == ERROR_PTR)
207 return 1;
208
hailfinger1e2e3442011-05-03 21:49:41 +0000209 /* Automatic restore of EECD on shutdown is not possible because EECD
210 * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED,
211 * but other bits with side effects as well. Those other bits must be
212 * left untouched.
213 */
uwe6764e922010-09-03 18:21:21 +0000214 tmp = pci_mmio_readl(nicintel_spibar + EECD);
215 tmp &= ~FLASH_WRITES_DISABLED;
216 tmp |= FLASH_WRITES_ENABLED;
217 pci_mmio_writel(tmp, nicintel_spibar + EECD);
218
Patrick Georgi5dc3d882017-03-31 16:57:08 +0200219 /* test if FWE is really set to allow writes */
220 tmp = pci_mmio_readl(nicintel_spibar + EECD);
221 if ( (tmp & FLASH_WRITES_DISABLED) || !(tmp & FLASH_WRITES_ENABLED) ) {
222 msg_perr("Enabling flash write access failed.\n");
223 return 1;
224 }
225
dhendrix0ffc2eb2011-06-14 01:35:36 +0000226 if (register_shutdown(nicintel_spi_shutdown, NULL))
227 return 1;
228
Craig Hesling65eb8812019-08-01 09:33:56 -0700229 if (register_spi_bitbang_master(&bitbang_spi_master_nicintel))
uwe6764e922010-09-03 18:21:21 +0000230 return 1;
231
uwe6764e922010-09-03 18:21:21 +0000232 return 0;
233}