blob: 77d236a5cf376d9253a2e1040227363bff51dcfe [file] [log] [blame]
Dominik Geyerb46acba2008-05-16 12:55:55 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2008 Stefan Wildemann <stefan.wildemann@kontron.com>
5 * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com>
6 * Copyright (C) 2008 Dominik Geyer <dominik.geyer@kontron.com>
Stefan Reinauera9424d52008-06-27 16:28:34 +00007 * Copyright (C) 2008 coresystems GmbH <info@coresystems.de>
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00008 * Copyright (C) 2009 Carl-Daniel Hailfinger
Dominik Geyerb46acba2008-05-16 12:55:55 +00009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Dominik Geyerb46acba2008-05-16 12:55:55 +000023 */
24
25/*
26 * This module is designed for supporting the devices
27 * ST M25P40
28 * ST M25P80
29 * ST M25P16
30 * ST M25P32 already tested
31 * ST M25P64
32 * AT 25DF321 already tested
33 *
34 */
35
Dominik Geyerb46acba2008-05-16 12:55:55 +000036#include <string.h>
Dominik Geyerb46acba2008-05-16 12:55:55 +000037#include "flash.h"
38#include "spi.h"
39
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +000040/* Change this to #define if you want lowlevel debugging of commands
41 * sent to the ICH/VIA SPI controller.
42 */
43#undef COMM_DEBUG
44
45#ifdef COMM_DEBUG
46#define msg_comm_debug printf_debug
47#else
48#define msg_comm_debug(...) do {} while (0)
49#endif
50
Stefan Reinauera9424d52008-06-27 16:28:34 +000051/* ICH9 controller register definition */
52#define ICH9_REG_FADDR 0x08 /* 32 Bits */
53#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
54
55#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000056#define SSFS_SCIP 0x00000001
57#define SSFS_CDS 0x00000004
58#define SSFS_FCERR 0x00000008
59#define SSFS_AEL 0x00000010
Stefan Reinauera9424d52008-06-27 16:28:34 +000060
61#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000062#define SSFC_SCGO 0x00000200
63#define SSFC_ACS 0x00000400
64#define SSFC_SPOP 0x00000800
65#define SSFC_COP 0x00001000
66#define SSFC_DBC 0x00010000
67#define SSFC_DS 0x00400000
68#define SSFC_SME 0x00800000
69#define SSFC_SCF 0x01000000
70#define SSFC_SCF_20MHZ 0x00000000
71#define SSFC_SCF_33MHZ 0x01000000
Stefan Reinauera9424d52008-06-27 16:28:34 +000072
73#define ICH9_REG_PREOP 0x94 /* 16 Bits */
74#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
75#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000076
77// ICH9R SPI commands
78#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
79#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
80#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
81#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
82
Stefan Reinauera9424d52008-06-27 16:28:34 +000083// ICH7 registers
84#define ICH7_REG_SPIS 0x00 /* 16 Bits */
85#define SPIS_SCIP 0x00000001
86#define SPIS_CDS 0x00000004
87#define SPIS_FCERR 0x00000008
88
Rudolf Marek3fdbccf2008-06-30 21:38:30 +000089/* VIA SPI is compatible with ICH7, but maxdata
90 to transfer is 16 bytes.
91
92 DATA byte count on ICH7 is 8:13, on VIA 8:11
93
94 bit 12 is port select CS0 CS1
95 bit 13 is FAST READ enable
96 bit 7 is used with fast read and one shot controls CS de-assert?
97*/
98
Stefan Reinauera9424d52008-06-27 16:28:34 +000099#define ICH7_REG_SPIC 0x02 /* 16 Bits */
100#define SPIC_SCGO 0x0002
101#define SPIC_ACS 0x0004
102#define SPIC_SPOP 0x0008
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000103#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000104
105#define ICH7_REG_SPIA 0x04 /* 32 Bits */
106#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
107#define ICH7_REG_PREOP 0x54 /* 16 Bits */
108#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
109#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
110
FENG yu ningc05a2952008-12-08 18:16:58 +0000111/* ICH SPI configuration lock-down. May be set during chipset enabling. */
112int ichspi_lock = 0;
113
Dominik Geyerb46acba2008-05-16 12:55:55 +0000114typedef struct _OPCODE {
115 uint8_t opcode; //This commands spi opcode
116 uint8_t spi_type; //This commands spi type
117 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
118} OPCODE;
119
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000120/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000121 * Preop 1: Write Enable
122 * Preop 2: Write Status register enable
123 *
124 * OP 0: Write address
125 * OP 1: Read Address
126 * OP 2: ERASE block
127 * OP 3: Read Status register
128 * OP 4: Read ID
129 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000130 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000131 * OP 7: Chip erase
132 */
133typedef struct _OPCODES {
134 uint8_t preop[2];
135 OPCODE opcode[8];
136} OPCODES;
137
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000138static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000139
140/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000141static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000142{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000143 return mmio_readl(spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000144}
145
Uwe Hermann09e04f72009-05-16 22:36:00 +0000146static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000147{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000148 return mmio_readw(spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000149}
150
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000151#define REGWRITE32(X,Y) mmio_writel(Y, spibar+X)
152#define REGWRITE16(X,Y) mmio_writew(Y, spibar+X)
153#define REGWRITE8(X,Y) mmio_writeb(Y, spibar+X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000154
Dominik Geyerb46acba2008-05-16 12:55:55 +0000155/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000156static int find_opcode(OPCODES *op, uint8_t opcode);
157static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000158static int generate_opcodes(OPCODES * op);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000159static int program_opcodes(OPCODES * op);
Stefan Reinauer43119562008-11-02 19:51:50 +0000160static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000161 uint8_t datalength, uint8_t * data);
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000162static int ich_spi_write_page(struct flashchip *flash, uint8_t * bytes,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000163 int offset, int maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000164
FENG yu ningf041e9b2008-12-15 02:32:11 +0000165/* for pairing opcodes with their required preop */
166struct preop_opcode_pair {
167 uint8_t preop;
168 uint8_t opcode;
169};
170
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000171/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000172struct preop_opcode_pair pops[] = {
173 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
174 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
175 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
176 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
177 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
178 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000179 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
180 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000181 {JEDEC_EWSR, JEDEC_WRSR},
182 {0,}
183};
184
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000185/* Reasonable default configuration. Needs ad-hoc modifications if we
186 * encounter unlisted opcodes. Fun.
187 */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000188OPCODES O_ST_M25P = {
189 {
190 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000191 JEDEC_EWSR,
192 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000193 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000194 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000195 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000196 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000197 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000198 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000199 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000200 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000201 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
202 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000203};
204
FENG yu ningc05a2952008-12-08 18:16:58 +0000205OPCODES O_EXISTING = {};
206
Uwe Hermann09e04f72009-05-16 22:36:00 +0000207static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000208{
209 int a;
210
211 for (a = 0; a < 8; a++) {
212 if (op->opcode[a].opcode == opcode)
213 return a;
214 }
215
216 return -1;
217}
218
Uwe Hermann09e04f72009-05-16 22:36:00 +0000219static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000220{
221 int a;
222
223 for (a = 0; a < 2; a++) {
224 if (op->preop[a] == preop)
225 return a;
226 }
227
228 return -1;
229}
230
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000231/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000232static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000233{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000234 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000235 uint16_t preop, optype;
236 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000237
238 if (op == NULL) {
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000239 printf_debug("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000240 return -1;
241 }
242
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000243 switch (spi_controller) {
244 case SPI_CONTROLLER_ICH7:
245 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000246 preop = REGREAD16(ICH7_REG_PREOP);
247 optype = REGREAD16(ICH7_REG_OPTYPE);
248 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
249 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
250 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000251 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000252 preop = REGREAD16(ICH9_REG_PREOP);
253 optype = REGREAD16(ICH9_REG_OPTYPE);
254 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
255 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
256 break;
257 default:
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000258 printf_debug("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000259 return -1;
260 }
261
262 op->preop[0] = (uint8_t) preop;
263 op->preop[1] = (uint8_t) (preop >> 8);
264
265 for (a = 0; a < 8; a++) {
266 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
267 optype >>= 2;
268 }
269
270 for (a = 0; a < 4; a++) {
271 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
272 opmenu[0] >>= 8;
273 }
274
275 for (a = 4; a < 8; a++) {
276 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
277 opmenu[1] >>= 8;
278 }
279
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000280 /* No preopcodes used by default. */
281 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000282 op->opcode[a].atomic = 0;
283
FENG yu ningc05a2952008-12-08 18:16:58 +0000284 return 0;
285}
286
Dominik Geyerb46acba2008-05-16 12:55:55 +0000287int program_opcodes(OPCODES * op)
288{
289 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000290 uint16_t preop, optype;
291 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000292
293 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000294 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000295 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000296 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000297 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000298
Stefan Reinauera9424d52008-06-27 16:28:34 +0000299 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000300 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000301 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000302 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000303 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000304
Stefan Reinauera9424d52008-06-27 16:28:34 +0000305 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000306 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000307 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000308 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000309 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000310
Dominik Geyerb46acba2008-05-16 12:55:55 +0000311 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000312 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000313 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000314 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000315 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000316
Peter Stuge016d4e12009-01-15 02:13:18 +0000317 printf_debug("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000318 switch (spi_controller) {
319 case SPI_CONTROLLER_ICH7:
320 case SPI_CONTROLLER_VIA:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000321 REGWRITE16(ICH7_REG_PREOP, preop);
322 REGWRITE16(ICH7_REG_OPTYPE, optype);
323 REGWRITE32(ICH7_REG_OPMENU, opmenu[0]);
324 REGWRITE32(ICH7_REG_OPMENU + 4, opmenu[1]);
325 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000326 case SPI_CONTROLLER_ICH9:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000327 REGWRITE16(ICH9_REG_PREOP, preop);
328 REGWRITE16(ICH9_REG_OPTYPE, optype);
329 REGWRITE32(ICH9_REG_OPMENU, opmenu[0]);
330 REGWRITE32(ICH9_REG_OPMENU + 4, opmenu[1]);
331 break;
332 default:
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000333 printf_debug("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000334 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000335 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000336
337 return 0;
338}
339
FENG yu ningf041e9b2008-12-15 02:32:11 +0000340/* This function generates OPCODES from or programs OPCODES to ICH according to
341 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000342 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000343 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000344 */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000345int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000346{
347 int rc = 0;
348 OPCODES *curopcodes_done;
349
350 if (curopcodes)
351 return 0;
352
353 if (ichspi_lock) {
354 printf_debug("Generating OPCODES... ");
355 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000356 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000357 } else {
358 printf_debug("Programming OPCODES... ");
359 curopcodes_done = &O_ST_M25P;
360 rc = program_opcodes(curopcodes_done);
361 }
362
363 if (rc) {
364 curopcodes = NULL;
365 printf_debug("failed\n");
366 return 1;
367 } else {
368 curopcodes = curopcodes_done;
369 printf_debug("done\n");
370 return 0;
371 }
372}
373
Stefan Reinauer43119562008-11-02 19:51:50 +0000374static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000375 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000376{
377 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000378 int timeout;
Peter Stuge7e2c0792008-06-29 01:30:41 +0000379 uint32_t temp32 = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000380 uint16_t temp16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000381 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000382 uint64_t opmenu;
383 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000384
385 /* Is it a write command? */
386 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
387 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
388 write_cmd = 1;
389 }
390
391 /* Programm Offset in Flash into FADDR */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000392 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000393
394 /* Program data into FDATA0 to N */
395 if (write_cmd && (datalength != 0)) {
396 temp32 = 0;
397 for (a = 0; a < datalength; a++) {
398 if ((a % 4) == 0) {
399 temp32 = 0;
400 }
401
402 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
403
404 if ((a % 4) == 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000405 REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)),
406 temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000407 }
408 }
409 if (((a - 1) % 4) != 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000410 REGWRITE32(ICH7_REG_SPID0 +
411 ((a - 1) - ((a - 1) % 4)), temp32);
412 }
413
414 }
415
416 /* Assemble SPIS */
417 temp16 = 0;
418 /* clear error status registers */
419 temp16 |= (SPIS_CDS + SPIS_FCERR);
420 REGWRITE16(ICH7_REG_SPIS, temp16);
421
422 /* Assemble SPIC */
423 temp16 = 0;
424
425 if (datalength != 0) {
426 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000427 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000428 }
429
430 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000431 opmenu = REGREAD32(ICH7_REG_OPMENU);
432 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
433
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000434 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
435 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000436 break;
437 }
438 opmenu >>= 8;
439 }
440 if (opcode_index == 8) {
441 printf_debug("Opcode %x not found.\n", op.opcode);
442 return 1;
443 }
444 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000445
446 /* Handle Atomic */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000447 switch (op.atomic) {
448 case 2:
449 /* Select second preop. */
450 temp16 |= SPIC_SPOP;
451 /* And fall through. */
452 case 1:
453 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000454 temp16 |= SPIC_ACS;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000455 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000456 }
457
458 /* Start */
459 temp16 |= SPIC_SCGO;
460
461 /* write it */
462 REGWRITE16(ICH7_REG_SPIC, temp16);
463
464 /* wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000465 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauera9424d52008-06-27 16:28:34 +0000466 while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000467 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000468 }
469 if (!timeout) {
470 printf_debug("timeout\n");
471 }
472
473 if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) {
474 printf_debug("Transaction error!\n");
475 return 1;
476 }
477
478 if ((!write_cmd) && (datalength != 0)) {
479 for (a = 0; a < datalength; a++) {
480 if ((a % 4) == 0) {
481 temp32 = REGREAD32(ICH7_REG_SPID0 + (a));
482 }
483
484 data[a] =
485 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
486 >> ((a % 4) * 8);
487 }
488 }
489
490 return 0;
491}
492
Stefan Reinauer43119562008-11-02 19:51:50 +0000493static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000494 uint8_t datalength, uint8_t * data)
495{
496 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000497 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000498 uint32_t temp32;
499 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000500 uint64_t opmenu;
501 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000502
503 /* Is it a write command? */
504 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
505 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
506 write_cmd = 1;
507 }
508
509 /* Programm Offset in Flash into FADDR */
510 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
511
512 /* Program data into FDATA0 to N */
513 if (write_cmd && (datalength != 0)) {
514 temp32 = 0;
515 for (a = 0; a < datalength; a++) {
516 if ((a % 4) == 0) {
517 temp32 = 0;
518 }
519
520 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
521
522 if ((a % 4) == 3) {
523 REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
524 temp32);
525 }
526 }
527 if (((a - 1) % 4) != 3) {
528 REGWRITE32(ICH9_REG_FDATA0 +
529 ((a - 1) - ((a - 1) % 4)), temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000530 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000531 }
532
533 /* Assemble SSFS + SSFC */
534 temp32 = 0;
535
536 /* clear error status registers */
537 temp32 |= (SSFS_CDS + SSFS_FCERR);
538 /* USE 20 MhZ */
539 temp32 |= SSFC_SCF_20MHZ;
540
541 if (datalength != 0) {
542 uint32_t datatemp;
543 temp32 |= SSFC_DS;
544 datatemp = ((uint32_t) ((datalength - 1) & 0x3f)) << (8 + 8);
545 temp32 |= datatemp;
546 }
547
548 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000549 opmenu = REGREAD32(ICH9_REG_OPMENU);
550 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
551
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000552 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
553 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000554 break;
555 }
556 opmenu >>= 8;
557 }
558 if (opcode_index == 8) {
559 printf_debug("Opcode %x not found.\n", op.opcode);
560 return 1;
561 }
562 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000563
564 /* Handle Atomic */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000565 switch (op.atomic) {
566 case 2:
567 /* Select second preop. */
568 temp32 |= SSFC_SPOP;
569 /* And fall through. */
570 case 1:
571 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000572 temp32 |= SSFC_ACS;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000573 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000574 }
575
576 /* Start */
577 temp32 |= SSFC_SCGO;
578
579 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000580 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000581
582 /*wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000583 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000584 while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000585 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000586 }
587 if (!timeout) {
588 printf_debug("timeout\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000589 }
590
Stefan Reinauera9424d52008-06-27 16:28:34 +0000591 if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000592 printf_debug("Transaction error!\n");
593 return 1;
594 }
595
596 if ((!write_cmd) && (datalength != 0)) {
597 for (a = 0; a < datalength; a++) {
598 if ((a % 4) == 0) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000599 temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
Dominik Geyerb46acba2008-05-16 12:55:55 +0000600 }
601
602 data[a] =
Stefan Reinauera9424d52008-06-27 16:28:34 +0000603 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
604 >> ((a % 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000605 }
606 }
607
608 return 0;
609}
610
Stefan Reinauer43119562008-11-02 19:51:50 +0000611static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000612 uint8_t datalength, uint8_t * data)
613{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000614 switch (spi_controller) {
615 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000616 if (datalength > 16) {
617 fprintf(stderr, "%s: Internal command size error for "
618 "opcode 0x%02x, got datalength=%i, want <=16\n",
619 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000620 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000621 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000622 return ich7_run_opcode(op, offset, datalength, data, 16);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000623 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000624 if (datalength > 64) {
625 fprintf(stderr, "%s: Internal command size error for "
626 "opcode 0x%02x, got datalength=%i, want <=16\n",
627 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000628 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000629 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000630 return ich7_run_opcode(op, offset, datalength, data, 64);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000631 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000632 if (datalength > 64) {
633 fprintf(stderr, "%s: Internal command size error for "
634 "opcode 0x%02x, got datalength=%i, want <=16\n",
635 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000636 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000637 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000638 return ich9_run_opcode(op, offset, datalength, data);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000639 default:
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000640 printf_debug("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000641 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000642
643 /* If we ever get here, something really weird happened */
644 return -1;
645}
646
Dominik Geyerb46acba2008-05-16 12:55:55 +0000647static int ich_spi_write_page(struct flashchip *flash, uint8_t * bytes,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000648 int offset, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000649{
650 int page_size = flash->page_size;
651 uint32_t remaining = page_size;
Carl-Daniel Hailfingerd1680572009-06-17 10:13:42 +0000652 int towrite;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000653
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000654 msg_comm_debug("ich_spi_write_page: offset=%d, number=%d, buf=%p\n",
Stefan Reinauera9424d52008-06-27 16:28:34 +0000655 offset, page_size, bytes);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000656
Carl-Daniel Hailfingerd1680572009-06-17 10:13:42 +0000657 for (; remaining > 0; remaining -= towrite) {
658 towrite = min(remaining, maxdata);
659 if (spi_nbyte_program(offset + (page_size - remaining),
660 &bytes[page_size - remaining], towrite)) {
661 printf_debug("Error writing");
662 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000663 }
664 }
665
666 return 0;
667}
668
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000669int ich_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000670{
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000671 int maxdata = 64;
672
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000673 if (spi_controller == SPI_CONTROLLER_VIA)
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000674 maxdata = 16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000675
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000676 return spi_read_chunked(flash, buf, start, len, maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000677}
678
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000679int ich_spi_write_256(struct flashchip *flash, uint8_t * buf)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000680{
681 int i, j, rc = 0;
682 int total_size = flash->total_size * 1024;
683 int page_size = flash->page_size;
684 int erase_size = 64 * 1024;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000685 int maxdata = 64;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000686
687 spi_disable_blockprotect();
Carl-Daniel Hailfinger96123032009-11-25 02:07:30 +0000688 /* Erase first */
689 printf("Erasing flash before programming... ");
690 if (erase_flash(flash)) {
691 fprintf(stderr, "ERASE FAILED!\n");
692 return -1;
693 }
694 printf("done.\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000695
696 printf("Programming page: \n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000697 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000698 if (spi_controller == SPI_CONTROLLER_VIA)
Peter Stuge6a214162008-07-07 05:14:06 +0000699 maxdata = 16;
700
Dominik Geyerb46acba2008-05-16 12:55:55 +0000701 for (j = 0; j < erase_size / page_size; j++) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000702 ich_spi_write_page(flash,
703 (void *)(buf + (i * erase_size) + (j * page_size)),
704 (i * erase_size) + (j * page_size), maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000705 }
706 }
707
708 printf("\n");
709
710 return rc;
711}
712
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000713int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000714 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000715{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000716 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000717 int opcode_index = -1;
718 const unsigned char cmd = *writearr;
719 OPCODE *opcode;
720 uint32_t addr = 0;
721 uint8_t *data;
722 int count;
723
Dominik Geyerb46acba2008-05-16 12:55:55 +0000724 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000725 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000726 if (opcode_index == -1) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000727 /* FIXME: Reprogram opcodes if possible. Autodetect type of
728 * opcode by checking readcnt/writecnt.
729 */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000730 printf_debug("Invalid OPCODE 0x%02x\n", cmd);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000731 return SPI_INVALID_OPCODE;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000732 }
733
734 opcode = &(curopcodes->opcode[opcode_index]);
735
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000736 /* The following valid writecnt/readcnt combinations exist:
737 * writecnt = 4, readcnt >= 0
738 * writecnt = 1, readcnt >= 0
739 * writecnt >= 4, readcnt = 0
740 * writecnt >= 1, readcnt = 0
741 * writecnt >= 1 is guaranteed for all commands.
742 */
743 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
744 (writecnt != 4)) {
745 fprintf(stderr, "%s: Internal command size error for opcode "
746 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
747 writecnt);
748 return SPI_INVALID_LENGTH;
749 }
750 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
751 (writecnt != 1)) {
752 fprintf(stderr, "%s: Internal command size error for opcode "
753 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
754 writecnt);
755 return SPI_INVALID_LENGTH;
756 }
757 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
758 (writecnt < 4)) {
759 fprintf(stderr, "%s: Internal command size error for opcode "
760 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
761 writecnt);
762 return SPI_INVALID_LENGTH;
763 }
764 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
765 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
766 (readcnt)) {
767 fprintf(stderr, "%s: Internal command size error for opcode "
768 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
769 readcnt);
770 return SPI_INVALID_LENGTH;
771 }
772
Dominik Geyerb46acba2008-05-16 12:55:55 +0000773 /* if opcode-type requires an address */
774 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
775 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000776 addr = (writearr[1] << 16) |
777 (writearr[2] << 8) | (writearr[3] << 0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000778 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000779
Dominik Geyerb46acba2008-05-16 12:55:55 +0000780 /* translate read/write array/count */
781 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000782 data = (uint8_t *) (writearr + 1);
783 count = writecnt - 1;
784 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
785 data = (uint8_t *) (writearr + 4);
786 count = writecnt - 4;
787 } else {
788 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000789 count = readcnt;
790 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000791
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000792 result = run_opcode(*opcode, addr, count, data);
793 if (result) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000794 printf_debug("run OPCODE 0x%02x failed\n", opcode->opcode);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000795 }
796
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000797 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000798}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000799
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000800int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000801{
802 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000803 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000804 int oppos, preoppos;
805 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000806 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000807 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000808 preoppos = find_preop(curopcodes, cmds->writearr[0]);
809 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000810 if ((oppos == -1) && (preoppos != -1)) {
811 /* Current command is listed as preopcode in
812 * ICH struct OPCODES, but next command is not
813 * listed as opcode in that struct.
814 * Check for command sanity, then
815 * try to reprogram the ICH opcode list.
816 */
817 if (find_preop(curopcodes,
818 (cmds + 1)->writearr[0]) != -1) {
819 fprintf(stderr, "%s: Two subsequent "
820 "preopcodes 0x%02x and 0x%02x, "
821 "ignoring the first.\n",
822 __func__, cmds->writearr[0],
823 (cmds + 1)->writearr[0]);
824 continue;
825 }
826 /* If the chipset is locked down, we'll fail
827 * during execution of the next command anyway.
828 * No need to bother with fixups.
829 */
830 if (!ichspi_lock) {
831 printf_debug("%s: FIXME: Add on-the-fly"
832 " reprogramming of the "
833 "chipset opcode list.\n",
834 __func__);
835 /* FIXME: Reprogram opcode menu.
836 * Find a less-useful opcode, replace it
837 * with the wanted opcode, detect optype
838 * and reprogram the opcode menu.
839 * Update oppos so the next if-statement
840 * can do something useful.
841 */
842 //curopcodes.opcode[lessusefulindex] = (cmds + 1)->writearr[0]);
843 //update_optypes(curopcodes);
844 //program_opcodes(curopcodes);
845 //oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
846 continue;
847 }
848 }
849 if ((oppos != -1) && (preoppos != -1)) {
850 /* Current command is listed as preopcode in
851 * ICH struct OPCODES and next command is listed
852 * as opcode in that struct. Match them up.
853 */
854 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000855 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000856 }
857 /* If none of the above if-statements about oppos or
858 * preoppos matched, this is a normal opcode.
859 */
860 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000861 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
862 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000863 /* Reset the type of all opcodes to non-atomic. */
864 for (i = 0; i < 8; i++)
865 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000866 }
867 return ret;
868}