blob: e9a3611fc0f92679c24fd7b4051d0eaff4698780 [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
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000036#if defined(__i386__) || defined(__x86_64__)
37
Dominik Geyerb46acba2008-05-16 12:55:55 +000038#include <string.h>
Dominik Geyerb46acba2008-05-16 12:55:55 +000039#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000040#include "chipdrivers.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000041#include "spi.h"
42
Stefan Reinauera9424d52008-06-27 16:28:34 +000043/* ICH9 controller register definition */
44#define ICH9_REG_FADDR 0x08 /* 32 Bits */
45#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
46
47#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000048#define SSFS_SCIP 0x00000001
49#define SSFS_CDS 0x00000004
50#define SSFS_FCERR 0x00000008
51#define SSFS_AEL 0x00000010
Stefan Reinauera9424d52008-06-27 16:28:34 +000052
53#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000054#define SSFC_SCGO 0x00000200
55#define SSFC_ACS 0x00000400
56#define SSFC_SPOP 0x00000800
57#define SSFC_COP 0x00001000
58#define SSFC_DBC 0x00010000
59#define SSFC_DS 0x00400000
60#define SSFC_SME 0x00800000
61#define SSFC_SCF 0x01000000
62#define SSFC_SCF_20MHZ 0x00000000
63#define SSFC_SCF_33MHZ 0x01000000
Stefan Reinauera9424d52008-06-27 16:28:34 +000064
65#define ICH9_REG_PREOP 0x94 /* 16 Bits */
66#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
67#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000068
69// ICH9R SPI commands
70#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
71#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
72#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
73#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
74
Stefan Reinauera9424d52008-06-27 16:28:34 +000075// ICH7 registers
76#define ICH7_REG_SPIS 0x00 /* 16 Bits */
77#define SPIS_SCIP 0x00000001
78#define SPIS_CDS 0x00000004
79#define SPIS_FCERR 0x00000008
80
Rudolf Marek3fdbccf2008-06-30 21:38:30 +000081/* VIA SPI is compatible with ICH7, but maxdata
82 to transfer is 16 bytes.
83
84 DATA byte count on ICH7 is 8:13, on VIA 8:11
85
86 bit 12 is port select CS0 CS1
87 bit 13 is FAST READ enable
88 bit 7 is used with fast read and one shot controls CS de-assert?
89*/
90
Stefan Reinauera9424d52008-06-27 16:28:34 +000091#define ICH7_REG_SPIC 0x02 /* 16 Bits */
92#define SPIC_SCGO 0x0002
93#define SPIC_ACS 0x0004
94#define SPIC_SPOP 0x0008
Rudolf Marek3fdbccf2008-06-30 21:38:30 +000095#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +000096
97#define ICH7_REG_SPIA 0x04 /* 32 Bits */
98#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
99#define ICH7_REG_PREOP 0x54 /* 16 Bits */
100#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
101#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
102
FENG yu ningc05a2952008-12-08 18:16:58 +0000103/* ICH SPI configuration lock-down. May be set during chipset enabling. */
104int ichspi_lock = 0;
105
Dominik Geyerb46acba2008-05-16 12:55:55 +0000106typedef struct _OPCODE {
107 uint8_t opcode; //This commands spi opcode
108 uint8_t spi_type; //This commands spi type
109 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
110} OPCODE;
111
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000112/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000113 * Preop 1: Write Enable
114 * Preop 2: Write Status register enable
115 *
116 * OP 0: Write address
117 * OP 1: Read Address
118 * OP 2: ERASE block
119 * OP 3: Read Status register
120 * OP 4: Read ID
121 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000122 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000123 * OP 7: Chip erase
124 */
125typedef struct _OPCODES {
126 uint8_t preop[2];
127 OPCODE opcode[8];
128} OPCODES;
129
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000130static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000131
132/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000133static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000134{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000135 return mmio_readl(spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000136}
137
Uwe Hermann09e04f72009-05-16 22:36:00 +0000138static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000139{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000140 return mmio_readw(spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000141}
142
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000143#define REGWRITE32(X,Y) mmio_writel(Y, spibar+X)
144#define REGWRITE16(X,Y) mmio_writew(Y, spibar+X)
145#define REGWRITE8(X,Y) mmio_writeb(Y, spibar+X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000146
Dominik Geyerb46acba2008-05-16 12:55:55 +0000147/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000148static int find_opcode(OPCODES *op, uint8_t opcode);
149static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000150static int generate_opcodes(OPCODES * op);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000151static int program_opcodes(OPCODES * op);
Stefan Reinauer43119562008-11-02 19:51:50 +0000152static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000153 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000154
FENG yu ningf041e9b2008-12-15 02:32:11 +0000155/* for pairing opcodes with their required preop */
156struct preop_opcode_pair {
157 uint8_t preop;
158 uint8_t opcode;
159};
160
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000161/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000162struct preop_opcode_pair pops[] = {
163 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
164 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
165 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
166 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
167 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
168 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000169 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
170 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000171 {JEDEC_EWSR, JEDEC_WRSR},
172 {0,}
173};
174
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000175/* Reasonable default configuration. Needs ad-hoc modifications if we
176 * encounter unlisted opcodes. Fun.
177 */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000178OPCODES O_ST_M25P = {
179 {
180 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000181 JEDEC_EWSR,
182 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000183 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000184 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000185 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000186 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000187 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000188 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000189 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000190 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000191 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
192 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000193};
194
FENG yu ningc05a2952008-12-08 18:16:58 +0000195OPCODES O_EXISTING = {};
196
Uwe Hermann09e04f72009-05-16 22:36:00 +0000197static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000198{
199 int a;
200
201 for (a = 0; a < 8; a++) {
202 if (op->opcode[a].opcode == opcode)
203 return a;
204 }
205
206 return -1;
207}
208
Uwe Hermann09e04f72009-05-16 22:36:00 +0000209static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000210{
211 int a;
212
213 for (a = 0; a < 2; a++) {
214 if (op->preop[a] == preop)
215 return a;
216 }
217
218 return -1;
219}
220
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000221/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000222static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000223{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000224 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000225 uint16_t preop, optype;
226 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000227
228 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000229 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000230 return -1;
231 }
232
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000233 switch (spi_controller) {
234 case SPI_CONTROLLER_ICH7:
235 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000236 preop = REGREAD16(ICH7_REG_PREOP);
237 optype = REGREAD16(ICH7_REG_OPTYPE);
238 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
239 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
240 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000241 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000242 preop = REGREAD16(ICH9_REG_PREOP);
243 optype = REGREAD16(ICH9_REG_OPTYPE);
244 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
245 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
246 break;
247 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000248 msg_perr("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000249 return -1;
250 }
251
252 op->preop[0] = (uint8_t) preop;
253 op->preop[1] = (uint8_t) (preop >> 8);
254
255 for (a = 0; a < 8; a++) {
256 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
257 optype >>= 2;
258 }
259
260 for (a = 0; a < 4; a++) {
261 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
262 opmenu[0] >>= 8;
263 }
264
265 for (a = 4; a < 8; a++) {
266 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
267 opmenu[1] >>= 8;
268 }
269
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000270 /* No preopcodes used by default. */
271 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000272 op->opcode[a].atomic = 0;
273
FENG yu ningc05a2952008-12-08 18:16:58 +0000274 return 0;
275}
276
Dominik Geyerb46acba2008-05-16 12:55:55 +0000277int program_opcodes(OPCODES * op)
278{
279 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000280 uint16_t preop, optype;
281 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000282
283 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000284 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000285 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000286 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000287 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000288
Stefan Reinauera9424d52008-06-27 16:28:34 +0000289 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000290 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000291 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000292 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000293 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000294
Stefan Reinauera9424d52008-06-27 16:28:34 +0000295 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000296 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000297 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000298 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000299 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000300
Dominik Geyerb46acba2008-05-16 12:55:55 +0000301 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000302 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000303 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000304 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000305 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000306
Sean Nelson316a29f2010-05-07 20:09:04 +0000307 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 +0000308 switch (spi_controller) {
309 case SPI_CONTROLLER_ICH7:
310 case SPI_CONTROLLER_VIA:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000311 REGWRITE16(ICH7_REG_PREOP, preop);
312 REGWRITE16(ICH7_REG_OPTYPE, optype);
313 REGWRITE32(ICH7_REG_OPMENU, opmenu[0]);
314 REGWRITE32(ICH7_REG_OPMENU + 4, opmenu[1]);
315 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000316 case SPI_CONTROLLER_ICH9:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000317 REGWRITE16(ICH9_REG_PREOP, preop);
318 REGWRITE16(ICH9_REG_OPTYPE, optype);
319 REGWRITE32(ICH9_REG_OPMENU, opmenu[0]);
320 REGWRITE32(ICH9_REG_OPMENU + 4, opmenu[1]);
321 break;
322 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000323 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000324 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000325 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000326
327 return 0;
328}
329
FENG yu ningf041e9b2008-12-15 02:32:11 +0000330/* This function generates OPCODES from or programs OPCODES to ICH according to
331 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000332 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000333 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000334 */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000335int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000336{
337 int rc = 0;
338 OPCODES *curopcodes_done;
339
340 if (curopcodes)
341 return 0;
342
343 if (ichspi_lock) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000344 msg_pdbg("Generating OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000345 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000346 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000347 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000348 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000349 curopcodes_done = &O_ST_M25P;
350 rc = program_opcodes(curopcodes_done);
351 }
352
353 if (rc) {
354 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000355 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000356 return 1;
357 } else {
358 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000359 msg_pdbg("done\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000360 return 0;
361 }
362}
363
Stefan Reinauer43119562008-11-02 19:51:50 +0000364static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000365 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000366{
367 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000368 int timeout;
Peter Stuge7e2c0792008-06-29 01:30:41 +0000369 uint32_t temp32 = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000370 uint16_t temp16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000371 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000372 uint64_t opmenu;
373 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000374
375 /* Is it a write command? */
376 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
377 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
378 write_cmd = 1;
379 }
380
381 /* Programm Offset in Flash into FADDR */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000382 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000383
384 /* Program data into FDATA0 to N */
385 if (write_cmd && (datalength != 0)) {
386 temp32 = 0;
387 for (a = 0; a < datalength; a++) {
388 if ((a % 4) == 0) {
389 temp32 = 0;
390 }
391
392 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
393
394 if ((a % 4) == 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000395 REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)),
396 temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000397 }
398 }
399 if (((a - 1) % 4) != 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000400 REGWRITE32(ICH7_REG_SPID0 +
401 ((a - 1) - ((a - 1) % 4)), temp32);
402 }
403
404 }
405
406 /* Assemble SPIS */
407 temp16 = 0;
408 /* clear error status registers */
409 temp16 |= (SPIS_CDS + SPIS_FCERR);
410 REGWRITE16(ICH7_REG_SPIS, temp16);
411
412 /* Assemble SPIC */
413 temp16 = 0;
414
415 if (datalength != 0) {
416 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000417 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000418 }
419
420 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000421 opmenu = REGREAD32(ICH7_REG_OPMENU);
422 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
423
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000424 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
425 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000426 break;
427 }
428 opmenu >>= 8;
429 }
430 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000431 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000432 return 1;
433 }
434 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000435
436 /* Handle Atomic */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000437 switch (op.atomic) {
438 case 2:
439 /* Select second preop. */
440 temp16 |= SPIC_SPOP;
441 /* And fall through. */
442 case 1:
443 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000444 temp16 |= SPIC_ACS;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000445 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000446 }
447
448 /* Start */
449 temp16 |= SPIC_SCGO;
450
451 /* write it */
452 REGWRITE16(ICH7_REG_SPIC, temp16);
453
454 /* wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000455 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauera9424d52008-06-27 16:28:34 +0000456 while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000457 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000458 }
459 if (!timeout) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000460 msg_perr("timeout\n");
Stefan Reinauera9424d52008-06-27 16:28:34 +0000461 }
462
Sean Nelson316a29f2010-05-07 20:09:04 +0000463 /* FIXME: make sure we do not needlessly cause transaction errors. */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000464 if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000465 msg_pdbg("Transaction error!\n");
Stefan Reinauera9424d52008-06-27 16:28:34 +0000466 return 1;
467 }
468
469 if ((!write_cmd) && (datalength != 0)) {
470 for (a = 0; a < datalength; a++) {
471 if ((a % 4) == 0) {
472 temp32 = REGREAD32(ICH7_REG_SPID0 + (a));
473 }
474
475 data[a] =
476 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
477 >> ((a % 4) * 8);
478 }
479 }
480
481 return 0;
482}
483
Stefan Reinauer43119562008-11-02 19:51:50 +0000484static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000485 uint8_t datalength, uint8_t * data)
486{
487 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000488 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000489 uint32_t temp32;
490 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000491 uint64_t opmenu;
492 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000493
494 /* Is it a write command? */
495 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
496 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
497 write_cmd = 1;
498 }
499
500 /* Programm Offset in Flash into FADDR */
501 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
502
503 /* Program data into FDATA0 to N */
504 if (write_cmd && (datalength != 0)) {
505 temp32 = 0;
506 for (a = 0; a < datalength; a++) {
507 if ((a % 4) == 0) {
508 temp32 = 0;
509 }
510
511 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
512
513 if ((a % 4) == 3) {
514 REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
515 temp32);
516 }
517 }
518 if (((a - 1) % 4) != 3) {
519 REGWRITE32(ICH9_REG_FDATA0 +
520 ((a - 1) - ((a - 1) % 4)), temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000521 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000522 }
523
524 /* Assemble SSFS + SSFC */
525 temp32 = 0;
526
527 /* clear error status registers */
528 temp32 |= (SSFS_CDS + SSFS_FCERR);
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000529 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000530 temp32 |= SSFC_SCF_20MHZ;
531
532 if (datalength != 0) {
533 uint32_t datatemp;
534 temp32 |= SSFC_DS;
535 datatemp = ((uint32_t) ((datalength - 1) & 0x3f)) << (8 + 8);
536 temp32 |= datatemp;
537 }
538
539 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000540 opmenu = REGREAD32(ICH9_REG_OPMENU);
541 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
542
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000543 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
544 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000545 break;
546 }
547 opmenu >>= 8;
548 }
549 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000550 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000551 return 1;
552 }
553 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000554
555 /* Handle Atomic */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000556 switch (op.atomic) {
557 case 2:
558 /* Select second preop. */
559 temp32 |= SSFC_SPOP;
560 /* And fall through. */
561 case 1:
562 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000563 temp32 |= SSFC_ACS;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000564 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000565 }
566
567 /* Start */
568 temp32 |= SSFC_SCGO;
569
570 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000571 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000572
573 /*wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000574 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000575 while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000576 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000577 }
578 if (!timeout) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000579 msg_perr("timeout\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000580 }
581
Sean Nelson316a29f2010-05-07 20:09:04 +0000582 /* FIXME make sure we do not needlessly cause transaction errors. */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000583 if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000584 msg_pdbg("Transaction error!\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000585 return 1;
586 }
587
588 if ((!write_cmd) && (datalength != 0)) {
589 for (a = 0; a < datalength; a++) {
590 if ((a % 4) == 0) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000591 temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
Dominik Geyerb46acba2008-05-16 12:55:55 +0000592 }
593
594 data[a] =
Stefan Reinauera9424d52008-06-27 16:28:34 +0000595 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
596 >> ((a % 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000597 }
598 }
599
600 return 0;
601}
602
Stefan Reinauer43119562008-11-02 19:51:50 +0000603static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000604 uint8_t datalength, uint8_t * data)
605{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000606 switch (spi_controller) {
607 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000608 if (datalength > 16) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000609 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000610 "opcode 0x%02x, got datalength=%i, want <=16\n",
611 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000612 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000613 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000614 return ich7_run_opcode(op, offset, datalength, data, 16);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000615 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000616 if (datalength > 64) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000617 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000618 "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, 64);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000623 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000624 if (datalength > 64) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000625 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000626 "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 ich9_run_opcode(op, offset, datalength, data);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000631 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000632 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000633 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000634
635 /* If we ever get here, something really weird happened */
636 return -1;
637}
638
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000639int ich_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000640{
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000641 int maxdata = 64;
642
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000643 if (spi_controller == SPI_CONTROLLER_VIA)
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000644 maxdata = 16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000645
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000646 return spi_read_chunked(flash, buf, start, len, maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000647}
648
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000649int ich_spi_write_256(struct flashchip *flash, uint8_t * buf)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000650{
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000651 int i, ret = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000652 int total_size = flash->total_size * 1024;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000653 int erase_size = 64 * 1024;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000654 int maxdata = 64;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000655
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000656 if (spi_controller == SPI_CONTROLLER_VIA)
657 maxdata = 16;
658
Dominik Geyerb46acba2008-05-16 12:55:55 +0000659 spi_disable_blockprotect();
Carl-Daniel Hailfinger96123032009-11-25 02:07:30 +0000660 /* Erase first */
Sean Nelson316a29f2010-05-07 20:09:04 +0000661 msg_pinfo("Erasing flash before programming... ");
Carl-Daniel Hailfinger96123032009-11-25 02:07:30 +0000662 if (erase_flash(flash)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000663 msg_perr("ERASE FAILED!\n");
Carl-Daniel Hailfinger96123032009-11-25 02:07:30 +0000664 return -1;
665 }
Sean Nelson316a29f2010-05-07 20:09:04 +0000666 msg_pinfo("done.\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000667
Sean Nelson316a29f2010-05-07 20:09:04 +0000668 msg_pinfo("Programming page: \n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000669 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000670 ret = spi_write_chunked(flash, buf + (i * erase_size),
671 i * erase_size, erase_size, maxdata);
672 if (ret)
673 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000674 }
675
Sean Nelson316a29f2010-05-07 20:09:04 +0000676 msg_pinfo("\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000677
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000678 return ret;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000679}
680
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000681int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000682 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000683{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000684 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000685 int opcode_index = -1;
686 const unsigned char cmd = *writearr;
687 OPCODE *opcode;
688 uint32_t addr = 0;
689 uint8_t *data;
690 int count;
691
Dominik Geyerb46acba2008-05-16 12:55:55 +0000692 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000693 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000694 if (opcode_index == -1) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000695 /* FIXME: Reprogram opcodes if possible. Autodetect type of
696 * opcode by checking readcnt/writecnt.
697 */
Sean Nelson316a29f2010-05-07 20:09:04 +0000698 msg_pdbg("Invalid OPCODE 0x%02x\n", cmd);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000699 return SPI_INVALID_OPCODE;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000700 }
701
702 opcode = &(curopcodes->opcode[opcode_index]);
703
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000704 /* The following valid writecnt/readcnt combinations exist:
705 * writecnt = 4, readcnt >= 0
706 * writecnt = 1, readcnt >= 0
707 * writecnt >= 4, readcnt = 0
708 * writecnt >= 1, readcnt = 0
709 * writecnt >= 1 is guaranteed for all commands.
710 */
711 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
712 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000713 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000714 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
715 writecnt);
716 return SPI_INVALID_LENGTH;
717 }
718 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
719 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000720 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000721 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
722 writecnt);
723 return SPI_INVALID_LENGTH;
724 }
725 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
726 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000727 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000728 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
729 writecnt);
730 return SPI_INVALID_LENGTH;
731 }
732 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
733 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
734 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000735 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000736 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
737 readcnt);
738 return SPI_INVALID_LENGTH;
739 }
740
Dominik Geyerb46acba2008-05-16 12:55:55 +0000741 /* if opcode-type requires an address */
742 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
743 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000744 addr = (writearr[1] << 16) |
745 (writearr[2] << 8) | (writearr[3] << 0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000746 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000747
Dominik Geyerb46acba2008-05-16 12:55:55 +0000748 /* translate read/write array/count */
749 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000750 data = (uint8_t *) (writearr + 1);
751 count = writecnt - 1;
752 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
753 data = (uint8_t *) (writearr + 4);
754 count = writecnt - 4;
755 } else {
756 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000757 count = readcnt;
758 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000759
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000760 result = run_opcode(*opcode, addr, count, data);
761 if (result) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000762 msg_pdbg("run OPCODE 0x%02x failed\n", opcode->opcode);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000763 }
764
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000765 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000766}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000767
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000768int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000769{
770 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000771 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000772 int oppos, preoppos;
773 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000774 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000775 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000776 preoppos = find_preop(curopcodes, cmds->writearr[0]);
777 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000778 if ((oppos == -1) && (preoppos != -1)) {
779 /* Current command is listed as preopcode in
780 * ICH struct OPCODES, but next command is not
781 * listed as opcode in that struct.
782 * Check for command sanity, then
783 * try to reprogram the ICH opcode list.
784 */
785 if (find_preop(curopcodes,
786 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000787 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000788 "preopcodes 0x%02x and 0x%02x, "
789 "ignoring the first.\n",
790 __func__, cmds->writearr[0],
791 (cmds + 1)->writearr[0]);
792 continue;
793 }
794 /* If the chipset is locked down, we'll fail
795 * during execution of the next command anyway.
796 * No need to bother with fixups.
797 */
798 if (!ichspi_lock) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000799 msg_pdbg("%s: FIXME: Add on-the-fly"
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000800 " reprogramming of the "
801 "chipset opcode list.\n",
802 __func__);
803 /* FIXME: Reprogram opcode menu.
804 * Find a less-useful opcode, replace it
805 * with the wanted opcode, detect optype
806 * and reprogram the opcode menu.
807 * Update oppos so the next if-statement
808 * can do something useful.
809 */
810 //curopcodes.opcode[lessusefulindex] = (cmds + 1)->writearr[0]);
811 //update_optypes(curopcodes);
812 //program_opcodes(curopcodes);
813 //oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
814 continue;
815 }
816 }
817 if ((oppos != -1) && (preoppos != -1)) {
818 /* Current command is listed as preopcode in
819 * ICH struct OPCODES and next command is listed
820 * as opcode in that struct. Match them up.
821 */
822 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000823 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000824 }
825 /* If none of the above if-statements about oppos or
826 * preoppos matched, this is a normal opcode.
827 */
828 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000829 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
830 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000831 /* Reset the type of all opcodes to non-atomic. */
832 for (i = 0; i < 8; i++)
833 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000834 }
835 return ret;
836}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000837
838#endif