blob: 9ab61ffb0e685604f3b9be3560d7ccae4265d019 [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"
Sean Nelson14ba6682010-02-26 05:48:29 +000038#include "chipdrivers.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000039#include "spi.h"
40
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +000041/* Change this to #define if you want lowlevel debugging of commands
42 * sent to the ICH/VIA SPI controller.
43 */
44#undef COMM_DEBUG
45
46#ifdef COMM_DEBUG
47#define msg_comm_debug printf_debug
48#else
49#define msg_comm_debug(...) do {} while (0)
50#endif
51
Stefan Reinauera9424d52008-06-27 16:28:34 +000052/* ICH9 controller register definition */
53#define ICH9_REG_FADDR 0x08 /* 32 Bits */
54#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
55
56#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000057#define SSFS_SCIP 0x00000001
58#define SSFS_CDS 0x00000004
59#define SSFS_FCERR 0x00000008
60#define SSFS_AEL 0x00000010
Stefan Reinauera9424d52008-06-27 16:28:34 +000061
62#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000063#define SSFC_SCGO 0x00000200
64#define SSFC_ACS 0x00000400
65#define SSFC_SPOP 0x00000800
66#define SSFC_COP 0x00001000
67#define SSFC_DBC 0x00010000
68#define SSFC_DS 0x00400000
69#define SSFC_SME 0x00800000
70#define SSFC_SCF 0x01000000
71#define SSFC_SCF_20MHZ 0x00000000
72#define SSFC_SCF_33MHZ 0x01000000
Stefan Reinauera9424d52008-06-27 16:28:34 +000073
74#define ICH9_REG_PREOP 0x94 /* 16 Bits */
75#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
76#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000077
78// ICH9R SPI commands
79#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
80#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
81#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
82#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
83
Stefan Reinauera9424d52008-06-27 16:28:34 +000084// ICH7 registers
85#define ICH7_REG_SPIS 0x00 /* 16 Bits */
86#define SPIS_SCIP 0x00000001
87#define SPIS_CDS 0x00000004
88#define SPIS_FCERR 0x00000008
89
Rudolf Marek3fdbccf2008-06-30 21:38:30 +000090/* VIA SPI is compatible with ICH7, but maxdata
91 to transfer is 16 bytes.
92
93 DATA byte count on ICH7 is 8:13, on VIA 8:11
94
95 bit 12 is port select CS0 CS1
96 bit 13 is FAST READ enable
97 bit 7 is used with fast read and one shot controls CS de-assert?
98*/
99
Stefan Reinauera9424d52008-06-27 16:28:34 +0000100#define ICH7_REG_SPIC 0x02 /* 16 Bits */
101#define SPIC_SCGO 0x0002
102#define SPIC_ACS 0x0004
103#define SPIC_SPOP 0x0008
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000104#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000105
106#define ICH7_REG_SPIA 0x04 /* 32 Bits */
107#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
108#define ICH7_REG_PREOP 0x54 /* 16 Bits */
109#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
110#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
111
FENG yu ningc05a2952008-12-08 18:16:58 +0000112/* ICH SPI configuration lock-down. May be set during chipset enabling. */
113int ichspi_lock = 0;
114
Dominik Geyerb46acba2008-05-16 12:55:55 +0000115typedef struct _OPCODE {
116 uint8_t opcode; //This commands spi opcode
117 uint8_t spi_type; //This commands spi type
118 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
119} OPCODE;
120
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000121/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000122 * Preop 1: Write Enable
123 * Preop 2: Write Status register enable
124 *
125 * OP 0: Write address
126 * OP 1: Read Address
127 * OP 2: ERASE block
128 * OP 3: Read Status register
129 * OP 4: Read ID
130 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000131 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000132 * OP 7: Chip erase
133 */
134typedef struct _OPCODES {
135 uint8_t preop[2];
136 OPCODE opcode[8];
137} OPCODES;
138
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000139static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000140
141/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000142static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000143{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000144 return mmio_readl(spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000145}
146
Uwe Hermann09e04f72009-05-16 22:36:00 +0000147static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000148{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000149 return mmio_readw(spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000150}
151
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000152#define REGWRITE32(X,Y) mmio_writel(Y, spibar+X)
153#define REGWRITE16(X,Y) mmio_writew(Y, spibar+X)
154#define REGWRITE8(X,Y) mmio_writeb(Y, spibar+X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000155
Dominik Geyerb46acba2008-05-16 12:55:55 +0000156/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000157static int find_opcode(OPCODES *op, uint8_t opcode);
158static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000159static int generate_opcodes(OPCODES * op);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000160static int program_opcodes(OPCODES * op);
Stefan Reinauer43119562008-11-02 19:51:50 +0000161static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000162 uint8_t datalength, uint8_t * data);
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000163static int ich_spi_write_page(struct flashchip *flash, uint8_t * bytes,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000164 int offset, int maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000165
FENG yu ningf041e9b2008-12-15 02:32:11 +0000166/* for pairing opcodes with their required preop */
167struct preop_opcode_pair {
168 uint8_t preop;
169 uint8_t opcode;
170};
171
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000172/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000173struct preop_opcode_pair pops[] = {
174 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
175 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
176 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
177 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
178 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
179 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000180 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
181 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000182 {JEDEC_EWSR, JEDEC_WRSR},
183 {0,}
184};
185
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000186/* Reasonable default configuration. Needs ad-hoc modifications if we
187 * encounter unlisted opcodes. Fun.
188 */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000189OPCODES O_ST_M25P = {
190 {
191 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000192 JEDEC_EWSR,
193 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000194 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000195 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000196 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000197 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000198 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000199 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000200 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000201 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000202 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
203 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000204};
205
FENG yu ningc05a2952008-12-08 18:16:58 +0000206OPCODES O_EXISTING = {};
207
Uwe Hermann09e04f72009-05-16 22:36:00 +0000208static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000209{
210 int a;
211
212 for (a = 0; a < 8; a++) {
213 if (op->opcode[a].opcode == opcode)
214 return a;
215 }
216
217 return -1;
218}
219
Uwe Hermann09e04f72009-05-16 22:36:00 +0000220static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000221{
222 int a;
223
224 for (a = 0; a < 2; a++) {
225 if (op->preop[a] == preop)
226 return a;
227 }
228
229 return -1;
230}
231
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000232/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000233static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000234{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000235 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000236 uint16_t preop, optype;
237 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000238
239 if (op == NULL) {
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000240 printf_debug("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000241 return -1;
242 }
243
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000244 switch (spi_controller) {
245 case SPI_CONTROLLER_ICH7:
246 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000247 preop = REGREAD16(ICH7_REG_PREOP);
248 optype = REGREAD16(ICH7_REG_OPTYPE);
249 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
250 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
251 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000252 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000253 preop = REGREAD16(ICH9_REG_PREOP);
254 optype = REGREAD16(ICH9_REG_OPTYPE);
255 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
256 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
257 break;
258 default:
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000259 printf_debug("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000260 return -1;
261 }
262
263 op->preop[0] = (uint8_t) preop;
264 op->preop[1] = (uint8_t) (preop >> 8);
265
266 for (a = 0; a < 8; a++) {
267 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
268 optype >>= 2;
269 }
270
271 for (a = 0; a < 4; a++) {
272 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
273 opmenu[0] >>= 8;
274 }
275
276 for (a = 4; a < 8; a++) {
277 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
278 opmenu[1] >>= 8;
279 }
280
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000281 /* No preopcodes used by default. */
282 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000283 op->opcode[a].atomic = 0;
284
FENG yu ningc05a2952008-12-08 18:16:58 +0000285 return 0;
286}
287
Dominik Geyerb46acba2008-05-16 12:55:55 +0000288int program_opcodes(OPCODES * op)
289{
290 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000291 uint16_t preop, optype;
292 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000293
294 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000295 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000296 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000297 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000298 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000299
Stefan Reinauera9424d52008-06-27 16:28:34 +0000300 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000301 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000302 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000303 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000304 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000305
Stefan Reinauera9424d52008-06-27 16:28:34 +0000306 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000307 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000308 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000309 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000310 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000311
Dominik Geyerb46acba2008-05-16 12:55:55 +0000312 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000313 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000314 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000315 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000316 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000317
Peter Stuge016d4e12009-01-15 02:13:18 +0000318 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 +0000319 switch (spi_controller) {
320 case SPI_CONTROLLER_ICH7:
321 case SPI_CONTROLLER_VIA:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000322 REGWRITE16(ICH7_REG_PREOP, preop);
323 REGWRITE16(ICH7_REG_OPTYPE, optype);
324 REGWRITE32(ICH7_REG_OPMENU, opmenu[0]);
325 REGWRITE32(ICH7_REG_OPMENU + 4, opmenu[1]);
326 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000327 case SPI_CONTROLLER_ICH9:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000328 REGWRITE16(ICH9_REG_PREOP, preop);
329 REGWRITE16(ICH9_REG_OPTYPE, optype);
330 REGWRITE32(ICH9_REG_OPMENU, opmenu[0]);
331 REGWRITE32(ICH9_REG_OPMENU + 4, opmenu[1]);
332 break;
333 default:
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000334 printf_debug("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000335 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000336 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000337
338 return 0;
339}
340
FENG yu ningf041e9b2008-12-15 02:32:11 +0000341/* This function generates OPCODES from or programs OPCODES to ICH according to
342 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000343 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000344 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000345 */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000346int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000347{
348 int rc = 0;
349 OPCODES *curopcodes_done;
350
351 if (curopcodes)
352 return 0;
353
354 if (ichspi_lock) {
355 printf_debug("Generating OPCODES... ");
356 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000357 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000358 } else {
359 printf_debug("Programming OPCODES... ");
360 curopcodes_done = &O_ST_M25P;
361 rc = program_opcodes(curopcodes_done);
362 }
363
364 if (rc) {
365 curopcodes = NULL;
366 printf_debug("failed\n");
367 return 1;
368 } else {
369 curopcodes = curopcodes_done;
370 printf_debug("done\n");
371 return 0;
372 }
373}
374
Stefan Reinauer43119562008-11-02 19:51:50 +0000375static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000376 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000377{
378 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000379 int timeout;
Peter Stuge7e2c0792008-06-29 01:30:41 +0000380 uint32_t temp32 = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000381 uint16_t temp16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000382 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000383 uint64_t opmenu;
384 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000385
386 /* Is it a write command? */
387 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
388 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
389 write_cmd = 1;
390 }
391
392 /* Programm Offset in Flash into FADDR */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000393 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000394
395 /* Program data into FDATA0 to N */
396 if (write_cmd && (datalength != 0)) {
397 temp32 = 0;
398 for (a = 0; a < datalength; a++) {
399 if ((a % 4) == 0) {
400 temp32 = 0;
401 }
402
403 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
404
405 if ((a % 4) == 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000406 REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)),
407 temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000408 }
409 }
410 if (((a - 1) % 4) != 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000411 REGWRITE32(ICH7_REG_SPID0 +
412 ((a - 1) - ((a - 1) % 4)), temp32);
413 }
414
415 }
416
417 /* Assemble SPIS */
418 temp16 = 0;
419 /* clear error status registers */
420 temp16 |= (SPIS_CDS + SPIS_FCERR);
421 REGWRITE16(ICH7_REG_SPIS, temp16);
422
423 /* Assemble SPIC */
424 temp16 = 0;
425
426 if (datalength != 0) {
427 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000428 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000429 }
430
431 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000432 opmenu = REGREAD32(ICH7_REG_OPMENU);
433 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
434
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000435 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
436 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000437 break;
438 }
439 opmenu >>= 8;
440 }
441 if (opcode_index == 8) {
442 printf_debug("Opcode %x not found.\n", op.opcode);
443 return 1;
444 }
445 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000446
447 /* Handle Atomic */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000448 switch (op.atomic) {
449 case 2:
450 /* Select second preop. */
451 temp16 |= SPIC_SPOP;
452 /* And fall through. */
453 case 1:
454 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000455 temp16 |= SPIC_ACS;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000456 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000457 }
458
459 /* Start */
460 temp16 |= SPIC_SCGO;
461
462 /* write it */
463 REGWRITE16(ICH7_REG_SPIC, temp16);
464
465 /* wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000466 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauera9424d52008-06-27 16:28:34 +0000467 while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000468 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000469 }
470 if (!timeout) {
471 printf_debug("timeout\n");
472 }
473
474 if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) {
475 printf_debug("Transaction error!\n");
476 return 1;
477 }
478
479 if ((!write_cmd) && (datalength != 0)) {
480 for (a = 0; a < datalength; a++) {
481 if ((a % 4) == 0) {
482 temp32 = REGREAD32(ICH7_REG_SPID0 + (a));
483 }
484
485 data[a] =
486 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
487 >> ((a % 4) * 8);
488 }
489 }
490
491 return 0;
492}
493
Stefan Reinauer43119562008-11-02 19:51:50 +0000494static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000495 uint8_t datalength, uint8_t * data)
496{
497 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000498 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000499 uint32_t temp32;
500 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000501 uint64_t opmenu;
502 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000503
504 /* Is it a write command? */
505 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
506 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
507 write_cmd = 1;
508 }
509
510 /* Programm Offset in Flash into FADDR */
511 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
512
513 /* Program data into FDATA0 to N */
514 if (write_cmd && (datalength != 0)) {
515 temp32 = 0;
516 for (a = 0; a < datalength; a++) {
517 if ((a % 4) == 0) {
518 temp32 = 0;
519 }
520
521 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
522
523 if ((a % 4) == 3) {
524 REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
525 temp32);
526 }
527 }
528 if (((a - 1) % 4) != 3) {
529 REGWRITE32(ICH9_REG_FDATA0 +
530 ((a - 1) - ((a - 1) % 4)), temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000531 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000532 }
533
534 /* Assemble SSFS + SSFC */
535 temp32 = 0;
536
537 /* clear error status registers */
538 temp32 |= (SSFS_CDS + SSFS_FCERR);
539 /* USE 20 MhZ */
540 temp32 |= SSFC_SCF_20MHZ;
541
542 if (datalength != 0) {
543 uint32_t datatemp;
544 temp32 |= SSFC_DS;
545 datatemp = ((uint32_t) ((datalength - 1) & 0x3f)) << (8 + 8);
546 temp32 |= datatemp;
547 }
548
549 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000550 opmenu = REGREAD32(ICH9_REG_OPMENU);
551 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
552
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000553 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
554 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000555 break;
556 }
557 opmenu >>= 8;
558 }
559 if (opcode_index == 8) {
560 printf_debug("Opcode %x not found.\n", op.opcode);
561 return 1;
562 }
563 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000564
565 /* Handle Atomic */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000566 switch (op.atomic) {
567 case 2:
568 /* Select second preop. */
569 temp32 |= SSFC_SPOP;
570 /* And fall through. */
571 case 1:
572 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000573 temp32 |= SSFC_ACS;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000574 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000575 }
576
577 /* Start */
578 temp32 |= SSFC_SCGO;
579
580 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000581 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000582
583 /*wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000584 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000585 while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000586 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000587 }
588 if (!timeout) {
589 printf_debug("timeout\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000590 }
591
Stefan Reinauera9424d52008-06-27 16:28:34 +0000592 if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000593 printf_debug("Transaction error!\n");
594 return 1;
595 }
596
597 if ((!write_cmd) && (datalength != 0)) {
598 for (a = 0; a < datalength; a++) {
599 if ((a % 4) == 0) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000600 temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
Dominik Geyerb46acba2008-05-16 12:55:55 +0000601 }
602
603 data[a] =
Stefan Reinauera9424d52008-06-27 16:28:34 +0000604 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
605 >> ((a % 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000606 }
607 }
608
609 return 0;
610}
611
Stefan Reinauer43119562008-11-02 19:51:50 +0000612static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000613 uint8_t datalength, uint8_t * data)
614{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000615 switch (spi_controller) {
616 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000617 if (datalength > 16) {
618 fprintf(stderr, "%s: Internal command size error for "
619 "opcode 0x%02x, got datalength=%i, want <=16\n",
620 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000621 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000622 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000623 return ich7_run_opcode(op, offset, datalength, data, 16);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000624 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000625 if (datalength > 64) {
626 fprintf(stderr, "%s: Internal command size error for "
627 "opcode 0x%02x, got datalength=%i, want <=16\n",
628 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000629 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000630 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000631 return ich7_run_opcode(op, offset, datalength, data, 64);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000632 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000633 if (datalength > 64) {
634 fprintf(stderr, "%s: Internal command size error for "
635 "opcode 0x%02x, got datalength=%i, want <=16\n",
636 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000637 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000638 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000639 return ich9_run_opcode(op, offset, datalength, data);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000640 default:
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000641 printf_debug("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000642 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000643
644 /* If we ever get here, something really weird happened */
645 return -1;
646}
647
Dominik Geyerb46acba2008-05-16 12:55:55 +0000648static int ich_spi_write_page(struct flashchip *flash, uint8_t * bytes,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000649 int offset, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000650{
651 int page_size = flash->page_size;
652 uint32_t remaining = page_size;
Carl-Daniel Hailfingerd1680572009-06-17 10:13:42 +0000653 int towrite;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000654
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000655 msg_comm_debug("ich_spi_write_page: offset=%d, number=%d, buf=%p\n",
Stefan Reinauera9424d52008-06-27 16:28:34 +0000656 offset, page_size, bytes);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000657
Carl-Daniel Hailfingerd1680572009-06-17 10:13:42 +0000658 for (; remaining > 0; remaining -= towrite) {
659 towrite = min(remaining, maxdata);
660 if (spi_nbyte_program(offset + (page_size - remaining),
661 &bytes[page_size - remaining], towrite)) {
662 printf_debug("Error writing");
663 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000664 }
665 }
666
667 return 0;
668}
669
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000670int ich_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000671{
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000672 int maxdata = 64;
673
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000674 if (spi_controller == SPI_CONTROLLER_VIA)
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000675 maxdata = 16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000676
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000677 return spi_read_chunked(flash, buf, start, len, maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000678}
679
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000680int ich_spi_write_256(struct flashchip *flash, uint8_t * buf)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000681{
682 int i, j, rc = 0;
683 int total_size = flash->total_size * 1024;
684 int page_size = flash->page_size;
685 int erase_size = 64 * 1024;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000686 int maxdata = 64;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000687
688 spi_disable_blockprotect();
Carl-Daniel Hailfinger96123032009-11-25 02:07:30 +0000689 /* Erase first */
690 printf("Erasing flash before programming... ");
691 if (erase_flash(flash)) {
692 fprintf(stderr, "ERASE FAILED!\n");
693 return -1;
694 }
695 printf("done.\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000696
697 printf("Programming page: \n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000698 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000699 if (spi_controller == SPI_CONTROLLER_VIA)
Peter Stuge6a214162008-07-07 05:14:06 +0000700 maxdata = 16;
701
Dominik Geyerb46acba2008-05-16 12:55:55 +0000702 for (j = 0; j < erase_size / page_size; j++) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000703 ich_spi_write_page(flash,
704 (void *)(buf + (i * erase_size) + (j * page_size)),
705 (i * erase_size) + (j * page_size), maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000706 }
707 }
708
709 printf("\n");
710
711 return rc;
712}
713
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000714int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000715 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000716{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000717 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000718 int opcode_index = -1;
719 const unsigned char cmd = *writearr;
720 OPCODE *opcode;
721 uint32_t addr = 0;
722 uint8_t *data;
723 int count;
724
Dominik Geyerb46acba2008-05-16 12:55:55 +0000725 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000726 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000727 if (opcode_index == -1) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000728 /* FIXME: Reprogram opcodes if possible. Autodetect type of
729 * opcode by checking readcnt/writecnt.
730 */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000731 printf_debug("Invalid OPCODE 0x%02x\n", cmd);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000732 return SPI_INVALID_OPCODE;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000733 }
734
735 opcode = &(curopcodes->opcode[opcode_index]);
736
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000737 /* The following valid writecnt/readcnt combinations exist:
738 * writecnt = 4, readcnt >= 0
739 * writecnt = 1, readcnt >= 0
740 * writecnt >= 4, readcnt = 0
741 * writecnt >= 1, readcnt = 0
742 * writecnt >= 1 is guaranteed for all commands.
743 */
744 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
745 (writecnt != 4)) {
746 fprintf(stderr, "%s: Internal command size error for opcode "
747 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
748 writecnt);
749 return SPI_INVALID_LENGTH;
750 }
751 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
752 (writecnt != 1)) {
753 fprintf(stderr, "%s: Internal command size error for opcode "
754 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
755 writecnt);
756 return SPI_INVALID_LENGTH;
757 }
758 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
759 (writecnt < 4)) {
760 fprintf(stderr, "%s: Internal command size error for opcode "
761 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
762 writecnt);
763 return SPI_INVALID_LENGTH;
764 }
765 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
766 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
767 (readcnt)) {
768 fprintf(stderr, "%s: Internal command size error for opcode "
769 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
770 readcnt);
771 return SPI_INVALID_LENGTH;
772 }
773
Dominik Geyerb46acba2008-05-16 12:55:55 +0000774 /* if opcode-type requires an address */
775 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
776 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000777 addr = (writearr[1] << 16) |
778 (writearr[2] << 8) | (writearr[3] << 0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000779 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000780
Dominik Geyerb46acba2008-05-16 12:55:55 +0000781 /* translate read/write array/count */
782 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000783 data = (uint8_t *) (writearr + 1);
784 count = writecnt - 1;
785 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
786 data = (uint8_t *) (writearr + 4);
787 count = writecnt - 4;
788 } else {
789 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000790 count = readcnt;
791 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000792
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000793 result = run_opcode(*opcode, addr, count, data);
794 if (result) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000795 printf_debug("run OPCODE 0x%02x failed\n", opcode->opcode);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000796 }
797
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000798 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000799}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000800
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000801int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000802{
803 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000804 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000805 int oppos, preoppos;
806 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000807 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000808 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000809 preoppos = find_preop(curopcodes, cmds->writearr[0]);
810 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000811 if ((oppos == -1) && (preoppos != -1)) {
812 /* Current command is listed as preopcode in
813 * ICH struct OPCODES, but next command is not
814 * listed as opcode in that struct.
815 * Check for command sanity, then
816 * try to reprogram the ICH opcode list.
817 */
818 if (find_preop(curopcodes,
819 (cmds + 1)->writearr[0]) != -1) {
820 fprintf(stderr, "%s: Two subsequent "
821 "preopcodes 0x%02x and 0x%02x, "
822 "ignoring the first.\n",
823 __func__, cmds->writearr[0],
824 (cmds + 1)->writearr[0]);
825 continue;
826 }
827 /* If the chipset is locked down, we'll fail
828 * during execution of the next command anyway.
829 * No need to bother with fixups.
830 */
831 if (!ichspi_lock) {
832 printf_debug("%s: FIXME: Add on-the-fly"
833 " reprogramming of the "
834 "chipset opcode list.\n",
835 __func__);
836 /* FIXME: Reprogram opcode menu.
837 * Find a less-useful opcode, replace it
838 * with the wanted opcode, detect optype
839 * and reprogram the opcode menu.
840 * Update oppos so the next if-statement
841 * can do something useful.
842 */
843 //curopcodes.opcode[lessusefulindex] = (cmds + 1)->writearr[0]);
844 //update_optypes(curopcodes);
845 //program_opcodes(curopcodes);
846 //oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
847 continue;
848 }
849 }
850 if ((oppos != -1) && (preoppos != -1)) {
851 /* Current command is listed as preopcode in
852 * ICH struct OPCODES and next command is listed
853 * as opcode in that struct. Match them up.
854 */
855 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000856 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000857 }
858 /* If none of the above if-statements about oppos or
859 * preoppos matched, this is a normal opcode.
860 */
861 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000862 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
863 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000864 /* Reset the type of all opcodes to non-atomic. */
865 for (i = 0; i < 8; i++)
866 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000867 }
868 return ret;
869}