ichspi.c 28.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
 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
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
 *
 * 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
 *
 */

36 37
#if defined(__i386__) || defined(__x86_64__)

38 39
#include <string.h>
#include "flash.h"
40
#include "chipdrivers.h"
41
#include "programmer.h"
42 43
#include "spi.h"

Stefan Reinauer's avatar
Stefan Reinauer committed
44 45 46 47 48
/* 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 */
49 50 51 52
#define SSFS_SCIP		0x00000001
#define SSFS_CDS		0x00000004
#define SSFS_FCERR		0x00000008
#define SSFS_AEL		0x00000010
Stefan Reinauer's avatar
Stefan Reinauer committed
53 54

#define ICH9_REG_SSFC          0x91	/* 24 Bits */
55 56 57 58 59 60 61 62 63 64
#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
65 66 67 68

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

// 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
76 77 78 79 80 81
// ICH7 registers
#define ICH7_REG_SPIS          0x00	/* 16 Bits */
#define SPIS_SCIP              0x00000001
#define SPIS_CDS               0x00000004
#define SPIS_FCERR             0x00000008

82 83 84 85 86 87 88 89 90 91
/* 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
92 93 94 95
#define ICH7_REG_SPIC          0x02	/* 16 Bits */
#define SPIC_SCGO              0x0002
#define SPIC_ACS               0x0004
#define SPIC_SPOP              0x0008
96
#define SPIC_DS                0x4000
Stefan Reinauer's avatar
Stefan Reinauer committed
97 98 99 100 101 102 103

#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 */

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

107 108
uint32_t ichspi_bbar = 0;

109
static void *ich_spibar = NULL;
110

111 112 113 114 115 116
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;

117
/* Suggested opcode definition:
118 119 120 121 122 123 124 125 126
 * 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
127
 * OP 6: chip private (read JEDEC id)
128 129 130 131 132 133 134
 * OP 7: Chip erase
 */
typedef struct _OPCODES {
	uint8_t preop[2];
	OPCODE opcode[8];
} OPCODES;

135
static OPCODES *curopcodes = NULL;
136 137

/* HW access functions */
138
static uint32_t REGREAD32(int X)
139
{
140
	return mmio_readl(ich_spibar + X);
Stefan Reinauer's avatar
Stefan Reinauer committed
141 142
}

143
static uint16_t REGREAD16(int X)
Stefan Reinauer's avatar
Stefan Reinauer committed
144
{
145
	return mmio_readw(ich_spibar + X);
146 147
}

148 149 150
#define REGWRITE32(X,Y) mmio_writel(Y, ich_spibar+X)
#define REGWRITE16(X,Y) mmio_writew(Y, ich_spibar+X)
#define REGWRITE8(X,Y)  mmio_writeb(Y, ich_spibar+X)
151 152

/* Common SPI functions */
153 154
static int find_opcode(OPCODES *op, uint8_t opcode);
static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ning's avatar
FENG yu ning committed
155
static int generate_opcodes(OPCODES * op);
156
static int program_opcodes(OPCODES * op);
157
static int run_opcode(OPCODE op, uint32_t offset,
158
		      uint8_t datalength, uint8_t * data);
159

FENG yu ning's avatar
FENG yu ning committed
160 161 162 163 164 165
/* for pairing opcodes with their required preop */
struct preop_opcode_pair {
	uint8_t preop;
	uint8_t opcode;
};

166
/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
167
const struct preop_opcode_pair pops[] = {
FENG yu ning's avatar
FENG yu ning committed
168 169 170 171 172 173
	{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 */
174 175
	 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
	{JEDEC_WREN, JEDEC_WRSR},
FENG yu ning's avatar
FENG yu ning committed
176 177 178 179
	{JEDEC_EWSR, JEDEC_WRSR},
	{0,}
};

180 181 182
/* Reasonable default configuration. Needs ad-hoc modifications if we
 * encounter unlisted opcodes. Fun.
 */
183
static OPCODES O_ST_M25P = {
184 185
	{
	 JEDEC_WREN,
186 187
	 JEDEC_EWSR,
	},
188
	{
189
	 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0},	// Write Byte
190
	 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0},	// Read Data
191
	 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0},	// Erase Sector
192
	 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0},	// Read Device Status Reg
193
	 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0},	// Read Electronic Manufacturer Signature
194
	 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0},	// Write Status Register
195
	 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0},	// Read JDEC ID
196 197
	 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0},	// Bulk erase
	}
198 199
};

200
static OPCODES O_EXISTING = {};
201

202
static int find_opcode(OPCODES *op, uint8_t opcode)
203 204 205 206 207 208 209 210 211 212 213
{
	int a;

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

	return -1;
}

214
static int find_preop(OPCODES *op, uint8_t preop)
215 216 217 218 219 220 221 222 223 224 225
{
	int a;

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

	return -1;
}

226
/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ning's avatar
FENG yu ning committed
227
static int generate_opcodes(OPCODES * op)
228
{
229
	int a;
230 231 232 233
	uint16_t preop, optype;
	uint32_t opmenu[2];

	if (op == NULL) {
234
		msg_perr("\n%s: null OPCODES pointer!\n", __func__);
235 236 237
		return -1;
	}

238 239 240
	switch (spi_controller) {
	case SPI_CONTROLLER_ICH7:
	case SPI_CONTROLLER_VIA:
241 242 243 244 245
		preop = REGREAD16(ICH7_REG_PREOP);
		optype = REGREAD16(ICH7_REG_OPTYPE);
		opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
		opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
		break;
246
	case SPI_CONTROLLER_ICH9:
247 248 249 250 251 252
		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:
253
		msg_perr("%s: unsupported chipset\n", __func__);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
		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;
	}

275 276
	/* No preopcodes used by default. */
	for (a = 0; a < 8; a++)
277 278 279 280 281
		op->opcode[a].atomic = 0;

	return 0;
}

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

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

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

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

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

312
	msg_pdbg("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
313 314 315
	switch (spi_controller) {
	case SPI_CONTROLLER_ICH7:
	case SPI_CONTROLLER_VIA:
316 317 318 319 320
		REGWRITE16(ICH7_REG_PREOP, preop);
		REGWRITE16(ICH7_REG_OPTYPE, optype);
		REGWRITE32(ICH7_REG_OPMENU, opmenu[0]);
		REGWRITE32(ICH7_REG_OPMENU + 4, opmenu[1]);
		break;
321
	case SPI_CONTROLLER_ICH9:
322 323 324 325 326 327
		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:
328
		msg_perr("%s: unsupported chipset\n", __func__);
329
		return -1;
Stefan Reinauer's avatar
Stefan Reinauer committed
330 331 332 333 334
	}

	return 0;
}

335 336 337 338 339 340 341 342
/*
 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
 * it didn't stick.
 */
void ich_set_bbar(uint32_t minaddr)
{
	switch (spi_controller) {
	case SPI_CONTROLLER_ICH7:
343 344
		mmio_writel(minaddr, ich_spibar + 0x50);
		ichspi_bbar = mmio_readl(ich_spibar + 0x50);
345 346 347 348 349
		/* We don't have any option except complaining. */
		if (ichspi_bbar != minaddr)
			msg_perr("Setting BBAR failed!\n");
		break;
	case SPI_CONTROLLER_ICH9:
350 351
		mmio_writel(minaddr, ich_spibar + 0xA0);
		ichspi_bbar = mmio_readl(ich_spibar + 0xA0);
352 353 354 355 356 357 358 359 360 361 362
		/* We don't have any option except complaining. */
		if (ichspi_bbar != minaddr)
			msg_perr("Setting BBAR failed!\n");
		break;
	default:
		/* Not sure if BBAR actually exists on VIA. */
		msg_pdbg("Setting BBAR is not implemented for VIA yet.\n");
		break;
	}
}

FENG yu ning's avatar
FENG yu ning committed
363 364
/* This function generates OPCODES from or programs OPCODES to ICH according to
 * the chipset's SPI configuration lock.
365
 *
FENG yu ning's avatar
FENG yu ning committed
366
 * It should be called before ICH sends any spi command.
367
 */
368
static int ich_init_opcodes(void)
369 370 371 372 373 374 375 376
{
	int rc = 0;
	OPCODES *curopcodes_done;

	if (curopcodes)
		return 0;

	if (ichspi_lock) {
377
		msg_pdbg("Reading OPCODES... ");
378
		curopcodes_done = &O_EXISTING;
FENG yu ning's avatar
FENG yu ning committed
379
		rc = generate_opcodes(curopcodes_done);
380
	} else {
381
		msg_pdbg("Programming OPCODES... ");
382 383
		curopcodes_done = &O_ST_M25P;
		rc = program_opcodes(curopcodes_done);
384 385 386 387 388
		/* Technically not part of opcode init, but it allows opcodes
		 * to run without transaction errors by setting the lowest
		 * allowed address to zero.
		 */
		ich_set_bbar(0);
389 390 391 392
	}

	if (rc) {
		curopcodes = NULL;
393
		msg_perr("failed\n");
394 395 396
		return 1;
	} else {
		curopcodes = curopcodes_done;
397
		msg_pdbg("done\n");
398 399 400 401
		return 0;
	}
}

402
static int ich7_run_opcode(OPCODE op, uint32_t offset,
403
			   uint8_t datalength, uint8_t * data, int maxdata)
Stefan Reinauer's avatar
Stefan Reinauer committed
404 405 406
{
	int write_cmd = 0;
	int timeout;
407
	uint32_t temp32 = 0;
Stefan Reinauer's avatar
Stefan Reinauer committed
408 409
	uint16_t temp16;
	uint32_t a;
410 411
	uint64_t opmenu;
	int opcode_index;
Stefan Reinauer's avatar
Stefan Reinauer committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

	/* 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;
455
		temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauer's avatar
Stefan Reinauer committed
456 457 458
	}

	/* Select opcode */
459 460 461
	opmenu = REGREAD32(ICH7_REG_OPMENU);
	opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;

462 463
	for (opcode_index = 0; opcode_index < 8; opcode_index++) {
		if ((opmenu & 0xff) == op.opcode) {
464 465 466 467 468
			break;
		}
		opmenu >>= 8;
	}
	if (opcode_index == 8) {
469
		msg_pdbg("Opcode %x not found.\n", op.opcode);
470 471 472
		return 1;
	}
	temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauer's avatar
Stefan Reinauer committed
473 474

	/* Handle Atomic */
475 476 477 478 479 480 481
	switch (op.atomic) {
	case 2:
		/* Select second preop. */
		temp16 |= SPIC_SPOP;
		/* And fall through. */
	case 1:
		/* Atomic command (preop+op) */
Stefan Reinauer's avatar
Stefan Reinauer committed
482
		temp16 |= SPIC_ACS;
483
		break;
Stefan Reinauer's avatar
Stefan Reinauer committed
484 485 486 487 488 489 490 491 492
	}

	/* Start */
	temp16 |= SPIC_SCGO;

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

	/* wait for cycle complete */
493
	timeout = 100 * 1000 * 60;	// 60s is a looong timeout.
Stefan Reinauer's avatar
Stefan Reinauer committed
494
	while (((REGREAD16(ICH7_REG_SPIS) & SPIS_CDS) == 0) && --timeout) {
495
		programmer_delay(10);
Stefan Reinauer's avatar
Stefan Reinauer committed
496 497
	}
	if (!timeout) {
498
		msg_perr("timeout\n");
Stefan Reinauer's avatar
Stefan Reinauer committed
499 500
	}

501
	/* FIXME: make sure we do not needlessly cause transaction errors. */
Stefan Reinauer's avatar
Stefan Reinauer committed
502
	if ((REGREAD16(ICH7_REG_SPIS) & SPIS_FCERR) != 0) {
503
		msg_pdbg("Transaction error!\n");
Stefan Reinauer's avatar
Stefan Reinauer committed
504 505 506 507 508 509 510 511 512 513 514 515 516
		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);
		}
517 518 519 520 521
	}

	return 0;
}

522
static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer's avatar
Stefan Reinauer committed
523
			   uint8_t datalength, uint8_t * data)
524 525
{
	int write_cmd = 0;
526
	int timeout;
527 528
	uint32_t temp32;
	uint32_t a;
529 530
	uint64_t opmenu;
	int opcode_index;
531 532 533 534 535 536 537 538

	/* 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
539
	REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF));	/* SPI addresses are 24 BIT only */
540 541 542 543 544 545 546 547 548 549 550 551

	/* 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
552 553
				REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
					   temp32);
554 555 556
			}
		}
		if (((a - 1) % 4) != 3) {
Stefan Reinauer's avatar
Stefan Reinauer committed
557 558
			REGWRITE32(ICH9_REG_FDATA0 +
				   ((a - 1) - ((a - 1) % 4)), temp32);
559 560 561 562
		}
	}

	/* Assemble SSFS + SSFC */
563 564 565
	/* keep reserved bits (23-19,7,0) */
	temp32 = REGREAD32(ICH9_REG_SSFS);
	temp32 &= 0xF8008100;
566 567 568

	/* clear error status registers */
	temp32 |= (SSFS_CDS + SSFS_FCERR);
569
	/* Use 20 MHz */
570 571 572 573 574 575 576 577 578 579
	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 */
580 581 582
	opmenu = REGREAD32(ICH9_REG_OPMENU);
	opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;

583 584
	for (opcode_index = 0; opcode_index < 8; opcode_index++) {
		if ((opmenu & 0xff) == op.opcode) {
585 586 587 588 589
			break;
		}
		opmenu >>= 8;
	}
	if (opcode_index == 8) {
590
		msg_pdbg("Opcode %x not found.\n", op.opcode);
591 592 593
		return 1;
	}
	temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
594 595

	/* Handle Atomic */
596 597 598 599 600 601 602
	switch (op.atomic) {
	case 2:
		/* Select second preop. */
		temp32 |= SSFC_SPOP;
		/* And fall through. */
	case 1:
		/* Atomic command (preop+op) */
603
		temp32 |= SSFC_ACS;
604
		break;
605 606 607 608 609 610
	}

	/* Start */
	temp32 |= SSFC_SCGO;

	/* write it */
Stefan Reinauer's avatar
Stefan Reinauer committed
611
	REGWRITE32(ICH9_REG_SSFS, temp32);
612 613

	/*wait for cycle complete */
614
	timeout = 100 * 1000 * 60;	// 60s is a looong timeout.
615
	while (((REGREAD32(ICH9_REG_SSFS) & SSFS_CDS) == 0) && --timeout) {
616
		programmer_delay(10);
617 618
	}
	if (!timeout) {
619
		msg_perr("timeout\n");
620 621
	}

622
	/* FIXME make sure we do not needlessly cause transaction errors. */
Stefan Reinauer's avatar
Stefan Reinauer committed
623
	if ((REGREAD32(ICH9_REG_SSFS) & SSFS_FCERR) != 0) {
624
		msg_pdbg("Transaction error!\n");
625 626 627 628 629 630
		return 1;
	}

	if ((!write_cmd) && (datalength != 0)) {
		for (a = 0; a < datalength; a++) {
			if ((a % 4) == 0) {
Stefan Reinauer's avatar
Stefan Reinauer committed
631
				temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
632 633 634
			}

			data[a] =
Stefan Reinauer's avatar
Stefan Reinauer committed
635 636
			    (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
			    >> ((a % 4) * 8);
637 638 639 640 641 642
		}
	}

	return 0;
}

643
static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer's avatar
Stefan Reinauer committed
644 645
		      uint8_t datalength, uint8_t * data)
{
646 647
	switch (spi_controller) {
	case SPI_CONTROLLER_VIA:
648
		if (datalength > 16) {
649
			msg_perr("%s: Internal command size error for "
650 651
				"opcode 0x%02x, got datalength=%i, want <=16\n",
				__func__, op.opcode, datalength);
652
			return SPI_INVALID_LENGTH;
653
		}
654
		return ich7_run_opcode(op, offset, datalength, data, 16);
655
	case SPI_CONTROLLER_ICH7:
656
		if (datalength > 64) {
657
			msg_perr("%s: Internal command size error for "
658 659
				"opcode 0x%02x, got datalength=%i, want <=16\n",
				__func__, op.opcode, datalength);
660
			return SPI_INVALID_LENGTH;
661
		}
662
		return ich7_run_opcode(op, offset, datalength, data, 64);
663
	case SPI_CONTROLLER_ICH9:
664
		if (datalength > 64) {
665
			msg_perr("%s: Internal command size error for "
666 667
				"opcode 0x%02x, got datalength=%i, want <=16\n",
				__func__, op.opcode, datalength);
668
			return SPI_INVALID_LENGTH;
669
		}
670
		return ich9_run_opcode(op, offset, datalength, data);
671
	default:
672
		msg_perr("%s: unsupported chipset\n", __func__);
673
	}
Stefan Reinauer's avatar
Stefan Reinauer committed
674 675 676 677 678

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

679
int ich_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len)
680
{
681 682
	int maxdata = 64;

683
	if (spi_controller == SPI_CONTROLLER_VIA)
684
		maxdata = 16;
685

686
	return spi_read_chunked(flash, buf, start, len, maxdata);
687 688
}

689
int ich_spi_write_256(struct flashchip *flash, uint8_t * buf, int start, int len)
690
{
691
	int maxdata = 64;
692

693 694 695
	if (spi_controller == SPI_CONTROLLER_VIA)
		maxdata = 16;

696
	return spi_write_chunked(flash, buf, start, len, maxdata);
697 698
}

699
int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
700
		    const unsigned char *writearr, unsigned char *readarr)
701
{
702
	int result;
703 704 705 706 707 708 709 710
	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 */
711
	opcode_index = find_opcode(curopcodes, cmd);
712
	if (opcode_index == -1) {
713 714 715
		/* FIXME: Reprogram opcodes if possible. Autodetect type of
		 * opcode by checking readcnt/writecnt.
		 */
716
		msg_pdbg("Invalid OPCODE 0x%02x\n", cmd);
717
		return SPI_INVALID_OPCODE;
718 719 720 721
	}

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

722 723 724 725 726 727 728 729 730
	/* The following valid writecnt/readcnt combinations exist:
	 * writecnt  = 4, readcnt >= 0
	 * writecnt  = 1, readcnt >= 0
	 * writecnt >= 4, readcnt  = 0
	 * writecnt >= 1, readcnt  = 0
	 * writecnt >= 1 is guaranteed for all commands.
	 */
	if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
	    (writecnt != 4)) {
731
		msg_perr("%s: Internal command size error for opcode "
732 733 734 735 736 737
			"0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
			writecnt);
		return SPI_INVALID_LENGTH;
	}
	if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
	    (writecnt != 1)) {
738
		msg_perr("%s: Internal command size error for opcode "
739 740 741 742 743 744
			"0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
			writecnt);
		return SPI_INVALID_LENGTH;
	}
	if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
	    (writecnt < 4)) {
745
		msg_perr("%s: Internal command size error for opcode "
746 747 748 749 750 751 752
			"0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
			writecnt);
		return SPI_INVALID_LENGTH;
	}
	if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
	     (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
	    (readcnt)) {
753
		msg_perr("%s: Internal command size error for opcode "
754 755 756 757 758
			"0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
			readcnt);
		return SPI_INVALID_LENGTH;
	}

759 760 761
	/* 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) {
762 763
		addr = (writearr[1] << 16) |
		    (writearr[2] << 8) | (writearr[3] << 0);
764 765 766 767 768 769 770 771 772 773 774 775 776
		switch (spi_controller) {
		case SPI_CONTROLLER_ICH7:
		case SPI_CONTROLLER_ICH9:
			if (addr < ichspi_bbar) {
				msg_perr("%s: Address 0x%06x below allowed "
					 "range 0x%06x-0xffffff\n", __func__,
					 addr, ichspi_bbar);
				return SPI_INVALID_ADDRESS;
			}
			break;
		default:
			break;
		}
777
	}
778

779 780
	/* translate read/write array/count */
	if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
781 782 783 784 785 786 787
		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;
788 789
		count = readcnt;
	}
790

791 792
	result = run_opcode(*opcode, addr, count, data);
	if (result) {
793
		msg_pdbg("run OPCODE 0x%02x failed\n", opcode->opcode);
794 795
	}

796
	return result;
797
}
798

799
int ich_spi_send_multicommand(struct spi_command *cmds)
800 801
{
	int ret = 0;
802
	int i;
803 804 805
	int oppos, preoppos;
	for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
		if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
806
			/* Next command is valid. */
807 808
			preoppos = find_preop(curopcodes, cmds->writearr[0]);
			oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
809 810 811 812 813 814 815 816 817
			if ((oppos == -1) && (preoppos != -1)) {
				/* Current command is listed as preopcode in
				 * ICH struct OPCODES, but next command is not
				 * listed as opcode in that struct.
				 * Check for command sanity, then
				 * try to reprogram the ICH opcode list.
				 */
				if (find_preop(curopcodes,
					       (cmds + 1)->writearr[0]) != -1) {
818
					msg_perr("%s: Two subsequent "
819 820 821 822 823 824 825 826 827 828 829
						"preopcodes 0x%02x and 0x%02x, "
						"ignoring the first.\n",
						__func__, cmds->writearr[0],
						(cmds + 1)->writearr[0]);
					continue;
				}
				/* If the chipset is locked down, we'll fail
				 * during execution of the next command anyway.
				 * No need to bother with fixups.
				 */
				if (!ichspi_lock) {
830
					msg_pdbg("%s: FIXME: Add on-the-fly"
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
						     " reprogramming of the "
						     "chipset opcode list.\n",
						     __func__);
				 	/* FIXME: Reprogram opcode menu.
					 * Find a less-useful opcode, replace it
					 * with the wanted opcode, detect optype
					 * and reprogram the opcode menu.
					 * Update oppos so the next if-statement
					 * can do something useful.
					 */
					//curopcodes.opcode[lessusefulindex] = (cmds + 1)->writearr[0]);
					//update_optypes(curopcodes);
					//program_opcodes(curopcodes);
					//oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
					continue;
				}
			}
			if ((oppos != -1) && (preoppos != -1)) {
				/* Current command is listed as preopcode in
				 * ICH struct OPCODES and next command is listed
				 * as opcode in that struct. Match them up.
				 */
				curopcodes->opcode[oppos].atomic = preoppos + 1;
854
				continue;
855 856 857 858 859
			}
			/* If none of the above if-statements about oppos or
			 * preoppos matched, this is a normal opcode.
			 */
		}
860 861
		ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
					   cmds->writearr, cmds->readarr);
862 863 864
		/* Reset the type of all opcodes to non-atomic. */
		for (i = 0; i < 8; i++)
			curopcodes->opcode[i].atomic = 0;
865 866 867
	}
	return ret;
}
868

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
#define ICH_BMWAG(x) ((x >> 24) & 0xff)
#define ICH_BMRAG(x) ((x >> 16) & 0xff)
#define ICH_BRWA(x)  ((x >>  8) & 0xff)
#define ICH_BRRA(x)  ((x >>  0) & 0xff)

#define ICH_FREG_BASE(x)  ((x >>  0) & 0x1fff)
#define ICH_FREG_LIMIT(x) ((x >> 16) & 0x1fff)

static void do_ich9_spi_frap(uint32_t frap, int i)
{
	const char *access_names[4] = {
		"locked", "read-only", "write-only", "read-write"
	};
	const char *region_names[5] = {
		"Flash Descriptor", "BIOS", "Management Engine",
		"Gigabit Ethernet", "Platform Data"
	};
	uint32_t base, limit;
	int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
		      (((ICH_BRRA(frap) >> i) & 1) << 0);
	int offset = 0x54 + i * 4;
	uint32_t freg = mmio_readl(ich_spibar + offset);

	msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n",
		     offset, freg, i, region_names[i]);

	base  = ICH_FREG_BASE(freg);
	limit = ICH_FREG_LIMIT(freg);
	if (base == 0x1fff && limit == 0) {
		/* this FREG is disabled */
		msg_pdbg("%s region is unused.\n", region_names[i]);
		return;
	}

	msg_pdbg("0x%08x-0x%08x is %s\n",
		    (base << 12), (limit << 12) | 0x0fff,
		    access_names[rwperms]);
}

int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb,
			int ich_generation)
{
	int i;
	uint8_t old, new;
	uint16_t spibar_offset, tmp2;
	uint32_t tmp;

	buses_supported |= CHIP_BUSTYPE_SPI;
	switch (ich_generation) {
	case 7:
		spi_controller = SPI_CONTROLLER_ICH7;
		spibar_offset = 0x3020;
		break;
	case 8:
		spi_controller = SPI_CONTROLLER_ICH9;
		spibar_offset = 0x3020;
		break;
	case 9:
	case 10:
	default:		/* Future version might behave the same */
		spi_controller = SPI_CONTROLLER_ICH9;
		spibar_offset = 0x3800;
		break;
	}

	/* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
	msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset);

	/* Assign Virtual Address */
	ich_spibar = rcrb + spibar_offset;

	switch (spi_controller) {
	case SPI_CONTROLLER_ICH7:
		msg_pdbg("0x00: 0x%04x     (SPIS)\n",
			     mmio_readw(ich_spibar + 0));
		msg_pdbg("0x02: 0x%04x     (SPIC)\n",
			     mmio_readw(ich_spibar + 2));
		msg_pdbg("0x04: 0x%08x (SPIA)\n",
			     mmio_readl(ich_spibar + 4));
		for (i = 0; i < 8; i++) {
			int offs;
			offs = 8 + (i * 8);
			msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
				     mmio_readl(ich_spibar + offs), i);
			msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
				     mmio_readl(ich_spibar + offs + 4), i);
		}
		ichspi_bbar = mmio_readl(ich_spibar + 0x50);
		msg_pdbg("0x50: 0x%08x (BBAR)\n",
			     ichspi_bbar);
		msg_pdbg("0x54: 0x%04x     (PREOP)\n",
			     mmio_readw(ich_spibar + 0x54));
		msg_pdbg("0x56: 0x%04x     (OPTYPE)\n",
			     mmio_readw(ich_spibar + 0x56));
		msg_pdbg("0x58: 0x%08x (OPMENU)\n",
			     mmio_readl(ich_spibar + 0x58));
		msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
			     mmio_readl(ich_spibar + 0x5c));
		for (i = 0; i < 4; i++) {
			int offs;
			offs = 0x60 + (i * 4);
			msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
				     mmio_readl(ich_spibar + offs), i);
		}
		msg_pdbg("\n");
		if (mmio_readw(ich_spibar) & (1 << 15)) {
			msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
			ichspi_lock = 1;
		}
		ich_init_opcodes();
		break;
	case SPI_CONTROLLER_ICH9:
		tmp2 = mmio_readw(ich_spibar + 4);
		msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
		msg_pdbg("FLOCKDN %i, ", (tmp2 >> 15 & 1));
		msg_pdbg("FDV %i, ", (tmp2 >> 14) & 1);
		msg_pdbg("FDOPSS %i, ", (tmp2 >> 13) & 1);
		msg_pdbg("SCIP %i, ", (tmp2 >> 5) & 1);
		msg_pdbg("BERASE %i, ", (tmp2 >> 3) & 3);
		msg_pdbg("AEL %i, ", (tmp2 >> 2) & 1);
		msg_pdbg("FCERR %i, ", (tmp2 >> 1) & 1);
		msg_pdbg("FDONE %i\n", (tmp2 >> 0) & 1);

		tmp = mmio_readl(ich_spibar + 0x50);
		msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
		msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
		msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
		msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
		msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));

		/* print out the FREGx registers along with FRAP access bits */
		for(i = 0; i < 5; i++)
			do_ich9_spi_frap(tmp, i);

		msg_pdbg("0x74: 0x%08x (PR0)\n",
			     mmio_readl(ich_spibar + 0x74));
		msg_pdbg("0x78: 0x%08x (PR1)\n",
			     mmio_readl(ich_spibar + 0x78));
		msg_pdbg("0x7C: 0x%08x (PR2)\n",
			     mmio_readl(ich_spibar + 0x7C));
		msg_pdbg("0x80: 0x%08x (PR3)\n",
			     mmio_readl(ich_spibar + 0x80));
		msg_pdbg("0x84: 0x%08x (PR4)\n",
			     mmio_readl(ich_spibar + 0x84));
		msg_pdbg("0x90: 0x%08x (SSFS, SSFC)\n",
			     mmio_readl(ich_spibar + 0x90));
		msg_pdbg("0x94: 0x%04x     (PREOP)\n",
			     mmio_readw(ich_spibar + 0x94));
		msg_pdbg("0x96: 0x%04x     (OPTYPE)\n",
			     mmio_readw(ich_spibar + 0x96));
		msg_pdbg("0x98: 0x%08x (OPMENU)\n",
			     mmio_readl(ich_spibar + 0x98));
		msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
			     mmio_readl(ich_spibar + 0x9C));
		ichspi_bbar = mmio_readl(ich_spibar + 0xA0);
		msg_pdbg("0xA0: 0x%08x (BBAR)\n",
			     ichspi_bbar);
		msg_pdbg("0xB0: 0x%08x (FDOC)\n",
			     mmio_readl(ich_spibar + 0xB0));
		if (tmp2 & (1 << 15)) {
			msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
			ichspi_lock = 1;
		}
		ich_init_opcodes();
		break;
	default:
		/* Nothing */
		break;
	}

	old = pci_read_byte(dev, 0xdc);
	msg_pdbg("SPI Read Configuration: ");
	new = (old >> 2) & 0x3;
	switch (new) {
	case 0:
	case 1:
	case 2:
		msg_pdbg("prefetching %sabled, caching %sabled, ",
			     (new & 0x2) ? "en" : "dis",
			     (new & 0x1) ? "dis" : "en");
		break;
	default:
		msg_pdbg("invalid prefetching/caching settings, ");
		break;
	}
	return 0;
}

int via_init_spi(struct pci_dev *dev)
{
	uint32_t mmio_base;

	mmio_base = (pci_read_long(dev, 0xbc)) << 8;
	msg_pdbg("MMIO base at = 0x%x\n", mmio_base);
	ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);

	msg_pdbg("0x6c: 0x%04x     (CLOCK/DEBUG)\n",
		     mmio_readw(ich_spibar + 0x6c));

	/* Not sure if it speaks all these bus protocols. */
	buses_supported = CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH | CHIP_BUSTYPE_SPI;
	spi_controller = SPI_CONTROLLER_VIA;
	ich_init_opcodes();

	return 0;
}

1076
#endif