blob: 8b4210ee3fcddbcf0eeeaad7445c22f3b899d07c [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
Stefan Tauner8b391b82011-08-09 01:49:34 +00009 * Copyright (C) 2011 Stefan Tauner
Dominik Geyerb46acba2008-05-16 12:55:55 +000010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Dominik Geyerb46acba2008-05-16 12:55:55 +000024 */
25
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000026#if defined(__i386__) || defined(__x86_64__)
27
Dominik Geyerb46acba2008-05-16 12:55:55 +000028#include <string.h>
Dominik Geyerb46acba2008-05-16 12:55:55 +000029#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000030#include "programmer.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000031#include "spi.h"
32
Stefan Reinauera9424d52008-06-27 16:28:34 +000033/* ICH9 controller register definition */
Stefan Tauner55206942011-06-11 09:53:22 +000034#define ICH9_REG_HSFS 0x04 /* 16 Bits Hardware Sequencing Flash Status */
35#define HSFS_FDONE_OFF 0 /* 0: Flash Cycle Done */
36#define HSFS_FDONE (0x1 << HSFS_FDONE_OFF)
37#define HSFS_FCERR_OFF 1 /* 1: Flash Cycle Error */
38#define HSFS_FCERR (0x1 << HSFS_FCERR_OFF)
39#define HSFS_AEL_OFF 2 /* 2: Access Error Log */
40#define HSFS_AEL (0x1 << HSFS_AEL_OFF)
41#define HSFS_BERASE_OFF 3 /* 3-4: Block/Sector Erase Size */
42#define HSFS_BERASE (0x3 << HSFS_BERASE_OFF)
43#define HSFS_SCIP_OFF 5 /* 5: SPI Cycle In Progress */
44#define HSFS_SCIP (0x1 << HSFS_SCIP_OFF)
45 /* 6-12: reserved */
46#define HSFS_FDOPSS_OFF 13 /* 13: Flash Descriptor Override Pin-Strap Status */
47#define HSFS_FDOPSS (0x1 << HSFS_FDOPSS_OFF)
48#define HSFS_FDV_OFF 14 /* 14: Flash Descriptor Valid */
49#define HSFS_FDV (0x1 << HSFS_FDV_OFF)
50#define HSFS_FLOCKDN_OFF 15 /* 15: Flash Configuration Lock-Down */
51#define HSFS_FLOCKDN (0x1 << HSFS_FLOCKDN_OFF)
52
53#define ICH9_REG_HSFC 0x06 /* 16 Bits Hardware Sequencing Flash Control */
54#define HSFC_FGO_OFF 0 /* 0: Flash Cycle Go */
55#define HSFC_FGO (0x1 << HSFC_FGO_OFF)
56#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
57#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
58 /* 3-7: reserved */
59#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
60#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
61 /* 14: reserved */
62#define HSFC_SME_OFF 15 /* 15: SPI SMI# Enable */
63#define HSFC_SME (0x1 << HSFC_SME_OFF)
64
Stefan Taunerc0aaf952011-05-19 02:58:17 +000065#define ICH9_REG_FADDR 0x08 /* 32 Bits */
66#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
Stefan Reinauera9424d52008-06-27 16:28:34 +000067
Stefan Tauner29c80832011-06-12 08:14:10 +000068#define ICH9_REG_FRAP 0x50 /* 32 Bytes Flash Region Access Permissions */
69#define ICH9_REG_FREG0 0x54 /* 32 Bytes Flash Region 0 */
70
71#define ICH9_REG_PR0 0x74 /* 32 Bytes Protected Range 0 */
72#define ICH9_REG_PR1 0x78 /* 32 Bytes Protected Range 1 */
73#define ICH9_REG_PR2 0x7c /* 32 Bytes Protected Range 2 */
74#define ICH9_REG_PR3 0x80 /* 32 Bytes Protected Range 3 */
75#define ICH9_REG_PR4 0x84 /* 32 Bytes Protected Range 4 */
76
Stefan Taunerc0aaf952011-05-19 02:58:17 +000077#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Stefan Tauner0c1ec452011-06-11 09:53:09 +000078#define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */
79#define SSFS_SCIP (0x1 << SSFS_SCIP_OFF)
80#define SSFS_FDONE_OFF 2 /* Cycle Done Status */
81#define SSFS_FDONE (0x1 << SSFS_FDONE_OFF)
82#define SSFS_FCERR_OFF 3 /* Flash Cycle Error */
83#define SSFS_FCERR (0x1 << SSFS_FCERR_OFF)
84#define SSFS_AEL_OFF 4 /* Access Error Log */
85#define SSFS_AEL (0x1 << SSFS_AEL_OFF)
Stefan Taunerc0aaf952011-05-19 02:58:17 +000086/* The following bits are reserved in SSFS: 1,5-7. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +000087#define SSFS_RESERVED_MASK 0x000000e2
Stefan Reinauera9424d52008-06-27 16:28:34 +000088
Stefan Taunerc0aaf952011-05-19 02:58:17 +000089#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Stefan Taunerc0aaf952011-05-19 02:58:17 +000090/* We combine SSFS and SSFC to one 32-bit word,
Stefan Tauner0c1ec452011-06-11 09:53:09 +000091 * therefore SSFC bits are off by 8. */
92 /* 0: reserved */
93#define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */
94#define SSFC_SCGO (0x1 << SSFC_SCGO_OFF)
95#define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */
96#define SSFC_ACS (0x1 << SSFC_ACS_OFF)
97#define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */
98#define SSFC_SPOP (0x1 << SSFC_SPOP_OFF)
99#define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */
100#define SSFC_COP (0x7 << SSFC_COP_OFF)
101 /* 7: reserved */
102#define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */
103#define SSFC_DBC (0x3f << SSFC_DBC_OFF)
104#define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */
105#define SSFC_DS (0x1 << SSFC_DS_OFF)
106#define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */
107#define SSFC_SME (0x1 << SSFC_SME_OFF)
108#define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */
109#define SSFC_SCF (0x7 << SSFC_SCF_OFF)
110#define SSFC_SCF_20MHZ 0x00000000
111#define SSFC_SCF_33MHZ 0x01000000
112 /* 19-23: reserved */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000113#define SSFC_RESERVED_MASK 0xf8008100
Stefan Reinauera9424d52008-06-27 16:28:34 +0000114
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000115#define ICH9_REG_PREOP 0x94 /* 16 Bits */
116#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
117#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000118
Stefan Tauner29c80832011-06-12 08:14:10 +0000119#define ICH9_REG_BBAR 0xA0 /* 32 Bits BIOS Base Address Configuration */
120#define BBAR_MASK 0x00ffff00 /* 8-23: Bottom of System Flash */
121
Stefan Taunerbd649e42011-07-01 00:39:16 +0000122#define ICH9_REG_FPB 0xD0 /* 32 Bits Flash Partition Boundary */
123#define FPB_FPBA_OFF 0 /* 0-12: Block/Sector Erase Size */
124#define FPB_FPBA (0x1FFF << FPB_FPBA_OFF)
125
Dominik Geyerb46acba2008-05-16 12:55:55 +0000126// ICH9R SPI commands
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000127#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
128#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
129#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
130#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
Dominik Geyerb46acba2008-05-16 12:55:55 +0000131
Stefan Reinauera9424d52008-06-27 16:28:34 +0000132// ICH7 registers
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000133#define ICH7_REG_SPIS 0x00 /* 16 Bits */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000134#define SPIS_SCIP 0x0001
135#define SPIS_GRANT 0x0002
136#define SPIS_CDS 0x0004
137#define SPIS_FCERR 0x0008
138#define SPIS_RESERVED_MASK 0x7ff0
Stefan Reinauera9424d52008-06-27 16:28:34 +0000139
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000140/* VIA SPI is compatible with ICH7, but maxdata
141 to transfer is 16 bytes.
142
143 DATA byte count on ICH7 is 8:13, on VIA 8:11
144
145 bit 12 is port select CS0 CS1
146 bit 13 is FAST READ enable
147 bit 7 is used with fast read and one shot controls CS de-assert?
148*/
149
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000150#define ICH7_REG_SPIC 0x02 /* 16 Bits */
151#define SPIC_SCGO 0x0002
152#define SPIC_ACS 0x0004
153#define SPIC_SPOP 0x0008
154#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000155
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000156#define ICH7_REG_SPIA 0x04 /* 32 Bits */
157#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
158#define ICH7_REG_PREOP 0x54 /* 16 Bits */
159#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
160#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000161
FENG yu ningc05a2952008-12-08 18:16:58 +0000162/* ICH SPI configuration lock-down. May be set during chipset enabling. */
Michael Karchera4448d92010-07-22 18:04:15 +0000163static int ichspi_lock = 0;
FENG yu ningc05a2952008-12-08 18:16:58 +0000164
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000165uint32_t ichspi_bbar = 0;
166
Michael Karchera4448d92010-07-22 18:04:15 +0000167static void *ich_spibar = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000168
Dominik Geyerb46acba2008-05-16 12:55:55 +0000169typedef struct _OPCODE {
170 uint8_t opcode; //This commands spi opcode
171 uint8_t spi_type; //This commands spi type
172 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
173} OPCODE;
174
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000175/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000176 * Preop 1: Write Enable
177 * Preop 2: Write Status register enable
178 *
179 * OP 0: Write address
180 * OP 1: Read Address
181 * OP 2: ERASE block
182 * OP 3: Read Status register
183 * OP 4: Read ID
184 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000185 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000186 * OP 7: Chip erase
187 */
188typedef struct _OPCODES {
189 uint8_t preop[2];
190 OPCODE opcode[8];
191} OPCODES;
192
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000193static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000194
195/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000196static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000197{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000198 return mmio_readl(ich_spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000199}
200
Uwe Hermann09e04f72009-05-16 22:36:00 +0000201static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000202{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000203 return mmio_readw(ich_spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000204}
205
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000206static uint16_t REGREAD8(int X)
207{
208 return mmio_readb(ich_spibar + X);
209}
210
Stefan Taunerccd92a12011-07-01 00:39:01 +0000211#define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
212#define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
213#define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off))
Dominik Geyerb46acba2008-05-16 12:55:55 +0000214
Dominik Geyerb46acba2008-05-16 12:55:55 +0000215/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000216static int find_opcode(OPCODES *op, uint8_t opcode);
217static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000218static int generate_opcodes(OPCODES * op);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000219static int program_opcodes(OPCODES *op, int enable_undo);
Stefan Reinauer43119562008-11-02 19:51:50 +0000220static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000221 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000222
FENG yu ningf041e9b2008-12-15 02:32:11 +0000223/* for pairing opcodes with their required preop */
224struct preop_opcode_pair {
225 uint8_t preop;
226 uint8_t opcode;
227};
228
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000229/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000230const struct preop_opcode_pair pops[] = {
FENG yu ningf041e9b2008-12-15 02:32:11 +0000231 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
232 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
233 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
234 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
235 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
236 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000237 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
238 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000239 {JEDEC_EWSR, JEDEC_WRSR},
240 {0,}
241};
242
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000243/* Reasonable default configuration. Needs ad-hoc modifications if we
244 * encounter unlisted opcodes. Fun.
245 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000246static OPCODES O_ST_M25P = {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000247 {
248 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000249 JEDEC_EWSR,
250 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000251 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000252 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000253 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000254 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000255 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000256 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000257 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000258 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000259 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
260 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000261};
262
Helge Wagner738e2522010-10-05 22:06:05 +0000263/* List of opcodes with their corresponding spi_type
264 * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
265 * is needed which is currently not in the chipset OPCODE table
266 */
267static OPCODE POSSIBLE_OPCODES[] = {
268 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
269 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
270 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
271 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
272 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
273 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
274 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
275 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
276 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
277 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
278 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
279};
280
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000281static OPCODES O_EXISTING = {};
FENG yu ningc05a2952008-12-08 18:16:58 +0000282
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000283/* pretty printing functions */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000284static void prettyprint_opcodes(OPCODES *ops)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000285{
286 if(ops == NULL)
287 return;
288
289 msg_pdbg("preop0=0x%02x, preop1=0x%02x\n", ops->preop[0],
290 ops->preop[1]);
291
292 OPCODE oc;
293 uint8_t i;
294 for (i = 0; i < 8; i++) {
295 oc = ops->opcode[i];
296 msg_pdbg("op[%d]=0x%02x, %d, %d\n",
297 i,
298 oc.opcode,
299 oc.spi_type,
300 oc.atomic);
301 }
302}
303
304#define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF)
305
Stefan Tauner55206942011-06-11 09:53:22 +0000306static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
307{
308 msg_pdbg("HSFS: ");
309 pprint_reg(HSFS, FDONE, reg_val, ", ");
310 pprint_reg(HSFS, FCERR, reg_val, ", ");
311 pprint_reg(HSFS, AEL, reg_val, ", ");
312 pprint_reg(HSFS, BERASE, reg_val, ", ");
313 pprint_reg(HSFS, SCIP, reg_val, ", ");
314 pprint_reg(HSFS, FDOPSS, reg_val, ", ");
315 pprint_reg(HSFS, FDV, reg_val, ", ");
316 pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
317}
318
319static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
320{
321 msg_pdbg("HSFC: ");
322 pprint_reg(HSFC, FGO, reg_val, ", ");
323 pprint_reg(HSFC, FCYCLE, reg_val, ", ");
324 pprint_reg(HSFC, FDBC, reg_val, ", ");
325 pprint_reg(HSFC, SME, reg_val, "\n");
326}
327
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000328static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
329{
330 msg_pdbg("SSFS: ");
331 pprint_reg(SSFS, SCIP, reg_val, ", ");
332 pprint_reg(SSFS, FDONE, reg_val, ", ");
333 pprint_reg(SSFS, FCERR, reg_val, ", ");
334 pprint_reg(SSFS, AEL, reg_val, "\n");
335}
336
337static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
338{
339 msg_pdbg("SSFC: ");
340 pprint_reg(SSFC, SCGO, reg_val, ", ");
341 pprint_reg(SSFC, ACS, reg_val, ", ");
342 pprint_reg(SSFC, SPOP, reg_val, ", ");
343 pprint_reg(SSFC, COP, reg_val, ", ");
344 pprint_reg(SSFC, DBC, reg_val, ", ");
345 pprint_reg(SSFC, SME, reg_val, ", ");
346 pprint_reg(SSFC, SCF, reg_val, "\n");
347}
348
Helge Wagner738e2522010-10-05 22:06:05 +0000349static uint8_t lookup_spi_type(uint8_t opcode)
350{
351 int a;
352
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000353 for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
Helge Wagner738e2522010-10-05 22:06:05 +0000354 if (POSSIBLE_OPCODES[a].opcode == opcode)
355 return POSSIBLE_OPCODES[a].spi_type;
356 }
357
358 return 0xFF;
359}
360
361static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
362{
363 uint8_t spi_type;
364
365 spi_type = lookup_spi_type(opcode);
366 if (spi_type > 3) {
367 /* Try to guess spi type from read/write sizes.
368 * The following valid writecnt/readcnt combinations exist:
369 * writecnt = 4, readcnt >= 0
370 * writecnt = 1, readcnt >= 0
371 * writecnt >= 4, readcnt = 0
372 * writecnt >= 1, readcnt = 0
373 * writecnt >= 1 is guaranteed for all commands.
374 */
375 if (readcnt == 0)
376 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
377 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
378 * bytes are actual the address, they go to the bus anyhow
379 */
380 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
381 else if (writecnt == 1) // and readcnt is > 0
382 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
383 else if (writecnt == 4) // and readcnt is > 0
384 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
385 // else we have an invalid case, will be handled below
386 }
387 if (spi_type <= 3) {
388 int oppos=2; // use original JEDEC_BE_D8 offset
389 curopcodes->opcode[oppos].opcode = opcode;
390 curopcodes->opcode[oppos].spi_type = spi_type;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000391 program_opcodes(curopcodes, 0);
Helge Wagner738e2522010-10-05 22:06:05 +0000392 oppos = find_opcode(curopcodes, opcode);
393 msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
394 return oppos;
395 }
396 return -1;
397}
398
Uwe Hermann09e04f72009-05-16 22:36:00 +0000399static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000400{
401 int a;
402
403 for (a = 0; a < 8; a++) {
404 if (op->opcode[a].opcode == opcode)
405 return a;
406 }
407
408 return -1;
409}
410
Uwe Hermann09e04f72009-05-16 22:36:00 +0000411static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000412{
413 int a;
414
415 for (a = 0; a < 2; a++) {
416 if (op->preop[a] == preop)
417 return a;
418 }
419
420 return -1;
421}
422
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000423/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000424static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000425{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000426 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000427 uint16_t preop, optype;
428 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000429
430 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000431 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000432 return -1;
433 }
434
Michael Karcherb9dbe482011-05-11 17:07:07 +0000435 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000436 case SPI_CONTROLLER_ICH7:
437 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000438 preop = REGREAD16(ICH7_REG_PREOP);
439 optype = REGREAD16(ICH7_REG_OPTYPE);
440 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
441 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
442 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000443 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000444 preop = REGREAD16(ICH9_REG_PREOP);
445 optype = REGREAD16(ICH9_REG_OPTYPE);
446 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
447 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
448 break;
449 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000450 msg_perr("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000451 return -1;
452 }
453
454 op->preop[0] = (uint8_t) preop;
455 op->preop[1] = (uint8_t) (preop >> 8);
456
457 for (a = 0; a < 8; a++) {
458 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
459 optype >>= 2;
460 }
461
462 for (a = 0; a < 4; a++) {
463 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
464 opmenu[0] >>= 8;
465 }
466
467 for (a = 4; a < 8; a++) {
468 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
469 opmenu[1] >>= 8;
470 }
471
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000472 /* No preopcodes used by default. */
473 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000474 op->opcode[a].atomic = 0;
475
FENG yu ningc05a2952008-12-08 18:16:58 +0000476 return 0;
477}
478
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000479static int program_opcodes(OPCODES *op, int enable_undo)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000480{
481 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000482 uint16_t preop, optype;
483 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000484
485 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000486 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000487 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000488 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000489 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000490
Stefan Reinauera9424d52008-06-27 16:28:34 +0000491 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000492 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000493 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000494 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000495 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000496
Stefan Reinauera9424d52008-06-27 16:28:34 +0000497 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000498 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000499 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000500 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000501 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000502
Dominik Geyerb46acba2008-05-16 12:55:55 +0000503 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000504 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000505 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000506 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000507 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000508
Sean Nelson316a29f2010-05-07 20:09:04 +0000509 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 +0000510 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000511 case SPI_CONTROLLER_ICH7:
512 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000513 /* Register undo only for enable_undo=1, i.e. first call. */
514 if (enable_undo) {
515 rmmio_valw(ich_spibar + ICH7_REG_PREOP);
516 rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
517 rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
518 rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
519 }
520 mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
521 mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
522 mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
523 mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000524 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000525 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000526 /* Register undo only for enable_undo=1, i.e. first call. */
527 if (enable_undo) {
528 rmmio_valw(ich_spibar + ICH9_REG_PREOP);
529 rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
530 rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
531 rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
532 }
533 mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
534 mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
535 mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
536 mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000537 break;
538 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000539 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000540 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000541 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000542
543 return 0;
544}
545
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000546/*
547 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
548 * it didn't stick.
549 */
Stefan Taunere27b2d42011-07-01 00:39:09 +0000550static void ich_set_bbar(uint32_t min_addr)
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000551{
Stefan Taunere27b2d42011-07-01 00:39:09 +0000552 int bbar_off;
Michael Karcherb9dbe482011-05-11 17:07:07 +0000553 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000554 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000555 case SPI_CONTROLLER_VIA:
Stefan Taunere27b2d42011-07-01 00:39:09 +0000556 bbar_off = 0x50;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000557 break;
558 case SPI_CONTROLLER_ICH9:
Stefan Taunere27b2d42011-07-01 00:39:09 +0000559 bbar_off = ICH9_REG_BBAR;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000560 break;
561 default:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000562 msg_perr("Unknown chipset for BBAR setting!\n");
Stefan Taunere27b2d42011-07-01 00:39:09 +0000563 return;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000564 }
Stefan Taunere27b2d42011-07-01 00:39:09 +0000565
566 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
567 if (ichspi_bbar) {
568 msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
569 ichspi_bbar);
570 }
571 min_addr &= BBAR_MASK;
572 ichspi_bbar |= min_addr;
573 rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
574 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
575
576 /* We don't have any option except complaining. And if the write
577 * failed, the restore will fail as well, so no problem there.
578 */
579 if (ichspi_bbar != min_addr)
580 msg_perr("Setting BBAR failed!\n");
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000581}
582
Stefan Tauner8b391b82011-08-09 01:49:34 +0000583/* Read len bytes from the fdata/spid register into the data array.
584 *
585 * Note that using len > spi_programmer->max_data_read will return garbage or
586 * may even crash.
587 */
588 static void ich_read_data(uint8_t *data, int len, int reg0_off)
589 {
590 int i;
591 uint32_t temp32 = 0;
592
593 for (i = 0; i < len; i++) {
594 if ((i % 4) == 0)
595 temp32 = REGREAD32(reg0_off + i);
596
597 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
598 }
599}
600
601/* Fill len bytes from the data array into the fdata/spid registers.
602 *
603 * Note that using len > spi_programmer->max_data_write will trash the registers
604 * following the data registers.
605 */
606static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
607{
608 uint32_t temp32 = 0;
609 int i;
610
611 if (len <= 0)
612 return;
613
614 for (i = 0; i < len; i++) {
615 if ((i % 4) == 0)
616 temp32 = 0;
617
618 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
619
620 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
621 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
622 }
623 i--;
624 if ((i % 4) != 3) /* Write remaining data to regs. */
625 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
626}
627
FENG yu ningf041e9b2008-12-15 02:32:11 +0000628/* This function generates OPCODES from or programs OPCODES to ICH according to
629 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000630 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000631 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000632 */
Michael Karchera4448d92010-07-22 18:04:15 +0000633static int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000634{
635 int rc = 0;
636 OPCODES *curopcodes_done;
637
638 if (curopcodes)
639 return 0;
640
641 if (ichspi_lock) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000642 msg_pdbg("Reading OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000643 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000644 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000645 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000646 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000647 curopcodes_done = &O_ST_M25P;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000648 rc = program_opcodes(curopcodes_done, 1);
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000649 /* Technically not part of opcode init, but it allows opcodes
650 * to run without transaction errors by setting the lowest
651 * allowed address to zero.
652 */
653 ich_set_bbar(0);
FENG yu ningc05a2952008-12-08 18:16:58 +0000654 }
655
656 if (rc) {
657 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000658 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000659 return 1;
660 } else {
661 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000662 msg_pdbg("done\n");
Stefan Tauner8b391b82011-08-09 01:49:34 +0000663 prettyprint_opcodes(curopcodes);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000664 msg_pdbg("\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000665 return 0;
666 }
667}
668
Stefan Reinauer43119562008-11-02 19:51:50 +0000669static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000670 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000671{
672 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000673 int timeout;
Stefan Tauner8b391b82011-08-09 01:49:34 +0000674 uint32_t temp32;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000675 uint16_t temp16;
Stefan Reinauer43119562008-11-02 19:51:50 +0000676 uint64_t opmenu;
677 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000678
679 /* Is it a write command? */
680 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
681 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
682 write_cmd = 1;
683 }
684
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000685 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
686 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
687 programmer_delay(10);
688 }
689 if (!timeout) {
690 msg_perr("Error: SCIP never cleared!\n");
691 return 1;
692 }
693
Stefan Tauner10b3e222011-07-01 00:39:23 +0000694 /* Program offset in flash into SPIA while preserving reserved bits. */
695 temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
696 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000697
Stefan Tauner10b3e222011-07-01 00:39:23 +0000698 /* Program data into SPID0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000699 if (write_cmd && (datalength != 0))
700 ich_fill_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000701
702 /* Assemble SPIS */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000703 temp16 = REGREAD16(ICH7_REG_SPIS);
704 /* keep reserved bits */
705 temp16 &= SPIS_RESERVED_MASK;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000706 /* clear error status registers */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000707 temp16 |= (SPIS_CDS | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000708 REGWRITE16(ICH7_REG_SPIS, temp16);
709
710 /* Assemble SPIC */
711 temp16 = 0;
712
713 if (datalength != 0) {
714 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000715 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000716 }
717
718 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000719 opmenu = REGREAD32(ICH7_REG_OPMENU);
720 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
721
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000722 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
723 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000724 break;
725 }
726 opmenu >>= 8;
727 }
728 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000729 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000730 return 1;
731 }
732 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000733
Michael Karcher136125a2011-04-29 22:11:36 +0000734 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
735 /* Handle Atomic. Atomic commands include three steps:
736 - sending the preop (mainly EWSR or WREN)
737 - sending the main command
738 - waiting for the busy bit (WIP) to be cleared
739 This means the timeout must be sufficient for chip erase
740 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000741 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000742 switch (op.atomic) {
743 case 2:
744 /* Select second preop. */
745 temp16 |= SPIC_SPOP;
746 /* And fall through. */
747 case 1:
748 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000749 temp16 |= SPIC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000750 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000751 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000752 }
753
754 /* Start */
755 temp16 |= SPIC_SCGO;
756
757 /* write it */
758 REGWRITE16(ICH7_REG_SPIC, temp16);
759
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000760 /* Wait for Cycle Done Status or Flash Cycle Error. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000761 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
762 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000763 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000764 }
765 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000766 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
767 REGREAD16(ICH7_REG_SPIS));
768 return 1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000769 }
770
Sean Nelson316a29f2010-05-07 20:09:04 +0000771 /* FIXME: make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000772 temp16 = REGREAD16(ICH7_REG_SPIS);
773 if (temp16 & SPIS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000774 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000775 /* keep reserved bits */
776 temp16 &= SPIS_RESERVED_MASK;
777 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000778 return 1;
779 }
780
Stefan Tauner8b391b82011-08-09 01:49:34 +0000781 if ((!write_cmd) && (datalength != 0))
782 ich_read_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000783
784 return 0;
785}
786
Stefan Reinauer43119562008-11-02 19:51:50 +0000787static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000788 uint8_t datalength, uint8_t * data)
789{
790 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000791 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000792 uint32_t temp32;
Stefan Reinauer43119562008-11-02 19:51:50 +0000793 uint64_t opmenu;
794 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000795
796 /* Is it a write command? */
797 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
798 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
799 write_cmd = 1;
800 }
801
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000802 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
803 while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
804 programmer_delay(10);
805 }
806 if (!timeout) {
807 msg_perr("Error: SCIP never cleared!\n");
808 return 1;
809 }
810
Stefan Tauner10b3e222011-07-01 00:39:23 +0000811 /* Program offset in flash into FADDR while preserve the reserved bits
812 * and clearing the 25. address bit which is only useable in hwseq. */
813 temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
814 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000815
816 /* Program data into FDATA0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000817 if (write_cmd && (datalength != 0))
818 ich_fill_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000819
820 /* Assemble SSFS + SSFC */
Helge Wagnera319be12010-08-11 21:06:10 +0000821 temp32 = REGREAD32(ICH9_REG_SSFS);
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000822 /* Keep reserved bits only */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000823 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000824 /* Clear cycle done and cycle error status registers */
825 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000826 REGWRITE32(ICH9_REG_SSFS, temp32);
827
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000828 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000829 temp32 |= SSFC_SCF_20MHZ;
830
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000831 /* Set data byte count (DBC) and data cycle bit (DS) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000832 if (datalength != 0) {
833 uint32_t datatemp;
834 temp32 |= SSFC_DS;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000835 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
836 SSFC_DBC);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000837 temp32 |= datatemp;
838 }
839
840 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000841 opmenu = REGREAD32(ICH9_REG_OPMENU);
842 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
843
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000844 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
845 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000846 break;
847 }
848 opmenu >>= 8;
849 }
850 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000851 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000852 return 1;
853 }
854 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000855
Michael Karcher136125a2011-04-29 22:11:36 +0000856 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
857 /* Handle Atomic. Atomic commands include three steps:
858 - sending the preop (mainly EWSR or WREN)
859 - sending the main command
860 - waiting for the busy bit (WIP) to be cleared
861 This means the timeout must be sufficient for chip erase
862 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000863 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000864 switch (op.atomic) {
865 case 2:
866 /* Select second preop. */
867 temp32 |= SSFC_SPOP;
868 /* And fall through. */
869 case 1:
870 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000871 temp32 |= SSFC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000872 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000873 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000874 }
875
876 /* Start */
877 temp32 |= SSFC_SCGO;
878
879 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000880 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000881
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000882 /* Wait for Cycle Done Status or Flash Cycle Error. */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000883 while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000884 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000885 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000886 }
887 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000888 msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
889 REGREAD32(ICH9_REG_SSFS));
890 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000891 }
892
Sean Nelson316a29f2010-05-07 20:09:04 +0000893 /* FIXME make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000894 temp32 = REGREAD32(ICH9_REG_SSFS);
895 if (temp32 & SSFS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000896 msg_perr("Transaction error!\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000897 prettyprint_ich9_reg_ssfs(temp32);
898 prettyprint_ich9_reg_ssfc(temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000899 /* keep reserved bits */
900 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
901 /* Clear the transaction error. */
902 REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000903 return 1;
904 }
905
Stefan Tauner8b391b82011-08-09 01:49:34 +0000906 if ((!write_cmd) && (datalength != 0))
907 ich_read_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000908
909 return 0;
910}
911
Stefan Reinauer43119562008-11-02 19:51:50 +0000912static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000913 uint8_t datalength, uint8_t * data)
914{
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000915 /* max_data_read == max_data_write for all Intel/VIA SPI masters */
916 uint8_t maxlength = spi_programmer->max_data_read;
917
918 if (spi_programmer->type == SPI_CONTROLLER_NONE) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000919 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000920 return -1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000921 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000922
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000923 if (datalength > maxlength) {
924 msg_perr("%s: Internal command size error for "
925 "opcode 0x%02x, got datalength=%i, want <=%i\n",
926 __func__, op.opcode, datalength, maxlength);
927 return SPI_INVALID_LENGTH;
928 }
929
930 switch (spi_programmer->type) {
931 case SPI_CONTROLLER_VIA:
932 case SPI_CONTROLLER_ICH7:
933 return ich7_run_opcode(op, offset, datalength, data, maxlength);
934 case SPI_CONTROLLER_ICH9:
935 return ich9_run_opcode(op, offset, datalength, data);
936 default:
937 /* If we ever get here, something really weird happened */
938 return -1;
939 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000940}
941
Michael Karcherb9dbe482011-05-11 17:07:07 +0000942static int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000943 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000944{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000945 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000946 int opcode_index = -1;
947 const unsigned char cmd = *writearr;
948 OPCODE *opcode;
949 uint32_t addr = 0;
950 uint8_t *data;
951 int count;
952
Dominik Geyerb46acba2008-05-16 12:55:55 +0000953 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000954 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000955 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +0000956 if (!ichspi_lock)
957 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
958 if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000959 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
960 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +0000961 return SPI_INVALID_OPCODE;
962 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000963 }
964
965 opcode = &(curopcodes->opcode[opcode_index]);
966
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000967 /* The following valid writecnt/readcnt combinations exist:
968 * writecnt = 4, readcnt >= 0
969 * writecnt = 1, readcnt >= 0
970 * writecnt >= 4, readcnt = 0
971 * writecnt >= 1, readcnt = 0
972 * writecnt >= 1 is guaranteed for all commands.
973 */
974 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
975 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000976 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000977 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
978 writecnt);
979 return SPI_INVALID_LENGTH;
980 }
981 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
982 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000983 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000984 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
985 writecnt);
986 return SPI_INVALID_LENGTH;
987 }
988 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
989 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000990 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000991 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
992 writecnt);
993 return SPI_INVALID_LENGTH;
994 }
995 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
996 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
997 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000998 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000999 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1000 readcnt);
1001 return SPI_INVALID_LENGTH;
1002 }
1003
Dominik Geyerb46acba2008-05-16 12:55:55 +00001004 /* if opcode-type requires an address */
1005 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1006 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001007 addr = (writearr[1] << 16) |
1008 (writearr[2] << 8) | (writearr[3] << 0);
Michael Karcherb9dbe482011-05-11 17:07:07 +00001009 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001010 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001011 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001012 case SPI_CONTROLLER_ICH9:
1013 if (addr < ichspi_bbar) {
1014 msg_perr("%s: Address 0x%06x below allowed "
1015 "range 0x%06x-0xffffff\n", __func__,
1016 addr, ichspi_bbar);
1017 return SPI_INVALID_ADDRESS;
1018 }
1019 break;
1020 default:
1021 break;
1022 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001023 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001024
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001025 /* Translate read/write array/count.
1026 * The maximum data length is identical for the maximum read length and
1027 * for the maximum write length excluding opcode and address. Opcode and
1028 * address are stored in separate registers, not in the data registers
1029 * and are thus not counted towards data length. The only exception
1030 * applies if the opcode definition (un)intentionally classifies said
1031 * opcode incorrectly as non-address opcode or vice versa. */
Dominik Geyerb46acba2008-05-16 12:55:55 +00001032 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001033 data = (uint8_t *) (writearr + 1);
1034 count = writecnt - 1;
1035 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1036 data = (uint8_t *) (writearr + 4);
1037 count = writecnt - 4;
1038 } else {
1039 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001040 count = readcnt;
1041 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001042
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001043 result = run_opcode(*opcode, addr, count, data);
1044 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001045 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1046 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1047 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1048 msg_pdbg("at address 0x%06x ", addr);
1049 }
1050 msg_pdbg("(payload length was %d).\n", count);
1051
1052 /* Print out the data array if it contains data to write.
1053 * Errors are detected before the received data is read back into
1054 * the array so it won't make sense to print it then. */
1055 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1056 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1057 int i;
1058 msg_pspew("The data was:\n");
1059 for(i=0; i<count; i++){
1060 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1061 }
1062 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001063 }
1064
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001065 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001066}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001067
Michael Karcherb9dbe482011-05-11 17:07:07 +00001068static int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001069{
1070 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001071 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001072 int oppos, preoppos;
1073 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001074 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001075 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001076 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1077 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001078 if ((oppos == -1) && (preoppos != -1)) {
1079 /* Current command is listed as preopcode in
1080 * ICH struct OPCODES, but next command is not
1081 * listed as opcode in that struct.
1082 * Check for command sanity, then
1083 * try to reprogram the ICH opcode list.
1084 */
1085 if (find_preop(curopcodes,
1086 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001087 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001088 "preopcodes 0x%02x and 0x%02x, "
1089 "ignoring the first.\n",
1090 __func__, cmds->writearr[0],
1091 (cmds + 1)->writearr[0]);
1092 continue;
1093 }
1094 /* If the chipset is locked down, we'll fail
1095 * during execution of the next command anyway.
1096 * No need to bother with fixups.
1097 */
1098 if (!ichspi_lock) {
Helge Wagner738e2522010-10-05 22:06:05 +00001099 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1100 if (oppos == -1)
1101 continue;
1102 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001103 continue;
1104 }
1105 }
1106 if ((oppos != -1) && (preoppos != -1)) {
1107 /* Current command is listed as preopcode in
1108 * ICH struct OPCODES and next command is listed
1109 * as opcode in that struct. Match them up.
1110 */
1111 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001112 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001113 }
1114 /* If none of the above if-statements about oppos or
1115 * preoppos matched, this is a normal opcode.
1116 */
1117 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001118 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
1119 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001120 /* Reset the type of all opcodes to non-atomic. */
1121 for (i = 0; i < 8; i++)
1122 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001123 }
1124 return ret;
1125}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001126
Michael Karchera4448d92010-07-22 18:04:15 +00001127#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1128#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1129#define ICH_BRWA(x) ((x >> 8) & 0xff)
1130#define ICH_BRRA(x) ((x >> 0) & 0xff)
1131
1132#define ICH_FREG_BASE(x) ((x >> 0) & 0x1fff)
1133#define ICH_FREG_LIMIT(x) ((x >> 16) & 0x1fff)
1134
1135static void do_ich9_spi_frap(uint32_t frap, int i)
1136{
Mathias Krausea60faab2011-01-17 07:50:42 +00001137 static const char *const access_names[4] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001138 "locked", "read-only", "write-only", "read-write"
1139 };
Mathias Krausea60faab2011-01-17 07:50:42 +00001140 static const char *const region_names[5] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001141 "Flash Descriptor", "BIOS", "Management Engine",
1142 "Gigabit Ethernet", "Platform Data"
1143 };
1144 uint32_t base, limit;
1145 int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1146 (((ICH_BRRA(frap) >> i) & 1) << 0);
Stefan Tauner29c80832011-06-12 08:14:10 +00001147 int offset = ICH9_REG_FREG0 + i * 4;
Michael Karchera4448d92010-07-22 18:04:15 +00001148 uint32_t freg = mmio_readl(ich_spibar + offset);
1149
1150 msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n",
1151 offset, freg, i, region_names[i]);
1152
1153 base = ICH_FREG_BASE(freg);
1154 limit = ICH_FREG_LIMIT(freg);
Joshua Roysd172ecd2011-05-26 13:30:51 +00001155 if (base > limit) {
Michael Karchera4448d92010-07-22 18:04:15 +00001156 /* this FREG is disabled */
1157 msg_pdbg("%s region is unused.\n", region_names[i]);
1158 return;
1159 }
1160
1161 msg_pdbg("0x%08x-0x%08x is %s\n",
1162 (base << 12), (limit << 12) | 0x0fff,
1163 access_names[rwperms]);
1164}
1165
Michael Karcherb9dbe482011-05-11 17:07:07 +00001166static const struct spi_programmer spi_programmer_ich7 = {
1167 .type = SPI_CONTROLLER_ICH7,
1168 .max_data_read = 64,
1169 .max_data_write = 64,
1170 .command = ich_spi_send_command,
1171 .multicommand = ich_spi_send_multicommand,
1172 .read = default_spi_read,
1173 .write_256 = default_spi_write_256,
1174};
1175
1176static const struct spi_programmer spi_programmer_ich9 = {
1177 .type = SPI_CONTROLLER_ICH9,
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
Michael Karchera4448d92010-07-22 18:04:15 +00001186int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb,
1187 int ich_generation)
1188{
1189 int i;
1190 uint8_t old, new;
1191 uint16_t spibar_offset, tmp2;
1192 uint32_t tmp;
1193
Michael Karchera4448d92010-07-22 18:04:15 +00001194 switch (ich_generation) {
1195 case 7:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001196 register_spi_programmer(&spi_programmer_ich7);
Michael Karchera4448d92010-07-22 18:04:15 +00001197 spibar_offset = 0x3020;
1198 break;
1199 case 8:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001200 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001201 spibar_offset = 0x3020;
1202 break;
1203 case 9:
1204 case 10:
1205 default: /* Future version might behave the same */
Michael Karcherb9dbe482011-05-11 17:07:07 +00001206 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001207 spibar_offset = 0x3800;
1208 break;
1209 }
1210
1211 /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
1212 msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset);
1213
1214 /* Assign Virtual Address */
1215 ich_spibar = rcrb + spibar_offset;
1216
Michael Karcherb9dbe482011-05-11 17:07:07 +00001217 switch (spi_programmer->type) {
Michael Karchera4448d92010-07-22 18:04:15 +00001218 case SPI_CONTROLLER_ICH7:
1219 msg_pdbg("0x00: 0x%04x (SPIS)\n",
1220 mmio_readw(ich_spibar + 0));
1221 msg_pdbg("0x02: 0x%04x (SPIC)\n",
1222 mmio_readw(ich_spibar + 2));
1223 msg_pdbg("0x04: 0x%08x (SPIA)\n",
1224 mmio_readl(ich_spibar + 4));
1225 for (i = 0; i < 8; i++) {
1226 int offs;
1227 offs = 8 + (i * 8);
1228 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1229 mmio_readl(ich_spibar + offs), i);
1230 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1231 mmio_readl(ich_spibar + offs + 4), i);
1232 }
1233 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1234 msg_pdbg("0x50: 0x%08x (BBAR)\n",
1235 ichspi_bbar);
1236 msg_pdbg("0x54: 0x%04x (PREOP)\n",
1237 mmio_readw(ich_spibar + 0x54));
1238 msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
1239 mmio_readw(ich_spibar + 0x56));
1240 msg_pdbg("0x58: 0x%08x (OPMENU)\n",
1241 mmio_readl(ich_spibar + 0x58));
1242 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
1243 mmio_readl(ich_spibar + 0x5c));
Stefan Tauner122dd122011-07-24 15:34:56 +00001244 for (i = 0; i < 3; i++) {
Michael Karchera4448d92010-07-22 18:04:15 +00001245 int offs;
1246 offs = 0x60 + (i * 4);
1247 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1248 mmio_readl(ich_spibar + offs), i);
1249 }
Michael Karchera4448d92010-07-22 18:04:15 +00001250 if (mmio_readw(ich_spibar) & (1 << 15)) {
1251 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1252 ichspi_lock = 1;
1253 }
1254 ich_init_opcodes();
1255 break;
1256 case SPI_CONTROLLER_ICH9:
Stefan Tauner29c80832011-06-12 08:14:10 +00001257 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
Michael Karchera4448d92010-07-22 18:04:15 +00001258 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
Stefan Tauner55206942011-06-11 09:53:22 +00001259 prettyprint_ich9_reg_hsfs(tmp2);
Stefan Tauner29c80832011-06-12 08:14:10 +00001260 if (tmp2 & HSFS_FLOCKDN) {
Stefan Tauner55206942011-06-11 09:53:22 +00001261 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1262 ichspi_lock = 1;
1263 }
1264
Stefan Tauner29c80832011-06-12 08:14:10 +00001265 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
Stefan Tauner55206942011-06-11 09:53:22 +00001266 msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
1267 prettyprint_ich9_reg_hsfc(tmp2);
Michael Karchera4448d92010-07-22 18:04:15 +00001268
Stefan Tauner5ffe65b2011-07-07 04:10:57 +00001269 tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
1270 msg_pdbg("0x08: 0x%08x (FADDR)\n", tmp);
Stefan Tauner29c80832011-06-12 08:14:10 +00001271 tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
Michael Karchera4448d92010-07-22 18:04:15 +00001272 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1273 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1274 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1275 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1276 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1277
1278 /* print out the FREGx registers along with FRAP access bits */
1279 for(i = 0; i < 5; i++)
1280 do_ich9_spi_frap(tmp, i);
1281
1282 msg_pdbg("0x74: 0x%08x (PR0)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001283 mmio_readl(ich_spibar + ICH9_REG_PR0));
Michael Karchera4448d92010-07-22 18:04:15 +00001284 msg_pdbg("0x78: 0x%08x (PR1)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001285 mmio_readl(ich_spibar + ICH9_REG_PR1));
Michael Karchera4448d92010-07-22 18:04:15 +00001286 msg_pdbg("0x7C: 0x%08x (PR2)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001287 mmio_readl(ich_spibar + ICH9_REG_PR2));
Michael Karchera4448d92010-07-22 18:04:15 +00001288 msg_pdbg("0x80: 0x%08x (PR3)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001289 mmio_readl(ich_spibar + ICH9_REG_PR3));
Michael Karchera4448d92010-07-22 18:04:15 +00001290 msg_pdbg("0x84: 0x%08x (PR4)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001291 mmio_readl(ich_spibar + ICH9_REG_PR4));
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001292
Stefan Tauner29c80832011-06-12 08:14:10 +00001293 tmp = mmio_readl(ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001294 msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001295 prettyprint_ich9_reg_ssfs(tmp);
Stefan Tauner29c80832011-06-12 08:14:10 +00001296 if (tmp & SSFS_FCERR) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001297 msg_pdbg("Clearing SSFS.FCERR\n");
Stefan Tauner29c80832011-06-12 08:14:10 +00001298 mmio_writeb(SSFS_FCERR, ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001299 }
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001300 msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8);
1301 prettyprint_ich9_reg_ssfc(tmp);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001302
Michael Karchera4448d92010-07-22 18:04:15 +00001303 msg_pdbg("0x94: 0x%04x (PREOP)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001304 mmio_readw(ich_spibar + ICH9_REG_PREOP));
Michael Karchera4448d92010-07-22 18:04:15 +00001305 msg_pdbg("0x96: 0x%04x (OPTYPE)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001306 mmio_readw(ich_spibar + ICH9_REG_OPTYPE));
Michael Karchera4448d92010-07-22 18:04:15 +00001307 msg_pdbg("0x98: 0x%08x (OPMENU)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001308 mmio_readl(ich_spibar + ICH9_REG_OPMENU));
Michael Karchera4448d92010-07-22 18:04:15 +00001309 msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001310 mmio_readl(ich_spibar + ICH9_REG_OPMENU + 4));
1311 ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
Michael Karchera4448d92010-07-22 18:04:15 +00001312 msg_pdbg("0xA0: 0x%08x (BBAR)\n",
1313 ichspi_bbar);
Stefan Taunerbd649e42011-07-01 00:39:16 +00001314 tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
1315 msg_pdbg("0xD0: 0x%08x (FPB)\n", tmp);
1316
Michael Karchera4448d92010-07-22 18:04:15 +00001317 ich_init_opcodes();
1318 break;
1319 default:
1320 /* Nothing */
1321 break;
1322 }
1323
1324 old = pci_read_byte(dev, 0xdc);
1325 msg_pdbg("SPI Read Configuration: ");
1326 new = (old >> 2) & 0x3;
1327 switch (new) {
1328 case 0:
1329 case 1:
1330 case 2:
1331 msg_pdbg("prefetching %sabled, caching %sabled, ",
1332 (new & 0x2) ? "en" : "dis",
1333 (new & 0x1) ? "dis" : "en");
1334 break;
1335 default:
1336 msg_pdbg("invalid prefetching/caching settings, ");
1337 break;
1338 }
1339 return 0;
1340}
1341
Michael Karcherb9dbe482011-05-11 17:07:07 +00001342static const struct spi_programmer spi_programmer_via = {
1343 .type = SPI_CONTROLLER_VIA,
1344 .max_data_read = 16,
1345 .max_data_write = 16,
1346 .command = ich_spi_send_command,
1347 .multicommand = ich_spi_send_multicommand,
1348 .read = default_spi_read,
1349 .write_256 = default_spi_write_256,
1350};
1351
Michael Karchera4448d92010-07-22 18:04:15 +00001352int via_init_spi(struct pci_dev *dev)
1353{
1354 uint32_t mmio_base;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001355 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00001356
1357 mmio_base = (pci_read_long(dev, 0xbc)) << 8;
1358 msg_pdbg("MMIO base at = 0x%x\n", mmio_base);
1359 ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
1360
Michael Karchera4448d92010-07-22 18:04:15 +00001361 /* Not sure if it speaks all these bus protocols. */
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001362 buses_supported = BUS_LPC | BUS_FWH;
Michael Karcherb9dbe482011-05-11 17:07:07 +00001363 register_spi_programmer(&spi_programmer_via);
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001364
1365 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1366 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1367 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1368 for (i = 0; i < 2; i++) {
1369 int offs;
1370 offs = 8 + (i * 8);
1371 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1372 mmio_readl(ich_spibar + offs), i);
1373 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1374 mmio_readl(ich_spibar + offs + 4), i);
1375 }
1376 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1377 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1378 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1379 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1380 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1381 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1382 for (i = 0; i < 3; i++) {
1383 int offs;
1384 offs = 0x60 + (i * 4);
1385 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1386 mmio_readl(ich_spibar + offs), i);
1387 }
1388 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
1389 mmio_readw(ich_spibar + 0x6c));
1390 if (mmio_readw(ich_spibar) & (1 << 15)) {
1391 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1392 ichspi_lock = 1;
1393 }
1394
Michael Karchera4448d92010-07-22 18:04:15 +00001395 ich_init_opcodes();
1396
1397 return 0;
1398}
1399
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001400#endif