blob: bad2e2781f0a5989130b9b319a8a57b23629f7ff [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
22 *
23 */
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
36#include <stdio.h>
37#include <string.h>
38#include <stdint.h>
39#include <sys/mman.h>
40#include <pci/pci.h>
41#include "flash.h"
42#include "spi.h"
43
Stefan Reinauera9424d52008-06-27 16:28:34 +000044/* ICH9 controller register definition */
45#define ICH9_REG_FADDR 0x08 /* 32 Bits */
46#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
47
48#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000049#define SSFS_SCIP 0x00000001
50#define SSFS_CDS 0x00000004
51#define SSFS_FCERR 0x00000008
52#define SSFS_AEL 0x00000010
Stefan Reinauera9424d52008-06-27 16:28:34 +000053
54#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000055#define SSFC_SCGO 0x00000200
56#define SSFC_ACS 0x00000400
57#define SSFC_SPOP 0x00000800
58#define SSFC_COP 0x00001000
59#define SSFC_DBC 0x00010000
60#define SSFC_DS 0x00400000
61#define SSFC_SME 0x00800000
62#define SSFC_SCF 0x01000000
63#define SSFC_SCF_20MHZ 0x00000000
64#define SSFC_SCF_33MHZ 0x01000000
Stefan Reinauera9424d52008-06-27 16:28:34 +000065
66#define ICH9_REG_PREOP 0x94 /* 16 Bits */
67#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
68#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000069
70// ICH9R SPI commands
71#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
72#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
73#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
74#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
75
Stefan Reinauera9424d52008-06-27 16:28:34 +000076// ICH7 registers
77#define ICH7_REG_SPIS 0x00 /* 16 Bits */
78#define SPIS_SCIP 0x00000001
79#define SPIS_CDS 0x00000004
80#define SPIS_FCERR 0x00000008
81
Rudolf Marek3fdbccf2008-06-30 21:38:30 +000082/* VIA SPI is compatible with ICH7, but maxdata
83 to transfer is 16 bytes.
84
85 DATA byte count on ICH7 is 8:13, on VIA 8:11
86
87 bit 12 is port select CS0 CS1
88 bit 13 is FAST READ enable
89 bit 7 is used with fast read and one shot controls CS de-assert?
90*/
91
Stefan Reinauera9424d52008-06-27 16:28:34 +000092#define ICH7_REG_SPIC 0x02 /* 16 Bits */
93#define SPIC_SCGO 0x0002
94#define SPIC_ACS 0x0004
95#define SPIC_SPOP 0x0008
Rudolf Marek3fdbccf2008-06-30 21:38:30 +000096#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +000097
98#define ICH7_REG_SPIA 0x04 /* 32 Bits */
99#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
100#define ICH7_REG_PREOP 0x54 /* 16 Bits */
101#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
102#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
103
FENG yu ningc05a2952008-12-08 18:16:58 +0000104/* ICH SPI configuration lock-down. May be set during chipset enabling. */
105int ichspi_lock = 0;
106
Dominik Geyerb46acba2008-05-16 12:55:55 +0000107typedef struct _OPCODE {
108 uint8_t opcode; //This commands spi opcode
109 uint8_t spi_type; //This commands spi type
110 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
111} OPCODE;
112
113/* Opcode definition:
114 * Preop 1: Write Enable
115 * Preop 2: Write Status register enable
116 *
117 * OP 0: Write address
118 * OP 1: Read Address
119 * OP 2: ERASE block
120 * OP 3: Read Status register
121 * OP 4: Read ID
122 * OP 5: Write Status register
123 * OP 6: chip private (read JDEC id)
124 * OP 7: Chip erase
125 */
126typedef struct _OPCODES {
127 uint8_t preop[2];
128 OPCODE opcode[8];
129} OPCODES;
130
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000131static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000132
133/* HW access functions */
134static inline uint32_t REGREAD32(int X)
135{
136 volatile uint32_t regval;
Uwe Hermann394131e2008-10-18 21:14:13 +0000137 regval = *(volatile uint32_t *)((uint8_t *) spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000138 return regval;
139}
140
141static inline uint16_t REGREAD16(int X)
142{
143 volatile uint16_t regval;
Uwe Hermann394131e2008-10-18 21:14:13 +0000144 regval = *(volatile uint16_t *)((uint8_t *) spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000145 return regval;
146}
147
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000148#define REGWRITE32(X,Y) (*(uint32_t *)((uint8_t *)spibar+X)=Y)
149#define REGWRITE16(X,Y) (*(uint16_t *)((uint8_t *)spibar+X)=Y)
150#define REGWRITE8(X,Y) (*(uint8_t *)((uint8_t *)spibar+X)=Y)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000151
Dominik Geyerb46acba2008-05-16 12:55:55 +0000152/* Common SPI functions */
FENG yu ningc05a2952008-12-08 18:16:58 +0000153static inline int find_opcode(OPCODES *op, uint8_t opcode);
154static inline int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000155static int generate_opcodes(OPCODES * op);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000156static int program_opcodes(OPCODES * op);
Stefan Reinauer43119562008-11-02 19:51:50 +0000157static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000158 uint8_t datalength, uint8_t * data);
159static int ich_spi_read_page(struct flashchip *flash, uint8_t * buf,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000160 int offset, int maxdata);
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
190 {JEDEC_RES, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Resume Deep Power-Down
191 {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
199static inline int find_opcode(OPCODES *op, uint8_t opcode)
200{
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
211static inline int find_preop(OPCODES *op, uint8_t preop)
212{
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) {
230 printf_debug("\n%s: null OPCODES pointer!\n", __FUNCTION__);
231 return -1;
232 }
233
234 switch (flashbus) {
235 case BUS_TYPE_ICH7_SPI:
236 case BUS_TYPE_VIA_SPI:
237 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;
242 case BUS_TYPE_ICH9_SPI:
243 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:
249 printf_debug("%s: unsupported chipset\n", __FUNCTION__);
250 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 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000292 preop = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000293 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000294 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000295 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000296 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000297
Stefan Reinauera9424d52008-06-27 16:28:34 +0000298 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000299 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000300 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000301 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000302 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000303
Stefan Reinauera9424d52008-06-27 16:28:34 +0000304 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000305 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000306 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000307 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000308 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000309
Dominik Geyerb46acba2008-05-16 12:55:55 +0000310 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000311 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000312 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000313 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000314 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000315
Peter Stuge016d4e12009-01-15 02:13:18 +0000316 printf_debug("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000317 switch (flashbus) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000318 case BUS_TYPE_ICH7_SPI:
319 case BUS_TYPE_VIA_SPI:
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000320 REGWRITE16(ICH7_REG_PREOP, preop);
321 REGWRITE16(ICH7_REG_OPTYPE, optype);
322 REGWRITE32(ICH7_REG_OPMENU, opmenu[0]);
323 REGWRITE32(ICH7_REG_OPMENU + 4, opmenu[1]);
324 break;
325 case BUS_TYPE_ICH9_SPI:
326 REGWRITE16(ICH9_REG_PREOP, preop);
327 REGWRITE16(ICH9_REG_OPTYPE, optype);
328 REGWRITE32(ICH9_REG_OPMENU, opmenu[0]);
329 REGWRITE32(ICH9_REG_OPMENU + 4, opmenu[1]);
330 break;
331 default:
332 printf_debug("%s: unsupported chipset\n", __FUNCTION__);
333 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000334 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000335
336 return 0;
337}
338
FENG yu ningf041e9b2008-12-15 02:32:11 +0000339/* This function generates OPCODES from or programs OPCODES to ICH according to
340 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000341 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000342 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000343 */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000344int ich_init_opcodes()
FENG yu ningc05a2952008-12-08 18:16:58 +0000345{
346 int rc = 0;
347 OPCODES *curopcodes_done;
348
349 if (curopcodes)
350 return 0;
351
352 if (ichspi_lock) {
353 printf_debug("Generating OPCODES... ");
354 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000355 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000356 } else {
357 printf_debug("Programming OPCODES... ");
358 curopcodes_done = &O_ST_M25P;
359 rc = program_opcodes(curopcodes_done);
360 }
361
362 if (rc) {
363 curopcodes = NULL;
364 printf_debug("failed\n");
365 return 1;
366 } else {
367 curopcodes = curopcodes_done;
368 printf_debug("done\n");
369 return 0;
370 }
371}
372
Stefan Reinauer43119562008-11-02 19:51:50 +0000373static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000374 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000375{
376 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000377 int timeout;
Peter Stuge7e2c0792008-06-29 01:30:41 +0000378 uint32_t temp32 = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000379 uint16_t temp16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000380 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000381 uint64_t opmenu;
382 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000383
384 /* Is it a write command? */
385 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
386 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
387 write_cmd = 1;
388 }
389
390 /* Programm Offset in Flash into FADDR */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000391 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000392
393 /* Program data into FDATA0 to N */
394 if (write_cmd && (datalength != 0)) {
395 temp32 = 0;
396 for (a = 0; a < datalength; a++) {
397 if ((a % 4) == 0) {
398 temp32 = 0;
399 }
400
401 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
402
403 if ((a % 4) == 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000404 REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)),
405 temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000406 }
407 }
408 if (((a - 1) % 4) != 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000409 REGWRITE32(ICH7_REG_SPID0 +
410 ((a - 1) - ((a - 1) % 4)), temp32);
411 }
412
413 }
414
415 /* Assemble SPIS */
416 temp16 = 0;
417 /* clear error status registers */
418 temp16 |= (SPIS_CDS + SPIS_FCERR);
419 REGWRITE16(ICH7_REG_SPIS, temp16);
420
421 /* Assemble SPIC */
422 temp16 = 0;
423
424 if (datalength != 0) {
425 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000426 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000427 }
428
429 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000430 opmenu = REGREAD32(ICH7_REG_OPMENU);
431 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
432
433 for (opcode_index=0; opcode_index<8; opcode_index++) {
434 if((opmenu & 0xff) == op.opcode) {
435 break;
436 }
437 opmenu >>= 8;
438 }
439 if (opcode_index == 8) {
440 printf_debug("Opcode %x not found.\n", op.opcode);
441 return 1;
442 }
443 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000444
445 /* Handle Atomic */
446 if (op.atomic != 0) {
447 /* Select atomic command */
448 temp16 |= SPIC_ACS;
Carl-Daniel Hailfinger738fdff2008-11-18 00:43:14 +0000449 /* Select prefix opcode */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000450 if ((op.atomic - 1) == 1) {
451 /*Select prefix opcode 2 */
452 temp16 |= SPIC_SPOP;
453 }
454 }
455
456 /* Start */
457 temp16 |= SPIC_SCGO;
458
459 /* write it */
460 REGWRITE16(ICH7_REG_SPIC, temp16);
461
462 /* wait for cycle complete */
463 timeout = 1000 * 60; // 60s is a looong timeout.
464 while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) {
465 myusec_delay(1000);
466 }
467 if (!timeout) {
468 printf_debug("timeout\n");
469 }
470
471 if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) {
472 printf_debug("Transaction error!\n");
473 return 1;
474 }
475
476 if ((!write_cmd) && (datalength != 0)) {
477 for (a = 0; a < datalength; a++) {
478 if ((a % 4) == 0) {
479 temp32 = REGREAD32(ICH7_REG_SPID0 + (a));
480 }
481
482 data[a] =
483 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
484 >> ((a % 4) * 8);
485 }
486 }
487
488 return 0;
489}
490
Stefan Reinauer43119562008-11-02 19:51:50 +0000491static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000492 uint8_t datalength, uint8_t * data)
493{
494 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000495 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000496 uint32_t temp32;
497 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000498 uint64_t opmenu;
499 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000500
501 /* Is it a write command? */
502 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
503 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
504 write_cmd = 1;
505 }
506
507 /* Programm Offset in Flash into FADDR */
508 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
509
510 /* Program data into FDATA0 to N */
511 if (write_cmd && (datalength != 0)) {
512 temp32 = 0;
513 for (a = 0; a < datalength; a++) {
514 if ((a % 4) == 0) {
515 temp32 = 0;
516 }
517
518 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
519
520 if ((a % 4) == 3) {
521 REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
522 temp32);
523 }
524 }
525 if (((a - 1) % 4) != 3) {
526 REGWRITE32(ICH9_REG_FDATA0 +
527 ((a - 1) - ((a - 1) % 4)), temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000528 }
529
530 }
531
532 /* Assemble SSFS + SSFC */
533 temp32 = 0;
534
535 /* clear error status registers */
536 temp32 |= (SSFS_CDS + SSFS_FCERR);
537 /* USE 20 MhZ */
538 temp32 |= SSFC_SCF_20MHZ;
539
540 if (datalength != 0) {
541 uint32_t datatemp;
542 temp32 |= SSFC_DS;
543 datatemp = ((uint32_t) ((datalength - 1) & 0x3f)) << (8 + 8);
544 temp32 |= datatemp;
545 }
546
547 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000548 opmenu = REGREAD32(ICH9_REG_OPMENU);
549 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
550
551 for (opcode_index=0; opcode_index<8; opcode_index++) {
552 if((opmenu & 0xff) == op.opcode) {
553 break;
554 }
555 opmenu >>= 8;
556 }
557 if (opcode_index == 8) {
558 printf_debug("Opcode %x not found.\n", op.opcode);
559 return 1;
560 }
561 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000562
563 /* Handle Atomic */
564 if (op.atomic != 0) {
565 /* Select atomic command */
566 temp32 |= SSFC_ACS;
567 /* Selct prefix opcode */
568 if ((op.atomic - 1) == 1) {
569 /*Select prefix opcode 2 */
570 temp32 |= SSFC_SPOP;
571 }
572 }
573
574 /* Start */
575 temp32 |= SSFC_SCGO;
576
577 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000578 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000579
580 /*wait for cycle complete */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000581 timeout = 1000 * 60; // 60s is a looong timeout.
582 while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) {
583 myusec_delay(1000);
584 }
585 if (!timeout) {
586 printf_debug("timeout\n");
Dominik Geyerb46acba2008-05-16 12:55:55 +0000587 }
588
Stefan Reinauera9424d52008-06-27 16:28:34 +0000589 if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000590 printf_debug("Transaction error!\n");
591 return 1;
592 }
593
594 if ((!write_cmd) && (datalength != 0)) {
595 for (a = 0; a < datalength; a++) {
596 if ((a % 4) == 0) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000597 temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
Dominik Geyerb46acba2008-05-16 12:55:55 +0000598 }
599
600 data[a] =
Stefan Reinauera9424d52008-06-27 16:28:34 +0000601 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
602 >> ((a % 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000603 }
604 }
605
606 return 0;
607}
608
Stefan Reinauer43119562008-11-02 19:51:50 +0000609static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000610 uint8_t datalength, uint8_t * data)
611{
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000612 switch (flashbus) {
613 case BUS_TYPE_VIA_SPI:
Stefan Reinauer43119562008-11-02 19:51:50 +0000614 return ich7_run_opcode(op, offset, datalength, data, 16);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000615 case BUS_TYPE_ICH7_SPI:
Stefan Reinauer43119562008-11-02 19:51:50 +0000616 return ich7_run_opcode(op, offset, datalength, data, 64);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000617 case BUS_TYPE_ICH9_SPI:
Stefan Reinauer43119562008-11-02 19:51:50 +0000618 return ich9_run_opcode(op, offset, datalength, data);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000619 default:
620 printf_debug("%s: unsupported chipset\n", __FUNCTION__);
621 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000622
623 /* If we ever get here, something really weird happened */
624 return -1;
625}
626
Uwe Hermann394131e2008-10-18 21:14:13 +0000627static int ich_spi_read_page(struct flashchip *flash, uint8_t * buf, int offset,
628 int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000629{
630 int page_size = flash->page_size;
631 uint32_t remaining = flash->page_size;
632 int a;
633
Stefan Reinauera9424d52008-06-27 16:28:34 +0000634 printf_debug("ich_spi_read_page: offset=%d, number=%d, buf=%p\n",
635 offset, page_size, buf);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000636
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000637 for (a = 0; a < page_size; a += maxdata) {
638 if (remaining < maxdata) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000639
Carl-Daniel Hailfinger738fdff2008-11-18 00:43:14 +0000640 if (spi_nbyte_read(offset + (page_size - remaining),
641 &buf[page_size - remaining], remaining)) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000642 printf_debug("Error reading");
643 return 1;
644 }
645 remaining = 0;
646 } else {
Carl-Daniel Hailfinger738fdff2008-11-18 00:43:14 +0000647 if (spi_nbyte_read(offset + (page_size - remaining),
648 &buf[page_size - remaining], maxdata)) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000649 printf_debug("Error reading");
650 return 1;
651 }
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000652 remaining -= maxdata;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000653 }
654 }
655
656 return 0;
657}
658
659static int ich_spi_write_page(struct flashchip *flash, uint8_t * bytes,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000660 int offset, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000661{
662 int page_size = flash->page_size;
663 uint32_t remaining = page_size;
664 int a;
665
Stefan Reinauera9424d52008-06-27 16:28:34 +0000666 printf_debug("ich_spi_write_page: offset=%d, number=%d, buf=%p\n",
667 offset, page_size, bytes);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000668
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000669 for (a = 0; a < page_size; a += maxdata) {
670 if (remaining < maxdata) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000671 if (run_opcode
Stefan Reinauer43119562008-11-02 19:51:50 +0000672 (curopcodes->opcode[0],
Stefan Reinauera9424d52008-06-27 16:28:34 +0000673 offset + (page_size - remaining), remaining,
Dominik Geyerb46acba2008-05-16 12:55:55 +0000674 &bytes[page_size - remaining]) != 0) {
675 printf_debug("Error writing");
676 return 1;
677 }
678 remaining = 0;
679 } else {
680 if (run_opcode
Stefan Reinauer43119562008-11-02 19:51:50 +0000681 (curopcodes->opcode[0],
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000682 offset + (page_size - remaining), maxdata,
Dominik Geyerb46acba2008-05-16 12:55:55 +0000683 &bytes[page_size - remaining]) != 0) {
684 printf_debug("Error writing");
685 return 1;
686 }
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000687 remaining -= maxdata;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000688 }
689 }
690
691 return 0;
692}
693
Dominik Geyerb46acba2008-05-16 12:55:55 +0000694int ich_spi_read(struct flashchip *flash, uint8_t * buf)
695{
696 int i, rc = 0;
697 int total_size = flash->total_size * 1024;
698 int page_size = flash->page_size;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000699 int maxdata = 64;
700
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000701 if (flashbus == BUS_TYPE_VIA_SPI) {
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000702 maxdata = 16;
703 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000704
705 for (i = 0; (i < total_size / page_size) && (rc == 0); i++) {
706 rc = ich_spi_read_page(flash, (void *)(buf + i * page_size),
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000707 i * page_size, maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000708 }
709
710 return rc;
711}
712
Dominik Geyerb46acba2008-05-16 12:55:55 +0000713int ich_spi_write(struct flashchip *flash, uint8_t * buf)
714{
715 int i, j, rc = 0;
716 int total_size = flash->total_size * 1024;
717 int page_size = flash->page_size;
718 int erase_size = 64 * 1024;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000719 int maxdata = 64;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000720
721 spi_disable_blockprotect();
722
723 printf("Programming page: \n");
724
725 for (i = 0; i < total_size / erase_size; i++) {
Carl-Daniel Hailfinger6afb6132008-11-03 00:02:11 +0000726 /* FIMXE: call the chip-specific spi_block_erase_XX instead.
727 * For this, we need to add a block erase function to
728 * struct flashchip.
729 */
730 rc = spi_block_erase_d8(flash, i * erase_size);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000731 if (rc) {
732 printf("Error erasing block at 0x%x\n", i);
733 break;
734 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000735
Peter Stuge6a214162008-07-07 05:14:06 +0000736 if (flashbus == BUS_TYPE_VIA_SPI)
737 maxdata = 16;
738
Dominik Geyerb46acba2008-05-16 12:55:55 +0000739 for (j = 0; j < erase_size / page_size; j++) {
Uwe Hermann394131e2008-10-18 21:14:13 +0000740 ich_spi_write_page(flash,
741 (void *)(buf + (i * erase_size) + (j * page_size)),
742 (i * erase_size) + (j * page_size), maxdata);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000743 }
744 }
745
746 printf("\n");
747
748 return rc;
749}
750
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000751int ich_spi_command(unsigned int writecnt, unsigned int readcnt,
752 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000753{
754 int a;
755 int opcode_index = -1;
756 const unsigned char cmd = *writearr;
757 OPCODE *opcode;
758 uint32_t addr = 0;
759 uint8_t *data;
760 int count;
761
Dominik Geyerb46acba2008-05-16 12:55:55 +0000762 /* find cmd in opcodes-table */
763 for (a = 0; a < 8; a++) {
764 if ((curopcodes->opcode[a]).opcode == cmd) {
765 opcode_index = a;
766 break;
767 }
768 }
769
770 /* unknown / not programmed command */
771 if (opcode_index == -1) {
772 printf_debug("Invalid OPCODE 0x%02x\n", cmd);
773 return 1;
774 }
775
776 opcode = &(curopcodes->opcode[opcode_index]);
777
778 /* if opcode-type requires an address */
779 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
780 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000781 addr = (writearr[1] << 16) |
782 (writearr[2] << 8) | (writearr[3] << 0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000783 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000784
Dominik Geyerb46acba2008-05-16 12:55:55 +0000785 /* translate read/write array/count */
786 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000787 data = (uint8_t *) (writearr + 1);
788 count = writecnt - 1;
789 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
790 data = (uint8_t *) (writearr + 4);
791 count = writecnt - 4;
792 } else {
793 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000794 count = readcnt;
795 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000796
Stefan Reinauer43119562008-11-02 19:51:50 +0000797 if (run_opcode(*opcode, addr, count, data) != 0) {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000798 printf_debug("run OPCODE 0x%02x failed\n", opcode->opcode);
799 return 1;
800 }
801
802 return 0;
803}