blob: ebc8f45979857ab0968c0cc4e844b8881a443d33 [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
Martin Rothfe589772021-06-25 15:09:43 -060020__weak void mb_set_up_early_espi(void)
21{
22}
23
Furquan Shaikhf318e032020-05-04 23:38:53 -070024static uintptr_t espi_get_bar(void)
25{
Martin Rothb39e10d2020-07-14 11:08:55 -060026 if (ENV_X86 && !espi_bar)
27 espi_update_static_bar(lpc_get_spibase() + ESPI_OFFSET_FROM_BAR);
Furquan Shaikh98bc9612020-05-09 19:31:55 -070028 return espi_bar;
Furquan Shaikhf318e032020-05-04 23:38:53 -070029}
30
Felix Held92dd6782020-08-10 20:27:58 +020031static uint32_t espi_read32(unsigned int reg)
Furquan Shaikhf318e032020-05-04 23:38:53 -070032{
33 return read32((void *)(espi_get_bar() + reg));
34}
35
Felix Held92dd6782020-08-10 20:27:58 +020036static void espi_write32(unsigned int reg, uint32_t val)
Furquan Shaikhf318e032020-05-04 23:38:53 -070037{
38 write32((void *)(espi_get_bar() + reg), val);
39}
40
Felix Held92dd6782020-08-10 20:27:58 +020041static uint16_t espi_read16(unsigned int reg)
Furquan Shaikhf318e032020-05-04 23:38:53 -070042{
43 return read16((void *)(espi_get_bar() + reg));
44}
45
Felix Held92dd6782020-08-10 20:27:58 +020046static void espi_write16(unsigned int reg, uint16_t val)
Furquan Shaikhf318e032020-05-04 23:38:53 -070047{
48 write16((void *)(espi_get_bar() + reg), val);
49}
50
Felix Held92dd6782020-08-10 20:27:58 +020051static uint8_t espi_read8(unsigned int reg)
Furquan Shaikhf318e032020-05-04 23:38:53 -070052{
53 return read8((void *)(espi_get_bar() + reg));
54}
55
Felix Held92dd6782020-08-10 20:27:58 +020056static void espi_write8(unsigned int reg, uint8_t val)
Furquan Shaikhf318e032020-05-04 23:38:53 -070057{
58 write8((void *)(espi_get_bar() + reg), val);
59}
60
Felix Heldf08fbf82020-08-10 20:30:36 +020061static void espi_enable_decode(uint32_t decode_en)
Furquan Shaikhf318e032020-05-04 23:38:53 -070062{
63 uint32_t val;
64
65 val = espi_read32(ESPI_DECODE);
66 val |= decode_en;
67 espi_write32(ESPI_DECODE, val);
68}
69
Felix Heldf08fbf82020-08-10 20:30:36 +020070static bool espi_is_decode_enabled(uint32_t decode)
Furquan Shaikhf318e032020-05-04 23:38:53 -070071{
72 uint32_t val;
73
74 val = espi_read32(ESPI_DECODE);
75 return !!(val & decode);
76}
77
78static int espi_find_io_window(uint16_t win_base)
79{
80 int i;
81
82 for (i = 0; i < ESPI_GENERIC_IO_WIN_COUNT; i++) {
83 if (!espi_is_decode_enabled(ESPI_DECODE_IO_RANGE_EN(i)))
84 continue;
85
86 if (espi_read16(ESPI_IO_RANGE_BASE(i)) == win_base)
87 return i;
88 }
89
90 return -1;
91}
92
93static int espi_get_unused_io_window(void)
94{
95 int i;
96
97 for (i = 0; i < ESPI_GENERIC_IO_WIN_COUNT; i++) {
98 if (!espi_is_decode_enabled(ESPI_DECODE_IO_RANGE_EN(i)))
99 return i;
100 }
101
102 return -1;
103}
104
Raul E Rangelb95f8482021-04-02 13:47:09 -0600105static void espi_clear_decodes(void)
Martin Roth011bf132021-03-23 13:20:42 -0600106{
107 unsigned int idx;
108
109 /* First turn off all enable bits, then zero base, range, and size registers */
Raul E Rangel01792e32021-04-26 13:52:38 -0600110 espi_write16(ESPI_DECODE, 0);
Martin Roth011bf132021-03-23 13:20:42 -0600111
112 for (idx = 0; idx < ESPI_GENERIC_IO_WIN_COUNT; idx++) {
113 espi_write16(ESPI_IO_RANGE_BASE(idx), 0);
114 espi_write8(ESPI_IO_RANGE_SIZE(idx), 0);
115 }
116 for (idx = 0; idx < ESPI_GENERIC_MMIO_WIN_COUNT; idx++) {
117 espi_write32(ESPI_MMIO_RANGE_BASE(idx), 0);
118 espi_write16(ESPI_MMIO_RANGE_SIZE(idx), 0);
119 }
120}
121
Furquan Shaikhf318e032020-05-04 23:38:53 -0700122/*
123 * Returns decode enable bits for standard IO port addresses. If port address is not supported
124 * by standard decode or if the size of window is not 1, then it returns -1.
125 */
126static int espi_std_io_decode(uint16_t base, size_t size)
127{
Felix Heldc0d4eeb2020-08-10 20:37:16 +0200128 if (size == 2 && base == 0x2e)
129 return ESPI_DECODE_IO_0X2E_0X2F_EN;
130
Furquan Shaikhf318e032020-05-04 23:38:53 -0700131 if (size != 1)
Felix Held4bf419f2020-08-10 20:33:25 +0200132 return -1;
Furquan Shaikhf318e032020-05-04 23:38:53 -0700133
134 switch (base) {
135 case 0x80:
Felix Held4bf419f2020-08-10 20:33:25 +0200136 return ESPI_DECODE_IO_0x80_EN;
Furquan Shaikhf318e032020-05-04 23:38:53 -0700137 case 0x60:
138 case 0x64:
Felix Held4bf419f2020-08-10 20:33:25 +0200139 return ESPI_DECODE_IO_0X60_0X64_EN;
Furquan Shaikhf318e032020-05-04 23:38:53 -0700140 case 0x2e:
141 case 0x2f:
Felix Held4bf419f2020-08-10 20:33:25 +0200142 return ESPI_DECODE_IO_0X2E_0X2F_EN;
Furquan Shaikhf318e032020-05-04 23:38:53 -0700143 default:
Felix Held4bf419f2020-08-10 20:33:25 +0200144 return -1;
Furquan Shaikhf318e032020-05-04 23:38:53 -0700145 }
Furquan Shaikhf318e032020-05-04 23:38:53 -0700146}
147
148static size_t espi_get_io_window_size(int idx)
149{
150 return espi_read8(ESPI_IO_RANGE_SIZE(idx)) + 1;
151}
152
153static void espi_write_io_window(int idx, uint16_t base, size_t size)
154{
155 espi_write16(ESPI_IO_RANGE_BASE(idx), base);
156 espi_write8(ESPI_IO_RANGE_SIZE(idx), size - 1);
157}
158
159static int espi_open_generic_io_window(uint16_t base, size_t size)
160{
161 size_t win_size;
162 int idx;
163
164 for (; size; size -= win_size, base += win_size) {
165 win_size = MIN(size, ESPI_GENERIC_IO_MAX_WIN_SIZE);
166
167 idx = espi_find_io_window(base);
168 if (idx != -1) {
169 size_t curr_size = espi_get_io_window_size(idx);
170
171 if (curr_size > win_size) {
172 printk(BIOS_INFO, "eSPI window already configured to be larger than requested! ");
173 printk(BIOS_INFO, "Base: 0x%x, Requested size: 0x%zx, Actual size: 0x%zx\n",
174 base, win_size, curr_size);
175 } else if (curr_size < win_size) {
176 espi_write_io_window(idx, base, win_size);
177 printk(BIOS_INFO, "eSPI window at base: 0x%x resized from 0x%zx to 0x%zx\n",
178 base, curr_size, win_size);
179 }
180
181 continue;
182 }
183
184 idx = espi_get_unused_io_window();
185 if (idx == -1) {
186 printk(BIOS_ERR, "Cannot open IO window base %x size %zx\n", base,
187 size);
188 printk(BIOS_ERR, "ERROR: No more available IO windows!\n");
189 return -1;
190 }
191
192 espi_write_io_window(idx, base, win_size);
193 espi_enable_decode(ESPI_DECODE_IO_RANGE_EN(idx));
194 }
195
196 return 0;
197}
198
199int espi_open_io_window(uint16_t base, size_t size)
200{
201 int std_io;
202
203 std_io = espi_std_io_decode(base, size);
204 if (std_io != -1) {
205 espi_enable_decode(std_io);
206 return 0;
Felix Heldb026c7c2020-08-10 20:43:53 +0200207 } else {
208 return espi_open_generic_io_window(base, size);
Furquan Shaikhf318e032020-05-04 23:38:53 -0700209 }
Furquan Shaikhf318e032020-05-04 23:38:53 -0700210}
211
212static int espi_find_mmio_window(uint32_t win_base)
213{
214 int i;
215
216 for (i = 0; i < ESPI_GENERIC_MMIO_WIN_COUNT; i++) {
217 if (!espi_is_decode_enabled(ESPI_DECODE_MMIO_RANGE_EN(i)))
218 continue;
219
220 if (espi_read32(ESPI_MMIO_RANGE_BASE(i)) == win_base)
221 return i;
222 }
223
224 return -1;
225}
226
227static int espi_get_unused_mmio_window(void)
228{
229 int i;
230
231 for (i = 0; i < ESPI_GENERIC_MMIO_WIN_COUNT; i++) {
232 if (!espi_is_decode_enabled(ESPI_DECODE_MMIO_RANGE_EN(i)))
233 return i;
234 }
235
236 return -1;
237
238}
239
240static size_t espi_get_mmio_window_size(int idx)
241{
242 return espi_read16(ESPI_MMIO_RANGE_SIZE(idx)) + 1;
243}
244
245static void espi_write_mmio_window(int idx, uint32_t base, size_t size)
246{
247 espi_write32(ESPI_MMIO_RANGE_BASE(idx), base);
248 espi_write16(ESPI_MMIO_RANGE_SIZE(idx), size - 1);
249}
250
251int espi_open_mmio_window(uint32_t base, size_t size)
252{
253 size_t win_size;
254 int idx;
255
256 for (; size; size -= win_size, base += win_size) {
257 win_size = MIN(size, ESPI_GENERIC_MMIO_MAX_WIN_SIZE);
258
259 idx = espi_find_mmio_window(base);
260 if (idx != -1) {
261 size_t curr_size = espi_get_mmio_window_size(idx);
262
263 if (curr_size > win_size) {
264 printk(BIOS_INFO, "eSPI window already configured to be larger than requested! ");
265 printk(BIOS_INFO, "Base: 0x%x, Requested size: 0x%zx, Actual size: 0x%zx\n",
266 base, win_size, curr_size);
267 } else if (curr_size < win_size) {
268 espi_write_mmio_window(idx, base, win_size);
269 printk(BIOS_INFO, "eSPI window at base: 0x%x resized from 0x%zx to 0x%zx\n",
270 base, curr_size, win_size);
271 }
272
273 continue;
274 }
275
276 idx = espi_get_unused_mmio_window();
277 if (idx == -1) {
278 printk(BIOS_ERR, "Cannot open IO window base %x size %zx\n", base,
279 size);
280 printk(BIOS_ERR, "ERROR: No more available MMIO windows!\n");
281 return -1;
282 }
283
284 espi_write_mmio_window(idx, base, win_size);
285 espi_enable_decode(ESPI_DECODE_MMIO_RANGE_EN(idx));
286 }
287
288 return 0;
289}
290
291static const struct espi_config *espi_get_config(void)
292{
293 const struct soc_amd_common_config *soc_cfg = soc_get_common_config();
294
295 if (!soc_cfg)
296 die("Common config structure is NULL!\n");
297
298 return &soc_cfg->espi_config;
299}
300
Raul E Rangel61ac1bc2021-04-02 10:55:27 -0600301static int espi_configure_decodes(const struct espi_config *cfg)
Furquan Shaikhf318e032020-05-04 23:38:53 -0700302{
Raul E Rangel61ac1bc2021-04-02 10:55:27 -0600303 int i, ret;
Furquan Shaikhf318e032020-05-04 23:38:53 -0700304
305 espi_enable_decode(cfg->std_io_decode_bitmap);
306
307 for (i = 0; i < ESPI_GENERIC_IO_WIN_COUNT; i++) {
308 if (cfg->generic_io_range[i].size == 0)
309 continue;
Raul E Rangel61ac1bc2021-04-02 10:55:27 -0600310 ret = espi_open_generic_io_window(cfg->generic_io_range[i].base,
311 cfg->generic_io_range[i].size);
312 if (ret)
313 return ret;
Furquan Shaikhf318e032020-05-04 23:38:53 -0700314 }
Raul E Rangel61ac1bc2021-04-02 10:55:27 -0600315
316 return 0;
Furquan Shaikhf318e032020-05-04 23:38:53 -0700317}
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700318
319#define ESPI_DN_TX_HDR0 0x00
320enum espi_cmd_type {
321 CMD_TYPE_SET_CONFIGURATION = 0,
322 CMD_TYPE_GET_CONFIGURATION = 1,
323 CMD_TYPE_IN_BAND_RESET = 2,
324 CMD_TYPE_PERIPHERAL = 4,
325 CMD_TYPE_VW = 5,
326 CMD_TYPE_OOB = 6,
327 CMD_TYPE_FLASH = 7,
328};
329
330#define ESPI_DN_TX_HDR1 0x04
331#define ESPI_DN_TX_HDR2 0x08
332#define ESPI_DN_TX_DATA 0x0c
333
334#define ESPI_MASTER_CAP 0x2c
335#define ESPI_VW_MAX_SIZE_SHIFT 13
336#define ESPI_VW_MAX_SIZE_MASK (0x3f << ESPI_VW_MAX_SIZE_SHIFT)
337
Raul E Rangel1d0e4932021-04-02 10:27:11 -0600338#define ESPI_GLOBAL_CONTROL_0 0x30
339#define ESPI_WAIT_CNT_SHIFT 24
340#define ESPI_WAIT_CNT_MASK (0x3F << ESPI_WAIT_CNT_SHIFT)
341#define ESPI_WDG_CNT_SHIFT 8
342#define ESPI_WDG_CNT_MASK (0xFFFF << ESPI_WDG_CNT_SHIFT)
343#define ESPI_AL_IDLE_TIMER_SHIFT 4
344#define ESPI_AL_IDLE_TIMER_MASK (0x7 << ESPI_AL_IDLE_TIMER_SHIFT)
345#define ESPI_AL_STOP_EN (1 << 3)
346#define ESPI_PR_CLKGAT_EN (1 << 2)
347#define ESPI_WAIT_CHKEN (1 << 1)
348#define ESPI_WDG_EN (1 << 0)
349
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700350#define ESPI_GLOBAL_CONTROL_1 0x34
Raul E Rangel1d0e4932021-04-02 10:27:11 -0600351#define ESPI_RGCMD_INT_MAP_SHIFT 13
352#define ESPI_RGCMD_INT_MAP_MASK (0x1F << ESPI_RGCMD_INT_MAP_SHIFT)
353#define ESPI_RGCMD_INT(irq) ((irq) << ESPI_RGCMD_INT_MAP_SHIFT)
354#define ESPI_RGCMD_INT_SMI (0x1F << ESPI_RGCMD_INT_MAP_SHIFT)
355#define ESPI_ERR_INT_MAP_SHIFT 8
356#define ESPI_ERR_INT_MAP_MASK (0x1F << ESPI_ERR_INT_MAP_SHIFT)
357#define ESPI_ERR_INT(irq) ((irq) << ESPI_ERR_INT_MAP_SHIFT)
358#define ESPI_ERR_INT_SMI (0x1F << ESPI_ERR_INT_MAP_SHIFT)
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700359#define ESPI_SUB_DECODE_SLV_SHIFT 3
360#define ESPI_SUB_DECODE_SLV_MASK (0x3 << ESPI_SUB_DECODE_SLV_SHIFT)
361#define ESPI_SUB_DECODE_EN (1 << 2)
Raul E Rangel1d0e4932021-04-02 10:27:11 -0600362#define ESPI_BUS_MASTER_EN (1 << 1)
363#define ESPI_SW_RST (1 << 0)
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700364
Raul E Rangel1d0e4932021-04-02 10:27:11 -0600365#define ESPI_SLAVE0_INT_EN 0x6C
Raul E Rangel47740122021-04-02 10:16:54 -0600366#define ESPI_SLAVE0_INT_STS 0x70
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700367#define ESPI_STATUS_DNCMD_COMPLETE (1 << 28)
368#define ESPI_STATUS_NON_FATAL_ERROR (1 << 6)
369#define ESPI_STATUS_FATAL_ERROR (1 << 5)
370#define ESPI_STATUS_NO_RESPONSE (1 << 4)
371#define ESPI_STATUS_CRC_ERR (1 << 2)
372#define ESPI_STATUS_WAIT_TIMEOUT (1 << 1)
373#define ESPI_STATUS_BUS_ERROR (1 << 0)
374
375#define ESPI_RXVW_POLARITY 0xac
376
377#define ESPI_CMD_TIMEOUT_US 100
Raul E Rangel0318dc12021-05-21 16:31:52 -0600378#define ESPI_CH_READY_TIMEOUT_US 10000
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700379
380union espi_txhdr0 {
381 uint32_t val;
382 struct {
383 uint32_t cmd_type:3;
384 uint32_t cmd_sts:1;
385 uint32_t slave_sel:2;
386 uint32_t rsvd:2;
387 uint32_t hdata0:8;
388 uint32_t hdata1:8;
389 uint32_t hdata2:8;
390 };
391} __packed;
392
393union espi_txhdr1 {
394 uint32_t val;
395 struct {
396 uint32_t hdata3:8;
397 uint32_t hdata4:8;
398 uint32_t hdata5:8;
399 uint32_t hdata6:8;
400 };
401} __packed;
402
403union espi_txhdr2 {
404 uint32_t val;
405 struct {
406 uint32_t hdata7:8;
407 uint32_t rsvd:24;
408 };
409} __packed;
410
411union espi_txdata {
412 uint32_t val;
413 struct {
414 uint32_t byte0:8;
415 uint32_t byte1:8;
416 uint32_t byte2:8;
417 uint32_t byte3:8;
418 };
419} __packed;
420
421struct espi_cmd {
422 union espi_txhdr0 hdr0;
423 union espi_txhdr1 hdr1;
424 union espi_txhdr2 hdr2;
425 union espi_txdata data;
Raul E Rangel12c05422021-05-11 11:13:38 -0600426 uint32_t expected_status_codes;
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700427} __packed;
428
429/* Wait up to ESPI_CMD_TIMEOUT_US for hardware to clear DNCMD_STATUS bit. */
430static int espi_wait_ready(void)
431{
432 struct stopwatch sw;
433 union espi_txhdr0 hdr0;
434
435 stopwatch_init_usecs_expire(&sw, ESPI_CMD_TIMEOUT_US);
436 do {
437 hdr0.val = espi_read32(ESPI_DN_TX_HDR0);
438 if (!hdr0.cmd_sts)
439 return 0;
440 } while (!stopwatch_expired(&sw));
441
442 return -1;
443}
444
445/* Clear interrupt status register */
446static void espi_clear_status(void)
447{
Raul E Rangel47740122021-04-02 10:16:54 -0600448 uint32_t status = espi_read32(ESPI_SLAVE0_INT_STS);
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700449 if (status)
Raul E Rangel47740122021-04-02 10:16:54 -0600450 espi_write32(ESPI_SLAVE0_INT_STS, status);
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700451}
452
453/*
454 * Wait up to ESPI_CMD_TIMEOUT_US for interrupt status register to update after sending a
455 * command.
456 */
Felix Held1ba38332020-08-10 20:45:30 +0200457static int espi_poll_status(uint32_t *status)
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700458{
459 struct stopwatch sw;
460
461 stopwatch_init_usecs_expire(&sw, ESPI_CMD_TIMEOUT_US);
462 do {
Raul E Rangel47740122021-04-02 10:16:54 -0600463 *status = espi_read32(ESPI_SLAVE0_INT_STS);
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700464 if (*status)
465 return 0;
466 } while (!stopwatch_expired(&sw));
467
468 printk(BIOS_ERR, "Error: eSPI timed out waiting for status update.\n");
469
470 return -1;
471}
472
473static void espi_show_failure(const struct espi_cmd *cmd, const char *str, uint32_t status)
474{
475 printk(BIOS_ERR, "eSPI cmd0-cmd2: %08x %08x %08x data: %08x.\n",
476 cmd->hdr0.val, cmd->hdr1.val, cmd->hdr2.val, cmd->data.val);
477 printk(BIOS_ERR, "%s (Status = 0x%x)\n", str, status);
478}
479
480static int espi_send_command(const struct espi_cmd *cmd)
481{
482 uint32_t status;
483
484 if (CONFIG(ESPI_DEBUG))
Raul E Rangelf7027052021-06-29 13:12:19 -0600485 printk(BIOS_DEBUG, "eSPI cmd0-cmd2: %08x %08x %08x data: %08x.\n",
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700486 cmd->hdr0.val, cmd->hdr1.val, cmd->hdr2.val, cmd->data.val);
487
488 if (espi_wait_ready() == -1) {
489 espi_show_failure(cmd, "Error: eSPI was not ready to accept a command", 0);
490 return -1;
491 }
492
493 espi_clear_status();
494
495 espi_write32(ESPI_DN_TX_HDR1, cmd->hdr1.val);
496 espi_write32(ESPI_DN_TX_HDR2, cmd->hdr2.val);
497 espi_write32(ESPI_DN_TX_DATA, cmd->data.val);
498
499 /* Dword 0 must be last as this write triggers the transaction */
500 espi_write32(ESPI_DN_TX_HDR0, cmd->hdr0.val);
501
502 if (espi_wait_ready() == -1) {
503 espi_show_failure(cmd,
504 "Error: eSPI timed out waiting for command to complete", 0);
505 return -1;
506 }
507
Felix Held1ba38332020-08-10 20:45:30 +0200508 if (espi_poll_status(&status) == -1) {
509 espi_show_failure(cmd, "Error: eSPI poll status failed", 0);
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700510 return -1;
511 }
512
513 /* If command did not complete downstream, return error. */
514 if (!(status & ESPI_STATUS_DNCMD_COMPLETE)) {
515 espi_show_failure(cmd, "Error: eSPI downstream command completion failure",
516 status);
517 return -1;
518 }
519
Raul E Rangel12c05422021-05-11 11:13:38 -0600520 if (status & ~(ESPI_STATUS_DNCMD_COMPLETE | cmd->expected_status_codes)) {
Felix Held316d59c2020-08-10 20:42:20 +0200521 espi_show_failure(cmd, "Error: unexpected eSPI status register bits set",
522 status);
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700523 return -1;
524 }
525
Raul E Rangel12c05422021-05-11 11:13:38 -0600526 espi_write32(ESPI_SLAVE0_INT_STS, status);
Raul E Rangel66c52ff2021-04-02 10:18:25 -0600527
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700528 return 0;
529}
530
531static int espi_send_reset(void)
532{
533 struct espi_cmd cmd = {
534 .hdr0 = {
535 .cmd_type = CMD_TYPE_IN_BAND_RESET,
536 .cmd_sts = 1,
537 },
Raul E Rangel12c05422021-05-11 11:13:38 -0600538
539 /*
540 * When performing an in-band reset the host controller and the
541 * peripheral can have mismatched IO configs.
542 *
543 * i.e., The eSPI peripheral can be in IO-4 mode while, the
544 * eSPI host will be in IO-1. This results in the peripheral
545 * getting invalid packets and thus not responding.
546 *
547 * If the peripheral is alerting when we perform an in-band
548 * reset, there is a race condition in espi_send_command.
549 * 1) espi_send_command clears the interrupt status.
550 * 2) eSPI host controller hardware notices the alert and sends
551 * a GET_STATUS.
552 * 3) espi_send_command writes the in-band reset command.
553 * 4) eSPI hardware enqueues the in-band reset until GET_STATUS
554 * is complete.
555 * 5) GET_STATUS fails with NO_RESPONSE and sets the interrupt
556 * status.
557 * 6) eSPI hardware performs in-band reset.
558 * 7) espi_send_command checks the status and sees a
559 * NO_RESPONSE bit.
560 *
561 * As a workaround we allow the NO_RESPONSE status code when
562 * we perform an in-band reset.
563 */
564 .expected_status_codes = ESPI_STATUS_NO_RESPONSE,
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700565 };
566
567 return espi_send_command(&cmd);
568}
569
Raul E Rangel43aa5272021-05-21 17:04:28 -0600570static int espi_send_pltrst(const struct espi_config *mb_cfg, bool assert)
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700571{
572 struct espi_cmd cmd = {
573 .hdr0 = {
574 .cmd_type = CMD_TYPE_VW,
575 .cmd_sts = 1,
576 .hdata0 = 0, /* 1 VW group */
577 },
578 .data = {
579 .byte0 = ESPI_VW_INDEX_SYSTEM_EVENT_3,
Raul E Rangel43aa5272021-05-21 17:04:28 -0600580 .byte1 = assert ? ESPI_VW_SIGNAL_LOW(ESPI_VW_PLTRST)
581 : ESPI_VW_SIGNAL_HIGH(ESPI_VW_PLTRST),
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700582 },
583 };
584
585 if (!mb_cfg->vw_ch_en)
586 return 0;
587
588 return espi_send_command(&cmd);
589}
590
591/*
592 * In case of get configuration command, hdata0 contains bits 15:8 of the slave register address
593 * and hdata1 contains bits 7:0 of the slave register address.
594 */
595#define ESPI_CONFIGURATION_HDATA0(a) (((a) >> 8) & 0xff)
596#define ESPI_CONFIGURATION_HDATA1(a) ((a) & 0xff)
597
598static int espi_get_configuration(uint16_t slave_reg_addr, uint32_t *config)
599{
600 struct espi_cmd cmd = {
601 .hdr0 = {
602 .cmd_type = CMD_TYPE_GET_CONFIGURATION,
603 .cmd_sts = 1,
604 .hdata0 = ESPI_CONFIGURATION_HDATA0(slave_reg_addr),
605 .hdata1 = ESPI_CONFIGURATION_HDATA1(slave_reg_addr),
606 },
607 };
608
609 *config = 0;
610
611 if (espi_send_command(&cmd))
612 return -1;
613
614 *config = espi_read32(ESPI_DN_TX_HDR1);
615
616 if (CONFIG(ESPI_DEBUG))
617 printk(BIOS_DEBUG, "Get configuration for slave register(0x%x): 0x%x\n",
618 slave_reg_addr, *config);
619
620 return 0;
621}
622
623static int espi_set_configuration(uint16_t slave_reg_addr, uint32_t config)
624{
625 struct espi_cmd cmd = {
626 .hdr0 = {
627 .cmd_type = CMD_TYPE_SET_CONFIGURATION,
628 .cmd_sts = 1,
629 .hdata0 = ESPI_CONFIGURATION_HDATA0(slave_reg_addr),
630 .hdata1 = ESPI_CONFIGURATION_HDATA1(slave_reg_addr),
631 },
632 .hdr1 = {
633 .val = config,
634 },
635 };
636
637 return espi_send_command(&cmd);
638}
639
640static int espi_get_general_configuration(uint32_t *config)
641{
642 int ret = espi_get_configuration(ESPI_SLAVE_GENERAL_CFG, config);
643 if (ret == -1)
644 return -1;
645
646 espi_show_slave_general_configuration(*config);
647 return 0;
648}
649
650static void espi_set_io_mode_config(enum espi_io_mode mb_io_mode, uint32_t slave_caps,
651 uint32_t *slave_config, uint32_t *ctrlr_config)
652{
653 switch (mb_io_mode) {
654 case ESPI_IO_MODE_QUAD:
655 if (espi_slave_supports_quad_io(slave_caps)) {
656 *slave_config |= ESPI_SLAVE_IO_MODE_SEL_QUAD;
657 *ctrlr_config |= ESPI_IO_MODE_QUAD;
658 break;
659 }
660 printk(BIOS_ERR, "Error: eSPI Quad I/O not supported. Dropping to dual mode.\n");
661 /* Intentional fall-through */
662 case ESPI_IO_MODE_DUAL:
663 if (espi_slave_supports_dual_io(slave_caps)) {
664 *slave_config |= ESPI_SLAVE_IO_MODE_SEL_DUAL;
665 *ctrlr_config |= ESPI_IO_MODE_DUAL;
666 break;
667 }
668 printk(BIOS_ERR,
669 "Error: eSPI Dual I/O not supported. Dropping to single mode.\n");
670 /* Intentional fall-through */
671 case ESPI_IO_MODE_SINGLE:
672 /* Single I/O mode is always supported. */
673 *slave_config |= ESPI_SLAVE_IO_MODE_SEL_SINGLE;
674 *ctrlr_config |= ESPI_IO_MODE_SINGLE;
675 break;
676 default:
677 die("No supported eSPI I/O modes!\n");
678 }
679}
680
681static void espi_set_op_freq_config(enum espi_op_freq mb_op_freq, uint32_t slave_caps,
682 uint32_t *slave_config, uint32_t *ctrlr_config)
683{
684 int slave_max_speed_mhz = espi_slave_max_speed_mhz_supported(slave_caps);
685
686 switch (mb_op_freq) {
687 case ESPI_OP_FREQ_66_MHZ:
688 if (slave_max_speed_mhz >= 66) {
689 *slave_config |= ESPI_SLAVE_OP_FREQ_SEL_66_MHZ;
690 *ctrlr_config |= ESPI_OP_FREQ_66_MHZ;
691 break;
692 }
693 printk(BIOS_ERR, "Error: eSPI 66MHz not supported. Dropping to 33MHz.\n");
694 /* Intentional fall-through */
695 case ESPI_OP_FREQ_33_MHZ:
696 if (slave_max_speed_mhz >= 33) {
697 *slave_config |= ESPI_SLAVE_OP_FREQ_SEL_33_MHZ;
698 *ctrlr_config |= ESPI_OP_FREQ_33_MHZ;
699 break;
700 }
701 printk(BIOS_ERR, "Error: eSPI 33MHz not supported. Dropping to 16MHz.\n");
702 /* Intentional fall-through */
703 case ESPI_OP_FREQ_16_MHZ:
704 /*
705 * eSPI spec says the minimum frequency is 20MHz, but AMD datasheets support
706 * 16.7 Mhz.
707 */
708 if (slave_max_speed_mhz > 0) {
709 *slave_config |= ESPI_SLAVE_OP_FREQ_SEL_20_MHZ;
710 *ctrlr_config |= ESPI_OP_FREQ_16_MHZ;
711 break;
712 }
713 /* Intentional fall-through */
714 default:
715 die("No supported eSPI Operating Frequency!\n");
716 }
717}
718
Raul E Rangel8317e722021-05-05 13:38:27 -0600719static void espi_set_alert_pin_config(enum espi_alert_pin alert_pin, uint32_t slave_caps,
720 uint32_t *slave_config, uint32_t *ctrlr_config)
721{
722 switch (alert_pin) {
723 case ESPI_ALERT_PIN_IN_BAND:
724 *slave_config |= ESPI_SLAVE_ALERT_MODE_IO1;
725 return;
726 case ESPI_ALERT_PIN_PUSH_PULL:
727 *slave_config |= ESPI_SLAVE_ALERT_MODE_PIN | ESPI_SLAVE_PUSH_PULL_ALERT_SEL;
728 *ctrlr_config |= ESPI_ALERT_MODE;
729 return;
730 case ESPI_ALERT_PIN_OPEN_DRAIN:
731 if (!(slave_caps & ESPI_SLAVE_OPEN_DRAIN_ALERT_SUPP))
732 die("eSPI peripheral does not support open drain alert!");
733
734 *slave_config |= ESPI_SLAVE_ALERT_MODE_PIN | ESPI_SLAVE_OPEN_DRAIN_ALERT_SEL;
735 *ctrlr_config |= ESPI_ALERT_MODE;
736 return;
737 default:
738 die("Unknown espi alert config: %u!\n", alert_pin);
739 }
740}
741
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700742static int espi_set_general_configuration(const struct espi_config *mb_cfg, uint32_t slave_caps)
743{
744 uint32_t slave_config = 0;
745 uint32_t ctrlr_config = 0;
746
747 if (mb_cfg->crc_check_enable) {
748 slave_config |= ESPI_SLAVE_CRC_ENABLE;
749 ctrlr_config |= ESPI_CRC_CHECKING_EN;
750 }
751
Raul E Rangel8317e722021-05-05 13:38:27 -0600752 espi_set_alert_pin_config(mb_cfg->alert_pin, slave_caps, &slave_config, &ctrlr_config);
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700753 espi_set_io_mode_config(mb_cfg->io_mode, slave_caps, &slave_config, &ctrlr_config);
754 espi_set_op_freq_config(mb_cfg->op_freq_mhz, slave_caps, &slave_config, &ctrlr_config);
755
756 if (CONFIG(ESPI_DEBUG))
757 printk(BIOS_INFO, "Setting general configuration: slave: 0x%x controller: 0x%x\n",
758 slave_config, ctrlr_config);
759
Raul E Rangeld2d762a2021-05-05 13:30:10 -0600760 espi_show_slave_general_configuration(slave_config);
761
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700762 if (espi_set_configuration(ESPI_SLAVE_GENERAL_CFG, slave_config) == -1)
763 return -1;
764
765 espi_write32(ESPI_SLAVE0_CONFIG, ctrlr_config);
766 return 0;
767}
768
769static int espi_wait_channel_ready(uint16_t slave_reg_addr)
770{
771 struct stopwatch sw;
772 uint32_t config;
773
774 stopwatch_init_usecs_expire(&sw, ESPI_CH_READY_TIMEOUT_US);
775 do {
776 espi_get_configuration(slave_reg_addr, &config);
777 if (espi_slave_is_channel_ready(config))
778 return 0;
779 } while (!stopwatch_expired(&sw));
780
781 printk(BIOS_ERR, "Error: Channel is not ready after %d usec (slave addr: 0x%x)\n",
782 ESPI_CH_READY_TIMEOUT_US, slave_reg_addr);
783 return -1;
784
785}
786
787static void espi_enable_ctrlr_channel(uint32_t channel_en)
788{
789 uint32_t reg = espi_read32(ESPI_SLAVE0_CONFIG);
790
791 reg |= channel_en;
792
793 espi_write32(ESPI_SLAVE0_CONFIG, reg);
794}
795
796static int espi_set_channel_configuration(uint32_t slave_config, uint32_t slave_reg_addr,
797 uint32_t ctrlr_enable)
798{
799 if (espi_set_configuration(slave_reg_addr, slave_config) == -1)
800 return -1;
801
802 if (!(slave_config & ESPI_SLAVE_CHANNEL_ENABLE))
803 return 0;
804
805 if (espi_wait_channel_ready(slave_reg_addr) == -1)
806 return -1;
807
808 espi_enable_ctrlr_channel(ctrlr_enable);
809 return 0;
810}
811
812static int espi_setup_vw_channel(const struct espi_config *mb_cfg, uint32_t slave_caps)
813{
814 uint32_t slave_vw_caps;
815 uint32_t ctrlr_vw_caps;
816 uint32_t slave_vw_count_supp;
817 uint32_t ctrlr_vw_count_supp;
818 uint32_t use_vw_count;
819 uint32_t slave_config;
820
821 if (!mb_cfg->vw_ch_en)
822 return 0;
823
824 if (!espi_slave_supports_vw_channel(slave_caps)) {
825 printk(BIOS_ERR, "Error: eSPI slave doesn't support VW channel!\n");
826 return -1;
827 }
828
829 if (espi_get_configuration(ESPI_SLAVE_VW_CFG, &slave_vw_caps) == -1)
830 return -1;
831
832 ctrlr_vw_caps = espi_read32(ESPI_MASTER_CAP);
833 ctrlr_vw_count_supp = (ctrlr_vw_caps & ESPI_VW_MAX_SIZE_MASK) >> ESPI_VW_MAX_SIZE_SHIFT;
834
835 slave_vw_count_supp = espi_slave_get_vw_count_supp(slave_vw_caps);
836 use_vw_count = MIN(ctrlr_vw_count_supp, slave_vw_count_supp);
837
838 slave_config = ESPI_SLAVE_CHANNEL_ENABLE | ESPI_SLAVE_VW_COUNT_SEL_VAL(use_vw_count);
839 return espi_set_channel_configuration(slave_config, ESPI_SLAVE_VW_CFG, ESPI_VW_CH_EN);
840}
841
842static int espi_setup_periph_channel(const struct espi_config *mb_cfg, uint32_t slave_caps)
843{
844 uint32_t slave_config;
845 /* Peripheral channel requires BME bit to be set when enabling the channel. */
Raul E Rangel8fef0b72021-05-24 13:02:40 -0600846 const uint32_t slave_en_mask =
847 ESPI_SLAVE_CHANNEL_ENABLE | ESPI_SLAVE_PERIPH_BUS_MASTER_ENABLE;
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700848
849 if (espi_get_configuration(ESPI_SLAVE_PERIPH_CFG, &slave_config) == -1)
850 return -1;
851
852 /*
853 * Peripheral channel is the only one which is enabled on reset. So, if the mainboard
854 * wants to disable it, set configuration to disable peripheral channel. It also
855 * requires that BME bit be cleared.
856 */
857 if (mb_cfg->periph_ch_en) {
858 if (!espi_slave_supports_periph_channel(slave_caps)) {
859 printk(BIOS_ERR, "Error: eSPI slave doesn't support periph channel!\n");
860 return -1;
861 }
862 slave_config |= slave_en_mask;
863 } else {
864 slave_config &= ~slave_en_mask;
865 }
866
Raul E Rangel7222f7e2021-04-09 14:15:42 -0600867 espi_show_slave_peripheral_channel_configuration(slave_config);
868
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700869 return espi_set_channel_configuration(slave_config, ESPI_SLAVE_PERIPH_CFG,
870 ESPI_PERIPH_CH_EN);
871}
872
873static int espi_setup_oob_channel(const struct espi_config *mb_cfg, uint32_t slave_caps)
874{
875 uint32_t slave_config;
876
877 if (!mb_cfg->oob_ch_en)
878 return 0;
879
880 if (!espi_slave_supports_oob_channel(slave_caps)) {
881 printk(BIOS_ERR, "Error: eSPI slave doesn't support OOB channel!\n");
882 return -1;
883 }
884
885 if (espi_get_configuration(ESPI_SLAVE_OOB_CFG, &slave_config) == -1)
886 return -1;
887
888 slave_config |= ESPI_SLAVE_CHANNEL_ENABLE;
889
890 return espi_set_channel_configuration(slave_config, ESPI_SLAVE_OOB_CFG,
891 ESPI_OOB_CH_EN);
892}
893
894static int espi_setup_flash_channel(const struct espi_config *mb_cfg, uint32_t slave_caps)
895{
896 uint32_t slave_config;
897
898 if (!mb_cfg->flash_ch_en)
899 return 0;
900
901 if (!espi_slave_supports_flash_channel(slave_caps)) {
902 printk(BIOS_ERR, "Error: eSPI slave doesn't support flash channel!\n");
903 return -1;
904 }
905
906 if (espi_get_configuration(ESPI_SLAVE_FLASH_CFG, &slave_config) == -1)
907 return -1;
908
909 slave_config |= ESPI_SLAVE_CHANNEL_ENABLE;
910
911 return espi_set_channel_configuration(slave_config, ESPI_SLAVE_FLASH_CFG,
912 ESPI_FLASH_CH_EN);
913}
914
915static void espi_set_initial_config(const struct espi_config *mb_cfg)
916{
917 uint32_t espi_initial_mode = ESPI_OP_FREQ_16_MHZ | ESPI_IO_MODE_SINGLE;
918
Raul E Rangeldcec4092021-05-07 15:35:10 -0600919 switch (mb_cfg->alert_pin) {
920 case ESPI_ALERT_PIN_IN_BAND:
921 break;
922 case ESPI_ALERT_PIN_PUSH_PULL:
923 case ESPI_ALERT_PIN_OPEN_DRAIN:
924 espi_initial_mode |= ESPI_ALERT_MODE;
925 break;
926 default:
927 die("Unknown espi alert config: %u!\n", mb_cfg->alert_pin);
928 }
929
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700930 espi_write32(ESPI_SLAVE0_CONFIG, espi_initial_mode);
931}
932
933static void espi_setup_subtractive_decode(const struct espi_config *mb_cfg)
934{
935 uint32_t global_ctrl_reg;
936 global_ctrl_reg = espi_read32(ESPI_GLOBAL_CONTROL_1);
937
938 if (mb_cfg->subtractive_decode) {
939 global_ctrl_reg &= ~ESPI_SUB_DECODE_SLV_MASK;
940 global_ctrl_reg |= ESPI_SUB_DECODE_EN;
941
942 } else {
943 global_ctrl_reg &= ~ESPI_SUB_DECODE_EN;
944 }
945 espi_write32(ESPI_GLOBAL_CONTROL_1, global_ctrl_reg);
946}
947
948int espi_setup(void)
949{
950 uint32_t slave_caps;
951 const struct espi_config *cfg = espi_get_config();
952
Martin Roth7a2bfeb2021-05-14 10:57:31 -0600953 printk(BIOS_SPEW, "Initializing ESPI.\n");
954
Raul E Rangelb92383a2021-04-02 10:32:03 -0600955 espi_write32(ESPI_GLOBAL_CONTROL_0, ESPI_AL_STOP_EN);
956 espi_write32(ESPI_GLOBAL_CONTROL_1, ESPI_RGCMD_INT(23) | ESPI_ERR_INT_SMI);
957 espi_write32(ESPI_SLAVE0_INT_EN, 0);
958 espi_clear_status();
Raul E Rangelb95f8482021-04-02 13:47:09 -0600959 espi_clear_decodes();
Raul E Rangelb92383a2021-04-02 10:32:03 -0600960
Furquan Shaikh70063ff52020-05-11 14:28:13 -0700961 /*
962 * Boot sequence: Step 1
963 * Set correct initial configuration to talk to the slave:
964 * Set clock frequency to 16.7MHz and single IO mode.
965 */
966 espi_set_initial_config(cfg);
967
968 /*
969 * Boot sequence: Step 2
970 * Send in-band reset
971 * The resets affects both host and slave devices, so set initial config again.
972 */
973 if (espi_send_reset() == -1) {
974 printk(BIOS_ERR, "Error: In-band reset failed!\n");
975 return -1;
976 }
977 espi_set_initial_config(cfg);
978
979 /*
980 * Boot sequence: Step 3
981 * Get configuration of slave device.
982 */
983 if (espi_get_general_configuration(&slave_caps) == -1) {
984 printk(BIOS_ERR, "Error: Slave GET_CONFIGURATION failed!\n");
985 return -1;
986 }
987
988 /*
989 * Boot sequence:
990 * Step 4: Write slave device general config
991 * Step 5: Set host slave config
992 */
993 if (espi_set_general_configuration(cfg, slave_caps) == -1) {
994 printk(BIOS_ERR, "Error: Slave SET_CONFIGURATION failed!\n");
995 return -1;
996 }
997
998 /*
999 * Setup polarity before enabling the VW channel so any interrupts
1000 * received will have the correct polarity.
1001 */
1002 espi_write32(ESPI_RXVW_POLARITY, cfg->vw_irq_polarity);
1003
1004 /*
1005 * Boot Sequences: Steps 6 - 9
1006 * Channel setup
1007 */
1008 /* Set up VW first so we can deassert PLTRST#. */
1009 if (espi_setup_vw_channel(cfg, slave_caps) == -1) {
1010 printk(BIOS_ERR, "Error: Setup VW channel failed!\n");
1011 return -1;
1012 }
1013
Raul E Rangel43aa5272021-05-21 17:04:28 -06001014 /* Assert PLTRST# if VW channel is enabled by mainboard. */
1015 if (espi_send_pltrst(cfg, true) == -1) {
1016 printk(BIOS_ERR, "Error: PLTRST# assertion failed!\n");
1017 return -1;
1018 }
1019
Furquan Shaikh70063ff52020-05-11 14:28:13 -07001020 /* De-assert PLTRST# if VW channel is enabled by mainboard. */
Raul E Rangel43aa5272021-05-21 17:04:28 -06001021 if (espi_send_pltrst(cfg, false) == -1) {
1022 printk(BIOS_ERR, "Error: PLTRST# deassertion failed!\n");
Furquan Shaikh70063ff52020-05-11 14:28:13 -07001023 return -1;
1024 }
1025
1026 if (espi_setup_periph_channel(cfg, slave_caps) == -1) {
1027 printk(BIOS_ERR, "Error: Setup Periph channel failed!\n");
1028 return -1;
1029 }
1030
1031 if (espi_setup_oob_channel(cfg, slave_caps) == -1) {
1032 printk(BIOS_ERR, "Error: Setup OOB channel failed!\n");
1033 return -1;
1034 }
1035
1036 if (espi_setup_flash_channel(cfg, slave_caps) == -1) {
1037 printk(BIOS_ERR, "Error: Setup Flash channel failed!\n");
1038 return -1;
1039 }
1040
Raul E Rangel61ac1bc2021-04-02 10:55:27 -06001041 if (espi_configure_decodes(cfg) == -1) {
1042 printk(BIOS_ERR, "Error: Configuring decodes failed!\n");
1043 return -1;
1044 }
1045
Furquan Shaikh70063ff52020-05-11 14:28:13 -07001046 /* Enable subtractive decode if configured */
Felix Helda2642d02021-02-17 00:32:46 +01001047 espi_setup_subtractive_decode(cfg);
Furquan Shaikh70063ff52020-05-11 14:28:13 -07001048
Raul E Rangelb92383a2021-04-02 10:32:03 -06001049 espi_write32(ESPI_GLOBAL_CONTROL_1,
1050 espi_read32(ESPI_GLOBAL_CONTROL_1) | ESPI_BUS_MASTER_EN);
1051
Martin Roth7a2bfeb2021-05-14 10:57:31 -06001052 printk(BIOS_SPEW, "Finished initializing ESPI.\n");
1053
Furquan Shaikh70063ff52020-05-11 14:28:13 -07001054 return 0;
1055}