blob: be13f915ec933b6f7ef37f761184c80c484fdd86 [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"
Patrick Georgi32508eb2012-07-20 20:35:14 +000032#include "hwaccess.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000033#include "spi.h"
Stefan Tauner1e146392011-09-15 23:52:55 +000034#include "ich_descriptors.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000035
Stefan Reinauera9424d52008-06-27 16:28:34 +000036/* ICH9 controller register definition */
Stefan Tauner55206942011-06-11 09:53:22 +000037#define ICH9_REG_HSFS 0x04 /* 16 Bits Hardware Sequencing Flash Status */
38#define HSFS_FDONE_OFF 0 /* 0: Flash Cycle Done */
39#define HSFS_FDONE (0x1 << HSFS_FDONE_OFF)
40#define HSFS_FCERR_OFF 1 /* 1: Flash Cycle Error */
41#define HSFS_FCERR (0x1 << HSFS_FCERR_OFF)
42#define HSFS_AEL_OFF 2 /* 2: Access Error Log */
43#define HSFS_AEL (0x1 << HSFS_AEL_OFF)
44#define HSFS_BERASE_OFF 3 /* 3-4: Block/Sector Erase Size */
45#define HSFS_BERASE (0x3 << HSFS_BERASE_OFF)
46#define HSFS_SCIP_OFF 5 /* 5: SPI Cycle In Progress */
47#define HSFS_SCIP (0x1 << HSFS_SCIP_OFF)
48 /* 6-12: reserved */
49#define HSFS_FDOPSS_OFF 13 /* 13: Flash Descriptor Override Pin-Strap Status */
50#define HSFS_FDOPSS (0x1 << HSFS_FDOPSS_OFF)
51#define HSFS_FDV_OFF 14 /* 14: Flash Descriptor Valid */
52#define HSFS_FDV (0x1 << HSFS_FDV_OFF)
53#define HSFS_FLOCKDN_OFF 15 /* 15: Flash Configuration Lock-Down */
54#define HSFS_FLOCKDN (0x1 << HSFS_FLOCKDN_OFF)
55
56#define ICH9_REG_HSFC 0x06 /* 16 Bits Hardware Sequencing Flash Control */
57#define HSFC_FGO_OFF 0 /* 0: Flash Cycle Go */
58#define HSFC_FGO (0x1 << HSFC_FGO_OFF)
59#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
60#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
61 /* 3-7: reserved */
62#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
63#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
64 /* 14: reserved */
65#define HSFC_SME_OFF 15 /* 15: SPI SMI# Enable */
66#define HSFC_SME (0x1 << HSFC_SME_OFF)
67
Stefan Taunerc0aaf952011-05-19 02:58:17 +000068#define ICH9_REG_FADDR 0x08 /* 32 Bits */
69#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
Stefan Reinauera9424d52008-06-27 16:28:34 +000070
Stefan Tauner29c80832011-06-12 08:14:10 +000071#define ICH9_REG_FRAP 0x50 /* 32 Bytes Flash Region Access Permissions */
72#define ICH9_REG_FREG0 0x54 /* 32 Bytes Flash Region 0 */
73
74#define ICH9_REG_PR0 0x74 /* 32 Bytes Protected Range 0 */
Stefan Taunerbf69aaa2011-09-17 21:21:48 +000075#define PR_WP_OFF 31 /* 31: write protection enable */
76#define PR_RP_OFF 15 /* 15: read protection enable */
Stefan Tauner29c80832011-06-12 08:14:10 +000077
Stefan Taunerc0aaf952011-05-19 02:58:17 +000078#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Stefan Tauner0c1ec452011-06-11 09:53:09 +000079#define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */
80#define SSFS_SCIP (0x1 << SSFS_SCIP_OFF)
81#define SSFS_FDONE_OFF 2 /* Cycle Done Status */
82#define SSFS_FDONE (0x1 << SSFS_FDONE_OFF)
83#define SSFS_FCERR_OFF 3 /* Flash Cycle Error */
84#define SSFS_FCERR (0x1 << SSFS_FCERR_OFF)
85#define SSFS_AEL_OFF 4 /* Access Error Log */
86#define SSFS_AEL (0x1 << SSFS_AEL_OFF)
Stefan Taunerc0aaf952011-05-19 02:58:17 +000087/* The following bits are reserved in SSFS: 1,5-7. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +000088#define SSFS_RESERVED_MASK 0x000000e2
Stefan Reinauera9424d52008-06-27 16:28:34 +000089
Stefan Taunerc0aaf952011-05-19 02:58:17 +000090#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Stefan Taunerc0aaf952011-05-19 02:58:17 +000091/* We combine SSFS and SSFC to one 32-bit word,
Stefan Tauner0c1ec452011-06-11 09:53:09 +000092 * therefore SSFC bits are off by 8. */
93 /* 0: reserved */
94#define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */
95#define SSFC_SCGO (0x1 << SSFC_SCGO_OFF)
96#define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */
97#define SSFC_ACS (0x1 << SSFC_ACS_OFF)
98#define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */
99#define SSFC_SPOP (0x1 << SSFC_SPOP_OFF)
100#define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */
101#define SSFC_COP (0x7 << SSFC_COP_OFF)
102 /* 7: reserved */
103#define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */
104#define SSFC_DBC (0x3f << SSFC_DBC_OFF)
105#define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */
106#define SSFC_DS (0x1 << SSFC_DS_OFF)
107#define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */
108#define SSFC_SME (0x1 << SSFC_SME_OFF)
109#define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */
110#define SSFC_SCF (0x7 << SSFC_SCF_OFF)
111#define SSFC_SCF_20MHZ 0x00000000
112#define SSFC_SCF_33MHZ 0x01000000
113 /* 19-23: reserved */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000114#define SSFC_RESERVED_MASK 0xf8008100
Stefan Reinauera9424d52008-06-27 16:28:34 +0000115
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000116#define ICH9_REG_PREOP 0x94 /* 16 Bits */
117#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
118#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000119
Stefan Tauner29c80832011-06-12 08:14:10 +0000120#define ICH9_REG_BBAR 0xA0 /* 32 Bits BIOS Base Address Configuration */
121#define BBAR_MASK 0x00ffff00 /* 8-23: Bottom of System Flash */
122
Stefan Tauner1e146392011-09-15 23:52:55 +0000123#define ICH8_REG_VSCC 0xC1 /* 32 Bits Vendor Specific Component Capabilities */
124#define ICH9_REG_LVSCC 0xC4 /* 32 Bits Host Lower Vendor Specific Component Capabilities */
125#define ICH9_REG_UVSCC 0xC8 /* 32 Bits Host Upper Vendor Specific Component Capabilities */
126/* The individual fields of the VSCC registers are defined in the file
127 * ich_descriptors.h. The reason is that the same layout is also used in the
128 * flash descriptor to define the properties of the different flash chips
129 * supported. The BIOS (or the ME?) is responsible to populate the ICH registers
130 * with the information from the descriptor on startup depending on the actual
131 * chip(s) detected. */
132
Stefan Taunerbd649e42011-07-01 00:39:16 +0000133#define ICH9_REG_FPB 0xD0 /* 32 Bits Flash Partition Boundary */
134#define FPB_FPBA_OFF 0 /* 0-12: Block/Sector Erase Size */
135#define FPB_FPBA (0x1FFF << FPB_FPBA_OFF)
136
Dominik Geyerb46acba2008-05-16 12:55:55 +0000137// ICH9R SPI commands
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000138#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
139#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
140#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
141#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
Dominik Geyerb46acba2008-05-16 12:55:55 +0000142
Stefan Reinauera9424d52008-06-27 16:28:34 +0000143// ICH7 registers
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000144#define ICH7_REG_SPIS 0x00 /* 16 Bits */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000145#define SPIS_SCIP 0x0001
146#define SPIS_GRANT 0x0002
147#define SPIS_CDS 0x0004
148#define SPIS_FCERR 0x0008
149#define SPIS_RESERVED_MASK 0x7ff0
Stefan Reinauera9424d52008-06-27 16:28:34 +0000150
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000151/* VIA SPI is compatible with ICH7, but maxdata
152 to transfer is 16 bytes.
153
154 DATA byte count on ICH7 is 8:13, on VIA 8:11
155
156 bit 12 is port select CS0 CS1
157 bit 13 is FAST READ enable
158 bit 7 is used with fast read and one shot controls CS de-assert?
159*/
160
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000161#define ICH7_REG_SPIC 0x02 /* 16 Bits */
162#define SPIC_SCGO 0x0002
163#define SPIC_ACS 0x0004
164#define SPIC_SPOP 0x0008
165#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000166
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000167#define ICH7_REG_SPIA 0x04 /* 32 Bits */
168#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
169#define ICH7_REG_PREOP 0x54 /* 16 Bits */
170#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
171#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000172
FENG yu ningc05a2952008-12-08 18:16:58 +0000173/* ICH SPI configuration lock-down. May be set during chipset enabling. */
Michael Karchera4448d92010-07-22 18:04:15 +0000174static int ichspi_lock = 0;
FENG yu ningc05a2952008-12-08 18:16:58 +0000175
Stefan Taunera8d838d2011-11-06 23:51:09 +0000176static enum ich_chipset ich_generation = CHIPSET_ICH_UNKNOWN;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000177uint32_t ichspi_bbar = 0;
178
Michael Karchera4448d92010-07-22 18:04:15 +0000179static void *ich_spibar = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000180
Dominik Geyerb46acba2008-05-16 12:55:55 +0000181typedef struct _OPCODE {
182 uint8_t opcode; //This commands spi opcode
183 uint8_t spi_type; //This commands spi type
184 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
185} OPCODE;
186
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000187/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000188 * Preop 1: Write Enable
189 * Preop 2: Write Status register enable
190 *
191 * OP 0: Write address
192 * OP 1: Read Address
193 * OP 2: ERASE block
194 * OP 3: Read Status register
195 * OP 4: Read ID
196 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000197 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000198 * OP 7: Chip erase
199 */
200typedef struct _OPCODES {
201 uint8_t preop[2];
202 OPCODE opcode[8];
203} OPCODES;
204
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000205static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000206
207/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000208static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000209{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000210 return mmio_readl(ich_spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000211}
212
Uwe Hermann09e04f72009-05-16 22:36:00 +0000213static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000214{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000215 return mmio_readw(ich_spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000216}
217
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000218static uint16_t REGREAD8(int X)
219{
220 return mmio_readb(ich_spibar + X);
221}
222
Stefan Taunerccd92a12011-07-01 00:39:01 +0000223#define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
224#define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
225#define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off))
Dominik Geyerb46acba2008-05-16 12:55:55 +0000226
Dominik Geyerb46acba2008-05-16 12:55:55 +0000227/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000228static int find_opcode(OPCODES *op, uint8_t opcode);
229static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000230static int generate_opcodes(OPCODES * op);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000231static int program_opcodes(OPCODES *op, int enable_undo);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000232static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000233 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000234
FENG yu ningf041e9b2008-12-15 02:32:11 +0000235/* for pairing opcodes with their required preop */
236struct preop_opcode_pair {
237 uint8_t preop;
238 uint8_t opcode;
239};
240
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000241/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000242const struct preop_opcode_pair pops[] = {
FENG yu ningf041e9b2008-12-15 02:32:11 +0000243 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
244 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
245 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
246 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
247 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
248 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000249 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
250 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000251 {JEDEC_EWSR, JEDEC_WRSR},
252 {0,}
253};
254
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000255/* Reasonable default configuration. Needs ad-hoc modifications if we
256 * encounter unlisted opcodes. Fun.
257 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000258static OPCODES O_ST_M25P = {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000259 {
260 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000261 JEDEC_EWSR,
262 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000263 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000264 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000265 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000266 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000267 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000268 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000269 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000270 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000271 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
272 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000273};
274
Helge Wagner738e2522010-10-05 22:06:05 +0000275/* List of opcodes with their corresponding spi_type
276 * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
277 * is needed which is currently not in the chipset OPCODE table
278 */
279static OPCODE POSSIBLE_OPCODES[] = {
280 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
281 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
282 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
283 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
284 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
285 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
286 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
287 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
288 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
289 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
290 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
291};
292
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000293static OPCODES O_EXISTING = {};
FENG yu ningc05a2952008-12-08 18:16:58 +0000294
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000295/* pretty printing functions */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000296static void prettyprint_opcodes(OPCODES *ops)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000297{
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000298 OPCODE oc;
299 const char *t;
300 const char *a;
301 uint8_t i;
302 static const char *const spi_type[4] = {
303 "read w/o addr",
304 "write w/o addr",
305 "read w/ addr",
306 "write w/ addr"
307 };
308 static const char *const atomic_type[3] = {
309 "none",
310 " 0 ",
311 " 1 "
312 };
313
314 if (ops == NULL)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000315 return;
316
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000317 msg_pdbg2(" OP Type Pre-OP\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000318 for (i = 0; i < 8; i++) {
319 oc = ops->opcode[i];
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000320 t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type];
321 a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic];
322 msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000323 }
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000324 msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0],
325 ops->preop[1]);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000326}
327
328#define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF)
329
Stefan Tauner55206942011-06-11 09:53:22 +0000330static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
331{
332 msg_pdbg("HSFS: ");
333 pprint_reg(HSFS, FDONE, reg_val, ", ");
334 pprint_reg(HSFS, FCERR, reg_val, ", ");
335 pprint_reg(HSFS, AEL, reg_val, ", ");
336 pprint_reg(HSFS, BERASE, reg_val, ", ");
337 pprint_reg(HSFS, SCIP, reg_val, ", ");
338 pprint_reg(HSFS, FDOPSS, reg_val, ", ");
339 pprint_reg(HSFS, FDV, reg_val, ", ");
340 pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
341}
342
343static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
344{
345 msg_pdbg("HSFC: ");
346 pprint_reg(HSFC, FGO, reg_val, ", ");
347 pprint_reg(HSFC, FCYCLE, reg_val, ", ");
348 pprint_reg(HSFC, FDBC, reg_val, ", ");
349 pprint_reg(HSFC, SME, reg_val, "\n");
350}
351
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000352static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
353{
354 msg_pdbg("SSFS: ");
355 pprint_reg(SSFS, SCIP, reg_val, ", ");
356 pprint_reg(SSFS, FDONE, reg_val, ", ");
357 pprint_reg(SSFS, FCERR, reg_val, ", ");
358 pprint_reg(SSFS, AEL, reg_val, "\n");
359}
360
361static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
362{
363 msg_pdbg("SSFC: ");
364 pprint_reg(SSFC, SCGO, reg_val, ", ");
365 pprint_reg(SSFC, ACS, reg_val, ", ");
366 pprint_reg(SSFC, SPOP, reg_val, ", ");
367 pprint_reg(SSFC, COP, reg_val, ", ");
368 pprint_reg(SSFC, DBC, reg_val, ", ");
369 pprint_reg(SSFC, SME, reg_val, ", ");
370 pprint_reg(SSFC, SCF, reg_val, "\n");
371}
372
Helge Wagner738e2522010-10-05 22:06:05 +0000373static uint8_t lookup_spi_type(uint8_t opcode)
374{
375 int a;
376
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000377 for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
Helge Wagner738e2522010-10-05 22:06:05 +0000378 if (POSSIBLE_OPCODES[a].opcode == opcode)
379 return POSSIBLE_OPCODES[a].spi_type;
380 }
381
382 return 0xFF;
383}
384
385static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
386{
387 uint8_t spi_type;
388
389 spi_type = lookup_spi_type(opcode);
390 if (spi_type > 3) {
391 /* Try to guess spi type from read/write sizes.
392 * The following valid writecnt/readcnt combinations exist:
393 * writecnt = 4, readcnt >= 0
394 * writecnt = 1, readcnt >= 0
395 * writecnt >= 4, readcnt = 0
396 * writecnt >= 1, readcnt = 0
397 * writecnt >= 1 is guaranteed for all commands.
398 */
399 if (readcnt == 0)
400 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
401 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
402 * bytes are actual the address, they go to the bus anyhow
403 */
404 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
405 else if (writecnt == 1) // and readcnt is > 0
406 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
407 else if (writecnt == 4) // and readcnt is > 0
408 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
Stefan Taunerdc704ed2012-05-06 15:11:26 +0000409 else // we have an invalid case
410 return SPI_INVALID_LENGTH;
Helge Wagner738e2522010-10-05 22:06:05 +0000411 }
Stefan Taunerdc704ed2012-05-06 15:11:26 +0000412 int oppos = 2; // use original JEDEC_BE_D8 offset
413 curopcodes->opcode[oppos].opcode = opcode;
414 curopcodes->opcode[oppos].spi_type = spi_type;
415 program_opcodes(curopcodes, 0);
416 oppos = find_opcode(curopcodes, opcode);
Stefan Taunerd94d25d2012-07-28 03:17:15 +0000417 msg_pdbg2("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
Stefan Taunerdc704ed2012-05-06 15:11:26 +0000418 return oppos;
Helge Wagner738e2522010-10-05 22:06:05 +0000419}
420
Uwe Hermann09e04f72009-05-16 22:36:00 +0000421static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000422{
423 int a;
424
Stefan Tauner50e7c602011-11-08 10:55:54 +0000425 if (op == NULL) {
426 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
427 return -1;
428 }
429
FENG yu ningc05a2952008-12-08 18:16:58 +0000430 for (a = 0; a < 8; a++) {
431 if (op->opcode[a].opcode == opcode)
432 return a;
433 }
434
435 return -1;
436}
437
Uwe Hermann09e04f72009-05-16 22:36:00 +0000438static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000439{
440 int a;
441
Stefan Tauner50e7c602011-11-08 10:55:54 +0000442 if (op == NULL) {
443 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
444 return -1;
445 }
446
FENG yu ningc05a2952008-12-08 18:16:58 +0000447 for (a = 0; a < 2; a++) {
448 if (op->preop[a] == preop)
449 return a;
450 }
451
452 return -1;
453}
454
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000455/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000456static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000457{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000458 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000459 uint16_t preop, optype;
460 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000461
462 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000463 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000464 return -1;
465 }
466
Stefan Taunera8d838d2011-11-06 23:51:09 +0000467 switch (ich_generation) {
468 case CHIPSET_ICH7:
Stefan Tauner92d6a862013-10-25 00:33:37 +0000469 case CHIPSET_TUNNEL_CREEK:
470 case CHIPSET_CENTERTON:
FENG yu ningc05a2952008-12-08 18:16:58 +0000471 preop = REGREAD16(ICH7_REG_PREOP);
472 optype = REGREAD16(ICH7_REG_OPTYPE);
473 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
474 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
475 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000476 case CHIPSET_ICH8:
477 default: /* Future version might behave the same */
FENG yu ningc05a2952008-12-08 18:16:58 +0000478 preop = REGREAD16(ICH9_REG_PREOP);
479 optype = REGREAD16(ICH9_REG_OPTYPE);
480 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
481 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
482 break;
FENG yu ningc05a2952008-12-08 18:16:58 +0000483 }
484
485 op->preop[0] = (uint8_t) preop;
486 op->preop[1] = (uint8_t) (preop >> 8);
487
488 for (a = 0; a < 8; a++) {
489 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
490 optype >>= 2;
491 }
492
493 for (a = 0; a < 4; a++) {
494 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
495 opmenu[0] >>= 8;
496 }
497
498 for (a = 4; a < 8; a++) {
499 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
500 opmenu[1] >>= 8;
501 }
502
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000503 /* No preopcodes used by default. */
504 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000505 op->opcode[a].atomic = 0;
506
FENG yu ningc05a2952008-12-08 18:16:58 +0000507 return 0;
508}
509
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000510static int program_opcodes(OPCODES *op, int enable_undo)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000511{
512 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000513 uint16_t preop, optype;
514 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000515
516 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000517 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000518 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000519 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000520 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000521
Stefan Reinauera9424d52008-06-27 16:28:34 +0000522 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000523 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000524 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000525 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000526 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000527
Stefan Reinauera9424d52008-06-27 16:28:34 +0000528 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000529 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000530 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000531 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000532 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000533
Stefan Tauner92d6a862013-10-25 00:33:37 +0000534 /* Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000535 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000536 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000537 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000538 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000539
Stefan Taunerd94d25d2012-07-28 03:17:15 +0000540 msg_pdbg2("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
Stefan Taunera8d838d2011-11-06 23:51:09 +0000541 switch (ich_generation) {
542 case CHIPSET_ICH7:
Stefan Tauner92d6a862013-10-25 00:33:37 +0000543 case CHIPSET_TUNNEL_CREEK:
544 case CHIPSET_CENTERTON:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000545 /* Register undo only for enable_undo=1, i.e. first call. */
546 if (enable_undo) {
547 rmmio_valw(ich_spibar + ICH7_REG_PREOP);
548 rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
549 rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
550 rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
551 }
552 mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
553 mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
554 mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
555 mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000556 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000557 case CHIPSET_ICH8:
558 default: /* Future version might behave the same */
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000559 /* Register undo only for enable_undo=1, i.e. first call. */
560 if (enable_undo) {
561 rmmio_valw(ich_spibar + ICH9_REG_PREOP);
562 rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
563 rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
564 rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
565 }
566 mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
567 mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
568 mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
569 mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000570 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000571 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000572
573 return 0;
574}
575
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000576/*
Stefan Tauner50e7c602011-11-08 10:55:54 +0000577 * Returns -1 if at least one mandatory opcode is inaccessible, 0 otherwise.
578 * FIXME: this should also check for
579 * - at least one probing opcode (RDID (incl. AT25F variants?), REMS, RES?)
580 * - at least one erasing opcode (lots.)
581 * - at least one program opcode (BYTE_PROGRAM, AAI_WORD_PROGRAM, ...?)
582 * - necessary preops? (EWSR, WREN, ...?)
583 */
584static int ich_missing_opcodes()
585{
586 uint8_t ops[] = {
587 JEDEC_READ,
588 JEDEC_RDSR,
589 0
590 };
591 int i = 0;
592 while (ops[i] != 0) {
593 msg_pspew("checking for opcode 0x%02x\n", ops[i]);
594 if (find_opcode(curopcodes, ops[i]) == -1)
595 return -1;
596 i++;
597 }
598 return 0;
599}
600
601/*
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000602 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
603 * it didn't stick.
604 */
Stefan Taunera8d838d2011-11-06 23:51:09 +0000605static void ich_set_bbar(uint32_t min_addr)
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000606{
Stefan Taunere27b2d42011-07-01 00:39:09 +0000607 int bbar_off;
Stefan Tauner7783f312011-09-17 21:21:42 +0000608 switch (ich_generation) {
Stefan Taunera8d838d2011-11-06 23:51:09 +0000609 case CHIPSET_ICH7:
Stefan Tauner92d6a862013-10-25 00:33:37 +0000610 case CHIPSET_TUNNEL_CREEK:
611 case CHIPSET_CENTERTON:
Stefan Taunere27b2d42011-07-01 00:39:09 +0000612 bbar_off = 0x50;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000613 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000614 case CHIPSET_ICH8:
Duncan Laurie4095ed72014-08-20 15:39:32 +0000615 case CHIPSET_BAYTRAIL:
616 msg_pdbg("BBAR offset is unknown!\n");
Stefan Tauner7783f312011-09-17 21:21:42 +0000617 return;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000618 case CHIPSET_ICH9:
Stefan Tauner7783f312011-09-17 21:21:42 +0000619 default: /* Future version might behave the same */
Stefan Taunere27b2d42011-07-01 00:39:09 +0000620 bbar_off = ICH9_REG_BBAR;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000621 break;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000622 }
Stefan Taunere27b2d42011-07-01 00:39:09 +0000623
624 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
625 if (ichspi_bbar) {
626 msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
627 ichspi_bbar);
628 }
629 min_addr &= BBAR_MASK;
630 ichspi_bbar |= min_addr;
631 rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
632 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
633
634 /* We don't have any option except complaining. And if the write
635 * failed, the restore will fail as well, so no problem there.
636 */
637 if (ichspi_bbar != min_addr)
Stefan Tauner7783f312011-09-17 21:21:42 +0000638 msg_perr("Setting BBAR to 0x%08x failed! New value: 0x%08x.\n",
639 min_addr, ichspi_bbar);
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000640}
641
Stefan Tauner8b391b82011-08-09 01:49:34 +0000642/* Read len bytes from the fdata/spid register into the data array.
643 *
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000644 * Note that using len > flash->mst->spi.max_data_read will return garbage or
Stefan Tauner8b391b82011-08-09 01:49:34 +0000645 * may even crash.
646 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000647static void ich_read_data(uint8_t *data, int len, int reg0_off)
Stefan Tauner8b391b82011-08-09 01:49:34 +0000648 {
649 int i;
650 uint32_t temp32 = 0;
651
652 for (i = 0; i < len; i++) {
653 if ((i % 4) == 0)
654 temp32 = REGREAD32(reg0_off + i);
655
656 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
657 }
658}
659
660/* Fill len bytes from the data array into the fdata/spid registers.
661 *
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000662 * Note that using len > flash->mst->spi.max_data_write will trash the registers
Stefan Tauner8b391b82011-08-09 01:49:34 +0000663 * following the data registers.
664 */
665static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
666{
667 uint32_t temp32 = 0;
668 int i;
669
670 if (len <= 0)
671 return;
672
673 for (i = 0; i < len; i++) {
674 if ((i % 4) == 0)
675 temp32 = 0;
676
677 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
678
679 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
680 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
681 }
682 i--;
683 if ((i % 4) != 3) /* Write remaining data to regs. */
684 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
685}
686
FENG yu ningf041e9b2008-12-15 02:32:11 +0000687/* This function generates OPCODES from or programs OPCODES to ICH according to
688 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000689 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000690 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000691 */
Michael Karchera4448d92010-07-22 18:04:15 +0000692static int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000693{
694 int rc = 0;
695 OPCODES *curopcodes_done;
696
697 if (curopcodes)
698 return 0;
699
700 if (ichspi_lock) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000701 msg_pdbg("Reading OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000702 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000703 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000704 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000705 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000706 curopcodes_done = &O_ST_M25P;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000707 rc = program_opcodes(curopcodes_done, 1);
FENG yu ningc05a2952008-12-08 18:16:58 +0000708 }
709
710 if (rc) {
711 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000712 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000713 return 1;
714 } else {
715 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000716 msg_pdbg("done\n");
Stefan Tauner8b391b82011-08-09 01:49:34 +0000717 prettyprint_opcodes(curopcodes);
FENG yu ningc05a2952008-12-08 18:16:58 +0000718 return 0;
719 }
720}
721
Stefan Reinauer43119562008-11-02 19:51:50 +0000722static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000723 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000724{
725 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000726 int timeout;
Stefan Tauner8b391b82011-08-09 01:49:34 +0000727 uint32_t temp32;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000728 uint16_t temp16;
Stefan Reinauer43119562008-11-02 19:51:50 +0000729 uint64_t opmenu;
730 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000731
732 /* Is it a write command? */
733 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
734 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
735 write_cmd = 1;
736 }
737
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000738 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
739 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
740 programmer_delay(10);
741 }
742 if (!timeout) {
743 msg_perr("Error: SCIP never cleared!\n");
744 return 1;
745 }
746
Stefan Tauner10b3e222011-07-01 00:39:23 +0000747 /* Program offset in flash into SPIA while preserving reserved bits. */
748 temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
749 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000750
Stefan Tauner10b3e222011-07-01 00:39:23 +0000751 /* Program data into SPID0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000752 if (write_cmd && (datalength != 0))
753 ich_fill_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000754
755 /* Assemble SPIS */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000756 temp16 = REGREAD16(ICH7_REG_SPIS);
757 /* keep reserved bits */
758 temp16 &= SPIS_RESERVED_MASK;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000759 /* clear error status registers */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000760 temp16 |= (SPIS_CDS | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000761 REGWRITE16(ICH7_REG_SPIS, temp16);
762
763 /* Assemble SPIC */
764 temp16 = 0;
765
766 if (datalength != 0) {
767 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000768 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000769 }
770
771 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000772 opmenu = REGREAD32(ICH7_REG_OPMENU);
773 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
774
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000775 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
776 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000777 break;
778 }
779 opmenu >>= 8;
780 }
781 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000782 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000783 return 1;
784 }
785 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000786
Michael Karcher136125a2011-04-29 22:11:36 +0000787 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
788 /* Handle Atomic. Atomic commands include three steps:
789 - sending the preop (mainly EWSR or WREN)
790 - sending the main command
791 - waiting for the busy bit (WIP) to be cleared
792 This means the timeout must be sufficient for chip erase
793 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000794 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000795 switch (op.atomic) {
796 case 2:
797 /* Select second preop. */
798 temp16 |= SPIC_SPOP;
799 /* And fall through. */
800 case 1:
801 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000802 temp16 |= SPIC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000803 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000804 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000805 }
806
807 /* Start */
808 temp16 |= SPIC_SCGO;
809
810 /* write it */
811 REGWRITE16(ICH7_REG_SPIC, temp16);
812
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000813 /* Wait for Cycle Done Status or Flash Cycle Error. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000814 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
815 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000816 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000817 }
818 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000819 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
820 REGREAD16(ICH7_REG_SPIS));
821 return 1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000822 }
823
Sean Nelson316a29f2010-05-07 20:09:04 +0000824 /* FIXME: make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000825 temp16 = REGREAD16(ICH7_REG_SPIS);
826 if (temp16 & SPIS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000827 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000828 /* keep reserved bits */
829 temp16 &= SPIS_RESERVED_MASK;
830 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000831 return 1;
832 }
833
Stefan Tauner8b391b82011-08-09 01:49:34 +0000834 if ((!write_cmd) && (datalength != 0))
835 ich_read_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000836
837 return 0;
838}
839
Stefan Reinauer43119562008-11-02 19:51:50 +0000840static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000841 uint8_t datalength, uint8_t * data)
842{
843 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000844 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000845 uint32_t temp32;
Stefan Reinauer43119562008-11-02 19:51:50 +0000846 uint64_t opmenu;
847 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000848
849 /* Is it a write command? */
850 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
851 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
852 write_cmd = 1;
853 }
854
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000855 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
856 while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
857 programmer_delay(10);
858 }
859 if (!timeout) {
860 msg_perr("Error: SCIP never cleared!\n");
861 return 1;
862 }
863
Stefan Tauner10b3e222011-07-01 00:39:23 +0000864 /* Program offset in flash into FADDR while preserve the reserved bits
865 * and clearing the 25. address bit which is only useable in hwseq. */
866 temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
867 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000868
869 /* Program data into FDATA0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000870 if (write_cmd && (datalength != 0))
871 ich_fill_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000872
873 /* Assemble SSFS + SSFC */
Helge Wagnera319be12010-08-11 21:06:10 +0000874 temp32 = REGREAD32(ICH9_REG_SSFS);
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000875 /* Keep reserved bits only */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000876 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000877 /* Clear cycle done and cycle error status registers */
878 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000879 REGWRITE32(ICH9_REG_SSFS, temp32);
880
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000881 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000882 temp32 |= SSFC_SCF_20MHZ;
883
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000884 /* Set data byte count (DBC) and data cycle bit (DS) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000885 if (datalength != 0) {
886 uint32_t datatemp;
887 temp32 |= SSFC_DS;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000888 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
889 SSFC_DBC);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000890 temp32 |= datatemp;
891 }
892
893 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000894 opmenu = REGREAD32(ICH9_REG_OPMENU);
895 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
896
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000897 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
898 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000899 break;
900 }
901 opmenu >>= 8;
902 }
903 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000904 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000905 return 1;
906 }
907 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000908
Michael Karcher136125a2011-04-29 22:11:36 +0000909 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
910 /* Handle Atomic. Atomic commands include three steps:
911 - sending the preop (mainly EWSR or WREN)
912 - sending the main command
913 - waiting for the busy bit (WIP) to be cleared
914 This means the timeout must be sufficient for chip erase
915 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000916 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000917 switch (op.atomic) {
918 case 2:
919 /* Select second preop. */
920 temp32 |= SSFC_SPOP;
921 /* And fall through. */
922 case 1:
923 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000924 temp32 |= SSFC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000925 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000926 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000927 }
928
929 /* Start */
930 temp32 |= SSFC_SCGO;
931
932 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000933 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000934
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000935 /* Wait for Cycle Done Status or Flash Cycle Error. */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000936 while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000937 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000938 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000939 }
940 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000941 msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
942 REGREAD32(ICH9_REG_SSFS));
943 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000944 }
945
Sean Nelson316a29f2010-05-07 20:09:04 +0000946 /* FIXME make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000947 temp32 = REGREAD32(ICH9_REG_SSFS);
948 if (temp32 & SSFS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000949 msg_perr("Transaction error!\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000950 prettyprint_ich9_reg_ssfs(temp32);
951 prettyprint_ich9_reg_ssfc(temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000952 /* keep reserved bits */
953 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
954 /* Clear the transaction error. */
955 REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000956 return 1;
957 }
958
Stefan Tauner8b391b82011-08-09 01:49:34 +0000959 if ((!write_cmd) && (datalength != 0))
960 ich_read_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000961
962 return 0;
963}
964
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000965static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000966 uint8_t datalength, uint8_t * data)
967{
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000968 /* max_data_read == max_data_write for all Intel/VIA SPI masters */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000969 uint8_t maxlength = flash->mst->spi.max_data_read;
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000970
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000971 if (ich_generation == CHIPSET_ICH_UNKNOWN) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000972 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000973 return -1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000974 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000975
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000976 if (datalength > maxlength) {
977 msg_perr("%s: Internal command size error for "
978 "opcode 0x%02x, got datalength=%i, want <=%i\n",
979 __func__, op.opcode, datalength, maxlength);
980 return SPI_INVALID_LENGTH;
981 }
982
Stefan Taunera8d838d2011-11-06 23:51:09 +0000983 switch (ich_generation) {
984 case CHIPSET_ICH7:
Stefan Tauner92d6a862013-10-25 00:33:37 +0000985 case CHIPSET_TUNNEL_CREEK:
986 case CHIPSET_CENTERTON:
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000987 return ich7_run_opcode(op, offset, datalength, data, maxlength);
Stefan Taunera8d838d2011-11-06 23:51:09 +0000988 case CHIPSET_ICH8:
989 default: /* Future version might behave the same */
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000990 return ich9_run_opcode(op, offset, datalength, data);
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000991 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000992}
993
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000994static int ich_spi_send_command(struct flashctx *flash, unsigned int writecnt,
995 unsigned int readcnt,
996 const unsigned char *writearr,
997 unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000998{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000999 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001000 int opcode_index = -1;
1001 const unsigned char cmd = *writearr;
1002 OPCODE *opcode;
1003 uint32_t addr = 0;
1004 uint8_t *data;
1005 int count;
1006
Dominik Geyerb46acba2008-05-16 12:55:55 +00001007 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001008 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +00001009 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +00001010 if (!ichspi_lock)
1011 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
Stefan Taunerdc704ed2012-05-06 15:11:26 +00001012 if (opcode_index == SPI_INVALID_LENGTH) {
1013 msg_pdbg("OPCODE 0x%02x has unsupported length, will not execute.\n", cmd);
1014 return SPI_INVALID_LENGTH;
1015 } else if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001016 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
1017 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +00001018 return SPI_INVALID_OPCODE;
1019 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001020 }
1021
1022 opcode = &(curopcodes->opcode[opcode_index]);
1023
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001024 /* The following valid writecnt/readcnt combinations exist:
1025 * writecnt = 4, readcnt >= 0
1026 * writecnt = 1, readcnt >= 0
1027 * writecnt >= 4, readcnt = 0
1028 * writecnt >= 1, readcnt = 0
1029 * writecnt >= 1 is guaranteed for all commands.
1030 */
1031 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
1032 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001033 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001034 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
1035 writecnt);
1036 return SPI_INVALID_LENGTH;
1037 }
1038 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
1039 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001040 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001041 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
1042 writecnt);
1043 return SPI_INVALID_LENGTH;
1044 }
1045 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1046 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001047 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001048 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
1049 writecnt);
1050 return SPI_INVALID_LENGTH;
1051 }
1052 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1053 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1054 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001055 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001056 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1057 readcnt);
1058 return SPI_INVALID_LENGTH;
1059 }
1060
Dominik Geyerb46acba2008-05-16 12:55:55 +00001061 /* if opcode-type requires an address */
1062 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1063 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001064 addr = (writearr[1] << 16) |
1065 (writearr[2] << 8) | (writearr[3] << 0);
Stefan Taunera8d838d2011-11-06 23:51:09 +00001066 if (addr < ichspi_bbar) {
1067 msg_perr("%s: Address 0x%06x below allowed "
1068 "range 0x%06x-0xffffff\n", __func__,
1069 addr, ichspi_bbar);
1070 return SPI_INVALID_ADDRESS;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001071 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001072 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001073
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001074 /* Translate read/write array/count.
1075 * The maximum data length is identical for the maximum read length and
1076 * for the maximum write length excluding opcode and address. Opcode and
1077 * address are stored in separate registers, not in the data registers
1078 * and are thus not counted towards data length. The only exception
1079 * applies if the opcode definition (un)intentionally classifies said
1080 * opcode incorrectly as non-address opcode or vice versa. */
Dominik Geyerb46acba2008-05-16 12:55:55 +00001081 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001082 data = (uint8_t *) (writearr + 1);
1083 count = writecnt - 1;
1084 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1085 data = (uint8_t *) (writearr + 4);
1086 count = writecnt - 4;
1087 } else {
1088 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001089 count = readcnt;
1090 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001091
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001092 result = run_opcode(flash, *opcode, addr, count, data);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001093 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001094 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1095 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1096 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1097 msg_pdbg("at address 0x%06x ", addr);
1098 }
1099 msg_pdbg("(payload length was %d).\n", count);
1100
1101 /* Print out the data array if it contains data to write.
1102 * Errors are detected before the received data is read back into
1103 * the array so it won't make sense to print it then. */
1104 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1105 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1106 int i;
1107 msg_pspew("The data was:\n");
Stefan Taunerf382e352011-11-08 11:55:24 +00001108 for (i = 0; i < count; i++){
Stefan Tauner8ed29342011-04-29 23:53:09 +00001109 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1110 }
1111 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001112 }
1113
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001114 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001115}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001116
Stefan Tauner50e7c602011-11-08 10:55:54 +00001117static struct hwseq_data {
1118 uint32_t size_comp0;
1119 uint32_t size_comp1;
1120} hwseq_data;
1121
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001122/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
1123static void ich_hwseq_set_addr(uint32_t addr)
1124{
1125 uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
1126 REGWRITE32(ICH9_REG_FADDR, (addr & 0x01FFFFFF) | addr_old);
1127}
1128
1129/* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
1130 * of the block containing this address. May return nonsense if the address is
1131 * not valid. The erase block size for a specific address depends on the flash
1132 * partition layout as specified by FPB and the partition properties as defined
1133 * by UVSCC and LVSCC respectively. An alternative to implement this method
1134 * would be by querying FPB and the respective VSCC register directly.
1135 */
1136static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr)
1137{
1138 uint8_t enc_berase;
Stefan Tauner67d163d2013-01-15 17:37:48 +00001139 static const uint32_t dec_berase[4] = {
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001140 256,
1141 4 * 1024,
1142 8 * 1024,
1143 64 * 1024
1144 };
1145
1146 ich_hwseq_set_addr(addr);
1147 enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >>
1148 HSFS_BERASE_OFF;
1149 return dec_berase[enc_berase];
1150}
1151
1152/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
1153 Resets all error flags in HSFS.
1154 Returns 0 if the cycle completes successfully without errors within
1155 timeout us, 1 on errors. */
1156static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
1157 unsigned int len)
1158{
1159 uint16_t hsfs;
1160 uint32_t addr;
1161
1162 timeout /= 8; /* scale timeout duration to counter */
1163 while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) &
1164 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
1165 --timeout) {
1166 programmer_delay(8);
1167 }
1168 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1169 if (!timeout) {
1170 addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
1171 msg_perr("Timeout error between offset 0x%08x and "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001172 "0x%08x (= 0x%08x + %d)!\n",
1173 addr, addr + len - 1, addr, len - 1);
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001174 prettyprint_ich9_reg_hsfs(hsfs);
1175 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1176 return 1;
1177 }
1178
1179 if (hsfs & HSFS_FCERR) {
1180 addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
1181 msg_perr("Transaction error between offset 0x%08x and "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001182 "0x%08x (= 0x%08x + %d)!\n",
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001183 addr, addr + len - 1, addr, len - 1);
1184 prettyprint_ich9_reg_hsfs(hsfs);
1185 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1186 return 1;
1187 }
1188 return 0;
1189}
Stefan Tauner50e7c602011-11-08 10:55:54 +00001190
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001191static int ich_hwseq_probe(struct flashctx *flash)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001192{
1193 uint32_t total_size, boundary;
1194 uint32_t erase_size_low, size_low, erase_size_high, size_high;
1195 struct block_eraser *eraser;
1196
1197 total_size = hwseq_data.size_comp0 + hwseq_data.size_comp1;
Stefan Tauner5c316f92015-02-08 21:57:52 +00001198 msg_cdbg("Hardware sequencing reports %d attached SPI flash chip",
Stefan Tauner50e7c602011-11-08 10:55:54 +00001199 (hwseq_data.size_comp1 != 0) ? 2 : 1);
1200 if (hwseq_data.size_comp1 != 0)
1201 msg_cdbg("s with a combined");
1202 else
1203 msg_cdbg(" with a");
1204 msg_cdbg(" density of %d kB.\n", total_size / 1024);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001205 flash->chip->total_size = total_size / 1024;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001206
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001207 eraser = &(flash->chip->block_erasers[0]);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001208 boundary = (REGREAD32(ICH9_REG_FPB) & FPB_FPBA) << 12;
1209 size_high = total_size - boundary;
1210 erase_size_high = ich_hwseq_get_erase_block_size(boundary);
1211
1212 if (boundary == 0) {
Stefan Tauner5c316f92015-02-08 21:57:52 +00001213 msg_cdbg2("There is only one partition containing the whole "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001214 "address space (0x%06x - 0x%06x).\n", 0, size_high-1);
1215 eraser->eraseblocks[0].size = erase_size_high;
1216 eraser->eraseblocks[0].count = size_high / erase_size_high;
Stefan Tauner5c316f92015-02-08 21:57:52 +00001217 msg_cdbg2("There are %d erase blocks with %d B each.\n",
Stefan Tauner50e7c602011-11-08 10:55:54 +00001218 size_high / erase_size_high, erase_size_high);
1219 } else {
Stefan Tauner5c316f92015-02-08 21:57:52 +00001220 msg_cdbg2("The flash address space (0x%06x - 0x%06x) is divided "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001221 "at address 0x%06x in two partitions.\n",
Stefan Taunerdbac46c2013-08-13 22:10:41 +00001222 0, total_size-1, boundary);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001223 size_low = total_size - size_high;
1224 erase_size_low = ich_hwseq_get_erase_block_size(0);
1225
1226 eraser->eraseblocks[0].size = erase_size_low;
1227 eraser->eraseblocks[0].count = size_low / erase_size_low;
1228 msg_cdbg("The first partition ranges from 0x%06x to 0x%06x.\n",
1229 0, size_low-1);
1230 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1231 size_low / erase_size_low, erase_size_low);
1232
1233 eraser->eraseblocks[1].size = erase_size_high;
1234 eraser->eraseblocks[1].count = size_high / erase_size_high;
1235 msg_cdbg("The second partition ranges from 0x%06x to 0x%06x.\n",
Stefan Taunerdbac46c2013-08-13 22:10:41 +00001236 boundary, total_size-1);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001237 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1238 size_high / erase_size_high, erase_size_high);
1239 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001240 flash->chip->tested = TEST_OK_PREW;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001241 return 1;
1242}
1243
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001244static int ich_hwseq_block_erase(struct flashctx *flash, unsigned int addr,
1245 unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001246{
1247 uint32_t erase_block;
1248 uint16_t hsfc;
1249 uint32_t timeout = 5000 * 1000; /* 5 s for max 64 kB */
1250
1251 erase_block = ich_hwseq_get_erase_block_size(addr);
1252 if (len != erase_block) {
1253 msg_cerr("Erase block size for address 0x%06x is %d B, "
1254 "but requested erase block size is %d B. "
1255 "Not erasing anything.\n", addr, erase_block, len);
1256 return -1;
1257 }
1258
1259 /* Although the hardware supports this (it would erase the whole block
1260 * containing the address) we play safe here. */
1261 if (addr % erase_block != 0) {
1262 msg_cerr("Erase address 0x%06x is not aligned to the erase "
1263 "block boundary (any multiple of %d). "
1264 "Not erasing anything.\n", addr, erase_block);
1265 return -1;
1266 }
1267
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001268 if (addr + len > flash->chip->total_size * 1024) {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001269 msg_perr("Request to erase some inaccessible memory address(es)"
1270 " (addr=0x%x, len=%d). "
1271 "Not erasing anything.\n", addr, len);
1272 return -1;
1273 }
1274
1275 msg_pdbg("Erasing %d bytes starting at 0x%06x.\n", len, addr);
Stefan Tauner7608d362014-08-05 23:28:47 +00001276 ich_hwseq_set_addr(addr);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001277
1278 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
1279 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1280
1281 hsfc = REGREAD16(ICH9_REG_HSFC);
1282 hsfc &= ~HSFC_FCYCLE; /* clear operation */
1283 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
1284 hsfc |= HSFC_FGO; /* start */
1285 msg_pdbg("HSFC used for block erasing: ");
1286 prettyprint_ich9_reg_hsfc(hsfc);
1287 REGWRITE16(ICH9_REG_HSFC, hsfc);
1288
1289 if (ich_hwseq_wait_for_cycle_complete(timeout, len))
1290 return -1;
1291 return 0;
1292}
1293
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001294static int ich_hwseq_read(struct flashctx *flash, uint8_t *buf,
1295 unsigned int addr, unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001296{
1297 uint16_t hsfc;
1298 uint16_t timeout = 100 * 60;
1299 uint8_t block_len;
1300
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001301 if (addr + len > flash->chip->total_size * 1024) {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001302 msg_perr("Request to read from an inaccessible memory address "
1303 "(addr=0x%x, len=%d).\n", addr, len);
1304 return -1;
1305 }
1306
1307 msg_pdbg("Reading %d bytes starting at 0x%06x.\n", len, addr);
1308 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1309 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1310
1311 while (len > 0) {
Stefan Tauner7608d362014-08-05 23:28:47 +00001312 /* Obey programmer limit... */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001313 block_len = min(len, flash->mst->opaque.max_data_read);
Stefan Tauner7608d362014-08-05 23:28:47 +00001314 /* as well as flash chip page borders as demanded in the Intel datasheets. */
1315 block_len = min(block_len, 256 - (addr & 0xFF));
1316
Stefan Tauner50e7c602011-11-08 10:55:54 +00001317 ich_hwseq_set_addr(addr);
1318 hsfc = REGREAD16(ICH9_REG_HSFC);
1319 hsfc &= ~HSFC_FCYCLE; /* set read operation */
1320 hsfc &= ~HSFC_FDBC; /* clear byte count */
1321 /* set byte count */
1322 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1323 hsfc |= HSFC_FGO; /* start */
1324 REGWRITE16(ICH9_REG_HSFC, hsfc);
1325
1326 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
1327 return 1;
1328 ich_read_data(buf, block_len, ICH9_REG_FDATA0);
1329 addr += block_len;
1330 buf += block_len;
1331 len -= block_len;
1332 }
1333 return 0;
1334}
1335
Mark Marshallf20b7be2014-05-09 21:16:21 +00001336static int ich_hwseq_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001337{
1338 uint16_t hsfc;
1339 uint16_t timeout = 100 * 60;
1340 uint8_t block_len;
1341
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001342 if (addr + len > flash->chip->total_size * 1024) {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001343 msg_perr("Request to write to an inaccessible memory address "
1344 "(addr=0x%x, len=%d).\n", addr, len);
1345 return -1;
1346 }
1347
1348 msg_pdbg("Writing %d bytes starting at 0x%06x.\n", len, addr);
1349 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1350 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1351
1352 while (len > 0) {
1353 ich_hwseq_set_addr(addr);
Stefan Tauner7608d362014-08-05 23:28:47 +00001354 /* Obey programmer limit... */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001355 block_len = min(len, flash->mst->opaque.max_data_write);
Stefan Tauner7608d362014-08-05 23:28:47 +00001356 /* as well as flash chip page borders as demanded in the Intel datasheets. */
1357 block_len = min(block_len, 256 - (addr & 0xFF));
Stefan Tauner50e7c602011-11-08 10:55:54 +00001358 ich_fill_data(buf, block_len, ICH9_REG_FDATA0);
1359 hsfc = REGREAD16(ICH9_REG_HSFC);
1360 hsfc &= ~HSFC_FCYCLE; /* clear operation */
1361 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
1362 hsfc &= ~HSFC_FDBC; /* clear byte count */
1363 /* set byte count */
1364 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1365 hsfc |= HSFC_FGO; /* start */
1366 REGWRITE16(ICH9_REG_HSFC, hsfc);
1367
1368 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
1369 return -1;
1370 addr += block_len;
1371 buf += block_len;
1372 len -= block_len;
1373 }
1374 return 0;
1375}
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001376
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001377static int ich_spi_send_multicommand(struct flashctx *flash,
1378 struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001379{
1380 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001381 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001382 int oppos, preoppos;
1383 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001384 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001385 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001386 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1387 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001388 if ((oppos == -1) && (preoppos != -1)) {
1389 /* Current command is listed as preopcode in
1390 * ICH struct OPCODES, but next command is not
1391 * listed as opcode in that struct.
1392 * Check for command sanity, then
1393 * try to reprogram the ICH opcode list.
1394 */
1395 if (find_preop(curopcodes,
1396 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001397 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001398 "preopcodes 0x%02x and 0x%02x, "
1399 "ignoring the first.\n",
1400 __func__, cmds->writearr[0],
1401 (cmds + 1)->writearr[0]);
1402 continue;
1403 }
1404 /* If the chipset is locked down, we'll fail
1405 * during execution of the next command anyway.
1406 * No need to bother with fixups.
1407 */
1408 if (!ichspi_lock) {
Helge Wagner738e2522010-10-05 22:06:05 +00001409 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1410 if (oppos == -1)
1411 continue;
1412 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001413 continue;
1414 }
1415 }
1416 if ((oppos != -1) && (preoppos != -1)) {
1417 /* Current command is listed as preopcode in
1418 * ICH struct OPCODES and next command is listed
1419 * as opcode in that struct. Match them up.
1420 */
1421 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001422 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001423 }
1424 /* If none of the above if-statements about oppos or
1425 * preoppos matched, this is a normal opcode.
1426 */
1427 }
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001428 ret = ich_spi_send_command(flash, cmds->writecnt, cmds->readcnt,
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001429 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001430 /* Reset the type of all opcodes to non-atomic. */
1431 for (i = 0; i < 8; i++)
1432 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001433 }
1434 return ret;
1435}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001436
Michael Karchera4448d92010-07-22 18:04:15 +00001437#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1438#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1439#define ICH_BRWA(x) ((x >> 8) & 0xff)
1440#define ICH_BRRA(x) ((x >> 0) & 0xff)
1441
Stefan Tauner5210e722012-02-16 01:13:00 +00001442/* returns 0 if region is unused or r/w */
1443static int ich9_handle_frap(uint32_t frap, int i)
Michael Karchera4448d92010-07-22 18:04:15 +00001444{
Mathias Krausea60faab2011-01-17 07:50:42 +00001445 static const char *const access_names[4] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001446 "locked", "read-only", "write-only", "read-write"
1447 };
Mathias Krausea60faab2011-01-17 07:50:42 +00001448 static const char *const region_names[5] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001449 "Flash Descriptor", "BIOS", "Management Engine",
1450 "Gigabit Ethernet", "Platform Data"
1451 };
1452 uint32_t base, limit;
1453 int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1454 (((ICH_BRRA(frap) >> i) & 1) << 0);
Stefan Tauner29c80832011-06-12 08:14:10 +00001455 int offset = ICH9_REG_FREG0 + i * 4;
Michael Karchera4448d92010-07-22 18:04:15 +00001456 uint32_t freg = mmio_readl(ich_spibar + offset);
1457
Michael Karchera4448d92010-07-22 18:04:15 +00001458 base = ICH_FREG_BASE(freg);
1459 limit = ICH_FREG_LIMIT(freg);
Stefan Taunere3adea02012-08-27 15:12:36 +00001460 if (base > limit || (freg == 0 && i > 0)) {
Michael Karchera4448d92010-07-22 18:04:15 +00001461 /* this FREG is disabled */
Stefan Tauner5210e722012-02-16 01:13:00 +00001462 msg_pdbg2("0x%02X: 0x%08x FREG%i: %s region is unused.\n",
1463 offset, freg, i, region_names[i]);
1464 return 0;
1465 }
1466 msg_pdbg("0x%02X: 0x%08x ", offset, freg);
1467 if (rwperms == 0x3) {
1468 msg_pdbg("FREG%i: %s region (0x%08x-0x%08x) is %s.\n", i,
1469 region_names[i], base, (limit | 0x0fff),
1470 access_names[rwperms]);
1471 return 0;
Michael Karchera4448d92010-07-22 18:04:15 +00001472 }
1473
Stefan Taunerc6fa32d2013-01-04 22:54:07 +00001474 msg_pwarn("FREG%i: Warning: %s region (0x%08x-0x%08x) is %s.\n", i,
Stefan Tauner5210e722012-02-16 01:13:00 +00001475 region_names[i], base, (limit | 0x0fff),
1476 access_names[rwperms]);
1477 return 1;
Michael Karchera4448d92010-07-22 18:04:15 +00001478}
1479
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001480 /* In contrast to FRAP and the master section of the descriptor the bits
1481 * in the PR registers have an inverted meaning. The bits in FRAP
1482 * indicate read and write access _grant_. Here they indicate read
1483 * and write _protection_ respectively. If both bits are 0 the address
1484 * bits are ignored.
1485 */
1486#define ICH_PR_PERMS(pr) (((~((pr) >> PR_RP_OFF) & 1) << 0) | \
1487 ((~((pr) >> PR_WP_OFF) & 1) << 1))
1488
Stefan Tauner5210e722012-02-16 01:13:00 +00001489/* returns 0 if range is unused (i.e. r/w) */
1490static int ich9_handle_pr(int i)
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001491{
Stefan Tauner5210e722012-02-16 01:13:00 +00001492 static const char *const access_names[3] = {
1493 "locked", "read-only", "write-only"
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001494 };
1495 uint8_t off = ICH9_REG_PR0 + (i * 4);
1496 uint32_t pr = mmio_readl(ich_spibar + off);
Stefan Tauner5210e722012-02-16 01:13:00 +00001497 unsigned int rwperms = ICH_PR_PERMS(pr);
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001498
Stefan Tauner5210e722012-02-16 01:13:00 +00001499 if (rwperms == 0x3) {
1500 msg_pdbg2("0x%02X: 0x%08x (PR%u is unused)\n", off, pr, i);
1501 return 0;
1502 }
1503
1504 msg_pdbg("0x%02X: 0x%08x ", off, pr);
Stefan Taunerc6fa32d2013-01-04 22:54:07 +00001505 msg_pwarn("PR%u: Warning: 0x%08x-0x%08x is %s.\n", i, ICH_FREG_BASE(pr),
Stefan Tauner5210e722012-02-16 01:13:00 +00001506 ICH_FREG_LIMIT(pr) | 0x0fff, access_names[rwperms]);
1507 return 1;
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001508}
1509
Stefan Tauner75da80c2011-09-17 22:21:55 +00001510/* Set/Clear the read and write protection enable bits of PR register @i
1511 * according to @read_prot and @write_prot. */
1512static void ich9_set_pr(int i, int read_prot, int write_prot)
1513{
1514 void *addr = ich_spibar + ICH9_REG_PR0 + (i * 4);
1515 uint32_t old = mmio_readl(addr);
1516 uint32_t new;
1517
1518 msg_gspew("PR%u is 0x%08x", i, old);
1519 new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF));
1520 if (read_prot)
1521 new |= (1 << PR_RP_OFF);
1522 if (write_prot)
1523 new |= (1 << PR_WP_OFF);
1524 if (old == new) {
1525 msg_gspew(" already.\n");
1526 return;
1527 }
1528 msg_gspew(", trying to set it to 0x%08x ", new);
1529 rmmio_writel(new, addr);
1530 msg_gspew("resulted in 0x%08x.\n", mmio_readl(addr));
1531}
1532
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001533static const struct spi_master spi_master_ich7 = {
Michael Karcherb9dbe482011-05-11 17:07:07 +00001534 .type = SPI_CONTROLLER_ICH7,
1535 .max_data_read = 64,
1536 .max_data_write = 64,
1537 .command = ich_spi_send_command,
1538 .multicommand = ich_spi_send_multicommand,
1539 .read = default_spi_read,
1540 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +00001541 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001542};
1543
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001544static const struct spi_master spi_master_ich9 = {
Michael Karcherb9dbe482011-05-11 17:07:07 +00001545 .type = SPI_CONTROLLER_ICH9,
1546 .max_data_read = 64,
1547 .max_data_write = 64,
1548 .command = ich_spi_send_command,
1549 .multicommand = ich_spi_send_multicommand,
1550 .read = default_spi_read,
1551 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +00001552 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001553};
1554
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001555static const struct opaque_master opaque_master_ich_hwseq = {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001556 .max_data_read = 64,
1557 .max_data_write = 64,
1558 .probe = ich_hwseq_probe,
1559 .read = ich_hwseq_read,
1560 .write = ich_hwseq_write,
1561 .erase = ich_hwseq_block_erase,
1562};
1563
Nico Huber560111e2017-04-26 12:27:17 +02001564int ich_init_spi(void *spibar, enum ich_chipset ich_gen)
Michael Karchera4448d92010-07-22 18:04:15 +00001565{
1566 int i;
Stefan Tauner92d6a862013-10-25 00:33:37 +00001567 uint16_t tmp2;
Michael Karchera4448d92010-07-22 18:04:15 +00001568 uint32_t tmp;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001569 char *arg;
Stefan Tauner5210e722012-02-16 01:13:00 +00001570 int ich_spi_force = 0;
1571 int ich_spi_rw_restricted = 0;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001572 int desc_valid = 0;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001573 struct ich_descriptors desc = {{ 0 }};
1574 enum ich_spi_mode {
1575 ich_auto,
1576 ich_hwseq,
1577 ich_swseq
1578 } ich_spi_mode = ich_auto;
Michael Karchera4448d92010-07-22 18:04:15 +00001579
Stefan Taunera8d838d2011-11-06 23:51:09 +00001580 ich_generation = ich_gen;
Stefan Tauner92d6a862013-10-25 00:33:37 +00001581 ich_spibar = spibar;
Michael Karchera4448d92010-07-22 18:04:15 +00001582
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001583 switch (ich_generation) {
Stefan Taunera8d838d2011-11-06 23:51:09 +00001584 case CHIPSET_ICH7:
Stefan Tauner92d6a862013-10-25 00:33:37 +00001585 case CHIPSET_TUNNEL_CREEK:
1586 case CHIPSET_CENTERTON:
Michael Karchera4448d92010-07-22 18:04:15 +00001587 msg_pdbg("0x00: 0x%04x (SPIS)\n",
1588 mmio_readw(ich_spibar + 0));
1589 msg_pdbg("0x02: 0x%04x (SPIC)\n",
1590 mmio_readw(ich_spibar + 2));
1591 msg_pdbg("0x04: 0x%08x (SPIA)\n",
1592 mmio_readl(ich_spibar + 4));
Michael Karchera4448d92010-07-22 18:04:15 +00001593 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1594 msg_pdbg("0x50: 0x%08x (BBAR)\n",
1595 ichspi_bbar);
1596 msg_pdbg("0x54: 0x%04x (PREOP)\n",
1597 mmio_readw(ich_spibar + 0x54));
1598 msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
1599 mmio_readw(ich_spibar + 0x56));
1600 msg_pdbg("0x58: 0x%08x (OPMENU)\n",
1601 mmio_readl(ich_spibar + 0x58));
1602 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
1603 mmio_readl(ich_spibar + 0x5c));
Stefan Tauner122dd122011-07-24 15:34:56 +00001604 for (i = 0; i < 3; i++) {
Michael Karchera4448d92010-07-22 18:04:15 +00001605 int offs;
1606 offs = 0x60 + (i * 4);
1607 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1608 mmio_readl(ich_spibar + offs), i);
1609 }
Michael Karchera4448d92010-07-22 18:04:15 +00001610 if (mmio_readw(ich_spibar) & (1 << 15)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +00001611 msg_pwarn("WARNING: SPI Configuration Lockdown activated.\n");
Michael Karchera4448d92010-07-22 18:04:15 +00001612 ichspi_lock = 1;
1613 }
Stefan Tauner745f6bb2011-11-13 15:17:10 +00001614 ich_init_opcodes();
Stefan Taunera8d838d2011-11-06 23:51:09 +00001615 ich_set_bbar(0);
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001616 register_spi_master(&spi_master_ich7);
Michael Karchera4448d92010-07-22 18:04:15 +00001617 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +00001618 case CHIPSET_ICH8:
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001619 default: /* Future version might behave the same */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001620 arg = extract_programmer_param("ich_spi_mode");
1621 if (arg && !strcmp(arg, "hwseq")) {
1622 ich_spi_mode = ich_hwseq;
1623 msg_pspew("user selected hwseq\n");
1624 } else if (arg && !strcmp(arg, "swseq")) {
1625 ich_spi_mode = ich_swseq;
1626 msg_pspew("user selected swseq\n");
1627 } else if (arg && !strcmp(arg, "auto")) {
1628 msg_pspew("user selected auto\n");
1629 ich_spi_mode = ich_auto;
1630 } else if (arg && !strlen(arg)) {
1631 msg_perr("Missing argument for ich_spi_mode.\n");
1632 free(arg);
1633 return ERROR_FATAL;
1634 } else if (arg) {
1635 msg_perr("Unknown argument for ich_spi_mode: %s\n",
1636 arg);
1637 free(arg);
1638 return ERROR_FATAL;
1639 }
1640 free(arg);
1641
Stefan Tauner5210e722012-02-16 01:13:00 +00001642 arg = extract_programmer_param("ich_spi_force");
1643 if (arg && !strcmp(arg, "yes")) {
1644 ich_spi_force = 1;
1645 msg_pspew("ich_spi_force enabled.\n");
1646 } else if (arg && !strlen(arg)) {
1647 msg_perr("Missing argument for ich_spi_force.\n");
1648 free(arg);
1649 return ERROR_FATAL;
1650 } else if (arg) {
1651 msg_perr("Unknown argument for ich_spi_force: \"%s\" "
1652 "(not \"yes\").\n", arg);
1653 free(arg);
1654 return ERROR_FATAL;
1655 }
1656 free(arg);
1657
Stefan Tauner29c80832011-06-12 08:14:10 +00001658 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
Michael Karchera4448d92010-07-22 18:04:15 +00001659 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
Stefan Tauner55206942011-06-11 09:53:22 +00001660 prettyprint_ich9_reg_hsfs(tmp2);
Stefan Tauner29c80832011-06-12 08:14:10 +00001661 if (tmp2 & HSFS_FLOCKDN) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +00001662 msg_pwarn("Warning: SPI Configuration Lockdown activated.\n");
Stefan Tauner55206942011-06-11 09:53:22 +00001663 ichspi_lock = 1;
1664 }
Stefan Tauner1e146392011-09-15 23:52:55 +00001665 if (tmp2 & HSFS_FDV)
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001666 desc_valid = 1;
1667 if (!(tmp2 & HSFS_FDOPSS) && desc_valid)
Stefan Taunerd7d423b2012-10-20 09:13:16 +00001668 msg_pinfo("The Flash Descriptor Override Strap-Pin is set. Restrictions implied by\n"
1669 "the Master Section of the flash descriptor are NOT in effect. Please note\n"
1670 "that Protected Range (PR) restrictions still apply.\n");
Stefan Tauner745f6bb2011-11-13 15:17:10 +00001671 ich_init_opcodes();
Stefan Tauner55206942011-06-11 09:53:22 +00001672
Stefan Taunerf382e352011-11-08 11:55:24 +00001673 if (desc_valid) {
1674 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
1675 msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
1676 prettyprint_ich9_reg_hsfc(tmp2);
1677 }
Michael Karchera4448d92010-07-22 18:04:15 +00001678
Stefan Tauner5ffe65b2011-07-07 04:10:57 +00001679 tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
Stefan Taunereb582572012-09-21 12:52:50 +00001680 msg_pdbg2("0x08: 0x%08x (FADDR)\n", tmp);
Michael Karchera4448d92010-07-22 18:04:15 +00001681
Stefan Taunerf382e352011-11-08 11:55:24 +00001682 if (desc_valid) {
1683 tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
1684 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1685 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1686 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1687 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1688 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1689
Stefan Tauner5210e722012-02-16 01:13:00 +00001690 /* Handle FREGx and FRAP registers */
Stefan Taunerf382e352011-11-08 11:55:24 +00001691 for (i = 0; i < 5; i++)
Stefan Tauner5210e722012-02-16 01:13:00 +00001692 ich_spi_rw_restricted |= ich9_handle_frap(tmp, i);
Stefan Tauner27cb34b2013-06-01 00:06:12 +00001693 if (ich_spi_rw_restricted)
1694 msg_pwarn("Not all flash regions are freely accessible by flashrom. This is "
Stefan Tauner4c723152016-01-14 22:47:55 +00001695 "most likely\ndue to an active ME. Please see "
1696 "https://flashrom.org/ME for details.\n");
Stefan Taunerf382e352011-11-08 11:55:24 +00001697 }
Michael Karchera4448d92010-07-22 18:04:15 +00001698
Stefan Taunereb582572012-09-21 12:52:50 +00001699 /* Handle PR registers */
Stefan Tauner5210e722012-02-16 01:13:00 +00001700 for (i = 0; i < 5; i++) {
1701 /* if not locked down try to disable PR locks first */
1702 if (!ichspi_lock)
Stefan Tauner75da80c2011-09-17 22:21:55 +00001703 ich9_set_pr(i, 0, 0);
Stefan Tauner5210e722012-02-16 01:13:00 +00001704 ich_spi_rw_restricted |= ich9_handle_pr(i);
1705 }
1706
1707 if (ich_spi_rw_restricted) {
Stefan Tauner5210e722012-02-16 01:13:00 +00001708 if (!ich_spi_force)
1709 programmer_may_write = 0;
Stefan Taunerd7d423b2012-10-20 09:13:16 +00001710 msg_pinfo("Writes have been disabled for safety reasons. You can enforce write\n"
1711 "support with the ich_spi_force programmer option, but you will most likely\n"
1712 "harm your hardware! If you force flashrom you will get no support if\n"
1713 "something breaks. On a few mainboards it is possible to enable write\n"
1714 "access by setting a jumper (see its documentation or the board itself).\n");
Stefan Tauner5210e722012-02-16 01:13:00 +00001715 if (ich_spi_force)
Stefan Taunerd7d423b2012-10-20 09:13:16 +00001716 msg_pinfo("Continuing with write support because the user forced us to!\n");
Stefan Tauner5210e722012-02-16 01:13:00 +00001717 }
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001718
Stefan Tauner29c80832011-06-12 08:14:10 +00001719 tmp = mmio_readl(ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001720 msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001721 prettyprint_ich9_reg_ssfs(tmp);
Stefan Tauner29c80832011-06-12 08:14:10 +00001722 if (tmp & SSFS_FCERR) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001723 msg_pdbg("Clearing SSFS.FCERR\n");
Stefan Tauner29c80832011-06-12 08:14:10 +00001724 mmio_writeb(SSFS_FCERR, ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001725 }
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001726 msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8);
1727 prettyprint_ich9_reg_ssfc(tmp);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001728
Michael Karchera4448d92010-07-22 18:04:15 +00001729 msg_pdbg("0x94: 0x%04x (PREOP)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001730 mmio_readw(ich_spibar + ICH9_REG_PREOP));
Michael Karchera4448d92010-07-22 18:04:15 +00001731 msg_pdbg("0x96: 0x%04x (OPTYPE)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001732 mmio_readw(ich_spibar + ICH9_REG_OPTYPE));
Michael Karchera4448d92010-07-22 18:04:15 +00001733 msg_pdbg("0x98: 0x%08x (OPMENU)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001734 mmio_readl(ich_spibar + ICH9_REG_OPMENU));
Michael Karchera4448d92010-07-22 18:04:15 +00001735 msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001736 mmio_readl(ich_spibar + ICH9_REG_OPMENU + 4));
Stefan Taunerf382e352011-11-08 11:55:24 +00001737 if (ich_generation == CHIPSET_ICH8 && desc_valid) {
Stefan Tauner1e146392011-09-15 23:52:55 +00001738 tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC);
1739 msg_pdbg("0xC1: 0x%08x (VSCC)\n", tmp);
1740 msg_pdbg("VSCC: ");
Nico Huberd152fb92017-06-19 12:57:10 +02001741 prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, true);
Stefan Tauner1e146392011-09-15 23:52:55 +00001742 } else {
Duncan Laurie4095ed72014-08-20 15:39:32 +00001743 if (ich_generation != CHIPSET_BAYTRAIL && desc_valid) {
1744 ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
1745 msg_pdbg("0xA0: 0x%08x (BBAR)\n",
1746 ichspi_bbar);
1747 ich_set_bbar(0);
1748 }
Stefan Taunerbd649e42011-07-01 00:39:16 +00001749
Stefan Taunerf382e352011-11-08 11:55:24 +00001750 if (desc_valid) {
1751 tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC);
1752 msg_pdbg("0xC4: 0x%08x (LVSCC)\n", tmp);
1753 msg_pdbg("LVSCC: ");
Nico Huberd152fb92017-06-19 12:57:10 +02001754 prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, true);
Stefan Tauner1e146392011-09-15 23:52:55 +00001755
Stefan Taunerf382e352011-11-08 11:55:24 +00001756 tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC);
1757 msg_pdbg("0xC8: 0x%08x (UVSCC)\n", tmp);
1758 msg_pdbg("UVSCC: ");
Nico Huberd152fb92017-06-19 12:57:10 +02001759 prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, false);
Stefan Tauner1e146392011-09-15 23:52:55 +00001760
Stefan Taunerf382e352011-11-08 11:55:24 +00001761 tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
1762 msg_pdbg("0xD0: 0x%08x (FPB)\n", tmp);
1763 }
Stefan Tauner1e146392011-09-15 23:52:55 +00001764 }
1765
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001766 if (desc_valid) {
Stefan Tauner2ba9f6e2014-08-20 15:39:19 +00001767 if (read_ich_descriptors_via_fdo(ich_spibar, &desc) == ICH_RET_OK)
1768 prettyprint_ich_descriptors(ich_gen, &desc);
1769
Stefan Tauner50e7c602011-11-08 10:55:54 +00001770 /* If the descriptor is valid and indicates multiple
1771 * flash devices we need to use hwseq to be able to
1772 * access the second flash device.
1773 */
1774 if (ich_spi_mode == ich_auto && desc.content.NC != 0) {
1775 msg_pinfo("Enabling hardware sequencing due to "
1776 "multiple flash chips detected.\n");
1777 ich_spi_mode = ich_hwseq;
1778 }
Stefan Tauner1e146392011-09-15 23:52:55 +00001779 }
Stefan Tauner50e7c602011-11-08 10:55:54 +00001780
1781 if (ich_spi_mode == ich_auto && ichspi_lock &&
1782 ich_missing_opcodes()) {
1783 msg_pinfo("Enabling hardware sequencing because "
1784 "some important opcode is locked.\n");
1785 ich_spi_mode = ich_hwseq;
1786 }
1787
1788 if (ich_spi_mode == ich_hwseq) {
1789 if (!desc_valid) {
1790 msg_perr("Hardware sequencing was requested "
1791 "but the flash descriptor is not "
1792 "valid. Aborting.\n");
1793 return ERROR_FATAL;
1794 }
Stefan Tauner2ba9f6e2014-08-20 15:39:19 +00001795
1796 int tmpi = getFCBA_component_density(ich_generation, &desc, 0);
1797 if (tmpi < 0) {
1798 msg_perr("Could not determine density of flash component %d.\n", 0);
1799 return ERROR_FATAL;
1800 }
1801 hwseq_data.size_comp0 = tmpi;
1802
1803 tmpi = getFCBA_component_density(ich_generation, &desc, 1);
1804 if (tmpi < 0) {
1805 msg_perr("Could not determine density of flash component %d.\n", 1);
1806 return ERROR_FATAL;
1807 }
1808 hwseq_data.size_comp1 = tmpi;
1809
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001810 register_opaque_master(&opaque_master_ich_hwseq);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001811 } else {
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001812 register_spi_master(&spi_master_ich9);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001813 }
Michael Karchera4448d92010-07-22 18:04:15 +00001814 break;
Michael Karchera4448d92010-07-22 18:04:15 +00001815 }
1816
Michael Karchera4448d92010-07-22 18:04:15 +00001817 return 0;
1818}
1819
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001820static const struct spi_master spi_master_via = {
Michael Karcherb9dbe482011-05-11 17:07:07 +00001821 .type = SPI_CONTROLLER_VIA,
1822 .max_data_read = 16,
1823 .max_data_write = 16,
1824 .command = ich_spi_send_command,
1825 .multicommand = ich_spi_send_multicommand,
1826 .read = default_spi_read,
1827 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +00001828 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001829};
1830
Nico Huber560111e2017-04-26 12:27:17 +02001831int via_init_spi(uint32_t mmio_base)
Michael Karchera4448d92010-07-22 18:04:15 +00001832{
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001833 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00001834
Stefan Tauner7fb5aa02013-08-14 15:48:44 +00001835 ich_spibar = rphysmap("VIA SPI MMIO registers", mmio_base, 0x70);
1836 if (ich_spibar == ERROR_PTR)
1837 return ERROR_FATAL;
Helge Wagnerdd73d832012-08-24 23:03:46 +00001838 /* Do we really need no write enable? Like the LPC one at D17F0 0x40 */
Michael Karchera4448d92010-07-22 18:04:15 +00001839
Michael Karchera4448d92010-07-22 18:04:15 +00001840 /* Not sure if it speaks all these bus protocols. */
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +00001841 internal_buses_supported = BUS_LPC | BUS_FWH;
Stefan Taunera8d838d2011-11-06 23:51:09 +00001842 ich_generation = CHIPSET_ICH7;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001843 register_spi_master(&spi_master_via);
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001844
1845 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1846 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1847 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1848 for (i = 0; i < 2; i++) {
1849 int offs;
1850 offs = 8 + (i * 8);
1851 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1852 mmio_readl(ich_spibar + offs), i);
1853 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1854 mmio_readl(ich_spibar + offs + 4), i);
1855 }
1856 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1857 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1858 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1859 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1860 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1861 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1862 for (i = 0; i < 3; i++) {
1863 int offs;
1864 offs = 0x60 + (i * 4);
1865 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1866 mmio_readl(ich_spibar + offs), i);
1867 }
1868 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
1869 mmio_readw(ich_spibar + 0x6c));
1870 if (mmio_readw(ich_spibar) & (1 << 15)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +00001871 msg_pwarn("Warning: SPI Configuration Lockdown activated.\n");
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001872 ichspi_lock = 1;
1873 }
1874
Stefan Taunera8d838d2011-11-06 23:51:09 +00001875 ich_set_bbar(0);
Michael Karchera4448d92010-07-22 18:04:15 +00001876 ich_init_opcodes();
1877
1878 return 0;
1879}
1880
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001881#endif