blob: 9a5f1b793b6ecafff62b921953e197bc11f234a0 [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.
Dominik Geyerb46acba2008-05-16 12:55:55 +000020 */
21
Dominik Geyerb46acba2008-05-16 12:55:55 +000022#include <string.h>
Felix Singer8cfc7372022-08-19 03:10:29 +020023#include <stdbool.h>
Stefan Taunerd0c5dc22011-10-20 12:57:14 +000024#include <stdlib.h>
Dominik Geyerb46acba2008-05-16 12:55:55 +000025#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000026#include "programmer.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010027#include "hwaccess_physmap.h"
Nico Huberd5185632024-01-05 18:44:41 +010028#include "spi_command.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000029#include "spi.h"
Stefan Tauner1e146392011-09-15 23:52:55 +000030#include "ich_descriptors.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000031
Nico Huber42daab12024-07-16 00:27:27 +020032/* New since C740 series Emmitsburg */
33#define BIOS_BM_RAP 0x118
34#define BIOS_BM_WAP 0x11c
35
Nico Huberd2d39932019-01-18 16:49:37 +010036/* Apollo Lake */
37#define APL_REG_FREG12 0xe0 /* 32 Bytes Flash Region 12 */
38
Nico Huberd54e4f42017-03-23 23:45:47 +010039/* Sunrise Point */
40
41/* Added HSFS Status bits */
42#define HSFS_WRSDIS_OFF 11 /* 11: Flash Configuration Lock-Down */
43#define HSFS_WRSDIS (0x1 << HSFS_WRSDIS_OFF)
44#define HSFS_PRR34_LOCKDN_OFF 12 /* 12: PRR3 PRR4 Lock-Down */
45#define HSFS_PRR34_LOCKDN (0x1 << HSFS_PRR34_LOCKDN_OFF)
46/* HSFS_BERASE vanished */
47
48/*
49 * HSFC and HSFS 16-bit registers are combined into the 32-bit
50 * BIOS_HSFSTS_CTL register in the Sunrise Point datasheet,
51 * however we still treat them separately in order to reuse code.
52 */
53
54/* Changed HSFC Control bits */
55#define PCH100_HSFC_FCYCLE_OFF (17 - 16) /* 1-4: FLASH Cycle */
56#define PCH100_HSFC_FCYCLE (0xf << PCH100_HSFC_FCYCLE_OFF)
57/* New HSFC Control bit */
58#define HSFC_WET_OFF (21 - 16) /* 5: Write Enable Type */
59#define HSFC_WET (0x1 << HSFC_WET_OFF)
60
61#define PCH100_FADDR_FLA 0x07ffffff
62
63#define PCH100_REG_DLOCK 0x0c /* 32 Bits Discrete Lock Bits */
64#define DLOCK_BMWAG_LOCKDN_OFF 0
65#define DLOCK_BMWAG_LOCKDN (0x1 << DLOCK_BMWAG_LOCKDN_OFF)
66#define DLOCK_BMRAG_LOCKDN_OFF 1
67#define DLOCK_BMRAG_LOCKDN (0x1 << DLOCK_BMRAG_LOCKDN_OFF)
68#define DLOCK_SBMWAG_LOCKDN_OFF 2
69#define DLOCK_SBMWAG_LOCKDN (0x1 << DLOCK_SBMWAG_LOCKDN_OFF)
70#define DLOCK_SBMRAG_LOCKDN_OFF 3
71#define DLOCK_SBMRAG_LOCKDN (0x1 << DLOCK_SBMRAG_LOCKDN_OFF)
72#define DLOCK_PR0_LOCKDN_OFF 8
73#define DLOCK_PR0_LOCKDN (0x1 << DLOCK_PR0_LOCKDN_OFF)
74#define DLOCK_PR1_LOCKDN_OFF 9
75#define DLOCK_PR1_LOCKDN (0x1 << DLOCK_PR1_LOCKDN_OFF)
76#define DLOCK_PR2_LOCKDN_OFF 10
77#define DLOCK_PR2_LOCKDN (0x1 << DLOCK_PR2_LOCKDN_OFF)
78#define DLOCK_PR3_LOCKDN_OFF 11
79#define DLOCK_PR3_LOCKDN (0x1 << DLOCK_PR3_LOCKDN_OFF)
80#define DLOCK_PR4_LOCKDN_OFF 12
81#define DLOCK_PR4_LOCKDN (0x1 << DLOCK_PR4_LOCKDN_OFF)
82#define DLOCK_SSEQ_LOCKDN_OFF 16
83#define DLOCK_SSEQ_LOCKDN (0x1 << DLOCK_SSEQ_LOCKDN_OFF)
84
85#define PCH100_REG_FPR0 0x84 /* 32 Bits Protected Range 0 */
86#define PCH100_REG_GPR0 0x98 /* 32 Bits Global Protected Range 0 */
87
88#define PCH100_REG_SSFSC 0xA0 /* 32 Bits Status (8) + Control (24) */
89#define PCH100_REG_PREOP 0xA4 /* 16 Bits */
90#define PCH100_REG_OPTYPE 0xA6 /* 16 Bits */
91#define PCH100_REG_OPMENU 0xA8 /* 64 Bits */
92
Stefan Reinauera9424d52008-06-27 16:28:34 +000093/* ICH9 controller register definition */
Stefan Tauner55206942011-06-11 09:53:22 +000094#define ICH9_REG_HSFS 0x04 /* 16 Bits Hardware Sequencing Flash Status */
95#define HSFS_FDONE_OFF 0 /* 0: Flash Cycle Done */
96#define HSFS_FDONE (0x1 << HSFS_FDONE_OFF)
97#define HSFS_FCERR_OFF 1 /* 1: Flash Cycle Error */
98#define HSFS_FCERR (0x1 << HSFS_FCERR_OFF)
99#define HSFS_AEL_OFF 2 /* 2: Access Error Log */
100#define HSFS_AEL (0x1 << HSFS_AEL_OFF)
101#define HSFS_BERASE_OFF 3 /* 3-4: Block/Sector Erase Size */
102#define HSFS_BERASE (0x3 << HSFS_BERASE_OFF)
103#define HSFS_SCIP_OFF 5 /* 5: SPI Cycle In Progress */
104#define HSFS_SCIP (0x1 << HSFS_SCIP_OFF)
105 /* 6-12: reserved */
106#define HSFS_FDOPSS_OFF 13 /* 13: Flash Descriptor Override Pin-Strap Status */
107#define HSFS_FDOPSS (0x1 << HSFS_FDOPSS_OFF)
108#define HSFS_FDV_OFF 14 /* 14: Flash Descriptor Valid */
109#define HSFS_FDV (0x1 << HSFS_FDV_OFF)
110#define HSFS_FLOCKDN_OFF 15 /* 15: Flash Configuration Lock-Down */
111#define HSFS_FLOCKDN (0x1 << HSFS_FLOCKDN_OFF)
112
113#define ICH9_REG_HSFC 0x06 /* 16 Bits Hardware Sequencing Flash Control */
114#define HSFC_FGO_OFF 0 /* 0: Flash Cycle Go */
115#define HSFC_FGO (0x1 << HSFC_FGO_OFF)
116#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
117#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
118 /* 3-7: reserved */
119#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
120#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
121 /* 14: reserved */
122#define HSFC_SME_OFF 15 /* 15: SPI SMI# Enable */
123#define HSFC_SME (0x1 << HSFC_SME_OFF)
124
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000125#define ICH9_REG_FADDR 0x08 /* 32 Bits */
Nico Huberd54e4f42017-03-23 23:45:47 +0100126#define ICH9_FADDR_FLA 0x01ffffff
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000127#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000128
Stefan Tauner29c80832011-06-12 08:14:10 +0000129#define ICH9_REG_FRAP 0x50 /* 32 Bytes Flash Region Access Permissions */
130#define ICH9_REG_FREG0 0x54 /* 32 Bytes Flash Region 0 */
131
132#define ICH9_REG_PR0 0x74 /* 32 Bytes Protected Range 0 */
Stefan Taunerbf69aaa2011-09-17 21:21:48 +0000133#define PR_WP_OFF 31 /* 31: write protection enable */
134#define PR_RP_OFF 15 /* 15: read protection enable */
Stefan Tauner29c80832011-06-12 08:14:10 +0000135
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000136#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000137#define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */
138#define SSFS_SCIP (0x1 << SSFS_SCIP_OFF)
139#define SSFS_FDONE_OFF 2 /* Cycle Done Status */
140#define SSFS_FDONE (0x1 << SSFS_FDONE_OFF)
141#define SSFS_FCERR_OFF 3 /* Flash Cycle Error */
142#define SSFS_FCERR (0x1 << SSFS_FCERR_OFF)
143#define SSFS_AEL_OFF 4 /* Access Error Log */
144#define SSFS_AEL (0x1 << SSFS_AEL_OFF)
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000145/* The following bits are reserved in SSFS: 1,5-7. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000146#define SSFS_RESERVED_MASK 0x000000e2
Stefan Reinauera9424d52008-06-27 16:28:34 +0000147
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000148#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000149/* We combine SSFS and SSFC to one 32-bit word,
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000150 * therefore SSFC bits are off by 8. */
151 /* 0: reserved */
152#define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */
153#define SSFC_SCGO (0x1 << SSFC_SCGO_OFF)
154#define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */
155#define SSFC_ACS (0x1 << SSFC_ACS_OFF)
156#define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */
157#define SSFC_SPOP (0x1 << SSFC_SPOP_OFF)
158#define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */
159#define SSFC_COP (0x7 << SSFC_COP_OFF)
160 /* 7: reserved */
161#define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */
162#define SSFC_DBC (0x3f << SSFC_DBC_OFF)
163#define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */
164#define SSFC_DS (0x1 << SSFC_DS_OFF)
165#define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */
166#define SSFC_SME (0x1 << SSFC_SME_OFF)
167#define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */
168#define SSFC_SCF (0x7 << SSFC_SCF_OFF)
169#define SSFC_SCF_20MHZ 0x00000000
170#define SSFC_SCF_33MHZ 0x01000000
171 /* 19-23: reserved */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000172#define SSFC_RESERVED_MASK 0xf8008100
Stefan Reinauera9424d52008-06-27 16:28:34 +0000173
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000174#define ICH9_REG_PREOP 0x94 /* 16 Bits */
175#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
176#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000177
Stefan Tauner29c80832011-06-12 08:14:10 +0000178#define ICH9_REG_BBAR 0xA0 /* 32 Bits BIOS Base Address Configuration */
179#define BBAR_MASK 0x00ffff00 /* 8-23: Bottom of System Flash */
180
Stefan Tauner1e146392011-09-15 23:52:55 +0000181#define ICH8_REG_VSCC 0xC1 /* 32 Bits Vendor Specific Component Capabilities */
182#define ICH9_REG_LVSCC 0xC4 /* 32 Bits Host Lower Vendor Specific Component Capabilities */
183#define ICH9_REG_UVSCC 0xC8 /* 32 Bits Host Upper Vendor Specific Component Capabilities */
184/* The individual fields of the VSCC registers are defined in the file
185 * ich_descriptors.h. The reason is that the same layout is also used in the
186 * flash descriptor to define the properties of the different flash chips
187 * supported. The BIOS (or the ME?) is responsible to populate the ICH registers
188 * with the information from the descriptor on startup depending on the actual
189 * chip(s) detected. */
190
Stefan Taunerbd649e42011-07-01 00:39:16 +0000191#define ICH9_REG_FPB 0xD0 /* 32 Bits Flash Partition Boundary */
192#define FPB_FPBA_OFF 0 /* 0-12: Block/Sector Erase Size */
193#define FPB_FPBA (0x1FFF << FPB_FPBA_OFF)
194
Dominik Geyerb46acba2008-05-16 12:55:55 +0000195// ICH9R SPI commands
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000196#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
197#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
198#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
199#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
Dominik Geyerb46acba2008-05-16 12:55:55 +0000200
Stefan Reinauera9424d52008-06-27 16:28:34 +0000201// ICH7 registers
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000202#define ICH7_REG_SPIS 0x00 /* 16 Bits */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000203#define SPIS_SCIP 0x0001
204#define SPIS_GRANT 0x0002
205#define SPIS_CDS 0x0004
206#define SPIS_FCERR 0x0008
207#define SPIS_RESERVED_MASK 0x7ff0
Stefan Reinauera9424d52008-06-27 16:28:34 +0000208
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000209/* VIA SPI is compatible with ICH7, but maxdata
210 to transfer is 16 bytes.
211
212 DATA byte count on ICH7 is 8:13, on VIA 8:11
213
214 bit 12 is port select CS0 CS1
215 bit 13 is FAST READ enable
216 bit 7 is used with fast read and one shot controls CS de-assert?
217*/
218
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000219#define ICH7_REG_SPIC 0x02 /* 16 Bits */
220#define SPIC_SCGO 0x0002
221#define SPIC_ACS 0x0004
222#define SPIC_SPOP 0x0008
223#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000224
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000225#define ICH7_REG_SPIA 0x04 /* 32 Bits */
226#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
227#define ICH7_REG_PREOP 0x54 /* 16 Bits */
228#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
229#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000230
Nico Huber7590d1a2016-05-03 13:38:28 +0200231enum ich_access_protection {
232 NO_PROT = 0,
233 READ_PROT = 1,
234 WRITE_PROT = 2,
235 LOCKED = 3,
236};
237
FENG yu ningc05a2952008-12-08 18:16:58 +0000238/* ICH SPI configuration lock-down. May be set during chipset enabling. */
Felix Singer8cfc7372022-08-19 03:10:29 +0200239static bool ichspi_lock = false;
FENG yu ningc05a2952008-12-08 18:16:58 +0000240
Stefan Taunera8d838d2011-11-06 23:51:09 +0000241static enum ich_chipset ich_generation = CHIPSET_ICH_UNKNOWN;
Nico Hubered098d62017-04-21 23:47:08 +0200242static uint32_t ichspi_bbar;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000243
Michael Karchera4448d92010-07-22 18:04:15 +0000244static void *ich_spibar = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000245
Dominik Geyerb46acba2008-05-16 12:55:55 +0000246typedef struct _OPCODE {
247 uint8_t opcode; //This commands spi opcode
248 uint8_t spi_type; //This commands spi type
249 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
250} OPCODE;
251
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000252/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000253 * Preop 1: Write Enable
254 * Preop 2: Write Status register enable
255 *
256 * OP 0: Write address
257 * OP 1: Read Address
258 * OP 2: ERASE block
259 * OP 3: Read Status register
260 * OP 4: Read ID
261 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000262 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000263 * OP 7: Chip erase
264 */
265typedef struct _OPCODES {
266 uint8_t preop[2];
267 OPCODE opcode[8];
268} OPCODES;
269
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000270static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000271
272/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000273static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000274{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000275 return mmio_readl(ich_spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000276}
277
Uwe Hermann09e04f72009-05-16 22:36:00 +0000278static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000279{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000280 return mmio_readw(ich_spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000281}
282
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000283static uint16_t REGREAD8(int X)
284{
285 return mmio_readb(ich_spibar + X);
286}
287
Stefan Taunerccd92a12011-07-01 00:39:01 +0000288#define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
289#define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
290#define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off))
Dominik Geyerb46acba2008-05-16 12:55:55 +0000291
Dominik Geyerb46acba2008-05-16 12:55:55 +0000292/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000293static int find_opcode(OPCODES *op, uint8_t opcode);
294static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000295static int generate_opcodes(OPCODES * op);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000296static int program_opcodes(OPCODES *op, int enable_undo);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000297static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000298 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000299
FENG yu ningf041e9b2008-12-15 02:32:11 +0000300/* for pairing opcodes with their required preop */
301struct preop_opcode_pair {
302 uint8_t preop;
303 uint8_t opcode;
304};
305
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000306/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000307const struct preop_opcode_pair pops[] = {
FENG yu ningf041e9b2008-12-15 02:32:11 +0000308 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
309 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
310 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
311 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
312 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
313 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000314 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
315 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000316 {JEDEC_EWSR, JEDEC_WRSR},
317 {0,}
318};
319
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000320/* Reasonable default configuration. Needs ad-hoc modifications if we
321 * encounter unlisted opcodes. Fun.
322 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000323static OPCODES O_ST_M25P = {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000324 {
325 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000326 JEDEC_EWSR,
327 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000328 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000329 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000330 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
David Hendricks15f539c2010-08-26 21:27:17 -0700331 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000332 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000333 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000334 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000335 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000336 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
337 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000338};
339
Helge Wagner738e2522010-10-05 22:06:05 +0000340/* List of opcodes with their corresponding spi_type
341 * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
342 * is needed which is currently not in the chipset OPCODE table
343 */
344static OPCODE POSSIBLE_OPCODES[] = {
345 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
346 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
347 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
348 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
349 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
350 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
351 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
352 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
353 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
354 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
355 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
356};
357
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000358static OPCODES O_EXISTING = {};
FENG yu ningc05a2952008-12-08 18:16:58 +0000359
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000360/* pretty printing functions */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000361static void prettyprint_opcodes(OPCODES *ops)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000362{
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000363 OPCODE oc;
364 const char *t;
365 const char *a;
366 uint8_t i;
367 static const char *const spi_type[4] = {
368 "read w/o addr",
369 "write w/o addr",
370 "read w/ addr",
371 "write w/ addr"
372 };
373 static const char *const atomic_type[3] = {
374 "none",
375 " 0 ",
376 " 1 "
377 };
378
379 if (ops == NULL)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000380 return;
381
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000382 msg_pdbg2(" OP Type Pre-OP\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000383 for (i = 0; i < 8; i++) {
384 oc = ops->opcode[i];
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000385 t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type];
386 a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic];
387 msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000388 }
Stefan Tauner84e1dde2011-09-17 19:53:11 +0000389 msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0],
390 ops->preop[1]);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000391}
392
Nico Huberd54e4f42017-03-23 23:45:47 +0100393#define _pprint_reg(bit, mask, off, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & mask) >> off)
394#define pprint_reg(reg, bit, val, sep) _pprint_reg(bit, reg##_##bit, reg##_##bit##_OFF, val, sep)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000395
Stefan Tauner55206942011-06-11 09:53:22 +0000396static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
397{
398 msg_pdbg("HSFS: ");
399 pprint_reg(HSFS, FDONE, reg_val, ", ");
400 pprint_reg(HSFS, FCERR, reg_val, ", ");
401 pprint_reg(HSFS, AEL, reg_val, ", ");
Nico Hubereeee91b2024-07-14 21:12:21 +0200402 if (ich_generation < SPI_ENGINE_PCH100)
Nico Huberd54e4f42017-03-23 23:45:47 +0100403 pprint_reg(HSFS, BERASE, reg_val, ", ");
Stefan Tauner55206942011-06-11 09:53:22 +0000404 pprint_reg(HSFS, SCIP, reg_val, ", ");
Nico Hubereeee91b2024-07-14 21:12:21 +0200405 if (ich_generation >= SPI_ENGINE_PCH100) {
Nico Huberd54e4f42017-03-23 23:45:47 +0100406 pprint_reg(HSFS, PRR34_LOCKDN, reg_val, ", ");
407 pprint_reg(HSFS, WRSDIS, reg_val, ", ");
408 }
Stefan Tauner55206942011-06-11 09:53:22 +0000409 pprint_reg(HSFS, FDOPSS, reg_val, ", ");
410 pprint_reg(HSFS, FDV, reg_val, ", ");
411 pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
412}
413
414static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
415{
416 msg_pdbg("HSFC: ");
417 pprint_reg(HSFC, FGO, reg_val, ", ");
Nico Hubereeee91b2024-07-14 21:12:21 +0200418 if (ich_generation >= SPI_ENGINE_PCH100) {
Nico Huberd54e4f42017-03-23 23:45:47 +0100419 _pprint_reg(HSFC, PCH100_HSFC_FCYCLE, PCH100_HSFC_FCYCLE_OFF, reg_val, ", ");
420 pprint_reg(HSFC, WET, reg_val, ", ");
Nico Hubereeee91b2024-07-14 21:12:21 +0200421 } else {
Nico Huber2a5dfaf2019-07-04 16:01:51 +0200422 pprint_reg(HSFC, FCYCLE, reg_val, ", ");
Nico Huberd54e4f42017-03-23 23:45:47 +0100423 }
Stefan Tauner55206942011-06-11 09:53:22 +0000424 pprint_reg(HSFC, FDBC, reg_val, ", ");
425 pprint_reg(HSFC, SME, reg_val, "\n");
426}
427
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000428static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
429{
430 msg_pdbg("SSFS: ");
431 pprint_reg(SSFS, SCIP, reg_val, ", ");
432 pprint_reg(SSFS, FDONE, reg_val, ", ");
433 pprint_reg(SSFS, FCERR, reg_val, ", ");
434 pprint_reg(SSFS, AEL, reg_val, "\n");
435}
436
437static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
438{
439 msg_pdbg("SSFC: ");
440 pprint_reg(SSFC, SCGO, reg_val, ", ");
441 pprint_reg(SSFC, ACS, reg_val, ", ");
442 pprint_reg(SSFC, SPOP, reg_val, ", ");
443 pprint_reg(SSFC, COP, reg_val, ", ");
444 pprint_reg(SSFC, DBC, reg_val, ", ");
445 pprint_reg(SSFC, SME, reg_val, ", ");
446 pprint_reg(SSFC, SCF, reg_val, "\n");
447}
448
Nico Huberd54e4f42017-03-23 23:45:47 +0100449static void prettyprint_pch100_reg_dlock(const uint32_t reg_val)
450{
451 msg_pdbg("DLOCK: ");
452 pprint_reg(DLOCK, BMWAG_LOCKDN, reg_val, ", ");
453 pprint_reg(DLOCK, BMRAG_LOCKDN, reg_val, ", ");
454 pprint_reg(DLOCK, SBMWAG_LOCKDN, reg_val, ", ");
455 pprint_reg(DLOCK, SBMRAG_LOCKDN, reg_val, ",\n ");
456 pprint_reg(DLOCK, PR0_LOCKDN, reg_val, ", ");
457 pprint_reg(DLOCK, PR1_LOCKDN, reg_val, ", ");
458 pprint_reg(DLOCK, PR2_LOCKDN, reg_val, ", ");
459 pprint_reg(DLOCK, PR3_LOCKDN, reg_val, ", ");
460 pprint_reg(DLOCK, PR4_LOCKDN, reg_val, ",\n ");
461 pprint_reg(DLOCK, SSEQ_LOCKDN, reg_val, "\n");
462}
463
464static struct {
465 size_t reg_ssfsc;
466 size_t reg_preop;
467 size_t reg_optype;
468 size_t reg_opmenu;
469} swseq_data;
470
Helge Wagner738e2522010-10-05 22:06:05 +0000471static uint8_t lookup_spi_type(uint8_t opcode)
472{
Nico Huber519be662018-12-23 20:03:35 +0100473 unsigned int a;
Helge Wagner738e2522010-10-05 22:06:05 +0000474
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000475 for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
Helge Wagner738e2522010-10-05 22:06:05 +0000476 if (POSSIBLE_OPCODES[a].opcode == opcode)
477 return POSSIBLE_OPCODES[a].spi_type;
478 }
479
480 return 0xFF;
481}
482
483static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
484{
485 uint8_t spi_type;
486
487 spi_type = lookup_spi_type(opcode);
488 if (spi_type > 3) {
489 /* Try to guess spi type from read/write sizes.
490 * The following valid writecnt/readcnt combinations exist:
491 * writecnt = 4, readcnt >= 0
492 * writecnt = 1, readcnt >= 0
493 * writecnt >= 4, readcnt = 0
494 * writecnt >= 1, readcnt = 0
495 * writecnt >= 1 is guaranteed for all commands.
496 */
497 if (readcnt == 0)
498 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
499 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
500 * bytes are actual the address, they go to the bus anyhow
501 */
502 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
503 else if (writecnt == 1) // and readcnt is > 0
504 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
505 else if (writecnt == 4) // and readcnt is > 0
506 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
Stefan Taunerdc704ed2012-05-06 15:11:26 +0000507 else // we have an invalid case
508 return SPI_INVALID_LENGTH;
Helge Wagner738e2522010-10-05 22:06:05 +0000509 }
Stefan Taunerdc704ed2012-05-06 15:11:26 +0000510 int oppos = 2; // use original JEDEC_BE_D8 offset
511 curopcodes->opcode[oppos].opcode = opcode;
512 curopcodes->opcode[oppos].spi_type = spi_type;
513 program_opcodes(curopcodes, 0);
514 oppos = find_opcode(curopcodes, opcode);
Stefan Taunerd94d25d2012-07-28 03:17:15 +0000515 msg_pdbg2("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
Stefan Taunerdc704ed2012-05-06 15:11:26 +0000516 return oppos;
Helge Wagner738e2522010-10-05 22:06:05 +0000517}
518
Uwe Hermann09e04f72009-05-16 22:36:00 +0000519static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000520{
521 int a;
522
Stefan Tauner50e7c602011-11-08 10:55:54 +0000523 if (op == NULL) {
524 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
525 return -1;
526 }
527
FENG yu ningc05a2952008-12-08 18:16:58 +0000528 for (a = 0; a < 8; a++) {
529 if (op->opcode[a].opcode == opcode)
530 return a;
531 }
532
533 return -1;
534}
535
Uwe Hermann09e04f72009-05-16 22:36:00 +0000536static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000537{
538 int a;
539
Stefan Tauner50e7c602011-11-08 10:55:54 +0000540 if (op == NULL) {
541 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
542 return -1;
543 }
544
FENG yu ningc05a2952008-12-08 18:16:58 +0000545 for (a = 0; a < 2; a++) {
546 if (op->preop[a] == preop)
547 return a;
548 }
549
550 return -1;
551}
552
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000553/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000554static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000555{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000556 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000557 uint16_t preop, optype;
558 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000559
560 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000561 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000562 return -1;
563 }
564
Nico Huber869f0e72024-07-14 22:58:39 +0200565 preop = REGREAD16(swseq_data.reg_preop);
566 optype = REGREAD16(swseq_data.reg_optype);
567 opmenu[0] = REGREAD32(swseq_data.reg_opmenu);
568 opmenu[1] = REGREAD32(swseq_data.reg_opmenu + 4);
FENG yu ningc05a2952008-12-08 18:16:58 +0000569
570 op->preop[0] = (uint8_t) preop;
571 op->preop[1] = (uint8_t) (preop >> 8);
572
573 for (a = 0; a < 8; a++) {
574 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
575 optype >>= 2;
576 }
577
578 for (a = 0; a < 4; a++) {
579 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
580 opmenu[0] >>= 8;
581 }
582
583 for (a = 4; a < 8; a++) {
584 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
585 opmenu[1] >>= 8;
586 }
587
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000588 /* No preopcodes used by default. */
589 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000590 op->opcode[a].atomic = 0;
591
FENG yu ningc05a2952008-12-08 18:16:58 +0000592 return 0;
593}
594
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000595static int program_opcodes(OPCODES *op, int enable_undo)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000596{
597 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000598 uint16_t preop, optype;
599 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000600
601 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000602 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000603 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000604 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000605 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000606
Stefan Reinauera9424d52008-06-27 16:28:34 +0000607 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000608 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000609 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000610 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000611 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000612
Stefan Reinauera9424d52008-06-27 16:28:34 +0000613 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000614 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000615 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000616 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000617 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000618
Stefan Tauner92d6a862013-10-25 00:33:37 +0000619 /* Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000620 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000621 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000622 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000623 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000624
Stefan Taunerd94d25d2012-07-28 03:17:15 +0000625 msg_pdbg2("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
Nico Huber869f0e72024-07-14 22:58:39 +0200626
627 /* Register undo only for enable_undo=1, i.e. first call. */
628 if (enable_undo) {
629 rmmio_valw(ich_spibar + swseq_data.reg_preop);
630 rmmio_valw(ich_spibar + swseq_data.reg_optype);
631 rmmio_vall(ich_spibar + swseq_data.reg_opmenu);
632 rmmio_vall(ich_spibar + swseq_data.reg_opmenu + 4);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000633 }
Nico Huber869f0e72024-07-14 22:58:39 +0200634 mmio_writew(preop, ich_spibar + swseq_data.reg_preop);
635 mmio_writew(optype, ich_spibar + swseq_data.reg_optype);
636 mmio_writel(opmenu[0], ich_spibar + swseq_data.reg_opmenu);
637 mmio_writel(opmenu[1], ich_spibar + swseq_data.reg_opmenu + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000638
639 return 0;
640}
641
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000642/*
Stefan Tauner50e7c602011-11-08 10:55:54 +0000643 * Returns -1 if at least one mandatory opcode is inaccessible, 0 otherwise.
644 * FIXME: this should also check for
645 * - at least one probing opcode (RDID (incl. AT25F variants?), REMS, RES?)
646 * - at least one erasing opcode (lots.)
647 * - at least one program opcode (BYTE_PROGRAM, AAI_WORD_PROGRAM, ...?)
648 * - necessary preops? (EWSR, WREN, ...?)
649 */
Richard Hughes93e16252018-12-19 11:54:47 +0000650static int ich_missing_opcodes(void)
Stefan Tauner50e7c602011-11-08 10:55:54 +0000651{
652 uint8_t ops[] = {
653 JEDEC_READ,
654 JEDEC_RDSR,
655 0
656 };
657 int i = 0;
658 while (ops[i] != 0) {
659 msg_pspew("checking for opcode 0x%02x\n", ops[i]);
660 if (find_opcode(curopcodes, ops[i]) == -1)
661 return -1;
662 i++;
663 }
664 return 0;
665}
666
667/*
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000668 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
669 * it didn't stick.
670 */
Stefan Taunera8d838d2011-11-06 23:51:09 +0000671static void ich_set_bbar(uint32_t min_addr)
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000672{
Nico Hubereeee91b2024-07-14 21:12:21 +0200673 const int bbar_off = ich_generation < SPI_ENGINE_ICH9 ? 0x50 : ICH9_REG_BBAR;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200674
Stefan Taunere27b2d42011-07-01 00:39:09 +0000675 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
676 if (ichspi_bbar) {
677 msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
678 ichspi_bbar);
679 }
680 min_addr &= BBAR_MASK;
681 ichspi_bbar |= min_addr;
682 rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
683 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
684
685 /* We don't have any option except complaining. And if the write
686 * failed, the restore will fail as well, so no problem there.
687 */
688 if (ichspi_bbar != min_addr)
Stefan Tauner7783f312011-09-17 21:21:42 +0000689 msg_perr("Setting BBAR to 0x%08x failed! New value: 0x%08x.\n",
690 min_addr, ichspi_bbar);
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000691}
692
Stefan Tauner8b391b82011-08-09 01:49:34 +0000693/* Read len bytes from the fdata/spid register into the data array.
694 *
Nico Huber9a11cbf2023-01-13 01:19:07 +0100695 * Note that using len > flash->mst.spi->max_data_read will return garbage or
Stefan Tauner8b391b82011-08-09 01:49:34 +0000696 * may even crash.
697 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000698static void ich_read_data(uint8_t *data, int len, int reg0_off)
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200699{
Stefan Tauner8b391b82011-08-09 01:49:34 +0000700 int i;
701 uint32_t temp32 = 0;
702
703 for (i = 0; i < len; i++) {
704 if ((i % 4) == 0)
705 temp32 = REGREAD32(reg0_off + i);
706
707 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
708 }
709}
710
711/* Fill len bytes from the data array into the fdata/spid registers.
712 *
Nico Huber9a11cbf2023-01-13 01:19:07 +0100713 * Note that using len > flash->mst.spi->max_data_write will trash the registers
Stefan Tauner8b391b82011-08-09 01:49:34 +0000714 * following the data registers.
715 */
716static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
717{
718 uint32_t temp32 = 0;
719 int i;
720
721 if (len <= 0)
722 return;
723
724 for (i = 0; i < len; i++) {
725 if ((i % 4) == 0)
726 temp32 = 0;
727
728 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
729
730 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
731 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
732 }
733 i--;
734 if ((i % 4) != 3) /* Write remaining data to regs. */
735 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
736}
737
FENG yu ningf041e9b2008-12-15 02:32:11 +0000738/* This function generates OPCODES from or programs OPCODES to ICH according to
739 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000740 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000741 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000742 */
Michael Karchera4448d92010-07-22 18:04:15 +0000743static int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000744{
745 int rc = 0;
746 OPCODES *curopcodes_done;
747
748 if (curopcodes)
749 return 0;
750
751 if (ichspi_lock) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000752 msg_pdbg("Reading OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000753 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000754 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000755 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000756 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000757 curopcodes_done = &O_ST_M25P;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000758 rc = program_opcodes(curopcodes_done, 1);
FENG yu ningc05a2952008-12-08 18:16:58 +0000759 }
760
761 if (rc) {
762 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000763 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000764 return 1;
765 } else {
766 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000767 msg_pdbg("done\n");
Stefan Tauner8b391b82011-08-09 01:49:34 +0000768 prettyprint_opcodes(curopcodes);
FENG yu ningc05a2952008-12-08 18:16:58 +0000769 return 0;
770 }
771}
772
Stefan Reinauer43119562008-11-02 19:51:50 +0000773static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000774 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000775{
Felix Singer8cfc7372022-08-19 03:10:29 +0200776 bool write_cmd = false;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000777 int timeout;
Stefan Tauner8b391b82011-08-09 01:49:34 +0000778 uint32_t temp32;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000779 uint16_t temp16;
Stefan Reinauer43119562008-11-02 19:51:50 +0000780 uint64_t opmenu;
781 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000782
783 /* Is it a write command? */
784 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
785 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
Felix Singer8cfc7372022-08-19 03:10:29 +0200786 write_cmd = true;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000787 }
788
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000789 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
790 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
791 programmer_delay(10);
792 }
793 if (!timeout) {
794 msg_perr("Error: SCIP never cleared!\n");
795 return 1;
796 }
797
Stefan Tauner10b3e222011-07-01 00:39:23 +0000798 /* Program offset in flash into SPIA while preserving reserved bits. */
799 temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
800 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000801
Stefan Tauner10b3e222011-07-01 00:39:23 +0000802 /* Program data into SPID0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000803 if (write_cmd && (datalength != 0))
804 ich_fill_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000805
806 /* Assemble SPIS */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000807 temp16 = REGREAD16(ICH7_REG_SPIS);
808 /* keep reserved bits */
809 temp16 &= SPIS_RESERVED_MASK;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000810 /* clear error status registers */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000811 temp16 |= (SPIS_CDS | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000812 REGWRITE16(ICH7_REG_SPIS, temp16);
813
814 /* Assemble SPIC */
815 temp16 = 0;
816
817 if (datalength != 0) {
818 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000819 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000820 }
821
822 /* Select opcode */
Nico Huber869f0e72024-07-14 22:58:39 +0200823 opmenu = REGREAD32(swseq_data.reg_opmenu);
824 opmenu |= ((uint64_t)REGREAD32(swseq_data.reg_opmenu + 4)) << 32;
Stefan Reinauer43119562008-11-02 19:51:50 +0000825
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000826 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
827 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000828 break;
829 }
830 opmenu >>= 8;
831 }
832 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000833 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000834 return 1;
835 }
836 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000837
Michael Karcher136125a2011-04-29 22:11:36 +0000838 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
839 /* Handle Atomic. Atomic commands include three steps:
840 - sending the preop (mainly EWSR or WREN)
841 - sending the main command
842 - waiting for the busy bit (WIP) to be cleared
843 This means the timeout must be sufficient for chip erase
844 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000845 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000846 switch (op.atomic) {
847 case 2:
848 /* Select second preop. */
849 temp16 |= SPIC_SPOP;
Richard Hughesdb7482b2018-12-19 12:04:30 +0000850 /* Fall through. */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000851 case 1:
852 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000853 temp16 |= SPIC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000854 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000855 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000856 }
857
858 /* Start */
859 temp16 |= SPIC_SCGO;
860
861 /* write it */
862 REGWRITE16(ICH7_REG_SPIC, temp16);
863
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000864 /* Wait for Cycle Done Status or Flash Cycle Error. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000865 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
866 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000867 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000868 }
869 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000870 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
871 REGREAD16(ICH7_REG_SPIS));
872 return 1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000873 }
874
Sean Nelson316a29f2010-05-07 20:09:04 +0000875 /* FIXME: make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000876 temp16 = REGREAD16(ICH7_REG_SPIS);
877 if (temp16 & SPIS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000878 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000879 /* keep reserved bits */
880 temp16 &= SPIS_RESERVED_MASK;
881 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000882 return 1;
883 }
884
Stefan Tauner8b391b82011-08-09 01:49:34 +0000885 if ((!write_cmd) && (datalength != 0))
886 ich_read_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000887
888 return 0;
889}
890
Stefan Reinauer43119562008-11-02 19:51:50 +0000891static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000892 uint8_t datalength, uint8_t * data)
893{
Felix Singer8cfc7372022-08-19 03:10:29 +0200894 bool write_cmd = false;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000895 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000896 uint32_t temp32;
Stefan Reinauer43119562008-11-02 19:51:50 +0000897 uint64_t opmenu;
898 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000899
900 /* Is it a write command? */
901 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
902 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
Felix Singer8cfc7372022-08-19 03:10:29 +0200903 write_cmd = true;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000904 }
905
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000906 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
Nico Huberd54e4f42017-03-23 23:45:47 +0100907 while ((REGREAD8(swseq_data.reg_ssfsc) & SSFS_SCIP) && --timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000908 programmer_delay(10);
909 }
910 if (!timeout) {
911 msg_perr("Error: SCIP never cleared!\n");
912 return 1;
913 }
914
Stefan Tauner10b3e222011-07-01 00:39:23 +0000915 /* Program offset in flash into FADDR while preserve the reserved bits
Martin Rothf6c1cb12022-03-15 10:55:25 -0600916 * and clearing the 25. address bit which is only usable in hwseq. */
Stefan Tauner10b3e222011-07-01 00:39:23 +0000917 temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
918 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000919
920 /* Program data into FDATA0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000921 if (write_cmd && (datalength != 0))
922 ich_fill_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000923
924 /* Assemble SSFS + SSFC */
Nico Huberd54e4f42017-03-23 23:45:47 +0100925 temp32 = REGREAD32(swseq_data.reg_ssfsc);
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000926 /* Keep reserved bits only */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000927 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000928 /* Clear cycle done and cycle error status registers */
929 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Nico Huberd54e4f42017-03-23 23:45:47 +0100930 REGWRITE32(swseq_data.reg_ssfsc, temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000931
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000932 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000933 temp32 |= SSFC_SCF_20MHZ;
934
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000935 /* Set data byte count (DBC) and data cycle bit (DS) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000936 if (datalength != 0) {
937 uint32_t datatemp;
938 temp32 |= SSFC_DS;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000939 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
940 SSFC_DBC);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000941 temp32 |= datatemp;
942 }
943
944 /* Select opcode */
Nico Huber8b2152d2017-08-31 13:18:49 +0200945 opmenu = REGREAD32(swseq_data.reg_opmenu);
946 opmenu |= ((uint64_t)REGREAD32(swseq_data.reg_opmenu + 4)) << 32;
Stefan Reinauer43119562008-11-02 19:51:50 +0000947
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000948 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
949 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000950 break;
951 }
952 opmenu >>= 8;
953 }
954 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000955 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000956 return 1;
957 }
958 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000959
Michael Karcher136125a2011-04-29 22:11:36 +0000960 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
961 /* Handle Atomic. Atomic commands include three steps:
962 - sending the preop (mainly EWSR or WREN)
963 - sending the main command
964 - waiting for the busy bit (WIP) to be cleared
965 This means the timeout must be sufficient for chip erase
966 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000967 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000968 switch (op.atomic) {
969 case 2:
970 /* Select second preop. */
971 temp32 |= SSFC_SPOP;
Richard Hughesdb7482b2018-12-19 12:04:30 +0000972 /* Fall through. */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000973 case 1:
974 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000975 temp32 |= SSFC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000976 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000977 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000978 }
979
980 /* Start */
981 temp32 |= SSFC_SCGO;
982
983 /* write it */
Nico Huberd54e4f42017-03-23 23:45:47 +0100984 REGWRITE32(swseq_data.reg_ssfsc, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000985
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000986 /* Wait for Cycle Done Status or Flash Cycle Error. */
Nico Huberd54e4f42017-03-23 23:45:47 +0100987 while (((REGREAD32(swseq_data.reg_ssfsc) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000988 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000989 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000990 }
991 if (!timeout) {
Nico Huberd54e4f42017-03-23 23:45:47 +0100992 msg_perr("timeout, REG_SSFS=0x%08x\n",
993 REGREAD32(swseq_data.reg_ssfsc));
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000994 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000995 }
996
Sean Nelson316a29f2010-05-07 20:09:04 +0000997 /* FIXME make sure we do not needlessly cause transaction errors. */
Nico Huberd54e4f42017-03-23 23:45:47 +0100998 temp32 = REGREAD32(swseq_data.reg_ssfsc);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000999 if (temp32 & SSFS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001000 msg_perr("Transaction error!\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001001 prettyprint_ich9_reg_ssfs(temp32);
1002 prettyprint_ich9_reg_ssfc(temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001003 /* keep reserved bits */
1004 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
1005 /* Clear the transaction error. */
Nico Huberd54e4f42017-03-23 23:45:47 +01001006 REGWRITE32(swseq_data.reg_ssfsc, temp32 | SSFS_FCERR);
Dominik Geyerb46acba2008-05-16 12:55:55 +00001007 return 1;
1008 }
1009
Stefan Tauner8b391b82011-08-09 01:49:34 +00001010 if ((!write_cmd) && (datalength != 0))
1011 ich_read_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +00001012
1013 return 0;
1014}
1015
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001016static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +00001017 uint8_t datalength, uint8_t * data)
1018{
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001019 /* max_data_read == max_data_write for all Intel/VIA SPI masters */
Nico Huber9a11cbf2023-01-13 01:19:07 +01001020 uint8_t maxlength = flash->mst.spi->max_data_read;
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001021
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00001022 if (ich_generation == CHIPSET_ICH_UNKNOWN) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001023 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001024 return -1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +00001025 }
Stefan Reinauera9424d52008-06-27 16:28:34 +00001026
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001027 if (datalength > maxlength) {
1028 msg_perr("%s: Internal command size error for "
1029 "opcode 0x%02x, got datalength=%i, want <=%i\n",
1030 __func__, op.opcode, datalength, maxlength);
1031 return SPI_INVALID_LENGTH;
1032 }
1033
Nico Hubereeee91b2024-07-14 21:12:21 +02001034 if (ich_generation < SPI_ENGINE_ICH9)
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001035 return ich7_run_opcode(op, offset, datalength, data, maxlength);
Nico Hubereeee91b2024-07-14 21:12:21 +02001036 else
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001037 return ich9_run_opcode(op, offset, datalength, data);
Stefan Reinauera9424d52008-06-27 16:28:34 +00001038}
1039
Edward O'Callaghan5eca4272020-04-12 17:27:53 +10001040static int ich_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001041 unsigned int readcnt,
1042 const unsigned char *writearr,
1043 unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +00001044{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001045 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001046 int opcode_index = -1;
1047 const unsigned char cmd = *writearr;
1048 OPCODE *opcode;
1049 uint32_t addr = 0;
1050 uint8_t *data;
1051 int count;
1052
Dominik Geyerb46acba2008-05-16 12:55:55 +00001053 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001054 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +00001055 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +00001056 if (!ichspi_lock)
1057 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
Stefan Taunerdc704ed2012-05-06 15:11:26 +00001058 if (opcode_index == SPI_INVALID_LENGTH) {
1059 msg_pdbg("OPCODE 0x%02x has unsupported length, will not execute.\n", cmd);
1060 return SPI_INVALID_LENGTH;
1061 } else if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001062 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
1063 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +00001064 return SPI_INVALID_OPCODE;
1065 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001066 }
1067
1068 opcode = &(curopcodes->opcode[opcode_index]);
1069
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001070 /* The following valid writecnt/readcnt combinations exist:
1071 * writecnt = 4, readcnt >= 0
1072 * writecnt = 1, readcnt >= 0
1073 * writecnt >= 4, readcnt = 0
1074 * writecnt >= 1, readcnt = 0
1075 * writecnt >= 1 is guaranteed for all commands.
1076 */
1077 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
1078 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001079 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001080 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
1081 writecnt);
1082 return SPI_INVALID_LENGTH;
1083 }
1084 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
1085 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001086 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001087 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
1088 writecnt);
1089 return SPI_INVALID_LENGTH;
1090 }
1091 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1092 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001093 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001094 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
1095 writecnt);
1096 return SPI_INVALID_LENGTH;
1097 }
1098 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1099 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1100 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001101 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001102 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1103 readcnt);
1104 return SPI_INVALID_LENGTH;
1105 }
1106
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001107 /* Translate read/write array/count.
1108 * The maximum data length is identical for the maximum read length and
1109 * for the maximum write length excluding opcode and address. Opcode and
1110 * address are stored in separate registers, not in the data registers
1111 * and are thus not counted towards data length. The only exception
1112 * applies if the opcode definition (un)intentionally classifies said
1113 * opcode incorrectly as non-address opcode or vice versa. */
Dominik Geyerb46acba2008-05-16 12:55:55 +00001114 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001115 data = (uint8_t *) (writearr + 1);
1116 count = writecnt - 1;
1117 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1118 data = (uint8_t *) (writearr + 4);
1119 count = writecnt - 4;
1120 } else {
1121 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001122 count = readcnt;
1123 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001124
Nico Hubered098d62017-04-21 23:47:08 +02001125 /* if opcode-type requires an address */
1126 if (cmd == JEDEC_REMS || cmd == JEDEC_RES) {
1127 addr = ichspi_bbar;
1128 } else if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1129 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1130 /* BBAR may cut part of the chip off at the lower end. */
1131 const uint32_t valid_base = ichspi_bbar & ((flash->chip->total_size * 1024) - 1);
1132 const uint32_t addr_offset = ichspi_bbar - valid_base;
1133 /* Highest address we can program is (2^24 - 1). */
1134 const uint32_t valid_end = (1 << 24) - addr_offset;
1135
1136 addr = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
1137 const uint32_t addr_end = addr + count;
1138
1139 if (addr < valid_base ||
1140 addr_end < addr || /* integer overflow check */
1141 addr_end > valid_end) {
1142 msg_perr("%s: Addressed region 0x%06x-0x%06x not in allowed range 0x%06x-0x%06x\n",
1143 __func__, addr, addr_end - 1, valid_base, valid_end - 1);
1144 return SPI_INVALID_ADDRESS;
1145 }
1146 addr += addr_offset;
1147 }
1148
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001149 result = run_opcode(flash, *opcode, addr, count, data);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001150 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001151 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1152 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1153 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1154 msg_pdbg("at address 0x%06x ", addr);
1155 }
1156 msg_pdbg("(payload length was %d).\n", count);
1157
1158 /* Print out the data array if it contains data to write.
1159 * Errors are detected before the received data is read back into
1160 * the array so it won't make sense to print it then. */
1161 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1162 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1163 int i;
1164 msg_pspew("The data was:\n");
Stefan Taunerf382e352011-11-08 11:55:24 +00001165 for (i = 0; i < count; i++){
Stefan Tauner8ed29342011-04-29 23:53:09 +00001166 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1167 }
1168 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001169 }
1170
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001171 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001172}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001173
Stefan Tauner50e7c602011-11-08 10:55:54 +00001174static struct hwseq_data {
1175 uint32_t size_comp0;
1176 uint32_t size_comp1;
Nico Huberd54e4f42017-03-23 23:45:47 +01001177 uint32_t addr_mask;
1178 bool only_4k;
1179 uint32_t hsfc_fcycle;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001180} hwseq_data;
1181
Nico Huberd54e4f42017-03-23 23:45:47 +01001182/* Sets FLA in FADDR to (addr & hwseq_data.addr_mask) without touching other bits. */
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001183static void ich_hwseq_set_addr(uint32_t addr)
1184{
Nico Huberd54e4f42017-03-23 23:45:47 +01001185 uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~hwseq_data.addr_mask;
1186 REGWRITE32(ICH9_REG_FADDR, (addr & hwseq_data.addr_mask) | addr_old);
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001187}
1188
1189/* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
1190 * of the block containing this address. May return nonsense if the address is
1191 * not valid. The erase block size for a specific address depends on the flash
1192 * partition layout as specified by FPB and the partition properties as defined
1193 * by UVSCC and LVSCC respectively. An alternative to implement this method
1194 * would be by querying FPB and the respective VSCC register directly.
1195 */
1196static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr)
1197{
Elyes HAOUAS29e46d02019-06-09 17:38:25 +02001198 uint8_t enc_berase;
1199 static const uint32_t dec_berase[4] = {
1200 256,
1201 4 * 1024,
1202 8 * 1024,
1203 64 * 1024
1204 };
1205
Nico Huberd54e4f42017-03-23 23:45:47 +01001206 if (hwseq_data.only_4k) {
1207 return 4 * 1024;
Nico Huberd54e4f42017-03-23 23:45:47 +01001208 }
Elyes HAOUAS29e46d02019-06-09 17:38:25 +02001209
1210 ich_hwseq_set_addr(addr);
1211 enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >> HSFS_BERASE_OFF;
1212 return dec_berase[enc_berase];
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001213}
1214
1215/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
1216 Resets all error flags in HSFS.
1217 Returns 0 if the cycle completes successfully without errors within
1218 timeout us, 1 on errors. */
Subrata Banik7cb43952022-03-16 20:40:42 +05301219static int ich_hwseq_wait_for_cycle_complete(unsigned int len)
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001220{
Subrata Banik7cb43952022-03-16 20:40:42 +05301221 /*
1222 * The SPI bus may be busy due to performing operations from other masters, hence
1223 * introduce the long timeout of 30s to cover the worst case scenarios as well.
1224 */
1225 unsigned int timeout_us = 30 * 1000 * 1000;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001226 uint16_t hsfs;
1227 uint32_t addr;
1228
Subrata Banik7cb43952022-03-16 20:40:42 +05301229 timeout_us /= 8; /* scale timeout duration to counter */
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001230 while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) &
1231 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
Subrata Banik7cb43952022-03-16 20:40:42 +05301232 --timeout_us) {
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001233 programmer_delay(8);
1234 }
1235 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
Subrata Banik7cb43952022-03-16 20:40:42 +05301236 if (!timeout_us) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001237 addr = REGREAD32(ICH9_REG_FADDR) & hwseq_data.addr_mask;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001238 msg_perr("Timeout error between offset 0x%08x and "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001239 "0x%08x (= 0x%08x + %d)!\n",
1240 addr, addr + len - 1, addr, len - 1);
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001241 prettyprint_ich9_reg_hsfs(hsfs);
1242 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1243 return 1;
1244 }
1245
1246 if (hsfs & HSFS_FCERR) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001247 addr = REGREAD32(ICH9_REG_FADDR) & hwseq_data.addr_mask;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001248 msg_perr("Transaction error between offset 0x%08x and "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001249 "0x%08x (= 0x%08x + %d)!\n",
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001250 addr, addr + len - 1, addr, len - 1);
1251 prettyprint_ich9_reg_hsfs(hsfs);
1252 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1253 return 1;
1254 }
1255 return 0;
1256}
Stefan Tauner50e7c602011-11-08 10:55:54 +00001257
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001258static int ich_hwseq_probe(struct flashctx *flash)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001259{
1260 uint32_t total_size, boundary;
1261 uint32_t erase_size_low, size_low, erase_size_high, size_high;
1262 struct block_eraser *eraser;
1263
1264 total_size = hwseq_data.size_comp0 + hwseq_data.size_comp1;
Stefan Tauner5c316f92015-02-08 21:57:52 +00001265 msg_cdbg("Hardware sequencing reports %d attached SPI flash chip",
Stefan Tauner50e7c602011-11-08 10:55:54 +00001266 (hwseq_data.size_comp1 != 0) ? 2 : 1);
1267 if (hwseq_data.size_comp1 != 0)
1268 msg_cdbg("s with a combined");
1269 else
1270 msg_cdbg(" with a");
1271 msg_cdbg(" density of %d kB.\n", total_size / 1024);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001272 flash->chip->total_size = total_size / 1024;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001273
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001274 eraser = &(flash->chip->block_erasers[0]);
Nico Huberd54e4f42017-03-23 23:45:47 +01001275 if (!hwseq_data.only_4k)
1276 boundary = (REGREAD32(ICH9_REG_FPB) & FPB_FPBA) << 12;
1277 else
1278 boundary = 0;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001279 size_high = total_size - boundary;
1280 erase_size_high = ich_hwseq_get_erase_block_size(boundary);
1281
1282 if (boundary == 0) {
Stefan Tauner5c316f92015-02-08 21:57:52 +00001283 msg_cdbg2("There is only one partition containing the whole "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001284 "address space (0x%06x - 0x%06x).\n", 0, size_high-1);
1285 eraser->eraseblocks[0].size = erase_size_high;
1286 eraser->eraseblocks[0].count = size_high / erase_size_high;
Stefan Tauner5c316f92015-02-08 21:57:52 +00001287 msg_cdbg2("There are %d erase blocks with %d B each.\n",
Stefan Tauner50e7c602011-11-08 10:55:54 +00001288 size_high / erase_size_high, erase_size_high);
1289 } else {
Stefan Tauner5c316f92015-02-08 21:57:52 +00001290 msg_cdbg2("The flash address space (0x%06x - 0x%06x) is divided "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001291 "at address 0x%06x in two partitions.\n",
Stefan Taunerdbac46c2013-08-13 22:10:41 +00001292 0, total_size-1, boundary);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001293 size_low = total_size - size_high;
1294 erase_size_low = ich_hwseq_get_erase_block_size(0);
1295
1296 eraser->eraseblocks[0].size = erase_size_low;
1297 eraser->eraseblocks[0].count = size_low / erase_size_low;
1298 msg_cdbg("The first partition ranges from 0x%06x to 0x%06x.\n",
1299 0, size_low-1);
1300 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1301 size_low / erase_size_low, erase_size_low);
1302
1303 eraser->eraseblocks[1].size = erase_size_high;
1304 eraser->eraseblocks[1].count = size_high / erase_size_high;
1305 msg_cdbg("The second partition ranges from 0x%06x to 0x%06x.\n",
Stefan Taunerdbac46c2013-08-13 22:10:41 +00001306 boundary, total_size-1);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001307 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1308 size_high / erase_size_high, erase_size_high);
1309 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001310 flash->chip->tested = TEST_OK_PREW;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001311 return 1;
1312}
1313
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001314static int ich_hwseq_block_erase(struct flashctx *flash, unsigned int addr,
1315 unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001316{
1317 uint32_t erase_block;
1318 uint16_t hsfc;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001319
1320 erase_block = ich_hwseq_get_erase_block_size(addr);
1321 if (len != erase_block) {
1322 msg_cerr("Erase block size for address 0x%06x is %d B, "
1323 "but requested erase block size is %d B. "
1324 "Not erasing anything.\n", addr, erase_block, len);
1325 return -1;
1326 }
1327
1328 /* Although the hardware supports this (it would erase the whole block
1329 * containing the address) we play safe here. */
1330 if (addr % erase_block != 0) {
1331 msg_cerr("Erase address 0x%06x is not aligned to the erase "
1332 "block boundary (any multiple of %d). "
1333 "Not erasing anything.\n", addr, erase_block);
1334 return -1;
1335 }
1336
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001337 if (addr + len > flash->chip->total_size * 1024) {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001338 msg_perr("Request to erase some inaccessible memory address(es)"
1339 " (addr=0x%x, len=%d). "
1340 "Not erasing anything.\n", addr, len);
1341 return -1;
1342 }
1343
1344 msg_pdbg("Erasing %d bytes starting at 0x%06x.\n", len, addr);
Stefan Tauner7608d362014-08-05 23:28:47 +00001345 ich_hwseq_set_addr(addr);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001346
1347 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
1348 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1349
1350 hsfc = REGREAD16(ICH9_REG_HSFC);
Nico Huberd54e4f42017-03-23 23:45:47 +01001351 hsfc &= ~hwseq_data.hsfc_fcycle; /* clear operation */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001352 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
1353 hsfc |= HSFC_FGO; /* start */
1354 msg_pdbg("HSFC used for block erasing: ");
1355 prettyprint_ich9_reg_hsfc(hsfc);
1356 REGWRITE16(ICH9_REG_HSFC, hsfc);
1357
Subrata Banik7cb43952022-03-16 20:40:42 +05301358 if (ich_hwseq_wait_for_cycle_complete(len))
Stefan Tauner50e7c602011-11-08 10:55:54 +00001359 return -1;
1360 return 0;
1361}
1362
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001363static int ich_hwseq_read(struct flashctx *flash, uint8_t *buf,
1364 unsigned int addr, unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001365{
1366 uint16_t hsfc;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001367 uint8_t block_len;
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 read from an inaccessible memory address "
1371 "(addr=0x%x, len=%d).\n", addr, len);
1372 return -1;
1373 }
1374
1375 msg_pdbg("Reading %d bytes starting at 0x%06x.\n", len, addr);
1376 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1377 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1378
1379 while (len > 0) {
Stefan Tauner7608d362014-08-05 23:28:47 +00001380 /* Obey programmer limit... */
Nico Huber9a11cbf2023-01-13 01:19:07 +01001381 block_len = min(len, flash->mst.opaque->max_data_read);
Stefan Tauner7608d362014-08-05 23:28:47 +00001382 /* as well as flash chip page borders as demanded in the Intel datasheets. */
1383 block_len = min(block_len, 256 - (addr & 0xFF));
1384
Stefan Tauner50e7c602011-11-08 10:55:54 +00001385 ich_hwseq_set_addr(addr);
1386 hsfc = REGREAD16(ICH9_REG_HSFC);
Nico Huberd54e4f42017-03-23 23:45:47 +01001387 hsfc &= ~hwseq_data.hsfc_fcycle; /* set read operation */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001388 hsfc &= ~HSFC_FDBC; /* clear byte count */
1389 /* set byte count */
1390 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1391 hsfc |= HSFC_FGO; /* start */
1392 REGWRITE16(ICH9_REG_HSFC, hsfc);
1393
Subrata Banik7cb43952022-03-16 20:40:42 +05301394 if (ich_hwseq_wait_for_cycle_complete(block_len))
Stefan Tauner50e7c602011-11-08 10:55:54 +00001395 return 1;
1396 ich_read_data(buf, block_len, ICH9_REG_FDATA0);
Richard Hughes842d6782021-01-15 09:48:12 +00001397 flashprog_progress_add(flash, block_len);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001398 addr += block_len;
1399 buf += block_len;
1400 len -= block_len;
1401 }
1402 return 0;
1403}
1404
Mark Marshallf20b7be2014-05-09 21:16:21 +00001405static int ich_hwseq_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001406{
1407 uint16_t hsfc;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001408 uint8_t block_len;
1409
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001410 if (addr + len > flash->chip->total_size * 1024) {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001411 msg_perr("Request to write to an inaccessible memory address "
1412 "(addr=0x%x, len=%d).\n", addr, len);
1413 return -1;
1414 }
1415
1416 msg_pdbg("Writing %d bytes starting at 0x%06x.\n", len, addr);
1417 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1418 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1419
1420 while (len > 0) {
1421 ich_hwseq_set_addr(addr);
Stefan Tauner7608d362014-08-05 23:28:47 +00001422 /* Obey programmer limit... */
Nico Huber9a11cbf2023-01-13 01:19:07 +01001423 block_len = min(len, flash->mst.opaque->max_data_write);
Stefan Tauner7608d362014-08-05 23:28:47 +00001424 /* as well as flash chip page borders as demanded in the Intel datasheets. */
1425 block_len = min(block_len, 256 - (addr & 0xFF));
Stefan Tauner50e7c602011-11-08 10:55:54 +00001426 ich_fill_data(buf, block_len, ICH9_REG_FDATA0);
1427 hsfc = REGREAD16(ICH9_REG_HSFC);
Nico Huberd54e4f42017-03-23 23:45:47 +01001428 hsfc &= ~hwseq_data.hsfc_fcycle; /* clear operation */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001429 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
1430 hsfc &= ~HSFC_FDBC; /* clear byte count */
1431 /* set byte count */
1432 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1433 hsfc |= HSFC_FGO; /* start */
1434 REGWRITE16(ICH9_REG_HSFC, hsfc);
1435
Subrata Banik7cb43952022-03-16 20:40:42 +05301436 if (ich_hwseq_wait_for_cycle_complete(block_len))
Stefan Tauner50e7c602011-11-08 10:55:54 +00001437 return -1;
Richard Hughes842d6782021-01-15 09:48:12 +00001438 flashprog_progress_add(flash, block_len);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001439 addr += block_len;
1440 buf += block_len;
1441 len -= block_len;
1442 }
1443 return 0;
1444}
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001445
Edward O'Callaghan5eca4272020-04-12 17:27:53 +10001446static int ich_spi_send_multicommand(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001447 struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001448{
1449 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001450 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001451 int oppos, preoppos;
Nico Huberd5185632024-01-05 18:44:41 +01001452 for (; !spi_is_empty(cmds) && !ret; cmds++) {
1453 if (!spi_is_empty(cmds + 1)) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001454 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001455 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1456 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001457 if ((oppos == -1) && (preoppos != -1)) {
1458 /* Current command is listed as preopcode in
1459 * ICH struct OPCODES, but next command is not
1460 * listed as opcode in that struct.
1461 * Check for command sanity, then
1462 * try to reprogram the ICH opcode list.
1463 */
1464 if (find_preop(curopcodes,
1465 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001466 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001467 "preopcodes 0x%02x and 0x%02x, "
1468 "ignoring the first.\n",
1469 __func__, cmds->writearr[0],
1470 (cmds + 1)->writearr[0]);
1471 continue;
1472 }
1473 /* If the chipset is locked down, we'll fail
1474 * during execution of the next command anyway.
1475 * No need to bother with fixups.
1476 */
1477 if (!ichspi_lock) {
Nico Huberd5185632024-01-05 18:44:41 +01001478 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0],
1479 spi_write_len(cmds + 1), spi_read_len(cmds + 1));
Helge Wagner738e2522010-10-05 22:06:05 +00001480 if (oppos == -1)
1481 continue;
1482 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001483 continue;
1484 }
1485 }
1486 if ((oppos != -1) && (preoppos != -1)) {
1487 /* Current command is listed as preopcode in
1488 * ICH struct OPCODES and next command is listed
1489 * as opcode in that struct. Match them up.
1490 */
1491 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001492 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001493 }
1494 /* If none of the above if-statements about oppos or
1495 * preoppos matched, this is a normal opcode.
1496 */
1497 }
Nico Huberd5185632024-01-05 18:44:41 +01001498 ret = ich_spi_send_command(flash, spi_write_len(cmds), spi_read_len(cmds),
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001499 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001500 /* Reset the type of all opcodes to non-atomic. */
1501 for (i = 0; i < 8; i++)
1502 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001503 }
1504 return ret;
1505}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001506
Nikolai Artemieve7a41e32022-11-28 17:40:56 +11001507static bool ich_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode)
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301508{
Nico Huber3824c8d2024-05-26 16:59:22 +02001509 return !ichspi_lock || find_opcode(curopcodes, opcode) >= 0;
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301510}
1511
Michael Karchera4448d92010-07-22 18:04:15 +00001512#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1513#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1514#define ICH_BRWA(x) ((x >> 8) & 0xff)
1515#define ICH_BRRA(x) ((x >> 0) & 0xff)
1516
Nico Huber7590d1a2016-05-03 13:38:28 +02001517static const enum ich_access_protection access_perms_to_protection[] = {
1518 LOCKED, WRITE_PROT, READ_PROT, NO_PROT
1519};
1520static const char *const access_names[] = {
1521 "locked", "read-only", "write-only", "read-write"
1522};
1523
Nico Huber42daab12024-07-16 00:27:27 +02001524static enum ich_access_protection ich9_handle_access_perm(
1525 uint32_t bm_rap, uint32_t bm_wap, unsigned int max, unsigned int i)
Michael Karchera4448d92010-07-22 18:04:15 +00001526{
Nico Huberaa91d5c2017-08-19 17:04:21 +02001527 const int rwperms_unknown = ARRAY_SIZE(access_names);
Nico Huber2a5dfaf2019-07-04 16:01:51 +02001528 static const char *const region_names[] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001529 "Flash Descriptor", "BIOS", "Management Engine",
Nico Huberd2d39932019-01-18 16:49:37 +01001530 "Gigabit Ethernet", "Platform Data", "Device Expansion",
Nico Huber2a5dfaf2019-07-04 16:01:51 +02001531 "BIOS2", "unknown", "EC/BMC",
Michael Karchera4448d92010-07-22 18:04:15 +00001532 };
Nico Huberd54e4f42017-03-23 23:45:47 +01001533 const char *const region_name = i < ARRAY_SIZE(region_names) ? region_names[i] : "unknown";
1534
Michael Karchera4448d92010-07-22 18:04:15 +00001535 uint32_t base, limit;
Nico Huberaa91d5c2017-08-19 17:04:21 +02001536 int rwperms;
Nico Huberd2d39932019-01-18 16:49:37 +01001537 const int offset = i < 12
1538 ? ICH9_REG_FREG0 + i * 4
1539 : APL_REG_FREG12 + (i - 12) * 4;
Michael Karchera4448d92010-07-22 18:04:15 +00001540 uint32_t freg = mmio_readl(ich_spibar + offset);
1541
Nico Huber42daab12024-07-16 00:27:27 +02001542 if (i < max) {
1543 rwperms = (((bm_wap >> i) & 1) << 1) |
1544 (((bm_rap >> i) & 1) << 0);
Nico Huberaa91d5c2017-08-19 17:04:21 +02001545 } else {
1546 /* Datasheets don't define any access bits for regions > 7. We
1547 can't rely on the actual descriptor settings either as there
1548 are several overrides for them (those by other masters are
1549 not even readable by us, *shrug*). */
1550 rwperms = rwperms_unknown;
1551 }
1552
Michael Karchera4448d92010-07-22 18:04:15 +00001553 base = ICH_FREG_BASE(freg);
1554 limit = ICH_FREG_LIMIT(freg);
Stefan Taunere3adea02012-08-27 15:12:36 +00001555 if (base > limit || (freg == 0 && i > 0)) {
Michael Karchera4448d92010-07-22 18:04:15 +00001556 /* this FREG is disabled */
Nico Huber519be662018-12-23 20:03:35 +01001557 msg_pdbg2("0x%02X: 0x%08x FREG%u: %s region is unused.\n",
Nico Huberd54e4f42017-03-23 23:45:47 +01001558 offset, freg, i, region_name);
Nico Huber7590d1a2016-05-03 13:38:28 +02001559 return NO_PROT;
Stefan Tauner5210e722012-02-16 01:13:00 +00001560 }
1561 msg_pdbg("0x%02X: 0x%08x ", offset, freg);
1562 if (rwperms == 0x3) {
Nico Huber519be662018-12-23 20:03:35 +01001563 msg_pdbg("FREG%u: %s region (0x%08x-0x%08x) is %s.\n", i,
Nico Huber0bb3f712017-03-29 16:44:33 +02001564 region_name, base, limit, access_names[rwperms]);
Nico Huber7590d1a2016-05-03 13:38:28 +02001565 return NO_PROT;
Michael Karchera4448d92010-07-22 18:04:15 +00001566 }
Nico Huberaa91d5c2017-08-19 17:04:21 +02001567 if (rwperms == rwperms_unknown) {
Nico Huber519be662018-12-23 20:03:35 +01001568 msg_pdbg("FREG%u: %s region (0x%08x-0x%08x) has unknown permissions.\n",
Nico Huberaa91d5c2017-08-19 17:04:21 +02001569 i, region_name, base, limit);
Nico Huber7590d1a2016-05-03 13:38:28 +02001570 return NO_PROT;
Nico Huberaa91d5c2017-08-19 17:04:21 +02001571 }
Michael Karchera4448d92010-07-22 18:04:15 +00001572
Nico Huber519be662018-12-23 20:03:35 +01001573 msg_pinfo("FREG%u: %s region (0x%08x-0x%08x) is %s.\n", i,
Nico Huber0bb3f712017-03-29 16:44:33 +02001574 region_name, base, limit, access_names[rwperms]);
Nico Huber7590d1a2016-05-03 13:38:28 +02001575 return access_perms_to_protection[rwperms];
Michael Karchera4448d92010-07-22 18:04:15 +00001576}
1577
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001578 /* In contrast to FRAP and the master section of the descriptor the bits
1579 * in the PR registers have an inverted meaning. The bits in FRAP
1580 * indicate read and write access _grant_. Here they indicate read
1581 * and write _protection_ respectively. If both bits are 0 the address
1582 * bits are ignored.
1583 */
1584#define ICH_PR_PERMS(pr) (((~((pr) >> PR_RP_OFF) & 1) << 0) | \
1585 ((~((pr) >> PR_WP_OFF) & 1) << 1))
1586
Nico Huber519be662018-12-23 20:03:35 +01001587static enum ich_access_protection ich9_handle_pr(const size_t reg_pr0, unsigned int i)
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001588{
Nico Huberd54e4f42017-03-23 23:45:47 +01001589 uint8_t off = reg_pr0 + (i * 4);
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001590 uint32_t pr = mmio_readl(ich_spibar + off);
Stefan Tauner5210e722012-02-16 01:13:00 +00001591 unsigned int rwperms = ICH_PR_PERMS(pr);
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001592
Nico Huberd54e4f42017-03-23 23:45:47 +01001593 /* From 5 on we have GPR registers and start from 0 again. */
1594 const char *const prefix = i >= 5 ? "G" : "";
1595 if (i >= 5)
1596 i -= 5;
1597
Stefan Tauner5210e722012-02-16 01:13:00 +00001598 if (rwperms == 0x3) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001599 msg_pdbg2("0x%02X: 0x%08x (%sPR%u is unused)\n", off, pr, prefix, i);
Nico Huber7590d1a2016-05-03 13:38:28 +02001600 return NO_PROT;
Stefan Tauner5210e722012-02-16 01:13:00 +00001601 }
1602
1603 msg_pdbg("0x%02X: 0x%08x ", off, pr);
Nico Huberd54e4f42017-03-23 23:45:47 +01001604 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 +02001605 ICH_FREG_LIMIT(pr), access_names[rwperms]);
Nico Huber7590d1a2016-05-03 13:38:28 +02001606 return access_perms_to_protection[rwperms];
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001607}
1608
Stefan Tauner75da80c2011-09-17 22:21:55 +00001609/* Set/Clear the read and write protection enable bits of PR register @i
1610 * according to @read_prot and @write_prot. */
Nico Huberd54e4f42017-03-23 23:45:47 +01001611static void ich9_set_pr(const size_t reg_pr0, int i, int read_prot, int write_prot)
Stefan Tauner75da80c2011-09-17 22:21:55 +00001612{
Nico Huberd54e4f42017-03-23 23:45:47 +01001613 void *addr = ich_spibar + reg_pr0 + (i * 4);
Stefan Tauner75da80c2011-09-17 22:21:55 +00001614 uint32_t old = mmio_readl(addr);
1615 uint32_t new;
1616
1617 msg_gspew("PR%u is 0x%08x", i, old);
1618 new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF));
1619 if (read_prot)
1620 new |= (1 << PR_RP_OFF);
1621 if (write_prot)
1622 new |= (1 << PR_WP_OFF);
1623 if (old == new) {
1624 msg_gspew(" already.\n");
1625 return;
1626 }
1627 msg_gspew(", trying to set it to 0x%08x ", new);
1628 rmmio_writel(new, addr);
1629 msg_gspew("resulted in 0x%08x.\n", mmio_readl(addr));
1630}
1631
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001632static const struct spi_master spi_master_ich7 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +02001633 .max_data_read = 64,
1634 .max_data_write = 64,
1635 .command = ich_spi_send_command,
1636 .multicommand = ich_spi_send_multicommand,
1637 .read = default_spi_read,
1638 .write_256 = default_spi_write_256,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301639 .probe_opcode = ich_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001640};
1641
Nico Hubera1f64762024-07-14 20:23:28 +02001642int ich7_init_spi(void *spibar, enum ich_chipset ich_gen)
1643{
1644 unsigned int i;
1645
1646 ich_generation = ich_gen;
1647 ich_spibar = spibar;
1648
Nico Huber869f0e72024-07-14 22:58:39 +02001649 swseq_data.reg_preop = ICH7_REG_PREOP;
1650 swseq_data.reg_optype = ICH7_REG_OPTYPE;
1651 swseq_data.reg_opmenu = ICH7_REG_OPMENU;
1652
Nico Hubera1f64762024-07-14 20:23:28 +02001653 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1654 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1655 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1656
1657 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1658 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1659 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1660 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1661 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1662 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1663
1664 for (i = 0; i < 3; i++) {
1665 const int offs = 0x60 + (i * 4);
1666 msg_pdbg("0x%02x: 0x%08x (PBR%u)\n", offs,
1667 mmio_readl(ich_spibar + offs), i);
1668 }
1669
1670 if (mmio_readw(ich_spibar) & (1 << 15)) {
1671 msg_pwarn("WARNING: SPI Configuration Lockdown activated.\n");
1672 ichspi_lock = true;
1673 }
1674
1675 ich_init_opcodes();
1676 ich_set_bbar(0);
1677
1678 return register_spi_master(&spi_master_ich7, 0, NULL);
1679}
1680
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001681static const struct spi_master spi_master_ich9 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +02001682 .max_data_read = 64,
1683 .max_data_write = 64,
1684 .command = ich_spi_send_command,
1685 .multicommand = ich_spi_send_multicommand,
1686 .read = default_spi_read,
1687 .write_256 = default_spi_write_256,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301688 .probe_opcode = ich_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001689};
1690
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001691static const struct opaque_master opaque_master_ich_hwseq = {
Thomas Heijligen43040f22022-06-23 14:38:35 +02001692 .max_data_read = 64,
1693 .max_data_write = 64,
1694 .probe = ich_hwseq_probe,
1695 .read = ich_hwseq_read,
1696 .write = ich_hwseq_write,
1697 .erase = ich_hwseq_block_erase,
Stefan Tauner50e7c602011-11-08 10:55:54 +00001698};
1699
Nico Hubera1f64762024-07-14 20:23:28 +02001700int ich9_init_spi(void *spibar, enum ich_chipset ich_gen)
Michael Karchera4448d92010-07-22 18:04:15 +00001701{
Nico Huber519be662018-12-23 20:03:35 +01001702 unsigned int i;
Stefan Tauner92d6a862013-10-25 00:33:37 +00001703 uint16_t tmp2;
Michael Karchera4448d92010-07-22 18:04:15 +00001704 uint32_t tmp;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001705 char *arg;
Stefan Tauner5210e722012-02-16 01:13:00 +00001706 int ich_spi_rw_restricted = 0;
Felix Singer8cfc7372022-08-19 03:10:29 +02001707 bool desc_valid = false;
Angel Pons7e134562021-06-07 13:29:13 +02001708 struct ich_descriptors desc = { 0 };
Stefan Tauner50e7c602011-11-08 10:55:54 +00001709 enum ich_spi_mode {
1710 ich_auto,
1711 ich_hwseq,
1712 ich_swseq
1713 } ich_spi_mode = ich_auto;
Nico Huberd54e4f42017-03-23 23:45:47 +01001714 size_t num_freg, num_pr, reg_pr0;
Michael Karchera4448d92010-07-22 18:04:15 +00001715
Stefan Taunera8d838d2011-11-06 23:51:09 +00001716 ich_generation = ich_gen;
Stefan Tauner92d6a862013-10-25 00:33:37 +00001717 ich_spibar = spibar;
Michael Karchera4448d92010-07-22 18:04:15 +00001718
Nico Huberd54e4f42017-03-23 23:45:47 +01001719 /* Moving registers / bits */
Nico Huberfda324b2024-07-14 20:36:21 +02001720 if (ich_generation >= SPI_ENGINE_PCH100) {
David Hendricksa5216362017-08-08 20:02:22 -07001721 num_pr = 6; /* Includes GPR0 */
1722 reg_pr0 = PCH100_REG_FPR0;
1723 swseq_data.reg_ssfsc = PCH100_REG_SSFSC;
1724 swseq_data.reg_preop = PCH100_REG_PREOP;
1725 swseq_data.reg_optype = PCH100_REG_OPTYPE;
1726 swseq_data.reg_opmenu = PCH100_REG_OPMENU;
1727 hwseq_data.addr_mask = PCH100_FADDR_FLA;
1728 hwseq_data.only_4k = true;
1729 hwseq_data.hsfc_fcycle = PCH100_HSFC_FCYCLE;
Nico Huberfda324b2024-07-14 20:36:21 +02001730 } else {
Nico Huberd54e4f42017-03-23 23:45:47 +01001731 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;
Nico Huberd2d39932019-01-18 16:49:37 +01001740 }
Nico Hubereeee91b2024-07-14 21:12:21 +02001741
1742 if (ich_generation == CHIPSET_100_SERIES_SUNRISE_POINT)
Nico Huberd2d39932019-01-18 16:49:37 +01001743 num_freg = 10;
Nico Hubereeee91b2024-07-14 21:12:21 +02001744 else if (ich_generation == CHIPSET_C620_SERIES_LEWISBURG)
Nico Huberd2d39932019-01-18 16:49:37 +01001745 num_freg = 12; /* 12 MMIO regs, but 16 regions in FD spec */
Nico Hubereeee91b2024-07-14 21:12:21 +02001746 else if (ich_generation >= SPI_ENGINE_PCH100)
Nico Huberd2d39932019-01-18 16:49:37 +01001747 num_freg = 16;
Nico Hubereeee91b2024-07-14 21:12:21 +02001748 else
Nico Huberd2d39932019-01-18 16:49:37 +01001749 num_freg = 5;
Nico Huberd54e4f42017-03-23 23:45:47 +01001750
Nico Hubera1f64762024-07-14 20:23:28 +02001751 arg = extract_programmer_param("ich_spi_mode");
1752 if (arg && !strcmp(arg, "hwseq")) {
1753 ich_spi_mode = ich_hwseq;
1754 msg_pspew("user selected hwseq\n");
1755 } else if (arg && !strcmp(arg, "swseq")) {
1756 ich_spi_mode = ich_swseq;
1757 msg_pspew("user selected swseq\n");
1758 } else if (arg && !strcmp(arg, "auto")) {
1759 msg_pspew("user selected auto\n");
1760 ich_spi_mode = ich_auto;
1761 } else if (arg && !strlen(arg)) {
1762 msg_perr("Missing argument for ich_spi_mode.\n");
Stefan Tauner50e7c602011-11-08 10:55:54 +00001763 free(arg);
Nico Hubera1f64762024-07-14 20:23:28 +02001764 return ERROR_FATAL;
1765 } else if (arg) {
1766 msg_perr("Unknown argument for ich_spi_mode: %s\n",
1767 arg);
1768 free(arg);
1769 return ERROR_FATAL;
1770 }
1771 free(arg);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001772
Nico Hubera1f64762024-07-14 20:23:28 +02001773 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
1774 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
1775 prettyprint_ich9_reg_hsfs(tmp2);
1776 if (tmp2 & HSFS_FLOCKDN) {
1777 msg_pinfo("SPI Configuration is locked down.\n");
1778 ichspi_lock = true;
1779 }
1780 if (tmp2 & HSFS_FDV)
1781 desc_valid = true;
1782 if (!(tmp2 & HSFS_FDOPSS) && desc_valid)
1783 msg_pinfo("The Flash Descriptor Override Strap-Pin is set. Restrictions implied by\n"
1784 "the Master Section of the flash descriptor are NOT in effect. Please note\n"
1785 "that Protected Range (PR) restrictions still apply.\n");
1786 ich_init_opcodes();
Stefan Tauner55206942011-06-11 09:53:22 +00001787
Nico Hubera1f64762024-07-14 20:23:28 +02001788 if (desc_valid) {
1789 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
1790 msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
1791 prettyprint_ich9_reg_hsfc(tmp2);
1792 }
Michael Karchera4448d92010-07-22 18:04:15 +00001793
Nico Hubera1f64762024-07-14 20:23:28 +02001794 tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
1795 msg_pdbg2("0x08: 0x%08x (FADDR)\n", tmp);
Michael Karchera4448d92010-07-22 18:04:15 +00001796
Nico Huberfda324b2024-07-14 20:36:21 +02001797 if (ich_gen >= SPI_ENGINE_PCH100) {
Nico Hubera1f64762024-07-14 20:23:28 +02001798 tmp = mmio_readl(ich_spibar + PCH100_REG_DLOCK);
1799 msg_pdbg("0x0c: 0x%08x (DLOCK)\n", tmp);
1800 prettyprint_pch100_reg_dlock(tmp);
Nico Hubera1f64762024-07-14 20:23:28 +02001801 }
1802
1803 if (desc_valid) {
1804 tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
1805 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1806 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1807 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1808 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1809 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1810
Nico Huber42daab12024-07-16 00:27:27 +02001811 unsigned int max = 8; /* old, FRAP max. */
1812 uint32_t bm_wap = ICH_BRWA(tmp), bm_rap = ICH_BRRA(tmp);
1813
1814 if (ich_gen >= CHIPSET_HAS_NEW_ACCESS_PERM) {
1815 max = 32;
1816 bm_wap = mmio_readl(ich_spibar + BIOS_BM_WAP);
1817 bm_rap = mmio_readl(ich_spibar + BIOS_BM_RAP);
1818 msg_pdbg("0x%x: 0x%08x (BIOS_BM_WAP)\n", BIOS_BM_WAP, bm_wap);
1819 msg_pdbg("0x%x: 0x%08x (BIOS_BM_RAP)\n", BIOS_BM_RAP, bm_rap);
1820 }
1821
Nico Hubera1f64762024-07-14 20:23:28 +02001822 /* Handle FREGx and FRAP registers */
1823 for (i = 0; i < num_freg; i++)
Nico Huber42daab12024-07-16 00:27:27 +02001824 ich_spi_rw_restricted |= ich9_handle_access_perm(bm_rap, bm_wap, max, i);
Nico Hubera1f64762024-07-14 20:23:28 +02001825 if (ich_spi_rw_restricted)
1826 msg_pinfo("Not all flash regions are freely accessible by flashprog. This is "
1827 "most likely\ndue to an active ME. Please see "
1828 "https://flashprog.org/ME for details.\n");
1829 }
1830
1831 /* Handle PR registers */
1832 for (i = 0; i < num_pr; i++) {
1833 /* if not locked down try to disable PR locks first */
1834 if (!ichspi_lock)
1835 ich9_set_pr(reg_pr0, i, 0, 0);
1836 ich_spi_rw_restricted |= ich9_handle_pr(reg_pr0, i);
1837 }
1838
1839 switch (ich_spi_rw_restricted) {
1840 case WRITE_PROT:
1841 msg_pwarn("At least some flash regions are write protected. For write operations,\n"
1842 "you should use a flash layout and include only writable regions. See\n"
1843 "manpage for more details.\n");
1844 break;
1845 case READ_PROT:
1846 case LOCKED:
1847 msg_pwarn("At least some flash regions are read protected. You have to use a flash\n"
1848 "layout and include only accessible regions. For write operations, you'll\n"
1849 "additionally need the --noverify-all switch. See manpage for more details.\n"
1850 );
1851 break;
1852 }
1853
1854 tmp = mmio_readl(ich_spibar + swseq_data.reg_ssfsc);
1855 msg_pdbg("0x%zx: 0x%02x (SSFS)\n", swseq_data.reg_ssfsc, tmp & 0xff);
1856 prettyprint_ich9_reg_ssfs(tmp);
1857 if (tmp & SSFS_FCERR) {
1858 msg_pdbg("Clearing SSFS.FCERR\n");
1859 mmio_writeb(SSFS_FCERR, ich_spibar + swseq_data.reg_ssfsc);
1860 }
1861 msg_pdbg("0x%zx: 0x%06x (SSFC)\n", swseq_data.reg_ssfsc + 1, tmp >> 8);
1862 prettyprint_ich9_reg_ssfc(tmp);
1863
1864 msg_pdbg("0x%zx: 0x%04x (PREOP)\n",
1865 swseq_data.reg_preop, mmio_readw(ich_spibar + swseq_data.reg_preop));
1866 msg_pdbg("0x%zx: 0x%04x (OPTYPE)\n",
1867 swseq_data.reg_optype, mmio_readw(ich_spibar + swseq_data.reg_optype));
1868 msg_pdbg("0x%zx: 0x%08x (OPMENU)\n",
1869 swseq_data.reg_opmenu, mmio_readl(ich_spibar + swseq_data.reg_opmenu));
1870 msg_pdbg("0x%zx: 0x%08x (OPMENU+4)\n",
1871 swseq_data.reg_opmenu + 4, mmio_readl(ich_spibar + swseq_data.reg_opmenu + 4));
1872
1873 if (desc_valid) {
Nico Huberfda324b2024-07-14 20:36:21 +02001874 if (ich_gen < SPI_ENGINE_PCH100 &&
1875 ich_gen != CHIPSET_ICH8 &&
1876 ich_gen != CHIPSET_BAYTRAIL) {
Nico Hubera1f64762024-07-14 20:23:28 +02001877 ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
1878 msg_pdbg("0x%x: 0x%08x (BBAR)\n", ICH9_REG_BBAR, ichspi_bbar);
1879 ich_set_bbar(0);
Nico Huberd54e4f42017-03-23 23:45:47 +01001880 }
1881
Nico Hubera1f64762024-07-14 20:23:28 +02001882 if (ich_gen == CHIPSET_ICH8) {
1883 tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC);
1884 msg_pdbg("0x%x: 0x%08x (VSCC)\n", ICH8_REG_VSCC, tmp);
1885 msg_pdbg("VSCC: ");
1886 prettyprint_ich_reg_vscc(tmp, FLASHPROG_MSG_DEBUG, true);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001887 } else {
Nico Hubera1f64762024-07-14 20:23:28 +02001888 tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC);
1889 msg_pdbg("0x%x: 0x%08x (LVSCC)\n", ICH9_REG_LVSCC, tmp);
1890 msg_pdbg("LVSCC: ");
1891 prettyprint_ich_reg_vscc(tmp, FLASHPROG_MSG_DEBUG, true);
1892
1893 tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC);
1894 msg_pdbg("0x%x: 0x%08x (UVSCC)\n", ICH9_REG_UVSCC, tmp);
1895 msg_pdbg("UVSCC: ");
1896 prettyprint_ich_reg_vscc(tmp, FLASHPROG_MSG_DEBUG, false);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001897 }
Nico Hubera1f64762024-07-14 20:23:28 +02001898
Nico Huberfda324b2024-07-14 20:36:21 +02001899 if (ich_gen < SPI_ENGINE_PCH100 && ich_gen != CHIPSET_ICH8) {
Nico Hubera1f64762024-07-14 20:23:28 +02001900 tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
1901 msg_pdbg("0x%x: 0x%08x (FPB)\n", ICH9_REG_FPB, tmp);
Nico Hubera1f64762024-07-14 20:23:28 +02001902 }
1903
1904 if (read_ich_descriptors_via_fdo(ich_gen, ich_spibar, &desc) == ICH_RET_OK)
1905 prettyprint_ich_descriptors(ich_gen, &desc);
1906
1907 /* If the descriptor is valid and indicates multiple
1908 * flash devices we need to use hwseq to be able to
1909 * access the second flash device.
1910 */
1911 if (ich_spi_mode == ich_auto && desc.content.NC != 0) {
1912 msg_pinfo("Enabling hardware sequencing due to "
1913 "multiple flash chips detected.\n");
1914 ich_spi_mode = ich_hwseq;
1915 }
1916 }
1917
1918 if (ich_spi_mode == ich_auto && ichspi_lock &&
1919 ich_missing_opcodes()) {
1920 msg_pinfo("Enabling hardware sequencing because "
1921 "some important opcode is locked.\n");
1922 ich_spi_mode = ich_hwseq;
1923 }
1924
Nico Hubere8babf42024-07-14 20:40:56 +02001925 if (ich_spi_mode == ich_auto && ich_gen >= SPI_ENGINE_PCH100) {
1926 msg_pdbg("Enabling hardware sequencing by default for 100+ series SPI.\n");
Nico Hubera1f64762024-07-14 20:23:28 +02001927 ich_spi_mode = ich_hwseq;
1928 }
1929
1930 if (ich_spi_mode == ich_hwseq) {
1931 if (!desc_valid) {
1932 msg_perr("Hardware sequencing was requested "
1933 "but the flash descriptor is not "
1934 "valid. Aborting.\n");
1935 return ERROR_FATAL;
1936 }
1937
1938 int tmpi = getFCBA_component_density(ich_generation, &desc, 0);
1939 if (tmpi < 0) {
1940 msg_perr("Could not determine density of flash component %d.\n", 0);
1941 return ERROR_FATAL;
1942 }
1943 hwseq_data.size_comp0 = tmpi;
1944
1945 tmpi = getFCBA_component_density(ich_generation, &desc, 1);
1946 if (tmpi < 0) {
1947 msg_perr("Could not determine density of flash component %d.\n", 1);
1948 return ERROR_FATAL;
1949 }
1950 hwseq_data.size_comp1 = tmpi;
1951
1952 register_opaque_master(&opaque_master_ich_hwseq, NULL);
1953 } else {
1954 register_spi_master(&spi_master_ich9, 0, NULL);
Michael Karchera4448d92010-07-22 18:04:15 +00001955 }
1956
Michael Karchera4448d92010-07-22 18:04:15 +00001957 return 0;
1958}
1959
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001960static const struct spi_master spi_master_via = {
Thomas Heijligen43040f22022-06-23 14:38:35 +02001961 .max_data_read = 16,
1962 .max_data_write = 16,
1963 .command = ich_spi_send_command,
1964 .multicommand = ich_spi_send_multicommand,
1965 .read = default_spi_read,
1966 .write_256 = default_spi_write_256,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301967 .probe_opcode = ich_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001968};
1969
Nico Huber560111e2017-04-26 12:27:17 +02001970int via_init_spi(uint32_t mmio_base)
Michael Karchera4448d92010-07-22 18:04:15 +00001971{
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001972 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00001973
Stefan Tauner7fb5aa02013-08-14 15:48:44 +00001974 ich_spibar = rphysmap("VIA SPI MMIO registers", mmio_base, 0x70);
1975 if (ich_spibar == ERROR_PTR)
1976 return ERROR_FATAL;
Helge Wagnerdd73d832012-08-24 23:03:46 +00001977 /* Do we really need no write enable? Like the LPC one at D17F0 0x40 */
Michael Karchera4448d92010-07-22 18:04:15 +00001978
Michael Karchera4448d92010-07-22 18:04:15 +00001979 /* Not sure if it speaks all these bus protocols. */
Nico Huber2e50cdc2018-09-23 20:20:26 +02001980 internal_buses_supported &= BUS_LPC | BUS_FWH;
Stefan Taunera8d838d2011-11-06 23:51:09 +00001981 ich_generation = CHIPSET_ICH7;
Nico Huber89569d62023-01-12 23:31:40 +01001982 register_spi_master(&spi_master_via, 0, NULL);
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001983
1984 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1985 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1986 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1987 for (i = 0; i < 2; i++) {
1988 int offs;
1989 offs = 8 + (i * 8);
1990 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1991 mmio_readl(ich_spibar + offs), i);
1992 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1993 mmio_readl(ich_spibar + offs + 4), i);
1994 }
1995 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1996 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1997 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1998 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1999 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
2000 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
2001 for (i = 0; i < 3; i++) {
2002 int offs;
2003 offs = 0x60 + (i * 4);
2004 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
2005 mmio_readl(ich_spibar + offs), i);
2006 }
2007 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
2008 mmio_readw(ich_spibar + 0x6c));
2009 if (mmio_readw(ich_spibar) & (1 << 15)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +00002010 msg_pwarn("Warning: SPI Configuration Lockdown activated.\n");
Felix Singer8cfc7372022-08-19 03:10:29 +02002011 ichspi_lock = true;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00002012 }
2013
Stefan Taunera8d838d2011-11-06 23:51:09 +00002014 ich_set_bbar(0);
Michael Karchera4448d92010-07-22 18:04:15 +00002015 ich_init_opcodes();
2016
2017 return 0;
2018}