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