blob: 983a7548e2d2ef790a721d13e99db1b99e1c934e [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>
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00008 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
Dominik Geyerb46acba2008-05-16 12:55:55 +00009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Dominik Geyerb46acba2008-05-16 12:55:55 +000023 */
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
Helge Wagner738e2522010-10-05 22:06:05 +000033 * ... and many more SPI flash devices
Dominik Geyerb46acba2008-05-16 12:55:55 +000034 *
35 */
36
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000037#if defined(__i386__) || defined(__x86_64__)
38
Dominik Geyerb46acba2008-05-16 12:55:55 +000039#include <string.h>
Dominik Geyerb46acba2008-05-16 12:55:55 +000040#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000041#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000042#include "programmer.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000043#include "spi.h"
44
Stefan Reinauera9424d52008-06-27 16:28:34 +000045/* ICH9 controller register definition */
Stefan Taunerc0aaf952011-05-19 02:58:17 +000046#define ICH9_REG_FADDR 0x08 /* 32 Bits */
47#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
Stefan Reinauera9424d52008-06-27 16:28:34 +000048
Stefan Taunerc0aaf952011-05-19 02:58:17 +000049#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Stefan Tauner0c1ec452011-06-11 09:53:09 +000050#define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */
51#define SSFS_SCIP (0x1 << SSFS_SCIP_OFF)
52#define SSFS_FDONE_OFF 2 /* Cycle Done Status */
53#define SSFS_FDONE (0x1 << SSFS_FDONE_OFF)
54#define SSFS_FCERR_OFF 3 /* Flash Cycle Error */
55#define SSFS_FCERR (0x1 << SSFS_FCERR_OFF)
56#define SSFS_AEL_OFF 4 /* Access Error Log */
57#define SSFS_AEL (0x1 << SSFS_AEL_OFF)
Stefan Taunerc0aaf952011-05-19 02:58:17 +000058/* The following bits are reserved in SSFS: 1,5-7. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +000059#define SSFS_RESERVED_MASK 0x000000e2
Stefan Reinauera9424d52008-06-27 16:28:34 +000060
Stefan Taunerc0aaf952011-05-19 02:58:17 +000061#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Stefan Taunerc0aaf952011-05-19 02:58:17 +000062/* We combine SSFS and SSFC to one 32-bit word,
Stefan Tauner0c1ec452011-06-11 09:53:09 +000063 * therefore SSFC bits are off by 8. */
64 /* 0: reserved */
65#define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */
66#define SSFC_SCGO (0x1 << SSFC_SCGO_OFF)
67#define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */
68#define SSFC_ACS (0x1 << SSFC_ACS_OFF)
69#define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */
70#define SSFC_SPOP (0x1 << SSFC_SPOP_OFF)
71#define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */
72#define SSFC_COP (0x7 << SSFC_COP_OFF)
73 /* 7: reserved */
74#define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */
75#define SSFC_DBC (0x3f << SSFC_DBC_OFF)
76#define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */
77#define SSFC_DS (0x1 << SSFC_DS_OFF)
78#define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */
79#define SSFC_SME (0x1 << SSFC_SME_OFF)
80#define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */
81#define SSFC_SCF (0x7 << SSFC_SCF_OFF)
82#define SSFC_SCF_20MHZ 0x00000000
83#define SSFC_SCF_33MHZ 0x01000000
84 /* 19-23: reserved */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +000085#define SSFC_RESERVED_MASK 0xf8008100
Stefan Reinauera9424d52008-06-27 16:28:34 +000086
Stefan Taunerc0aaf952011-05-19 02:58:17 +000087#define ICH9_REG_PREOP 0x94 /* 16 Bits */
88#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
89#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +000090
91// ICH9R SPI commands
Stefan Taunerc0aaf952011-05-19 02:58:17 +000092#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
93#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
94#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
95#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
Dominik Geyerb46acba2008-05-16 12:55:55 +000096
Stefan Reinauera9424d52008-06-27 16:28:34 +000097// ICH7 registers
Stefan Taunerc0aaf952011-05-19 02:58:17 +000098#define ICH7_REG_SPIS 0x00 /* 16 Bits */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +000099#define SPIS_SCIP 0x0001
100#define SPIS_GRANT 0x0002
101#define SPIS_CDS 0x0004
102#define SPIS_FCERR 0x0008
103#define SPIS_RESERVED_MASK 0x7ff0
Stefan Reinauera9424d52008-06-27 16:28:34 +0000104
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000105/* VIA SPI is compatible with ICH7, but maxdata
106 to transfer is 16 bytes.
107
108 DATA byte count on ICH7 is 8:13, on VIA 8:11
109
110 bit 12 is port select CS0 CS1
111 bit 13 is FAST READ enable
112 bit 7 is used with fast read and one shot controls CS de-assert?
113*/
114
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000115#define ICH7_REG_SPIC 0x02 /* 16 Bits */
116#define SPIC_SCGO 0x0002
117#define SPIC_ACS 0x0004
118#define SPIC_SPOP 0x0008
119#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000120
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000121#define ICH7_REG_SPIA 0x04 /* 32 Bits */
122#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
123#define ICH7_REG_PREOP 0x54 /* 16 Bits */
124#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
125#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000126
FENG yu ningc05a2952008-12-08 18:16:58 +0000127/* ICH SPI configuration lock-down. May be set during chipset enabling. */
Michael Karchera4448d92010-07-22 18:04:15 +0000128static int ichspi_lock = 0;
FENG yu ningc05a2952008-12-08 18:16:58 +0000129
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000130uint32_t ichspi_bbar = 0;
131
Michael Karchera4448d92010-07-22 18:04:15 +0000132static void *ich_spibar = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000133
Dominik Geyerb46acba2008-05-16 12:55:55 +0000134typedef struct _OPCODE {
135 uint8_t opcode; //This commands spi opcode
136 uint8_t spi_type; //This commands spi type
137 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
138} OPCODE;
139
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000140/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000141 * Preop 1: Write Enable
142 * Preop 2: Write Status register enable
143 *
144 * OP 0: Write address
145 * OP 1: Read Address
146 * OP 2: ERASE block
147 * OP 3: Read Status register
148 * OP 4: Read ID
149 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000150 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000151 * OP 7: Chip erase
152 */
153typedef struct _OPCODES {
154 uint8_t preop[2];
155 OPCODE opcode[8];
156} OPCODES;
157
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000158static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000159
160/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000161static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000162{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000163 return mmio_readl(ich_spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000164}
165
Uwe Hermann09e04f72009-05-16 22:36:00 +0000166static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000167{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000168 return mmio_readw(ich_spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000169}
170
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000171static uint16_t REGREAD8(int X)
172{
173 return mmio_readb(ich_spibar + X);
174}
175
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000176#define REGWRITE32(off,val) mmio_writel(val, ich_spibar+off)
177#define REGWRITE16(off,val) mmio_writew(val, ich_spibar+off)
178#define REGWRITE8(off,val) mmio_writeb(val, ich_spibar+off)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000179
Dominik Geyerb46acba2008-05-16 12:55:55 +0000180/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000181static int find_opcode(OPCODES *op, uint8_t opcode);
182static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000183static int generate_opcodes(OPCODES * op);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000184static int program_opcodes(OPCODES *op, int enable_undo);
Stefan Reinauer43119562008-11-02 19:51:50 +0000185static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000186 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000187
FENG yu ningf041e9b2008-12-15 02:32:11 +0000188/* for pairing opcodes with their required preop */
189struct preop_opcode_pair {
190 uint8_t preop;
191 uint8_t opcode;
192};
193
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000194/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000195const struct preop_opcode_pair pops[] = {
FENG yu ningf041e9b2008-12-15 02:32:11 +0000196 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
197 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
198 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
199 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
200 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
201 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000202 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
203 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000204 {JEDEC_EWSR, JEDEC_WRSR},
205 {0,}
206};
207
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000208/* Reasonable default configuration. Needs ad-hoc modifications if we
209 * encounter unlisted opcodes. Fun.
210 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000211static OPCODES O_ST_M25P = {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000212 {
213 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000214 JEDEC_EWSR,
215 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000216 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000217 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000218 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000219 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000220 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000221 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000222 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000223 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000224 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
225 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000226};
227
Helge Wagner738e2522010-10-05 22:06:05 +0000228/* List of opcodes with their corresponding spi_type
229 * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
230 * is needed which is currently not in the chipset OPCODE table
231 */
232static OPCODE POSSIBLE_OPCODES[] = {
233 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
234 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
235 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
236 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
237 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
238 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
239 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
240 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
241 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
242 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
243 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
244};
245
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000246static OPCODES O_EXISTING = {};
FENG yu ningc05a2952008-12-08 18:16:58 +0000247
Helge Wagner738e2522010-10-05 22:06:05 +0000248static uint8_t lookup_spi_type(uint8_t opcode)
249{
250 int a;
251
252 for (a = 0; a < sizeof(POSSIBLE_OPCODES)/sizeof(POSSIBLE_OPCODES[0]); a++) {
253 if (POSSIBLE_OPCODES[a].opcode == opcode)
254 return POSSIBLE_OPCODES[a].spi_type;
255 }
256
257 return 0xFF;
258}
259
260static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
261{
262 uint8_t spi_type;
263
264 spi_type = lookup_spi_type(opcode);
265 if (spi_type > 3) {
266 /* Try to guess spi type from read/write sizes.
267 * The following valid writecnt/readcnt combinations exist:
268 * writecnt = 4, readcnt >= 0
269 * writecnt = 1, readcnt >= 0
270 * writecnt >= 4, readcnt = 0
271 * writecnt >= 1, readcnt = 0
272 * writecnt >= 1 is guaranteed for all commands.
273 */
274 if (readcnt == 0)
275 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
276 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
277 * bytes are actual the address, they go to the bus anyhow
278 */
279 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
280 else if (writecnt == 1) // and readcnt is > 0
281 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
282 else if (writecnt == 4) // and readcnt is > 0
283 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
284 // else we have an invalid case, will be handled below
285 }
286 if (spi_type <= 3) {
287 int oppos=2; // use original JEDEC_BE_D8 offset
288 curopcodes->opcode[oppos].opcode = opcode;
289 curopcodes->opcode[oppos].spi_type = spi_type;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000290 program_opcodes(curopcodes, 0);
Helge Wagner738e2522010-10-05 22:06:05 +0000291 oppos = find_opcode(curopcodes, opcode);
292 msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
293 return oppos;
294 }
295 return -1;
296}
297
Uwe Hermann09e04f72009-05-16 22:36:00 +0000298static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000299{
300 int a;
301
302 for (a = 0; a < 8; a++) {
303 if (op->opcode[a].opcode == opcode)
304 return a;
305 }
306
307 return -1;
308}
309
Uwe Hermann09e04f72009-05-16 22:36:00 +0000310static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000311{
312 int a;
313
314 for (a = 0; a < 2; a++) {
315 if (op->preop[a] == preop)
316 return a;
317 }
318
319 return -1;
320}
321
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000322/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000323static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000324{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000325 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000326 uint16_t preop, optype;
327 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000328
329 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000330 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000331 return -1;
332 }
333
Michael Karcherb9dbe482011-05-11 17:07:07 +0000334 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000335 case SPI_CONTROLLER_ICH7:
336 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000337 preop = REGREAD16(ICH7_REG_PREOP);
338 optype = REGREAD16(ICH7_REG_OPTYPE);
339 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
340 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
341 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000342 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000343 preop = REGREAD16(ICH9_REG_PREOP);
344 optype = REGREAD16(ICH9_REG_OPTYPE);
345 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
346 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
347 break;
348 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000349 msg_perr("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000350 return -1;
351 }
352
353 op->preop[0] = (uint8_t) preop;
354 op->preop[1] = (uint8_t) (preop >> 8);
355
356 for (a = 0; a < 8; a++) {
357 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
358 optype >>= 2;
359 }
360
361 for (a = 0; a < 4; a++) {
362 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
363 opmenu[0] >>= 8;
364 }
365
366 for (a = 4; a < 8; a++) {
367 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
368 opmenu[1] >>= 8;
369 }
370
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000371 /* No preopcodes used by default. */
372 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000373 op->opcode[a].atomic = 0;
374
FENG yu ningc05a2952008-12-08 18:16:58 +0000375 return 0;
376}
377
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000378static int program_opcodes(OPCODES *op, int enable_undo)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000379{
380 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000381 uint16_t preop, optype;
382 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000383
384 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000385 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000386 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000387 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000388 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000389
Stefan Reinauera9424d52008-06-27 16:28:34 +0000390 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000391 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000392 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000393 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000394 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000395
Stefan Reinauera9424d52008-06-27 16:28:34 +0000396 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000397 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000398 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000399 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000400 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000401
Dominik Geyerb46acba2008-05-16 12:55:55 +0000402 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000403 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000404 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000405 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000406 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000407
Sean Nelson316a29f2010-05-07 20:09:04 +0000408 msg_pdbg("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000409 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000410 case SPI_CONTROLLER_ICH7:
411 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000412 /* Register undo only for enable_undo=1, i.e. first call. */
413 if (enable_undo) {
414 rmmio_valw(ich_spibar + ICH7_REG_PREOP);
415 rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
416 rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
417 rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
418 }
419 mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
420 mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
421 mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
422 mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000423 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000424 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000425 /* Register undo only for enable_undo=1, i.e. first call. */
426 if (enable_undo) {
427 rmmio_valw(ich_spibar + ICH9_REG_PREOP);
428 rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
429 rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
430 rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
431 }
432 mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
433 mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
434 mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
435 mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000436 break;
437 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000438 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000439 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000440 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000441
442 return 0;
443}
444
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000445/*
446 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
447 * it didn't stick.
448 */
449void ich_set_bbar(uint32_t minaddr)
450{
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000451#define BBAR_MASK 0x00ffff00
452 minaddr &= BBAR_MASK;
Michael Karcherb9dbe482011-05-11 17:07:07 +0000453 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000454 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000455 case SPI_CONTROLLER_VIA:
456 ichspi_bbar = mmio_readl(ich_spibar + 0x50) & ~BBAR_MASK;
457 if (ichspi_bbar)
458 msg_pdbg("Reserved bits in BBAR not zero: 0x%04x",
459 ichspi_bbar);
460 ichspi_bbar |= minaddr;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000461 rmmio_writel(ichspi_bbar, ich_spibar + 0x50);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000462 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000463 /* We don't have any option except complaining. And if the write
464 * failed, the restore will fail as well, so no problem there.
465 */
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000466 if (ichspi_bbar != minaddr)
467 msg_perr("Setting BBAR failed!\n");
468 break;
469 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000470 ichspi_bbar = mmio_readl(ich_spibar + 0xA0) & ~BBAR_MASK;
471 if (ichspi_bbar)
472 msg_pdbg("Reserved bits in BBAR not zero: 0x%04x",
473 ichspi_bbar);
474 ichspi_bbar |= minaddr;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000475 rmmio_writel(ichspi_bbar, ich_spibar + 0xA0);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000476 ichspi_bbar = mmio_readl(ich_spibar + 0xA0);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000477 /* We don't have any option except complaining. And if the write
478 * failed, the restore will fail as well, so no problem there.
479 */
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000480 if (ichspi_bbar != minaddr)
481 msg_perr("Setting BBAR failed!\n");
482 break;
483 default:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000484 msg_perr("Unknown chipset for BBAR setting!\n");
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000485 break;
486 }
487}
488
FENG yu ningf041e9b2008-12-15 02:32:11 +0000489/* This function generates OPCODES from or programs OPCODES to ICH according to
490 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000491 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000492 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000493 */
Michael Karchera4448d92010-07-22 18:04:15 +0000494static int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000495{
496 int rc = 0;
497 OPCODES *curopcodes_done;
498
499 if (curopcodes)
500 return 0;
501
502 if (ichspi_lock) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000503 msg_pdbg("Reading OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000504 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000505 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000506 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000507 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000508 curopcodes_done = &O_ST_M25P;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000509 rc = program_opcodes(curopcodes_done, 1);
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000510 /* Technically not part of opcode init, but it allows opcodes
511 * to run without transaction errors by setting the lowest
512 * allowed address to zero.
513 */
514 ich_set_bbar(0);
FENG yu ningc05a2952008-12-08 18:16:58 +0000515 }
516
517 if (rc) {
518 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000519 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000520 return 1;
521 } else {
522 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000523 msg_pdbg("done\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000524 return 0;
525 }
526}
527
Stefan Reinauer43119562008-11-02 19:51:50 +0000528static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000529 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000530{
531 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000532 int timeout;
Peter Stuge7e2c0792008-06-29 01:30:41 +0000533 uint32_t temp32 = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000534 uint16_t temp16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000535 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000536 uint64_t opmenu;
537 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000538
539 /* Is it a write command? */
540 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
541 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
542 write_cmd = 1;
543 }
544
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000545 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
546 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
547 programmer_delay(10);
548 }
549 if (!timeout) {
550 msg_perr("Error: SCIP never cleared!\n");
551 return 1;
552 }
553
Dominik Geyerb46acba2008-05-16 12:55:55 +0000554 /* Programm Offset in Flash into FADDR */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000555 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000556
557 /* Program data into FDATA0 to N */
558 if (write_cmd && (datalength != 0)) {
559 temp32 = 0;
560 for (a = 0; a < datalength; a++) {
561 if ((a % 4) == 0) {
562 temp32 = 0;
563 }
564
565 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
566
567 if ((a % 4) == 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000568 REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)),
569 temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000570 }
571 }
572 if (((a - 1) % 4) != 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000573 REGWRITE32(ICH7_REG_SPID0 +
574 ((a - 1) - ((a - 1) % 4)), temp32);
575 }
576
577 }
578
579 /* Assemble SPIS */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000580 temp16 = REGREAD16(ICH7_REG_SPIS);
581 /* keep reserved bits */
582 temp16 &= SPIS_RESERVED_MASK;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000583 /* clear error status registers */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000584 temp16 |= (SPIS_CDS | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000585 REGWRITE16(ICH7_REG_SPIS, temp16);
586
587 /* Assemble SPIC */
588 temp16 = 0;
589
590 if (datalength != 0) {
591 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000592 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000593 }
594
595 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000596 opmenu = REGREAD32(ICH7_REG_OPMENU);
597 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
598
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000599 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
600 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000601 break;
602 }
603 opmenu >>= 8;
604 }
605 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000606 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000607 return 1;
608 }
609 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000610
Michael Karcher136125a2011-04-29 22:11:36 +0000611 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
612 /* Handle Atomic. Atomic commands include three steps:
613 - sending the preop (mainly EWSR or WREN)
614 - sending the main command
615 - waiting for the busy bit (WIP) to be cleared
616 This means the timeout must be sufficient for chip erase
617 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000618 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000619 switch (op.atomic) {
620 case 2:
621 /* Select second preop. */
622 temp16 |= SPIC_SPOP;
623 /* And fall through. */
624 case 1:
625 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000626 temp16 |= SPIC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000627 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000628 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000629 }
630
631 /* Start */
632 temp16 |= SPIC_SCGO;
633
634 /* write it */
635 REGWRITE16(ICH7_REG_SPIC, temp16);
636
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000637 /* Wait for Cycle Done Status or Flash Cycle Error. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000638 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
639 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000640 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000641 }
642 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000643 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
644 REGREAD16(ICH7_REG_SPIS));
645 return 1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000646 }
647
Sean Nelson316a29f2010-05-07 20:09:04 +0000648 /* FIXME: make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000649 temp16 = REGREAD16(ICH7_REG_SPIS);
650 if (temp16 & SPIS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000651 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000652 /* keep reserved bits */
653 temp16 &= SPIS_RESERVED_MASK;
654 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000655 return 1;
656 }
657
658 if ((!write_cmd) && (datalength != 0)) {
659 for (a = 0; a < datalength; a++) {
660 if ((a % 4) == 0) {
661 temp32 = REGREAD32(ICH7_REG_SPID0 + (a));
662 }
663
664 data[a] =
665 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
666 >> ((a % 4) * 8);
667 }
668 }
669
670 return 0;
671}
672
Stefan Reinauer43119562008-11-02 19:51:50 +0000673static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000674 uint8_t datalength, uint8_t * data)
675{
676 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000677 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000678 uint32_t temp32;
679 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000680 uint64_t opmenu;
681 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000682
683 /* Is it a write command? */
684 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
685 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
686 write_cmd = 1;
687 }
688
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000689 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
690 while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
691 programmer_delay(10);
692 }
693 if (!timeout) {
694 msg_perr("Error: SCIP never cleared!\n");
695 return 1;
696 }
697
Stefan Reinauera9424d52008-06-27 16:28:34 +0000698 /* Programm Offset in Flash into FADDR */
699 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
700
701 /* Program data into FDATA0 to N */
702 if (write_cmd && (datalength != 0)) {
703 temp32 = 0;
704 for (a = 0; a < datalength; a++) {
705 if ((a % 4) == 0) {
706 temp32 = 0;
707 }
708
709 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
710
711 if ((a % 4) == 3) {
712 REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
713 temp32);
714 }
715 }
716 if (((a - 1) % 4) != 3) {
717 REGWRITE32(ICH9_REG_FDATA0 +
718 ((a - 1) - ((a - 1) % 4)), temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000719 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000720 }
721
722 /* Assemble SSFS + SSFC */
Helge Wagnera319be12010-08-11 21:06:10 +0000723 temp32 = REGREAD32(ICH9_REG_SSFS);
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000724 /* Keep reserved bits only */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000725 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000726 /* Clear cycle done and cycle error status registers */
727 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000728 REGWRITE32(ICH9_REG_SSFS, temp32);
729
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000730 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000731 temp32 |= SSFC_SCF_20MHZ;
732
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000733 /* Set data byte count (DBC) and data cycle bit (DS) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000734 if (datalength != 0) {
735 uint32_t datatemp;
736 temp32 |= SSFC_DS;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000737 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
738 SSFC_DBC);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000739 temp32 |= datatemp;
740 }
741
742 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000743 opmenu = REGREAD32(ICH9_REG_OPMENU);
744 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
745
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000746 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
747 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000748 break;
749 }
750 opmenu >>= 8;
751 }
752 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000753 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000754 return 1;
755 }
756 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000757
Michael Karcher136125a2011-04-29 22:11:36 +0000758 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
759 /* Handle Atomic. Atomic commands include three steps:
760 - sending the preop (mainly EWSR or WREN)
761 - sending the main command
762 - waiting for the busy bit (WIP) to be cleared
763 This means the timeout must be sufficient for chip erase
764 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000765 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000766 switch (op.atomic) {
767 case 2:
768 /* Select second preop. */
769 temp32 |= SSFC_SPOP;
770 /* And fall through. */
771 case 1:
772 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000773 temp32 |= SSFC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000774 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000775 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000776 }
777
778 /* Start */
779 temp32 |= SSFC_SCGO;
780
781 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000782 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000783
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000784 /* Wait for Cycle Done Status or Flash Cycle Error. */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000785 while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000786 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000787 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000788 }
789 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000790 msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
791 REGREAD32(ICH9_REG_SSFS));
792 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000793 }
794
Sean Nelson316a29f2010-05-07 20:09:04 +0000795 /* FIXME make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000796 temp32 = REGREAD32(ICH9_REG_SSFS);
797 if (temp32 & SSFS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000798 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000799 /* keep reserved bits */
800 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
801 /* Clear the transaction error. */
802 REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000803 return 1;
804 }
805
806 if ((!write_cmd) && (datalength != 0)) {
807 for (a = 0; a < datalength; a++) {
808 if ((a % 4) == 0) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000809 temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
Dominik Geyerb46acba2008-05-16 12:55:55 +0000810 }
811
812 data[a] =
Stefan Reinauera9424d52008-06-27 16:28:34 +0000813 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
814 >> ((a % 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000815 }
816 }
817
818 return 0;
819}
820
Stefan Reinauer43119562008-11-02 19:51:50 +0000821static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000822 uint8_t datalength, uint8_t * data)
823{
Michael Karcherb9dbe482011-05-11 17:07:07 +0000824 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000825 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000826 if (datalength > 16) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000827 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000828 "opcode 0x%02x, got datalength=%i, want <=16\n",
829 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000830 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000831 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000832 return ich7_run_opcode(op, offset, datalength, data, 16);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000833 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000834 if (datalength > 64) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000835 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000836 "opcode 0x%02x, got datalength=%i, want <=16\n",
837 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000838 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000839 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000840 return ich7_run_opcode(op, offset, datalength, data, 64);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000841 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000842 if (datalength > 64) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000843 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000844 "opcode 0x%02x, got datalength=%i, want <=16\n",
845 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000846 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000847 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000848 return ich9_run_opcode(op, offset, datalength, data);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000849 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000850 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000851 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000852
853 /* If we ever get here, something really weird happened */
854 return -1;
855}
856
Michael Karcherb9dbe482011-05-11 17:07:07 +0000857static int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000858 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000859{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000860 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000861 int opcode_index = -1;
862 const unsigned char cmd = *writearr;
863 OPCODE *opcode;
864 uint32_t addr = 0;
865 uint8_t *data;
866 int count;
867
Dominik Geyerb46acba2008-05-16 12:55:55 +0000868 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000869 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000870 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +0000871 if (!ichspi_lock)
872 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
873 if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000874 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
875 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +0000876 return SPI_INVALID_OPCODE;
877 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000878 }
879
880 opcode = &(curopcodes->opcode[opcode_index]);
881
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000882 /* The following valid writecnt/readcnt combinations exist:
883 * writecnt = 4, readcnt >= 0
884 * writecnt = 1, readcnt >= 0
885 * writecnt >= 4, readcnt = 0
886 * writecnt >= 1, readcnt = 0
887 * writecnt >= 1 is guaranteed for all commands.
888 */
889 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
890 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000891 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000892 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
893 writecnt);
894 return SPI_INVALID_LENGTH;
895 }
896 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
897 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000898 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000899 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
900 writecnt);
901 return SPI_INVALID_LENGTH;
902 }
903 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
904 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000905 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000906 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
907 writecnt);
908 return SPI_INVALID_LENGTH;
909 }
910 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
911 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
912 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000913 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000914 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
915 readcnt);
916 return SPI_INVALID_LENGTH;
917 }
918
Dominik Geyerb46acba2008-05-16 12:55:55 +0000919 /* if opcode-type requires an address */
920 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
921 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000922 addr = (writearr[1] << 16) |
923 (writearr[2] << 8) | (writearr[3] << 0);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000924 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000925 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000926 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000927 case SPI_CONTROLLER_ICH9:
928 if (addr < ichspi_bbar) {
929 msg_perr("%s: Address 0x%06x below allowed "
930 "range 0x%06x-0xffffff\n", __func__,
931 addr, ichspi_bbar);
932 return SPI_INVALID_ADDRESS;
933 }
934 break;
935 default:
936 break;
937 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000938 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000939
Dominik Geyerb46acba2008-05-16 12:55:55 +0000940 /* translate read/write array/count */
941 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000942 data = (uint8_t *) (writearr + 1);
943 count = writecnt - 1;
944 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
945 data = (uint8_t *) (writearr + 4);
946 count = writecnt - 4;
947 } else {
948 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000949 count = readcnt;
950 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000951
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000952 result = run_opcode(*opcode, addr, count, data);
953 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000954 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
955 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
956 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
957 msg_pdbg("at address 0x%06x ", addr);
958 }
959 msg_pdbg("(payload length was %d).\n", count);
960
961 /* Print out the data array if it contains data to write.
962 * Errors are detected before the received data is read back into
963 * the array so it won't make sense to print it then. */
964 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
965 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
966 int i;
967 msg_pspew("The data was:\n");
968 for(i=0; i<count; i++){
969 msg_pspew("%3d: 0x%02x\n", i, data[i]);
970 }
971 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000972 }
973
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000974 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000975}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000976
Michael Karcherb9dbe482011-05-11 17:07:07 +0000977static int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000978{
979 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000980 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000981 int oppos, preoppos;
982 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000983 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000984 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000985 preoppos = find_preop(curopcodes, cmds->writearr[0]);
986 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000987 if ((oppos == -1) && (preoppos != -1)) {
988 /* Current command is listed as preopcode in
989 * ICH struct OPCODES, but next command is not
990 * listed as opcode in that struct.
991 * Check for command sanity, then
992 * try to reprogram the ICH opcode list.
993 */
994 if (find_preop(curopcodes,
995 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000996 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000997 "preopcodes 0x%02x and 0x%02x, "
998 "ignoring the first.\n",
999 __func__, cmds->writearr[0],
1000 (cmds + 1)->writearr[0]);
1001 continue;
1002 }
1003 /* If the chipset is locked down, we'll fail
1004 * during execution of the next command anyway.
1005 * No need to bother with fixups.
1006 */
1007 if (!ichspi_lock) {
Helge Wagner738e2522010-10-05 22:06:05 +00001008 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1009 if (oppos == -1)
1010 continue;
1011 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001012 continue;
1013 }
1014 }
1015 if ((oppos != -1) && (preoppos != -1)) {
1016 /* Current command is listed as preopcode in
1017 * ICH struct OPCODES and next command is listed
1018 * as opcode in that struct. Match them up.
1019 */
1020 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001021 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001022 }
1023 /* If none of the above if-statements about oppos or
1024 * preoppos matched, this is a normal opcode.
1025 */
1026 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001027 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
1028 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001029 /* Reset the type of all opcodes to non-atomic. */
1030 for (i = 0; i < 8; i++)
1031 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001032 }
1033 return ret;
1034}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001035
Michael Karchera4448d92010-07-22 18:04:15 +00001036#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1037#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1038#define ICH_BRWA(x) ((x >> 8) & 0xff)
1039#define ICH_BRRA(x) ((x >> 0) & 0xff)
1040
1041#define ICH_FREG_BASE(x) ((x >> 0) & 0x1fff)
1042#define ICH_FREG_LIMIT(x) ((x >> 16) & 0x1fff)
1043
1044static void do_ich9_spi_frap(uint32_t frap, int i)
1045{
Mathias Krausea60faab2011-01-17 07:50:42 +00001046 static const char *const access_names[4] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001047 "locked", "read-only", "write-only", "read-write"
1048 };
Mathias Krausea60faab2011-01-17 07:50:42 +00001049 static const char *const region_names[5] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001050 "Flash Descriptor", "BIOS", "Management Engine",
1051 "Gigabit Ethernet", "Platform Data"
1052 };
1053 uint32_t base, limit;
1054 int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1055 (((ICH_BRRA(frap) >> i) & 1) << 0);
1056 int offset = 0x54 + i * 4;
1057 uint32_t freg = mmio_readl(ich_spibar + offset);
1058
1059 msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n",
1060 offset, freg, i, region_names[i]);
1061
1062 base = ICH_FREG_BASE(freg);
1063 limit = ICH_FREG_LIMIT(freg);
Joshua Roysd172ecd2011-05-26 13:30:51 +00001064 if (base > limit) {
Michael Karchera4448d92010-07-22 18:04:15 +00001065 /* this FREG is disabled */
1066 msg_pdbg("%s region is unused.\n", region_names[i]);
1067 return;
1068 }
1069
1070 msg_pdbg("0x%08x-0x%08x is %s\n",
1071 (base << 12), (limit << 12) | 0x0fff,
1072 access_names[rwperms]);
1073}
1074
Michael Karcherb9dbe482011-05-11 17:07:07 +00001075static const struct spi_programmer spi_programmer_ich7 = {
1076 .type = SPI_CONTROLLER_ICH7,
1077 .max_data_read = 64,
1078 .max_data_write = 64,
1079 .command = ich_spi_send_command,
1080 .multicommand = ich_spi_send_multicommand,
1081 .read = default_spi_read,
1082 .write_256 = default_spi_write_256,
1083};
1084
1085static const struct spi_programmer spi_programmer_ich9 = {
1086 .type = SPI_CONTROLLER_ICH9,
1087 .max_data_read = 64,
1088 .max_data_write = 64,
1089 .command = ich_spi_send_command,
1090 .multicommand = ich_spi_send_multicommand,
1091 .read = default_spi_read,
1092 .write_256 = default_spi_write_256,
1093};
1094
Michael Karchera4448d92010-07-22 18:04:15 +00001095int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb,
1096 int ich_generation)
1097{
1098 int i;
1099 uint8_t old, new;
1100 uint16_t spibar_offset, tmp2;
1101 uint32_t tmp;
1102
Michael Karchera4448d92010-07-22 18:04:15 +00001103 switch (ich_generation) {
1104 case 7:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001105 register_spi_programmer(&spi_programmer_ich7);
Michael Karchera4448d92010-07-22 18:04:15 +00001106 spibar_offset = 0x3020;
1107 break;
1108 case 8:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001109 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001110 spibar_offset = 0x3020;
1111 break;
1112 case 9:
1113 case 10:
1114 default: /* Future version might behave the same */
Michael Karcherb9dbe482011-05-11 17:07:07 +00001115 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001116 spibar_offset = 0x3800;
1117 break;
1118 }
1119
1120 /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
1121 msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset);
1122
1123 /* Assign Virtual Address */
1124 ich_spibar = rcrb + spibar_offset;
1125
Michael Karcherb9dbe482011-05-11 17:07:07 +00001126 switch (spi_programmer->type) {
Michael Karchera4448d92010-07-22 18:04:15 +00001127 case SPI_CONTROLLER_ICH7:
1128 msg_pdbg("0x00: 0x%04x (SPIS)\n",
1129 mmio_readw(ich_spibar + 0));
1130 msg_pdbg("0x02: 0x%04x (SPIC)\n",
1131 mmio_readw(ich_spibar + 2));
1132 msg_pdbg("0x04: 0x%08x (SPIA)\n",
1133 mmio_readl(ich_spibar + 4));
1134 for (i = 0; i < 8; i++) {
1135 int offs;
1136 offs = 8 + (i * 8);
1137 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1138 mmio_readl(ich_spibar + offs), i);
1139 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1140 mmio_readl(ich_spibar + offs + 4), i);
1141 }
1142 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1143 msg_pdbg("0x50: 0x%08x (BBAR)\n",
1144 ichspi_bbar);
1145 msg_pdbg("0x54: 0x%04x (PREOP)\n",
1146 mmio_readw(ich_spibar + 0x54));
1147 msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
1148 mmio_readw(ich_spibar + 0x56));
1149 msg_pdbg("0x58: 0x%08x (OPMENU)\n",
1150 mmio_readl(ich_spibar + 0x58));
1151 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
1152 mmio_readl(ich_spibar + 0x5c));
1153 for (i = 0; i < 4; i++) {
1154 int offs;
1155 offs = 0x60 + (i * 4);
1156 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1157 mmio_readl(ich_spibar + offs), i);
1158 }
Michael Karchera4448d92010-07-22 18:04:15 +00001159 if (mmio_readw(ich_spibar) & (1 << 15)) {
1160 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1161 ichspi_lock = 1;
1162 }
1163 ich_init_opcodes();
1164 break;
1165 case SPI_CONTROLLER_ICH9:
1166 tmp2 = mmio_readw(ich_spibar + 4);
1167 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
1168 msg_pdbg("FLOCKDN %i, ", (tmp2 >> 15 & 1));
1169 msg_pdbg("FDV %i, ", (tmp2 >> 14) & 1);
1170 msg_pdbg("FDOPSS %i, ", (tmp2 >> 13) & 1);
1171 msg_pdbg("SCIP %i, ", (tmp2 >> 5) & 1);
1172 msg_pdbg("BERASE %i, ", (tmp2 >> 3) & 3);
1173 msg_pdbg("AEL %i, ", (tmp2 >> 2) & 1);
1174 msg_pdbg("FCERR %i, ", (tmp2 >> 1) & 1);
1175 msg_pdbg("FDONE %i\n", (tmp2 >> 0) & 1);
1176
1177 tmp = mmio_readl(ich_spibar + 0x50);
1178 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1179 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1180 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1181 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1182 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1183
1184 /* print out the FREGx registers along with FRAP access bits */
1185 for(i = 0; i < 5; i++)
1186 do_ich9_spi_frap(tmp, i);
1187
1188 msg_pdbg("0x74: 0x%08x (PR0)\n",
1189 mmio_readl(ich_spibar + 0x74));
1190 msg_pdbg("0x78: 0x%08x (PR1)\n",
1191 mmio_readl(ich_spibar + 0x78));
1192 msg_pdbg("0x7C: 0x%08x (PR2)\n",
1193 mmio_readl(ich_spibar + 0x7C));
1194 msg_pdbg("0x80: 0x%08x (PR3)\n",
1195 mmio_readl(ich_spibar + 0x80));
1196 msg_pdbg("0x84: 0x%08x (PR4)\n",
1197 mmio_readl(ich_spibar + 0x84));
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001198
1199 tmp = mmio_readl(ich_spibar + 0x90);
1200 msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
1201 msg_pdbg("AEL %i, ", (tmp >> 4) & 1);
1202 msg_pdbg("FCERR %i, ", (tmp >> 3) & 1);
1203 msg_pdbg("FDONE %i, ", (tmp >> 2) & 1);
1204 msg_pdbg("SCIP %i\n", (tmp >> 0) & 1);
1205 if (tmp & (1 << 3)) {
1206 msg_pdbg("Clearing SSFS.FCERR\n");
1207 mmio_writeb(1 << 3, ich_spibar + 0x90);
1208 }
1209 tmp >>= 8;
1210 msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp);
1211
Michael Karchera4448d92010-07-22 18:04:15 +00001212 msg_pdbg("0x94: 0x%04x (PREOP)\n",
1213 mmio_readw(ich_spibar + 0x94));
1214 msg_pdbg("0x96: 0x%04x (OPTYPE)\n",
1215 mmio_readw(ich_spibar + 0x96));
1216 msg_pdbg("0x98: 0x%08x (OPMENU)\n",
1217 mmio_readl(ich_spibar + 0x98));
1218 msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
1219 mmio_readl(ich_spibar + 0x9C));
1220 ichspi_bbar = mmio_readl(ich_spibar + 0xA0);
1221 msg_pdbg("0xA0: 0x%08x (BBAR)\n",
1222 ichspi_bbar);
1223 msg_pdbg("0xB0: 0x%08x (FDOC)\n",
1224 mmio_readl(ich_spibar + 0xB0));
1225 if (tmp2 & (1 << 15)) {
1226 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1227 ichspi_lock = 1;
1228 }
1229 ich_init_opcodes();
1230 break;
1231 default:
1232 /* Nothing */
1233 break;
1234 }
1235
1236 old = pci_read_byte(dev, 0xdc);
1237 msg_pdbg("SPI Read Configuration: ");
1238 new = (old >> 2) & 0x3;
1239 switch (new) {
1240 case 0:
1241 case 1:
1242 case 2:
1243 msg_pdbg("prefetching %sabled, caching %sabled, ",
1244 (new & 0x2) ? "en" : "dis",
1245 (new & 0x1) ? "dis" : "en");
1246 break;
1247 default:
1248 msg_pdbg("invalid prefetching/caching settings, ");
1249 break;
1250 }
1251 return 0;
1252}
1253
Michael Karcherb9dbe482011-05-11 17:07:07 +00001254static const struct spi_programmer spi_programmer_via = {
1255 .type = SPI_CONTROLLER_VIA,
1256 .max_data_read = 16,
1257 .max_data_write = 16,
1258 .command = ich_spi_send_command,
1259 .multicommand = ich_spi_send_multicommand,
1260 .read = default_spi_read,
1261 .write_256 = default_spi_write_256,
1262};
1263
Michael Karchera4448d92010-07-22 18:04:15 +00001264int via_init_spi(struct pci_dev *dev)
1265{
1266 uint32_t mmio_base;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001267 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00001268
1269 mmio_base = (pci_read_long(dev, 0xbc)) << 8;
1270 msg_pdbg("MMIO base at = 0x%x\n", mmio_base);
1271 ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
1272
Michael Karchera4448d92010-07-22 18:04:15 +00001273 /* Not sure if it speaks all these bus protocols. */
Michael Karcherb9dbe482011-05-11 17:07:07 +00001274 buses_supported = CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH;
1275 register_spi_programmer(&spi_programmer_via);
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001276
1277 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1278 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1279 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1280 for (i = 0; i < 2; i++) {
1281 int offs;
1282 offs = 8 + (i * 8);
1283 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1284 mmio_readl(ich_spibar + offs), i);
1285 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1286 mmio_readl(ich_spibar + offs + 4), i);
1287 }
1288 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1289 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1290 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1291 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1292 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1293 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1294 for (i = 0; i < 3; i++) {
1295 int offs;
1296 offs = 0x60 + (i * 4);
1297 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1298 mmio_readl(ich_spibar + offs), i);
1299 }
1300 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
1301 mmio_readw(ich_spibar + 0x6c));
1302 if (mmio_readw(ich_spibar) & (1 << 15)) {
1303 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1304 ichspi_lock = 1;
1305 }
1306
Michael Karchera4448d92010-07-22 18:04:15 +00001307 ich_init_opcodes();
1308
1309 return 0;
1310}
1311
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001312#endif