blob: 1c66986728bac2607cd7e084aebd2026f3ccc32d [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
Stefan Taunera8d838d2011-11-06 23:51:09 +0000175static enum ich_chipset ich_generation = CHIPSET_ICH_UNKNOWN;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000176uint32_t ichspi_bbar = 0;
177
Michael Karchera4448d92010-07-22 18:04:15 +0000178static void *ich_spibar = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000179
Dominik Geyerb46acba2008-05-16 12:55:55 +0000180typedef struct _OPCODE {
181 uint8_t opcode; //This commands spi opcode
182 uint8_t spi_type; //This commands spi type
183 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
184} OPCODE;
185
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000186/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000187 * Preop 1: Write Enable
188 * Preop 2: Write Status register enable
189 *
190 * OP 0: Write address
191 * OP 1: Read Address
192 * OP 2: ERASE block
193 * OP 3: Read Status register
194 * OP 4: Read ID
195 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000196 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000197 * OP 7: Chip erase
198 */
199typedef struct _OPCODES {
200 uint8_t preop[2];
201 OPCODE opcode[8];
202} OPCODES;
203
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000204static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000205
206/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000207static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000208{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000209 return mmio_readl(ich_spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000210}
211
Uwe Hermann09e04f72009-05-16 22:36:00 +0000212static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000213{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000214 return mmio_readw(ich_spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000215}
216
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000217static uint16_t REGREAD8(int X)
218{
219 return mmio_readb(ich_spibar + X);
220}
221
Stefan Taunerccd92a12011-07-01 00:39:01 +0000222#define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
223#define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
224#define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off))
Dominik Geyerb46acba2008-05-16 12:55:55 +0000225
Dominik Geyerb46acba2008-05-16 12:55:55 +0000226/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000227static int find_opcode(OPCODES *op, uint8_t opcode);
228static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000229static int generate_opcodes(OPCODES * op);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000230static int program_opcodes(OPCODES *op, int enable_undo);
Stefan Reinauer43119562008-11-02 19:51:50 +0000231static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000232 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000233
FENG yu ningf041e9b2008-12-15 02:32:11 +0000234/* for pairing opcodes with their required preop */
235struct preop_opcode_pair {
236 uint8_t preop;
237 uint8_t opcode;
238};
239
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000240/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000241const struct preop_opcode_pair pops[] = {
FENG yu ningf041e9b2008-12-15 02:32:11 +0000242 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
243 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
244 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
245 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
246 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
247 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000248 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
249 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000250 {JEDEC_EWSR, JEDEC_WRSR},
251 {0,}
252};
253
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000254/* Reasonable default configuration. Needs ad-hoc modifications if we
255 * encounter unlisted opcodes. Fun.
256 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000257static OPCODES O_ST_M25P = {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000258 {
259 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000260 JEDEC_EWSR,
261 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000262 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000263 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000264 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000265 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000266 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000267 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000268 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000269 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000270 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
271 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000272};
273
Helge Wagner738e2522010-10-05 22:06:05 +0000274/* List of opcodes with their corresponding spi_type
275 * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
276 * is needed which is currently not in the chipset OPCODE table
277 */
278static OPCODE POSSIBLE_OPCODES[] = {
279 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
280 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
281 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
282 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
283 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
284 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
285 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
286 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
287 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
288 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
289 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
290};
291
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000292static OPCODES O_EXISTING = {};
FENG yu ningc05a2952008-12-08 18:16:58 +0000293
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000294/* pretty printing functions */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000295static void prettyprint_opcodes(OPCODES *ops)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000296{
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000297 OPCODE oc;
298 const char *t;
299 const char *a;
300 uint8_t i;
301 static const char *const spi_type[4] = {
302 "read w/o addr",
303 "write w/o addr",
304 "read w/ addr",
305 "write w/ addr"
306 };
307 static const char *const atomic_type[3] = {
308 "none",
309 " 0 ",
310 " 1 "
311 };
312
313 if (ops == NULL)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000314 return;
315
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000316 msg_pdbg2(" OP Type Pre-OP\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000317 for (i = 0; i < 8; i++) {
318 oc = ops->opcode[i];
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000319 t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type];
320 a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic];
321 msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000322 }
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000323 msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0],
324 ops->preop[1]);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000325}
326
327#define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF)
328
Stefan Tauner55206942011-06-11 09:53:22 +0000329static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
330{
331 msg_pdbg("HSFS: ");
332 pprint_reg(HSFS, FDONE, reg_val, ", ");
333 pprint_reg(HSFS, FCERR, reg_val, ", ");
334 pprint_reg(HSFS, AEL, reg_val, ", ");
335 pprint_reg(HSFS, BERASE, reg_val, ", ");
336 pprint_reg(HSFS, SCIP, reg_val, ", ");
337 pprint_reg(HSFS, FDOPSS, reg_val, ", ");
338 pprint_reg(HSFS, FDV, reg_val, ", ");
339 pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
340}
341
342static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
343{
344 msg_pdbg("HSFC: ");
345 pprint_reg(HSFC, FGO, reg_val, ", ");
346 pprint_reg(HSFC, FCYCLE, reg_val, ", ");
347 pprint_reg(HSFC, FDBC, reg_val, ", ");
348 pprint_reg(HSFC, SME, reg_val, "\n");
349}
350
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000351static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
352{
353 msg_pdbg("SSFS: ");
354 pprint_reg(SSFS, SCIP, reg_val, ", ");
355 pprint_reg(SSFS, FDONE, reg_val, ", ");
356 pprint_reg(SSFS, FCERR, reg_val, ", ");
357 pprint_reg(SSFS, AEL, reg_val, "\n");
358}
359
360static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
361{
362 msg_pdbg("SSFC: ");
363 pprint_reg(SSFC, SCGO, reg_val, ", ");
364 pprint_reg(SSFC, ACS, reg_val, ", ");
365 pprint_reg(SSFC, SPOP, reg_val, ", ");
366 pprint_reg(SSFC, COP, reg_val, ", ");
367 pprint_reg(SSFC, DBC, reg_val, ", ");
368 pprint_reg(SSFC, SME, reg_val, ", ");
369 pprint_reg(SSFC, SCF, reg_val, "\n");
370}
371
Helge Wagner738e2522010-10-05 22:06:05 +0000372static uint8_t lookup_spi_type(uint8_t opcode)
373{
374 int a;
375
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000376 for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
Helge Wagner738e2522010-10-05 22:06:05 +0000377 if (POSSIBLE_OPCODES[a].opcode == opcode)
378 return POSSIBLE_OPCODES[a].spi_type;
379 }
380
381 return 0xFF;
382}
383
384static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
385{
386 uint8_t spi_type;
387
388 spi_type = lookup_spi_type(opcode);
389 if (spi_type > 3) {
390 /* Try to guess spi type from read/write sizes.
391 * The following valid writecnt/readcnt combinations exist:
392 * writecnt = 4, readcnt >= 0
393 * writecnt = 1, readcnt >= 0
394 * writecnt >= 4, readcnt = 0
395 * writecnt >= 1, readcnt = 0
396 * writecnt >= 1 is guaranteed for all commands.
397 */
398 if (readcnt == 0)
399 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
400 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
401 * bytes are actual the address, they go to the bus anyhow
402 */
403 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
404 else if (writecnt == 1) // and readcnt is > 0
405 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
406 else if (writecnt == 4) // and readcnt is > 0
407 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
408 // else we have an invalid case, will be handled below
409 }
410 if (spi_type <= 3) {
411 int oppos=2; // use original JEDEC_BE_D8 offset
412 curopcodes->opcode[oppos].opcode = opcode;
413 curopcodes->opcode[oppos].spi_type = spi_type;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000414 program_opcodes(curopcodes, 0);
Helge Wagner738e2522010-10-05 22:06:05 +0000415 oppos = find_opcode(curopcodes, opcode);
416 msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
417 return oppos;
418 }
419 return -1;
420}
421
Uwe Hermann09e04f72009-05-16 22:36:00 +0000422static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000423{
424 int a;
425
Stefan Tauner50e7c602011-11-08 10:55:54 +0000426 if (op == NULL) {
427 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
428 return -1;
429 }
430
FENG yu ningc05a2952008-12-08 18:16:58 +0000431 for (a = 0; a < 8; a++) {
432 if (op->opcode[a].opcode == opcode)
433 return a;
434 }
435
436 return -1;
437}
438
Uwe Hermann09e04f72009-05-16 22:36:00 +0000439static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000440{
441 int a;
442
Stefan Tauner50e7c602011-11-08 10:55:54 +0000443 if (op == NULL) {
444 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
445 return -1;
446 }
447
FENG yu ningc05a2952008-12-08 18:16:58 +0000448 for (a = 0; a < 2; a++) {
449 if (op->preop[a] == preop)
450 return a;
451 }
452
453 return -1;
454}
455
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000456/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000457static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000458{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000459 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000460 uint16_t preop, optype;
461 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000462
463 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000464 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000465 return -1;
466 }
467
Stefan Taunera8d838d2011-11-06 23:51:09 +0000468 switch (ich_generation) {
469 case CHIPSET_ICH7:
FENG yu ningc05a2952008-12-08 18:16:58 +0000470 preop = REGREAD16(ICH7_REG_PREOP);
471 optype = REGREAD16(ICH7_REG_OPTYPE);
472 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
473 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
474 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000475 case CHIPSET_ICH8:
476 default: /* Future version might behave the same */
FENG yu ningc05a2952008-12-08 18:16:58 +0000477 preop = REGREAD16(ICH9_REG_PREOP);
478 optype = REGREAD16(ICH9_REG_OPTYPE);
479 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
480 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
481 break;
FENG yu ningc05a2952008-12-08 18:16:58 +0000482 }
483
484 op->preop[0] = (uint8_t) preop;
485 op->preop[1] = (uint8_t) (preop >> 8);
486
487 for (a = 0; a < 8; a++) {
488 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
489 optype >>= 2;
490 }
491
492 for (a = 0; a < 4; a++) {
493 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
494 opmenu[0] >>= 8;
495 }
496
497 for (a = 4; a < 8; a++) {
498 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
499 opmenu[1] >>= 8;
500 }
501
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000502 /* No preopcodes used by default. */
503 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000504 op->opcode[a].atomic = 0;
505
FENG yu ningc05a2952008-12-08 18:16:58 +0000506 return 0;
507}
508
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000509static int program_opcodes(OPCODES *op, int enable_undo)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000510{
511 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000512 uint16_t preop, optype;
513 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000514
515 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000516 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000517 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000518 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000519 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000520
Stefan Reinauera9424d52008-06-27 16:28:34 +0000521 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000522 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000523 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000524 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000525 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000526
Stefan Reinauera9424d52008-06-27 16:28:34 +0000527 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000528 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000529 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000530 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000531 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000532
Dominik Geyerb46acba2008-05-16 12:55:55 +0000533 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000534 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000535 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000536 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000537 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000538
Sean Nelson316a29f2010-05-07 20:09:04 +0000539 msg_pdbg("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
Stefan Taunera8d838d2011-11-06 23:51:09 +0000540 switch (ich_generation) {
541 case CHIPSET_ICH7:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000542 /* Register undo only for enable_undo=1, i.e. first call. */
543 if (enable_undo) {
544 rmmio_valw(ich_spibar + ICH7_REG_PREOP);
545 rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
546 rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
547 rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
548 }
549 mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
550 mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
551 mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
552 mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000553 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000554 case CHIPSET_ICH8:
555 default: /* Future version might behave the same */
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000556 /* Register undo only for enable_undo=1, i.e. first call. */
557 if (enable_undo) {
558 rmmio_valw(ich_spibar + ICH9_REG_PREOP);
559 rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
560 rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
561 rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
562 }
563 mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
564 mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
565 mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
566 mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000567 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000568 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000569
570 return 0;
571}
572
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000573/*
Stefan Tauner50e7c602011-11-08 10:55:54 +0000574 * Returns -1 if at least one mandatory opcode is inaccessible, 0 otherwise.
575 * FIXME: this should also check for
576 * - at least one probing opcode (RDID (incl. AT25F variants?), REMS, RES?)
577 * - at least one erasing opcode (lots.)
578 * - at least one program opcode (BYTE_PROGRAM, AAI_WORD_PROGRAM, ...?)
579 * - necessary preops? (EWSR, WREN, ...?)
580 */
581static int ich_missing_opcodes()
582{
583 uint8_t ops[] = {
584 JEDEC_READ,
585 JEDEC_RDSR,
586 0
587 };
588 int i = 0;
589 while (ops[i] != 0) {
590 msg_pspew("checking for opcode 0x%02x\n", ops[i]);
591 if (find_opcode(curopcodes, ops[i]) == -1)
592 return -1;
593 i++;
594 }
595 return 0;
596}
597
598/*
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000599 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
600 * it didn't stick.
601 */
Stefan Taunera8d838d2011-11-06 23:51:09 +0000602static void ich_set_bbar(uint32_t min_addr)
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000603{
Stefan Taunere27b2d42011-07-01 00:39:09 +0000604 int bbar_off;
Stefan Tauner7783f312011-09-17 21:21:42 +0000605 switch (ich_generation) {
Stefan Taunera8d838d2011-11-06 23:51:09 +0000606 case CHIPSET_ICH7:
Stefan Taunere27b2d42011-07-01 00:39:09 +0000607 bbar_off = 0x50;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000608 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000609 case CHIPSET_ICH8:
Stefan Tauner7783f312011-09-17 21:21:42 +0000610 msg_perr("BBAR offset is unknown on ICH8!\n");
611 return;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000612 case CHIPSET_ICH9:
Stefan Tauner7783f312011-09-17 21:21:42 +0000613 default: /* Future version might behave the same */
Stefan Taunere27b2d42011-07-01 00:39:09 +0000614 bbar_off = ICH9_REG_BBAR;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000615 break;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000616 }
Stefan Taunere27b2d42011-07-01 00:39:09 +0000617
618 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
619 if (ichspi_bbar) {
620 msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
621 ichspi_bbar);
622 }
623 min_addr &= BBAR_MASK;
624 ichspi_bbar |= min_addr;
625 rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
626 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
627
628 /* We don't have any option except complaining. And if the write
629 * failed, the restore will fail as well, so no problem there.
630 */
631 if (ichspi_bbar != min_addr)
Stefan Tauner7783f312011-09-17 21:21:42 +0000632 msg_perr("Setting BBAR to 0x%08x failed! New value: 0x%08x.\n",
633 min_addr, ichspi_bbar);
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000634}
635
Stefan Tauner8b391b82011-08-09 01:49:34 +0000636/* Read len bytes from the fdata/spid register into the data array.
637 *
638 * Note that using len > spi_programmer->max_data_read will return garbage or
639 * may even crash.
640 */
641 static void ich_read_data(uint8_t *data, int len, int reg0_off)
642 {
643 int i;
644 uint32_t temp32 = 0;
645
646 for (i = 0; i < len; i++) {
647 if ((i % 4) == 0)
648 temp32 = REGREAD32(reg0_off + i);
649
650 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
651 }
652}
653
654/* Fill len bytes from the data array into the fdata/spid registers.
655 *
656 * Note that using len > spi_programmer->max_data_write will trash the registers
657 * following the data registers.
658 */
659static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
660{
661 uint32_t temp32 = 0;
662 int i;
663
664 if (len <= 0)
665 return;
666
667 for (i = 0; i < len; i++) {
668 if ((i % 4) == 0)
669 temp32 = 0;
670
671 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
672
673 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
674 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
675 }
676 i--;
677 if ((i % 4) != 3) /* Write remaining data to regs. */
678 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
679}
680
FENG yu ningf041e9b2008-12-15 02:32:11 +0000681/* This function generates OPCODES from or programs OPCODES to ICH according to
682 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000683 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000684 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000685 */
Michael Karchera4448d92010-07-22 18:04:15 +0000686static int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000687{
688 int rc = 0;
689 OPCODES *curopcodes_done;
690
691 if (curopcodes)
692 return 0;
693
694 if (ichspi_lock) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000695 msg_pdbg("Reading OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000696 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000697 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000698 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000699 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000700 curopcodes_done = &O_ST_M25P;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000701 rc = program_opcodes(curopcodes_done, 1);
FENG yu ningc05a2952008-12-08 18:16:58 +0000702 }
703
704 if (rc) {
705 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000706 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000707 return 1;
708 } else {
709 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000710 msg_pdbg("done\n");
Stefan Tauner8b391b82011-08-09 01:49:34 +0000711 prettyprint_opcodes(curopcodes);
FENG yu ningc05a2952008-12-08 18:16:58 +0000712 return 0;
713 }
714}
715
Stefan Reinauer43119562008-11-02 19:51:50 +0000716static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000717 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000718{
719 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000720 int timeout;
Stefan Tauner8b391b82011-08-09 01:49:34 +0000721 uint32_t temp32;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000722 uint16_t temp16;
Stefan Reinauer43119562008-11-02 19:51:50 +0000723 uint64_t opmenu;
724 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000725
726 /* Is it a write command? */
727 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
728 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
729 write_cmd = 1;
730 }
731
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000732 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
733 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
734 programmer_delay(10);
735 }
736 if (!timeout) {
737 msg_perr("Error: SCIP never cleared!\n");
738 return 1;
739 }
740
Stefan Tauner10b3e222011-07-01 00:39:23 +0000741 /* Program offset in flash into SPIA while preserving reserved bits. */
742 temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
743 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000744
Stefan Tauner10b3e222011-07-01 00:39:23 +0000745 /* Program data into SPID0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000746 if (write_cmd && (datalength != 0))
747 ich_fill_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000748
749 /* Assemble SPIS */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000750 temp16 = REGREAD16(ICH7_REG_SPIS);
751 /* keep reserved bits */
752 temp16 &= SPIS_RESERVED_MASK;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000753 /* clear error status registers */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000754 temp16 |= (SPIS_CDS | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000755 REGWRITE16(ICH7_REG_SPIS, temp16);
756
757 /* Assemble SPIC */
758 temp16 = 0;
759
760 if (datalength != 0) {
761 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000762 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000763 }
764
765 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000766 opmenu = REGREAD32(ICH7_REG_OPMENU);
767 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
768
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000769 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
770 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000771 break;
772 }
773 opmenu >>= 8;
774 }
775 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000776 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000777 return 1;
778 }
779 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000780
Michael Karcher136125a2011-04-29 22:11:36 +0000781 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
782 /* Handle Atomic. Atomic commands include three steps:
783 - sending the preop (mainly EWSR or WREN)
784 - sending the main command
785 - waiting for the busy bit (WIP) to be cleared
786 This means the timeout must be sufficient for chip erase
787 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000788 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000789 switch (op.atomic) {
790 case 2:
791 /* Select second preop. */
792 temp16 |= SPIC_SPOP;
793 /* And fall through. */
794 case 1:
795 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000796 temp16 |= SPIC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000797 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000798 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000799 }
800
801 /* Start */
802 temp16 |= SPIC_SCGO;
803
804 /* write it */
805 REGWRITE16(ICH7_REG_SPIC, temp16);
806
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000807 /* Wait for Cycle Done Status or Flash Cycle Error. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000808 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
809 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000810 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000811 }
812 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000813 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
814 REGREAD16(ICH7_REG_SPIS));
815 return 1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000816 }
817
Sean Nelson316a29f2010-05-07 20:09:04 +0000818 /* FIXME: make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000819 temp16 = REGREAD16(ICH7_REG_SPIS);
820 if (temp16 & SPIS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000821 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000822 /* keep reserved bits */
823 temp16 &= SPIS_RESERVED_MASK;
824 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000825 return 1;
826 }
827
Stefan Tauner8b391b82011-08-09 01:49:34 +0000828 if ((!write_cmd) && (datalength != 0))
829 ich_read_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000830
831 return 0;
832}
833
Stefan Reinauer43119562008-11-02 19:51:50 +0000834static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000835 uint8_t datalength, uint8_t * data)
836{
837 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000838 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000839 uint32_t temp32;
Stefan Reinauer43119562008-11-02 19:51:50 +0000840 uint64_t opmenu;
841 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000842
843 /* Is it a write command? */
844 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
845 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
846 write_cmd = 1;
847 }
848
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000849 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
850 while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
851 programmer_delay(10);
852 }
853 if (!timeout) {
854 msg_perr("Error: SCIP never cleared!\n");
855 return 1;
856 }
857
Stefan Tauner10b3e222011-07-01 00:39:23 +0000858 /* Program offset in flash into FADDR while preserve the reserved bits
859 * and clearing the 25. address bit which is only useable in hwseq. */
860 temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
861 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000862
863 /* Program data into FDATA0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000864 if (write_cmd && (datalength != 0))
865 ich_fill_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000866
867 /* Assemble SSFS + SSFC */
Helge Wagnera319be12010-08-11 21:06:10 +0000868 temp32 = REGREAD32(ICH9_REG_SSFS);
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000869 /* Keep reserved bits only */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000870 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000871 /* Clear cycle done and cycle error status registers */
872 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000873 REGWRITE32(ICH9_REG_SSFS, temp32);
874
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000875 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000876 temp32 |= SSFC_SCF_20MHZ;
877
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000878 /* Set data byte count (DBC) and data cycle bit (DS) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000879 if (datalength != 0) {
880 uint32_t datatemp;
881 temp32 |= SSFC_DS;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000882 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
883 SSFC_DBC);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000884 temp32 |= datatemp;
885 }
886
887 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000888 opmenu = REGREAD32(ICH9_REG_OPMENU);
889 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
890
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000891 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
892 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000893 break;
894 }
895 opmenu >>= 8;
896 }
897 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000898 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000899 return 1;
900 }
901 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000902
Michael Karcher136125a2011-04-29 22:11:36 +0000903 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
904 /* Handle Atomic. Atomic commands include three steps:
905 - sending the preop (mainly EWSR or WREN)
906 - sending the main command
907 - waiting for the busy bit (WIP) to be cleared
908 This means the timeout must be sufficient for chip erase
909 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000910 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000911 switch (op.atomic) {
912 case 2:
913 /* Select second preop. */
914 temp32 |= SSFC_SPOP;
915 /* And fall through. */
916 case 1:
917 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000918 temp32 |= SSFC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000919 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000920 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000921 }
922
923 /* Start */
924 temp32 |= SSFC_SCGO;
925
926 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000927 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000928
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000929 /* Wait for Cycle Done Status or Flash Cycle Error. */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000930 while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000931 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000932 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000933 }
934 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000935 msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
936 REGREAD32(ICH9_REG_SSFS));
937 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000938 }
939
Sean Nelson316a29f2010-05-07 20:09:04 +0000940 /* FIXME make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000941 temp32 = REGREAD32(ICH9_REG_SSFS);
942 if (temp32 & SSFS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000943 msg_perr("Transaction error!\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000944 prettyprint_ich9_reg_ssfs(temp32);
945 prettyprint_ich9_reg_ssfc(temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000946 /* keep reserved bits */
947 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
948 /* Clear the transaction error. */
949 REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000950 return 1;
951 }
952
Stefan Tauner8b391b82011-08-09 01:49:34 +0000953 if ((!write_cmd) && (datalength != 0))
954 ich_read_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000955
956 return 0;
957}
958
Stefan Reinauer43119562008-11-02 19:51:50 +0000959static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000960 uint8_t datalength, uint8_t * data)
961{
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000962 /* max_data_read == max_data_write for all Intel/VIA SPI masters */
963 uint8_t maxlength = spi_programmer->max_data_read;
964
965 if (spi_programmer->type == SPI_CONTROLLER_NONE) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000966 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000967 return -1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000968 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000969
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000970 if (datalength > maxlength) {
971 msg_perr("%s: Internal command size error for "
972 "opcode 0x%02x, got datalength=%i, want <=%i\n",
973 __func__, op.opcode, datalength, maxlength);
974 return SPI_INVALID_LENGTH;
975 }
976
Stefan Taunera8d838d2011-11-06 23:51:09 +0000977 switch (ich_generation) {
978 case CHIPSET_ICH7:
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000979 return ich7_run_opcode(op, offset, datalength, data, maxlength);
Stefan Taunera8d838d2011-11-06 23:51:09 +0000980 case CHIPSET_ICH8:
981 default: /* Future version might behave the same */
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000982 return ich9_run_opcode(op, offset, datalength, data);
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000983 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000984}
985
Michael Karcherb9dbe482011-05-11 17:07:07 +0000986static int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000987 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000988{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000989 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000990 int opcode_index = -1;
991 const unsigned char cmd = *writearr;
992 OPCODE *opcode;
993 uint32_t addr = 0;
994 uint8_t *data;
995 int count;
996
Dominik Geyerb46acba2008-05-16 12:55:55 +0000997 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000998 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000999 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +00001000 if (!ichspi_lock)
1001 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
1002 if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001003 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
1004 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +00001005 return SPI_INVALID_OPCODE;
1006 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001007 }
1008
1009 opcode = &(curopcodes->opcode[opcode_index]);
1010
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001011 /* The following valid writecnt/readcnt combinations exist:
1012 * writecnt = 4, readcnt >= 0
1013 * writecnt = 1, readcnt >= 0
1014 * writecnt >= 4, readcnt = 0
1015 * writecnt >= 1, readcnt = 0
1016 * writecnt >= 1 is guaranteed for all commands.
1017 */
1018 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
1019 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001020 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001021 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
1022 writecnt);
1023 return SPI_INVALID_LENGTH;
1024 }
1025 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
1026 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001027 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001028 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
1029 writecnt);
1030 return SPI_INVALID_LENGTH;
1031 }
1032 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1033 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001034 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001035 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
1036 writecnt);
1037 return SPI_INVALID_LENGTH;
1038 }
1039 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1040 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1041 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001042 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001043 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1044 readcnt);
1045 return SPI_INVALID_LENGTH;
1046 }
1047
Dominik Geyerb46acba2008-05-16 12:55:55 +00001048 /* if opcode-type requires an address */
1049 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1050 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001051 addr = (writearr[1] << 16) |
1052 (writearr[2] << 8) | (writearr[3] << 0);
Stefan Taunera8d838d2011-11-06 23:51:09 +00001053 if (addr < ichspi_bbar) {
1054 msg_perr("%s: Address 0x%06x below allowed "
1055 "range 0x%06x-0xffffff\n", __func__,
1056 addr, ichspi_bbar);
1057 return SPI_INVALID_ADDRESS;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001058 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001059 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001060
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001061 /* Translate read/write array/count.
1062 * The maximum data length is identical for the maximum read length and
1063 * for the maximum write length excluding opcode and address. Opcode and
1064 * address are stored in separate registers, not in the data registers
1065 * and are thus not counted towards data length. The only exception
1066 * applies if the opcode definition (un)intentionally classifies said
1067 * opcode incorrectly as non-address opcode or vice versa. */
Dominik Geyerb46acba2008-05-16 12:55:55 +00001068 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001069 data = (uint8_t *) (writearr + 1);
1070 count = writecnt - 1;
1071 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1072 data = (uint8_t *) (writearr + 4);
1073 count = writecnt - 4;
1074 } else {
1075 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001076 count = readcnt;
1077 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001078
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001079 result = run_opcode(*opcode, addr, count, data);
1080 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001081 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1082 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1083 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1084 msg_pdbg("at address 0x%06x ", addr);
1085 }
1086 msg_pdbg("(payload length was %d).\n", count);
1087
1088 /* Print out the data array if it contains data to write.
1089 * Errors are detected before the received data is read back into
1090 * the array so it won't make sense to print it then. */
1091 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1092 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1093 int i;
1094 msg_pspew("The data was:\n");
Stefan Taunerf382e352011-11-08 11:55:24 +00001095 for (i = 0; i < count; i++){
Stefan Tauner8ed29342011-04-29 23:53:09 +00001096 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1097 }
1098 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001099 }
1100
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001101 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001102}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001103
Stefan Tauner50e7c602011-11-08 10:55:54 +00001104static struct hwseq_data {
1105 uint32_t size_comp0;
1106 uint32_t size_comp1;
1107} hwseq_data;
1108
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001109/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
1110static void ich_hwseq_set_addr(uint32_t addr)
1111{
1112 uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
1113 REGWRITE32(ICH9_REG_FADDR, (addr & 0x01FFFFFF) | addr_old);
1114}
1115
1116/* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
1117 * of the block containing this address. May return nonsense if the address is
1118 * not valid. The erase block size for a specific address depends on the flash
1119 * partition layout as specified by FPB and the partition properties as defined
1120 * by UVSCC and LVSCC respectively. An alternative to implement this method
1121 * would be by querying FPB and the respective VSCC register directly.
1122 */
1123static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr)
1124{
1125 uint8_t enc_berase;
1126 static const uint32_t const dec_berase[4] = {
1127 256,
1128 4 * 1024,
1129 8 * 1024,
1130 64 * 1024
1131 };
1132
1133 ich_hwseq_set_addr(addr);
1134 enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >>
1135 HSFS_BERASE_OFF;
1136 return dec_berase[enc_berase];
1137}
1138
1139/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
1140 Resets all error flags in HSFS.
1141 Returns 0 if the cycle completes successfully without errors within
1142 timeout us, 1 on errors. */
1143static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
1144 unsigned int len)
1145{
1146 uint16_t hsfs;
1147 uint32_t addr;
1148
1149 timeout /= 8; /* scale timeout duration to counter */
1150 while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) &
1151 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
1152 --timeout) {
1153 programmer_delay(8);
1154 }
1155 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1156 if (!timeout) {
1157 addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
1158 msg_perr("Timeout error between offset 0x%08x and "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001159 "0x%08x (= 0x%08x + %d)!\n",
1160 addr, addr + len - 1, addr, len - 1);
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001161 prettyprint_ich9_reg_hsfs(hsfs);
1162 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1163 return 1;
1164 }
1165
1166 if (hsfs & HSFS_FCERR) {
1167 addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
1168 msg_perr("Transaction error between offset 0x%08x and "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001169 "0x%08x (= 0x%08x + %d)!\n",
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001170 addr, addr + len - 1, addr, len - 1);
1171 prettyprint_ich9_reg_hsfs(hsfs);
1172 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1173 return 1;
1174 }
1175 return 0;
1176}
Stefan Tauner50e7c602011-11-08 10:55:54 +00001177
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001178int ich_hwseq_probe(struct flashctx *flash)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001179{
1180 uint32_t total_size, boundary;
1181 uint32_t erase_size_low, size_low, erase_size_high, size_high;
1182 struct block_eraser *eraser;
1183
1184 total_size = hwseq_data.size_comp0 + hwseq_data.size_comp1;
1185 msg_cdbg("Found %d attached SPI flash chip",
1186 (hwseq_data.size_comp1 != 0) ? 2 : 1);
1187 if (hwseq_data.size_comp1 != 0)
1188 msg_cdbg("s with a combined");
1189 else
1190 msg_cdbg(" with a");
1191 msg_cdbg(" density of %d kB.\n", total_size / 1024);
1192 flash->total_size = total_size / 1024;
1193
1194 eraser = &(flash->block_erasers[0]);
1195 boundary = (REGREAD32(ICH9_REG_FPB) & FPB_FPBA) << 12;
1196 size_high = total_size - boundary;
1197 erase_size_high = ich_hwseq_get_erase_block_size(boundary);
1198
1199 if (boundary == 0) {
1200 msg_cdbg("There is only one partition containing the whole "
1201 "address space (0x%06x - 0x%06x).\n", 0, size_high-1);
1202 eraser->eraseblocks[0].size = erase_size_high;
1203 eraser->eraseblocks[0].count = size_high / erase_size_high;
1204 msg_cdbg("There are %d erase blocks with %d B each.\n",
1205 size_high / erase_size_high, erase_size_high);
1206 } else {
1207 msg_cdbg("The flash address space (0x%06x - 0x%06x) is divided "
1208 "at address 0x%06x in two partitions.\n",
1209 0, size_high-1, boundary);
1210 size_low = total_size - size_high;
1211 erase_size_low = ich_hwseq_get_erase_block_size(0);
1212
1213 eraser->eraseblocks[0].size = erase_size_low;
1214 eraser->eraseblocks[0].count = size_low / erase_size_low;
1215 msg_cdbg("The first partition ranges from 0x%06x to 0x%06x.\n",
1216 0, size_low-1);
1217 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1218 size_low / erase_size_low, erase_size_low);
1219
1220 eraser->eraseblocks[1].size = erase_size_high;
1221 eraser->eraseblocks[1].count = size_high / erase_size_high;
1222 msg_cdbg("The second partition ranges from 0x%06x to 0x%06x.\n",
1223 boundary, size_high-1);
1224 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1225 size_high / erase_size_high, erase_size_high);
1226 }
1227 flash->tested = TEST_OK_PREW;
1228 return 1;
1229}
1230
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001231int ich_hwseq_block_erase(struct flashctx *flash,
Stefan Tauner50e7c602011-11-08 10:55:54 +00001232 unsigned int addr,
1233 unsigned int len)
1234{
1235 uint32_t erase_block;
1236 uint16_t hsfc;
1237 uint32_t timeout = 5000 * 1000; /* 5 s for max 64 kB */
1238
1239 erase_block = ich_hwseq_get_erase_block_size(addr);
1240 if (len != erase_block) {
1241 msg_cerr("Erase block size for address 0x%06x is %d B, "
1242 "but requested erase block size is %d B. "
1243 "Not erasing anything.\n", addr, erase_block, len);
1244 return -1;
1245 }
1246
1247 /* Although the hardware supports this (it would erase the whole block
1248 * containing the address) we play safe here. */
1249 if (addr % erase_block != 0) {
1250 msg_cerr("Erase address 0x%06x is not aligned to the erase "
1251 "block boundary (any multiple of %d). "
1252 "Not erasing anything.\n", addr, erase_block);
1253 return -1;
1254 }
1255
1256 if (addr + len > flash->total_size * 1024) {
1257 msg_perr("Request to erase some inaccessible memory address(es)"
1258 " (addr=0x%x, len=%d). "
1259 "Not erasing anything.\n", addr, len);
1260 return -1;
1261 }
1262
1263 msg_pdbg("Erasing %d bytes starting at 0x%06x.\n", len, addr);
1264
1265 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
1266 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1267
1268 hsfc = REGREAD16(ICH9_REG_HSFC);
1269 hsfc &= ~HSFC_FCYCLE; /* clear operation */
1270 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
1271 hsfc |= HSFC_FGO; /* start */
1272 msg_pdbg("HSFC used for block erasing: ");
1273 prettyprint_ich9_reg_hsfc(hsfc);
1274 REGWRITE16(ICH9_REG_HSFC, hsfc);
1275
1276 if (ich_hwseq_wait_for_cycle_complete(timeout, len))
1277 return -1;
1278 return 0;
1279}
1280
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001281int ich_hwseq_read(struct flashctx *flash, uint8_t *buf, unsigned int addr,
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001282 unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001283{
1284 uint16_t hsfc;
1285 uint16_t timeout = 100 * 60;
1286 uint8_t block_len;
1287
1288 if (addr < 0 || addr + len > flash->total_size * 1024) {
1289 msg_perr("Request to read from an inaccessible memory address "
1290 "(addr=0x%x, len=%d).\n", addr, len);
1291 return -1;
1292 }
1293
1294 msg_pdbg("Reading %d bytes starting at 0x%06x.\n", len, addr);
1295 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1296 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1297
1298 while (len > 0) {
1299 block_len = min(len, opaque_programmer->max_data_read);
1300 ich_hwseq_set_addr(addr);
1301 hsfc = REGREAD16(ICH9_REG_HSFC);
1302 hsfc &= ~HSFC_FCYCLE; /* set read operation */
1303 hsfc &= ~HSFC_FDBC; /* clear byte count */
1304 /* set byte count */
1305 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1306 hsfc |= HSFC_FGO; /* start */
1307 REGWRITE16(ICH9_REG_HSFC, hsfc);
1308
1309 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
1310 return 1;
1311 ich_read_data(buf, block_len, ICH9_REG_FDATA0);
1312 addr += block_len;
1313 buf += block_len;
1314 len -= block_len;
1315 }
1316 return 0;
1317}
1318
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001319int ich_hwseq_write(struct flashctx *flash, uint8_t *buf, unsigned int addr,
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001320 unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001321{
1322 uint16_t hsfc;
1323 uint16_t timeout = 100 * 60;
1324 uint8_t block_len;
1325
1326 if (addr < 0 || addr + len > flash->total_size * 1024) {
1327 msg_perr("Request to write to an inaccessible memory address "
1328 "(addr=0x%x, len=%d).\n", addr, len);
1329 return -1;
1330 }
1331
1332 msg_pdbg("Writing %d bytes starting at 0x%06x.\n", len, addr);
1333 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1334 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1335
1336 while (len > 0) {
1337 ich_hwseq_set_addr(addr);
1338 block_len = min(len, opaque_programmer->max_data_write);
1339 ich_fill_data(buf, block_len, ICH9_REG_FDATA0);
1340 hsfc = REGREAD16(ICH9_REG_HSFC);
1341 hsfc &= ~HSFC_FCYCLE; /* clear operation */
1342 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
1343 hsfc &= ~HSFC_FDBC; /* clear byte count */
1344 /* set byte count */
1345 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1346 hsfc |= HSFC_FGO; /* start */
1347 REGWRITE16(ICH9_REG_HSFC, hsfc);
1348
1349 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
1350 return -1;
1351 addr += block_len;
1352 buf += block_len;
1353 len -= block_len;
1354 }
1355 return 0;
1356}
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001357
Michael Karcherb9dbe482011-05-11 17:07:07 +00001358static int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001359{
1360 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001361 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001362 int oppos, preoppos;
1363 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001364 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001365 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001366 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1367 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001368 if ((oppos == -1) && (preoppos != -1)) {
1369 /* Current command is listed as preopcode in
1370 * ICH struct OPCODES, but next command is not
1371 * listed as opcode in that struct.
1372 * Check for command sanity, then
1373 * try to reprogram the ICH opcode list.
1374 */
1375 if (find_preop(curopcodes,
1376 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001377 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001378 "preopcodes 0x%02x and 0x%02x, "
1379 "ignoring the first.\n",
1380 __func__, cmds->writearr[0],
1381 (cmds + 1)->writearr[0]);
1382 continue;
1383 }
1384 /* If the chipset is locked down, we'll fail
1385 * during execution of the next command anyway.
1386 * No need to bother with fixups.
1387 */
1388 if (!ichspi_lock) {
Helge Wagner738e2522010-10-05 22:06:05 +00001389 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1390 if (oppos == -1)
1391 continue;
1392 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001393 continue;
1394 }
1395 }
1396 if ((oppos != -1) && (preoppos != -1)) {
1397 /* Current command is listed as preopcode in
1398 * ICH struct OPCODES and next command is listed
1399 * as opcode in that struct. Match them up.
1400 */
1401 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001402 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001403 }
1404 /* If none of the above if-statements about oppos or
1405 * preoppos matched, this is a normal opcode.
1406 */
1407 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001408 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
1409 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001410 /* Reset the type of all opcodes to non-atomic. */
1411 for (i = 0; i < 8; i++)
1412 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001413 }
1414 return ret;
1415}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001416
Michael Karchera4448d92010-07-22 18:04:15 +00001417#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1418#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1419#define ICH_BRWA(x) ((x >> 8) & 0xff)
1420#define ICH_BRRA(x) ((x >> 0) & 0xff)
1421
Michael Karchera4448d92010-07-22 18:04:15 +00001422static void do_ich9_spi_frap(uint32_t frap, int i)
1423{
Mathias Krausea60faab2011-01-17 07:50:42 +00001424 static const char *const access_names[4] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001425 "locked", "read-only", "write-only", "read-write"
1426 };
Mathias Krausea60faab2011-01-17 07:50:42 +00001427 static const char *const region_names[5] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001428 "Flash Descriptor", "BIOS", "Management Engine",
1429 "Gigabit Ethernet", "Platform Data"
1430 };
1431 uint32_t base, limit;
1432 int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1433 (((ICH_BRRA(frap) >> i) & 1) << 0);
Stefan Tauner29c80832011-06-12 08:14:10 +00001434 int offset = ICH9_REG_FREG0 + i * 4;
Michael Karchera4448d92010-07-22 18:04:15 +00001435 uint32_t freg = mmio_readl(ich_spibar + offset);
1436
1437 msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n",
1438 offset, freg, i, region_names[i]);
1439
1440 base = ICH_FREG_BASE(freg);
1441 limit = ICH_FREG_LIMIT(freg);
Joshua Roysd172ecd2011-05-26 13:30:51 +00001442 if (base > limit) {
Michael Karchera4448d92010-07-22 18:04:15 +00001443 /* this FREG is disabled */
1444 msg_pdbg("%s region is unused.\n", region_names[i]);
1445 return;
1446 }
1447
Stefan Tauner1e146392011-09-15 23:52:55 +00001448 msg_pdbg("0x%08x-0x%08x is %s\n", base, (limit | 0x0fff),
1449 access_names[rwperms]);
Michael Karchera4448d92010-07-22 18:04:15 +00001450}
1451
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001452 /* In contrast to FRAP and the master section of the descriptor the bits
1453 * in the PR registers have an inverted meaning. The bits in FRAP
1454 * indicate read and write access _grant_. Here they indicate read
1455 * and write _protection_ respectively. If both bits are 0 the address
1456 * bits are ignored.
1457 */
1458#define ICH_PR_PERMS(pr) (((~((pr) >> PR_RP_OFF) & 1) << 0) | \
1459 ((~((pr) >> PR_WP_OFF) & 1) << 1))
1460
1461static void prettyprint_ich9_reg_pr(int i)
1462{
1463 static const char *const access_names[4] = {
1464 "locked", "read-only", "write-only", "read-write"
1465 };
1466 uint8_t off = ICH9_REG_PR0 + (i * 4);
1467 uint32_t pr = mmio_readl(ich_spibar + off);
1468 int rwperms = ICH_PR_PERMS(pr);
1469
1470 msg_pdbg2("0x%02X: 0x%08x (PR%u", off, pr, i);
1471 if (rwperms != 0x3)
1472 msg_pdbg2(")\n0x%08x-0x%08x is %s\n", ICH_FREG_BASE(pr),
1473 ICH_FREG_LIMIT(pr) | 0x0fff, access_names[rwperms]);
1474 else
1475 msg_pdbg2(", unused)\n");
1476}
1477
Stefan Tauner75da80c2011-09-17 22:21:55 +00001478/* Set/Clear the read and write protection enable bits of PR register @i
1479 * according to @read_prot and @write_prot. */
1480static void ich9_set_pr(int i, int read_prot, int write_prot)
1481{
1482 void *addr = ich_spibar + ICH9_REG_PR0 + (i * 4);
1483 uint32_t old = mmio_readl(addr);
1484 uint32_t new;
1485
1486 msg_gspew("PR%u is 0x%08x", i, old);
1487 new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF));
1488 if (read_prot)
1489 new |= (1 << PR_RP_OFF);
1490 if (write_prot)
1491 new |= (1 << PR_WP_OFF);
1492 if (old == new) {
1493 msg_gspew(" already.\n");
1494 return;
1495 }
1496 msg_gspew(", trying to set it to 0x%08x ", new);
1497 rmmio_writel(new, addr);
1498 msg_gspew("resulted in 0x%08x.\n", mmio_readl(addr));
1499}
1500
Michael Karcherb9dbe482011-05-11 17:07:07 +00001501static const struct spi_programmer spi_programmer_ich7 = {
1502 .type = SPI_CONTROLLER_ICH7,
1503 .max_data_read = 64,
1504 .max_data_write = 64,
1505 .command = ich_spi_send_command,
1506 .multicommand = ich_spi_send_multicommand,
1507 .read = default_spi_read,
1508 .write_256 = default_spi_write_256,
1509};
1510
1511static const struct spi_programmer spi_programmer_ich9 = {
1512 .type = SPI_CONTROLLER_ICH9,
1513 .max_data_read = 64,
1514 .max_data_write = 64,
1515 .command = ich_spi_send_command,
1516 .multicommand = ich_spi_send_multicommand,
1517 .read = default_spi_read,
1518 .write_256 = default_spi_write_256,
1519};
1520
Stefan Tauner50e7c602011-11-08 10:55:54 +00001521static const struct opaque_programmer opaque_programmer_ich_hwseq = {
1522 .max_data_read = 64,
1523 .max_data_write = 64,
1524 .probe = ich_hwseq_probe,
1525 .read = ich_hwseq_read,
1526 .write = ich_hwseq_write,
1527 .erase = ich_hwseq_block_erase,
1528};
1529
Michael Karchera4448d92010-07-22 18:04:15 +00001530int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb,
Stefan Taunera8d838d2011-11-06 23:51:09 +00001531 enum ich_chipset ich_gen)
Michael Karchera4448d92010-07-22 18:04:15 +00001532{
1533 int i;
1534 uint8_t old, new;
1535 uint16_t spibar_offset, tmp2;
1536 uint32_t tmp;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001537 char *arg;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001538 int desc_valid = 0;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001539 struct ich_descriptors desc = {{ 0 }};
1540 enum ich_spi_mode {
1541 ich_auto,
1542 ich_hwseq,
1543 ich_swseq
1544 } ich_spi_mode = ich_auto;
Michael Karchera4448d92010-07-22 18:04:15 +00001545
Stefan Taunera8d838d2011-11-06 23:51:09 +00001546 ich_generation = ich_gen;
1547
Michael Karchera4448d92010-07-22 18:04:15 +00001548 switch (ich_generation) {
Stefan Taunera8d838d2011-11-06 23:51:09 +00001549 case CHIPSET_ICH_UNKNOWN:
Stefan Tauner50e7c602011-11-08 10:55:54 +00001550 return ERROR_FATAL;
Stefan Taunera8d838d2011-11-06 23:51:09 +00001551 case CHIPSET_ICH7:
1552 case CHIPSET_ICH8:
Michael Karchera4448d92010-07-22 18:04:15 +00001553 spibar_offset = 0x3020;
1554 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +00001555 case CHIPSET_ICH9:
Michael Karchera4448d92010-07-22 18:04:15 +00001556 default: /* Future version might behave the same */
Michael Karchera4448d92010-07-22 18:04:15 +00001557 spibar_offset = 0x3800;
1558 break;
1559 }
1560
1561 /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
1562 msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset);
1563
1564 /* Assign Virtual Address */
1565 ich_spibar = rcrb + spibar_offset;
1566
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001567 switch (ich_generation) {
Stefan Taunera8d838d2011-11-06 23:51:09 +00001568 case CHIPSET_ICH7:
Michael Karchera4448d92010-07-22 18:04:15 +00001569 msg_pdbg("0x00: 0x%04x (SPIS)\n",
1570 mmio_readw(ich_spibar + 0));
1571 msg_pdbg("0x02: 0x%04x (SPIC)\n",
1572 mmio_readw(ich_spibar + 2));
1573 msg_pdbg("0x04: 0x%08x (SPIA)\n",
1574 mmio_readl(ich_spibar + 4));
1575 for (i = 0; i < 8; i++) {
1576 int offs;
1577 offs = 8 + (i * 8);
1578 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1579 mmio_readl(ich_spibar + offs), i);
1580 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1581 mmio_readl(ich_spibar + offs + 4), i);
1582 }
1583 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1584 msg_pdbg("0x50: 0x%08x (BBAR)\n",
1585 ichspi_bbar);
1586 msg_pdbg("0x54: 0x%04x (PREOP)\n",
1587 mmio_readw(ich_spibar + 0x54));
1588 msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
1589 mmio_readw(ich_spibar + 0x56));
1590 msg_pdbg("0x58: 0x%08x (OPMENU)\n",
1591 mmio_readl(ich_spibar + 0x58));
1592 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
1593 mmio_readl(ich_spibar + 0x5c));
Stefan Tauner122dd122011-07-24 15:34:56 +00001594 for (i = 0; i < 3; i++) {
Michael Karchera4448d92010-07-22 18:04:15 +00001595 int offs;
1596 offs = 0x60 + (i * 4);
1597 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1598 mmio_readl(ich_spibar + offs), i);
1599 }
Michael Karchera4448d92010-07-22 18:04:15 +00001600 if (mmio_readw(ich_spibar) & (1 << 15)) {
1601 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1602 ichspi_lock = 1;
1603 }
Stefan Tauner745f6bb2011-11-13 15:17:10 +00001604 ich_init_opcodes();
Stefan Taunera8d838d2011-11-06 23:51:09 +00001605 ich_set_bbar(0);
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001606 register_spi_programmer(&spi_programmer_ich7);
Michael Karchera4448d92010-07-22 18:04:15 +00001607 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +00001608 case CHIPSET_ICH8:
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001609 default: /* Future version might behave the same */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001610 arg = extract_programmer_param("ich_spi_mode");
1611 if (arg && !strcmp(arg, "hwseq")) {
1612 ich_spi_mode = ich_hwseq;
1613 msg_pspew("user selected hwseq\n");
1614 } else if (arg && !strcmp(arg, "swseq")) {
1615 ich_spi_mode = ich_swseq;
1616 msg_pspew("user selected swseq\n");
1617 } else if (arg && !strcmp(arg, "auto")) {
1618 msg_pspew("user selected auto\n");
1619 ich_spi_mode = ich_auto;
1620 } else if (arg && !strlen(arg)) {
1621 msg_perr("Missing argument for ich_spi_mode.\n");
1622 free(arg);
1623 return ERROR_FATAL;
1624 } else if (arg) {
1625 msg_perr("Unknown argument for ich_spi_mode: %s\n",
1626 arg);
1627 free(arg);
1628 return ERROR_FATAL;
1629 }
1630 free(arg);
1631
Stefan Tauner29c80832011-06-12 08:14:10 +00001632 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
Michael Karchera4448d92010-07-22 18:04:15 +00001633 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
Stefan Tauner55206942011-06-11 09:53:22 +00001634 prettyprint_ich9_reg_hsfs(tmp2);
Stefan Tauner29c80832011-06-12 08:14:10 +00001635 if (tmp2 & HSFS_FLOCKDN) {
Stefan Tauner55206942011-06-11 09:53:22 +00001636 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1637 ichspi_lock = 1;
1638 }
Stefan Tauner1e146392011-09-15 23:52:55 +00001639 if (tmp2 & HSFS_FDV)
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001640 desc_valid = 1;
1641 if (!(tmp2 & HSFS_FDOPSS) && desc_valid)
Stefan Taunere3185c02011-09-18 15:15:31 +00001642 msg_pinfo("The Flash Descriptor Security Override "
1643 "Strap-Pin is set. Restrictions implied\n"
1644 "by the FRAP and FREG registers are NOT in "
1645 "effect. Please note that Protected\n"
1646 "Range (PR) restrictions still apply.\n");
Stefan Tauner745f6bb2011-11-13 15:17:10 +00001647 ich_init_opcodes();
Stefan Tauner55206942011-06-11 09:53:22 +00001648
Stefan Taunerf382e352011-11-08 11:55:24 +00001649 if (desc_valid) {
1650 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
1651 msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
1652 prettyprint_ich9_reg_hsfc(tmp2);
1653 }
Michael Karchera4448d92010-07-22 18:04:15 +00001654
Stefan Tauner5ffe65b2011-07-07 04:10:57 +00001655 tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
1656 msg_pdbg("0x08: 0x%08x (FADDR)\n", tmp);
Michael Karchera4448d92010-07-22 18:04:15 +00001657
Stefan Taunerf382e352011-11-08 11:55:24 +00001658 if (desc_valid) {
1659 tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
1660 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1661 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1662 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1663 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1664 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1665
1666 /* Decode and print FREGx and FRAP registers */
1667 for (i = 0; i < 5; i++)
1668 do_ich9_spi_frap(tmp, i);
1669 }
Michael Karchera4448d92010-07-22 18:04:15 +00001670
Stefan Tauner75da80c2011-09-17 22:21:55 +00001671 /* try to disable PR locks before printing them */
1672 if (!ichspi_lock)
Stefan Taunerf382e352011-11-08 11:55:24 +00001673 for (i = 0; i < 5; i++)
Stefan Tauner75da80c2011-09-17 22:21:55 +00001674 ich9_set_pr(i, 0, 0);
Stefan Taunerf382e352011-11-08 11:55:24 +00001675 for (i = 0; i < 5; i++)
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001676 prettyprint_ich9_reg_pr(i);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001677
Stefan Tauner29c80832011-06-12 08:14:10 +00001678 tmp = mmio_readl(ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001679 msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001680 prettyprint_ich9_reg_ssfs(tmp);
Stefan Tauner29c80832011-06-12 08:14:10 +00001681 if (tmp & SSFS_FCERR) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001682 msg_pdbg("Clearing SSFS.FCERR\n");
Stefan Tauner29c80832011-06-12 08:14:10 +00001683 mmio_writeb(SSFS_FCERR, ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001684 }
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001685 msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8);
1686 prettyprint_ich9_reg_ssfc(tmp);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001687
Michael Karchera4448d92010-07-22 18:04:15 +00001688 msg_pdbg("0x94: 0x%04x (PREOP)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001689 mmio_readw(ich_spibar + ICH9_REG_PREOP));
Michael Karchera4448d92010-07-22 18:04:15 +00001690 msg_pdbg("0x96: 0x%04x (OPTYPE)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001691 mmio_readw(ich_spibar + ICH9_REG_OPTYPE));
Michael Karchera4448d92010-07-22 18:04:15 +00001692 msg_pdbg("0x98: 0x%08x (OPMENU)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001693 mmio_readl(ich_spibar + ICH9_REG_OPMENU));
Michael Karchera4448d92010-07-22 18:04:15 +00001694 msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001695 mmio_readl(ich_spibar + ICH9_REG_OPMENU + 4));
Stefan Taunerf382e352011-11-08 11:55:24 +00001696 if (ich_generation == CHIPSET_ICH8 && desc_valid) {
Stefan Tauner1e146392011-09-15 23:52:55 +00001697 tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC);
1698 msg_pdbg("0xC1: 0x%08x (VSCC)\n", tmp);
1699 msg_pdbg("VSCC: ");
1700 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1701 } else {
1702 ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
1703 msg_pdbg("0xA0: 0x%08x (BBAR)\n",
1704 ichspi_bbar);
Stefan Taunerbd649e42011-07-01 00:39:16 +00001705
Stefan Taunerf382e352011-11-08 11:55:24 +00001706 if (desc_valid) {
1707 tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC);
1708 msg_pdbg("0xC4: 0x%08x (LVSCC)\n", tmp);
1709 msg_pdbg("LVSCC: ");
1710 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
Stefan Tauner1e146392011-09-15 23:52:55 +00001711
Stefan Taunerf382e352011-11-08 11:55:24 +00001712 tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC);
1713 msg_pdbg("0xC8: 0x%08x (UVSCC)\n", tmp);
1714 msg_pdbg("UVSCC: ");
1715 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
Stefan Tauner1e146392011-09-15 23:52:55 +00001716
Stefan Taunerf382e352011-11-08 11:55:24 +00001717 tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
1718 msg_pdbg("0xD0: 0x%08x (FPB)\n", tmp);
1719 }
Stefan Taunera8d838d2011-11-06 23:51:09 +00001720 ich_set_bbar(0);
Stefan Tauner1e146392011-09-15 23:52:55 +00001721 }
1722
1723 msg_pdbg("\n");
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001724 if (desc_valid) {
Stefan Tauner1e146392011-09-15 23:52:55 +00001725 if (read_ich_descriptors_via_fdo(ich_spibar, &desc) ==
1726 ICH_RET_OK)
1727 prettyprint_ich_descriptors(CHIPSET_ICH_UNKNOWN,
1728 &desc);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001729 /* If the descriptor is valid and indicates multiple
1730 * flash devices we need to use hwseq to be able to
1731 * access the second flash device.
1732 */
1733 if (ich_spi_mode == ich_auto && desc.content.NC != 0) {
1734 msg_pinfo("Enabling hardware sequencing due to "
1735 "multiple flash chips detected.\n");
1736 ich_spi_mode = ich_hwseq;
1737 }
Stefan Tauner1e146392011-09-15 23:52:55 +00001738 }
Stefan Tauner50e7c602011-11-08 10:55:54 +00001739
1740 if (ich_spi_mode == ich_auto && ichspi_lock &&
1741 ich_missing_opcodes()) {
1742 msg_pinfo("Enabling hardware sequencing because "
1743 "some important opcode is locked.\n");
1744 ich_spi_mode = ich_hwseq;
1745 }
1746
1747 if (ich_spi_mode == ich_hwseq) {
1748 if (!desc_valid) {
1749 msg_perr("Hardware sequencing was requested "
1750 "but the flash descriptor is not "
1751 "valid. Aborting.\n");
1752 return ERROR_FATAL;
1753 }
1754 hwseq_data.size_comp0 = getFCBA_component_density(&desc, 0);
1755 hwseq_data.size_comp1 = getFCBA_component_density(&desc, 1);
1756 register_opaque_programmer(&opaque_programmer_ich_hwseq);
1757 } else {
1758 register_spi_programmer(&spi_programmer_ich9);
1759 }
Michael Karchera4448d92010-07-22 18:04:15 +00001760 break;
Michael Karchera4448d92010-07-22 18:04:15 +00001761 }
1762
1763 old = pci_read_byte(dev, 0xdc);
1764 msg_pdbg("SPI Read Configuration: ");
1765 new = (old >> 2) & 0x3;
1766 switch (new) {
1767 case 0:
1768 case 1:
1769 case 2:
1770 msg_pdbg("prefetching %sabled, caching %sabled, ",
1771 (new & 0x2) ? "en" : "dis",
1772 (new & 0x1) ? "dis" : "en");
1773 break;
1774 default:
1775 msg_pdbg("invalid prefetching/caching settings, ");
1776 break;
1777 }
1778 return 0;
1779}
1780
Michael Karcherb9dbe482011-05-11 17:07:07 +00001781static const struct spi_programmer spi_programmer_via = {
1782 .type = SPI_CONTROLLER_VIA,
1783 .max_data_read = 16,
1784 .max_data_write = 16,
1785 .command = ich_spi_send_command,
1786 .multicommand = ich_spi_send_multicommand,
1787 .read = default_spi_read,
1788 .write_256 = default_spi_write_256,
1789};
1790
Michael Karchera4448d92010-07-22 18:04:15 +00001791int via_init_spi(struct pci_dev *dev)
1792{
1793 uint32_t mmio_base;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001794 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00001795
1796 mmio_base = (pci_read_long(dev, 0xbc)) << 8;
1797 msg_pdbg("MMIO base at = 0x%x\n", mmio_base);
1798 ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
1799
Michael Karchera4448d92010-07-22 18:04:15 +00001800 /* Not sure if it speaks all these bus protocols. */
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +00001801 internal_buses_supported = BUS_LPC | BUS_FWH;
Stefan Taunera8d838d2011-11-06 23:51:09 +00001802 ich_generation = CHIPSET_ICH7;
Michael Karcherb9dbe482011-05-11 17:07:07 +00001803 register_spi_programmer(&spi_programmer_via);
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001804
1805 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1806 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1807 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1808 for (i = 0; i < 2; i++) {
1809 int offs;
1810 offs = 8 + (i * 8);
1811 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1812 mmio_readl(ich_spibar + offs), i);
1813 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1814 mmio_readl(ich_spibar + offs + 4), i);
1815 }
1816 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1817 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1818 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1819 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1820 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1821 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1822 for (i = 0; i < 3; i++) {
1823 int offs;
1824 offs = 0x60 + (i * 4);
1825 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1826 mmio_readl(ich_spibar + offs), i);
1827 }
1828 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
1829 mmio_readw(ich_spibar + 0x6c));
1830 if (mmio_readw(ich_spibar) & (1 << 15)) {
1831 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1832 ichspi_lock = 1;
1833 }
1834
Stefan Taunera8d838d2011-11-06 23:51:09 +00001835 ich_set_bbar(0);
Michael Karchera4448d92010-07-22 18:04:15 +00001836 ich_init_opcodes();
1837
1838 return 0;
1839}
1840
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001841#endif