2 * Copyright (c) 2011 The Chromium OS Authors.
3 * Copyright (C) 2013-2014 Sage Electronic Engineering, LLC.
4 * Copyright (C) 2016 Siemens AG
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but without any warranty; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 /* This file is derived from the flashrom project. */
22 #include <commonlib/helpers.h>
25 #include <console/console.h>
26 #include <device/pci_ids.h>
27 #include <spi_flash.h>
28 #include <spi-generic.h>
31 #include <soc/pci_devs.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 uint8_t _reserved3
[16];
87 uint8_t _reserved4
[8];
91 uint8_t _reserved5
[4];
93 uint8_t _reserved6
[28];
97 } __packed ich9_spi_regs
;
99 typedef struct ich_spi_controller
{
111 } ich_spi_controller
;
113 static ich_spi_controller cntlr
;
122 SPIS_RESERVED_MASK
= 0x7ff0,
123 SSFS_RESERVED_MASK
= 0x7fe2
127 SPIC_SCGO
= 0x000002,
129 SPIC_SPOP
= 0x000008,
133 SSFC_SCF_MASK
= 0x070000,
134 SSFC_RESERVED
= 0xf80000
141 HSFS_BERASE_MASK
= 0x0018,
142 HSFS_BERASE_SHIFT
= 3,
144 HSFS_FDOPSS
= 0x2000,
146 HSFS_FLOCKDN
= 0x8000
151 HSFC_FCYCLE_MASK
= 0x0006,
152 HSFC_FCYCLE_SHIFT
= 1,
153 HSFC_FDBC_MASK
= 0x3f00,
159 SPI_OPCODE_TYPE_READ_NO_ADDRESS
= 0,
160 SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
= 1,
161 SPI_OPCODE_TYPE_READ_WITH_ADDRESS
= 2,
162 SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
= 3
165 #define SPI_OFFSET_MASK 0x3ff
167 static uint8_t readb_(const void *addr
)
169 uint8_t v
= read8(addr
);
170 if (IS_ENABLED(CONFIG_DEBUG_SPI_FLASH
)) {
171 printk(BIOS_DEBUG
, "SPI: read %2.2x from %4.4x\n",
172 v
, (((uint32_t) addr
) & SPI_OFFSET_MASK
));
177 static uint16_t readw_(const void *addr
)
179 uint16_t v
= read16(addr
);
180 if (IS_ENABLED(CONFIG_DEBUG_SPI_FLASH
)) {
181 printk(BIOS_DEBUG
, "SPI: read %4.4x from %4.4x\n",
182 v
, (((uint32_t) addr
) & SPI_OFFSET_MASK
));
187 static uint32_t readl_(const void *addr
)
189 uint32_t v
= read32(addr
);
190 if (IS_ENABLED(CONFIG_DEBUG_SPI_FLASH
)) {
191 printk(BIOS_DEBUG
, "SPI: read %8.8x from %4.4x\n",
192 v
, (((uint32_t) addr
) & SPI_OFFSET_MASK
));
197 static void writeb_(uint8_t b
, void *addr
)
200 if (IS_ENABLED(CONFIG_DEBUG_SPI_FLASH
)) {
201 printk(BIOS_DEBUG
, "SPI: wrote %2.2x to %4.4x\n",
202 b
, (((uint32_t) addr
) & SPI_OFFSET_MASK
));
206 static void writew_(uint16_t b
, void *addr
)
209 if (IS_ENABLED(CONFIG_DEBUG_SPI_FLASH
)) {
210 printk(BIOS_DEBUG
, "SPI: wrote %4.4x to %4.4x\n",
211 b
, (((uint32_t) addr
) & SPI_OFFSET_MASK
));
215 static void writel_(uint32_t b
, void *addr
)
218 if (IS_ENABLED(CONFIG_DEBUG_SPI_FLASH
)) {
219 printk(BIOS_DEBUG
, "SPI: wrote %8.8x to %4.4x\n",
220 b
, (((uint32_t) addr
) & SPI_OFFSET_MASK
));
224 static void write_reg(const void *value
, void *dest
, uint32_t size
)
226 const uint8_t *bvalue
= value
;
227 uint8_t *bdest
= dest
;
230 writel_(*(const uint32_t *)bvalue
, bdest
);
231 bdest
+= 4; bvalue
+= 4; size
-= 4;
234 writeb_(*bvalue
, bdest
);
235 bdest
++; bvalue
++; size
--;
239 static void read_reg(const void *src
, void *value
, uint32_t size
)
241 const uint8_t *bsrc
= src
;
242 uint8_t *bvalue
= value
;
245 *(uint32_t *)bvalue
= readl_(bsrc
);
246 bsrc
+= 4; bvalue
+= 4; size
-= 4;
249 *bvalue
= readb_(bsrc
);
250 bsrc
++; bvalue
++; size
--;
254 static ich9_spi_regs
*spi_regs(void)
260 dev
= PCI_DEV(0, LPC_DEV
, LPC_FUNC
);
263 dev
= dev_find_slot(0, PCI_DEVFN(LPC_DEV
, LPC_FUNC
));
265 pci_read_config_dword(dev
, SBASE
, &sbase
);
268 return (void *)sbase
;
273 ich9_spi_regs
*ich9_spi
= spi_regs();
275 ichspi_lock
= readw_(&ich9_spi
->hsfs
) & HSFS_FLOCKDN
;
276 cntlr
.opmenu
= ich9_spi
->opmenu
;
277 cntlr
.menubytes
= sizeof(ich9_spi
->opmenu
);
278 cntlr
.optype
= &ich9_spi
->optype
;
279 cntlr
.addr
= &ich9_spi
->faddr
;
280 cntlr
.data
= (uint8_t *)ich9_spi
->fdata
;
281 cntlr
.databytes
= sizeof(ich9_spi
->fdata
);
282 cntlr
.status
= &ich9_spi
->ssfs
;
283 cntlr
.control
= (uint16_t *)ich9_spi
->ssfc
;
284 cntlr
.preop
= &ich9_spi
->preop
;
287 typedef struct spi_transaction
{
297 static inline void spi_use_out(spi_transaction
*trans
, unsigned bytes
)
300 trans
->bytesout
-= bytes
;
303 static inline void spi_use_in(spi_transaction
*trans
, unsigned bytes
)
306 trans
->bytesin
-= bytes
;
309 static void spi_setup_type(spi_transaction
*trans
)
313 /* Try to guess spi type from read/write sizes. */
314 if (trans
->bytesin
== 0) {
315 if (trans
->bytesout
> 4)
317 * If bytesin = 0 and bytesout > 4, we presume this is
318 * a write data operation, which is accompanied by an
321 trans
->type
= SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
;
323 trans
->type
= SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
;
327 if (trans
->bytesout
== 1) { /* and bytesin is > 0 */
328 trans
->type
= SPI_OPCODE_TYPE_READ_NO_ADDRESS
;
332 if (trans
->bytesout
== 4) { /* and bytesin is > 0 */
333 trans
->type
= SPI_OPCODE_TYPE_READ_WITH_ADDRESS
;
336 /* Fast read command is called with 5 bytes instead of 4 */
337 if (trans
->out
[0] == SPI_OPCODE_FAST_READ
&& trans
->bytesout
== 5) {
338 trans
->type
= SPI_OPCODE_TYPE_READ_WITH_ADDRESS
;
343 static int spi_setup_opcode(spi_transaction
*trans
)
346 uint8_t opmenu
[cntlr
.menubytes
];
348 trans
->opcode
= trans
->out
[0];
349 spi_use_out(trans
, 1);
351 /* The lock is off, so just use index 0. */
352 writeb_(trans
->opcode
, cntlr
.opmenu
);
353 optypes
= readw_(cntlr
.optype
);
354 optypes
= (optypes
& 0xfffc) | (trans
->type
& 0x3);
355 writew_(optypes
, cntlr
.optype
);
358 /* The lock is on. See if what we need is on the menu. */
360 uint16_t opcode_index
;
362 /* Write Enable is handled as atomic prefix */
363 if (trans
->opcode
== SPI_OPCODE_WREN
)
366 read_reg(cntlr
.opmenu
, opmenu
, sizeof(opmenu
));
367 for (opcode_index
= 0; opcode_index
< cntlr
.menubytes
;
369 if (opmenu
[opcode_index
] == trans
->opcode
)
373 if (opcode_index
== cntlr
.menubytes
) {
374 printk(BIOS_DEBUG
, "ICH SPI: Opcode %x not found\n",
379 optypes
= readw_(cntlr
.optype
);
380 optype
= (optypes
>> (opcode_index
* 2)) & 0x3;
381 if (trans
->type
== SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
&&
382 optype
== SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
&&
383 trans
->bytesout
>= 3) {
384 /* We guessed wrong earlier. Fix it up. */
385 trans
->type
= optype
;
387 if (optype
!= trans
->type
) {
388 printk(BIOS_DEBUG
, "ICH SPI: Transaction doesn't fit type %d\n",
396 static int spi_setup_offset(spi_transaction
*trans
)
398 /* Separate the SPI address and data. */
399 switch (trans
->type
) {
400 case SPI_OPCODE_TYPE_READ_NO_ADDRESS
:
401 case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS
:
403 case SPI_OPCODE_TYPE_READ_WITH_ADDRESS
:
404 case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS
:
405 trans
->offset
= ((uint32_t)trans
->out
[0] << 16) |
406 ((uint32_t)trans
->out
[1] << 8) |
407 ((uint32_t)trans
->out
[2] << 0);
408 spi_use_out(trans
, 3);
411 printk(BIOS_DEBUG
, "Unrecognized SPI transaction type %#x\n", trans
->type
);
417 * Wait for up to 60ms til status register bit(s) turn 1 (in case wait_til_set
418 * below is True) or 0. In case the wait was for the bit(s) to set - write
419 * those bits back, which would cause resetting them.
421 * Return the last read status value on success or -1 on failure.
423 static int ich_status_poll(uint16_t bitmask
, int wait_til_set
)
425 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_DEBUG
, "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 if ((opcode_index
= spi_setup_opcode(&trans
)) < 0)
476 if ((with_address
= spi_setup_offset(&trans
)) < 0)
479 if (!ichspi_lock
&& trans
.opcode
== SPI_OPCODE_WREN
) {
481 * Treat Write Enable as Atomic Pre-Op if possible
482 * in order to prevent the Management Engine from
483 * issuing a transaction between WREN and DATA.
485 writew_(trans
.opcode
, cntlr
.preop
);
489 /* Preset control fields */
490 control
= SPIC_SCGO
| ((opcode_index
& 0x07) << 4);
492 /* Issue atomic preop cycle if needed */
493 if (readw_(cntlr
.preop
))
496 if (!trans
.bytesout
&& !trans
.bytesin
) {
497 /* SPI addresses are 24 bit only */
499 writel_(trans
.offset
& 0x00FFFFFF, cntlr
.addr
);
502 * This is a 'no data' command (like Write Enable), its
503 * bytesout size was 1, decremented to zero while executing
504 * spi_setup_opcode() above. Tell the chip to send the
507 writew_(control
, cntlr
.control
);
509 /* wait for the result */
510 status
= ich_status_poll(SPIS_CDS
| SPIS_FCERR
, 1);
514 if (status
& SPIS_FCERR
) {
515 printk(BIOS_DEBUG
, "ICH SPI: Command transaction error\n");
523 * Check if this is a write command attempting to transfer more bytes
524 * than the controller can handle. Iterations for writes are not
525 * supported here because each SPI write command needs to be preceded
526 * and followed by other SPI commands, and this sequence is controlled
527 * by the SPI chip driver.
529 if (trans
.bytesout
> cntlr
.databytes
) {
530 printk(BIOS_DEBUG
, "ICH SPI: Too much to write. Does your SPI chip driver use"
531 " spi_crop_chunk()?\n");
536 * Read or write up to databytes bytes at a time until everything has
539 while (trans
.bytesout
|| trans
.bytesin
) {
540 uint32_t data_length
;
542 /* SPI addresses are 24 bit only */
543 writel_(trans
.offset
& 0x00FFFFFF, cntlr
.addr
);
546 data_length
= min(trans
.bytesout
, cntlr
.databytes
);
548 data_length
= min(trans
.bytesin
, cntlr
.databytes
);
550 /* Program data into FDATA0 to N */
551 if (trans
.bytesout
) {
552 write_reg(trans
.out
, cntlr
.data
, data_length
);
553 spi_use_out(&trans
, data_length
);
555 trans
.offset
+= data_length
;
558 /* Add proper control fields' values */
559 control
&= ~((cntlr
.databytes
- 1) << 8);
561 control
|= (data_length
- 1) << 8;
564 writew_(control
, cntlr
.control
);
566 /* Wait for Cycle Done Status or Flash Cycle Error. */
567 status
= ich_status_poll(SPIS_CDS
| SPIS_FCERR
, 1);
571 if (status
& SPIS_FCERR
) {
572 printk(BIOS_DEBUG
, "ICH SPI: Data transaction error\n");
577 read_reg(cntlr
.data
, trans
.in
, data_length
);
578 spi_use_in(&trans
, data_length
);
580 trans
.offset
+= data_length
;
585 /* Clear atomic preop now that xfer is done */
586 writew_(0, cntlr
.preop
);
591 static const struct spi_ctrlr spi_ctrlr
= {
592 .xfer
= spi_ctrlr_xfer
,
593 .xfer_vector
= spi_xfer_two_vectors
,
594 .max_xfer_size
= member_size(ich9_spi_regs
, fdata
),
597 const struct spi_ctrlr_buses spi_ctrlr_bus_map
[] = {
605 const size_t spi_ctrlr_bus_map_count
= ARRAY_SIZE(spi_ctrlr_bus_map
);