blob: e3fb7400508e0a25c48ab9d34a9b138f00b4410c [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 <sys/mman.h>
Dominik Geyerb46acba2008-05-16 12:55:55 +000037#include "flash.h"
38#include "spi.h"
39
Stefan Reinauera9424d52008-06-27 16:28:34 +000040/* ICH9 controller register definition */
41#define ICH9_REG_FADDR 0x08 /* 32 Bits */
42#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
43
44#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000045#define SSFS_SCIP 0x00000001
46#define SSFS_CDS 0x00000004
47#define SSFS_FCERR 0x00000008
48#define SSFS_AEL 0x00000010
Stefan Reinauera9424d52008-06-27 16:28:34 +000049
50#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000051#define SSFC_SCGO 0x00000200
52#define SSFC_ACS 0x00000400
53#define SSFC_SPOP 0x00000800
54#define SSFC_COP 0x00001000
55#define SSFC_DBC 0x00010000
56#define SSFC_DS 0x00400000
57#define SSFC_SME 0x00800000
58#define SSFC_SCF 0x01000000
59#define SSFC_SCF_20MHZ 0x00000000
60#define SSFC_SCF_33MHZ 0x01000000
Stefan Reinauera9424d52008-06-27 16:28:34 +000061
62#define ICH9_REG_PREOP 0x94 /* 16 Bits */
63#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
64#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000065
66// ICH9R SPI commands
67#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
68#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
69#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
70#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
71
Stefan Reinauera9424d52008-06-27 16:28:34 +000072// ICH7 registers
73#define ICH7_REG_SPIS 0x00 /* 16 Bits */
74#define SPIS_SCIP 0x00000001
75#define SPIS_CDS 0x00000004
76#define SPIS_FCERR 0x00000008
77
Rudolf Marek3fdbccf2008-06-30 21:38:30 +000078/* VIA SPI is compatible with ICH7, but maxdata
79 to transfer is 16 bytes.
80
81 DATA byte count on ICH7 is 8:13, on VIA 8:11
82
83 bit 12 is port select CS0 CS1
84 bit 13 is FAST READ enable
85 bit 7 is used with fast read and one shot controls CS de-assert?
86*/
87
Stefan Reinauera9424d52008-06-27 16:28:34 +000088#define ICH7_REG_SPIC 0x02 /* 16 Bits */
89#define SPIC_SCGO 0x0002
90#define SPIC_ACS 0x0004
91#define SPIC_SPOP 0x0008
Rudolf Marek3fdbccf2008-06-30 21:38:30 +000092#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +000093
94#define ICH7_REG_SPIA 0x04 /* 32 Bits */
95#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
96#define ICH7_REG_PREOP 0x54 /* 16 Bits */
97#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
98#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
99
FENG yu ningc05a2952008-12-08 18:16:58 +0000100/* ICH SPI configuration lock-down. May be set during chipset enabling. */
101int ichspi_lock = 0;
102
Dominik Geyerb46acba2008-05-16 12:55:55 +0000103typedef struct _OPCODE {
104 uint8_t opcode; //This commands spi opcode
105 uint8_t spi_type; //This commands spi type
106 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
107} OPCODE;
108
109/* Opcode definition:
110 * Preop 1: Write Enable
111 * Preop 2: Write Status register enable
112 *
113 * OP 0: Write address
114 * OP 1: Read Address
115 * OP 2: ERASE block
116 * OP 3: Read Status register
117 * OP 4: Read ID
118 * OP 5: Write Status register
119 * OP 6: chip private (read JDEC id)
120 * OP 7: Chip erase
121 */
122typedef struct _OPCODES {
123 uint8_t preop[2];
124 OPCODE opcode[8];
125} OPCODES;
126
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000127static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000128
129/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000130static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000131{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000132 return mmio_readl(spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000133}
134
Uwe Hermann09e04f72009-05-16 22:36:00 +0000135static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000136{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000137 return mmio_readw(spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000138}
139
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000140#define REGWRITE32(X,Y) mmio_writel(Y, spibar+X)
141#define REGWRITE16(X,Y) mmio_writew(Y, spibar+X)
142#define REGWRITE8(X,Y) mmio_writeb(Y, spibar+X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000143
Dominik Geyerb46acba2008-05-16 12:55:55 +0000144/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000145static int find_opcode(OPCODES *op, uint8_t opcode);
146static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000147static int generate_opcodes(OPCODES * op);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000148static int program_opcodes(OPCODES * op);
Stefan Reinauer43119562008-11-02 19:51:50 +0000149static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000150 uint8_t datalength, uint8_t * data);
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000151static int ich_spi_write_page(struct flashchip *flash, uint8_t * bytes,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000152 int offset, int maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000153
FENG yu ningf041e9b2008-12-15 02:32:11 +0000154/* for pairing opcodes with their required preop */
155struct preop_opcode_pair {
156 uint8_t preop;
157 uint8_t opcode;
158};
159
160struct 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 */
167 {JEDEC_EWSR, JEDEC_WRSR},
168 {0,}
169};
170
Dominik Geyerb46acba2008-05-16 12:55:55 +0000171OPCODES O_ST_M25P = {
172 {
173 JEDEC_WREN,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000174 0},
Dominik Geyerb46acba2008-05-16 12:55:55 +0000175 {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000176 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 1}, // Write Byte
177 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
178 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 1}, // Erase Sector
179 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000180 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000181 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 1}, // Write Status Register
182 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
183 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 1}, // Bulk erase
184 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000185};
186
FENG yu ningc05a2952008-12-08 18:16:58 +0000187OPCODES O_EXISTING = {};
188
Uwe Hermann09e04f72009-05-16 22:36:00 +0000189static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000190{
191 int a;
192
193 for (a = 0; a < 8; a++) {
194 if (op->opcode[a].opcode == opcode)
195 return a;
196 }
197
198 return -1;
199}
200
Uwe Hermann09e04f72009-05-16 22:36:00 +0000201static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000202{
203 int a;
204
205 for (a = 0; a < 2; a++) {
206 if (op->preop[a] == preop)
207 return a;
208 }
209
210 return -1;
211}
212
FENG yu ningf041e9b2008-12-15 02:32:11 +0000213static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000214{
FENG yu ningf041e9b2008-12-15 02:32:11 +0000215 int a, b, i;
FENG yu ningc05a2952008-12-08 18:16:58 +0000216 uint16_t preop, optype;
217 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000218
219 if (op == NULL) {
220 printf_debug("\n%s: null OPCODES pointer!\n", __FUNCTION__);
221 return -1;
222 }
223
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000224 switch (spi_controller) {
225 case SPI_CONTROLLER_ICH7:
226 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000227 preop = REGREAD16(ICH7_REG_PREOP);
228 optype = REGREAD16(ICH7_REG_OPTYPE);
229 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
230 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
231 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000232 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000233 preop = REGREAD16(ICH9_REG_PREOP);
234 optype = REGREAD16(ICH9_REG_OPTYPE);
235 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
236 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
237 break;
238 default:
239 printf_debug("%s: unsupported chipset\n", __FUNCTION__);
240 return -1;
241 }
242
243 op->preop[0] = (uint8_t) preop;
244 op->preop[1] = (uint8_t) (preop >> 8);
245
246 for (a = 0; a < 8; a++) {
247 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
248 optype >>= 2;
249 }
250
251 for (a = 0; a < 4; a++) {
252 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
253 opmenu[0] >>= 8;
254 }
255
256 for (a = 4; a < 8; a++) {
257 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
258 opmenu[1] >>= 8;
259 }
260
261 /* atomic (link opcode with required pre-op) */
262 for (a = 4; a < 8; a++)
263 op->opcode[a].atomic = 0;
264
FENG yu ningf041e9b2008-12-15 02:32:11 +0000265 for (i = 0; pops[i].opcode; i++) {
266 a = find_opcode(op, pops[i].opcode);
267 b = find_preop(op, pops[i].preop);
268 if ((a != -1) && (b != -1))
269 op->opcode[a].atomic = (uint8_t) ++b;
FENG yu ningc05a2952008-12-08 18:16:58 +0000270 }
271
272 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
Peter Stuge016d4e12009-01-15 02:13:18 +0000305 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 +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:
321 printf_debug("%s: unsupported chipset\n", __FUNCTION__);
322 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) {
342 printf_debug("Generating OPCODES... ");
343 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 {
346 printf_debug("Programming OPCODES... ");
347 curopcodes_done = &O_ST_M25P;
348 rc = program_opcodes(curopcodes_done);
349 }
350
351 if (rc) {
352 curopcodes = NULL;
353 printf_debug("failed\n");
354 return 1;
355 } else {
356 curopcodes = curopcodes_done;
357 printf_debug("done\n");
358 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) {
429 printf_debug("Opcode %x not found.\n", op.opcode);
430 return 1;
431 }
432 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000433
434 /* Handle Atomic */
435 if (op.atomic != 0) {
436 /* Select atomic command */
437 temp16 |= SPIC_ACS;
Carl-Daniel Hailfinger738fdff2008-11-18 00:43:14 +0000438 /* Select prefix opcode */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000439 if ((op.atomic - 1) == 1) {
440 /*Select prefix opcode 2 */
441 temp16 |= SPIC_SPOP;
442 }
443 }
444
445 /* Start */
446 temp16 |= SPIC_SCGO;
447
448 /* write it */
449 REGWRITE16(ICH7_REG_SPIC, temp16);
450
451 /* wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000452 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauera9424d52008-06-27 16:28:34 +0000453 while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000454 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000455 }
456 if (!timeout) {
457 printf_debug("timeout\n");
458 }
459
460 if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) {
461 printf_debug("Transaction error!\n");
462 return 1;
463 }
464
465 if ((!write_cmd) && (datalength != 0)) {
466 for (a = 0; a < datalength; a++) {
467 if ((a % 4) == 0) {
468 temp32 = REGREAD32(ICH7_REG_SPID0 + (a));
469 }
470
471 data[a] =
472 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
473 >> ((a % 4) * 8);
474 }
475 }
476
477 return 0;
478}
479
Stefan Reinauer43119562008-11-02 19:51:50 +0000480static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000481 uint8_t datalength, uint8_t * data)
482{
483 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000484 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000485 uint32_t temp32;
486 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000487 uint64_t opmenu;
488 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000489
490 /* Is it a write command? */
491 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
492 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
493 write_cmd = 1;
494 }
495
496 /* Programm Offset in Flash into FADDR */
497 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
498
499 /* Program data into FDATA0 to N */
500 if (write_cmd && (datalength != 0)) {
501 temp32 = 0;
502 for (a = 0; a < datalength; a++) {
503 if ((a % 4) == 0) {
504 temp32 = 0;
505 }
506
507 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
508
509 if ((a % 4) == 3) {
510 REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
511 temp32);
512 }
513 }
514 if (((a - 1) % 4) != 3) {
515 REGWRITE32(ICH9_REG_FDATA0 +
516 ((a - 1) - ((a - 1) % 4)), temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000517 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000518 }
519
520 /* Assemble SSFS + SSFC */
521 temp32 = 0;
522
523 /* clear error status registers */
524 temp32 |= (SSFS_CDS + SSFS_FCERR);
525 /* USE 20 MhZ */
526 temp32 |= SSFC_SCF_20MHZ;
527
528 if (datalength != 0) {
529 uint32_t datatemp;
530 temp32 |= SSFC_DS;
531 datatemp = ((uint32_t) ((datalength - 1) & 0x3f)) << (8 + 8);
532 temp32 |= datatemp;
533 }
534
535 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000536 opmenu = REGREAD32(ICH9_REG_OPMENU);
537 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
538
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000539 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
540 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000541 break;
542 }
543 opmenu >>= 8;
544 }
545 if (opcode_index == 8) {
546 printf_debug("Opcode %x not found.\n", op.opcode);
547 return 1;
548 }
549 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000550
551 /* Handle Atomic */
552 if (op.atomic != 0) {
553 /* Select atomic command */
554 temp32 |= SSFC_ACS;
555 /* Selct prefix opcode */
556 if ((op.atomic - 1) == 1) {
557 /*Select prefix opcode 2 */
558 temp32 |= SSFC_SPOP;
559 }
560 }
561
562 /* Start */
563 temp32 |= SSFC_SCGO;
564
565 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000566 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000567
568 /*wait for cycle complete */
Carl-Daniel Hailfinger4c24ad42009-05-09 07:24:23 +0000569 timeout = 100 * 1000 * 60; // 60s is a looong timeout.
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000570 while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000571 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000572 }
573 if (!timeout) {
574 printf_debug("timeout\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000575 }
576
Stefan Reinauera9424d52008-06-27 16:28:34 +0000577 if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000578 printf_debug("Transaction error!\n");
579 return 1;
580 }
581
582 if ((!write_cmd) && (datalength != 0)) {
583 for (a = 0; a < datalength; a++) {
584 if ((a % 4) == 0) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000585 temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
Dominik Geyerb46acba2008-05-16 12:55:55 +0000586 }
587
588 data[a] =
Stefan Reinauera9424d52008-06-27 16:28:34 +0000589 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
590 >> ((a % 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000591 }
592 }
593
594 return 0;
595}
596
Stefan Reinauer43119562008-11-02 19:51:50 +0000597static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000598 uint8_t datalength, uint8_t * data)
599{
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000600 switch (spi_controller) {
601 case SPI_CONTROLLER_VIA:
Stefan Reinauer43119562008-11-02 19:51:50 +0000602 return ich7_run_opcode(op, offset, datalength, data, 16);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000603 case SPI_CONTROLLER_ICH7:
Stefan Reinauer43119562008-11-02 19:51:50 +0000604 return ich7_run_opcode(op, offset, datalength, data, 64);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000605 case SPI_CONTROLLER_ICH9:
Stefan Reinauer43119562008-11-02 19:51:50 +0000606 return ich9_run_opcode(op, offset, datalength, data);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000607 default:
608 printf_debug("%s: unsupported chipset\n", __FUNCTION__);
609 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000610
611 /* If we ever get here, something really weird happened */
612 return -1;
613}
614
Dominik Geyerb46acba2008-05-16 12:55:55 +0000615static int ich_spi_write_page(struct flashchip *flash, uint8_t * bytes,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000616 int offset, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000617{
618 int page_size = flash->page_size;
619 uint32_t remaining = page_size;
620 int a;
621
Stefan Reinauera9424d52008-06-27 16:28:34 +0000622 printf_debug("ich_spi_write_page: offset=%d, number=%d, buf=%p\n",
623 offset, page_size, bytes);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000624
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000625 for (a = 0; a < page_size; a += maxdata) {
626 if (remaining < maxdata) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000627 if (run_opcode
Stefan Reinauer43119562008-11-02 19:51:50 +0000628 (curopcodes->opcode[0],
Stefan Reinauera9424d52008-06-27 16:28:34 +0000629 offset + (page_size - remaining), remaining,
Dominik Geyerb46acba2008-05-16 12:55:55 +0000630 &bytes[page_size - remaining]) != 0) {
631 printf_debug("Error writing");
632 return 1;
633 }
634 remaining = 0;
635 } else {
636 if (run_opcode
Stefan Reinauer43119562008-11-02 19:51:50 +0000637 (curopcodes->opcode[0],
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000638 offset + (page_size - remaining), maxdata,
Dominik Geyerb46acba2008-05-16 12:55:55 +0000639 &bytes[page_size - remaining]) != 0) {
640 printf_debug("Error writing");
641 return 1;
642 }
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000643 remaining -= maxdata;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000644 }
645 }
646
647 return 0;
648}
649
Dominik Geyerb46acba2008-05-16 12:55:55 +0000650int ich_spi_read(struct flashchip *flash, uint8_t * buf)
651{
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000652 int maxdata = 64;
653
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000654 if (spi_controller == SPI_CONTROLLER_VIA)
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000655 maxdata = 16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000656
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000657 return spi_read_chunked(flash, buf, maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000658}
659
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000660int ich_spi_write_256(struct flashchip *flash, uint8_t * buf)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000661{
662 int i, j, rc = 0;
663 int total_size = flash->total_size * 1024;
664 int page_size = flash->page_size;
665 int erase_size = 64 * 1024;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000666 int maxdata = 64;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000667
668 spi_disable_blockprotect();
669
670 printf("Programming page: \n");
671
672 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000673 /* FIMXE: call the chip-specific spi_block_erase_XX instead.
674 * For this, we need to add a block erase function to
675 * struct flashchip.
676 */
677 rc = spi_block_erase_d8(flash, i * erase_size);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000678 if (rc) {
679 printf("Error erasing block at 0x%x\n", i);
680 break;
681 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000682
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000683 if (spi_controller == SPI_CONTROLLER_VIA)
Peter Stuge6a214162008-07-07 05:14:06 +0000684 maxdata = 16;
685
Dominik Geyerb46acba2008-05-16 12:55:55 +0000686 for (j = 0; j < erase_size / page_size; j++) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000687 ich_spi_write_page(flash,
688 (void *)(buf + (i * erase_size) + (j * page_size)),
689 (i * erase_size) + (j * page_size), maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000690 }
691 }
692
693 printf("\n");
694
695 return rc;
696}
697
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000698int ich_spi_command(unsigned int writecnt, unsigned int readcnt,
699 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000700{
701 int a;
702 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
Stefan Reinauer43119562008-11-02 19:51:50 +0000744 if (run_opcode(*opcode, addr, count, data) != 0) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000745 printf_debug("run OPCODE 0x%02x failed\n", opcode->opcode);
746 return 1;
747 }
748
749 return 0;
750}