source: trunk/ichspi.c @ 1531

Revision 1531, 56.5 KB checked in by stefanct, 2 weeks ago (diff)

Refine reprogram_opcode_on_the_fly to indicate wrong readcnt/writecnt combinations.

Signed-off-by: Stefan Tauner <stefan.tauner@…>
Acked-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@…>

Line 
1/*
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>
7 * Copyright (C) 2008 coresystems GmbH <info@coresystems.de>
8 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
9 * Copyright (C) 2011 Stefan Tauner
10 *
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
24 */
25
26#if defined(__i386__) || defined(__x86_64__)
27
28#include <string.h>
29#include <stdlib.h>
30#include "flash.h"
31#include "programmer.h"
32#include "spi.h"
33#include "ich_descriptors.h"
34
35/* ICH9 controller register definition */
36#define ICH9_REG_HSFS           0x04    /* 16 Bits Hardware Sequencing Flash Status */
37#define HSFS_FDONE_OFF          0       /* 0: Flash Cycle Done */
38#define HSFS_FDONE              (0x1 << HSFS_FDONE_OFF)
39#define HSFS_FCERR_OFF          1       /* 1: Flash Cycle Error */
40#define HSFS_FCERR              (0x1 << HSFS_FCERR_OFF)
41#define HSFS_AEL_OFF            2       /* 2: Access Error Log */
42#define HSFS_AEL                (0x1 << HSFS_AEL_OFF)
43#define HSFS_BERASE_OFF         3       /* 3-4: Block/Sector Erase Size */
44#define HSFS_BERASE             (0x3 << HSFS_BERASE_OFF)
45#define HSFS_SCIP_OFF           5       /* 5: SPI Cycle In Progress */
46#define HSFS_SCIP               (0x1 << HSFS_SCIP_OFF)
47                                        /* 6-12: reserved */
48#define HSFS_FDOPSS_OFF         13      /* 13: Flash Descriptor Override Pin-Strap Status */
49#define HSFS_FDOPSS             (0x1 << HSFS_FDOPSS_OFF)
50#define HSFS_FDV_OFF            14      /* 14: Flash Descriptor Valid */
51#define HSFS_FDV                (0x1 << HSFS_FDV_OFF)
52#define HSFS_FLOCKDN_OFF        15      /* 15: Flash Configuration Lock-Down */
53#define HSFS_FLOCKDN            (0x1 << HSFS_FLOCKDN_OFF)
54
55#define ICH9_REG_HSFC           0x06    /* 16 Bits Hardware Sequencing Flash Control */
56#define HSFC_FGO_OFF            0       /* 0: Flash Cycle Go */
57#define HSFC_FGO                (0x1 << HSFC_FGO_OFF)
58#define HSFC_FCYCLE_OFF         1       /* 1-2: FLASH Cycle */
59#define HSFC_FCYCLE             (0x3 << HSFC_FCYCLE_OFF)
60                                        /* 3-7: reserved */
61#define HSFC_FDBC_OFF           8       /* 8-13: Flash Data Byte Count */
62#define HSFC_FDBC               (0x3f << HSFC_FDBC_OFF)
63                                        /* 14: reserved */
64#define HSFC_SME_OFF            15      /* 15: SPI SMI# Enable */
65#define HSFC_SME                (0x1 << HSFC_SME_OFF)
66
67#define ICH9_REG_FADDR          0x08    /* 32 Bits */
68#define ICH9_REG_FDATA0         0x10    /* 64 Bytes */
69
70#define ICH9_REG_FRAP           0x50    /* 32 Bytes Flash Region Access Permissions */
71#define ICH9_REG_FREG0          0x54    /* 32 Bytes Flash Region 0 */
72
73#define ICH9_REG_PR0            0x74    /* 32 Bytes Protected Range 0 */
74#define PR_WP_OFF               31      /* 31: write protection enable */
75#define PR_RP_OFF               15      /* 15: read protection enable */
76
77#define ICH9_REG_SSFS           0x90    /* 08 Bits */
78#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)
86/* The following bits are reserved in SSFS: 1,5-7. */
87#define SSFS_RESERVED_MASK      0x000000e2
88
89#define ICH9_REG_SSFC           0x91    /* 24 Bits */
90/* We combine SSFS and SSFC to one 32-bit word,
91 * 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 */
113#define SSFC_RESERVED_MASK      0xf8008100
114
115#define ICH9_REG_PREOP          0x94    /* 16 Bits */
116#define ICH9_REG_OPTYPE         0x96    /* 16 Bits */
117#define ICH9_REG_OPMENU         0x98    /* 64 Bits */
118
119#define ICH9_REG_BBAR           0xA0    /* 32 Bits BIOS Base Address Configuration */
120#define BBAR_MASK       0x00ffff00              /* 8-23: Bottom of System Flash */
121
122#define ICH8_REG_VSCC           0xC1    /* 32 Bits Vendor Specific Component Capabilities */
123#define ICH9_REG_LVSCC          0xC4    /* 32 Bits Host Lower Vendor Specific Component Capabilities */
124#define ICH9_REG_UVSCC          0xC8    /* 32 Bits Host Upper Vendor Specific Component Capabilities */
125/* The individual fields of the VSCC registers are defined in the file
126 * ich_descriptors.h. The reason is that the same layout is also used in the
127 * flash descriptor to define the properties of the different flash chips
128 * supported. The BIOS (or the ME?) is responsible to populate the ICH registers
129 * with the information from the descriptor on startup depending on the actual
130 * chip(s) detected. */
131
132#define ICH9_REG_FPB            0xD0    /* 32 Bits Flash Partition Boundary */
133#define FPB_FPBA_OFF            0       /* 0-12: Block/Sector Erase Size */
134#define FPB_FPBA                        (0x1FFF << FPB_FPBA_OFF)
135
136// ICH9R SPI commands
137#define SPI_OPCODE_TYPE_READ_NO_ADDRESS         0
138#define SPI_OPCODE_TYPE_WRITE_NO_ADDRESS        1
139#define SPI_OPCODE_TYPE_READ_WITH_ADDRESS       2
140#define SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS      3
141
142// ICH7 registers
143#define ICH7_REG_SPIS           0x00    /* 16 Bits */
144#define SPIS_SCIP               0x0001
145#define SPIS_GRANT              0x0002
146#define SPIS_CDS                0x0004
147#define SPIS_FCERR              0x0008
148#define SPIS_RESERVED_MASK      0x7ff0
149
150/* VIA SPI is compatible with ICH7, but maxdata
151   to transfer is 16 bytes.
152
153   DATA byte count on ICH7 is 8:13, on VIA 8:11
154
155   bit 12 is port select CS0 CS1
156   bit 13 is FAST READ enable
157   bit 7  is used with fast read and one shot controls CS de-assert?
158*/
159
160#define ICH7_REG_SPIC           0x02    /* 16 Bits */
161#define SPIC_SCGO               0x0002
162#define SPIC_ACS                0x0004
163#define SPIC_SPOP               0x0008
164#define SPIC_DS                 0x4000
165
166#define ICH7_REG_SPIA           0x04    /* 32 Bits */
167#define ICH7_REG_SPID0          0x08    /* 64 Bytes */
168#define ICH7_REG_PREOP          0x54    /* 16 Bits */
169#define ICH7_REG_OPTYPE         0x56    /* 16 Bits */
170#define ICH7_REG_OPMENU         0x58    /* 64 Bits */
171
172/* ICH SPI configuration lock-down. May be set during chipset enabling. */
173static int ichspi_lock = 0;
174
175static enum ich_chipset ich_generation = CHIPSET_ICH_UNKNOWN;
176uint32_t ichspi_bbar = 0;
177
178static void *ich_spibar = NULL;
179
180typedef 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
186/* Suggested opcode definition:
187 * 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
196 * OP 6: chip private (read JEDEC id)
197 * OP 7: Chip erase
198 */
199typedef struct _OPCODES {
200        uint8_t preop[2];
201        OPCODE opcode[8];
202} OPCODES;
203
204static OPCODES *curopcodes = NULL;
205
206/* HW access functions */
207static uint32_t REGREAD32(int X)
208{
209        return mmio_readl(ich_spibar + X);
210}
211
212static uint16_t REGREAD16(int X)
213{
214        return mmio_readw(ich_spibar + X);
215}
216
217static uint16_t REGREAD8(int X)
218{
219        return mmio_readb(ich_spibar + X);
220}
221
222#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))
225
226/* Common SPI functions */
227static int find_opcode(OPCODES *op, uint8_t opcode);
228static int find_preop(OPCODES *op, uint8_t preop);
229static int generate_opcodes(OPCODES * op);
230static int program_opcodes(OPCODES *op, int enable_undo);
231static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
232                      uint8_t datalength, uint8_t * data);
233
234/* for pairing opcodes with their required preop */
235struct preop_opcode_pair {
236        uint8_t preop;
237        uint8_t opcode;
238};
239
240/* List of opcodes which need preopcodes and matching preopcodes. Unused. */
241const struct preop_opcode_pair pops[] = {
242        {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 */
248         /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
249        {JEDEC_WREN, JEDEC_WRSR},
250        {JEDEC_EWSR, JEDEC_WRSR},
251        {0,}
252};
253
254/* Reasonable default configuration. Needs ad-hoc modifications if we
255 * encounter unlisted opcodes. Fun.
256 */
257static OPCODES O_ST_M25P = {
258        {
259         JEDEC_WREN,
260         JEDEC_EWSR,
261        },
262        {
263         {JEDEC_BYTE_PROGRAM, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0},   // Write Byte
264         {JEDEC_READ, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0},    // Read Data
265         {JEDEC_BE_D8, SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS, 0},  // Erase Sector
266         {JEDEC_RDSR, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0},      // Read Device Status Reg
267         {JEDEC_REMS, SPI_OPCODE_TYPE_READ_WITH_ADDRESS, 0},    // Read Electronic Manufacturer Signature
268         {JEDEC_WRSR, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0},     // Write Status Register
269         {JEDEC_RDID, SPI_OPCODE_TYPE_READ_NO_ADDRESS, 0},      // Read JDEC ID
270         {JEDEC_CE_C7, SPI_OPCODE_TYPE_WRITE_NO_ADDRESS, 0},    // Bulk erase
271        }
272};
273
274/* 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
292static OPCODES O_EXISTING = {};
293
294/* pretty printing functions */
295static void prettyprint_opcodes(OPCODES *ops)
296{
297        OPCODE oc;
298        const char *t;
299        const char *a;
300        uint8_t i;
301        static const char *const spi_type[4] = {
302                "read  w/o addr",
303                "write w/o addr",
304                "read  w/  addr",
305                "write w/  addr"
306        };
307        static const char *const atomic_type[3] = {
308                "none",
309                " 0  ",
310                " 1  "
311        };
312
313        if (ops == NULL)
314                return;
315
316        msg_pdbg2("        OP        Type      Pre-OP\n");
317        for (i = 0; i < 8; i++) {
318                oc = ops->opcode[i];
319                t = (oc.spi_type > 3) ? "invalid" : spi_type[oc.spi_type];
320                a = (oc.atomic > 2) ? "invalid" : atomic_type[oc.atomic];
321                msg_pdbg2("op[%d]: 0x%02x, %s, %s\n", i, oc.opcode, t, a);
322        }
323        msg_pdbg2("Pre-OP 0: 0x%02x, Pre-OP 1: 0x%02x\n", ops->preop[0],
324                 ops->preop[1]);
325}
326
327#define pprint_reg(reg, bit, val, sep) msg_pdbg("%s=%d" sep, #bit, (val & reg##_##bit)>>reg##_##bit##_OFF)
328
329static void prettyprint_ich9_reg_hsfs(uint16_t reg_val)
330{
331        msg_pdbg("HSFS: ");
332        pprint_reg(HSFS, FDONE, reg_val, ", ");
333        pprint_reg(HSFS, FCERR, reg_val, ", ");
334        pprint_reg(HSFS, AEL, reg_val, ", ");
335        pprint_reg(HSFS, BERASE, reg_val, ", ");
336        pprint_reg(HSFS, SCIP, reg_val, ", ");
337        pprint_reg(HSFS, FDOPSS, reg_val, ", ");
338        pprint_reg(HSFS, FDV, reg_val, ", ");
339        pprint_reg(HSFS, FLOCKDN, reg_val, "\n");
340}
341
342static void prettyprint_ich9_reg_hsfc(uint16_t reg_val)
343{
344        msg_pdbg("HSFC: ");
345        pprint_reg(HSFC, FGO, reg_val, ", ");
346        pprint_reg(HSFC, FCYCLE, reg_val, ", ");
347        pprint_reg(HSFC, FDBC, reg_val, ", ");
348        pprint_reg(HSFC, SME, reg_val, "\n");
349}
350
351static void prettyprint_ich9_reg_ssfs(uint32_t reg_val)
352{
353        msg_pdbg("SSFS: ");
354        pprint_reg(SSFS, SCIP, reg_val, ", ");
355        pprint_reg(SSFS, FDONE, reg_val, ", ");
356        pprint_reg(SSFS, FCERR, reg_val, ", ");
357        pprint_reg(SSFS, AEL, reg_val, "\n");
358}
359
360static void prettyprint_ich9_reg_ssfc(uint32_t reg_val)
361{
362        msg_pdbg("SSFC: ");
363        pprint_reg(SSFC, SCGO, reg_val, ", ");
364        pprint_reg(SSFC, ACS, reg_val, ", ");
365        pprint_reg(SSFC, SPOP, reg_val, ", ");
366        pprint_reg(SSFC, COP, reg_val, ", ");
367        pprint_reg(SSFC, DBC, reg_val, ", ");
368        pprint_reg(SSFC, SME, reg_val, ", ");
369        pprint_reg(SSFC, SCF, reg_val, "\n");
370}
371
372static uint8_t lookup_spi_type(uint8_t opcode)
373{
374        int a;
375
376        for (a = 0; a < ARRAY_SIZE(POSSIBLE_OPCODES); a++) {
377                if (POSSIBLE_OPCODES[a].opcode == opcode)
378                        return POSSIBLE_OPCODES[a].spi_type;
379        }
380
381        return 0xFF;
382}
383
384static int reprogram_opcode_on_the_fly(uint8_t opcode, unsigned int writecnt, unsigned int readcnt)
385{
386        uint8_t spi_type;
387
388        spi_type = lookup_spi_type(opcode);
389        if (spi_type > 3) {
390                /* Try to guess spi type from read/write sizes.
391                 * The following valid writecnt/readcnt combinations exist:
392                 * writecnt  = 4, readcnt >= 0
393                 * writecnt  = 1, readcnt >= 0
394                 * writecnt >= 4, readcnt  = 0
395                 * writecnt >= 1, readcnt  = 0
396                 * writecnt >= 1 is guaranteed for all commands.
397                 */
398                if (readcnt == 0)
399                        /* if readcnt=0 and writecount >= 4, we don't know if it is WRITE_NO_ADDRESS
400                         * or WRITE_WITH_ADDRESS. But if we use WRITE_NO_ADDRESS and the first 3 data
401                         * bytes are actual the address, they go to the bus anyhow
402                         */
403                        spi_type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
404                else if (writecnt == 1) // and readcnt is > 0
405                        spi_type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
406                else if (writecnt == 4) // and readcnt is > 0
407                        spi_type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
408                else // we have an invalid case
409                        return SPI_INVALID_LENGTH;
410        }
411        int oppos = 2;  // use original JEDEC_BE_D8 offset
412        curopcodes->opcode[oppos].opcode = opcode;
413        curopcodes->opcode[oppos].spi_type = spi_type;
414        program_opcodes(curopcodes, 0);
415        oppos = find_opcode(curopcodes, opcode);
416        msg_pdbg ("on-the-fly OPCODE (0x%02X) re-programmed, op-pos=%d\n", opcode, oppos);
417        return oppos;
418}
419
420static int find_opcode(OPCODES *op, uint8_t opcode)
421{
422        int a;
423
424        if (op == NULL) {
425                msg_perr("\n%s: null OPCODES pointer!\n", __func__);
426                return -1;
427        }
428
429        for (a = 0; a < 8; a++) {
430                if (op->opcode[a].opcode == opcode)
431                        return a;
432        }
433
434        return -1;
435}
436
437static int find_preop(OPCODES *op, uint8_t preop)
438{
439        int a;
440
441        if (op == NULL) {
442                msg_perr("\n%s: null OPCODES pointer!\n", __func__);
443                return -1;
444        }
445
446        for (a = 0; a < 2; a++) {
447                if (op->preop[a] == preop)
448                        return a;
449        }
450
451        return -1;
452}
453
454/* Create a struct OPCODES based on what we find in the locked down chipset. */
455static int generate_opcodes(OPCODES * op)
456{
457        int a;
458        uint16_t preop, optype;
459        uint32_t opmenu[2];
460
461        if (op == NULL) {
462                msg_perr("\n%s: null OPCODES pointer!\n", __func__);
463                return -1;
464        }
465
466        switch (ich_generation) {
467        case CHIPSET_ICH7:
468                preop = REGREAD16(ICH7_REG_PREOP);
469                optype = REGREAD16(ICH7_REG_OPTYPE);
470                opmenu[0] = REGREAD32(ICH7_REG_OPMENU);
471                opmenu[1] = REGREAD32(ICH7_REG_OPMENU + 4);
472                break;
473        case CHIPSET_ICH8:
474        default:                /* Future version might behave the same */
475                preop = REGREAD16(ICH9_REG_PREOP);
476                optype = REGREAD16(ICH9_REG_OPTYPE);
477                opmenu[0] = REGREAD32(ICH9_REG_OPMENU);
478                opmenu[1] = REGREAD32(ICH9_REG_OPMENU + 4);
479                break;
480        }
481
482        op->preop[0] = (uint8_t) preop;
483        op->preop[1] = (uint8_t) (preop >> 8);
484
485        for (a = 0; a < 8; a++) {
486                op->opcode[a].spi_type = (uint8_t) (optype & 0x3);
487                optype >>= 2;
488        }
489
490        for (a = 0; a < 4; a++) {
491                op->opcode[a].opcode = (uint8_t) (opmenu[0] & 0xff);
492                opmenu[0] >>= 8;
493        }
494
495        for (a = 4; a < 8; a++) {
496                op->opcode[a].opcode = (uint8_t) (opmenu[1] & 0xff);
497                opmenu[1] >>= 8;
498        }
499
500        /* No preopcodes used by default. */
501        for (a = 0; a < 8; a++)
502                op->opcode[a].atomic = 0;
503
504        return 0;
505}
506
507static int program_opcodes(OPCODES *op, int enable_undo)
508{
509        uint8_t a;
510        uint16_t preop, optype;
511        uint32_t opmenu[2];
512
513        /* Program Prefix Opcodes */
514        /* 0:7 Prefix Opcode 1 */
515        preop = (op->preop[0]);
516        /* 8:16 Prefix Opcode 2 */
517        preop |= ((uint16_t) op->preop[1]) << 8;
518
519        /* Program Opcode Types 0 - 7 */
520        optype = 0;
521        for (a = 0; a < 8; a++) {
522                optype |= ((uint16_t) op->opcode[a].spi_type) << (a * 2);
523        }
524
525        /* Program Allowable Opcodes 0 - 3 */
526        opmenu[0] = 0;
527        for (a = 0; a < 4; a++) {
528                opmenu[0] |= ((uint32_t) op->opcode[a].opcode) << (a * 8);
529        }
530
531        /*Program Allowable Opcodes 4 - 7 */
532        opmenu[1] = 0;
533        for (a = 4; a < 8; a++) {
534                opmenu[1] |= ((uint32_t) op->opcode[a].opcode) << ((a - 4) * 8);
535        }
536
537        msg_pdbg("\n%s: preop=%04x optype=%04x opmenu=%08x%08x\n", __func__, preop, optype, opmenu[0], opmenu[1]);
538        switch (ich_generation) {
539        case CHIPSET_ICH7:
540                /* Register undo only for enable_undo=1, i.e. first call. */
541                if (enable_undo) {
542                        rmmio_valw(ich_spibar + ICH7_REG_PREOP);
543                        rmmio_valw(ich_spibar + ICH7_REG_OPTYPE);
544                        rmmio_vall(ich_spibar + ICH7_REG_OPMENU);
545                        rmmio_vall(ich_spibar + ICH7_REG_OPMENU + 4);
546                }
547                mmio_writew(preop, ich_spibar + ICH7_REG_PREOP);
548                mmio_writew(optype, ich_spibar + ICH7_REG_OPTYPE);
549                mmio_writel(opmenu[0], ich_spibar + ICH7_REG_OPMENU);
550                mmio_writel(opmenu[1], ich_spibar + ICH7_REG_OPMENU + 4);
551                break;
552        case CHIPSET_ICH8:
553        default:                /* Future version might behave the same */
554                /* Register undo only for enable_undo=1, i.e. first call. */
555                if (enable_undo) {
556                        rmmio_valw(ich_spibar + ICH9_REG_PREOP);
557                        rmmio_valw(ich_spibar + ICH9_REG_OPTYPE);
558                        rmmio_vall(ich_spibar + ICH9_REG_OPMENU);
559                        rmmio_vall(ich_spibar + ICH9_REG_OPMENU + 4);
560                }
561                mmio_writew(preop, ich_spibar + ICH9_REG_PREOP);
562                mmio_writew(optype, ich_spibar + ICH9_REG_OPTYPE);
563                mmio_writel(opmenu[0], ich_spibar + ICH9_REG_OPMENU);
564                mmio_writel(opmenu[1], ich_spibar + ICH9_REG_OPMENU + 4);
565                break;
566        }
567
568        return 0;
569}
570
571/*
572 * Returns -1 if at least one mandatory opcode is inaccessible, 0 otherwise.
573 * FIXME: this should also check for
574 *   - at least one probing opcode (RDID (incl. AT25F variants?), REMS, RES?)
575 *   - at least one erasing opcode (lots.)
576 *   - at least one program opcode (BYTE_PROGRAM, AAI_WORD_PROGRAM, ...?)
577 *   - necessary preops? (EWSR, WREN, ...?)
578 */
579static int ich_missing_opcodes()
580{
581        uint8_t ops[] = {
582                JEDEC_READ,
583                JEDEC_RDSR,
584                0
585        };
586        int i = 0;
587        while (ops[i] != 0) {
588                msg_pspew("checking for opcode 0x%02x\n", ops[i]);
589                if (find_opcode(curopcodes, ops[i]) == -1)
590                        return -1;
591                i++;
592        }
593        return 0;
594}
595
596/*
597 * Try to set BBAR (BIOS Base Address Register), but read back the value in case
598 * it didn't stick.
599 */
600static void ich_set_bbar(uint32_t min_addr)
601{
602        int bbar_off;
603        switch (ich_generation) {
604        case CHIPSET_ICH7:
605                bbar_off = 0x50;
606                break;
607        case CHIPSET_ICH8:
608                msg_perr("BBAR offset is unknown on ICH8!\n");
609                return;
610        case CHIPSET_ICH9:
611        default:                /* Future version might behave the same */
612                bbar_off = ICH9_REG_BBAR;
613                break;
614        }
615       
616        ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & ~BBAR_MASK;
617        if (ichspi_bbar) {
618                msg_pdbg("Reserved bits in BBAR not zero: 0x%08x\n",
619                         ichspi_bbar);
620        }
621        min_addr &= BBAR_MASK;
622        ichspi_bbar |= min_addr;
623        rmmio_writel(ichspi_bbar, ich_spibar + bbar_off);
624        ichspi_bbar = mmio_readl(ich_spibar + bbar_off) & BBAR_MASK;
625
626        /* We don't have any option except complaining. And if the write
627         * failed, the restore will fail as well, so no problem there.
628         */
629        if (ichspi_bbar != min_addr)
630                msg_perr("Setting BBAR to 0x%08x failed! New value: 0x%08x.\n",
631                         min_addr, ichspi_bbar);
632}
633
634/* Read len bytes from the fdata/spid register into the data array.
635 *
636 * Note that using len > flash->pgm->spi.max_data_read will return garbage or
637 * may even crash.
638 */
639static void ich_read_data(uint8_t *data, int len, int reg0_off)
640 {
641        int i;
642        uint32_t temp32 = 0;
643
644        for (i = 0; i < len; i++) {
645                if ((i % 4) == 0)
646                        temp32 = REGREAD32(reg0_off + i);
647
648                data[i] = (temp32 >> ((i % 4) * 8)) & 0xff;
649        }
650}
651
652/* Fill len bytes from the data array into the fdata/spid registers.
653 *
654 * Note that using len > flash->pgm->spi.max_data_write will trash the registers
655 * following the data registers.
656 */
657static void ich_fill_data(const uint8_t *data, int len, int reg0_off)
658{
659        uint32_t temp32 = 0;
660        int i;
661
662        if (len <= 0)
663                return;
664
665        for (i = 0; i < len; i++) {
666                if ((i % 4) == 0)
667                        temp32 = 0;
668
669                temp32 |= ((uint32_t) data[i]) << ((i % 4) * 8);
670
671                if ((i % 4) == 3) /* 32 bits are full, write them to regs. */
672                        REGWRITE32(reg0_off + (i - (i % 4)), temp32);
673        }
674        i--;
675        if ((i % 4) != 3) /* Write remaining data to regs. */
676                REGWRITE32(reg0_off + (i - (i % 4)), temp32);
677}
678
679/* This function generates OPCODES from or programs OPCODES to ICH according to
680 * the chipset's SPI configuration lock.
681 *
682 * It should be called before ICH sends any spi command.
683 */
684static int ich_init_opcodes(void)
685{
686        int rc = 0;
687        OPCODES *curopcodes_done;
688
689        if (curopcodes)
690                return 0;
691
692        if (ichspi_lock) {
693                msg_pdbg("Reading OPCODES... ");
694                curopcodes_done = &O_EXISTING;
695                rc = generate_opcodes(curopcodes_done);
696        } else {
697                msg_pdbg("Programming OPCODES... ");
698                curopcodes_done = &O_ST_M25P;
699                rc = program_opcodes(curopcodes_done, 1);
700        }
701
702        if (rc) {
703                curopcodes = NULL;
704                msg_perr("failed\n");
705                return 1;
706        } else {
707                curopcodes = curopcodes_done;
708                msg_pdbg("done\n");
709                prettyprint_opcodes(curopcodes);
710                return 0;
711        }
712}
713
714static int ich7_run_opcode(OPCODE op, uint32_t offset,
715                           uint8_t datalength, uint8_t * data, int maxdata)
716{
717        int write_cmd = 0;
718        int timeout;
719        uint32_t temp32;
720        uint16_t temp16;
721        uint64_t opmenu;
722        int opcode_index;
723
724        /* Is it a write command? */
725        if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
726            || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
727                write_cmd = 1;
728        }
729
730        timeout = 100 * 60;     /* 60 ms are 9.6 million cycles at 16 MHz. */
731        while ((REGREAD16(ICH7_REG_SPIS) & SPIS_SCIP) && --timeout) {
732                programmer_delay(10);
733        }
734        if (!timeout) {
735                msg_perr("Error: SCIP never cleared!\n");
736                return 1;
737        }
738
739        /* Program offset in flash into SPIA while preserving reserved bits. */
740        temp32 = REGREAD32(ICH7_REG_SPIA) & ~0x00FFFFFF;
741        REGWRITE32(ICH7_REG_SPIA, (offset & 0x00FFFFFF) | temp32);
742
743        /* Program data into SPID0 to N */
744        if (write_cmd && (datalength != 0))
745                ich_fill_data(data, datalength, ICH7_REG_SPID0);
746
747        /* Assemble SPIS */
748        temp16 = REGREAD16(ICH7_REG_SPIS);
749        /* keep reserved bits */
750        temp16 &= SPIS_RESERVED_MASK;
751        /* clear error status registers */
752        temp16 |= (SPIS_CDS | SPIS_FCERR);
753        REGWRITE16(ICH7_REG_SPIS, temp16);
754
755        /* Assemble SPIC */
756        temp16 = 0;
757
758        if (datalength != 0) {
759                temp16 |= SPIC_DS;
760                temp16 |= ((uint32_t) ((datalength - 1) & (maxdata - 1))) << 8;
761        }
762
763        /* Select opcode */
764        opmenu = REGREAD32(ICH7_REG_OPMENU);
765        opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
766
767        for (opcode_index = 0; opcode_index < 8; opcode_index++) {
768                if ((opmenu & 0xff) == op.opcode) {
769                        break;
770                }
771                opmenu >>= 8;
772        }
773        if (opcode_index == 8) {
774                msg_pdbg("Opcode %x not found.\n", op.opcode);
775                return 1;
776        }
777        temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
778
779        timeout = 100 * 60;     /* 60 ms are 9.6 million cycles at 16 MHz. */
780        /* Handle Atomic. Atomic commands include three steps:
781            - sending the preop (mainly EWSR or WREN)
782            - sending the main command
783            - waiting for the busy bit (WIP) to be cleared
784           This means the timeout must be sufficient for chip erase
785           of slow high-capacity chips.
786         */
787        switch (op.atomic) {
788        case 2:
789                /* Select second preop. */
790                temp16 |= SPIC_SPOP;
791                /* And fall through. */
792        case 1:
793                /* Atomic command (preop+op) */
794                temp16 |= SPIC_ACS;
795                timeout = 100 * 1000 * 60;      /* 60 seconds */
796                break;
797        }
798
799        /* Start */
800        temp16 |= SPIC_SCGO;
801
802        /* write it */
803        REGWRITE16(ICH7_REG_SPIC, temp16);
804
805        /* Wait for Cycle Done Status or Flash Cycle Error. */
806        while (((REGREAD16(ICH7_REG_SPIS) & (SPIS_CDS | SPIS_FCERR)) == 0) &&
807               --timeout) {
808                programmer_delay(10);
809        }
810        if (!timeout) {
811                msg_perr("timeout, ICH7_REG_SPIS=0x%04x\n",
812                         REGREAD16(ICH7_REG_SPIS));
813                return 1;
814        }
815
816        /* FIXME: make sure we do not needlessly cause transaction errors. */
817        temp16 = REGREAD16(ICH7_REG_SPIS);
818        if (temp16 & SPIS_FCERR) {
819                msg_perr("Transaction error!\n");
820                /* keep reserved bits */
821                temp16 &= SPIS_RESERVED_MASK;
822                REGWRITE16(ICH7_REG_SPIS, temp16 | SPIS_FCERR);
823                return 1;
824        }
825
826        if ((!write_cmd) && (datalength != 0))
827                ich_read_data(data, datalength, ICH7_REG_SPID0);
828
829        return 0;
830}
831
832static int ich9_run_opcode(OPCODE op, uint32_t offset,
833                           uint8_t datalength, uint8_t * data)
834{
835        int write_cmd = 0;
836        int timeout;
837        uint32_t temp32;
838        uint64_t opmenu;
839        int opcode_index;
840
841        /* Is it a write command? */
842        if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
843            || (op.spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS)) {
844                write_cmd = 1;
845        }
846
847        timeout = 100 * 60;     /* 60 ms are 9.6 million cycles at 16 MHz. */
848        while ((REGREAD8(ICH9_REG_SSFS) & SSFS_SCIP) && --timeout) {
849                programmer_delay(10);
850        }
851        if (!timeout) {
852                msg_perr("Error: SCIP never cleared!\n");
853                return 1;
854        }
855
856        /* Program offset in flash into FADDR while preserve the reserved bits
857         * and clearing the 25. address bit which is only useable in hwseq. */
858        temp32 = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
859        REGWRITE32(ICH9_REG_FADDR, (offset & 0x00FFFFFF) | temp32);
860
861        /* Program data into FDATA0 to N */
862        if (write_cmd && (datalength != 0))
863                ich_fill_data(data, datalength, ICH9_REG_FDATA0);
864
865        /* Assemble SSFS + SSFC */
866        temp32 = REGREAD32(ICH9_REG_SSFS);
867        /* Keep reserved bits only */
868        temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
869        /* Clear cycle done and cycle error status registers */
870        temp32 |= (SSFS_FDONE | SSFS_FCERR);
871        REGWRITE32(ICH9_REG_SSFS, temp32);
872
873        /* Use 20 MHz */
874        temp32 |= SSFC_SCF_20MHZ;
875
876        /* Set data byte count (DBC) and data cycle bit (DS) */
877        if (datalength != 0) {
878                uint32_t datatemp;
879                temp32 |= SSFC_DS;
880                datatemp = ((((uint32_t)datalength - 1) << SSFC_DBC_OFF) &
881                            SSFC_DBC);
882                temp32 |= datatemp;
883        }
884
885        /* Select opcode */
886        opmenu = REGREAD32(ICH9_REG_OPMENU);
887        opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
888
889        for (opcode_index = 0; opcode_index < 8; opcode_index++) {
890                if ((opmenu & 0xff) == op.opcode) {
891                        break;
892                }
893                opmenu >>= 8;
894        }
895        if (opcode_index == 8) {
896                msg_pdbg("Opcode %x not found.\n", op.opcode);
897                return 1;
898        }
899        temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
900
901        timeout = 100 * 60;     /* 60 ms are 9.6 million cycles at 16 MHz. */
902        /* Handle Atomic. Atomic commands include three steps:
903            - sending the preop (mainly EWSR or WREN)
904            - sending the main command
905            - waiting for the busy bit (WIP) to be cleared
906           This means the timeout must be sufficient for chip erase
907           of slow high-capacity chips.
908         */
909        switch (op.atomic) {
910        case 2:
911                /* Select second preop. */
912                temp32 |= SSFC_SPOP;
913                /* And fall through. */
914        case 1:
915                /* Atomic command (preop+op) */
916                temp32 |= SSFC_ACS;
917                timeout = 100 * 1000 * 60;      /* 60 seconds */
918                break;
919        }
920
921        /* Start */
922        temp32 |= SSFC_SCGO;
923
924        /* write it */
925        REGWRITE32(ICH9_REG_SSFS, temp32);
926
927        /* Wait for Cycle Done Status or Flash Cycle Error. */
928        while (((REGREAD32(ICH9_REG_SSFS) & (SSFS_FDONE | SSFS_FCERR)) == 0) &&
929               --timeout) {
930                programmer_delay(10);
931        }
932        if (!timeout) {
933                msg_perr("timeout, ICH9_REG_SSFS=0x%08x\n",
934                         REGREAD32(ICH9_REG_SSFS));
935                return 1;
936        }
937
938        /* FIXME make sure we do not needlessly cause transaction errors. */
939        temp32 = REGREAD32(ICH9_REG_SSFS);
940        if (temp32 & SSFS_FCERR) {
941                msg_perr("Transaction error!\n");
942                prettyprint_ich9_reg_ssfs(temp32);
943                prettyprint_ich9_reg_ssfc(temp32);
944                /* keep reserved bits */
945                temp32 &= SSFS_RESERVED_MASK | SSFC_RESERVED_MASK;
946                /* Clear the transaction error. */
947                REGWRITE32(ICH9_REG_SSFS, temp32 | SSFS_FCERR);
948                return 1;
949        }
950
951        if ((!write_cmd) && (datalength != 0))
952                ich_read_data(data, datalength, ICH9_REG_FDATA0);
953
954        return 0;
955}
956
957static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
958                      uint8_t datalength, uint8_t * data)
959{
960        /* max_data_read == max_data_write for all Intel/VIA SPI masters */
961        uint8_t maxlength = flash->pgm->spi.max_data_read;
962
963        if (ich_generation == CHIPSET_ICH_UNKNOWN) {
964                msg_perr("%s: unsupported chipset\n", __func__);
965                return -1;
966        }
967
968        if (datalength > maxlength) {
969                msg_perr("%s: Internal command size error for "
970                        "opcode 0x%02x, got datalength=%i, want <=%i\n",
971                        __func__, op.opcode, datalength, maxlength);
972                return SPI_INVALID_LENGTH;
973        }
974
975        switch (ich_generation) {
976        case CHIPSET_ICH7:
977                return ich7_run_opcode(op, offset, datalength, data, maxlength);
978        case CHIPSET_ICH8:
979        default:                /* Future version might behave the same */
980                return ich9_run_opcode(op, offset, datalength, data);
981        }
982}
983
984static int ich_spi_send_command(struct flashctx *flash, unsigned int writecnt,
985                                unsigned int readcnt,
986                                const unsigned char *writearr,
987                                unsigned char *readarr)
988{
989        int result;
990        int opcode_index = -1;
991        const unsigned char cmd = *writearr;
992        OPCODE *opcode;
993        uint32_t addr = 0;
994        uint8_t *data;
995        int count;
996
997        /* find cmd in opcodes-table */
998        opcode_index = find_opcode(curopcodes, cmd);
999        if (opcode_index == -1) {
1000                if (!ichspi_lock)
1001                        opcode_index = reprogram_opcode_on_the_fly(cmd, writecnt, readcnt);
1002                if (opcode_index == SPI_INVALID_LENGTH) {
1003                        msg_pdbg("OPCODE 0x%02x has unsupported length, will not execute.\n", cmd);
1004                        return SPI_INVALID_LENGTH;
1005                } else if (opcode_index == -1) {
1006                        msg_pdbg("Invalid OPCODE 0x%02x, will not execute.\n",
1007                                 cmd);
1008                        return SPI_INVALID_OPCODE;
1009                }
1010        }
1011
1012        opcode = &(curopcodes->opcode[opcode_index]);
1013
1014        /* The following valid writecnt/readcnt combinations exist:
1015         * writecnt  = 4, readcnt >= 0
1016         * writecnt  = 1, readcnt >= 0
1017         * writecnt >= 4, readcnt  = 0
1018         * writecnt >= 1, readcnt  = 0
1019         * writecnt >= 1 is guaranteed for all commands.
1020         */
1021        if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS) &&
1022            (writecnt != 4)) {
1023                msg_perr("%s: Internal command size error for opcode "
1024                        "0x%02x, got writecnt=%i, want =4\n", __func__, cmd,
1025                        writecnt);
1026                return SPI_INVALID_LENGTH;
1027        }
1028        if ((opcode->spi_type == SPI_OPCODE_TYPE_READ_NO_ADDRESS) &&
1029            (writecnt != 1)) {
1030                msg_perr("%s: Internal command size error for opcode "
1031                        "0x%02x, got writecnt=%i, want =1\n", __func__, cmd,
1032                        writecnt);
1033                return SPI_INVALID_LENGTH;
1034        }
1035        if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) &&
1036            (writecnt < 4)) {
1037                msg_perr("%s: Internal command size error for opcode "
1038                        "0x%02x, got writecnt=%i, want >=4\n", __func__, cmd,
1039                        writecnt);
1040                return SPI_INVALID_LENGTH;
1041        }
1042        if (((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1043             (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) &&
1044            (readcnt)) {
1045                msg_perr("%s: Internal command size error for opcode "
1046                        "0x%02x, got readcnt=%i, want =0\n", __func__, cmd,
1047                        readcnt);
1048                return SPI_INVALID_LENGTH;
1049        }
1050
1051        /* if opcode-type requires an address */
1052        if (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS ||
1053            opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1054                addr = (writearr[1] << 16) |
1055                    (writearr[2] << 8) | (writearr[3] << 0);
1056                if (addr < ichspi_bbar) {
1057                        msg_perr("%s: Address 0x%06x below allowed "
1058                                 "range 0x%06x-0xffffff\n", __func__,
1059                                 addr, ichspi_bbar);
1060                        return SPI_INVALID_ADDRESS;
1061                }
1062        }
1063
1064        /* Translate read/write array/count.
1065         * The maximum data length is identical for the maximum read length and
1066         * for the maximum write length excluding opcode and address. Opcode and
1067         * address are stored in separate registers, not in the data registers
1068         * and are thus not counted towards data length. The only exception
1069         * applies if the opcode definition (un)intentionally classifies said
1070         * opcode incorrectly as non-address opcode or vice versa. */
1071        if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS) {
1072                data = (uint8_t *) (writearr + 1);
1073                count = writecnt - 1;
1074        } else if (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) {
1075                data = (uint8_t *) (writearr + 4);
1076                count = writecnt - 4;
1077        } else {
1078                data = (uint8_t *) readarr;
1079                count = readcnt;
1080        }
1081
1082        result = run_opcode(flash, *opcode, addr, count, data);
1083        if (result) {
1084                msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
1085                if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1086                    (opcode->spi_type == SPI_OPCODE_TYPE_READ_WITH_ADDRESS)) {
1087                        msg_pdbg("at address 0x%06x ", addr);
1088                }
1089                msg_pdbg("(payload length was %d).\n", count);
1090
1091                /* Print out the data array if it contains data to write.
1092                 * Errors are detected before the received data is read back into
1093                 * the array so it won't make sense to print it then. */
1094                if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
1095                    (opcode->spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)) {
1096                        int i;
1097                        msg_pspew("The data was:\n");
1098                        for (i = 0; i < count; i++){
1099                                msg_pspew("%3d: 0x%02x\n", i, data[i]);
1100                        }
1101                }
1102        }
1103
1104        return result;
1105}
1106
1107static struct hwseq_data {
1108        uint32_t size_comp0;
1109        uint32_t size_comp1;
1110} hwseq_data;
1111
1112/* Sets FLA in FADDR to (addr & 0x01FFFFFF) without touching other bits. */
1113static void ich_hwseq_set_addr(uint32_t addr)
1114{
1115        uint32_t addr_old = REGREAD32(ICH9_REG_FADDR) & ~0x01FFFFFF;
1116        REGWRITE32(ICH9_REG_FADDR, (addr & 0x01FFFFFF) | addr_old);
1117}
1118
1119/* Sets FADDR.FLA to 'addr' and returns the erase block size in bytes
1120 * of the block containing this address. May return nonsense if the address is
1121 * not valid. The erase block size for a specific address depends on the flash
1122 * partition layout as specified by FPB and the partition properties as defined
1123 * by UVSCC and LVSCC respectively. An alternative to implement this method
1124 * would be by querying FPB and the respective VSCC register directly.
1125 */
1126static uint32_t ich_hwseq_get_erase_block_size(unsigned int addr)
1127{
1128        uint8_t enc_berase;
1129        static const uint32_t const dec_berase[4] = {
1130                256,
1131                4 * 1024,
1132                8 * 1024,
1133                64 * 1024
1134        };
1135
1136        ich_hwseq_set_addr(addr);
1137        enc_berase = (REGREAD16(ICH9_REG_HSFS) & HSFS_BERASE) >>
1138                     HSFS_BERASE_OFF;
1139        return dec_berase[enc_berase];
1140}
1141
1142/* Polls for Cycle Done Status, Flash Cycle Error or timeout in 8 us intervals.
1143   Resets all error flags in HSFS.
1144   Returns 0 if the cycle completes successfully without errors within
1145   timeout us, 1 on errors. */
1146static int ich_hwseq_wait_for_cycle_complete(unsigned int timeout,
1147                                             unsigned int len)
1148{
1149        uint16_t hsfs;
1150        uint32_t addr;
1151
1152        timeout /= 8; /* scale timeout duration to counter */
1153        while ((((hsfs = REGREAD16(ICH9_REG_HSFS)) &
1154                 (HSFS_FDONE | HSFS_FCERR)) == 0) &&
1155               --timeout) {
1156                programmer_delay(8);
1157        }
1158        REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1159        if (!timeout) {
1160                addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
1161                msg_perr("Timeout error between offset 0x%08x and "
1162                         "0x%08x (= 0x%08x + %d)!\n",
1163                         addr, addr + len - 1, addr, len - 1);
1164                prettyprint_ich9_reg_hsfs(hsfs);
1165                prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1166                return 1;
1167        }
1168
1169        if (hsfs & HSFS_FCERR) {
1170                addr = REGREAD32(ICH9_REG_FADDR) & 0x01FFFFFF;
1171                msg_perr("Transaction error between offset 0x%08x and "
1172                         "0x%08x (= 0x%08x + %d)!\n",
1173                         addr, addr + len - 1, addr, len - 1);
1174                prettyprint_ich9_reg_hsfs(hsfs);
1175                prettyprint_ich9_reg_hsfc(REGREAD16(ICH9_REG_HSFC));
1176                return 1;
1177        }
1178        return 0;
1179}
1180
1181static int ich_hwseq_probe(struct flashctx *flash)
1182{
1183        uint32_t total_size, boundary;
1184        uint32_t erase_size_low, size_low, erase_size_high, size_high;
1185        struct block_eraser *eraser;
1186
1187        total_size = hwseq_data.size_comp0 + hwseq_data.size_comp1;
1188        msg_cdbg("Found %d attached SPI flash chip",
1189                 (hwseq_data.size_comp1 != 0) ? 2 : 1);
1190        if (hwseq_data.size_comp1 != 0)
1191                msg_cdbg("s with a combined");
1192        else
1193                msg_cdbg(" with a");
1194        msg_cdbg(" density of %d kB.\n", total_size / 1024);
1195        flash->total_size = total_size / 1024;
1196
1197        eraser = &(flash->block_erasers[0]);
1198        boundary = (REGREAD32(ICH9_REG_FPB) & FPB_FPBA) << 12;
1199        size_high = total_size - boundary;
1200        erase_size_high = ich_hwseq_get_erase_block_size(boundary);
1201
1202        if (boundary == 0) {
1203                msg_cdbg("There is only one partition containing the whole "
1204                         "address space (0x%06x - 0x%06x).\n", 0, size_high-1);
1205                eraser->eraseblocks[0].size = erase_size_high;
1206                eraser->eraseblocks[0].count = size_high / erase_size_high;
1207                msg_cdbg("There are %d erase blocks with %d B each.\n",
1208                         size_high / erase_size_high, erase_size_high);
1209        } else {
1210                msg_cdbg("The flash address space (0x%06x - 0x%06x) is divided "
1211                         "at address 0x%06x in two partitions.\n",
1212                         0, size_high-1, boundary);
1213                size_low = total_size - size_high;
1214                erase_size_low = ich_hwseq_get_erase_block_size(0);
1215
1216                eraser->eraseblocks[0].size = erase_size_low;
1217                eraser->eraseblocks[0].count = size_low / erase_size_low;
1218                msg_cdbg("The first partition ranges from 0x%06x to 0x%06x.\n",
1219                         0, size_low-1);
1220                msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1221                         size_low / erase_size_low, erase_size_low);
1222
1223                eraser->eraseblocks[1].size = erase_size_high;
1224                eraser->eraseblocks[1].count = size_high / erase_size_high;
1225                msg_cdbg("The second partition ranges from 0x%06x to 0x%06x.\n",
1226                         boundary, size_high-1);
1227                msg_cdbg("In that range are %d erase blocks with %d B each.\n",
1228                         size_high / erase_size_high, erase_size_high);
1229        }
1230        flash->tested = TEST_OK_PREW;
1231        return 1;
1232}
1233
1234static int ich_hwseq_block_erase(struct flashctx *flash, unsigned int addr,
1235                                 unsigned int len)
1236{
1237        uint32_t erase_block;
1238        uint16_t hsfc;
1239        uint32_t timeout = 5000 * 1000; /* 5 s for max 64 kB */
1240
1241        erase_block = ich_hwseq_get_erase_block_size(addr);
1242        if (len != erase_block) {
1243                msg_cerr("Erase block size for address 0x%06x is %d B, "
1244                         "but requested erase block size is %d B. "
1245                         "Not erasing anything.\n", addr, erase_block, len);
1246                return -1;
1247        }
1248
1249        /* Although the hardware supports this (it would erase the whole block
1250         * containing the address) we play safe here. */
1251        if (addr % erase_block != 0) {
1252                msg_cerr("Erase address 0x%06x is not aligned to the erase "
1253                         "block boundary (any multiple of %d). "
1254                         "Not erasing anything.\n", addr, erase_block);
1255                return -1;
1256        }
1257
1258        if (addr + len > flash->total_size * 1024) {
1259                msg_perr("Request to erase some inaccessible memory address(es)"
1260                         " (addr=0x%x, len=%d). "
1261                         "Not erasing anything.\n", addr, len);
1262                return -1;
1263        }
1264
1265        msg_pdbg("Erasing %d bytes starting at 0x%06x.\n", len, addr);
1266
1267        /* make sure FDONE, FCERR, AEL are cleared by writing 1 to them */
1268        REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1269
1270        hsfc = REGREAD16(ICH9_REG_HSFC);
1271        hsfc &= ~HSFC_FCYCLE; /* clear operation */
1272        hsfc |= (0x3 << HSFC_FCYCLE_OFF); /* set erase operation */
1273        hsfc |= HSFC_FGO; /* start */
1274        msg_pdbg("HSFC used for block erasing: ");
1275        prettyprint_ich9_reg_hsfc(hsfc);
1276        REGWRITE16(ICH9_REG_HSFC, hsfc);
1277
1278        if (ich_hwseq_wait_for_cycle_complete(timeout, len))
1279                return -1;
1280        return 0;
1281}
1282
1283static int ich_hwseq_read(struct flashctx *flash, uint8_t *buf,
1284                          unsigned int addr, unsigned int len)
1285{
1286        uint16_t hsfc;
1287        uint16_t timeout = 100 * 60;
1288        uint8_t block_len;
1289
1290        if (addr + len > flash->total_size * 1024) {
1291                msg_perr("Request to read from an inaccessible memory address "
1292                         "(addr=0x%x, len=%d).\n", addr, len);
1293                return -1;
1294        }
1295
1296        msg_pdbg("Reading %d bytes starting at 0x%06x.\n", len, addr);
1297        /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1298        REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1299
1300        while (len > 0) {
1301                block_len = min(len, flash->pgm->opaque.max_data_read);
1302                ich_hwseq_set_addr(addr);
1303                hsfc = REGREAD16(ICH9_REG_HSFC);
1304                hsfc &= ~HSFC_FCYCLE; /* set read operation */
1305                hsfc &= ~HSFC_FDBC; /* clear byte count */
1306                /* set byte count */
1307                hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1308                hsfc |= HSFC_FGO; /* start */
1309                REGWRITE16(ICH9_REG_HSFC, hsfc);
1310
1311                if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
1312                        return 1;
1313                ich_read_data(buf, block_len, ICH9_REG_FDATA0);
1314                addr += block_len;
1315                buf += block_len;
1316                len -= block_len;
1317        }
1318        return 0;
1319}
1320
1321static int ich_hwseq_write(struct flashctx *flash, uint8_t *buf,
1322                           unsigned int addr, unsigned int len)
1323{
1324        uint16_t hsfc;
1325        uint16_t timeout = 100 * 60;
1326        uint8_t block_len;
1327
1328        if (addr + len > flash->total_size * 1024) {
1329                msg_perr("Request to write to an inaccessible memory address "
1330                         "(addr=0x%x, len=%d).\n", addr, len);
1331                return -1;
1332        }
1333
1334        msg_pdbg("Writing %d bytes starting at 0x%06x.\n", len, addr);
1335        /* clear FDONE, FCERR, AEL by writing 1 to them (if they are set) */
1336        REGWRITE16(ICH9_REG_HSFS, REGREAD16(ICH9_REG_HSFS));
1337
1338        while (len > 0) {
1339                ich_hwseq_set_addr(addr);
1340                block_len = min(len, flash->pgm->opaque.max_data_write);
1341                ich_fill_data(buf, block_len, ICH9_REG_FDATA0);
1342                hsfc = REGREAD16(ICH9_REG_HSFC);
1343                hsfc &= ~HSFC_FCYCLE; /* clear operation */
1344                hsfc |= (0x2 << HSFC_FCYCLE_OFF); /* set write operation */
1345                hsfc &= ~HSFC_FDBC; /* clear byte count */
1346                /* set byte count */
1347                hsfc |= (((block_len - 1) << HSFC_FDBC_OFF) & HSFC_FDBC);
1348                hsfc |= HSFC_FGO; /* start */
1349                REGWRITE16(ICH9_REG_HSFC, hsfc);
1350
1351                if (ich_hwseq_wait_for_cycle_complete(timeout, block_len))
1352                        return -1;
1353                addr += block_len;
1354                buf += block_len;
1355                len -= block_len;
1356        }
1357        return 0;
1358}
1359
1360static int ich_spi_send_multicommand(struct flashctx *flash,
1361                                     struct spi_command *cmds)
1362{
1363        int ret = 0;
1364        int i;
1365        int oppos, preoppos;
1366        for (; (cmds->writecnt || cmds->readcnt) && !ret; cmds++) {
1367                if ((cmds + 1)->writecnt || (cmds + 1)->readcnt) {
1368                        /* Next command is valid. */
1369                        preoppos = find_preop(curopcodes, cmds->writearr[0]);
1370                        oppos = find_opcode(curopcodes, (cmds + 1)->writearr[0]);
1371                        if ((oppos == -1) && (preoppos != -1)) {
1372                                /* Current command is listed as preopcode in
1373                                 * ICH struct OPCODES, but next command is not
1374                                 * listed as opcode in that struct.
1375                                 * Check for command sanity, then
1376                                 * try to reprogram the ICH opcode list.
1377                                 */
1378                                if (find_preop(curopcodes,
1379                                               (cmds + 1)->writearr[0]) != -1) {
1380                                        msg_perr("%s: Two subsequent "
1381                                                "preopcodes 0x%02x and 0x%02x, "
1382                                                "ignoring the first.\n",
1383                                                __func__, cmds->writearr[0],
1384                                                (cmds + 1)->writearr[0]);
1385                                        continue;
1386                                }
1387                                /* If the chipset is locked down, we'll fail
1388                                 * during execution of the next command anyway.
1389                                 * No need to bother with fixups.
1390                                 */
1391                                if (!ichspi_lock) {
1392                                        oppos = reprogram_opcode_on_the_fly((cmds + 1)->writearr[0], (cmds + 1)->writecnt, (cmds + 1)->readcnt);
1393                                        if (oppos == -1)
1394                                                continue;
1395                                        curopcodes->opcode[oppos].atomic = preoppos + 1;
1396                                        continue;
1397                                }
1398                        }
1399                        if ((oppos != -1) && (preoppos != -1)) {
1400                                /* Current command is listed as preopcode in
1401                                 * ICH struct OPCODES and next command is listed
1402                                 * as opcode in that struct. Match them up.
1403                                 */
1404                                curopcodes->opcode[oppos].atomic = preoppos + 1;
1405                                continue;
1406                        }
1407                        /* If none of the above if-statements about oppos or
1408                         * preoppos matched, this is a normal opcode.
1409                         */
1410                }
1411                ret = ich_spi_send_command(flash, cmds->writecnt, cmds->readcnt,
1412                                           cmds->writearr, cmds->readarr);
1413                /* Reset the type of all opcodes to non-atomic. */
1414                for (i = 0; i < 8; i++)
1415                        curopcodes->opcode[i].atomic = 0;
1416        }
1417        return ret;
1418}
1419
1420#define ICH_BMWAG(x) ((x >> 24) & 0xff)
1421#define ICH_BMRAG(x) ((x >> 16) & 0xff)
1422#define ICH_BRWA(x)  ((x >>  8) & 0xff)
1423#define ICH_BRRA(x)  ((x >>  0) & 0xff)
1424
1425/* returns 0 if region is unused or r/w */
1426static int ich9_handle_frap(uint32_t frap, int i)
1427{
1428        static const char *const access_names[4] = {
1429                "locked", "read-only", "write-only", "read-write"
1430        };
1431        static const char *const region_names[5] = {
1432                "Flash Descriptor", "BIOS", "Management Engine",
1433                "Gigabit Ethernet", "Platform Data"
1434        };
1435        uint32_t base, limit;
1436        int rwperms = (((ICH_BRWA(frap) >> i) & 1) << 1) |
1437                      (((ICH_BRRA(frap) >> i) & 1) << 0);
1438        int offset = ICH9_REG_FREG0 + i * 4;
1439        uint32_t freg = mmio_readl(ich_spibar + offset);
1440
1441        base  = ICH_FREG_BASE(freg);
1442        limit = ICH_FREG_LIMIT(freg);
1443        if (base > limit) {
1444                /* this FREG is disabled */
1445                msg_pdbg2("0x%02X: 0x%08x FREG%i: %s region is unused.\n",
1446                          offset, freg, i, region_names[i]);
1447                return 0;
1448        }
1449        msg_pdbg("0x%02X: 0x%08x ", offset, freg);
1450        if (rwperms == 0x3) {
1451                msg_pdbg("FREG%i: %s region (0x%08x-0x%08x) is %s.\n", i,
1452                         region_names[i], base, (limit | 0x0fff),
1453                         access_names[rwperms]);
1454                return 0;
1455        }
1456
1457        msg_pinfo("FREG%i: WARNING: %s region (0x%08x-0x%08x) is %s.\n", i,
1458                  region_names[i], base, (limit | 0x0fff),
1459                  access_names[rwperms]);
1460        return 1;
1461}
1462
1463        /* In contrast to FRAP and the master section of the descriptor the bits
1464         * in the PR registers have an inverted meaning. The bits in FRAP
1465         * indicate read and write access _grant_. Here they indicate read
1466         * and write _protection_ respectively. If both bits are 0 the address
1467         * bits are ignored.
1468         */
1469#define ICH_PR_PERMS(pr)        (((~((pr) >> PR_RP_OFF) & 1) << 0) | \
1470                                 ((~((pr) >> PR_WP_OFF) & 1) << 1))
1471
1472/* returns 0 if range is unused (i.e. r/w) */
1473static int ich9_handle_pr(int i)
1474{
1475        static const char *const access_names[3] = {
1476                "locked", "read-only", "write-only"
1477        };
1478        uint8_t off = ICH9_REG_PR0 + (i * 4);
1479        uint32_t pr = mmio_readl(ich_spibar + off);
1480        unsigned int rwperms = ICH_PR_PERMS(pr);
1481
1482        if (rwperms == 0x3) {
1483                msg_pdbg2("0x%02X: 0x%08x (PR%u is unused)\n", off, pr, i);
1484                return 0;
1485        }
1486
1487        msg_pdbg("0x%02X: 0x%08x ", off, pr);
1488        msg_pinfo("PR%u: WARNING: 0x%08x-0x%08x is %s.\n", i, ICH_FREG_BASE(pr),
1489                  ICH_FREG_LIMIT(pr) | 0x0fff, access_names[rwperms]);
1490        return 1;
1491}
1492
1493/* Set/Clear the read and write protection enable bits of PR register @i
1494 * according to @read_prot and @write_prot. */
1495static void ich9_set_pr(int i, int read_prot, int write_prot)
1496{
1497        void *addr = ich_spibar + ICH9_REG_PR0 + (i * 4);
1498        uint32_t old = mmio_readl(addr);
1499        uint32_t new;
1500
1501        msg_gspew("PR%u is 0x%08x", i, old);
1502        new = old & ~((1 << PR_RP_OFF) | (1 << PR_WP_OFF));
1503        if (read_prot)
1504                new |= (1 << PR_RP_OFF);
1505        if (write_prot)
1506                new |= (1 << PR_WP_OFF);
1507        if (old == new) {
1508                msg_gspew(" already.\n");
1509                return;
1510        }
1511        msg_gspew(", trying to set it to 0x%08x ", new);
1512        rmmio_writel(new, addr);
1513        msg_gspew("resulted in 0x%08x.\n", mmio_readl(addr));
1514}
1515
1516static const struct spi_programmer spi_programmer_ich7 = {
1517        .type = SPI_CONTROLLER_ICH7,
1518        .max_data_read = 64,
1519        .max_data_write = 64,
1520        .command = ich_spi_send_command,
1521        .multicommand = ich_spi_send_multicommand,
1522        .read = default_spi_read,
1523        .write_256 = default_spi_write_256,
1524};
1525
1526static const struct spi_programmer spi_programmer_ich9 = {
1527        .type = SPI_CONTROLLER_ICH9,
1528        .max_data_read = 64,
1529        .max_data_write = 64,
1530        .command = ich_spi_send_command,
1531        .multicommand = ich_spi_send_multicommand,
1532        .read = default_spi_read,
1533        .write_256 = default_spi_write_256,
1534};
1535
1536static const struct opaque_programmer opaque_programmer_ich_hwseq = {
1537        .max_data_read = 64,
1538        .max_data_write = 64,
1539        .probe = ich_hwseq_probe,
1540        .read = ich_hwseq_read,
1541        .write = ich_hwseq_write,
1542        .erase = ich_hwseq_block_erase,
1543};
1544
1545int ich_init_spi(struct pci_dev *dev, uint32_t base, void *rcrb,
1546                 enum ich_chipset ich_gen)
1547{
1548        int i;
1549        uint8_t old, new;
1550        uint16_t spibar_offset, tmp2;
1551        uint32_t tmp;
1552        char *arg;
1553        int ich_spi_force = 0;
1554        int ich_spi_rw_restricted = 0;
1555        int desc_valid = 0;
1556        struct ich_descriptors desc = {{ 0 }};
1557        enum ich_spi_mode {
1558                ich_auto,
1559                ich_hwseq,
1560                ich_swseq
1561        } ich_spi_mode = ich_auto;
1562
1563        ich_generation = ich_gen;
1564
1565        switch (ich_generation) {
1566        case CHIPSET_ICH_UNKNOWN:
1567                return ERROR_FATAL;
1568        case CHIPSET_ICH7:
1569        case CHIPSET_ICH8:
1570                spibar_offset = 0x3020;
1571                break;
1572        case CHIPSET_ICH9:
1573        default:                /* Future version might behave the same */
1574                spibar_offset = 0x3800;
1575                break;
1576        }
1577
1578        /* SPIBAR is at RCRB+0x3020 for ICH[78] and RCRB+0x3800 for ICH9. */
1579        msg_pdbg("SPIBAR = 0x%x + 0x%04x\n", base, spibar_offset);
1580
1581        /* Assign Virtual Address */
1582        ich_spibar = rcrb + spibar_offset;
1583
1584        switch (ich_generation) {
1585        case CHIPSET_ICH7:
1586                msg_pdbg("0x00: 0x%04x     (SPIS)\n",
1587                             mmio_readw(ich_spibar + 0));
1588                msg_pdbg("0x02: 0x%04x     (SPIC)\n",
1589                             mmio_readw(ich_spibar + 2));
1590                msg_pdbg("0x04: 0x%08x (SPIA)\n",
1591                             mmio_readl(ich_spibar + 4));
1592                for (i = 0; i < 8; i++) {
1593                        int offs;
1594                        offs = 8 + (i * 8);
1595                        msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1596                                     mmio_readl(ich_spibar + offs), i);
1597                        msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1598                                     mmio_readl(ich_spibar + offs + 4), i);
1599                }
1600                ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1601                msg_pdbg("0x50: 0x%08x (BBAR)\n",
1602                             ichspi_bbar);
1603                msg_pdbg("0x54: 0x%04x     (PREOP)\n",
1604                             mmio_readw(ich_spibar + 0x54));
1605                msg_pdbg("0x56: 0x%04x     (OPTYPE)\n",
1606                             mmio_readw(ich_spibar + 0x56));
1607                msg_pdbg("0x58: 0x%08x (OPMENU)\n",
1608                             mmio_readl(ich_spibar + 0x58));
1609                msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n",
1610                             mmio_readl(ich_spibar + 0x5c));
1611                for (i = 0; i < 3; i++) {
1612                        int offs;
1613                        offs = 0x60 + (i * 4);
1614                        msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1615                                     mmio_readl(ich_spibar + offs), i);
1616                }
1617                if (mmio_readw(ich_spibar) & (1 << 15)) {
1618                        msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1619                        ichspi_lock = 1;
1620                }
1621                ich_init_opcodes();
1622                ich_set_bbar(0);
1623                register_spi_programmer(&spi_programmer_ich7);
1624                break;
1625        case CHIPSET_ICH8:
1626        default:                /* Future version might behave the same */
1627                arg = extract_programmer_param("ich_spi_mode");
1628                if (arg && !strcmp(arg, "hwseq")) {
1629                        ich_spi_mode = ich_hwseq;
1630                        msg_pspew("user selected hwseq\n");
1631                } else if (arg && !strcmp(arg, "swseq")) {
1632                        ich_spi_mode = ich_swseq;
1633                        msg_pspew("user selected swseq\n");
1634                } else if (arg && !strcmp(arg, "auto")) {
1635                        msg_pspew("user selected auto\n");
1636                        ich_spi_mode = ich_auto;
1637                } else if (arg && !strlen(arg)) {
1638                        msg_perr("Missing argument for ich_spi_mode.\n");
1639                        free(arg);
1640                        return ERROR_FATAL;
1641                } else if (arg) {
1642                        msg_perr("Unknown argument for ich_spi_mode: %s\n",
1643                                 arg);
1644                        free(arg);
1645                        return ERROR_FATAL;
1646                }
1647                free(arg);
1648
1649                arg = extract_programmer_param("ich_spi_force");
1650                if (arg && !strcmp(arg, "yes")) {
1651                        ich_spi_force = 1;
1652                        msg_pspew("ich_spi_force enabled.\n");
1653                } else if (arg && !strlen(arg)) {
1654                        msg_perr("Missing argument for ich_spi_force.\n");
1655                        free(arg);
1656                        return ERROR_FATAL;
1657                } else if (arg) {
1658                        msg_perr("Unknown argument for ich_spi_force: \"%s\" "
1659                                 "(not \"yes\").\n", arg);
1660                        free(arg);
1661                        return ERROR_FATAL;
1662                }
1663                free(arg);
1664
1665                tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFS);
1666                msg_pdbg("0x04: 0x%04x (HSFS)\n", tmp2);
1667                prettyprint_ich9_reg_hsfs(tmp2);
1668                if (tmp2 & HSFS_FLOCKDN) {
1669                        msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1670                        ichspi_lock = 1;
1671                }
1672                if (tmp2 & HSFS_FDV)
1673                        desc_valid = 1;
1674                if (!(tmp2 & HSFS_FDOPSS) && desc_valid)
1675                        msg_pinfo("The Flash Descriptor Security Override "
1676                                  "Strap-Pin is set. Restrictions implied\n"
1677                                  "by the FRAP and FREG registers are NOT in "
1678                                  "effect. Please note that Protected\n"
1679                                  "Range (PR) restrictions still apply.\n");
1680                ich_init_opcodes();
1681
1682                if (desc_valid) {
1683                        tmp2 = mmio_readw(ich_spibar + ICH9_REG_HSFC);
1684                        msg_pdbg("0x06: 0x%04x (HSFC)\n", tmp2);
1685                        prettyprint_ich9_reg_hsfc(tmp2);
1686                }
1687
1688                tmp = mmio_readl(ich_spibar + ICH9_REG_FADDR);
1689                msg_pdbg("0x08: 0x%08x (FADDR)\n", tmp);
1690
1691                if (desc_valid) {
1692                        tmp = mmio_readl(ich_spibar + ICH9_REG_FRAP);
1693                        msg_pdbg("0x50: 0x%08x (FRAP)\n", tmp);
1694                        msg_pdbg("BMWAG 0x%02x, ", ICH_BMWAG(tmp));
1695                        msg_pdbg("BMRAG 0x%02x, ", ICH_BMRAG(tmp));
1696                        msg_pdbg("BRWA 0x%02x, ", ICH_BRWA(tmp));
1697                        msg_pdbg("BRRA 0x%02x\n", ICH_BRRA(tmp));
1698
1699                        /* Handle FREGx and FRAP registers */
1700                        for (i = 0; i < 5; i++)
1701                                ich_spi_rw_restricted |= ich9_handle_frap(tmp, i);
1702                }
1703
1704                for (i = 0; i < 5; i++) {
1705                        /* if not locked down try to disable PR locks first */
1706                        if (!ichspi_lock)
1707                                ich9_set_pr(i, 0, 0);
1708                        ich_spi_rw_restricted |= ich9_handle_pr(i);
1709                }
1710
1711                if (ich_spi_rw_restricted) {
1712                        msg_pinfo("Please send a verbose log to "
1713                                  "flashrom@flashrom.org if this board is not "
1714                                  "listed on\n"
1715                                  "http://flashrom.org/Supported_hardware#Supported_mainboards "
1716                                  "yet.\n");
1717                        if (!ich_spi_force)
1718                                programmer_may_write = 0;
1719                        msg_pinfo("Writes have been disabled. You can enforce "
1720                                  "write support with the\nich_spi_force "
1721                                  "programmer option, but it will most likely "
1722                                  "harm your hardware!\nIf you force flashrom "
1723                                  "you will get no support if something "
1724                                  "breaks.\n");
1725                        if (ich_spi_force)
1726                                msg_pinfo("Continuing with write support "
1727                                          "because the user forced us to!\n");
1728                }
1729
1730                tmp = mmio_readl(ich_spibar + ICH9_REG_SSFS);
1731                msg_pdbg("0x90: 0x%02x (SSFS)\n", tmp & 0xff);
1732                prettyprint_ich9_reg_ssfs(tmp);
1733                if (tmp & SSFS_FCERR) {
1734                        msg_pdbg("Clearing SSFS.FCERR\n");
1735                        mmio_writeb(SSFS_FCERR, ich_spibar + ICH9_REG_SSFS);
1736                }
1737                msg_pdbg("0x91: 0x%06x (SSFC)\n", tmp >> 8);
1738                prettyprint_ich9_reg_ssfc(tmp);
1739
1740                msg_pdbg("0x94: 0x%04x     (PREOP)\n",
1741                             mmio_readw(ich_spibar + ICH9_REG_PREOP));
1742                msg_pdbg("0x96: 0x%04x     (OPTYPE)\n",
1743                             mmio_readw(ich_spibar + ICH9_REG_OPTYPE));
1744                msg_pdbg("0x98: 0x%08x (OPMENU)\n",
1745                             mmio_readl(ich_spibar + ICH9_REG_OPMENU));
1746                msg_pdbg("0x9C: 0x%08x (OPMENU+4)\n",
1747                             mmio_readl(ich_spibar + ICH9_REG_OPMENU + 4));
1748                if (ich_generation == CHIPSET_ICH8 && desc_valid) {
1749                        tmp = mmio_readl(ich_spibar + ICH8_REG_VSCC);
1750                        msg_pdbg("0xC1: 0x%08x (VSCC)\n", tmp);
1751                        msg_pdbg("VSCC: ");
1752                        prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1753                } else {
1754                        ichspi_bbar = mmio_readl(ich_spibar + ICH9_REG_BBAR);
1755                        msg_pdbg("0xA0: 0x%08x (BBAR)\n",
1756                                     ichspi_bbar);
1757
1758                        if (desc_valid) {
1759                                tmp = mmio_readl(ich_spibar + ICH9_REG_LVSCC);
1760                                msg_pdbg("0xC4: 0x%08x (LVSCC)\n", tmp);
1761                                msg_pdbg("LVSCC: ");
1762                                prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1763
1764                                tmp = mmio_readl(ich_spibar + ICH9_REG_UVSCC);
1765                                msg_pdbg("0xC8: 0x%08x (UVSCC)\n", tmp);
1766                                msg_pdbg("UVSCC: ");
1767                                prettyprint_ich_reg_vscc(tmp, MSG_DEBUG);
1768
1769                                tmp = mmio_readl(ich_spibar + ICH9_REG_FPB);
1770                                msg_pdbg("0xD0: 0x%08x (FPB)\n", tmp);
1771                        }
1772                        ich_set_bbar(0);
1773                }
1774
1775                msg_pdbg("\n");
1776                if (desc_valid) {
1777                        if (read_ich_descriptors_via_fdo(ich_spibar, &desc) ==
1778                            ICH_RET_OK)
1779                                prettyprint_ich_descriptors(CHIPSET_ICH_UNKNOWN,
1780                                                            &desc);
1781                        /* If the descriptor is valid and indicates multiple
1782                         * flash devices we need to use hwseq to be able to
1783                         * access the second flash device.
1784                         */
1785                        if (ich_spi_mode == ich_auto && desc.content.NC != 0) {
1786                                msg_pinfo("Enabling hardware sequencing due to "
1787                                          "multiple flash chips detected.\n");
1788                                ich_spi_mode = ich_hwseq;
1789                        }
1790                }
1791
1792                if (ich_spi_mode == ich_auto && ichspi_lock &&
1793                    ich_missing_opcodes()) {
1794                        msg_pinfo("Enabling hardware sequencing because "
1795                                  "some important opcode is locked.\n");
1796                        ich_spi_mode = ich_hwseq;
1797                }
1798
1799                if (ich_spi_mode == ich_hwseq) {
1800                        if (!desc_valid) {
1801                                msg_perr("Hardware sequencing was requested "
1802                                         "but the flash descriptor is not "
1803                                         "valid. Aborting.\n");
1804                                return ERROR_FATAL;
1805                        }
1806                        hwseq_data.size_comp0 = getFCBA_component_density(&desc, 0);
1807                        hwseq_data.size_comp1 = getFCBA_component_density(&desc, 1);
1808                        register_opaque_programmer(&opaque_programmer_ich_hwseq);
1809                } else {
1810                        register_spi_programmer(&spi_programmer_ich9);
1811                }
1812                break;
1813        }
1814
1815        old = pci_read_byte(dev, 0xdc);
1816        msg_pdbg("SPI Read Configuration: ");
1817        new = (old >> 2) & 0x3;
1818        switch (new) {
1819        case 0:
1820        case 1:
1821        case 2:
1822                msg_pdbg("prefetching %sabled, caching %sabled, ",
1823                             (new & 0x2) ? "en" : "dis",
1824                             (new & 0x1) ? "dis" : "en");
1825                break;
1826        default:
1827                msg_pdbg("invalid prefetching/caching settings, ");
1828                break;
1829        }
1830        return 0;
1831}
1832
1833static const struct spi_programmer spi_programmer_via = {
1834        .type = SPI_CONTROLLER_VIA,
1835        .max_data_read = 16,
1836        .max_data_write = 16,
1837        .command = ich_spi_send_command,
1838        .multicommand = ich_spi_send_multicommand,
1839        .read = default_spi_read,
1840        .write_256 = default_spi_write_256,
1841};
1842
1843int via_init_spi(struct pci_dev *dev)
1844{
1845        uint32_t mmio_base;
1846        int i;
1847
1848        mmio_base = (pci_read_long(dev, 0xbc)) << 8;
1849        msg_pdbg("MMIO base at = 0x%x\n", mmio_base);
1850        ich_spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
1851
1852        /* Not sure if it speaks all these bus protocols. */
1853        internal_buses_supported = BUS_LPC | BUS_FWH;
1854        ich_generation = CHIPSET_ICH7;
1855        register_spi_programmer(&spi_programmer_via);
1856
1857        msg_pdbg("0x00: 0x%04x     (SPIS)\n", mmio_readw(ich_spibar + 0));
1858        msg_pdbg("0x02: 0x%04x     (SPIC)\n", mmio_readw(ich_spibar + 2));
1859        msg_pdbg("0x04: 0x%08x (SPIA)\n", mmio_readl(ich_spibar + 4));
1860        for (i = 0; i < 2; i++) {
1861                int offs;
1862                offs = 8 + (i * 8);
1863                msg_pdbg("0x%02x: 0x%08x (SPID%d)\n", offs,
1864                         mmio_readl(ich_spibar + offs), i);
1865                msg_pdbg("0x%02x: 0x%08x (SPID%d+4)\n", offs + 4,
1866                         mmio_readl(ich_spibar + offs + 4), i);
1867        }
1868        ichspi_bbar = mmio_readl(ich_spibar + 0x50);
1869        msg_pdbg("0x50: 0x%08x (BBAR)\n", ichspi_bbar);
1870        msg_pdbg("0x54: 0x%04x     (PREOP)\n", mmio_readw(ich_spibar + 0x54));
1871        msg_pdbg("0x56: 0x%04x     (OPTYPE)\n", mmio_readw(ich_spibar + 0x56));
1872        msg_pdbg("0x58: 0x%08x (OPMENU)\n", mmio_readl(ich_spibar + 0x58));
1873        msg_pdbg("0x5c: 0x%08x (OPMENU+4)\n", mmio_readl(ich_spibar + 0x5c));
1874        for (i = 0; i < 3; i++) {
1875                int offs;
1876                offs = 0x60 + (i * 4);
1877                msg_pdbg("0x%02x: 0x%08x (PBR%d)\n", offs,
1878                         mmio_readl(ich_spibar + offs), i);
1879        }
1880        msg_pdbg("0x6c: 0x%04x     (CLOCK/DEBUG)\n",
1881                 mmio_readw(ich_spibar + 0x6c));
1882        if (mmio_readw(ich_spibar) & (1 << 15)) {
1883                msg_pinfo("WARNING: SPI Configuration Lockdown activated.\n");
1884                ichspi_lock = 1;
1885        }
1886
1887        ich_set_bbar(0);
1888        ich_init_opcodes();
1889
1890        return 0;
1891}
1892
1893#endif
Note: See TracBrowser for help on using the repository browser.