blob: 3bc43f5e1f2c15da16d338ac7d46ffab54c7c377 [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"
Stefan Tauner1e146392011-09-15 23:52:55 +000032#include "ich_descriptors.h"
Dominik Geyerb46acba2008-05-16 12:55:55 +000033
Stefan Reinauera9424d52008-06-27 16:28:34 +000034/* ICH9 controller register definition */
Stefan Tauner55206942011-06-11 09:53:22 +000035#define ICH9_REG_HSFS 0x04 /* 16 Bits Hardware Sequencing Flash Status */
36#define HSFS_FDONE_OFF 0 /* 0: Flash Cycle Done */
37#define HSFS_FDONE (0x1 << HSFS_FDONE_OFF)
38#define HSFS_FCERR_OFF 1 /* 1: Flash Cycle Error */
39#define HSFS_FCERR (0x1 << HSFS_FCERR_OFF)
40#define HSFS_AEL_OFF 2 /* 2: Access Error Log */
41#define HSFS_AEL (0x1 << HSFS_AEL_OFF)
42#define HSFS_BERASE_OFF 3 /* 3-4: Block/Sector Erase Size */
43#define HSFS_BERASE (0x3 << HSFS_BERASE_OFF)
44#define HSFS_SCIP_OFF 5 /* 5: SPI Cycle In Progress */
45#define HSFS_SCIP (0x1 << HSFS_SCIP_OFF)
46 /* 6-12: reserved */
47#define HSFS_FDOPSS_OFF 13 /* 13: Flash Descriptor Override Pin-Strap Status */
48#define HSFS_FDOPSS (0x1 << HSFS_FDOPSS_OFF)
49#define HSFS_FDV_OFF 14 /* 14: Flash Descriptor Valid */
50#define HSFS_FDV (0x1 << HSFS_FDV_OFF)
51#define HSFS_FLOCKDN_OFF 15 /* 15: Flash Configuration Lock-Down */
52#define HSFS_FLOCKDN (0x1 << HSFS_FLOCKDN_OFF)
53
54#define ICH9_REG_HSFC 0x06 /* 16 Bits Hardware Sequencing Flash Control */
55#define HSFC_FGO_OFF 0 /* 0: Flash Cycle Go */
56#define HSFC_FGO (0x1 << HSFC_FGO_OFF)
57#define HSFC_FCYCLE_OFF 1 /* 1-2: FLASH Cycle */
58#define HSFC_FCYCLE (0x3 << HSFC_FCYCLE_OFF)
59 /* 3-7: reserved */
60#define HSFC_FDBC_OFF 8 /* 8-13: Flash Data Byte Count */
61#define HSFC_FDBC (0x3f << HSFC_FDBC_OFF)
62 /* 14: reserved */
63#define HSFC_SME_OFF 15 /* 15: SPI SMI# Enable */
64#define HSFC_SME (0x1 << HSFC_SME_OFF)
65
Stefan Taunerc0aaf952011-05-19 02:58:17 +000066#define ICH9_REG_FADDR 0x08 /* 32 Bits */
67#define ICH9_REG_FDATA0 0x10 /* 64 Bytes */
Stefan Reinauera9424d52008-06-27 16:28:34 +000068
Stefan Tauner29c80832011-06-12 08:14:10 +000069#define ICH9_REG_FRAP 0x50 /* 32 Bytes Flash Region Access Permissions */
70#define ICH9_REG_FREG0 0x54 /* 32 Bytes Flash Region 0 */
71
72#define ICH9_REG_PR0 0x74 /* 32 Bytes Protected Range 0 */
73#define ICH9_REG_PR1 0x78 /* 32 Bytes Protected Range 1 */
74#define ICH9_REG_PR2 0x7c /* 32 Bytes Protected Range 2 */
75#define ICH9_REG_PR3 0x80 /* 32 Bytes Protected Range 3 */
76#define ICH9_REG_PR4 0x84 /* 32 Bytes Protected Range 4 */
77
Stefan Taunerc0aaf952011-05-19 02:58:17 +000078#define ICH9_REG_SSFS 0x90 /* 08 Bits */
Stefan Tauner0c1ec452011-06-11 09:53:09 +000079#define SSFS_SCIP_OFF 0 /* SPI Cycle In Progress */
80#define SSFS_SCIP (0x1 << SSFS_SCIP_OFF)
81#define SSFS_FDONE_OFF 2 /* Cycle Done Status */
82#define SSFS_FDONE (0x1 << SSFS_FDONE_OFF)
83#define SSFS_FCERR_OFF 3 /* Flash Cycle Error */
84#define SSFS_FCERR (0x1 << SSFS_FCERR_OFF)
85#define SSFS_AEL_OFF 4 /* Access Error Log */
86#define SSFS_AEL (0x1 << SSFS_AEL_OFF)
Stefan Taunerc0aaf952011-05-19 02:58:17 +000087/* The following bits are reserved in SSFS: 1,5-7. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +000088#define SSFS_RESERVED_MASK 0x000000e2
Stefan Reinauera9424d52008-06-27 16:28:34 +000089
Stefan Taunerc0aaf952011-05-19 02:58:17 +000090#define ICH9_REG_SSFC 0x91 /* 24 Bits */
Stefan Taunerc0aaf952011-05-19 02:58:17 +000091/* We combine SSFS and SSFC to one 32-bit word,
Stefan Tauner0c1ec452011-06-11 09:53:09 +000092 * therefore SSFC bits are off by 8. */
93 /* 0: reserved */
94#define SSFC_SCGO_OFF (1 + 8) /* 1: SPI Cycle Go */
95#define SSFC_SCGO (0x1 << SSFC_SCGO_OFF)
96#define SSFC_ACS_OFF (2 + 8) /* 2: Atomic Cycle Sequence */
97#define SSFC_ACS (0x1 << SSFC_ACS_OFF)
98#define SSFC_SPOP_OFF (3 + 8) /* 3: Sequence Prefix Opcode Pointer */
99#define SSFC_SPOP (0x1 << SSFC_SPOP_OFF)
100#define SSFC_COP_OFF (4 + 8) /* 4-6: Cycle Opcode Pointer */
101#define SSFC_COP (0x7 << SSFC_COP_OFF)
102 /* 7: reserved */
103#define SSFC_DBC_OFF (8 + 8) /* 8-13: Data Byte Count */
104#define SSFC_DBC (0x3f << SSFC_DBC_OFF)
105#define SSFC_DS_OFF (14 + 8) /* 14: Data Cycle */
106#define SSFC_DS (0x1 << SSFC_DS_OFF)
107#define SSFC_SME_OFF (15 + 8) /* 15: SPI SMI# Enable */
108#define SSFC_SME (0x1 << SSFC_SME_OFF)
109#define SSFC_SCF_OFF (16 + 8) /* 16-18: SPI Cycle Frequency */
110#define SSFC_SCF (0x7 << SSFC_SCF_OFF)
111#define SSFC_SCF_20MHZ 0x00000000
112#define SSFC_SCF_33MHZ 0x01000000
113 /* 19-23: reserved */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000114#define SSFC_RESERVED_MASK 0xf8008100
Stefan Reinauera9424d52008-06-27 16:28:34 +0000115
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000116#define ICH9_REG_PREOP 0x94 /* 16 Bits */
117#define ICH9_REG_OPTYPE 0x96 /* 16 Bits */
118#define ICH9_REG_OPMENU 0x98 /* 64 Bits */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000119
Stefan Tauner29c80832011-06-12 08:14:10 +0000120#define ICH9_REG_BBAR 0xA0 /* 32 Bits BIOS Base Address Configuration */
121#define BBAR_MASK 0x00ffff00 /* 8-23: Bottom of System Flash */
122
Stefan Tauner1e146392011-09-15 23:52:55 +0000123#define ICH8_REG_VSCC 0xC1 /* 32 Bits Vendor Specific Component Capabilities */
124#define ICH9_REG_LVSCC 0xC4 /* 32 Bits Host Lower Vendor Specific Component Capabilities */
125#define ICH9_REG_UVSCC 0xC8 /* 32 Bits Host Upper Vendor Specific Component Capabilities */
126/* The individual fields of the VSCC registers are defined in the file
127 * ich_descriptors.h. The reason is that the same layout is also used in the
128 * flash descriptor to define the properties of the different flash chips
129 * supported. The BIOS (or the ME?) is responsible to populate the ICH registers
130 * with the information from the descriptor on startup depending on the actual
131 * chip(s) detected. */
132
Stefan Taunerbd649e42011-07-01 00:39:16 +0000133#define ICH9_REG_FPB 0xD0 /* 32 Bits Flash Partition Boundary */
134#define FPB_FPBA_OFF 0 /* 0-12: Block/Sector Erase Size */
135#define FPB_FPBA (0x1FFF << FPB_FPBA_OFF)
136
Dominik Geyerb46acba2008-05-16 12:55:55 +0000137// ICH9R SPI commands
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000138#define SPI_OPCODE_TYPE_READ_NO_ADDRESS 0
139#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS 1
140#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS 2
141#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS 3
Dominik Geyerb46acba2008-05-16 12:55:55 +0000142
Stefan Reinauera9424d52008-06-27 16:28:34 +0000143// ICH7 registers
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000144#define ICH7_REG_SPIS 0x00 /* 16 Bits */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000145#define SPIS_SCIP 0x0001
146#define SPIS_GRANT 0x0002
147#define SPIS_CDS 0x0004
148#define SPIS_FCERR 0x0008
149#define SPIS_RESERVED_MASK 0x7ff0
Stefan Reinauera9424d52008-06-27 16:28:34 +0000150
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000151/* VIA SPI is compatible with ICH7, but maxdata
152 to transfer is 16 bytes.
153
154 DATA byte count on ICH7 is 8:13, on VIA 8:11
155
156 bit 12 is port select CS0 CS1
157 bit 13 is FAST READ enable
158 bit 7 is used with fast read and one shot controls CS de-assert?
159*/
160
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000161#define ICH7_REG_SPIC 0x02 /* 16 Bits */
162#define SPIC_SCGO 0x0002
163#define SPIC_ACS 0x0004
164#define SPIC_SPOP 0x0008
165#define SPIC_DS 0x4000
Stefan Reinauera9424d52008-06-27 16:28:34 +0000166
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000167#define ICH7_REG_SPIA 0x04 /* 32 Bits */
168#define ICH7_REG_SPID0 0x08 /* 64 Bytes */
169#define ICH7_REG_PREOP 0x54 /* 16 Bits */
170#define ICH7_REG_OPTYPE 0x56 /* 16 Bits */
171#define ICH7_REG_OPMENU 0x58 /* 64 Bits */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000172
FENG yu ningc05a2952008-12-08 18:16:58 +0000173/* ICH SPI configuration lock-down. May be set during chipset enabling. */
Michael Karchera4448d92010-07-22 18:04:15 +0000174static int ichspi_lock = 0;
FENG yu ningc05a2952008-12-08 18:16:58 +0000175
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000176uint32_t ichspi_bbar = 0;
177
Michael Karchera4448d92010-07-22 18:04:15 +0000178static void *ich_spibar = NULL;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000179
Dominik Geyerb46acba2008-05-16 12:55:55 +0000180typedef struct _OPCODE {
181 uint8_t opcode; //This commands spi opcode
182 uint8_t spi_type; //This commands spi type
183 uint8_t atomic; //Use preop: (0: none, 1: preop0, 2: preop1
184} OPCODE;
185
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000186/* Suggested opcode definition:
Dominik Geyerb46acba2008-05-16 12:55:55 +0000187 * Preop 1: Write Enable
188 * Preop 2: Write Status register enable
189 *
190 * OP 0: Write address
191 * OP 1: Read Address
192 * OP 2: ERASE block
193 * OP 3: Read Status register
194 * OP 4: Read ID
195 * OP 5: Write Status register
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000196 * OP 6: chip private (read JEDEC id)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000197 * OP 7: Chip erase
198 */
199typedef struct _OPCODES {
200 uint8_t preop[2];
201 OPCODE opcode[8];
202} OPCODES;
203
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000204static OPCODES *curopcodes = NULL;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000205
206/* HW access functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000207static uint32_t REGREAD32(int X)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000208{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000209 return mmio_readl(ich_spibar + X);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000210}
211
Uwe Hermann09e04f72009-05-16 22:36:00 +0000212static uint16_t REGREAD16(int X)
Stefan Reinauera9424d52008-06-27 16:28:34 +0000213{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000214 return mmio_readw(ich_spibar + X);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000215}
216
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000217static uint16_t REGREAD8(int X)
218{
219 return mmio_readb(ich_spibar + X);
220}
221
Stefan Taunerccd92a12011-07-01 00:39:01 +0000222#define REGWRITE32(off, val) mmio_writel(val, ich_spibar+(off))
223#define REGWRITE16(off, val) mmio_writew(val, ich_spibar+(off))
224#define REGWRITE8(off, val) mmio_writeb(val, ich_spibar+(off))
Dominik Geyerb46acba2008-05-16 12:55:55 +0000225
Dominik Geyerb46acba2008-05-16 12:55:55 +0000226/* Common SPI functions */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000227static int find_opcode(OPCODES *op, uint8_t opcode);
228static int find_preop(OPCODES *op, uint8_t preop);
FENG yu ningf041e9b2008-12-15 02:32:11 +0000229static int generate_opcodes(OPCODES * op);
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000230static int program_opcodes(OPCODES *op, int enable_undo);
Stefan Reinauer43119562008-11-02 19:51:50 +0000231static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000232 uint8_t datalength, uint8_t * data);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000233
FENG yu ningf041e9b2008-12-15 02:32:11 +0000234/* for pairing opcodes with their required preop */
235struct preop_opcode_pair {
236 uint8_t preop;
237 uint8_t opcode;
238};
239
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000240/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000241const struct preop_opcode_pair pops[] = {
FENG yu ningf041e9b2008-12-15 02:32:11 +0000242 {JEDEC_WREN, JEDEC_BYTE_PROGRAM},
243 {JEDEC_WREN, JEDEC_SE}, /* sector erase */
244 {JEDEC_WREN, JEDEC_BE_52}, /* block erase */
245 {JEDEC_WREN, JEDEC_BE_D8}, /* block erase */
246 {JEDEC_WREN, JEDEC_CE_60}, /* chip erase */
247 {JEDEC_WREN, JEDEC_CE_C7}, /* chip erase */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000248 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
249 {JEDEC_WREN, JEDEC_WRSR},
FENG yu ningf041e9b2008-12-15 02:32:11 +0000250 {JEDEC_EWSR, JEDEC_WRSR},
251 {0,}
252};
253
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000254/* Reasonable default configuration. Needs ad-hoc modifications if we
255 * encounter unlisted opcodes. Fun.
256 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000257static OPCODES O_ST_M25P = {
Dominik Geyerb46acba2008-05-16 12:55:55 +0000258 {
259 JEDEC_WREN,
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000260 JEDEC_EWSR,
261 },
Dominik Geyerb46acba2008-05-16 12:55:55 +0000262 {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000263 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000264 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000265 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000266 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
Carl-Daniel Hailfinger15aa7c62009-05-26 21:25:08 +0000267 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000268 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000269 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000270 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
271 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000272};
273
Helge Wagner738e2522010-10-05 22:06:05 +0000274/* List of opcodes with their corresponding spi_type
275 * It is used to reprogram the chipset OPCODE table on-the-fly if an opcode
276 * is needed which is currently not in the chipset OPCODE table
277 */
278static OPCODE POSSIBLE_OPCODES[] = {
279 {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Write Byte
280 {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Data
281 {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Erase Sector
282 {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read Device Status Reg
283 {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0}, // Read Electronic Manufacturer Signature
284 {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Write Status Register
285 {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0}, // Read JDEC ID
286 {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Bulk erase
287 {JEDEC_SE, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Sector erase
288 {JEDEC_BE_52, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0}, // Block erase
289 {JEDEC_AAI_WORD_PROGRAM, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0}, // Auto Address Increment
290};
291
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000292static OPCODES O_EXISTING = {};
FENG yu ningc05a2952008-12-08 18:16:58 +0000293
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000294/* pretty printing functions */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000295static void prettyprint_opcodes(OPCODES *ops)
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000296{
297 if(ops == NULL)
298 return;
299
300 msg_pdbg("preop0=0x%02x, preop1=0x%02x\n", ops->preop[0],
301 ops->preop[1]);
302
303 OPCODE oc;
304 uint8_t i;
305 for (i = 0; i < 8; i++) {
306 oc = ops->opcode[i];
307 msg_pdbg("op[%d]=0x%02x, %d, %d\n",
308 i,
309 oc.opcode,
310 oc.spi_type,
311 oc.atomic);
312 }
313}
314
315#define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF)
316
Stefan Tauner55206942011-06-11 09:53:22 +0000317static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
318{
319 msg_pdbg("HSFS: ");
320 pprint_reg(HSFS, FDONE, reg_val, ", ");
321 pprint_reg(HSFS, FCERR, reg_val, ", ");
322 pprint_reg(HSFS, AEL, reg_val, ", ");
323 pprint_reg(HSFS, BERASE, reg_val, ", ");
324 pprint_reg(HSFS, SCIP, reg_val, ", ");
325 pprint_reg(HSFS, FDOPSS, reg_val, ", ");
326 pprint_reg(HSFS, FDV, reg_val, ", ");
327 pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
328}
329
330static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
331{
332 msg_pdbg("HSFC: ");
333 pprint_reg(HSFC, FGO, reg_val, ", ");
334 pprint_reg(HSFC, FCYCLE, reg_val, ", ");
335 pprint_reg(HSFC, FDBC, reg_val, ", ");
336 pprint_reg(HSFC, SME, reg_val, "\n");
337}
338
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000339static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
340{
341 msg_pdbg("SSFS: ");
342 pprint_reg(SSFS, SCIP, reg_val, ", ");
343 pprint_reg(SSFS, FDONE, reg_val, ", ");
344 pprint_reg(SSFS, FCERR, reg_val, ", ");
345 pprint_reg(SSFS, AEL, reg_val, "\n");
346}
347
348static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
349{
350 msg_pdbg("SSFC: ");
351 pprint_reg(SSFC, SCGO, reg_val, ", ");
352 pprint_reg(SSFC, ACS, reg_val, ", ");
353 pprint_reg(SSFC, SPOP, reg_val, ", ");
354 pprint_reg(SSFC, COP, reg_val, ", ");
355 pprint_reg(SSFC, DBC, reg_val, ", ");
356 pprint_reg(SSFC, SME, reg_val, ", ");
357 pprint_reg(SSFC, SCF, reg_val, "\n");
358}
359
Helge Wagner738e2522010-10-05 22:06:05 +0000360static uint8_t lookup_spi_type(uint8_t opcode)
361{
362 int a;
363
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000364 for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
Helge Wagner738e2522010-10-05 22:06:05 +0000365 if (POSSIBLE_OPCODES[a].opcode == opcode)
366 return POSSIBLE_OPCODES[a].spi_type;
367 }
368
369 return 0xFF;
370}
371
372static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
373{
374 uint8_t spi_type;
375
376 spi_type = lookup_spi_type(opcode);
377 if (spi_type > 3) {
378 /* Try to guess spi type from read/write sizes.
379 * The following valid writecnt/readcnt combinations exist:
380 * writecnt = 4, readcnt >= 0
381 * writecnt = 1, readcnt >= 0
382 * writecnt >= 4, readcnt = 0
383 * writecnt >= 1, readcnt = 0
384 * writecnt >= 1 is guaranteed for all commands.
385 */
386 if (readcnt == 0)
387 /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
388 * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
389 * bytes are actual the address, they go to the bus anyhow
390 */
391 spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
392 else if (writecnt == 1) // and readcnt is > 0
393 spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
394 else if (writecnt == 4) // and readcnt is > 0
395 spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
396 // else we have an invalid case, will be handled below
397 }
398 if (spi_type <= 3) {
399 int oppos=2; // use original JEDEC_BE_D8 offset
400 curopcodes->opcode[oppos].opcode = opcode;
401 curopcodes->opcode[oppos].spi_type = spi_type;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000402 program_opcodes(curopcodes, 0);
Helge Wagner738e2522010-10-05 22:06:05 +0000403 oppos = find_opcode(curopcodes, opcode);
404 msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
405 return oppos;
406 }
407 return -1;
408}
409
Uwe Hermann09e04f72009-05-16 22:36:00 +0000410static int find_opcode(OPCODES *op, uint8_t opcode)
FENG yu ningc05a2952008-12-08 18:16:58 +0000411{
412 int a;
413
414 for (a = 0; a < 8; a++) {
415 if (op->opcode[a].opcode == opcode)
416 return a;
417 }
418
419 return -1;
420}
421
Uwe Hermann09e04f72009-05-16 22:36:00 +0000422static int find_preop(OPCODES *op, uint8_t preop)
FENG yu ningc05a2952008-12-08 18:16:58 +0000423{
424 int a;
425
426 for (a = 0; a < 2; a++) {
427 if (op->preop[a] == preop)
428 return a;
429 }
430
431 return -1;
432}
433
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000434/* Create a struct OPCODES based on what we find in the locked down chipset. */
FENG yu ningf041e9b2008-12-15 02:32:11 +0000435static int generate_opcodes(OPCODES * op)
FENG yu ningc05a2952008-12-08 18:16:58 +0000436{
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000437 int a;
FENG yu ningc05a2952008-12-08 18:16:58 +0000438 uint16_t preop, optype;
439 uint32_t opmenu[2];
FENG yu ningc05a2952008-12-08 18:16:58 +0000440
441 if (op == NULL) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000442 msg_perr("\n%s: null OPCODES pointer!\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000443 return -1;
444 }
445
Michael Karcherb9dbe482011-05-11 17:07:07 +0000446 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000447 case SPI_CONTROLLER_ICH7:
448 case SPI_CONTROLLER_VIA:
FENG yu ningc05a2952008-12-08 18:16:58 +0000449 preop = REGREAD16(ICH7_REG_PREOP);
450 optype = REGREAD16(ICH7_REG_OPTYPE);
451 opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
452 opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
453 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000454 case SPI_CONTROLLER_ICH9:
FENG yu ningc05a2952008-12-08 18:16:58 +0000455 preop = REGREAD16(ICH9_REG_PREOP);
456 optype = REGREAD16(ICH9_REG_OPTYPE);
457 opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
458 opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
459 break;
460 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000461 msg_perr("%s: unsupported chipset\n", __func__);
FENG yu ningc05a2952008-12-08 18:16:58 +0000462 return -1;
463 }
464
465 op->preop[0] = (uint8_t) preop;
466 op->preop[1] = (uint8_t) (preop >> 8);
467
468 for (a = 0; a < 8; a++) {
469 op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
470 optype >>= 2;
471 }
472
473 for (a = 0; a < 4; a++) {
474 op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
475 opmenu[0] >>= 8;
476 }
477
478 for (a = 4; a < 8; a++) {
479 op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
480 opmenu[1] >>= 8;
481 }
482
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000483 /* No preopcodes used by default. */
484 for (a = 0; a < 8; a++)
FENG yu ningc05a2952008-12-08 18:16:58 +0000485 op->opcode[a].atomic = 0;
486
FENG yu ningc05a2952008-12-08 18:16:58 +0000487 return 0;
488}
489
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000490static int program_opcodes(OPCODES *op, int enable_undo)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000491{
492 uint8_t a;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000493 uint16_t preop, optype;
494 uint32_t opmenu[2];
Dominik Geyerb46acba2008-05-16 12:55:55 +0000495
496 /* Program Prefix Opcodes */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000497 /* 0:7 Prefix Opcode 1 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000498 preop = (op->preop[0]);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000499 /* 8:16 Prefix Opcode 2 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000500 preop |= ((uint16_t) op->preop[1]) << 8;
Uwe Hermann394131e2008-10-18 21:14:13 +0000501
Stefan Reinauera9424d52008-06-27 16:28:34 +0000502 /* Program Opcode Types 0 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000503 optype = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000504 for (a = 0; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000505 optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000506 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000507
Stefan Reinauera9424d52008-06-27 16:28:34 +0000508 /* Program Allowable Opcodes 0 - 3 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000509 opmenu[0] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000510 for (a = 0; a < 4; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000511 opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000512 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000513
Dominik Geyerb46acba2008-05-16 12:55:55 +0000514 /*Program Allowable Opcodes 4 - 7 */
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000515 opmenu[1] = 0;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000516 for (a = 4; a < 8; a++) {
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000517 opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000518 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000519
Sean Nelson316a29f2010-05-07 20:09:04 +0000520 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 +0000521 switch (spi_programmer->type) {
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000522 case SPI_CONTROLLER_ICH7:
523 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000524 /* Register undo only for enable_undo=1, i.e. first call. */
525 if (enable_undo) {
526 rmmio_valw(ich_spibar + ICH7_REG_PREOP);
527 rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
528 rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
529 rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
530 }
531 mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
532 mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
533 mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
534 mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000535 break;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +0000536 case SPI_CONTROLLER_ICH9:
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000537 /* Register undo only for enable_undo=1, i.e. first call. */
538 if (enable_undo) {
539 rmmio_valw(ich_spibar + ICH9_REG_PREOP);
540 rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
541 rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
542 rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
543 }
544 mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
545 mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
546 mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
547 mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000548 break;
549 default:
Sean Nelson316a29f2010-05-07 20:09:04 +0000550 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000551 return -1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000552 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000553
554 return 0;
555}
556
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000557/*
558 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
559 * it didn't stick.
560 */
Stefan Taunere27b2d42011-07-01 00:39:09 +0000561static void ich_set_bbar(uint32_t min_addr)
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000562{
Stefan Taunere27b2d42011-07-01 00:39:09 +0000563 int bbar_off;
Michael Karcherb9dbe482011-05-11 17:07:07 +0000564 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000565 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000566 case SPI_CONTROLLER_VIA:
Stefan Taunere27b2d42011-07-01 00:39:09 +0000567 bbar_off = 0x50;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000568 break;
569 case SPI_CONTROLLER_ICH9:
Stefan Taunere27b2d42011-07-01 00:39:09 +0000570 bbar_off = ICH9_REG_BBAR;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000571 break;
572 default:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +0000573 msg_perr("Unknown chipset for BBAR setting!\n");
Stefan Taunere27b2d42011-07-01 00:39:09 +0000574 return;
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000575 }
Stefan Taunere27b2d42011-07-01 00:39:09 +0000576
577 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
578 if (ichspi_bbar) {
579 msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
580 ichspi_bbar);
581 }
582 min_addr &= BBAR_MASK;
583 ichspi_bbar |= min_addr;
584 rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
585 ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
586
587 /* We don't have any option except complaining. And if the write
588 * failed, the restore will fail as well, so no problem there.
589 */
590 if (ichspi_bbar != min_addr)
591 msg_perr("Setting BBAR failed!\n");
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000592}
593
Stefan Tauner8b391b82011-08-09 01:49:34 +0000594/* Read len bytes from the fdata/spid register into the data array.
595 *
596 * Note that using len > spi_programmer->max_data_read will return garbage or
597 * may even crash.
598 */
599 static void ich_read_data(uint8_t *data, int len, int reg0_off)
600 {
601 int i;
602 uint32_t temp32 = 0;
603
604 for (i = 0; i < len; i++) {
605 if ((i % 4) == 0)
606 temp32 = REGREAD32(reg0_off + i);
607
608 data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
609 }
610}
611
612/* Fill len bytes from the data array into the fdata/spid registers.
613 *
614 * Note that using len > spi_programmer->max_data_write will trash the registers
615 * following the data registers.
616 */
617static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
618{
619 uint32_t temp32 = 0;
620 int i;
621
622 if (len <= 0)
623 return;
624
625 for (i = 0; i < len; i++) {
626 if ((i % 4) == 0)
627 temp32 = 0;
628
629 temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
630
631 if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
632 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
633 }
634 i--;
635 if ((i % 4) != 3) /* Write remaining data to regs. */
636 REGWRITE32(reg0_off + (i - (i % 4)), temp32);
637}
638
FENG yu ningf041e9b2008-12-15 02:32:11 +0000639/* This function generates OPCODES from or programs OPCODES to ICH according to
640 * the chipset's SPI configuration lock.
FENG yu ningc05a2952008-12-08 18:16:58 +0000641 *
FENG yu ningf041e9b2008-12-15 02:32:11 +0000642 * It should be called before ICH sends any spi command.
FENG yu ningc05a2952008-12-08 18:16:58 +0000643 */
Michael Karchera4448d92010-07-22 18:04:15 +0000644static int ich_init_opcodes(void)
FENG yu ningc05a2952008-12-08 18:16:58 +0000645{
646 int rc = 0;
647 OPCODES *curopcodes_done;
648
649 if (curopcodes)
650 return 0;
651
652 if (ichspi_lock) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000653 msg_pdbg("Reading OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000654 curopcodes_done = &O_EXISTING;
FENG yu ningf041e9b2008-12-15 02:32:11 +0000655 rc = generate_opcodes(curopcodes_done);
FENG yu ningc05a2952008-12-08 18:16:58 +0000656 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +0000657 msg_pdbg("Programming OPCODES... ");
FENG yu ningc05a2952008-12-08 18:16:58 +0000658 curopcodes_done = &O_ST_M25P;
Carl-Daniel Hailfinger54ce73a2011-05-03 21:49:41 +0000659 rc = program_opcodes(curopcodes_done, 1);
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000660 /* Technically not part of opcode init, but it allows opcodes
661 * to run without transaction errors by setting the lowest
662 * allowed address to zero.
663 */
664 ich_set_bbar(0);
FENG yu ningc05a2952008-12-08 18:16:58 +0000665 }
666
667 if (rc) {
668 curopcodes = NULL;
Sean Nelson316a29f2010-05-07 20:09:04 +0000669 msg_perr("failed\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000670 return 1;
671 } else {
672 curopcodes = curopcodes_done;
Sean Nelson316a29f2010-05-07 20:09:04 +0000673 msg_pdbg("done\n");
Stefan Tauner8b391b82011-08-09 01:49:34 +0000674 prettyprint_opcodes(curopcodes);
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000675 msg_pdbg("\n");
FENG yu ningc05a2952008-12-08 18:16:58 +0000676 return 0;
677 }
678}
679
Stefan Reinauer43119562008-11-02 19:51:50 +0000680static int ich7_run_opcode(OPCODE op, uint32_t offset,
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000681 uint8_t datalength, uint8_t * data, int maxdata)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000682{
683 int write_cmd = 0;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000684 int timeout;
Stefan Tauner8b391b82011-08-09 01:49:34 +0000685 uint32_t temp32;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000686 uint16_t temp16;
Stefan Reinauer43119562008-11-02 19:51:50 +0000687 uint64_t opmenu;
688 int opcode_index;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000689
690 /* Is it a write command? */
691 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
692 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
693 write_cmd = 1;
694 }
695
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000696 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
697 while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
698 programmer_delay(10);
699 }
700 if (!timeout) {
701 msg_perr("Error: SCIP never cleared!\n");
702 return 1;
703 }
704
Stefan Tauner10b3e222011-07-01 00:39:23 +0000705 /* Program offset in flash into SPIA while preserving reserved bits. */
706 temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
707 REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000708
Stefan Tauner10b3e222011-07-01 00:39:23 +0000709 /* Program data into SPID0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000710 if (write_cmd && (datalength != 0))
711 ich_fill_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000712
713 /* Assemble SPIS */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000714 temp16 = REGREAD16(ICH7_REG_SPIS);
715 /* keep reserved bits */
716 temp16 &= SPIS_RESERVED_MASK;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000717 /* clear error status registers */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000718 temp16 |= (SPIS_CDS | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000719 REGWRITE16(ICH7_REG_SPIS, temp16);
720
721 /* Assemble SPIC */
722 temp16 = 0;
723
724 if (datalength != 0) {
725 temp16 |= SPIC_DS;
Rudolf Marek3fdbccf2008-06-30 21:38:30 +0000726 temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000727 }
728
729 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000730 opmenu = REGREAD32(ICH7_REG_OPMENU);
731 opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
732
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000733 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
734 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000735 break;
736 }
737 opmenu >>= 8;
738 }
739 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000740 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000741 return 1;
742 }
743 temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000744
Michael Karcher136125a2011-04-29 22:11:36 +0000745 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
746 /* Handle Atomic. Atomic commands include three steps:
747 - sending the preop (mainly EWSR or WREN)
748 - sending the main command
749 - waiting for the busy bit (WIP) to be cleared
750 This means the timeout must be sufficient for chip erase
751 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000752 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000753 switch (op.atomic) {
754 case 2:
755 /* Select second preop. */
756 temp16 |= SPIC_SPOP;
757 /* And fall through. */
758 case 1:
759 /* Atomic command (preop+op) */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000760 temp16 |= SPIC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000761 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000762 break;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000763 }
764
765 /* Start */
766 temp16 |= SPIC_SCGO;
767
768 /* write it */
769 REGWRITE16(ICH7_REG_SPIC, temp16);
770
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000771 /* Wait for Cycle Done Status or Flash Cycle Error. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000772 while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
773 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000774 programmer_delay(10);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000775 }
776 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000777 msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
778 REGREAD16(ICH7_REG_SPIS));
779 return 1;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000780 }
781
Sean Nelson316a29f2010-05-07 20:09:04 +0000782 /* FIXME: make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000783 temp16 = REGREAD16(ICH7_REG_SPIS);
784 if (temp16 & SPIS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000785 msg_perr("Transaction error!\n");
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000786 /* keep reserved bits */
787 temp16 &= SPIS_RESERVED_MASK;
788 REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000789 return 1;
790 }
791
Stefan Tauner8b391b82011-08-09 01:49:34 +0000792 if ((!write_cmd) && (datalength != 0))
793 ich_read_data(data, datalength, ICH7_REG_SPID0);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000794
795 return 0;
796}
797
Stefan Reinauer43119562008-11-02 19:51:50 +0000798static int ich9_run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000799 uint8_t datalength, uint8_t * data)
800{
801 int write_cmd = 0;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000802 int timeout;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000803 uint32_t temp32;
Stefan Reinauer43119562008-11-02 19:51:50 +0000804 uint64_t opmenu;
805 int opcode_index;
Stefan Reinauera9424d52008-06-27 16:28:34 +0000806
807 /* Is it a write command? */
808 if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
809 || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
810 write_cmd = 1;
811 }
812
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000813 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
814 while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
815 programmer_delay(10);
816 }
817 if (!timeout) {
818 msg_perr("Error: SCIP never cleared!\n");
819 return 1;
820 }
821
Stefan Tauner10b3e222011-07-01 00:39:23 +0000822 /* Program offset in flash into FADDR while preserve the reserved bits
823 * and clearing the 25. address bit which is only useable in hwseq. */
824 temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
825 REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
Stefan Reinauera9424d52008-06-27 16:28:34 +0000826
827 /* Program data into FDATA0 to N */
Stefan Tauner8b391b82011-08-09 01:49:34 +0000828 if (write_cmd && (datalength != 0))
829 ich_fill_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000830
831 /* Assemble SSFS + SSFC */
Helge Wagnera319be12010-08-11 21:06:10 +0000832 temp32 = REGREAD32(ICH9_REG_SSFS);
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000833 /* Keep reserved bits only */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000834 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000835 /* Clear cycle done and cycle error status registers */
836 temp32 |= (SSFS_FDONE | SSFS_FCERR);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000837 REGWRITE32(ICH9_REG_SSFS, temp32);
838
Uwe Hermann4e3d0b32010-03-25 23:18:41 +0000839 /* Use 20 MHz */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000840 temp32 |= SSFC_SCF_20MHZ;
841
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000842 /* Set data byte count (DBC) and data cycle bit (DS) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000843 if (datalength != 0) {
844 uint32_t datatemp;
845 temp32 |= SSFC_DS;
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000846 datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
847 SSFC_DBC);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000848 temp32 |= datatemp;
849 }
850
851 /* Select opcode */
Stefan Reinauer43119562008-11-02 19:51:50 +0000852 opmenu = REGREAD32(ICH9_REG_OPMENU);
853 opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
854
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000855 for (opcode_index = 0; opcode_index < 8; opcode_index++) {
856 if ((opmenu & 0xff) == op.opcode) {
Stefan Reinauer43119562008-11-02 19:51:50 +0000857 break;
858 }
859 opmenu >>= 8;
860 }
861 if (opcode_index == 8) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000862 msg_pdbg("Opcode %x not found.\n", op.opcode);
Stefan Reinauer43119562008-11-02 19:51:50 +0000863 return 1;
864 }
865 temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000866
Michael Karcher136125a2011-04-29 22:11:36 +0000867 timeout = 100 * 60; /* 60 ms are 9.6 million cycles at 16 MHz. */
868 /* Handle Atomic. Atomic commands include three steps:
869 - sending the preop (mainly EWSR or WREN)
870 - sending the main command
871 - waiting for the busy bit (WIP) to be cleared
872 This means the timeout must be sufficient for chip erase
873 of slow high-capacity chips.
Stefan Taunerc0aaf952011-05-19 02:58:17 +0000874 */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000875 switch (op.atomic) {
876 case 2:
877 /* Select second preop. */
878 temp32 |= SSFC_SPOP;
879 /* And fall through. */
880 case 1:
881 /* Atomic command (preop+op) */
Dominik Geyerb46acba2008-05-16 12:55:55 +0000882 temp32 |= SSFC_ACS;
Michael Karcher136125a2011-04-29 22:11:36 +0000883 timeout = 100 * 1000 * 60; /* 60 seconds */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000884 break;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000885 }
886
887 /* Start */
888 temp32 |= SSFC_SCGO;
889
890 /* write it */
Stefan Reinauera9424d52008-06-27 16:28:34 +0000891 REGWRITE32(ICH9_REG_SSFS, temp32);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000892
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000893 /* Wait for Cycle Done Status or Flash Cycle Error. */
Stefan Tauner0c1ec452011-06-11 09:53:09 +0000894 while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000895 --timeout) {
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000896 programmer_delay(10);
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000897 }
898 if (!timeout) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000899 msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
900 REGREAD32(ICH9_REG_SSFS));
901 return 1;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000902 }
903
Sean Nelson316a29f2010-05-07 20:09:04 +0000904 /* FIXME make sure we do not needlessly cause transaction errors. */
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000905 temp32 = REGREAD32(ICH9_REG_SSFS);
906 if (temp32 & SSFS_FCERR) {
Stefan Tauner8ed29342011-04-29 23:53:09 +0000907 msg_perr("Transaction error!\n");
Stefan Tauner2a8b2622011-06-11 09:53:16 +0000908 prettyprint_ich9_reg_ssfs(temp32);
909 prettyprint_ich9_reg_ssfc(temp32);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +0000910 /* keep reserved bits */
911 temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
912 /* Clear the transaction error. */
913 REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000914 return 1;
915 }
916
Stefan Tauner8b391b82011-08-09 01:49:34 +0000917 if ((!write_cmd) && (datalength != 0))
918 ich_read_data(data, datalength, ICH9_REG_FDATA0);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000919
920 return 0;
921}
922
Stefan Reinauer43119562008-11-02 19:51:50 +0000923static int run_opcode(OPCODE op, uint32_t offset,
Stefan Reinauera9424d52008-06-27 16:28:34 +0000924 uint8_t datalength, uint8_t * data)
925{
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000926 /* max_data_read == max_data_write for all Intel/VIA SPI masters */
927 uint8_t maxlength = spi_programmer->max_data_read;
928
929 if (spi_programmer->type == SPI_CONTROLLER_NONE) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000930 msg_perr("%s: unsupported chipset\n", __func__);
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000931 return -1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000932 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000933
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +0000934 if (datalength > maxlength) {
935 msg_perr("%s: Internal command size error for "
936 "opcode 0x%02x, got datalength=%i, want <=%i\n",
937 __func__, op.opcode, datalength, maxlength);
938 return SPI_INVALID_LENGTH;
939 }
940
941 switch (spi_programmer->type) {
942 case SPI_CONTROLLER_VIA:
943 case SPI_CONTROLLER_ICH7:
944 return ich7_run_opcode(op, offset, datalength, data, maxlength);
945 case SPI_CONTROLLER_ICH9:
946 return ich9_run_opcode(op, offset, datalength, data);
947 default:
948 /* If we ever get here, something really weird happened */
949 return -1;
950 }
Stefan Reinauera9424d52008-06-27 16:28:34 +0000951}
952
Michael Karcherb9dbe482011-05-11 17:07:07 +0000953static int ich_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Stefan Reinauer325b5d42008-06-27 15:18:20 +0000954 const unsigned char *writearr, unsigned char *readarr)
Dominik Geyerb46acba2008-05-16 12:55:55 +0000955{
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000956 int result;
Dominik Geyerb46acba2008-05-16 12:55:55 +0000957 int opcode_index = -1;
958 const unsigned char cmd = *writearr;
959 OPCODE *opcode;
960 uint32_t addr = 0;
961 uint8_t *data;
962 int count;
963
Dominik Geyerb46acba2008-05-16 12:55:55 +0000964 /* find cmd in opcodes-table */
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000965 opcode_index = find_opcode(curopcodes, cmd);
Dominik Geyerb46acba2008-05-16 12:55:55 +0000966 if (opcode_index == -1) {
Helge Wagner738e2522010-10-05 22:06:05 +0000967 if (!ichspi_lock)
968 opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
969 if (opcode_index == -1) {
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000970 msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
971 cmd);
Helge Wagner738e2522010-10-05 22:06:05 +0000972 return SPI_INVALID_OPCODE;
973 }
Dominik Geyerb46acba2008-05-16 12:55:55 +0000974 }
975
976 opcode = &(curopcodes->opcode[opcode_index]);
977
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000978 /* The following valid writecnt/readcnt combinations exist:
979 * writecnt = 4, readcnt >= 0
980 * writecnt = 1, readcnt >= 0
981 * writecnt >= 4, readcnt = 0
982 * writecnt >= 1, readcnt = 0
983 * writecnt >= 1 is guaranteed for all commands.
984 */
985 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
986 (writecnt != 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000987 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000988 "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
989 writecnt);
990 return SPI_INVALID_LENGTH;
991 }
992 if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
993 (writecnt != 1)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000994 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +0000995 "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
996 writecnt);
997 return SPI_INVALID_LENGTH;
998 }
999 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1000 (writecnt < 4)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001001 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001002 "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
1003 writecnt);
1004 return SPI_INVALID_LENGTH;
1005 }
1006 if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1007 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1008 (readcnt)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001009 msg_perr("%s: Internal command size error for opcode "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001010 "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1011 readcnt);
1012 return SPI_INVALID_LENGTH;
1013 }
1014
Dominik Geyerb46acba2008-05-16 12:55:55 +00001015 /* if opcode-type requires an address */
1016 if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1017 opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001018 addr = (writearr[1] << 16) |
1019 (writearr[2] << 8) | (writearr[3] << 0);
Michael Karcherb9dbe482011-05-11 17:07:07 +00001020 switch (spi_programmer->type) {
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001021 case SPI_CONTROLLER_ICH7:
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001022 case SPI_CONTROLLER_VIA:
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +00001023 case SPI_CONTROLLER_ICH9:
1024 if (addr < ichspi_bbar) {
1025 msg_perr("%s: Address 0x%06x below allowed "
1026 "range 0x%06x-0xffffff\n", __func__,
1027 addr, ichspi_bbar);
1028 return SPI_INVALID_ADDRESS;
1029 }
1030 break;
1031 default:
1032 break;
1033 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001034 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001035
Stefan Taunerb2d5f6a2011-06-11 19:44:31 +00001036 /* Translate read/write array/count.
1037 * The maximum data length is identical for the maximum read length and
1038 * for the maximum write length excluding opcode and address. Opcode and
1039 * address are stored in separate registers, not in the data registers
1040 * and are thus not counted towards data length. The only exception
1041 * applies if the opcode definition (un)intentionally classifies said
1042 * opcode incorrectly as non-address opcode or vice versa. */
Dominik Geyerb46acba2008-05-16 12:55:55 +00001043 if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001044 data = (uint8_t *) (writearr + 1);
1045 count = writecnt - 1;
1046 } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1047 data = (uint8_t *) (writearr + 4);
1048 count = writecnt - 4;
1049 } else {
1050 data = (uint8_t *) readarr;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001051 count = readcnt;
1052 }
Stefan Reinauer325b5d42008-06-27 15:18:20 +00001053
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001054 result = run_opcode(*opcode, addr, count, data);
1055 if (result) {
Stefan Tauner8ed29342011-04-29 23:53:09 +00001056 msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1057 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1058 (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1059 msg_pdbg("at address 0x%06x ", addr);
1060 }
1061 msg_pdbg("(payload length was %d).\n", count);
1062
1063 /* Print out the data array if it contains data to write.
1064 * Errors are detected before the received data is read back into
1065 * the array so it won't make sense to print it then. */
1066 if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1067 (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1068 int i;
1069 msg_pspew("The data was:\n");
1070 for(i=0; i<count; i++){
1071 msg_pspew("%3d: 0x%02x\n", i, data[i]);
1072 }
1073 }
Dominik Geyerb46acba2008-05-16 12:55:55 +00001074 }
1075
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +00001076 return result;
Dominik Geyerb46acba2008-05-16 12:55:55 +00001077}
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001078
Michael Karcherb9dbe482011-05-11 17:07:07 +00001079static int ich_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001080{
1081 int ret = 0;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001082 int i;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001083 int oppos, preoppos;
1084 for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001085 if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001086 /* Next command is valid. */
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001087 preoppos = find_preop(curopcodes, cmds->writearr[0]);
1088 oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001089 if ((oppos == -1) && (preoppos != -1)) {
1090 /* Current command is listed as preopcode in
1091 * ICH struct OPCODES, but next command is not
1092 * listed as opcode in that struct.
1093 * Check for command sanity, then
1094 * try to reprogram the ICH opcode list.
1095 */
1096 if (find_preop(curopcodes,
1097 (cmds + 1)->writearr[0]) != -1) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001098 msg_perr("%s: Two subsequent "
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001099 "preopcodes 0x%02x and 0x%02x, "
1100 "ignoring the first.\n",
1101 __func__, cmds->writearr[0],
1102 (cmds + 1)->writearr[0]);
1103 continue;
1104 }
1105 /* If the chipset is locked down, we'll fail
1106 * during execution of the next command anyway.
1107 * No need to bother with fixups.
1108 */
1109 if (!ichspi_lock) {
Helge Wagner738e2522010-10-05 22:06:05 +00001110 oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1111 if (oppos == -1)
1112 continue;
1113 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001114 continue;
1115 }
1116 }
1117 if ((oppos != -1) && (preoppos != -1)) {
1118 /* Current command is listed as preopcode in
1119 * ICH struct OPCODES and next command is listed
1120 * as opcode in that struct. Match them up.
1121 */
1122 curopcodes->opcode[oppos].atomic = preoppos + 1;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001123 continue;
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001124 }
1125 /* If none of the above if-statements about oppos or
1126 * preoppos matched, this is a normal opcode.
1127 */
1128 }
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +00001129 ret = ich_spi_send_command(cmds->writecnt, cmds->readcnt,
1130 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfingerf15e1ab2010-02-11 11:28:37 +00001131 /* Reset the type of all opcodes to non-atomic. */
1132 for (i = 0; i < 8; i++)
1133 curopcodes->opcode[i].atomic = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +00001134 }
1135 return ret;
1136}
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001137
Michael Karchera4448d92010-07-22 18:04:15 +00001138#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1139#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1140#define ICH_BRWA(x) ((x >> 8) & 0xff)
1141#define ICH_BRRA(x) ((x >> 0) & 0xff)
1142
Michael Karchera4448d92010-07-22 18:04:15 +00001143static void do_ich9_spi_frap(uint32_t frap, int i)
1144{
Mathias Krausea60faab2011-01-17 07:50:42 +00001145 static const char *const access_names[4] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001146 "locked", "read-only", "write-only", "read-write"
1147 };
Mathias Krausea60faab2011-01-17 07:50:42 +00001148 static const char *const region_names[5] = {
Michael Karchera4448d92010-07-22 18:04:15 +00001149 "Flash Descriptor", "BIOS", "Management Engine",
1150 "Gigabit Ethernet", "Platform Data"
1151 };
1152 uint32_t base, limit;
1153 int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1154 (((ICH_BRRA(frap) >> i) & 1) << 0);
Stefan Tauner29c80832011-06-12 08:14:10 +00001155 int offset = ICH9_REG_FREG0 + i * 4;
Michael Karchera4448d92010-07-22 18:04:15 +00001156 uint32_t freg = mmio_readl(ich_spibar + offset);
1157
1158 msg_pdbg("0x%02X: 0x%08x (FREG%i: %s)\n",
1159 offset, freg, i, region_names[i]);
1160
1161 base = ICH_FREG_BASE(freg);
1162 limit = ICH_FREG_LIMIT(freg);
Joshua Roysd172ecd2011-05-26 13:30:51 +00001163 if (base > limit) {
Michael Karchera4448d92010-07-22 18:04:15 +00001164 /* this FREG is disabled */
1165 msg_pdbg("%s region is unused.\n", region_names[i]);
1166 return;
1167 }
1168
Stefan Tauner1e146392011-09-15 23:52:55 +00001169 msg_pdbg("0x%08x-0x%08x is %s\n", base, (limit | 0x0fff),
1170 access_names[rwperms]);
Michael Karchera4448d92010-07-22 18:04:15 +00001171}
1172
Michael Karcherb9dbe482011-05-11 17:07:07 +00001173static const struct spi_programmer spi_programmer_ich7 = {
1174 .type = SPI_CONTROLLER_ICH7,
1175 .max_data_read = 64,
1176 .max_data_write = 64,
1177 .command = ich_spi_send_command,
1178 .multicommand = ich_spi_send_multicommand,
1179 .read = default_spi_read,
1180 .write_256 = default_spi_write_256,
1181};
1182
1183static const struct spi_programmer spi_programmer_ich9 = {
1184 .type = SPI_CONTROLLER_ICH9,
1185 .max_data_read = 64,
1186 .max_data_write = 64,
1187 .command = ich_spi_send_command,
1188 .multicommand = ich_spi_send_multicommand,
1189 .read = default_spi_read,
1190 .write_256 = default_spi_write_256,
1191};
1192
Michael Karchera4448d92010-07-22 18:04:15 +00001193int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb,
1194 int ich_generation)
1195{
1196 int i;
1197 uint8_t old, new;
1198 uint16_t spibar_offset, tmp2;
1199 uint32_t tmp;
Stefan Tauner1e146392011-09-15 23:52:55 +00001200 int ichspi_desc = 0;
Michael Karchera4448d92010-07-22 18:04:15 +00001201
Michael Karchera4448d92010-07-22 18:04:15 +00001202 switch (ich_generation) {
1203 case 7:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001204 register_spi_programmer(&spi_programmer_ich7);
Michael Karchera4448d92010-07-22 18:04:15 +00001205 spibar_offset = 0x3020;
1206 break;
1207 case 8:
Michael Karcherb9dbe482011-05-11 17:07:07 +00001208 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001209 spibar_offset = 0x3020;
1210 break;
1211 case 9:
1212 case 10:
1213 default: /* Future version might behave the same */
Michael Karcherb9dbe482011-05-11 17:07:07 +00001214 register_spi_programmer(&spi_programmer_ich9);
Michael Karchera4448d92010-07-22 18:04:15 +00001215 spibar_offset = 0x3800;
1216 break;
1217 }
1218
1219 /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
1220 msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset);
1221
1222 /* Assign Virtual Address */
1223 ich_spibar = rcrb + spibar_offset;
1224
Michael Karcherb9dbe482011-05-11 17:07:07 +00001225 switch (spi_programmer->type) {
Michael Karchera4448d92010-07-22 18:04:15 +00001226 case SPI_CONTROLLER_ICH7:
1227 msg_pdbg("0x00: 0x%04x (SPIS)\n",
1228 mmio_readw(ich_spibar + 0));
1229 msg_pdbg("0x02: 0x%04x (SPIC)\n",
1230 mmio_readw(ich_spibar + 2));
1231 msg_pdbg("0x04: 0x%08x (SPIA)\n",
1232 mmio_readl(ich_spibar + 4));
1233 for (i = 0; i < 8; i++) {
1234 int offs;
1235 offs = 8 + (i * 8);
1236 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1237 mmio_readl(ich_spibar + offs), i);
1238 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1239 mmio_readl(ich_spibar + offs + 4), i);
1240 }
1241 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1242 msg_pdbg("0x50: 0x%08x (BBAR)\n",
1243 ichspi_bbar);
1244 msg_pdbg("0x54: 0x%04x (PREOP)\n",
1245 mmio_readw(ich_spibar + 0x54));
1246 msg_pdbg("0x56: 0x%04x (OPTYPE)\n",
1247 mmio_readw(ich_spibar + 0x56));
1248 msg_pdbg("0x58: 0x%08x (OPMENU)\n",
1249 mmio_readl(ich_spibar + 0x58));
1250 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
1251 mmio_readl(ich_spibar + 0x5c));
Stefan Tauner122dd122011-07-24 15:34:56 +00001252 for (i = 0; i < 3; i++) {
Michael Karchera4448d92010-07-22 18:04:15 +00001253 int offs;
1254 offs = 0x60 + (i * 4);
1255 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1256 mmio_readl(ich_spibar + offs), i);
1257 }
Michael Karchera4448d92010-07-22 18:04:15 +00001258 if (mmio_readw(ich_spibar) & (1 << 15)) {
1259 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1260 ichspi_lock = 1;
1261 }
1262 ich_init_opcodes();
1263 break;
1264 case SPI_CONTROLLER_ICH9:
Stefan Tauner29c80832011-06-12 08:14:10 +00001265 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
Michael Karchera4448d92010-07-22 18:04:15 +00001266 msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
Stefan Tauner55206942011-06-11 09:53:22 +00001267 prettyprint_ich9_reg_hsfs(tmp2);
Stefan Tauner29c80832011-06-12 08:14:10 +00001268 if (tmp2 & HSFS_FLOCKDN) {
Stefan Tauner55206942011-06-11 09:53:22 +00001269 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1270 ichspi_lock = 1;
1271 }
Stefan Tauner1e146392011-09-15 23:52:55 +00001272 if (tmp2 & HSFS_FDV)
1273 ichspi_desc = 1;
Stefan Tauner55206942011-06-11 09:53:22 +00001274
Stefan Tauner29c80832011-06-12 08:14:10 +00001275 tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
Stefan Tauner55206942011-06-11 09:53:22 +00001276 msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
1277 prettyprint_ich9_reg_hsfc(tmp2);
Michael Karchera4448d92010-07-22 18:04:15 +00001278
Stefan Tauner5ffe65b2011-07-07 04:10:57 +00001279 tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
1280 msg_pdbg("0x08: 0x%08x (FADDR)\n", tmp);
Stefan Tauner29c80832011-06-12 08:14:10 +00001281 tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
Michael Karchera4448d92010-07-22 18:04:15 +00001282 msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1283 msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1284 msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1285 msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1286 msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1287
1288 /* print out the FREGx registers along with FRAP access bits */
1289 for(i = 0; i < 5; i++)
1290 do_ich9_spi_frap(tmp, i);
1291
1292 msg_pdbg("0x74: 0x%08x (PR0)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001293 mmio_readl(ich_spibar + ICH9_REG_PR0));
Michael Karchera4448d92010-07-22 18:04:15 +00001294 msg_pdbg("0x78: 0x%08x (PR1)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001295 mmio_readl(ich_spibar + ICH9_REG_PR1));
Michael Karchera4448d92010-07-22 18:04:15 +00001296 msg_pdbg("0x7C: 0x%08x (PR2)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001297 mmio_readl(ich_spibar + ICH9_REG_PR2));
Michael Karchera4448d92010-07-22 18:04:15 +00001298 msg_pdbg("0x80: 0x%08x (PR3)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001299 mmio_readl(ich_spibar + ICH9_REG_PR3));
Michael Karchera4448d92010-07-22 18:04:15 +00001300 msg_pdbg("0x84: 0x%08x (PR4)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001301 mmio_readl(ich_spibar + ICH9_REG_PR4));
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001302
Stefan Tauner29c80832011-06-12 08:14:10 +00001303 tmp = mmio_readl(ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001304 msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001305 prettyprint_ich9_reg_ssfs(tmp);
Stefan Tauner29c80832011-06-12 08:14:10 +00001306 if (tmp & SSFS_FCERR) {
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001307 msg_pdbg("Clearing SSFS.FCERR\n");
Stefan Tauner29c80832011-06-12 08:14:10 +00001308 mmio_writeb(SSFS_FCERR, ich_spibar + ICH9_REG_SSFS);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001309 }
Stefan Tauner2a8b2622011-06-11 09:53:16 +00001310 msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8);
1311 prettyprint_ich9_reg_ssfc(tmp);
Carl-Daniel Hailfingereacbd162011-03-17 00:10:25 +00001312
Michael Karchera4448d92010-07-22 18:04:15 +00001313 msg_pdbg("0x94: 0x%04x (PREOP)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001314 mmio_readw(ich_spibar + ICH9_REG_PREOP));
Michael Karchera4448d92010-07-22 18:04:15 +00001315 msg_pdbg("0x96: 0x%04x (OPTYPE)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001316 mmio_readw(ich_spibar + ICH9_REG_OPTYPE));
Michael Karchera4448d92010-07-22 18:04:15 +00001317 msg_pdbg("0x98: 0x%08x (OPMENU)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001318 mmio_readl(ich_spibar + ICH9_REG_OPMENU));
Michael Karchera4448d92010-07-22 18:04:15 +00001319 msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
Stefan Tauner29c80832011-06-12 08:14:10 +00001320 mmio_readl(ich_spibar + ICH9_REG_OPMENU + 4));
Stefan Tauner1e146392011-09-15 23:52:55 +00001321 if (ich_generation == 8) {
1322 tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC);
1323 msg_pdbg("0xC1: 0x%08x (VSCC)\n", tmp);
1324 msg_pdbg("VSCC: ");
1325 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1326 } else {
1327 ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
1328 msg_pdbg("0xA0: 0x%08x (BBAR)\n",
1329 ichspi_bbar);
Stefan Taunerbd649e42011-07-01 00:39:16 +00001330
Stefan Tauner1e146392011-09-15 23:52:55 +00001331 tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC);
1332 msg_pdbg("0xC4: 0x%08x (LVSCC)\n", tmp);
1333 msg_pdbg("LVSCC: ");
1334 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1335
1336 tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC);
1337 msg_pdbg("0xC8: 0x%08x (UVSCC)\n", tmp);
1338 msg_pdbg("UVSCC: ");
1339 prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1340
1341 tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
1342 msg_pdbg("0xD0: 0x%08x (FPB)\n", tmp);
1343 }
1344
1345 msg_pdbg("\n");
1346 if (ichspi_desc) {
1347 struct ich_descriptors desc = {{ 0 }};
1348 if (read_ich_descriptors_via_fdo(ich_spibar, &desc) ==
1349 ICH_RET_OK)
1350 prettyprint_ich_descriptors(CHIPSET_ICH_UNKNOWN,
1351 &desc);
1352 }
Michael Karchera4448d92010-07-22 18:04:15 +00001353 ich_init_opcodes();
1354 break;
1355 default:
1356 /* Nothing */
1357 break;
1358 }
1359
1360 old = pci_read_byte(dev, 0xdc);
1361 msg_pdbg("SPI Read Configuration: ");
1362 new = (old >> 2) & 0x3;
1363 switch (new) {
1364 case 0:
1365 case 1:
1366 case 2:
1367 msg_pdbg("prefetching %sabled, caching %sabled, ",
1368 (new & 0x2) ? "en" : "dis",
1369 (new & 0x1) ? "dis" : "en");
1370 break;
1371 default:
1372 msg_pdbg("invalid prefetching/caching settings, ");
1373 break;
1374 }
1375 return 0;
1376}
1377
Michael Karcherb9dbe482011-05-11 17:07:07 +00001378static const struct spi_programmer spi_programmer_via = {
1379 .type = SPI_CONTROLLER_VIA,
1380 .max_data_read = 16,
1381 .max_data_write = 16,
1382 .command = ich_spi_send_command,
1383 .multicommand = ich_spi_send_multicommand,
1384 .read = default_spi_read,
1385 .write_256 = default_spi_write_256,
1386};
1387
Michael Karchera4448d92010-07-22 18:04:15 +00001388int via_init_spi(struct pci_dev *dev)
1389{
1390 uint32_t mmio_base;
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001391 int i;
Michael Karchera4448d92010-07-22 18:04:15 +00001392
1393 mmio_base = (pci_read_long(dev, 0xbc)) << 8;
1394 msg_pdbg("MMIO base at = 0x%x\n", mmio_base);
1395 ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
1396
Michael Karchera4448d92010-07-22 18:04:15 +00001397 /* Not sure if it speaks all these bus protocols. */
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001398 buses_supported = BUS_LPC | BUS_FWH;
Michael Karcherb9dbe482011-05-11 17:07:07 +00001399 register_spi_programmer(&spi_programmer_via);
Carl-Daniel Hailfinger841d6312010-11-24 23:37:22 +00001400
1401 msg_pdbg("0x00: 0x%04x (SPIS)\n", mmio_readw(ich_spibar + 0));
1402 msg_pdbg("0x02: 0x%04x (SPIC)\n", mmio_readw(ich_spibar + 2));
1403 msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1404 for (i = 0; i < 2; i++) {
1405 int offs;
1406 offs = 8 + (i * 8);
1407 msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1408 mmio_readl(ich_spibar + offs), i);
1409 msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1410 mmio_readl(ich_spibar + offs + 4), i);
1411 }
1412 ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1413 msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1414 msg_pdbg("0x54: 0x%04x (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1415 msg_pdbg("0x56: 0x%04x (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1416 msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1417 msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1418 for (i = 0; i < 3; i++) {
1419 int offs;
1420 offs = 0x60 + (i * 4);
1421 msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1422 mmio_readl(ich_spibar + offs), i);
1423 }
1424 msg_pdbg("0x6c: 0x%04x (CLOCK/DEBUG)\n",
1425 mmio_readw(ich_spibar + 0x6c));
1426 if (mmio_readw(ich_spibar) & (1 << 15)) {
1427 msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1428 ichspi_lock = 1;
1429 }
1430
Michael Karchera4448d92010-07-22 18:04:15 +00001431 ich_init_opcodes();
1432
1433 return 0;
1434}
1435
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001436#endif