blob: caa14eaac386e29bc667a11b7e098ccd8e114ca0 [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
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000248/* pretty printing functions */
249static void pretty_print_opcodes(OPCODES *ops)
250{
251 if(ops == NULL)
252 return;
253
254 msg_pdbg("preop0=0x%02x, preop1=0x%02x\n", ops->preop[0],
255 ops->preop[1]);
256
257 OPCODE oc;
258 uint8_t i;
259 for (i = 0; i < 8; i++) {
260 oc = ops->opcode[i];
261 msg_pdbg("op[%d]=0x%02x, %d, %d\n",
262 i,
263 oc.opcode,
264 oc.spi_type,
265 oc.atomic);
266 }
267}
268
269#define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF)
270
271static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
272{
273 msg_pdbg("SSFS: ");
274 pprint_reg(SSFS, SCIP, reg_val, ", ");
275 pprint_reg(SSFS, FDONE, reg_val, ", ");
276 pprint_reg(SSFS, FCERR, reg_val, ", ");
277 pprint_reg(SSFS, AEL, reg_val, "\n");
278}
279
280static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
281{
282 msg_pdbg("SSFC: ");
283 pprint_reg(SSFC, SCGO, reg_val, ", ");
284 pprint_reg(SSFC, ACS, reg_val, ", ");
285 pprint_reg(SSFC, SPOP, reg_val, ", ");
286 pprint_reg(SSFC, COP, reg_val, ", ");
287 pprint_reg(SSFC, DBC, reg_val, ", ");
288 pprint_reg(SSFC, SME, reg_val, ", ");
289 pprint_reg(SSFC, SCF, reg_val, "\n");
290}
291
Helge Wagner738e2522010-10-05 22:06:05 +0000292static uint8_t lookup_spi_type(uint8_t opcode)
293{
294 int a;
295
296 for (a = 0; a < sizeof(POSSIBLE_OPCODES)/sizeof(POSSIBLE_OPCODES[0]); a++) {
297 if (POSSIBLE_OPCODES[a].opcode == opcode)
298 return POSSIBLE_OPCODES[a].spi_type;
299 }
300
301 return 0xFF;
302}
303
304static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
305{
306 uint8_t spi_type;
307
308 spi_type = lookup_spi_type(opcode);
309 if (spi_type > 3) {
310 /* Try to guess spi type from read/write sizes.
311 * The following valid writecnt/readcnt combinations exist:
312 * writecnt = 4, readcnt >= 0
313 * writecnt = 1, readcnt >= 0
314 * writecnt >= 4, readcnt = 0
315 * writecnt >= 1, readcnt = 0
316 * writecnt >= 1 is guaranteed for all commands.
317 */
318 if (readcnt == 0)
319 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
320 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
321 * bytes are actual the address, they go to the bus anyhow
322 */
323 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
324 else if (writecnt == 1) // and readcnt is > 0
325 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
326 else if (writecnt == 4) // and readcnt is > 0
327 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
328 // else we have an invalid case, will be handled below
329 }
330 if (spi_type <= 3) {
331 int oppos=2; // use original JEDEC_BE_D8 offset
332 curopcodes->opcode[oppos].opcode = opcode;
333 curopcodes->opcode[oppos].spi_type = spi_type;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000334 program_opcodes(curopcodes, 0);
Helge Wagner738e2522010-10-05 22:06:05 +0000335 oppos = find_opcode(curopcodes, opcode);
336 msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
337 return oppos;
338 }
339 return -1;
340}
341
Uwe Hermann09e04f72009-05-16 22:36:00 +0000342static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000343{
344 int a;
345
346 for (a = 0; a < 8; a++) {
347 if (op->opcode[a].opcode == opcode)
348 return a;
349 }
350
351 return -1;
352}
353
Uwe Hermann09e04f72009-05-16 22:36:00 +0000354static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000355{
356 int a;
357
358 for (a = 0; a < 2; a++) {
359 if (op->preop[a] == preop)
360 return a;
361 }
362
363 return -1;
364}
365
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000366/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000367static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000368{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000369 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000370 uint16_t preop, optype;
371 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000372
373 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000374 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000375 return -1;
376 }
377
Michael Karcherb9dbe482011-05-11 17:07:07 +0000378 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000379 case SPI_CONTROLLER_ICH7:
380 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000381 preop = REGREAD16(ICH7_REG_PREOP);
382 optype = REGREAD16(ICH7_REG_OPTYPE);
383 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
384 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
385 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000386 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000387 preop = REGREAD16(ICH9_REG_PREOP);
388 optype = REGREAD16(ICH9_REG_OPTYPE);
389 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
390 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
391 break;
392 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000393 msg_perr("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000394 return -1;
395 }
396
397 op->preop[0] = (uint8_t) preop;
398 op->preop[1] = (uint8_t) (preop >> 8);
399
400 for (a = 0; a < 8; a++) {
401 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
402 optype >>= 2;
403 }
404
405 for (a = 0; a < 4; a++) {
406 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
407 opmenu[0] >>= 8;
408 }
409
410 for (a = 4; a < 8; a++) {
411 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
412 opmenu[1] >>= 8;
413 }
414
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000415 /* No preopcodes used by default. */
416 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000417 op->opcode[a].atomic = 0;
418
FENG yu ningc05a2952008-12-08 18:16:58 +0000419 return 0;
420}
421
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000422static int program_opcodes(OPCODES *op, int enable_undo)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000423{
424 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000425 uint16_t preop, optype;
426 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000427
428 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000429 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000430 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000431 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000432 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000433
Stefan Reinauera9424d52008-06-27 16:28:34 +0000434 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000435 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000436 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000437 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000438 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000439
Stefan Reinauera9424d52008-06-27 16:28:34 +0000440 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000441 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000442 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000443 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000444 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000445
Dominik Geyerb46acba2008-05-16 12:55:55 +0000446 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000447 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000448 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000449 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000450 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000451
Sean Nelson316a29f2010-05-07 20:09:04 +0000452 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 +0000453 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000454 case SPI_CONTROLLER_ICH7:
455 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000456 /* Register undo only for enable_undo=1, i.e. first call. */
457 if (enable_undo) {
458 rmmio_valw(ich_spibar + ICH7_REG_PREOP);
459 rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
460 rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
461 rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
462 }
463 mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
464 mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
465 mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
466 mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000467 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000468 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000469 /* Register undo only for enable_undo=1, i.e. first call. */
470 if (enable_undo) {
471 rmmio_valw(ich_spibar + ICH9_REG_PREOP);
472 rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
473 rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
474 rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
475 }
476 mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
477 mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
478 mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
479 mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000480 break;
481 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000482 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000483 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000484 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000485
486 return 0;
487}
488
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000489/*
490 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
491 * it didn't stick.
492 */
493void ich_set_bbar(uint32_t minaddr)
494{
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000495#define BBAR_MASK 0x00ffff00
496 minaddr &= BBAR_MASK;
Michael Karcherb9dbe482011-05-11 17:07:07 +0000497 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000498 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000499 case SPI_CONTROLLER_VIA:
500 ichspi_bbar = mmio_readl(ich_spibar + 0x50) & ~BBAR_MASK;
501 if (ichspi_bbar)
502 msg_pdbg("Reserved bits in BBAR not zero: 0x%04x",
503 ichspi_bbar);
504 ichspi_bbar |= minaddr;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000505 rmmio_writel(ichspi_bbar, ich_spibar + 0x50);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000506 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000507 /* We don't have any option except complaining. And if the write
508 * failed, the restore will fail as well, so no problem there.
509 */
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000510 if (ichspi_bbar != minaddr)
511 msg_perr("Setting BBAR failed!\n");
512 break;
513 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000514 ichspi_bbar = mmio_readl(ich_spibar + 0xA0) & ~BBAR_MASK;
515 if (ichspi_bbar)
516 msg_pdbg("Reserved bits in BBAR not zero: 0x%04x",
517 ichspi_bbar);
518 ichspi_bbar |= minaddr;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000519 rmmio_writel(ichspi_bbar, ich_spibar + 0xA0);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000520 ichspi_bbar = mmio_readl(ich_spibar + 0xA0);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000521 /* We don't have any option except complaining. And if the write
522 * failed, the restore will fail as well, so no problem there.
523 */
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000524 if (ichspi_bbar != minaddr)
525 msg_perr("Setting BBAR failed!\n");
526 break;
527 default:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000528 msg_perr("Unknown chipset for BBAR setting!\n");
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000529 break;
530 }
531}
532
FENG yu ningf041e9b2008-12-15 02:32:11 +0000533/* This function generates OPCODES from or programs OPCODES to ICH according to
534 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000535 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000536 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000537 */
Michael Karchera4448d92010-07-22 18:04:15 +0000538static int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000539{
540 int rc = 0;
541 OPCODES *curopcodes_done;
542
543 if (curopcodes)
544 return 0;
545
546 if (ichspi_lock) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000547 msg_pdbg("Reading OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000548 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000549 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000550 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000551 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000552 curopcodes_done = &O_ST_M25P;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000553 rc = program_opcodes(curopcodes_done, 1);
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000554 /* Technically not part of opcode init, but it allows opcodes
555 * to run without transaction errors by setting the lowest
556 * allowed address to zero.
557 */
558 ich_set_bbar(0);
FENG yu ningc05a2952008-12-08 18:16:58 +0000559 }
560
561 if (rc) {
562 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000563 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000564 return 1;
565 } else {
566 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000567 msg_pdbg("done\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000568 pretty_print_opcodes(curopcodes);
569 msg_pdbg("\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000570 return 0;
571 }
572}
573
Stefan Reinauer43119562008-11-02 19:51:50 +0000574static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000575 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000576{
577 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000578 int timeout;
Peter Stuge7e2c0792008-06-29 01:30:41 +0000579 uint32_t temp32 = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000580 uint16_t temp16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000581 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000582 uint64_t opmenu;
583 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000584
585 /* Is it a write command? */
586 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
587 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
588 write_cmd = 1;
589 }
590
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000591 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
592 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
593 programmer_delay(10);
594 }
595 if (!timeout) {
596 msg_perr("Error: SCIP never cleared!\n");
597 return 1;
598 }
599
Dominik Geyerb46acba2008-05-16 12:55:55 +0000600 /* Programm Offset in Flash into FADDR */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000601 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000602
603 /* Program data into FDATA0 to N */
604 if (write_cmd && (datalength != 0)) {
605 temp32 = 0;
606 for (a = 0; a < datalength; a++) {
607 if ((a % 4) == 0) {
608 temp32 = 0;
609 }
610
611 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
612
613 if ((a % 4) == 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000614 REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)),
615 temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000616 }
617 }
618 if (((a - 1) % 4) != 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000619 REGWRITE32(ICH7_REG_SPID0 +
620 ((a - 1) - ((a - 1) % 4)), temp32);
621 }
622
623 }
624
625 /* Assemble SPIS */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000626 temp16 = REGREAD16(ICH7_REG_SPIS);
627 /* keep reserved bits */
628 temp16 &= SPIS_RESERVED_MASK;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000629 /* clear error status registers */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000630 temp16 |= (SPIS_CDS | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000631 REGWRITE16(ICH7_REG_SPIS, temp16);
632
633 /* Assemble SPIC */
634 temp16 = 0;
635
636 if (datalength != 0) {
637 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000638 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000639 }
640
641 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000642 opmenu = REGREAD32(ICH7_REG_OPMENU);
643 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
644
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000645 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
646 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000647 break;
648 }
649 opmenu >>= 8;
650 }
651 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000652 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000653 return 1;
654 }
655 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000656
Michael Karcher136125a2011-04-29 22:11:36 +0000657 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
658 /* Handle Atomic. Atomic commands include three steps:
659 - sending the preop (mainly EWSR or WREN)
660 - sending the main command
661 - waiting for the busy bit (WIP) to be cleared
662 This means the timeout must be sufficient for chip erase
663 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000664 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000665 switch (op.atomic) {
666 case 2:
667 /* Select second preop. */
668 temp16 |= SPIC_SPOP;
669 /* And fall through. */
670 case 1:
671 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000672 temp16 |= SPIC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000673 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000674 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000675 }
676
677 /* Start */
678 temp16 |= SPIC_SCGO;
679
680 /* write it */
681 REGWRITE16(ICH7_REG_SPIC, temp16);
682
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000683 /* Wait for Cycle Done Status or Flash Cycle Error. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000684 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
685 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000686 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000687 }
688 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000689 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
690 REGREAD16(ICH7_REG_SPIS));
691 return 1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000692 }
693
Sean Nelson316a29f2010-05-07 20:09:04 +0000694 /* FIXME: make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000695 temp16 = REGREAD16(ICH7_REG_SPIS);
696 if (temp16 & SPIS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000697 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000698 /* keep reserved bits */
699 temp16 &= SPIS_RESERVED_MASK;
700 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000701 return 1;
702 }
703
704 if ((!write_cmd) && (datalength != 0)) {
705 for (a = 0; a < datalength; a++) {
706 if ((a % 4) == 0) {
707 temp32 = REGREAD32(ICH7_REG_SPID0 + (a));
708 }
709
710 data[a] =
711 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
712 >> ((a % 4) * 8);
713 }
714 }
715
716 return 0;
717}
718
Stefan Reinauer43119562008-11-02 19:51:50 +0000719static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000720 uint8_t datalength, uint8_t * data)
721{
722 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000723 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000724 uint32_t temp32;
725 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000726 uint64_t opmenu;
727 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000728
729 /* Is it a write command? */
730 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
731 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
732 write_cmd = 1;
733 }
734
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000735 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
736 while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
737 programmer_delay(10);
738 }
739 if (!timeout) {
740 msg_perr("Error: SCIP never cleared!\n");
741 return 1;
742 }
743
Stefan Reinauera9424d52008-06-27 16:28:34 +0000744 /* Programm Offset in Flash into FADDR */
745 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
746
747 /* Program data into FDATA0 to N */
748 if (write_cmd && (datalength != 0)) {
749 temp32 = 0;
750 for (a = 0; a < datalength; a++) {
751 if ((a % 4) == 0) {
752 temp32 = 0;
753 }
754
755 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
756
757 if ((a % 4) == 3) {
758 REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
759 temp32);
760 }
761 }
762 if (((a - 1) % 4) != 3) {
763 REGWRITE32(ICH9_REG_FDATA0 +
764 ((a - 1) - ((a - 1) % 4)), temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000765 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000766 }
767
768 /* Assemble SSFS + SSFC */
Helge Wagnera319be12010-08-11 21:06:10 +0000769 temp32 = REGREAD32(ICH9_REG_SSFS);
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000770 /* Keep reserved bits only */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000771 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000772 /* Clear cycle done and cycle error status registers */
773 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000774 REGWRITE32(ICH9_REG_SSFS, temp32);
775
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000776 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000777 temp32 |= SSFC_SCF_20MHZ;
778
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000779 /* Set data byte count (DBC) and data cycle bit (DS) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000780 if (datalength != 0) {
781 uint32_t datatemp;
782 temp32 |= SSFC_DS;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000783 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
784 SSFC_DBC);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000785 temp32 |= datatemp;
786 }
787
788 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000789 opmenu = REGREAD32(ICH9_REG_OPMENU);
790 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
791
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000792 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
793 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000794 break;
795 }
796 opmenu >>= 8;
797 }
798 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000799 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000800 return 1;
801 }
802 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000803
Michael Karcher136125a2011-04-29 22:11:36 +0000804 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
805 /* Handle Atomic. Atomic commands include three steps:
806 - sending the preop (mainly EWSR or WREN)
807 - sending the main command
808 - waiting for the busy bit (WIP) to be cleared
809 This means the timeout must be sufficient for chip erase
810 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000811 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000812 switch (op.atomic) {
813 case 2:
814 /* Select second preop. */
815 temp32 |= SSFC_SPOP;
816 /* And fall through. */
817 case 1:
818 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000819 temp32 |= SSFC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000820 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000821 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000822 }
823
824 /* Start */
825 temp32 |= SSFC_SCGO;
826
827 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000828 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000829
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000830 /* Wait for Cycle Done Status or Flash Cycle Error. */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000831 while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000832 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000833 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000834 }
835 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000836 msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
837 REGREAD32(ICH9_REG_SSFS));
838 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000839 }
840
Sean Nelson316a29f2010-05-07 20:09:04 +0000841 /* FIXME make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000842 temp32 = REGREAD32(ICH9_REG_SSFS);
843 if (temp32 & SSFS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000844 msg_perr("Transaction error!\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000845 prettyprint_ich9_reg_ssfs(temp32);
846 prettyprint_ich9_reg_ssfc(temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000847 /* keep reserved bits */
848 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
849 /* Clear the transaction error. */
850 REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000851 return 1;
852 }
853
854 if ((!write_cmd) && (datalength != 0)) {
855 for (a = 0; a < datalength; a++) {
856 if ((a % 4) == 0) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000857 temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
Dominik Geyerb46acba2008-05-16 12:55:55 +0000858 }
859
860 data[a] =
Stefan Reinauera9424d52008-06-27 16:28:34 +0000861 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
862 >> ((a % 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000863 }
864 }
865
866 return 0;
867}
868
Stefan Reinauer43119562008-11-02 19:51:50 +0000869static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000870 uint8_t datalength, uint8_t * data)
871{
Michael Karcherb9dbe482011-05-11 17:07:07 +0000872 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000873 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000874 if (datalength > 16) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000875 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000876 "opcode 0x%02x, got datalength=%i, want <=16\n",
877 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000878 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000879 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000880 return ich7_run_opcode(op, offset, datalength, data, 16);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000881 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000882 if (datalength > 64) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000883 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000884 "opcode 0x%02x, got datalength=%i, want <=16\n",
885 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000886 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000887 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000888 return ich7_run_opcode(op, offset, datalength, data, 64);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000889 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000890 if (datalength > 64) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000891 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000892 "opcode 0x%02x, got datalength=%i, want <=16\n",
893 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000894 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000895 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000896 return ich9_run_opcode(op, offset, datalength, data);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000897 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000898 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000899 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000900
901 /* If we ever get here, something really weird happened */
902 return -1;
903}
904
Michael Karcherb9dbe482011-05-11 17:07:07 +0000905static int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000906 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000907{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000908 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000909 int opcode_index = -1;
910 const unsigned char cmd = *writearr;
911 OPCODE *opcode;
912 uint32_t addr = 0;
913 uint8_t *data;
914 int count;
915
Dominik Geyerb46acba2008-05-16 12:55:55 +0000916 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000917 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000918 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +0000919 if (!ichspi_lock)
920 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
921 if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000922 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
923 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +0000924 return SPI_INVALID_OPCODE;
925 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000926 }
927
928 opcode = &(curopcodes->opcode[opcode_index]);
929
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000930 /* The following valid writecnt/readcnt combinations exist:
931 * writecnt = 4, readcnt >= 0
932 * writecnt = 1, readcnt >= 0
933 * writecnt >= 4, readcnt = 0
934 * writecnt >= 1, readcnt = 0
935 * writecnt >= 1 is guaranteed for all commands.
936 */
937 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
938 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000939 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000940 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
941 writecnt);
942 return SPI_INVALID_LENGTH;
943 }
944 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
945 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000946 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000947 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
948 writecnt);
949 return SPI_INVALID_LENGTH;
950 }
951 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
952 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000953 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000954 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
955 writecnt);
956 return SPI_INVALID_LENGTH;
957 }
958 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
959 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
960 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000961 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000962 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
963 readcnt);
964 return SPI_INVALID_LENGTH;
965 }
966
Dominik Geyerb46acba2008-05-16 12:55:55 +0000967 /* if opcode-type requires an address */
968 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
969 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000970 addr = (writearr[1] << 16) |
971 (writearr[2] << 8) | (writearr[3] << 0);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000972 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000973 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000974 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000975 case SPI_CONTROLLER_ICH9:
976 if (addr < ichspi_bbar) {
977 msg_perr("%s: Address 0x%06x below allowed "
978 "range 0x%06x-0xffffff\n", __func__,
979 addr, ichspi_bbar);
980 return SPI_INVALID_ADDRESS;
981 }
982 break;
983 default:
984 break;
985 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000986 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000987
Dominik Geyerb46acba2008-05-16 12:55:55 +0000988 /* translate read/write array/count */
989 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000990 data = (uint8_t *) (writearr + 1);
991 count = writecnt - 1;
992 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
993 data = (uint8_t *) (writearr + 4);
994 count = writecnt - 4;
995 } else {
996 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000997 count = readcnt;
998 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000999
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001000 result = run_opcode(*opcode, addr, count, data);
1001 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001002 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1003 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1004 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1005 msg_pdbg("at address 0x%06x ", addr);
1006 }
1007 msg_pdbg("(payload length was %d).\n", count);
1008
1009 /* Print out the data array if it contains data to write.
1010 * Errors are detected before the received data is read back into
1011 * the array so it won't make sense to print it then. */
1012 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1013 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1014 int i;
1015 msg_pspew("The data was:\n");
1016 for(i=0; i<count; i++){
1017 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1018 }
1019 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001020 }
1021
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001022 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001023}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001024
Michael Karcherb9dbe482011-05-11 17:07:07 +00001025static int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001026{
1027 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001028 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001029 int oppos, preoppos;
1030 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001031 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001032 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001033 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1034 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001035 if ((oppos == -1) && (preoppos != -1)) {
1036 /* Current command is listed as preopcode in
1037 * ICH struct OPCODES, but next command is not
1038 * listed as opcode in that struct.
1039 * Check for command sanity, then
1040 * try to reprogram the ICH opcode list.
1041 */
1042 if (find_preop(curopcodes,
1043 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001044 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001045 "preopcodes 0x%02x and 0x%02x, "
1046 "ignoring the first.\n",
1047 __func__, cmds->writearr[0],
1048 (cmds + 1)->writearr[0]);
1049 continue;
1050 }
1051 /* If the chipset is locked down, we'll fail
1052 * during execution of the next command anyway.
1053 * No need to bother with fixups.
1054 */
1055 if (!ichspi_lock) {
Helge Wagner738e2522010-10-05 22:06:05 +00001056 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1057 if (oppos == -1)
1058 continue;
1059 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001060 continue;
1061 }
1062 }
1063 if ((oppos != -1) && (preoppos != -1)) {
1064 /* Current command is listed as preopcode in
1065 * ICH struct OPCODES and next command is listed
1066 * as opcode in that struct. Match them up.
1067 */
1068 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001069 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001070 }
1071 /* If none of the above if-statements about oppos or
1072 * preoppos matched, this is a normal opcode.
1073 */
1074 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001075 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
1076 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001077 /* Reset the type of all opcodes to non-atomic. */
1078 for (i = 0; i < 8; i++)
1079 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001080 }
1081 return ret;
1082}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001083
Michael Karchera4448d92010-07-22 18:04:15 +00001084#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1085#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1086#define ICH_BRWA(x) ((x >> 8) & 0xff)
1087#define ICH_BRRA(x) ((x >> 0) & 0xff)
1088
1089#define ICH_FREG_BASE(x) ((x >> 0) & 0x1fff)
1090#define ICH_FREG_LIMIT(x) ((x >> 16) & 0x1fff)
1091
1092static void do_ich9_spi_frap(uint32_t frap, int i)
1093{
Mathias Krausea60faab2011-01-17 07:50:42 +00001094 static const char *const access_names[4] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001095 "locked", "read-only", "write-only", "read-write"
1096 };
Mathias Krausea60faab2011-01-17 07:50:42 +00001097 static const char *const region_names[5] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001098 "Flash Descriptor", "BIOS", "Management Engine",
1099 "Gigabit Ethernet", "Platform Data"
1100 };
1101 uint32_t base, limit;
1102 int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1103 (((ICH_BRRA(frap) >> i) & 1) << 0);
1104 int offset = 0x54 + i * 4;
1105 uint32_t freg = mmio_readl(ich_spibar + offset);
1106
1107 msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n",
1108 offset, freg, i, region_names[i]);
1109
1110 base = ICH_FREG_BASE(freg);
1111 limit = ICH_FREG_LIMIT(freg);
Joshua Roysd172ecd2011-05-26 13:30:51 +00001112 if (base > limit) {
Michael Karchera4448d92010-07-22 18:04:15 +00001113 /* this FREG is disabled */
1114 msg_pdbg("%s region is unused.\n", region_names[i]);
1115 return;
1116 }
1117
1118 msg_pdbg("0x%08x-0x%08x is %s\n",
1119 (base << 12), (limit << 12) | 0x0fff,
1120 access_names[rwperms]);
1121}
1122
Michael Karcherb9dbe482011-05-11 17:07:07 +00001123static const struct spi_programmer spi_programmer_ich7 = {
1124 .type = SPI_CONTROLLER_ICH7,
1125 .max_data_read = 64,
1126 .max_data_write = 64,
1127 .command = ich_spi_send_command,
1128 .multicommand = ich_spi_send_multicommand,
1129 .read = default_spi_read,
1130 .write_256 = default_spi_write_256,
1131};
1132
1133static const struct spi_programmer spi_programmer_ich9 = {
1134 .type = SPI_CONTROLLER_ICH9,
1135 .max_data_read = 64,
1136 .max_data_write = 64,
1137 .command = ich_spi_send_command,
1138 .multicommand = ich_spi_send_multicommand,
1139 .read = default_spi_read,
1140 .write_256 = default_spi_write_256,
1141};
1142
Michael Karchera4448d92010-07-22 18:04:15 +00001143int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb,
1144 int ich_generation)
1145{
1146 int i;
1147 uint8_t old, new;
1148 uint16_t spibar_offset, tmp2;
1149 uint32_t tmp;
1150
Michael Karchera4448d92010-07-22 18:04:15 +00001151 switch (ich_generation) {
1152 case 7:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001153 register_spi_programmer(&spi_programmer_ich7);
Michael Karchera4448d92010-07-22 18:04:15 +00001154 spibar_offset = 0x3020;
1155 break;
1156 case 8:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001157 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001158 spibar_offset = 0x3020;
1159 break;
1160 case 9:
1161 case 10:
1162 default: /* Future version might behave the same */
Michael Karcherb9dbe482011-05-11 17:07:07 +00001163 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001164 spibar_offset = 0x3800;
1165 break;
1166 }
1167
1168 /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
1169 msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset);
1170
1171 /* Assign Virtual Address */
1172 ich_spibar = rcrb + spibar_offset;
1173
Michael Karcherb9dbe482011-05-11 17:07:07 +00001174 switch (spi_programmer->type) {
Michael Karchera4448d92010-07-22 18:04:15 +00001175 case SPI_CONTROLLER_ICH7:
1176 msg_pdbg("0x00: 0x%04x (SPIS)\n",
1177 mmio_readw(ich_spibar + 0));
1178 msg_pdbg("0x02: 0x%04x (SPIC)\n",
1179 mmio_readw(ich_spibar + 2));
1180 msg_pdbg("0x04: 0x%08x (SPIA)\n",
1181 mmio_readl(ich_spibar + 4));
1182 for (i = 0; i < 8; i++) {
1183 int offs;
1184 offs = 8 + (i * 8);
1185 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1186 mmio_readl(ich_spibar + offs), i);
1187 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1188 mmio_readl(ich_spibar + offs + 4), i);
1189 }
1190 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1191 msg_pdbg("0x50: 0x%08x (BBAR)\n",
1192 ichspi_bbar);
1193 msg_pdbg("0x54: 0x%04x (PREOP)\n",
1194 mmio_readw(ich_spibar + 0x54));
1195 msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
1196 mmio_readw(ich_spibar + 0x56));
1197 msg_pdbg("0x58: 0x%08x (OPMENU)\n",
1198 mmio_readl(ich_spibar + 0x58));
1199 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
1200 mmio_readl(ich_spibar + 0x5c));
1201 for (i = 0; i < 4; i++) {
1202 int offs;
1203 offs = 0x60 + (i * 4);
1204 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1205 mmio_readl(ich_spibar + offs), i);
1206 }
Michael Karchera4448d92010-07-22 18:04:15 +00001207 if (mmio_readw(ich_spibar) & (1 << 15)) {
1208 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1209 ichspi_lock = 1;
1210 }
1211 ich_init_opcodes();
1212 break;
1213 case SPI_CONTROLLER_ICH9:
1214 tmp2 = mmio_readw(ich_spibar + 4);
1215 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
1216 msg_pdbg("FLOCKDN %i, ", (tmp2 >> 15 & 1));
1217 msg_pdbg("FDV %i, ", (tmp2 >> 14) & 1);
1218 msg_pdbg("FDOPSS %i, ", (tmp2 >> 13) & 1);
1219 msg_pdbg("SCIP %i, ", (tmp2 >> 5) & 1);
1220 msg_pdbg("BERASE %i, ", (tmp2 >> 3) & 3);
1221 msg_pdbg("AEL %i, ", (tmp2 >> 2) & 1);
1222 msg_pdbg("FCERR %i, ", (tmp2 >> 1) & 1);
1223 msg_pdbg("FDONE %i\n", (tmp2 >> 0) & 1);
1224
1225 tmp = mmio_readl(ich_spibar + 0x50);
1226 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1227 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1228 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1229 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1230 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1231
1232 /* print out the FREGx registers along with FRAP access bits */
1233 for(i = 0; i < 5; i++)
1234 do_ich9_spi_frap(tmp, i);
1235
1236 msg_pdbg("0x74: 0x%08x (PR0)\n",
1237 mmio_readl(ich_spibar + 0x74));
1238 msg_pdbg("0x78: 0x%08x (PR1)\n",
1239 mmio_readl(ich_spibar + 0x78));
1240 msg_pdbg("0x7C: 0x%08x (PR2)\n",
1241 mmio_readl(ich_spibar + 0x7C));
1242 msg_pdbg("0x80: 0x%08x (PR3)\n",
1243 mmio_readl(ich_spibar + 0x80));
1244 msg_pdbg("0x84: 0x%08x (PR4)\n",
1245 mmio_readl(ich_spibar + 0x84));
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001246
1247 tmp = mmio_readl(ich_spibar + 0x90);
1248 msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001249 prettyprint_ich9_reg_ssfs(tmp);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001250 if (tmp & (1 << 3)) {
1251 msg_pdbg("Clearing SSFS.FCERR\n");
1252 mmio_writeb(1 << 3, ich_spibar + 0x90);
1253 }
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001254 msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8);
1255 prettyprint_ich9_reg_ssfc(tmp);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001256
Michael Karchera4448d92010-07-22 18:04:15 +00001257 msg_pdbg("0x94: 0x%04x (PREOP)\n",
1258 mmio_readw(ich_spibar + 0x94));
1259 msg_pdbg("0x96: 0x%04x (OPTYPE)\n",
1260 mmio_readw(ich_spibar + 0x96));
1261 msg_pdbg("0x98: 0x%08x (OPMENU)\n",
1262 mmio_readl(ich_spibar + 0x98));
1263 msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
1264 mmio_readl(ich_spibar + 0x9C));
1265 ichspi_bbar = mmio_readl(ich_spibar + 0xA0);
1266 msg_pdbg("0xA0: 0x%08x (BBAR)\n",
1267 ichspi_bbar);
1268 msg_pdbg("0xB0: 0x%08x (FDOC)\n",
1269 mmio_readl(ich_spibar + 0xB0));
1270 if (tmp2 & (1 << 15)) {
1271 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1272 ichspi_lock = 1;
1273 }
1274 ich_init_opcodes();
1275 break;
1276 default:
1277 /* Nothing */
1278 break;
1279 }
1280
1281 old = pci_read_byte(dev, 0xdc);
1282 msg_pdbg("SPI Read Configuration: ");
1283 new = (old >> 2) & 0x3;
1284 switch (new) {
1285 case 0:
1286 case 1:
1287 case 2:
1288 msg_pdbg("prefetching %sabled, caching %sabled, ",
1289 (new & 0x2) ? "en" : "dis",
1290 (new & 0x1) ? "dis" : "en");
1291 break;
1292 default:
1293 msg_pdbg("invalid prefetching/caching settings, ");
1294 break;
1295 }
1296 return 0;
1297}
1298
Michael Karcherb9dbe482011-05-11 17:07:07 +00001299static const struct spi_programmer spi_programmer_via = {
1300 .type = SPI_CONTROLLER_VIA,
1301 .max_data_read = 16,
1302 .max_data_write = 16,
1303 .command = ich_spi_send_command,
1304 .multicommand = ich_spi_send_multicommand,
1305 .read = default_spi_read,
1306 .write_256 = default_spi_write_256,
1307};
1308
Michael Karchera4448d92010-07-22 18:04:15 +00001309int via_init_spi(struct pci_dev *dev)
1310{
1311 uint32_t mmio_base;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001312 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00001313
1314 mmio_base = (pci_read_long(dev, 0xbc)) << 8;
1315 msg_pdbg("MMIO base at = 0x%x\n", mmio_base);
1316 ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
1317
Michael Karchera4448d92010-07-22 18:04:15 +00001318 /* Not sure if it speaks all these bus protocols. */
Michael Karcherb9dbe482011-05-11 17:07:07 +00001319 buses_supported = CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH;
1320 register_spi_programmer(&spi_programmer_via);
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001321
1322 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1323 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1324 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1325 for (i = 0; i < 2; i++) {
1326 int offs;
1327 offs = 8 + (i * 8);
1328 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1329 mmio_readl(ich_spibar + offs), i);
1330 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1331 mmio_readl(ich_spibar + offs + 4), i);
1332 }
1333 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1334 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1335 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1336 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1337 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1338 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1339 for (i = 0; i < 3; i++) {
1340 int offs;
1341 offs = 0x60 + (i * 4);
1342 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1343 mmio_readl(ich_spibar + offs), i);
1344 }
1345 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
1346 mmio_readw(ich_spibar + 0x6c));
1347 if (mmio_readw(ich_spibar) & (1 << 15)) {
1348 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1349 ichspi_lock = 1;
1350 }
1351
Michael Karchera4448d92010-07-22 18:04:15 +00001352 ich_init_opcodes();
1353
1354 return 0;
1355}
1356
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001357#endif