blob: 5ca0d2831a6eedd37c808d847ca5d91fb5a05e2e [file] [log] [blame]
Furquan Shaikhf318e032020-05-04 23:38:53 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Furquan Shaikhf318e032020-05-04 23:38:53 -07002
3#include <amdblocks/chip.h>
4#include <amdblocks/espi.h>
5#include <amdblocks/lpc.h>
6#include <arch/mmio.h>
7#include <console/console.h>
Furquan Shaikh70063ff52020-05-11 14:28:13 -07008#include <espi.h>
Furquan Shaikhf318e032020-05-04 23:38:53 -07009#include <soc/pci_devs.h>
10#include <stddef.h>
11#include <stdint.h>
Furquan Shaikh70063ff52020-05-11 14:28:13 -070012#include <timer.h>
Furquan Shaikhf318e032020-05-04 23:38:53 -070013#include <types.h>
14
Furquan Shaikh98bc9612020-05-09 19:31:55 -070015static uintptr_t espi_bar;
16
17void espi_update_static_bar(uintptr_t bar)
18{
19 espi_bar = bar;
20}
21
Furquan Shaikhf318e032020-05-04 23:38:53 -070022static uintptr_t espi_get_bar(void)
23{
Furquan Shaikh98bc9612020-05-09 19:31:55 -070024 uintptr_t espi_spi_base;
25
26 if (espi_bar)
27 return espi_bar;
28
29 espi_spi_base = lpc_get_spibase();
30 espi_update_static_bar(espi_spi_base + ESPI_OFFSET_FROM_BAR);
31
32 return espi_bar;
Furquan Shaikhf318e032020-05-04 23:38:53 -070033}
34
35static uint32_t espi_read32(int reg)
36{
37 return read32((void *)(espi_get_bar() + reg));
38}
39
40static void espi_write32(int reg, uint32_t val)
41{
42 write32((void *)(espi_get_bar() + reg), val);
43}
44
45static uint16_t espi_read16(int reg)
46{
47 return read16((void *)(espi_get_bar() + reg));
48}
49
50static void espi_write16(int reg, uint16_t val)
51{
52 write16((void *)(espi_get_bar() + reg), val);
53}
54
55static uint8_t espi_read8(int reg)
56{
57 return read8((void *)(espi_get_bar() + reg));
58}
59
60static void espi_write8(int reg, uint8_t val)
61{
62 write8((void *)(espi_get_bar() + reg), val);
63}
64
65static void espi_enable_decode(int decode_en)
66{
67 uint32_t val;
68
69 val = espi_read32(ESPI_DECODE);
70 val |= decode_en;
71 espi_write32(ESPI_DECODE, val);
72}
73
74static bool espi_is_decode_enabled(int decode)
75{
76 uint32_t val;
77
78 val = espi_read32(ESPI_DECODE);
79 return !!(val & decode);
80}
81
82static int espi_find_io_window(uint16_t win_base)
83{
84 int i;
85
86 for (i = 0; i < ESPI_GENERIC_IO_WIN_COUNT; i++) {
87 if (!espi_is_decode_enabled(ESPI_DECODE_IO_RANGE_EN(i)))
88 continue;
89
90 if (espi_read16(ESPI_IO_RANGE_BASE(i)) == win_base)
91 return i;
92 }
93
94 return -1;
95}
96
97static int espi_get_unused_io_window(void)
98{
99 int i;
100
101 for (i = 0; i < ESPI_GENERIC_IO_WIN_COUNT; i++) {
102 if (!espi_is_decode_enabled(ESPI_DECODE_IO_RANGE_EN(i)))
103 return i;
104 }
105
106 return -1;
107}
108
109/*
110 * Returns decode enable bits for standard IO port addresses. If port address is not supported
111 * by standard decode or if the size of window is not 1, then it returns -1.
112 */
113static int espi_std_io_decode(uint16_t base, size_t size)
114{
115 int ret = -1;
116
117 if (size != 1)
118 return ret;
119
120 switch (base) {
121 case 0x80:
122 ret = ESPI_DECODE_IO_0x80_EN;
123 break;
124 case 0x60:
125 case 0x64:
126 ret = ESPI_DECODE_IO_0X60_0X64_EN;
127 break;
128 case 0x2e:
129 case 0x2f:
130 ret = ESPI_DECODE_IO_0X2E_0X2F_EN;
131 break;
132 default:
133 ret = -1;
134 break;
135 }
136
137 return ret;
138}
139
140static size_t espi_get_io_window_size(int idx)
141{
142 return espi_read8(ESPI_IO_RANGE_SIZE(idx)) + 1;
143}
144
145static void espi_write_io_window(int idx, uint16_t base, size_t size)
146{
147 espi_write16(ESPI_IO_RANGE_BASE(idx), base);
148 espi_write8(ESPI_IO_RANGE_SIZE(idx), size - 1);
149}
150
151static int espi_open_generic_io_window(uint16_t base, size_t size)
152{
153 size_t win_size;
154 int idx;
155
156 for (; size; size -= win_size, base += win_size) {
157 win_size = MIN(size, ESPI_GENERIC_IO_MAX_WIN_SIZE);
158
159 idx = espi_find_io_window(base);
160 if (idx != -1) {
161 size_t curr_size = espi_get_io_window_size(idx);
162
163 if (curr_size > win_size) {
164 printk(BIOS_INFO, "eSPI window already configured to be larger than requested! ");
165 printk(BIOS_INFO, "Base: 0x%x, Requested size: 0x%zx, Actual size: 0x%zx\n",
166 base, win_size, curr_size);
167 } else if (curr_size < win_size) {
168 espi_write_io_window(idx, base, win_size);
169 printk(BIOS_INFO, "eSPI window at base: 0x%x resized from 0x%zx to 0x%zx\n",
170 base, curr_size, win_size);
171 }
172
173 continue;
174 }
175
176 idx = espi_get_unused_io_window();
177 if (idx == -1) {
178 printk(BIOS_ERR, "Cannot open IO window base %x size %zx\n", base,
179 size);
180 printk(BIOS_ERR, "ERROR: No more available IO windows!\n");
181 return -1;
182 }
183
184 espi_write_io_window(idx, base, win_size);
185 espi_enable_decode(ESPI_DECODE_IO_RANGE_EN(idx));
186 }
187
188 return 0;
189}
190
191int espi_open_io_window(uint16_t base, size_t size)
192{
193 int std_io;
194
195 std_io = espi_std_io_decode(base, size);
196 if (std_io != -1) {
197 espi_enable_decode(std_io);
198 return 0;
199 }
200
201 return espi_open_generic_io_window(base, size);
202}
203
204static int espi_find_mmio_window(uint32_t win_base)
205{
206 int i;
207
208 for (i = 0; i < ESPI_GENERIC_MMIO_WIN_COUNT; i++) {
209 if (!espi_is_decode_enabled(ESPI_DECODE_MMIO_RANGE_EN(i)))
210 continue;
211
212 if (espi_read32(ESPI_MMIO_RANGE_BASE(i)) == win_base)
213 return i;
214 }
215
216 return -1;
217}
218
219static int espi_get_unused_mmio_window(void)
220{
221 int i;
222
223 for (i = 0; i < ESPI_GENERIC_MMIO_WIN_COUNT; i++) {
224 if (!espi_is_decode_enabled(ESPI_DECODE_MMIO_RANGE_EN(i)))
225 return i;
226 }
227
228 return -1;
229
230}
231
232static size_t espi_get_mmio_window_size(int idx)
233{
234 return espi_read16(ESPI_MMIO_RANGE_SIZE(idx)) + 1;
235}
236
237static void espi_write_mmio_window(int idx, uint32_t base, size_t size)
238{
239 espi_write32(ESPI_MMIO_RANGE_BASE(idx), base);
240 espi_write16(ESPI_MMIO_RANGE_SIZE(idx), size - 1);
241}
242
243int espi_open_mmio_window(uint32_t base, size_t size)
244{
245 size_t win_size;
246 int idx;
247
248 for (; size; size -= win_size, base += win_size) {
249 win_size = MIN(size, ESPI_GENERIC_MMIO_MAX_WIN_SIZE);
250
251 idx = espi_find_mmio_window(base);
252 if (idx != -1) {
253 size_t curr_size = espi_get_mmio_window_size(idx);
254
255 if (curr_size > win_size) {
256 printk(BIOS_INFO, "eSPI window already configured to be larger than requested! ");
257 printk(BIOS_INFO, "Base: 0x%x, Requested size: 0x%zx, Actual size: 0x%zx\n",
258 base, win_size, curr_size);
259 } else if (curr_size < win_size) {
260 espi_write_mmio_window(idx, base, win_size);
261 printk(BIOS_INFO, "eSPI window at base: 0x%x resized from 0x%zx to 0x%zx\n",
262 base, curr_size, win_size);
263 }
264
265 continue;
266 }
267
268 idx = espi_get_unused_mmio_window();
269 if (idx == -1) {
270 printk(BIOS_ERR, "Cannot open IO window base %x size %zx\n", base,
271 size);
272 printk(BIOS_ERR, "ERROR: No more available MMIO windows!\n");
273 return -1;
274 }
275
276 espi_write_mmio_window(idx, base, win_size);
277 espi_enable_decode(ESPI_DECODE_MMIO_RANGE_EN(idx));
278 }
279
280 return 0;
281}
282
283static const struct espi_config *espi_get_config(void)
284{
285 const struct soc_amd_common_config *soc_cfg = soc_get_common_config();
286
287 if (!soc_cfg)
288 die("Common config structure is NULL!\n");
289
290 return &soc_cfg->espi_config;
291}
292
293void espi_configure_decodes(void)
294{
295 int i;
296 const struct espi_config *cfg = espi_get_config();
297
298 espi_enable_decode(cfg->std_io_decode_bitmap);
299
300 for (i = 0; i < ESPI_GENERIC_IO_WIN_COUNT; i++) {
301 if (cfg->generic_io_range[i].size == 0)
302 continue;
303 espi_open_generic_io_window(cfg->generic_io_range[i].base,
304 cfg->generic_io_range[i].size);
305 }
306}
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700307
308#define ESPI_DN_TX_HDR0 0x00
309enum espi_cmd_type {
310 CMD_TYPE_SET_CONFIGURATION = 0,
311 CMD_TYPE_GET_CONFIGURATION = 1,
312 CMD_TYPE_IN_BAND_RESET = 2,
313 CMD_TYPE_PERIPHERAL = 4,
314 CMD_TYPE_VW = 5,
315 CMD_TYPE_OOB = 6,
316 CMD_TYPE_FLASH = 7,
317};
318
319#define ESPI_DN_TX_HDR1 0x04
320#define ESPI_DN_TX_HDR2 0x08
321#define ESPI_DN_TX_DATA 0x0c
322
323#define ESPI_MASTER_CAP 0x2c
324#define ESPI_VW_MAX_SIZE_SHIFT 13
325#define ESPI_VW_MAX_SIZE_MASK (0x3f << ESPI_VW_MAX_SIZE_SHIFT)
326
327#define ESPI_GLOBAL_CONTROL_1 0x34
328#define ESPI_SUB_DECODE_SLV_SHIFT 3
329#define ESPI_SUB_DECODE_SLV_MASK (0x3 << ESPI_SUB_DECODE_SLV_SHIFT)
330#define ESPI_SUB_DECODE_EN (1 << 2)
331
332#define SLAVE0_INT_STS 0x70
333#define ESPI_STATUS_DNCMD_COMPLETE (1 << 28)
334#define ESPI_STATUS_NON_FATAL_ERROR (1 << 6)
335#define ESPI_STATUS_FATAL_ERROR (1 << 5)
336#define ESPI_STATUS_NO_RESPONSE (1 << 4)
337#define ESPI_STATUS_CRC_ERR (1 << 2)
338#define ESPI_STATUS_WAIT_TIMEOUT (1 << 1)
339#define ESPI_STATUS_BUS_ERROR (1 << 0)
340
341#define ESPI_RXVW_POLARITY 0xac
342
343#define ESPI_CMD_TIMEOUT_US 100
344#define ESPI_CH_READY_TIMEOUT_US 1000
345
346union espi_txhdr0 {
347 uint32_t val;
348 struct {
349 uint32_t cmd_type:3;
350 uint32_t cmd_sts:1;
351 uint32_t slave_sel:2;
352 uint32_t rsvd:2;
353 uint32_t hdata0:8;
354 uint32_t hdata1:8;
355 uint32_t hdata2:8;
356 };
357} __packed;
358
359union espi_txhdr1 {
360 uint32_t val;
361 struct {
362 uint32_t hdata3:8;
363 uint32_t hdata4:8;
364 uint32_t hdata5:8;
365 uint32_t hdata6:8;
366 };
367} __packed;
368
369union espi_txhdr2 {
370 uint32_t val;
371 struct {
372 uint32_t hdata7:8;
373 uint32_t rsvd:24;
374 };
375} __packed;
376
377union espi_txdata {
378 uint32_t val;
379 struct {
380 uint32_t byte0:8;
381 uint32_t byte1:8;
382 uint32_t byte2:8;
383 uint32_t byte3:8;
384 };
385} __packed;
386
387struct espi_cmd {
388 union espi_txhdr0 hdr0;
389 union espi_txhdr1 hdr1;
390 union espi_txhdr2 hdr2;
391 union espi_txdata data;
392} __packed;
393
394/* Wait up to ESPI_CMD_TIMEOUT_US for hardware to clear DNCMD_STATUS bit. */
395static int espi_wait_ready(void)
396{
397 struct stopwatch sw;
398 union espi_txhdr0 hdr0;
399
400 stopwatch_init_usecs_expire(&sw, ESPI_CMD_TIMEOUT_US);
401 do {
402 hdr0.val = espi_read32(ESPI_DN_TX_HDR0);
403 if (!hdr0.cmd_sts)
404 return 0;
405 } while (!stopwatch_expired(&sw));
406
407 return -1;
408}
409
410/* Clear interrupt status register */
411static void espi_clear_status(void)
412{
413 uint32_t status = espi_read32(SLAVE0_INT_STS);
414 if (status)
415 espi_write32(SLAVE0_INT_STS, status);
416}
417
418/*
419 * Wait up to ESPI_CMD_TIMEOUT_US for interrupt status register to update after sending a
420 * command.
421 */
422static int espi_check_status(uint32_t *status)
423{
424 struct stopwatch sw;
425
426 stopwatch_init_usecs_expire(&sw, ESPI_CMD_TIMEOUT_US);
427 do {
428 *status = espi_read32(SLAVE0_INT_STS);
429 if (*status)
430 return 0;
431 } while (!stopwatch_expired(&sw));
432
433 printk(BIOS_ERR, "Error: eSPI timed out waiting for status update.\n");
434
435 return -1;
436}
437
438static void espi_show_failure(const struct espi_cmd *cmd, const char *str, uint32_t status)
439{
440 printk(BIOS_ERR, "eSPI cmd0-cmd2: %08x %08x %08x data: %08x.\n",
441 cmd->hdr0.val, cmd->hdr1.val, cmd->hdr2.val, cmd->data.val);
442 printk(BIOS_ERR, "%s (Status = 0x%x)\n", str, status);
443}
444
445static int espi_send_command(const struct espi_cmd *cmd)
446{
447 uint32_t status;
448
449 if (CONFIG(ESPI_DEBUG))
450 printk(BIOS_ERR, "eSPI cmd0-cmd2: %08x %08x %08x data: %08x.\n",
451 cmd->hdr0.val, cmd->hdr1.val, cmd->hdr2.val, cmd->data.val);
452
453 if (espi_wait_ready() == -1) {
454 espi_show_failure(cmd, "Error: eSPI was not ready to accept a command", 0);
455 return -1;
456 }
457
458 espi_clear_status();
459
460 espi_write32(ESPI_DN_TX_HDR1, cmd->hdr1.val);
461 espi_write32(ESPI_DN_TX_HDR2, cmd->hdr2.val);
462 espi_write32(ESPI_DN_TX_DATA, cmd->data.val);
463
464 /* Dword 0 must be last as this write triggers the transaction */
465 espi_write32(ESPI_DN_TX_HDR0, cmd->hdr0.val);
466
467 if (espi_wait_ready() == -1) {
468 espi_show_failure(cmd,
469 "Error: eSPI timed out waiting for command to complete", 0);
470 return -1;
471 }
472
473 if (espi_check_status(&status) == -1) {
474 espi_show_failure(cmd, "Error: eSPI check status failed", 0);
475 return -1;
476 }
477
478 /* If command did not complete downstream, return error. */
479 if (!(status & ESPI_STATUS_DNCMD_COMPLETE)) {
480 espi_show_failure(cmd, "Error: eSPI downstream command completion failure",
481 status);
482 return -1;
483 }
484
485 if (status & ~ESPI_STATUS_DNCMD_COMPLETE) {
486 espi_show_failure(cmd, "Error: eSPI status register bits set", status);
487 return -1;
488 }
489
490 return 0;
491}
492
493static int espi_send_reset(void)
494{
495 struct espi_cmd cmd = {
496 .hdr0 = {
497 .cmd_type = CMD_TYPE_IN_BAND_RESET,
498 .cmd_sts = 1,
499 },
500 };
501
502 return espi_send_command(&cmd);
503}
504
505static int espi_send_pltrst_deassert(const struct espi_config *mb_cfg)
506{
507 struct espi_cmd cmd = {
508 .hdr0 = {
509 .cmd_type = CMD_TYPE_VW,
510 .cmd_sts = 1,
511 .hdata0 = 0, /* 1 VW group */
512 },
513 .data = {
514 .byte0 = ESPI_VW_INDEX_SYSTEM_EVENT_3,
515 .byte1 = ESPI_VW_SIGNAL_HIGH(ESPI_VW_PLTRST),
516 },
517 };
518
519 if (!mb_cfg->vw_ch_en)
520 return 0;
521
522 return espi_send_command(&cmd);
523}
524
525/*
526 * In case of get configuration command, hdata0 contains bits 15:8 of the slave register address
527 * and hdata1 contains bits 7:0 of the slave register address.
528 */
529#define ESPI_CONFIGURATION_HDATA0(a) (((a) >> 8) & 0xff)
530#define ESPI_CONFIGURATION_HDATA1(a) ((a) & 0xff)
531
532static int espi_get_configuration(uint16_t slave_reg_addr, uint32_t *config)
533{
534 struct espi_cmd cmd = {
535 .hdr0 = {
536 .cmd_type = CMD_TYPE_GET_CONFIGURATION,
537 .cmd_sts = 1,
538 .hdata0 = ESPI_CONFIGURATION_HDATA0(slave_reg_addr),
539 .hdata1 = ESPI_CONFIGURATION_HDATA1(slave_reg_addr),
540 },
541 };
542
543 *config = 0;
544
545 if (espi_send_command(&cmd))
546 return -1;
547
548 *config = espi_read32(ESPI_DN_TX_HDR1);
549
550 if (CONFIG(ESPI_DEBUG))
551 printk(BIOS_DEBUG, "Get configuration for slave register(0x%x): 0x%x\n",
552 slave_reg_addr, *config);
553
554 return 0;
555}
556
557static int espi_set_configuration(uint16_t slave_reg_addr, uint32_t config)
558{
559 struct espi_cmd cmd = {
560 .hdr0 = {
561 .cmd_type = CMD_TYPE_SET_CONFIGURATION,
562 .cmd_sts = 1,
563 .hdata0 = ESPI_CONFIGURATION_HDATA0(slave_reg_addr),
564 .hdata1 = ESPI_CONFIGURATION_HDATA1(slave_reg_addr),
565 },
566 .hdr1 = {
567 .val = config,
568 },
569 };
570
571 return espi_send_command(&cmd);
572}
573
574static int espi_get_general_configuration(uint32_t *config)
575{
576 int ret = espi_get_configuration(ESPI_SLAVE_GENERAL_CFG, config);
577 if (ret == -1)
578 return -1;
579
580 espi_show_slave_general_configuration(*config);
581 return 0;
582}
583
584static void espi_set_io_mode_config(enum espi_io_mode mb_io_mode, uint32_t slave_caps,
585 uint32_t *slave_config, uint32_t *ctrlr_config)
586{
587 switch (mb_io_mode) {
588 case ESPI_IO_MODE_QUAD:
589 if (espi_slave_supports_quad_io(slave_caps)) {
590 *slave_config |= ESPI_SLAVE_IO_MODE_SEL_QUAD;
591 *ctrlr_config |= ESPI_IO_MODE_QUAD;
592 break;
593 }
594 printk(BIOS_ERR, "Error: eSPI Quad I/O not supported. Dropping to dual mode.\n");
595 /* Intentional fall-through */
596 case ESPI_IO_MODE_DUAL:
597 if (espi_slave_supports_dual_io(slave_caps)) {
598 *slave_config |= ESPI_SLAVE_IO_MODE_SEL_DUAL;
599 *ctrlr_config |= ESPI_IO_MODE_DUAL;
600 break;
601 }
602 printk(BIOS_ERR,
603 "Error: eSPI Dual I/O not supported. Dropping to single mode.\n");
604 /* Intentional fall-through */
605 case ESPI_IO_MODE_SINGLE:
606 /* Single I/O mode is always supported. */
607 *slave_config |= ESPI_SLAVE_IO_MODE_SEL_SINGLE;
608 *ctrlr_config |= ESPI_IO_MODE_SINGLE;
609 break;
610 default:
611 die("No supported eSPI I/O modes!\n");
612 }
613}
614
615static void espi_set_op_freq_config(enum espi_op_freq mb_op_freq, uint32_t slave_caps,
616 uint32_t *slave_config, uint32_t *ctrlr_config)
617{
618 int slave_max_speed_mhz = espi_slave_max_speed_mhz_supported(slave_caps);
619
620 switch (mb_op_freq) {
621 case ESPI_OP_FREQ_66_MHZ:
622 if (slave_max_speed_mhz >= 66) {
623 *slave_config |= ESPI_SLAVE_OP_FREQ_SEL_66_MHZ;
624 *ctrlr_config |= ESPI_OP_FREQ_66_MHZ;
625 break;
626 }
627 printk(BIOS_ERR, "Error: eSPI 66MHz not supported. Dropping to 33MHz.\n");
628 /* Intentional fall-through */
629 case ESPI_OP_FREQ_33_MHZ:
630 if (slave_max_speed_mhz >= 33) {
631 *slave_config |= ESPI_SLAVE_OP_FREQ_SEL_33_MHZ;
632 *ctrlr_config |= ESPI_OP_FREQ_33_MHZ;
633 break;
634 }
635 printk(BIOS_ERR, "Error: eSPI 33MHz not supported. Dropping to 16MHz.\n");
636 /* Intentional fall-through */
637 case ESPI_OP_FREQ_16_MHZ:
638 /*
639 * eSPI spec says the minimum frequency is 20MHz, but AMD datasheets support
640 * 16.7 Mhz.
641 */
642 if (slave_max_speed_mhz > 0) {
643 *slave_config |= ESPI_SLAVE_OP_FREQ_SEL_20_MHZ;
644 *ctrlr_config |= ESPI_OP_FREQ_16_MHZ;
645 break;
646 }
647 /* Intentional fall-through */
648 default:
649 die("No supported eSPI Operating Frequency!\n");
650 }
651}
652
653static int espi_set_general_configuration(const struct espi_config *mb_cfg, uint32_t slave_caps)
654{
655 uint32_t slave_config = 0;
656 uint32_t ctrlr_config = 0;
657
658 if (mb_cfg->crc_check_enable) {
659 slave_config |= ESPI_SLAVE_CRC_ENABLE;
660 ctrlr_config |= ESPI_CRC_CHECKING_EN;
661 }
662
663 if (mb_cfg->dedicated_alert_pin) {
664 slave_config |= ESPI_SLAVE_ALERT_MODE_PIN;
665 ctrlr_config |= ESPI_ALERT_MODE;
666 }
667
668 espi_set_io_mode_config(mb_cfg->io_mode, slave_caps, &slave_config, &ctrlr_config);
669 espi_set_op_freq_config(mb_cfg->op_freq_mhz, slave_caps, &slave_config, &ctrlr_config);
670
671 if (CONFIG(ESPI_DEBUG))
672 printk(BIOS_INFO, "Setting general configuration: slave: 0x%x controller: 0x%x\n",
673 slave_config, ctrlr_config);
674
675 if (espi_set_configuration(ESPI_SLAVE_GENERAL_CFG, slave_config) == -1)
676 return -1;
677
678 espi_write32(ESPI_SLAVE0_CONFIG, ctrlr_config);
679 return 0;
680}
681
682static int espi_wait_channel_ready(uint16_t slave_reg_addr)
683{
684 struct stopwatch sw;
685 uint32_t config;
686
687 stopwatch_init_usecs_expire(&sw, ESPI_CH_READY_TIMEOUT_US);
688 do {
689 espi_get_configuration(slave_reg_addr, &config);
690 if (espi_slave_is_channel_ready(config))
691 return 0;
692 } while (!stopwatch_expired(&sw));
693
694 printk(BIOS_ERR, "Error: Channel is not ready after %d usec (slave addr: 0x%x)\n",
695 ESPI_CH_READY_TIMEOUT_US, slave_reg_addr);
696 return -1;
697
698}
699
700static void espi_enable_ctrlr_channel(uint32_t channel_en)
701{
702 uint32_t reg = espi_read32(ESPI_SLAVE0_CONFIG);
703
704 reg |= channel_en;
705
706 espi_write32(ESPI_SLAVE0_CONFIG, reg);
707}
708
709static int espi_set_channel_configuration(uint32_t slave_config, uint32_t slave_reg_addr,
710 uint32_t ctrlr_enable)
711{
712 if (espi_set_configuration(slave_reg_addr, slave_config) == -1)
713 return -1;
714
715 if (!(slave_config & ESPI_SLAVE_CHANNEL_ENABLE))
716 return 0;
717
718 if (espi_wait_channel_ready(slave_reg_addr) == -1)
719 return -1;
720
721 espi_enable_ctrlr_channel(ctrlr_enable);
722 return 0;
723}
724
725static int espi_setup_vw_channel(const struct espi_config *mb_cfg, uint32_t slave_caps)
726{
727 uint32_t slave_vw_caps;
728 uint32_t ctrlr_vw_caps;
729 uint32_t slave_vw_count_supp;
730 uint32_t ctrlr_vw_count_supp;
731 uint32_t use_vw_count;
732 uint32_t slave_config;
733
734 if (!mb_cfg->vw_ch_en)
735 return 0;
736
737 if (!espi_slave_supports_vw_channel(slave_caps)) {
738 printk(BIOS_ERR, "Error: eSPI slave doesn't support VW channel!\n");
739 return -1;
740 }
741
742 if (espi_get_configuration(ESPI_SLAVE_VW_CFG, &slave_vw_caps) == -1)
743 return -1;
744
745 ctrlr_vw_caps = espi_read32(ESPI_MASTER_CAP);
746 ctrlr_vw_count_supp = (ctrlr_vw_caps & ESPI_VW_MAX_SIZE_MASK) >> ESPI_VW_MAX_SIZE_SHIFT;
747
748 slave_vw_count_supp = espi_slave_get_vw_count_supp(slave_vw_caps);
749 use_vw_count = MIN(ctrlr_vw_count_supp, slave_vw_count_supp);
750
751 slave_config = ESPI_SLAVE_CHANNEL_ENABLE | ESPI_SLAVE_VW_COUNT_SEL_VAL(use_vw_count);
752 return espi_set_channel_configuration(slave_config, ESPI_SLAVE_VW_CFG, ESPI_VW_CH_EN);
753}
754
755static int espi_setup_periph_channel(const struct espi_config *mb_cfg, uint32_t slave_caps)
756{
757 uint32_t slave_config;
758 /* Peripheral channel requires BME bit to be set when enabling the channel. */
759 const uint32_t slave_en_mask = ESPI_SLAVE_CHANNEL_READY |
760 ESPI_SLAVE_PERIPH_BUS_MASTER_ENABLE;
761
762 if (espi_get_configuration(ESPI_SLAVE_PERIPH_CFG, &slave_config) == -1)
763 return -1;
764
765 /*
766 * Peripheral channel is the only one which is enabled on reset. So, if the mainboard
767 * wants to disable it, set configuration to disable peripheral channel. It also
768 * requires that BME bit be cleared.
769 */
770 if (mb_cfg->periph_ch_en) {
771 if (!espi_slave_supports_periph_channel(slave_caps)) {
772 printk(BIOS_ERR, "Error: eSPI slave doesn't support periph channel!\n");
773 return -1;
774 }
775 slave_config |= slave_en_mask;
776 } else {
777 slave_config &= ~slave_en_mask;
778 }
779
780 return espi_set_channel_configuration(slave_config, ESPI_SLAVE_PERIPH_CFG,
781 ESPI_PERIPH_CH_EN);
782}
783
784static int espi_setup_oob_channel(const struct espi_config *mb_cfg, uint32_t slave_caps)
785{
786 uint32_t slave_config;
787
788 if (!mb_cfg->oob_ch_en)
789 return 0;
790
791 if (!espi_slave_supports_oob_channel(slave_caps)) {
792 printk(BIOS_ERR, "Error: eSPI slave doesn't support OOB channel!\n");
793 return -1;
794 }
795
796 if (espi_get_configuration(ESPI_SLAVE_OOB_CFG, &slave_config) == -1)
797 return -1;
798
799 slave_config |= ESPI_SLAVE_CHANNEL_ENABLE;
800
801 return espi_set_channel_configuration(slave_config, ESPI_SLAVE_OOB_CFG,
802 ESPI_OOB_CH_EN);
803}
804
805static int espi_setup_flash_channel(const struct espi_config *mb_cfg, uint32_t slave_caps)
806{
807 uint32_t slave_config;
808
809 if (!mb_cfg->flash_ch_en)
810 return 0;
811
812 if (!espi_slave_supports_flash_channel(slave_caps)) {
813 printk(BIOS_ERR, "Error: eSPI slave doesn't support flash channel!\n");
814 return -1;
815 }
816
817 if (espi_get_configuration(ESPI_SLAVE_FLASH_CFG, &slave_config) == -1)
818 return -1;
819
820 slave_config |= ESPI_SLAVE_CHANNEL_ENABLE;
821
822 return espi_set_channel_configuration(slave_config, ESPI_SLAVE_FLASH_CFG,
823 ESPI_FLASH_CH_EN);
824}
825
826static void espi_set_initial_config(const struct espi_config *mb_cfg)
827{
828 uint32_t espi_initial_mode = ESPI_OP_FREQ_16_MHZ | ESPI_IO_MODE_SINGLE;
829
830 if (mb_cfg->dedicated_alert_pin)
831 espi_initial_mode |= ESPI_ALERT_MODE;
832
833 espi_write32(ESPI_SLAVE0_CONFIG, espi_initial_mode);
834}
835
836static void espi_setup_subtractive_decode(const struct espi_config *mb_cfg)
837{
838 uint32_t global_ctrl_reg;
839 global_ctrl_reg = espi_read32(ESPI_GLOBAL_CONTROL_1);
840
841 if (mb_cfg->subtractive_decode) {
842 global_ctrl_reg &= ~ESPI_SUB_DECODE_SLV_MASK;
843 global_ctrl_reg |= ESPI_SUB_DECODE_EN;
844
845 } else {
846 global_ctrl_reg &= ~ESPI_SUB_DECODE_EN;
847 }
848 espi_write32(ESPI_GLOBAL_CONTROL_1, global_ctrl_reg);
849}
850
851int espi_setup(void)
852{
853 uint32_t slave_caps;
854 const struct espi_config *cfg = espi_get_config();
855
856 /*
857 * Boot sequence: Step 1
858 * Set correct initial configuration to talk to the slave:
859 * Set clock frequency to 16.7MHz and single IO mode.
860 */
861 espi_set_initial_config(cfg);
862
863 /*
864 * Boot sequence: Step 2
865 * Send in-band reset
866 * The resets affects both host and slave devices, so set initial config again.
867 */
868 if (espi_send_reset() == -1) {
869 printk(BIOS_ERR, "Error: In-band reset failed!\n");
870 return -1;
871 }
872 espi_set_initial_config(cfg);
873
874 /*
875 * Boot sequence: Step 3
876 * Get configuration of slave device.
877 */
878 if (espi_get_general_configuration(&slave_caps) == -1) {
879 printk(BIOS_ERR, "Error: Slave GET_CONFIGURATION failed!\n");
880 return -1;
881 }
882
883 /*
884 * Boot sequence:
885 * Step 4: Write slave device general config
886 * Step 5: Set host slave config
887 */
888 if (espi_set_general_configuration(cfg, slave_caps) == -1) {
889 printk(BIOS_ERR, "Error: Slave SET_CONFIGURATION failed!\n");
890 return -1;
891 }
892
893 /*
894 * Setup polarity before enabling the VW channel so any interrupts
895 * received will have the correct polarity.
896 */
897 espi_write32(ESPI_RXVW_POLARITY, cfg->vw_irq_polarity);
898
899 /*
900 * Boot Sequences: Steps 6 - 9
901 * Channel setup
902 */
903 /* Set up VW first so we can deassert PLTRST#. */
904 if (espi_setup_vw_channel(cfg, slave_caps) == -1) {
905 printk(BIOS_ERR, "Error: Setup VW channel failed!\n");
906 return -1;
907 }
908
909 /* De-assert PLTRST# if VW channel is enabled by mainboard. */
910 if (espi_send_pltrst_deassert(cfg) == -1) {
911 printk(BIOS_ERR, "Error: PLTRST deassertion failed!\n");
912 return -1;
913 }
914
915 if (espi_setup_periph_channel(cfg, slave_caps) == -1) {
916 printk(BIOS_ERR, "Error: Setup Periph channel failed!\n");
917 return -1;
918 }
919
920 if (espi_setup_oob_channel(cfg, slave_caps) == -1) {
921 printk(BIOS_ERR, "Error: Setup OOB channel failed!\n");
922 return -1;
923 }
924
925 if (espi_setup_flash_channel(cfg, slave_caps) == -1) {
926 printk(BIOS_ERR, "Error: Setup Flash channel failed!\n");
927 return -1;
928 }
929
930 /* Enable subtractive decode if configured */
931 espi_setup_subtractive_decode(cfg);
932
933 return 0;
934}