2 * Copyright (C) 2014 Google Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 /* This file is derived from the flashrom project. */
18 #include <bootstate.h>
21 #include <console/console.h>
22 #include <device/pci_ids.h>
23 #include <spi-generic.h>
24 #include <soc/pci_devs.h>
29 #define pci_read_config_byte(dev, reg, targ)\
30 *(targ) = pci_read_config8(dev, reg)
31 #define pci_read_config_word(dev, reg, targ)\
32 *(targ) = pci_read_config16(dev, reg)
33 #define pci_read_config_dword(dev, reg, targ)\
34 *(targ) = pci_read_config32(dev, reg)
35 #define pci_write_config_byte(dev, reg, val)\
36 pci_write_config8(dev, reg, val)
37 #define pci_write_config_word(dev, reg, val)\
38 pci_write_config16(dev, reg, val)
39 #define pci_write_config_dword(dev, reg, val)\
40 pci_write_config32(dev, reg, val)
42 #include <device/device.h>
43 #include <device/pci.h>
44 #define pci_read_config_byte(dev, reg, targ)\
45 *(targ) = pci_read_config8(dev, reg)
46 #define pci_read_config_word(dev, reg, targ)\
47 *(targ) = pci_read_config16(dev, reg)
48 #define pci_read_config_dword(dev, reg, targ)\
49 *(targ) = pci_read_config32(dev, reg)
50 #define pci_write_config_byte(dev, reg, val)\
51 pci_write_config8(dev, reg, val)
52 #define pci_write_config_word(dev, reg, val)\
53 pci_write_config16(dev, reg, val)
54 #define pci_write_config_dword(dev, reg, val)\
55 pci_write_config32(dev, reg, val)
58 typedef struct spi_slave ich_spi_slave
;
60 static int ichspi_lock
= 0;
62 typedef struct ich9_spi_regs
{
71 uint32_t _reserved1
[3];
73 uint32_t _reserved2
[2];
80 uint8_t _reserved3
[12];
83 uint8_t _reserved4
[8];
87 uint8_t _reserved5
[4];
89 uint8_t _reserved6
[28];
93 } __attribute__((packed
)) ich9_spi_regs
;
95 typedef struct ich_spi_controller
{
108 } ich_spi_controller
;
110 static ich_spi_controller cntlr
;
119 SPIS_RESERVED_MASK
= 0x7ff0,
120 SSFS_RESERVED_MASK
= 0x7fe2
124 SPIC_SCGO
= 0x000002,
126 SPIC_SPOP
= 0x000008,
130 SSFC_SCF_MASK
= 0x070000,
131 SSFC_RESERVED
= 0xf80000
138 HSFS_BERASE_MASK
= 0x0018,
139 HSFS_BERASE_SHIFT
= 3,
141 HSFS_FDOPSS
= 0x2000,
143 HSFS_FLOCKDN
= 0x8000
148 HSFC_FCYCLE_MASK
= 0x0006,
149 HSFC_FCYCLE_SHIFT
= 1,
150 HSFC_FDBC_MASK
= 0x3f00,
156 SPI_OPCODE_TYPE_READ_NO_ADDRESS
= 0,
157 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
= 1,
158 SPI_OPCODE_TYPE_READ_WITH_ADDRESS
= 2,
159 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
= 3
162 #if CONFIG_DEBUG_SPI_FLASH
164 static u8
readb_(const void *addr
)
167 printk(BIOS_DEBUG
, "read %2.2x from %4.4x\n",
168 v
, ((unsigned) addr
& 0xffff) - 0xf020);
172 static u16
readw_(const void *addr
)
174 u16 v
= read16(addr
);
175 printk(BIOS_DEBUG
, "read %4.4x from %4.4x\n",
176 v
, ((unsigned) addr
& 0xffff) - 0xf020);
180 static u32
readl_(const void *addr
)
182 u32 v
= read32(addr
);
183 printk(BIOS_DEBUG
, "read %8.8x from %4.4x\n",
184 v
, ((unsigned) addr
& 0xffff) - 0xf020);
188 static void writeb_(u8 b
, const void *addr
)
191 printk(BIOS_DEBUG
, "wrote %2.2x to %4.4x\n",
192 b
, ((unsigned) addr
& 0xffff) - 0xf020);
195 static void writew_(u16 b
, const void *addr
)
198 printk(BIOS_DEBUG
, "wrote %4.4x to %4.4x\n",
199 b
, ((unsigned) addr
& 0xffff) - 0xf020);
202 static void writel_(u32 b
, const void *addr
)
205 printk(BIOS_DEBUG
, "wrote %8.8x to %4.4x\n",
206 b
, ((unsigned) addr
& 0xffff) - 0xf020);
209 #else /* CONFIG_DEBUG_SPI_FLASH ^^^ enabled vvv NOT enabled */
211 #define readb_(a) read8(a)
212 #define readw_(a) read16(a)
213 #define readl_(a) read32(a)
214 #define writeb_(val, addr) write8(addr, val)
215 #define writew_(val, addr) write16(addr, val)
216 #define writel_(val, addr) write32(addr, val)
218 #endif /* CONFIG_DEBUG_SPI_FLASH ^^^ NOT enabled */
220 static void write_reg(const void *value
, void *dest
, uint32_t size
)
222 const uint8_t *bvalue
= value
;
223 uint8_t *bdest
= dest
;
226 writel_(*(const uint32_t *)bvalue
, bdest
);
227 bdest
+= 4; bvalue
+= 4; size
-= 4;
230 writeb_(*bvalue
, bdest
);
231 bdest
++; bvalue
++; size
--;
235 static void read_reg(const void *src
, void *value
, uint32_t size
)
237 const uint8_t *bsrc
= src
;
238 uint8_t *bvalue
= value
;
241 *(uint32_t *)bvalue
= readl_(bsrc
);
242 bsrc
+= 4; bvalue
+= 4; size
-= 4;
245 *bvalue
= readb_(bsrc
);
246 bsrc
++; bvalue
++; size
--;
250 static void ich_set_bbar(uint32_t minaddr
)
252 const uint32_t bbar_mask
= 0x00ffff00;
253 uint32_t ichspi_bbar
;
255 minaddr
&= bbar_mask
;
256 ichspi_bbar
= readl_(cntlr
.bbar
) & ~bbar_mask
;
257 ichspi_bbar
|= minaddr
;
258 writel_(ichspi_bbar
, cntlr
.bbar
);
261 struct spi_slave
*spi_setup_slave(unsigned int bus
, unsigned int cs
)
263 ich_spi_slave
*slave
= malloc(sizeof(*slave
));
266 printk(BIOS_DEBUG
, "ICH SPI: Bad allocation\n");
270 memset(slave
, 0, sizeof(*slave
));
279 uint8_t *rcrb
; /* Root Complex Register Block */
280 uint32_t rcba
; /* Root Complex Base Address */
282 device_t dev
= PCH_DEV_LPC
;
283 ich9_spi_regs
*ich9_spi
;
285 pci_read_config_dword(dev
, 0xf0, &rcba
);
286 /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable. */
287 rcrb
= (uint8_t *)(rcba
& 0xffffc000);
288 ich9_spi
= (ich9_spi_regs
*)(rcrb
+ 0x3800);
289 ichspi_lock
= readw_(&ich9_spi
->hsfs
) & HSFS_FLOCKDN
;
290 cntlr
.opmenu
= ich9_spi
->opmenu
;
291 cntlr
.menubytes
= sizeof(ich9_spi
->opmenu
);
292 cntlr
.optype
= &ich9_spi
->optype
;
293 cntlr
.addr
= &ich9_spi
->faddr
;
294 cntlr
.data
= (uint8_t *)ich9_spi
->fdata
;
295 cntlr
.databytes
= sizeof(ich9_spi
->fdata
);
296 cntlr
.status
= &ich9_spi
->ssfs
;
297 cntlr
.control
= (uint16_t *)ich9_spi
->ssfc
;
298 cntlr
.bbar
= &ich9_spi
->bbar
;
299 cntlr
.preop
= &ich9_spi
->preop
;
302 /* Disable the BIOS write protect so write commands are allowed. */
303 pci_read_config_byte(dev
, 0xdc, &bios_cntl
);
304 bios_cntl
&= ~(1 << 5);
305 pci_write_config_byte(dev
, 0xdc, bios_cntl
| 0x1);
308 static void spi_init_cb(void *unused
)
313 BOOT_STATE_INIT_ENTRY(BS_DEV_INIT
, BS_ON_ENTRY
, spi_init_cb
, NULL
);
315 int spi_claim_bus(struct spi_slave
*slave
)
317 /* Handled by ICH automatically. */
321 void spi_release_bus(struct spi_slave
*slave
)
323 /* Handled by ICH automatically. */
326 typedef struct spi_transaction
{
336 static inline void spi_use_out(spi_transaction
*trans
, unsigned bytes
)
339 trans
->bytesout
-= bytes
;
342 static inline void spi_use_in(spi_transaction
*trans
, unsigned bytes
)
345 trans
->bytesin
-= bytes
;
348 static void spi_setup_type(spi_transaction
*trans
)
352 /* Try to guess spi type from read/write sizes. */
353 if (trans
->bytesin
== 0) {
354 if (trans
->bytesout
> 4)
356 * If bytesin = 0 and bytesout > 4, we presume this is
357 * a write data operation, which is accompanied by an
360 trans
->type
= SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
;
362 trans
->type
= SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
;
366 if (trans
->bytesout
== 1) { /* and bytesin is > 0 */
367 trans
->type
= SPI_OPCODE_TYPE_READ_NO_ADDRESS
;
371 if (trans
->bytesout
== 4) { /* and bytesin is > 0 */
372 trans
->type
= SPI_OPCODE_TYPE_READ_WITH_ADDRESS
;
375 /* Fast read command is called with 5 bytes instead of 4 */
376 if (trans
->out
[0] == SPI_OPCODE_FAST_READ
&& trans
->bytesout
== 5) {
377 trans
->type
= SPI_OPCODE_TYPE_READ_WITH_ADDRESS
;
382 static int spi_setup_opcode(spi_transaction
*trans
)
385 uint8_t opmenu
[cntlr
.menubytes
];
387 trans
->opcode
= trans
->out
[0];
388 spi_use_out(trans
, 1);
390 /* The lock is off, so just use index 0. */
391 writeb_(trans
->opcode
, cntlr
.opmenu
);
392 optypes
= readw_(cntlr
.optype
);
393 optypes
= (optypes
& 0xfffc) | (trans
->type
& 0x3);
394 writew_(optypes
, cntlr
.optype
);
397 /* The lock is on. See if what we need is on the menu. */
399 uint16_t opcode_index
;
401 /* Write Enable is handled as atomic prefix */
402 if (trans
->opcode
== SPI_OPCODE_WREN
)
405 read_reg(cntlr
.opmenu
, opmenu
, sizeof(opmenu
));
406 for (opcode_index
= 0; opcode_index
< cntlr
.menubytes
;
408 if (opmenu
[opcode_index
] == trans
->opcode
)
412 if (opcode_index
== cntlr
.menubytes
) {
413 printk(BIOS_DEBUG
, "ICH SPI: Opcode %x not found\n",
418 optypes
= readw_(cntlr
.optype
);
419 optype
= (optypes
>> (opcode_index
* 2)) & 0x3;
420 if (trans
->type
== SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
&&
421 optype
== SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
&&
422 trans
->bytesout
>= 3) {
423 /* We guessed wrong earlier. Fix it up. */
424 trans
->type
= optype
;
426 if (optype
!= trans
->type
) {
427 printk(BIOS_DEBUG
, "ICH SPI: Transaction doesn't fit type %d\n",
435 static int spi_setup_offset(spi_transaction
*trans
)
437 /* Separate the SPI address and data. */
438 switch (trans
->type
) {
439 case SPI_OPCODE_TYPE_READ_NO_ADDRESS
:
440 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
:
442 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS
:
443 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
:
444 trans
->offset
= ((uint32_t)trans
->out
[0] << 16) |
445 ((uint32_t)trans
->out
[1] << 8) |
446 ((uint32_t)trans
->out
[2] << 0);
447 spi_use_out(trans
, 3);
450 printk(BIOS_DEBUG
, "Unrecognized SPI transaction type %#x\n", trans
->type
);
456 * Wait for up to 60ms til status register bit(s) turn 1 (in case wait_til_set
457 * below is True) or 0. In case the wait was for the bit(s) to set - write
458 * those bits back, which would cause resetting them.
460 * Return the last read status value on success or -1 on failure.
462 static int ich_status_poll(u16 bitmask
, int wait_til_set
)
464 int timeout
= 6000; /* This will result in 60 ms */
468 status
= readw_(cntlr
.status
);
469 if (wait_til_set
^ ((status
& bitmask
) == 0)) {
471 writew_((status
& bitmask
), cntlr
.status
);
477 printk(BIOS_DEBUG
, "ICH SPI: SCIP timeout, read %x, expected %x\n",
482 unsigned int spi_crop_chunk(unsigned int cmd_len
, unsigned int buf_len
)
484 return min(cntlr
.databytes
, buf_len
);
487 int spi_xfer(struct spi_slave
*slave
, const void *dout
,
488 unsigned int bytesout
, void *din
, unsigned int bytesin
)
491 int16_t opcode_index
;
495 spi_transaction trans
= {
501 /* There has to always at least be an opcode. */
502 if (!bytesout
|| !dout
) {
503 printk(BIOS_DEBUG
, "ICH SPI: No opcode for transfer\n");
506 /* Make sure if we read something we have a place to put it. */
507 if (bytesin
!= 0 && !din
) {
508 printk(BIOS_DEBUG
, "ICH SPI: Read but no target buffer\n");
512 if (ich_status_poll(SPIS_SCIP
, 0) == -1)
515 writew_(SPIS_CDS
| SPIS_FCERR
, cntlr
.status
);
517 spi_setup_type(&trans
);
518 if ((opcode_index
= spi_setup_opcode(&trans
)) < 0)
520 if ((with_address
= spi_setup_offset(&trans
)) < 0)
523 if (trans
.opcode
== SPI_OPCODE_WREN
) {
525 * Treat Write Enable as Atomic Pre-Op if possible
526 * in order to prevent the Management Engine from
527 * issuing a transaction between WREN and DATA.
530 writew_(trans
.opcode
, cntlr
.preop
);
534 /* Preset control fields */
535 control
= SPIC_SCGO
| ((opcode_index
& 0x07) << 4);
537 /* Issue atomic preop cycle if needed */
538 if (readw_(cntlr
.preop
))
541 if (!trans
.bytesout
&& !trans
.bytesin
) {
542 /* SPI addresses are 24 bit only */
544 writel_(trans
.offset
& 0x00FFFFFF, cntlr
.addr
);
547 * This is a 'no data' command (like Write Enable), its
548 * bytesout size was 1, decremented to zero while executing
549 * spi_setup_opcode() above. Tell the chip to send the
552 writew_(control
, cntlr
.control
);
554 /* wait for the result */
555 status
= ich_status_poll(SPIS_CDS
| SPIS_FCERR
, 1);
559 if (status
& SPIS_FCERR
) {
560 printk(BIOS_DEBUG
, "ICH SPI: Command transaction error\n");
568 * Check if this is a write command attempting to transfer more bytes
569 * than the controller can handle. Iterations for writes are not
570 * supported here because each SPI write command needs to be preceded
571 * and followed by other SPI commands, and this sequence is controlled
572 * by the SPI chip driver.
574 if (trans
.bytesout
> cntlr
.databytes
) {
575 printk(BIOS_DEBUG
, "ICH SPI: Too much to write. Does your SPI chip driver use"
576 " CONTROLLER_PAGE_LIMIT?\n");
581 * Read or write up to databytes bytes at a time until everything has
584 while (trans
.bytesout
|| trans
.bytesin
) {
585 uint32_t data_length
;
587 /* SPI addresses are 24 bit only */
588 /* http://www.intel.com/content/dam/www/public/us/en/documents/datasheets/pentium-n3520-j2850-celeron-n2920-n2820-n2815-n2806-j1850-j1750-datasheet.pdf */
589 writel_(trans
.offset
& 0x00FFFFFF, cntlr
.addr
);
592 data_length
= min(trans
.bytesout
, cntlr
.databytes
);
594 data_length
= min(trans
.bytesin
, cntlr
.databytes
);
596 /* Program data into FDATA0 to N */
597 if (trans
.bytesout
) {
598 write_reg(trans
.out
, cntlr
.data
, data_length
);
599 spi_use_out(&trans
, data_length
);
601 trans
.offset
+= data_length
;
604 /* Add proper control fields' values */
605 control
&= ~((cntlr
.databytes
- 1) << 8);
607 control
|= (data_length
- 1) << 8;
610 writew_(control
, cntlr
.control
);
612 /* Wait for Cycle Done Status or Flash Cycle Error. */
613 status
= ich_status_poll(SPIS_CDS
| SPIS_FCERR
, 1);
617 if (status
& SPIS_FCERR
) {
618 printk(BIOS_DEBUG
, "ICH SPI: Data transaction error\n");
623 read_reg(cntlr
.data
, trans
.in
, data_length
);
624 spi_use_in(&trans
, data_length
);
626 trans
.offset
+= data_length
;
630 /* Clear atomic preop now that xfer is done */
631 writew_(0, cntlr
.preop
);
636 /* Use first empty Protected Range Register to cover region of flash */
637 int spi_flash_protect(u32 start
, u32 size
)
639 u32 end
= start
+ size
- 1;
643 /* Find first empty PRR */
644 for (prr
= 0; prr
< SPI_PRR_MAX
; prr
++) {
645 reg
= SPIBAR32(SPI_PRR(prr
));
649 if (prr
>= SPI_PRR_MAX
) {
650 printk(BIOS_ERR
, "ERROR: No SPI PRR free!\n");
654 /* Set protected range base and limit */
655 reg
= ((end
>> SPI_PRR_SHIFT
) & SPI_PRR_MASK
);
656 reg
<<= SPI_PRR_LIMIT_SHIFT
;
657 reg
|= ((start
>> SPI_PRR_SHIFT
) & SPI_PRR_MASK
);
660 /* Set the PRR register and verify it is protected */
661 SPIBAR32(SPI_PRR(prr
)) = reg
;
662 reg
= SPIBAR32(SPI_PRR(prr
));
663 if (!(reg
& SPI_PRR_WPE
)) {
664 printk(BIOS_ERR
, "ERROR: Unable to set SPI PRR %d\n", prr
);
668 printk(BIOS_INFO
, "%s: PRR %d is enabled for range 0x%08x-0x%08x\n",
669 __func__
, prr
, start
, end
);