blob: 8add13e9a02373d5a1aeecebefba283e097e7962 [file] [log] [blame]
Dominik Geyerb46acba2008-05-16 12:55:55 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2008 Stefan Wildemann <stefan.wildemann@kontron.com>
5 * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com>
6 * Copyright (C) 2008 Dominik Geyer <dominik.geyer@kontron.com>
Stefan Reinauera9424d52008-06-27 16:28:34 +00007 * Copyright (C) 2008 coresystems GmbH <info@coresystems.de>
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00008 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
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
Stefan Reinauera9424d52008-06-27 16:28:34 +000041/* ICH9 controller register definition */
42#define ICH9_REG_FADDR 0x08 /* 32 Bits */
43#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
44
45#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000046#define SSFS_SCIP 0x00000001
47#define SSFS_CDS 0x00000004
48#define SSFS_FCERR 0x00000008
49#define SSFS_AEL 0x00000010
Stefan Reinauera9424d52008-06-27 16:28:34 +000050
51#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000052#define SSFC_SCGO 0x00000200
53#define SSFC_ACS 0x00000400
54#define SSFC_SPOP 0x00000800
55#define SSFC_COP 0x00001000
56#define SSFC_DBC 0x00010000
57#define SSFC_DS 0x00400000
58#define SSFC_SME 0x00800000
59#define SSFC_SCF 0x01000000
60#define SSFC_SCF_20MHZ 0x00000000
61#define SSFC_SCF_33MHZ 0x01000000
Stefan Reinauera9424d52008-06-27 16:28:34 +000062
63#define ICH9_REG_PREOP 0x94 /* 16 Bits */
64#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
65#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000066
67// ICH9R SPI commands
68#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
69#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
70#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
71#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
72
Stefan Reinauera9424d52008-06-27 16:28:34 +000073// ICH7 registers
74#define ICH7_REG_SPIS 0x00 /* 16 Bits */
75#define SPIS_SCIP 0x00000001
76#define SPIS_CDS 0x00000004
77#define SPIS_FCERR 0x00000008
78
Rudolf Marek3fdbccf2008-06-30 21:38:30 +000079/* VIA SPI is compatible with ICH7, but maxdata
80 to transfer is 16 bytes.
81
82 DATA byte count on ICH7 is 8:13, on VIA 8:11
83
84 bit 12 is port select CS0 CS1
85 bit 13 is FAST READ enable
86 bit 7 is used with fast read and one shot controls CS de-assert?
87*/
88
Stefan Reinauera9424d52008-06-27 16:28:34 +000089#define ICH7_REG_SPIC 0x02 /* 16 Bits */
90#define SPIC_SCGO 0x0002
91#define SPIC_ACS 0x0004
92#define SPIC_SPOP 0x0008
Rudolf Marek3fdbccf2008-06-30 21:38:30 +000093#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +000094
95#define ICH7_REG_SPIA 0x04 /* 32 Bits */
96#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
97#define ICH7_REG_PREOP 0x54 /* 16 Bits */
98#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
99#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
100
FENG yu ningc05a2952008-12-08 18:16:58 +0000101/* ICH SPI configuration lock-down. May be set during chipset enabling. */
102int ichspi_lock = 0;
103
Dominik Geyerb46acba2008-05-16 12:55:55 +0000104typedef struct _OPCODE {
105 uint8_t opcode; //This commands spi opcode
106 uint8_t spi_type; //This commands spi type
107 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
108} OPCODE;
109
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000110/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000111 * Preop 1: Write Enable
112 * Preop 2: Write Status register enable
113 *
114 * OP 0: Write address
115 * OP 1: Read Address
116 * OP 2: ERASE block
117 * OP 3: Read Status register
118 * OP 4: Read ID
119 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000120 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000121 * OP 7: Chip erase
122 */
123typedef struct _OPCODES {
124 uint8_t preop[2];
125 OPCODE opcode[8];
126} OPCODES;
127
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000128static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000129
130/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000131static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000132{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000133 return mmio_readl(spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000134}
135
Uwe Hermann09e04f72009-05-16 22:36:00 +0000136static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000137{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000138 return mmio_readw(spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000139}
140
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000141#define REGWRITE32(X,Y) mmio_writel(Y, spibar+X)
142#define REGWRITE16(X,Y) mmio_writew(Y, spibar+X)
143#define REGWRITE8(X,Y) mmio_writeb(Y, spibar+X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000144
Dominik Geyerb46acba2008-05-16 12:55:55 +0000145/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000146static int find_opcode(OPCODES *op, uint8_t opcode);
147static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000148static int generate_opcodes(OPCODES * op);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000149static int program_opcodes(OPCODES * op);
Stefan Reinauer43119562008-11-02 19:51:50 +0000150static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000151 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000152
FENG yu ningf041e9b2008-12-15 02:32:11 +0000153/* for pairing opcodes with their required preop */
154struct preop_opcode_pair {
155 uint8_t preop;
156 uint8_t opcode;
157};
158
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000159/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000160struct preop_opcode_pair pops[] = {
161 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
162 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
163 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
164 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
165 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
166 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000167 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
168 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000169 {JEDEC_EWSR, JEDEC_WRSR},
170 {0,}
171};
172
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000173/* Reasonable default configuration. Needs ad-hoc modifications if we
174 * encounter unlisted opcodes. Fun.
175 */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000176OPCODES O_ST_M25P = {
177 {
178 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000179 JEDEC_EWSR,
180 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000181 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000182 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000183 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000184 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000185 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000186 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000187 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000188 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000189 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
190 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000191};
192
FENG yu ningc05a2952008-12-08 18:16:58 +0000193OPCODES O_EXISTING = {};
194
Uwe Hermann09e04f72009-05-16 22:36:00 +0000195static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000196{
197 int a;
198
199 for (a = 0; a < 8; a++) {
200 if (op->opcode[a].opcode == opcode)
201 return a;
202 }
203
204 return -1;
205}
206
Uwe Hermann09e04f72009-05-16 22:36:00 +0000207static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000208{
209 int a;
210
211 for (a = 0; a < 2; a++) {
212 if (op->preop[a] == preop)
213 return a;
214 }
215
216 return -1;
217}
218
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000219/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000220static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000221{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000222 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000223 uint16_t preop, optype;
224 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000225
226 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000227 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000228 return -1;
229 }
230
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000231 switch (spi_controller) {
232 case SPI_CONTROLLER_ICH7:
233 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000234 preop = REGREAD16(ICH7_REG_PREOP);
235 optype = REGREAD16(ICH7_REG_OPTYPE);
236 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
237 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
238 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000239 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000240 preop = REGREAD16(ICH9_REG_PREOP);
241 optype = REGREAD16(ICH9_REG_OPTYPE);
242 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
243 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
244 break;
245 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000246 msg_perr("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000247 return -1;
248 }
249
250 op->preop[0] = (uint8_t) preop;
251 op->preop[1] = (uint8_t) (preop >> 8);
252
253 for (a = 0; a < 8; a++) {
254 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
255 optype >>= 2;
256 }
257
258 for (a = 0; a < 4; a++) {
259 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
260 opmenu[0] >>= 8;
261 }
262
263 for (a = 4; a < 8; a++) {
264 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
265 opmenu[1] >>= 8;
266 }
267
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000268 /* No preopcodes used by default. */
269 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000270 op->opcode[a].atomic = 0;
271
FENG yu ningc05a2952008-12-08 18:16:58 +0000272 return 0;
273}
274
Dominik Geyerb46acba2008-05-16 12:55:55 +0000275int program_opcodes(OPCODES * op)
276{
277 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000278 uint16_t preop, optype;
279 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000280
281 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000282 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000283 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000284 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000285 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000286
Stefan Reinauera9424d52008-06-27 16:28:34 +0000287 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000288 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000289 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000290 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000291 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000292
Stefan Reinauera9424d52008-06-27 16:28:34 +0000293 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000294 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000295 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000296 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000297 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000298
Dominik Geyerb46acba2008-05-16 12:55:55 +0000299 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000300 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000301 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000302 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000303 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000304
Sean Nelson316a29f2010-05-07 20:09:04 +0000305 msg_pdbg("\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 +0000306 switch (spi_controller) {
307 case SPI_CONTROLLER_ICH7:
308 case SPI_CONTROLLER_VIA:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000309 REGWRITE16(ICH7_REG_PREOP, preop);
310 REGWRITE16(ICH7_REG_OPTYPE, optype);
311 REGWRITE32(ICH7_REG_OPMENU, opmenu[0]);
312 REGWRITE32(ICH7_REG_OPMENU + 4, opmenu[1]);
313 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000314 case SPI_CONTROLLER_ICH9:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000315 REGWRITE16(ICH9_REG_PREOP, preop);
316 REGWRITE16(ICH9_REG_OPTYPE, optype);
317 REGWRITE32(ICH9_REG_OPMENU, opmenu[0]);
318 REGWRITE32(ICH9_REG_OPMENU + 4, opmenu[1]);
319 break;
320 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000321 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000322 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000323 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000324
325 return 0;
326}
327
FENG yu ningf041e9b2008-12-15 02:32:11 +0000328/* This function generates OPCODES from or programs OPCODES to ICH according to
329 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000330 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000331 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000332 */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000333int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000334{
335 int rc = 0;
336 OPCODES *curopcodes_done;
337
338 if (curopcodes)
339 return 0;
340
341 if (ichspi_lock) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000342 msg_pdbg("Generating OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000343 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000344 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000345 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000346 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000347 curopcodes_done = &O_ST_M25P;
348 rc = program_opcodes(curopcodes_done);
349 }
350
351 if (rc) {
352 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000353 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000354 return 1;
355 } else {
356 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000357 msg_pdbg("done\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000358 return 0;
359 }
360}
361
Stefan Reinauer43119562008-11-02 19:51:50 +0000362static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000363 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000364{
365 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000366 int timeout;
Peter Stuge7e2c0792008-06-29 01:30:41 +0000367 uint32_t temp32 = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000368 uint16_t temp16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000369 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000370 uint64_t opmenu;
371 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000372
373 /* Is it a write command? */
374 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
375 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
376 write_cmd = 1;
377 }
378
379 /* Programm Offset in Flash into FADDR */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000380 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000381
382 /* Program data into FDATA0 to N */
383 if (write_cmd && (datalength != 0)) {
384 temp32 = 0;
385 for (a = 0; a < datalength; a++) {
386 if ((a % 4) == 0) {
387 temp32 = 0;
388 }
389
390 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
391
392 if ((a % 4) == 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000393 REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)),
394 temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000395 }
396 }
397 if (((a - 1) % 4) != 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000398 REGWRITE32(ICH7_REG_SPID0 +
399 ((a - 1) - ((a - 1) % 4)), temp32);
400 }
401
402 }
403
404 /* Assemble SPIS */
405 temp16 = 0;
406 /* clear error status registers */
407 temp16 |= (SPIS_CDS + SPIS_FCERR);
408 REGWRITE16(ICH7_REG_SPIS, temp16);
409
410 /* Assemble SPIC */
411 temp16 = 0;
412
413 if (datalength != 0) {
414 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000415 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000416 }
417
418 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000419 opmenu = REGREAD32(ICH7_REG_OPMENU);
420 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
421
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000422 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
423 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000424 break;
425 }
426 opmenu >>= 8;
427 }
428 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000429 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000430 return 1;
431 }
432 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000433
434 /* Handle Atomic */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000435 switch (op.atomic) {
436 case 2:
437 /* Select second preop. */
438 temp16 |= SPIC_SPOP;
439 /* And fall through. */
440 case 1:
441 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000442 temp16 |= SPIC_ACS;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000443 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000444 }
445
446 /* Start */
447 temp16 |= SPIC_SCGO;
448
449 /* write it */
450 REGWRITE16(ICH7_REG_SPIC, temp16);
451
452 /* wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000453 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauera9424d52008-06-27 16:28:34 +0000454 while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000455 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000456 }
457 if (!timeout) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000458 msg_perr("timeout\n");
Stefan Reinauera9424d52008-06-27 16:28:34 +0000459 }
460
Sean Nelson316a29f2010-05-07 20:09:04 +0000461 /* FIXME: make sure we do not needlessly cause transaction errors. */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000462 if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000463 msg_pdbg("Transaction error!\n");
Stefan Reinauera9424d52008-06-27 16:28:34 +0000464 return 1;
465 }
466
467 if ((!write_cmd) && (datalength != 0)) {
468 for (a = 0; a < datalength; a++) {
469 if ((a % 4) == 0) {
470 temp32 = REGREAD32(ICH7_REG_SPID0 + (a));
471 }
472
473 data[a] =
474 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
475 >> ((a % 4) * 8);
476 }
477 }
478
479 return 0;
480}
481
Stefan Reinauer43119562008-11-02 19:51:50 +0000482static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000483 uint8_t datalength, uint8_t * data)
484{
485 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000486 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000487 uint32_t temp32;
488 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000489 uint64_t opmenu;
490 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000491
492 /* Is it a write command? */
493 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
494 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
495 write_cmd = 1;
496 }
497
498 /* Programm Offset in Flash into FADDR */
499 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
500
501 /* Program data into FDATA0 to N */
502 if (write_cmd && (datalength != 0)) {
503 temp32 = 0;
504 for (a = 0; a < datalength; a++) {
505 if ((a % 4) == 0) {
506 temp32 = 0;
507 }
508
509 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
510
511 if ((a % 4) == 3) {
512 REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
513 temp32);
514 }
515 }
516 if (((a - 1) % 4) != 3) {
517 REGWRITE32(ICH9_REG_FDATA0 +
518 ((a - 1) - ((a - 1) % 4)), temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000519 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000520 }
521
522 /* Assemble SSFS + SSFC */
523 temp32 = 0;
524
525 /* clear error status registers */
526 temp32 |= (SSFS_CDS + SSFS_FCERR);
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000527 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000528 temp32 |= SSFC_SCF_20MHZ;
529
530 if (datalength != 0) {
531 uint32_t datatemp;
532 temp32 |= SSFC_DS;
533 datatemp = ((uint32_t) ((datalength - 1) & 0x3f)) << (8 + 8);
534 temp32 |= datatemp;
535 }
536
537 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000538 opmenu = REGREAD32(ICH9_REG_OPMENU);
539 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
540
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000541 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
542 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000543 break;
544 }
545 opmenu >>= 8;
546 }
547 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000548 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000549 return 1;
550 }
551 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000552
553 /* Handle Atomic */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000554 switch (op.atomic) {
555 case 2:
556 /* Select second preop. */
557 temp32 |= SSFC_SPOP;
558 /* And fall through. */
559 case 1:
560 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000561 temp32 |= SSFC_ACS;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000562 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000563 }
564
565 /* Start */
566 temp32 |= SSFC_SCGO;
567
568 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000569 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000570
571 /*wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000572 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000573 while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000574 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000575 }
576 if (!timeout) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000577 msg_perr("timeout\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000578 }
579
Sean Nelson316a29f2010-05-07 20:09:04 +0000580 /* FIXME make sure we do not needlessly cause transaction errors. */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000581 if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000582 msg_pdbg("Transaction error!\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000583 return 1;
584 }
585
586 if ((!write_cmd) && (datalength != 0)) {
587 for (a = 0; a < datalength; a++) {
588 if ((a % 4) == 0) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000589 temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
Dominik Geyerb46acba2008-05-16 12:55:55 +0000590 }
591
592 data[a] =
Stefan Reinauera9424d52008-06-27 16:28:34 +0000593 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
594 >> ((a % 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000595 }
596 }
597
598 return 0;
599}
600
Stefan Reinauer43119562008-11-02 19:51:50 +0000601static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000602 uint8_t datalength, uint8_t * data)
603{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000604 switch (spi_controller) {
605 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000606 if (datalength > 16) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000607 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000608 "opcode 0x%02x, got datalength=%i, want <=16\n",
609 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000610 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000611 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000612 return ich7_run_opcode(op, offset, datalength, data, 16);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000613 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000614 if (datalength > 64) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000615 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000616 "opcode 0x%02x, got datalength=%i, want <=16\n",
617 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000618 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000619 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000620 return ich7_run_opcode(op, offset, datalength, data, 64);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000621 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000622 if (datalength > 64) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000623 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000624 "opcode 0x%02x, got datalength=%i, want <=16\n",
625 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000626 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000627 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000628 return ich9_run_opcode(op, offset, datalength, data);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000629 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000630 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000631 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000632
633 /* If we ever get here, something really weird happened */
634 return -1;
635}
636
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000637int ich_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000638{
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000639 int maxdata = 64;
640
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000641 if (spi_controller == SPI_CONTROLLER_VIA)
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000642 maxdata = 16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000643
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000644 return spi_read_chunked(flash, buf, start, len, maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000645}
646
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000647int ich_spi_write_256(struct flashchip *flash, uint8_t * buf)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000648{
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000649 int i, ret = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000650 int total_size = flash->total_size * 1024;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000651 int erase_size = 64 * 1024;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000652 int maxdata = 64;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000653
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000654 if (spi_controller == SPI_CONTROLLER_VIA)
655 maxdata = 16;
656
Dominik Geyerb46acba2008-05-16 12:55:55 +0000657 spi_disable_blockprotect();
Carl-Daniel Hailfinger96123032009-11-25 02:07:30 +0000658 /* Erase first */
Sean Nelson316a29f2010-05-07 20:09:04 +0000659 msg_pinfo("Erasing flash before programming... ");
Carl-Daniel Hailfinger96123032009-11-25 02:07:30 +0000660 if (erase_flash(flash)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000661 msg_perr("ERASE FAILED!\n");
Carl-Daniel Hailfinger96123032009-11-25 02:07:30 +0000662 return -1;
663 }
Sean Nelson316a29f2010-05-07 20:09:04 +0000664 msg_pinfo("done.\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000665
Sean Nelson316a29f2010-05-07 20:09:04 +0000666 msg_pinfo("Programming page: \n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000667 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000668 ret = spi_write_chunked(flash, buf + (i * erase_size),
669 i * erase_size, erase_size, maxdata);
670 if (ret)
671 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000672 }
673
Sean Nelson316a29f2010-05-07 20:09:04 +0000674 msg_pinfo("\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000675
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000676 return ret;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000677}
678
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000679int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000680 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000681{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000682 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000683 int opcode_index = -1;
684 const unsigned char cmd = *writearr;
685 OPCODE *opcode;
686 uint32_t addr = 0;
687 uint8_t *data;
688 int count;
689
Dominik Geyerb46acba2008-05-16 12:55:55 +0000690 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000691 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000692 if (opcode_index == -1) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000693 /* FIXME: Reprogram opcodes if possible. Autodetect type of
694 * opcode by checking readcnt/writecnt.
695 */
Sean Nelson316a29f2010-05-07 20:09:04 +0000696 msg_pdbg("Invalid OPCODE 0x%02x\n", cmd);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000697 return SPI_INVALID_OPCODE;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000698 }
699
700 opcode = &(curopcodes->opcode[opcode_index]);
701
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000702 /* The following valid writecnt/readcnt combinations exist:
703 * writecnt = 4, readcnt >= 0
704 * writecnt = 1, readcnt >= 0
705 * writecnt >= 4, readcnt = 0
706 * writecnt >= 1, readcnt = 0
707 * writecnt >= 1 is guaranteed for all commands.
708 */
709 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
710 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000711 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000712 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
713 writecnt);
714 return SPI_INVALID_LENGTH;
715 }
716 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
717 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000718 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000719 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
720 writecnt);
721 return SPI_INVALID_LENGTH;
722 }
723 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
724 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000725 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000726 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
727 writecnt);
728 return SPI_INVALID_LENGTH;
729 }
730 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
731 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
732 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000733 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000734 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
735 readcnt);
736 return SPI_INVALID_LENGTH;
737 }
738
Dominik Geyerb46acba2008-05-16 12:55:55 +0000739 /* if opcode-type requires an address */
740 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
741 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000742 addr = (writearr[1] << 16) |
743 (writearr[2] << 8) | (writearr[3] << 0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000744 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000745
Dominik Geyerb46acba2008-05-16 12:55:55 +0000746 /* translate read/write array/count */
747 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000748 data = (uint8_t *) (writearr + 1);
749 count = writecnt - 1;
750 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
751 data = (uint8_t *) (writearr + 4);
752 count = writecnt - 4;
753 } else {
754 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000755 count = readcnt;
756 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000757
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000758 result = run_opcode(*opcode, addr, count, data);
759 if (result) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000760 msg_pdbg("run OPCODE 0x%02x failed\n", opcode->opcode);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000761 }
762
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000763 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000764}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000765
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000766int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000767{
768 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000769 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000770 int oppos, preoppos;
771 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000772 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000773 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000774 preoppos = find_preop(curopcodes, cmds->writearr[0]);
775 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000776 if ((oppos == -1) && (preoppos != -1)) {
777 /* Current command is listed as preopcode in
778 * ICH struct OPCODES, but next command is not
779 * listed as opcode in that struct.
780 * Check for command sanity, then
781 * try to reprogram the ICH opcode list.
782 */
783 if (find_preop(curopcodes,
784 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000785 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000786 "preopcodes 0x%02x and 0x%02x, "
787 "ignoring the first.\n",
788 __func__, cmds->writearr[0],
789 (cmds + 1)->writearr[0]);
790 continue;
791 }
792 /* If the chipset is locked down, we'll fail
793 * during execution of the next command anyway.
794 * No need to bother with fixups.
795 */
796 if (!ichspi_lock) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000797 msg_pdbg("%s: FIXME: Add on-the-fly"
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000798 " reprogramming of the "
799 "chipset opcode list.\n",
800 __func__);
801 /* FIXME: Reprogram opcode menu.
802 * Find a less-useful opcode, replace it
803 * with the wanted opcode, detect optype
804 * and reprogram the opcode menu.
805 * Update oppos so the next if-statement
806 * can do something useful.
807 */
808 //curopcodes.opcode[lessusefulindex] = (cmds + 1)->writearr[0]);
809 //update_optypes(curopcodes);
810 //program_opcodes(curopcodes);
811 //oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
812 continue;
813 }
814 }
815 if ((oppos != -1) && (preoppos != -1)) {
816 /* Current command is listed as preopcode in
817 * ICH struct OPCODES and next command is listed
818 * as opcode in that struct. Match them up.
819 */
820 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000821 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000822 }
823 /* If none of the above if-statements about oppos or
824 * preoppos matched, this is a normal opcode.
825 */
826 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000827 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
828 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000829 /* Reset the type of all opcodes to non-atomic. */
830 for (i = 0; i < 8; i++)
831 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000832 }
833 return ret;
834}