blob: 19edb834cc7ec2f27143e4d1c003aeb6fa783b5f [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>
Dominik Geyerb46acba2008-05-16 12:55:55 +000029#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000030#include "programmer.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000031#include "spi.h"
Stefan Tauner1e146392011-09-15 23:52:55 +000032#include "ich_descriptors.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000033
Stefan Reinauera9424d52008-06-27 16:28:34 +000034/* ICH9 controller register definition */
Stefan Tauner55206942011-06-11 09:53:22 +000035#define ICH9_REG_HSFS 0x04 /* 16 Bits Hardware Sequencing Flash Status */
36#define HSFS_FDONE_OFF 0 /* 0: Flash Cycle Done */
37#define HSFS_FDONE (0x1 << HSFS_FDONE_OFF)
38#define HSFS_FCERR_OFF 1 /* 1: Flash Cycle Error */
39#define HSFS_FCERR (0x1 << HSFS_FCERR_OFF)
40#define HSFS_AEL_OFF 2 /* 2: Access Error Log */
41#define HSFS_AEL (0x1 << HSFS_AEL_OFF)
42#define HSFS_BERASE_OFF 3 /* 3-4: Block/Sector Erase Size */
43#define HSFS_BERASE (0x3 << HSFS_BERASE_OFF)
44#define HSFS_SCIP_OFF 5 /* 5: SPI Cycle In Progress */
45#define HSFS_SCIP (0x1 << HSFS_SCIP_OFF)
46 /* 6-12: reserved */
47#define HSFS_FDOPSS_OFF 13 /* 13: Flash Descriptor Override Pin-Strap Status */
48#define HSFS_FDOPSS (0x1 << HSFS_FDOPSS_OFF)
49#define HSFS_FDV_OFF 14 /* 14: Flash Descriptor Valid */
50#define HSFS_FDV (0x1 << HSFS_FDV_OFF)
51#define HSFS_FLOCKDN_OFF 15 /* 15: Flash Configuration Lock-Down */
52#define HSFS_FLOCKDN (0x1 << HSFS_FLOCKDN_OFF)
53
54#define ICH9_REG_HSFC 0x06 /* 16 Bits Hardware Sequencing Flash Control */
55#define HSFC_FGO_OFF 0 /* 0: Flash Cycle Go */
56#define HSFC_FGO (0x1 << HSFC_FGO_OFF)
57#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
58#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
59 /* 3-7: reserved */
60#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
61#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
62 /* 14: reserved */
63#define HSFC_SME_OFF 15 /* 15: SPI SMI# Enable */
64#define HSFC_SME (0x1 << HSFC_SME_OFF)
65
Stefan Taunerc0aaf952011-05-19 02:58:17 +000066#define ICH9_REG_FADDR 0x08 /* 32 Bits */
67#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
Stefan Reinauera9424d52008-06-27 16:28:34 +000068
Stefan Tauner29c80832011-06-12 08:14:10 +000069#define ICH9_REG_FRAP 0x50 /* 32 Bytes Flash Region Access Permissions */
70#define ICH9_REG_FREG0 0x54 /* 32 Bytes Flash Region 0 */
71
72#define ICH9_REG_PR0 0x74 /* 32 Bytes Protected Range 0 */
Stefan Taunerbf69aaa2011-09-17 21:21:48 +000073#define PR_WP_OFF 31 /* 31: write protection enable */
74#define PR_RP_OFF 15 /* 15: read protection enable */
Stefan Tauner29c80832011-06-12 08:14:10 +000075
Stefan Taunerc0aaf952011-05-19 02:58:17 +000076#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Stefan Tauner0c1ec452011-06-11 09:53:09 +000077#define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */
78#define SSFS_SCIP (0x1 << SSFS_SCIP_OFF)
79#define SSFS_FDONE_OFF 2 /* Cycle Done Status */
80#define SSFS_FDONE (0x1 << SSFS_FDONE_OFF)
81#define SSFS_FCERR_OFF 3 /* Flash Cycle Error */
82#define SSFS_FCERR (0x1 << SSFS_FCERR_OFF)
83#define SSFS_AEL_OFF 4 /* Access Error Log */
84#define SSFS_AEL (0x1 << SSFS_AEL_OFF)
Stefan Taunerc0aaf952011-05-19 02:58:17 +000085/* The following bits are reserved in SSFS: 1,5-7. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +000086#define SSFS_RESERVED_MASK 0x000000e2
Stefan Reinauera9424d52008-06-27 16:28:34 +000087
Stefan Taunerc0aaf952011-05-19 02:58:17 +000088#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Stefan Taunerc0aaf952011-05-19 02:58:17 +000089/* We combine SSFS and SSFC to one 32-bit word,
Stefan Tauner0c1ec452011-06-11 09:53:09 +000090 * therefore SSFC bits are off by 8. */
91 /* 0: reserved */
92#define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */
93#define SSFC_SCGO (0x1 << SSFC_SCGO_OFF)
94#define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */
95#define SSFC_ACS (0x1 << SSFC_ACS_OFF)
96#define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */
97#define SSFC_SPOP (0x1 << SSFC_SPOP_OFF)
98#define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */
99#define SSFC_COP (0x7 << SSFC_COP_OFF)
100 /* 7: reserved */
101#define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */
102#define SSFC_DBC (0x3f << SSFC_DBC_OFF)
103#define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */
104#define SSFC_DS (0x1 << SSFC_DS_OFF)
105#define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */
106#define SSFC_SME (0x1 << SSFC_SME_OFF)
107#define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */
108#define SSFC_SCF (0x7 << SSFC_SCF_OFF)
109#define SSFC_SCF_20MHZ 0x00000000
110#define SSFC_SCF_33MHZ 0x01000000
111 /* 19-23: reserved */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000112#define SSFC_RESERVED_MASK 0xf8008100
Stefan Reinauera9424d52008-06-27 16:28:34 +0000113
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000114#define ICH9_REG_PREOP 0x94 /* 16 Bits */
115#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
116#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000117
Stefan Tauner29c80832011-06-12 08:14:10 +0000118#define ICH9_REG_BBAR 0xA0 /* 32 Bits BIOS Base Address Configuration */
119#define BBAR_MASK 0x00ffff00 /* 8-23: Bottom of System Flash */
120
Stefan Tauner1e146392011-09-15 23:52:55 +0000121#define ICH8_REG_VSCC 0xC1 /* 32 Bits Vendor Specific Component Capabilities */
122#define ICH9_REG_LVSCC 0xC4 /* 32 Bits Host Lower Vendor Specific Component Capabilities */
123#define ICH9_REG_UVSCC 0xC8 /* 32 Bits Host Upper Vendor Specific Component Capabilities */
124/* The individual fields of the VSCC registers are defined in the file
125 * ich_descriptors.h. The reason is that the same layout is also used in the
126 * flash descriptor to define the properties of the different flash chips
127 * supported. The BIOS (or the ME?) is responsible to populate the ICH registers
128 * with the information from the descriptor on startup depending on the actual
129 * chip(s) detected. */
130
Stefan Taunerbd649e42011-07-01 00:39:16 +0000131#define ICH9_REG_FPB 0xD0 /* 32 Bits Flash Partition Boundary */
132#define FPB_FPBA_OFF 0 /* 0-12: Block/Sector Erase Size */
133#define FPB_FPBA (0x1FFF << FPB_FPBA_OFF)
134
Dominik Geyerb46acba2008-05-16 12:55:55 +0000135// ICH9R SPI commands
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000136#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
137#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
138#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
139#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
Dominik Geyerb46acba2008-05-16 12:55:55 +0000140
Stefan Reinauera9424d52008-06-27 16:28:34 +0000141// ICH7 registers
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000142#define ICH7_REG_SPIS 0x00 /* 16 Bits */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000143#define SPIS_SCIP 0x0001
144#define SPIS_GRANT 0x0002
145#define SPIS_CDS 0x0004
146#define SPIS_FCERR 0x0008
147#define SPIS_RESERVED_MASK 0x7ff0
Stefan Reinauera9424d52008-06-27 16:28:34 +0000148
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000149/* VIA SPI is compatible with ICH7, but maxdata
150 to transfer is 16 bytes.
151
152 DATA byte count on ICH7 is 8:13, on VIA 8:11
153
154 bit 12 is port select CS0 CS1
155 bit 13 is FAST READ enable
156 bit 7 is used with fast read and one shot controls CS de-assert?
157*/
158
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000159#define ICH7_REG_SPIC 0x02 /* 16 Bits */
160#define SPIC_SCGO 0x0002
161#define SPIC_ACS 0x0004
162#define SPIC_SPOP 0x0008
163#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000164
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000165#define ICH7_REG_SPIA 0x04 /* 32 Bits */
166#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
167#define ICH7_REG_PREOP 0x54 /* 16 Bits */
168#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
169#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000170
FENG yu ningc05a2952008-12-08 18:16:58 +0000171/* ICH SPI configuration lock-down. May be set during chipset enabling. */
Michael Karchera4448d92010-07-22 18:04:15 +0000172static int ichspi_lock = 0;
FENG yu ningc05a2952008-12-08 18:16:58 +0000173
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000174uint32_t ichspi_bbar = 0;
175
Michael Karchera4448d92010-07-22 18:04:15 +0000176static void *ich_spibar = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000177
Dominik Geyerb46acba2008-05-16 12:55:55 +0000178typedef struct _OPCODE {
179 uint8_t opcode; //This commands spi opcode
180 uint8_t spi_type; //This commands spi type
181 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
182} OPCODE;
183
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000184/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000185 * Preop 1: Write Enable
186 * Preop 2: Write Status register enable
187 *
188 * OP 0: Write address
189 * OP 1: Read Address
190 * OP 2: ERASE block
191 * OP 3: Read Status register
192 * OP 4: Read ID
193 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000194 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000195 * OP 7: Chip erase
196 */
197typedef struct _OPCODES {
198 uint8_t preop[2];
199 OPCODE opcode[8];
200} OPCODES;
201
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000202static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000203
204/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000205static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000206{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000207 return mmio_readl(ich_spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000208}
209
Uwe Hermann09e04f72009-05-16 22:36:00 +0000210static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000211{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000212 return mmio_readw(ich_spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000213}
214
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000215static uint16_t REGREAD8(int X)
216{
217 return mmio_readb(ich_spibar + X);
218}
219
Stefan Taunerccd92a12011-07-01 00:39:01 +0000220#define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
221#define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
222#define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off))
Dominik Geyerb46acba2008-05-16 12:55:55 +0000223
Dominik Geyerb46acba2008-05-16 12:55:55 +0000224/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000225static int find_opcode(OPCODES *op, uint8_t opcode);
226static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000227static int generate_opcodes(OPCODES * op);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000228static int program_opcodes(OPCODES *op, int enable_undo);
Stefan Reinauer43119562008-11-02 19:51:50 +0000229static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000230 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000231
FENG yu ningf041e9b2008-12-15 02:32:11 +0000232/* for pairing opcodes with their required preop */
233struct preop_opcode_pair {
234 uint8_t preop;
235 uint8_t opcode;
236};
237
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000238/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000239const struct preop_opcode_pair pops[] = {
FENG yu ningf041e9b2008-12-15 02:32:11 +0000240 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
241 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
242 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
243 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
244 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
245 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000246 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
247 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000248 {JEDEC_EWSR, JEDEC_WRSR},
249 {0,}
250};
251
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000252/* Reasonable default configuration. Needs ad-hoc modifications if we
253 * encounter unlisted opcodes. Fun.
254 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000255static OPCODES O_ST_M25P = {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000256 {
257 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000258 JEDEC_EWSR,
259 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000260 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000261 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000262 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000263 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000264 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000265 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000266 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000267 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000268 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
269 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000270};
271
Helge Wagner738e2522010-10-05 22:06:05 +0000272/* List of opcodes with their corresponding spi_type
273 * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
274 * is needed which is currently not in the chipset OPCODE table
275 */
276static OPCODE POSSIBLE_OPCODES[] = {
277 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
278 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
279 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
280 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
281 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
282 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
283 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
284 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
285 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
286 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
287 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
288};
289
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000290static OPCODES O_EXISTING = {};
FENG yu ningc05a2952008-12-08 18:16:58 +0000291
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000292/* pretty printing functions */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000293static void prettyprint_opcodes(OPCODES *ops)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000294{
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000295 OPCODE oc;
296 const char *t;
297 const char *a;
298 uint8_t i;
299 static const char *const spi_type[4] = {
300 "read w/o addr",
301 "write w/o addr",
302 "read w/ addr",
303 "write w/ addr"
304 };
305 static const char *const atomic_type[3] = {
306 "none",
307 " 0 ",
308 " 1 "
309 };
310
311 if (ops == NULL)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000312 return;
313
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000314 msg_pdbg2(" OP Type Pre-OP\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000315 for (i = 0; i < 8; i++) {
316 oc = ops->opcode[i];
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000317 t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type];
318 a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic];
319 msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000320 }
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000321 msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0],
322 ops->preop[1]);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000323}
324
325#define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF)
326
Stefan Tauner55206942011-06-11 09:53:22 +0000327static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
328{
329 msg_pdbg("HSFS: ");
330 pprint_reg(HSFS, FDONE, reg_val, ", ");
331 pprint_reg(HSFS, FCERR, reg_val, ", ");
332 pprint_reg(HSFS, AEL, reg_val, ", ");
333 pprint_reg(HSFS, BERASE, reg_val, ", ");
334 pprint_reg(HSFS, SCIP, reg_val, ", ");
335 pprint_reg(HSFS, FDOPSS, reg_val, ", ");
336 pprint_reg(HSFS, FDV, reg_val, ", ");
337 pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
338}
339
340static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
341{
342 msg_pdbg("HSFC: ");
343 pprint_reg(HSFC, FGO, reg_val, ", ");
344 pprint_reg(HSFC, FCYCLE, reg_val, ", ");
345 pprint_reg(HSFC, FDBC, reg_val, ", ");
346 pprint_reg(HSFC, SME, reg_val, "\n");
347}
348
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000349static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
350{
351 msg_pdbg("SSFS: ");
352 pprint_reg(SSFS, SCIP, reg_val, ", ");
353 pprint_reg(SSFS, FDONE, reg_val, ", ");
354 pprint_reg(SSFS, FCERR, reg_val, ", ");
355 pprint_reg(SSFS, AEL, reg_val, "\n");
356}
357
358static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
359{
360 msg_pdbg("SSFC: ");
361 pprint_reg(SSFC, SCGO, reg_val, ", ");
362 pprint_reg(SSFC, ACS, reg_val, ", ");
363 pprint_reg(SSFC, SPOP, reg_val, ", ");
364 pprint_reg(SSFC, COP, reg_val, ", ");
365 pprint_reg(SSFC, DBC, reg_val, ", ");
366 pprint_reg(SSFC, SME, reg_val, ", ");
367 pprint_reg(SSFC, SCF, reg_val, "\n");
368}
369
Helge Wagner738e2522010-10-05 22:06:05 +0000370static uint8_t lookup_spi_type(uint8_t opcode)
371{
372 int a;
373
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000374 for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
Helge Wagner738e2522010-10-05 22:06:05 +0000375 if (POSSIBLE_OPCODES[a].opcode == opcode)
376 return POSSIBLE_OPCODES[a].spi_type;
377 }
378
379 return 0xFF;
380}
381
382static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
383{
384 uint8_t spi_type;
385
386 spi_type = lookup_spi_type(opcode);
387 if (spi_type > 3) {
388 /* Try to guess spi type from read/write sizes.
389 * The following valid writecnt/readcnt combinations exist:
390 * writecnt = 4, readcnt >= 0
391 * writecnt = 1, readcnt >= 0
392 * writecnt >= 4, readcnt = 0
393 * writecnt >= 1, readcnt = 0
394 * writecnt >= 1 is guaranteed for all commands.
395 */
396 if (readcnt == 0)
397 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
398 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
399 * bytes are actual the address, they go to the bus anyhow
400 */
401 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
402 else if (writecnt == 1) // and readcnt is > 0
403 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
404 else if (writecnt == 4) // and readcnt is > 0
405 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
406 // else we have an invalid case, will be handled below
407 }
408 if (spi_type <= 3) {
409 int oppos=2; // use original JEDEC_BE_D8 offset
410 curopcodes->opcode[oppos].opcode = opcode;
411 curopcodes->opcode[oppos].spi_type = spi_type;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000412 program_opcodes(curopcodes, 0);
Helge Wagner738e2522010-10-05 22:06:05 +0000413 oppos = find_opcode(curopcodes, opcode);
414 msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
415 return oppos;
416 }
417 return -1;
418}
419
Uwe Hermann09e04f72009-05-16 22:36:00 +0000420static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000421{
422 int a;
423
424 for (a = 0; a < 8; a++) {
425 if (op->opcode[a].opcode == opcode)
426 return a;
427 }
428
429 return -1;
430}
431
Uwe Hermann09e04f72009-05-16 22:36:00 +0000432static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000433{
434 int a;
435
436 for (a = 0; a < 2; a++) {
437 if (op->preop[a] == preop)
438 return a;
439 }
440
441 return -1;
442}
443
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000444/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000445static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000446{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000447 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000448 uint16_t preop, optype;
449 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000450
451 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000452 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000453 return -1;
454 }
455
Michael Karcherb9dbe482011-05-11 17:07:07 +0000456 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000457 case SPI_CONTROLLER_ICH7:
458 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000459 preop = REGREAD16(ICH7_REG_PREOP);
460 optype = REGREAD16(ICH7_REG_OPTYPE);
461 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
462 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
463 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000464 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000465 preop = REGREAD16(ICH9_REG_PREOP);
466 optype = REGREAD16(ICH9_REG_OPTYPE);
467 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
468 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
469 break;
470 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000471 msg_perr("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000472 return -1;
473 }
474
475 op->preop[0] = (uint8_t) preop;
476 op->preop[1] = (uint8_t) (preop >> 8);
477
478 for (a = 0; a < 8; a++) {
479 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
480 optype >>= 2;
481 }
482
483 for (a = 0; a < 4; a++) {
484 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
485 opmenu[0] >>= 8;
486 }
487
488 for (a = 4; a < 8; a++) {
489 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
490 opmenu[1] >>= 8;
491 }
492
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000493 /* No preopcodes used by default. */
494 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000495 op->opcode[a].atomic = 0;
496
FENG yu ningc05a2952008-12-08 18:16:58 +0000497 return 0;
498}
499
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000500static int program_opcodes(OPCODES *op, int enable_undo)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000501{
502 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000503 uint16_t preop, optype;
504 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000505
506 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000507 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000508 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000509 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000510 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000511
Stefan Reinauera9424d52008-06-27 16:28:34 +0000512 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000513 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000514 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000515 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000516 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000517
Stefan Reinauera9424d52008-06-27 16:28:34 +0000518 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000519 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000520 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000521 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000522 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000523
Dominik Geyerb46acba2008-05-16 12:55:55 +0000524 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000525 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000526 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000527 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000528 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000529
Sean Nelson316a29f2010-05-07 20:09:04 +0000530 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 +0000531 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000532 case SPI_CONTROLLER_ICH7:
533 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000534 /* Register undo only for enable_undo=1, i.e. first call. */
535 if (enable_undo) {
536 rmmio_valw(ich_spibar + ICH7_REG_PREOP);
537 rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
538 rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
539 rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
540 }
541 mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
542 mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
543 mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
544 mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000545 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000546 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000547 /* Register undo only for enable_undo=1, i.e. first call. */
548 if (enable_undo) {
549 rmmio_valw(ich_spibar + ICH9_REG_PREOP);
550 rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
551 rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
552 rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
553 }
554 mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
555 mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
556 mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
557 mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000558 break;
559 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000560 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000561 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000562 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000563
564 return 0;
565}
566
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000567/*
568 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
569 * it didn't stick.
570 */
Stefan Tauner7783f312011-09-17 21:21:42 +0000571static void ich_set_bbar(int ich_generation, uint32_t min_addr)
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000572{
Stefan Taunere27b2d42011-07-01 00:39:09 +0000573 int bbar_off;
Stefan Tauner7783f312011-09-17 21:21:42 +0000574 switch (ich_generation) {
575 case 7:
Stefan Taunere27b2d42011-07-01 00:39:09 +0000576 bbar_off = 0x50;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000577 break;
Stefan Tauner7783f312011-09-17 21:21:42 +0000578 case 8:
579 msg_perr("BBAR offset is unknown on ICH8!\n");
580 return;
581 default: /* Future version might behave the same */
Stefan Taunere27b2d42011-07-01 00:39:09 +0000582 bbar_off = ICH9_REG_BBAR;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000583 break;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000584 }
Stefan Taunere27b2d42011-07-01 00:39:09 +0000585
586 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
587 if (ichspi_bbar) {
588 msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
589 ichspi_bbar);
590 }
591 min_addr &= BBAR_MASK;
592 ichspi_bbar |= min_addr;
593 rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
594 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
595
596 /* We don't have any option except complaining. And if the write
597 * failed, the restore will fail as well, so no problem there.
598 */
599 if (ichspi_bbar != min_addr)
Stefan Tauner7783f312011-09-17 21:21:42 +0000600 msg_perr("Setting BBAR to 0x%08x failed! New value: 0x%08x.\n",
601 min_addr, ichspi_bbar);
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000602}
603
Stefan Tauner8b391b82011-08-09 01:49:34 +0000604/* Read len bytes from the fdata/spid register into the data array.
605 *
606 * Note that using len > spi_programmer->max_data_read will return garbage or
607 * may even crash.
608 */
609 static void ich_read_data(uint8_t *data, int len, int reg0_off)
610 {
611 int i;
612 uint32_t temp32 = 0;
613
614 for (i = 0; i < len; i++) {
615 if ((i % 4) == 0)
616 temp32 = REGREAD32(reg0_off + i);
617
618 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
619 }
620}
621
622/* Fill len bytes from the data array into the fdata/spid registers.
623 *
624 * Note that using len > spi_programmer->max_data_write will trash the registers
625 * following the data registers.
626 */
627static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
628{
629 uint32_t temp32 = 0;
630 int i;
631
632 if (len <= 0)
633 return;
634
635 for (i = 0; i < len; i++) {
636 if ((i % 4) == 0)
637 temp32 = 0;
638
639 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
640
641 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
642 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
643 }
644 i--;
645 if ((i % 4) != 3) /* Write remaining data to regs. */
646 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
647}
648
FENG yu ningf041e9b2008-12-15 02:32:11 +0000649/* This function generates OPCODES from or programs OPCODES to ICH according to
650 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000651 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000652 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000653 */
Michael Karchera4448d92010-07-22 18:04:15 +0000654static int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000655{
656 int rc = 0;
657 OPCODES *curopcodes_done;
658
659 if (curopcodes)
660 return 0;
661
662 if (ichspi_lock) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000663 msg_pdbg("Reading OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000664 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000665 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000666 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000667 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000668 curopcodes_done = &O_ST_M25P;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000669 rc = program_opcodes(curopcodes_done, 1);
FENG yu ningc05a2952008-12-08 18:16:58 +0000670 }
671
672 if (rc) {
673 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000674 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000675 return 1;
676 } else {
677 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000678 msg_pdbg("done\n");
Stefan Tauner8b391b82011-08-09 01:49:34 +0000679 prettyprint_opcodes(curopcodes);
FENG yu ningc05a2952008-12-08 18:16:58 +0000680 return 0;
681 }
682}
683
Stefan Reinauer43119562008-11-02 19:51:50 +0000684static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000685 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000686{
687 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000688 int timeout;
Stefan Tauner8b391b82011-08-09 01:49:34 +0000689 uint32_t temp32;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000690 uint16_t temp16;
Stefan Reinauer43119562008-11-02 19:51:50 +0000691 uint64_t opmenu;
692 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000693
694 /* Is it a write command? */
695 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
696 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
697 write_cmd = 1;
698 }
699
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000700 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
701 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
702 programmer_delay(10);
703 }
704 if (!timeout) {
705 msg_perr("Error: SCIP never cleared!\n");
706 return 1;
707 }
708
Stefan Tauner10b3e222011-07-01 00:39:23 +0000709 /* Program offset in flash into SPIA while preserving reserved bits. */
710 temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
711 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000712
Stefan Tauner10b3e222011-07-01 00:39:23 +0000713 /* Program data into SPID0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000714 if (write_cmd && (datalength != 0))
715 ich_fill_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000716
717 /* Assemble SPIS */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000718 temp16 = REGREAD16(ICH7_REG_SPIS);
719 /* keep reserved bits */
720 temp16 &= SPIS_RESERVED_MASK;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000721 /* clear error status registers */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000722 temp16 |= (SPIS_CDS | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000723 REGWRITE16(ICH7_REG_SPIS, temp16);
724
725 /* Assemble SPIC */
726 temp16 = 0;
727
728 if (datalength != 0) {
729 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000730 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000731 }
732
733 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000734 opmenu = REGREAD32(ICH7_REG_OPMENU);
735 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
736
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000737 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
738 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000739 break;
740 }
741 opmenu >>= 8;
742 }
743 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000744 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000745 return 1;
746 }
747 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000748
Michael Karcher136125a2011-04-29 22:11:36 +0000749 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
750 /* Handle Atomic. Atomic commands include three steps:
751 - sending the preop (mainly EWSR or WREN)
752 - sending the main command
753 - waiting for the busy bit (WIP) to be cleared
754 This means the timeout must be sufficient for chip erase
755 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000756 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000757 switch (op.atomic) {
758 case 2:
759 /* Select second preop. */
760 temp16 |= SPIC_SPOP;
761 /* And fall through. */
762 case 1:
763 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000764 temp16 |= SPIC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000765 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000766 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000767 }
768
769 /* Start */
770 temp16 |= SPIC_SCGO;
771
772 /* write it */
773 REGWRITE16(ICH7_REG_SPIC, temp16);
774
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000775 /* Wait for Cycle Done Status or Flash Cycle Error. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000776 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
777 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000778 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000779 }
780 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000781 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
782 REGREAD16(ICH7_REG_SPIS));
783 return 1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000784 }
785
Sean Nelson316a29f2010-05-07 20:09:04 +0000786 /* FIXME: make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000787 temp16 = REGREAD16(ICH7_REG_SPIS);
788 if (temp16 & SPIS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000789 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000790 /* keep reserved bits */
791 temp16 &= SPIS_RESERVED_MASK;
792 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000793 return 1;
794 }
795
Stefan Tauner8b391b82011-08-09 01:49:34 +0000796 if ((!write_cmd) && (datalength != 0))
797 ich_read_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000798
799 return 0;
800}
801
Stefan Reinauer43119562008-11-02 19:51:50 +0000802static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000803 uint8_t datalength, uint8_t * data)
804{
805 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000806 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000807 uint32_t temp32;
Stefan Reinauer43119562008-11-02 19:51:50 +0000808 uint64_t opmenu;
809 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000810
811 /* Is it a write command? */
812 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
813 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
814 write_cmd = 1;
815 }
816
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000817 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
818 while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
819 programmer_delay(10);
820 }
821 if (!timeout) {
822 msg_perr("Error: SCIP never cleared!\n");
823 return 1;
824 }
825
Stefan Tauner10b3e222011-07-01 00:39:23 +0000826 /* Program offset in flash into FADDR while preserve the reserved bits
827 * and clearing the 25. address bit which is only useable in hwseq. */
828 temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
829 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000830
831 /* Program data into FDATA0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000832 if (write_cmd && (datalength != 0))
833 ich_fill_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000834
835 /* Assemble SSFS + SSFC */
Helge Wagnera319be12010-08-11 21:06:10 +0000836 temp32 = REGREAD32(ICH9_REG_SSFS);
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000837 /* Keep reserved bits only */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000838 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000839 /* Clear cycle done and cycle error status registers */
840 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000841 REGWRITE32(ICH9_REG_SSFS, temp32);
842
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000843 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000844 temp32 |= SSFC_SCF_20MHZ;
845
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000846 /* Set data byte count (DBC) and data cycle bit (DS) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000847 if (datalength != 0) {
848 uint32_t datatemp;
849 temp32 |= SSFC_DS;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000850 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
851 SSFC_DBC);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000852 temp32 |= datatemp;
853 }
854
855 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000856 opmenu = REGREAD32(ICH9_REG_OPMENU);
857 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
858
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000859 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
860 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000861 break;
862 }
863 opmenu >>= 8;
864 }
865 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000866 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000867 return 1;
868 }
869 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000870
Michael Karcher136125a2011-04-29 22:11:36 +0000871 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
872 /* Handle Atomic. Atomic commands include three steps:
873 - sending the preop (mainly EWSR or WREN)
874 - sending the main command
875 - waiting for the busy bit (WIP) to be cleared
876 This means the timeout must be sufficient for chip erase
877 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000878 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000879 switch (op.atomic) {
880 case 2:
881 /* Select second preop. */
882 temp32 |= SSFC_SPOP;
883 /* And fall through. */
884 case 1:
885 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000886 temp32 |= SSFC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000887 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000888 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000889 }
890
891 /* Start */
892 temp32 |= SSFC_SCGO;
893
894 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000895 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000896
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000897 /* Wait for Cycle Done Status or Flash Cycle Error. */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000898 while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000899 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000900 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000901 }
902 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000903 msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
904 REGREAD32(ICH9_REG_SSFS));
905 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000906 }
907
Sean Nelson316a29f2010-05-07 20:09:04 +0000908 /* FIXME make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000909 temp32 = REGREAD32(ICH9_REG_SSFS);
910 if (temp32 & SSFS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000911 msg_perr("Transaction error!\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000912 prettyprint_ich9_reg_ssfs(temp32);
913 prettyprint_ich9_reg_ssfc(temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000914 /* keep reserved bits */
915 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
916 /* Clear the transaction error. */
917 REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000918 return 1;
919 }
920
Stefan Tauner8b391b82011-08-09 01:49:34 +0000921 if ((!write_cmd) && (datalength != 0))
922 ich_read_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000923
924 return 0;
925}
926
Stefan Reinauer43119562008-11-02 19:51:50 +0000927static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000928 uint8_t datalength, uint8_t * data)
929{
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000930 /* max_data_read == max_data_write for all Intel/VIA SPI masters */
931 uint8_t maxlength = spi_programmer->max_data_read;
932
933 if (spi_programmer->type == SPI_CONTROLLER_NONE) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000934 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000935 return -1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000936 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000937
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000938 if (datalength > maxlength) {
939 msg_perr("%s: Internal command size error for "
940 "opcode 0x%02x, got datalength=%i, want <=%i\n",
941 __func__, op.opcode, datalength, maxlength);
942 return SPI_INVALID_LENGTH;
943 }
944
945 switch (spi_programmer->type) {
946 case SPI_CONTROLLER_VIA:
947 case SPI_CONTROLLER_ICH7:
948 return ich7_run_opcode(op, offset, datalength, data, maxlength);
949 case SPI_CONTROLLER_ICH9:
950 return ich9_run_opcode(op, offset, datalength, data);
951 default:
952 /* If we ever get here, something really weird happened */
953 return -1;
954 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000955}
956
Michael Karcherb9dbe482011-05-11 17:07:07 +0000957static int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000958 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000959{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000960 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000961 int opcode_index = -1;
962 const unsigned char cmd = *writearr;
963 OPCODE *opcode;
964 uint32_t addr = 0;
965 uint8_t *data;
966 int count;
967
Dominik Geyerb46acba2008-05-16 12:55:55 +0000968 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000969 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000970 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +0000971 if (!ichspi_lock)
972 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
973 if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000974 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
975 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +0000976 return SPI_INVALID_OPCODE;
977 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000978 }
979
980 opcode = &(curopcodes->opcode[opcode_index]);
981
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000982 /* The following valid writecnt/readcnt combinations exist:
983 * writecnt = 4, readcnt >= 0
984 * writecnt = 1, readcnt >= 0
985 * writecnt >= 4, readcnt = 0
986 * writecnt >= 1, readcnt = 0
987 * writecnt >= 1 is guaranteed for all commands.
988 */
989 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
990 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000991 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000992 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
993 writecnt);
994 return SPI_INVALID_LENGTH;
995 }
996 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
997 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000998 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000999 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
1000 writecnt);
1001 return SPI_INVALID_LENGTH;
1002 }
1003 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1004 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001005 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001006 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
1007 writecnt);
1008 return SPI_INVALID_LENGTH;
1009 }
1010 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1011 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1012 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001013 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001014 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1015 readcnt);
1016 return SPI_INVALID_LENGTH;
1017 }
1018
Dominik Geyerb46acba2008-05-16 12:55:55 +00001019 /* if opcode-type requires an address */
1020 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1021 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001022 addr = (writearr[1] << 16) |
1023 (writearr[2] << 8) | (writearr[3] << 0);
Michael Karcherb9dbe482011-05-11 17:07:07 +00001024 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001025 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001026 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001027 case SPI_CONTROLLER_ICH9:
1028 if (addr < ichspi_bbar) {
1029 msg_perr("%s: Address 0x%06x below allowed "
1030 "range 0x%06x-0xffffff\n", __func__,
1031 addr, ichspi_bbar);
1032 return SPI_INVALID_ADDRESS;
1033 }
1034 break;
1035 default:
1036 break;
1037 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001038 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001039
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001040 /* Translate read/write array/count.
1041 * The maximum data length is identical for the maximum read length and
1042 * for the maximum write length excluding opcode and address. Opcode and
1043 * address are stored in separate registers, not in the data registers
1044 * and are thus not counted towards data length. The only exception
1045 * applies if the opcode definition (un)intentionally classifies said
1046 * opcode incorrectly as non-address opcode or vice versa. */
Dominik Geyerb46acba2008-05-16 12:55:55 +00001047 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001048 data = (uint8_t *) (writearr + 1);
1049 count = writecnt - 1;
1050 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1051 data = (uint8_t *) (writearr + 4);
1052 count = writecnt - 4;
1053 } else {
1054 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001055 count = readcnt;
1056 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001057
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001058 result = run_opcode(*opcode, addr, count, data);
1059 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001060 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1061 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1062 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1063 msg_pdbg("at address 0x%06x ", addr);
1064 }
1065 msg_pdbg("(payload length was %d).\n", count);
1066
1067 /* Print out the data array if it contains data to write.
1068 * Errors are detected before the received data is read back into
1069 * the array so it won't make sense to print it then. */
1070 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1071 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1072 int i;
1073 msg_pspew("The data was:\n");
1074 for(i=0; i<count; i++){
1075 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1076 }
1077 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001078 }
1079
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001080 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001081}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001082
Michael Karcherb9dbe482011-05-11 17:07:07 +00001083static int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001084{
1085 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001086 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001087 int oppos, preoppos;
1088 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001089 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001090 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001091 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1092 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001093 if ((oppos == -1) && (preoppos != -1)) {
1094 /* Current command is listed as preopcode in
1095 * ICH struct OPCODES, but next command is not
1096 * listed as opcode in that struct.
1097 * Check for command sanity, then
1098 * try to reprogram the ICH opcode list.
1099 */
1100 if (find_preop(curopcodes,
1101 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001102 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001103 "preopcodes 0x%02x and 0x%02x, "
1104 "ignoring the first.\n",
1105 __func__, cmds->writearr[0],
1106 (cmds + 1)->writearr[0]);
1107 continue;
1108 }
1109 /* If the chipset is locked down, we'll fail
1110 * during execution of the next command anyway.
1111 * No need to bother with fixups.
1112 */
1113 if (!ichspi_lock) {
Helge Wagner738e2522010-10-05 22:06:05 +00001114 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1115 if (oppos == -1)
1116 continue;
1117 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001118 continue;
1119 }
1120 }
1121 if ((oppos != -1) && (preoppos != -1)) {
1122 /* Current command is listed as preopcode in
1123 * ICH struct OPCODES and next command is listed
1124 * as opcode in that struct. Match them up.
1125 */
1126 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001127 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001128 }
1129 /* If none of the above if-statements about oppos or
1130 * preoppos matched, this is a normal opcode.
1131 */
1132 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001133 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
1134 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001135 /* Reset the type of all opcodes to non-atomic. */
1136 for (i = 0; i < 8; i++)
1137 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001138 }
1139 return ret;
1140}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001141
Michael Karchera4448d92010-07-22 18:04:15 +00001142#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1143#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1144#define ICH_BRWA(x) ((x >> 8) & 0xff)
1145#define ICH_BRRA(x) ((x >> 0) & 0xff)
1146
Michael Karchera4448d92010-07-22 18:04:15 +00001147static void do_ich9_spi_frap(uint32_t frap, int i)
1148{
Mathias Krausea60faab2011-01-17 07:50:42 +00001149 static const char *const access_names[4] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001150 "locked", "read-only", "write-only", "read-write"
1151 };
Mathias Krausea60faab2011-01-17 07:50:42 +00001152 static const char *const region_names[5] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001153 "Flash Descriptor", "BIOS", "Management Engine",
1154 "Gigabit Ethernet", "Platform Data"
1155 };
1156 uint32_t base, limit;
1157 int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1158 (((ICH_BRRA(frap) >> i) & 1) << 0);
Stefan Tauner29c80832011-06-12 08:14:10 +00001159 int offset = ICH9_REG_FREG0 + i * 4;
Michael Karchera4448d92010-07-22 18:04:15 +00001160 uint32_t freg = mmio_readl(ich_spibar + offset);
1161
1162 msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n",
1163 offset, freg, i, region_names[i]);
1164
1165 base = ICH_FREG_BASE(freg);
1166 limit = ICH_FREG_LIMIT(freg);
Joshua Roysd172ecd2011-05-26 13:30:51 +00001167 if (base > limit) {
Michael Karchera4448d92010-07-22 18:04:15 +00001168 /* this FREG is disabled */
1169 msg_pdbg("%s region is unused.\n", region_names[i]);
1170 return;
1171 }
1172
Stefan Tauner1e146392011-09-15 23:52:55 +00001173 msg_pdbg("0x%08x-0x%08x is %s\n", base, (limit | 0x0fff),
1174 access_names[rwperms]);
Michael Karchera4448d92010-07-22 18:04:15 +00001175}
1176
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001177 /* In contrast to FRAP and the master section of the descriptor the bits
1178 * in the PR registers have an inverted meaning. The bits in FRAP
1179 * indicate read and write access _grant_. Here they indicate read
1180 * and write _protection_ respectively. If both bits are 0 the address
1181 * bits are ignored.
1182 */
1183#define ICH_PR_PERMS(pr) (((~((pr) >> PR_RP_OFF) & 1) << 0) | \
1184 ((~((pr) >> PR_WP_OFF) & 1) << 1))
1185
1186static void prettyprint_ich9_reg_pr(int i)
1187{
1188 static const char *const access_names[4] = {
1189 "locked", "read-only", "write-only", "read-write"
1190 };
1191 uint8_t off = ICH9_REG_PR0 + (i * 4);
1192 uint32_t pr = mmio_readl(ich_spibar + off);
1193 int rwperms = ICH_PR_PERMS(pr);
1194
1195 msg_pdbg2("0x%02X: 0x%08x (PR%u", off, pr, i);
1196 if (rwperms != 0x3)
1197 msg_pdbg2(")\n0x%08x-0x%08x is %s\n", ICH_FREG_BASE(pr),
1198 ICH_FREG_LIMIT(pr) | 0x0fff, access_names[rwperms]);
1199 else
1200 msg_pdbg2(", unused)\n");
1201}
1202
Michael Karcherb9dbe482011-05-11 17:07:07 +00001203static const struct spi_programmer spi_programmer_ich7 = {
1204 .type = SPI_CONTROLLER_ICH7,
1205 .max_data_read = 64,
1206 .max_data_write = 64,
1207 .command = ich_spi_send_command,
1208 .multicommand = ich_spi_send_multicommand,
1209 .read = default_spi_read,
1210 .write_256 = default_spi_write_256,
1211};
1212
1213static const struct spi_programmer spi_programmer_ich9 = {
1214 .type = SPI_CONTROLLER_ICH9,
1215 .max_data_read = 64,
1216 .max_data_write = 64,
1217 .command = ich_spi_send_command,
1218 .multicommand = ich_spi_send_multicommand,
1219 .read = default_spi_read,
1220 .write_256 = default_spi_write_256,
1221};
1222
Michael Karchera4448d92010-07-22 18:04:15 +00001223int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb,
1224 int ich_generation)
1225{
1226 int i;
1227 uint8_t old, new;
1228 uint16_t spibar_offset, tmp2;
1229 uint32_t tmp;
Stefan Tauner1e146392011-09-15 23:52:55 +00001230 int ichspi_desc = 0;
Michael Karchera4448d92010-07-22 18:04:15 +00001231
Michael Karchera4448d92010-07-22 18:04:15 +00001232 switch (ich_generation) {
1233 case 7:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001234 register_spi_programmer(&spi_programmer_ich7);
Michael Karchera4448d92010-07-22 18:04:15 +00001235 spibar_offset = 0x3020;
1236 break;
1237 case 8:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001238 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001239 spibar_offset = 0x3020;
1240 break;
1241 case 9:
1242 case 10:
1243 default: /* Future version might behave the same */
Michael Karcherb9dbe482011-05-11 17:07:07 +00001244 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001245 spibar_offset = 0x3800;
1246 break;
1247 }
1248
1249 /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
1250 msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset);
1251
1252 /* Assign Virtual Address */
1253 ich_spibar = rcrb + spibar_offset;
1254
Michael Karcherb9dbe482011-05-11 17:07:07 +00001255 switch (spi_programmer->type) {
Michael Karchera4448d92010-07-22 18:04:15 +00001256 case SPI_CONTROLLER_ICH7:
1257 msg_pdbg("0x00: 0x%04x (SPIS)\n",
1258 mmio_readw(ich_spibar + 0));
1259 msg_pdbg("0x02: 0x%04x (SPIC)\n",
1260 mmio_readw(ich_spibar + 2));
1261 msg_pdbg("0x04: 0x%08x (SPIA)\n",
1262 mmio_readl(ich_spibar + 4));
1263 for (i = 0; i < 8; i++) {
1264 int offs;
1265 offs = 8 + (i * 8);
1266 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1267 mmio_readl(ich_spibar + offs), i);
1268 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1269 mmio_readl(ich_spibar + offs + 4), i);
1270 }
1271 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1272 msg_pdbg("0x50: 0x%08x (BBAR)\n",
1273 ichspi_bbar);
1274 msg_pdbg("0x54: 0x%04x (PREOP)\n",
1275 mmio_readw(ich_spibar + 0x54));
1276 msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
1277 mmio_readw(ich_spibar + 0x56));
1278 msg_pdbg("0x58: 0x%08x (OPMENU)\n",
1279 mmio_readl(ich_spibar + 0x58));
1280 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
1281 mmio_readl(ich_spibar + 0x5c));
Stefan Tauner122dd122011-07-24 15:34:56 +00001282 for (i = 0; i < 3; i++) {
Michael Karchera4448d92010-07-22 18:04:15 +00001283 int offs;
1284 offs = 0x60 + (i * 4);
1285 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1286 mmio_readl(ich_spibar + offs), i);
1287 }
Michael Karchera4448d92010-07-22 18:04:15 +00001288 if (mmio_readw(ich_spibar) & (1 << 15)) {
1289 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1290 ichspi_lock = 1;
1291 }
Stefan Tauner7783f312011-09-17 21:21:42 +00001292 ich_set_bbar(ich_generation, 0);
Michael Karchera4448d92010-07-22 18:04:15 +00001293 ich_init_opcodes();
1294 break;
1295 case SPI_CONTROLLER_ICH9:
Stefan Tauner29c80832011-06-12 08:14:10 +00001296 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
Michael Karchera4448d92010-07-22 18:04:15 +00001297 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
Stefan Tauner55206942011-06-11 09:53:22 +00001298 prettyprint_ich9_reg_hsfs(tmp2);
Stefan Tauner29c80832011-06-12 08:14:10 +00001299 if (tmp2 & HSFS_FLOCKDN) {
Stefan Tauner55206942011-06-11 09:53:22 +00001300 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1301 ichspi_lock = 1;
1302 }
Stefan Tauner1e146392011-09-15 23:52:55 +00001303 if (tmp2 & HSFS_FDV)
1304 ichspi_desc = 1;
Stefan Tauner55206942011-06-11 09:53:22 +00001305
Stefan Tauner29c80832011-06-12 08:14:10 +00001306 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
Stefan Tauner55206942011-06-11 09:53:22 +00001307 msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
1308 prettyprint_ich9_reg_hsfc(tmp2);
Michael Karchera4448d92010-07-22 18:04:15 +00001309
Stefan Tauner5ffe65b2011-07-07 04:10:57 +00001310 tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
1311 msg_pdbg("0x08: 0x%08x (FADDR)\n", tmp);
Stefan Tauner29c80832011-06-12 08:14:10 +00001312 tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
Michael Karchera4448d92010-07-22 18:04:15 +00001313 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1314 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1315 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1316 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1317 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1318
1319 /* print out the FREGx registers along with FRAP access bits */
1320 for(i = 0; i < 5; i++)
1321 do_ich9_spi_frap(tmp, i);
1322
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001323 for(i = 0; i < 5; i++)
1324 prettyprint_ich9_reg_pr(i);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001325
Stefan Tauner29c80832011-06-12 08:14:10 +00001326 tmp = mmio_readl(ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001327 msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001328 prettyprint_ich9_reg_ssfs(tmp);
Stefan Tauner29c80832011-06-12 08:14:10 +00001329 if (tmp & SSFS_FCERR) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001330 msg_pdbg("Clearing SSFS.FCERR\n");
Stefan Tauner29c80832011-06-12 08:14:10 +00001331 mmio_writeb(SSFS_FCERR, ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001332 }
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001333 msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8);
1334 prettyprint_ich9_reg_ssfc(tmp);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001335
Michael Karchera4448d92010-07-22 18:04:15 +00001336 msg_pdbg("0x94: 0x%04x (PREOP)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001337 mmio_readw(ich_spibar + ICH9_REG_PREOP));
Michael Karchera4448d92010-07-22 18:04:15 +00001338 msg_pdbg("0x96: 0x%04x (OPTYPE)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001339 mmio_readw(ich_spibar + ICH9_REG_OPTYPE));
Michael Karchera4448d92010-07-22 18:04:15 +00001340 msg_pdbg("0x98: 0x%08x (OPMENU)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001341 mmio_readl(ich_spibar + ICH9_REG_OPMENU));
Michael Karchera4448d92010-07-22 18:04:15 +00001342 msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001343 mmio_readl(ich_spibar + ICH9_REG_OPMENU + 4));
Stefan Tauner1e146392011-09-15 23:52:55 +00001344 if (ich_generation == 8) {
1345 tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC);
1346 msg_pdbg("0xC1: 0x%08x (VSCC)\n", tmp);
1347 msg_pdbg("VSCC: ");
1348 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1349 } else {
1350 ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
1351 msg_pdbg("0xA0: 0x%08x (BBAR)\n",
1352 ichspi_bbar);
Stefan Taunerbd649e42011-07-01 00:39:16 +00001353
Stefan Tauner1e146392011-09-15 23:52:55 +00001354 tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC);
1355 msg_pdbg("0xC4: 0x%08x (LVSCC)\n", tmp);
1356 msg_pdbg("LVSCC: ");
1357 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1358
1359 tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC);
1360 msg_pdbg("0xC8: 0x%08x (UVSCC)\n", tmp);
1361 msg_pdbg("UVSCC: ");
1362 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1363
1364 tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
1365 msg_pdbg("0xD0: 0x%08x (FPB)\n", tmp);
Stefan Tauner7783f312011-09-17 21:21:42 +00001366 ich_set_bbar(ich_generation, 0);
Stefan Tauner1e146392011-09-15 23:52:55 +00001367 }
1368
1369 msg_pdbg("\n");
1370 if (ichspi_desc) {
1371 struct ich_descriptors desc = {{ 0 }};
1372 if (read_ich_descriptors_via_fdo(ich_spibar, &desc) ==
1373 ICH_RET_OK)
1374 prettyprint_ich_descriptors(CHIPSET_ICH_UNKNOWN,
1375 &desc);
1376 }
Michael Karchera4448d92010-07-22 18:04:15 +00001377 ich_init_opcodes();
1378 break;
1379 default:
1380 /* Nothing */
1381 break;
1382 }
1383
1384 old = pci_read_byte(dev, 0xdc);
1385 msg_pdbg("SPI Read Configuration: ");
1386 new = (old >> 2) & 0x3;
1387 switch (new) {
1388 case 0:
1389 case 1:
1390 case 2:
1391 msg_pdbg("prefetching %sabled, caching %sabled, ",
1392 (new & 0x2) ? "en" : "dis",
1393 (new & 0x1) ? "dis" : "en");
1394 break;
1395 default:
1396 msg_pdbg("invalid prefetching/caching settings, ");
1397 break;
1398 }
1399 return 0;
1400}
1401
Michael Karcherb9dbe482011-05-11 17:07:07 +00001402static const struct spi_programmer spi_programmer_via = {
1403 .type = SPI_CONTROLLER_VIA,
1404 .max_data_read = 16,
1405 .max_data_write = 16,
1406 .command = ich_spi_send_command,
1407 .multicommand = ich_spi_send_multicommand,
1408 .read = default_spi_read,
1409 .write_256 = default_spi_write_256,
1410};
1411
Michael Karchera4448d92010-07-22 18:04:15 +00001412int via_init_spi(struct pci_dev *dev)
1413{
1414 uint32_t mmio_base;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001415 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00001416
1417 mmio_base = (pci_read_long(dev, 0xbc)) << 8;
1418 msg_pdbg("MMIO base at = 0x%x\n", mmio_base);
1419 ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
1420
Michael Karchera4448d92010-07-22 18:04:15 +00001421 /* Not sure if it speaks all these bus protocols. */
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001422 buses_supported = BUS_LPC | BUS_FWH;
Michael Karcherb9dbe482011-05-11 17:07:07 +00001423 register_spi_programmer(&spi_programmer_via);
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001424
1425 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1426 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1427 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1428 for (i = 0; i < 2; i++) {
1429 int offs;
1430 offs = 8 + (i * 8);
1431 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1432 mmio_readl(ich_spibar + offs), i);
1433 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1434 mmio_readl(ich_spibar + offs + 4), i);
1435 }
1436 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1437 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1438 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1439 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1440 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1441 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1442 for (i = 0; i < 3; i++) {
1443 int offs;
1444 offs = 0x60 + (i * 4);
1445 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1446 mmio_readl(ich_spibar + offs), i);
1447 }
1448 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
1449 mmio_readw(ich_spibar + 0x6c));
1450 if (mmio_readw(ich_spibar) & (1 << 15)) {
1451 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1452 ichspi_lock = 1;
1453 }
1454
Stefan Tauner7783f312011-09-17 21:21:42 +00001455 ich_set_bbar(7, 0);
Michael Karchera4448d92010-07-22 18:04:15 +00001456 ich_init_opcodes();
1457
1458 return 0;
1459}
1460
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001461#endif