blob: 36e1dce23e4aa86cd8e9bca9512302f18ebf8727 [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);
Nico Huber610c1aa2023-02-15 02:56:05 +0100297static int run_opcode(const struct spi_master *, 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
Nico Huber610c1aa2023-02-15 02:56:05 +01001016static int run_opcode(const struct spi_master *spi, 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 Huber610c1aa2023-02-15 02:56:05 +01001020 uint8_t maxlength = 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
Nico Huber610c1aa2023-02-15 02:56:05 +01001040static int ich_spi_send_command(const struct spi_master *spi,
1041 unsigned int writecnt, unsigned int readcnt,
1042 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +00001043{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001044 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001045 int opcode_index = -1;
1046 const unsigned char cmd = *writearr;
1047 OPCODE *opcode;
1048 uint32_t addr = 0;
1049 uint8_t *data;
1050 int count;
1051
Dominik Geyerb46acba2008-05-16 12:55:55 +00001052 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001053 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +00001054 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +00001055 if (!ichspi_lock)
1056 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
Stefan Taunerdc704ed2012-05-06 15:11:26 +00001057 if (opcode_index == SPI_INVALID_LENGTH) {
1058 msg_pdbg("OPCODE 0x%02x has unsupported length, will not execute.\n", cmd);
1059 return SPI_INVALID_LENGTH;
1060 } else if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001061 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
1062 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +00001063 return SPI_INVALID_OPCODE;
1064 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001065 }
1066
1067 opcode = &(curopcodes->opcode[opcode_index]);
1068
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001069 /* The following valid writecnt/readcnt combinations exist:
1070 * writecnt = 4, readcnt >= 0
1071 * writecnt = 1, readcnt >= 0
1072 * writecnt >= 4, readcnt = 0
1073 * writecnt >= 1, readcnt = 0
1074 * writecnt >= 1 is guaranteed for all commands.
1075 */
1076 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
1077 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001078 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001079 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
1080 writecnt);
1081 return SPI_INVALID_LENGTH;
1082 }
1083 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
1084 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001085 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001086 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
1087 writecnt);
1088 return SPI_INVALID_LENGTH;
1089 }
1090 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1091 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001092 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001093 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
1094 writecnt);
1095 return SPI_INVALID_LENGTH;
1096 }
1097 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1098 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1099 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001100 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001101 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1102 readcnt);
1103 return SPI_INVALID_LENGTH;
1104 }
1105
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001106 /* Translate read/write array/count.
1107 * The maximum data length is identical for the maximum read length and
1108 * for the maximum write length excluding opcode and address. Opcode and
1109 * address are stored in separate registers, not in the data registers
1110 * and are thus not counted towards data length. The only exception
1111 * applies if the opcode definition (un)intentionally classifies said
1112 * opcode incorrectly as non-address opcode or vice versa. */
Dominik Geyerb46acba2008-05-16 12:55:55 +00001113 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001114 data = (uint8_t *) (writearr + 1);
1115 count = writecnt - 1;
1116 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1117 data = (uint8_t *) (writearr + 4);
1118 count = writecnt - 4;
1119 } else {
1120 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001121 count = readcnt;
1122 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001123
Nico Hubered098d62017-04-21 23:47:08 +02001124 /* if opcode-type requires an address */
1125 if (cmd == JEDEC_REMS || cmd == JEDEC_RES) {
1126 addr = ichspi_bbar;
1127 } else if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1128 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Nico Hubered098d62017-04-21 23:47:08 +02001129 /* Highest address we can program is (2^24 - 1). */
Nico Hubere3f648c2023-02-15 02:55:23 +01001130 const uint32_t valid_end = 1 << 24;
Nico Hubered098d62017-04-21 23:47:08 +02001131
1132 addr = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Nico Hubered098d62017-04-21 23:47:08 +02001133
Nico Hubere3f648c2023-02-15 02:55:23 +01001134 /* BBAR may cut part of the chip off at the lower end. */
1135 if (addr < ichspi_bbar) {
1136 msg_perr("%s: Address 0x%06x not in allowed range 0x%06x-0x%06x\n",
1137 __func__, addr, ichspi_bbar, valid_end - 1);
Nico Hubered098d62017-04-21 23:47:08 +02001138 return SPI_INVALID_ADDRESS;
1139 }
Nico Hubered098d62017-04-21 23:47:08 +02001140 }
1141
Nico Huber610c1aa2023-02-15 02:56:05 +01001142 result = run_opcode(spi, *opcode, addr, count, data);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001143 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001144 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1145 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1146 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1147 msg_pdbg("at address 0x%06x ", addr);
1148 }
1149 msg_pdbg("(payload length was %d).\n", count);
1150
1151 /* Print out the data array if it contains data to write.
1152 * Errors are detected before the received data is read back into
1153 * the array so it won't make sense to print it then. */
1154 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1155 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1156 int i;
1157 msg_pspew("The data was:\n");
Stefan Taunerf382e352011-11-08 11:55:24 +00001158 for (i = 0; i < count; i++){
Stefan Tauner8ed29342011-04-29 23:53:09 +00001159 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1160 }
1161 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001162 }
1163
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001164 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001165}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001166
Stefan Tauner50e7c602011-11-08 10:55:54 +00001167static struct hwseq_data {
1168 uint32_t size_comp0;
1169 uint32_t size_comp1;
Nico Huberd54e4f42017-03-23 23:45:47 +01001170 uint32_t addr_mask;
1171 bool only_4k;
1172 uint32_t hsfc_fcycle;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001173} hwseq_data;
1174
Nico Huberd54e4f42017-03-23 23:45:47 +01001175/* Sets FLA in FADDR to (addr & hwseq_data.addr_mask) without touching other bits. */
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001176static void ich_hwseq_set_addr(uint32_t addr)
1177{
Nico Huberd54e4f42017-03-23 23:45:47 +01001178 uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~hwseq_data.addr_mask;
1179 REGWRITE32(ICH9_REG_FADDR, (addr & hwseq_data.addr_mask) | addr_old);
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001180}
1181
1182/* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
1183 * of the block containing this address. May return nonsense if the address is
1184 * not valid. The erase block size for a specific address depends on the flash
1185 * partition layout as specified by FPB and the partition properties as defined
1186 * by UVSCC and LVSCC respectively. An alternative to implement this method
1187 * would be by querying FPB and the respective VSCC register directly.
1188 */
1189static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr)
1190{
Elyes HAOUAS29e46d02019-06-09 17:38:25 +02001191 uint8_t enc_berase;
1192 static const uint32_t dec_berase[4] = {
1193 256,
1194 4 * 1024,
1195 8 * 1024,
1196 64 * 1024
1197 };
1198
Nico Huberd54e4f42017-03-23 23:45:47 +01001199 if (hwseq_data.only_4k) {
1200 return 4 * 1024;
Nico Huberd54e4f42017-03-23 23:45:47 +01001201 }
Elyes HAOUAS29e46d02019-06-09 17:38:25 +02001202
1203 ich_hwseq_set_addr(addr);
1204 enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >> HSFS_BERASE_OFF;
1205 return dec_berase[enc_berase];
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001206}
1207
1208/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
1209 Resets all error flags in HSFS.
1210 Returns 0 if the cycle completes successfully without errors within
1211 timeout us, 1 on errors. */
Subrata Banik7cb43952022-03-16 20:40:42 +05301212static int ich_hwseq_wait_for_cycle_complete(unsigned int len)
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001213{
Subrata Banik7cb43952022-03-16 20:40:42 +05301214 /*
1215 * The SPI bus may be busy due to performing operations from other masters, hence
1216 * introduce the long timeout of 30s to cover the worst case scenarios as well.
1217 */
1218 unsigned int timeout_us = 30 * 1000 * 1000;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001219 uint16_t hsfs;
1220 uint32_t addr;
1221
Subrata Banik7cb43952022-03-16 20:40:42 +05301222 timeout_us /= 8; /* scale timeout duration to counter */
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001223 while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) &
1224 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
Subrata Banik7cb43952022-03-16 20:40:42 +05301225 --timeout_us) {
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001226 programmer_delay(8);
1227 }
1228 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
Subrata Banik7cb43952022-03-16 20:40:42 +05301229 if (!timeout_us) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001230 addr = REGREAD32(ICH9_REG_FADDR) & hwseq_data.addr_mask;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001231 msg_perr("Timeout error between offset 0x%08x and "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001232 "0x%08x (= 0x%08x + %d)!\n",
1233 addr, addr + len - 1, addr, len - 1);
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001234 prettyprint_ich9_reg_hsfs(hsfs);
1235 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1236 return 1;
1237 }
1238
1239 if (hsfs & HSFS_FCERR) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001240 addr = REGREAD32(ICH9_REG_FADDR) & hwseq_data.addr_mask;
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001241 msg_perr("Transaction error between offset 0x%08x and "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001242 "0x%08x (= 0x%08x + %d)!\n",
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001243 addr, addr + len - 1, addr, len - 1);
1244 prettyprint_ich9_reg_hsfs(hsfs);
1245 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1246 return 1;
1247 }
1248 return 0;
1249}
Stefan Tauner50e7c602011-11-08 10:55:54 +00001250
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001251static int ich_hwseq_probe(struct flashctx *flash)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001252{
1253 uint32_t total_size, boundary;
1254 uint32_t erase_size_low, size_low, erase_size_high, size_high;
1255 struct block_eraser *eraser;
1256
1257 total_size = hwseq_data.size_comp0 + hwseq_data.size_comp1;
Stefan Tauner5c316f92015-02-08 21:57:52 +00001258 msg_cdbg("Hardware sequencing reports %d attached SPI flash chip",
Stefan Tauner50e7c602011-11-08 10:55:54 +00001259 (hwseq_data.size_comp1 != 0) ? 2 : 1);
1260 if (hwseq_data.size_comp1 != 0)
1261 msg_cdbg("s with a combined");
1262 else
1263 msg_cdbg(" with a");
1264 msg_cdbg(" density of %d kB.\n", total_size / 1024);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001265 flash->chip->total_size = total_size / 1024;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001266
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001267 eraser = &(flash->chip->block_erasers[0]);
Nico Huberd54e4f42017-03-23 23:45:47 +01001268 if (!hwseq_data.only_4k)
1269 boundary = (REGREAD32(ICH9_REG_FPB) & FPB_FPBA) << 12;
1270 else
1271 boundary = 0;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001272 size_high = total_size - boundary;
1273 erase_size_high = ich_hwseq_get_erase_block_size(boundary);
1274
1275 if (boundary == 0) {
Stefan Tauner5c316f92015-02-08 21:57:52 +00001276 msg_cdbg2("There is only one partition containing the whole "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001277 "address space (0x%06x - 0x%06x).\n", 0, size_high-1);
1278 eraser->eraseblocks[0].size = erase_size_high;
1279 eraser->eraseblocks[0].count = size_high / erase_size_high;
Stefan Tauner5c316f92015-02-08 21:57:52 +00001280 msg_cdbg2("There are %d erase blocks with %d B each.\n",
Stefan Tauner50e7c602011-11-08 10:55:54 +00001281 size_high / erase_size_high, erase_size_high);
1282 } else {
Stefan Tauner5c316f92015-02-08 21:57:52 +00001283 msg_cdbg2("The flash address space (0x%06x - 0x%06x) is divided "
Stefan Tauner50e7c602011-11-08 10:55:54 +00001284 "at address 0x%06x in two partitions.\n",
Stefan Taunerdbac46c2013-08-13 22:10:41 +00001285 0, total_size-1, boundary);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001286 size_low = total_size - size_high;
1287 erase_size_low = ich_hwseq_get_erase_block_size(0);
1288
1289 eraser->eraseblocks[0].size = erase_size_low;
1290 eraser->eraseblocks[0].count = size_low / erase_size_low;
1291 msg_cdbg("The first partition ranges from 0x%06x to 0x%06x.\n",
1292 0, size_low-1);
1293 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1294 size_low / erase_size_low, erase_size_low);
1295
1296 eraser->eraseblocks[1].size = erase_size_high;
1297 eraser->eraseblocks[1].count = size_high / erase_size_high;
1298 msg_cdbg("The second partition ranges from 0x%06x to 0x%06x.\n",
Stefan Taunerdbac46c2013-08-13 22:10:41 +00001299 boundary, total_size-1);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001300 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1301 size_high / erase_size_high, erase_size_high);
1302 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001303 flash->chip->tested = TEST_OK_PREW;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001304 return 1;
1305}
1306
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001307static int ich_hwseq_block_erase(struct flashctx *flash, unsigned int addr,
1308 unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001309{
1310 uint32_t erase_block;
1311 uint16_t hsfc;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001312
1313 erase_block = ich_hwseq_get_erase_block_size(addr);
1314 if (len != erase_block) {
1315 msg_cerr("Erase block size for address 0x%06x is %d B, "
1316 "but requested erase block size is %d B. "
1317 "Not erasing anything.\n", addr, erase_block, len);
1318 return -1;
1319 }
1320
1321 /* Although the hardware supports this (it would erase the whole block
1322 * containing the address) we play safe here. */
1323 if (addr % erase_block != 0) {
1324 msg_cerr("Erase address 0x%06x is not aligned to the erase "
1325 "block boundary (any multiple of %d). "
1326 "Not erasing anything.\n", addr, erase_block);
1327 return -1;
1328 }
1329
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001330 if (addr + len > flash->chip->total_size * 1024) {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001331 msg_perr("Request to erase some inaccessible memory address(es)"
1332 " (addr=0x%x, len=%d). "
1333 "Not erasing anything.\n", addr, len);
1334 return -1;
1335 }
1336
1337 msg_pdbg("Erasing %d bytes starting at 0x%06x.\n", len, addr);
Stefan Tauner7608d362014-08-05 23:28:47 +00001338 ich_hwseq_set_addr(addr);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001339
1340 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
1341 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1342
1343 hsfc = REGREAD16(ICH9_REG_HSFC);
Nico Huberd54e4f42017-03-23 23:45:47 +01001344 hsfc &= ~hwseq_data.hsfc_fcycle; /* clear operation */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001345 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
1346 hsfc |= HSFC_FGO; /* start */
1347 msg_pdbg("HSFC used for block erasing: ");
1348 prettyprint_ich9_reg_hsfc(hsfc);
1349 REGWRITE16(ICH9_REG_HSFC, hsfc);
1350
Subrata Banik7cb43952022-03-16 20:40:42 +05301351 if (ich_hwseq_wait_for_cycle_complete(len))
Stefan Tauner50e7c602011-11-08 10:55:54 +00001352 return -1;
1353 return 0;
1354}
1355
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001356static int ich_hwseq_read(struct flashctx *flash, uint8_t *buf,
1357 unsigned int addr, unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001358{
1359 uint16_t hsfc;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001360 uint8_t block_len;
1361
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001362 if (addr + len > flash->chip->total_size * 1024) {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001363 msg_perr("Request to read from an inaccessible memory address "
1364 "(addr=0x%x, len=%d).\n", addr, len);
1365 return -1;
1366 }
1367
1368 msg_pdbg("Reading %d bytes starting at 0x%06x.\n", len, addr);
1369 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1370 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1371
1372 while (len > 0) {
Stefan Tauner7608d362014-08-05 23:28:47 +00001373 /* Obey programmer limit... */
Nico Huber9a11cbf2023-01-13 01:19:07 +01001374 block_len = min(len, flash->mst.opaque->max_data_read);
Stefan Tauner7608d362014-08-05 23:28:47 +00001375 /* as well as flash chip page borders as demanded in the Intel datasheets. */
1376 block_len = min(block_len, 256 - (addr & 0xFF));
1377
Stefan Tauner50e7c602011-11-08 10:55:54 +00001378 ich_hwseq_set_addr(addr);
1379 hsfc = REGREAD16(ICH9_REG_HSFC);
Nico Huberd54e4f42017-03-23 23:45:47 +01001380 hsfc &= ~hwseq_data.hsfc_fcycle; /* set read operation */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001381 hsfc &= ~HSFC_FDBC; /* clear byte count */
1382 /* set byte count */
1383 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1384 hsfc |= HSFC_FGO; /* start */
1385 REGWRITE16(ICH9_REG_HSFC, hsfc);
1386
Subrata Banik7cb43952022-03-16 20:40:42 +05301387 if (ich_hwseq_wait_for_cycle_complete(block_len))
Stefan Tauner50e7c602011-11-08 10:55:54 +00001388 return 1;
1389 ich_read_data(buf, block_len, ICH9_REG_FDATA0);
Richard Hughes842d6782021-01-15 09:48:12 +00001390 flashprog_progress_add(flash, block_len);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001391 addr += block_len;
1392 buf += block_len;
1393 len -= block_len;
1394 }
1395 return 0;
1396}
1397
Mark Marshallf20b7be2014-05-09 21:16:21 +00001398static int ich_hwseq_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len)
Stefan Tauner50e7c602011-11-08 10:55:54 +00001399{
1400 uint16_t hsfc;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001401 uint8_t block_len;
1402
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001403 if (addr + len > flash->chip->total_size * 1024) {
Stefan Tauner50e7c602011-11-08 10:55:54 +00001404 msg_perr("Request to write to an inaccessible memory address "
1405 "(addr=0x%x, len=%d).\n", addr, len);
1406 return -1;
1407 }
1408
1409 msg_pdbg("Writing %d bytes starting at 0x%06x.\n", len, addr);
1410 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1411 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1412
1413 while (len > 0) {
1414 ich_hwseq_set_addr(addr);
Stefan Tauner7608d362014-08-05 23:28:47 +00001415 /* Obey programmer limit... */
Nico Huber9a11cbf2023-01-13 01:19:07 +01001416 block_len = min(len, flash->mst.opaque->max_data_write);
Stefan Tauner7608d362014-08-05 23:28:47 +00001417 /* as well as flash chip page borders as demanded in the Intel datasheets. */
1418 block_len = min(block_len, 256 - (addr & 0xFF));
Stefan Tauner50e7c602011-11-08 10:55:54 +00001419 ich_fill_data(buf, block_len, ICH9_REG_FDATA0);
1420 hsfc = REGREAD16(ICH9_REG_HSFC);
Nico Huberd54e4f42017-03-23 23:45:47 +01001421 hsfc &= ~hwseq_data.hsfc_fcycle; /* clear operation */
Stefan Tauner50e7c602011-11-08 10:55:54 +00001422 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
1423 hsfc &= ~HSFC_FDBC; /* clear byte count */
1424 /* set byte count */
1425 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1426 hsfc |= HSFC_FGO; /* start */
1427 REGWRITE16(ICH9_REG_HSFC, hsfc);
1428
Subrata Banik7cb43952022-03-16 20:40:42 +05301429 if (ich_hwseq_wait_for_cycle_complete(block_len))
Stefan Tauner50e7c602011-11-08 10:55:54 +00001430 return -1;
Richard Hughes842d6782021-01-15 09:48:12 +00001431 flashprog_progress_add(flash, block_len);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001432 addr += block_len;
1433 buf += block_len;
1434 len -= block_len;
1435 }
1436 return 0;
1437}
Stefan Taunerd0c5dc22011-10-20 12:57:14 +00001438
Nico Huber610c1aa2023-02-15 02:56:05 +01001439static int ich_spi_send_multicommand(const struct spi_master *spi, struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001440{
1441 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001442 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001443 int oppos, preoppos;
Nico Huberd5185632024-01-05 18:44:41 +01001444 for (; !spi_is_empty(cmds) && !ret; cmds++) {
1445 if (!spi_is_empty(cmds + 1)) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001446 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001447 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1448 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001449 if ((oppos == -1) && (preoppos != -1)) {
1450 /* Current command is listed as preopcode in
1451 * ICH struct OPCODES, but next command is not
1452 * listed as opcode in that struct.
1453 * Check for command sanity, then
1454 * try to reprogram the ICH opcode list.
1455 */
1456 if (find_preop(curopcodes,
1457 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001458 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001459 "preopcodes 0x%02x and 0x%02x, "
1460 "ignoring the first.\n",
1461 __func__, cmds->writearr[0],
1462 (cmds + 1)->writearr[0]);
1463 continue;
1464 }
1465 /* If the chipset is locked down, we'll fail
1466 * during execution of the next command anyway.
1467 * No need to bother with fixups.
1468 */
1469 if (!ichspi_lock) {
Nico Huberd5185632024-01-05 18:44:41 +01001470 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0],
1471 spi_write_len(cmds + 1), spi_read_len(cmds + 1));
Helge Wagner738e2522010-10-05 22:06:05 +00001472 if (oppos == -1)
1473 continue;
1474 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001475 continue;
1476 }
1477 }
1478 if ((oppos != -1) && (preoppos != -1)) {
1479 /* Current command is listed as preopcode in
1480 * ICH struct OPCODES and next command is listed
1481 * as opcode in that struct. Match them up.
1482 */
1483 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001484 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001485 }
1486 /* If none of the above if-statements about oppos or
1487 * preoppos matched, this is a normal opcode.
1488 */
1489 }
Nico Huber610c1aa2023-02-15 02:56:05 +01001490 ret = ich_spi_send_command(spi, spi_write_len(cmds), spi_read_len(cmds),
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001491 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001492 /* Reset the type of all opcodes to non-atomic. */
1493 for (i = 0; i < 8; i++)
1494 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001495 }
1496 return ret;
1497}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001498
Nikolai Artemieve7a41e32022-11-28 17:40:56 +11001499static bool ich_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode)
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301500{
Nico Huber3824c8d2024-05-26 16:59:22 +02001501 return !ichspi_lock || find_opcode(curopcodes, opcode) >= 0;
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301502}
1503
Michael Karchera4448d92010-07-22 18:04:15 +00001504#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1505#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1506#define ICH_BRWA(x) ((x >> 8) & 0xff)
1507#define ICH_BRRA(x) ((x >> 0) & 0xff)
1508
Nico Huber7590d1a2016-05-03 13:38:28 +02001509static const enum ich_access_protection access_perms_to_protection[] = {
1510 LOCKED, WRITE_PROT, READ_PROT, NO_PROT
1511};
1512static const char *const access_names[] = {
1513 "locked", "read-only", "write-only", "read-write"
1514};
1515
Nico Huber42daab12024-07-16 00:27:27 +02001516static enum ich_access_protection ich9_handle_access_perm(
1517 uint32_t bm_rap, uint32_t bm_wap, unsigned int max, unsigned int i)
Michael Karchera4448d92010-07-22 18:04:15 +00001518{
Nico Huberaa91d5c2017-08-19 17:04:21 +02001519 const int rwperms_unknown = ARRAY_SIZE(access_names);
Nico Huber2a5dfaf2019-07-04 16:01:51 +02001520 static const char *const region_names[] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001521 "Flash Descriptor", "BIOS", "Management Engine",
Nico Huberd2d39932019-01-18 16:49:37 +01001522 "Gigabit Ethernet", "Platform Data", "Device Expansion",
Nico Huber2a5dfaf2019-07-04 16:01:51 +02001523 "BIOS2", "unknown", "EC/BMC",
Michael Karchera4448d92010-07-22 18:04:15 +00001524 };
Nico Huberd54e4f42017-03-23 23:45:47 +01001525 const char *const region_name = i < ARRAY_SIZE(region_names) ? region_names[i] : "unknown";
1526
Michael Karchera4448d92010-07-22 18:04:15 +00001527 uint32_t base, limit;
Nico Huberaa91d5c2017-08-19 17:04:21 +02001528 int rwperms;
Nico Huberd2d39932019-01-18 16:49:37 +01001529 const int offset = i < 12
1530 ? ICH9_REG_FREG0 + i * 4
1531 : APL_REG_FREG12 + (i - 12) * 4;
Michael Karchera4448d92010-07-22 18:04:15 +00001532 uint32_t freg = mmio_readl(ich_spibar + offset);
1533
Nico Huber42daab12024-07-16 00:27:27 +02001534 if (i < max) {
1535 rwperms = (((bm_wap >> i) & 1) << 1) |
1536 (((bm_rap >> i) & 1) << 0);
Nico Huberaa91d5c2017-08-19 17:04:21 +02001537 } else {
1538 /* Datasheets don't define any access bits for regions > 7. We
1539 can't rely on the actual descriptor settings either as there
1540 are several overrides for them (those by other masters are
1541 not even readable by us, *shrug*). */
1542 rwperms = rwperms_unknown;
1543 }
1544
Michael Karchera4448d92010-07-22 18:04:15 +00001545 base = ICH_FREG_BASE(freg);
1546 limit = ICH_FREG_LIMIT(freg);
Stefan Taunere3adea02012-08-27 15:12:36 +00001547 if (base > limit || (freg == 0 && i > 0)) {
Michael Karchera4448d92010-07-22 18:04:15 +00001548 /* this FREG is disabled */
Nico Huber519be662018-12-23 20:03:35 +01001549 msg_pdbg2("0x%02X: 0x%08x FREG%u: %s region is unused.\n",
Nico Huberd54e4f42017-03-23 23:45:47 +01001550 offset, freg, i, region_name);
Nico Huber7590d1a2016-05-03 13:38:28 +02001551 return NO_PROT;
Stefan Tauner5210e722012-02-16 01:13:00 +00001552 }
1553 msg_pdbg("0x%02X: 0x%08x ", offset, freg);
1554 if (rwperms == 0x3) {
Nico Huber519be662018-12-23 20:03:35 +01001555 msg_pdbg("FREG%u: %s region (0x%08x-0x%08x) is %s.\n", i,
Nico Huber0bb3f712017-03-29 16:44:33 +02001556 region_name, base, limit, access_names[rwperms]);
Nico Huber7590d1a2016-05-03 13:38:28 +02001557 return NO_PROT;
Michael Karchera4448d92010-07-22 18:04:15 +00001558 }
Nico Huberaa91d5c2017-08-19 17:04:21 +02001559 if (rwperms == rwperms_unknown) {
Nico Huber519be662018-12-23 20:03:35 +01001560 msg_pdbg("FREG%u: %s region (0x%08x-0x%08x) has unknown permissions.\n",
Nico Huberaa91d5c2017-08-19 17:04:21 +02001561 i, region_name, base, limit);
Nico Huber7590d1a2016-05-03 13:38:28 +02001562 return NO_PROT;
Nico Huberaa91d5c2017-08-19 17:04:21 +02001563 }
Michael Karchera4448d92010-07-22 18:04:15 +00001564
Nico Huber519be662018-12-23 20:03:35 +01001565 msg_pinfo("FREG%u: %s region (0x%08x-0x%08x) is %s.\n", i,
Nico Huber0bb3f712017-03-29 16:44:33 +02001566 region_name, base, limit, access_names[rwperms]);
Nico Huber7590d1a2016-05-03 13:38:28 +02001567 return access_perms_to_protection[rwperms];
Michael Karchera4448d92010-07-22 18:04:15 +00001568}
1569
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001570 /* In contrast to FRAP and the master section of the descriptor the bits
1571 * in the PR registers have an inverted meaning. The bits in FRAP
1572 * indicate read and write access _grant_. Here they indicate read
1573 * and write _protection_ respectively. If both bits are 0 the address
1574 * bits are ignored.
1575 */
1576#define ICH_PR_PERMS(pr) (((~((pr) >> PR_RP_OFF) & 1) << 0) | \
1577 ((~((pr) >> PR_WP_OFF) & 1) << 1))
1578
Nico Huber519be662018-12-23 20:03:35 +01001579static enum ich_access_protection ich9_handle_pr(const size_t reg_pr0, unsigned int i)
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001580{
Nico Huberd54e4f42017-03-23 23:45:47 +01001581 uint8_t off = reg_pr0 + (i * 4);
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001582 uint32_t pr = mmio_readl(ich_spibar + off);
Stefan Tauner5210e722012-02-16 01:13:00 +00001583 unsigned int rwperms = ICH_PR_PERMS(pr);
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001584
Nico Huberd54e4f42017-03-23 23:45:47 +01001585 /* From 5 on we have GPR registers and start from 0 again. */
1586 const char *const prefix = i >= 5 ? "G" : "";
1587 if (i >= 5)
1588 i -= 5;
1589
Stefan Tauner5210e722012-02-16 01:13:00 +00001590 if (rwperms == 0x3) {
Nico Huberd54e4f42017-03-23 23:45:47 +01001591 msg_pdbg2("0x%02X: 0x%08x (%sPR%u is unused)\n", off, pr, prefix, i);
Nico Huber7590d1a2016-05-03 13:38:28 +02001592 return NO_PROT;
Stefan Tauner5210e722012-02-16 01:13:00 +00001593 }
1594
1595 msg_pdbg("0x%02X: 0x%08x ", off, pr);
Nico Huberd54e4f42017-03-23 23:45:47 +01001596 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 +02001597 ICH_FREG_LIMIT(pr), access_names[rwperms]);
Nico Huber7590d1a2016-05-03 13:38:28 +02001598 return access_perms_to_protection[rwperms];
Stefan Taunerbf69aaa2011-09-17 21:21:48 +00001599}
1600
Stefan Tauner75da80c2011-09-17 22:21:55 +00001601/* Set/Clear the read and write protection enable bits of PR register @i
1602 * according to @read_prot and @write_prot. */
Nico Huberd54e4f42017-03-23 23:45:47 +01001603static void ich9_set_pr(const size_t reg_pr0, int i, int read_prot, int write_prot)
Stefan Tauner75da80c2011-09-17 22:21:55 +00001604{
Nico Huberd54e4f42017-03-23 23:45:47 +01001605 void *addr = ich_spibar + reg_pr0 + (i * 4);
Stefan Tauner75da80c2011-09-17 22:21:55 +00001606 uint32_t old = mmio_readl(addr);
1607 uint32_t new;
1608
1609 msg_gspew("PR%u is 0x%08x", i, old);
1610 new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF));
1611 if (read_prot)
1612 new |= (1 << PR_RP_OFF);
1613 if (write_prot)
1614 new |= (1 << PR_WP_OFF);
1615 if (old == new) {
1616 msg_gspew(" already.\n");
1617 return;
1618 }
1619 msg_gspew(", trying to set it to 0x%08x ", new);
1620 rmmio_writel(new, addr);
1621 msg_gspew("resulted in 0x%08x.\n", mmio_readl(addr));
1622}
1623
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001624static const struct spi_master spi_master_ich7 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +02001625 .max_data_read = 64,
1626 .max_data_write = 64,
1627 .command = ich_spi_send_command,
1628 .multicommand = ich_spi_send_multicommand,
1629 .read = default_spi_read,
1630 .write_256 = default_spi_write_256,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301631 .probe_opcode = ich_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001632};
1633
Nico Hubera1f64762024-07-14 20:23:28 +02001634int ich7_init_spi(void *spibar, enum ich_chipset ich_gen)
1635{
Nico Hubere3f648c2023-02-15 02:55:23 +01001636 struct spi_master mst = spi_master_ich7;
Nico Hubera1f64762024-07-14 20:23:28 +02001637 unsigned int i;
1638
1639 ich_generation = ich_gen;
1640 ich_spibar = spibar;
1641
Nico Huber869f0e72024-07-14 22:58:39 +02001642 swseq_data.reg_preop = ICH7_REG_PREOP;
1643 swseq_data.reg_optype = ICH7_REG_OPTYPE;
1644 swseq_data.reg_opmenu = ICH7_REG_OPMENU;
1645
Nico Hubera1f64762024-07-14 20:23:28 +02001646 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1647 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1648 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1649
1650 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1651 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1652 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1653 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1654 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1655 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1656
1657 for (i = 0; i < 3; i++) {
1658 const int offs = 0x60 + (i * 4);
1659 msg_pdbg("0x%02x: 0x%08x (PBR%u)\n", offs,
1660 mmio_readl(ich_spibar + offs), i);
1661 }
1662
1663 if (mmio_readw(ich_spibar) & (1 << 15)) {
1664 msg_pwarn("WARNING: SPI Configuration Lockdown activated.\n");
1665 ichspi_lock = true;
1666 }
1667
1668 ich_init_opcodes();
Nico Hubera1f64762024-07-14 20:23:28 +02001669
Nico Hubere3f648c2023-02-15 02:55:23 +01001670 ich_set_bbar(0);
1671 if (ichspi_bbar > 0)
1672 mst.features |= SPI_MASTER_TOP_ALIGNED;
1673
1674 return register_spi_master(&mst, 0, NULL);
Nico Hubera1f64762024-07-14 20:23:28 +02001675}
1676
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001677static const struct spi_master spi_master_ich9 = {
Thomas Heijligen43040f22022-06-23 14:38:35 +02001678 .max_data_read = 64,
1679 .max_data_write = 64,
1680 .command = ich_spi_send_command,
1681 .multicommand = ich_spi_send_multicommand,
1682 .read = default_spi_read,
1683 .write_256 = default_spi_write_256,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301684 .probe_opcode = ich_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001685};
1686
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001687static const struct opaque_master opaque_master_ich_hwseq = {
Thomas Heijligen43040f22022-06-23 14:38:35 +02001688 .max_data_read = 64,
1689 .max_data_write = 64,
1690 .probe = ich_hwseq_probe,
1691 .read = ich_hwseq_read,
1692 .write = ich_hwseq_write,
1693 .erase = ich_hwseq_block_erase,
Stefan Tauner50e7c602011-11-08 10:55:54 +00001694};
1695
Nico Hubera1f64762024-07-14 20:23:28 +02001696int ich9_init_spi(void *spibar, enum ich_chipset ich_gen)
Michael Karchera4448d92010-07-22 18:04:15 +00001697{
Nico Huber519be662018-12-23 20:03:35 +01001698 unsigned int i;
Stefan Tauner92d6a862013-10-25 00:33:37 +00001699 uint16_t tmp2;
Michael Karchera4448d92010-07-22 18:04:15 +00001700 uint32_t tmp;
Stefan Tauner50e7c602011-11-08 10:55:54 +00001701 char *arg;
Stefan Tauner5210e722012-02-16 01:13:00 +00001702 int ich_spi_rw_restricted = 0;
Felix Singer8cfc7372022-08-19 03:10:29 +02001703 bool desc_valid = false;
Angel Pons7e134562021-06-07 13:29:13 +02001704 struct ich_descriptors desc = { 0 };
Stefan Tauner50e7c602011-11-08 10:55:54 +00001705 enum ich_spi_mode {
1706 ich_auto,
1707 ich_hwseq,
1708 ich_swseq
1709 } ich_spi_mode = ich_auto;
Nico Huberd54e4f42017-03-23 23:45:47 +01001710 size_t num_freg, num_pr, reg_pr0;
Michael Karchera4448d92010-07-22 18:04:15 +00001711
Stefan Taunera8d838d2011-11-06 23:51:09 +00001712 ich_generation = ich_gen;
Stefan Tauner92d6a862013-10-25 00:33:37 +00001713 ich_spibar = spibar;
Michael Karchera4448d92010-07-22 18:04:15 +00001714
Nico Huberd54e4f42017-03-23 23:45:47 +01001715 /* Moving registers / bits */
Nico Huberfda324b2024-07-14 20:36:21 +02001716 if (ich_generation >= SPI_ENGINE_PCH100) {
David Hendricksa5216362017-08-08 20:02:22 -07001717 num_pr = 6; /* Includes GPR0 */
1718 reg_pr0 = PCH100_REG_FPR0;
1719 swseq_data.reg_ssfsc = PCH100_REG_SSFSC;
1720 swseq_data.reg_preop = PCH100_REG_PREOP;
1721 swseq_data.reg_optype = PCH100_REG_OPTYPE;
1722 swseq_data.reg_opmenu = PCH100_REG_OPMENU;
1723 hwseq_data.addr_mask = PCH100_FADDR_FLA;
1724 hwseq_data.only_4k = true;
1725 hwseq_data.hsfc_fcycle = PCH100_HSFC_FCYCLE;
Nico Huberfda324b2024-07-14 20:36:21 +02001726 } else {
Nico Huberd54e4f42017-03-23 23:45:47 +01001727 num_pr = 5;
1728 reg_pr0 = ICH9_REG_PR0;
1729 swseq_data.reg_ssfsc = ICH9_REG_SSFS;
1730 swseq_data.reg_preop = ICH9_REG_PREOP;
1731 swseq_data.reg_optype = ICH9_REG_OPTYPE;
1732 swseq_data.reg_opmenu = ICH9_REG_OPMENU;
1733 hwseq_data.addr_mask = ICH9_FADDR_FLA;
1734 hwseq_data.only_4k = false;
1735 hwseq_data.hsfc_fcycle = HSFC_FCYCLE;
Nico Huberd2d39932019-01-18 16:49:37 +01001736 }
Nico Hubereeee91b2024-07-14 21:12:21 +02001737
1738 if (ich_generation == CHIPSET_100_SERIES_SUNRISE_POINT)
Nico Huberd2d39932019-01-18 16:49:37 +01001739 num_freg = 10;
Nico Hubereeee91b2024-07-14 21:12:21 +02001740 else if (ich_generation == CHIPSET_C620_SERIES_LEWISBURG)
Nico Huberd2d39932019-01-18 16:49:37 +01001741 num_freg = 12; /* 12 MMIO regs, but 16 regions in FD spec */
Nico Hubereeee91b2024-07-14 21:12:21 +02001742 else if (ich_generation >= SPI_ENGINE_PCH100)
Nico Huberd2d39932019-01-18 16:49:37 +01001743 num_freg = 16;
Nico Hubereeee91b2024-07-14 21:12:21 +02001744 else
Nico Huberd2d39932019-01-18 16:49:37 +01001745 num_freg = 5;
Nico Huberd54e4f42017-03-23 23:45:47 +01001746
Nico Hubera1f64762024-07-14 20:23:28 +02001747 arg = extract_programmer_param("ich_spi_mode");
1748 if (arg && !strcmp(arg, "hwseq")) {
1749 ich_spi_mode = ich_hwseq;
1750 msg_pspew("user selected hwseq\n");
1751 } else if (arg && !strcmp(arg, "swseq")) {
1752 ich_spi_mode = ich_swseq;
1753 msg_pspew("user selected swseq\n");
1754 } else if (arg && !strcmp(arg, "auto")) {
1755 msg_pspew("user selected auto\n");
1756 ich_spi_mode = ich_auto;
1757 } else if (arg && !strlen(arg)) {
1758 msg_perr("Missing argument for ich_spi_mode.\n");
Stefan Tauner50e7c602011-11-08 10:55:54 +00001759 free(arg);
Nico Hubera1f64762024-07-14 20:23:28 +02001760 return ERROR_FATAL;
1761 } else if (arg) {
1762 msg_perr("Unknown argument for ich_spi_mode: %s\n",
1763 arg);
1764 free(arg);
1765 return ERROR_FATAL;
1766 }
1767 free(arg);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001768
Nico Hubera1f64762024-07-14 20:23:28 +02001769 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
1770 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
1771 prettyprint_ich9_reg_hsfs(tmp2);
1772 if (tmp2 & HSFS_FLOCKDN) {
1773 msg_pinfo("SPI Configuration is locked down.\n");
1774 ichspi_lock = true;
1775 }
1776 if (tmp2 & HSFS_FDV)
1777 desc_valid = true;
1778 if (!(tmp2 & HSFS_FDOPSS) && desc_valid)
1779 msg_pinfo("The Flash Descriptor Override Strap-Pin is set. Restrictions implied by\n"
1780 "the Master Section of the flash descriptor are NOT in effect. Please note\n"
1781 "that Protected Range (PR) restrictions still apply.\n");
1782 ich_init_opcodes();
Stefan Tauner55206942011-06-11 09:53:22 +00001783
Nico Hubera1f64762024-07-14 20:23:28 +02001784 if (desc_valid) {
1785 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
1786 msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
1787 prettyprint_ich9_reg_hsfc(tmp2);
1788 }
Michael Karchera4448d92010-07-22 18:04:15 +00001789
Nico Hubera1f64762024-07-14 20:23:28 +02001790 tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
1791 msg_pdbg2("0x08: 0x%08x (FADDR)\n", tmp);
Michael Karchera4448d92010-07-22 18:04:15 +00001792
Nico Huberfda324b2024-07-14 20:36:21 +02001793 if (ich_gen >= SPI_ENGINE_PCH100) {
Nico Hubera1f64762024-07-14 20:23:28 +02001794 tmp = mmio_readl(ich_spibar + PCH100_REG_DLOCK);
1795 msg_pdbg("0x0c: 0x%08x (DLOCK)\n", tmp);
1796 prettyprint_pch100_reg_dlock(tmp);
Nico Hubera1f64762024-07-14 20:23:28 +02001797 }
1798
1799 if (desc_valid) {
1800 tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
1801 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1802 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1803 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1804 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1805 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1806
Nico Huber42daab12024-07-16 00:27:27 +02001807 unsigned int max = 8; /* old, FRAP max. */
1808 uint32_t bm_wap = ICH_BRWA(tmp), bm_rap = ICH_BRRA(tmp);
1809
1810 if (ich_gen >= CHIPSET_HAS_NEW_ACCESS_PERM) {
1811 max = 32;
1812 bm_wap = mmio_readl(ich_spibar + BIOS_BM_WAP);
1813 bm_rap = mmio_readl(ich_spibar + BIOS_BM_RAP);
1814 msg_pdbg("0x%x: 0x%08x (BIOS_BM_WAP)\n", BIOS_BM_WAP, bm_wap);
1815 msg_pdbg("0x%x: 0x%08x (BIOS_BM_RAP)\n", BIOS_BM_RAP, bm_rap);
1816 }
1817
Nico Hubera1f64762024-07-14 20:23:28 +02001818 /* Handle FREGx and FRAP registers */
1819 for (i = 0; i < num_freg; i++)
Nico Huber42daab12024-07-16 00:27:27 +02001820 ich_spi_rw_restricted |= ich9_handle_access_perm(bm_rap, bm_wap, max, i);
Nico Hubera1f64762024-07-14 20:23:28 +02001821 if (ich_spi_rw_restricted)
1822 msg_pinfo("Not all flash regions are freely accessible by flashprog. This is "
1823 "most likely\ndue to an active ME. Please see "
1824 "https://flashprog.org/ME for details.\n");
1825 }
1826
1827 /* Handle PR registers */
1828 for (i = 0; i < num_pr; i++) {
1829 /* if not locked down try to disable PR locks first */
1830 if (!ichspi_lock)
1831 ich9_set_pr(reg_pr0, i, 0, 0);
1832 ich_spi_rw_restricted |= ich9_handle_pr(reg_pr0, i);
1833 }
1834
1835 switch (ich_spi_rw_restricted) {
1836 case WRITE_PROT:
1837 msg_pwarn("At least some flash regions are write protected. For write operations,\n"
1838 "you should use a flash layout and include only writable regions. See\n"
1839 "manpage for more details.\n");
1840 break;
1841 case READ_PROT:
1842 case LOCKED:
1843 msg_pwarn("At least some flash regions are read protected. You have to use a flash\n"
1844 "layout and include only accessible regions. For write operations, you'll\n"
1845 "additionally need the --noverify-all switch. See manpage for more details.\n"
1846 );
1847 break;
1848 }
1849
1850 tmp = mmio_readl(ich_spibar + swseq_data.reg_ssfsc);
1851 msg_pdbg("0x%zx: 0x%02x (SSFS)\n", swseq_data.reg_ssfsc, tmp & 0xff);
1852 prettyprint_ich9_reg_ssfs(tmp);
1853 if (tmp & SSFS_FCERR) {
1854 msg_pdbg("Clearing SSFS.FCERR\n");
1855 mmio_writeb(SSFS_FCERR, ich_spibar + swseq_data.reg_ssfsc);
1856 }
1857 msg_pdbg("0x%zx: 0x%06x (SSFC)\n", swseq_data.reg_ssfsc + 1, tmp >> 8);
1858 prettyprint_ich9_reg_ssfc(tmp);
1859
1860 msg_pdbg("0x%zx: 0x%04x (PREOP)\n",
1861 swseq_data.reg_preop, mmio_readw(ich_spibar + swseq_data.reg_preop));
1862 msg_pdbg("0x%zx: 0x%04x (OPTYPE)\n",
1863 swseq_data.reg_optype, mmio_readw(ich_spibar + swseq_data.reg_optype));
1864 msg_pdbg("0x%zx: 0x%08x (OPMENU)\n",
1865 swseq_data.reg_opmenu, mmio_readl(ich_spibar + swseq_data.reg_opmenu));
1866 msg_pdbg("0x%zx: 0x%08x (OPMENU+4)\n",
1867 swseq_data.reg_opmenu + 4, mmio_readl(ich_spibar + swseq_data.reg_opmenu + 4));
1868
1869 if (desc_valid) {
Nico Huberfda324b2024-07-14 20:36:21 +02001870 if (ich_gen < SPI_ENGINE_PCH100 &&
1871 ich_gen != CHIPSET_ICH8 &&
1872 ich_gen != CHIPSET_BAYTRAIL) {
Nico Hubera1f64762024-07-14 20:23:28 +02001873 ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
1874 msg_pdbg("0x%x: 0x%08x (BBAR)\n", ICH9_REG_BBAR, ichspi_bbar);
1875 ich_set_bbar(0);
Nico Huberd54e4f42017-03-23 23:45:47 +01001876 }
1877
Nico Hubera1f64762024-07-14 20:23:28 +02001878 if (ich_gen == CHIPSET_ICH8) {
1879 tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC);
1880 msg_pdbg("0x%x: 0x%08x (VSCC)\n", ICH8_REG_VSCC, tmp);
1881 msg_pdbg("VSCC: ");
1882 prettyprint_ich_reg_vscc(tmp, FLASHPROG_MSG_DEBUG, true);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001883 } else {
Nico Hubera1f64762024-07-14 20:23:28 +02001884 tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC);
1885 msg_pdbg("0x%x: 0x%08x (LVSCC)\n", ICH9_REG_LVSCC, tmp);
1886 msg_pdbg("LVSCC: ");
1887 prettyprint_ich_reg_vscc(tmp, FLASHPROG_MSG_DEBUG, true);
1888
1889 tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC);
1890 msg_pdbg("0x%x: 0x%08x (UVSCC)\n", ICH9_REG_UVSCC, tmp);
1891 msg_pdbg("UVSCC: ");
1892 prettyprint_ich_reg_vscc(tmp, FLASHPROG_MSG_DEBUG, false);
Stefan Tauner50e7c602011-11-08 10:55:54 +00001893 }
Nico Hubera1f64762024-07-14 20:23:28 +02001894
Nico Huberfda324b2024-07-14 20:36:21 +02001895 if (ich_gen < SPI_ENGINE_PCH100 && ich_gen != CHIPSET_ICH8) {
Nico Hubera1f64762024-07-14 20:23:28 +02001896 tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
1897 msg_pdbg("0x%x: 0x%08x (FPB)\n", ICH9_REG_FPB, tmp);
Nico Hubera1f64762024-07-14 20:23:28 +02001898 }
1899
1900 if (read_ich_descriptors_via_fdo(ich_gen, ich_spibar, &desc) == ICH_RET_OK)
1901 prettyprint_ich_descriptors(ich_gen, &desc);
1902
1903 /* If the descriptor is valid and indicates multiple
1904 * flash devices we need to use hwseq to be able to
1905 * access the second flash device.
1906 */
1907 if (ich_spi_mode == ich_auto && desc.content.NC != 0) {
1908 msg_pinfo("Enabling hardware sequencing due to "
1909 "multiple flash chips detected.\n");
1910 ich_spi_mode = ich_hwseq;
1911 }
1912 }
1913
1914 if (ich_spi_mode == ich_auto && ichspi_lock &&
1915 ich_missing_opcodes()) {
1916 msg_pinfo("Enabling hardware sequencing because "
1917 "some important opcode is locked.\n");
1918 ich_spi_mode = ich_hwseq;
1919 }
1920
Nico Hubere8babf42024-07-14 20:40:56 +02001921 if (ich_spi_mode == ich_auto && ich_gen >= SPI_ENGINE_PCH100) {
1922 msg_pdbg("Enabling hardware sequencing by default for 100+ series SPI.\n");
Nico Hubera1f64762024-07-14 20:23:28 +02001923 ich_spi_mode = ich_hwseq;
1924 }
1925
1926 if (ich_spi_mode == ich_hwseq) {
1927 if (!desc_valid) {
1928 msg_perr("Hardware sequencing was requested "
1929 "but the flash descriptor is not "
1930 "valid. Aborting.\n");
1931 return ERROR_FATAL;
1932 }
1933
1934 int tmpi = getFCBA_component_density(ich_generation, &desc, 0);
1935 if (tmpi < 0) {
1936 msg_perr("Could not determine density of flash component %d.\n", 0);
1937 return ERROR_FATAL;
1938 }
1939 hwseq_data.size_comp0 = tmpi;
1940
1941 tmpi = getFCBA_component_density(ich_generation, &desc, 1);
1942 if (tmpi < 0) {
1943 msg_perr("Could not determine density of flash component %d.\n", 1);
1944 return ERROR_FATAL;
1945 }
1946 hwseq_data.size_comp1 = tmpi;
1947
1948 register_opaque_master(&opaque_master_ich_hwseq, NULL);
1949 } else {
1950 register_spi_master(&spi_master_ich9, 0, NULL);
Michael Karchera4448d92010-07-22 18:04:15 +00001951 }
1952
Michael Karchera4448d92010-07-22 18:04:15 +00001953 return 0;
1954}
1955
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001956static const struct spi_master spi_master_via = {
Thomas Heijligen43040f22022-06-23 14:38:35 +02001957 .max_data_read = 16,
1958 .max_data_write = 16,
1959 .command = ich_spi_send_command,
1960 .multicommand = ich_spi_send_multicommand,
1961 .read = default_spi_read,
1962 .write_256 = default_spi_write_256,
Aarya Chaumal0cea7532022-07-04 18:21:50 +05301963 .probe_opcode = ich_spi_probe_opcode,
Michael Karcherb9dbe482011-05-11 17:07:07 +00001964};
1965
Nico Huber560111e2017-04-26 12:27:17 +02001966int via_init_spi(uint32_t mmio_base)
Michael Karchera4448d92010-07-22 18:04:15 +00001967{
Nico Hubere3f648c2023-02-15 02:55:23 +01001968 struct spi_master mst = spi_master_via;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001969 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00001970
Stefan Tauner7fb5aa02013-08-14 15:48:44 +00001971 ich_spibar = rphysmap("VIA SPI MMIO registers", mmio_base, 0x70);
1972 if (ich_spibar == ERROR_PTR)
1973 return ERROR_FATAL;
Helge Wagnerdd73d832012-08-24 23:03:46 +00001974 /* Do we really need no write enable? Like the LPC one at D17F0 0x40 */
Michael Karchera4448d92010-07-22 18:04:15 +00001975
Michael Karchera4448d92010-07-22 18:04:15 +00001976 /* Not sure if it speaks all these bus protocols. */
Nico Huber2e50cdc2018-09-23 20:20:26 +02001977 internal_buses_supported &= BUS_LPC | BUS_FWH;
Stefan Taunera8d838d2011-11-06 23:51:09 +00001978 ich_generation = CHIPSET_ICH7;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001979
1980 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1981 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1982 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1983 for (i = 0; i < 2; i++) {
1984 int offs;
1985 offs = 8 + (i * 8);
1986 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1987 mmio_readl(ich_spibar + offs), i);
1988 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1989 mmio_readl(ich_spibar + offs + 4), i);
1990 }
1991 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1992 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1993 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1994 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1995 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1996 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1997 for (i = 0; i < 3; i++) {
1998 int offs;
1999 offs = 0x60 + (i * 4);
2000 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
2001 mmio_readl(ich_spibar + offs), i);
2002 }
2003 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
2004 mmio_readw(ich_spibar + 0x6c));
2005 if (mmio_readw(ich_spibar) & (1 << 15)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +00002006 msg_pwarn("Warning: SPI Configuration Lockdown activated.\n");
Felix Singer8cfc7372022-08-19 03:10:29 +02002007 ichspi_lock = true;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00002008 }
2009
Michael Karchera4448d92010-07-22 18:04:15 +00002010 ich_init_opcodes();
2011
Nico Hubere3f648c2023-02-15 02:55:23 +01002012 ich_set_bbar(0);
2013 if (ichspi_bbar > 0)
2014 mst.features |= SPI_MASTER_TOP_ALIGNED;
2015
2016 return register_spi_master(&mst, 0, NULL);
Michael Karchera4448d92010-07-22 18:04:15 +00002017}