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