blob: a6bf15459aac6e6d998726f72da97be97f715a51 [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>
Dominik Geyerb46acba2008-05-16 12:55:55 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Dominik Geyerb46acba2008-05-16 12:55:55 +000022 */
23
24/*
25 * This module is designed for supporting the devices
26 * ST M25P40
27 * ST M25P80
28 * ST M25P16
29 * ST M25P32 already tested
30 * ST M25P64
31 * AT 25DF321 already tested
32 *
33 */
34
Dominik Geyerb46acba2008-05-16 12:55:55 +000035#include <string.h>
Dominik Geyerb46acba2008-05-16 12:55:55 +000036#include "flash.h"
37#include "spi.h"
38
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +000039/* Change this to #define if you want lowlevel debugging of commands
40 * sent to the ICH/VIA SPI controller.
41 */
42#undef COMM_DEBUG
43
44#ifdef COMM_DEBUG
45#define msg_comm_debug printf_debug
46#else
47#define msg_comm_debug(...) do {} while (0)
48#endif
49
Stefan Reinauera9424d52008-06-27 16:28:34 +000050/* ICH9 controller register definition */
51#define ICH9_REG_FADDR 0x08 /* 32 Bits */
52#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
53
54#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000055#define SSFS_SCIP 0x00000001
56#define SSFS_CDS 0x00000004
57#define SSFS_FCERR 0x00000008
58#define SSFS_AEL 0x00000010
Stefan Reinauera9424d52008-06-27 16:28:34 +000059
60#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000061#define SSFC_SCGO 0x00000200
62#define SSFC_ACS 0x00000400
63#define SSFC_SPOP 0x00000800
64#define SSFC_COP 0x00001000
65#define SSFC_DBC 0x00010000
66#define SSFC_DS 0x00400000
67#define SSFC_SME 0x00800000
68#define SSFC_SCF 0x01000000
69#define SSFC_SCF_20MHZ 0x00000000
70#define SSFC_SCF_33MHZ 0x01000000
Stefan Reinauera9424d52008-06-27 16:28:34 +000071
72#define ICH9_REG_PREOP 0x94 /* 16 Bits */
73#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
74#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000075
76// ICH9R SPI commands
77#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
78#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
79#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
80#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
81
Stefan Reinauera9424d52008-06-27 16:28:34 +000082// ICH7 registers
83#define ICH7_REG_SPIS 0x00 /* 16 Bits */
84#define SPIS_SCIP 0x00000001
85#define SPIS_CDS 0x00000004
86#define SPIS_FCERR 0x00000008
87
Rudolf Marek3fdbccf2008-06-30 21:38:30 +000088/* VIA SPI is compatible with ICH7, but maxdata
89 to transfer is 16 bytes.
90
91 DATA byte count on ICH7 is 8:13, on VIA 8:11
92
93 bit 12 is port select CS0 CS1
94 bit 13 is FAST READ enable
95 bit 7 is used with fast read and one shot controls CS de-assert?
96*/
97
Stefan Reinauera9424d52008-06-27 16:28:34 +000098#define ICH7_REG_SPIC 0x02 /* 16 Bits */
99#define SPIC_SCGO 0x0002
100#define SPIC_ACS 0x0004
101#define SPIC_SPOP 0x0008
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000102#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000103
104#define ICH7_REG_SPIA 0x04 /* 32 Bits */
105#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
106#define ICH7_REG_PREOP 0x54 /* 16 Bits */
107#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
108#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
109
FENG yu ningc05a2952008-12-08 18:16:58 +0000110/* ICH SPI configuration lock-down. May be set during chipset enabling. */
111int ichspi_lock = 0;
112
Dominik Geyerb46acba2008-05-16 12:55:55 +0000113typedef struct _OPCODE {
114 uint8_t opcode; //This commands spi opcode
115 uint8_t spi_type; //This commands spi type
116 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
117} OPCODE;
118
119/* Opcode definition:
120 * Preop 1: Write Enable
121 * Preop 2: Write Status register enable
122 *
123 * OP 0: Write address
124 * OP 1: Read Address
125 * OP 2: ERASE block
126 * OP 3: Read Status register
127 * OP 4: Read ID
128 * OP 5: Write Status register
129 * OP 6: chip private (read JDEC id)
130 * OP 7: Chip erase
131 */
132typedef struct _OPCODES {
133 uint8_t preop[2];
134 OPCODE opcode[8];
135} OPCODES;
136
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000137static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000138
139/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000140static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000141{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000142 return mmio_readl(spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000143}
144
Uwe Hermann09e04f72009-05-16 22:36:00 +0000145static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000146{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000147 return mmio_readw(spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000148}
149
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000150#define REGWRITE32(X,Y) mmio_writel(Y, spibar+X)
151#define REGWRITE16(X,Y) mmio_writew(Y, spibar+X)
152#define REGWRITE8(X,Y) mmio_writeb(Y, spibar+X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000153
Dominik Geyerb46acba2008-05-16 12:55:55 +0000154/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000155static int find_opcode(OPCODES *op, uint8_t opcode);
156static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000157static int generate_opcodes(OPCODES * op);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000158static int program_opcodes(OPCODES * op);
Stefan Reinauer43119562008-11-02 19:51:50 +0000159static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000160 uint8_t datalength, uint8_t * data);
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000161static int ich_spi_write_page(struct flashchip *flash, uint8_t * bytes,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000162 int offset, int maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000163
FENG yu ningf041e9b2008-12-15 02:32:11 +0000164/* for pairing opcodes with their required preop */
165struct preop_opcode_pair {
166 uint8_t preop;
167 uint8_t opcode;
168};
169
170struct preop_opcode_pair pops[] = {
171 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
172 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
173 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
174 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
175 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
176 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
177 {JEDEC_EWSR, JEDEC_WRSR},
178 {0,}
179};
180
Dominik Geyerb46acba2008-05-16 12:55:55 +0000181OPCODES O_ST_M25P = {
182 {
183 JEDEC_WREN,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000184 0},
Dominik Geyerb46acba2008-05-16 12:55:55 +0000185 {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000186 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 1}, // Write Byte
187 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
188 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 1}, // Erase Sector
189 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000190 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000191 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 1}, // Write Status Register
192 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
193 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 1}, // Bulk erase
194 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000195};
196
FENG yu ningc05a2952008-12-08 18:16:58 +0000197OPCODES O_EXISTING = {};
198
Uwe Hermann09e04f72009-05-16 22:36:00 +0000199static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000200{
201 int a;
202
203 for (a = 0; a < 8; a++) {
204 if (op->opcode[a].opcode == opcode)
205 return a;
206 }
207
208 return -1;
209}
210
Uwe Hermann09e04f72009-05-16 22:36:00 +0000211static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000212{
213 int a;
214
215 for (a = 0; a < 2; a++) {
216 if (op->preop[a] == preop)
217 return a;
218 }
219
220 return -1;
221}
222
FENG yu ningf041e9b2008-12-15 02:32:11 +0000223static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000224{
FENG yu ningf041e9b2008-12-15 02:32:11 +0000225 int a, b, i;
FENG yu ningc05a2952008-12-08 18:16:58 +0000226 uint16_t preop, optype;
227 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000228
229 if (op == NULL) {
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000230 printf_debug("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000231 return -1;
232 }
233
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000234 switch (spi_controller) {
235 case SPI_CONTROLLER_ICH7:
236 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000237 preop = REGREAD16(ICH7_REG_PREOP);
238 optype = REGREAD16(ICH7_REG_OPTYPE);
239 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
240 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
241 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000242 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000243 preop = REGREAD16(ICH9_REG_PREOP);
244 optype = REGREAD16(ICH9_REG_OPTYPE);
245 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
246 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
247 break;
248 default:
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000249 printf_debug("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000250 return -1;
251 }
252
253 op->preop[0] = (uint8_t) preop;
254 op->preop[1] = (uint8_t) (preop >> 8);
255
256 for (a = 0; a < 8; a++) {
257 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
258 optype >>= 2;
259 }
260
261 for (a = 0; a < 4; a++) {
262 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
263 opmenu[0] >>= 8;
264 }
265
266 for (a = 4; a < 8; a++) {
267 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
268 opmenu[1] >>= 8;
269 }
270
271 /* atomic (link opcode with required pre-op) */
272 for (a = 4; a < 8; a++)
273 op->opcode[a].atomic = 0;
274
FENG yu ningf041e9b2008-12-15 02:32:11 +0000275 for (i = 0; pops[i].opcode; i++) {
276 a = find_opcode(op, pops[i].opcode);
277 b = find_preop(op, pops[i].preop);
278 if ((a != -1) && (b != -1))
279 op->opcode[a].atomic = (uint8_t) ++b;
FENG yu ningc05a2952008-12-08 18:16:58 +0000280 }
281
282 return 0;
283}
284
Dominik Geyerb46acba2008-05-16 12:55:55 +0000285int program_opcodes(OPCODES * op)
286{
287 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000288 uint16_t preop, optype;
289 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000290
291 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000292 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000293 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000294 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000295 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000296
Stefan Reinauera9424d52008-06-27 16:28:34 +0000297 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000298 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000299 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000300 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000301 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000302
Stefan Reinauera9424d52008-06-27 16:28:34 +0000303 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000304 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000305 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000306 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000307 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000308
Dominik Geyerb46acba2008-05-16 12:55:55 +0000309 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000310 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000311 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000312 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000313 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000314
Peter Stuge016d4e12009-01-15 02:13:18 +0000315 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 +0000316 switch (spi_controller) {
317 case SPI_CONTROLLER_ICH7:
318 case SPI_CONTROLLER_VIA:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000319 REGWRITE16(ICH7_REG_PREOP, preop);
320 REGWRITE16(ICH7_REG_OPTYPE, optype);
321 REGWRITE32(ICH7_REG_OPMENU, opmenu[0]);
322 REGWRITE32(ICH7_REG_OPMENU + 4, opmenu[1]);
323 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000324 case SPI_CONTROLLER_ICH9:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000325 REGWRITE16(ICH9_REG_PREOP, preop);
326 REGWRITE16(ICH9_REG_OPTYPE, optype);
327 REGWRITE32(ICH9_REG_OPMENU, opmenu[0]);
328 REGWRITE32(ICH9_REG_OPMENU + 4, opmenu[1]);
329 break;
330 default:
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000331 printf_debug("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000332 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000333 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000334
335 return 0;
336}
337
FENG yu ningf041e9b2008-12-15 02:32:11 +0000338/* This function generates OPCODES from or programs OPCODES to ICH according to
339 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000340 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000341 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000342 */
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000343int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000344{
345 int rc = 0;
346 OPCODES *curopcodes_done;
347
348 if (curopcodes)
349 return 0;
350
351 if (ichspi_lock) {
352 printf_debug("Generating OPCODES... ");
353 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000354 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000355 } else {
356 printf_debug("Programming OPCODES... ");
357 curopcodes_done = &O_ST_M25P;
358 rc = program_opcodes(curopcodes_done);
359 }
360
361 if (rc) {
362 curopcodes = NULL;
363 printf_debug("failed\n");
364 return 1;
365 } else {
366 curopcodes = curopcodes_done;
367 printf_debug("done\n");
368 return 0;
369 }
370}
371
Stefan Reinauer43119562008-11-02 19:51:50 +0000372static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000373 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000374{
375 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000376 int timeout;
Peter Stuge7e2c0792008-06-29 01:30:41 +0000377 uint32_t temp32 = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000378 uint16_t temp16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000379 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000380 uint64_t opmenu;
381 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000382
383 /* Is it a write command? */
384 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
385 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
386 write_cmd = 1;
387 }
388
389 /* Programm Offset in Flash into FADDR */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000390 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000391
392 /* Program data into FDATA0 to N */
393 if (write_cmd && (datalength != 0)) {
394 temp32 = 0;
395 for (a = 0; a < datalength; a++) {
396 if ((a % 4) == 0) {
397 temp32 = 0;
398 }
399
400 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
401
402 if ((a % 4) == 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000403 REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)),
404 temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000405 }
406 }
407 if (((a - 1) % 4) != 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000408 REGWRITE32(ICH7_REG_SPID0 +
409 ((a - 1) - ((a - 1) % 4)), temp32);
410 }
411
412 }
413
414 /* Assemble SPIS */
415 temp16 = 0;
416 /* clear error status registers */
417 temp16 |= (SPIS_CDS + SPIS_FCERR);
418 REGWRITE16(ICH7_REG_SPIS, temp16);
419
420 /* Assemble SPIC */
421 temp16 = 0;
422
423 if (datalength != 0) {
424 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000425 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000426 }
427
428 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000429 opmenu = REGREAD32(ICH7_REG_OPMENU);
430 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
431
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000432 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
433 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000434 break;
435 }
436 opmenu >>= 8;
437 }
438 if (opcode_index == 8) {
439 printf_debug("Opcode %x not found.\n", op.opcode);
440 return 1;
441 }
442 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000443
444 /* Handle Atomic */
445 if (op.atomic != 0) {
446 /* Select atomic command */
447 temp16 |= SPIC_ACS;
Carl-Daniel Hailfinger738fdff2008-11-18 00:43:14 +0000448 /* Select prefix opcode */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000449 if ((op.atomic - 1) == 1) {
450 /*Select prefix opcode 2 */
451 temp16 |= SPIC_SPOP;
452 }
453 }
454
455 /* Start */
456 temp16 |= SPIC_SCGO;
457
458 /* write it */
459 REGWRITE16(ICH7_REG_SPIC, temp16);
460
461 /* wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000462 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauera9424d52008-06-27 16:28:34 +0000463 while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000464 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000465 }
466 if (!timeout) {
467 printf_debug("timeout\n");
468 }
469
470 if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) {
471 printf_debug("Transaction error!\n");
472 return 1;
473 }
474
475 if ((!write_cmd) && (datalength != 0)) {
476 for (a = 0; a < datalength; a++) {
477 if ((a % 4) == 0) {
478 temp32 = REGREAD32(ICH7_REG_SPID0 + (a));
479 }
480
481 data[a] =
482 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
483 >> ((a % 4) * 8);
484 }
485 }
486
487 return 0;
488}
489
Stefan Reinauer43119562008-11-02 19:51:50 +0000490static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000491 uint8_t datalength, uint8_t * data)
492{
493 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000494 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000495 uint32_t temp32;
496 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000497 uint64_t opmenu;
498 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000499
500 /* Is it a write command? */
501 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
502 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
503 write_cmd = 1;
504 }
505
506 /* Programm Offset in Flash into FADDR */
507 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
508
509 /* Program data into FDATA0 to N */
510 if (write_cmd && (datalength != 0)) {
511 temp32 = 0;
512 for (a = 0; a < datalength; a++) {
513 if ((a % 4) == 0) {
514 temp32 = 0;
515 }
516
517 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
518
519 if ((a % 4) == 3) {
520 REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
521 temp32);
522 }
523 }
524 if (((a - 1) % 4) != 3) {
525 REGWRITE32(ICH9_REG_FDATA0 +
526 ((a - 1) - ((a - 1) % 4)), temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000527 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000528 }
529
530 /* Assemble SSFS + SSFC */
531 temp32 = 0;
532
533 /* clear error status registers */
534 temp32 |= (SSFS_CDS + SSFS_FCERR);
535 /* USE 20 MhZ */
536 temp32 |= SSFC_SCF_20MHZ;
537
538 if (datalength != 0) {
539 uint32_t datatemp;
540 temp32 |= SSFC_DS;
541 datatemp = ((uint32_t) ((datalength - 1) & 0x3f)) << (8 + 8);
542 temp32 |= datatemp;
543 }
544
545 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000546 opmenu = REGREAD32(ICH9_REG_OPMENU);
547 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
548
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000549 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
550 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000551 break;
552 }
553 opmenu >>= 8;
554 }
555 if (opcode_index == 8) {
556 printf_debug("Opcode %x not found.\n", op.opcode);
557 return 1;
558 }
559 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000560
561 /* Handle Atomic */
562 if (op.atomic != 0) {
563 /* Select atomic command */
564 temp32 |= SSFC_ACS;
565 /* Selct prefix opcode */
566 if ((op.atomic - 1) == 1) {
567 /*Select prefix opcode 2 */
568 temp32 |= SSFC_SPOP;
569 }
570 }
571
572 /* Start */
573 temp32 |= SSFC_SCGO;
574
575 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000576 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000577
578 /*wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000579 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000580 while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000581 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000582 }
583 if (!timeout) {
584 printf_debug("timeout\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000585 }
586
Stefan Reinauera9424d52008-06-27 16:28:34 +0000587 if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000588 printf_debug("Transaction error!\n");
589 return 1;
590 }
591
592 if ((!write_cmd) && (datalength != 0)) {
593 for (a = 0; a < datalength; a++) {
594 if ((a % 4) == 0) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000595 temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
Dominik Geyerb46acba2008-05-16 12:55:55 +0000596 }
597
598 data[a] =
Stefan Reinauera9424d52008-06-27 16:28:34 +0000599 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
600 >> ((a % 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000601 }
602 }
603
604 return 0;
605}
606
Stefan Reinauer43119562008-11-02 19:51:50 +0000607static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000608 uint8_t datalength, uint8_t * data)
609{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000610 switch (spi_controller) {
611 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000612 if (datalength > 16)
613 return SPI_INVALID_LENGTH;
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 Hailfinger142e30f2009-07-14 10:26:56 +0000616 if (datalength > 64)
617 return SPI_INVALID_LENGTH;
Stefan Reinauer43119562008-11-02 19:51:50 +0000618 return ich7_run_opcode(op, offset, datalength, data, 64);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000619 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000620 if (datalength > 64)
621 return SPI_INVALID_LENGTH;
Stefan Reinauer43119562008-11-02 19:51:50 +0000622 return ich9_run_opcode(op, offset, datalength, data);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000623 default:
Uwe Hermann04aa59a2009-09-02 22:09:00 +0000624 printf_debug("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000625 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000626
627 /* If we ever get here, something really weird happened */
628 return -1;
629}
630
Dominik Geyerb46acba2008-05-16 12:55:55 +0000631static int ich_spi_write_page(struct flashchip *flash, uint8_t * bytes,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000632 int offset, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000633{
634 int page_size = flash->page_size;
635 uint32_t remaining = page_size;
Carl-Daniel Hailfingerd1680572009-06-17 10:13:42 +0000636 int towrite;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000637
Carl-Daniel Hailfinger5609fa72010-01-07 03:32:17 +0000638 msg_comm_debug("ich_spi_write_page: offset=%d, number=%d, buf=%p\n",
Stefan Reinauera9424d52008-06-27 16:28:34 +0000639 offset, page_size, bytes);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000640
Carl-Daniel Hailfingerd1680572009-06-17 10:13:42 +0000641 for (; remaining > 0; remaining -= towrite) {
642 towrite = min(remaining, maxdata);
643 if (spi_nbyte_program(offset + (page_size - remaining),
644 &bytes[page_size - remaining], towrite)) {
645 printf_debug("Error writing");
646 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000647 }
648 }
649
650 return 0;
651}
652
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000653int ich_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000654{
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000655 int maxdata = 64;
656
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000657 if (spi_controller == SPI_CONTROLLER_VIA)
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000658 maxdata = 16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000659
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000660 return spi_read_chunked(flash, buf, start, len, maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000661}
662
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000663int ich_spi_write_256(struct flashchip *flash, uint8_t * buf)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000664{
665 int i, j, rc = 0;
666 int total_size = flash->total_size * 1024;
667 int page_size = flash->page_size;
668 int erase_size = 64 * 1024;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000669 int maxdata = 64;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000670
671 spi_disable_blockprotect();
Carl-Daniel Hailfinger96123032009-11-25 02:07:30 +0000672 /* Erase first */
673 printf("Erasing flash before programming... ");
674 if (erase_flash(flash)) {
675 fprintf(stderr, "ERASE FAILED!\n");
676 return -1;
677 }
678 printf("done.\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000679
680 printf("Programming page: \n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000681 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000682 if (spi_controller == SPI_CONTROLLER_VIA)
Peter Stuge6a214162008-07-07 05:14:06 +0000683 maxdata = 16;
684
Dominik Geyerb46acba2008-05-16 12:55:55 +0000685 for (j = 0; j < erase_size / page_size; j++) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000686 ich_spi_write_page(flash,
687 (void *)(buf + (i * erase_size) + (j * page_size)),
688 (i * erase_size) + (j * page_size), maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000689 }
690 }
691
692 printf("\n");
693
694 return rc;
695}
696
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000697int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000698 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000699{
700 int a;
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000701 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000702 int opcode_index = -1;
703 const unsigned char cmd = *writearr;
704 OPCODE *opcode;
705 uint32_t addr = 0;
706 uint8_t *data;
707 int count;
708
Dominik Geyerb46acba2008-05-16 12:55:55 +0000709 /* find cmd in opcodes-table */
710 for (a = 0; a < 8; a++) {
711 if ((curopcodes->opcode[a]).opcode == cmd) {
712 opcode_index = a;
713 break;
714 }
715 }
716
717 /* unknown / not programmed command */
718 if (opcode_index == -1) {
719 printf_debug("Invalid OPCODE 0x%02x\n", cmd);
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000720 return SPI_INVALID_OPCODE;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000721 }
722
723 opcode = &(curopcodes->opcode[opcode_index]);
724
725 /* if opcode-type requires an address */
726 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
727 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000728 addr = (writearr[1] << 16) |
729 (writearr[2] << 8) | (writearr[3] << 0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000730 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000731
Dominik Geyerb46acba2008-05-16 12:55:55 +0000732 /* translate read/write array/count */
733 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000734 data = (uint8_t *) (writearr + 1);
735 count = writecnt - 1;
736 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
737 data = (uint8_t *) (writearr + 4);
738 count = writecnt - 4;
739 } else {
740 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000741 count = readcnt;
742 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000743
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000744 result = run_opcode(*opcode, addr, count, data);
745 if (result) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000746 printf_debug("run OPCODE 0x%02x failed\n", opcode->opcode);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000747 }
748
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000749 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000750}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000751
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000752int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000753{
754 int ret = 0;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000755 int oppos, preoppos;
756 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
757 /* Is the next command valid or a terminator? */
758 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
759 preoppos = find_preop(curopcodes, cmds->writearr[0]);
760 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
761 /* Is the opcode of the current command listed in the
762 * ICH struct OPCODES as associated preopcode for the
763 * opcode of the next command?
764 */
765 if ((oppos != -1) && (preoppos != -1) &&
Carl-Daniel Hailfinger0709a782009-10-22 15:07:30 +0000766 ((curopcodes->opcode[oppos].atomic - 1) == preoppos))
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000767 continue;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000768 }
769
770 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
771 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000772 }
773 return ret;
774}