xref: /aosp_15_r20/external/flashrom/ichspi.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2008 Stefan Wildemann <[email protected]>
5  * Copyright (C) 2008 Claus Gindhart <[email protected]>
6  * Copyright (C) 2008 Dominik Geyer <[email protected]>
7  * Copyright (C) 2008 coresystems GmbH <[email protected]>
8  * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
9  * Copyright (C) 2011 Stefan Tauner
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  */
21 
22 #include <string.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include "flash.h"
26 #include "programmer.h"
27 #include "hwaccess_physmap.h"
28 #include "spi.h"
29 #include "ich_descriptors.h"
30 #include "action_descriptor.h"
31 
32 /* Apollo Lake */
33 #define APL_REG_FREG12		0xe0	/* 32 Bytes Flash Region 12 */
34 
35 /* Sunrise Point */
36 
37 /* Added HSFS Status bits */
38 #define HSFS_WRSDIS_OFF		11	/* 11: Flash Configuration Lock-Down */
39 #define HSFS_WRSDIS		(0x1 << HSFS_WRSDIS_OFF)
40 #define HSFS_PRR34_LOCKDN_OFF	12	/* 12: PRR3 PRR4 Lock-Down */
41 #define HSFS_PRR34_LOCKDN	(0x1 << HSFS_PRR34_LOCKDN_OFF)
42 /* HSFS_BERASE vanished */
43 
44 /*
45  * HSFC and HSFS 16-bit registers are combined into the 32-bit
46  * BIOS_HSFSTS_CTL register in the Sunrise Point datasheet,
47  * however we still treat them separately in order to reuse code.
48  */
49 
50 /*
51  * HSFC Control bits
52  *
53  * FCYCLE is a 2 bit field (HSFC bits 1-2) on ICH9 and 4 bit field
54  * (HSFC bits 1-4) on PCH100.
55  *
56  * ICH9 and PCH100 use the same FCYCLE values for flash operations,
57  * however FCYCLE values above 3 are only supported by PCH100.
58  *
59  * 0: SPI Read
60  * 2: SPI Write
61  * 3: SPI Erase 4K
62  * 4: SPI Erase 64K
63  * 6: SPI RDID
64  * 7: SPI Write Status
65  * 8: SPI Read Status
66  */
67 #define HSFC_FGO_OFF		0
68 #define HSFC_FGO		(0x1 << HSFC_FGO_OFF)
69 #define HSFC_FCYCLE_MASK(n)	((n) << HSFC_FCYCLE_OFF)
70 #define HSFC_FCYCLE_OFF		1
71 #define HSFC_CYCLE_READ		HSFC_FCYCLE_MASK(0x0)
72 #define HSFC_CYCLE_WRITE	HSFC_FCYCLE_MASK(0x2)
73 #define HSFC_CYCLE_BLOCK_ERASE	HSFC_FCYCLE_MASK(0x3)
74 #define HSFC_CYCLE_RDID		HSFC_FCYCLE_MASK(0x6)
75 #define HSFC_CYCLE_WR_STATUS	HSFC_FCYCLE_MASK(0x7)
76 #define HSFC_CYCLE_RD_STATUS	HSFC_FCYCLE_MASK(0x8)
77 
78 /* PCH100 controller register definition */
79 #define PCH100_HSFC_FCYCLE_OFF		1
80 #define PCH100_HSFC_FCYCLE_BIT_WIDTH	0xf
81 #define PCH100_HSFC_FCYCLE	HSFC_FCYCLE_MASK(PCH100_HSFC_FCYCLE_BIT_WIDTH)
82 /* New HSFC Control bit */
83 #define PCH100_HSFC_WET_OFF	(21 - 16)	/* 5: Write Enable Type */
84 #define PCH100_HSFC_WET		(0x1 << PCH100_HSFC_WET_OFF)
85 
86 #define PCH100_FADDR_FLA	0x07ffffff
87 
88 #define PCH100_REG_DLOCK	0x0c	/* 32 Bits Discrete Lock Bits */
89 #define DLOCK_BMWAG_LOCKDN_OFF	0
90 #define DLOCK_BMWAG_LOCKDN	(0x1 << DLOCK_BMWAG_LOCKDN_OFF)
91 #define DLOCK_BMRAG_LOCKDN_OFF	1
92 #define DLOCK_BMRAG_LOCKDN	(0x1 << DLOCK_BMRAG_LOCKDN_OFF)
93 #define DLOCK_SBMWAG_LOCKDN_OFF	2
94 #define DLOCK_SBMWAG_LOCKDN	(0x1 << DLOCK_SBMWAG_LOCKDN_OFF)
95 #define DLOCK_SBMRAG_LOCKDN_OFF	3
96 #define DLOCK_SBMRAG_LOCKDN	(0x1 << DLOCK_SBMRAG_LOCKDN_OFF)
97 #define DLOCK_PR0_LOCKDN_OFF	8
98 #define DLOCK_PR0_LOCKDN	(0x1 << DLOCK_PR0_LOCKDN_OFF)
99 #define DLOCK_PR1_LOCKDN_OFF	9
100 #define DLOCK_PR1_LOCKDN	(0x1 << DLOCK_PR1_LOCKDN_OFF)
101 #define DLOCK_PR2_LOCKDN_OFF	10
102 #define DLOCK_PR2_LOCKDN	(0x1 << DLOCK_PR2_LOCKDN_OFF)
103 #define DLOCK_PR3_LOCKDN_OFF	11
104 #define DLOCK_PR3_LOCKDN	(0x1 << DLOCK_PR3_LOCKDN_OFF)
105 #define DLOCK_PR4_LOCKDN_OFF	12
106 #define DLOCK_PR4_LOCKDN	(0x1 << DLOCK_PR4_LOCKDN_OFF)
107 #define DLOCK_SSEQ_LOCKDN_OFF	16
108 #define DLOCK_SSEQ_LOCKDN	(0x1 << DLOCK_SSEQ_LOCKDN_OFF)
109 
110 #define PCH100_REG_FPR0		0x84	/* 32 Bits Protected Range 0 */
111 #define PCH100_REG_GPR0		0x98	/* 32 Bits Global Protected Range 0 */
112 
113 #define PCH100_REG_SSFSC	0xA0	/* 32 Bits Status (8) + Control (24) */
114 #define PCH100_REG_PREOP	0xA4	/* 16 Bits */
115 #define PCH100_REG_OPTYPE	0xA6	/* 16 Bits */
116 #define PCH100_REG_OPMENU	0xA8	/* 64 Bits */
117 
118 /* ICH9 controller register definition */
119 #define ICH9_REG_HSFS		0x04	/* 16 Bits Hardware Sequencing Flash Status */
120 #define HSFS_FDONE_OFF		0	/* 0: Flash Cycle Done */
121 #define HSFS_FDONE		(0x1 << HSFS_FDONE_OFF)
122 #define HSFS_FCERR_OFF		1	/* 1: Flash Cycle Error */
123 #define HSFS_FCERR		(0x1 << HSFS_FCERR_OFF)
124 #define HSFS_AEL_OFF		2	/* 2: Access Error Log */
125 #define HSFS_AEL		(0x1 << HSFS_AEL_OFF)
126 #define HSFS_BERASE_OFF		3	/* 3-4: Block/Sector Erase Size */
127 #define HSFS_BERASE		(0x3 << HSFS_BERASE_OFF)
128 #define HSFS_SCIP_OFF		5	/* 5: SPI Cycle In Progress */
129 #define HSFS_SCIP		(0x1 << HSFS_SCIP_OFF)
130 					/* 6-12: reserved */
131 #define HSFS_FDOPSS_OFF		13	/* 13: Flash Descriptor Override Pin-Strap Status */
132 #define HSFS_FDOPSS		(0x1 << HSFS_FDOPSS_OFF)
133 #define HSFS_FDV_OFF		14	/* 14: Flash Descriptor Valid */
134 #define HSFS_FDV		(0x1 << HSFS_FDV_OFF)
135 #define HSFS_FLOCKDN_OFF	15	/* 15: Flash Configuration Lock-Down */
136 #define HSFS_FLOCKDN		(0x1 << HSFS_FLOCKDN_OFF)
137 
138 #define ICH9_REG_HSFC		0x06	/* 16 Bits Hardware Sequencing Flash Control */
139 
140 					/* 0: Flash Cycle Go */
141 					/* 1-2: FLASH Cycle */
142 #define ICH9_HSFC_FCYCLE_OFF		1
143 #define ICH9_HSFC_FCYCLE_BIT_WIDTH	3
144 #define ICH9_HSFC_FCYCLE	HSFC_FCYCLE_MASK(ICH9_HSFC_FCYCLE_BIT_WIDTH)
145 					/* 3-7: reserved */
146 #define HSFC_FDBC_OFF		8	/* 8-13: Flash Data Byte Count */
147 #define HSFC_FDBC		(0x3f << HSFC_FDBC_OFF)
148 #define HSFC_FDBC_VAL(n)	(((n) << HSFC_FDBC_OFF) & HSFC_FDBC)
149 					/* 14: reserved */
150 #define HSFC_SME_OFF		15	/* 15: SPI SMI# Enable */
151 #define HSFC_SME		(0x1 << HSFC_SME_OFF)
152 
153 #define ICH9_REG_FADDR		0x08	/* 32 Bits */
154 #define ICH9_FADDR_FLA		0x01ffffff
155 #define ICH9_REG_FDATA0		0x10	/* 64 Bytes */
156 
157 #define ICH_REG_BIOS_BM_RAP	0x118	/* 16 Bits BIOS Master Read Access Permissions */
158 #define ICH_REG_BIOS_BM_WAP	0x11c	/* 16 Bits BIOS Master Write Access Permissions */
159 
160 #define ICH9_REG_FRAP		0x50	/* 32 Bytes Flash Region Access Permissions */
161 #define ICH9_REG_FREG0		0x54	/* 32 Bytes Flash Region 0 */
162 
163 #define ICH9_REG_PR0		0x74	/* 32 Bytes Protected Range 0 */
164 #define PR_WP_OFF		31	/* 31: write protection enable */
165 #define PR_RP_OFF		15	/* 15: read protection enable */
166 
167 #define ICH9_REG_SSFS		0x90	/* 08 Bits */
168 #define SSFS_SCIP_OFF		0	/* SPI Cycle In Progress */
169 #define SSFS_SCIP		(0x1 << SSFS_SCIP_OFF)
170 #define SSFS_FDONE_OFF		2	/* Cycle Done Status */
171 #define SSFS_FDONE		(0x1 << SSFS_FDONE_OFF)
172 #define SSFS_FCERR_OFF		3	/* Flash Cycle Error */
173 #define SSFS_FCERR		(0x1 << SSFS_FCERR_OFF)
174 #define SSFS_AEL_OFF		4	/* Access Error Log */
175 #define SSFS_AEL		(0x1 << SSFS_AEL_OFF)
176 /* The following bits are reserved in SSFS: 1,5-7. */
177 #define SSFS_RESERVED_MASK	0x000000e2
178 
179 #define ICH9_REG_SSFC		0x91	/* 24 Bits */
180 /* We combine SSFS and SSFC to one 32-bit word,
181  * therefore SSFC bits are off by 8. */
182 						/* 0: reserved */
183 #define SSFC_SCGO_OFF		(1 + 8)		/* 1: SPI Cycle Go */
184 #define SSFC_SCGO		(0x1 << SSFC_SCGO_OFF)
185 #define SSFC_ACS_OFF		(2 + 8)		/* 2: Atomic Cycle Sequence */
186 #define SSFC_ACS		(0x1 << SSFC_ACS_OFF)
187 #define SSFC_SPOP_OFF		(3 + 8)		/* 3: Sequence Prefix Opcode Pointer */
188 #define SSFC_SPOP		(0x1 << SSFC_SPOP_OFF)
189 #define SSFC_COP_OFF		(4 + 8)		/* 4-6: Cycle Opcode Pointer */
190 #define SSFC_COP		(0x7 << SSFC_COP_OFF)
191 						/* 7: reserved */
192 #define SSFC_DBC_OFF		(8 + 8)		/* 8-13: Data Byte Count */
193 #define SSFC_DBC		(0x3f << SSFC_DBC_OFF)
194 #define SSFC_DS_OFF		(14 + 8)	/* 14: Data Cycle */
195 #define SSFC_DS			(0x1 << SSFC_DS_OFF)
196 #define SSFC_SME_OFF		(15 + 8)	/* 15: SPI SMI# Enable */
197 #define SSFC_SME		(0x1 << SSFC_SME_OFF)
198 #define SSFC_SCF_OFF		(16 + 8)	/* 16-18: SPI Cycle Frequency */
199 #define SSFC_SCF		(0x7 << SSFC_SCF_OFF)
200 #define SSFC_SCF_20MHZ		0x00000000
201 #define SSFC_SCF_33MHZ		0x01000000
202 						/* 19-23: reserved */
203 #define SSFC_RESERVED_MASK	0xf8008100
204 
205 #define ICH9_REG_PREOP		0x94	/* 16 Bits */
206 #define ICH9_REG_OPTYPE		0x96	/* 16 Bits */
207 #define ICH9_REG_OPMENU		0x98	/* 64 Bits */
208 
209 #define ICH9_REG_BBAR		0xA0	/* 32 Bits BIOS Base Address Configuration */
210 #define BBAR_MASK	0x00ffff00		/* 8-23: Bottom of System Flash */
211 
212 #define ICH8_REG_VSCC		0xC1	/* 32 Bits Vendor Specific Component Capabilities */
213 #define ICH9_REG_LVSCC		0xC4	/* 32 Bits Host Lower Vendor Specific Component Capabilities */
214 #define ICH9_REG_UVSCC		0xC8	/* 32 Bits Host Upper Vendor Specific Component Capabilities */
215 /* The individual fields of the VSCC registers are defined in the file
216  * ich_descriptors.h. The reason is that the same layout is also used in the
217  * flash descriptor to define the properties of the different flash chips
218  * supported. The BIOS (or the ME?) is responsible to populate the ICH registers
219  * with the information from the descriptor on startup depending on the actual
220  * chip(s) detected. */
221 
222 #define ICH9_REG_FPB		0xD0	/* 32 Bits Flash Partition Boundary */
223 #define FPB_FPBA_OFF		0	/* 0-12: Block/Sector Erase Size */
224 #define FPB_FPBA			(0x1FFF << FPB_FPBA_OFF)
225 
226 // ICH9R SPI commands
227 #define SPI_OPCODE_TYPE_READ_NO_ADDRESS		0
228 #define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS	1
229 #define SPI_OPCODE_TYPE_READ_WITH_ADDRESS	2
230 #define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS	3
231 
232 // ICH7 registers
233 #define ICH7_REG_SPIS		0x00	/* 16 Bits */
234 #define SPIS_SCIP		0x0001
235 #define SPIS_GRANT		0x0002
236 #define SPIS_CDS		0x0004
237 #define SPIS_FCERR		0x0008
238 #define SPIS_RESERVED_MASK	0x7ff0
239 
240 /* VIA SPI is compatible with ICH7, but maxdata
241    to transfer is 16 bytes.
242 
243    DATA byte count on ICH7 is 8:13, on VIA 8:11
244 
245    bit 12 is port select CS0 CS1
246    bit 13 is FAST READ enable
247    bit 7  is used with fast read and one shot controls CS de-assert?
248 */
249 
250 #define ICH7_REG_SPIC		0x02	/* 16 Bits */
251 #define SPIC_SCGO		0x0002
252 #define SPIC_ACS		0x0004
253 #define SPIC_SPOP		0x0008
254 #define SPIC_DS			0x4000
255 
256 #define ICH7_REG_SPIA		0x04	/* 32 Bits */
257 #define ICH7_REG_SPID0		0x08	/* 64 Bytes */
258 #define ICH7_REG_PREOP		0x54	/* 16 Bits */
259 #define ICH7_REG_OPTYPE		0x56	/* 16 Bits */
260 #define ICH7_REG_OPMENU		0x58	/* 64 Bits */
261 
262 enum ich_access_protection {
263 	NO_PROT		= 0,
264 	READ_PROT	= 1,
265 	WRITE_PROT	= 2,
266 	LOCKED		= 3,
267 };
268 
269 /* ICH SPI configuration lock-down. May be set during chipset enabling. */
270 static bool ichspi_lock = false;
271 
272 enum ich_chipset ich_generation = CHIPSET_ICH_UNKNOWN;
273 static uint32_t ichspi_bbar;
274 
275 static void *ich_spibar = NULL;
276 
277 typedef struct _OPCODE {
278 	uint8_t opcode;		//This commands spi opcode
279 	uint8_t spi_type;	//This commands spi type
280 	uint8_t atomic;		//Use preop: (0: none, 1: preop0, 2: preop1
281 } OPCODE;
282 
283 /* Suggested opcode definition:
284  * Preop 1: Write Enable
285  * Preop 2: Write Status register enable
286  *
287  * OP 0: Write address
288  * OP 1: Read Address
289  * OP 2: ERASE block
290  * OP 3: Read Status register
291  * OP 4: Read ID
292  * OP 5: Write Status register
293  * OP 6: chip private (read JEDEC id)
294  * OP 7: Chip erase
295  */
296 typedef struct _OPCODES {
297 	uint8_t preop[2];
298 	OPCODE opcode[8];
299 } OPCODES;
300 
301 static OPCODES *curopcodes = NULL;
302 
303 /* HW access functions */
REGREAD32(int X)304 static uint32_t REGREAD32(int X)
305 {
306 	return mmio_readl(ich_spibar + X);
307 }
308 
REGREAD16(int X)309 static uint16_t REGREAD16(int X)
310 {
311 	return mmio_readw(ich_spibar + X);
312 }
313 
REGREAD8(int X)314 static uint16_t REGREAD8(int X)
315 {
316 	return mmio_readb(ich_spibar + X);
317 }
318 
319 #define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
320 #define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
321 #define REGWRITE8(off, val)  mmio_writeb(val, ich_spibar+(off))
322 
323 /* Common SPI functions */
324 
find_opcode(OPCODES * op,uint8_t opcode)325 static int find_opcode(OPCODES *op, uint8_t opcode)
326 {
327 	int a;
328 
329 	if (op == NULL) {
330 		msg_perr("\n%s: null OPCODES pointer!\n", __func__);
331 		return -1;
332 	}
333 
334 	for (a = 0; a < 8; a++) {
335 		if (op->opcode[a].opcode == opcode)
336 			return a;
337 	}
338 
339 	return -1;
340 }
341 
find_preop(OPCODES * op,uint8_t preop)342 static int find_preop(OPCODES *op, uint8_t preop)
343 {
344 	int a;
345 
346 	if (op == NULL) {
347 		msg_perr("\n%s: null OPCODES pointer!\n", __func__);
348 		return -1;
349 	}
350 
351 	for (a = 0; a < 2; a++) {
352 		if (op->preop[a] == preop)
353 			return a;
354 	}
355 
356 	return -1;
357 }
358 
359 /* for pairing opcodes with their required preop */
360 struct preop_opcode_pair {
361 	uint8_t preop;
362 	uint8_t opcode;
363 };
364 
365 /* List of opcodes which need preopcodes and matching preopcodes. Unused. */
366 const struct preop_opcode_pair pops[] = {
367 	{JEDEC_WREN, JEDEC_BYTE_PROGRAM},
368 	{JEDEC_WREN, JEDEC_SE}, /* sector erase */
369 	{JEDEC_WREN, JEDEC_BE_52}, /* block erase */
370 	{JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
371 	{JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
372 	{JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
373 	 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
374 	{JEDEC_WREN, JEDEC_WRSR},
375 	{JEDEC_EWSR, JEDEC_WRSR},
376 	{0,}
377 };
378 
379 /* Reasonable default configuration. Needs ad-hoc modifications if we
380  * encounter unlisted opcodes. Fun.
381  */
382 static OPCODES O_ST_M25P = {
383 	{
384 	 JEDEC_WREN,
385 	 JEDEC_EWSR,
386 	},
387 	{
388 	 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0},	// Write Byte
389 	 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0},	// Read Data
390 	 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0},	// Erase Sector
391 	 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0},	// Read Device Status Reg
392 	 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0},	// Read Electronic Manufacturer Signature
393 	 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0},	// Write Status Register
394 	 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0},	// Read JDEC ID
395 	 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0},	// Bulk erase
396 	}
397 };
398 
399 /* List of opcodes with their corresponding spi_type
400  * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
401  * is needed which is currently not in the chipset OPCODE table
402  */
403 static const OPCODE POSSIBLE_OPCODES[] = {
404 	 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0},	// Write Byte
405 	 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0},	// Read Data
406 	 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0},	// Erase Sector
407 	 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0},	// Read Device Status Reg
408 	 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0},	// Read Electronic Manufacturer Signature
409 	 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0},	// Write Status Register
410 	 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0},	// Read JDEC ID
411 	 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0},	// Bulk erase
412 	 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0},	// Sector erase
413 	 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0},	// Block erase
414 	 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0},	// Auto Address Increment
415 };
416 
417 static OPCODES O_EXISTING = {};
418 
419 /* pretty printing functions */
prettyprint_opcodes(OPCODES * ops)420 static void prettyprint_opcodes(OPCODES *ops)
421 {
422 	OPCODE oc;
423 	const char *t;
424 	const char *a;
425 	uint8_t i;
426 	static const char *const spi_type[4] = {
427 		"read  w/o addr",
428 		"write w/o addr",
429 		"read  w/  addr",
430 		"write w/  addr"
431 	};
432 	static const char *const atomic_type[3] = {
433 		"none",
434 		" 0  ",
435 		" 1  "
436 	};
437 
438 	if (ops == NULL)
439 		return;
440 
441 	msg_pdbg2("        OP        Type      Pre-OP\n");
442 	for (i = 0; i < 8; i++) {
443 		oc = ops->opcode[i];
444 		t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type];
445 		a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic];
446 		msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a);
447 	}
448 	msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0],
449 		 ops->preop[1]);
450 }
451 
452 #define pprint_reg16(reg, bit, val, sep) msg_pdbg("%s=%"PRId16"" sep, #bit, (val & reg##_##bit) >> reg##_##bit##_OFF)
453 #define pprint_reg32(reg, bit, val, sep) msg_pdbg("%s=%"PRId32"" sep, #bit, (val & reg##_##bit) >> reg##_##bit##_OFF)
454 
prettyprint_ich9_reg_hsfs(uint16_t reg_val,enum ich_chipset ich_gen)455 static void prettyprint_ich9_reg_hsfs(uint16_t reg_val, enum ich_chipset ich_gen)
456 {
457 	msg_pdbg("HSFS: ");
458 	pprint_reg16(HSFS, FDONE, reg_val, ", ");
459 	pprint_reg16(HSFS, FCERR, reg_val, ", ");
460 	pprint_reg16(HSFS, AEL, reg_val, ", ");
461 	switch (ich_gen) {
462 	case CHIPSET_100_SERIES_SUNRISE_POINT:
463 	case CHIPSET_C620_SERIES_LEWISBURG:
464 	case CHIPSET_C740_SERIES_EMMITSBURG:
465 	case CHIPSET_300_SERIES_CANNON_POINT:
466 	case CHIPSET_400_SERIES_COMET_POINT:
467 	case CHIPSET_500_SERIES_TIGER_POINT:
468 	case CHIPSET_ELKHART_LAKE:
469 		break;
470 	default:
471 		pprint_reg16(HSFS, BERASE, reg_val, ", ");
472 		break;
473 	}
474 	pprint_reg16(HSFS, SCIP, reg_val, ", ");
475 	switch (ich_gen) {
476 	case CHIPSET_100_SERIES_SUNRISE_POINT:
477 	case CHIPSET_C620_SERIES_LEWISBURG:
478 	case CHIPSET_C740_SERIES_EMMITSBURG:
479 	case CHIPSET_300_SERIES_CANNON_POINT:
480 	case CHIPSET_400_SERIES_COMET_POINT:
481 	case CHIPSET_500_SERIES_TIGER_POINT:
482 	case CHIPSET_ELKHART_LAKE:
483 		pprint_reg16(HSFS, PRR34_LOCKDN, reg_val, ", ");
484 		pprint_reg16(HSFS, WRSDIS, reg_val, ", ");
485 		break;
486 	default:
487 		break;
488 	}
489 	pprint_reg16(HSFS, FDOPSS, reg_val, ", ");
490 	pprint_reg16(HSFS, FDV, reg_val, ", ");
491 	pprint_reg16(HSFS, FLOCKDN, reg_val, "\n");
492 }
493 
prettyprint_ich9_reg_hsfc(uint16_t reg_val,enum ich_chipset ich_gen)494 static void prettyprint_ich9_reg_hsfc(uint16_t reg_val, enum ich_chipset ich_gen)
495 {
496 	msg_pdbg("HSFC: ");
497 	pprint_reg16(HSFC, FGO, reg_val, ", ");
498 	switch (ich_gen) {
499 	case CHIPSET_100_SERIES_SUNRISE_POINT:
500 	case CHIPSET_C620_SERIES_LEWISBURG:
501 	case CHIPSET_C740_SERIES_EMMITSBURG:
502 	case CHIPSET_300_SERIES_CANNON_POINT:
503 	case CHIPSET_400_SERIES_COMET_POINT:
504 	case CHIPSET_500_SERIES_TIGER_POINT:
505 	case CHIPSET_ELKHART_LAKE:
506 		pprint_reg16(PCH100_HSFC, FCYCLE, reg_val, ", ");
507 		pprint_reg16(PCH100_HSFC, WET, reg_val, ", ");
508 		break;
509 	default:
510 		pprint_reg16(ICH9_HSFC, FCYCLE, reg_val, ", ");
511 		break;
512 	}
513 	pprint_reg16(HSFC, FDBC, reg_val, ", ");
514 	pprint_reg16(HSFC, SME, reg_val, "\n");
515 }
516 
prettyprint_ich9_reg_ssfs(uint32_t reg_val)517 static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
518 {
519 	msg_pdbg("SSFS: ");
520 	pprint_reg32(SSFS, SCIP, reg_val, ", ");
521 	pprint_reg32(SSFS, FDONE, reg_val, ", ");
522 	pprint_reg32(SSFS, FCERR, reg_val, ", ");
523 	pprint_reg32(SSFS, AEL, reg_val, "\n");
524 }
525 
prettyprint_ich9_reg_ssfc(uint32_t reg_val)526 static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
527 {
528 	msg_pdbg("SSFC: ");
529 	pprint_reg32(SSFC, SCGO, reg_val, ", ");
530 	pprint_reg32(SSFC, ACS, reg_val, ", ");
531 	pprint_reg32(SSFC, SPOP, reg_val, ", ");
532 	pprint_reg32(SSFC, COP, reg_val, ", ");
533 	pprint_reg32(SSFC, DBC, reg_val, ", ");
534 	pprint_reg32(SSFC, SME, reg_val, ", ");
535 	pprint_reg32(SSFC, SCF, reg_val, "\n");
536 }
537 
prettyprint_pch100_reg_dlock(const uint32_t reg_val)538 static void prettyprint_pch100_reg_dlock(const uint32_t reg_val)
539 {
540 	msg_pdbg("DLOCK: ");
541 	pprint_reg32(DLOCK, BMWAG_LOCKDN, reg_val, ", ");
542 	pprint_reg32(DLOCK, BMRAG_LOCKDN, reg_val, ", ");
543 	pprint_reg32(DLOCK, SBMWAG_LOCKDN, reg_val, ", ");
544 	pprint_reg32(DLOCK, SBMRAG_LOCKDN, reg_val, ",\n       ");
545 	pprint_reg32(DLOCK, PR0_LOCKDN, reg_val, ", ");
546 	pprint_reg32(DLOCK, PR1_LOCKDN, reg_val, ", ");
547 	pprint_reg32(DLOCK, PR2_LOCKDN, reg_val, ", ");
548 	pprint_reg32(DLOCK, PR3_LOCKDN, reg_val, ", ");
549 	pprint_reg32(DLOCK, PR4_LOCKDN, reg_val, ",\n       ");
550 	pprint_reg32(DLOCK, SSEQ_LOCKDN, reg_val, "\n");
551 }
552 
553 static struct swseq_data {
554 	size_t reg_ssfsc;
555 	size_t reg_preop;
556 	size_t reg_optype;
557 	size_t reg_opmenu;
558 } swseq_data;
559 
lookup_spi_type(uint8_t opcode)560 static uint8_t lookup_spi_type(uint8_t opcode)
561 {
562 	unsigned int a;
563 
564 	for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
565 		if (POSSIBLE_OPCODES[a].opcode == opcode)
566 			return POSSIBLE_OPCODES[a].spi_type;
567 	}
568 
569 	return 0xFF;
570 }
571 
program_opcodes(OPCODES * op,int enable_undo,enum ich_chipset ich_gen)572 static int program_opcodes(OPCODES *op, int enable_undo, enum ich_chipset ich_gen)
573 {
574 	uint8_t a;
575 	uint16_t preop, optype;
576 	uint32_t opmenu[2];
577 
578 	/* Program Prefix Opcodes */
579 	/* 0:7 Prefix Opcode 1 */
580 	preop = (op->preop[0]);
581 	/* 8:16 Prefix Opcode 2 */
582 	preop |= ((uint16_t) op->preop[1]) << 8;
583 
584 	/* Program Opcode Types 0 - 7 */
585 	optype = 0;
586 	for (a = 0; a < 8; a++) {
587 		optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
588 	}
589 
590 	/* Program Allowable Opcodes 0 - 3 */
591 	opmenu[0] = 0;
592 	for (a = 0; a < 4; a++) {
593 		opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
594 	}
595 
596 	/* Program Allowable Opcodes 4 - 7 */
597 	opmenu[1] = 0;
598 	for (a = 4; a < 8; a++) {
599 		opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
600 	}
601 
602 	msg_pdbg2("\n%s: preop=%04x optype=%04x opmenu=%08"PRIx32"%08"PRIx32"\n", __func__, preop, optype, opmenu[0], opmenu[1]);
603 	switch (ich_gen) {
604 	case CHIPSET_ICH7:
605 	case CHIPSET_TUNNEL_CREEK:
606 	case CHIPSET_CENTERTON:
607 		/* Register undo only for enable_undo=1, i.e. first call. */
608 		if (enable_undo) {
609 			rmmio_valw(ich_spibar + ICH7_REG_PREOP);
610 			rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
611 			rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
612 			rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
613 		}
614 		mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
615 		mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
616 		mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
617 		mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
618 		break;
619 	case CHIPSET_ICH8:
620 	default:		/* Future version might behave the same */
621 		/* Register undo only for enable_undo=1, i.e. first call. */
622 		if (enable_undo) {
623 			rmmio_valw(ich_spibar + swseq_data.reg_preop);
624 			rmmio_valw(ich_spibar + swseq_data.reg_optype);
625 			rmmio_vall(ich_spibar + swseq_data.reg_opmenu);
626 			rmmio_vall(ich_spibar + swseq_data.reg_opmenu + 4);
627 		}
628 		mmio_writew(preop, ich_spibar + swseq_data.reg_preop);
629 		mmio_writew(optype, ich_spibar + swseq_data.reg_optype);
630 		mmio_writel(opmenu[0], ich_spibar + swseq_data.reg_opmenu);
631 		mmio_writel(opmenu[1], ich_spibar + swseq_data.reg_opmenu + 4);
632 		break;
633 	}
634 
635 	return 0;
636 }
637 
reprogram_opcode_on_the_fly(uint8_t opcode,unsigned int writecnt,unsigned int readcnt)638 static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
639 {
640 	uint8_t spi_type;
641 
642 	spi_type = lookup_spi_type(opcode);
643 	if (spi_type > 3) {
644 		/* Try to guess spi type from read/write sizes.
645 		 * The following valid writecnt/readcnt combinations exist:
646 		 * writecnt  = 4, readcnt >= 0
647 		 * writecnt  = 1, readcnt >= 0
648 		 * writecnt >= 4, readcnt  = 0
649 		 * writecnt >= 1, readcnt  = 0
650 		 * writecnt >= 1 is guaranteed for all commands.
651 		 */
652 		if (readcnt == 0)
653 			/* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
654 			 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
655 			 * bytes are actual the address, they go to the bus anyhow
656 			 */
657 			spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
658 		else if (writecnt == 1) // and readcnt is > 0
659 			spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
660 		else if (writecnt == 4) // and readcnt is > 0
661 			spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
662 		else // we have an invalid case
663 			return SPI_INVALID_LENGTH;
664 	}
665 	int oppos = 4;	// use the original position of JEDEC_REMS
666 	curopcodes->opcode[oppos].opcode = opcode;
667 	curopcodes->opcode[oppos].spi_type = spi_type;
668 	program_opcodes(curopcodes, 0, ich_generation);
669 	oppos = find_opcode(curopcodes, opcode);
670 	msg_pdbg2("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
671 	return oppos;
672 }
673 
674 /*
675  * Returns -1 if at least one mandatory opcode is inaccessible, 0 otherwise.
676  * FIXME: this should also check for
677  *   - at least one probing opcode (RDID (incl. AT25F variants?), REMS, RES?)
678  *   - at least one erasing opcode (lots.)
679  *   - at least one program opcode (BYTE_PROGRAM, AAI_WORD_PROGRAM, ...?)
680  *   - necessary preops? (EWSR, WREN, ...?)
681  */
ich_missing_opcodes(void)682 static int ich_missing_opcodes(void)
683 {
684 	uint8_t ops[] = {
685 		JEDEC_READ,
686 		JEDEC_RDSR,
687 		0
688 	};
689 	int i = 0;
690 	while (ops[i] != 0) {
691 		msg_pspew("checking for opcode 0x%02x\n", ops[i]);
692 		if (find_opcode(curopcodes, ops[i]) == -1)
693 			return -1;
694 		i++;
695 	}
696 	return 0;
697 }
698 
699 /*
700  * Try to set BBAR (BIOS Base Address Register), but read back the value in case
701  * it didn't stick.
702  */
ich_set_bbar(uint32_t min_addr,enum ich_chipset ich_gen)703 static void ich_set_bbar(uint32_t min_addr, enum ich_chipset ich_gen)
704 {
705 	int bbar_off;
706 	switch (ich_gen) {
707 	case CHIPSET_ICH7:
708 	case CHIPSET_TUNNEL_CREEK:
709 	case CHIPSET_CENTERTON:
710 		bbar_off = 0x50;
711 		break;
712 	case CHIPSET_ICH8:
713 	case CHIPSET_BAYTRAIL:
714 		msg_pdbg("BBAR offset is unknown!\n");
715 		return;
716 	case CHIPSET_ICH9:
717 	default:		/* Future version might behave the same */
718 		bbar_off = ICH9_REG_BBAR;
719 		break;
720 	}
721 
722 	ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
723 	if (ichspi_bbar) {
724 		msg_pdbg("Reserved bits in BBAR not zero: 0x%08"PRIx32"\n",
725 			 ichspi_bbar);
726 	}
727 	min_addr &= BBAR_MASK;
728 	ichspi_bbar |= min_addr;
729 	rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
730 	ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
731 
732 	/* We don't have any option except complaining. And if the write
733 	 * failed, the restore will fail as well, so no problem there.
734 	 */
735 	if (ichspi_bbar != min_addr)
736 		msg_perr("Setting BBAR to 0x%08"PRIx32" failed! New value: 0x%08"PRIx32".\n",
737 			 min_addr, ichspi_bbar);
738 }
739 
740 /* Create a struct OPCODES based on what we find in the locked down chipset. */
generate_opcodes(OPCODES * op,enum ich_chipset ich_gen)741 static int generate_opcodes(OPCODES * op, enum ich_chipset ich_gen)
742 {
743 	int a;
744 	uint16_t preop, optype;
745 	uint32_t opmenu[2];
746 
747 	if (op == NULL) {
748 		msg_perr("\n%s: null OPCODES pointer!\n", __func__);
749 		return -1;
750 	}
751 
752 	switch (ich_gen) {
753 	case CHIPSET_ICH7:
754 	case CHIPSET_TUNNEL_CREEK:
755 	case CHIPSET_CENTERTON:
756 		preop = REGREAD16(ICH7_REG_PREOP);
757 		optype = REGREAD16(ICH7_REG_OPTYPE);
758 		opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
759 		opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
760 		break;
761 	case CHIPSET_ICH8:
762 	default:		/* Future version might behave the same */
763 		preop = REGREAD16(swseq_data.reg_preop);
764 		optype = REGREAD16(swseq_data.reg_optype);
765 		opmenu[0] = REGREAD32(swseq_data.reg_opmenu);
766 		opmenu[1] = REGREAD32(swseq_data.reg_opmenu + 4);
767 		break;
768 	}
769 
770 	op->preop[0] = (uint8_t) preop;
771 	op->preop[1] = (uint8_t) (preop >> 8);
772 
773 	for (a = 0; a < 8; a++) {
774 		op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
775 		optype >>= 2;
776 	}
777 
778 	for (a = 0; a < 4; a++) {
779 		op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
780 		opmenu[0] >>= 8;
781 	}
782 
783 	for (a = 4; a < 8; a++) {
784 		op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
785 		opmenu[1] >>= 8;
786 	}
787 
788 	/* No preopcodes used by default. */
789 	for (a = 0; a < 8; a++)
790 		op->opcode[a].atomic = 0;
791 
792 	return 0;
793 }
794 
795 /* This function generates OPCODES from or programs OPCODES to ICH according to
796  * the chipset's SPI configuration lock.
797  *
798  * It should be called before ICH sends any spi command.
799  */
ich_init_opcodes(enum ich_chipset ich_gen)800 static int ich_init_opcodes(enum ich_chipset ich_gen)
801 {
802 	int rc = 0;
803 	OPCODES *curopcodes_done;
804 
805 	if (curopcodes)
806 		return 0;
807 
808 	if (ichspi_lock) {
809 		msg_pdbg("Reading OPCODES... ");
810 		curopcodes_done = &O_EXISTING;
811 		rc = generate_opcodes(curopcodes_done, ich_gen);
812 	} else {
813 		msg_pdbg("Programming OPCODES... ");
814 		curopcodes_done = &O_ST_M25P;
815 		rc = program_opcodes(curopcodes_done, 1, ich_gen);
816 	}
817 
818 	if (rc) {
819 		curopcodes = NULL;
820 		msg_perr("failed\n");
821 		return 1;
822 	} else {
823 		curopcodes = curopcodes_done;
824 		msg_pdbg("done\n");
825 		prettyprint_opcodes(curopcodes);
826 		return 0;
827 	}
828 }
829 
830 /* Fill len bytes from the data array into the fdata/spid registers.
831  *
832  * Note that using len > flash->mst->spi.max_data_write will trash the registers
833  * following the data registers.
834  */
ich_fill_data(const uint8_t * data,int len,int reg0_off)835 static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
836 {
837 	uint32_t temp32 = 0;
838 	int i;
839 
840 	if (len <= 0)
841 		return;
842 
843 	for (i = 0; i < len; i++) {
844 		if ((i % 4) == 0)
845 			temp32 = 0;
846 
847 		temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
848 
849 		if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
850 			REGWRITE32(reg0_off + (i - (i % 4)), temp32);
851 	}
852 	i--;
853 	if ((i % 4) != 3) /* Write remaining data to regs. */
854 		REGWRITE32(reg0_off + (i - (i % 4)), temp32);
855 }
856 
857 /* Read len bytes from the fdata/spid register into the data array.
858  *
859  * Note that using len > flash->mst->spi.max_data_read will return garbage or
860  * may even crash.
861  */
ich_read_data(uint8_t * data,int len,int reg0_off)862 static void ich_read_data(uint8_t *data, int len, int reg0_off)
863 {
864 	int i;
865 	uint32_t temp32 = 0;
866 
867 	for (i = 0; i < len; i++) {
868 		if ((i % 4) == 0)
869 			temp32 = REGREAD32(reg0_off + i);
870 
871 		data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
872 	}
873 }
874 
ich7_run_opcode(OPCODE op,uint32_t offset,uint8_t datalength,uint8_t * data,int maxdata)875 static int ich7_run_opcode(OPCODE op, uint32_t offset,
876 			   uint8_t datalength, uint8_t * data, int maxdata)
877 {
878 	bool write_cmd = false;
879 	int timeout;
880 	uint32_t temp32;
881 	uint16_t temp16;
882 	uint64_t opmenu;
883 	int opcode_index;
884 
885 	/* Is it a write command? */
886 	if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
887 	    || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
888 		write_cmd = true;
889 	}
890 
891 	timeout = 100 * 60;	/* 60 ms are 9.6 million cycles at 16 MHz. */
892 	while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
893 		default_delay(10);
894 	}
895 	if (!timeout) {
896 		msg_perr("Error: SCIP never cleared!\n");
897 		return 1;
898 	}
899 
900 	/* Program offset in flash into SPIA while preserving reserved bits. */
901 	temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
902 	REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
903 
904 	/* Program data into SPID0 to N */
905 	if (write_cmd && (datalength != 0))
906 		ich_fill_data(data, datalength, ICH7_REG_SPID0);
907 
908 	/* Assemble SPIS */
909 	temp16 = REGREAD16(ICH7_REG_SPIS);
910 	/* keep reserved bits */
911 	temp16 &= SPIS_RESERVED_MASK;
912 	/* clear error status registers */
913 	temp16 |= (SPIS_CDS | SPIS_FCERR);
914 	REGWRITE16(ICH7_REG_SPIS, temp16);
915 
916 	/* Assemble SPIC */
917 	temp16 = 0;
918 
919 	if (datalength != 0) {
920 		temp16 |= SPIC_DS;
921 		temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
922 	}
923 
924 	/* Select opcode */
925 	opmenu = REGREAD32(ICH7_REG_OPMENU);
926 	opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
927 
928 	for (opcode_index = 0; opcode_index < 8; opcode_index++) {
929 		if ((opmenu & 0xff) == op.opcode) {
930 			break;
931 		}
932 		opmenu >>= 8;
933 	}
934 	if (opcode_index == 8) {
935 		msg_pdbg("Opcode %x not found.\n", op.opcode);
936 		return 1;
937 	}
938 	temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
939 
940 	timeout = 100 * 60;	/* 60 ms are 9.6 million cycles at 16 MHz. */
941 	/* Handle Atomic. Atomic commands include three steps:
942 	    - sending the preop (mainly EWSR or WREN)
943 	    - sending the main command
944 	    - waiting for the busy bit (WIP) to be cleared
945 	   This means the timeout must be sufficient for chip erase
946 	   of slow high-capacity chips.
947 	 */
948 	switch (op.atomic) {
949 	case 2:
950 		/* Select second preop. */
951 		temp16 |= SPIC_SPOP;
952 		/* Fall through. */
953 	case 1:
954 		/* Atomic command (preop+op) */
955 		temp16 |= SPIC_ACS;
956 		timeout = 100 * 1000 * 60;	/* 60 seconds */
957 		break;
958 	}
959 
960 	/* Start */
961 	temp16 |= SPIC_SCGO;
962 
963 	/* write it */
964 	REGWRITE16(ICH7_REG_SPIC, temp16);
965 
966 	/* Wait for Cycle Done Status or Flash Cycle Error. */
967 	while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
968 	       --timeout) {
969 		default_delay(10);
970 	}
971 	if (!timeout) {
972 		msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n", REGREAD16(ICH7_REG_SPIS));
973 		return 1;
974 	}
975 
976 	/* FIXME: make sure we do not needlessly cause transaction errors. */
977 	temp16 = REGREAD16(ICH7_REG_SPIS);
978 	if (temp16 & SPIS_FCERR) {
979 		msg_perr("Transaction error!\n");
980 		/* keep reserved bits */
981 		temp16 &= SPIS_RESERVED_MASK;
982 		REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
983 		return 1;
984 	}
985 
986 	if ((!write_cmd) && (datalength != 0))
987 		ich_read_data(data, datalength, ICH7_REG_SPID0);
988 
989 	return 0;
990 }
991 
ich9_run_opcode(OPCODE op,uint32_t offset,uint8_t datalength,uint8_t * data)992 static int ich9_run_opcode(OPCODE op, uint32_t offset,
993 			   uint8_t datalength, uint8_t * data)
994 {
995 	bool write_cmd = false;
996 	int timeout;
997 	uint32_t temp32;
998 	uint64_t opmenu;
999 	int opcode_index;
1000 
1001 	/* Is it a write command? */
1002 	if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
1003 	    || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
1004 		write_cmd = true;
1005 	}
1006 
1007 	timeout = 100 * 60;	/* 60 ms are 9.6 million cycles at 16 MHz. */
1008 	while ((REGREAD8(swseq_data.reg_ssfsc) & SSFS_SCIP) && --timeout) {
1009 		default_delay(10);
1010 	}
1011 	if (!timeout) {
1012 		msg_perr("Error: SCIP never cleared!\n");
1013 		return 1;
1014 	}
1015 
1016 	/* Program offset in flash into FADDR while preserve the reserved bits
1017 	 * and clearing the 25. address bit which is only usable in hwseq. */
1018 	temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
1019 	REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
1020 
1021 	/* Program data into FDATA0 to N */
1022 	if (write_cmd && (datalength != 0))
1023 		ich_fill_data(data, datalength, ICH9_REG_FDATA0);
1024 
1025 	/* Assemble SSFS + SSFC */
1026 	temp32 = REGREAD32(swseq_data.reg_ssfsc);
1027 	/* Keep reserved bits only */
1028 	temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
1029 	/* Clear cycle done and cycle error status registers */
1030 	temp32 |= (SSFS_FDONE | SSFS_FCERR);
1031 	REGWRITE32(swseq_data.reg_ssfsc, temp32);
1032 
1033 	/* Use 20 MHz */
1034 	temp32 |= SSFC_SCF_20MHZ;
1035 
1036 	/* Set data byte count (DBC) and data cycle bit (DS) */
1037 	if (datalength != 0) {
1038 		uint32_t datatemp;
1039 		temp32 |= SSFC_DS;
1040 		datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) & SSFC_DBC);
1041 		temp32 |= datatemp;
1042 	}
1043 
1044 	/* Select opcode */
1045 	opmenu = REGREAD32(swseq_data.reg_opmenu);
1046 	opmenu |= ((uint64_t)REGREAD32(swseq_data.reg_opmenu + 4)) << 32;
1047 
1048 	for (opcode_index = 0; opcode_index < 8; opcode_index++) {
1049 		if ((opmenu & 0xff) == op.opcode) {
1050 			break;
1051 		}
1052 		opmenu >>= 8;
1053 	}
1054 	if (opcode_index == 8) {
1055 		msg_pdbg("Opcode %x not found.\n", op.opcode);
1056 		return 1;
1057 	}
1058 	temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
1059 
1060 	timeout = 100 * 60;	/* 60 ms are 9.6 million cycles at 16 MHz. */
1061 	/* Handle Atomic. Atomic commands include three steps:
1062 	    - sending the preop (mainly EWSR or WREN)
1063 	    - sending the main command
1064 	    - waiting for the busy bit (WIP) to be cleared
1065 	   This means the timeout must be sufficient for chip erase
1066 	   of slow high-capacity chips.
1067 	 */
1068 	switch (op.atomic) {
1069 	case 2:
1070 		/* Select second preop. */
1071 		temp32 |= SSFC_SPOP;
1072 		/* Fall through. */
1073 	case 1:
1074 		/* Atomic command (preop+op) */
1075 		temp32 |= SSFC_ACS;
1076 		timeout = 100 * 1000 * 60;	/* 60 seconds */
1077 		break;
1078 	}
1079 
1080 	/* Start */
1081 	temp32 |= SSFC_SCGO;
1082 
1083 	/* write it */
1084 	REGWRITE32(swseq_data.reg_ssfsc, temp32);
1085 
1086 	/* Wait for Cycle Done Status or Flash Cycle Error. */
1087 	while (((REGREAD32(swseq_data.reg_ssfsc) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
1088 	       --timeout) {
1089 		default_delay(10);
1090 	}
1091 	if (!timeout) {
1092 		msg_perr("timeout, REG_SSFS=0x%08"PRIx32"\n", REGREAD32(swseq_data.reg_ssfsc));
1093 		return 1;
1094 	}
1095 
1096 	/* FIXME make sure we do not needlessly cause transaction errors. */
1097 	temp32 = REGREAD32(swseq_data.reg_ssfsc);
1098 	if (temp32 & SSFS_FCERR) {
1099 		msg_perr("Transaction error!\n");
1100 		prettyprint_ich9_reg_ssfs(temp32);
1101 		prettyprint_ich9_reg_ssfc(temp32);
1102 		/* keep reserved bits */
1103 		temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
1104 		/* Clear the transaction error. */
1105 		REGWRITE32(swseq_data.reg_ssfsc, temp32 | SSFS_FCERR);
1106 		return 1;
1107 	}
1108 
1109 	if ((!write_cmd) && (datalength != 0))
1110 		ich_read_data(data, datalength, ICH9_REG_FDATA0);
1111 
1112 	return 0;
1113 }
1114 
run_opcode(const struct flashctx * flash,OPCODE op,uint32_t offset,uint8_t datalength,uint8_t * data)1115 static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
1116 		      uint8_t datalength, uint8_t * data)
1117 {
1118 	/* max_data_read == max_data_write for all Intel/VIA SPI masters */
1119 	uint8_t maxlength = flash->mst->spi.max_data_read;
1120 
1121 	if (ich_generation == CHIPSET_ICH_UNKNOWN) {
1122 		msg_perr("%s: unsupported chipset\n", __func__);
1123 		return -1;
1124 	}
1125 
1126 	if (datalength > maxlength) {
1127 		msg_perr("%s: Internal command size error for "
1128 			"opcode 0x%02x, got datalength=%i, want <=%i\n",
1129 			__func__, op.opcode, datalength, maxlength);
1130 		return SPI_INVALID_LENGTH;
1131 	}
1132 
1133 	switch (ich_generation) {
1134 	case CHIPSET_ICH7:
1135 	case CHIPSET_TUNNEL_CREEK:
1136 	case CHIPSET_CENTERTON:
1137 		return ich7_run_opcode(op, offset, datalength, data, maxlength);
1138 	case CHIPSET_ICH8:
1139 	default:		/* Future version might behave the same */
1140 		return ich9_run_opcode(op, offset, datalength, data);
1141 	}
1142 }
1143 
ich_spi_send_command(const struct flashctx * flash,unsigned int writecnt,unsigned int readcnt,const unsigned char * writearr,unsigned char * readarr)1144 static int ich_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
1145 				unsigned int readcnt,
1146 				const unsigned char *writearr,
1147 				unsigned char *readarr)
1148 {
1149 	int result;
1150 	int opcode_index = -1;
1151 	const unsigned char cmd = *writearr;
1152 	OPCODE *opcode;
1153 	uint32_t addr = 0;
1154 	uint8_t *data;
1155 	int count;
1156 
1157 	/* find cmd in opcodes-table */
1158 	opcode_index = find_opcode(curopcodes, cmd);
1159 	if (opcode_index == -1) {
1160 		if (!ichspi_lock)
1161 			opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
1162 		if (opcode_index == SPI_INVALID_LENGTH) {
1163 			msg_pdbg("OPCODE 0x%02x has unsupported length, will not execute.\n", cmd);
1164 			return SPI_INVALID_LENGTH;
1165 		} else if (opcode_index == -1) {
1166 			msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n", cmd);
1167 			return SPI_INVALID_OPCODE;
1168 		}
1169 	}
1170 
1171 	if (is_dry_run())
1172 		return 0;
1173 
1174 	opcode = &(curopcodes->opcode[opcode_index]);
1175 
1176 	/* The following valid writecnt/readcnt combinations exist:
1177 	 * writecnt  = 4, readcnt >= 0
1178 	 * writecnt  = 1, readcnt >= 0
1179 	 * writecnt >= 4, readcnt  = 0
1180 	 * writecnt >= 1, readcnt  = 0
1181 	 * writecnt >= 1 is guaranteed for all commands.
1182 	 */
1183 	if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
1184 	    (writecnt != 4)) {
1185 		msg_perr("%s: Internal command size error for opcode "
1186 			"0x%02x, got writecnt=%i, want =4\n", __func__, cmd, writecnt);
1187 		return SPI_INVALID_LENGTH;
1188 	}
1189 	if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
1190 	    (writecnt != 1)) {
1191 		msg_perr("%s: Internal command size error for opcode "
1192 			"0x%02x, got writecnt=%i, want =1\n", __func__, cmd, writecnt);
1193 		return SPI_INVALID_LENGTH;
1194 	}
1195 	if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1196 	    (writecnt < 4)) {
1197 		msg_perr("%s: Internal command size error for opcode "
1198 			"0x%02x, got writecnt=%i, want >=4\n", __func__, cmd, writecnt);
1199 		return SPI_INVALID_LENGTH;
1200 	}
1201 	if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1202 	     (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1203 	    (readcnt)) {
1204 		msg_perr("%s: Internal command size error for opcode "
1205 			"0x%02x, got readcnt=%i, want =0\n", __func__, cmd, readcnt);
1206 		return SPI_INVALID_LENGTH;
1207 	}
1208 
1209 	/* Translate read/write array/count.
1210 	 * The maximum data length is identical for the maximum read length and
1211 	 * for the maximum write length excluding opcode and address. Opcode and
1212 	 * address are stored in separate registers, not in the data registers
1213 	 * and are thus not counted towards data length. The only exception
1214 	 * applies if the opcode definition (un)intentionally classifies said
1215 	 * opcode incorrectly as non-address opcode or vice versa. */
1216 	if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
1217 		data = (uint8_t *) (writearr + 1);
1218 		count = writecnt - 1;
1219 	} else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1220 		data = (uint8_t *) (writearr + 4);
1221 		count = writecnt - 4;
1222 	} else {
1223 		data = (uint8_t *) readarr;
1224 		count = readcnt;
1225 	}
1226 
1227 	/* if opcode-type requires an address */
1228 	if (cmd == JEDEC_REMS || cmd == JEDEC_RES) {
1229 		addr = ichspi_bbar;
1230 	} else if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1231 	    opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1232 		/* BBAR may cut part of the chip off at the lower end. */
1233 		const uint32_t valid_base = ichspi_bbar & ((flash->chip->total_size * 1024) - 1);
1234 		const uint32_t addr_offset = ichspi_bbar - valid_base;
1235 		/* Highest address we can program is (2^24 - 1). */
1236 		const uint32_t valid_end = (1 << 24) - addr_offset;
1237 
1238 		addr = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
1239 		const uint32_t addr_end = addr + count;
1240 
1241 		if (addr < valid_base ||
1242 		    addr_end < addr || /* integer overflow check */
1243 		    addr_end > valid_end) {
1244 			msg_perr("%s: Addressed region 0x%06"PRIx32"-0x%06"PRIx32" not in allowed range 0x%06"PRIx32"-0x%06"PRIx32"\n",
1245 				 __func__, addr, addr_end - 1, valid_base, valid_end - 1);
1246 			return SPI_INVALID_ADDRESS;
1247 		}
1248 		addr += addr_offset;
1249 	}
1250 
1251 	result = run_opcode(flash, *opcode, addr, count, data);
1252 	if (result) {
1253 		msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1254 		if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1255 		    (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1256 			msg_pdbg("at address 0x%06"PRIx32" ", addr);
1257 		}
1258 		msg_pdbg("(payload length was %d).\n", count);
1259 
1260 		/* Print out the data array if it contains data to write.
1261 		 * Errors are detected before the received data is read back into
1262 		 * the array so it won't make sense to print it then. */
1263 		if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1264 		    (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1265 			int i;
1266 			msg_pspew("The data was:\n");
1267 			for (i = 0; i < count; i++){
1268 				msg_pspew("%3d: 0x%02"PRIx8"\n", i, data[i]);
1269 			}
1270 		}
1271 	}
1272 
1273 	return result;
1274 }
1275 
1276 #define MAX_FD_REGIONS 16
1277 struct fd_region {
1278 	const char* name;
1279 	enum ich_access_protection level;
1280 	uint32_t base;
1281 	uint32_t limit;
1282 };
1283 
1284 struct hwseq_data {
1285 	uint32_t size_comp0;
1286 	uint32_t size_comp1;
1287 	uint32_t addr_mask;
1288 	bool only_4k;
1289 	uint32_t hsfc_fcycle;
1290 
1291 	struct fd_region fd_regions[MAX_FD_REGIONS];
1292 };
1293 
get_hwseq_data_from_context(const struct flashctx * flash)1294 static struct hwseq_data *get_hwseq_data_from_context(const struct flashctx *flash)
1295 {
1296 	return flash->mst->opaque.data;
1297 }
1298 
1299 /* Sets FLA in FADDR to (addr & hwseq_data->addr_mask) without touching other bits. */
ich_hwseq_set_addr(uint32_t addr,uint32_t mask)1300 static void ich_hwseq_set_addr(uint32_t addr, uint32_t mask)
1301 {
1302 	uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~mask;
1303 	REGWRITE32(ICH9_REG_FADDR, (addr & mask) | addr_old);
1304 }
1305 
1306 /* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
1307  * of the block containing this address. May return nonsense if the address is
1308  * not valid. The erase block size for a specific address depends on the flash
1309  * partition layout as specified by FPB and the partition properties as defined
1310  * by UVSCC and LVSCC respectively. An alternative to implement this method
1311  * would be by querying FPB and the respective VSCC register directly.
1312  */
ich_hwseq_get_erase_block_size(unsigned int addr,uint32_t addr_mask,bool only_4k)1313 static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr, uint32_t addr_mask, bool only_4k)
1314 {
1315 	uint8_t enc_berase;
1316 	static const uint32_t dec_berase[4] = {
1317 		256,
1318 		4 * 1024,
1319 		8 * 1024,
1320 		64 * 1024
1321 	};
1322 
1323 	if (only_4k) {
1324 		return 4 * 1024;
1325 	}
1326 
1327 	ich_hwseq_set_addr(addr, addr_mask);
1328 	enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >> HSFS_BERASE_OFF;
1329 	return dec_berase[enc_berase];
1330 }
1331 
1332 /* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
1333    Resets all error flags in HSFS.
1334    Returns 0 if the cycle completes successfully without errors within
1335    timeout us, 1 on errors. */
ich_hwseq_wait_for_cycle_complete(unsigned int len,enum ich_chipset ich_gen,uint32_t addr_mask)1336 static int ich_hwseq_wait_for_cycle_complete(unsigned int len, enum ich_chipset ich_gen, uint32_t addr_mask)
1337 {
1338 	/*
1339 	 * The SPI bus may be busy due to performing operations from other masters, hence
1340 	 * introduce the long timeout of 30s to cover the worst case scenarios as well.
1341 	 */
1342 	unsigned int timeout_us = 30 * 1000 * 1000;
1343 	uint16_t hsfs;
1344 	uint32_t addr;
1345 
1346 	timeout_us /= 8; /* scale timeout duration to counter */
1347 	while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) &
1348 		 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
1349 	       --timeout_us) {
1350 		default_delay(8);
1351 	}
1352 	REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1353 	if (!timeout_us) {
1354 		addr = REGREAD32(ICH9_REG_FADDR) & addr_mask;
1355 		msg_perr("Timeout error between offset 0x%08"PRIx32" and "
1356 			 "0x%08"PRIx32" (= 0x%08"PRIx32" + %d)!\n",
1357 			 addr, addr + len - 1, addr, len - 1);
1358 		prettyprint_ich9_reg_hsfs(hsfs, ich_gen);
1359 		prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC), ich_gen);
1360 		return 1;
1361 	}
1362 
1363 	if (hsfs & HSFS_FCERR) {
1364 		addr = REGREAD32(ICH9_REG_FADDR) & addr_mask;
1365 		msg_perr("Transaction error between offset 0x%08"PRIx32" and "
1366 			 "0x%08"PRIx32" (= 0x%08"PRIx32" + %d)!\n",
1367 			 addr, addr + len - 1, addr, len - 1);
1368 		prettyprint_ich9_reg_hsfs(hsfs, ich_gen);
1369 		prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC), ich_gen);
1370 		return 1;
1371 	}
1372 	return 0;
1373 }
1374 
1375 /* Fire up a transfer using the hardware sequencer. */
ich_start_hwseq_xfer(const struct flashctx * flash,uint32_t hsfc_cycle,uint32_t flash_addr,size_t len,uint32_t addr_mask)1376 static void ich_start_hwseq_xfer(const struct flashctx *flash,
1377 		uint32_t hsfc_cycle, uint32_t flash_addr, size_t len,
1378 		uint32_t addr_mask)
1379 {
1380 	/* make sure HSFC register is cleared before initiate any operation */
1381 	uint16_t hsfc = 0;
1382 
1383 	/* Sets flash_addr in FADDR */
1384 	ich_hwseq_set_addr(flash_addr, addr_mask);
1385 
1386 	/* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
1387 	REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1388 
1389 	/* Set up transaction parameters. */
1390 	hsfc |= hsfc_cycle;
1391 	/*
1392 	 * The number of bytes transferred is the value of `FDBC` plus 1, hence,
1393 	 * subtracted 1 from the length field.
1394 	 * As per Intel EDS, `0b` in the FDBC represents 1 byte while `0x3f`
1395 	 * represents 64-bytes to be transferred.
1396 	 */
1397 	hsfc |= HSFC_FDBC_VAL(len - 1);
1398 	hsfc |= HSFC_FGO; /* start */
1399 	prettyprint_ich9_reg_hsfc(hsfc, ich_generation);
1400 	REGWRITE16(ICH9_REG_HSFC, hsfc);
1401 }
1402 
ich_wait_for_hwseq_spi_cycle_complete(void)1403 static int ich_wait_for_hwseq_spi_cycle_complete(void)
1404 {
1405 	if (REGREAD8(ICH9_REG_HSFS) & HSFS_SCIP) {
1406 		msg_perr("Error: SCIP bit is unexpectedly set.\n");
1407 		return 1;
1408 	}
1409 	return 0;
1410 }
1411 
1412 /* Execute SPI flash transfer */
ich_exec_sync_hwseq_xfer(const struct flashctx * flash,uint32_t hsfc_cycle,uint32_t flash_addr,size_t len,enum ich_chipset ich_gen,uint32_t addr_mask)1413 static int ich_exec_sync_hwseq_xfer(const struct flashctx *flash, uint32_t hsfc_cycle, uint32_t flash_addr,
1414 				size_t len, enum ich_chipset ich_gen, uint32_t addr_mask)
1415 {
1416 	if (ich_wait_for_hwseq_spi_cycle_complete()) {
1417 		msg_perr("SPI Transaction Timeout due to previous operation in process!\n");
1418 		return 1;
1419 	}
1420 
1421 	ich_start_hwseq_xfer(flash, hsfc_cycle, flash_addr, len, addr_mask);
1422 	return ich_hwseq_wait_for_cycle_complete(len, ich_gen, addr_mask);
1423 }
1424 
ich_get_region(const struct flashctx * flash,unsigned int addr,struct flash_region * region)1425 static void ich_get_region(const struct flashctx *flash, unsigned int addr, struct flash_region *region)
1426 {
1427 	struct ich_descriptors desc = { 0 };
1428 	const ssize_t nr = ich_number_of_regions(ich_generation, &desc.content);
1429 	const struct hwseq_data *hwseq_data = get_hwseq_data_from_context(flash);
1430 	const struct fd_region *fd_regions = hwseq_data->fd_regions;
1431 
1432 	/*
1433 	 * Set default values for the region. If no flash descriptor containing
1434 	 * addr is found, these values will be used instead.
1435 	 *
1436 	 * The region start and end are constrained so that they do not overlap
1437 	 * any flash descriptor regions.
1438 	 */
1439 	const char *name = "";
1440 	region->read_prot  = false;
1441 	region->write_prot = false;
1442 	region->start = 0;
1443 	region->end = flashrom_flash_getsize(flash);
1444 
1445 	for (ssize_t i = 0; i < nr; i++) {
1446 		uint32_t base = fd_regions[i].base;
1447 		uint32_t limit = fd_regions[i].limit;
1448 		enum ich_access_protection level = fd_regions[i].level;
1449 
1450 		if (addr < base) {
1451 			/*
1452 			 * fd_regions[i] starts after addr, constrain
1453 			 * region->end so that it does not overlap.
1454 			 */
1455 			region->end = min(region->end, base);
1456 		} else if (addr > limit) {
1457 			/*
1458 			 * fd_regions[i] ends before addr, constrain
1459 			 * region->start so that it does not overlap.
1460 			 */
1461 			region->start = max(region->start, limit + 1);
1462 		} else {
1463 			/* fd_regions[i] contains addr, copy to *region. */
1464 			name = fd_regions[i].name;
1465 			region->start = base;
1466 			region->end = limit + 1;
1467 			region->read_prot  = (level == LOCKED) || (level == READ_PROT);
1468 			region->write_prot = (level == LOCKED) || (level == WRITE_PROT);
1469 			break;
1470 		}
1471 	}
1472 
1473 	region->name = strdup(name);
1474 }
1475 
1476 /* Given RDID info, return pointer to entry in flashchips[] */
flash_id_to_entry(uint32_t mfg_id,uint32_t model_id)1477 static const struct flashchip *flash_id_to_entry(uint32_t mfg_id, uint32_t model_id)
1478 {
1479 	const struct flashchip *chip;
1480 
1481 	for (chip = &flashchips[0]; chip->vendor; chip++) {
1482 		if(is_chipname_duplicate(chip))
1483 			continue;
1484 
1485 		if ((chip->manufacture_id == mfg_id) &&
1486 		    (chip->model_id == model_id) &&
1487 		    (chip->probe == PROBE_SPI_RDID) &&
1488 		    ((chip->bustype & BUS_SPI) == BUS_SPI))
1489 			return chip;
1490 	}
1491 
1492 	return NULL;
1493 }
1494 
ich_hwseq_read_status(const struct flashctx * flash,enum flash_reg reg,uint8_t * value)1495 static int ich_hwseq_read_status(const struct flashctx *flash, enum flash_reg reg, uint8_t *value)
1496 {
1497 	const int len = 1;
1498 	const struct hwseq_data *hwseq_data = get_hwseq_data_from_context(flash);
1499 
1500 	if (reg != STATUS1) {
1501 		msg_pdbg("%s: only supports STATUS1\n", __func__);
1502 		/*
1503 		 * Return SPI_INVALID_OPCODE to be consistent with spi_read_register()
1504 		 * and make error handling simpler even though this isn't a SPI master.
1505 		 */
1506 		return SPI_INVALID_OPCODE;
1507 	}
1508 	msg_pdbg("Reading Status register\n");
1509 
1510 	if (ich_exec_sync_hwseq_xfer(flash, HSFC_CYCLE_RD_STATUS, 1, len, ich_generation,
1511 		hwseq_data->addr_mask)) {
1512 		msg_perr("Reading Status register failed\n!!");
1513 		return -1;
1514 	}
1515 	ich_read_data(value, len, ICH9_REG_FDATA0);
1516 
1517 	return 0;
1518 }
1519 
ich_hwseq_write_status(const struct flashctx * flash,enum flash_reg reg,uint8_t value)1520 static int ich_hwseq_write_status(const struct flashctx *flash, enum flash_reg reg, uint8_t value)
1521 {
1522 	const int len = 1;
1523 	const struct hwseq_data *hwseq_data = get_hwseq_data_from_context(flash);
1524 
1525 	if (reg != STATUS1) {
1526 		msg_pdbg("%s: only supports STATUS1\n", __func__);
1527 		/*
1528 		 * Return SPI_INVALID_OPCODE to be consistent with spi_write_register()
1529 		 * and make error handling simpler even though this isn't a SPI master.
1530 		 */
1531 		return SPI_INVALID_OPCODE;
1532 	}
1533 	msg_pdbg("Writing status register\n");
1534 
1535 	ich_fill_data(&value, len, ICH9_REG_FDATA0);
1536 
1537 	if (ich_exec_sync_hwseq_xfer(flash, HSFC_CYCLE_WR_STATUS, 1, len, ich_generation,
1538 		hwseq_data->addr_mask)) {
1539 		msg_perr("Writing Status register failed\n!!");
1540 		return -1;
1541 	}
1542 
1543 	return 0;
1544 }
1545 
ich_hwseq_get_flash_id(struct flashctx * flash,enum ich_chipset ich_gen)1546 static void ich_hwseq_get_flash_id(struct flashctx *flash, enum ich_chipset ich_gen)
1547 {
1548 	const struct hwseq_data *hwseq_data = get_hwseq_data_from_context(flash);
1549 	if (hwseq_data->size_comp1 != 0) {
1550 		msg_pinfo("Multiple flash components detected, skipping flash identification.\n");
1551 		return;
1552 	}
1553 
1554 	/* PCH100 or above is required for RDID, ICH9 does not support it. */
1555 	if (hwseq_data->hsfc_fcycle != PCH100_HSFC_FCYCLE) {
1556 		msg_pinfo("RDID cycle not supported, skipping flash identification.\n");
1557 		return;
1558 	}
1559 
1560 	/*
1561 	 * RDID gives 3 byte output:
1562 	 *     Byte 0: Manufacturer ID
1563 	 *     Byte 1: Model ID (MSB)
1564 	 *     Byte 2: Model ID (LSB)
1565 	 */
1566 	const int len = 3;
1567 	uint8_t data[len];
1568 
1569 	if (ich_exec_sync_hwseq_xfer(flash, HSFC_CYCLE_RDID, 1, len, ich_gen,
1570 		hwseq_data->addr_mask)) {
1571 		msg_perr("Timed out waiting for RDID to complete.\n");
1572 	}
1573 
1574 	ich_read_data(data, len, ICH9_REG_FDATA0);
1575 	uint32_t mfg_id = data[0];
1576 	uint32_t model_id = (data[1] << 8) | data[2];
1577 
1578 	const struct flashchip *entry = flash_id_to_entry(mfg_id, model_id);
1579 	if (!entry) {
1580 		msg_pwarn("Unable to identify chip, mfg_id: 0x%02"PRIx32", "
1581 				"model_id: 0x%02"PRIx32"\n", mfg_id, model_id);
1582 		return;
1583 	}
1584 
1585 	msg_pdbg("Chip identified: %s\n", entry->name);
1586 
1587 	/* Update informational flash chip entries only */
1588 	flash->chip->vendor = entry->vendor;
1589 	flash->chip->name = entry->name;
1590 	flash->chip->manufacture_id = entry->manufacture_id;
1591 	flash->chip->model_id = entry->model_id;
1592 	/* total_size read from flash descriptor */
1593 	flash->chip->page_size = entry->page_size;
1594 	flash->chip->feature_bits = entry->feature_bits;
1595 	flash->chip->tested = entry->tested;
1596 	/* Support writeprotect */
1597 	flash->chip->reg_bits = entry->reg_bits;
1598 	flash->chip->decode_range = entry->decode_range;
1599 }
1600 
ich_hwseq_probe(struct flashctx * flash)1601 static int ich_hwseq_probe(struct flashctx *flash)
1602 {
1603 	uint32_t total_size, boundary;
1604 	uint32_t erase_size_low, size_low, erase_size_high, size_high;
1605 	struct block_eraser *eraser;
1606 	const struct hwseq_data *hwseq_data = get_hwseq_data_from_context(flash);
1607 
1608 	total_size = hwseq_data->size_comp0 + hwseq_data->size_comp1;
1609 	msg_cdbg("Hardware sequencing reports %d attached SPI flash chip",
1610 		 (hwseq_data->size_comp1 != 0) ? 2 : 1);
1611 	if (hwseq_data->size_comp1 != 0)
1612 		msg_cdbg("s with a combined");
1613 	else
1614 		msg_cdbg(" with a");
1615 	msg_cdbg(" density of %"PRId32" kB.\n", total_size / 1024);
1616 	flash->chip->total_size = total_size / 1024;
1617 
1618 	eraser = &(flash->chip->block_erasers[0]);
1619 	if (!hwseq_data->only_4k)
1620 		boundary = (REGREAD32(ICH9_REG_FPB) & FPB_FPBA) << 12;
1621 	else
1622 		boundary = 0;
1623 	size_high = total_size - boundary;
1624 	erase_size_high = ich_hwseq_get_erase_block_size(boundary, hwseq_data->addr_mask, hwseq_data->only_4k);
1625 
1626 	if (boundary == 0) {
1627 		msg_cdbg2("There is only one partition containing the whole "
1628 			 "address space (0x%06x - 0x%06"PRIx32").\n", 0, size_high-1);
1629 		eraser->eraseblocks[0].size = erase_size_high;
1630 		eraser->eraseblocks[0].count = size_high / erase_size_high;
1631 		msg_cdbg2("There are %"PRId32" erase blocks with %"PRId32" B each.\n",
1632 			 size_high / erase_size_high, erase_size_high);
1633 	} else {
1634 		msg_cdbg2("The flash address space (0x%06x - 0x%06"PRIx32") is divided "
1635 			 "at address 0x%06"PRIx32" in two partitions.\n",
1636 			 0, total_size-1, boundary);
1637 		size_low = total_size - size_high;
1638 		erase_size_low = ich_hwseq_get_erase_block_size(0, hwseq_data->addr_mask, hwseq_data->only_4k);
1639 
1640 		eraser->eraseblocks[0].size = erase_size_low;
1641 		eraser->eraseblocks[0].count = size_low / erase_size_low;
1642 		msg_cdbg("The first partition ranges from 0x%06x to 0x%06"PRIx32".\n", 0, size_low-1);
1643 		msg_cdbg("In that range are %"PRId32" erase blocks with %"PRId32" B each.\n",
1644 			 size_low / erase_size_low, erase_size_low);
1645 
1646 		eraser->eraseblocks[1].size = erase_size_high;
1647 		eraser->eraseblocks[1].count = size_high / erase_size_high;
1648 		msg_cdbg("The second partition ranges from 0x%06"PRIx32" to 0x%06"PRIx32".\n",
1649 			 boundary, total_size-1);
1650 		msg_cdbg("In that range are %"PRId32" erase blocks with %"PRId32" B each.\n",
1651 			 size_high / erase_size_high, erase_size_high);
1652 	}
1653 
1654 	/* May be overwritten by ich_hwseq_get_flash_id(). */
1655 	flash->chip->tested = TEST_OK_PREWB;
1656 
1657 	ich_hwseq_get_flash_id(flash, ich_generation);
1658 
1659 	return 1;
1660 }
1661 
ich_hwseq_block_erase(struct flashctx * flash,unsigned int addr,unsigned int len)1662 static int ich_hwseq_block_erase(struct flashctx *flash, unsigned int addr,
1663 				 unsigned int len)
1664 {
1665 	uint32_t erase_block;
1666 	const struct hwseq_data *hwseq_data = get_hwseq_data_from_context(flash);
1667 
1668 	if (is_dry_run())
1669 		return 0;
1670 
1671 	erase_block = ich_hwseq_get_erase_block_size(addr, hwseq_data->addr_mask, hwseq_data->only_4k);
1672 	if (len != erase_block) {
1673 		msg_cerr("Erase block size for address 0x%06x is %"PRId32" B, "
1674 			 "but requested erase block size is %d B. "
1675 			 "Not erasing anything.\n", addr, erase_block, len);
1676 		return -1;
1677 	}
1678 
1679 	/* Although the hardware supports this (it would erase the whole block
1680 	 * containing the address) we play safe here. */
1681 	if (addr % erase_block != 0) {
1682 		msg_cerr("Erase address 0x%06x is not aligned to the erase "
1683 			 "block boundary (any multiple of %"PRId32"). "
1684 			 "Not erasing anything.\n", addr, erase_block);
1685 		return -1;
1686 	}
1687 
1688 	if (addr + len > flash->chip->total_size * 1024) {
1689 		msg_perr("Request to erase some inaccessible memory address(es)"
1690 			 " (addr=0x%x, len=%d). Not erasing anything.\n", addr, len);
1691 		return -1;
1692 	}
1693 
1694 	msg_pdbg("Erasing %d bytes starting at 0x%06x.\n", len, addr);
1695 
1696 	if (ich_exec_sync_hwseq_xfer(flash, HSFC_CYCLE_BLOCK_ERASE, addr, 1, ich_generation,
1697 		hwseq_data->addr_mask))
1698 		return -1;
1699 	return 0;
1700 }
1701 
ich_hwseq_read(struct flashctx * flash,uint8_t * buf,unsigned int addr,unsigned int len)1702 static int ich_hwseq_read(struct flashctx *flash, uint8_t *buf,
1703 			  unsigned int addr, unsigned int len)
1704 {
1705 	uint8_t block_len;
1706 	const struct hwseq_data *hwseq_data = get_hwseq_data_from_context(flash);
1707 
1708 	if (addr + len > flash->chip->total_size * 1024) {
1709 		msg_perr("Request to read from an inaccessible memory address "
1710 			 "(addr=0x%x, len=%d).\n", addr, len);
1711 		return -1;
1712 	}
1713 
1714 	msg_pdbg("Reading %d bytes starting at 0x%06x.\n", len, addr);
1715 	/* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1716 	REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1717 
1718 	while (len > 0) {
1719 		/* Obey programmer limit... */
1720 		block_len = min(len, flash->mst->opaque.max_data_read);
1721 		/* as well as flash chip page borders as demanded in the Intel datasheets. */
1722 		block_len = min(block_len, 256 - (addr & 0xFF));
1723 
1724 		if (ich_exec_sync_hwseq_xfer(flash, HSFC_CYCLE_READ, addr, block_len, ich_generation,
1725 			hwseq_data->addr_mask))
1726 			return 1;
1727 		ich_read_data(buf, block_len, ICH9_REG_FDATA0);
1728 		addr += block_len;
1729 		buf += block_len;
1730 		len -= block_len;
1731 	}
1732 	return 0;
1733 }
1734 
ich_hwseq_write(struct flashctx * flash,const uint8_t * buf,unsigned int addr,unsigned int len)1735 static int ich_hwseq_write(struct flashctx *flash, const uint8_t *buf, unsigned int addr, unsigned int len)
1736 {
1737 	uint8_t block_len;
1738 	const struct hwseq_data *hwseq_data = get_hwseq_data_from_context(flash);
1739 
1740 	if (addr + len > flash->chip->total_size * 1024) {
1741 		msg_perr("Request to write to an inaccessible memory address "
1742 			 "(addr=0x%x, len=%d).\n", addr, len);
1743 		return -1;
1744 	}
1745 
1746 	msg_pdbg("Writing %d bytes starting at 0x%06x.\n", len, addr);
1747 	/* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1748 	REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1749 
1750 	while (len > 0) {
1751 		/* Obey programmer limit... */
1752 		block_len = min(len, flash->mst->opaque.max_data_write);
1753 		/* as well as flash chip page borders as demanded in the Intel datasheets. */
1754 		block_len = min(block_len, 256 - (addr & 0xFF));
1755 		ich_fill_data(buf, block_len, ICH9_REG_FDATA0);
1756 
1757 		if (ich_exec_sync_hwseq_xfer(flash, HSFC_CYCLE_WRITE, addr, block_len, ich_generation,
1758 			hwseq_data->addr_mask))
1759 			return -1;
1760 		addr += block_len;
1761 		buf += block_len;
1762 		len -= block_len;
1763 	}
1764 	return 0;
1765 }
1766 
ich_hwseq_shutdown(void * data)1767 static int ich_hwseq_shutdown(void *data)
1768 {
1769 	free(data);
1770 	return 0;
1771 }
1772 
ich_spi_send_multicommand(const struct flashctx * flash,struct spi_command * cmds)1773 static int ich_spi_send_multicommand(const struct flashctx *flash,
1774 				     struct spi_command *cmds)
1775 {
1776 	int ret = 0;
1777 	int i;
1778 	int oppos, preoppos;
1779 	for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
1780 		if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
1781 			/* Next command is valid. */
1782 			preoppos = find_preop(curopcodes, cmds->writearr[0]);
1783 			oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
1784 			if ((oppos == -1) && (preoppos != -1)) {
1785 				/* Current command is listed as preopcode in
1786 				 * ICH struct OPCODES, but next command is not
1787 				 * listed as opcode in that struct.
1788 				 * Check for command sanity, then
1789 				 * try to reprogram the ICH opcode list.
1790 				 */
1791 				if (find_preop(curopcodes,
1792 					       (cmds + 1)->writearr[0]) != -1) {
1793 					msg_perr("%s: Two subsequent "
1794 						"preopcodes 0x%02x and 0x%02x, "
1795 						"ignoring the first.\n",
1796 						__func__, cmds->writearr[0],
1797 						(cmds + 1)->writearr[0]);
1798 					continue;
1799 				}
1800 				/* If the chipset is locked down, we'll fail
1801 				 * during execution of the next command anyway.
1802 				 * No need to bother with fixups.
1803 				 */
1804 				if (!ichspi_lock) {
1805 					oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1806 					if (oppos == -1)
1807 						continue;
1808 					curopcodes->opcode[oppos].atomic = preoppos + 1;
1809 					continue;
1810 				}
1811 			}
1812 			if ((oppos != -1) && (preoppos != -1)) {
1813 				/* Current command is listed as preopcode in
1814 				 * ICH struct OPCODES and next command is listed
1815 				 * as opcode in that struct. Match them up.
1816 				 */
1817 				curopcodes->opcode[oppos].atomic = preoppos + 1;
1818 				continue;
1819 			}
1820 			/* If none of the above if-statements about oppos or
1821 			 * preoppos matched, this is a normal opcode.
1822 			 */
1823 		}
1824 		ret = ich_spi_send_command(flash, cmds->writecnt, cmds->readcnt,
1825 					   cmds->writearr, cmds->readarr);
1826 		/* Reset the type of all opcodes to non-atomic. */
1827 		for (i = 0; i < 8; i++)
1828 			curopcodes->opcode[i].atomic = 0;
1829 	}
1830 	return ret;
1831 }
1832 
ich_spi_probe_opcode(const struct flashctx * flash,uint8_t opcode)1833 static bool ich_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode)
1834 {
1835 	int ret = find_opcode(curopcodes, opcode);
1836 	if ((ret == -1) && (lookup_spi_type(opcode) <= 3))
1837 		/* opcode is in POSSIBLE_OPCODES, report supported. */
1838 		return true;
1839 	return ret >= 0;
1840 }
1841 
1842 #define ICH_BMWAG(x) ((x >> 24) & 0xff)
1843 #define ICH_BMRAG(x) ((x >> 16) & 0xff)
1844 #define ICH_BRWA(x)  ((x >>  8) & 0xff)
1845 #define ICH_BRRA(x)  ((x >>  0) & 0xff)
1846 
1847 static const enum ich_access_protection access_perms_to_protection[] = {
1848 	LOCKED, WRITE_PROT, READ_PROT, NO_PROT
1849 };
1850 static const char *const access_names[] = {
1851 	"read-write", "write-only", "read-only", "locked"
1852 };
1853 
1854 #define EMBEDDED_CONTROLLER_REGION	8
1855 
ec_region_rwperms(unsigned int i)1856 static enum ich_access_protection ec_region_rwperms(unsigned int i)
1857 {
1858 	/*
1859 	 * Use Flash Descriptor Observe register to determine if
1860 	 * the EC region can be written by the BIOS master.
1861 	 */
1862 	int rwperms = NO_PROT;
1863 
1864 	struct ich_descriptors desc;
1865 	memset(&desc, 0, sizeof(desc));
1866 
1867 	/* Region is RW if flash descriptor override is set */
1868 	uint16_t freg = REGREAD16(ICH9_REG_HSFS);
1869 	if ((freg & HSFS_FDV) && !(freg & HSFS_FDOPSS)) {
1870 		rwperms = NO_PROT;
1871 	} else if (read_ich_descriptors_via_fdo(ich_generation, ich_spibar, &desc) == ICH_RET_OK) {
1872 		const struct ich_desc_master *const mstr = &desc.master;
1873 		int bios_ec_r = mstr->mstr[i].read  & BIT(16); /* BIOS_EC_r in PCH100+ */
1874 		int bios_ec_w = mstr->mstr[i].write & BIT(28); /* BIOS_EC_w in PCH100+ */
1875 		if (bios_ec_r && bios_ec_w)
1876 			rwperms = NO_PROT;
1877 		else if (bios_ec_r && !bios_ec_w)
1878 			rwperms = WRITE_PROT;
1879 		else if (!bios_ec_r && bios_ec_w)
1880 			rwperms = READ_PROT;
1881 		else
1882 			rwperms = LOCKED;
1883 	}
1884 
1885 	return rwperms;
1886 }
1887 
ich_get_bios_region_access(uint16_t * region_read_access,uint16_t * region_write_access)1888 static void ich_get_bios_region_access(uint16_t *region_read_access,
1889 				       uint16_t *region_write_access)
1890 {
1891 	uint32_t tmp;
1892 	*region_read_access = 0;
1893 	*region_write_access = 0;
1894 
1895 	if (ich_generation >= CHIPSET_METEOR_LAKE) {
1896 		/*
1897 		 * Starting from Meteor Lake, we need to fetch the region
1898 		 * read/write access permissions from the BIOS_BM registers
1899 		 * because we need to support FREG9 or above.
1900 		 */
1901 		*region_read_access = mmio_readw(ich_spibar + ICH_REG_BIOS_BM_RAP);
1902 		*region_write_access = mmio_readw(ich_spibar + ICH_REG_BIOS_BM_WAP);
1903 		msg_pdbg("0x118: 0x%04"PRIx16" (BIOS_BM_RAP)\n", *region_read_access);
1904 		msg_pdbg("0x11a: 0x%04"PRIx16" (BIOS_BM_WAP)\n", *region_write_access);
1905 	} else {
1906 		/*
1907 		 * FRAP - Flash Regions Access Permissions Register
1908 		 * Bit Descriptions:
1909 		 * 31:24 BIOS Master Write Access Grant (BMWAG)
1910 		 * 23:16 BIOS Master Read Access Grant (BMRAG)
1911 		 * 15:8 BIOS Region Write Access (BRWA)
1912 		 * 7:0 BIOS Region Read Access (BRRA)
1913 		 */
1914 		tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
1915 		msg_pdbg("0x50: 0x%08"PRIx32" (FRAP)\n", tmp);
1916 		msg_pdbg("BMWAG 0x%02"PRIx32", ", ICH_BMWAG(tmp));
1917 		msg_pdbg("BMRAG 0x%02"PRIx32", ", ICH_BMRAG(tmp));
1918 		msg_pdbg("BRWA 0x%02"PRIx32", ", ICH_BRWA(tmp));
1919 		msg_pdbg("BRRA 0x%02"PRIx32"\n", ICH_BRRA(tmp));
1920 
1921 		*region_read_access = (uint16_t)ICH_BRRA(tmp);
1922 		*region_write_access = (uint16_t)ICH_BRWA(tmp);
1923 	}
1924 }
1925 
ich_get_defined_region_count(void)1926 static unsigned int ich_get_defined_region_count(void) {
1927 	return (ich_generation >= CHIPSET_METEOR_LAKE) ? 16 : 8;
1928 }
1929 
ich9_handle_region_access(struct fd_region * fd_regions,uint16_t region_read_access,uint16_t region_write_access,unsigned int i)1930 static enum ich_access_protection ich9_handle_region_access(struct fd_region *fd_regions,
1931 							    uint16_t region_read_access,
1932 							    uint16_t region_write_access,
1933 							    unsigned int i)
1934 {
1935 	static const char *const region_names[] = {
1936 		"Flash Descriptor", "BIOS", "Management Engine",
1937 		"Gigabit Ethernet", "Platform Data", "Device Expansion",
1938 		"BIOS2", "unknown", "EC/BMC", "Device Expansion 2",
1939 		"Innovation Engine", "10GbE0", "10GbE1", "unknown", "unknown", "PTT",
1940 	};
1941 	const char *const region_name = i < ARRAY_SIZE(region_names) ? region_names[i] : "unknown";
1942 
1943 	uint32_t base, limit;
1944 	unsigned int rwperms_idx;
1945 	enum ich_access_protection rwperms;
1946 	const int offset = i < 12
1947 		? ICH9_REG_FREG0 + i * 4
1948 		: APL_REG_FREG12 + (i - 12) * 4;
1949 	uint32_t freg = mmio_readl(ich_spibar + offset);
1950 
1951 	base  = ICH_FREG_BASE(freg);
1952 	limit = ICH_FREG_LIMIT(freg);
1953 	if (base > limit || (freg == 0 && i > 0)) {
1954 		/* this FREG is disabled */
1955 		msg_pdbg2("0x%02X: 0x%08"PRIx32" FREG%u: %s region is unused.\n",
1956 			  offset, freg, i, region_name);
1957 		return NO_PROT;
1958 	}
1959 	msg_pdbg("0x%02X: 0x%08"PRIx32" ", offset, freg);
1960 
1961 	if (i < ich_get_defined_region_count()) {
1962 		rwperms_idx = (((region_write_access >> i) & 1) << 1) |
1963 			      (((region_read_access >> i) & 1) << 0);
1964 		rwperms = access_perms_to_protection[rwperms_idx];
1965 	} else if (i == EMBEDDED_CONTROLLER_REGION && ich_generation >= CHIPSET_100_SERIES_SUNRISE_POINT) {
1966 		rwperms = ec_region_rwperms(i);
1967 	} else {
1968 		/* Datasheets might not define all the access bits for regions. We
1969 		   can't rely on the actual descriptor settings either as there
1970 		   are several overrides for them (those by other masters are
1971 		   not even readable by us, *shrug*). */
1972 		msg_pdbg("FREG%u: %s region (0x%08"PRIx32"-0x%08"PRIx32") has unknown permissions.\n",
1973 				i, region_name, base, limit);
1974 		return NO_PROT;
1975 	}
1976 	msg_pinfo("FREG%u: %s region (0x%08"PRIx32"-0x%08"PRIx32") is %s.\n", i,
1977 		  region_name, base, limit, access_names[rwperms]);
1978 
1979 	/* Save region attributes for use by ich_get_region(). */
1980 	fd_regions[i].base = base;
1981 	fd_regions[i].limit = limit;
1982 	fd_regions[i].level = rwperms;
1983 	fd_regions[i].name = region_name;
1984 
1985 	return rwperms;
1986 }
1987 
1988 	/* In contrast to FRAP and the master section of the descriptor the bits
1989 	 * in the PR registers have an inverted meaning. The bits in FRAP
1990 	 * indicate read and write access _grant_. Here they indicate read
1991 	 * and write _protection_ respectively. If both bits are 0 the address
1992 	 * bits are ignored.
1993 	 */
1994 #define ICH_PR_PERMS(pr)	(((~((pr) >> PR_RP_OFF) & 1) << 0) | \
1995 				 ((~((pr) >> PR_WP_OFF) & 1) << 1))
1996 
ich9_handle_pr(const size_t reg_pr0,unsigned int i)1997 static enum ich_access_protection ich9_handle_pr(const size_t reg_pr0, unsigned int i)
1998 {
1999 	uint8_t off = reg_pr0 + (i * 4);
2000 	uint32_t pr = mmio_readl(ich_spibar + off);
2001 	unsigned int rwperms_idx = ICH_PR_PERMS(pr);
2002 	enum ich_access_protection rwperms = access_perms_to_protection[rwperms_idx];
2003 
2004 	/* From 5 on we have GPR registers and start from 0 again. */
2005 	const char *const prefix = i >= 5 ? "G" : "";
2006 	if (i >= 5)
2007 		i -= 5;
2008 
2009 	if (rwperms == NO_PROT) {
2010 		msg_pdbg2("0x%02"PRIX8": 0x%08"PRIx32" (%sPR%u is unused)\n", off, pr, prefix, i);
2011 		return NO_PROT;
2012 	}
2013 
2014 	msg_pdbg("0x%02"PRIX8": 0x%08"PRIx32" ", off, pr);
2015 	msg_pwarn("%sPR%u: Warning: 0x%08"PRIx32"-0x%08"PRIx32" is %s.\n", prefix, i, ICH_FREG_BASE(pr),
2016 		  ICH_FREG_LIMIT(pr), access_names[rwperms]);
2017 
2018 	return rwperms;
2019 }
2020 
2021 /* Set/Clear the read and write protection enable bits of PR register @i
2022  * according to @read_prot and @write_prot. */
ich9_set_pr(const size_t reg_pr0,int i,int read_prot,int write_prot)2023 static void ich9_set_pr(const size_t reg_pr0, int i, int read_prot, int write_prot)
2024 {
2025 	void *addr = ich_spibar + reg_pr0 + (i * 4);
2026 	uint32_t old = mmio_readl(addr);
2027 	uint32_t new;
2028 
2029 	msg_gspew("PR%u is 0x%08"PRIx32"", i, old);
2030 	new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF));
2031 	if (read_prot)
2032 		new |= (1 << PR_RP_OFF);
2033 	if (write_prot)
2034 		new |= (1 << PR_WP_OFF);
2035 	if (old == new) {
2036 		msg_gspew(" already.\n");
2037 		return;
2038 	}
2039 	msg_gspew(", trying to set it to 0x%08"PRIx32" ", new);
2040 	rmmio_writel(new, addr);
2041 	msg_gspew("resulted in 0x%08"PRIx32".\n", mmio_readl(addr));
2042 }
2043 
2044 static const struct spi_master spi_master_ich = {
2045 	.max_data_read	= 64,
2046 	.max_data_write	= 64,
2047 	.command	= ich_spi_send_command,
2048 	.multicommand	= ich_spi_send_multicommand,
2049 	.map_flash_region	= physmap,
2050 	.unmap_flash_region	= physunmap,
2051 	.read		= default_spi_read,
2052 	.write_256	= default_spi_write_256,
2053 	.probe_opcode	= ich_spi_probe_opcode,
2054 };
2055 
2056 static const struct opaque_master opaque_master_ich_hwseq = {
2057 	.max_data_read	= 64,
2058 	.max_data_write	= 64,
2059 	.probe		= ich_hwseq_probe,
2060 	.read		= ich_hwseq_read,
2061 	.write		= ich_hwseq_write,
2062 	.erase		= ich_hwseq_block_erase,
2063 	.read_register	= ich_hwseq_read_status,
2064 	.write_register	= ich_hwseq_write_status,
2065 	.get_region	= ich_get_region,
2066 	.shutdown	= ich_hwseq_shutdown,
2067 };
2068 
init_ich7_spi(void * spibar,enum ich_chipset ich_gen)2069 static int init_ich7_spi(void *spibar, enum ich_chipset ich_gen)
2070 {
2071 	unsigned int i;
2072 
2073 	msg_pdbg("0x00: 0x%04"PRIx16"     (SPIS)\n",	mmio_readw(spibar + 0));
2074 	msg_pdbg("0x02: 0x%04"PRIx16"     (SPIC)\n",	mmio_readw(spibar + 2));
2075 	msg_pdbg("0x04: 0x%08"PRIx32" (SPIA)\n",	mmio_readl(spibar + 4));
2076 
2077 	ichspi_bbar = mmio_readl(spibar + 0x50);
2078 
2079 	msg_pdbg("0x50: 0x%08"PRIx32" (BBAR)\n",	ichspi_bbar);
2080 	msg_pdbg("0x54: 0x%04"PRIx16"     (PREOP)\n",	mmio_readw(spibar + 0x54));
2081 	msg_pdbg("0x56: 0x%04"PRIx16"     (OPTYPE)\n",	mmio_readw(spibar + 0x56));
2082 	msg_pdbg("0x58: 0x%08"PRIx32" (OPMENU)\n",	mmio_readl(spibar + 0x58));
2083 	msg_pdbg("0x5c: 0x%08"PRIx32" (OPMENU+4)\n",	mmio_readl(spibar + 0x5c));
2084 
2085 	for (i = 0; i < 3; i++) {
2086 		int offs;
2087 		offs = 0x60 + (i * 4);
2088 		msg_pdbg("0x%02x: 0x%08"PRIx32" (PBR%u)\n", offs, mmio_readl(spibar + offs), i);
2089 	}
2090 	if (mmio_readw(spibar) & (1 << 15)) {
2091 		msg_pwarn("WARNING: SPI Configuration Lockdown activated.\n");
2092 		ichspi_lock = true;
2093 	}
2094 	ich_init_opcodes(ich_gen);
2095 	ich_set_bbar(0, ich_gen);
2096 	register_spi_master(&spi_master_ich, NULL);
2097 
2098 	return 0;
2099 }
2100 
2101 enum ich_spi_mode {
2102 	ich_auto,
2103 	ich_hwseq,
2104 	ich_swseq
2105 };
2106 
get_ich_spi_mode_param(const struct programmer_cfg * cfg,enum ich_spi_mode * ich_spi_mode)2107 static int get_ich_spi_mode_param(const struct programmer_cfg *cfg, enum ich_spi_mode *ich_spi_mode)
2108 {
2109 	char *const arg = extract_programmer_param_str(cfg, "ich_spi_mode");
2110 	if (!arg) {
2111 		return 0;
2112 	} else if (!strcmp(arg, "hwseq")) {
2113 		*ich_spi_mode = ich_hwseq;
2114 		msg_pspew("user selected hwseq\n");
2115 	} else if (!strcmp(arg, "swseq")) {
2116 		*ich_spi_mode = ich_swseq;
2117 		msg_pspew("user selected swseq\n");
2118 	} else if (!strcmp(arg, "auto")) {
2119 		msg_pspew("user selected auto\n");
2120 		*ich_spi_mode = ich_auto;
2121 	} else if (!strlen(arg)) {
2122 		msg_perr("Missing argument for ich_spi_mode.\n");
2123 		free(arg);
2124 		return ERROR_FLASHROM_FATAL;
2125 	} else {
2126 		msg_perr("Unknown argument for ich_spi_mode: %s\n", arg);
2127 		free(arg);
2128 		return ERROR_FLASHROM_FATAL;
2129 	}
2130 	free(arg);
2131 
2132 	return 0;
2133 }
2134 
init_chipset_properties(struct swseq_data * swseq,struct hwseq_data * hwseq,size_t * num_freg,size_t * num_pr,size_t * reg_pr0,enum ich_chipset ich_gen)2135 static void init_chipset_properties(struct swseq_data *swseq, struct hwseq_data *hwseq,
2136 					size_t *num_freg, size_t *num_pr, size_t *reg_pr0,
2137 					enum ich_chipset ich_gen)
2138 {
2139 	/* Moving registers / bits */
2140 	switch (ich_gen) {
2141 	case CHIPSET_100_SERIES_SUNRISE_POINT:
2142 	case CHIPSET_C620_SERIES_LEWISBURG:
2143 	case CHIPSET_C740_SERIES_EMMITSBURG:
2144 	case CHIPSET_300_SERIES_CANNON_POINT:
2145 	case CHIPSET_400_SERIES_COMET_POINT:
2146 	case CHIPSET_500_SERIES_TIGER_POINT:
2147 	case CHIPSET_600_SERIES_ALDER_POINT:
2148 	case CHIPSET_700_SERIES_RAPTOR_POINT:
2149 	case CHIPSET_APOLLO_LAKE:
2150 	case CHIPSET_GEMINI_LAKE:
2151 	case CHIPSET_JASPER_LAKE:
2152 	case CHIPSET_ELKHART_LAKE:
2153 	case CHIPSET_METEOR_LAKE:
2154 	case CHIPSET_PANTHER_LAKE:
2155 		*num_pr			= 6;	/* Includes GPR0 */
2156 		*reg_pr0		= PCH100_REG_FPR0;
2157 		swseq->reg_ssfsc	= PCH100_REG_SSFSC;
2158 		swseq->reg_preop	= PCH100_REG_PREOP;
2159 		swseq->reg_optype	= PCH100_REG_OPTYPE;
2160 		swseq->reg_opmenu	= PCH100_REG_OPMENU;
2161 		hwseq->addr_mask	= PCH100_FADDR_FLA;
2162 		hwseq->only_4k		= true;
2163 		hwseq->hsfc_fcycle	= PCH100_HSFC_FCYCLE;
2164 		break;
2165 	default:
2166 		*num_pr			= 5;
2167 		*reg_pr0		= ICH9_REG_PR0;
2168 		swseq->reg_ssfsc	= ICH9_REG_SSFS;
2169 		swseq->reg_preop	= ICH9_REG_PREOP;
2170 		swseq->reg_optype	= ICH9_REG_OPTYPE;
2171 		swseq->reg_opmenu	= ICH9_REG_OPMENU;
2172 		hwseq->addr_mask	= ICH9_FADDR_FLA;
2173 		hwseq->only_4k		= false;
2174 		hwseq->hsfc_fcycle	= ICH9_HSFC_FCYCLE;
2175 		break;
2176 	}
2177 
2178 	switch (ich_gen) {
2179 	case CHIPSET_100_SERIES_SUNRISE_POINT:
2180 		*num_freg = 10;
2181 		break;
2182 	case CHIPSET_C620_SERIES_LEWISBURG:
2183 	case CHIPSET_C740_SERIES_EMMITSBURG:
2184 		*num_freg = 12;	/* 12 MMIO regs, but 16 regions in FD spec */
2185 		break;
2186 	case CHIPSET_300_SERIES_CANNON_POINT:
2187 	case CHIPSET_400_SERIES_COMET_POINT:
2188 	case CHIPSET_500_SERIES_TIGER_POINT:
2189 	case CHIPSET_600_SERIES_ALDER_POINT:
2190 	case CHIPSET_700_SERIES_RAPTOR_POINT:
2191 	case CHIPSET_APOLLO_LAKE:
2192 	case CHIPSET_GEMINI_LAKE:
2193 	case CHIPSET_JASPER_LAKE:
2194 	case CHIPSET_ELKHART_LAKE:
2195 	case CHIPSET_METEOR_LAKE:
2196 	case CHIPSET_PANTHER_LAKE:
2197 		*num_freg = 16;
2198 		break;
2199 	default:
2200 		*num_freg = 5;
2201 		break;
2202 	}
2203 }
2204 
init_ich_default(const struct programmer_cfg * cfg,void * spibar,enum ich_chipset ich_gen)2205 static int init_ich_default(const struct programmer_cfg *cfg, void *spibar, enum ich_chipset ich_gen)
2206 {
2207 	unsigned int i;
2208 	uint16_t tmp2;
2209 	uint32_t tmp;
2210 	int ich_spi_rw_restricted = 0;
2211 	bool desc_valid = false;
2212 	struct ich_descriptors desc = { 0 };
2213 	enum ich_spi_mode ich_spi_mode = ich_auto;
2214 	size_t num_freg, num_pr, reg_pr0;
2215 	struct hwseq_data hwseq_data = { 0 };
2216 	init_chipset_properties(&swseq_data, &hwseq_data, &num_freg, &num_pr, &reg_pr0, ich_gen);
2217 
2218 	int ret = get_ich_spi_mode_param(cfg, &ich_spi_mode);
2219 	if (ret)
2220 		return ret;
2221 
2222 	tmp2 = mmio_readw(spibar + ICH9_REG_HSFS);
2223 	msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
2224 	prettyprint_ich9_reg_hsfs(tmp2, ich_gen);
2225 	if (tmp2 & HSFS_FLOCKDN) {
2226 		msg_pinfo("SPI Configuration is locked down.\n");
2227 		ichspi_lock = true;
2228 	}
2229 	if (tmp2 & HSFS_FDV)
2230 		desc_valid = true;
2231 	if (!(tmp2 & HSFS_FDOPSS) && desc_valid)
2232 		msg_pinfo("The Flash Descriptor Override Strap-Pin is set. Restrictions implied by\n"
2233 			  "the Master Section of the flash descriptor are NOT in effect. Please note\n"
2234 			  "that Protected Range (PR) restrictions still apply.\n");
2235 	ich_init_opcodes(ich_gen);
2236 
2237 	if (desc_valid) {
2238 		tmp2 = mmio_readw(spibar + ICH9_REG_HSFC);
2239 		msg_pdbg("0x06: 0x%04"PRIx16" (HSFC)\n", tmp2);
2240 		prettyprint_ich9_reg_hsfc(tmp2, ich_gen);
2241 	}
2242 
2243 	tmp = mmio_readl(spibar + ICH9_REG_FADDR);
2244 	msg_pdbg2("0x08: 0x%08"PRIx32" (FADDR)\n", tmp);
2245 
2246 	switch (ich_gen) {
2247 	case CHIPSET_100_SERIES_SUNRISE_POINT:
2248 	case CHIPSET_C620_SERIES_LEWISBURG:
2249 	case CHIPSET_C740_SERIES_EMMITSBURG:
2250 	case CHIPSET_300_SERIES_CANNON_POINT:
2251 	case CHIPSET_400_SERIES_COMET_POINT:
2252 	case CHIPSET_500_SERIES_TIGER_POINT:
2253 	case CHIPSET_600_SERIES_ALDER_POINT:
2254 	case CHIPSET_700_SERIES_RAPTOR_POINT:
2255 	case CHIPSET_APOLLO_LAKE:
2256 	case CHIPSET_GEMINI_LAKE:
2257 	case CHIPSET_JASPER_LAKE:
2258 	case CHIPSET_ELKHART_LAKE:
2259 	case CHIPSET_METEOR_LAKE:
2260 	case CHIPSET_PANTHER_LAKE:
2261 		tmp = mmio_readl(spibar + PCH100_REG_DLOCK);
2262 		msg_pdbg("0x0c: 0x%08"PRIx32" (DLOCK)\n", tmp);
2263 		prettyprint_pch100_reg_dlock(tmp);
2264 		break;
2265 	default:
2266 		break;
2267 	}
2268 
2269 	if (desc_valid) {
2270 		/* Get the region access data from FRAP/BIOS_BM */
2271 		uint16_t region_read_access, region_write_access;
2272 		ich_get_bios_region_access(&region_read_access, &region_write_access);
2273 
2274 		/* Handle FREGx and region access registers */
2275 		for (i = 0; i < num_freg; i++)
2276 			ich_spi_rw_restricted |= ich9_handle_region_access(hwseq_data.fd_regions,
2277 									   region_read_access,
2278 									   region_write_access, i);
2279 		if (ich_spi_rw_restricted)
2280 			msg_pinfo("Not all flash regions are freely accessible by flashrom. This is "
2281 				  "most likely\ndue to an active ME. Please see "
2282 				  "https://flashrom.org/ME for details.\n");
2283 	}
2284 
2285 	/* Handle PR registers */
2286 	for (i = 0; i < num_pr; i++) {
2287 		/* if not locked down try to disable PR locks first */
2288 		if (!ichspi_lock)
2289 			ich9_set_pr(reg_pr0, i, 0, 0);
2290 		ich_spi_rw_restricted |= ich9_handle_pr(reg_pr0, i);
2291 	}
2292 
2293 	switch (ich_spi_rw_restricted) {
2294 	case WRITE_PROT:
2295 		msg_pwarn("At least some flash regions are write protected. For write operations,\n"
2296 			  "you should use a flash layout and include only writable regions. See\n"
2297 			  "manpage for more details.\n");
2298 		break;
2299 	case READ_PROT:
2300 	case LOCKED:
2301 		msg_pwarn("At least some flash regions are read protected. You have to use a flash\n"
2302 			  "layout and include only accessible regions. For write operations, you'll\n"
2303 			  "additionally need the --noverify-all switch. See manpage for more details.\n");
2304 		break;
2305 	}
2306 
2307 	tmp = mmio_readl(spibar + swseq_data.reg_ssfsc);
2308 	msg_pdbg("0x%zx: 0x%02"PRIx32" (SSFS)\n", swseq_data.reg_ssfsc, tmp & 0xff);
2309 	prettyprint_ich9_reg_ssfs(tmp);
2310 	if (tmp & SSFS_FCERR) {
2311 		msg_pdbg("Clearing SSFS.FCERR\n");
2312 		mmio_writeb(SSFS_FCERR, spibar + swseq_data.reg_ssfsc);
2313 	}
2314 	msg_pdbg("0x%zx: 0x%06"PRIx32" (SSFC)\n", swseq_data.reg_ssfsc + 1, tmp >> 8);
2315 	prettyprint_ich9_reg_ssfc(tmp);
2316 
2317 	msg_pdbg("0x%zx: 0x%04"PRIx16" (PREOP)\n",
2318 		 swseq_data.reg_preop, mmio_readw(spibar + swseq_data.reg_preop));
2319 	msg_pdbg("0x%zx: 0x%04"PRIx16" (OPTYPE)\n",
2320 		 swseq_data.reg_optype, mmio_readw(spibar + swseq_data.reg_optype));
2321 	msg_pdbg("0x%zx: 0x%08"PRIx32" (OPMENU)\n",
2322 		 swseq_data.reg_opmenu, mmio_readl(spibar + swseq_data.reg_opmenu));
2323 	msg_pdbg("0x%zx: 0x%08"PRIx32" (OPMENU+4)\n",
2324 		 swseq_data.reg_opmenu + 4, mmio_readl(spibar + swseq_data.reg_opmenu + 4));
2325 
2326 	if (desc_valid) {
2327 		switch (ich_gen) {
2328 		case CHIPSET_ICH8:
2329 		case CHIPSET_100_SERIES_SUNRISE_POINT:
2330 		case CHIPSET_C620_SERIES_LEWISBURG:
2331 		case CHIPSET_C740_SERIES_EMMITSBURG:
2332 		case CHIPSET_300_SERIES_CANNON_POINT:
2333 		case CHIPSET_400_SERIES_COMET_POINT:
2334 		case CHIPSET_500_SERIES_TIGER_POINT:
2335 		case CHIPSET_600_SERIES_ALDER_POINT:
2336 		case CHIPSET_700_SERIES_RAPTOR_POINT:
2337 		case CHIPSET_APOLLO_LAKE:
2338 		case CHIPSET_GEMINI_LAKE:
2339 		case CHIPSET_JASPER_LAKE:
2340 		case CHIPSET_BAYTRAIL:
2341 		case CHIPSET_ELKHART_LAKE:
2342 		case CHIPSET_METEOR_LAKE:
2343 		case CHIPSET_PANTHER_LAKE:
2344 			break;
2345 		default:
2346 			ichspi_bbar = mmio_readl(spibar + ICH9_REG_BBAR);
2347 			msg_pdbg("0x%x: 0x%08"PRIx32" (BBAR)\n", ICH9_REG_BBAR, ichspi_bbar);
2348 			ich_set_bbar(0, ich_gen);
2349 			break;
2350 		}
2351 
2352 		if (ich_gen == CHIPSET_ICH8) {
2353 			tmp = mmio_readl(spibar + ICH8_REG_VSCC);
2354 			msg_pdbg("0x%x: 0x%08"PRIx32" (VSCC)\n", ICH8_REG_VSCC, tmp);
2355 			msg_pdbg("VSCC: ");
2356 			prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, true);
2357 		} else {
2358 			tmp = mmio_readl(spibar + ICH9_REG_LVSCC);
2359 			msg_pdbg("0x%x: 0x%08"PRIx32" (LVSCC)\n", ICH9_REG_LVSCC, tmp);
2360 			msg_pdbg("LVSCC: ");
2361 			prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, true);
2362 
2363 			tmp = mmio_readl(spibar + ICH9_REG_UVSCC);
2364 			msg_pdbg("0x%x: 0x%08"PRIx32" (UVSCC)\n", ICH9_REG_UVSCC, tmp);
2365 			msg_pdbg("UVSCC: ");
2366 			prettyprint_ich_reg_vscc(tmp, FLASHROM_MSG_DEBUG, false);
2367 		}
2368 
2369 		switch (ich_gen) {
2370 		case CHIPSET_ICH8:
2371 		case CHIPSET_100_SERIES_SUNRISE_POINT:
2372 		case CHIPSET_C620_SERIES_LEWISBURG:
2373 		case CHIPSET_C740_SERIES_EMMITSBURG:
2374 		case CHIPSET_300_SERIES_CANNON_POINT:
2375 		case CHIPSET_400_SERIES_COMET_POINT:
2376 		case CHIPSET_500_SERIES_TIGER_POINT:
2377 		case CHIPSET_600_SERIES_ALDER_POINT:
2378 		case CHIPSET_700_SERIES_RAPTOR_POINT:
2379 		case CHIPSET_APOLLO_LAKE:
2380 		case CHIPSET_GEMINI_LAKE:
2381 		case CHIPSET_JASPER_LAKE:
2382 		case CHIPSET_ELKHART_LAKE:
2383 		case CHIPSET_METEOR_LAKE:
2384 		case CHIPSET_PANTHER_LAKE:
2385 			break;
2386 		default:
2387 			tmp = mmio_readl(spibar + ICH9_REG_FPB);
2388 			msg_pdbg("0x%x: 0x%08"PRIx32" (FPB)\n", ICH9_REG_FPB, tmp);
2389 			break;
2390 		}
2391 
2392 		if (read_ich_descriptors_via_fdo(ich_gen, spibar, &desc) == ICH_RET_OK)
2393 			prettyprint_ich_descriptors(ich_gen, &desc);
2394 
2395 		/* If the descriptor is valid and indicates multiple
2396 		 * flash devices we need to use hwseq to be able to
2397 		 * access the second flash device.
2398 		 */
2399 		if (ich_spi_mode == ich_auto && desc.content.NC != 0) {
2400 			msg_pinfo("Enabling hardware sequencing due to multiple flash chips detected.\n");
2401 			ich_spi_mode = ich_hwseq;
2402 		}
2403 	}
2404 
2405 	if (ich_spi_mode == ich_auto && ichspi_lock &&
2406 	    ich_missing_opcodes()) {
2407 		msg_pinfo("Enabling hardware sequencing because "
2408 			  "some important opcode is locked.\n");
2409 		ich_spi_mode = ich_hwseq;
2410 	}
2411 
2412 	if (ich_spi_mode == ich_auto &&
2413 	    (ich_gen == CHIPSET_100_SERIES_SUNRISE_POINT ||
2414 	     ich_gen == CHIPSET_300_SERIES_CANNON_POINT ||
2415 	     ich_gen == CHIPSET_400_SERIES_COMET_POINT ||
2416 	     ich_gen == CHIPSET_500_SERIES_TIGER_POINT ||
2417 	     ich_gen == CHIPSET_600_SERIES_ALDER_POINT ||
2418 	     ich_gen == CHIPSET_700_SERIES_RAPTOR_POINT ||
2419 	     ich_gen == CHIPSET_C740_SERIES_EMMITSBURG)) {
2420 		msg_pdbg("Enabling hardware sequencing by default for 100+ series PCH.\n");
2421 		ich_spi_mode = ich_hwseq;
2422 	}
2423 
2424 	if (ich_spi_mode == ich_auto &&
2425 	    (ich_gen == CHIPSET_APOLLO_LAKE ||
2426 	     ich_gen == CHIPSET_GEMINI_LAKE ||
2427 	     ich_gen == CHIPSET_JASPER_LAKE ||
2428 	     ich_gen == CHIPSET_ELKHART_LAKE ||
2429 	     ich_gen == CHIPSET_METEOR_LAKE ||
2430 	     ich_gen == CHIPSET_PANTHER_LAKE)) {
2431 		msg_pdbg("Enabling hardware sequencing by default for Apollo/Gemini/Jasper/Elkhart/Meteor/Panther Lake.\n");
2432 		ich_spi_mode = ich_hwseq;
2433 	}
2434 
2435 	if (ich_spi_mode == ich_hwseq) {
2436 		if (!desc_valid) {
2437 			msg_perr("Hardware sequencing was requested "
2438 				 "but the flash descriptor is not valid. Aborting.\n");
2439 			return ERROR_FLASHROM_FATAL;
2440 		}
2441 
2442 		int tmpi = getFCBA_component_density(ich_gen, &desc, 0);
2443 		if (tmpi < 0) {
2444 			msg_perr("Could not determine density of flash component %d.\n", 0);
2445 			return ERROR_FLASHROM_FATAL;
2446 		}
2447 		hwseq_data.size_comp0 = tmpi;
2448 
2449 		tmpi = getFCBA_component_density(ich_gen, &desc, 1);
2450 		if (tmpi < 0) {
2451 			msg_perr("Could not determine density of flash component %d.\n", 1);
2452 			return ERROR_FLASHROM_FATAL;
2453 		}
2454 		hwseq_data.size_comp1 = tmpi;
2455 
2456 		struct hwseq_data *opaque_hwseq_data = calloc(1, sizeof(struct hwseq_data));
2457 		if (!opaque_hwseq_data)
2458 			return ERROR_FLASHROM_FATAL;
2459 		memcpy(opaque_hwseq_data, &hwseq_data, sizeof(*opaque_hwseq_data));
2460 		register_opaque_master(&opaque_master_ich_hwseq, opaque_hwseq_data);
2461 	} else {
2462 		register_spi_master(&spi_master_ich, NULL);
2463 	}
2464 
2465 	return 0;
2466 }
2467 
ich_init_spi(const struct programmer_cfg * cfg,void * spibar,enum ich_chipset ich_gen)2468 int ich_init_spi(const struct programmer_cfg *cfg, void *spibar, enum ich_chipset ich_gen)
2469 {
2470 	ich_generation = ich_gen;
2471 	ich_spibar = spibar;
2472 
2473 	switch (ich_gen) {
2474 	case CHIPSET_ICH7:
2475 	case CHIPSET_TUNNEL_CREEK:
2476 	case CHIPSET_CENTERTON:
2477 		return init_ich7_spi(spibar, ich_gen);
2478 	case CHIPSET_ICH8:
2479 	default:	/* Future version might behave the same */
2480 		return init_ich_default(cfg, spibar, ich_gen);
2481 	}
2482 }
2483 
2484 static const struct spi_master spi_master_via = {
2485 	.max_data_read	= 16,
2486 	.max_data_write	= 16,
2487 	.command	= ich_spi_send_command,
2488 	.multicommand	= ich_spi_send_multicommand,
2489 	.map_flash_region	= physmap,
2490 	.unmap_flash_region	= physunmap,
2491 	.read		= default_spi_read,
2492 	.write_256	= default_spi_write_256,
2493 	.probe_opcode	= ich_spi_probe_opcode,
2494 };
2495 
via_init_spi(uint32_t mmio_base)2496 int via_init_spi(uint32_t mmio_base)
2497 {
2498 	int i;
2499 
2500 	ich_spibar = rphysmap("VIA SPI MMIO registers", mmio_base, 0x70);
2501 	if (ich_spibar == ERROR_PTR)
2502 		return ERROR_FLASHROM_FATAL;
2503 	/* Do we really need no write enable? Like the LPC one at D17F0 0x40 */
2504 
2505 	/* Not sure if it speaks all these bus protocols. */
2506 	internal_buses_supported &= BUS_LPC | BUS_FWH;
2507 	ich_generation = CHIPSET_ICH7;
2508 	register_spi_master(&spi_master_via, NULL);
2509 
2510 	msg_pdbg("0x00: 0x%04"PRIx16" (SPIS)\n",	mmio_readw(ich_spibar + 0));
2511 	msg_pdbg("0x02: 0x%04"PRIx16" (SPIC)\n",	mmio_readw(ich_spibar + 2));
2512 	msg_pdbg("0x04: 0x%08"PRIx32" (SPIA)\n",	mmio_readl(ich_spibar + 4));
2513 	for (i = 0; i < 2; i++) {
2514 		int offs;
2515 		offs = 8 + (i * 8);
2516 		msg_pdbg("0x%02x: 0x%08"PRIx32" (SPID%d)\n", offs, mmio_readl(ich_spibar + offs), i);
2517 		msg_pdbg("0x%02x: 0x%08"PRIx32" (SPID%d+4)\n", offs + 4,
2518 			 mmio_readl(ich_spibar + offs + 4), i);
2519 	}
2520 	ichspi_bbar = mmio_readl(ich_spibar + 0x50);
2521 
2522 	msg_pdbg("0x50: 0x%08"PRIx32" (BBAR)\n",	ichspi_bbar);
2523 	msg_pdbg("0x54: 0x%04"PRIx16" (PREOP)\n",	mmio_readw(ich_spibar + 0x54));
2524 	msg_pdbg("0x56: 0x%04"PRIx16" (OPTYPE)\n",	mmio_readw(ich_spibar + 0x56));
2525 	msg_pdbg("0x58: 0x%08"PRIx32" (OPMENU)\n",	mmio_readl(ich_spibar + 0x58));
2526 	msg_pdbg("0x5c: 0x%08"PRIx32" (OPMENU+4)\n",	mmio_readl(ich_spibar + 0x5c));
2527 	for (i = 0; i < 3; i++) {
2528 		int offs;
2529 		offs = 0x60 + (i * 4);
2530 		msg_pdbg("0x%02x: 0x%08"PRIx32" (PBR%d)\n", offs, mmio_readl(ich_spibar + offs), i);
2531 	}
2532 	msg_pdbg("0x6c: 0x%04x     (CLOCK/DEBUG)\n", mmio_readw(ich_spibar + 0x6c));
2533 	if (mmio_readw(ich_spibar) & (1 << 15)) {
2534 		msg_pwarn("Warning: SPI Configuration Lockdown activated.\n");
2535 		ichspi_lock = true;
2536 	}
2537 
2538 	ich_set_bbar(0, ich_generation);
2539 	ich_init_opcodes(ich_generation);
2540 
2541 	return 0;
2542 }
2543