blob: ec81628e14a9f5d2267ae2b891c81de5834af0fd [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
Nico Huberd54e4f42017-03-23 23:45:47 +010036/* Sunrise Point */
37
38/* Added HSFS Status bits */
39#define HSFS_WRSDIS_OFF 11 /* 11: Flash Configuration Lock-Down */
40#define HSFS_WRSDIS (0x1 << HSFS_WRSDIS_OFF)
41#define HSFS_PRR34_LOCKDN_OFF 12 /* 12: PRR3 PRR4 Lock-Down */
42#define HSFS_PRR34_LOCKDN (0x1 << HSFS_PRR34_LOCKDN_OFF)
43/* HSFS_BERASE vanished */
44
45/*
46 * HSFC and HSFS 16-bit registers are combined into the 32-bit
47 * BIOS_HSFSTS_CTL register in the Sunrise Point datasheet,
48 * however we still treat them separately in order to reuse code.
49 */
50
51/* Changed HSFC Control bits */
52#define PCH100_HSFC_FCYCLE_OFF (17 - 16) /* 1-4: FLASH Cycle */
53#define PCH100_HSFC_FCYCLE (0xf << PCH100_HSFC_FCYCLE_OFF)
54/* New HSFC Control bit */
55#define HSFC_WET_OFF (21 - 16) /* 5: Write Enable Type */
56#define HSFC_WET (0x1 << HSFC_WET_OFF)
57
58#define PCH100_FADDR_FLA 0x07ffffff
59
60#define PCH100_REG_DLOCK 0x0c /* 32 Bits Discrete Lock Bits */
61#define DLOCK_BMWAG_LOCKDN_OFF 0
62#define DLOCK_BMWAG_LOCKDN (0x1 << DLOCK_BMWAG_LOCKDN_OFF)
63#define DLOCK_BMRAG_LOCKDN_OFF 1
64#define DLOCK_BMRAG_LOCKDN (0x1 << DLOCK_BMRAG_LOCKDN_OFF)
65#define DLOCK_SBMWAG_LOCKDN_OFF 2
66#define DLOCK_SBMWAG_LOCKDN (0x1 << DLOCK_SBMWAG_LOCKDN_OFF)
67#define DLOCK_SBMRAG_LOCKDN_OFF 3
68#define DLOCK_SBMRAG_LOCKDN (0x1 << DLOCK_SBMRAG_LOCKDN_OFF)
69#define DLOCK_PR0_LOCKDN_OFF 8
70#define DLOCK_PR0_LOCKDN (0x1 << DLOCK_PR0_LOCKDN_OFF)
71#define DLOCK_PR1_LOCKDN_OFF 9
72#define DLOCK_PR1_LOCKDN (0x1 << DLOCK_PR1_LOCKDN_OFF)
73#define DLOCK_PR2_LOCKDN_OFF 10
74#define DLOCK_PR2_LOCKDN (0x1 << DLOCK_PR2_LOCKDN_OFF)
75#define DLOCK_PR3_LOCKDN_OFF 11
76#define DLOCK_PR3_LOCKDN (0x1 << DLOCK_PR3_LOCKDN_OFF)
77#define DLOCK_PR4_LOCKDN_OFF 12
78#define DLOCK_PR4_LOCKDN (0x1 << DLOCK_PR4_LOCKDN_OFF)
79#define DLOCK_SSEQ_LOCKDN_OFF 16
80#define DLOCK_SSEQ_LOCKDN (0x1 << DLOCK_SSEQ_LOCKDN_OFF)
81
82#define PCH100_REG_FPR0 0x84 /* 32 Bits Protected Range 0 */
83#define PCH100_REG_GPR0 0x98 /* 32 Bits Global Protected Range 0 */
84
85#define PCH100_REG_SSFSC 0xA0 /* 32 Bits Status (8) + Control (24) */
86#define PCH100_REG_PREOP 0xA4 /* 16 Bits */
87#define PCH100_REG_OPTYPE 0xA6 /* 16 Bits */
88#define PCH100_REG_OPMENU 0xA8 /* 64 Bits */
89
Stefan Reinauera9424d52008-06-27 16:28:34 +000090/* ICH9 controller register definition */
Stefan Tauner55206942011-06-11 09:53:22 +000091#define ICH9_REG_HSFS 0x04 /* 16 Bits Hardware Sequencing Flash Status */
92#define HSFS_FDONE_OFF 0 /* 0: Flash Cycle Done */
93#define HSFS_FDONE (0x1 << HSFS_FDONE_OFF)
94#define HSFS_FCERR_OFF 1 /* 1: Flash Cycle Error */
95#define HSFS_FCERR (0x1 << HSFS_FCERR_OFF)
96#define HSFS_AEL_OFF 2 /* 2: Access Error Log */
97#define HSFS_AEL (0x1 << HSFS_AEL_OFF)
98#define HSFS_BERASE_OFF 3 /* 3-4: Block/Sector Erase Size */
99#define HSFS_BERASE (0x3 << HSFS_BERASE_OFF)
100#define HSFS_SCIP_OFF 5 /* 5: SPI Cycle In Progress */
101#define HSFS_SCIP (0x1 << HSFS_SCIP_OFF)
102 /* 6-12: reserved */
103#define HSFS_FDOPSS_OFF 13 /* 13: Flash Descriptor Override Pin-Strap Status */
104#define HSFS_FDOPSS (0x1 << HSFS_FDOPSS_OFF)
105#define HSFS_FDV_OFF 14 /* 14: Flash Descriptor Valid */
106#define HSFS_FDV (0x1 << HSFS_FDV_OFF)
107#define HSFS_FLOCKDN_OFF 15 /* 15: Flash Configuration Lock-Down */
108#define HSFS_FLOCKDN (0x1 << HSFS_FLOCKDN_OFF)
109
110#define ICH9_REG_HSFC 0x06 /* 16 Bits Hardware Sequencing Flash Control */
111#define HSFC_FGO_OFF 0 /* 0: Flash Cycle Go */
112#define HSFC_FGO (0x1 << HSFC_FGO_OFF)
113#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
114#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
115 /* 3-7: reserved */
116#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
117#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
118 /* 14: reserved */
119#define HSFC_SME_OFF 15 /* 15: SPI SMI# Enable */
120#define HSFC_SME (0x1 << HSFC_SME_OFF)
121
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000122#define ICH9_REG_FADDR 0x08 /* 32 Bits */
Nico Huberd54e4f42017-03-23 23:45:47 +0100123#define ICH9_FADDR_FLA 0x01ffffff
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000124#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000125
Stefan Tauner29c80832011-06-12 08:14:10 +0000126#define ICH9_REG_FRAP 0x50 /* 32 Bytes Flash Region Access Permissions */
127#define ICH9_REG_FREG0 0x54 /* 32 Bytes Flash Region 0 */
128
129#define ICH9_REG_PR0 0x74 /* 32 Bytes Protected Range 0 */
Stefan Taunerbf69aaa2011-09-17 21:21:48 +0000130#define PR_WP_OFF 31 /* 31: write protection enable */
131#define PR_RP_OFF 15 /* 15: read protection enable */
Stefan Tauner29c80832011-06-12 08:14:10 +0000132
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000133#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000134#define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */
135#define SSFS_SCIP (0x1 << SSFS_SCIP_OFF)
136#define SSFS_FDONE_OFF 2 /* Cycle Done Status */
137#define SSFS_FDONE (0x1 << SSFS_FDONE_OFF)
138#define SSFS_FCERR_OFF 3 /* Flash Cycle Error */
139#define SSFS_FCERR (0x1 << SSFS_FCERR_OFF)
140#define SSFS_AEL_OFF 4 /* Access Error Log */
141#define SSFS_AEL (0x1 << SSFS_AEL_OFF)
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000142/* The following bits are reserved in SSFS: 1,5-7. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000143#define SSFS_RESERVED_MASK 0x000000e2
Stefan Reinauera9424d52008-06-27 16:28:34 +0000144
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000145#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000146/* We combine SSFS and SSFC to one 32-bit word,
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000147 * therefore SSFC bits are off by 8. */
148 /* 0: reserved */
149#define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */
150#define SSFC_SCGO (0x1 << SSFC_SCGO_OFF)
151#define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */
152#define SSFC_ACS (0x1 << SSFC_ACS_OFF)
153#define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */
154#define SSFC_SPOP (0x1 << SSFC_SPOP_OFF)
155#define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */
156#define SSFC_COP (0x7 << SSFC_COP_OFF)
157 /* 7: reserved */
158#define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */
159#define SSFC_DBC (0x3f << SSFC_DBC_OFF)
160#define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */
161#define SSFC_DS (0x1 << SSFC_DS_OFF)
162#define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */
163#define SSFC_SME (0x1 << SSFC_SME_OFF)
164#define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */
165#define SSFC_SCF (0x7 << SSFC_SCF_OFF)
166#define SSFC_SCF_20MHZ 0x00000000
167#define SSFC_SCF_33MHZ 0x01000000
168 /* 19-23: reserved */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000169#define SSFC_RESERVED_MASK 0xf8008100
Stefan Reinauera9424d52008-06-27 16:28:34 +0000170
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000171#define ICH9_REG_PREOP 0x94 /* 16 Bits */
172#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
173#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000174
Stefan Tauner29c80832011-06-12 08:14:10 +0000175#define ICH9_REG_BBAR 0xA0 /* 32 Bits BIOS Base Address Configuration */
176#define BBAR_MASK 0x00ffff00 /* 8-23: Bottom of System Flash */
177
Stefan Tauner1e146392011-09-15 23:52:55 +0000178#define ICH8_REG_VSCC 0xC1 /* 32 Bits Vendor Specific Component Capabilities */
179#define ICH9_REG_LVSCC 0xC4 /* 32 Bits Host Lower Vendor Specific Component Capabilities */
180#define ICH9_REG_UVSCC 0xC8 /* 32 Bits Host Upper Vendor Specific Component Capabilities */
181/* The individual fields of the VSCC registers are defined in the file
182 * ich_descriptors.h. The reason is that the same layout is also used in the
183 * flash descriptor to define the properties of the different flash chips
184 * supported. The BIOS (or the ME?) is responsible to populate the ICH registers
185 * with the information from the descriptor on startup depending on the actual
186 * chip(s) detected. */
187
Stefan Taunerbd649e42011-07-01 00:39:16 +0000188#define ICH9_REG_FPB 0xD0 /* 32 Bits Flash Partition Boundary */
189#define FPB_FPBA_OFF 0 /* 0-12: Block/Sector Erase Size */
190#define FPB_FPBA (0x1FFF << FPB_FPBA_OFF)
191
Dominik Geyerb46acba2008-05-16 12:55:55 +0000192// ICH9R SPI commands
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000193#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
194#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
195#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
196#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
Dominik Geyerb46acba2008-05-16 12:55:55 +0000197
Stefan Reinauera9424d52008-06-27 16:28:34 +0000198// ICH7 registers
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000199#define ICH7_REG_SPIS 0x00 /* 16 Bits */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000200#define SPIS_SCIP 0x0001
201#define SPIS_GRANT 0x0002
202#define SPIS_CDS 0x0004
203#define SPIS_FCERR 0x0008
204#define SPIS_RESERVED_MASK 0x7ff0
Stefan Reinauera9424d52008-06-27 16:28:34 +0000205
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000206/* VIA SPI is compatible with ICH7, but maxdata
207 to transfer is 16 bytes.
208
209 DATA byte count on ICH7 is 8:13, on VIA 8:11
210
211 bit 12 is port select CS0 CS1
212 bit 13 is FAST READ enable
213 bit 7 is used with fast read and one shot controls CS de-assert?
214*/
215
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000216#define ICH7_REG_SPIC 0x02 /* 16 Bits */
217#define SPIC_SCGO 0x0002
218#define SPIC_ACS 0x0004
219#define SPIC_SPOP 0x0008
220#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000221
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000222#define ICH7_REG_SPIA 0x04 /* 32 Bits */
223#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
224#define ICH7_REG_PREOP 0x54 /* 16 Bits */
225#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
226#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000227
FENG yu ningc05a2952008-12-08 18:16:58 +0000228/* ICH SPI configuration lock-down. May be set during chipset enabling. */
Michael Karchera4448d92010-07-22 18:04:15 +0000229static int ichspi_lock = 0;
FENG yu ningc05a2952008-12-08 18:16:58 +0000230
Stefan Taunera8d838d2011-11-06 23:51:09 +0000231static enum ich_chipset ich_generation = CHIPSET_ICH_UNKNOWN;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000232uint32_t ichspi_bbar = 0;
233
Michael Karchera4448d92010-07-22 18:04:15 +0000234static void *ich_spibar = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000235
Dominik Geyerb46acba2008-05-16 12:55:55 +0000236typedef struct _OPCODE {
237 uint8_t opcode; //This commands spi opcode
238 uint8_t spi_type; //This commands spi type
239 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
240} OPCODE;
241
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000242/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000243 * Preop 1: Write Enable
244 * Preop 2: Write Status register enable
245 *
246 * OP 0: Write address
247 * OP 1: Read Address
248 * OP 2: ERASE block
249 * OP 3: Read Status register
250 * OP 4: Read ID
251 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000252 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000253 * OP 7: Chip erase
254 */
255typedef struct _OPCODES {
256 uint8_t preop[2];
257 OPCODE opcode[8];
258} OPCODES;
259
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000260static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000261
262/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000263static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000264{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000265 return mmio_readl(ich_spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000266}
267
Uwe Hermann09e04f72009-05-16 22:36:00 +0000268static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000269{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000270 return mmio_readw(ich_spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000271}
272
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000273static uint16_t REGREAD8(int X)
274{
275 return mmio_readb(ich_spibar + X);
276}
277
Stefan Taunerccd92a12011-07-01 00:39:01 +0000278#define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
279#define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
280#define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off))
Dominik Geyerb46acba2008-05-16 12:55:55 +0000281
Dominik Geyerb46acba2008-05-16 12:55:55 +0000282/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000283static int find_opcode(OPCODES *op, uint8_t opcode);
284static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000285static int generate_opcodes(OPCODES * op);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000286static int program_opcodes(OPCODES *op, int enable_undo);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000287static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000288 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000289
FENG yu ningf041e9b2008-12-15 02:32:11 +0000290/* for pairing opcodes with their required preop */
291struct preop_opcode_pair {
292 uint8_t preop;
293 uint8_t opcode;
294};
295
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000296/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000297const struct preop_opcode_pair pops[] = {
FENG yu ningf041e9b2008-12-15 02:32:11 +0000298 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
299 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
300 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
301 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
302 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
303 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000304 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
305 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000306 {JEDEC_EWSR, JEDEC_WRSR},
307 {0,}
308};
309
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000310/* Reasonable default configuration. Needs ad-hoc modifications if we
311 * encounter unlisted opcodes. Fun.
312 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000313static OPCODES O_ST_M25P = {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000314 {
315 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000316 JEDEC_EWSR,
317 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000318 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000319 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000320 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000321 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000322 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000323 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000324 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000325 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000326 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
327 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000328};
329
Helge Wagner738e2522010-10-05 22:06:05 +0000330/* List of opcodes with their corresponding spi_type
331 * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
332 * is needed which is currently not in the chipset OPCODE table
333 */
334static OPCODE POSSIBLE_OPCODES[] = {
335 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
336 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
337 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
338 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
339 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
340 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
341 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
342 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
343 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
344 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
345 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
346};
347
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000348static OPCODES O_EXISTING = {};
FENG yu ningc05a2952008-12-08 18:16:58 +0000349
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000350/* pretty printing functions */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000351static void prettyprint_opcodes(OPCODES *ops)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000352{
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000353 OPCODE oc;
354 const char *t;
355 const char *a;
356 uint8_t i;
357 static const char *const spi_type[4] = {
358 "read w/o addr",
359 "write w/o addr",
360 "read w/ addr",
361 "write w/ addr"
362 };
363 static const char *const atomic_type[3] = {
364 "none",
365 " 0 ",
366 " 1 "
367 };
368
369 if (ops == NULL)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000370 return;
371
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000372 msg_pdbg2(" OP Type Pre-OP\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000373 for (i = 0; i < 8; i++) {
374 oc = ops->opcode[i];
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000375 t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type];
376 a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic];
377 msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000378 }
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000379 msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0],
380 ops->preop[1]);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000381}
382
Nico Huberd54e4f42017-03-23 23:45:47 +0100383#define _pprint_reg(bit, mask, off, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & mask) >> off)
384#define pprint_reg(reg, bit, val, sep) _pprint_reg(bit, reg##_##bit, reg##_##bit##_OFF, val, sep)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000385
Stefan Tauner55206942011-06-11 09:53:22 +0000386static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
387{
388 msg_pdbg("HSFS: ");
389 pprint_reg(HSFS, FDONE, reg_val, ", ");
390 pprint_reg(HSFS, FCERR, reg_val, ", ");
391 pprint_reg(HSFS, AEL, reg_val, ", ");
David Hendricksa5216362017-08-08 20:02:22 -0700392 if (ich_generation != CHIPSET_100_SERIES_SUNRISE_POINT &&
393 ich_generation != CHIPSET_C620_SERIES_LEWISBURG) {
Nico Huberd54e4f42017-03-23 23:45:47 +0100394 pprint_reg(HSFS, BERASE, reg_val, ", ");
395 }
Stefan Tauner55206942011-06-11 09:53:22 +0000396 pprint_reg(HSFS, SCIP, reg_val, ", ");
David Hendricksa5216362017-08-08 20:02:22 -0700397 if (ich_generation == CHIPSET_100_SERIES_SUNRISE_POINT ||
398 ich_generation == CHIPSET_C620_SERIES_LEWISBURG) {
Nico Huberd54e4f42017-03-23 23:45:47 +0100399 pprint_reg(HSFS, PRR34_LOCKDN, reg_val, ", ");
400 pprint_reg(HSFS, WRSDIS, reg_val, ", ");
401 }
Stefan Tauner55206942011-06-11 09:53:22 +0000402 pprint_reg(HSFS, FDOPSS, reg_val, ", ");
403 pprint_reg(HSFS, FDV, reg_val, ", ");
404 pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
405}
406
407static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
408{
409 msg_pdbg("HSFC: ");
410 pprint_reg(HSFC, FGO, reg_val, ", ");
David Hendricksa5216362017-08-08 20:02:22 -0700411 if (ich_generation != CHIPSET_100_SERIES_SUNRISE_POINT &&
412 ich_generation != CHIPSET_C620_SERIES_LEWISBURG) {
Nico Huberd54e4f42017-03-23 23:45:47 +0100413 pprint_reg(HSFC, FCYCLE, reg_val, ", ");
414 } else {
415 _pprint_reg(HSFC, PCH100_HSFC_FCYCLE, PCH100_HSFC_FCYCLE_OFF, reg_val, ", ");
416 pprint_reg(HSFC, WET, reg_val, ", ");
417 }
Stefan Tauner55206942011-06-11 09:53:22 +0000418 pprint_reg(HSFC, FDBC, reg_val, ", ");
419 pprint_reg(HSFC, SME, reg_val, "\n");
420}
421
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000422static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
423{
424 msg_pdbg("SSFS: ");
425 pprint_reg(SSFS, SCIP, reg_val, ", ");
426 pprint_reg(SSFS, FDONE, reg_val, ", ");
427 pprint_reg(SSFS, FCERR, reg_val, ", ");
428 pprint_reg(SSFS, AEL, reg_val, "\n");
429}
430
431static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
432{
433 msg_pdbg("SSFC: ");
434 pprint_reg(SSFC, SCGO, reg_val, ", ");
435 pprint_reg(SSFC, ACS, reg_val, ", ");
436 pprint_reg(SSFC, SPOP, reg_val, ", ");
437 pprint_reg(SSFC, COP, reg_val, ", ");
438 pprint_reg(SSFC, DBC, reg_val, ", ");
439 pprint_reg(SSFC, SME, reg_val, ", ");
440 pprint_reg(SSFC, SCF, reg_val, "\n");
441}
442
Nico Huberd54e4f42017-03-23 23:45:47 +0100443static void prettyprint_pch100_reg_dlock(const uint32_t reg_val)
444{
445 msg_pdbg("DLOCK: ");
446 pprint_reg(DLOCK, BMWAG_LOCKDN, reg_val, ", ");
447 pprint_reg(DLOCK, BMRAG_LOCKDN, reg_val, ", ");
448 pprint_reg(DLOCK, SBMWAG_LOCKDN, reg_val, ", ");
449 pprint_reg(DLOCK, SBMRAG_LOCKDN, reg_val, ",\n ");
450 pprint_reg(DLOCK, PR0_LOCKDN, reg_val, ", ");
451 pprint_reg(DLOCK, PR1_LOCKDN, reg_val, ", ");
452 pprint_reg(DLOCK, PR2_LOCKDN, reg_val, ", ");
453 pprint_reg(DLOCK, PR3_LOCKDN, reg_val, ", ");
454 pprint_reg(DLOCK, PR4_LOCKDN, reg_val, ",\n ");
455 pprint_reg(DLOCK, SSEQ_LOCKDN, reg_val, "\n");
456}
457
458static struct {
459 size_t reg_ssfsc;
460 size_t reg_preop;
461 size_t reg_optype;
462 size_t reg_opmenu;
463} swseq_data;
464
Helge Wagner738e2522010-10-05 22:06:05 +0000465static uint8_t lookup_spi_type(uint8_t opcode)
466{
467 int a;
468
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000469 for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
Helge Wagner738e2522010-10-05 22:06:05 +0000470 if (POSSIBLE_OPCODES[a].opcode == opcode)
471 return POSSIBLE_OPCODES[a].spi_type;
472 }
473
474 return 0xFF;
475}
476
477static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
478{
479 uint8_t spi_type;
480
481 spi_type = lookup_spi_type(opcode);
482 if (spi_type > 3) {
483 /* Try to guess spi type from read/write sizes.
484 * The following valid writecnt/readcnt combinations exist:
485 * writecnt = 4, readcnt >= 0
486 * writecnt = 1, readcnt >= 0
487 * writecnt >= 4, readcnt = 0
488 * writecnt >= 1, readcnt = 0
489 * writecnt >= 1 is guaranteed for all commands.
490 */
491 if (readcnt == 0)
492 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
493 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
494 * bytes are actual the address, they go to the bus anyhow
495 */
496 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
497 else if (writecnt == 1) // and readcnt is > 0
498 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
499 else if (writecnt == 4) // and readcnt is > 0
500 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
Stefan Taunerdc704ed2012-05-06 15:11:26 +0000501 else // we have an invalid case
502 return SPI_INVALID_LENGTH;
Helge Wagner738e2522010-10-05 22:06:05 +0000503 }
Stefan Taunerdc704ed2012-05-06 15:11:26 +0000504 int oppos = 2; // use original JEDEC_BE_D8 offset
505 curopcodes->opcode[oppos].opcode = opcode;
506 curopcodes->opcode[oppos].spi_type = spi_type;
507 program_opcodes(curopcodes, 0);
508 oppos = find_opcode(curopcodes, opcode);
Stefan Taunerd94d25d2012-07-28 03:17:15 +0000509 msg_pdbg2("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
Stefan Taunerdc704ed2012-05-06 15:11:26 +0000510 return oppos;
Helge Wagner738e2522010-10-05 22:06:05 +0000511}
512
Uwe Hermann09e04f72009-05-16 22:36:00 +0000513static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000514{
515 int a;
516
Stefan Tauner50e7c602011-11-08 10:55:54 +0000517 if (op == NULL) {
518 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
519 return -1;
520 }
521
FENG yu ningc05a2952008-12-08 18:16:58 +0000522 for (a = 0; a < 8; a++) {
523 if (op->opcode[a].opcode == opcode)
524 return a;
525 }
526
527 return -1;
528}
529
Uwe Hermann09e04f72009-05-16 22:36:00 +0000530static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000531{
532 int a;
533
Stefan Tauner50e7c602011-11-08 10:55:54 +0000534 if (op == NULL) {
535 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
536 return -1;
537 }
538
FENG yu ningc05a2952008-12-08 18:16:58 +0000539 for (a = 0; a < 2; a++) {
540 if (op->preop[a] == preop)
541 return a;
542 }
543
544 return -1;
545}
546
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000547/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000548static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000549{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000550 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000551 uint16_t preop, optype;
552 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000553
554 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000555 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000556 return -1;
557 }
558
Stefan Taunera8d838d2011-11-06 23:51:09 +0000559 switch (ich_generation) {
560 case CHIPSET_ICH7:
Stefan Tauner92d6a862013-10-25 00:33:37 +0000561 case CHIPSET_TUNNEL_CREEK:
562 case CHIPSET_CENTERTON:
FENG yu ningc05a2952008-12-08 18:16:58 +0000563 preop = REGREAD16(ICH7_REG_PREOP);
564 optype = REGREAD16(ICH7_REG_OPTYPE);
565 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
566 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
567 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000568 case CHIPSET_ICH8:
569 default: /* Future version might behave the same */
Nico Huberd54e4f42017-03-23 23:45:47 +0100570 preop = REGREAD16(swseq_data.reg_preop);
571 optype = REGREAD16(swseq_data.reg_optype);
572 opmenu[0] = REGREAD32(swseq_data.reg_opmenu);
573 opmenu[1] = REGREAD32(swseq_data.reg_opmenu + 4);
FENG yu ningc05a2952008-12-08 18:16:58 +0000574 break;
FENG yu ningc05a2952008-12-08 18:16:58 +0000575 }
576
577 op->preop[0] = (uint8_t) preop;
578 op->preop[1] = (uint8_t) (preop >> 8);
579
580 for (a = 0; a < 8; a++) {
581 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
582 optype >>= 2;
583 }
584
585 for (a = 0; a < 4; a++) {
586 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
587 opmenu[0] >>= 8;
588 }
589
590 for (a = 4; a < 8; a++) {
591 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
592 opmenu[1] >>= 8;
593 }
594
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000595 /* No preopcodes used by default. */
596 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000597 op->opcode[a].atomic = 0;
598
FENG yu ningc05a2952008-12-08 18:16:58 +0000599 return 0;
600}
601
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000602static int program_opcodes(OPCODES *op, int enable_undo)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000603{
604 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000605 uint16_t preop, optype;
606 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000607
608 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000609 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000610 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000611 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000612 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000613
Stefan Reinauera9424d52008-06-27 16:28:34 +0000614 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000615 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000616 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000617 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000618 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000619
Stefan Reinauera9424d52008-06-27 16:28:34 +0000620 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000621 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000622 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000623 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000624 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000625
Stefan Tauner92d6a862013-10-25 00:33:37 +0000626 /* Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000627 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000628 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000629 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000630 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000631
Stefan Taunerd94d25d2012-07-28 03:17:15 +0000632 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 +0000633 switch (ich_generation) {
634 case CHIPSET_ICH7:
Stefan Tauner92d6a862013-10-25 00:33:37 +0000635 case CHIPSET_TUNNEL_CREEK:
636 case CHIPSET_CENTERTON:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000637 /* Register undo only for enable_undo=1, i.e. first call. */
638 if (enable_undo) {
639 rmmio_valw(ich_spibar + ICH7_REG_PREOP);
640 rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
641 rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
642 rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
643 }
644 mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
645 mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
646 mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
647 mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000648 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000649 case CHIPSET_ICH8:
650 default: /* Future version might behave the same */
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000651 /* Register undo only for enable_undo=1, i.e. first call. */
652 if (enable_undo) {
Nico Huberd54e4f42017-03-23 23:45:47 +0100653 rmmio_valw(ich_spibar + swseq_data.reg_preop);
654 rmmio_valw(ich_spibar + swseq_data.reg_optype);
655 rmmio_vall(ich_spibar + swseq_data.reg_opmenu);
656 rmmio_vall(ich_spibar + swseq_data.reg_opmenu + 4);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000657 }
Nico Huberd54e4f42017-03-23 23:45:47 +0100658 mmio_writew(preop, ich_spibar + swseq_data.reg_preop);
659 mmio_writew(optype, ich_spibar + swseq_data.reg_optype);
660 mmio_writel(opmenu[0], ich_spibar + swseq_data.reg_opmenu);
661 mmio_writel(opmenu[1], ich_spibar + swseq_data.reg_opmenu + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000662 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000663 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000664
665 return 0;
666}
667
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000668/*
Stefan Tauner50e7c602011-11-08 10:55:54 +0000669 * Returns -1 if at least one mandatory opcode is inaccessible, 0 otherwise.
670 * FIXME: this should also check for
671 * - at least one probing opcode (RDID (incl. AT25F variants?), REMS, RES?)
672 * - at least one erasing opcode (lots.)
673 * - at least one program opcode (BYTE_PROGRAM, AAI_WORD_PROGRAM, ...?)
674 * - necessary preops? (EWSR, WREN, ...?)
675 */
676static int ich_missing_opcodes()
677{
678 uint8_t ops[] = {
679 JEDEC_READ,
680 JEDEC_RDSR,
681 0
682 };
683 int i = 0;
684 while (ops[i] != 0) {
685 msg_pspew("checking for opcode 0x%02x\n", ops[i]);
686 if (find_opcode(curopcodes, ops[i]) == -1)
687 return -1;
688 i++;
689 }
690 return 0;
691}
692
693/*
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000694 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
695 * it didn't stick.
696 */
Stefan Taunera8d838d2011-11-06 23:51:09 +0000697static void ich_set_bbar(uint32_t min_addr)
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000698{
Stefan Taunere27b2d42011-07-01 00:39:09 +0000699 int bbar_off;
Stefan Tauner7783f312011-09-17 21:21:42 +0000700 switch (ich_generation) {
Stefan Taunera8d838d2011-11-06 23:51:09 +0000701 case CHIPSET_ICH7:
Stefan Tauner92d6a862013-10-25 00:33:37 +0000702 case CHIPSET_TUNNEL_CREEK:
703 case CHIPSET_CENTERTON:
Stefan Taunere27b2d42011-07-01 00:39:09 +0000704 bbar_off = 0x50;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000705 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000706 case CHIPSET_ICH8:
Duncan Laurie4095ed72014-08-20 15:39:32 +0000707 case CHIPSET_BAYTRAIL:
708 msg_pdbg("BBAR offset is unknown!\n");
Stefan Tauner7783f312011-09-17 21:21:42 +0000709 return;
Stefan Taunera8d838d2011-11-06 23:51:09 +0000710 case CHIPSET_ICH9:
Stefan Tauner7783f312011-09-17 21:21:42 +0000711 default: /* Future version might behave the same */
Stefan Taunere27b2d42011-07-01 00:39:09 +0000712 bbar_off = ICH9_REG_BBAR;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000713 break;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000714 }
Stefan Taunere27b2d42011-07-01 00:39:09 +0000715
716 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
717 if (ichspi_bbar) {
718 msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
719 ichspi_bbar);
720 }
721 min_addr &= BBAR_MASK;
722 ichspi_bbar |= min_addr;
723 rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
724 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
725
726 /* We don't have any option except complaining. And if the write
727 * failed, the restore will fail as well, so no problem there.
728 */
729 if (ichspi_bbar != min_addr)
Stefan Tauner7783f312011-09-17 21:21:42 +0000730 msg_perr("Setting BBAR to 0x%08x failed! New value: 0x%08x.\n",
731 min_addr, ichspi_bbar);
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000732}
733
Stefan Tauner8b391b82011-08-09 01:49:34 +0000734/* Read len bytes from the fdata/spid register into the data array.
735 *
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000736 * Note that using len > flash->mst->spi.max_data_read will return garbage or
Stefan Tauner8b391b82011-08-09 01:49:34 +0000737 * may even crash.
738 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000739static void ich_read_data(uint8_t *data, int len, int reg0_off)
Stefan Tauner8b391b82011-08-09 01:49:34 +0000740 {
741 int i;
742 uint32_t temp32 = 0;
743
744 for (i = 0; i < len; i++) {
745 if ((i % 4) == 0)
746 temp32 = REGREAD32(reg0_off + i);
747
748 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
749 }
750}
751
752/* Fill len bytes from the data array into the fdata/spid registers.
753 *
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000754 * Note that using len > flash->mst->spi.max_data_write will trash the registers
Stefan Tauner8b391b82011-08-09 01:49:34 +0000755 * following the data registers.
756 */
757static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
758{
759 uint32_t temp32 = 0;
760 int i;
761
762 if (len <= 0)
763 return;
764
765 for (i = 0; i < len; i++) {
766 if ((i % 4) == 0)
767 temp32 = 0;
768
769 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
770
771 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
772 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
773 }
774 i--;
775 if ((i % 4) != 3) /* Write remaining data to regs. */
776 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
777}
778
FENG yu ningf041e9b2008-12-15 02:32:11 +0000779/* This function generates OPCODES from or programs OPCODES to ICH according to
780 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000781 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000782 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000783 */
Michael Karchera4448d92010-07-22 18:04:15 +0000784static int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000785{
786 int rc = 0;
787 OPCODES *curopcodes_done;
788
789 if (curopcodes)
790 return 0;
791
792 if (ichspi_lock) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000793 msg_pdbg("Reading OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000794 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000795 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000796 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000797 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000798 curopcodes_done = &O_ST_M25P;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000799 rc = program_opcodes(curopcodes_done, 1);
FENG yu ningc05a2952008-12-08 18:16:58 +0000800 }
801
802 if (rc) {
803 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000804 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000805 return 1;
806 } else {
807 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000808 msg_pdbg("done\n");
Stefan Tauner8b391b82011-08-09 01:49:34 +0000809 prettyprint_opcodes(curopcodes);
FENG yu ningc05a2952008-12-08 18:16:58 +0000810 return 0;
811 }
812}
813
Stefan Reinauer43119562008-11-02 19:51:50 +0000814static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000815 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000816{
817 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000818 int timeout;
Stefan Tauner8b391b82011-08-09 01:49:34 +0000819 uint32_t temp32;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000820 uint16_t temp16;
Stefan Reinauer43119562008-11-02 19:51:50 +0000821 uint64_t opmenu;
822 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000823
824 /* Is it a write command? */
825 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
826 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
827 write_cmd = 1;
828 }
829
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000830 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
831 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
832 programmer_delay(10);
833 }
834 if (!timeout) {
835 msg_perr("Error: SCIP never cleared!\n");
836 return 1;
837 }
838
Stefan Tauner10b3e222011-07-01 00:39:23 +0000839 /* Program offset in flash into SPIA while preserving reserved bits. */
840 temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
841 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000842
Stefan Tauner10b3e222011-07-01 00:39:23 +0000843 /* Program data into SPID0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000844 if (write_cmd && (datalength != 0))
845 ich_fill_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000846
847 /* Assemble SPIS */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000848 temp16 = REGREAD16(ICH7_REG_SPIS);
849 /* keep reserved bits */
850 temp16 &= SPIS_RESERVED_MASK;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000851 /* clear error status registers */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000852 temp16 |= (SPIS_CDS | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000853 REGWRITE16(ICH7_REG_SPIS, temp16);
854
855 /* Assemble SPIC */
856 temp16 = 0;
857
858 if (datalength != 0) {
859 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000860 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000861 }
862
863 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000864 opmenu = REGREAD32(ICH7_REG_OPMENU);
865 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
866
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000867 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
868 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000869 break;
870 }
871 opmenu >>= 8;
872 }
873 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000874 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000875 return 1;
876 }
877 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000878
Michael Karcher136125a2011-04-29 22:11:36 +0000879 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
880 /* Handle Atomic. Atomic commands include three steps:
881 - sending the preop (mainly EWSR or WREN)
882 - sending the main command
883 - waiting for the busy bit (WIP) to be cleared
884 This means the timeout must be sufficient for chip erase
885 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000886 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000887 switch (op.atomic) {
888 case 2:
889 /* Select second preop. */
890 temp16 |= SPIC_SPOP;
891 /* And fall through. */
892 case 1:
893 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000894 temp16 |= SPIC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000895 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000896 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000897 }
898
899 /* Start */
900 temp16 |= SPIC_SCGO;
901
902 /* write it */
903 REGWRITE16(ICH7_REG_SPIC, temp16);
904
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000905 /* Wait for Cycle Done Status or Flash Cycle Error. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000906 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
907 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000908 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000909 }
910 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000911 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
912 REGREAD16(ICH7_REG_SPIS));
913 return 1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000914 }
915
Sean Nelson316a29f2010-05-07 20:09:04 +0000916 /* FIXME: make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000917 temp16 = REGREAD16(ICH7_REG_SPIS);
918 if (temp16 & SPIS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000919 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000920 /* keep reserved bits */
921 temp16 &= SPIS_RESERVED_MASK;
922 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000923 return 1;
924 }
925
Stefan Tauner8b391b82011-08-09 01:49:34 +0000926 if ((!write_cmd) && (datalength != 0))
927 ich_read_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000928
929 return 0;
930}
931
Stefan Reinauer43119562008-11-02 19:51:50 +0000932static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000933 uint8_t datalength, uint8_t * data)
934{
935 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000936 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000937 uint32_t temp32;
Stefan Reinauer43119562008-11-02 19:51:50 +0000938 uint64_t opmenu;
939 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000940
941 /* Is it a write command? */
942 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
943 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
944 write_cmd = 1;
945 }
946
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000947 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
Nico Huberd54e4f42017-03-23 23:45:47 +0100948 while ((REGREAD8(swseq_data.reg_ssfsc) & SSFS_SCIP) && --timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000949 programmer_delay(10);
950 }
951 if (!timeout) {
952 msg_perr("Error: SCIP never cleared!\n");
953 return 1;
954 }
955
Stefan Tauner10b3e222011-07-01 00:39:23 +0000956 /* Program offset in flash into FADDR while preserve the reserved bits
957 * and clearing the 25. address bit which is only useable in hwseq. */
958 temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
959 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000960
961 /* Program data into FDATA0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000962 if (write_cmd && (datalength != 0))
963 ich_fill_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000964
965 /* Assemble SSFS + SSFC */
Nico Huberd54e4f42017-03-23 23:45:47 +0100966 temp32 = REGREAD32(swseq_data.reg_ssfsc);
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000967 /* Keep reserved bits only */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000968 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000969 /* Clear cycle done and cycle error status registers */
970 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Nico Huberd54e4f42017-03-23 23:45:47 +0100971 REGWRITE32(swseq_data.reg_ssfsc, temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000972
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000973 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000974 temp32 |= SSFC_SCF_20MHZ;
975
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000976 /* Set data byte count (DBC) and data cycle bit (DS) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000977 if (datalength != 0) {
978 uint32_t datatemp;
979 temp32 |= SSFC_DS;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000980 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
981 SSFC_DBC);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000982 temp32 |= datatemp;
983 }
984
985 /* Select opcode */
Nico Huber8b2152d2017-08-31 13:18:49 +0200986 opmenu = REGREAD32(swseq_data.reg_opmenu);
987 opmenu |= ((uint64_t)REGREAD32(swseq_data.reg_opmenu + 4)) << 32;
Stefan Reinauer43119562008-11-02 19:51:50 +0000988
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000989 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
990 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000991 break;
992 }
993 opmenu >>= 8;
994 }
995 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000996 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000997 return 1;
998 }
999 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +00001000
Michael Karcher136125a2011-04-29 22:11:36 +00001001 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
1002 /* Handle Atomic. Atomic commands include three steps:
1003 - sending the preop (mainly EWSR or WREN)
1004 - sending the main command
1005 - waiting for the busy bit (WIP) to be cleared
1006 This means the timeout must be sufficient for chip erase
1007 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +00001008 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001009 switch (op.atomic) {
1010 case 2:
1011 /* Select second preop. */
1012 temp32 |= SSFC_SPOP;
1013 /* And fall through. */
1014 case 1:
1015 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +00001016 temp32 |= SSFC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +00001017 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001018 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001019 }
1020
1021 /* Start */
1022 temp32 |= SSFC_SCGO;
1023
1024 /* write it */
Nico Huberd54e4f42017-03-23 23:45:47 +01001025 REGWRITE32(swseq_data.reg_ssfsc, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +00001026
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001027 /* Wait for Cycle Done Status or Flash Cycle Error. */
Nico Huberd54e4f42017-03-23 23:45:47 +01001028 while (((REGREAD32(swseq_data.reg_ssfsc) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001029 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +00001030 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +00001031 }
1032 if (!timeout) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001033 msg_perr("timeout, REG_SSFS=0x%08x\n",
1034 REGREAD32(swseq_data.reg_ssfsc));
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001035 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001036 }
1037
Sean Nelson316a29f2010-05-07 20:09:04 +00001038 /* FIXME make sure we do not needlessly cause transaction errors. */
Nico Huberd54e4f42017-03-23 23:45:47 +01001039 temp32 = REGREAD32(swseq_data.reg_ssfsc);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001040 if (temp32 & SSFS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001041 msg_perr("Transaction error!\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001042 prettyprint_ich9_reg_ssfs(temp32);
1043 prettyprint_ich9_reg_ssfc(temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001044 /* keep reserved bits */
1045 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
1046 /* Clear the transaction error. */
Nico Huberd54e4f42017-03-23 23:45:47 +01001047 REGWRITE32(swseq_data.reg_ssfsc, temp32 | SSFS_FCERR);
Dominik Geyerb46acba2008-05-16 12:55:55 +00001048 return 1;
1049 }
1050
Stefan Tauner8b391b82011-08-09 01:49:34 +00001051 if ((!write_cmd) && (datalength != 0))
1052 ich_read_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +00001053
1054 return 0;
1055}
1056
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001057static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +00001058 uint8_t datalength, uint8_t * data)
1059{
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001060 /* max_data_read == max_data_write for all Intel/VIA SPI masters */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001061 uint8_t maxlength = flash->mst->spi.max_data_read;
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001062
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00001063 if (ich_generation == CHIPSET_ICH_UNKNOWN) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001064 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001065 return -1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +00001066 }
Stefan Reinauera9424d52008-06-27 16:28:34 +00001067
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001068 if (datalength > maxlength) {
1069 msg_perr("%s: Internal command size error for "
1070 "opcode 0x%02x, got datalength=%i, want <=%i\n",
1071 __func__, op.opcode, datalength, maxlength);
1072 return SPI_INVALID_LENGTH;
1073 }
1074
Stefan Taunera8d838d2011-11-06 23:51:09 +00001075 switch (ich_generation) {
1076 case CHIPSET_ICH7:
Stefan Tauner92d6a862013-10-25 00:33:37 +00001077 case CHIPSET_TUNNEL_CREEK:
1078 case CHIPSET_CENTERTON:
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001079 return ich7_run_opcode(op, offset, datalength, data, maxlength);
Stefan Taunera8d838d2011-11-06 23:51:09 +00001080 case CHIPSET_ICH8:
1081 default: /* Future version might behave the same */
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001082 return ich9_run_opcode(op, offset, datalength, data);
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001083 }
Stefan Reinauera9424d52008-06-27 16:28:34 +00001084}
1085
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001086static int ich_spi_send_command(struct flashctx *flash, unsigned int writecnt,
1087 unsigned int readcnt,
1088 const unsigned char *writearr,
1089 unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +00001090{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001091 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001092 int opcode_index = -1;
1093 const unsigned char cmd = *writearr;
1094 OPCODE *opcode;
1095 uint32_t addr = 0;
1096 uint8_t *data;
1097 int count;
1098
Dominik Geyerb46acba2008-05-16 12:55:55 +00001099 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001100 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +00001101 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +00001102 if (!ichspi_lock)
1103 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
Stefan Taunerdc704ed2012-05-06 15:11:26 +00001104 if (opcode_index == SPI_INVALID_LENGTH) {
1105 msg_pdbg("OPCODE 0x%02x has unsupported length, will not execute.\n", cmd);
1106 return SPI_INVALID_LENGTH;
1107 } else if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001108 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
1109 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +00001110 return SPI_INVALID_OPCODE;
1111 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001112 }
1113
1114 opcode = &(curopcodes->opcode[opcode_index]);
1115
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001116 /* The following valid writecnt/readcnt combinations exist:
1117 * writecnt = 4, readcnt >= 0
1118 * writecnt = 1, readcnt >= 0
1119 * writecnt >= 4, readcnt = 0
1120 * writecnt >= 1, readcnt = 0
1121 * writecnt >= 1 is guaranteed for all commands.
1122 */
1123 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
1124 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001125 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001126 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
1127 writecnt);
1128 return SPI_INVALID_LENGTH;
1129 }
1130 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
1131 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001132 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001133 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
1134 writecnt);
1135 return SPI_INVALID_LENGTH;
1136 }
1137 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1138 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001139 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001140 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
1141 writecnt);
1142 return SPI_INVALID_LENGTH;
1143 }
1144 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1145 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1146 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001147 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001148 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1149 readcnt);
1150 return SPI_INVALID_LENGTH;
1151 }
1152
Dominik Geyerb46acba2008-05-16 12:55:55 +00001153 /* if opcode-type requires an address */
1154 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1155 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001156 addr = (writearr[1] << 16) |
1157 (writearr[2] << 8) | (writearr[3] << 0);
Stefan Taunera8d838d2011-11-06 23:51:09 +00001158 if (addr < ichspi_bbar) {
1159 msg_perr("%s: Address 0x%06x below allowed "
1160 "range 0x%06x-0xffffff\n", __func__,
1161 addr, ichspi_bbar);
1162 return SPI_INVALID_ADDRESS;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001163 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001164 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001165
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001166 /* Translate read/write array/count.
1167 * The maximum data length is identical for the maximum read length and
1168 * for the maximum write length excluding opcode and address. Opcode and
1169 * address are stored in separate registers, not in the data registers
1170 * and are thus not counted towards data length. The only exception
1171 * applies if the opcode definition (un)intentionally classifies said
1172 * opcode incorrectly as non-address opcode or vice versa. */
Dominik Geyerb46acba2008-05-16 12:55:55 +00001173 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001174 data = (uint8_t *) (writearr + 1);
1175 count = writecnt - 1;
1176 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1177 data = (uint8_t *) (writearr + 4);
1178 count = writecnt - 4;
1179 } else {
1180 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001181 count = readcnt;
1182 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001183
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001184 result = run_opcode(flash, *opcode, addr, count, data);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001185 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001186 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1187 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1188 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1189 msg_pdbg("at address 0x%06x ", addr);
1190 }
1191 msg_pdbg("(payload length was %d).\n", count);
1192
1193 /* Print out the data array if it contains data to write.
1194 * Errors are detected before the received data is read back into
1195 * the array so it won't make sense to print it then. */
1196 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1197 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1198 int i;
1199 msg_pspew("The data was:\n");
Stefan Taunerf382e352011-11-08 11:55:24 +00001200 for (i = 0; i < count; i++){
Stefan Tauner8ed29342011-04-29 23:53:09 +00001201 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1202 }
1203 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001204 }
1205
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001206 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001207}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001208
Stefan Tauner50e7c602011-11-08 10:55:54 +00001209static struct hwseq_data {
1210 uint32_t size_comp0;
1211 uint32_t size_comp1;
Nico Huberd54e4f42017-03-23 23:45:47 +01001212 uint32_t addr_mask;
1213 bool only_4k;
1214 uint32_t hsfc_fcycle;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001215} hwseq_data;
1216
Nico Huberd54e4f42017-03-23 23:45:47 +01001217/* Sets FLA in FADDR to (addr & hwseq_data.addr_mask) without touching other bits. */
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001218static void ich_hwseq_set_addr(uint32_t addr)
1219{
Nico Huberd54e4f42017-03-23 23:45:47 +01001220 uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~hwseq_data.addr_mask;
1221 REGWRITE32(ICH9_REG_FADDR, (addr & hwseq_data.addr_mask) | addr_old);
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001222}
1223
1224/* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
1225 * of the block containing this address. May return nonsense if the address is
1226 * not valid. The erase block size for a specific address depends on the flash
1227 * partition layout as specified by FPB and the partition properties as defined
1228 * by UVSCC and LVSCC respectively. An alternative to implement this method
1229 * would be by querying FPB and the respective VSCC register directly.
1230 */
1231static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr)
1232{
Nico Huberd54e4f42017-03-23 23:45:47 +01001233 if (hwseq_data.only_4k) {
1234 return 4 * 1024;
1235 } else {
1236 uint8_t enc_berase;
1237 static const uint32_t dec_berase[4] = {
1238 256,
1239 4 * 1024,
1240 8 * 1024,
1241 64 * 1024
1242 };
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001243
Nico Huberd54e4f42017-03-23 23:45:47 +01001244 ich_hwseq_set_addr(addr);
1245 enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >> HSFS_BERASE_OFF;
1246 return dec_berase[enc_berase];
1247 }
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001248}
1249
1250/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
1251 Resets all error flags in HSFS.
1252 Returns 0 if the cycle completes successfully without errors within
1253 timeout us, 1 on errors. */
1254static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
1255 unsigned int len)
1256{
1257 uint16_t hsfs;
1258 uint32_t addr;
1259
1260 timeout /= 8; /* scale timeout duration to counter */
1261 while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) &
1262 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
1263 --timeout) {
1264 programmer_delay(8);
1265 }
1266 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1267 if (!timeout) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001268 addr = REGREAD32(ICH9_REG_FADDR) & hwseq_data.addr_mask;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001269 msg_perr("Timeout error between offset 0x%08x and "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001270 "0x%08x (= 0x%08x + %d)!\n",
1271 addr, addr + len - 1, addr, len - 1);
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001272 prettyprint_ich9_reg_hsfs(hsfs);
1273 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1274 return 1;
1275 }
1276
1277 if (hsfs & HSFS_FCERR) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001278 addr = REGREAD32(ICH9_REG_FADDR) & hwseq_data.addr_mask;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001279 msg_perr("Transaction error between offset 0x%08x and "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001280 "0x%08x (= 0x%08x + %d)!\n",
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001281 addr, addr + len - 1, addr, len - 1);
1282 prettyprint_ich9_reg_hsfs(hsfs);
1283 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1284 return 1;
1285 }
1286 return 0;
1287}
Stefan Tauner50e7c602011-11-08 10:55:54 +00001288
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001289static int ich_hwseq_probe(struct flashctx *flash)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001290{
1291 uint32_t total_size, boundary;
1292 uint32_t erase_size_low, size_low, erase_size_high, size_high;
1293 struct block_eraser *eraser;
1294
1295 total_size = hwseq_data.size_comp0 + hwseq_data.size_comp1;
Stefan Tauner5c316f92015-02-08 21:57:52 +00001296 msg_cdbg("Hardware sequencing reports %d attached SPI flash chip",
Stefan Tauner50e7c602011-11-08 10:55:54 +00001297 (hwseq_data.size_comp1 != 0) ? 2 : 1);
1298 if (hwseq_data.size_comp1 != 0)
1299 msg_cdbg("s with a combined");
1300 else
1301 msg_cdbg(" with a");
1302 msg_cdbg(" density of %d kB.\n", total_size / 1024);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001303 flash->chip->total_size = total_size / 1024;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001304
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001305 eraser = &(flash->chip->block_erasers[0]);
Nico Huberd54e4f42017-03-23 23:45:47 +01001306 if (!hwseq_data.only_4k)
1307 boundary = (REGREAD32(ICH9_REG_FPB) & FPB_FPBA) << 12;
1308 else
1309 boundary = 0;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001310 size_high = total_size - boundary;
1311 erase_size_high = ich_hwseq_get_erase_block_size(boundary);
1312
1313 if (boundary == 0) {
Stefan Tauner5c316f92015-02-08 21:57:52 +00001314 msg_cdbg2("There is only one partition containing the whole "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001315 "address space (0x%06x - 0x%06x).\n", 0, size_high-1);
1316 eraser->eraseblocks[0].size = erase_size_high;
1317 eraser->eraseblocks[0].count = size_high / erase_size_high;
Stefan Tauner5c316f92015-02-08 21:57:52 +00001318 msg_cdbg2("There are %d erase blocks with %d B each.\n",
Stefan Tauner50e7c602011-11-08 10:55:54 +00001319 size_high / erase_size_high, erase_size_high);
1320 } else {
Stefan Tauner5c316f92015-02-08 21:57:52 +00001321 msg_cdbg2("The flash address space (0x%06x - 0x%06x) is divided "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001322 "at address 0x%06x in two partitions.\n",
Stefan Taunerdbac46c2013-08-13 22:10:41 +00001323 0, total_size-1, boundary);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001324 size_low = total_size - size_high;
1325 erase_size_low = ich_hwseq_get_erase_block_size(0);
1326
1327 eraser->eraseblocks[0].size = erase_size_low;
1328 eraser->eraseblocks[0].count = size_low / erase_size_low;
1329 msg_cdbg("The first partition ranges from 0x%06x to 0x%06x.\n",
1330 0, size_low-1);
1331 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1332 size_low / erase_size_low, erase_size_low);
1333
1334 eraser->eraseblocks[1].size = erase_size_high;
1335 eraser->eraseblocks[1].count = size_high / erase_size_high;
1336 msg_cdbg("The second partition ranges from 0x%06x to 0x%06x.\n",
Stefan Taunerdbac46c2013-08-13 22:10:41 +00001337 boundary, total_size-1);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001338 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1339 size_high / erase_size_high, erase_size_high);
1340 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001341 flash->chip->tested = TEST_OK_PREW;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001342 return 1;
1343}
1344
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001345static int ich_hwseq_block_erase(struct flashctx *flash, unsigned int addr,
1346 unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001347{
1348 uint32_t erase_block;
1349 uint16_t hsfc;
1350 uint32_t timeout = 5000 * 1000; /* 5 s for max 64 kB */
1351
1352 erase_block = ich_hwseq_get_erase_block_size(addr);
1353 if (len != erase_block) {
1354 msg_cerr("Erase block size for address 0x%06x is %d B, "
1355 "but requested erase block size is %d B. "
1356 "Not erasing anything.\n", addr, erase_block, len);
1357 return -1;
1358 }
1359
1360 /* Although the hardware supports this (it would erase the whole block
1361 * containing the address) we play safe here. */
1362 if (addr % erase_block != 0) {
1363 msg_cerr("Erase address 0x%06x is not aligned to the erase "
1364 "block boundary (any multiple of %d). "
1365 "Not erasing anything.\n", addr, erase_block);
1366 return -1;
1367 }
1368
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001369 if (addr + len > flash->chip->total_size * 1024) {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001370 msg_perr("Request to erase some inaccessible memory address(es)"
1371 " (addr=0x%x, len=%d). "
1372 "Not erasing anything.\n", addr, len);
1373 return -1;
1374 }
1375
1376 msg_pdbg("Erasing %d bytes starting at 0x%06x.\n", len, addr);
Stefan Tauner7608d362014-08-05 23:28:47 +00001377 ich_hwseq_set_addr(addr);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001378
1379 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
1380 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1381
1382 hsfc = REGREAD16(ICH9_REG_HSFC);
Nico Huberd54e4f42017-03-23 23:45:47 +01001383 hsfc &= ~hwseq_data.hsfc_fcycle; /* clear operation */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001384 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
1385 hsfc |= HSFC_FGO; /* start */
1386 msg_pdbg("HSFC used for block erasing: ");
1387 prettyprint_ich9_reg_hsfc(hsfc);
1388 REGWRITE16(ICH9_REG_HSFC, hsfc);
1389
1390 if (ich_hwseq_wait_for_cycle_complete(timeout, len))
1391 return -1;
1392 return 0;
1393}
1394
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001395static int ich_hwseq_read(struct flashctx *flash, uint8_t *buf,
1396 unsigned int addr, unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001397{
1398 uint16_t hsfc;
1399 uint16_t timeout = 100 * 60;
1400 uint8_t block_len;
1401
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001402 if (addr + len > flash->chip->total_size * 1024) {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001403 msg_perr("Request to read from an inaccessible memory address "
1404 "(addr=0x%x, len=%d).\n", addr, len);
1405 return -1;
1406 }
1407
1408 msg_pdbg("Reading %d bytes starting at 0x%06x.\n", len, addr);
1409 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1410 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1411
1412 while (len > 0) {
Stefan Tauner7608d362014-08-05 23:28:47 +00001413 /* Obey programmer limit... */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001414 block_len = min(len, flash->mst->opaque.max_data_read);
Stefan Tauner7608d362014-08-05 23:28:47 +00001415 /* as well as flash chip page borders as demanded in the Intel datasheets. */
1416 block_len = min(block_len, 256 - (addr & 0xFF));
1417
Stefan Tauner50e7c602011-11-08 10:55:54 +00001418 ich_hwseq_set_addr(addr);
1419 hsfc = REGREAD16(ICH9_REG_HSFC);
Nico Huberd54e4f42017-03-23 23:45:47 +01001420 hsfc &= ~hwseq_data.hsfc_fcycle; /* set read operation */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001421 hsfc &= ~HSFC_FDBC; /* clear byte count */
1422 /* set byte count */
1423 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1424 hsfc |= HSFC_FGO; /* start */
1425 REGWRITE16(ICH9_REG_HSFC, hsfc);
1426
1427 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
1428 return 1;
1429 ich_read_data(buf, block_len, ICH9_REG_FDATA0);
1430 addr += block_len;
1431 buf += block_len;
1432 len -= block_len;
1433 }
1434 return 0;
1435}
1436
Mark Marshallf20b7be2014-05-09 21:16:21 +00001437static int ich_hwseq_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001438{
1439 uint16_t hsfc;
1440 uint16_t timeout = 100 * 60;
1441 uint8_t block_len;
1442
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001443 if (addr + len > flash->chip->total_size * 1024) {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001444 msg_perr("Request to write to an inaccessible memory address "
1445 "(addr=0x%x, len=%d).\n", addr, len);
1446 return -1;
1447 }
1448
1449 msg_pdbg("Writing %d bytes starting at 0x%06x.\n", len, addr);
1450 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1451 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1452
1453 while (len > 0) {
1454 ich_hwseq_set_addr(addr);
Stefan Tauner7608d362014-08-05 23:28:47 +00001455 /* Obey programmer limit... */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001456 block_len = min(len, flash->mst->opaque.max_data_write);
Stefan Tauner7608d362014-08-05 23:28:47 +00001457 /* as well as flash chip page borders as demanded in the Intel datasheets. */
1458 block_len = min(block_len, 256 - (addr & 0xFF));
Stefan Tauner50e7c602011-11-08 10:55:54 +00001459 ich_fill_data(buf, block_len, ICH9_REG_FDATA0);
1460 hsfc = REGREAD16(ICH9_REG_HSFC);
Nico Huberd54e4f42017-03-23 23:45:47 +01001461 hsfc &= ~hwseq_data.hsfc_fcycle; /* clear operation */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001462 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
1463 hsfc &= ~HSFC_FDBC; /* clear byte count */
1464 /* set byte count */
1465 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1466 hsfc |= HSFC_FGO; /* start */
1467 REGWRITE16(ICH9_REG_HSFC, hsfc);
1468
1469 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
1470 return -1;
1471 addr += block_len;
1472 buf += block_len;
1473 len -= block_len;
1474 }
1475 return 0;
1476}
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001477
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001478static int ich_spi_send_multicommand(struct flashctx *flash,
1479 struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001480{
1481 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001482 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001483 int oppos, preoppos;
1484 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001485 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001486 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001487 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1488 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001489 if ((oppos == -1) && (preoppos != -1)) {
1490 /* Current command is listed as preopcode in
1491 * ICH struct OPCODES, but next command is not
1492 * listed as opcode in that struct.
1493 * Check for command sanity, then
1494 * try to reprogram the ICH opcode list.
1495 */
1496 if (find_preop(curopcodes,
1497 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001498 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001499 "preopcodes 0x%02x and 0x%02x, "
1500 "ignoring the first.\n",
1501 __func__, cmds->writearr[0],
1502 (cmds + 1)->writearr[0]);
1503 continue;
1504 }
1505 /* If the chipset is locked down, we'll fail
1506 * during execution of the next command anyway.
1507 * No need to bother with fixups.
1508 */
1509 if (!ichspi_lock) {
Helge Wagner738e2522010-10-05 22:06:05 +00001510 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1511 if (oppos == -1)
1512 continue;
1513 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001514 continue;
1515 }
1516 }
1517 if ((oppos != -1) && (preoppos != -1)) {
1518 /* Current command is listed as preopcode in
1519 * ICH struct OPCODES and next command is listed
1520 * as opcode in that struct. Match them up.
1521 */
1522 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001523 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001524 }
1525 /* If none of the above if-statements about oppos or
1526 * preoppos matched, this is a normal opcode.
1527 */
1528 }
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001529 ret = ich_spi_send_command(flash, cmds->writecnt, cmds->readcnt,
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001530 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001531 /* Reset the type of all opcodes to non-atomic. */
1532 for (i = 0; i < 8; i++)
1533 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001534 }
1535 return ret;
1536}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001537
Michael Karchera4448d92010-07-22 18:04:15 +00001538#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1539#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1540#define ICH_BRWA(x) ((x >> 8) & 0xff)
1541#define ICH_BRRA(x) ((x >> 0) & 0xff)
1542
Stefan Tauner5210e722012-02-16 01:13:00 +00001543/* returns 0 if region is unused or r/w */
1544static int ich9_handle_frap(uint32_t frap, int i)
Michael Karchera4448d92010-07-22 18:04:15 +00001545{
Mathias Krausea60faab2011-01-17 07:50:42 +00001546 static const char *const access_names[4] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001547 "locked", "read-only", "write-only", "read-write"
1548 };
Nico Huberaa91d5c2017-08-19 17:04:21 +02001549 const int rwperms_unknown = ARRAY_SIZE(access_names);
Mathias Krausea60faab2011-01-17 07:50:42 +00001550 static const char *const region_names[5] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001551 "Flash Descriptor", "BIOS", "Management Engine",
1552 "Gigabit Ethernet", "Platform Data"
1553 };
Nico Huberd54e4f42017-03-23 23:45:47 +01001554 const char *const region_name = i < ARRAY_SIZE(region_names) ? region_names[i] : "unknown";
1555
Michael Karchera4448d92010-07-22 18:04:15 +00001556 uint32_t base, limit;
Nico Huberaa91d5c2017-08-19 17:04:21 +02001557 int rwperms;
Stefan Tauner29c80832011-06-12 08:14:10 +00001558 int offset = ICH9_REG_FREG0 + i * 4;
Michael Karchera4448d92010-07-22 18:04:15 +00001559 uint32_t freg = mmio_readl(ich_spibar + offset);
1560
Nico Huberaa91d5c2017-08-19 17:04:21 +02001561 if (i < 8) {
1562 rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1563 (((ICH_BRRA(frap) >> i) & 1) << 0);
1564 } else {
1565 /* Datasheets don't define any access bits for regions > 7. We
1566 can't rely on the actual descriptor settings either as there
1567 are several overrides for them (those by other masters are
1568 not even readable by us, *shrug*). */
1569 rwperms = rwperms_unknown;
1570 }
1571
Michael Karchera4448d92010-07-22 18:04:15 +00001572 base = ICH_FREG_BASE(freg);
1573 limit = ICH_FREG_LIMIT(freg);
Stefan Taunere3adea02012-08-27 15:12:36 +00001574 if (base > limit || (freg == 0 && i > 0)) {
Michael Karchera4448d92010-07-22 18:04:15 +00001575 /* this FREG is disabled */
Stefan Tauner5210e722012-02-16 01:13:00 +00001576 msg_pdbg2("0x%02X: 0x%08x FREG%i: %s region is unused.\n",
Nico Huberd54e4f42017-03-23 23:45:47 +01001577 offset, freg, i, region_name);
Stefan Tauner5210e722012-02-16 01:13:00 +00001578 return 0;
1579 }
1580 msg_pdbg("0x%02X: 0x%08x ", offset, freg);
1581 if (rwperms == 0x3) {
1582 msg_pdbg("FREG%i: %s region (0x%08x-0x%08x) is %s.\n", i,
Nico Huber0bb3f712017-03-29 16:44:33 +02001583 region_name, base, limit, access_names[rwperms]);
Stefan Tauner5210e722012-02-16 01:13:00 +00001584 return 0;
Michael Karchera4448d92010-07-22 18:04:15 +00001585 }
Nico Huberaa91d5c2017-08-19 17:04:21 +02001586 if (rwperms == rwperms_unknown) {
1587 msg_pdbg("FREG%i: %s region (0x%08x-0x%08x) has unknown permissions.\n",
1588 i, region_name, base, limit);
1589 return 0;
1590 }
Michael Karchera4448d92010-07-22 18:04:15 +00001591
Stefan Taunerc6fa32d2013-01-04 22:54:07 +00001592 msg_pwarn("FREG%i: Warning: %s region (0x%08x-0x%08x) is %s.\n", i,
Nico Huber0bb3f712017-03-29 16:44:33 +02001593 region_name, base, limit, access_names[rwperms]);
Stefan Tauner5210e722012-02-16 01:13:00 +00001594 return 1;
Michael Karchera4448d92010-07-22 18:04:15 +00001595}
1596
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001597 /* In contrast to FRAP and the master section of the descriptor the bits
1598 * in the PR registers have an inverted meaning. The bits in FRAP
1599 * indicate read and write access _grant_. Here they indicate read
1600 * and write _protection_ respectively. If both bits are 0 the address
1601 * bits are ignored.
1602 */
1603#define ICH_PR_PERMS(pr) (((~((pr) >> PR_RP_OFF) & 1) << 0) | \
1604 ((~((pr) >> PR_WP_OFF) & 1) << 1))
1605
Stefan Tauner5210e722012-02-16 01:13:00 +00001606/* returns 0 if range is unused (i.e. r/w) */
Nico Huberd54e4f42017-03-23 23:45:47 +01001607static int ich9_handle_pr(const size_t reg_pr0, int i)
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001608{
Stefan Tauner5210e722012-02-16 01:13:00 +00001609 static const char *const access_names[3] = {
1610 "locked", "read-only", "write-only"
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001611 };
Nico Huberd54e4f42017-03-23 23:45:47 +01001612 uint8_t off = reg_pr0 + (i * 4);
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001613 uint32_t pr = mmio_readl(ich_spibar + off);
Stefan Tauner5210e722012-02-16 01:13:00 +00001614 unsigned int rwperms = ICH_PR_PERMS(pr);
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001615
Nico Huberd54e4f42017-03-23 23:45:47 +01001616 /* From 5 on we have GPR registers and start from 0 again. */
1617 const char *const prefix = i >= 5 ? "G" : "";
1618 if (i >= 5)
1619 i -= 5;
1620
Stefan Tauner5210e722012-02-16 01:13:00 +00001621 if (rwperms == 0x3) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001622 msg_pdbg2("0x%02X: 0x%08x (%sPR%u is unused)\n", off, pr, prefix, i);
Stefan Tauner5210e722012-02-16 01:13:00 +00001623 return 0;
1624 }
1625
1626 msg_pdbg("0x%02X: 0x%08x ", off, pr);
Nico Huberd54e4f42017-03-23 23:45:47 +01001627 msg_pwarn("%sPR%u: Warning: 0x%08x-0x%08x is %s.\n", prefix, i, ICH_FREG_BASE(pr),
Nico Huber0bb3f712017-03-29 16:44:33 +02001628 ICH_FREG_LIMIT(pr), access_names[rwperms]);
Stefan Tauner5210e722012-02-16 01:13:00 +00001629 return 1;
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001630}
1631
Stefan Tauner75da80c2011-09-17 22:21:55 +00001632/* Set/Clear the read and write protection enable bits of PR register @i
1633 * according to @read_prot and @write_prot. */
Nico Huberd54e4f42017-03-23 23:45:47 +01001634static void ich9_set_pr(const size_t reg_pr0, int i, int read_prot, int write_prot)
Stefan Tauner75da80c2011-09-17 22:21:55 +00001635{
Nico Huberd54e4f42017-03-23 23:45:47 +01001636 void *addr = ich_spibar + reg_pr0 + (i * 4);
Stefan Tauner75da80c2011-09-17 22:21:55 +00001637 uint32_t old = mmio_readl(addr);
1638 uint32_t new;
1639
1640 msg_gspew("PR%u is 0x%08x", i, old);
1641 new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF));
1642 if (read_prot)
1643 new |= (1 << PR_RP_OFF);
1644 if (write_prot)
1645 new |= (1 << PR_WP_OFF);
1646 if (old == new) {
1647 msg_gspew(" already.\n");
1648 return;
1649 }
1650 msg_gspew(", trying to set it to 0x%08x ", new);
1651 rmmio_writel(new, addr);
1652 msg_gspew("resulted in 0x%08x.\n", mmio_readl(addr));
1653}
1654
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001655static const struct spi_master spi_master_ich7 = {
Michael Karcherb9dbe482011-05-11 17:07:07 +00001656 .type = SPI_CONTROLLER_ICH7,
1657 .max_data_read = 64,
1658 .max_data_write = 64,
1659 .command = ich_spi_send_command,
1660 .multicommand = ich_spi_send_multicommand,
1661 .read = default_spi_read,
1662 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +00001663 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001664};
1665
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001666static const struct spi_master spi_master_ich9 = {
Michael Karcherb9dbe482011-05-11 17:07:07 +00001667 .type = SPI_CONTROLLER_ICH9,
1668 .max_data_read = 64,
1669 .max_data_write = 64,
1670 .command = ich_spi_send_command,
1671 .multicommand = ich_spi_send_multicommand,
1672 .read = default_spi_read,
1673 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +00001674 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001675};
1676
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001677static const struct opaque_master opaque_master_ich_hwseq = {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001678 .max_data_read = 64,
1679 .max_data_write = 64,
1680 .probe = ich_hwseq_probe,
1681 .read = ich_hwseq_read,
1682 .write = ich_hwseq_write,
1683 .erase = ich_hwseq_block_erase,
1684};
1685
Nico Huber560111e2017-04-26 12:27:17 +02001686int ich_init_spi(void *spibar, enum ich_chipset ich_gen)
Michael Karchera4448d92010-07-22 18:04:15 +00001687{
1688 int i;
Stefan Tauner92d6a862013-10-25 00:33:37 +00001689 uint16_t tmp2;
Michael Karchera4448d92010-07-22 18:04:15 +00001690 uint32_t tmp;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001691 char *arg;
Stefan Tauner5210e722012-02-16 01:13:00 +00001692 int ich_spi_force = 0;
1693 int ich_spi_rw_restricted = 0;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001694 int desc_valid = 0;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001695 struct ich_descriptors desc = {{ 0 }};
1696 enum ich_spi_mode {
1697 ich_auto,
1698 ich_hwseq,
1699 ich_swseq
1700 } ich_spi_mode = ich_auto;
Nico Huberd54e4f42017-03-23 23:45:47 +01001701 size_t num_freg, num_pr, reg_pr0;
Michael Karchera4448d92010-07-22 18:04:15 +00001702
Stefan Taunera8d838d2011-11-06 23:51:09 +00001703 ich_generation = ich_gen;
Stefan Tauner92d6a862013-10-25 00:33:37 +00001704 ich_spibar = spibar;
Michael Karchera4448d92010-07-22 18:04:15 +00001705
Nico Huberd54e4f42017-03-23 23:45:47 +01001706 /* Moving registers / bits */
1707 if (ich_generation == CHIPSET_100_SERIES_SUNRISE_POINT) {
1708 num_freg = 10;
David Hendricksa5216362017-08-08 20:02:22 -07001709 num_pr = 6; /* Includes GPR0 */
1710 reg_pr0 = PCH100_REG_FPR0;
1711 swseq_data.reg_ssfsc = PCH100_REG_SSFSC;
1712 swseq_data.reg_preop = PCH100_REG_PREOP;
1713 swseq_data.reg_optype = PCH100_REG_OPTYPE;
1714 swseq_data.reg_opmenu = PCH100_REG_OPMENU;
1715 hwseq_data.addr_mask = PCH100_FADDR_FLA;
1716 hwseq_data.only_4k = true;
1717 hwseq_data.hsfc_fcycle = PCH100_HSFC_FCYCLE;
1718 } if (ich_generation == CHIPSET_C620_SERIES_LEWISBURG) {
1719 num_freg = 12; /* 12 MMIO regs, but 16 regions in FD spec */
1720 num_pr = 6; /* Includes GPR0 */
Nico Huberd54e4f42017-03-23 23:45:47 +01001721 reg_pr0 = PCH100_REG_FPR0;
1722 swseq_data.reg_ssfsc = PCH100_REG_SSFSC;
1723 swseq_data.reg_preop = PCH100_REG_PREOP;
1724 swseq_data.reg_optype = PCH100_REG_OPTYPE;
1725 swseq_data.reg_opmenu = PCH100_REG_OPMENU;
1726 hwseq_data.addr_mask = PCH100_FADDR_FLA;
1727 hwseq_data.only_4k = true;
1728 hwseq_data.hsfc_fcycle = PCH100_HSFC_FCYCLE;
1729 } else {
1730 num_freg = 5;
1731 num_pr = 5;
1732 reg_pr0 = ICH9_REG_PR0;
1733 swseq_data.reg_ssfsc = ICH9_REG_SSFS;
1734 swseq_data.reg_preop = ICH9_REG_PREOP;
1735 swseq_data.reg_optype = ICH9_REG_OPTYPE;
1736 swseq_data.reg_opmenu = ICH9_REG_OPMENU;
1737 hwseq_data.addr_mask = ICH9_FADDR_FLA;
1738 hwseq_data.only_4k = false;
1739 hwseq_data.hsfc_fcycle = HSFC_FCYCLE;
1740 }
1741
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001742 switch (ich_generation) {
Stefan Taunera8d838d2011-11-06 23:51:09 +00001743 case CHIPSET_ICH7:
Stefan Tauner92d6a862013-10-25 00:33:37 +00001744 case CHIPSET_TUNNEL_CREEK:
1745 case CHIPSET_CENTERTON:
Michael Karchera4448d92010-07-22 18:04:15 +00001746 msg_pdbg("0x00: 0x%04x (SPIS)\n",
1747 mmio_readw(ich_spibar + 0));
1748 msg_pdbg("0x02: 0x%04x (SPIC)\n",
1749 mmio_readw(ich_spibar + 2));
1750 msg_pdbg("0x04: 0x%08x (SPIA)\n",
1751 mmio_readl(ich_spibar + 4));
Michael Karchera4448d92010-07-22 18:04:15 +00001752 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1753 msg_pdbg("0x50: 0x%08x (BBAR)\n",
1754 ichspi_bbar);
1755 msg_pdbg("0x54: 0x%04x (PREOP)\n",
1756 mmio_readw(ich_spibar + 0x54));
1757 msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
1758 mmio_readw(ich_spibar + 0x56));
1759 msg_pdbg("0x58: 0x%08x (OPMENU)\n",
1760 mmio_readl(ich_spibar + 0x58));
1761 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
1762 mmio_readl(ich_spibar + 0x5c));
Stefan Tauner122dd122011-07-24 15:34:56 +00001763 for (i = 0; i < 3; i++) {
Michael Karchera4448d92010-07-22 18:04:15 +00001764 int offs;
1765 offs = 0x60 + (i * 4);
1766 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1767 mmio_readl(ich_spibar + offs), i);
1768 }
Michael Karchera4448d92010-07-22 18:04:15 +00001769 if (mmio_readw(ich_spibar) & (1 << 15)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +00001770 msg_pwarn("WARNING: SPI Configuration Lockdown activated.\n");
Michael Karchera4448d92010-07-22 18:04:15 +00001771 ichspi_lock = 1;
1772 }
Stefan Tauner745f6bb2011-11-13 15:17:10 +00001773 ich_init_opcodes();
Stefan Taunera8d838d2011-11-06 23:51:09 +00001774 ich_set_bbar(0);
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001775 register_spi_master(&spi_master_ich7);
Michael Karchera4448d92010-07-22 18:04:15 +00001776 break;
Stefan Taunera8d838d2011-11-06 23:51:09 +00001777 case CHIPSET_ICH8:
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001778 default: /* Future version might behave the same */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001779 arg = extract_programmer_param("ich_spi_mode");
1780 if (arg && !strcmp(arg, "hwseq")) {
1781 ich_spi_mode = ich_hwseq;
1782 msg_pspew("user selected hwseq\n");
1783 } else if (arg && !strcmp(arg, "swseq")) {
1784 ich_spi_mode = ich_swseq;
1785 msg_pspew("user selected swseq\n");
1786 } else if (arg && !strcmp(arg, "auto")) {
1787 msg_pspew("user selected auto\n");
1788 ich_spi_mode = ich_auto;
1789 } else if (arg && !strlen(arg)) {
1790 msg_perr("Missing argument for ich_spi_mode.\n");
1791 free(arg);
1792 return ERROR_FATAL;
1793 } else if (arg) {
1794 msg_perr("Unknown argument for ich_spi_mode: %s\n",
1795 arg);
1796 free(arg);
1797 return ERROR_FATAL;
1798 }
1799 free(arg);
1800
Stefan Tauner5210e722012-02-16 01:13:00 +00001801 arg = extract_programmer_param("ich_spi_force");
1802 if (arg && !strcmp(arg, "yes")) {
1803 ich_spi_force = 1;
1804 msg_pspew("ich_spi_force enabled.\n");
1805 } else if (arg && !strlen(arg)) {
1806 msg_perr("Missing argument for ich_spi_force.\n");
1807 free(arg);
1808 return ERROR_FATAL;
1809 } else if (arg) {
1810 msg_perr("Unknown argument for ich_spi_force: \"%s\" "
1811 "(not \"yes\").\n", arg);
1812 free(arg);
1813 return ERROR_FATAL;
1814 }
1815 free(arg);
1816
Stefan Tauner29c80832011-06-12 08:14:10 +00001817 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
Michael Karchera4448d92010-07-22 18:04:15 +00001818 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
Stefan Tauner55206942011-06-11 09:53:22 +00001819 prettyprint_ich9_reg_hsfs(tmp2);
Stefan Tauner29c80832011-06-12 08:14:10 +00001820 if (tmp2 & HSFS_FLOCKDN) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +00001821 msg_pwarn("Warning: SPI Configuration Lockdown activated.\n");
Stefan Tauner55206942011-06-11 09:53:22 +00001822 ichspi_lock = 1;
1823 }
Stefan Tauner1e146392011-09-15 23:52:55 +00001824 if (tmp2 & HSFS_FDV)
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001825 desc_valid = 1;
1826 if (!(tmp2 & HSFS_FDOPSS) && desc_valid)
Stefan Taunerd7d423b2012-10-20 09:13:16 +00001827 msg_pinfo("The Flash Descriptor Override Strap-Pin is set. Restrictions implied by\n"
1828 "the Master Section of the flash descriptor are NOT in effect. Please note\n"
1829 "that Protected Range (PR) restrictions still apply.\n");
Stefan Tauner745f6bb2011-11-13 15:17:10 +00001830 ich_init_opcodes();
Stefan Tauner55206942011-06-11 09:53:22 +00001831
Stefan Taunerf382e352011-11-08 11:55:24 +00001832 if (desc_valid) {
1833 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
1834 msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
1835 prettyprint_ich9_reg_hsfc(tmp2);
1836 }
Michael Karchera4448d92010-07-22 18:04:15 +00001837
Stefan Tauner5ffe65b2011-07-07 04:10:57 +00001838 tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
Stefan Taunereb582572012-09-21 12:52:50 +00001839 msg_pdbg2("0x08: 0x%08x (FADDR)\n", tmp);
Michael Karchera4448d92010-07-22 18:04:15 +00001840
David Hendricksa5216362017-08-08 20:02:22 -07001841 if (ich_gen == CHIPSET_100_SERIES_SUNRISE_POINT || ich_gen == CHIPSET_C620_SERIES_LEWISBURG) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001842 const uint32_t dlock = mmio_readl(ich_spibar + PCH100_REG_DLOCK);
1843 msg_pdbg("0x0c: 0x%08x (DLOCK)\n", dlock);
1844 prettyprint_pch100_reg_dlock(dlock);
1845 }
1846
Stefan Taunerf382e352011-11-08 11:55:24 +00001847 if (desc_valid) {
1848 tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
1849 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1850 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1851 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1852 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1853 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1854
Stefan Tauner5210e722012-02-16 01:13:00 +00001855 /* Handle FREGx and FRAP registers */
Nico Huberd54e4f42017-03-23 23:45:47 +01001856 for (i = 0; i < num_freg; i++)
Stefan Tauner5210e722012-02-16 01:13:00 +00001857 ich_spi_rw_restricted |= ich9_handle_frap(tmp, i);
Stefan Tauner27cb34b2013-06-01 00:06:12 +00001858 if (ich_spi_rw_restricted)
1859 msg_pwarn("Not all flash regions are freely accessible by flashrom. This is "
Stefan Tauner4c723152016-01-14 22:47:55 +00001860 "most likely\ndue to an active ME. Please see "
1861 "https://flashrom.org/ME for details.\n");
Stefan Taunerf382e352011-11-08 11:55:24 +00001862 }
Michael Karchera4448d92010-07-22 18:04:15 +00001863
Stefan Taunereb582572012-09-21 12:52:50 +00001864 /* Handle PR registers */
Nico Huberd54e4f42017-03-23 23:45:47 +01001865 for (i = 0; i < num_pr; i++) {
Stefan Tauner5210e722012-02-16 01:13:00 +00001866 /* if not locked down try to disable PR locks first */
1867 if (!ichspi_lock)
Nico Huberd54e4f42017-03-23 23:45:47 +01001868 ich9_set_pr(reg_pr0, i, 0, 0);
1869 ich_spi_rw_restricted |= ich9_handle_pr(reg_pr0, i);
Stefan Tauner5210e722012-02-16 01:13:00 +00001870 }
1871
1872 if (ich_spi_rw_restricted) {
Stefan Tauner5210e722012-02-16 01:13:00 +00001873 if (!ich_spi_force)
1874 programmer_may_write = 0;
Stefan Taunerd7d423b2012-10-20 09:13:16 +00001875 msg_pinfo("Writes have been disabled for safety reasons. You can enforce write\n"
1876 "support with the ich_spi_force programmer option, but you will most likely\n"
1877 "harm your hardware! If you force flashrom you will get no support if\n"
1878 "something breaks. On a few mainboards it is possible to enable write\n"
1879 "access by setting a jumper (see its documentation or the board itself).\n");
Stefan Tauner5210e722012-02-16 01:13:00 +00001880 if (ich_spi_force)
Stefan Taunerd7d423b2012-10-20 09:13:16 +00001881 msg_pinfo("Continuing with write support because the user forced us to!\n");
Stefan Tauner5210e722012-02-16 01:13:00 +00001882 }
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001883
Nico Huberd54e4f42017-03-23 23:45:47 +01001884 tmp = mmio_readl(ich_spibar + swseq_data.reg_ssfsc);
1885 msg_pdbg("0x%zx: 0x%02x (SSFS)\n", swseq_data.reg_ssfsc, tmp & 0xff);
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001886 prettyprint_ich9_reg_ssfs(tmp);
Stefan Tauner29c80832011-06-12 08:14:10 +00001887 if (tmp & SSFS_FCERR) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001888 msg_pdbg("Clearing SSFS.FCERR\n");
Nico Huberd54e4f42017-03-23 23:45:47 +01001889 mmio_writeb(SSFS_FCERR, ich_spibar + swseq_data.reg_ssfsc);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001890 }
Nico Huberd54e4f42017-03-23 23:45:47 +01001891 msg_pdbg("0x%zx: 0x%06x (SSFC)\n", swseq_data.reg_ssfsc + 1, tmp >> 8);
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001892 prettyprint_ich9_reg_ssfc(tmp);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001893
Nico Huberd54e4f42017-03-23 23:45:47 +01001894 msg_pdbg("0x%zx: 0x%04x (PREOP)\n",
1895 swseq_data.reg_preop, mmio_readw(ich_spibar + swseq_data.reg_preop));
1896 msg_pdbg("0x%zx: 0x%04x (OPTYPE)\n",
1897 swseq_data.reg_optype, mmio_readw(ich_spibar + swseq_data.reg_optype));
1898 msg_pdbg("0x%zx: 0x%08x (OPMENU)\n",
1899 swseq_data.reg_opmenu, mmio_readl(ich_spibar + swseq_data.reg_opmenu));
1900 msg_pdbg("0x%zx: 0x%08x (OPMENU+4)\n",
1901 swseq_data.reg_opmenu + 4, mmio_readl(ich_spibar + swseq_data.reg_opmenu + 4));
Stefan Taunerf382e352011-11-08 11:55:24 +00001902 if (ich_generation == CHIPSET_ICH8 && desc_valid) {
Stefan Tauner1e146392011-09-15 23:52:55 +00001903 tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC);
1904 msg_pdbg("0xC1: 0x%08x (VSCC)\n", tmp);
1905 msg_pdbg("VSCC: ");
Nico Huberd152fb92017-06-19 12:57:10 +02001906 prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, true);
David Hendricksa5216362017-08-08 20:02:22 -07001907 } else if (ich_generation != CHIPSET_100_SERIES_SUNRISE_POINT &&
1908 ich_generation != CHIPSET_C620_SERIES_LEWISBURG) {
Duncan Laurie4095ed72014-08-20 15:39:32 +00001909 if (ich_generation != CHIPSET_BAYTRAIL && desc_valid) {
1910 ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
1911 msg_pdbg("0xA0: 0x%08x (BBAR)\n",
1912 ichspi_bbar);
1913 ich_set_bbar(0);
1914 }
Stefan Taunerbd649e42011-07-01 00:39:16 +00001915
Stefan Taunerf382e352011-11-08 11:55:24 +00001916 if (desc_valid) {
1917 tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC);
1918 msg_pdbg("0xC4: 0x%08x (LVSCC)\n", tmp);
1919 msg_pdbg("LVSCC: ");
Nico Huberd152fb92017-06-19 12:57:10 +02001920 prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, true);
Stefan Tauner1e146392011-09-15 23:52:55 +00001921
Stefan Taunerf382e352011-11-08 11:55:24 +00001922 tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC);
1923 msg_pdbg("0xC8: 0x%08x (UVSCC)\n", tmp);
1924 msg_pdbg("UVSCC: ");
Nico Huberd152fb92017-06-19 12:57:10 +02001925 prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, false);
Stefan Tauner1e146392011-09-15 23:52:55 +00001926
Stefan Taunerf382e352011-11-08 11:55:24 +00001927 tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
1928 msg_pdbg("0xD0: 0x%08x (FPB)\n", tmp);
1929 }
Stefan Tauner1e146392011-09-15 23:52:55 +00001930 }
1931
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001932 if (desc_valid) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001933 if (read_ich_descriptors_via_fdo(ich_gen, ich_spibar, &desc) == ICH_RET_OK)
Stefan Tauner2ba9f6e2014-08-20 15:39:19 +00001934 prettyprint_ich_descriptors(ich_gen, &desc);
1935
Stefan Tauner50e7c602011-11-08 10:55:54 +00001936 /* If the descriptor is valid and indicates multiple
1937 * flash devices we need to use hwseq to be able to
1938 * access the second flash device.
1939 */
1940 if (ich_spi_mode == ich_auto && desc.content.NC != 0) {
1941 msg_pinfo("Enabling hardware sequencing due to "
1942 "multiple flash chips detected.\n");
1943 ich_spi_mode = ich_hwseq;
1944 }
Stefan Tauner1e146392011-09-15 23:52:55 +00001945 }
Stefan Tauner50e7c602011-11-08 10:55:54 +00001946
1947 if (ich_spi_mode == ich_auto && ichspi_lock &&
1948 ich_missing_opcodes()) {
1949 msg_pinfo("Enabling hardware sequencing because "
1950 "some important opcode is locked.\n");
1951 ich_spi_mode = ich_hwseq;
1952 }
1953
1954 if (ich_spi_mode == ich_hwseq) {
1955 if (!desc_valid) {
1956 msg_perr("Hardware sequencing was requested "
1957 "but the flash descriptor is not "
1958 "valid. Aborting.\n");
1959 return ERROR_FATAL;
1960 }
Stefan Tauner2ba9f6e2014-08-20 15:39:19 +00001961
1962 int tmpi = getFCBA_component_density(ich_generation, &desc, 0);
1963 if (tmpi < 0) {
1964 msg_perr("Could not determine density of flash component %d.\n", 0);
1965 return ERROR_FATAL;
1966 }
1967 hwseq_data.size_comp0 = tmpi;
1968
1969 tmpi = getFCBA_component_density(ich_generation, &desc, 1);
1970 if (tmpi < 0) {
1971 msg_perr("Could not determine density of flash component %d.\n", 1);
1972 return ERROR_FATAL;
1973 }
1974 hwseq_data.size_comp1 = tmpi;
1975
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001976 register_opaque_master(&opaque_master_ich_hwseq);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001977 } else {
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001978 register_spi_master(&spi_master_ich9);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001979 }
Michael Karchera4448d92010-07-22 18:04:15 +00001980 break;
Michael Karchera4448d92010-07-22 18:04:15 +00001981 }
1982
Michael Karchera4448d92010-07-22 18:04:15 +00001983 return 0;
1984}
1985
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001986static const struct spi_master spi_master_via = {
Michael Karcherb9dbe482011-05-11 17:07:07 +00001987 .type = SPI_CONTROLLER_VIA,
1988 .max_data_read = 16,
1989 .max_data_write = 16,
1990 .command = ich_spi_send_command,
1991 .multicommand = ich_spi_send_multicommand,
1992 .read = default_spi_read,
1993 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +00001994 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001995};
1996
Nico Huber560111e2017-04-26 12:27:17 +02001997int via_init_spi(uint32_t mmio_base)
Michael Karchera4448d92010-07-22 18:04:15 +00001998{
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001999 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00002000
Stefan Tauner7fb5aa02013-08-14 15:48:44 +00002001 ich_spibar = rphysmap("VIA SPI MMIO registers", mmio_base, 0x70);
2002 if (ich_spibar == ERROR_PTR)
2003 return ERROR_FATAL;
Helge Wagnerdd73d832012-08-24 23:03:46 +00002004 /* Do we really need no write enable? Like the LPC one at D17F0 0x40 */
Michael Karchera4448d92010-07-22 18:04:15 +00002005
Michael Karchera4448d92010-07-22 18:04:15 +00002006 /* Not sure if it speaks all these bus protocols. */
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +00002007 internal_buses_supported = BUS_LPC | BUS_FWH;
Stefan Taunera8d838d2011-11-06 23:51:09 +00002008 ich_generation = CHIPSET_ICH7;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00002009 register_spi_master(&spi_master_via);
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00002010
2011 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
2012 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
2013 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
2014 for (i = 0; i < 2; i++) {
2015 int offs;
2016 offs = 8 + (i * 8);
2017 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
2018 mmio_readl(ich_spibar + offs), i);
2019 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
2020 mmio_readl(ich_spibar + offs + 4), i);
2021 }
2022 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
2023 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
2024 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
2025 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
2026 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
2027 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
2028 for (i = 0; i < 3; i++) {
2029 int offs;
2030 offs = 0x60 + (i * 4);
2031 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
2032 mmio_readl(ich_spibar + offs), i);
2033 }
2034 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
2035 mmio_readw(ich_spibar + 0x6c));
2036 if (mmio_readw(ich_spibar) & (1 << 15)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +00002037 msg_pwarn("Warning: SPI Configuration Lockdown activated.\n");
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00002038 ichspi_lock = 1;
2039 }
2040
Stefan Taunera8d838d2011-11-06 23:51:09 +00002041 ich_set_bbar(0);
Michael Karchera4448d92010-07-22 18:04:15 +00002042 ich_init_opcodes();
2043
2044 return 0;
2045}
2046
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00002047#endif