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