blob: 194588d387f97d50ef931469673d5f4abd436c93 [file] [log] [blame]
hailfinger82e7ddb2008-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>
stepandbd3af12008-06-27 16:28:34 +00007 * Copyright (C) 2008 coresystems GmbH <info@coresystems.de>
hailfinger39d159a2010-05-21 23:09:42 +00008 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
David Hendricksce6b2fa2011-07-11 22:12:43 -07009 * Copyright (C) 2011 Stefan Tauner
hailfinger82e7ddb2008-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.
hailfinger82e7ddb2008-05-16 12:55:55 +000020 */
21
stefanctfa66c622011-08-09 01:49:34 +000022#if defined(__i386__) || defined(__x86_64__)
hailfinger324a9cc2010-05-26 01:45:41 +000023
hailfinger82e7ddb2008-05-16 12:55:55 +000024#include <string.h>
stefanct3d3b6ee2011-10-20 12:57:14 +000025#include <stdlib.h>
hailfinger82e7ddb2008-05-16 12:55:55 +000026#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000027#include "programmer.h"
Mayur Panchalf4796862019-08-05 15:46:12 +100028#include "hwaccess.h"
hailfinger82e7ddb2008-05-16 12:55:55 +000029#include "spi.h"
David Hendricksce6b2fa2011-07-11 22:12:43 -070030#include "ich_descriptors.h"
Edward O'Callaghand13334a2020-07-23 12:51:00 +100031#include "action_descriptor.h"
Nikolai Artemiev1ea569a2021-05-10 12:59:36 +100032#include "chipdrivers.h"
hailfinger82e7ddb2008-05-16 12:55:55 +000033
Edward O'Callaghan4eac7482020-05-26 21:54:52 +100034/* Apollo Lake */
35#define APL_REG_FREG12 0xe0 /* 32 Bytes Flash Region 12 */
36
Edward O'Callaghanbff65042020-05-18 17:53:07 +100037/* Sunrise Point */
38
39/* Added HSFS Status bits */
40#define HSFS_WRSDIS_OFF 11 /* 11: Flash Configuration Lock-Down */
41#define HSFS_WRSDIS (0x1 << HSFS_WRSDIS_OFF)
42#define HSFS_PRR34_LOCKDN_OFF 12 /* 12: PRR3 PRR4 Lock-Down */
43#define HSFS_PRR34_LOCKDN (0x1 << HSFS_PRR34_LOCKDN_OFF)
44/* HSFS_BERASE vanished */
45
46/*
47 * HSFC and HSFS 16-bit registers are combined into the 32-bit
48 * BIOS_HSFSTS_CTL register in the Sunrise Point datasheet,
49 * however we still treat them separately in order to reuse code.
50 */
51
52/* Changed HSFC Control bits */
53#define PCH100_HSFC_FCYCLE_OFF (17 - 16) /* 1-4: FLASH Cycle */
54#define PCH100_HSFC_FCYCLE (0xf << PCH100_HSFC_FCYCLE_OFF)
55/* New HSFC Control bit */
56#define HSFC_WET_OFF (21 - 16) /* 5: Write Enable Type */
57#define HSFC_WET (0x1 << HSFC_WET_OFF)
58
Edward O'Callaghanc093d682020-05-26 17:53:53 +100059#define PCH100_FADDR_FLA 0x07ffffff
60
Edward O'Callaghan78590d62020-07-04 15:41:20 +100061#define PCH100_REG_DLOCK 0x0c /* 32 Bits Discrete Lock Bits */
62#define DLOCK_BMWAG_LOCKDN_OFF 0
63#define DLOCK_BMWAG_LOCKDN (0x1 << DLOCK_BMWAG_LOCKDN_OFF)
64#define DLOCK_BMRAG_LOCKDN_OFF 1
65#define DLOCK_BMRAG_LOCKDN (0x1 << DLOCK_BMRAG_LOCKDN_OFF)
66#define DLOCK_SBMWAG_LOCKDN_OFF 2
67#define DLOCK_SBMWAG_LOCKDN (0x1 << DLOCK_SBMWAG_LOCKDN_OFF)
68#define DLOCK_SBMRAG_LOCKDN_OFF 3
69#define DLOCK_SBMRAG_LOCKDN (0x1 << DLOCK_SBMRAG_LOCKDN_OFF)
70#define DLOCK_PR0_LOCKDN_OFF 8
71#define DLOCK_PR0_LOCKDN (0x1 << DLOCK_PR0_LOCKDN_OFF)
72#define DLOCK_PR1_LOCKDN_OFF 9
73#define DLOCK_PR1_LOCKDN (0x1 << DLOCK_PR1_LOCKDN_OFF)
74#define DLOCK_PR2_LOCKDN_OFF 10
75#define DLOCK_PR2_LOCKDN (0x1 << DLOCK_PR2_LOCKDN_OFF)
76#define DLOCK_PR3_LOCKDN_OFF 11
77#define DLOCK_PR3_LOCKDN (0x1 << DLOCK_PR3_LOCKDN_OFF)
78#define DLOCK_PR4_LOCKDN_OFF 12
79#define DLOCK_PR4_LOCKDN (0x1 << DLOCK_PR4_LOCKDN_OFF)
80#define DLOCK_SSEQ_LOCKDN_OFF 16
81#define DLOCK_SSEQ_LOCKDN (0x1 << DLOCK_SSEQ_LOCKDN_OFF)
Edward O'Callaghan6be39a82020-05-18 18:02:02 +100082
Edward O'Callaghan0f183122020-08-01 21:17:36 +100083/* Control bits */
84#define HSFSC_FGO_OFF 16 /* 0: Flash Cycle Go */
85#define HSFSC_FGO (0x1 << HSFSC_FGO_OFF)
86#define HSFSC_FCYCLE_OFF 17 /* 17-20: FLASH Cycle */
87#define HSFSC_FCYCLE (0xf << HSFSC_FCYCLE_OFF)
88#define HSFSC_FDBC_OFF 24 /* 24-29 : Flash Data Byte Count */
89#define HSFSC_FDBC (0x3f << HSFSC_FDBC_OFF)
90
Edward O'Callaghan822037f2020-08-01 19:56:11 +100091#define PCH100_REG_FPR0 0x84 /* 32 Bits Protected Range 0 */
92#define PCH100_REG_GPR0 0x98 /* 32 Bits Global Protected Range 0 */
93#define PCH100_REG_HSFSC 0x04
94
Edward O'Callaghan78590d62020-07-04 15:41:20 +100095#define PCH100_REG_SSFSC 0xA0 /* 32 Bits Status (8) + Control (24) */
96#define PCH100_REG_PREOP 0xA4 /* 16 Bits */
97#define PCH100_REG_OPTYPE 0xA6 /* 16 Bits */
98#define PCH100_REG_OPMENU 0xA8 /* 64 Bits */
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +100099
stepandbd3af12008-06-27 16:28:34 +0000100/* ICH9 controller register definition */
stefanctc274c862011-06-11 09:53:22 +0000101#define ICH9_REG_HSFS 0x04 /* 16 Bits Hardware Sequencing Flash Status */
102#define HSFS_FDONE_OFF 0 /* 0: Flash Cycle Done */
103#define HSFS_FDONE (0x1 << HSFS_FDONE_OFF)
104#define HSFS_FCERR_OFF 1 /* 1: Flash Cycle Error */
105#define HSFS_FCERR (0x1 << HSFS_FCERR_OFF)
106#define HSFS_AEL_OFF 2 /* 2: Access Error Log */
107#define HSFS_AEL (0x1 << HSFS_AEL_OFF)
108#define HSFS_BERASE_OFF 3 /* 3-4: Block/Sector Erase Size */
109#define HSFS_BERASE (0x3 << HSFS_BERASE_OFF)
110#define HSFS_SCIP_OFF 5 /* 5: SPI Cycle In Progress */
111#define HSFS_SCIP (0x1 << HSFS_SCIP_OFF)
112 /* 6-12: reserved */
113#define HSFS_FDOPSS_OFF 13 /* 13: Flash Descriptor Override Pin-Strap Status */
114#define HSFS_FDOPSS (0x1 << HSFS_FDOPSS_OFF)
115#define HSFS_FDV_OFF 14 /* 14: Flash Descriptor Valid */
116#define HSFS_FDV (0x1 << HSFS_FDV_OFF)
117#define HSFS_FLOCKDN_OFF 15 /* 15: Flash Configuration Lock-Down */
118#define HSFS_FLOCKDN (0x1 << HSFS_FLOCKDN_OFF)
119
120#define ICH9_REG_HSFC 0x06 /* 16 Bits Hardware Sequencing Flash Control */
121#define HSFC_FGO_OFF 0 /* 0: Flash Cycle Go */
122#define HSFC_FGO (0x1 << HSFC_FGO_OFF)
123#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
124#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
125 /* 3-7: reserved */
126#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
127#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
128 /* 14: reserved */
129#define HSFC_SME_OFF 15 /* 15: SPI SMI# Enable */
130#define HSFC_SME (0x1 << HSFC_SME_OFF)
131
stefanct707f13b2011-05-19 02:58:17 +0000132#define ICH9_REG_FADDR 0x08 /* 32 Bits */
Edward O'Callaghanc093d682020-05-26 17:53:53 +1000133#define ICH9_FADDR_FLA 0x01ffffff
stefanct707f13b2011-05-19 02:58:17 +0000134#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
stepandbd3af12008-06-27 16:28:34 +0000135
stefanct24bda702011-06-12 08:14:10 +0000136#define ICH9_REG_FRAP 0x50 /* 32 Bytes Flash Region Access Permissions */
137#define ICH9_REG_FREG0 0x54 /* 32 Bytes Flash Region 0 */
138
139#define ICH9_REG_PR0 0x74 /* 32 Bytes Protected Range 0 */
stefanct7ab834a2011-09-17 21:21:48 +0000140#define PR_WP_OFF 31 /* 31: write protection enable */
141#define PR_RP_OFF 15 /* 15: read protection enable */
stefanct24bda702011-06-12 08:14:10 +0000142
stefanct707f13b2011-05-19 02:58:17 +0000143#define ICH9_REG_SSFS 0x90 /* 08 Bits */
stefanct04009ff2011-06-11 09:53:09 +0000144#define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */
145#define SSFS_SCIP (0x1 << SSFS_SCIP_OFF)
146#define SSFS_FDONE_OFF 2 /* Cycle Done Status */
147#define SSFS_FDONE (0x1 << SSFS_FDONE_OFF)
148#define SSFS_FCERR_OFF 3 /* Flash Cycle Error */
149#define SSFS_FCERR (0x1 << SSFS_FCERR_OFF)
150#define SSFS_AEL_OFF 4 /* Access Error Log */
151#define SSFS_AEL (0x1 << SSFS_AEL_OFF)
stefanct707f13b2011-05-19 02:58:17 +0000152/* The following bits are reserved in SSFS: 1,5-7. */
hailfinger01d05912011-03-17 00:10:25 +0000153#define SSFS_RESERVED_MASK 0x000000e2
stepandbd3af12008-06-27 16:28:34 +0000154
stefanct707f13b2011-05-19 02:58:17 +0000155#define ICH9_REG_SSFC 0x91 /* 24 Bits */
stefanct707f13b2011-05-19 02:58:17 +0000156/* We combine SSFS and SSFC to one 32-bit word,
stefanct04009ff2011-06-11 09:53:09 +0000157 * therefore SSFC bits are off by 8. */
158 /* 0: reserved */
159#define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */
160#define SSFC_SCGO (0x1 << SSFC_SCGO_OFF)
161#define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */
162#define SSFC_ACS (0x1 << SSFC_ACS_OFF)
163#define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */
164#define SSFC_SPOP (0x1 << SSFC_SPOP_OFF)
165#define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */
166#define SSFC_COP (0x7 << SSFC_COP_OFF)
167 /* 7: reserved */
168#define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */
169#define SSFC_DBC (0x3f << SSFC_DBC_OFF)
170#define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */
171#define SSFC_DS (0x1 << SSFC_DS_OFF)
172#define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */
173#define SSFC_SME (0x1 << SSFC_SME_OFF)
174#define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */
175#define SSFC_SCF (0x7 << SSFC_SCF_OFF)
176#define SSFC_SCF_20MHZ 0x00000000
177#define SSFC_SCF_33MHZ 0x01000000
178 /* 19-23: reserved */
hailfinger01d05912011-03-17 00:10:25 +0000179#define SSFC_RESERVED_MASK 0xf8008100
stepandbd3af12008-06-27 16:28:34 +0000180
stefanct707f13b2011-05-19 02:58:17 +0000181#define ICH9_REG_PREOP 0x94 /* 16 Bits */
182#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
183#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
hailfinger82e7ddb2008-05-16 12:55:55 +0000184
stefanct24bda702011-06-12 08:14:10 +0000185#define ICH9_REG_BBAR 0xA0 /* 32 Bits BIOS Base Address Configuration */
186#define BBAR_MASK 0x00ffff00 /* 8-23: Bottom of System Flash */
187
stefanct1fc3a732011-09-15 23:52:55 +0000188#define ICH8_REG_VSCC 0xC1 /* 32 Bits Vendor Specific Component Capabilities */
David Hendricksce6b2fa2011-07-11 22:12:43 -0700189#define ICH9_REG_LVSCC 0xC4 /* 32 Bits Host Lower Vendor Specific Component Capabilities */
190#define ICH9_REG_UVSCC 0xC8 /* 32 Bits Host Upper Vendor Specific Component Capabilities */
191/* The individual fields of the VSCC registers are defined in the file
stefanct1fc3a732011-09-15 23:52:55 +0000192 * ich_descriptors.h. The reason is that the same layout is also used in the
193 * flash descriptor to define the properties of the different flash chips
194 * supported. The BIOS (or the ME?) is responsible to populate the ICH registers
195 * with the information from the descriptor on startup depending on the actual
196 * chip(s) detected. */
David Hendricksce6b2fa2011-07-11 22:12:43 -0700197
stefanctd68db982011-07-01 00:39:16 +0000198#define ICH9_REG_FPB 0xD0 /* 32 Bits Flash Partition Boundary */
199#define FPB_FPBA_OFF 0 /* 0-12: Block/Sector Erase Size */
200#define FPB_FPBA (0x1FFF << FPB_FPBA_OFF)
201
David Hendricks210975e2015-08-25 21:36:13 +0000202// ICH9R SPI commands
203#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
204#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
205#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
206#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
207
stepandbd3af12008-06-27 16:28:34 +0000208// ICH7 registers
stefanct707f13b2011-05-19 02:58:17 +0000209#define ICH7_REG_SPIS 0x00 /* 16 Bits */
hailfinger01d05912011-03-17 00:10:25 +0000210#define SPIS_SCIP 0x0001
211#define SPIS_GRANT 0x0002
212#define SPIS_CDS 0x0004
213#define SPIS_FCERR 0x0008
214#define SPIS_RESERVED_MASK 0x7ff0
stepandbd3af12008-06-27 16:28:34 +0000215
ruik9bc51c02008-06-30 21:38:30 +0000216/* VIA SPI is compatible with ICH7, but maxdata
217 to transfer is 16 bytes.
218
219 DATA byte count on ICH7 is 8:13, on VIA 8:11
220
221 bit 12 is port select CS0 CS1
222 bit 13 is FAST READ enable
223 bit 7 is used with fast read and one shot controls CS de-assert?
224*/
225
stefanct707f13b2011-05-19 02:58:17 +0000226#define ICH7_REG_SPIC 0x02 /* 16 Bits */
227#define SPIC_SCGO 0x0002
228#define SPIC_ACS 0x0004
229#define SPIC_SPOP 0x0008
230#define SPIC_DS 0x4000
stepandbd3af12008-06-27 16:28:34 +0000231
stefanct707f13b2011-05-19 02:58:17 +0000232#define ICH7_REG_SPIA 0x04 /* 32 Bits */
233#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
234#define ICH7_REG_PREOP 0x54 /* 16 Bits */
235#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
236#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
stepandbd3af12008-06-27 16:28:34 +0000237
Edward O'Callaghan4eac7482020-05-26 21:54:52 +1000238enum ich_access_protection {
239 NO_PROT = 0,
240 READ_PROT = 1,
241 WRITE_PROT = 2,
242 LOCKED = 3,
243};
244
stepane1a13b92008-12-08 18:16:58 +0000245/* ICH SPI configuration lock-down. May be set during chipset enabling. */
mkarcher74d30132010-07-22 18:04:15 +0000246static int ichspi_lock = 0;
stepane1a13b92008-12-08 18:16:58 +0000247
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +1100248enum ich_chipset ich_generation = CHIPSET_ICH_UNKNOWN;
Edward O'Callaghan688d34a2020-11-28 17:56:25 +1100249static uint32_t ichspi_bbar = 0;
hailfingerb767c122010-05-28 15:53:08 +0000250
mkarcher74d30132010-07-22 18:04:15 +0000251static void *ich_spibar = NULL;
hailfinger1ff33dc2010-07-03 11:02:10 +0000252
David Hendricks210975e2015-08-25 21:36:13 +0000253typedef struct _OPCODE {
254 uint8_t opcode; //This commands spi opcode
255 uint8_t spi_type; //This commands spi type
256 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
257} OPCODE;
258
hailfinger82e32492010-02-11 11:28:37 +0000259/* Suggested opcode definition:
hailfinger82e7ddb2008-05-16 12:55:55 +0000260 * Preop 1: Write Enable
261 * Preop 2: Write Status register enable
262 *
263 * OP 0: Write address
264 * OP 1: Read Address
265 * OP 2: ERASE block
266 * OP 3: Read Status register
267 * OP 4: Read ID
268 * OP 5: Write Status register
hailfinger82e32492010-02-11 11:28:37 +0000269 * OP 6: chip private (read JEDEC id)
hailfinger82e7ddb2008-05-16 12:55:55 +0000270 * OP 7: Chip erase
271 */
272typedef struct _OPCODES {
273 uint8_t preop[2];
274 OPCODE opcode[8];
275} OPCODES;
276
stepan8f46dd62008-06-27 15:18:20 +0000277static OPCODES *curopcodes = NULL;
hailfinger82e7ddb2008-05-16 12:55:55 +0000278
279/* HW access functions */
uweabe92a52009-05-16 22:36:00 +0000280static uint32_t REGREAD32(int X)
hailfinger82e7ddb2008-05-16 12:55:55 +0000281{
hailfinger1ff33dc2010-07-03 11:02:10 +0000282 return mmio_readl(ich_spibar + X);
stepandbd3af12008-06-27 16:28:34 +0000283}
284
uweabe92a52009-05-16 22:36:00 +0000285static uint16_t REGREAD16(int X)
stepandbd3af12008-06-27 16:28:34 +0000286{
hailfinger1ff33dc2010-07-03 11:02:10 +0000287 return mmio_readw(ich_spibar + X);
hailfinger82e7ddb2008-05-16 12:55:55 +0000288}
289
hailfinger01d05912011-03-17 00:10:25 +0000290static uint16_t REGREAD8(int X)
291{
292 return mmio_readb(ich_spibar + X);
293}
294
stefanct15f3b932011-07-01 00:39:01 +0000295#define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
296#define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
297#define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off))
hailfinger82e7ddb2008-05-16 12:55:55 +0000298
hailfinger82e7ddb2008-05-16 12:55:55 +0000299/* Common SPI functions */
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100300
301static int find_opcode(OPCODES *op, uint8_t opcode)
302{
303 int a;
304
305 if (op == NULL) {
306 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
307 return -1;
308 }
309
310 for (a = 0; a < 8; a++) {
311 if (op->opcode[a].opcode == opcode)
312 return a;
313 }
314
315 return -1;
316}
317
318static int find_preop(OPCODES *op, uint8_t preop)
319{
320 int a;
321
322 if (op == NULL) {
323 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
324 return -1;
325 }
326
327 for (a = 0; a < 2; a++) {
328 if (op->preop[a] == preop)
329 return a;
330 }
331
332 return -1;
333}
hailfinger82e7ddb2008-05-16 12:55:55 +0000334
stuge7a651552008-12-15 02:32:11 +0000335/* for pairing opcodes with their required preop */
336struct preop_opcode_pair {
337 uint8_t preop;
338 uint8_t opcode;
339};
340
hailfinger82e32492010-02-11 11:28:37 +0000341/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
hailfinger1ff33dc2010-07-03 11:02:10 +0000342const struct preop_opcode_pair pops[] = {
stuge7a651552008-12-15 02:32:11 +0000343 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
344 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
345 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
346 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
347 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
348 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
hailfinger82e32492010-02-11 11:28:37 +0000349 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
350 {JEDEC_WREN, JEDEC_WRSR},
stuge7a651552008-12-15 02:32:11 +0000351 {JEDEC_EWSR, JEDEC_WRSR},
352 {0,}
353};
354
hailfinger82e32492010-02-11 11:28:37 +0000355/* Reasonable default configuration. Needs ad-hoc modifications if we
356 * encounter unlisted opcodes. Fun.
357 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000358static OPCODES O_ST_M25P = {
hailfinger82e7ddb2008-05-16 12:55:55 +0000359 {
360 JEDEC_WREN,
hailfinger82e32492010-02-11 11:28:37 +0000361 JEDEC_EWSR,
362 },
hailfinger82e7ddb2008-05-16 12:55:55 +0000363 {
hailfinger82e32492010-02-11 11:28:37 +0000364 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
stepan8f46dd62008-06-27 15:18:20 +0000365 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
David Hendricks813dd7a2010-08-26 21:27:17 -0700366 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
stepan8f46dd62008-06-27 15:18:20 +0000367 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
hailfingere092f842009-05-26 21:25:08 +0000368 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
hailfinger82e32492010-02-11 11:28:37 +0000369 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
stepan8f46dd62008-06-27 15:18:20 +0000370 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
hailfinger82e32492010-02-11 11:28:37 +0000371 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
372 }
hailfinger82e7ddb2008-05-16 12:55:55 +0000373};
374
hailfinger4c973122010-10-05 22:06:05 +0000375/* List of opcodes with their corresponding spi_type
376 * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
377 * is needed which is currently not in the chipset OPCODE table
378 */
379static OPCODE POSSIBLE_OPCODES[] = {
380 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
381 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
382 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
383 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
384 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
385 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
386 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
387 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
388 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
389 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
390 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
391};
392
hailfinger1ff33dc2010-07-03 11:02:10 +0000393static OPCODES O_EXISTING = {};
stepane1a13b92008-12-08 18:16:58 +0000394
stefancte4d1ef52011-06-11 09:53:16 +0000395/* pretty printing functions */
David Hendricksce6b2fa2011-07-11 22:12:43 -0700396static void prettyprint_opcodes(OPCODES *ops)
stefancte4d1ef52011-06-11 09:53:16 +0000397{
stefanct0aaebc42011-09-17 19:53:11 +0000398 OPCODE oc;
399 const char *t;
400 const char *a;
401 uint8_t i;
402 static const char *const spi_type[4] = {
403 "read w/o addr",
404 "write w/o addr",
405 "read w/ addr",
406 "write w/ addr"
407 };
408 static const char *const atomic_type[3] = {
409 "none",
410 " 0 ",
411 " 1 "
412 };
413
414 if (ops == NULL)
stefancte4d1ef52011-06-11 09:53:16 +0000415 return;
416
stefanct0aaebc42011-09-17 19:53:11 +0000417 msg_pdbg2(" OP Type Pre-OP\n");
stefancte4d1ef52011-06-11 09:53:16 +0000418 for (i = 0; i < 8; i++) {
419 oc = ops->opcode[i];
stefanct0aaebc42011-09-17 19:53:11 +0000420 t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type];
421 a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic];
422 msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a);
stefancte4d1ef52011-06-11 09:53:16 +0000423 }
stefanct0aaebc42011-09-17 19:53:11 +0000424 msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0],
425 ops->preop[1]);
stefancte4d1ef52011-06-11 09:53:16 +0000426}
427
Edward O'Callaghanbff65042020-05-18 17:53:07 +1000428#define _pprint_reg(bit, mask, off, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & mask) >> off)
429#define pprint_reg(reg, bit, val, sep) _pprint_reg(bit, reg##_##bit, reg##_##bit##_OFF, val, sep)
stefanct1fc3a732011-09-15 23:52:55 +0000430
Edward O'Callaghanacce4622020-07-16 15:39:19 +1000431static void prettyprint_ich9_reg_hsfs(uint16_t reg_val, enum ich_chipset ich_gen)
stefanctc274c862011-06-11 09:53:22 +0000432{
433 msg_pdbg("HSFS: ");
434 pprint_reg(HSFS, FDONE, reg_val, ", ");
435 pprint_reg(HSFS, FCERR, reg_val, ", ");
436 pprint_reg(HSFS, AEL, reg_val, ", ");
Edward O'Callaghanacce4622020-07-16 15:39:19 +1000437 switch (ich_gen) {
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +1000438 case CHIPSET_100_SERIES_SUNRISE_POINT:
439 case CHIPSET_C620_SERIES_LEWISBURG:
440 case CHIPSET_300_SERIES_CANNON_POINT:
Matt DeVillier81bc4d32020-08-12 12:48:06 -0500441 case CHIPSET_400_SERIES_COMET_POINT:
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +1000442 break;
443 default:
Edward O'Callaghanbff65042020-05-18 17:53:07 +1000444 pprint_reg(HSFS, BERASE, reg_val, ", ");
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +1000445 break;
Edward O'Callaghanbff65042020-05-18 17:53:07 +1000446 }
stefanctc274c862011-06-11 09:53:22 +0000447 pprint_reg(HSFS, SCIP, reg_val, ", ");
Edward O'Callaghanacce4622020-07-16 15:39:19 +1000448 switch (ich_gen) {
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +1000449 case CHIPSET_100_SERIES_SUNRISE_POINT:
450 case CHIPSET_C620_SERIES_LEWISBURG:
451 case CHIPSET_300_SERIES_CANNON_POINT:
Matt DeVillier81bc4d32020-08-12 12:48:06 -0500452 case CHIPSET_400_SERIES_COMET_POINT:
Edward O'Callaghanbff65042020-05-18 17:53:07 +1000453 pprint_reg(HSFS, PRR34_LOCKDN, reg_val, ", ");
454 pprint_reg(HSFS, WRSDIS, reg_val, ", ");
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +1000455 break;
456 default:
457 break;
Edward O'Callaghanbff65042020-05-18 17:53:07 +1000458 }
stefanctc274c862011-06-11 09:53:22 +0000459 pprint_reg(HSFS, FDOPSS, reg_val, ", ");
460 pprint_reg(HSFS, FDV, reg_val, ", ");
461 pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
462}
463
Edward O'Callaghanacce4622020-07-16 15:39:19 +1000464static void prettyprint_ich9_reg_hsfc(uint16_t reg_val, enum ich_chipset ich_gen)
stefanctc274c862011-06-11 09:53:22 +0000465{
466 msg_pdbg("HSFC: ");
467 pprint_reg(HSFC, FGO, reg_val, ", ");
Edward O'Callaghanacce4622020-07-16 15:39:19 +1000468 switch (ich_gen) {
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +1000469 case CHIPSET_100_SERIES_SUNRISE_POINT:
470 case CHIPSET_C620_SERIES_LEWISBURG:
471 case CHIPSET_300_SERIES_CANNON_POINT:
Matt DeVillier81bc4d32020-08-12 12:48:06 -0500472 case CHIPSET_400_SERIES_COMET_POINT:
Edward O'Callaghanbff65042020-05-18 17:53:07 +1000473 _pprint_reg(HSFC, PCH100_HSFC_FCYCLE, PCH100_HSFC_FCYCLE_OFF, reg_val, ", ");
474 pprint_reg(HSFC, WET, reg_val, ", ");
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +1000475 break;
476 default:
477 pprint_reg(HSFC, FCYCLE, reg_val, ", ");
478 break;
Edward O'Callaghanbff65042020-05-18 17:53:07 +1000479 }
stefanctc274c862011-06-11 09:53:22 +0000480 pprint_reg(HSFC, FDBC, reg_val, ", ");
481 pprint_reg(HSFC, SME, reg_val, "\n");
482}
483
stefancte4d1ef52011-06-11 09:53:16 +0000484static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
485{
486 msg_pdbg("SSFS: ");
487 pprint_reg(SSFS, SCIP, reg_val, ", ");
488 pprint_reg(SSFS, FDONE, reg_val, ", ");
489 pprint_reg(SSFS, FCERR, reg_val, ", ");
490 pprint_reg(SSFS, AEL, reg_val, "\n");
491}
492
493static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
494{
495 msg_pdbg("SSFC: ");
496 pprint_reg(SSFC, SCGO, reg_val, ", ");
497 pprint_reg(SSFC, ACS, reg_val, ", ");
498 pprint_reg(SSFC, SPOP, reg_val, ", ");
499 pprint_reg(SSFC, COP, reg_val, ", ");
500 pprint_reg(SSFC, DBC, reg_val, ", ");
501 pprint_reg(SSFC, SME, reg_val, ", ");
502 pprint_reg(SSFC, SCF, reg_val, "\n");
503}
504
Edward O'Callaghan6be39a82020-05-18 18:02:02 +1000505static void prettyprint_pch100_reg_dlock(const uint32_t reg_val)
506{
507 msg_pdbg("DLOCK: ");
508 pprint_reg(DLOCK, BMWAG_LOCKDN, reg_val, ", ");
509 pprint_reg(DLOCK, BMRAG_LOCKDN, reg_val, ", ");
510 pprint_reg(DLOCK, SBMWAG_LOCKDN, reg_val, ", ");
511 pprint_reg(DLOCK, SBMRAG_LOCKDN, reg_val, ",\n ");
512 pprint_reg(DLOCK, PR0_LOCKDN, reg_val, ", ");
513 pprint_reg(DLOCK, PR1_LOCKDN, reg_val, ", ");
514 pprint_reg(DLOCK, PR2_LOCKDN, reg_val, ", ");
515 pprint_reg(DLOCK, PR3_LOCKDN, reg_val, ", ");
516 pprint_reg(DLOCK, PR4_LOCKDN, reg_val, ",\n ");
517 pprint_reg(DLOCK, SSEQ_LOCKDN, reg_val, "\n");
518}
519
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +1000520static struct {
521 size_t reg_ssfsc;
522 size_t reg_preop;
523 size_t reg_optype;
524 size_t reg_opmenu;
525} swseq_data;
526
hailfinger4c973122010-10-05 22:06:05 +0000527static uint8_t lookup_spi_type(uint8_t opcode)
528{
Edward O'Callaghand757b422020-05-26 21:22:12 +1000529 unsigned int a;
hailfinger4c973122010-10-05 22:06:05 +0000530
uwe8d342eb2011-07-28 08:13:25 +0000531 for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
hailfinger4c973122010-10-05 22:06:05 +0000532 if (POSSIBLE_OPCODES[a].opcode == opcode)
533 return POSSIBLE_OPCODES[a].spi_type;
534 }
535
536 return 0xFF;
537}
538
Edward O'Callaghan6884e4f2020-07-16 15:35:00 +1000539static int program_opcodes(OPCODES *op, int enable_undo, enum ich_chipset ich_gen)
hailfinger82e7ddb2008-05-16 12:55:55 +0000540{
541 uint8_t a;
stepan3bdf6182008-06-30 23:45:22 +0000542 uint16_t preop, optype;
543 uint32_t opmenu[2];
hailfinger82e7ddb2008-05-16 12:55:55 +0000544
545 /* Program Prefix Opcodes */
hailfinger82e7ddb2008-05-16 12:55:55 +0000546 /* 0:7 Prefix Opcode 1 */
stepan3bdf6182008-06-30 23:45:22 +0000547 preop = (op->preop[0]);
hailfinger82e7ddb2008-05-16 12:55:55 +0000548 /* 8:16 Prefix Opcode 2 */
stepan3bdf6182008-06-30 23:45:22 +0000549 preop |= ((uint16_t) op->preop[1]) << 8;
uwefa98ca12008-10-18 21:14:13 +0000550
stepandbd3af12008-06-27 16:28:34 +0000551 /* Program Opcode Types 0 - 7 */
stepan3bdf6182008-06-30 23:45:22 +0000552 optype = 0;
hailfinger82e7ddb2008-05-16 12:55:55 +0000553 for (a = 0; a < 8; a++) {
stepan3bdf6182008-06-30 23:45:22 +0000554 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
hailfinger82e7ddb2008-05-16 12:55:55 +0000555 }
uwefa98ca12008-10-18 21:14:13 +0000556
stepandbd3af12008-06-27 16:28:34 +0000557 /* Program Allowable Opcodes 0 - 3 */
stepan3bdf6182008-06-30 23:45:22 +0000558 opmenu[0] = 0;
hailfinger82e7ddb2008-05-16 12:55:55 +0000559 for (a = 0; a < 4; a++) {
stepan3bdf6182008-06-30 23:45:22 +0000560 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
hailfinger82e7ddb2008-05-16 12:55:55 +0000561 }
stepandbd3af12008-06-27 16:28:34 +0000562
Edward O'Callaghan6f2f8322019-09-06 11:55:24 +1000563 /* Program Allowable Opcodes 4 - 7 */
stepan3bdf6182008-06-30 23:45:22 +0000564 opmenu[1] = 0;
hailfinger82e7ddb2008-05-16 12:55:55 +0000565 for (a = 4; a < 8; a++) {
stepan3bdf6182008-06-30 23:45:22 +0000566 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
hailfinger82e7ddb2008-05-16 12:55:55 +0000567 }
stepandbd3af12008-06-27 16:28:34 +0000568
Edward O'Callaghand757b422020-05-26 21:22:12 +1000569 msg_pdbg2("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
Edward O'Callaghan6884e4f2020-07-16 15:35:00 +1000570 switch (ich_gen) {
stefanctc035c192011-11-06 23:51:09 +0000571 case CHIPSET_ICH7:
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +1000572 case CHIPSET_TUNNEL_CREEK:
573 case CHIPSET_CENTERTON:
hailfinger1e2e3442011-05-03 21:49:41 +0000574 /* Register undo only for enable_undo=1, i.e. first call. */
575 if (enable_undo) {
576 rmmio_valw(ich_spibar + ICH7_REG_PREOP);
577 rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
578 rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
579 rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
580 }
581 mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
582 mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
583 mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
584 mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
stepan3bdf6182008-06-30 23:45:22 +0000585 break;
stefanctc035c192011-11-06 23:51:09 +0000586 case CHIPSET_ICH8:
587 default: /* Future version might behave the same */
hailfinger1e2e3442011-05-03 21:49:41 +0000588 /* Register undo only for enable_undo=1, i.e. first call. */
589 if (enable_undo) {
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +1000590 rmmio_valw(ich_spibar + swseq_data.reg_preop);
591 rmmio_valw(ich_spibar + swseq_data.reg_optype);
592 rmmio_vall(ich_spibar + swseq_data.reg_opmenu);
593 rmmio_vall(ich_spibar + swseq_data.reg_opmenu + 4);
hailfinger1e2e3442011-05-03 21:49:41 +0000594 }
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +1000595 mmio_writew(preop, ich_spibar + swseq_data.reg_preop);
596 mmio_writew(optype, ich_spibar + swseq_data.reg_optype);
597 mmio_writel(opmenu[0], ich_spibar + swseq_data.reg_opmenu);
598 mmio_writel(opmenu[1], ich_spibar + swseq_data.reg_opmenu + 4);
stepan3bdf6182008-06-30 23:45:22 +0000599 break;
stepandbd3af12008-06-27 16:28:34 +0000600 }
hailfinger82e7ddb2008-05-16 12:55:55 +0000601
602 return 0;
603}
604
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100605static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
606{
607 uint8_t spi_type;
608
609 spi_type = lookup_spi_type(opcode);
610 if (spi_type > 3) {
611 /* Try to guess spi type from read/write sizes.
612 * The following valid writecnt/readcnt combinations exist:
613 * writecnt = 4, readcnt >= 0
614 * writecnt = 1, readcnt >= 0
615 * writecnt >= 4, readcnt = 0
616 * writecnt >= 1, readcnt = 0
617 * writecnt >= 1 is guaranteed for all commands.
618 */
619 if (readcnt == 0)
620 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
621 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
622 * bytes are actual the address, they go to the bus anyhow
623 */
624 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
625 else if (writecnt == 1) // and readcnt is > 0
626 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
627 else if (writecnt == 4) // and readcnt is > 0
628 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
629 else // we have an invalid case
630 return SPI_INVALID_LENGTH;
631 }
632 int oppos = 2; // use original JEDEC_BE_D8 offset
633 curopcodes->opcode[oppos].opcode = opcode;
634 curopcodes->opcode[oppos].spi_type = spi_type;
635 program_opcodes(curopcodes, 0, ich_generation);
636 oppos = find_opcode(curopcodes, opcode);
637 msg_pdbg2("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
638 return oppos;
639}
640
hailfingerb767c122010-05-28 15:53:08 +0000641/*
stefanct83d99e82011-11-08 10:55:54 +0000642 * Returns -1 if at least one mandatory opcode is inaccessible, 0 otherwise.
643 * FIXME: this should also check for
644 * - at least one probing opcode (RDID (incl. AT25F variants?), REMS, RES?)
645 * - at least one erasing opcode (lots.)
646 * - at least one program opcode (BYTE_PROGRAM, AAI_WORD_PROGRAM, ...?)
647 * - necessary preops? (EWSR, WREN, ...?)
648 */
Edward O'Callaghand757b422020-05-26 21:22:12 +1000649static int ich_missing_opcodes(void)
stefanct83d99e82011-11-08 10:55:54 +0000650{
651 uint8_t ops[] = {
652 JEDEC_READ,
653 JEDEC_RDSR,
654 0
655 };
656 int i = 0;
657 while (ops[i] != 0) {
658 msg_pspew("checking for opcode 0x%02x\n", ops[i]);
659 if (find_opcode(curopcodes, ops[i]) == -1)
660 return -1;
661 i++;
662 }
663 return 0;
664}
665
666/*
hailfingerb767c122010-05-28 15:53:08 +0000667 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
668 * it didn't stick.
669 */
Edward O'Callaghand22862f2020-07-16 15:37:26 +1000670static void ich_set_bbar(uint32_t min_addr, enum ich_chipset ich_gen)
hailfingerb767c122010-05-28 15:53:08 +0000671{
stefanctebf900c2011-07-01 00:39:09 +0000672 int bbar_off;
Edward O'Callaghand22862f2020-07-16 15:37:26 +1000673 switch (ich_gen) {
stefanctc035c192011-11-06 23:51:09 +0000674 case CHIPSET_ICH7:
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +1000675 case CHIPSET_TUNNEL_CREEK:
676 case CHIPSET_CENTERTON:
stefanctebf900c2011-07-01 00:39:09 +0000677 bbar_off = 0x50;
hailfingerb767c122010-05-28 15:53:08 +0000678 break;
stefanctc035c192011-11-06 23:51:09 +0000679 case CHIPSET_ICH8:
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +1000680 case CHIPSET_BAYTRAIL:
681 msg_pdbg("BBAR offset is unknown!\n");
stefanct3a716ba2011-09-17 21:21:42 +0000682 return;
stefanctc035c192011-11-06 23:51:09 +0000683 case CHIPSET_ICH9:
stefanct3a716ba2011-09-17 21:21:42 +0000684 default: /* Future version might behave the same */
stefanctebf900c2011-07-01 00:39:09 +0000685 bbar_off = ICH9_REG_BBAR;
hailfingerb767c122010-05-28 15:53:08 +0000686 break;
hailfingerb767c122010-05-28 15:53:08 +0000687 }
David Hendricks7be91492016-12-27 18:43:45 -0800688
stefanctebf900c2011-07-01 00:39:09 +0000689 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
690 if (ichspi_bbar) {
691 msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
692 ichspi_bbar);
693 }
694 min_addr &= BBAR_MASK;
695 ichspi_bbar |= min_addr;
696 rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
697 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
698
699 /* We don't have any option except complaining. And if the write
700 * failed, the restore will fail as well, so no problem there.
701 */
702 if (ichspi_bbar != min_addr)
stefanct3a716ba2011-09-17 21:21:42 +0000703 msg_perr("Setting BBAR to 0x%08x failed! New value: 0x%08x.\n",
704 min_addr, ichspi_bbar);
hailfingerb767c122010-05-28 15:53:08 +0000705}
706
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100707/* Create a struct OPCODES based on what we find in the locked down chipset. */
708static int generate_opcodes(OPCODES * op, enum ich_chipset ich_gen)
Edward O'Callaghand757b422020-05-26 21:22:12 +1000709{
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100710 int a;
711 uint16_t preop, optype;
712 uint32_t opmenu[2];
David Hendricks1db25572011-07-11 22:07:58 -0700713
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100714 if (op == NULL) {
715 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
716 return -1;
David Hendricks1db25572011-07-11 22:07:58 -0700717 }
David Hendricks1db25572011-07-11 22:07:58 -0700718
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100719 switch (ich_gen) {
720 case CHIPSET_ICH7:
721 case CHIPSET_TUNNEL_CREEK:
722 case CHIPSET_CENTERTON:
723 preop = REGREAD16(ICH7_REG_PREOP);
724 optype = REGREAD16(ICH7_REG_OPTYPE);
725 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
726 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
727 break;
728 case CHIPSET_ICH8:
729 default: /* Future version might behave the same */
730 preop = REGREAD16(swseq_data.reg_preop);
731 optype = REGREAD16(swseq_data.reg_optype);
732 opmenu[0] = REGREAD32(swseq_data.reg_opmenu);
733 opmenu[1] = REGREAD32(swseq_data.reg_opmenu + 4);
734 break;
David Hendricks1db25572011-07-11 22:07:58 -0700735 }
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100736
737 op->preop[0] = (uint8_t) preop;
738 op->preop[1] = (uint8_t) (preop >> 8);
739
740 for (a = 0; a < 8; a++) {
741 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
742 optype >>= 2;
743 }
744
745 for (a = 0; a < 4; a++) {
746 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
747 opmenu[0] >>= 8;
748 }
749
750 for (a = 4; a < 8; a++) {
751 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
752 opmenu[1] >>= 8;
753 }
754
755 /* No preopcodes used by default. */
756 for (a = 0; a < 8; a++)
757 op->opcode[a].atomic = 0;
758
759 return 0;
David Hendricks1db25572011-07-11 22:07:58 -0700760}
761
stuge7a651552008-12-15 02:32:11 +0000762/* This function generates OPCODES from or programs OPCODES to ICH according to
763 * the chipset's SPI configuration lock.
stepane1a13b92008-12-08 18:16:58 +0000764 *
stuge7a651552008-12-15 02:32:11 +0000765 * It should be called before ICH sends any spi command.
stepane1a13b92008-12-08 18:16:58 +0000766 */
Edward O'Callaghan6884e4f2020-07-16 15:35:00 +1000767static int ich_init_opcodes(enum ich_chipset ich_gen)
stepane1a13b92008-12-08 18:16:58 +0000768{
769 int rc = 0;
770 OPCODES *curopcodes_done;
771
772 if (curopcodes)
773 return 0;
774
775 if (ichspi_lock) {
hailfingerb767c122010-05-28 15:53:08 +0000776 msg_pdbg("Reading OPCODES... ");
stepane1a13b92008-12-08 18:16:58 +0000777 curopcodes_done = &O_EXISTING;
Edward O'Callaghan6884e4f2020-07-16 15:35:00 +1000778 rc = generate_opcodes(curopcodes_done, ich_gen);
stepane1a13b92008-12-08 18:16:58 +0000779 } else {
snelsone42c3802010-05-07 20:09:04 +0000780 msg_pdbg("Programming OPCODES... ");
stepane1a13b92008-12-08 18:16:58 +0000781 curopcodes_done = &O_ST_M25P;
Edward O'Callaghan6884e4f2020-07-16 15:35:00 +1000782 rc = program_opcodes(curopcodes_done, 1, ich_gen);
stepane1a13b92008-12-08 18:16:58 +0000783 }
784
785 if (rc) {
786 curopcodes = NULL;
snelsone42c3802010-05-07 20:09:04 +0000787 msg_perr("failed\n");
stepane1a13b92008-12-08 18:16:58 +0000788 return 1;
789 } else {
790 curopcodes = curopcodes_done;
snelsone42c3802010-05-07 20:09:04 +0000791 msg_pdbg("done\n");
David Hendricksce6b2fa2011-07-11 22:12:43 -0700792 prettyprint_opcodes(curopcodes);
stepane1a13b92008-12-08 18:16:58 +0000793 return 0;
794 }
795}
796
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100797/* Fill len bytes from the data array into the fdata/spid registers.
798 *
799 * Note that using len > flash->mst->spi.max_data_write will trash the registers
800 * following the data registers.
801 */
802static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
803{
804 uint32_t temp32 = 0;
805 int i;
806
807 if (len <= 0)
808 return;
809
810 for (i = 0; i < len; i++) {
811 if ((i % 4) == 0)
812 temp32 = 0;
813
814 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
815
816 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
817 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
818 }
819 i--;
820 if ((i % 4) != 3) /* Write remaining data to regs. */
821 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
822}
823
824/* Read len bytes from the fdata/spid register into the data array.
825 *
826 * Note that using len > flash->mst->spi.max_data_read will return garbage or
827 * may even crash.
828 */
829static void ich_read_data(uint8_t *data, int len, int reg0_off)
830{
831 int i;
832 uint32_t temp32 = 0;
833
834 for (i = 0; i < len; i++) {
835 if ((i % 4) == 0)
836 temp32 = REGREAD32(reg0_off + i);
837
838 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
839 }
840}
841
stepan82c65bd2008-11-02 19:51:50 +0000842static int ich7_run_opcode(OPCODE op, uint32_t offset,
ruik9bc51c02008-06-30 21:38:30 +0000843 uint8_t datalength, uint8_t * data, int maxdata)
hailfinger82e7ddb2008-05-16 12:55:55 +0000844{
845 int write_cmd = 0;
stepandbd3af12008-06-27 16:28:34 +0000846 int timeout;
David Hendricks1db25572011-07-11 22:07:58 -0700847 uint32_t temp32;
stepandbd3af12008-06-27 16:28:34 +0000848 uint16_t temp16;
stepan82c65bd2008-11-02 19:51:50 +0000849 uint64_t opmenu;
850 int opcode_index;
hailfinger82e7ddb2008-05-16 12:55:55 +0000851
852 /* Is it a write command? */
853 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
854 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
855 write_cmd = 1;
856 }
857
hailfinger01d05912011-03-17 00:10:25 +0000858 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
859 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
860 programmer_delay(10);
861 }
862 if (!timeout) {
863 msg_perr("Error: SCIP never cleared!\n");
864 return 1;
865 }
866
stefancte5e09892011-07-01 00:39:23 +0000867 /* Program offset in flash into SPIA while preserving reserved bits. */
868 temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
869 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
hailfinger82e7ddb2008-05-16 12:55:55 +0000870
stefancte5e09892011-07-01 00:39:23 +0000871 /* Program data into SPID0 to N */
David Hendricks1db25572011-07-11 22:07:58 -0700872 if (write_cmd && (datalength != 0))
873 ich_fill_data(data, datalength, ICH7_REG_SPID0);
stepandbd3af12008-06-27 16:28:34 +0000874
875 /* Assemble SPIS */
hailfinger01d05912011-03-17 00:10:25 +0000876 temp16 = REGREAD16(ICH7_REG_SPIS);
877 /* keep reserved bits */
878 temp16 &= SPIS_RESERVED_MASK;
stepandbd3af12008-06-27 16:28:34 +0000879 /* clear error status registers */
stefanct04009ff2011-06-11 09:53:09 +0000880 temp16 |= (SPIS_CDS | SPIS_FCERR);
stepandbd3af12008-06-27 16:28:34 +0000881 REGWRITE16(ICH7_REG_SPIS, temp16);
882
883 /* Assemble SPIC */
884 temp16 = 0;
885
886 if (datalength != 0) {
887 temp16 |= SPIC_DS;
ruik9bc51c02008-06-30 21:38:30 +0000888 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
stepandbd3af12008-06-27 16:28:34 +0000889 }
890
891 /* Select opcode */
stepan82c65bd2008-11-02 19:51:50 +0000892 opmenu = REGREAD32(ICH7_REG_OPMENU);
893 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
894
uwe5e931bc2009-04-15 10:52:49 +0000895 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
896 if ((opmenu & 0xff) == op.opcode) {
stepan82c65bd2008-11-02 19:51:50 +0000897 break;
898 }
899 opmenu >>= 8;
900 }
901 if (opcode_index == 8) {
snelsone42c3802010-05-07 20:09:04 +0000902 msg_pdbg("Opcode %x not found.\n", op.opcode);
stepan82c65bd2008-11-02 19:51:50 +0000903 return 1;
904 }
905 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
stepandbd3af12008-06-27 16:28:34 +0000906
mkarcher15b92fe2011-04-29 22:11:36 +0000907 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
908 /* Handle Atomic. Atomic commands include three steps:
909 - sending the preop (mainly EWSR or WREN)
910 - sending the main command
911 - waiting for the busy bit (WIP) to be cleared
912 This means the timeout must be sufficient for chip erase
913 of slow high-capacity chips.
stefanct707f13b2011-05-19 02:58:17 +0000914 */
hailfinger82e32492010-02-11 11:28:37 +0000915 switch (op.atomic) {
916 case 2:
917 /* Select second preop. */
918 temp16 |= SPIC_SPOP;
Edward O'Callaghand757b422020-05-26 21:22:12 +1000919 /* Fall through. */
hailfinger82e32492010-02-11 11:28:37 +0000920 case 1:
921 /* Atomic command (preop+op) */
stepandbd3af12008-06-27 16:28:34 +0000922 temp16 |= SPIC_ACS;
mkarcher15b92fe2011-04-29 22:11:36 +0000923 timeout = 100 * 1000 * 60; /* 60 seconds */
hailfinger82e32492010-02-11 11:28:37 +0000924 break;
stepandbd3af12008-06-27 16:28:34 +0000925 }
926
927 /* Start */
928 temp16 |= SPIC_SCGO;
929
930 /* write it */
931 REGWRITE16(ICH7_REG_SPIC, temp16);
932
Edward O'Callaghand757b422020-05-26 21:22:12 +1000933 /* Wait for Cycle Done Status or Flash Cycle Error. */
hailfinger01d05912011-03-17 00:10:25 +0000934 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
935 --timeout) {
hailfingere5829f62009-06-05 17:48:08 +0000936 programmer_delay(10);
stepandbd3af12008-06-27 16:28:34 +0000937 }
938 if (!timeout) {
hailfinger01d05912011-03-17 00:10:25 +0000939 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
940 REGREAD16(ICH7_REG_SPIS));
941 return 1;
stepandbd3af12008-06-27 16:28:34 +0000942 }
943
snelsone42c3802010-05-07 20:09:04 +0000944 /* FIXME: make sure we do not needlessly cause transaction errors. */
hailfinger01d05912011-03-17 00:10:25 +0000945 temp16 = REGREAD16(ICH7_REG_SPIS);
946 if (temp16 & SPIS_FCERR) {
mkarcherdb7751e2011-04-29 23:53:09 +0000947 msg_perr("Transaction error!\n");
hailfinger01d05912011-03-17 00:10:25 +0000948 /* keep reserved bits */
949 temp16 &= SPIS_RESERVED_MASK;
950 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
stepandbd3af12008-06-27 16:28:34 +0000951 return 1;
952 }
953
stefanctfa66c622011-08-09 01:49:34 +0000954 if ((!write_cmd) && (datalength != 0))
David Hendricks1db25572011-07-11 22:07:58 -0700955 ich_read_data(data, datalength, ICH7_REG_SPID0);
stepandbd3af12008-06-27 16:28:34 +0000956
957 return 0;
958}
959
stepan82c65bd2008-11-02 19:51:50 +0000960static int ich9_run_opcode(OPCODE op, uint32_t offset,
stepandbd3af12008-06-27 16:28:34 +0000961 uint8_t datalength, uint8_t * data)
962{
963 int write_cmd = 0;
stepan3bdf6182008-06-30 23:45:22 +0000964 int timeout;
stepandbd3af12008-06-27 16:28:34 +0000965 uint32_t temp32;
stepan82c65bd2008-11-02 19:51:50 +0000966 uint64_t opmenu;
967 int opcode_index;
stepandbd3af12008-06-27 16:28:34 +0000968
969 /* Is it a write command? */
970 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
971 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
972 write_cmd = 1;
973 }
974
hailfinger01d05912011-03-17 00:10:25 +0000975 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +1000976 while ((REGREAD8(swseq_data.reg_ssfsc) & SSFS_SCIP) && --timeout) {
hailfinger01d05912011-03-17 00:10:25 +0000977 programmer_delay(10);
978 }
979 if (!timeout) {
980 msg_perr("Error: SCIP never cleared!\n");
981 return 1;
982 }
983
stefancte5e09892011-07-01 00:39:23 +0000984 /* Program offset in flash into FADDR while preserve the reserved bits
985 * and clearing the 25. address bit which is only useable in hwseq. */
986 temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
987 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
stepandbd3af12008-06-27 16:28:34 +0000988
989 /* Program data into FDATA0 to N */
David Hendricks1db25572011-07-11 22:07:58 -0700990 if (write_cmd && (datalength != 0))
991 ich_fill_data(data, datalength, ICH9_REG_FDATA0);
hailfinger82e7ddb2008-05-16 12:55:55 +0000992
993 /* Assemble SSFS + SSFC */
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +1000994 temp32 = REGREAD32(swseq_data.reg_ssfsc);
stefanct707f13b2011-05-19 02:58:17 +0000995 /* Keep reserved bits only */
hailfinger01d05912011-03-17 00:10:25 +0000996 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
stefanct04009ff2011-06-11 09:53:09 +0000997 /* Clear cycle done and cycle error status registers */
998 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +1000999 REGWRITE32(swseq_data.reg_ssfsc, temp32);
hailfinger01d05912011-03-17 00:10:25 +00001000
uwe3a3ab2f2010-03-25 23:18:41 +00001001 /* Use 20 MHz */
hailfinger82e7ddb2008-05-16 12:55:55 +00001002 temp32 |= SSFC_SCF_20MHZ;
1003
stefanct707f13b2011-05-19 02:58:17 +00001004 /* Set data byte count (DBC) and data cycle bit (DS) */
hailfinger82e7ddb2008-05-16 12:55:55 +00001005 if (datalength != 0) {
1006 uint32_t datatemp;
1007 temp32 |= SSFC_DS;
stefanct04009ff2011-06-11 09:53:09 +00001008 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
1009 SSFC_DBC);
hailfinger82e7ddb2008-05-16 12:55:55 +00001010 temp32 |= datatemp;
1011 }
1012
1013 /* Select opcode */
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10001014 opmenu = REGREAD32(swseq_data.reg_opmenu);
1015 opmenu |= ((uint64_t)REGREAD32(swseq_data.reg_opmenu + 4)) << 32;
stepan82c65bd2008-11-02 19:51:50 +00001016
uwe5e931bc2009-04-15 10:52:49 +00001017 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
1018 if ((opmenu & 0xff) == op.opcode) {
stepan82c65bd2008-11-02 19:51:50 +00001019 break;
1020 }
1021 opmenu >>= 8;
1022 }
1023 if (opcode_index == 8) {
snelsone42c3802010-05-07 20:09:04 +00001024 msg_pdbg("Opcode %x not found.\n", op.opcode);
stepan82c65bd2008-11-02 19:51:50 +00001025 return 1;
1026 }
1027 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
hailfinger82e7ddb2008-05-16 12:55:55 +00001028
mkarcher15b92fe2011-04-29 22:11:36 +00001029 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
1030 /* Handle Atomic. Atomic commands include three steps:
1031 - sending the preop (mainly EWSR or WREN)
1032 - sending the main command
1033 - waiting for the busy bit (WIP) to be cleared
1034 This means the timeout must be sufficient for chip erase
1035 of slow high-capacity chips.
stefanct707f13b2011-05-19 02:58:17 +00001036 */
hailfinger82e32492010-02-11 11:28:37 +00001037 switch (op.atomic) {
1038 case 2:
1039 /* Select second preop. */
1040 temp32 |= SSFC_SPOP;
Edward O'Callaghand757b422020-05-26 21:22:12 +10001041 /* Fall through. */
hailfinger82e32492010-02-11 11:28:37 +00001042 case 1:
1043 /* Atomic command (preop+op) */
hailfinger82e7ddb2008-05-16 12:55:55 +00001044 temp32 |= SSFC_ACS;
mkarcher15b92fe2011-04-29 22:11:36 +00001045 timeout = 100 * 1000 * 60; /* 60 seconds */
hailfinger82e32492010-02-11 11:28:37 +00001046 break;
hailfinger82e7ddb2008-05-16 12:55:55 +00001047 }
1048
1049 /* Start */
1050 temp32 |= SSFC_SCGO;
1051
1052 /* write it */
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10001053 REGWRITE32(swseq_data.reg_ssfsc, temp32);
hailfinger82e7ddb2008-05-16 12:55:55 +00001054
hailfinger01d05912011-03-17 00:10:25 +00001055 /* Wait for Cycle Done Status or Flash Cycle Error. */
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10001056 while (((REGREAD32(swseq_data.reg_ssfsc) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
hailfinger01d05912011-03-17 00:10:25 +00001057 --timeout) {
hailfingere5829f62009-06-05 17:48:08 +00001058 programmer_delay(10);
stepan3bdf6182008-06-30 23:45:22 +00001059 }
1060 if (!timeout) {
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10001061 msg_perr("timeout, REG_SSFS=0x%08x\n",
1062 REGREAD32(swseq_data.reg_ssfsc));
hailfinger01d05912011-03-17 00:10:25 +00001063 return 1;
hailfinger82e7ddb2008-05-16 12:55:55 +00001064 }
1065
snelsone42c3802010-05-07 20:09:04 +00001066 /* FIXME make sure we do not needlessly cause transaction errors. */
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10001067 temp32 = REGREAD32(swseq_data.reg_ssfsc);
hailfinger01d05912011-03-17 00:10:25 +00001068 if (temp32 & SSFS_FCERR) {
mkarcherdb7751e2011-04-29 23:53:09 +00001069 msg_perr("Transaction error!\n");
stefancte4d1ef52011-06-11 09:53:16 +00001070 prettyprint_ich9_reg_ssfs(temp32);
1071 prettyprint_ich9_reg_ssfc(temp32);
hailfinger01d05912011-03-17 00:10:25 +00001072 /* keep reserved bits */
1073 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
1074 /* Clear the transaction error. */
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10001075 REGWRITE32(swseq_data.reg_ssfsc, temp32 | SSFS_FCERR);
hailfinger82e7ddb2008-05-16 12:55:55 +00001076 return 1;
1077 }
1078
stefanctfa66c622011-08-09 01:49:34 +00001079 if ((!write_cmd) && (datalength != 0))
David Hendricks1db25572011-07-11 22:07:58 -07001080 ich_read_data(data, datalength, ICH9_REG_FDATA0);
hailfinger82e7ddb2008-05-16 12:55:55 +00001081
1082 return 0;
1083}
1084
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001085static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
stepandbd3af12008-06-27 16:28:34 +00001086 uint8_t datalength, uint8_t * data)
1087{
stefanct689eb6c2011-06-11 19:44:31 +00001088 /* max_data_read == max_data_write for all Intel/VIA SPI masters */
Edward O'Callaghanc66827e2020-10-09 12:22:04 +11001089 uint8_t maxlength = flash->mst->spi.max_data_read;
stefanct689eb6c2011-06-11 19:44:31 +00001090
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11001091 if (ich_generation == CHIPSET_ICH_UNKNOWN) {
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +10001092 msg_perr("%s: unsupported chipset\n", __func__);
1093 return -1;
1094 }
1095
stefanct689eb6c2011-06-11 19:44:31 +00001096 if (datalength > maxlength) {
1097 msg_perr("%s: Internal command size error for "
1098 "opcode 0x%02x, got datalength=%i, want <=%i\n",
1099 __func__, op.opcode, datalength, maxlength);
1100 return SPI_INVALID_LENGTH;
1101 }
1102
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11001103 switch (ich_generation) {
stefanctc035c192011-11-06 23:51:09 +00001104 case CHIPSET_ICH7:
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +10001105 case CHIPSET_TUNNEL_CREEK:
1106 case CHIPSET_CENTERTON:
stefanct689eb6c2011-06-11 19:44:31 +00001107 return ich7_run_opcode(op, offset, datalength, data, maxlength);
stefanctc035c192011-11-06 23:51:09 +00001108 case CHIPSET_ICH8:
1109 default: /* Future version might behave the same */
stefanct689eb6c2011-06-11 19:44:31 +00001110 return ich9_run_opcode(op, offset, datalength, data);
stefanct689eb6c2011-06-11 19:44:31 +00001111 }
stepandbd3af12008-06-27 16:28:34 +00001112}
1113
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10001114#define DEFAULT_NUM_FD_REGIONS 5
Furquan Shaikh5c100f22018-11-05 21:35:11 -08001115
1116/*
1117 * APL/GLK have the Device Expansion region as well. Hence, the number of
1118 * regions is 6.
1119 */
1120#define APL_GLK_NUM_FD_REGIONS 6
Bora Guvendikc34416b2019-01-07 16:10:48 -08001121
1122/*
1123 * Sunrisepoint have reserved regions and a region for Embedded Controller.
1124 * Hence, the number of regions is 9.
1125 */
1126#define SUNRISEPOINT_NUM_FD_REGIONS 9
1127
1128#define EMBEDDED_CONTROLLER_REGION 8
1129
David Hendricks1ed1d352011-11-23 17:54:37 -08001130static int num_fd_regions;
1131
David Hendricks1ed1d352011-11-23 17:54:37 -08001132enum fd_access_level {
1133 FD_REGION_LOCKED,
1134 FD_REGION_READ_ONLY,
1135 FD_REGION_WRITE_ONLY,
1136 FD_REGION_READ_WRITE,
1137};
1138
1139struct fd_region_permission {
1140 enum fd_access_level level;
1141 const char *name;
1142} fd_region_permissions[] = {
1143 /* order corresponds to FRAP bitfield */
1144 { FD_REGION_LOCKED, "locked" },
1145 { FD_REGION_READ_ONLY, "read-only" },
1146 { FD_REGION_WRITE_ONLY, "write-only" },
1147 { FD_REGION_READ_WRITE, "read-write" },
1148};
1149
David Hendricks1ed1d352011-11-23 17:54:37 -08001150struct fd_region {
1151 const char *name;
1152 struct fd_region_permission *permission;
1153 uint32_t base;
1154 uint32_t limit;
1155} fd_regions[] = {
1156 /* order corresponds to flash descriptor */
1157 { .name = "Flash Descriptor" },
1158 { .name = "BIOS" },
1159 { .name = "Management Engine" },
1160 { .name = "Gigabit Ethernet" },
1161 { .name = "Platform Data" },
Furquan Shaikh5c100f22018-11-05 21:35:11 -08001162 { .name = "Device Expansion" },
Bora Guvendikc34416b2019-01-07 16:10:48 -08001163 { .name = "Reserved 1" },
1164 { .name = "Reserved 2" },
1165 { .name = "Embedded Controller" },
Edward O'Callaghand7b95f22020-08-07 23:51:23 +10001166 { .name = "unknown" },
1167 { .name = "IE" },
1168 { .name = "10GbE" },
1169 { .name = "unknown" },
1170 { .name = "unknown" },
1171 { .name = "unknown" },
1172 { .name = "unknown" },
David Hendricks1ed1d352011-11-23 17:54:37 -08001173};
1174
Edward O'Callaghan69c3a0d2020-07-23 10:41:41 +10001175static int check_fd_permissions(OPCODE *opcode, int type, uint32_t addr, int count)
Ramya Vijaykumar71d69ad2015-10-01 11:26:40 +05301176{
1177 int i;
Edward O'Callaghan69c3a0d2020-07-23 10:41:41 +10001178 uint8_t op_type = opcode ? opcode->spi_type : type;
Edward O'Callaghand1dff072020-08-01 19:34:48 +10001179 int op_type_r = opcode ? SPI_OPCODE_TYPE_READ_WITH_ADDRESS : SPI_OPCODE_TYPE_READ_NO_ADDRESS;
1180 int op_type_w = opcode ? SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS : SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
David Hendricks1ed1d352011-11-23 17:54:37 -08001181 int ret = 0;
1182
1183 /* check flash descriptor permissions (if present) */
1184 for (i = 0; i < num_fd_regions; i++) {
1185 const char *name = fd_regions[i].name;
1186 enum fd_access_level level;
Edward O'Callaghan90de5fe2020-07-23 11:03:42 +10001187 uint32_t base = fd_regions[i].base;
1188 uint32_t limit = fd_regions[i].limit;
David Hendricks1ed1d352011-11-23 17:54:37 -08001189
Edward O'Callaghan90de5fe2020-07-23 11:03:42 +10001190 if ((addr + count - 1 < base) || (addr > limit))
David Hendricks1ed1d352011-11-23 17:54:37 -08001191 continue;
1192
1193 if (!fd_regions[i].permission) {
Edward O'Callaghan90de5fe2020-07-23 11:03:42 +10001194 msg_perr("No permissions set for flash region %s\n", name);
David Hendricks1ed1d352011-11-23 17:54:37 -08001195 break;
1196 }
David Hendricks1ed1d352011-11-23 17:54:37 -08001197 level = fd_regions[i].permission->level;
1198
Edward O'Callaghan69c3a0d2020-07-23 10:41:41 +10001199 if (op_type == op_type_r) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001200 if (level != FD_REGION_READ_ONLY &&
1201 level != FD_REGION_READ_WRITE) {
1202 msg_pspew("%s: Cannot read address 0x%08x in "
1203 "region %s\n", __func__,addr,name);
1204 ret = SPI_ACCESS_DENIED;
1205 }
Edward O'Callaghan69c3a0d2020-07-23 10:41:41 +10001206 } else if (op_type == op_type_w) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001207 if (level != FD_REGION_WRITE_ONLY &&
1208 level != FD_REGION_READ_WRITE) {
1209 msg_pspew("%s: Cannot write to address 0x%08x in"
1210 "region %s\n", __func__,addr,name);
1211 ret = SPI_ACCESS_DENIED;
1212 }
1213 }
1214 break;
1215 }
1216
Edward O'Callaghan93efced2020-11-04 16:31:26 +11001217 if ((i == num_fd_regions) && !opcode) { // FIXME(b/171892105).
Edward O'Callaghan69c3a0d2020-07-23 10:41:41 +10001218 msg_pspew("%s: Address not covered by any descriptor 0x%06x\n",
1219 __func__, addr);
1220 ret = SPI_ACCESS_DENIED;
1221 }
1222
David Hendricks1ed1d352011-11-23 17:54:37 -08001223 return ret;
1224}
1225
Edward O'Callaghand757b422020-05-26 21:22:12 +10001226static int ich_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
1227 unsigned int readcnt,
1228 const unsigned char *writearr,
1229 unsigned char *readarr)
hailfinger82e7ddb2008-05-16 12:55:55 +00001230{
hailfinger9c290a72009-07-14 10:26:56 +00001231 int result;
hailfinger82e7ddb2008-05-16 12:55:55 +00001232 int opcode_index = -1;
1233 const unsigned char cmd = *writearr;
1234 OPCODE *opcode;
1235 uint32_t addr = 0;
1236 uint8_t *data;
1237 int count;
1238
hailfinger82e7ddb2008-05-16 12:55:55 +00001239 /* find cmd in opcodes-table */
hailfinger82e32492010-02-11 11:28:37 +00001240 opcode_index = find_opcode(curopcodes, cmd);
hailfinger82e7ddb2008-05-16 12:55:55 +00001241 if (opcode_index == -1) {
hailfinger4c973122010-10-05 22:06:05 +00001242 if (!ichspi_lock)
1243 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
Edward O'Callaghan81eb09c2020-05-26 22:07:23 +10001244 if (opcode_index == SPI_INVALID_LENGTH) {
1245 msg_pdbg("OPCODE 0x%02x has unsupported length, will not execute.\n", cmd);
1246 return SPI_INVALID_LENGTH;
1247 } else if (opcode_index == -1) {
1248 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
1249 cmd);
hailfinger4c973122010-10-05 22:06:05 +00001250 return SPI_INVALID_OPCODE;
1251 }
hailfinger82e7ddb2008-05-16 12:55:55 +00001252 }
1253
Edward O'Callaghand13334a2020-07-23 12:51:00 +10001254 if (is_dry_run())
Vadim Bendebury066143d2018-07-16 18:20:33 -07001255 return 0;
1256
hailfinger82e7ddb2008-05-16 12:55:55 +00001257 opcode = &(curopcodes->opcode[opcode_index]);
1258
hailfinger82e32492010-02-11 11:28:37 +00001259 /* The following valid writecnt/readcnt combinations exist:
1260 * writecnt = 4, readcnt >= 0
1261 * writecnt = 1, readcnt >= 0
1262 * writecnt >= 4, readcnt = 0
1263 * writecnt >= 1, readcnt = 0
1264 * writecnt >= 1 is guaranteed for all commands.
1265 */
1266 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
1267 (writecnt != 4)) {
snelsone42c3802010-05-07 20:09:04 +00001268 msg_perr("%s: Internal command size error for opcode "
hailfinger82e32492010-02-11 11:28:37 +00001269 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
1270 writecnt);
1271 return SPI_INVALID_LENGTH;
1272 }
1273 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
1274 (writecnt != 1)) {
snelsone42c3802010-05-07 20:09:04 +00001275 msg_perr("%s: Internal command size error for opcode "
hailfinger82e32492010-02-11 11:28:37 +00001276 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
1277 writecnt);
1278 return SPI_INVALID_LENGTH;
1279 }
1280 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1281 (writecnt < 4)) {
snelsone42c3802010-05-07 20:09:04 +00001282 msg_perr("%s: Internal command size error for opcode "
hailfinger82e32492010-02-11 11:28:37 +00001283 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
1284 writecnt);
1285 return SPI_INVALID_LENGTH;
1286 }
1287 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1288 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1289 (readcnt)) {
snelsone42c3802010-05-07 20:09:04 +00001290 msg_perr("%s: Internal command size error for opcode "
hailfinger82e32492010-02-11 11:28:37 +00001291 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1292 readcnt);
1293 return SPI_INVALID_LENGTH;
1294 }
1295
stefanct689eb6c2011-06-11 19:44:31 +00001296 /* Translate read/write array/count.
1297 * The maximum data length is identical for the maximum read length and
1298 * for the maximum write length excluding opcode and address. Opcode and
1299 * address are stored in separate registers, not in the data registers
1300 * and are thus not counted towards data length. The only exception
1301 * applies if the opcode definition (un)intentionally classifies said
1302 * opcode incorrectly as non-address opcode or vice versa. */
hailfinger82e7ddb2008-05-16 12:55:55 +00001303 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
stepan8f46dd62008-06-27 15:18:20 +00001304 data = (uint8_t *) (writearr + 1);
1305 count = writecnt - 1;
1306 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1307 data = (uint8_t *) (writearr + 4);
1308 count = writecnt - 4;
1309 } else {
1310 data = (uint8_t *) readarr;
hailfinger82e7ddb2008-05-16 12:55:55 +00001311 count = readcnt;
1312 }
stepan8f46dd62008-06-27 15:18:20 +00001313
David Hendricks1ed1d352011-11-23 17:54:37 -08001314 /* if opcode-type requires an address */
Edward O'Callaghandfb71542020-05-14 18:41:42 +10001315 if (cmd == JEDEC_REMS || cmd == JEDEC_RES) {
1316 addr = ichspi_bbar;
1317 } else if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
David Hendricks1ed1d352011-11-23 17:54:37 -08001318 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Edward O'Callaghandfb71542020-05-14 18:41:42 +10001319 /* BBAR may cut part of the chip off at the lower end. */
1320 const uint32_t valid_base = ichspi_bbar & ((flash->chip->total_size * 1024) - 1);
1321 const uint32_t addr_offset = ichspi_bbar - valid_base;
1322 /* Highest address we can program is (2^24 - 1). */
1323 const uint32_t valid_end = (1 << 24) - addr_offset;
1324
Edward O'Callaghan03c389d2020-07-08 23:07:24 +10001325 addr = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
Edward O'Callaghandfb71542020-05-14 18:41:42 +10001326 const uint32_t addr_end = addr + count;
1327
1328 if (addr < valid_base ||
1329 addr_end < addr || /* integer overflow check */
1330 addr_end > valid_end) {
Edward O'Callaghand757b422020-05-26 21:22:12 +10001331 msg_perr("%s: Addressed region 0x%06x-0x%06x not in allowed range 0x%06x-0x%06x\n",
1332 __func__, addr, addr_end - 1, valid_base, valid_end - 1);
1333 return SPI_INVALID_ADDRESS;
David Hendricks1ed1d352011-11-23 17:54:37 -08001334 }
Edward O'Callaghandfb71542020-05-14 18:41:42 +10001335 addr += addr_offset;
1336
David Hendricks1ed1d352011-11-23 17:54:37 -08001337 if (num_fd_regions > 0) {
Edward O'Callaghan69c3a0d2020-07-23 10:41:41 +10001338 result = check_fd_permissions(opcode, 0, addr, count);
David Hendricks1ed1d352011-11-23 17:54:37 -08001339 if (result)
1340 return result;
1341 }
1342 }
1343
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001344 result = run_opcode(flash, *opcode, addr, count, data);
hailfinger9c290a72009-07-14 10:26:56 +00001345 if (result) {
mkarcherdb7751e2011-04-29 23:53:09 +00001346 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1347 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1348 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1349 msg_pdbg("at address 0x%06x ", addr);
1350 }
1351 msg_pdbg("(payload length was %d).\n", count);
1352
1353 /* Print out the data array if it contains data to write.
1354 * Errors are detected before the received data is read back into
1355 * the array so it won't make sense to print it then. */
1356 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1357 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1358 int i;
1359 msg_pspew("The data was:\n");
stefanctd0064e12011-11-08 11:55:24 +00001360 for (i = 0; i < count; i++){
mkarcherdb7751e2011-04-29 23:53:09 +00001361 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1362 }
1363 }
hailfinger82e7ddb2008-05-16 12:55:55 +00001364 }
1365
hailfinger9c290a72009-07-14 10:26:56 +00001366 return result;
hailfinger82e7ddb2008-05-16 12:55:55 +00001367}
hailfinger948b81f2009-07-22 15:36:50 +00001368
stefanct83d99e82011-11-08 10:55:54 +00001369static struct hwseq_data {
1370 uint32_t size_comp0;
1371 uint32_t size_comp1;
Edward O'Callaghanc093d682020-05-26 17:53:53 +10001372 uint32_t addr_mask;
1373 bool only_4k;
1374 uint32_t hsfc_fcycle;
stefanct83d99e82011-11-08 10:55:54 +00001375} hwseq_data;
1376
Edward O'Callaghanc093d682020-05-26 17:53:53 +10001377/* Sets FLA in FADDR to (addr & hwseq_data.addr_mask) without touching other bits. */
David Hendricks07af3a42011-07-11 22:13:02 -07001378static void ich_hwseq_set_addr(uint32_t addr)
1379{
Edward O'Callaghanc093d682020-05-26 17:53:53 +10001380 uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~hwseq_data.addr_mask;
1381 REGWRITE32(ICH9_REG_FADDR, (addr & hwseq_data.addr_mask) | addr_old);
David Hendricks07af3a42011-07-11 22:13:02 -07001382}
1383
Edward O'Callaghandf43e902020-11-13 23:08:26 +11001384static int ich_hwseq_check_access(const struct flashctx *flash, unsigned int start,
Edward O'Callaghand36b6e32020-08-07 21:07:25 +10001385 unsigned int len, int read)
1386{
1387 return check_fd_permissions(NULL, read ? SPI_OPCODE_TYPE_READ_NO_ADDRESS: SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, start, len);
1388}
1389
David Hendricks07af3a42011-07-11 22:13:02 -07001390/* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
stefanct3d3b6ee2011-10-20 12:57:14 +00001391 * of the block containing this address. May return nonsense if the address is
1392 * not valid. The erase block size for a specific address depends on the flash
1393 * partition layout as specified by FPB and the partition properties as defined
1394 * by UVSCC and LVSCC respectively. An alternative to implement this method
1395 * would be by querying FPB and the respective VSCC register directly.
1396 */
David Hendricks07af3a42011-07-11 22:13:02 -07001397static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr)
1398{
1399 uint8_t enc_berase;
Vadim Bendeburya6f9c4a2013-09-19 14:38:34 -07001400 static const uint32_t dec_berase[4] = {
David Hendricks07af3a42011-07-11 22:13:02 -07001401 256,
1402 4 * 1024,
1403 8 * 1024,
1404 64 * 1024
1405 };
1406
Edward O'Callaghanc093d682020-05-26 17:53:53 +10001407 if (hwseq_data.only_4k) {
1408 return 4 * 1024;
1409 }
1410
David Hendricks07af3a42011-07-11 22:13:02 -07001411 ich_hwseq_set_addr(addr);
Edward O'Callaghanc093d682020-05-26 17:53:53 +10001412 enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >> HSFS_BERASE_OFF;
David Hendricks07af3a42011-07-11 22:13:02 -07001413 return dec_berase[enc_berase];
1414}
1415
stefanct3d3b6ee2011-10-20 12:57:14 +00001416/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
David Hendricks07af3a42011-07-11 22:13:02 -07001417 Resets all error flags in HSFS.
1418 Returns 0 if the cycle completes successfully without errors within
1419 timeout us, 1 on errors. */
1420static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
Edward O'Callaghanacce4622020-07-16 15:39:19 +10001421 unsigned int len,
1422 enum ich_chipset ich_gen)
David Hendricks07af3a42011-07-11 22:13:02 -07001423{
1424 uint16_t hsfs;
1425 uint32_t addr;
1426
stefanct3d3b6ee2011-10-20 12:57:14 +00001427 timeout /= 8; /* scale timeout duration to counter */
David Hendricks07af3a42011-07-11 22:13:02 -07001428 while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) &
1429 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
1430 --timeout) {
stefanct3d3b6ee2011-10-20 12:57:14 +00001431 programmer_delay(8);
David Hendricks07af3a42011-07-11 22:13:02 -07001432 }
1433 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1434 if (!timeout) {
Edward O'Callaghanc093d682020-05-26 17:53:53 +10001435 addr = REGREAD32(ICH9_REG_FADDR) & hwseq_data.addr_mask;
David Hendricks07af3a42011-07-11 22:13:02 -07001436 msg_perr("Timeout error between offset 0x%08x and "
stefanct83d99e82011-11-08 10:55:54 +00001437 "0x%08x (= 0x%08x + %d)!\n",
1438 addr, addr + len - 1, addr, len - 1);
Edward O'Callaghanacce4622020-07-16 15:39:19 +10001439 prettyprint_ich9_reg_hsfs(hsfs, ich_gen);
1440 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC), ich_gen);
David Hendricks07af3a42011-07-11 22:13:02 -07001441 return 1;
1442 }
1443
1444 if (hsfs & HSFS_FCERR) {
Edward O'Callaghanc093d682020-05-26 17:53:53 +10001445 addr = REGREAD32(ICH9_REG_FADDR) & hwseq_data.addr_mask;
David Hendricks07af3a42011-07-11 22:13:02 -07001446 msg_perr("Transaction error between offset 0x%08x and "
stefanct83d99e82011-11-08 10:55:54 +00001447 "0x%08x (= 0x%08x + %d)!\n",
stefanct3d3b6ee2011-10-20 12:57:14 +00001448 addr, addr + len - 1, addr, len - 1);
Edward O'Callaghanacce4622020-07-16 15:39:19 +10001449 prettyprint_ich9_reg_hsfs(hsfs, ich_gen);
1450 prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC), ich_gen);
David Hendricks07af3a42011-07-11 22:13:02 -07001451 return 1;
1452 }
1453 return 0;
1454}
stefanct83d99e82011-11-08 10:55:54 +00001455
Edward O'Callaghan0f34ce82020-11-19 21:40:09 +11001456/* Given RDID info, return pointer to entry in flashchips[] */
1457static const struct flashchip *flash_id_to_entry(uint32_t mfg_id, uint32_t model_id)
1458{
1459 const struct flashchip *chip;
1460
1461 for (chip = &flashchips[0]; chip->vendor; chip++) {
1462 if ((chip->manufacture_id == mfg_id) &&
1463 (chip->model_id == model_id))
1464 return chip;
1465 }
1466
1467 return NULL;
1468}
1469
Nikolai Artemiev1ea569a2021-05-10 12:59:36 +10001470static uint8_t ich_hwseq_read_status(const struct flashctx *flash);
1471static int ich_hwseq_write_status(const struct flashctx *flash, int status);
1472
Edward O'Callaghan52261e42020-11-13 00:59:07 +11001473static int ich_hwseq_get_flash_id(struct flashctx *flash, enum ich_chipset ich_gen)
Edward O'Callaghan0f183122020-08-01 21:17:36 +10001474{
1475 uint32_t hsfsc, data, mfg_id, model_id;
1476 const struct flashchip *entry;
1477 const int len = sizeof(data);
1478
1479 /* make sure FDONE, FCERR, & AEL are cleared */
1480 REGWRITE32(ICH9_REG_HSFS, REGREAD32(ICH9_REG_HSFS));
1481
1482 /* Set RDID as flash cycle and FGO */
1483 hsfsc = REGREAD32(ICH9_REG_HSFS);
1484 hsfsc &= ~HSFSC_FCYCLE;
1485 hsfsc &= ~HSFSC_FDBC;
1486 hsfsc |= ((len - 1) << HSFSC_FDBC_OFF) & HSFSC_FDBC;
1487 hsfsc |= (0x6 << HSFSC_FCYCLE_OFF) | HSFSC_FGO;
1488 REGWRITE32(ICH9_REG_HSFS, hsfsc);
1489 /* poll for 100ms */
Edward O'Callaghan52261e42020-11-13 00:59:07 +11001490 if (ich_hwseq_wait_for_cycle_complete(100 * 1000, len, ich_gen)) {
Edward O'Callaghan0f183122020-08-01 21:17:36 +10001491 msg_perr("Timed out waiting for RDID to complete.\n");
1492 return 0;
1493 }
1494
1495 /*
1496 * Data will appear in reverse order:
1497 * Byte 0: Manufacturer ID
1498 * Byte 1: Model ID (MSB)
1499 * Byte 2: Model ID (LSB)
1500 */
1501 ich_read_data((uint8_t *)&data, len, ICH9_REG_FDATA0);
1502 mfg_id = data & 0xff;
1503 model_id = (data & 0xff00) | ((data >> 16) & 0xff);
1504
1505 entry = flash_id_to_entry(mfg_id, model_id);
1506 if (entry == NULL) {
1507 msg_perr("Unable to identify chip, mfg_id: 0x%02x, "
1508 "model_id: 0x%02x\n", mfg_id, model_id);
1509 return 0;
1510 } else {
1511 msg_pdbg("Chip identified: %s\n", entry->name);
1512 /* Update informational flash chip entries only */
1513 flash->chip->vendor = entry->vendor;
1514 flash->chip->name = entry->name;
1515 flash->chip->manufacture_id = entry->manufacture_id;
1516 flash->chip->model_id = entry->model_id;
1517 /* total_size read from flash descriptor */
1518 flash->chip->page_size = entry->page_size;
1519 flash->chip->feature_bits = entry->feature_bits;
1520 flash->chip->tested = entry->tested;
Nikolai Artemiev1ea569a2021-05-10 12:59:36 +10001521
1522 /* Access to status register and access checking*/
1523 flash->chip->check_access = ich_hwseq_check_access,
1524 flash->chip->read_status = ich_hwseq_read_status,
1525 flash->chip->write_status = ich_hwseq_write_status,
1526 flash->chip->unlock = &spi_disable_blockprotect;
Edward O'Callaghan0f183122020-08-01 21:17:36 +10001527 }
1528
1529 return 1;
1530}
1531
Edward O'Callaghandf43e902020-11-13 23:08:26 +11001532static uint8_t ich_hwseq_read_status(const struct flashctx *flash)
Edward O'Callaghan3fd6e002020-08-07 20:48:24 +10001533{
1534 uint32_t hsfc;
1535 uint32_t timeout = 5000 * 1000;
1536 int len = 1;
1537 uint8_t buf;
1538
1539 msg_pdbg("Reading Status register\n");
1540
1541 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1542 REGWRITE32(ICH9_REG_HSFS, REGREAD32(ICH9_REG_HSFS));
1543
1544 hsfc = REGREAD32(ICH9_REG_HSFS);
1545 hsfc &= ~HSFSC_FCYCLE; /* set read operation */
1546
1547 /* read status register */
1548 hsfc |= (0x8 << HSFSC_FCYCLE_OFF);
1549
1550 hsfc &= ~HSFSC_FDBC; /* clear byte count */
1551 /* set byte count */
1552 hsfc |= (((len - 1) << HSFSC_FDBC_OFF) & HSFSC_FDBC);
1553 hsfc |= HSFSC_FGO; /* start */
1554 REGWRITE32(ICH9_REG_HSFS, hsfc);
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11001555 if (ich_hwseq_wait_for_cycle_complete(timeout, len, ich_generation)) {
Edward O'Callaghan3fd6e002020-08-07 20:48:24 +10001556 msg_perr("Reading Status register failed\n!!");
1557 return -1;
1558 }
1559 ich_read_data(&buf, len, ICH9_REG_FDATA0);
1560 return buf;
1561}
1562
Edward O'Callaghandf43e902020-11-13 23:08:26 +11001563static int ich_hwseq_write_status(const struct flashctx *flash, int status)
Edward O'Callaghan3fd6e002020-08-07 20:48:24 +10001564{
1565 uint32_t hsfc;
1566 uint32_t timeout = 5000 * 1000;
1567 int len = 1;
1568 uint8_t buf = status;
1569
1570 msg_pdbg("Writing status register\n");
1571
1572 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1573 REGWRITE32(ICH9_REG_HSFS, REGREAD32(ICH9_REG_HSFS));
1574
1575 ich_fill_data(&buf, len, ICH9_REG_FDATA0);
1576 hsfc = REGREAD32(ICH9_REG_HSFS);
1577 hsfc &= ~HSFSC_FCYCLE; /* clear operation */
1578
1579 /* write status register */
1580 hsfc |= (0x7 << HSFSC_FCYCLE_OFF);
1581 hsfc &= ~HSFSC_FDBC; /* clear byte count */
1582
1583 /* set byte count */
1584 hsfc |= (((len - 1) << HSFSC_FDBC_OFF) & HSFSC_FDBC);
1585 hsfc |= HSFSC_FGO; /* start */
1586 REGWRITE32(ICH9_REG_HSFS, hsfc);
1587
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11001588 if (ich_hwseq_wait_for_cycle_complete(timeout, len, ich_generation)) {
Edward O'Callaghan3fd6e002020-08-07 20:48:24 +10001589 msg_perr("Writing Status register failed\n!!");
1590 return -1;
1591 }
1592 return 0;
1593}
1594
Edward O'Callaghand757b422020-05-26 21:22:12 +10001595static int ich_hwseq_probe(struct flashctx *flash)
stefanct83d99e82011-11-08 10:55:54 +00001596{
1597 uint32_t total_size, boundary;
1598 uint32_t erase_size_low, size_low, erase_size_high, size_high;
1599 struct block_eraser *eraser;
1600
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11001601 if (ich_hwseq_get_flash_id(flash, ich_generation) != 1) {
Edward O'Callaghan0f183122020-08-01 21:17:36 +10001602 msg_perr("Unable to read flash chip ID\n");
1603 return 0;
1604 }
1605
stefanct83d99e82011-11-08 10:55:54 +00001606 total_size = hwseq_data.size_comp0 + hwseq_data.size_comp1;
Edward O'Callaghan59919f92019-09-09 00:15:08 +10001607 msg_cdbg("Hardware sequencing reports %d attached SPI flash chip",
stefanct83d99e82011-11-08 10:55:54 +00001608 (hwseq_data.size_comp1 != 0) ? 2 : 1);
1609 if (hwseq_data.size_comp1 != 0)
1610 msg_cdbg("s with a combined");
1611 else
1612 msg_cdbg(" with a");
1613 msg_cdbg(" density of %d kB.\n", total_size / 1024);
Patrick Georgif3fa2992017-02-02 16:24:44 +01001614 flash->chip->total_size = total_size / 1024;
stefanct83d99e82011-11-08 10:55:54 +00001615
Patrick Georgif3fa2992017-02-02 16:24:44 +01001616 eraser = &(flash->chip->block_erasers[0]);
Edward O'Callaghanc093d682020-05-26 17:53:53 +10001617 if (!hwseq_data.only_4k)
1618 boundary = (REGREAD32(ICH9_REG_FPB) & FPB_FPBA) << 12;
1619 else
1620 boundary = 0;
stefanct83d99e82011-11-08 10:55:54 +00001621 size_high = total_size - boundary;
1622 erase_size_high = ich_hwseq_get_erase_block_size(boundary);
1623
1624 if (boundary == 0) {
Edward O'Callaghan59919f92019-09-09 00:15:08 +10001625 msg_cdbg2("There is only one partition containing the whole "
stefanct83d99e82011-11-08 10:55:54 +00001626 "address space (0x%06x - 0x%06x).\n", 0, size_high-1);
1627 eraser->eraseblocks[0].size = erase_size_high;
1628 eraser->eraseblocks[0].count = size_high / erase_size_high;
Edward O'Callaghan59919f92019-09-09 00:15:08 +10001629 msg_cdbg2("There are %d erase blocks with %d B each.\n",
stefanct83d99e82011-11-08 10:55:54 +00001630 size_high / erase_size_high, erase_size_high);
1631 } else {
Edward O'Callaghan59919f92019-09-09 00:15:08 +10001632 msg_cdbg2("The flash address space (0x%06x - 0x%06x) is divided "
stefanct83d99e82011-11-08 10:55:54 +00001633 "at address 0x%06x in two partitions.\n",
Edward O'Callaghan13e6abd2020-05-26 22:15:22 +10001634 0, total_size-1, boundary);
stefanct83d99e82011-11-08 10:55:54 +00001635 size_low = total_size - size_high;
1636 erase_size_low = ich_hwseq_get_erase_block_size(0);
1637
1638 eraser->eraseblocks[0].size = erase_size_low;
1639 eraser->eraseblocks[0].count = size_low / erase_size_low;
1640 msg_cdbg("The first partition ranges from 0x%06x to 0x%06x.\n",
1641 0, size_low-1);
1642 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1643 size_low / erase_size_low, erase_size_low);
1644
1645 eraser->eraseblocks[1].size = erase_size_high;
1646 eraser->eraseblocks[1].count = size_high / erase_size_high;
1647 msg_cdbg("The second partition ranges from 0x%06x to 0x%06x.\n",
Edward O'Callaghan13e6abd2020-05-26 22:15:22 +10001648 boundary, total_size-1);
stefanct83d99e82011-11-08 10:55:54 +00001649 msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1650 size_high / erase_size_high, erase_size_high);
1651 }
Edward O'Callaghan53f03f02020-05-27 14:16:28 +10001652 flash->chip->tested = TEST_OK_PREW;
stefanct83d99e82011-11-08 10:55:54 +00001653 return 1;
1654}
1655
Edward O'Callaghand757b422020-05-26 21:22:12 +10001656static int ich_hwseq_block_erase(struct flashctx *flash, unsigned int addr,
1657 unsigned int len)
stefanct83d99e82011-11-08 10:55:54 +00001658{
1659 uint32_t erase_block;
1660 uint16_t hsfc;
1661 uint32_t timeout = 5000 * 1000; /* 5 s for max 64 kB */
Edward O'Callaghan94967cc2020-08-07 20:19:15 +10001662 int result = 0;
stefanct83d99e82011-11-08 10:55:54 +00001663
Edward O'Callaghand13334a2020-07-23 12:51:00 +10001664 if (is_dry_run())
Vadim Bendebury066143d2018-07-16 18:20:33 -07001665 return 0;
1666
stefanct83d99e82011-11-08 10:55:54 +00001667 erase_block = ich_hwseq_get_erase_block_size(addr);
1668 if (len != erase_block) {
1669 msg_cerr("Erase block size for address 0x%06x is %d B, "
1670 "but requested erase block size is %d B. "
1671 "Not erasing anything.\n", addr, erase_block, len);
1672 return -1;
1673 }
1674
1675 /* Although the hardware supports this (it would erase the whole block
1676 * containing the address) we play safe here. */
1677 if (addr % erase_block != 0) {
1678 msg_cerr("Erase address 0x%06x is not aligned to the erase "
1679 "block boundary (any multiple of %d). "
1680 "Not erasing anything.\n", addr, erase_block);
1681 return -1;
1682 }
1683
Patrick Georgif3fa2992017-02-02 16:24:44 +01001684 if (addr + len > flash->chip->total_size * 1024) {
stefanct83d99e82011-11-08 10:55:54 +00001685 msg_perr("Request to erase some inaccessible memory address(es)"
1686 " (addr=0x%x, len=%d). "
1687 "Not erasing anything.\n", addr, len);
1688 return -1;
1689 }
1690
Edward O'Callaghan94967cc2020-08-07 20:19:15 +10001691 /* Check flash region permissions before erasing */
1692 result = check_fd_permissions(NULL, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, addr, len);
1693 if (result)
1694 return result;
1695
Edward O'Callaghan59919f92019-09-09 00:15:08 +10001696 msg_pdbg("Erasing %d bytes starting at 0x%06x.\n", len, addr);
Edward O'Callaghan434a9902020-05-26 22:19:04 +10001697 ich_hwseq_set_addr(addr);
stefanct83d99e82011-11-08 10:55:54 +00001698
1699 /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
1700 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1701
1702 hsfc = REGREAD16(ICH9_REG_HSFC);
Edward O'Callaghanc093d682020-05-26 17:53:53 +10001703 hsfc &= ~hwseq_data.hsfc_fcycle; /* clear operation */
stefanct83d99e82011-11-08 10:55:54 +00001704 hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
1705 hsfc |= HSFC_FGO; /* start */
Edward O'Callaghan59919f92019-09-09 00:15:08 +10001706 msg_pdbg("HSFC used for block erasing: ");
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11001707 prettyprint_ich9_reg_hsfc(hsfc, ich_generation);
stefanct83d99e82011-11-08 10:55:54 +00001708 REGWRITE16(ICH9_REG_HSFC, hsfc);
1709
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11001710 if (ich_hwseq_wait_for_cycle_complete(timeout, len, ich_generation))
stefanct83d99e82011-11-08 10:55:54 +00001711 return -1;
Edward O'Callaghan94967cc2020-08-07 20:19:15 +10001712
1713 return result;
stefanct83d99e82011-11-08 10:55:54 +00001714}
1715
Edward O'Callaghand757b422020-05-26 21:22:12 +10001716static int ich_hwseq_read(struct flashctx *flash, uint8_t *buf,
1717 unsigned int addr, unsigned int len)
stefanct83d99e82011-11-08 10:55:54 +00001718{
1719 uint16_t hsfc;
1720 uint16_t timeout = 100 * 60;
1721 uint8_t block_len;
Edward O'Callaghan94967cc2020-08-07 20:19:15 +10001722 int result = 0, chunk_status = 0;
stefanct83d99e82011-11-08 10:55:54 +00001723
Edward O'Callaghan03c389d2020-07-08 23:07:24 +10001724 if (addr + len > flash->chip->total_size * 1024) {
stefanct83d99e82011-11-08 10:55:54 +00001725 msg_perr("Request to read from an inaccessible memory address "
1726 "(addr=0x%x, len=%d).\n", addr, len);
1727 return -1;
1728 }
1729
Edward O'Callaghan59919f92019-09-09 00:15:08 +10001730 msg_pdbg("Reading %d bytes starting at 0x%06x.\n", len, addr);
stefanct83d99e82011-11-08 10:55:54 +00001731 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1732 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1733
1734 while (len > 0) {
Edward O'Callaghan434a9902020-05-26 22:19:04 +10001735 /* Obey programmer limit... */
Edward O'Callaghanc66827e2020-10-09 12:22:04 +11001736 block_len = min(len, flash->mst->opaque.max_data_read);
Edward O'Callaghan434a9902020-05-26 22:19:04 +10001737 /* as well as flash chip page borders as demanded in the Intel datasheets. */
1738 block_len = min(block_len, 256 - (addr & 0xFF));
1739
Edward O'Callaghan94967cc2020-08-07 20:19:15 +10001740 /* Check flash region permissions before reading */
1741 chunk_status = check_fd_permissions(NULL, SPI_OPCODE_TYPE_READ_NO_ADDRESS, addr, block_len);
1742 if (chunk_status) {
1743 if (ignore_error(chunk_status)) {
1744 /* fill this chunk with 0xff bytes and
1745 * inform the caller about the error */
1746 memset(buf, 0xff, block_len);
1747 result = chunk_status;
1748 } else {
1749 return chunk_status;
1750 }
1751 } else {
1752 ich_hwseq_set_addr(addr);
1753 hsfc = REGREAD16(ICH9_REG_HSFC);
1754 hsfc &= ~hwseq_data.hsfc_fcycle; /* set read operation */
1755 hsfc &= ~HSFC_FDBC; /* clear byte count */
1756 /* set byte count */
1757 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1758 hsfc |= HSFC_FGO; /* start */
1759 REGWRITE16(ICH9_REG_HSFC, hsfc);
stefanct83d99e82011-11-08 10:55:54 +00001760
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11001761 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len, ich_generation))
Edward O'Callaghan94967cc2020-08-07 20:19:15 +10001762 return 1;
1763 ich_read_data(buf, block_len, ICH9_REG_FDATA0);
1764 }
stefanct83d99e82011-11-08 10:55:54 +00001765 addr += block_len;
1766 buf += block_len;
1767 len -= block_len;
1768 }
Edward O'Callaghan94967cc2020-08-07 20:19:15 +10001769 return result;
stefanct83d99e82011-11-08 10:55:54 +00001770}
1771
Edward O'Callaghand757b422020-05-26 21:22:12 +10001772static int ich_hwseq_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len)
stefanct83d99e82011-11-08 10:55:54 +00001773{
1774 uint16_t hsfc;
1775 uint16_t timeout = 100 * 60;
1776 uint8_t block_len;
Edward O'Callaghan94967cc2020-08-07 20:19:15 +10001777 int result = 0;
stefanct83d99e82011-11-08 10:55:54 +00001778
Edward O'Callaghan03c389d2020-07-08 23:07:24 +10001779 if (addr + len > flash->chip->total_size * 1024) {
stefanct83d99e82011-11-08 10:55:54 +00001780 msg_perr("Request to write to an inaccessible memory address "
1781 "(addr=0x%x, len=%d).\n", addr, len);
1782 return -1;
1783 }
1784
Edward O'Callaghan59919f92019-09-09 00:15:08 +10001785 msg_pdbg("Writing %d bytes starting at 0x%06x.\n", len, addr);
stefanct83d99e82011-11-08 10:55:54 +00001786 /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1787 REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1788
1789 while (len > 0) {
1790 ich_hwseq_set_addr(addr);
Edward O'Callaghan434a9902020-05-26 22:19:04 +10001791 /* Obey programmer limit... */
Edward O'Callaghanc66827e2020-10-09 12:22:04 +11001792 block_len = min(len, flash->mst->opaque.max_data_write);
Edward O'Callaghan434a9902020-05-26 22:19:04 +10001793 /* as well as flash chip page borders as demanded in the Intel datasheets. */
1794 block_len = min(block_len, 256 - (addr & 0xFF));
Edward O'Callaghan94967cc2020-08-07 20:19:15 +10001795 /* Check flash region permissions before writing */
1796 result = check_fd_permissions(NULL, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, addr, block_len);
1797 if (result)
1798 return result;
stefanct83d99e82011-11-08 10:55:54 +00001799 ich_fill_data(buf, block_len, ICH9_REG_FDATA0);
1800 hsfc = REGREAD16(ICH9_REG_HSFC);
Edward O'Callaghanc093d682020-05-26 17:53:53 +10001801 hsfc &= ~hwseq_data.hsfc_fcycle; /* clear operation */
stefanct83d99e82011-11-08 10:55:54 +00001802 hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
1803 hsfc &= ~HSFC_FDBC; /* clear byte count */
1804 /* set byte count */
1805 hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1806 hsfc |= HSFC_FGO; /* start */
1807 REGWRITE16(ICH9_REG_HSFC, hsfc);
1808
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11001809 if (ich_hwseq_wait_for_cycle_complete(timeout, block_len, ich_generation))
stefanct83d99e82011-11-08 10:55:54 +00001810 return -1;
1811 addr += block_len;
1812 buf += block_len;
1813 len -= block_len;
1814 }
Edward O'Callaghan94967cc2020-08-07 20:19:15 +10001815
1816 return result;
stefanct83d99e82011-11-08 10:55:54 +00001817}
David Hendricks07af3a42011-07-11 22:13:02 -07001818
Edward O'Callaghand757b422020-05-26 21:22:12 +10001819static int ich_spi_send_multicommand(const struct flashctx *flash,
1820 struct spi_command *cmds)
hailfinger948b81f2009-07-22 15:36:50 +00001821{
1822 int ret = 0;
hailfinger82e32492010-02-11 11:28:37 +00001823 int i;
hailfingerbb092112009-09-18 15:50:56 +00001824 int oppos, preoppos;
1825 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
hailfingerbb092112009-09-18 15:50:56 +00001826 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
hailfinger82e32492010-02-11 11:28:37 +00001827 /* Next command is valid. */
hailfingerbb092112009-09-18 15:50:56 +00001828 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1829 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
hailfinger82e32492010-02-11 11:28:37 +00001830 if ((oppos == -1) && (preoppos != -1)) {
1831 /* Current command is listed as preopcode in
1832 * ICH struct OPCODES, but next command is not
1833 * listed as opcode in that struct.
1834 * Check for command sanity, then
1835 * try to reprogram the ICH opcode list.
1836 */
1837 if (find_preop(curopcodes,
1838 (cmds + 1)->writearr[0]) != -1) {
snelsone42c3802010-05-07 20:09:04 +00001839 msg_perr("%s: Two subsequent "
hailfinger82e32492010-02-11 11:28:37 +00001840 "preopcodes 0x%02x and 0x%02x, "
1841 "ignoring the first.\n",
1842 __func__, cmds->writearr[0],
1843 (cmds + 1)->writearr[0]);
1844 continue;
1845 }
1846 /* If the chipset is locked down, we'll fail
1847 * during execution of the next command anyway.
1848 * No need to bother with fixups.
1849 */
1850 if (!ichspi_lock) {
hailfinger4c973122010-10-05 22:06:05 +00001851 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1852 if (oppos == -1)
1853 continue;
1854 curopcodes->opcode[oppos].atomic = preoppos + 1;
hailfinger82e32492010-02-11 11:28:37 +00001855 continue;
1856 }
1857 }
1858 if ((oppos != -1) && (preoppos != -1)) {
1859 /* Current command is listed as preopcode in
1860 * ICH struct OPCODES and next command is listed
1861 * as opcode in that struct. Match them up.
1862 */
1863 curopcodes->opcode[oppos].atomic = preoppos + 1;
hailfingerbb092112009-09-18 15:50:56 +00001864 continue;
hailfinger82e32492010-02-11 11:28:37 +00001865 }
1866 /* If none of the above if-statements about oppos or
1867 * preoppos matched, this is a normal opcode.
1868 */
1869 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001870 ret = ich_spi_send_command(flash, cmds->writecnt, cmds->readcnt,
hailfingerbb092112009-09-18 15:50:56 +00001871 cmds->writearr, cmds->readarr);
hailfinger82e32492010-02-11 11:28:37 +00001872 /* Reset the type of all opcodes to non-atomic. */
1873 for (i = 0; i < 8; i++)
1874 curopcodes->opcode[i].atomic = 0;
hailfinger948b81f2009-07-22 15:36:50 +00001875 }
1876 return ret;
1877}
hailfinger324a9cc2010-05-26 01:45:41 +00001878
mkarcher74d30132010-07-22 18:04:15 +00001879#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1880#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1881#define ICH_BRWA(x) ((x >> 8) & 0xff)
1882#define ICH_BRRA(x) ((x >> 0) & 0xff)
1883
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10001884static const enum ich_access_protection access_perms_to_protection[] = {
1885 LOCKED, WRITE_PROT, READ_PROT, NO_PROT
1886};
1887static const char *const access_names[] = {
1888 "locked", "read-only", "write-only", "read-write"
1889};
1890
1891static enum ich_access_protection ich9_handle_frap(uint32_t frap, unsigned int i)
mkarcher74d30132010-07-22 18:04:15 +00001892{
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10001893 const int rwperms_unknown = ARRAY_SIZE(access_names);
1894 static const char *const region_names[] = {
1895 "Flash Descriptor", "BIOS", "Management Engine",
1896 "Gigabit Ethernet", "Platform Data", "Device Expansion",
1897 "BIOS2", "unknown", "EC/BMC",
1898 };
1899 const char *const region_name = i < ARRAY_SIZE(region_names) ? region_names[i] : "unknown";
1900
1901 uint32_t base, limit;
1902 int rwperms;
1903 const int offset = i < 12
1904 ? ICH9_REG_FREG0 + i * 4
1905 : APL_REG_FREG12 + (i - 12) * 4;
mkarcher74d30132010-07-22 18:04:15 +00001906 uint32_t freg = mmio_readl(ich_spibar + offset);
1907
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10001908 if (i < 8) {
1909 rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1910 (((ICH_BRRA(frap) >> i) & 1) << 0);
1911 } else {
1912 /* Datasheets don't define any access bits for regions > 7. We
1913 can't rely on the actual descriptor settings either as there
1914 are several overrides for them (those by other masters are
1915 not even readable by us, *shrug*). */
1916 rwperms = rwperms_unknown;
1917 }
David Hendricks53540f92016-09-03 00:34:41 +00001918
Bora Guvendikc34416b2019-01-07 16:10:48 -08001919 /*
1920 * Get Region 0 - 7 Permission bits, region 8 and above don't have
1921 * bits to indicate permissions in Flash Region Access Permissions
1922 * register.
1923 */
Duncan Lauriea62ff822019-04-25 12:12:20 -07001924 if ( i >= EMBEDDED_CONTROLLER_REGION ) {
1925 /*
1926 * Use Flash Descriptor Observe register to determine if
1927 * the EC region can be written by the BIOS master.
1928 */
Bora Guvendikc34416b2019-01-07 16:10:48 -08001929 rwperms = FD_REGION_READ_WRITE;
Duncan Lauriea62ff822019-04-25 12:12:20 -07001930 if (i == EMBEDDED_CONTROLLER_REGION &&
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11001931 ich_generation >= CHIPSET_100_SERIES_SUNRISE_POINT) {
Nikolai Artemiev4b29f9b2021-02-12 11:47:41 +11001932 struct ich_descriptors desc;
1933 memset(&desc, 0, sizeof(desc));
Duncan Laurieb2d845b2019-05-28 10:10:03 -07001934 /* Region is RW if flash descriptor override is set */
1935 freg = mmio_readl(ich_spibar + PCH100_REG_HSFSC);
Edward O'Callaghanf58c0e62020-08-01 20:58:18 +10001936 if ((freg & HSFS_FDV) && !(freg & HSFS_FDOPSS))
Duncan Laurieb2d845b2019-05-28 10:10:03 -07001937 rwperms = FD_REGION_READ_WRITE;
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11001938 else if (read_ich_descriptors_via_fdo(ich_generation, ich_spibar, &desc) == ICH_RET_OK) {
Edward O'Callaghanaef44cb2020-07-03 15:31:14 +10001939 const struct ich_desc_master *const mstr = &desc.master;
1940#define BIT(x) (1<<(x))
1941 int bios_ec_r = mstr->mstr[i].read & BIT(16); /* BIOS_EC_r in PCH100+ */
1942 int bios_ec_w = mstr->mstr[i].write & BIT(28); /* BIOS_EC_w in PCH100+ */
1943 if (bios_ec_r && bios_ec_w)
Duncan Lauriea62ff822019-04-25 12:12:20 -07001944 rwperms = FD_REGION_READ_WRITE;
Edward O'Callaghanaef44cb2020-07-03 15:31:14 +10001945 else if (bios_ec_r && !bios_ec_w)
Duncan Lauriea62ff822019-04-25 12:12:20 -07001946 rwperms = FD_REGION_READ_ONLY;
Edward O'Callaghanaef44cb2020-07-03 15:31:14 +10001947 else if (!bios_ec_r && bios_ec_w)
Duncan Lauriea62ff822019-04-25 12:12:20 -07001948 rwperms = FD_REGION_WRITE_ONLY;
1949 else
1950 rwperms = FD_REGION_LOCKED;
1951 }
1952 }
1953 }
Bora Guvendikc34416b2019-01-07 16:10:48 -08001954
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10001955 base = ICH_FREG_BASE(freg);
1956 limit = ICH_FREG_LIMIT(freg);
1957
1958 /* HACK to support check_fd_permissions() */
1959 fd_regions[i].base = base;
1960 fd_regions[i].limit = limit | 0x0fff;
David Hendricks1ed1d352011-11-23 17:54:37 -08001961 fd_regions[i].permission = &fd_region_permissions[rwperms];
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10001962
1963 if (base > limit || (freg == 0 && i > 0)) {
mkarcher74d30132010-07-22 18:04:15 +00001964 /* this FREG is disabled */
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10001965 msg_pdbg2("0x%02X: 0x%08x FREG%u: %s region is unused.\n",
1966 offset, freg, i, region_name);
1967 return NO_PROT;
1968 }
1969 msg_pdbg("0x%02X: 0x%08x ", offset, freg);
1970 if (rwperms == 0x3) {
1971 msg_pdbg("FREG%u: %s region (0x%08x-0x%08x) is %s.\n", i,
1972 region_name, base, limit, access_names[rwperms]);
1973 return NO_PROT;
1974 }
1975 if (rwperms == rwperms_unknown) {
1976 msg_pdbg("FREG%u: %s region (0x%08x-0x%08x) has unknown permissions.\n",
1977 i, region_name, base, limit);
1978 return NO_PROT;
mkarcher74d30132010-07-22 18:04:15 +00001979 }
David Hendricks53540f92016-09-03 00:34:41 +00001980
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10001981 msg_pinfo("FREG%u: %s region (0x%08x-0x%08x) is %s.\n", i,
1982 region_name, base, limit, access_names[rwperms]);
1983 return access_perms_to_protection[rwperms];
mkarcher74d30132010-07-22 18:04:15 +00001984}
1985
stefanct7ab834a2011-09-17 21:21:48 +00001986 /* In contrast to FRAP and the master section of the descriptor the bits
1987 * in the PR registers have an inverted meaning. The bits in FRAP
1988 * indicate read and write access _grant_. Here they indicate read
1989 * and write _protection_ respectively. If both bits are 0 the address
1990 * bits are ignored.
1991 */
1992#define ICH_PR_PERMS(pr) (((~((pr) >> PR_RP_OFF) & 1) << 0) | \
1993 ((~((pr) >> PR_WP_OFF) & 1) << 1))
1994
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10001995static enum ich_access_protection ich9_handle_pr(const size_t reg_pr0, unsigned int i)
stefanct7ab834a2011-09-17 21:21:48 +00001996{
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10001997 uint8_t off = reg_pr0 + (i * 4);
Jack Rosenthal47a3cc92020-07-28 10:20:02 -06001998 uint32_t pr = mmio_readl(ich_spibar + off);
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10001999 unsigned int rwperms = ICH_PR_PERMS(pr);
Edward O'Callaghan34b4b112020-05-26 21:54:52 +10002000
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10002001 /* From 5 on we have GPR registers and start from 0 again. */
2002 const char *const prefix = i >= 5 ? "G" : "";
2003 if (i >= 5)
2004 i -= 5;
2005
2006 if (rwperms == 0x3) {
2007 msg_pdbg2("0x%02X: 0x%08x (%sPR%u is unused)\n", off, pr, prefix, i);
2008 return NO_PROT;
2009 }
2010
2011 msg_pdbg("0x%02X: 0x%08x ", off, pr);
2012 msg_pwarn("%sPR%u: Warning: 0x%08x-0x%08x is %s.\n", prefix, i, ICH_FREG_BASE(pr),
2013 ICH_FREG_LIMIT(pr), access_names[rwperms]);
2014 return access_perms_to_protection[rwperms];
stefanct7ab834a2011-09-17 21:21:48 +00002015}
2016
stefanctdd95a212011-09-17 22:21:55 +00002017/* Set/Clear the read and write protection enable bits of PR register @i
2018 * according to @read_prot and @write_prot. */
Edward O'Callaghancdd87bb2020-05-18 15:32:08 +10002019static void ich9_set_pr(const size_t reg_pr0, int i, int read_prot, int write_prot)
stefanctdd95a212011-09-17 22:21:55 +00002020{
Edward O'Callaghancdd87bb2020-05-18 15:32:08 +10002021 void *addr = ich_spibar + reg_pr0 + (i * 4);
stefanctdd95a212011-09-17 22:21:55 +00002022 uint32_t old = mmio_readl(addr);
2023 uint32_t new;
2024
2025 msg_gspew("PR%u is 0x%08x", i, old);
2026 new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF));
2027 if (read_prot)
2028 new |= (1 << PR_RP_OFF);
2029 if (write_prot)
2030 new |= (1 << PR_WP_OFF);
2031 if (old == new) {
2032 msg_gspew(" already.\n");
2033 return;
2034 }
2035 msg_gspew(", trying to set it to 0x%08x ", new);
2036 rmmio_writel(new, addr);
2037 msg_gspew("resulted in 0x%08x.\n", mmio_readl(addr));
2038}
2039
Patrick Georgif4f1e2f2017-03-10 17:38:40 +01002040static const struct spi_master spi_master_ich7 = {
mkarcherd264e9e2011-05-11 17:07:07 +00002041 .max_data_read = 64,
2042 .max_data_write = 64,
2043 .command = ich_spi_send_command,
2044 .multicommand = ich_spi_send_multicommand,
2045 .read = default_spi_read,
2046 .write_256 = default_spi_write_256,
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +11002047 .write_aai = default_spi_write_aai,
mkarcherd264e9e2011-05-11 17:07:07 +00002048};
2049
Patrick Georgif4f1e2f2017-03-10 17:38:40 +01002050static const struct spi_master spi_master_ich9 = {
mkarcherd264e9e2011-05-11 17:07:07 +00002051 .max_data_read = 64,
2052 .max_data_write = 64,
2053 .command = ich_spi_send_command,
2054 .multicommand = ich_spi_send_multicommand,
2055 .read = default_spi_read,
2056 .write_256 = default_spi_write_256,
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +11002057 .write_aai = default_spi_write_aai,
mkarcherd264e9e2011-05-11 17:07:07 +00002058};
2059
Edward O'Callaghanabd30192019-05-14 15:58:19 +10002060static struct opaque_master opaque_master_ich_hwseq = {
David Hendricks07af3a42011-07-11 22:13:02 -07002061 .max_data_read = 64,
2062 .max_data_write = 64,
stefanct83d99e82011-11-08 10:55:54 +00002063 .probe = ich_hwseq_probe,
David Hendricks07af3a42011-07-11 22:13:02 -07002064 .read = ich_hwseq_read,
stefanct83d99e82011-11-08 10:55:54 +00002065 .write = ich_hwseq_write,
2066 .erase = ich_hwseq_block_erase,
David Hendricks07af3a42011-07-11 22:13:02 -07002067};
2068
Edward O'Callaghan397256f2020-07-09 09:59:32 +10002069int ich_init_spi(void *spibar, enum ich_chipset ich_gen)
mkarcher74d30132010-07-22 18:04:15 +00002070{
Edward O'Callaghand757b422020-05-26 21:22:12 +10002071 unsigned int i;
Edward O'Callaghan6f2f8322019-09-06 11:55:24 +10002072 uint16_t tmp2;
mkarcher74d30132010-07-22 18:04:15 +00002073 uint32_t tmp;
stefanct83d99e82011-11-08 10:55:54 +00002074 char *arg;
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10002075 int ich_spi_rw_restricted = 0;
stefanct3d3b6ee2011-10-20 12:57:14 +00002076 int desc_valid = 0;
Richard Hughes16eb3512019-01-02 21:11:08 +00002077 struct ich_descriptors desc;
stefanct83d99e82011-11-08 10:55:54 +00002078 enum ich_spi_mode {
2079 ich_auto,
2080 ich_hwseq,
2081 ich_swseq
2082 } ich_spi_mode = ich_auto;
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10002083 size_t num_freg, num_pr, reg_pr0;
stefanctc035c192011-11-06 23:51:09 +00002084
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11002085 ich_generation = ich_gen;
Edward O'Callaghan6f2f8322019-09-06 11:55:24 +10002086 ich_spibar = spibar;
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +10002087
Richard Hughes16eb3512019-01-02 21:11:08 +00002088 memset(&desc, 0x00, sizeof(struct ich_descriptors));
mkarcher74d30132010-07-22 18:04:15 +00002089
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10002090 /* Moving registers / bits */
Edward O'Callaghan397256f2020-07-09 09:59:32 +10002091 switch (ich_gen) {
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +10002092 case CHIPSET_100_SERIES_SUNRISE_POINT:
2093 case CHIPSET_C620_SERIES_LEWISBURG:
2094 case CHIPSET_300_SERIES_CANNON_POINT:
Matt DeVillier81bc4d32020-08-12 12:48:06 -05002095 case CHIPSET_400_SERIES_COMET_POINT:
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +10002096 case CHIPSET_APOLLO_LAKE:
Angel Pons00b29cf2020-07-10 17:04:10 +02002097 case CHIPSET_GEMINI_LAKE:
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10002098 num_pr = 6; /* Includes GPR0 */
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +10002099 reg_pr0 = PCH100_REG_FPR0;
2100 swseq_data.reg_ssfsc = PCH100_REG_SSFSC;
2101 swseq_data.reg_preop = PCH100_REG_PREOP;
2102 swseq_data.reg_optype = PCH100_REG_OPTYPE;
2103 swseq_data.reg_opmenu = PCH100_REG_OPMENU;
2104 hwseq_data.addr_mask = PCH100_FADDR_FLA;
2105 hwseq_data.only_4k = true;
2106 hwseq_data.hsfc_fcycle = PCH100_HSFC_FCYCLE;
2107 break;
2108 default:
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10002109 num_pr = 5;
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +10002110 reg_pr0 = ICH9_REG_PR0;
2111 swseq_data.reg_ssfsc = ICH9_REG_SSFS;
2112 swseq_data.reg_preop = ICH9_REG_PREOP;
2113 swseq_data.reg_optype = ICH9_REG_OPTYPE;
2114 swseq_data.reg_opmenu = ICH9_REG_OPMENU;
2115 hwseq_data.addr_mask = ICH9_FADDR_FLA;
2116 hwseq_data.only_4k = false;
2117 hwseq_data.hsfc_fcycle = HSFC_FCYCLE;
2118 break;
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10002119 }
Edward O'Callaghan397256f2020-07-09 09:59:32 +10002120 switch (ich_gen) {
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10002121 case CHIPSET_100_SERIES_SUNRISE_POINT:
2122 num_freg = 10;
2123 break;
2124 case CHIPSET_C620_SERIES_LEWISBURG:
2125 num_freg = 12; /* 12 MMIO regs, but 16 regions in FD spec */
2126 break;
2127 case CHIPSET_300_SERIES_CANNON_POINT:
Matt DeVillier81bc4d32020-08-12 12:48:06 -05002128 case CHIPSET_400_SERIES_COMET_POINT:
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10002129 case CHIPSET_APOLLO_LAKE:
Angel Pons00b29cf2020-07-10 17:04:10 +02002130 case CHIPSET_GEMINI_LAKE:
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10002131 num_freg = 16;
2132 break;
2133 default:
2134 num_freg = 5;
2135 break;
2136 }
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10002137
Edward O'Callaghan397256f2020-07-09 09:59:32 +10002138 switch (ich_gen) {
stefanctc035c192011-11-06 23:51:09 +00002139 case CHIPSET_ICH7:
Edward O'Callaghanc8e0a112020-05-26 21:38:37 +10002140 case CHIPSET_TUNNEL_CREEK:
2141 case CHIPSET_CENTERTON:
mkarcher74d30132010-07-22 18:04:15 +00002142 msg_pdbg("0x00: 0x%04x (SPIS)\n",
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002143 mmio_readw(spibar + 0));
mkarcher74d30132010-07-22 18:04:15 +00002144 msg_pdbg("0x02: 0x%04x (SPIC)\n",
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002145 mmio_readw(spibar + 2));
mkarcher74d30132010-07-22 18:04:15 +00002146 msg_pdbg("0x04: 0x%08x (SPIA)\n",
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002147 mmio_readl(spibar + 4));
2148 ichspi_bbar = mmio_readl(spibar + 0x50);
mkarcher74d30132010-07-22 18:04:15 +00002149 msg_pdbg("0x50: 0x%08x (BBAR)\n",
2150 ichspi_bbar);
2151 msg_pdbg("0x54: 0x%04x (PREOP)\n",
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002152 mmio_readw(spibar + 0x54));
mkarcher74d30132010-07-22 18:04:15 +00002153 msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002154 mmio_readw(spibar + 0x56));
mkarcher74d30132010-07-22 18:04:15 +00002155 msg_pdbg("0x58: 0x%08x (OPMENU)\n",
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002156 mmio_readl(spibar + 0x58));
mkarcher74d30132010-07-22 18:04:15 +00002157 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002158 mmio_readl(spibar + 0x5c));
stefanctc73c1db2011-07-24 15:34:56 +00002159 for (i = 0; i < 3; i++) {
mkarcher74d30132010-07-22 18:04:15 +00002160 int offs;
2161 offs = 0x60 + (i * 4);
Edward O'Callaghan59919f92019-09-09 00:15:08 +10002162 msg_pdbg("0x%02x: 0x%08x (PBR%u)\n", offs,
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002163 mmio_readl(spibar + offs), i);
mkarcher74d30132010-07-22 18:04:15 +00002164 }
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002165 if (mmio_readw(spibar) & (1 << 15)) {
Edward O'Callaghan59919f92019-09-09 00:15:08 +10002166 msg_pwarn("WARNING: SPI Configuration Lockdown activated.\n");
mkarcher74d30132010-07-22 18:04:15 +00002167 ichspi_lock = 1;
2168 }
Edward O'Callaghan6884e4f2020-07-16 15:35:00 +10002169 ich_init_opcodes(ich_gen);
Edward O'Callaghand22862f2020-07-16 15:37:26 +10002170 ich_set_bbar(0, ich_gen);
Nico Huberf1eeda62021-05-11 17:38:14 +02002171 register_spi_master(&spi_master_ich7, NULL);
mkarcher74d30132010-07-22 18:04:15 +00002172 break;
stefanctc035c192011-11-06 23:51:09 +00002173 case CHIPSET_ICH8:
David Hendricks07af3a42011-07-11 22:13:02 -07002174 default: /* Future version might behave the same */
stefanct83d99e82011-11-08 10:55:54 +00002175 arg = extract_programmer_param("ich_spi_mode");
2176 if (arg && !strcmp(arg, "hwseq")) {
2177 ich_spi_mode = ich_hwseq;
2178 msg_pspew("user selected hwseq\n");
2179 } else if (arg && !strcmp(arg, "swseq")) {
2180 ich_spi_mode = ich_swseq;
2181 msg_pspew("user selected swseq\n");
2182 } else if (arg && !strcmp(arg, "auto")) {
2183 msg_pspew("user selected auto\n");
2184 ich_spi_mode = ich_auto;
2185 } else if (arg && !strlen(arg)) {
2186 msg_perr("Missing argument for ich_spi_mode.\n");
2187 free(arg);
2188 return ERROR_FATAL;
2189 } else if (arg) {
2190 msg_perr("Unknown argument for ich_spi_mode: %s\n",
2191 arg);
2192 free(arg);
2193 return ERROR_FATAL;
2194 }
2195 free(arg);
2196
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002197 tmp2 = mmio_readw(spibar + ICH9_REG_HSFS);
mkarcher74d30132010-07-22 18:04:15 +00002198 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
Edward O'Callaghanacce4622020-07-16 15:39:19 +10002199 prettyprint_ich9_reg_hsfs(tmp2, ich_gen);
stefanct24bda702011-06-12 08:14:10 +00002200 if (tmp2 & HSFS_FLOCKDN) {
Edward O'Callaghan59919f92019-09-09 00:15:08 +10002201 msg_pinfo("SPI Configuration is locked down.\n");
stefanctc274c862011-06-11 09:53:22 +00002202 ichspi_lock = 1;
2203 }
David Hendricksce6b2fa2011-07-11 22:12:43 -07002204 if (tmp2 & HSFS_FDV)
stefanct3d3b6ee2011-10-20 12:57:14 +00002205 desc_valid = 1;
2206 if (!(tmp2 & HSFS_FDOPSS) && desc_valid)
Edward O'Callaghan130cade2020-07-30 15:42:35 +10002207 msg_pinfo("The Flash Descriptor Override Strap-Pin is set. Restrictions implied by\n"
2208 "the Master Section of the flash descriptor are NOT in effect. Please note\n"
2209 "that Protected Range (PR) restrictions still apply.\n");
Edward O'Callaghan6884e4f2020-07-16 15:35:00 +10002210 ich_init_opcodes(ich_gen);
stefanctc274c862011-06-11 09:53:22 +00002211
stefanctd0064e12011-11-08 11:55:24 +00002212 if (desc_valid) {
Jack Rosenthal47a3cc92020-07-28 10:20:02 -06002213 num_fd_regions = DEFAULT_NUM_FD_REGIONS;
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002214 tmp2 = mmio_readw(spibar + ICH9_REG_HSFC);
stefanctd0064e12011-11-08 11:55:24 +00002215 msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
Edward O'Callaghanacce4622020-07-16 15:39:19 +10002216 prettyprint_ich9_reg_hsfc(tmp2, ich_gen);
stefanctd0064e12011-11-08 11:55:24 +00002217 }
mkarcher74d30132010-07-22 18:04:15 +00002218
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002219 tmp = mmio_readl(spibar + ICH9_REG_FADDR);
Edward O'Callaghan59919f92019-09-09 00:15:08 +10002220 msg_pdbg2("0x08: 0x%08x (FADDR)\n", tmp);
mkarcher74d30132010-07-22 18:04:15 +00002221
Edward O'Callaghan843cc8d2020-07-09 10:13:11 +10002222 switch (ich_gen) {
2223 case CHIPSET_100_SERIES_SUNRISE_POINT:
2224 case CHIPSET_C620_SERIES_LEWISBURG:
2225 case CHIPSET_300_SERIES_CANNON_POINT:
Matt DeVillier81bc4d32020-08-12 12:48:06 -05002226 case CHIPSET_400_SERIES_COMET_POINT:
Edward O'Callaghan843cc8d2020-07-09 10:13:11 +10002227 case CHIPSET_APOLLO_LAKE:
Angel Pons00b29cf2020-07-10 17:04:10 +02002228 case CHIPSET_GEMINI_LAKE:
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002229 tmp = mmio_readl(spibar + PCH100_REG_DLOCK);
Edward O'Callaghan843cc8d2020-07-09 10:13:11 +10002230 msg_pdbg("0x0c: 0x%08x (DLOCK)\n", tmp);
2231 prettyprint_pch100_reg_dlock(tmp);
2232 break;
2233 default:
2234 break;
2235 }
2236
stefanctd0064e12011-11-08 11:55:24 +00002237 if (desc_valid) {
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002238 tmp = mmio_readl(spibar + ICH9_REG_FRAP);
stefanctd0064e12011-11-08 11:55:24 +00002239 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
2240 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
2241 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
2242 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
2243 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
2244
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10002245 /* Handle FREGx and FRAP registers */
2246 for (i = 0; i < num_freg; i++)
2247 ich_spi_rw_restricted |= ich9_handle_frap(tmp, i);
2248 if (ich_spi_rw_restricted)
2249 msg_pinfo("Not all flash regions are freely accessible by flashrom. This is "
2250 "most likely\ndue to an active ME. Please see "
2251 "https://flashrom.org/ME for details.\n");
stefanctd0064e12011-11-08 11:55:24 +00002252 }
mkarcher74d30132010-07-22 18:04:15 +00002253
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10002254 /* Handle PR registers */
2255 for (i = 0; i < num_pr; i++) {
2256 /* if not locked down try to disable PR locks first */
2257 if (!ichspi_lock)
Edward O'Callaghancdd87bb2020-05-18 15:32:08 +10002258 ich9_set_pr(reg_pr0, i, 0, 0);
Edward O'Callaghan4eac7482020-05-26 21:54:52 +10002259 ich_spi_rw_restricted |= ich9_handle_pr(reg_pr0, i);
2260 }
2261
2262 switch (ich_spi_rw_restricted) {
2263 case WRITE_PROT:
2264 msg_pwarn("At least some flash regions are write protected. For write operations,\n"
2265 "you should use a flash layout and include only writable regions. See\n"
2266 "manpage for more details.\n");
2267 break;
2268 case READ_PROT:
2269 case LOCKED:
2270 msg_pwarn("At least some flash regions are read protected. You have to use a flash\n"
2271 "layout and include only accessible regions. For write operations, you'll\n"
2272 "additionally need the --noverify-all switch. See manpage for more details.\n"
2273 );
2274 break;
2275 }
hailfinger01d05912011-03-17 00:10:25 +00002276
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002277 tmp = mmio_readl(spibar + swseq_data.reg_ssfsc);
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10002278 msg_pdbg("0x%zx: 0x%02x (SSFS)\n", swseq_data.reg_ssfsc, tmp & 0xff);
stefancte4d1ef52011-06-11 09:53:16 +00002279 prettyprint_ich9_reg_ssfs(tmp);
stefanct24bda702011-06-12 08:14:10 +00002280 if (tmp & SSFS_FCERR) {
hailfinger01d05912011-03-17 00:10:25 +00002281 msg_pdbg("Clearing SSFS.FCERR\n");
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002282 mmio_writeb(SSFS_FCERR, spibar + swseq_data.reg_ssfsc);
hailfinger01d05912011-03-17 00:10:25 +00002283 }
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10002284 msg_pdbg("0x%zx: 0x%06x (SSFC)\n", swseq_data.reg_ssfsc + 1, tmp >> 8);
stefancte4d1ef52011-06-11 09:53:16 +00002285 prettyprint_ich9_reg_ssfc(tmp);
hailfinger01d05912011-03-17 00:10:25 +00002286
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10002287 msg_pdbg("0x%zx: 0x%04x (PREOP)\n",
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002288 swseq_data.reg_preop, mmio_readw(spibar + swseq_data.reg_preop));
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10002289 msg_pdbg("0x%zx: 0x%04x (OPTYPE)\n",
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002290 swseq_data.reg_optype, mmio_readw(spibar + swseq_data.reg_optype));
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10002291 msg_pdbg("0x%zx: 0x%08x (OPMENU)\n",
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002292 swseq_data.reg_opmenu, mmio_readl(spibar + swseq_data.reg_opmenu));
Edward O'Callaghan9c34b3c2020-05-18 19:02:13 +10002293 msg_pdbg("0x%zx: 0x%08x (OPMENU+4)\n",
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002294 swseq_data.reg_opmenu + 4, mmio_readl(spibar + swseq_data.reg_opmenu + 4));
David Hendricksce6b2fa2011-07-11 22:12:43 -07002295
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002296 if (desc_valid) {
Edward O'Callaghan397256f2020-07-09 09:59:32 +10002297 switch (ich_gen) {
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002298 case CHIPSET_ICH8:
2299 case CHIPSET_100_SERIES_SUNRISE_POINT:
2300 case CHIPSET_C620_SERIES_LEWISBURG:
2301 case CHIPSET_300_SERIES_CANNON_POINT:
Matt DeVillier81bc4d32020-08-12 12:48:06 -05002302 case CHIPSET_400_SERIES_COMET_POINT:
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002303 case CHIPSET_APOLLO_LAKE:
Angel Pons00b29cf2020-07-10 17:04:10 +02002304 case CHIPSET_GEMINI_LAKE:
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002305 case CHIPSET_BAYTRAIL:
2306 break;
2307 default:
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002308 ichspi_bbar = mmio_readl(spibar + ICH9_REG_BBAR);
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002309 msg_pdbg("0x%x: 0x%08x (BBAR)\n", ICH9_REG_BBAR, ichspi_bbar);
Edward O'Callaghand22862f2020-07-16 15:37:26 +10002310 ich_set_bbar(0, ich_gen);
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002311 break;
2312 }
2313
Edward O'Callaghan397256f2020-07-09 09:59:32 +10002314 if (ich_gen == CHIPSET_ICH8) {
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002315 tmp = mmio_readl(spibar + ICH8_REG_VSCC);
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002316 msg_pdbg("0x%x: 0x%08x (VSCC)\n", ICH8_REG_VSCC, tmp);
2317 msg_pdbg("VSCC: ");
2318 prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, true);
2319 } else {
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002320 tmp = mmio_readl(spibar + ICH9_REG_LVSCC);
Edward O'Callaghand757b422020-05-26 21:22:12 +10002321 msg_pdbg("0x%x: 0x%08x (LVSCC)\n", ICH9_REG_LVSCC, tmp);
stefanctd0064e12011-11-08 11:55:24 +00002322 msg_pdbg("LVSCC: ");
Edward O'Callaghand757b422020-05-26 21:22:12 +10002323 prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, true);
David Hendricksce6b2fa2011-07-11 22:12:43 -07002324
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002325 tmp = mmio_readl(spibar + ICH9_REG_UVSCC);
Edward O'Callaghand757b422020-05-26 21:22:12 +10002326 msg_pdbg("0x%x: 0x%08x (UVSCC)\n", ICH9_REG_UVSCC, tmp);
stefanctd0064e12011-11-08 11:55:24 +00002327 msg_pdbg("UVSCC: ");
Edward O'Callaghand757b422020-05-26 21:22:12 +10002328 prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, false);
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002329 }
David Hendricksce6b2fa2011-07-11 22:12:43 -07002330
Edward O'Callaghan397256f2020-07-09 09:59:32 +10002331 switch (ich_gen) {
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002332 case CHIPSET_ICH8:
2333 case CHIPSET_100_SERIES_SUNRISE_POINT:
2334 case CHIPSET_C620_SERIES_LEWISBURG:
2335 case CHIPSET_300_SERIES_CANNON_POINT:
Matt DeVillier81bc4d32020-08-12 12:48:06 -05002336 case CHIPSET_400_SERIES_COMET_POINT:
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002337 case CHIPSET_APOLLO_LAKE:
Angel Pons00b29cf2020-07-10 17:04:10 +02002338 case CHIPSET_GEMINI_LAKE:
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002339 break;
2340 default:
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002341 tmp = mmio_readl(spibar + ICH9_REG_FPB);
Edward O'Callaghand757b422020-05-26 21:22:12 +10002342 msg_pdbg("0x%x: 0x%08x (FPB)\n", ICH9_REG_FPB, tmp);
Edward O'Callaghan469a5d72020-05-27 14:05:46 +10002343 break;
stefanctd0064e12011-11-08 11:55:24 +00002344 }
stefanctd68db982011-07-01 00:39:16 +00002345
Edward O'Callaghan1f11b162020-07-16 17:14:01 +10002346 if (read_ich_descriptors_via_fdo(ich_gen, spibar, &desc) == ICH_RET_OK)
Edward O'Callaghan397256f2020-07-09 09:59:32 +10002347 prettyprint_ich_descriptors(ich_gen, &desc);
Edward O'Callaghand757b422020-05-26 21:22:12 +10002348
stefanct83d99e82011-11-08 10:55:54 +00002349 /* If the descriptor is valid and indicates multiple
2350 * flash devices we need to use hwseq to be able to
2351 * access the second flash device.
2352 */
2353 if (ich_spi_mode == ich_auto && desc.content.NC != 0) {
2354 msg_pinfo("Enabling hardware sequencing due to "
2355 "multiple flash chips detected.\n");
2356 ich_spi_mode = ich_hwseq;
2357 }
David Hendricksce6b2fa2011-07-11 22:12:43 -07002358 }
stefanct83d99e82011-11-08 10:55:54 +00002359
2360 if (ich_spi_mode == ich_auto && ichspi_lock &&
2361 ich_missing_opcodes()) {
2362 msg_pinfo("Enabling hardware sequencing because "
2363 "some important opcode is locked.\n");
2364 ich_spi_mode = ich_hwseq;
2365 }
2366
Edward O'Callaghan0c1a3c92020-08-03 15:01:03 +10002367 if (ich_spi_mode == ich_auto) {
2368 switch(ich_gen) {
2369 case CHIPSET_100_SERIES_SUNRISE_POINT:
2370 case CHIPSET_300_SERIES_CANNON_POINT:
Matt DeVillier81bc4d32020-08-12 12:48:06 -05002371 case CHIPSET_400_SERIES_COMET_POINT:
Edward O'Callaghan0c1a3c92020-08-03 15:01:03 +10002372 case CHIPSET_APOLLO_LAKE:
Angel Pons00b29cf2020-07-10 17:04:10 +02002373 case CHIPSET_GEMINI_LAKE:
Edward O'Callaghan0c1a3c92020-08-03 15:01:03 +10002374 msg_pdbg("Enabling hardware sequencing by default "
2375 "for 100+ series PCH.\n");
2376 ich_spi_mode = ich_hwseq;
2377 break;
2378 default:
2379 break;
2380 }
2381 }
2382
Edward O'Callaghanea6ab742020-08-03 15:13:59 +10002383 switch(ich_gen) {
2384 case CHIPSET_APOLLO_LAKE:
Angel Pons00b29cf2020-07-10 17:04:10 +02002385 case CHIPSET_GEMINI_LAKE:
Edward O'Callaghanea6ab742020-08-03 15:13:59 +10002386 num_fd_regions = APL_GLK_NUM_FD_REGIONS;
2387 break;
2388 case CHIPSET_100_SERIES_SUNRISE_POINT:
Edward O'Callaghan949d9592021-01-06 13:27:53 +11002389 case CHIPSET_300_SERIES_CANNON_POINT:
Matt DeVillier81bc4d32020-08-12 12:48:06 -05002390 case CHIPSET_400_SERIES_COMET_POINT:
Edward O'Callaghanea6ab742020-08-03 15:13:59 +10002391 num_fd_regions = SUNRISEPOINT_NUM_FD_REGIONS;
2392 break;
2393 default:
2394 num_fd_regions = DEFAULT_NUM_FD_REGIONS;
2395 break;
2396 }
2397
stefanct83d99e82011-11-08 10:55:54 +00002398 if (ich_spi_mode == ich_hwseq) {
2399 if (!desc_valid) {
2400 msg_perr("Hardware sequencing was requested "
2401 "but the flash descriptor is not "
2402 "valid. Aborting.\n");
2403 return ERROR_FATAL;
2404 }
Edward O'Callaghane0845d72020-07-05 13:28:03 +10002405
Edward O'Callaghan397256f2020-07-09 09:59:32 +10002406 int tmpi = getFCBA_component_density(ich_gen, &desc, 0);
Edward O'Callaghane0845d72020-07-05 13:28:03 +10002407 if (tmpi < 0) {
2408 msg_perr("Could not determine density of flash component %d.\n", 0);
2409 return ERROR_FATAL;
2410 }
2411 hwseq_data.size_comp0 = tmpi;
2412
Edward O'Callaghan397256f2020-07-09 09:59:32 +10002413 tmpi = getFCBA_component_density(ich_gen, &desc, 1);
Edward O'Callaghane0845d72020-07-05 13:28:03 +10002414 if (tmpi < 0) {
2415 msg_perr("Could not determine density of flash component %d.\n", 1);
2416 return ERROR_FATAL;
2417 }
2418 hwseq_data.size_comp1 = tmpi;
2419
Edward O'Callaghanabd30192019-05-14 15:58:19 +10002420 register_opaque_master(&opaque_master_ich_hwseq);
stefanct83d99e82011-11-08 10:55:54 +00002421 } else {
Nico Huberf1eeda62021-05-11 17:38:14 +02002422 register_spi_master(&spi_master_ich9, NULL);
stefanct83d99e82011-11-08 10:55:54 +00002423 }
stefanct1fc3a732011-09-15 23:52:55 +00002424 break;
mkarcher74d30132010-07-22 18:04:15 +00002425 }
2426
mkarcher74d30132010-07-22 18:04:15 +00002427 return 0;
2428}
2429
Patrick Georgif4f1e2f2017-03-10 17:38:40 +01002430static const struct spi_master spi_master_via = {
mkarcherd264e9e2011-05-11 17:07:07 +00002431 .max_data_read = 16,
2432 .max_data_write = 16,
2433 .command = ich_spi_send_command,
2434 .multicommand = ich_spi_send_multicommand,
2435 .read = default_spi_read,
2436 .write_256 = default_spi_write_256,
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +11002437 .write_aai = default_spi_write_aai,
mkarcherd264e9e2011-05-11 17:07:07 +00002438};
2439
Edward O'Callaghan3300e4e2019-10-03 13:20:09 +10002440int via_init_spi(uint32_t mmio_base)
mkarcher74d30132010-07-22 18:04:15 +00002441{
hailfinger8fdd0a82010-11-24 23:37:22 +00002442 int i;
mkarcher74d30132010-07-22 18:04:15 +00002443
Edward O'Callaghand757b422020-05-26 21:22:12 +10002444 ich_spibar = rphysmap("VIA SPI MMIO registers", mmio_base, 0x70);
Edward O'Callaghanb9370cc2019-10-03 13:08:49 +10002445 if (ich_spibar == ERROR_PTR)
2446 return ERROR_FATAL;
Edward O'Callaghand757b422020-05-26 21:22:12 +10002447 /* Do we really need no write enable? Like the LPC one at D17F0 0x40 */
mkarcher74d30132010-07-22 18:04:15 +00002448
mkarcher74d30132010-07-22 18:04:15 +00002449 /* Not sure if it speaks all these bus protocols. */
Edward O'Callaghan26bf5c42019-08-02 23:28:03 +10002450 internal_buses_supported &= BUS_LPC | BUS_FWH;
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11002451 ich_generation = CHIPSET_ICH7;
Nico Huberf1eeda62021-05-11 17:38:14 +02002452 register_spi_master(&spi_master_via, NULL);
hailfinger8fdd0a82010-11-24 23:37:22 +00002453
2454 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
2455 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
2456 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
2457 for (i = 0; i < 2; i++) {
2458 int offs;
2459 offs = 8 + (i * 8);
2460 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
2461 mmio_readl(ich_spibar + offs), i);
2462 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
2463 mmio_readl(ich_spibar + offs + 4), i);
2464 }
2465 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
2466 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
2467 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
2468 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
2469 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
2470 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
2471 for (i = 0; i < 3; i++) {
2472 int offs;
2473 offs = 0x60 + (i * 4);
2474 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
2475 mmio_readl(ich_spibar + offs), i);
2476 }
2477 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
2478 mmio_readw(ich_spibar + 0x6c));
2479 if (mmio_readw(ich_spibar) & (1 << 15)) {
Edward O'Callaghan59919f92019-09-09 00:15:08 +10002480 msg_pwarn("Warning: SPI Configuration Lockdown activated.\n");
hailfinger8fdd0a82010-11-24 23:37:22 +00002481 ichspi_lock = 1;
2482 }
2483
Edward O'Callaghan0a217dd2020-11-28 18:00:01 +11002484 ich_set_bbar(0, ich_generation);
2485 ich_init_opcodes(ich_generation);
mkarcher74d30132010-07-22 18:04:15 +00002486
2487 return 0;
2488}
2489
hailfinger324a9cc2010-05-26 01:45:41 +00002490#endif