blob: afa420b9ce36aab302f9be2e7f05347902e42156 [file] [log] [blame]
Dominik Geyerb46acba2008-05-16 12:55:55 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2008 Stefan Wildemann <stefan.wildemann@kontron.com>
5 * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com>
6 * Copyright (C) 2008 Dominik Geyer <dominik.geyer@kontron.com>
Stefan Reinauera9424d52008-06-27 16:28:34 +00007 * Copyright (C) 2008 coresystems GmbH <info@coresystems.de>
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00008 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
Stefan Tauner8b391b82011-08-09 01:49:34 +00009 * Copyright (C) 2011 Stefan Tauner
Dominik Geyerb46acba2008-05-16 12:55:55 +000010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Dominik Geyerb46acba2008-05-16 12:55:55 +000024 */
25
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000026#if defined(__i386__) || defined(__x86_64__)
27
Dominik Geyerb46acba2008-05-16 12:55:55 +000028#include <string.h>
Stefan Taunerd0c5dc22011-10-20 12:57:14 +000029#include <stdlib.h>
Dominik Geyerb46acba2008-05-16 12:55:55 +000030#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000031#include "programmer.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000032#include "spi.h"
Stefan Tauner1e146392011-09-15 23:52:55 +000033#include "ich_descriptors.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000034
Stefan Reinauera9424d52008-06-27 16:28:34 +000035/* ICH9 controller register definition */
Stefan Tauner55206942011-06-11 09:53:22 +000036#define ICH9_REG_HSFS 0x04 /* 16 Bits Hardware Sequencing Flash Status */
37#define HSFS_FDONE_OFF 0 /* 0: Flash Cycle Done */
38#define HSFS_FDONE (0x1 << HSFS_FDONE_OFF)
39#define HSFS_FCERR_OFF 1 /* 1: Flash Cycle Error */
40#define HSFS_FCERR (0x1 << HSFS_FCERR_OFF)
41#define HSFS_AEL_OFF 2 /* 2: Access Error Log */
42#define HSFS_AEL (0x1 << HSFS_AEL_OFF)
43#define HSFS_BERASE_OFF 3 /* 3-4: Block/Sector Erase Size */
44#define HSFS_BERASE (0x3 << HSFS_BERASE_OFF)
45#define HSFS_SCIP_OFF 5 /* 5: SPI Cycle In Progress */
46#define HSFS_SCIP (0x1 << HSFS_SCIP_OFF)
47 /* 6-12: reserved */
48#define HSFS_FDOPSS_OFF 13 /* 13: Flash Descriptor Override Pin-Strap Status */
49#define HSFS_FDOPSS (0x1 << HSFS_FDOPSS_OFF)
50#define HSFS_FDV_OFF 14 /* 14: Flash Descriptor Valid */
51#define HSFS_FDV (0x1 << HSFS_FDV_OFF)
52#define HSFS_FLOCKDN_OFF 15 /* 15: Flash Configuration Lock-Down */
53#define HSFS_FLOCKDN (0x1 << HSFS_FLOCKDN_OFF)
54
55#define ICH9_REG_HSFC 0x06 /* 16 Bits Hardware Sequencing Flash Control */
56#define HSFC_FGO_OFF 0 /* 0: Flash Cycle Go */
57#define HSFC_FGO (0x1 << HSFC_FGO_OFF)
58#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
59#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
60 /* 3-7: reserved */
61#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
62#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
63 /* 14: reserved */
64#define HSFC_SME_OFF 15 /* 15: SPI SMI# Enable */
65#define HSFC_SME (0x1 << HSFC_SME_OFF)
66
Stefan Taunerc0aaf952011-05-19 02:58:17 +000067#define ICH9_REG_FADDR 0x08 /* 32 Bits */
68#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
Stefan Reinauera9424d52008-06-27 16:28:34 +000069
Stefan Tauner29c80832011-06-12 08:14:10 +000070#define ICH9_REG_FRAP 0x50 /* 32 Bytes Flash Region Access Permissions */
71#define ICH9_REG_FREG0 0x54 /* 32 Bytes Flash Region 0 */
72
73#define ICH9_REG_PR0 0x74 /* 32 Bytes Protected Range 0 */
Stefan Taunerbf69aaa2011-09-17 21:21:48 +000074#define PR_WP_OFF 31 /* 31: write protection enable */
75#define PR_RP_OFF 15 /* 15: read protection enable */
Stefan Tauner29c80832011-06-12 08:14:10 +000076
Stefan Taunerc0aaf952011-05-19 02:58:17 +000077#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Stefan Tauner0c1ec452011-06-11 09:53:09 +000078#define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */
79#define SSFS_SCIP (0x1 << SSFS_SCIP_OFF)
80#define SSFS_FDONE_OFF 2 /* Cycle Done Status */
81#define SSFS_FDONE (0x1 << SSFS_FDONE_OFF)
82#define SSFS_FCERR_OFF 3 /* Flash Cycle Error */
83#define SSFS_FCERR (0x1 << SSFS_FCERR_OFF)
84#define SSFS_AEL_OFF 4 /* Access Error Log */
85#define SSFS_AEL (0x1 << SSFS_AEL_OFF)
Stefan Taunerc0aaf952011-05-19 02:58:17 +000086/* The following bits are reserved in SSFS: 1,5-7. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +000087#define SSFS_RESERVED_MASK 0x000000e2
Stefan Reinauera9424d52008-06-27 16:28:34 +000088
Stefan Taunerc0aaf952011-05-19 02:58:17 +000089#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Stefan Taunerc0aaf952011-05-19 02:58:17 +000090/* We combine SSFS and SSFC to one 32-bit word,
Stefan Tauner0c1ec452011-06-11 09:53:09 +000091 * therefore SSFC bits are off by 8. */
92 /* 0: reserved */
93#define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */
94#define SSFC_SCGO (0x1 << SSFC_SCGO_OFF)
95#define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */
96#define SSFC_ACS (0x1 << SSFC_ACS_OFF)
97#define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */
98#define SSFC_SPOP (0x1 << SSFC_SPOP_OFF)
99#define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */
100#define SSFC_COP (0x7 << SSFC_COP_OFF)
101 /* 7: reserved */
102#define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */
103#define SSFC_DBC (0x3f << SSFC_DBC_OFF)
104#define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */
105#define SSFC_DS (0x1 << SSFC_DS_OFF)
106#define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */
107#define SSFC_SME (0x1 << SSFC_SME_OFF)
108#define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */
109#define SSFC_SCF (0x7 << SSFC_SCF_OFF)
110#define SSFC_SCF_20MHZ 0x00000000
111#define SSFC_SCF_33MHZ 0x01000000
112 /* 19-23: reserved */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000113#define SSFC_RESERVED_MASK 0xf8008100
Stefan Reinauera9424d52008-06-27 16:28:34 +0000114
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000115#define ICH9_REG_PREOP 0x94 /* 16 Bits */
116#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
117#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000118
Stefan Tauner29c80832011-06-12 08:14:10 +0000119#define ICH9_REG_BBAR 0xA0 /* 32 Bits BIOS Base Address Configuration */
120#define BBAR_MASK 0x00ffff00 /* 8-23: Bottom of System Flash */
121
Stefan Tauner1e146392011-09-15 23:52:55 +0000122#define ICH8_REG_VSCC 0xC1 /* 32 Bits Vendor Specific Component Capabilities */
123#define ICH9_REG_LVSCC 0xC4 /* 32 Bits Host Lower Vendor Specific Component Capabilities */
124#define ICH9_REG_UVSCC 0xC8 /* 32 Bits Host Upper Vendor Specific Component Capabilities */
125/* The individual fields of the VSCC registers are defined in the file
126 * ich_descriptors.h. The reason is that the same layout is also used in the
127 * flash descriptor to define the properties of the different flash chips
128 * supported. The BIOS (or the ME?) is responsible to populate the ICH registers
129 * with the information from the descriptor on startup depending on the actual
130 * chip(s) detected. */
131
Stefan Taunerbd649e42011-07-01 00:39:16 +0000132#define ICH9_REG_FPB 0xD0 /* 32 Bits Flash Partition Boundary */
133#define FPB_FPBA_OFF 0 /* 0-12: Block/Sector Erase Size */
134#define FPB_FPBA (0x1FFF << FPB_FPBA_OFF)
135
Dominik Geyerb46acba2008-05-16 12:55:55 +0000136// ICH9R SPI commands
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000137#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
138#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
139#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
140#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
Dominik Geyerb46acba2008-05-16 12:55:55 +0000141
Stefan Reinauera9424d52008-06-27 16:28:34 +0000142// ICH7 registers
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000143#define ICH7_REG_SPIS 0x00 /* 16 Bits */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000144#define SPIS_SCIP 0x0001
145#define SPIS_GRANT 0x0002
146#define SPIS_CDS 0x0004
147#define SPIS_FCERR 0x0008
148#define SPIS_RESERVED_MASK 0x7ff0
Stefan Reinauera9424d52008-06-27 16:28:34 +0000149
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000150/* VIA SPI is compatible with ICH7, but maxdata
151 to transfer is 16 bytes.
152
153 DATA byte count on ICH7 is 8:13, on VIA 8:11
154
155 bit 12 is port select CS0 CS1
156 bit 13 is FAST READ enable
157 bit 7 is used with fast read and one shot controls CS de-assert?
158*/
159
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000160#define ICH7_REG_SPIC 0x02 /* 16 Bits */
161#define SPIC_SCGO 0x0002
162#define SPIC_ACS 0x0004
163#define SPIC_SPOP 0x0008
164#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000165
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000166#define ICH7_REG_SPIA 0x04 /* 32 Bits */
167#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
168#define ICH7_REG_PREOP 0x54 /* 16 Bits */
169#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
170#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000171
FENG yu ningc05a2952008-12-08 18:16:58 +0000172/* ICH SPI configuration lock-down. May be set during chipset enabling. */
Michael Karchera4448d92010-07-22 18:04:15 +0000173static int ichspi_lock = 0;
FENG yu ningc05a2952008-12-08 18:16:58 +0000174
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000175uint32_t ichspi_bbar = 0;
176
Michael Karchera4448d92010-07-22 18:04:15 +0000177static void *ich_spibar = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000178
Dominik Geyerb46acba2008-05-16 12:55:55 +0000179typedef struct _OPCODE {
180 uint8_t opcode; //This commands spi opcode
181 uint8_t spi_type; //This commands spi type
182 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
183} OPCODE;
184
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000185/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000186 * Preop 1: Write Enable
187 * Preop 2: Write Status register enable
188 *
189 * OP 0: Write address
190 * OP 1: Read Address
191 * OP 2: ERASE block
192 * OP 3: Read Status register
193 * OP 4: Read ID
194 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000195 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000196 * OP 7: Chip erase
197 */
198typedef struct _OPCODES {
199 uint8_t preop[2];
200 OPCODE opcode[8];
201} OPCODES;
202
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000203static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000204
205/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000206static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000207{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000208 return mmio_readl(ich_spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000209}
210
Uwe Hermann09e04f72009-05-16 22:36:00 +0000211static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000212{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000213 return mmio_readw(ich_spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000214}
215
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000216static uint16_t REGREAD8(int X)
217{
218 return mmio_readb(ich_spibar + X);
219}
220
Stefan Taunerccd92a12011-07-01 00:39:01 +0000221#define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
222#define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
223#define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off))
Dominik Geyerb46acba2008-05-16 12:55:55 +0000224
Dominik Geyerb46acba2008-05-16 12:55:55 +0000225/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000226static int find_opcode(OPCODES *op, uint8_t opcode);
227static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000228static int generate_opcodes(OPCODES * op);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000229static int program_opcodes(OPCODES *op, int enable_undo);
Stefan Reinauer43119562008-11-02 19:51:50 +0000230static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000231 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000232
FENG yu ningf041e9b2008-12-15 02:32:11 +0000233/* for pairing opcodes with their required preop */
234struct preop_opcode_pair {
235 uint8_t preop;
236 uint8_t opcode;
237};
238
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000239/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000240const struct preop_opcode_pair pops[] = {
FENG yu ningf041e9b2008-12-15 02:32:11 +0000241 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
242 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
243 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
244 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
245 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
246 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000247 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
248 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000249 {JEDEC_EWSR, JEDEC_WRSR},
250 {0,}
251};
252
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000253/* Reasonable default configuration. Needs ad-hoc modifications if we
254 * encounter unlisted opcodes. Fun.
255 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000256static OPCODES O_ST_M25P = {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000257 {
258 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000259 JEDEC_EWSR,
260 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000261 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000262 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000263 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000264 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000265 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000266 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000267 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000268 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000269 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
270 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000271};
272
Helge Wagner738e2522010-10-05 22:06:05 +0000273/* List of opcodes with their corresponding spi_type
274 * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
275 * is needed which is currently not in the chipset OPCODE table
276 */
277static OPCODE POSSIBLE_OPCODES[] = {
278 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
279 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
280 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
281 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
282 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
283 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
284 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
285 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
286 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
287 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
288 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
289};
290
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000291static OPCODES O_EXISTING = {};
FENG yu ningc05a2952008-12-08 18:16:58 +0000292
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000293/* pretty printing functions */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000294static void prettyprint_opcodes(OPCODES *ops)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000295{
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000296 OPCODE oc;
297 const char *t;
298 const char *a;
299 uint8_t i;
300 static const char *const spi_type[4] = {
301 "read w/o addr",
302 "write w/o addr",
303 "read w/ addr",
304 "write w/ addr"
305 };
306 static const char *const atomic_type[3] = {
307 "none",
308 " 0 ",
309 " 1 "
310 };
311
312 if (ops == NULL)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000313 return;
314
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000315 msg_pdbg2(" OP Type Pre-OP\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000316 for (i = 0; i < 8; i++) {
317 oc = ops->opcode[i];
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000318 t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type];
319 a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic];
320 msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000321 }
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000322 msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0],
323 ops->preop[1]);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000324}
325
326#define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF)
327
Stefan Tauner55206942011-06-11 09:53:22 +0000328static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
329{
330 msg_pdbg("HSFS: ");
331 pprint_reg(HSFS, FDONE, reg_val, ", ");
332 pprint_reg(HSFS, FCERR, reg_val, ", ");
333 pprint_reg(HSFS, AEL, reg_val, ", ");
334 pprint_reg(HSFS, BERASE, reg_val, ", ");
335 pprint_reg(HSFS, SCIP, reg_val, ", ");
336 pprint_reg(HSFS, FDOPSS, reg_val, ", ");
337 pprint_reg(HSFS, FDV, reg_val, ", ");
338 pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
339}
340
341static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
342{
343 msg_pdbg("HSFC: ");
344 pprint_reg(HSFC, FGO, reg_val, ", ");
345 pprint_reg(HSFC, FCYCLE, reg_val, ", ");
346 pprint_reg(HSFC, FDBC, reg_val, ", ");
347 pprint_reg(HSFC, SME, reg_val, "\n");
348}
349
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000350static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
351{
352 msg_pdbg("SSFS: ");
353 pprint_reg(SSFS, SCIP, reg_val, ", ");
354 pprint_reg(SSFS, FDONE, reg_val, ", ");
355 pprint_reg(SSFS, FCERR, reg_val, ", ");
356 pprint_reg(SSFS, AEL, reg_val, "\n");
357}
358
359static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
360{
361 msg_pdbg("SSFC: ");
362 pprint_reg(SSFC, SCGO, reg_val, ", ");
363 pprint_reg(SSFC, ACS, reg_val, ", ");
364 pprint_reg(SSFC, SPOP, reg_val, ", ");
365 pprint_reg(SSFC, COP, reg_val, ", ");
366 pprint_reg(SSFC, DBC, reg_val, ", ");
367 pprint_reg(SSFC, SME, reg_val, ", ");
368 pprint_reg(SSFC, SCF, reg_val, "\n");
369}
370
Helge Wagner738e2522010-10-05 22:06:05 +0000371static uint8_t lookup_spi_type(uint8_t opcode)
372{
373 int a;
374
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000375 for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
Helge Wagner738e2522010-10-05 22:06:05 +0000376 if (POSSIBLE_OPCODES[a].opcode == opcode)
377 return POSSIBLE_OPCODES[a].spi_type;
378 }
379
380 return 0xFF;
381}
382
383static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
384{
385 uint8_t spi_type;
386
387 spi_type = lookup_spi_type(opcode);
388 if (spi_type > 3) {
389 /* Try to guess spi type from read/write sizes.
390 * The following valid writecnt/readcnt combinations exist:
391 * writecnt = 4, readcnt >= 0
392 * writecnt = 1, readcnt >= 0
393 * writecnt >= 4, readcnt = 0
394 * writecnt >= 1, readcnt = 0
395 * writecnt >= 1 is guaranteed for all commands.
396 */
397 if (readcnt == 0)
398 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
399 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
400 * bytes are actual the address, they go to the bus anyhow
401 */
402 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
403 else if (writecnt == 1) // and readcnt is > 0
404 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
405 else if (writecnt == 4) // and readcnt is > 0
406 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
407 // else we have an invalid case, will be handled below
408 }
409 if (spi_type <= 3) {
410 int oppos=2; // use original JEDEC_BE_D8 offset
411 curopcodes->opcode[oppos].opcode = opcode;
412 curopcodes->opcode[oppos].spi_type = spi_type;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000413 program_opcodes(curopcodes, 0);
Helge Wagner738e2522010-10-05 22:06:05 +0000414 oppos = find_opcode(curopcodes, opcode);
415 msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
416 return oppos;
417 }
418 return -1;
419}
420
Uwe Hermann09e04f72009-05-16 22:36:00 +0000421static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000422{
423 int a;
424
425 for (a = 0; a < 8; a++) {
426 if (op->opcode[a].opcode == opcode)
427 return a;
428 }
429
430 return -1;
431}
432
Uwe Hermann09e04f72009-05-16 22:36:00 +0000433static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000434{
435 int a;
436
437 for (a = 0; a < 2; a++) {
438 if (op->preop[a] == preop)
439 return a;
440 }
441
442 return -1;
443}
444
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000445/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000446static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000447{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000448 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000449 uint16_t preop, optype;
450 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000451
452 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000453 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000454 return -1;
455 }
456
Michael Karcherb9dbe482011-05-11 17:07:07 +0000457 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000458 case SPI_CONTROLLER_ICH7:
459 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000460 preop = REGREAD16(ICH7_REG_PREOP);
461 optype = REGREAD16(ICH7_REG_OPTYPE);
462 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
463 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
464 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000465 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000466 preop = REGREAD16(ICH9_REG_PREOP);
467 optype = REGREAD16(ICH9_REG_OPTYPE);
468 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
469 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
470 break;
471 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000472 msg_perr("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000473 return -1;
474 }
475
476 op->preop[0] = (uint8_t) preop;
477 op->preop[1] = (uint8_t) (preop >> 8);
478
479 for (a = 0; a < 8; a++) {
480 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
481 optype >>= 2;
482 }
483
484 for (a = 0; a < 4; a++) {
485 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
486 opmenu[0] >>= 8;
487 }
488
489 for (a = 4; a < 8; a++) {
490 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
491 opmenu[1] >>= 8;
492 }
493
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000494 /* No preopcodes used by default. */
495 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000496 op->opcode[a].atomic = 0;
497
FENG yu ningc05a2952008-12-08 18:16:58 +0000498 return 0;
499}
500
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000501static int program_opcodes(OPCODES *op, int enable_undo)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000502{
503 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000504 uint16_t preop, optype;
505 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000506
507 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000508 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000509 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000510 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000511 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000512
Stefan Reinauera9424d52008-06-27 16:28:34 +0000513 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000514 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000515 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000516 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000517 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000518
Stefan Reinauera9424d52008-06-27 16:28:34 +0000519 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000520 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000521 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000522 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000523 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000524
Dominik Geyerb46acba2008-05-16 12:55:55 +0000525 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000526 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000527 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000528 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000529 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000530
Sean Nelson316a29f2010-05-07 20:09:04 +0000531 msg_pdbg("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000532 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000533 case SPI_CONTROLLER_ICH7:
534 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000535 /* Register undo only for enable_undo=1, i.e. first call. */
536 if (enable_undo) {
537 rmmio_valw(ich_spibar + ICH7_REG_PREOP);
538 rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
539 rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
540 rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
541 }
542 mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
543 mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
544 mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
545 mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000546 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000547 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000548 /* Register undo only for enable_undo=1, i.e. first call. */
549 if (enable_undo) {
550 rmmio_valw(ich_spibar + ICH9_REG_PREOP);
551 rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
552 rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
553 rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
554 }
555 mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
556 mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
557 mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
558 mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000559 break;
560 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000561 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000562 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000563 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000564
565 return 0;
566}
567
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000568/*
569 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
570 * it didn't stick.
571 */
Stefan Tauner7783f312011-09-17 21:21:42 +0000572static void ich_set_bbar(int ich_generation, uint32_t min_addr)
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000573{
Stefan Taunere27b2d42011-07-01 00:39:09 +0000574 int bbar_off;
Stefan Tauner7783f312011-09-17 21:21:42 +0000575 switch (ich_generation) {
576 case 7:
Stefan Taunere27b2d42011-07-01 00:39:09 +0000577 bbar_off = 0x50;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000578 break;
Stefan Tauner7783f312011-09-17 21:21:42 +0000579 case 8:
580 msg_perr("BBAR offset is unknown on ICH8!\n");
581 return;
582 default: /* Future version might behave the same */
Stefan Taunere27b2d42011-07-01 00:39:09 +0000583 bbar_off = ICH9_REG_BBAR;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000584 break;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000585 }
Stefan Taunere27b2d42011-07-01 00:39:09 +0000586
587 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
588 if (ichspi_bbar) {
589 msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
590 ichspi_bbar);
591 }
592 min_addr &= BBAR_MASK;
593 ichspi_bbar |= min_addr;
594 rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
595 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
596
597 /* We don't have any option except complaining. And if the write
598 * failed, the restore will fail as well, so no problem there.
599 */
600 if (ichspi_bbar != min_addr)
Stefan Tauner7783f312011-09-17 21:21:42 +0000601 msg_perr("Setting BBAR to 0x%08x failed! New value: 0x%08x.\n",
602 min_addr, ichspi_bbar);
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000603}
604
Stefan Tauner8b391b82011-08-09 01:49:34 +0000605/* Read len bytes from the fdata/spid register into the data array.
606 *
607 * Note that using len > spi_programmer->max_data_read will return garbage or
608 * may even crash.
609 */
610 static void ich_read_data(uint8_t *data, int len, int reg0_off)
611 {
612 int i;
613 uint32_t temp32 = 0;
614
615 for (i = 0; i < len; i++) {
616 if ((i % 4) == 0)
617 temp32 = REGREAD32(reg0_off + i);
618
619 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
620 }
621}
622
623/* Fill len bytes from the data array into the fdata/spid registers.
624 *
625 * Note that using len > spi_programmer->max_data_write will trash the registers
626 * following the data registers.
627 */
628static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
629{
630 uint32_t temp32 = 0;
631 int i;
632
633 if (len <= 0)
634 return;
635
636 for (i = 0; i < len; i++) {
637 if ((i % 4) == 0)
638 temp32 = 0;
639
640 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
641
642 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
643 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
644 }
645 i--;
646 if ((i % 4) != 3) /* Write remaining data to regs. */
647 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
648}
649
FENG yu ningf041e9b2008-12-15 02:32:11 +0000650/* This function generates OPCODES from or programs OPCODES to ICH according to
651 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000652 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000653 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000654 */
Michael Karchera4448d92010-07-22 18:04:15 +0000655static int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000656{
657 int rc = 0;
658 OPCODES *curopcodes_done;
659
660 if (curopcodes)
661 return 0;
662
663 if (ichspi_lock) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000664 msg_pdbg("Reading OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000665 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000666 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000667 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000668 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000669 curopcodes_done = &O_ST_M25P;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000670 rc = program_opcodes(curopcodes_done, 1);
FENG yu ningc05a2952008-12-08 18:16:58 +0000671 }
672
673 if (rc) {
674 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000675 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000676 return 1;
677 } else {
678 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000679 msg_pdbg("done\n");
Stefan Tauner8b391b82011-08-09 01:49:34 +0000680 prettyprint_opcodes(curopcodes);
FENG yu ningc05a2952008-12-08 18:16:58 +0000681 return 0;
682 }
683}
684
Stefan Reinauer43119562008-11-02 19:51:50 +0000685static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000686 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000687{
688 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000689 int timeout;
Stefan Tauner8b391b82011-08-09 01:49:34 +0000690 uint32_t temp32;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000691 uint16_t temp16;
Stefan Reinauer43119562008-11-02 19:51:50 +0000692 uint64_t opmenu;
693 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000694
695 /* Is it a write command? */
696 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
697 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
698 write_cmd = 1;
699 }
700
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000701 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
702 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
703 programmer_delay(10);
704 }
705 if (!timeout) {
706 msg_perr("Error: SCIP never cleared!\n");
707 return 1;
708 }
709
Stefan Tauner10b3e222011-07-01 00:39:23 +0000710 /* Program offset in flash into SPIA while preserving reserved bits. */
711 temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
712 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000713
Stefan Tauner10b3e222011-07-01 00:39:23 +0000714 /* Program data into SPID0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000715 if (write_cmd && (datalength != 0))
716 ich_fill_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000717
718 /* Assemble SPIS */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000719 temp16 = REGREAD16(ICH7_REG_SPIS);
720 /* keep reserved bits */
721 temp16 &= SPIS_RESERVED_MASK;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000722 /* clear error status registers */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000723 temp16 |= (SPIS_CDS | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000724 REGWRITE16(ICH7_REG_SPIS, temp16);
725
726 /* Assemble SPIC */
727 temp16 = 0;
728
729 if (datalength != 0) {
730 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000731 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000732 }
733
734 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000735 opmenu = REGREAD32(ICH7_REG_OPMENU);
736 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
737
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000738 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
739 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000740 break;
741 }
742 opmenu >>= 8;
743 }
744 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000745 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000746 return 1;
747 }
748 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000749
Michael Karcher136125a2011-04-29 22:11:36 +0000750 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
751 /* Handle Atomic. Atomic commands include three steps:
752 - sending the preop (mainly EWSR or WREN)
753 - sending the main command
754 - waiting for the busy bit (WIP) to be cleared
755 This means the timeout must be sufficient for chip erase
756 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000757 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000758 switch (op.atomic) {
759 case 2:
760 /* Select second preop. */
761 temp16 |= SPIC_SPOP;
762 /* And fall through. */
763 case 1:
764 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000765 temp16 |= SPIC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000766 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000767 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000768 }
769
770 /* Start */
771 temp16 |= SPIC_SCGO;
772
773 /* write it */
774 REGWRITE16(ICH7_REG_SPIC, temp16);
775
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000776 /* Wait for Cycle Done Status or Flash Cycle Error. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000777 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
778 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000779 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000780 }
781 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000782 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
783 REGREAD16(ICH7_REG_SPIS));
784 return 1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000785 }
786
Sean Nelson316a29f2010-05-07 20:09:04 +0000787 /* FIXME: make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000788 temp16 = REGREAD16(ICH7_REG_SPIS);
789 if (temp16 & SPIS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000790 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000791 /* keep reserved bits */
792 temp16 &= SPIS_RESERVED_MASK;
793 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000794 return 1;
795 }
796
Stefan Tauner8b391b82011-08-09 01:49:34 +0000797 if ((!write_cmd) && (datalength != 0))
798 ich_read_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000799
800 return 0;
801}
802
Stefan Reinauer43119562008-11-02 19:51:50 +0000803static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000804 uint8_t datalength, uint8_t * data)
805{
806 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000807 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000808 uint32_t temp32;
Stefan Reinauer43119562008-11-02 19:51:50 +0000809 uint64_t opmenu;
810 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000811
812 /* Is it a write command? */
813 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
814 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
815 write_cmd = 1;
816 }
817
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000818 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
819 while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
820 programmer_delay(10);
821 }
822 if (!timeout) {
823 msg_perr("Error: SCIP never cleared!\n");
824 return 1;
825 }
826
Stefan Tauner10b3e222011-07-01 00:39:23 +0000827 /* Program offset in flash into FADDR while preserve the reserved bits
828 * and clearing the 25. address bit which is only useable in hwseq. */
829 temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
830 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000831
832 /* Program data into FDATA0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000833 if (write_cmd && (datalength != 0))
834 ich_fill_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000835
836 /* Assemble SSFS + SSFC */
Helge Wagnera319be12010-08-11 21:06:10 +0000837 temp32 = REGREAD32(ICH9_REG_SSFS);
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000838 /* Keep reserved bits only */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000839 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000840 /* Clear cycle done and cycle error status registers */
841 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000842 REGWRITE32(ICH9_REG_SSFS, temp32);
843
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000844 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000845 temp32 |= SSFC_SCF_20MHZ;
846
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000847 /* Set data byte count (DBC) and data cycle bit (DS) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000848 if (datalength != 0) {
849 uint32_t datatemp;
850 temp32 |= SSFC_DS;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000851 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
852 SSFC_DBC);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000853 temp32 |= datatemp;
854 }
855
856 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000857 opmenu = REGREAD32(ICH9_REG_OPMENU);
858 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
859
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000860 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
861 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000862 break;
863 }
864 opmenu >>= 8;
865 }
866 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000867 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000868 return 1;
869 }
870 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000871
Michael Karcher136125a2011-04-29 22:11:36 +0000872 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
873 /* Handle Atomic. Atomic commands include three steps:
874 - sending the preop (mainly EWSR or WREN)
875 - sending the main command
876 - waiting for the busy bit (WIP) to be cleared
877 This means the timeout must be sufficient for chip erase
878 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000879 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000880 switch (op.atomic) {
881 case 2:
882 /* Select second preop. */
883 temp32 |= SSFC_SPOP;
884 /* And fall through. */
885 case 1:
886 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000887 temp32 |= SSFC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000888 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000889 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000890 }
891
892 /* Start */
893 temp32 |= SSFC_SCGO;
894
895 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000896 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000897
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000898 /* Wait for Cycle Done Status or Flash Cycle Error. */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000899 while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000900 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000901 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000902 }
903 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000904 msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
905 REGREAD32(ICH9_REG_SSFS));
906 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000907 }
908
Sean Nelson316a29f2010-05-07 20:09:04 +0000909 /* FIXME make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000910 temp32 = REGREAD32(ICH9_REG_SSFS);
911 if (temp32 & SSFS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000912 msg_perr("Transaction error!\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000913 prettyprint_ich9_reg_ssfs(temp32);
914 prettyprint_ich9_reg_ssfc(temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000915 /* keep reserved bits */
916 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
917 /* Clear the transaction error. */
918 REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000919 return 1;
920 }
921
Stefan Tauner8b391b82011-08-09 01:49:34 +0000922 if ((!write_cmd) && (datalength != 0))
923 ich_read_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000924
925 return 0;
926}
927
Stefan Reinauer43119562008-11-02 19:51:50 +0000928static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000929 uint8_t datalength, uint8_t * data)
930{
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000931 /* max_data_read == max_data_write for all Intel/VIA SPI masters */
932 uint8_t maxlength = spi_programmer->max_data_read;
933
934 if (spi_programmer->type == SPI_CONTROLLER_NONE) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000935 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000936 return -1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000937 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000938
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000939 if (datalength > maxlength) {
940 msg_perr("%s: Internal command size error for "
941 "opcode 0x%02x, got datalength=%i, want <=%i\n",
942 __func__, op.opcode, datalength, maxlength);
943 return SPI_INVALID_LENGTH;
944 }
945
946 switch (spi_programmer->type) {
947 case SPI_CONTROLLER_VIA:
948 case SPI_CONTROLLER_ICH7:
949 return ich7_run_opcode(op, offset, datalength, data, maxlength);
950 case SPI_CONTROLLER_ICH9:
951 return ich9_run_opcode(op, offset, datalength, data);
952 default:
953 /* If we ever get here, something really weird happened */
954 return -1;
955 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000956}
957
Michael Karcherb9dbe482011-05-11 17:07:07 +0000958static int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000959 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000960{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000961 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000962 int opcode_index = -1;
963 const unsigned char cmd = *writearr;
964 OPCODE *opcode;
965 uint32_t addr = 0;
966 uint8_t *data;
967 int count;
968
Dominik Geyerb46acba2008-05-16 12:55:55 +0000969 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000970 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000971 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +0000972 if (!ichspi_lock)
973 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
974 if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000975 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
976 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +0000977 return SPI_INVALID_OPCODE;
978 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000979 }
980
981 opcode = &(curopcodes->opcode[opcode_index]);
982
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000983 /* The following valid writecnt/readcnt combinations exist:
984 * writecnt = 4, readcnt >= 0
985 * writecnt = 1, readcnt >= 0
986 * writecnt >= 4, readcnt = 0
987 * writecnt >= 1, readcnt = 0
988 * writecnt >= 1 is guaranteed for all commands.
989 */
990 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
991 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000992 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000993 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
994 writecnt);
995 return SPI_INVALID_LENGTH;
996 }
997 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
998 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000999 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001000 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
1001 writecnt);
1002 return SPI_INVALID_LENGTH;
1003 }
1004 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1005 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001006 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001007 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
1008 writecnt);
1009 return SPI_INVALID_LENGTH;
1010 }
1011 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1012 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1013 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001014 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001015 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1016 readcnt);
1017 return SPI_INVALID_LENGTH;
1018 }
1019
Dominik Geyerb46acba2008-05-16 12:55:55 +00001020 /* if opcode-type requires an address */
1021 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1022 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001023 addr = (writearr[1] << 16) |
1024 (writearr[2] << 8) | (writearr[3] << 0);
Michael Karcherb9dbe482011-05-11 17:07:07 +00001025 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001026 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001027 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001028 case SPI_CONTROLLER_ICH9:
1029 if (addr < ichspi_bbar) {
1030 msg_perr("%s: Address 0x%06x below allowed "
1031 "range 0x%06x-0xffffff\n", __func__,
1032 addr, ichspi_bbar);
1033 return SPI_INVALID_ADDRESS;
1034 }
1035 break;
1036 default:
1037 break;
1038 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001039 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001040
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001041 /* Translate read/write array/count.
1042 * The maximum data length is identical for the maximum read length and
1043 * for the maximum write length excluding opcode and address. Opcode and
1044 * address are stored in separate registers, not in the data registers
1045 * and are thus not counted towards data length. The only exception
1046 * applies if the opcode definition (un)intentionally classifies said
1047 * opcode incorrectly as non-address opcode or vice versa. */
Dominik Geyerb46acba2008-05-16 12:55:55 +00001048 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001049 data = (uint8_t *) (writearr + 1);
1050 count = writecnt - 1;
1051 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1052 data = (uint8_t *) (writearr + 4);
1053 count = writecnt - 4;
1054 } else {
1055 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001056 count = readcnt;
1057 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001058
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001059 result = run_opcode(*opcode, addr, count, data);
1060 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001061 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1062 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1063 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1064 msg_pdbg("at address 0x%06x ", addr);
1065 }
1066 msg_pdbg("(payload length was %d).\n", count);
1067
1068 /* Print out the data array if it contains data to write.
1069 * Errors are detected before the received data is read back into
1070 * the array so it won't make sense to print it then. */
1071 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1072 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1073 int i;
1074 msg_pspew("The data was:\n");
1075 for(i=0; i<count; i++){
1076 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1077 }
1078 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001079 }
1080
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001081 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001082}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001083
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001084#if 0
1085/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
1086static void ich_hwseq_set_addr(uint32_t addr)
1087{
1088 uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
1089 REGWRITE32(ICH9_REG_FADDR, (addr & 0x01FFFFFF) | addr_old);
1090}
1091
1092/* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
1093 * of the block containing this address. May return nonsense if the address is
1094 * not valid. The erase block size for a specific address depends on the flash
1095 * partition layout as specified by FPB and the partition properties as defined
1096 * by UVSCC and LVSCC respectively. An alternative to implement this method
1097 * would be by querying FPB and the respective VSCC register directly.
1098 */
1099static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr)
1100{
1101 uint8_t enc_berase;
1102 static const uint32_t const dec_berase[4] = {
1103 256,
1104 4 * 1024,
1105 8 * 1024,
1106 64 * 1024
1107 };
1108
1109 ich_hwseq_set_addr(addr);
1110 enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >>
1111 HSFS_BERASE_OFF;
1112 return dec_berase[enc_berase];
1113}
1114
1115/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
1116 Resets all error flags in HSFS.
1117 Returns 0 if the cycle completes successfully without errors within
1118 timeout us, 1 on errors. */
1119static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
1120 unsigned int len)
1121{
1122 uint16_t hsfs;
1123 uint32_t addr;
1124
1125 timeout /= 8; /* scale timeout duration to counter */
1126 while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) &
1127 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
1128 --timeout) {
1129 programmer_delay(8);
1130 }
1131 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1132 if (!timeout) {
1133 addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
1134 msg_perr("Timeout error between offset 0x%08x and "
1135 "0x%08x + %d (=0x%08x)!\n",
1136 addr, addr, len - 1, addr + len - 1);
1137 prettyprint_ich9_reg_hsfs(hsfs);
1138 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1139 return 1;
1140 }
1141
1142 if (hsfs & HSFS_FCERR) {
1143 addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
1144 msg_perr("Transaction error between offset 0x%08x and "
1145 "0x%08x (=0x%08x + %d)!\n",
1146 addr, addr + len - 1, addr, len - 1);
1147 prettyprint_ich9_reg_hsfs(hsfs);
1148 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1149 return 1;
1150 }
1151 return 0;
1152}
1153#endif
1154
Michael Karcherb9dbe482011-05-11 17:07:07 +00001155static int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001156{
1157 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001158 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001159 int oppos, preoppos;
1160 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001161 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001162 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001163 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1164 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001165 if ((oppos == -1) && (preoppos != -1)) {
1166 /* Current command is listed as preopcode in
1167 * ICH struct OPCODES, but next command is not
1168 * listed as opcode in that struct.
1169 * Check for command sanity, then
1170 * try to reprogram the ICH opcode list.
1171 */
1172 if (find_preop(curopcodes,
1173 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001174 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001175 "preopcodes 0x%02x and 0x%02x, "
1176 "ignoring the first.\n",
1177 __func__, cmds->writearr[0],
1178 (cmds + 1)->writearr[0]);
1179 continue;
1180 }
1181 /* If the chipset is locked down, we'll fail
1182 * during execution of the next command anyway.
1183 * No need to bother with fixups.
1184 */
1185 if (!ichspi_lock) {
Helge Wagner738e2522010-10-05 22:06:05 +00001186 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1187 if (oppos == -1)
1188 continue;
1189 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001190 continue;
1191 }
1192 }
1193 if ((oppos != -1) && (preoppos != -1)) {
1194 /* Current command is listed as preopcode in
1195 * ICH struct OPCODES and next command is listed
1196 * as opcode in that struct. Match them up.
1197 */
1198 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001199 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001200 }
1201 /* If none of the above if-statements about oppos or
1202 * preoppos matched, this is a normal opcode.
1203 */
1204 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001205 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
1206 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001207 /* Reset the type of all opcodes to non-atomic. */
1208 for (i = 0; i < 8; i++)
1209 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001210 }
1211 return ret;
1212}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001213
Michael Karchera4448d92010-07-22 18:04:15 +00001214#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1215#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1216#define ICH_BRWA(x) ((x >> 8) & 0xff)
1217#define ICH_BRRA(x) ((x >> 0) & 0xff)
1218
Michael Karchera4448d92010-07-22 18:04:15 +00001219static void do_ich9_spi_frap(uint32_t frap, int i)
1220{
Mathias Krausea60faab2011-01-17 07:50:42 +00001221 static const char *const access_names[4] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001222 "locked", "read-only", "write-only", "read-write"
1223 };
Mathias Krausea60faab2011-01-17 07:50:42 +00001224 static const char *const region_names[5] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001225 "Flash Descriptor", "BIOS", "Management Engine",
1226 "Gigabit Ethernet", "Platform Data"
1227 };
1228 uint32_t base, limit;
1229 int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1230 (((ICH_BRRA(frap) >> i) & 1) << 0);
Stefan Tauner29c80832011-06-12 08:14:10 +00001231 int offset = ICH9_REG_FREG0 + i * 4;
Michael Karchera4448d92010-07-22 18:04:15 +00001232 uint32_t freg = mmio_readl(ich_spibar + offset);
1233
1234 msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n",
1235 offset, freg, i, region_names[i]);
1236
1237 base = ICH_FREG_BASE(freg);
1238 limit = ICH_FREG_LIMIT(freg);
Joshua Roysd172ecd2011-05-26 13:30:51 +00001239 if (base > limit) {
Michael Karchera4448d92010-07-22 18:04:15 +00001240 /* this FREG is disabled */
1241 msg_pdbg("%s region is unused.\n", region_names[i]);
1242 return;
1243 }
1244
Stefan Tauner1e146392011-09-15 23:52:55 +00001245 msg_pdbg("0x%08x-0x%08x is %s\n", base, (limit | 0x0fff),
1246 access_names[rwperms]);
Michael Karchera4448d92010-07-22 18:04:15 +00001247}
1248
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001249 /* In contrast to FRAP and the master section of the descriptor the bits
1250 * in the PR registers have an inverted meaning. The bits in FRAP
1251 * indicate read and write access _grant_. Here they indicate read
1252 * and write _protection_ respectively. If both bits are 0 the address
1253 * bits are ignored.
1254 */
1255#define ICH_PR_PERMS(pr) (((~((pr) >> PR_RP_OFF) & 1) << 0) | \
1256 ((~((pr) >> PR_WP_OFF) & 1) << 1))
1257
1258static void prettyprint_ich9_reg_pr(int i)
1259{
1260 static const char *const access_names[4] = {
1261 "locked", "read-only", "write-only", "read-write"
1262 };
1263 uint8_t off = ICH9_REG_PR0 + (i * 4);
1264 uint32_t pr = mmio_readl(ich_spibar + off);
1265 int rwperms = ICH_PR_PERMS(pr);
1266
1267 msg_pdbg2("0x%02X: 0x%08x (PR%u", off, pr, i);
1268 if (rwperms != 0x3)
1269 msg_pdbg2(")\n0x%08x-0x%08x is %s\n", ICH_FREG_BASE(pr),
1270 ICH_FREG_LIMIT(pr) | 0x0fff, access_names[rwperms]);
1271 else
1272 msg_pdbg2(", unused)\n");
1273}
1274
Stefan Tauner75da80c2011-09-17 22:21:55 +00001275/* Set/Clear the read and write protection enable bits of PR register @i
1276 * according to @read_prot and @write_prot. */
1277static void ich9_set_pr(int i, int read_prot, int write_prot)
1278{
1279 void *addr = ich_spibar + ICH9_REG_PR0 + (i * 4);
1280 uint32_t old = mmio_readl(addr);
1281 uint32_t new;
1282
1283 msg_gspew("PR%u is 0x%08x", i, old);
1284 new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF));
1285 if (read_prot)
1286 new |= (1 << PR_RP_OFF);
1287 if (write_prot)
1288 new |= (1 << PR_WP_OFF);
1289 if (old == new) {
1290 msg_gspew(" already.\n");
1291 return;
1292 }
1293 msg_gspew(", trying to set it to 0x%08x ", new);
1294 rmmio_writel(new, addr);
1295 msg_gspew("resulted in 0x%08x.\n", mmio_readl(addr));
1296}
1297
Michael Karcherb9dbe482011-05-11 17:07:07 +00001298static const struct spi_programmer spi_programmer_ich7 = {
1299 .type = SPI_CONTROLLER_ICH7,
1300 .max_data_read = 64,
1301 .max_data_write = 64,
1302 .command = ich_spi_send_command,
1303 .multicommand = ich_spi_send_multicommand,
1304 .read = default_spi_read,
1305 .write_256 = default_spi_write_256,
1306};
1307
1308static const struct spi_programmer spi_programmer_ich9 = {
1309 .type = SPI_CONTROLLER_ICH9,
1310 .max_data_read = 64,
1311 .max_data_write = 64,
1312 .command = ich_spi_send_command,
1313 .multicommand = ich_spi_send_multicommand,
1314 .read = default_spi_read,
1315 .write_256 = default_spi_write_256,
1316};
1317
Michael Karchera4448d92010-07-22 18:04:15 +00001318int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb,
1319 int ich_generation)
1320{
1321 int i;
1322 uint8_t old, new;
1323 uint16_t spibar_offset, tmp2;
1324 uint32_t tmp;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001325 int desc_valid = 0;
Michael Karchera4448d92010-07-22 18:04:15 +00001326
Michael Karchera4448d92010-07-22 18:04:15 +00001327 switch (ich_generation) {
1328 case 7:
Michael Karchera4448d92010-07-22 18:04:15 +00001329 spibar_offset = 0x3020;
1330 break;
1331 case 8:
Michael Karchera4448d92010-07-22 18:04:15 +00001332 spibar_offset = 0x3020;
1333 break;
1334 case 9:
1335 case 10:
1336 default: /* Future version might behave the same */
Michael Karchera4448d92010-07-22 18:04:15 +00001337 spibar_offset = 0x3800;
1338 break;
1339 }
1340
1341 /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
1342 msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset);
1343
1344 /* Assign Virtual Address */
1345 ich_spibar = rcrb + spibar_offset;
1346
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001347 switch (ich_generation) {
1348 case 7:
Michael Karchera4448d92010-07-22 18:04:15 +00001349 msg_pdbg("0x00: 0x%04x (SPIS)\n",
1350 mmio_readw(ich_spibar + 0));
1351 msg_pdbg("0x02: 0x%04x (SPIC)\n",
1352 mmio_readw(ich_spibar + 2));
1353 msg_pdbg("0x04: 0x%08x (SPIA)\n",
1354 mmio_readl(ich_spibar + 4));
1355 for (i = 0; i < 8; i++) {
1356 int offs;
1357 offs = 8 + (i * 8);
1358 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1359 mmio_readl(ich_spibar + offs), i);
1360 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1361 mmio_readl(ich_spibar + offs + 4), i);
1362 }
1363 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1364 msg_pdbg("0x50: 0x%08x (BBAR)\n",
1365 ichspi_bbar);
1366 msg_pdbg("0x54: 0x%04x (PREOP)\n",
1367 mmio_readw(ich_spibar + 0x54));
1368 msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
1369 mmio_readw(ich_spibar + 0x56));
1370 msg_pdbg("0x58: 0x%08x (OPMENU)\n",
1371 mmio_readl(ich_spibar + 0x58));
1372 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
1373 mmio_readl(ich_spibar + 0x5c));
Stefan Tauner122dd122011-07-24 15:34:56 +00001374 for (i = 0; i < 3; i++) {
Michael Karchera4448d92010-07-22 18:04:15 +00001375 int offs;
1376 offs = 0x60 + (i * 4);
1377 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1378 mmio_readl(ich_spibar + offs), i);
1379 }
Michael Karchera4448d92010-07-22 18:04:15 +00001380 if (mmio_readw(ich_spibar) & (1 << 15)) {
1381 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1382 ichspi_lock = 1;
1383 }
Stefan Tauner7783f312011-09-17 21:21:42 +00001384 ich_set_bbar(ich_generation, 0);
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001385 register_spi_programmer(&spi_programmer_ich7);
Michael Karchera4448d92010-07-22 18:04:15 +00001386 ich_init_opcodes();
1387 break;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001388 case 8:
1389 case 9:
1390 case 10:
1391 default: /* Future version might behave the same */
Stefan Tauner29c80832011-06-12 08:14:10 +00001392 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
Michael Karchera4448d92010-07-22 18:04:15 +00001393 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
Stefan Tauner55206942011-06-11 09:53:22 +00001394 prettyprint_ich9_reg_hsfs(tmp2);
Stefan Tauner29c80832011-06-12 08:14:10 +00001395 if (tmp2 & HSFS_FLOCKDN) {
Stefan Tauner55206942011-06-11 09:53:22 +00001396 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1397 ichspi_lock = 1;
1398 }
Stefan Tauner1e146392011-09-15 23:52:55 +00001399 if (tmp2 & HSFS_FDV)
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001400 desc_valid = 1;
1401 if (!(tmp2 & HSFS_FDOPSS) && desc_valid)
Stefan Taunere3185c02011-09-18 15:15:31 +00001402 msg_pinfo("The Flash Descriptor Security Override "
1403 "Strap-Pin is set. Restrictions implied\n"
1404 "by the FRAP and FREG registers are NOT in "
1405 "effect. Please note that Protected\n"
1406 "Range (PR) restrictions still apply.\n");
Stefan Tauner55206942011-06-11 09:53:22 +00001407
Stefan Tauner29c80832011-06-12 08:14:10 +00001408 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
Stefan Tauner55206942011-06-11 09:53:22 +00001409 msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
1410 prettyprint_ich9_reg_hsfc(tmp2);
Michael Karchera4448d92010-07-22 18:04:15 +00001411
Stefan Tauner5ffe65b2011-07-07 04:10:57 +00001412 tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
1413 msg_pdbg("0x08: 0x%08x (FADDR)\n", tmp);
Stefan Tauner29c80832011-06-12 08:14:10 +00001414 tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
Michael Karchera4448d92010-07-22 18:04:15 +00001415 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1416 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1417 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1418 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1419 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1420
1421 /* print out the FREGx registers along with FRAP access bits */
1422 for(i = 0; i < 5; i++)
1423 do_ich9_spi_frap(tmp, i);
1424
Stefan Tauner75da80c2011-09-17 22:21:55 +00001425 /* try to disable PR locks before printing them */
1426 if (!ichspi_lock)
1427 for(i = 0; i < 5; i++)
1428 ich9_set_pr(i, 0, 0);
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001429 for(i = 0; i < 5; i++)
1430 prettyprint_ich9_reg_pr(i);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001431
Stefan Tauner29c80832011-06-12 08:14:10 +00001432 tmp = mmio_readl(ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001433 msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001434 prettyprint_ich9_reg_ssfs(tmp);
Stefan Tauner29c80832011-06-12 08:14:10 +00001435 if (tmp & SSFS_FCERR) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001436 msg_pdbg("Clearing SSFS.FCERR\n");
Stefan Tauner29c80832011-06-12 08:14:10 +00001437 mmio_writeb(SSFS_FCERR, ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001438 }
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001439 msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8);
1440 prettyprint_ich9_reg_ssfc(tmp);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001441
Michael Karchera4448d92010-07-22 18:04:15 +00001442 msg_pdbg("0x94: 0x%04x (PREOP)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001443 mmio_readw(ich_spibar + ICH9_REG_PREOP));
Michael Karchera4448d92010-07-22 18:04:15 +00001444 msg_pdbg("0x96: 0x%04x (OPTYPE)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001445 mmio_readw(ich_spibar + ICH9_REG_OPTYPE));
Michael Karchera4448d92010-07-22 18:04:15 +00001446 msg_pdbg("0x98: 0x%08x (OPMENU)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001447 mmio_readl(ich_spibar + ICH9_REG_OPMENU));
Michael Karchera4448d92010-07-22 18:04:15 +00001448 msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001449 mmio_readl(ich_spibar + ICH9_REG_OPMENU + 4));
Stefan Tauner1e146392011-09-15 23:52:55 +00001450 if (ich_generation == 8) {
1451 tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC);
1452 msg_pdbg("0xC1: 0x%08x (VSCC)\n", tmp);
1453 msg_pdbg("VSCC: ");
1454 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1455 } else {
1456 ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
1457 msg_pdbg("0xA0: 0x%08x (BBAR)\n",
1458 ichspi_bbar);
Stefan Taunerbd649e42011-07-01 00:39:16 +00001459
Stefan Tauner1e146392011-09-15 23:52:55 +00001460 tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC);
1461 msg_pdbg("0xC4: 0x%08x (LVSCC)\n", tmp);
1462 msg_pdbg("LVSCC: ");
1463 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1464
1465 tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC);
1466 msg_pdbg("0xC8: 0x%08x (UVSCC)\n", tmp);
1467 msg_pdbg("UVSCC: ");
1468 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1469
1470 tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
1471 msg_pdbg("0xD0: 0x%08x (FPB)\n", tmp);
Stefan Tauner7783f312011-09-17 21:21:42 +00001472 ich_set_bbar(ich_generation, 0);
Stefan Tauner1e146392011-09-15 23:52:55 +00001473 }
1474
1475 msg_pdbg("\n");
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001476 if (desc_valid) {
Stefan Tauner1e146392011-09-15 23:52:55 +00001477 struct ich_descriptors desc = {{ 0 }};
1478 if (read_ich_descriptors_via_fdo(ich_spibar, &desc) ==
1479 ICH_RET_OK)
1480 prettyprint_ich_descriptors(CHIPSET_ICH_UNKNOWN,
1481 &desc);
1482 }
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001483 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001484 ich_init_opcodes();
1485 break;
Michael Karchera4448d92010-07-22 18:04:15 +00001486 }
1487
1488 old = pci_read_byte(dev, 0xdc);
1489 msg_pdbg("SPI Read Configuration: ");
1490 new = (old >> 2) & 0x3;
1491 switch (new) {
1492 case 0:
1493 case 1:
1494 case 2:
1495 msg_pdbg("prefetching %sabled, caching %sabled, ",
1496 (new & 0x2) ? "en" : "dis",
1497 (new & 0x1) ? "dis" : "en");
1498 break;
1499 default:
1500 msg_pdbg("invalid prefetching/caching settings, ");
1501 break;
1502 }
1503 return 0;
1504}
1505
Michael Karcherb9dbe482011-05-11 17:07:07 +00001506static const struct spi_programmer spi_programmer_via = {
1507 .type = SPI_CONTROLLER_VIA,
1508 .max_data_read = 16,
1509 .max_data_write = 16,
1510 .command = ich_spi_send_command,
1511 .multicommand = ich_spi_send_multicommand,
1512 .read = default_spi_read,
1513 .write_256 = default_spi_write_256,
1514};
1515
Michael Karchera4448d92010-07-22 18:04:15 +00001516int via_init_spi(struct pci_dev *dev)
1517{
1518 uint32_t mmio_base;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001519 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00001520
1521 mmio_base = (pci_read_long(dev, 0xbc)) << 8;
1522 msg_pdbg("MMIO base at = 0x%x\n", mmio_base);
1523 ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
1524
Michael Karchera4448d92010-07-22 18:04:15 +00001525 /* Not sure if it speaks all these bus protocols. */
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001526 buses_supported = BUS_LPC | BUS_FWH;
Michael Karcherb9dbe482011-05-11 17:07:07 +00001527 register_spi_programmer(&spi_programmer_via);
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001528
1529 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1530 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1531 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1532 for (i = 0; i < 2; i++) {
1533 int offs;
1534 offs = 8 + (i * 8);
1535 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1536 mmio_readl(ich_spibar + offs), i);
1537 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1538 mmio_readl(ich_spibar + offs + 4), i);
1539 }
1540 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1541 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1542 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1543 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1544 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1545 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1546 for (i = 0; i < 3; i++) {
1547 int offs;
1548 offs = 0x60 + (i * 4);
1549 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1550 mmio_readl(ich_spibar + offs), i);
1551 }
1552 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
1553 mmio_readw(ich_spibar + 0x6c));
1554 if (mmio_readw(ich_spibar) & (1 << 15)) {
1555 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1556 ichspi_lock = 1;
1557 }
1558
Stefan Tauner7783f312011-09-17 21:21:42 +00001559 ich_set_bbar(7, 0);
Michael Karchera4448d92010-07-22 18:04:15 +00001560 ich_init_opcodes();
1561
1562 return 0;
1563}
1564
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001565#endif