1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <amdblocks/chip.h>
4 #include <amdblocks/espi.h>
5 #include <amdblocks/lpc.h>
6 #include <device/mmio.h>
7 #include <console/console.h>
9 #include <soc/pci_devs.h>
15 static uintptr_t espi_bar
;
17 void espi_update_static_bar(uintptr_t bar
)
22 __weak
void mb_set_up_early_espi(void)
26 static uintptr_t espi_get_bar(void)
28 if (ENV_X86
&& !espi_bar
)
29 espi_update_static_bar(lpc_get_spibase() + ESPI_OFFSET_FROM_BAR
);
33 static uint32_t espi_read32(unsigned int reg
)
35 return read32p(espi_get_bar() + reg
);
38 static void espi_write32(unsigned int reg
, uint32_t val
)
40 write32p(espi_get_bar() + reg
, val
);
43 static uint16_t espi_read16(unsigned int reg
)
45 return read16p(espi_get_bar() + reg
);
48 static void espi_write16(unsigned int reg
, uint16_t val
)
50 write16p(espi_get_bar() + reg
, val
);
53 static uint8_t espi_read8(unsigned int reg
)
55 return read8p(espi_get_bar() + reg
);
58 static void espi_write8(unsigned int reg
, uint8_t val
)
60 write8p(espi_get_bar() + reg
, val
);
63 static inline uint32_t espi_decode_io_range_en_bit(unsigned int idx
)
65 if (ESPI_DECODE_RANGE_TO_REG_GROUP(idx
) == 0)
66 return ESPI_DECODE_IO_RANGE_EN(idx
);
68 return ESPI_DECODE_IO_RANGE_EXT_EN(idx
- ESPI_DECODE_RANGES_PER_REG_GROUP
);
71 static inline uint32_t espi_decode_mmio_range_en_bit(unsigned int idx
)
73 if (ESPI_DECODE_RANGE_TO_REG_GROUP(idx
) == 0)
74 return ESPI_DECODE_MMIO_RANGE_EN(idx
);
76 return ESPI_DECODE_MMIO_RANGE_EXT_EN(idx
- ESPI_DECODE_RANGES_PER_REG_GROUP
);
79 static inline unsigned int espi_io_range_base_reg(unsigned int idx
)
81 unsigned int reg_base
;
82 switch (ESPI_DECODE_RANGE_TO_REG_GROUP(idx
)) {
84 reg_base
= ESPI_IO_BASE_REG0
;
87 reg_base
= ESPI_IO_BASE_REG2
;
90 reg_base
= ESPI_IO_BASE_REG4
;
93 reg_base
= ESPI_IO_BASE_REG6
;
96 return ESPI_IO_RANGE_BASE_REG(reg_base
, ESPI_DECODE_RANGE_TO_REG_OFFSET(idx
));
99 static inline unsigned int espi_io_range_size_reg(unsigned int idx
)
101 unsigned int reg_base
;
102 switch (ESPI_DECODE_RANGE_TO_REG_GROUP(idx
)) {
104 reg_base
= ESPI_IO_SIZE0
;
107 reg_base
= ESPI_IO_SIZE1
;
110 reg_base
= ESPI_IO_SIZE2
;
112 default: /* case 3 */
113 reg_base
= ESPI_IO_SIZE3
;
116 return ESPI_IO_RANGE_SIZE_REG(reg_base
, ESPI_DECODE_RANGE_TO_REG_OFFSET(idx
));
119 static inline unsigned int espi_mmio_range_base_reg(unsigned int idx
)
121 unsigned int reg_base
;
122 if (ESPI_DECODE_RANGE_TO_REG_GROUP(idx
) == 0)
123 reg_base
= ESPI_MMIO_BASE_REG0
;
125 reg_base
= ESPI_MMIO_BASE_REG4
;
127 return ESPI_MMIO_RANGE_BASE_REG(reg_base
, ESPI_DECODE_RANGE_TO_REG_OFFSET(idx
));
130 static inline unsigned int espi_mmio_range_size_reg(unsigned int idx
)
132 unsigned int reg_base
;
133 if (ESPI_DECODE_RANGE_TO_REG_GROUP(idx
) == 0)
134 reg_base
= ESPI_MMIO_SIZE_REG0
;
136 reg_base
= ESPI_MMIO_SIZE_REG2
;
138 return ESPI_MMIO_RANGE_SIZE_REG(reg_base
, ESPI_DECODE_RANGE_TO_REG_OFFSET(idx
));
141 static void espi_enable_decode(uint32_t decode_en
)
145 val
= espi_read32(ESPI_DECODE
);
147 espi_write32(ESPI_DECODE
, val
);
150 static bool espi_is_decode_enabled(uint32_t decode
)
154 val
= espi_read32(ESPI_DECODE
);
155 return !!(val
& decode
);
158 static int espi_find_io_window(uint16_t win_base
)
162 for (i
= 0; i
< ESPI_GENERIC_IO_WIN_COUNT
; i
++) {
163 if (!espi_is_decode_enabled(espi_decode_io_range_en_bit(i
)))
166 if (espi_read16(espi_io_range_base_reg(i
)) == win_base
)
173 static int espi_get_unused_io_window(void)
177 for (i
= 0; i
< ESPI_GENERIC_IO_WIN_COUNT
; i
++) {
178 if (!espi_is_decode_enabled(espi_decode_io_range_en_bit(i
)))
185 static void espi_clear_decodes(void)
189 /* First turn off all enable bits, then zero base, range, and size registers */
190 if (CONFIG(SOC_AMD_COMMON_BLOCK_ESPI_RETAIN_PORT80_EN
))
191 espi_write16(ESPI_DECODE
, (espi_read16(ESPI_DECODE
) & ESPI_DECODE_IO_0x80_EN
));
193 espi_write16(ESPI_DECODE
, 0);
195 for (idx
= 0; idx
< ESPI_GENERIC_IO_WIN_COUNT
; idx
++) {
196 espi_write16(espi_io_range_base_reg(idx
), 0);
197 espi_write8(espi_io_range_size_reg(idx
), 0);
199 for (idx
= 0; idx
< ESPI_GENERIC_MMIO_WIN_COUNT
; idx
++) {
200 espi_write32(espi_mmio_range_base_reg(idx
), 0);
201 espi_write16(espi_mmio_range_size_reg(idx
), 0);
206 * Returns decode enable bits for standard IO port addresses. If port address is not supported
207 * by standard decode or if the size of window is not 1, then it returns -1.
209 static int espi_std_io_decode(uint16_t base
, size_t size
)
211 if (size
== 2 && base
== 0x2e)
212 return ESPI_DECODE_IO_0X2E_0X2F_EN
;
219 return ESPI_DECODE_IO_0x80_EN
;
222 return ESPI_DECODE_IO_0X60_0X64_EN
;
225 return ESPI_DECODE_IO_0X2E_0X2F_EN
;
231 static size_t espi_get_io_window_size(int idx
)
233 return espi_read8(espi_io_range_size_reg(idx
)) + 1;
236 static void espi_write_io_window(int idx
, uint16_t base
, size_t size
)
238 espi_write16(espi_io_range_base_reg(idx
), base
);
239 espi_write8(espi_io_range_size_reg(idx
), size
- 1);
242 static enum cb_err
espi_open_generic_io_window(uint16_t base
, size_t size
)
247 for (; size
; size
-= win_size
, base
+= win_size
) {
248 win_size
= MIN(size
, ESPI_GENERIC_IO_MAX_WIN_SIZE
);
250 idx
= espi_find_io_window(base
);
252 size_t curr_size
= espi_get_io_window_size(idx
);
254 if (curr_size
> win_size
) {
255 printk(BIOS_INFO
, "eSPI window already configured to be larger than requested! ");
256 printk(BIOS_INFO
, "Base: 0x%x, Requested size: 0x%zx, Actual size: 0x%zx\n",
257 base
, win_size
, curr_size
);
258 } else if (curr_size
< win_size
) {
259 espi_write_io_window(idx
, base
, win_size
);
260 printk(BIOS_INFO
, "eSPI window at base: 0x%x resized from 0x%zx to 0x%zx\n",
261 base
, curr_size
, win_size
);
267 idx
= espi_get_unused_io_window();
269 printk(BIOS_ERR
, "Cannot open IO window base %x size %zx\n", base
,
271 printk(BIOS_ERR
, "No more available IO windows!\n");
275 espi_write_io_window(idx
, base
, win_size
);
276 espi_enable_decode(espi_decode_io_range_en_bit(idx
));
282 enum cb_err
espi_open_io_window(uint16_t base
, size_t size
)
286 std_io
= espi_std_io_decode(base
, size
);
288 espi_enable_decode(std_io
);
291 return espi_open_generic_io_window(base
, size
);
295 static int espi_find_mmio_window(uint32_t win_base
)
299 for (i
= 0; i
< ESPI_GENERIC_MMIO_WIN_COUNT
; i
++) {
300 if (!espi_is_decode_enabled(espi_decode_mmio_range_en_bit(i
)))
303 if (espi_read32(espi_mmio_range_base_reg(i
)) == win_base
)
310 static int espi_get_unused_mmio_window(void)
314 for (i
= 0; i
< ESPI_GENERIC_MMIO_WIN_COUNT
; i
++) {
315 if (!espi_is_decode_enabled(espi_decode_mmio_range_en_bit(i
)))
322 static size_t espi_get_mmio_window_size(int idx
)
324 return espi_read16(espi_mmio_range_size_reg(idx
)) + 1;
327 static void espi_write_mmio_window(int idx
, uint32_t base
, size_t size
)
329 espi_write32(espi_mmio_range_base_reg(idx
), base
);
330 espi_write16(espi_mmio_range_size_reg(idx
), size
- 1);
333 enum cb_err
espi_open_mmio_window(uint32_t base
, size_t size
)
338 for (; size
; size
-= win_size
, base
+= win_size
) {
339 win_size
= MIN(size
, ESPI_GENERIC_MMIO_MAX_WIN_SIZE
);
341 idx
= espi_find_mmio_window(base
);
343 size_t curr_size
= espi_get_mmio_window_size(idx
);
345 if (curr_size
> win_size
) {
346 printk(BIOS_INFO
, "eSPI window already configured to be larger than requested! ");
347 printk(BIOS_INFO
, "Base: 0x%x, Requested size: 0x%zx, Actual size: 0x%zx\n",
348 base
, win_size
, curr_size
);
349 } else if (curr_size
< win_size
) {
350 espi_write_mmio_window(idx
, base
, win_size
);
351 printk(BIOS_INFO
, "eSPI window at base: 0x%x resized from 0x%zx to 0x%zx\n",
352 base
, curr_size
, win_size
);
358 idx
= espi_get_unused_mmio_window();
360 printk(BIOS_ERR
, "Cannot open IO window base %x size %zx\n", base
,
362 printk(BIOS_ERR
, "No more available MMIO windows!\n");
366 espi_write_mmio_window(idx
, base
, win_size
);
367 espi_enable_decode(espi_decode_mmio_range_en_bit(idx
));
373 static const struct espi_config
*espi_get_config(void)
375 const struct soc_amd_common_config
*soc_cfg
= soc_get_common_config();
376 return &soc_cfg
->espi_config
;
379 static enum cb_err
espi_configure_decodes(const struct espi_config
*cfg
)
383 espi_enable_decode(cfg
->std_io_decode_bitmap
);
385 for (i
= 0; i
< ESPI_GENERIC_IO_WIN_COUNT
; i
++) {
386 if (cfg
->generic_io_range
[i
].size
== 0)
388 if (espi_open_generic_io_window(cfg
->generic_io_range
[i
].base
,
389 cfg
->generic_io_range
[i
].size
) != CB_SUCCESS
)
397 CMD_TYPE_SET_CONFIGURATION
= 0,
398 CMD_TYPE_GET_CONFIGURATION
= 1,
399 CMD_TYPE_IN_BAND_RESET
= 2,
400 CMD_TYPE_PERIPHERAL
= 4,
406 #define ESPI_CMD_TIMEOUT_US 100
407 #define ESPI_CH_READY_TIMEOUT_US 10000
414 uint32_t slave_sel
:2;
451 union espi_txhdr0 hdr0
;
452 union espi_txhdr1 hdr1
;
453 union espi_txhdr2 hdr2
;
454 union espi_txdata data
;
455 uint32_t expected_status_codes
;
458 /* Wait up to ESPI_CMD_TIMEOUT_US for hardware to clear DNCMD_STATUS bit. */
459 static enum cb_err
espi_wait_ready(void)
462 union espi_txhdr0 hdr0
;
464 stopwatch_init_usecs_expire(&sw
, ESPI_CMD_TIMEOUT_US
);
466 hdr0
.val
= espi_read32(ESPI_DN_TX_HDR0
);
469 } while (!stopwatch_expired(&sw
));
474 /* Clear interrupt status register */
475 static void espi_clear_status(void)
477 uint32_t status
= espi_read32(ESPI_SLAVE0_INT_STS
);
479 espi_write32(ESPI_SLAVE0_INT_STS
, status
);
483 * Wait up to ESPI_CMD_TIMEOUT_US for interrupt status register to update after sending a
486 static enum cb_err
espi_poll_status(uint32_t *status
)
490 stopwatch_init_usecs_expire(&sw
, ESPI_CMD_TIMEOUT_US
);
492 *status
= espi_read32(ESPI_SLAVE0_INT_STS
);
495 } while (!stopwatch_expired(&sw
));
497 printk(BIOS_ERR
, "eSPI timed out waiting for status update.\n");
502 static void espi_show_failure(const struct espi_cmd
*cmd
, const char *str
, uint32_t status
)
504 printk(BIOS_ERR
, "eSPI cmd0-cmd2: %08x %08x %08x data: %08x.\n",
505 cmd
->hdr0
.val
, cmd
->hdr1
.val
, cmd
->hdr2
.val
, cmd
->data
.val
);
506 printk(BIOS_ERR
, "%s (Status = 0x%x)\n", str
, status
);
509 static enum cb_err
espi_send_command(const struct espi_cmd
*cmd
)
513 if (CONFIG(ESPI_DEBUG
))
514 printk(BIOS_DEBUG
, "eSPI cmd0-cmd2: %08x %08x %08x data: %08x.\n",
515 cmd
->hdr0
.val
, cmd
->hdr1
.val
, cmd
->hdr2
.val
, cmd
->data
.val
);
517 if (espi_wait_ready() != CB_SUCCESS
) {
518 espi_show_failure(cmd
, "Error: eSPI was not ready to accept a command", 0);
524 espi_write32(ESPI_DN_TX_HDR1
, cmd
->hdr1
.val
);
525 espi_write32(ESPI_DN_TX_HDR2
, cmd
->hdr2
.val
);
526 espi_write32(ESPI_DN_TX_DATA
, cmd
->data
.val
);
528 /* Dword 0 must be last as this write triggers the transaction */
529 espi_write32(ESPI_DN_TX_HDR0
, cmd
->hdr0
.val
);
531 if (espi_wait_ready() != CB_SUCCESS
) {
532 espi_show_failure(cmd
,
533 "Error: eSPI timed out waiting for command to complete", 0);
537 if (espi_poll_status(&status
) != CB_SUCCESS
) {
538 espi_show_failure(cmd
, "Error: eSPI poll status failed", 0);
542 /* If command did not complete downstream, return error. */
543 if (!(status
& ESPI_STATUS_DNCMD_COMPLETE
)) {
544 espi_show_failure(cmd
, "Error: eSPI downstream command completion failure",
549 if (status
& ~(ESPI_STATUS_DNCMD_COMPLETE
| cmd
->expected_status_codes
)) {
550 espi_show_failure(cmd
, "Error: unexpected eSPI status register bits set",
555 espi_write32(ESPI_SLAVE0_INT_STS
, status
);
560 static enum cb_err
espi_send_reset(void)
562 struct espi_cmd cmd
= {
564 .cmd_type
= CMD_TYPE_IN_BAND_RESET
,
569 * When performing an in-band reset the host controller and the
570 * peripheral can have mismatched IO configs.
572 * i.e., The eSPI peripheral can be in IO-4 mode while, the
573 * eSPI host will be in IO-1. This results in the peripheral
574 * getting invalid packets and thus not responding.
576 * If the peripheral is alerting when we perform an in-band
577 * reset, there is a race condition in espi_send_command.
578 * 1) espi_send_command clears the interrupt status.
579 * 2) eSPI host controller hardware notices the alert and sends
581 * 3) espi_send_command writes the in-band reset command.
582 * 4) eSPI hardware enqueues the in-band reset until GET_STATUS
584 * 5) GET_STATUS fails with NO_RESPONSE and sets the interrupt
586 * 6) eSPI hardware performs in-band reset.
587 * 7) espi_send_command checks the status and sees a
590 * As a workaround we allow the NO_RESPONSE status code when
591 * we perform an in-band reset.
593 .expected_status_codes
= ESPI_STATUS_NO_RESPONSE
,
596 return espi_send_command(&cmd
);
599 static enum cb_err
espi_send_pltrst(const struct espi_config
*mb_cfg
, bool assert)
601 struct espi_cmd cmd
= {
603 .cmd_type
= CMD_TYPE_VW
,
605 .hdata0
= 0, /* 1 VW group */
608 .byte0
= ESPI_VW_INDEX_SYSTEM_EVENT_3
,
609 .byte1
= assert ? ESPI_VW_SIGNAL_LOW(ESPI_VW_PLTRST
)
610 : ESPI_VW_SIGNAL_HIGH(ESPI_VW_PLTRST
),
614 if (!mb_cfg
->vw_ch_en
)
617 return espi_send_command(&cmd
);
621 * In case of get configuration command, hdata0 contains bits 15:8 of the slave register address
622 * and hdata1 contains bits 7:0 of the slave register address.
624 #define ESPI_CONFIGURATION_HDATA0(a) (((a) >> 8) & 0xff)
625 #define ESPI_CONFIGURATION_HDATA1(a) ((a) & 0xff)
627 static enum cb_err
espi_get_configuration(uint16_t slave_reg_addr
, uint32_t *config
)
629 struct espi_cmd cmd
= {
631 .cmd_type
= CMD_TYPE_GET_CONFIGURATION
,
633 .hdata0
= ESPI_CONFIGURATION_HDATA0(slave_reg_addr
),
634 .hdata1
= ESPI_CONFIGURATION_HDATA1(slave_reg_addr
),
640 if (espi_send_command(&cmd
) != CB_SUCCESS
)
643 *config
= espi_read32(ESPI_DN_TX_HDR1
);
645 if (CONFIG(ESPI_DEBUG
))
646 printk(BIOS_DEBUG
, "Get configuration for slave register(0x%x): 0x%x\n",
647 slave_reg_addr
, *config
);
652 static enum cb_err
espi_set_configuration(uint16_t slave_reg_addr
, uint32_t config
)
654 struct espi_cmd cmd
= {
656 .cmd_type
= CMD_TYPE_SET_CONFIGURATION
,
658 .hdata0
= ESPI_CONFIGURATION_HDATA0(slave_reg_addr
),
659 .hdata1
= ESPI_CONFIGURATION_HDATA1(slave_reg_addr
),
666 return espi_send_command(&cmd
);
669 static enum cb_err
espi_get_general_configuration(uint32_t *config
)
671 if (espi_get_configuration(ESPI_SLAVE_GENERAL_CFG
, config
) != CB_SUCCESS
)
674 espi_show_slave_general_configuration(*config
);
678 static enum cb_err
espi_set_io_mode_cfg(enum espi_io_mode mb_io_mode
, uint32_t slave_caps
,
679 uint32_t *slave_config
, uint32_t *ctrlr_config
)
681 switch (mb_io_mode
) {
682 case ESPI_IO_MODE_QUAD
:
683 if (espi_slave_supports_quad_io(slave_caps
)) {
684 *slave_config
|= ESPI_SLAVE_IO_MODE_SEL_QUAD
;
685 *ctrlr_config
|= ESPI_IO_MODE_QUAD
;
688 printk(BIOS_ERR
, "eSPI Quad I/O not supported. Dropping to dual mode.\n");
690 case ESPI_IO_MODE_DUAL
:
691 if (espi_slave_supports_dual_io(slave_caps
)) {
692 *slave_config
|= ESPI_SLAVE_IO_MODE_SEL_DUAL
;
693 *ctrlr_config
|= ESPI_IO_MODE_DUAL
;
696 printk(BIOS_ERR
, "eSPI Dual I/O not supported. Dropping to single mode.\n");
698 case ESPI_IO_MODE_SINGLE
:
699 /* Single I/O mode is always supported. */
700 *slave_config
|= ESPI_SLAVE_IO_MODE_SEL_SINGLE
;
701 *ctrlr_config
|= ESPI_IO_MODE_SINGLE
;
704 printk(BIOS_ERR
, "No supported eSPI I/O modes!\n");
710 static enum cb_err
espi_set_op_freq_cfg(enum espi_op_freq mb_op_freq
, uint32_t slave_caps
,
711 uint32_t *slave_config
, uint32_t *ctrlr_config
)
713 int slave_max_speed_mhz
= espi_slave_max_speed_mhz_supported(slave_caps
);
715 switch (mb_op_freq
) {
716 case ESPI_OP_FREQ_66_MHZ
:
717 if (slave_max_speed_mhz
>= 66) {
718 *slave_config
|= ESPI_SLAVE_OP_FREQ_SEL_66_MHZ
;
719 *ctrlr_config
|= ESPI_OP_FREQ_66_MHZ
;
722 printk(BIOS_ERR
, "eSPI 66MHz not supported. Dropping to 33MHz.\n");
724 case ESPI_OP_FREQ_33_MHZ
:
725 if (slave_max_speed_mhz
>= 33) {
726 *slave_config
|= ESPI_SLAVE_OP_FREQ_SEL_33_MHZ
;
727 *ctrlr_config
|= ESPI_OP_FREQ_33_MHZ
;
730 printk(BIOS_ERR
, "eSPI 33MHz not supported. Dropping to 16MHz.\n");
732 case ESPI_OP_FREQ_16_MHZ
:
734 * eSPI spec says the minimum frequency is 20MHz, but AMD datasheets support
737 if (slave_max_speed_mhz
> 0) {
738 *slave_config
|= ESPI_SLAVE_OP_FREQ_SEL_20_MHZ
;
739 *ctrlr_config
|= ESPI_OP_FREQ_16_MHZ
;
744 printk(BIOS_ERR
, "No supported eSPI Operating Frequency!\n");
750 static enum cb_err
espi_set_alert_pin_cfg(enum espi_alert_pin alert_pin
, uint32_t slave_caps
,
751 uint32_t *slave_config
, uint32_t *ctrlr_config
)
754 case ESPI_ALERT_PIN_IN_BAND
:
755 *slave_config
|= ESPI_SLAVE_ALERT_MODE_IO1
;
757 case ESPI_ALERT_PIN_PUSH_PULL
:
758 *slave_config
|= ESPI_SLAVE_ALERT_MODE_PIN
| ESPI_SLAVE_PUSH_PULL_ALERT_SEL
;
759 *ctrlr_config
|= ESPI_ALERT_MODE
;
761 case ESPI_ALERT_PIN_OPEN_DRAIN
:
762 if (!(slave_caps
& ESPI_SLAVE_OPEN_DRAIN_ALERT_SUPP
)) {
763 printk(BIOS_ERR
, "eSPI peripheral does not support open drain alert!");
767 *slave_config
|= ESPI_SLAVE_ALERT_MODE_PIN
| ESPI_SLAVE_OPEN_DRAIN_ALERT_SEL
;
768 *ctrlr_config
|= ESPI_ALERT_MODE
;
771 printk(BIOS_ERR
, "Unknown espi alert config: %u!\n", alert_pin
);
776 static enum cb_err
espi_set_general_configuration(const struct espi_config
*mb_cfg
,
779 uint32_t slave_config
= 0;
780 uint32_t ctrlr_config
= 0;
782 if (mb_cfg
->crc_check_enable
) {
783 slave_config
|= ESPI_SLAVE_CRC_ENABLE
;
784 ctrlr_config
|= ESPI_CRC_CHECKING_EN
;
787 if (espi_set_alert_pin_cfg(mb_cfg
->alert_pin
, slave_caps
, &slave_config
, &ctrlr_config
)
790 if (espi_set_io_mode_cfg(mb_cfg
->io_mode
, slave_caps
, &slave_config
, &ctrlr_config
)
793 if (espi_set_op_freq_cfg(mb_cfg
->op_freq_mhz
, slave_caps
, &slave_config
, &ctrlr_config
)
797 if (CONFIG(ESPI_DEBUG
))
798 printk(BIOS_INFO
, "Setting general configuration: slave: 0x%x controller: 0x%x\n",
799 slave_config
, ctrlr_config
);
801 espi_show_slave_general_configuration(slave_config
);
803 if (espi_set_configuration(ESPI_SLAVE_GENERAL_CFG
, slave_config
) != CB_SUCCESS
)
806 espi_write32(ESPI_SLAVE0_CONFIG
, ctrlr_config
);
810 static enum cb_err
espi_wait_channel_ready(uint16_t slave_reg_addr
)
815 stopwatch_init_usecs_expire(&sw
, ESPI_CH_READY_TIMEOUT_US
);
817 if (espi_get_configuration(slave_reg_addr
, &config
) != CB_SUCCESS
)
819 if (espi_slave_is_channel_ready(config
))
821 } while (!stopwatch_expired(&sw
));
823 printk(BIOS_ERR
, "Channel is not ready after %d usec (slave addr: 0x%x)\n",
824 ESPI_CH_READY_TIMEOUT_US
, slave_reg_addr
);
828 static void espi_enable_ctrlr_channel(uint32_t channel_en
)
830 uint32_t reg
= espi_read32(ESPI_SLAVE0_CONFIG
);
834 espi_write32(ESPI_SLAVE0_CONFIG
, reg
);
837 static enum cb_err
espi_set_channel_configuration(uint32_t slave_config
,
838 uint32_t slave_reg_addr
,
839 uint32_t ctrlr_enable
)
841 if (espi_set_configuration(slave_reg_addr
, slave_config
) != CB_SUCCESS
)
844 if (!(slave_config
& ESPI_SLAVE_CHANNEL_ENABLE
))
847 if (espi_wait_channel_ready(slave_reg_addr
) != CB_SUCCESS
)
850 espi_enable_ctrlr_channel(ctrlr_enable
);
854 static enum cb_err
espi_setup_vw_channel(const struct espi_config
*mb_cfg
, uint32_t slave_caps
)
856 uint32_t slave_vw_caps
;
857 uint32_t ctrlr_vw_caps
;
858 uint32_t slave_vw_count_supp
;
859 uint32_t ctrlr_vw_count_supp
;
860 uint32_t use_vw_count
;
861 uint32_t slave_config
;
863 if (!mb_cfg
->vw_ch_en
)
866 if (!espi_slave_supports_vw_channel(slave_caps
)) {
867 printk(BIOS_ERR
, "eSPI slave doesn't support VW channel!\n");
871 if (espi_get_configuration(ESPI_SLAVE_VW_CFG
, &slave_vw_caps
) != CB_SUCCESS
)
874 ctrlr_vw_caps
= espi_read32(ESPI_MASTER_CAP
);
875 ctrlr_vw_count_supp
= (ctrlr_vw_caps
& ESPI_VW_MAX_SIZE_MASK
) >> ESPI_VW_MAX_SIZE_SHIFT
;
877 slave_vw_count_supp
= espi_slave_get_vw_count_supp(slave_vw_caps
);
878 use_vw_count
= MIN(ctrlr_vw_count_supp
, slave_vw_count_supp
);
880 slave_config
= ESPI_SLAVE_CHANNEL_ENABLE
| ESPI_SLAVE_VW_COUNT_SEL_VAL(use_vw_count
);
881 return espi_set_channel_configuration(slave_config
, ESPI_SLAVE_VW_CFG
, ESPI_VW_CH_EN
);
884 static enum cb_err
espi_setup_periph_channel(const struct espi_config
*mb_cfg
,
887 uint32_t slave_config
;
888 /* Peripheral channel requires BME bit to be set when enabling the channel. */
889 const uint32_t slave_en_mask
=
890 ESPI_SLAVE_CHANNEL_ENABLE
| ESPI_SLAVE_PERIPH_BUS_MASTER_ENABLE
;
892 if (espi_get_configuration(ESPI_SLAVE_PERIPH_CFG
, &slave_config
) != CB_SUCCESS
)
896 * Peripheral channel is the only one which is enabled on reset. So, if the mainboard
897 * wants to disable it, set configuration to disable peripheral channel. It also
898 * requires that BME bit be cleared.
900 if (mb_cfg
->periph_ch_en
) {
901 if (!espi_slave_supports_periph_channel(slave_caps
)) {
902 printk(BIOS_ERR
, "eSPI slave doesn't support periph channel!\n");
905 slave_config
|= slave_en_mask
;
907 slave_config
&= ~slave_en_mask
;
910 espi_show_slave_peripheral_channel_configuration(slave_config
);
912 return espi_set_channel_configuration(slave_config
, ESPI_SLAVE_PERIPH_CFG
,
916 static enum cb_err
espi_setup_oob_channel(const struct espi_config
*mb_cfg
, uint32_t slave_caps
)
918 uint32_t slave_config
;
920 if (!mb_cfg
->oob_ch_en
)
923 if (!espi_slave_supports_oob_channel(slave_caps
)) {
924 printk(BIOS_ERR
, "eSPI slave doesn't support OOB channel!\n");
928 if (espi_get_configuration(ESPI_SLAVE_OOB_CFG
, &slave_config
) != CB_SUCCESS
)
931 slave_config
|= ESPI_SLAVE_CHANNEL_ENABLE
;
933 return espi_set_channel_configuration(slave_config
, ESPI_SLAVE_OOB_CFG
,
937 static enum cb_err
espi_setup_flash_channel(const struct espi_config
*mb_cfg
,
940 uint32_t slave_config
;
942 if (!mb_cfg
->flash_ch_en
)
945 if (!espi_slave_supports_flash_channel(slave_caps
)) {
946 printk(BIOS_ERR
, "eSPI slave doesn't support flash channel!\n");
950 if (espi_get_configuration(ESPI_SLAVE_FLASH_CFG
, &slave_config
) != CB_SUCCESS
)
953 slave_config
|= ESPI_SLAVE_CHANNEL_ENABLE
;
955 return espi_set_channel_configuration(slave_config
, ESPI_SLAVE_FLASH_CFG
,
959 static enum cb_err
espi_set_initial_config(const struct espi_config
*mb_cfg
)
961 uint32_t espi_initial_mode
= ESPI_OP_FREQ_16_MHZ
| ESPI_IO_MODE_SINGLE
;
963 switch (mb_cfg
->alert_pin
) {
964 case ESPI_ALERT_PIN_IN_BAND
:
966 case ESPI_ALERT_PIN_PUSH_PULL
:
967 case ESPI_ALERT_PIN_OPEN_DRAIN
:
968 espi_initial_mode
|= ESPI_ALERT_MODE
;
971 printk(BIOS_ERR
, "Unknown espi alert config: %u!\n", mb_cfg
->alert_pin
);
975 espi_write32(ESPI_SLAVE0_CONFIG
, espi_initial_mode
);
979 static void espi_setup_subtractive_decode(const struct espi_config
*mb_cfg
)
981 uint32_t global_ctrl_reg
;
982 global_ctrl_reg
= espi_read32(ESPI_GLOBAL_CONTROL_1
);
984 if (mb_cfg
->subtractive_decode
) {
985 global_ctrl_reg
&= ~ESPI_SUB_DECODE_SLV_MASK
;
986 global_ctrl_reg
|= ESPI_SUB_DECODE_EN
;
989 global_ctrl_reg
&= ~ESPI_SUB_DECODE_EN
;
991 espi_write32(ESPI_GLOBAL_CONTROL_1
, global_ctrl_reg
);
994 enum cb_err
espi_setup(void)
996 uint32_t slave_caps
, ctrl
;
997 const struct espi_config
*cfg
= espi_get_config();
999 printk(BIOS_SPEW
, "Initializing ESPI.\n");
1001 espi_write32(ESPI_GLOBAL_CONTROL_0
, ESPI_AL_STOP_EN
);
1002 espi_write32(ESPI_GLOBAL_CONTROL_1
, ESPI_RGCMD_INT(23) | ESPI_ERR_INT_SMI
);
1003 espi_write32(ESPI_SLAVE0_INT_EN
, 0);
1004 espi_clear_status();
1005 espi_clear_decodes();
1008 * Boot sequence: Step 1
1009 * Set correct initial configuration to talk to the slave:
1010 * Set clock frequency to 16.7MHz and single IO mode.
1012 if (espi_set_initial_config(cfg
) != CB_SUCCESS
)
1016 * Boot sequence: Step 2
1017 * Send in-band reset
1018 * The resets affects both host and slave devices, so set initial config again.
1020 if (espi_send_reset() != CB_SUCCESS
) {
1021 printk(BIOS_ERR
, "In-band reset failed!\n");
1025 if (espi_set_initial_config(cfg
) != CB_SUCCESS
)
1029 * Boot sequence: Step 3
1030 * Get configuration of slave device.
1032 if (espi_get_general_configuration(&slave_caps
) != CB_SUCCESS
) {
1033 printk(BIOS_ERR
, "Slave GET_CONFIGURATION failed!\n");
1039 * Step 4: Write slave device general config
1040 * Step 5: Set host slave config
1042 if (espi_set_general_configuration(cfg
, slave_caps
) != CB_SUCCESS
) {
1043 printk(BIOS_ERR
, "Slave SET_CONFIGURATION failed!\n");
1048 * Setup polarity before enabling the VW channel so any interrupts
1049 * received will have the correct polarity.
1051 espi_write32(ESPI_RXVW_POLARITY
, cfg
->vw_irq_polarity
);
1054 * Boot Sequences: Steps 6 - 9
1057 /* Set up VW first so we can deassert PLTRST#. */
1058 if (espi_setup_vw_channel(cfg
, slave_caps
) != CB_SUCCESS
) {
1059 printk(BIOS_ERR
, "Setup VW channel failed!\n");
1063 /* Assert PLTRST# if VW channel is enabled by mainboard. */
1064 if (espi_send_pltrst(cfg
, true) != CB_SUCCESS
) {
1065 printk(BIOS_ERR
, "PLTRST# assertion failed!\n");
1069 /* De-assert PLTRST# if VW channel is enabled by mainboard. */
1070 if (espi_send_pltrst(cfg
, false) != CB_SUCCESS
) {
1071 printk(BIOS_ERR
, "PLTRST# deassertion failed!\n");
1075 if (espi_setup_periph_channel(cfg
, slave_caps
) != CB_SUCCESS
) {
1076 printk(BIOS_ERR
, "Setup Periph channel failed!\n");
1080 if (espi_setup_oob_channel(cfg
, slave_caps
) != CB_SUCCESS
) {
1081 printk(BIOS_ERR
, "Setup OOB channel failed!\n");
1085 if (espi_setup_flash_channel(cfg
, slave_caps
) != CB_SUCCESS
) {
1086 printk(BIOS_ERR
, "Setup Flash channel failed!\n");
1090 if (espi_configure_decodes(cfg
) != CB_SUCCESS
) {
1091 printk(BIOS_ERR
, "Configuring decodes failed!\n");
1095 /* Enable subtractive decode if configured */
1096 espi_setup_subtractive_decode(cfg
);
1098 ctrl
= espi_read32(ESPI_GLOBAL_CONTROL_1
);
1099 ctrl
|= ESPI_BUS_MASTER_EN
;
1101 if (CONFIG(SOC_AMD_COMMON_BLOCK_HAS_ESPI_ALERT_ENABLE
))
1102 ctrl
|= ESPI_ALERT_ENABLE
;
1104 espi_write32(ESPI_GLOBAL_CONTROL_1
, ctrl
);
1106 printk(BIOS_SPEW
, "Finished initializing ESPI.\n");
1111 /* Setup eSPI with any mainboard specific initialization. */
1112 void configure_espi_with_mb_hook(void)
1114 mb_set_up_early_espi();