ichspi.c 19.4 KB
Newer Older
1 2 3 4 5 6
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2008 Stefan Wildemann <stefan.wildemann@kontron.com>
 * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.com>
 * Copyright (C) 2008 Dominik Geyer <dominik.geyer@kontron.com>
Stefan Reinauer's avatar
Stefan Reinauer committed
7
 * Copyright (C) 2008 coresystems GmbH <info@coresystems.de>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

/*
 * This module is designed for supporting the devices
 * ST M25P40
 * ST M25P80
 * ST M25P16
 * ST M25P32 already tested
 * ST M25P64
 * AT 25DF321 already tested
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <sys/mman.h>
#include <pci/pci.h>
#include "flash.h"
#include "spi.h"

Stefan Reinauer's avatar
Stefan Reinauer committed
43 44 45 46 47
/* ICH9 controller register definition */
#define ICH9_REG_FADDR         0x08	/* 32 Bits */
#define ICH9_REG_FDATA0                0x10	/* 64 Bytes */

#define ICH9_REG_SSFS          0x90	/* 08 Bits */
48 49 50 51
#define SSFS_SCIP		0x00000001
#define SSFS_CDS		0x00000004
#define SSFS_FCERR		0x00000008
#define SSFS_AEL		0x00000010
Stefan Reinauer's avatar
Stefan Reinauer committed
52 53

#define ICH9_REG_SSFC          0x91	/* 24 Bits */
54 55 56 57 58 59 60 61 62 63
#define SSFC_SCGO		0x00000200
#define SSFC_ACS		0x00000400
#define SSFC_SPOP		0x00000800
#define SSFC_COP		0x00001000
#define SSFC_DBC		0x00010000
#define SSFC_DS			0x00400000
#define SSFC_SME		0x00800000
#define SSFC_SCF		0x01000000
#define SSFC_SCF_20MHZ 0x00000000
#define SSFC_SCF_33MHZ 0x01000000
Stefan Reinauer's avatar
Stefan Reinauer committed
64 65 66 67

#define ICH9_REG_PREOP         0x94	/* 16 Bits */
#define ICH9_REG_OPTYPE                0x96	/* 16 Bits */
#define ICH9_REG_OPMENU                0x98	/* 64 Bits */
68 69 70 71 72 73 74

// ICH9R SPI commands
#define SPI_OPCODE_TYPE_READ_NO_ADDRESS     0
#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS    1
#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS   2
#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS  3

Stefan Reinauer's avatar
Stefan Reinauer committed
75 76 77 78 79 80
// ICH7 registers
#define ICH7_REG_SPIS          0x00	/* 16 Bits */
#define SPIS_SCIP              0x00000001
#define SPIS_CDS               0x00000004
#define SPIS_FCERR             0x00000008

81 82 83 84 85 86 87 88 89 90
/* VIA SPI is compatible with ICH7, but maxdata
   to transfer is 16 bytes.

   DATA byte count on ICH7 is 8:13, on VIA 8:11

   bit 12 is port select CS0 CS1
   bit 13 is FAST READ enable
   bit 7  is used with fast read and one shot controls CS de-assert?
*/

Stefan Reinauer's avatar
Stefan Reinauer committed
91 92 93 94
#define ICH7_REG_SPIC          0x02	/* 16 Bits */
#define SPIC_SCGO              0x0002
#define SPIC_ACS               0x0004
#define SPIC_SPOP              0x0008
95
#define SPIC_DS                0x4000
Stefan Reinauer's avatar
Stefan Reinauer committed
96 97 98 99 100 101 102

#define ICH7_REG_SPIA          0x04	/* 32 Bits */
#define ICH7_REG_SPID0         0x08	/* 64 Bytes */
#define ICH7_REG_PREOP         0x54	/* 16 Bits */
#define ICH7_REG_OPTYPE                0x56	/* 16 Bits */
#define ICH7_REG_OPMENU                0x58	/* 64 Bits */

103 104 105
/* ICH SPI configuration lock-down. May be set during chipset enabling. */
int ichspi_lock = 0;

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
typedef struct _OPCODE {
	uint8_t opcode;		//This commands spi opcode
	uint8_t spi_type;	//This commands spi type
	uint8_t atomic;		//Use preop: (0: none, 1: preop0, 2: preop1
} OPCODE;

/* Opcode definition:
 * Preop 1: Write Enable
 * Preop 2: Write Status register enable
 *
 * OP 0: Write address
 * OP 1: Read Address
 * OP 2: ERASE block
 * OP 3: Read Status register
 * OP 4: Read ID
 * OP 5: Write Status register
 * OP 6: chip private (read JDEC id)
 * OP 7: Chip erase
 */
typedef struct _OPCODES {
	uint8_t preop[2];
	OPCODE opcode[8];
} OPCODES;

130
static OPCODES *curopcodes = NULL;
131 132 133 134 135

/* HW access functions */
static inline uint32_t REGREAD32(int X)
{
	volatile uint32_t regval;
136
	regval = *(volatile uint32_t *)((uint8_t *) spibar + X);
Stefan Reinauer's avatar
Stefan Reinauer committed
137 138 139 140 141 142
	return regval;
}

static inline uint16_t REGREAD16(int X)
{
	volatile uint16_t regval;
143
	regval = *(volatile uint16_t *)((uint8_t *) spibar + X);
144 145 146
	return regval;
}

147 148 149
#define REGWRITE32(X,Y) (*(uint32_t *)((uint8_t *)spibar+X)=Y)
#define REGWRITE16(X,Y) (*(uint16_t *)((uint8_t *)spibar+X)=Y)
#define REGWRITE8(X,Y)  (*(uint8_t *)((uint8_t *)spibar+X)=Y)
150 151

/* Common SPI functions */
152 153
static inline int find_opcode(OPCODES *op, uint8_t opcode);
static inline int find_preop(OPCODES *op, uint8_t preop);
FENG yu ning's avatar
FENG yu ning committed
154
static int generate_opcodes(OPCODES * op);
155
static int program_opcodes(OPCODES * op);
156
static int run_opcode(OPCODE op, uint32_t offset,
157 158
		      uint8_t datalength, uint8_t * data);
static int ich_spi_read_page(struct flashchip *flash, uint8_t * buf,
159
			     int offset, int maxdata);
160
static int ich_spi_write_page(struct flashchip *flash, uint8_t * bytes,
161
			      int offset, int maxdata);
162

FENG yu ning's avatar
FENG yu ning committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/* for pairing opcodes with their required preop */
struct preop_opcode_pair {
	uint8_t preop;
	uint8_t opcode;
};

struct preop_opcode_pair pops[] = {
	{JEDEC_WREN, JEDEC_BYTE_PROGRAM},
	{JEDEC_WREN, JEDEC_SE}, /* sector erase */
	{JEDEC_WREN, JEDEC_BE_52}, /* block erase */
	{JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
	{JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
	{JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
	{JEDEC_EWSR, JEDEC_WRSR},
	{0,}
};

180 181 182
OPCODES O_ST_M25P = {
	{
	 JEDEC_WREN,
183
	 0},
184
	{
185 186 187 188 189 190 191 192 193
	 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 1},	// Write Byte
	 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0},	// Read Data
	 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 1},	// Erase Sector
	 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0},	// Read Device Status Reg
	 {JEDEC_RES, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0},	// Resume Deep Power-Down
	 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 1},	// Write Status Register
	 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0},	// Read JDEC ID
	 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 1},	// Bulk erase
	 }
194 195
};

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
OPCODES O_EXISTING = {};

static inline int find_opcode(OPCODES *op, uint8_t opcode)
{
	int a;

	for (a = 0; a < 8; a++) {
		if (op->opcode[a].opcode == opcode)
			return a;
	}

	return -1;
}

static inline int find_preop(OPCODES *op, uint8_t preop)
{
	int a;

	for (a = 0; a < 2; a++) {
		if (op->preop[a] == preop)
			return a;
	}

	return -1;
}

FENG yu ning's avatar
FENG yu ning committed
222
static int generate_opcodes(OPCODES * op)
223
{
FENG yu ning's avatar
FENG yu ning committed
224
	int a, b, i;
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	uint16_t preop, optype;
	uint32_t opmenu[2];

	if (op == NULL) {
		printf_debug("\n%s: null OPCODES pointer!\n", __FUNCTION__);
		return -1;
	}

	switch (flashbus) {
	case BUS_TYPE_ICH7_SPI:
	case BUS_TYPE_VIA_SPI:
		preop = REGREAD16(ICH7_REG_PREOP);
		optype = REGREAD16(ICH7_REG_OPTYPE);
		opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
		opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
		break;
	case BUS_TYPE_ICH9_SPI:
		preop = REGREAD16(ICH9_REG_PREOP);
		optype = REGREAD16(ICH9_REG_OPTYPE);
		opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
		opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
		break;
	default:
		printf_debug("%s: unsupported chipset\n", __FUNCTION__);
		return -1;
	}

	op->preop[0] = (uint8_t) preop;
	op->preop[1] = (uint8_t) (preop >> 8);

	for (a = 0; a < 8; a++) {
		op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
		optype >>= 2;
	}

	for (a = 0; a < 4; a++) {
		op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
		opmenu[0] >>= 8;
	}

	for (a = 4; a < 8; a++) {
		op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
		opmenu[1] >>= 8;
	}

	/* atomic (link opcode with required pre-op) */
	for (a = 4; a < 8; a++)
		op->opcode[a].atomic = 0;

FENG yu ning's avatar
FENG yu ning committed
274 275 276 277 278
	for (i = 0; pops[i].opcode; i++) {
		a = find_opcode(op, pops[i].opcode);
		b = find_preop(op, pops[i].preop);
		if ((a != -1) && (b != -1))
			op->opcode[a].atomic = (uint8_t) ++b;
279 280 281 282 283
	}

	return 0;
}

284 285 286
int program_opcodes(OPCODES * op)
{
	uint8_t a;
287 288
	uint16_t preop, optype;
	uint32_t opmenu[2];
289 290 291

	/* Program Prefix Opcodes */
	/* 0:7 Prefix Opcode 1 */
292
	preop = (op->preop[0]);
293
	/* 8:16 Prefix Opcode 2 */
294
	preop |= ((uint16_t) op->preop[1]) << 8;
295

Stefan Reinauer's avatar
Stefan Reinauer committed
296
	/* Program Opcode Types 0 - 7 */
297
	optype = 0;
298
	for (a = 0; a < 8; a++) {
299
		optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
300
	}
301

Stefan Reinauer's avatar
Stefan Reinauer committed
302
	/* Program Allowable Opcodes 0 - 3 */
303
	opmenu[0] = 0;
304
	for (a = 0; a < 4; a++) {
305
		opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Stefan Reinauer's avatar
Stefan Reinauer committed
306 307
	}

308
	/*Program Allowable Opcodes 4 - 7 */
309
	opmenu[1] = 0;
310
	for (a = 4; a < 8; a++) {
311
		opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Stefan Reinauer's avatar
Stefan Reinauer committed
312 313
	}

Peter Stuge's avatar
Peter Stuge committed
314
	printf_debug("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
315
	switch (flashbus) {
316 317
	case BUS_TYPE_ICH7_SPI:
	case BUS_TYPE_VIA_SPI:
318 319 320 321 322 323 324 325 326 327 328 329 330 331
		REGWRITE16(ICH7_REG_PREOP, preop);
		REGWRITE16(ICH7_REG_OPTYPE, optype);
		REGWRITE32(ICH7_REG_OPMENU, opmenu[0]);
		REGWRITE32(ICH7_REG_OPMENU + 4, opmenu[1]);
		break;
	case BUS_TYPE_ICH9_SPI:
		REGWRITE16(ICH9_REG_PREOP, preop);
		REGWRITE16(ICH9_REG_OPTYPE, optype);
		REGWRITE32(ICH9_REG_OPMENU, opmenu[0]);
		REGWRITE32(ICH9_REG_OPMENU + 4, opmenu[1]);
		break;
	default:
		printf_debug("%s: unsupported chipset\n", __FUNCTION__);
		return -1;
Stefan Reinauer's avatar
Stefan Reinauer committed
332 333 334 335 336
	}

	return 0;
}

FENG yu ning's avatar
FENG yu ning committed
337 338
/* This function generates OPCODES from or programs OPCODES to ICH according to
 * the chipset's SPI configuration lock.
339
 *
FENG yu ning's avatar
FENG yu ning committed
340
 * It should be called before ICH sends any spi command.
341
 */
342
int ich_init_opcodes(void)
343 344 345 346 347 348 349 350 351 352
{
	int rc = 0;
	OPCODES *curopcodes_done;

	if (curopcodes)
		return 0;

	if (ichspi_lock) {
		printf_debug("Generating OPCODES... ");
		curopcodes_done = &O_EXISTING;
FENG yu ning's avatar
FENG yu ning committed
353
		rc = generate_opcodes(curopcodes_done);
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	} else {
		printf_debug("Programming OPCODES... ");
		curopcodes_done = &O_ST_M25P;
		rc = program_opcodes(curopcodes_done);
	}

	if (rc) {
		curopcodes = NULL;
		printf_debug("failed\n");
		return 1;
	} else {
		curopcodes = curopcodes_done;
		printf_debug("done\n");
		return 0;
	}
}

371
static int ich7_run_opcode(OPCODE op, uint32_t offset,
372
			   uint8_t datalength, uint8_t * data, int maxdata)
Stefan Reinauer's avatar
Stefan Reinauer committed
373 374 375
{
	int write_cmd = 0;
	int timeout;
376
	uint32_t temp32 = 0;
Stefan Reinauer's avatar
Stefan Reinauer committed
377 378
	uint16_t temp16;
	uint32_t a;
379 380
	uint64_t opmenu;
	int opcode_index;
Stefan Reinauer's avatar
Stefan Reinauer committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

	/* Is it a write command? */
	if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
	    || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
		write_cmd = 1;
	}

	/* Programm Offset in Flash into FADDR */
	REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF));	/* SPI addresses are 24 BIT only */

	/* Program data into FDATA0 to N */
	if (write_cmd && (datalength != 0)) {
		temp32 = 0;
		for (a = 0; a < datalength; a++) {
			if ((a % 4) == 0) {
				temp32 = 0;
			}

			temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);

			if ((a % 4) == 3) {
				REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)),
					   temp32);
			}
		}
		if (((a - 1) % 4) != 3) {
			REGWRITE32(ICH7_REG_SPID0 +
				   ((a - 1) - ((a - 1) % 4)), temp32);
		}

	}

	/* Assemble SPIS */
	temp16 = 0;
	/* clear error status registers */
	temp16 |= (SPIS_CDS + SPIS_FCERR);
	REGWRITE16(ICH7_REG_SPIS, temp16);

	/* Assemble SPIC */
	temp16 = 0;

	if (datalength != 0) {
		temp16 |= SPIC_DS;
424
		temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauer's avatar
Stefan Reinauer committed
425 426 427
	}

	/* Select opcode */
428 429 430
	opmenu = REGREAD32(ICH7_REG_OPMENU);
	opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;

431 432
	for (opcode_index = 0; opcode_index < 8; opcode_index++) {
		if ((opmenu & 0xff) == op.opcode) {
433 434 435 436 437 438 439 440 441
			break;
		}
		opmenu >>= 8;
	}
	if (opcode_index == 8) {
		printf_debug("Opcode %x not found.\n", op.opcode);
		return 1;
	}
	temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauer's avatar
Stefan Reinauer committed
442 443 444 445 446

	/* Handle Atomic */
	if (op.atomic != 0) {
		/* Select atomic command */
		temp16 |= SPIC_ACS;
447
		/* Select prefix opcode */
Stefan Reinauer's avatar
Stefan Reinauer committed
448 449 450 451 452 453 454 455 456 457 458 459 460
		if ((op.atomic - 1) == 1) {
			/*Select prefix opcode 2 */
			temp16 |= SPIC_SPOP;
		}
	}

	/* Start */
	temp16 |= SPIC_SCGO;

	/* write it */
	REGWRITE16(ICH7_REG_SPIC, temp16);

	/* wait for cycle complete */
461
	timeout = 100 * 1000 * 60;	// 60s is a looong timeout.
Stefan Reinauer's avatar
Stefan Reinauer committed
462
	while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) {
463
		myusec_delay(10);
Stefan Reinauer's avatar
Stefan Reinauer committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	}
	if (!timeout) {
		printf_debug("timeout\n");
	}

	if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) {
		printf_debug("Transaction error!\n");
		return 1;
	}

	if ((!write_cmd) && (datalength != 0)) {
		for (a = 0; a < datalength; a++) {
			if ((a % 4) == 0) {
				temp32 = REGREAD32(ICH7_REG_SPID0 + (a));
			}

			data[a] =
			    (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
			    >> ((a % 4) * 8);
		}
484 485 486 487 488
	}

	return 0;
}

489
static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer's avatar
Stefan Reinauer committed
490
			   uint8_t datalength, uint8_t * data)
491 492
{
	int write_cmd = 0;
493
	int timeout;
494 495
	uint32_t temp32;
	uint32_t a;
496 497
	uint64_t opmenu;
	int opcode_index;
498 499 500 501 502 503 504 505

	/* Is it a write command? */
	if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
	    || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
		write_cmd = 1;
	}

	/* Programm Offset in Flash into FADDR */
Stefan Reinauer's avatar
Stefan Reinauer committed
506
	REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF));	/* SPI addresses are 24 BIT only */
507 508 509 510 511 512 513 514 515 516 517 518

	/* Program data into FDATA0 to N */
	if (write_cmd && (datalength != 0)) {
		temp32 = 0;
		for (a = 0; a < datalength; a++) {
			if ((a % 4) == 0) {
				temp32 = 0;
			}

			temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);

			if ((a % 4) == 3) {
Stefan Reinauer's avatar
Stefan Reinauer committed
519 520
				REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
					   temp32);
521 522 523
			}
		}
		if (((a - 1) % 4) != 3) {
Stefan Reinauer's avatar
Stefan Reinauer committed
524 525
			REGWRITE32(ICH9_REG_FDATA0 +
				   ((a - 1) - ((a - 1) % 4)), temp32);
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
		}
	}

	/* Assemble SSFS + SSFC */
	temp32 = 0;

	/* clear error status registers */
	temp32 |= (SSFS_CDS + SSFS_FCERR);
	/* USE 20 MhZ */
	temp32 |= SSFC_SCF_20MHZ;

	if (datalength != 0) {
		uint32_t datatemp;
		temp32 |= SSFC_DS;
		datatemp = ((uint32_t) ((datalength - 1) & 0x3f)) << (8 + 8);
		temp32 |= datatemp;
	}

	/* Select opcode */
545 546 547
	opmenu = REGREAD32(ICH9_REG_OPMENU);
	opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;

548 549
	for (opcode_index = 0; opcode_index < 8; opcode_index++) {
		if ((opmenu & 0xff) == op.opcode) {
550 551 552 553 554 555 556 557 558
			break;
		}
		opmenu >>= 8;
	}
	if (opcode_index == 8) {
		printf_debug("Opcode %x not found.\n", op.opcode);
		return 1;
	}
	temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

	/* Handle Atomic */
	if (op.atomic != 0) {
		/* Select atomic command */
		temp32 |= SSFC_ACS;
		/* Selct prefix opcode */
		if ((op.atomic - 1) == 1) {
			/*Select prefix opcode 2 */
			temp32 |= SSFC_SPOP;
		}
	}

	/* Start */
	temp32 |= SSFC_SCGO;

	/* write it */
Stefan Reinauer's avatar
Stefan Reinauer committed
575
	REGWRITE32(ICH9_REG_SSFS, temp32);
576 577

	/*wait for cycle complete */
578
	timeout = 100 * 1000 * 60;	// 60s is a looong timeout.
579
	while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) {
580
		myusec_delay(10);
581 582 583
	}
	if (!timeout) {
		printf_debug("timeout\n");
584 585
	}

Stefan Reinauer's avatar
Stefan Reinauer committed
586
	if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) {
587 588 589 590 591 592 593
		printf_debug("Transaction error!\n");
		return 1;
	}

	if ((!write_cmd) && (datalength != 0)) {
		for (a = 0; a < datalength; a++) {
			if ((a % 4) == 0) {
Stefan Reinauer's avatar
Stefan Reinauer committed
594
				temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
595 596 597
			}

			data[a] =
Stefan Reinauer's avatar
Stefan Reinauer committed
598 599
			    (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
			    >> ((a % 4) * 8);
600 601 602 603 604 605
		}
	}

	return 0;
}

606
static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer's avatar
Stefan Reinauer committed
607 608
		      uint8_t datalength, uint8_t * data)
{
609 610
	switch (flashbus) {
	case BUS_TYPE_VIA_SPI:
611
		return ich7_run_opcode(op, offset, datalength, data, 16);
612
	case BUS_TYPE_ICH7_SPI:
613
		return ich7_run_opcode(op, offset, datalength, data, 64);
614
	case BUS_TYPE_ICH9_SPI:
615
		return ich9_run_opcode(op, offset, datalength, data);
616 617 618
	default:
		printf_debug("%s: unsupported chipset\n", __FUNCTION__);
	}
Stefan Reinauer's avatar
Stefan Reinauer committed
619 620 621 622 623

	/* If we ever get here, something really weird happened */
	return -1;
}

624 625
static int ich_spi_read_page(struct flashchip *flash, uint8_t * buf, int offset,
			     int maxdata)
626 627 628 629 630
{
	int page_size = flash->page_size;
	uint32_t remaining = flash->page_size;
	int a;

Stefan Reinauer's avatar
Stefan Reinauer committed
631 632
	printf_debug("ich_spi_read_page: offset=%d, number=%d, buf=%p\n",
		     offset, page_size, buf);
633

634 635
	for (a = 0; a < page_size; a += maxdata) {
		if (remaining < maxdata) {
636

637 638
			if (spi_nbyte_read(offset + (page_size - remaining),
				&buf[page_size - remaining], remaining)) {
639 640 641 642 643
				printf_debug("Error reading");
				return 1;
			}
			remaining = 0;
		} else {
644 645
			if (spi_nbyte_read(offset + (page_size - remaining),
				&buf[page_size - remaining], maxdata)) {
646 647 648
				printf_debug("Error reading");
				return 1;
			}
649
			remaining -= maxdata;
650 651 652 653 654 655 656
		}
	}

	return 0;
}

static int ich_spi_write_page(struct flashchip *flash, uint8_t * bytes,
657
			      int offset, int maxdata)
658 659 660 661 662
{
	int page_size = flash->page_size;
	uint32_t remaining = page_size;
	int a;

Stefan Reinauer's avatar
Stefan Reinauer committed
663 664
	printf_debug("ich_spi_write_page: offset=%d, number=%d, buf=%p\n",
		     offset, page_size, bytes);
665

666 667
	for (a = 0; a < page_size; a += maxdata) {
		if (remaining < maxdata) {
668
			if (run_opcode
669
			    (curopcodes->opcode[0],
Stefan Reinauer's avatar
Stefan Reinauer committed
670
			     offset + (page_size - remaining), remaining,
671 672 673 674 675 676 677
			     &bytes[page_size - remaining]) != 0) {
				printf_debug("Error writing");
				return 1;
			}
			remaining = 0;
		} else {
			if (run_opcode
678
			    (curopcodes->opcode[0],
679
			     offset + (page_size - remaining), maxdata,
680 681 682 683
			     &bytes[page_size - remaining]) != 0) {
				printf_debug("Error writing");
				return 1;
			}
684
			remaining -= maxdata;
685 686 687 688 689 690 691 692 693 694 695
		}
	}

	return 0;
}

int ich_spi_read(struct flashchip *flash, uint8_t * buf)
{
	int i, rc = 0;
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;
696 697
	int maxdata = 64;

698
	if (flashbus == BUS_TYPE_VIA_SPI) {
699 700
		maxdata = 16;
	}
701 702 703

	for (i = 0; (i < total_size / page_size) && (rc == 0); i++) {
		rc = ich_spi_read_page(flash, (void *)(buf + i * page_size),
704
				       i * page_size, maxdata);
705 706 707 708 709
	}

	return rc;
}

710
int ich_spi_write_256(struct flashchip *flash, uint8_t * buf)
711 712 713 714 715
{
	int i, j, rc = 0;
	int total_size = flash->total_size * 1024;
	int page_size = flash->page_size;
	int erase_size = 64 * 1024;
716
	int maxdata = 64;
717 718 719 720 721 722

	spi_disable_blockprotect();

	printf("Programming page: \n");

	for (i = 0; i < total_size / erase_size; i++) {
723 724 725 726 727
		/* FIMXE: call the chip-specific spi_block_erase_XX instead.
		 * For this, we need to add a block erase function to
		 * struct flashchip.
		 */
		rc = spi_block_erase_d8(flash, i * erase_size);
728 729 730 731
		if (rc) {
			printf("Error erasing block at 0x%x\n", i);
			break;
		}
732

Peter Stuge's avatar
Peter Stuge committed
733 734 735
		if (flashbus == BUS_TYPE_VIA_SPI)
			maxdata = 16;

736
		for (j = 0; j < erase_size / page_size; j++) {
737 738 739
			ich_spi_write_page(flash,
			   (void *)(buf + (i * erase_size) + (j * page_size)),
			   (i * erase_size) + (j * page_size), maxdata);
740 741 742 743 744 745 746 747
		}
	}

	printf("\n");

	return rc;
}

748 749
int ich_spi_command(unsigned int writecnt, unsigned int readcnt,
		    const unsigned char *writearr, unsigned char *readarr)
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
{
	int a;
	int opcode_index = -1;
	const unsigned char cmd = *writearr;
	OPCODE *opcode;
	uint32_t addr = 0;
	uint8_t *data;
	int count;

	/* find cmd in opcodes-table */
	for (a = 0; a < 8; a++) {
		if ((curopcodes->opcode[a]).opcode == cmd) {
			opcode_index = a;
			break;
		}
	}

	/* unknown / not programmed command */
	if (opcode_index == -1) {
		printf_debug("Invalid OPCODE 0x%02x\n", cmd);
		return 1;
	}

	opcode = &(curopcodes->opcode[opcode_index]);

	/* if opcode-type requires an address */
	if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
	    opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
778 779
		addr = (writearr[1] << 16) |
		    (writearr[2] << 8) | (writearr[3] << 0);
780
	}
781

782 783
	/* translate read/write array/count */
	if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
784 785 786 787 788 789 790
		data = (uint8_t *) (writearr + 1);
		count = writecnt - 1;
	} else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
		data = (uint8_t *) (writearr + 4);
		count = writecnt - 4;
	} else {
		data = (uint8_t *) readarr;
791 792
		count = readcnt;
	}
793

794
	if (run_opcode(*opcode, addr, count, data) != 0) {
795 796 797 798 799 800
		printf_debug("run OPCODE 0x%02x failed\n", opcode->opcode);
		return 1;
	}

	return 0;
}