blob: 1cbff56507238cb4dffedfd9dfe3ec616e379fb5 [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 Tauner55206942011-06-11 09:53:22 +000046#define ICH9_REG_HSFS 0x04 /* 16 Bits Hardware Sequencing Flash Status */
47#define HSFS_FDONE_OFF 0 /* 0: Flash Cycle Done */
48#define HSFS_FDONE (0x1 << HSFS_FDONE_OFF)
49#define HSFS_FCERR_OFF 1 /* 1: Flash Cycle Error */
50#define HSFS_FCERR (0x1 << HSFS_FCERR_OFF)
51#define HSFS_AEL_OFF 2 /* 2: Access Error Log */
52#define HSFS_AEL (0x1 << HSFS_AEL_OFF)
53#define HSFS_BERASE_OFF 3 /* 3-4: Block/Sector Erase Size */
54#define HSFS_BERASE (0x3 << HSFS_BERASE_OFF)
55#define HSFS_SCIP_OFF 5 /* 5: SPI Cycle In Progress */
56#define HSFS_SCIP (0x1 << HSFS_SCIP_OFF)
57 /* 6-12: reserved */
58#define HSFS_FDOPSS_OFF 13 /* 13: Flash Descriptor Override Pin-Strap Status */
59#define HSFS_FDOPSS (0x1 << HSFS_FDOPSS_OFF)
60#define HSFS_FDV_OFF 14 /* 14: Flash Descriptor Valid */
61#define HSFS_FDV (0x1 << HSFS_FDV_OFF)
62#define HSFS_FLOCKDN_OFF 15 /* 15: Flash Configuration Lock-Down */
63#define HSFS_FLOCKDN (0x1 << HSFS_FLOCKDN_OFF)
64
65#define ICH9_REG_HSFC 0x06 /* 16 Bits Hardware Sequencing Flash Control */
66#define HSFC_FGO_OFF 0 /* 0: Flash Cycle Go */
67#define HSFC_FGO (0x1 << HSFC_FGO_OFF)
68#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
69#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
70 /* 3-7: reserved */
71#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
72#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
73 /* 14: reserved */
74#define HSFC_SME_OFF 15 /* 15: SPI SMI# Enable */
75#define HSFC_SME (0x1 << HSFC_SME_OFF)
76
Stefan Taunerc0aaf952011-05-19 02:58:17 +000077#define ICH9_REG_FADDR 0x08 /* 32 Bits */
78#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
Stefan Reinauera9424d52008-06-27 16:28:34 +000079
Stefan Taunerc0aaf952011-05-19 02:58:17 +000080#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Stefan Tauner0c1ec452011-06-11 09:53:09 +000081#define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */
82#define SSFS_SCIP (0x1 << SSFS_SCIP_OFF)
83#define SSFS_FDONE_OFF 2 /* Cycle Done Status */
84#define SSFS_FDONE (0x1 << SSFS_FDONE_OFF)
85#define SSFS_FCERR_OFF 3 /* Flash Cycle Error */
86#define SSFS_FCERR (0x1 << SSFS_FCERR_OFF)
87#define SSFS_AEL_OFF 4 /* Access Error Log */
88#define SSFS_AEL (0x1 << SSFS_AEL_OFF)
Stefan Taunerc0aaf952011-05-19 02:58:17 +000089/* The following bits are reserved in SSFS: 1,5-7. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +000090#define SSFS_RESERVED_MASK 0x000000e2
Stefan Reinauera9424d52008-06-27 16:28:34 +000091
Stefan Taunerc0aaf952011-05-19 02:58:17 +000092#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Stefan Taunerc0aaf952011-05-19 02:58:17 +000093/* We combine SSFS and SSFC to one 32-bit word,
Stefan Tauner0c1ec452011-06-11 09:53:09 +000094 * therefore SSFC bits are off by 8. */
95 /* 0: reserved */
96#define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */
97#define SSFC_SCGO (0x1 << SSFC_SCGO_OFF)
98#define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */
99#define SSFC_ACS (0x1 << SSFC_ACS_OFF)
100#define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */
101#define SSFC_SPOP (0x1 << SSFC_SPOP_OFF)
102#define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */
103#define SSFC_COP (0x7 << SSFC_COP_OFF)
104 /* 7: reserved */
105#define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */
106#define SSFC_DBC (0x3f << SSFC_DBC_OFF)
107#define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */
108#define SSFC_DS (0x1 << SSFC_DS_OFF)
109#define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */
110#define SSFC_SME (0x1 << SSFC_SME_OFF)
111#define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */
112#define SSFC_SCF (0x7 << SSFC_SCF_OFF)
113#define SSFC_SCF_20MHZ 0x00000000
114#define SSFC_SCF_33MHZ 0x01000000
115 /* 19-23: reserved */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000116#define SSFC_RESERVED_MASK 0xf8008100
Stefan Reinauera9424d52008-06-27 16:28:34 +0000117
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000118#define ICH9_REG_PREOP 0x94 /* 16 Bits */
119#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
120#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000121
122// ICH9R SPI commands
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000123#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
124#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
125#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
126#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
Dominik Geyerb46acba2008-05-16 12:55:55 +0000127
Stefan Reinauera9424d52008-06-27 16:28:34 +0000128// ICH7 registers
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000129#define ICH7_REG_SPIS 0x00 /* 16 Bits */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000130#define SPIS_SCIP 0x0001
131#define SPIS_GRANT 0x0002
132#define SPIS_CDS 0x0004
133#define SPIS_FCERR 0x0008
134#define SPIS_RESERVED_MASK 0x7ff0
Stefan Reinauera9424d52008-06-27 16:28:34 +0000135
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000136/* VIA SPI is compatible with ICH7, but maxdata
137 to transfer is 16 bytes.
138
139 DATA byte count on ICH7 is 8:13, on VIA 8:11
140
141 bit 12 is port select CS0 CS1
142 bit 13 is FAST READ enable
143 bit 7 is used with fast read and one shot controls CS de-assert?
144*/
145
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000146#define ICH7_REG_SPIC 0x02 /* 16 Bits */
147#define SPIC_SCGO 0x0002
148#define SPIC_ACS 0x0004
149#define SPIC_SPOP 0x0008
150#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000151
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000152#define ICH7_REG_SPIA 0x04 /* 32 Bits */
153#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
154#define ICH7_REG_PREOP 0x54 /* 16 Bits */
155#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
156#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000157
FENG yu ningc05a2952008-12-08 18:16:58 +0000158/* ICH SPI configuration lock-down. May be set during chipset enabling. */
Michael Karchera4448d92010-07-22 18:04:15 +0000159static int ichspi_lock = 0;
FENG yu ningc05a2952008-12-08 18:16:58 +0000160
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000161uint32_t ichspi_bbar = 0;
162
Michael Karchera4448d92010-07-22 18:04:15 +0000163static void *ich_spibar = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000164
Dominik Geyerb46acba2008-05-16 12:55:55 +0000165typedef struct _OPCODE {
166 uint8_t opcode; //This commands spi opcode
167 uint8_t spi_type; //This commands spi type
168 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
169} OPCODE;
170
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000171/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000172 * Preop 1: Write Enable
173 * Preop 2: Write Status register enable
174 *
175 * OP 0: Write address
176 * OP 1: Read Address
177 * OP 2: ERASE block
178 * OP 3: Read Status register
179 * OP 4: Read ID
180 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000181 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000182 * OP 7: Chip erase
183 */
184typedef struct _OPCODES {
185 uint8_t preop[2];
186 OPCODE opcode[8];
187} OPCODES;
188
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000189static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000190
191/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000192static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000193{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000194 return mmio_readl(ich_spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000195}
196
Uwe Hermann09e04f72009-05-16 22:36:00 +0000197static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000198{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000199 return mmio_readw(ich_spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000200}
201
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000202static uint16_t REGREAD8(int X)
203{
204 return mmio_readb(ich_spibar + X);
205}
206
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000207#define REGWRITE32(off,val) mmio_writel(val, ich_spibar+off)
208#define REGWRITE16(off,val) mmio_writew(val, ich_spibar+off)
209#define REGWRITE8(off,val) mmio_writeb(val, ich_spibar+off)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000210
Dominik Geyerb46acba2008-05-16 12:55:55 +0000211/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000212static int find_opcode(OPCODES *op, uint8_t opcode);
213static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000214static int generate_opcodes(OPCODES * op);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000215static int program_opcodes(OPCODES *op, int enable_undo);
Stefan Reinauer43119562008-11-02 19:51:50 +0000216static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000217 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000218
FENG yu ningf041e9b2008-12-15 02:32:11 +0000219/* for pairing opcodes with their required preop */
220struct preop_opcode_pair {
221 uint8_t preop;
222 uint8_t opcode;
223};
224
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000225/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000226const struct preop_opcode_pair pops[] = {
FENG yu ningf041e9b2008-12-15 02:32:11 +0000227 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
228 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
229 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
230 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
231 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
232 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000233 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
234 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000235 {JEDEC_EWSR, JEDEC_WRSR},
236 {0,}
237};
238
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000239/* Reasonable default configuration. Needs ad-hoc modifications if we
240 * encounter unlisted opcodes. Fun.
241 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000242static OPCODES O_ST_M25P = {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000243 {
244 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000245 JEDEC_EWSR,
246 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000247 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000248 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000249 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000250 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000251 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000252 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000253 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000254 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000255 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
256 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000257};
258
Helge Wagner738e2522010-10-05 22:06:05 +0000259/* List of opcodes with their corresponding spi_type
260 * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
261 * is needed which is currently not in the chipset OPCODE table
262 */
263static OPCODE POSSIBLE_OPCODES[] = {
264 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
265 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
266 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
267 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
268 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
269 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
270 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
271 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
272 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
273 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
274 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
275};
276
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000277static OPCODES O_EXISTING = {};
FENG yu ningc05a2952008-12-08 18:16:58 +0000278
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000279/* pretty printing functions */
280static void pretty_print_opcodes(OPCODES *ops)
281{
282 if(ops == NULL)
283 return;
284
285 msg_pdbg("preop0=0x%02x, preop1=0x%02x\n", ops->preop[0],
286 ops->preop[1]);
287
288 OPCODE oc;
289 uint8_t i;
290 for (i = 0; i < 8; i++) {
291 oc = ops->opcode[i];
292 msg_pdbg("op[%d]=0x%02x, %d, %d\n",
293 i,
294 oc.opcode,
295 oc.spi_type,
296 oc.atomic);
297 }
298}
299
300#define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF)
301
Stefan Tauner55206942011-06-11 09:53:22 +0000302static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
303{
304 msg_pdbg("HSFS: ");
305 pprint_reg(HSFS, FDONE, reg_val, ", ");
306 pprint_reg(HSFS, FCERR, reg_val, ", ");
307 pprint_reg(HSFS, AEL, reg_val, ", ");
308 pprint_reg(HSFS, BERASE, reg_val, ", ");
309 pprint_reg(HSFS, SCIP, reg_val, ", ");
310 pprint_reg(HSFS, FDOPSS, reg_val, ", ");
311 pprint_reg(HSFS, FDV, reg_val, ", ");
312 pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
313}
314
315static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
316{
317 msg_pdbg("HSFC: ");
318 pprint_reg(HSFC, FGO, reg_val, ", ");
319 pprint_reg(HSFC, FCYCLE, reg_val, ", ");
320 pprint_reg(HSFC, FDBC, reg_val, ", ");
321 pprint_reg(HSFC, SME, reg_val, "\n");
322}
323
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000324static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
325{
326 msg_pdbg("SSFS: ");
327 pprint_reg(SSFS, SCIP, reg_val, ", ");
328 pprint_reg(SSFS, FDONE, reg_val, ", ");
329 pprint_reg(SSFS, FCERR, reg_val, ", ");
330 pprint_reg(SSFS, AEL, reg_val, "\n");
331}
332
333static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
334{
335 msg_pdbg("SSFC: ");
336 pprint_reg(SSFC, SCGO, reg_val, ", ");
337 pprint_reg(SSFC, ACS, reg_val, ", ");
338 pprint_reg(SSFC, SPOP, reg_val, ", ");
339 pprint_reg(SSFC, COP, reg_val, ", ");
340 pprint_reg(SSFC, DBC, reg_val, ", ");
341 pprint_reg(SSFC, SME, reg_val, ", ");
342 pprint_reg(SSFC, SCF, reg_val, "\n");
343}
344
Helge Wagner738e2522010-10-05 22:06:05 +0000345static uint8_t lookup_spi_type(uint8_t opcode)
346{
347 int a;
348
349 for (a = 0; a < sizeof(POSSIBLE_OPCODES)/sizeof(POSSIBLE_OPCODES[0]); a++) {
350 if (POSSIBLE_OPCODES[a].opcode == opcode)
351 return POSSIBLE_OPCODES[a].spi_type;
352 }
353
354 return 0xFF;
355}
356
357static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
358{
359 uint8_t spi_type;
360
361 spi_type = lookup_spi_type(opcode);
362 if (spi_type > 3) {
363 /* Try to guess spi type from read/write sizes.
364 * The following valid writecnt/readcnt combinations exist:
365 * writecnt = 4, readcnt >= 0
366 * writecnt = 1, readcnt >= 0
367 * writecnt >= 4, readcnt = 0
368 * writecnt >= 1, readcnt = 0
369 * writecnt >= 1 is guaranteed for all commands.
370 */
371 if (readcnt == 0)
372 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
373 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
374 * bytes are actual the address, they go to the bus anyhow
375 */
376 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
377 else if (writecnt == 1) // and readcnt is > 0
378 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
379 else if (writecnt == 4) // and readcnt is > 0
380 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
381 // else we have an invalid case, will be handled below
382 }
383 if (spi_type <= 3) {
384 int oppos=2; // use original JEDEC_BE_D8 offset
385 curopcodes->opcode[oppos].opcode = opcode;
386 curopcodes->opcode[oppos].spi_type = spi_type;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000387 program_opcodes(curopcodes, 0);
Helge Wagner738e2522010-10-05 22:06:05 +0000388 oppos = find_opcode(curopcodes, opcode);
389 msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
390 return oppos;
391 }
392 return -1;
393}
394
Uwe Hermann09e04f72009-05-16 22:36:00 +0000395static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000396{
397 int a;
398
399 for (a = 0; a < 8; a++) {
400 if (op->opcode[a].opcode == opcode)
401 return a;
402 }
403
404 return -1;
405}
406
Uwe Hermann09e04f72009-05-16 22:36:00 +0000407static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000408{
409 int a;
410
411 for (a = 0; a < 2; a++) {
412 if (op->preop[a] == preop)
413 return a;
414 }
415
416 return -1;
417}
418
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000419/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000420static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000421{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000422 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000423 uint16_t preop, optype;
424 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000425
426 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000427 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000428 return -1;
429 }
430
Michael Karcherb9dbe482011-05-11 17:07:07 +0000431 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000432 case SPI_CONTROLLER_ICH7:
433 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000434 preop = REGREAD16(ICH7_REG_PREOP);
435 optype = REGREAD16(ICH7_REG_OPTYPE);
436 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
437 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
438 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000439 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000440 preop = REGREAD16(ICH9_REG_PREOP);
441 optype = REGREAD16(ICH9_REG_OPTYPE);
442 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
443 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
444 break;
445 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000446 msg_perr("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000447 return -1;
448 }
449
450 op->preop[0] = (uint8_t) preop;
451 op->preop[1] = (uint8_t) (preop >> 8);
452
453 for (a = 0; a < 8; a++) {
454 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
455 optype >>= 2;
456 }
457
458 for (a = 0; a < 4; a++) {
459 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
460 opmenu[0] >>= 8;
461 }
462
463 for (a = 4; a < 8; a++) {
464 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
465 opmenu[1] >>= 8;
466 }
467
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000468 /* No preopcodes used by default. */
469 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000470 op->opcode[a].atomic = 0;
471
FENG yu ningc05a2952008-12-08 18:16:58 +0000472 return 0;
473}
474
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000475static int program_opcodes(OPCODES *op, int enable_undo)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000476{
477 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000478 uint16_t preop, optype;
479 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000480
481 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000482 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000483 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000484 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000485 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000486
Stefan Reinauera9424d52008-06-27 16:28:34 +0000487 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000488 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000489 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000490 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000491 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000492
Stefan Reinauera9424d52008-06-27 16:28:34 +0000493 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000494 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000495 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000496 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000497 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000498
Dominik Geyerb46acba2008-05-16 12:55:55 +0000499 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000500 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000501 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000502 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000503 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000504
Sean Nelson316a29f2010-05-07 20:09:04 +0000505 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 +0000506 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000507 case SPI_CONTROLLER_ICH7:
508 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000509 /* Register undo only for enable_undo=1, i.e. first call. */
510 if (enable_undo) {
511 rmmio_valw(ich_spibar + ICH7_REG_PREOP);
512 rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
513 rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
514 rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
515 }
516 mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
517 mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
518 mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
519 mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000520 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000521 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000522 /* Register undo only for enable_undo=1, i.e. first call. */
523 if (enable_undo) {
524 rmmio_valw(ich_spibar + ICH9_REG_PREOP);
525 rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
526 rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
527 rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
528 }
529 mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
530 mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
531 mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
532 mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000533 break;
534 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000535 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000536 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000537 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000538
539 return 0;
540}
541
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000542/*
543 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
544 * it didn't stick.
545 */
546void ich_set_bbar(uint32_t minaddr)
547{
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000548#define BBAR_MASK 0x00ffff00
549 minaddr &= BBAR_MASK;
Michael Karcherb9dbe482011-05-11 17:07:07 +0000550 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000551 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000552 case SPI_CONTROLLER_VIA:
553 ichspi_bbar = mmio_readl(ich_spibar + 0x50) & ~BBAR_MASK;
554 if (ichspi_bbar)
555 msg_pdbg("Reserved bits in BBAR not zero: 0x%04x",
556 ichspi_bbar);
557 ichspi_bbar |= minaddr;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000558 rmmio_writel(ichspi_bbar, ich_spibar + 0x50);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000559 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000560 /* We don't have any option except complaining. And if the write
561 * failed, the restore will fail as well, so no problem there.
562 */
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000563 if (ichspi_bbar != minaddr)
564 msg_perr("Setting BBAR failed!\n");
565 break;
566 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000567 ichspi_bbar = mmio_readl(ich_spibar + 0xA0) & ~BBAR_MASK;
568 if (ichspi_bbar)
569 msg_pdbg("Reserved bits in BBAR not zero: 0x%04x",
570 ichspi_bbar);
571 ichspi_bbar |= minaddr;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000572 rmmio_writel(ichspi_bbar, ich_spibar + 0xA0);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000573 ichspi_bbar = mmio_readl(ich_spibar + 0xA0);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000574 /* We don't have any option except complaining. And if the write
575 * failed, the restore will fail as well, so no problem there.
576 */
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000577 if (ichspi_bbar != minaddr)
578 msg_perr("Setting BBAR failed!\n");
579 break;
580 default:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000581 msg_perr("Unknown chipset for BBAR setting!\n");
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000582 break;
583 }
584}
585
FENG yu ningf041e9b2008-12-15 02:32:11 +0000586/* This function generates OPCODES from or programs OPCODES to ICH according to
587 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000588 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000589 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000590 */
Michael Karchera4448d92010-07-22 18:04:15 +0000591static int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000592{
593 int rc = 0;
594 OPCODES *curopcodes_done;
595
596 if (curopcodes)
597 return 0;
598
599 if (ichspi_lock) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000600 msg_pdbg("Reading OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000601 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000602 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000603 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000604 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000605 curopcodes_done = &O_ST_M25P;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000606 rc = program_opcodes(curopcodes_done, 1);
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000607 /* Technically not part of opcode init, but it allows opcodes
608 * to run without transaction errors by setting the lowest
609 * allowed address to zero.
610 */
611 ich_set_bbar(0);
FENG yu ningc05a2952008-12-08 18:16:58 +0000612 }
613
614 if (rc) {
615 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000616 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000617 return 1;
618 } else {
619 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000620 msg_pdbg("done\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000621 pretty_print_opcodes(curopcodes);
622 msg_pdbg("\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000623 return 0;
624 }
625}
626
Stefan Reinauer43119562008-11-02 19:51:50 +0000627static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000628 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000629{
630 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000631 int timeout;
Peter Stuge7e2c0792008-06-29 01:30:41 +0000632 uint32_t temp32 = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000633 uint16_t temp16;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000634 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000635 uint64_t opmenu;
636 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000637
638 /* Is it a write command? */
639 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
640 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
641 write_cmd = 1;
642 }
643
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000644 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
645 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
646 programmer_delay(10);
647 }
648 if (!timeout) {
649 msg_perr("Error: SCIP never cleared!\n");
650 return 1;
651 }
652
Dominik Geyerb46acba2008-05-16 12:55:55 +0000653 /* Programm Offset in Flash into FADDR */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000654 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000655
656 /* Program data into FDATA0 to N */
657 if (write_cmd && (datalength != 0)) {
658 temp32 = 0;
659 for (a = 0; a < datalength; a++) {
660 if ((a % 4) == 0) {
661 temp32 = 0;
662 }
663
664 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
665
666 if ((a % 4) == 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000667 REGWRITE32(ICH7_REG_SPID0 + (a - (a % 4)),
668 temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000669 }
670 }
671 if (((a - 1) % 4) != 3) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000672 REGWRITE32(ICH7_REG_SPID0 +
673 ((a - 1) - ((a - 1) % 4)), temp32);
674 }
675
676 }
677
678 /* Assemble SPIS */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000679 temp16 = REGREAD16(ICH7_REG_SPIS);
680 /* keep reserved bits */
681 temp16 &= SPIS_RESERVED_MASK;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000682 /* clear error status registers */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000683 temp16 |= (SPIS_CDS | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000684 REGWRITE16(ICH7_REG_SPIS, temp16);
685
686 /* Assemble SPIC */
687 temp16 = 0;
688
689 if (datalength != 0) {
690 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000691 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000692 }
693
694 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000695 opmenu = REGREAD32(ICH7_REG_OPMENU);
696 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
697
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000698 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
699 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000700 break;
701 }
702 opmenu >>= 8;
703 }
704 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000705 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000706 return 1;
707 }
708 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000709
Michael Karcher136125a2011-04-29 22:11:36 +0000710 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
711 /* Handle Atomic. Atomic commands include three steps:
712 - sending the preop (mainly EWSR or WREN)
713 - sending the main command
714 - waiting for the busy bit (WIP) to be cleared
715 This means the timeout must be sufficient for chip erase
716 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000717 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000718 switch (op.atomic) {
719 case 2:
720 /* Select second preop. */
721 temp16 |= SPIC_SPOP;
722 /* And fall through. */
723 case 1:
724 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000725 temp16 |= SPIC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000726 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000727 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000728 }
729
730 /* Start */
731 temp16 |= SPIC_SCGO;
732
733 /* write it */
734 REGWRITE16(ICH7_REG_SPIC, temp16);
735
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000736 /* Wait for Cycle Done Status or Flash Cycle Error. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000737 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
738 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000739 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000740 }
741 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000742 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
743 REGREAD16(ICH7_REG_SPIS));
744 return 1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000745 }
746
Sean Nelson316a29f2010-05-07 20:09:04 +0000747 /* FIXME: make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000748 temp16 = REGREAD16(ICH7_REG_SPIS);
749 if (temp16 & SPIS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000750 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000751 /* keep reserved bits */
752 temp16 &= SPIS_RESERVED_MASK;
753 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000754 return 1;
755 }
756
757 if ((!write_cmd) && (datalength != 0)) {
758 for (a = 0; a < datalength; a++) {
759 if ((a % 4) == 0) {
760 temp32 = REGREAD32(ICH7_REG_SPID0 + (a));
761 }
762
763 data[a] =
764 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
765 >> ((a % 4) * 8);
766 }
767 }
768
769 return 0;
770}
771
Stefan Reinauer43119562008-11-02 19:51:50 +0000772static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000773 uint8_t datalength, uint8_t * data)
774{
775 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000776 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000777 uint32_t temp32;
778 uint32_t a;
Stefan Reinauer43119562008-11-02 19:51:50 +0000779 uint64_t opmenu;
780 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000781
782 /* Is it a write command? */
783 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
784 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
785 write_cmd = 1;
786 }
787
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000788 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
789 while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
790 programmer_delay(10);
791 }
792 if (!timeout) {
793 msg_perr("Error: SCIP never cleared!\n");
794 return 1;
795 }
796
Stefan Reinauera9424d52008-06-27 16:28:34 +0000797 /* Programm Offset in Flash into FADDR */
798 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF)); /* SPI addresses are 24 BIT only */
799
800 /* Program data into FDATA0 to N */
801 if (write_cmd && (datalength != 0)) {
802 temp32 = 0;
803 for (a = 0; a < datalength; a++) {
804 if ((a % 4) == 0) {
805 temp32 = 0;
806 }
807
808 temp32 |= ((uint32_t) data[a]) << ((a % 4) * 8);
809
810 if ((a % 4) == 3) {
811 REGWRITE32(ICH9_REG_FDATA0 + (a - (a % 4)),
812 temp32);
813 }
814 }
815 if (((a - 1) % 4) != 3) {
816 REGWRITE32(ICH9_REG_FDATA0 +
817 ((a - 1) - ((a - 1) % 4)), temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000818 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000819 }
820
821 /* Assemble SSFS + SSFC */
Helge Wagnera319be12010-08-11 21:06:10 +0000822 temp32 = REGREAD32(ICH9_REG_SSFS);
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000823 /* Keep reserved bits only */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000824 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000825 /* Clear cycle done and cycle error status registers */
826 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000827 REGWRITE32(ICH9_REG_SSFS, temp32);
828
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000829 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000830 temp32 |= SSFC_SCF_20MHZ;
831
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000832 /* Set data byte count (DBC) and data cycle bit (DS) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000833 if (datalength != 0) {
834 uint32_t datatemp;
835 temp32 |= SSFC_DS;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000836 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
837 SSFC_DBC);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000838 temp32 |= datatemp;
839 }
840
841 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000842 opmenu = REGREAD32(ICH9_REG_OPMENU);
843 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
844
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000845 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
846 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000847 break;
848 }
849 opmenu >>= 8;
850 }
851 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000852 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000853 return 1;
854 }
855 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000856
Michael Karcher136125a2011-04-29 22:11:36 +0000857 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
858 /* Handle Atomic. Atomic commands include three steps:
859 - sending the preop (mainly EWSR or WREN)
860 - sending the main command
861 - waiting for the busy bit (WIP) to be cleared
862 This means the timeout must be sufficient for chip erase
863 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000864 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000865 switch (op.atomic) {
866 case 2:
867 /* Select second preop. */
868 temp32 |= SSFC_SPOP;
869 /* And fall through. */
870 case 1:
871 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000872 temp32 |= SSFC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000873 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000874 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000875 }
876
877 /* Start */
878 temp32 |= SSFC_SCGO;
879
880 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000881 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000882
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000883 /* Wait for Cycle Done Status or Flash Cycle Error. */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000884 while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000885 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000886 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000887 }
888 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000889 msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
890 REGREAD32(ICH9_REG_SSFS));
891 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000892 }
893
Sean Nelson316a29f2010-05-07 20:09:04 +0000894 /* FIXME make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000895 temp32 = REGREAD32(ICH9_REG_SSFS);
896 if (temp32 & SSFS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000897 msg_perr("Transaction error!\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000898 prettyprint_ich9_reg_ssfs(temp32);
899 prettyprint_ich9_reg_ssfc(temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000900 /* keep reserved bits */
901 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
902 /* Clear the transaction error. */
903 REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000904 return 1;
905 }
906
907 if ((!write_cmd) && (datalength != 0)) {
908 for (a = 0; a < datalength; a++) {
909 if ((a % 4) == 0) {
Stefan Reinauera9424d52008-06-27 16:28:34 +0000910 temp32 = REGREAD32(ICH9_REG_FDATA0 + (a));
Dominik Geyerb46acba2008-05-16 12:55:55 +0000911 }
912
913 data[a] =
Stefan Reinauera9424d52008-06-27 16:28:34 +0000914 (temp32 & (((uint32_t) 0xff) << ((a % 4) * 8)))
915 >> ((a % 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000916 }
917 }
918
919 return 0;
920}
921
Stefan Reinauer43119562008-11-02 19:51:50 +0000922static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000923 uint8_t datalength, uint8_t * data)
924{
Michael Karcherb9dbe482011-05-11 17:07:07 +0000925 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000926 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000927 if (datalength > 16) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000928 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000929 "opcode 0x%02x, got datalength=%i, want <=16\n",
930 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000931 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000932 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000933 return ich7_run_opcode(op, offset, datalength, data, 16);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000934 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000935 if (datalength > 64) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000936 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000937 "opcode 0x%02x, got datalength=%i, want <=16\n",
938 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000939 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000940 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000941 return ich7_run_opcode(op, offset, datalength, data, 64);
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000942 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000943 if (datalength > 64) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000944 msg_perr("%s: Internal command size error for "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000945 "opcode 0x%02x, got datalength=%i, want <=16\n",
946 __func__, op.opcode, datalength);
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000947 return SPI_INVALID_LENGTH;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000948 }
Stefan Reinauer43119562008-11-02 19:51:50 +0000949 return ich9_run_opcode(op, offset, datalength, data);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000950 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000951 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000952 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000953
954 /* If we ever get here, something really weird happened */
955 return -1;
956}
957
Michael Karcherb9dbe482011-05-11 17:07:07 +0000958static int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000959 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000960{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000961 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000962 int opcode_index = -1;
963 const unsigned char cmd = *writearr;
964 OPCODE *opcode;
965 uint32_t addr = 0;
966 uint8_t *data;
967 int count;
968
Dominik Geyerb46acba2008-05-16 12:55:55 +0000969 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000970 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000971 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +0000972 if (!ichspi_lock)
973 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
974 if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000975 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
976 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +0000977 return SPI_INVALID_OPCODE;
978 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000979 }
980
981 opcode = &(curopcodes->opcode[opcode_index]);
982
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000983 /* The following valid writecnt/readcnt combinations exist:
984 * writecnt = 4, readcnt >= 0
985 * writecnt = 1, readcnt >= 0
986 * writecnt >= 4, readcnt = 0
987 * writecnt >= 1, readcnt = 0
988 * writecnt >= 1 is guaranteed for all commands.
989 */
990 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
991 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000992 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000993 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
994 writecnt);
995 return SPI_INVALID_LENGTH;
996 }
997 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
998 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000999 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001000 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
1001 writecnt);
1002 return SPI_INVALID_LENGTH;
1003 }
1004 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1005 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001006 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001007 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
1008 writecnt);
1009 return SPI_INVALID_LENGTH;
1010 }
1011 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1012 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1013 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001014 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001015 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1016 readcnt);
1017 return SPI_INVALID_LENGTH;
1018 }
1019
Dominik Geyerb46acba2008-05-16 12:55:55 +00001020 /* if opcode-type requires an address */
1021 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1022 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001023 addr = (writearr[1] << 16) |
1024 (writearr[2] << 8) | (writearr[3] << 0);
Michael Karcherb9dbe482011-05-11 17:07:07 +00001025 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001026 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001027 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001028 case SPI_CONTROLLER_ICH9:
1029 if (addr < ichspi_bbar) {
1030 msg_perr("%s: Address 0x%06x below allowed "
1031 "range 0x%06x-0xffffff\n", __func__,
1032 addr, ichspi_bbar);
1033 return SPI_INVALID_ADDRESS;
1034 }
1035 break;
1036 default:
1037 break;
1038 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001039 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001040
Dominik Geyerb46acba2008-05-16 12:55:55 +00001041 /* translate read/write array/count */
1042 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001043 data = (uint8_t *) (writearr + 1);
1044 count = writecnt - 1;
1045 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1046 data = (uint8_t *) (writearr + 4);
1047 count = writecnt - 4;
1048 } else {
1049 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001050 count = readcnt;
1051 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001052
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001053 result = run_opcode(*opcode, addr, count, data);
1054 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001055 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1056 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1057 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1058 msg_pdbg("at address 0x%06x ", addr);
1059 }
1060 msg_pdbg("(payload length was %d).\n", count);
1061
1062 /* Print out the data array if it contains data to write.
1063 * Errors are detected before the received data is read back into
1064 * the array so it won't make sense to print it then. */
1065 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1066 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1067 int i;
1068 msg_pspew("The data was:\n");
1069 for(i=0; i<count; i++){
1070 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1071 }
1072 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001073 }
1074
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001075 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001076}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001077
Michael Karcherb9dbe482011-05-11 17:07:07 +00001078static int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001079{
1080 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001081 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001082 int oppos, preoppos;
1083 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001084 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001085 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001086 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1087 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001088 if ((oppos == -1) && (preoppos != -1)) {
1089 /* Current command is listed as preopcode in
1090 * ICH struct OPCODES, but next command is not
1091 * listed as opcode in that struct.
1092 * Check for command sanity, then
1093 * try to reprogram the ICH opcode list.
1094 */
1095 if (find_preop(curopcodes,
1096 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001097 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001098 "preopcodes 0x%02x and 0x%02x, "
1099 "ignoring the first.\n",
1100 __func__, cmds->writearr[0],
1101 (cmds + 1)->writearr[0]);
1102 continue;
1103 }
1104 /* If the chipset is locked down, we'll fail
1105 * during execution of the next command anyway.
1106 * No need to bother with fixups.
1107 */
1108 if (!ichspi_lock) {
Helge Wagner738e2522010-10-05 22:06:05 +00001109 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1110 if (oppos == -1)
1111 continue;
1112 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001113 continue;
1114 }
1115 }
1116 if ((oppos != -1) && (preoppos != -1)) {
1117 /* Current command is listed as preopcode in
1118 * ICH struct OPCODES and next command is listed
1119 * as opcode in that struct. Match them up.
1120 */
1121 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001122 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001123 }
1124 /* If none of the above if-statements about oppos or
1125 * preoppos matched, this is a normal opcode.
1126 */
1127 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001128 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
1129 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001130 /* Reset the type of all opcodes to non-atomic. */
1131 for (i = 0; i < 8; i++)
1132 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001133 }
1134 return ret;
1135}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001136
Michael Karchera4448d92010-07-22 18:04:15 +00001137#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1138#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1139#define ICH_BRWA(x) ((x >> 8) & 0xff)
1140#define ICH_BRRA(x) ((x >> 0) & 0xff)
1141
1142#define ICH_FREG_BASE(x) ((x >> 0) & 0x1fff)
1143#define ICH_FREG_LIMIT(x) ((x >> 16) & 0x1fff)
1144
1145static void do_ich9_spi_frap(uint32_t frap, int i)
1146{
Mathias Krausea60faab2011-01-17 07:50:42 +00001147 static const char *const access_names[4] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001148 "locked", "read-only", "write-only", "read-write"
1149 };
Mathias Krausea60faab2011-01-17 07:50:42 +00001150 static const char *const region_names[5] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001151 "Flash Descriptor", "BIOS", "Management Engine",
1152 "Gigabit Ethernet", "Platform Data"
1153 };
1154 uint32_t base, limit;
1155 int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1156 (((ICH_BRRA(frap) >> i) & 1) << 0);
1157 int offset = 0x54 + i * 4;
1158 uint32_t freg = mmio_readl(ich_spibar + offset);
1159
1160 msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n",
1161 offset, freg, i, region_names[i]);
1162
1163 base = ICH_FREG_BASE(freg);
1164 limit = ICH_FREG_LIMIT(freg);
Joshua Roysd172ecd2011-05-26 13:30:51 +00001165 if (base > limit) {
Michael Karchera4448d92010-07-22 18:04:15 +00001166 /* this FREG is disabled */
1167 msg_pdbg("%s region is unused.\n", region_names[i]);
1168 return;
1169 }
1170
1171 msg_pdbg("0x%08x-0x%08x is %s\n",
1172 (base << 12), (limit << 12) | 0x0fff,
1173 access_names[rwperms]);
1174}
1175
Michael Karcherb9dbe482011-05-11 17:07:07 +00001176static const struct spi_programmer spi_programmer_ich7 = {
1177 .type = SPI_CONTROLLER_ICH7,
1178 .max_data_read = 64,
1179 .max_data_write = 64,
1180 .command = ich_spi_send_command,
1181 .multicommand = ich_spi_send_multicommand,
1182 .read = default_spi_read,
1183 .write_256 = default_spi_write_256,
1184};
1185
1186static const struct spi_programmer spi_programmer_ich9 = {
1187 .type = SPI_CONTROLLER_ICH9,
1188 .max_data_read = 64,
1189 .max_data_write = 64,
1190 .command = ich_spi_send_command,
1191 .multicommand = ich_spi_send_multicommand,
1192 .read = default_spi_read,
1193 .write_256 = default_spi_write_256,
1194};
1195
Michael Karchera4448d92010-07-22 18:04:15 +00001196int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb,
1197 int ich_generation)
1198{
1199 int i;
1200 uint8_t old, new;
1201 uint16_t spibar_offset, tmp2;
1202 uint32_t tmp;
1203
Michael Karchera4448d92010-07-22 18:04:15 +00001204 switch (ich_generation) {
1205 case 7:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001206 register_spi_programmer(&spi_programmer_ich7);
Michael Karchera4448d92010-07-22 18:04:15 +00001207 spibar_offset = 0x3020;
1208 break;
1209 case 8:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001210 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001211 spibar_offset = 0x3020;
1212 break;
1213 case 9:
1214 case 10:
1215 default: /* Future version might behave the same */
Michael Karcherb9dbe482011-05-11 17:07:07 +00001216 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001217 spibar_offset = 0x3800;
1218 break;
1219 }
1220
1221 /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
1222 msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset);
1223
1224 /* Assign Virtual Address */
1225 ich_spibar = rcrb + spibar_offset;
1226
Michael Karcherb9dbe482011-05-11 17:07:07 +00001227 switch (spi_programmer->type) {
Michael Karchera4448d92010-07-22 18:04:15 +00001228 case SPI_CONTROLLER_ICH7:
1229 msg_pdbg("0x00: 0x%04x (SPIS)\n",
1230 mmio_readw(ich_spibar + 0));
1231 msg_pdbg("0x02: 0x%04x (SPIC)\n",
1232 mmio_readw(ich_spibar + 2));
1233 msg_pdbg("0x04: 0x%08x (SPIA)\n",
1234 mmio_readl(ich_spibar + 4));
1235 for (i = 0; i < 8; i++) {
1236 int offs;
1237 offs = 8 + (i * 8);
1238 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1239 mmio_readl(ich_spibar + offs), i);
1240 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1241 mmio_readl(ich_spibar + offs + 4), i);
1242 }
1243 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1244 msg_pdbg("0x50: 0x%08x (BBAR)\n",
1245 ichspi_bbar);
1246 msg_pdbg("0x54: 0x%04x (PREOP)\n",
1247 mmio_readw(ich_spibar + 0x54));
1248 msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
1249 mmio_readw(ich_spibar + 0x56));
1250 msg_pdbg("0x58: 0x%08x (OPMENU)\n",
1251 mmio_readl(ich_spibar + 0x58));
1252 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
1253 mmio_readl(ich_spibar + 0x5c));
1254 for (i = 0; i < 4; i++) {
1255 int offs;
1256 offs = 0x60 + (i * 4);
1257 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1258 mmio_readl(ich_spibar + offs), i);
1259 }
Michael Karchera4448d92010-07-22 18:04:15 +00001260 if (mmio_readw(ich_spibar) & (1 << 15)) {
1261 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1262 ichspi_lock = 1;
1263 }
1264 ich_init_opcodes();
1265 break;
1266 case SPI_CONTROLLER_ICH9:
1267 tmp2 = mmio_readw(ich_spibar + 4);
1268 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
Stefan Tauner55206942011-06-11 09:53:22 +00001269 prettyprint_ich9_reg_hsfs(tmp2);
1270 if (tmp2 & (1 << 15)) {
1271 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1272 ichspi_lock = 1;
1273 }
1274
1275 tmp2 = mmio_readw(ich_spibar + 6);
1276 msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
1277 prettyprint_ich9_reg_hsfc(tmp2);
Michael Karchera4448d92010-07-22 18:04:15 +00001278
1279 tmp = mmio_readl(ich_spibar + 0x50);
1280 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1281 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1282 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1283 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1284 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1285
1286 /* print out the FREGx registers along with FRAP access bits */
1287 for(i = 0; i < 5; i++)
1288 do_ich9_spi_frap(tmp, i);
1289
1290 msg_pdbg("0x74: 0x%08x (PR0)\n",
1291 mmio_readl(ich_spibar + 0x74));
1292 msg_pdbg("0x78: 0x%08x (PR1)\n",
1293 mmio_readl(ich_spibar + 0x78));
1294 msg_pdbg("0x7C: 0x%08x (PR2)\n",
1295 mmio_readl(ich_spibar + 0x7C));
1296 msg_pdbg("0x80: 0x%08x (PR3)\n",
1297 mmio_readl(ich_spibar + 0x80));
1298 msg_pdbg("0x84: 0x%08x (PR4)\n",
1299 mmio_readl(ich_spibar + 0x84));
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001300
1301 tmp = mmio_readl(ich_spibar + 0x90);
1302 msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001303 prettyprint_ich9_reg_ssfs(tmp);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001304 if (tmp & (1 << 3)) {
1305 msg_pdbg("Clearing SSFS.FCERR\n");
1306 mmio_writeb(1 << 3, ich_spibar + 0x90);
1307 }
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001308 msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8);
1309 prettyprint_ich9_reg_ssfc(tmp);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001310
Michael Karchera4448d92010-07-22 18:04:15 +00001311 msg_pdbg("0x94: 0x%04x (PREOP)\n",
1312 mmio_readw(ich_spibar + 0x94));
1313 msg_pdbg("0x96: 0x%04x (OPTYPE)\n",
1314 mmio_readw(ich_spibar + 0x96));
1315 msg_pdbg("0x98: 0x%08x (OPMENU)\n",
1316 mmio_readl(ich_spibar + 0x98));
1317 msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
1318 mmio_readl(ich_spibar + 0x9C));
1319 ichspi_bbar = mmio_readl(ich_spibar + 0xA0);
1320 msg_pdbg("0xA0: 0x%08x (BBAR)\n",
1321 ichspi_bbar);
1322 msg_pdbg("0xB0: 0x%08x (FDOC)\n",
1323 mmio_readl(ich_spibar + 0xB0));
Michael Karchera4448d92010-07-22 18:04:15 +00001324 ich_init_opcodes();
1325 break;
1326 default:
1327 /* Nothing */
1328 break;
1329 }
1330
1331 old = pci_read_byte(dev, 0xdc);
1332 msg_pdbg("SPI Read Configuration: ");
1333 new = (old >> 2) & 0x3;
1334 switch (new) {
1335 case 0:
1336 case 1:
1337 case 2:
1338 msg_pdbg("prefetching %sabled, caching %sabled, ",
1339 (new & 0x2) ? "en" : "dis",
1340 (new & 0x1) ? "dis" : "en");
1341 break;
1342 default:
1343 msg_pdbg("invalid prefetching/caching settings, ");
1344 break;
1345 }
1346 return 0;
1347}
1348
Michael Karcherb9dbe482011-05-11 17:07:07 +00001349static const struct spi_programmer spi_programmer_via = {
1350 .type = SPI_CONTROLLER_VIA,
1351 .max_data_read = 16,
1352 .max_data_write = 16,
1353 .command = ich_spi_send_command,
1354 .multicommand = ich_spi_send_multicommand,
1355 .read = default_spi_read,
1356 .write_256 = default_spi_write_256,
1357};
1358
Michael Karchera4448d92010-07-22 18:04:15 +00001359int via_init_spi(struct pci_dev *dev)
1360{
1361 uint32_t mmio_base;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001362 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00001363
1364 mmio_base = (pci_read_long(dev, 0xbc)) << 8;
1365 msg_pdbg("MMIO base at = 0x%x\n", mmio_base);
1366 ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
1367
Michael Karchera4448d92010-07-22 18:04:15 +00001368 /* Not sure if it speaks all these bus protocols. */
Michael Karcherb9dbe482011-05-11 17:07:07 +00001369 buses_supported = CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH;
1370 register_spi_programmer(&spi_programmer_via);
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001371
1372 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1373 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1374 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1375 for (i = 0; i < 2; i++) {
1376 int offs;
1377 offs = 8 + (i * 8);
1378 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1379 mmio_readl(ich_spibar + offs), i);
1380 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1381 mmio_readl(ich_spibar + offs + 4), i);
1382 }
1383 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1384 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1385 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1386 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1387 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1388 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1389 for (i = 0; i < 3; i++) {
1390 int offs;
1391 offs = 0x60 + (i * 4);
1392 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1393 mmio_readl(ich_spibar + offs), i);
1394 }
1395 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
1396 mmio_readw(ich_spibar + 0x6c));
1397 if (mmio_readw(ich_spibar) & (1 << 15)) {
1398 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1399 ichspi_lock = 1;
1400 }
1401
Michael Karchera4448d92010-07-22 18:04:15 +00001402 ich_init_opcodes();
1403
1404 return 0;
1405}
1406
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001407#endif