2 * Copyright (c) 2013 Google Inc.
3 * Copyright (C) 2015 Intel Corp.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but without any warranty; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 /* This file is derived from the flashrom project. */
18 #include <bootstate.h>
19 #include <commonlib/helpers.h>
20 #include <console/console.h>
22 #include <device/pci_ids.h>
25 #include <soc/pci_devs.h>
26 #include <spi_flash.h>
27 #include <spi-generic.h>
34 #define pci_read_config_byte(dev, reg, targ)\
35 *(targ) = pci_read_config8(dev, reg)
36 #define pci_read_config_word(dev, reg, targ)\
37 *(targ) = pci_read_config16(dev, reg)
38 #define pci_read_config_dword(dev, reg, targ)\
39 *(targ) = pci_read_config32(dev, reg)
40 #define pci_write_config_byte(dev, reg, val)\
41 pci_write_config8(dev, reg, val)
42 #define pci_write_config_word(dev, reg, val)\
43 pci_write_config16(dev, reg, val)
44 #define pci_write_config_dword(dev, reg, val)\
45 pci_write_config32(dev, reg, val)
47 #include <device/device.h>
48 #include <device/pci.h>
49 #define pci_read_config_byte(dev, reg, targ)\
50 *(targ) = pci_read_config8(dev, reg)
51 #define pci_read_config_word(dev, reg, targ)\
52 *(targ) = pci_read_config16(dev, reg)
53 #define pci_read_config_dword(dev, reg, targ)\
54 *(targ) = pci_read_config32(dev, reg)
55 #define pci_write_config_byte(dev, reg, val)\
56 pci_write_config8(dev, reg, val)
57 #define pci_write_config_word(dev, reg, val)\
58 pci_write_config16(dev, reg, val)
59 #define pci_write_config_dword(dev, reg, val)\
60 pci_write_config32(dev, reg, val)
63 typedef struct spi_slave ich_spi_slave
;
65 static int ichspi_lock
= 0;
67 typedef struct ich9_spi_regs
{
76 uint32_t _reserved1
[3];
78 uint32_t _reserved2
[2];
84 } __packed ich9_spi_regs
;
86 typedef struct ich_spi_controller
{
95 unsigned int databytes
;
100 static ich_spi_controller cntlr
;
109 SPIS_RESERVED_MASK
= 0x7ff0,
110 SSFS_RESERVED_MASK
= 0x7fe2
114 SPIC_SCGO
= 0x000002,
116 SPIC_SPOP
= 0x000008,
120 SSFC_SCF_MASK
= 0x070000,
121 SSFC_RESERVED
= 0xf80000
128 HSFS_BERASE_MASK
= 0x0018,
129 HSFS_BERASE_SHIFT
= 3,
131 HSFS_FDOPSS
= 0x2000,
133 HSFS_FLOCKDN
= 0x8000
138 HSFC_FCYCLE_MASK
= 0x0006,
139 HSFC_FCYCLE_SHIFT
= 1,
140 HSFC_FDBC_MASK
= 0x3f00,
146 SPI_OPCODE_TYPE_READ_NO_ADDRESS
= 0,
147 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
= 1,
148 SPI_OPCODE_TYPE_READ_WITH_ADDRESS
= 2,
149 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
= 3
152 #if IS_ENABLED(CONFIG_DEBUG_SPI_FLASH)
154 static u8
readb_(void *addr
)
157 printk(BIOS_DEBUG
, "0x%p --> 0x%2.2x\n", addr
, v
);
161 static u16
readw_(void *addr
)
163 u16 v
= read16(addr
);
164 printk(BIOS_DEBUG
, "0x%p --> 0x%4.4x\n", addr
, v
);
168 static u32
readl_(void *addr
)
170 u32 v
= read32(addr
);
171 printk(BIOS_DEBUG
, "0x%p --> 0x%8.8x\n", addr
, v
);
175 static void writeb_(u8 b
, void *addr
)
177 printk(BIOS_DEBUG
, "0x%p <-- 0x%2.2x\n", addr
, b
);
181 static void writew_(u16 b
, void *addr
)
183 printk(BIOS_DEBUG
, "0x%p <-- 0x%4.4x\n", addr
, b
);
187 static void writel_(u32 b
, void *addr
)
189 printk(BIOS_DEBUG
, "0x%p <-- 0x%8.8x\n", addr
, b
);
193 #else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
195 #define readb_(a) read8(a)
196 #define readw_(a) read16(a)
197 #define readl_(a) read32(a)
198 #define writeb_(val, addr) write8(addr, val)
199 #define writew_(val, addr) write16(addr, val)
200 #define writel_(val, addr) write32(addr, val)
202 #endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
204 static void write_reg(const void *value
, void *dest
, uint32_t size
)
206 const uint8_t *bvalue
= value
;
207 uint8_t *bdest
= dest
;
210 writel_(*(const uint32_t *)bvalue
, bdest
);
211 bdest
+= 4; bvalue
+= 4; size
-= 4;
214 writeb_(*bvalue
, bdest
);
215 bdest
++; bvalue
++; size
--;
219 static void read_reg(void *src
, void *value
, uint32_t size
)
222 uint8_t *bvalue
= value
;
225 *(uint32_t *)bvalue
= readl_(bsrc
);
226 bsrc
+= 4; bvalue
+= 4; size
-= 4;
229 *bvalue
= readb_(bsrc
);
230 bsrc
++; bvalue
++; size
--;
234 static ich9_spi_regs
*spi_regs(void)
240 dev
= PCI_DEV(0, LPC_DEV
, LPC_FUNC
);
242 dev
= dev_find_slot(0, PCI_DEVFN(LPC_DEV
, LPC_FUNC
));
245 printk(BIOS_ERR
, "%s: PCI device not found", __func__
);
249 pci_read_config_dword(dev
, SBASE
, &sbase
);
252 return (void *)sbase
;
257 ich9_spi_regs
*ich9_spi
;
259 ich9_spi
= spi_regs();
261 printk(BIOS_ERR
, "Not initialising spi as %s returned NULL\n",
266 ichspi_lock
= readw_(&ich9_spi
->hsfs
) & HSFS_FLOCKDN
;
267 cntlr
.opmenu
= ich9_spi
->opmenu
;
268 cntlr
.menubytes
= sizeof(ich9_spi
->opmenu
);
269 cntlr
.optype
= &ich9_spi
->optype
;
270 cntlr
.addr
= &ich9_spi
->faddr
;
271 cntlr
.data
= (uint8_t *)ich9_spi
->fdata
;
272 cntlr
.databytes
= sizeof(ich9_spi
->fdata
);
273 cntlr
.status
= &ich9_spi
->ssfs
;
274 cntlr
.control
= (uint16_t *)ich9_spi
->ssfc
;
275 cntlr
.preop
= &ich9_spi
->preop
;
278 static void spi_init_cb(void *unused
)
283 BOOT_STATE_INIT_ENTRY(BS_DEV_INIT
, BS_ON_ENTRY
, spi_init_cb
, NULL
);
285 typedef struct spi_transaction
{
295 static inline void spi_use_out(spi_transaction
*trans
, unsigned int bytes
)
298 trans
->bytesout
-= bytes
;
301 static inline void spi_use_in(spi_transaction
*trans
, unsigned int bytes
)
304 trans
->bytesin
-= bytes
;
307 static void spi_setup_type(spi_transaction
*trans
)
311 /* Try to guess spi type from read/write sizes. */
312 if (trans
->bytesin
== 0) {
313 if (trans
->bytesout
> 4)
315 * If bytesin = 0 and bytesout > 4, we presume this is
316 * a write data operation, which is accompanied by an
319 trans
->type
= SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
;
321 trans
->type
= SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
;
325 if (trans
->bytesout
== 1) { /* and bytesin is > 0 */
326 trans
->type
= SPI_OPCODE_TYPE_READ_NO_ADDRESS
;
330 if (trans
->bytesout
== 4) { /* and bytesin is > 0 */
331 trans
->type
= SPI_OPCODE_TYPE_READ_WITH_ADDRESS
;
334 /* Fast read command is called with 5 bytes instead of 4 */
335 if (trans
->out
[0] == SPI_OPCODE_FAST_READ
&& trans
->bytesout
== 5) {
336 trans
->type
= SPI_OPCODE_TYPE_READ_WITH_ADDRESS
;
341 static int spi_setup_opcode(spi_transaction
*trans
)
344 uint8_t opmenu
[cntlr
.menubytes
];
346 trans
->opcode
= trans
->out
[0];
347 spi_use_out(trans
, 1);
349 /* The lock is off, so just use index 0. */
350 writeb_(trans
->opcode
, cntlr
.opmenu
);
351 optypes
= readw_(cntlr
.optype
);
352 optypes
= (optypes
& 0xfffc) | (trans
->type
& 0x3);
353 writew_(optypes
, cntlr
.optype
);
357 /* The lock is on. See if what we need is on the menu. */
359 uint16_t opcode_index
;
361 /* Write Enable is handled as atomic prefix */
362 if (trans
->opcode
== SPI_OPCODE_WREN
)
365 read_reg(cntlr
.opmenu
, opmenu
, sizeof(opmenu
));
366 for (opcode_index
= 0; opcode_index
< cntlr
.menubytes
;
368 if (opmenu
[opcode_index
] == trans
->opcode
)
372 if (opcode_index
== cntlr
.menubytes
) {
373 printk(BIOS_DEBUG
, "ICH SPI: Opcode %x not found\n",
378 optypes
= readw_(cntlr
.optype
);
379 optype
= (optypes
>> (opcode_index
* 2)) & 0x3;
380 if (trans
->type
== SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
&&
381 optype
== SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
&&
382 trans
->bytesout
>= 3) {
383 /* We guessed wrong earlier. Fix it up. */
384 trans
->type
= optype
;
386 if (optype
!= trans
->type
) {
387 printk(BIOS_DEBUG
, "ICH SPI: Transaction doesn't fit type %d\n",
394 static int spi_setup_offset(spi_transaction
*trans
)
396 /* Separate the SPI address and data. */
397 switch (trans
->type
) {
398 case SPI_OPCODE_TYPE_READ_NO_ADDRESS
:
399 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
:
401 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS
:
402 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
:
403 trans
->offset
= ((uint32_t)trans
->out
[0] << 16) |
404 ((uint32_t)trans
->out
[1] << 8) |
405 ((uint32_t)trans
->out
[2] << 0);
406 spi_use_out(trans
, 3);
409 printk(BIOS_DEBUG
, "Unrecognized SPI transaction type %#x\n",
416 * Wait for up to 400ms til status register bit(s) turn 1 (in case wait_til_set
417 * below is True) or 0. In case the wait was for the bit(s) to set - write
418 * those bits back, which would cause resetting them.
420 * Return the last read status value on success or -1 on failure.
422 static int ich_status_poll(u16 bitmask
, int wait_til_set
)
424 int timeout
= 40000; /* This will result in 400 ms */
429 status
= readw_(cntlr
.status
);
430 if (wait_til_set
^ ((status
& bitmask
) == 0)) {
432 writew_((status
& bitmask
), cntlr
.status
);
438 printk(BIOS_ERR
, "ICH SPI: SCIP timeout, read %x, expected %x\n",
443 static int spi_ctrlr_xfer(const struct spi_slave
*slave
, const void *dout
,
444 size_t bytesout
, void *din
, size_t bytesin
)
447 int16_t opcode_index
;
451 spi_transaction trans
= {
457 /* There has to always at least be an opcode. */
458 if (!bytesout
|| !dout
) {
459 printk(BIOS_DEBUG
, "ICH SPI: No opcode for transfer\n");
462 /* Make sure if we read something we have a place to put it. */
463 if (bytesin
!= 0 && !din
) {
464 printk(BIOS_DEBUG
, "ICH SPI: Read but no target buffer\n");
468 if (ich_status_poll(SPIS_SCIP
, 0) == -1)
471 writew_(SPIS_CDS
| SPIS_FCERR
, cntlr
.status
);
473 spi_setup_type(&trans
);
474 opcode_index
= spi_setup_opcode(&trans
);
475 if (opcode_index
< 0)
477 with_address
= spi_setup_offset(&trans
);
478 if (with_address
< 0)
481 if (trans
.opcode
== SPI_OPCODE_WREN
) {
483 * Treat Write Enable as Atomic Pre-Op if possible
484 * in order to prevent the Management Engine from
485 * issuing a transaction between WREN and DATA.
488 writew_(trans
.opcode
, cntlr
.preop
);
492 /* Preset control fields */
493 control
= SPIC_SCGO
| ((opcode_index
& 0x07) << 4);
495 /* Issue atomic preop cycle if needed */
496 if (readw_(cntlr
.preop
))
499 if (!trans
.bytesout
&& !trans
.bytesin
) {
500 /* SPI addresses are 24 bit only */
502 writel_(trans
.offset
& 0x00FFFFFF, cntlr
.addr
);
505 * This is a 'no data' command (like Write Enable), its
506 * bytesout size was 1, decremented to zero while executing
507 * spi_setup_opcode() above. Tell the chip to send the
510 writew_(control
, cntlr
.control
);
512 /* wait for the result */
513 status
= ich_status_poll(SPIS_CDS
| SPIS_FCERR
, 1);
517 if (status
& SPIS_FCERR
) {
518 printk(BIOS_ERR
, "ICH SPI: Command transaction error\n");
526 * Check if this is a write command attempting to transfer more bytes
527 * than the controller can handle. Iterations for writes are not
528 * supported here because each SPI write command needs to be preceded
529 * and followed by other SPI commands, and this sequence is controlled
530 * by the SPI chip driver.
532 if (trans
.bytesout
> cntlr
.databytes
) {
534 "ICH SPI: Too much to write. Does your SPI chip driver use"
535 " CONTROLLER_PAGE_LIMIT?\n");
540 * Read or write up to databytes bytes at a time until everything has
543 while (trans
.bytesout
|| trans
.bytesin
) {
544 uint32_t data_length
;
546 /* SPI addresses are 24 bit only */
547 writel_(trans
.offset
& 0x00FFFFFF, cntlr
.addr
);
550 data_length
= min(trans
.bytesout
, cntlr
.databytes
);
552 data_length
= min(trans
.bytesin
, cntlr
.databytes
);
554 /* Program data into FDATA0 to N */
555 if (trans
.bytesout
) {
556 write_reg(trans
.out
, cntlr
.data
, data_length
);
557 spi_use_out(&trans
, data_length
);
559 trans
.offset
+= data_length
;
562 /* Add proper control fields' values */
563 control
&= ~((cntlr
.databytes
- 1) << 8);
565 control
|= (data_length
- 1) << 8;
568 writew_(control
, cntlr
.control
);
570 /* Wait for Cycle Done Status or Flash Cycle Error. */
571 status
= ich_status_poll(SPIS_CDS
| SPIS_FCERR
, 1);
575 if (status
& SPIS_FCERR
) {
576 printk(BIOS_ERR
, "ICH SPI: Data transaction error\n");
581 read_reg(cntlr
.data
, trans
.in
, data_length
);
582 spi_use_in(&trans
, data_length
);
584 trans
.offset
+= data_length
;
588 /* Clear atomic preop now that xfer is done */
589 writew_(0, cntlr
.preop
);
594 static int xfer_vectors(const struct spi_slave
*slave
,
595 struct spi_op vectors
[], size_t count
)
597 return spi_flash_vector_helper(slave
, vectors
, count
, spi_ctrlr_xfer
);
600 static const struct spi_ctrlr spi_ctrlr
= {
601 .xfer_vector
= xfer_vectors
,
602 .max_xfer_size
= member_size(ich9_spi_regs
, fdata
),
605 const struct spi_ctrlr_buses spi_ctrlr_bus_map
[] = {
613 const size_t spi_ctrlr_bus_map_count
= ARRAY_SIZE(spi_ctrlr_bus_map
);