blob: a59c808f9ebed32b06b359b9175b8b039e85ebfd [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 *
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/*
Patrick Georgie39d6442017-03-22 21:23:35 +010022 * Datasheets:
uwe6764e922010-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
Patrick Georgie39d6442017-03-22 21:23:35 +010025 * 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
32 *
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
uwe6764e922010-09-03 18:21:21 +000035 */
36
37#include <stdlib.h>
38#include "flash.h"
39#include "programmer.h"
40
41#define PCI_VENDOR_ID_INTEL 0x8086
42
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
74uint8_t *nicintel_spibar;
75
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"},
uwe6764e922010-09-03 18:21:21 +000081
Patrick Georgi8ddfee92017-03-20 14:54:28 +010082 {0},
uwe6764e922010-09-03 18:21:21 +000083};
84
85static void nicintel_request_spibus(void)
86{
87 uint32_t tmp;
88
89 tmp = pci_mmio_readl(nicintel_spibar + FLA);
90 tmp |= 1 << FL_REQ;
91 pci_mmio_writel(tmp, nicintel_spibar + FLA);
92
93 /* Wait until we are allowed to use the SPI bus. */
94 while (!(pci_mmio_readl(nicintel_spibar + FLA) & (1 << FL_GNT))) ;
95}
96
97static void nicintel_release_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
106static void nicintel_bitbang_set_cs(int val)
107{
108 uint32_t tmp;
109
uwe6764e922010-09-03 18:21:21 +0000110 tmp = pci_mmio_readl(nicintel_spibar + FLA);
111 tmp &= ~(1 << FL_CS);
112 tmp |= (val << FL_CS);
113 pci_mmio_writel(tmp, nicintel_spibar + FLA);
uwe6764e922010-09-03 18:21:21 +0000114}
115
116static void nicintel_bitbang_set_sck(int val)
117{
118 uint32_t tmp;
119
120 tmp = pci_mmio_readl(nicintel_spibar + FLA);
121 tmp &= ~(1 << FL_SCK);
122 tmp |= (val << FL_SCK);
123 pci_mmio_writel(tmp, nicintel_spibar + FLA);
124}
125
126static void nicintel_bitbang_set_mosi(int val)
127{
128 uint32_t tmp;
129
130 tmp = pci_mmio_readl(nicintel_spibar + FLA);
131 tmp &= ~(1 << FL_SI);
132 tmp |= (val << FL_SI);
133 pci_mmio_writel(tmp, nicintel_spibar + FLA);
134}
135
136static int nicintel_bitbang_get_miso(void)
137{
138 uint32_t tmp;
139
140 tmp = pci_mmio_readl(nicintel_spibar + FLA);
141 tmp = (tmp >> FL_SO) & 0x1;
142 return tmp;
143}
144
145static const struct bitbang_spi_master bitbang_spi_master_nicintel = {
146 .type = BITBANG_SPI_MASTER_NICINTEL,
147 .set_cs = nicintel_bitbang_set_cs,
148 .set_sck = nicintel_bitbang_set_sck,
149 .set_mosi = nicintel_bitbang_set_mosi,
150 .get_miso = nicintel_bitbang_get_miso,
hailfinger12cba9a2010-09-15 00:17:37 +0000151 .request_bus = nicintel_request_spibus,
152 .release_bus = nicintel_release_spibus,
Patrick Georgie081d5d2017-03-22 21:18:18 +0100153 .half_period = 1,
uwe6764e922010-09-03 18:21:21 +0000154};
155
David Hendricks93784b42016-08-09 17:00:38 -0700156static int nicintel_spi_shutdown(void *data)
dhendrix0ffc2eb2011-06-14 01:35:36 +0000157{
158 uint32_t tmp;
159
Patrick Georgie39d6442017-03-22 21:23:35 +0100160 /* Disable writes manually. See the comment about EECD in nicintel_spi_init() for details. */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000161 tmp = pci_mmio_readl(nicintel_spibar + EECD);
162 tmp &= ~FLASH_WRITES_ENABLED;
163 tmp |= FLASH_WRITES_DISABLED;
164 pci_mmio_writel(tmp, nicintel_spibar + EECD);
165
166 physunmap(nicintel_spibar, 4096);
167 pci_cleanup(pacc);
dhendrix0ffc2eb2011-06-14 01:35:36 +0000168
169 return 0;
170}
171
David Hendricksac1d25c2016-08-09 17:00:58 -0700172int nicintel_spi_init(void)
uwe6764e922010-09-03 18:21:21 +0000173{
174 uint32_t tmp;
175
Patrick Georgi2a2d67f2017-03-09 10:15:39 +0100176 if (rget_io_perms())
177 return 1;
uwe6764e922010-09-03 18:21:21 +0000178
hailfinger0d703d42011-03-07 01:08:09 +0000179 io_base_addr = pcidev_init(PCI_BASE_ADDRESS_0, nics_intel_spi);
uwe6764e922010-09-03 18:21:21 +0000180
181 nicintel_spibar = physmap("Intel Gigabit NIC w/ SPI flash",
182 io_base_addr, 4096);
hailfinger1e2e3442011-05-03 21:49:41 +0000183 /* Automatic restore of EECD on shutdown is not possible because EECD
184 * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED,
185 * but other bits with side effects as well. Those other bits must be
186 * left untouched.
187 */
uwe6764e922010-09-03 18:21:21 +0000188 tmp = pci_mmio_readl(nicintel_spibar + EECD);
189 tmp &= ~FLASH_WRITES_DISABLED;
190 tmp |= FLASH_WRITES_ENABLED;
191 pci_mmio_writel(tmp, nicintel_spibar + EECD);
192
dhendrix0ffc2eb2011-06-14 01:35:36 +0000193 if (register_shutdown(nicintel_spi_shutdown, NULL))
194 return 1;
195
Patrick Georgie081d5d2017-03-22 21:18:18 +0100196 if (register_spi_bitbang_master(&bitbang_spi_master_nicintel))
uwe6764e922010-09-03 18:21:21 +0000197 return 1;
198
uwe6764e922010-09-03 18:21:21 +0000199 return 0;
200}