blob: 531576c14eeb6d01f40fdba6ed09c8f0c3d18176 [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/*
22 * Datasheet:
23 * PCI/PCI-X Family of Gigabit Ethernet Controllers Software Developer's Manual
24 * 82540EP/EM, 82541xx, 82544GC/EI, 82545GM/EM, 82546GB/EB, and 82547xx
25 * http://download.intel.com/design/network/manuals/8254x_GBe_SDM.pdf
26 */
27
28#include <stdlib.h>
29#include "flash.h"
30#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000031#include "hwaccess.h"
Idwer Vollering004f4b72010-09-03 18:21:21 +000032
33#define PCI_VENDOR_ID_INTEL 0x8086
34
Stefan Tauner8ee180d2012-02-27 19:44:16 +000035/* EEPROM/Flash Control & Data Register */
Idwer Vollering004f4b72010-09-03 18:21:21 +000036#define EECD 0x10
Stefan Tauner8ee180d2012-02-27 19:44:16 +000037/* Flash Access Register */
Idwer Vollering004f4b72010-09-03 18:21:21 +000038#define FLA 0x1c
39
40/*
41 * Register bits of EECD.
Stefan Tauner8ee180d2012-02-27 19:44:16 +000042 * Table 13-6
43 *
Idwer Vollering004f4b72010-09-03 18:21:21 +000044 * Bit 04, 05: FWE (Flash Write Enable Control)
45 * 00b = not allowed
46 * 01b = flash writes disabled
47 * 10b = flash writes enabled
48 * 11b = not allowed
49 */
50#define FLASH_WRITES_DISABLED 0x10 /* FWE: 10000b */
51#define FLASH_WRITES_ENABLED 0x20 /* FWE: 100000b */
52
Stefan Tauner8ee180d2012-02-27 19:44:16 +000053/* Flash Access register bits
54 * Table 13-9
55 */
Idwer Vollering004f4b72010-09-03 18:21:21 +000056#define FL_SCK 0
57#define FL_CS 1
58#define FL_SI 2
59#define FL_SO 3
60#define FL_REQ 4
61#define FL_GNT 5
62/* Currently unused */
63// #define FL_BUSY 30
64// #define FL_ER 31
65
66uint8_t *nicintel_spibar;
67
68const struct pcidev_status nics_intel_spi[] = {
Idwer Volleringbdc48272010-10-05 11:16:14 +000069 {PCI_VENDOR_ID_INTEL, 0x105e, OK, "Intel", "82571EB Gigabit Ethernet Controller"},
Stefan Tauner4b90e6b2011-05-18 01:31:24 +000070 {PCI_VENDOR_ID_INTEL, 0x1076, OK, "Intel", "82541GI Gigabit Ethernet Controller"},
Idwer Vollering004f4b72010-09-03 18:21:21 +000071 {PCI_VENDOR_ID_INTEL, 0x107c, OK, "Intel", "82541PI Gigabit Ethernet Controller"},
Idwer Volleringbdc48272010-10-05 11:16:14 +000072 {PCI_VENDOR_ID_INTEL, 0x10b9, OK, "Intel", "82572EI Gigabit Ethernet Controller"},
Idwer Vollering004f4b72010-09-03 18:21:21 +000073
74 {},
75};
76
77static void nicintel_request_spibus(void)
78{
79 uint32_t tmp;
80
81 tmp = pci_mmio_readl(nicintel_spibar + FLA);
82 tmp |= 1 << FL_REQ;
83 pci_mmio_writel(tmp, nicintel_spibar + FLA);
84
85 /* Wait until we are allowed to use the SPI bus. */
86 while (!(pci_mmio_readl(nicintel_spibar + FLA) & (1 << FL_GNT))) ;
87}
88
89static void nicintel_release_spibus(void)
90{
91 uint32_t tmp;
92
93 tmp = pci_mmio_readl(nicintel_spibar + FLA);
94 tmp &= ~(1 << FL_REQ);
95 pci_mmio_writel(tmp, nicintel_spibar + FLA);
96}
97
98static void nicintel_bitbang_set_cs(int val)
99{
100 uint32_t tmp;
101
Idwer Vollering004f4b72010-09-03 18:21:21 +0000102 tmp = pci_mmio_readl(nicintel_spibar + FLA);
103 tmp &= ~(1 << FL_CS);
104 tmp |= (val << FL_CS);
105 pci_mmio_writel(tmp, nicintel_spibar + FLA);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000106}
107
108static void nicintel_bitbang_set_sck(int val)
109{
110 uint32_t tmp;
111
112 tmp = pci_mmio_readl(nicintel_spibar + FLA);
113 tmp &= ~(1 << FL_SCK);
114 tmp |= (val << FL_SCK);
115 pci_mmio_writel(tmp, nicintel_spibar + FLA);
116}
117
118static void nicintel_bitbang_set_mosi(int val)
119{
120 uint32_t tmp;
121
122 tmp = pci_mmio_readl(nicintel_spibar + FLA);
123 tmp &= ~(1 << FL_SI);
124 tmp |= (val << FL_SI);
125 pci_mmio_writel(tmp, nicintel_spibar + FLA);
126}
127
128static int nicintel_bitbang_get_miso(void)
129{
130 uint32_t tmp;
131
132 tmp = pci_mmio_readl(nicintel_spibar + FLA);
133 tmp = (tmp >> FL_SO) & 0x1;
134 return tmp;
135}
136
137static const struct bitbang_spi_master bitbang_spi_master_nicintel = {
138 .type = BITBANG_SPI_MASTER_NICINTEL,
139 .set_cs = nicintel_bitbang_set_cs,
140 .set_sck = nicintel_bitbang_set_sck,
141 .set_mosi = nicintel_bitbang_set_mosi,
142 .get_miso = nicintel_bitbang_get_miso,
Carl-Daniel Hailfinger28228882010-09-15 00:17:37 +0000143 .request_bus = nicintel_request_spibus,
144 .release_bus = nicintel_release_spibus,
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000145 .half_period = 1,
Idwer Vollering004f4b72010-09-03 18:21:21 +0000146};
147
David Hendricks8bb20212011-06-14 01:35:36 +0000148static int nicintel_spi_shutdown(void *data)
149{
150 uint32_t tmp;
151
152 /* Disable writes manually. See the comment about EECD in
153 * nicintel_spi_init() for details.
154 */
155 tmp = pci_mmio_readl(nicintel_spibar + EECD);
156 tmp &= ~FLASH_WRITES_ENABLED;
157 tmp |= FLASH_WRITES_DISABLED;
158 pci_mmio_writel(tmp, nicintel_spibar + EECD);
159
160 physunmap(nicintel_spibar, 4096);
161 pci_cleanup(pacc);
David Hendricks8bb20212011-06-14 01:35:36 +0000162
163 return 0;
164}
165
Idwer Vollering004f4b72010-09-03 18:21:21 +0000166int nicintel_spi_init(void)
167{
168 uint32_t tmp;
169
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000170 if (rget_io_perms())
171 return 1;
Idwer Vollering004f4b72010-09-03 18:21:21 +0000172
Carl-Daniel Hailfinger40446ee2011-03-07 01:08:09 +0000173 io_base_addr = pcidev_init(PCI_BASE_ADDRESS_0, nics_intel_spi);
Idwer Vollering004f4b72010-09-03 18:21:21 +0000174
175 nicintel_spibar = physmap("Intel Gigabit NIC w/ SPI flash",
176 io_base_addr, 4096);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000177 /* Automatic restore of EECD on shutdown is not possible because EECD
178 * does not only contain FLASH_WRITES_DISABLED|FLASH_WRITES_ENABLED,
179 * but other bits with side effects as well. Those other bits must be
180 * left untouched.
181 */
Idwer Vollering004f4b72010-09-03 18:21:21 +0000182 tmp = pci_mmio_readl(nicintel_spibar + EECD);
183 tmp &= ~FLASH_WRITES_DISABLED;
184 tmp |= FLASH_WRITES_ENABLED;
185 pci_mmio_writel(tmp, nicintel_spibar + EECD);
186
Stefan Tauner8ee180d2012-02-27 19:44:16 +0000187 /* test if FWE is really set to allow writes */
188 tmp = pci_mmio_readl(nicintel_spibar + EECD);
189 if ( (tmp & FLASH_WRITES_DISABLED) || !(tmp & FLASH_WRITES_ENABLED) ) {
190 msg_perr("Enabling flash write access failed.\n");
191 return 1;
192 }
193
David Hendricks8bb20212011-06-14 01:35:36 +0000194 if (register_shutdown(nicintel_spi_shutdown, NULL))
195 return 1;
196
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000197 if (bitbang_spi_init(&bitbang_spi_master_nicintel))
Idwer Vollering004f4b72010-09-03 18:21:21 +0000198 return 1;
199
Idwer Vollering004f4b72010-09-03 18:21:21 +0000200 return 0;
201}