Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 2 | |
| 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 Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 8 | #include <espi.h> |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 9 | #include <soc/pci_devs.h> |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 10 | #include <timer.h> |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 11 | #include <types.h> |
| 12 | |
Furquan Shaikh | 98bc961 | 2020-05-09 19:31:55 -0700 | [diff] [blame] | 13 | static uintptr_t espi_bar; |
| 14 | |
| 15 | void espi_update_static_bar(uintptr_t bar) |
| 16 | { |
| 17 | espi_bar = bar; |
| 18 | } |
| 19 | |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 20 | static uintptr_t espi_get_bar(void) |
| 21 | { |
Martin Roth | b39e10d | 2020-07-14 11:08:55 -0600 | [diff] [blame] | 22 | if (ENV_X86 && !espi_bar) |
| 23 | espi_update_static_bar(lpc_get_spibase() + ESPI_OFFSET_FROM_BAR); |
Furquan Shaikh | 98bc961 | 2020-05-09 19:31:55 -0700 | [diff] [blame] | 24 | return espi_bar; |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Felix Held | 92dd678 | 2020-08-10 20:27:58 +0200 | [diff] [blame] | 27 | static uint32_t espi_read32(unsigned int reg) |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 28 | { |
| 29 | return read32((void *)(espi_get_bar() + reg)); |
| 30 | } |
| 31 | |
Felix Held | 92dd678 | 2020-08-10 20:27:58 +0200 | [diff] [blame] | 32 | static void espi_write32(unsigned int reg, uint32_t val) |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 33 | { |
| 34 | write32((void *)(espi_get_bar() + reg), val); |
| 35 | } |
| 36 | |
Felix Held | 92dd678 | 2020-08-10 20:27:58 +0200 | [diff] [blame] | 37 | static uint16_t espi_read16(unsigned int reg) |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 38 | { |
| 39 | return read16((void *)(espi_get_bar() + reg)); |
| 40 | } |
| 41 | |
Felix Held | 92dd678 | 2020-08-10 20:27:58 +0200 | [diff] [blame] | 42 | static void espi_write16(unsigned int reg, uint16_t val) |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 43 | { |
| 44 | write16((void *)(espi_get_bar() + reg), val); |
| 45 | } |
| 46 | |
Felix Held | 92dd678 | 2020-08-10 20:27:58 +0200 | [diff] [blame] | 47 | static uint8_t espi_read8(unsigned int reg) |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 48 | { |
| 49 | return read8((void *)(espi_get_bar() + reg)); |
| 50 | } |
| 51 | |
Felix Held | 92dd678 | 2020-08-10 20:27:58 +0200 | [diff] [blame] | 52 | static void espi_write8(unsigned int reg, uint8_t val) |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 53 | { |
| 54 | write8((void *)(espi_get_bar() + reg), val); |
| 55 | } |
| 56 | |
Felix Held | f08fbf8 | 2020-08-10 20:30:36 +0200 | [diff] [blame] | 57 | static void espi_enable_decode(uint32_t decode_en) |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 58 | { |
| 59 | uint32_t val; |
| 60 | |
| 61 | val = espi_read32(ESPI_DECODE); |
| 62 | val |= decode_en; |
| 63 | espi_write32(ESPI_DECODE, val); |
| 64 | } |
| 65 | |
Felix Held | f08fbf8 | 2020-08-10 20:30:36 +0200 | [diff] [blame] | 66 | static bool espi_is_decode_enabled(uint32_t decode) |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 67 | { |
| 68 | uint32_t val; |
| 69 | |
| 70 | val = espi_read32(ESPI_DECODE); |
| 71 | return !!(val & decode); |
| 72 | } |
| 73 | |
| 74 | static 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 | |
| 89 | static 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 Rangel | b95f848 | 2021-04-02 13:47:09 -0600 | [diff] [blame] | 101 | static void espi_clear_decodes(void) |
Martin Roth | 011bf13 | 2021-03-23 13:20:42 -0600 | [diff] [blame] | 102 | { |
| 103 | unsigned int idx; |
| 104 | |
| 105 | /* First turn off all enable bits, then zero base, range, and size registers */ |
| 106 | /* |
| 107 | * There is currently a bug where the SMU will lock up at times if the port80h enable |
| 108 | * bit is cleared. See b/183974365 |
| 109 | */ |
| 110 | espi_write16(ESPI_DECODE, (espi_read16(ESPI_DECODE) & ESPI_DECODE_IO_0x80_EN)); |
| 111 | |
| 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 Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 122 | /* |
| 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 | */ |
| 126 | static int espi_std_io_decode(uint16_t base, size_t size) |
| 127 | { |
Felix Held | c0d4eeb | 2020-08-10 20:37:16 +0200 | [diff] [blame] | 128 | if (size == 2 && base == 0x2e) |
| 129 | return ESPI_DECODE_IO_0X2E_0X2F_EN; |
| 130 | |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 131 | if (size != 1) |
Felix Held | 4bf419f | 2020-08-10 20:33:25 +0200 | [diff] [blame] | 132 | return -1; |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 133 | |
| 134 | switch (base) { |
| 135 | case 0x80: |
Felix Held | 4bf419f | 2020-08-10 20:33:25 +0200 | [diff] [blame] | 136 | return ESPI_DECODE_IO_0x80_EN; |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 137 | case 0x60: |
| 138 | case 0x64: |
Felix Held | 4bf419f | 2020-08-10 20:33:25 +0200 | [diff] [blame] | 139 | return ESPI_DECODE_IO_0X60_0X64_EN; |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 140 | case 0x2e: |
| 141 | case 0x2f: |
Felix Held | 4bf419f | 2020-08-10 20:33:25 +0200 | [diff] [blame] | 142 | return ESPI_DECODE_IO_0X2E_0X2F_EN; |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 143 | default: |
Felix Held | 4bf419f | 2020-08-10 20:33:25 +0200 | [diff] [blame] | 144 | return -1; |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 145 | } |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static size_t espi_get_io_window_size(int idx) |
| 149 | { |
| 150 | return espi_read8(ESPI_IO_RANGE_SIZE(idx)) + 1; |
| 151 | } |
| 152 | |
| 153 | static 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 | |
| 159 | static 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 | |
| 199 | int 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 Held | b026c7c | 2020-08-10 20:43:53 +0200 | [diff] [blame] | 207 | } else { |
| 208 | return espi_open_generic_io_window(base, size); |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 209 | } |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static 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 | |
| 227 | static 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 | |
| 240 | static size_t espi_get_mmio_window_size(int idx) |
| 241 | { |
| 242 | return espi_read16(ESPI_MMIO_RANGE_SIZE(idx)) + 1; |
| 243 | } |
| 244 | |
| 245 | static 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 | |
| 251 | int 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 | |
| 291 | static 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 Rangel | 61ac1bc | 2021-04-02 10:55:27 -0600 | [diff] [blame] | 301 | static int espi_configure_decodes(const struct espi_config *cfg) |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 302 | { |
Raul E Rangel | 61ac1bc | 2021-04-02 10:55:27 -0600 | [diff] [blame] | 303 | int i, ret; |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 304 | |
| 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 Rangel | 61ac1bc | 2021-04-02 10:55:27 -0600 | [diff] [blame] | 310 | 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 Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 314 | } |
Raul E Rangel | 61ac1bc | 2021-04-02 10:55:27 -0600 | [diff] [blame] | 315 | |
| 316 | return 0; |
Furquan Shaikh | f318e03 | 2020-05-04 23:38:53 -0700 | [diff] [blame] | 317 | } |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 318 | |
| 319 | #define ESPI_DN_TX_HDR0 0x00 |
| 320 | enum 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 Rangel | 1d0e493 | 2021-04-02 10:27:11 -0600 | [diff] [blame] | 338 | #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 Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 350 | #define ESPI_GLOBAL_CONTROL_1 0x34 |
Raul E Rangel | 1d0e493 | 2021-04-02 10:27:11 -0600 | [diff] [blame] | 351 | #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 Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 359 | #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 Rangel | 1d0e493 | 2021-04-02 10:27:11 -0600 | [diff] [blame] | 362 | #define ESPI_BUS_MASTER_EN (1 << 1) |
| 363 | #define ESPI_SW_RST (1 << 0) |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 364 | |
Raul E Rangel | 1d0e493 | 2021-04-02 10:27:11 -0600 | [diff] [blame] | 365 | #define ESPI_SLAVE0_INT_EN 0x6C |
Raul E Rangel | 4774012 | 2021-04-02 10:16:54 -0600 | [diff] [blame] | 366 | #define ESPI_SLAVE0_INT_STS 0x70 |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 367 | #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 |
| 378 | #define ESPI_CH_READY_TIMEOUT_US 1000 |
| 379 | |
| 380 | union 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 | |
| 393 | union 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 | |
| 403 | union espi_txhdr2 { |
| 404 | uint32_t val; |
| 405 | struct { |
| 406 | uint32_t hdata7:8; |
| 407 | uint32_t rsvd:24; |
| 408 | }; |
| 409 | } __packed; |
| 410 | |
| 411 | union 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 | |
| 421 | struct espi_cmd { |
| 422 | union espi_txhdr0 hdr0; |
| 423 | union espi_txhdr1 hdr1; |
| 424 | union espi_txhdr2 hdr2; |
| 425 | union espi_txdata data; |
| 426 | } __packed; |
| 427 | |
| 428 | /* Wait up to ESPI_CMD_TIMEOUT_US for hardware to clear DNCMD_STATUS bit. */ |
| 429 | static int espi_wait_ready(void) |
| 430 | { |
| 431 | struct stopwatch sw; |
| 432 | union espi_txhdr0 hdr0; |
| 433 | |
| 434 | stopwatch_init_usecs_expire(&sw, ESPI_CMD_TIMEOUT_US); |
| 435 | do { |
| 436 | hdr0.val = espi_read32(ESPI_DN_TX_HDR0); |
| 437 | if (!hdr0.cmd_sts) |
| 438 | return 0; |
| 439 | } while (!stopwatch_expired(&sw)); |
| 440 | |
| 441 | return -1; |
| 442 | } |
| 443 | |
| 444 | /* Clear interrupt status register */ |
| 445 | static void espi_clear_status(void) |
| 446 | { |
Raul E Rangel | 4774012 | 2021-04-02 10:16:54 -0600 | [diff] [blame] | 447 | uint32_t status = espi_read32(ESPI_SLAVE0_INT_STS); |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 448 | if (status) |
Raul E Rangel | 4774012 | 2021-04-02 10:16:54 -0600 | [diff] [blame] | 449 | espi_write32(ESPI_SLAVE0_INT_STS, status); |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Wait up to ESPI_CMD_TIMEOUT_US for interrupt status register to update after sending a |
| 454 | * command. |
| 455 | */ |
Felix Held | 1ba3833 | 2020-08-10 20:45:30 +0200 | [diff] [blame] | 456 | static int espi_poll_status(uint32_t *status) |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 457 | { |
| 458 | struct stopwatch sw; |
| 459 | |
| 460 | stopwatch_init_usecs_expire(&sw, ESPI_CMD_TIMEOUT_US); |
| 461 | do { |
Raul E Rangel | 4774012 | 2021-04-02 10:16:54 -0600 | [diff] [blame] | 462 | *status = espi_read32(ESPI_SLAVE0_INT_STS); |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 463 | if (*status) |
| 464 | return 0; |
| 465 | } while (!stopwatch_expired(&sw)); |
| 466 | |
| 467 | printk(BIOS_ERR, "Error: eSPI timed out waiting for status update.\n"); |
| 468 | |
| 469 | return -1; |
| 470 | } |
| 471 | |
| 472 | static void espi_show_failure(const struct espi_cmd *cmd, const char *str, uint32_t status) |
| 473 | { |
| 474 | printk(BIOS_ERR, "eSPI cmd0-cmd2: %08x %08x %08x data: %08x.\n", |
| 475 | cmd->hdr0.val, cmd->hdr1.val, cmd->hdr2.val, cmd->data.val); |
| 476 | printk(BIOS_ERR, "%s (Status = 0x%x)\n", str, status); |
| 477 | } |
| 478 | |
| 479 | static int espi_send_command(const struct espi_cmd *cmd) |
| 480 | { |
| 481 | uint32_t status; |
| 482 | |
| 483 | if (CONFIG(ESPI_DEBUG)) |
| 484 | printk(BIOS_ERR, "eSPI cmd0-cmd2: %08x %08x %08x data: %08x.\n", |
| 485 | cmd->hdr0.val, cmd->hdr1.val, cmd->hdr2.val, cmd->data.val); |
| 486 | |
| 487 | if (espi_wait_ready() == -1) { |
| 488 | espi_show_failure(cmd, "Error: eSPI was not ready to accept a command", 0); |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | espi_clear_status(); |
| 493 | |
| 494 | espi_write32(ESPI_DN_TX_HDR1, cmd->hdr1.val); |
| 495 | espi_write32(ESPI_DN_TX_HDR2, cmd->hdr2.val); |
| 496 | espi_write32(ESPI_DN_TX_DATA, cmd->data.val); |
| 497 | |
| 498 | /* Dword 0 must be last as this write triggers the transaction */ |
| 499 | espi_write32(ESPI_DN_TX_HDR0, cmd->hdr0.val); |
| 500 | |
| 501 | if (espi_wait_ready() == -1) { |
| 502 | espi_show_failure(cmd, |
| 503 | "Error: eSPI timed out waiting for command to complete", 0); |
| 504 | return -1; |
| 505 | } |
| 506 | |
Felix Held | 1ba3833 | 2020-08-10 20:45:30 +0200 | [diff] [blame] | 507 | if (espi_poll_status(&status) == -1) { |
| 508 | espi_show_failure(cmd, "Error: eSPI poll status failed", 0); |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 509 | return -1; |
| 510 | } |
| 511 | |
| 512 | /* If command did not complete downstream, return error. */ |
| 513 | if (!(status & ESPI_STATUS_DNCMD_COMPLETE)) { |
| 514 | espi_show_failure(cmd, "Error: eSPI downstream command completion failure", |
| 515 | status); |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | if (status & ~ESPI_STATUS_DNCMD_COMPLETE) { |
Felix Held | 316d59c | 2020-08-10 20:42:20 +0200 | [diff] [blame] | 520 | espi_show_failure(cmd, "Error: unexpected eSPI status register bits set", |
| 521 | status); |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 522 | return -1; |
| 523 | } |
| 524 | |
Raul E Rangel | 66c52ff | 2021-04-02 10:18:25 -0600 | [diff] [blame] | 525 | espi_write32(ESPI_SLAVE0_INT_STS, ESPI_STATUS_DNCMD_COMPLETE); |
| 526 | |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static int espi_send_reset(void) |
| 531 | { |
| 532 | struct espi_cmd cmd = { |
| 533 | .hdr0 = { |
| 534 | .cmd_type = CMD_TYPE_IN_BAND_RESET, |
| 535 | .cmd_sts = 1, |
| 536 | }, |
| 537 | }; |
| 538 | |
| 539 | return espi_send_command(&cmd); |
| 540 | } |
| 541 | |
| 542 | static int espi_send_pltrst_deassert(const struct espi_config *mb_cfg) |
| 543 | { |
| 544 | struct espi_cmd cmd = { |
| 545 | .hdr0 = { |
| 546 | .cmd_type = CMD_TYPE_VW, |
| 547 | .cmd_sts = 1, |
| 548 | .hdata0 = 0, /* 1 VW group */ |
| 549 | }, |
| 550 | .data = { |
| 551 | .byte0 = ESPI_VW_INDEX_SYSTEM_EVENT_3, |
| 552 | .byte1 = ESPI_VW_SIGNAL_HIGH(ESPI_VW_PLTRST), |
| 553 | }, |
| 554 | }; |
| 555 | |
| 556 | if (!mb_cfg->vw_ch_en) |
| 557 | return 0; |
| 558 | |
| 559 | return espi_send_command(&cmd); |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * In case of get configuration command, hdata0 contains bits 15:8 of the slave register address |
| 564 | * and hdata1 contains bits 7:0 of the slave register address. |
| 565 | */ |
| 566 | #define ESPI_CONFIGURATION_HDATA0(a) (((a) >> 8) & 0xff) |
| 567 | #define ESPI_CONFIGURATION_HDATA1(a) ((a) & 0xff) |
| 568 | |
| 569 | static int espi_get_configuration(uint16_t slave_reg_addr, uint32_t *config) |
| 570 | { |
| 571 | struct espi_cmd cmd = { |
| 572 | .hdr0 = { |
| 573 | .cmd_type = CMD_TYPE_GET_CONFIGURATION, |
| 574 | .cmd_sts = 1, |
| 575 | .hdata0 = ESPI_CONFIGURATION_HDATA0(slave_reg_addr), |
| 576 | .hdata1 = ESPI_CONFIGURATION_HDATA1(slave_reg_addr), |
| 577 | }, |
| 578 | }; |
| 579 | |
| 580 | *config = 0; |
| 581 | |
| 582 | if (espi_send_command(&cmd)) |
| 583 | return -1; |
| 584 | |
| 585 | *config = espi_read32(ESPI_DN_TX_HDR1); |
| 586 | |
| 587 | if (CONFIG(ESPI_DEBUG)) |
| 588 | printk(BIOS_DEBUG, "Get configuration for slave register(0x%x): 0x%x\n", |
| 589 | slave_reg_addr, *config); |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static int espi_set_configuration(uint16_t slave_reg_addr, uint32_t config) |
| 595 | { |
| 596 | struct espi_cmd cmd = { |
| 597 | .hdr0 = { |
| 598 | .cmd_type = CMD_TYPE_SET_CONFIGURATION, |
| 599 | .cmd_sts = 1, |
| 600 | .hdata0 = ESPI_CONFIGURATION_HDATA0(slave_reg_addr), |
| 601 | .hdata1 = ESPI_CONFIGURATION_HDATA1(slave_reg_addr), |
| 602 | }, |
| 603 | .hdr1 = { |
| 604 | .val = config, |
| 605 | }, |
| 606 | }; |
| 607 | |
| 608 | return espi_send_command(&cmd); |
| 609 | } |
| 610 | |
| 611 | static int espi_get_general_configuration(uint32_t *config) |
| 612 | { |
| 613 | int ret = espi_get_configuration(ESPI_SLAVE_GENERAL_CFG, config); |
| 614 | if (ret == -1) |
| 615 | return -1; |
| 616 | |
| 617 | espi_show_slave_general_configuration(*config); |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | static void espi_set_io_mode_config(enum espi_io_mode mb_io_mode, uint32_t slave_caps, |
| 622 | uint32_t *slave_config, uint32_t *ctrlr_config) |
| 623 | { |
| 624 | switch (mb_io_mode) { |
| 625 | case ESPI_IO_MODE_QUAD: |
| 626 | if (espi_slave_supports_quad_io(slave_caps)) { |
| 627 | *slave_config |= ESPI_SLAVE_IO_MODE_SEL_QUAD; |
| 628 | *ctrlr_config |= ESPI_IO_MODE_QUAD; |
| 629 | break; |
| 630 | } |
| 631 | printk(BIOS_ERR, "Error: eSPI Quad I/O not supported. Dropping to dual mode.\n"); |
| 632 | /* Intentional fall-through */ |
| 633 | case ESPI_IO_MODE_DUAL: |
| 634 | if (espi_slave_supports_dual_io(slave_caps)) { |
| 635 | *slave_config |= ESPI_SLAVE_IO_MODE_SEL_DUAL; |
| 636 | *ctrlr_config |= ESPI_IO_MODE_DUAL; |
| 637 | break; |
| 638 | } |
| 639 | printk(BIOS_ERR, |
| 640 | "Error: eSPI Dual I/O not supported. Dropping to single mode.\n"); |
| 641 | /* Intentional fall-through */ |
| 642 | case ESPI_IO_MODE_SINGLE: |
| 643 | /* Single I/O mode is always supported. */ |
| 644 | *slave_config |= ESPI_SLAVE_IO_MODE_SEL_SINGLE; |
| 645 | *ctrlr_config |= ESPI_IO_MODE_SINGLE; |
| 646 | break; |
| 647 | default: |
| 648 | die("No supported eSPI I/O modes!\n"); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | static void espi_set_op_freq_config(enum espi_op_freq mb_op_freq, uint32_t slave_caps, |
| 653 | uint32_t *slave_config, uint32_t *ctrlr_config) |
| 654 | { |
| 655 | int slave_max_speed_mhz = espi_slave_max_speed_mhz_supported(slave_caps); |
| 656 | |
| 657 | switch (mb_op_freq) { |
| 658 | case ESPI_OP_FREQ_66_MHZ: |
| 659 | if (slave_max_speed_mhz >= 66) { |
| 660 | *slave_config |= ESPI_SLAVE_OP_FREQ_SEL_66_MHZ; |
| 661 | *ctrlr_config |= ESPI_OP_FREQ_66_MHZ; |
| 662 | break; |
| 663 | } |
| 664 | printk(BIOS_ERR, "Error: eSPI 66MHz not supported. Dropping to 33MHz.\n"); |
| 665 | /* Intentional fall-through */ |
| 666 | case ESPI_OP_FREQ_33_MHZ: |
| 667 | if (slave_max_speed_mhz >= 33) { |
| 668 | *slave_config |= ESPI_SLAVE_OP_FREQ_SEL_33_MHZ; |
| 669 | *ctrlr_config |= ESPI_OP_FREQ_33_MHZ; |
| 670 | break; |
| 671 | } |
| 672 | printk(BIOS_ERR, "Error: eSPI 33MHz not supported. Dropping to 16MHz.\n"); |
| 673 | /* Intentional fall-through */ |
| 674 | case ESPI_OP_FREQ_16_MHZ: |
| 675 | /* |
| 676 | * eSPI spec says the minimum frequency is 20MHz, but AMD datasheets support |
| 677 | * 16.7 Mhz. |
| 678 | */ |
| 679 | if (slave_max_speed_mhz > 0) { |
| 680 | *slave_config |= ESPI_SLAVE_OP_FREQ_SEL_20_MHZ; |
| 681 | *ctrlr_config |= ESPI_OP_FREQ_16_MHZ; |
| 682 | break; |
| 683 | } |
| 684 | /* Intentional fall-through */ |
| 685 | default: |
| 686 | die("No supported eSPI Operating Frequency!\n"); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | static int espi_set_general_configuration(const struct espi_config *mb_cfg, uint32_t slave_caps) |
| 691 | { |
| 692 | uint32_t slave_config = 0; |
| 693 | uint32_t ctrlr_config = 0; |
| 694 | |
| 695 | if (mb_cfg->crc_check_enable) { |
| 696 | slave_config |= ESPI_SLAVE_CRC_ENABLE; |
| 697 | ctrlr_config |= ESPI_CRC_CHECKING_EN; |
| 698 | } |
| 699 | |
| 700 | if (mb_cfg->dedicated_alert_pin) { |
| 701 | slave_config |= ESPI_SLAVE_ALERT_MODE_PIN; |
| 702 | ctrlr_config |= ESPI_ALERT_MODE; |
| 703 | } |
| 704 | |
| 705 | espi_set_io_mode_config(mb_cfg->io_mode, slave_caps, &slave_config, &ctrlr_config); |
| 706 | espi_set_op_freq_config(mb_cfg->op_freq_mhz, slave_caps, &slave_config, &ctrlr_config); |
| 707 | |
| 708 | if (CONFIG(ESPI_DEBUG)) |
| 709 | printk(BIOS_INFO, "Setting general configuration: slave: 0x%x controller: 0x%x\n", |
| 710 | slave_config, ctrlr_config); |
| 711 | |
| 712 | if (espi_set_configuration(ESPI_SLAVE_GENERAL_CFG, slave_config) == -1) |
| 713 | return -1; |
| 714 | |
| 715 | espi_write32(ESPI_SLAVE0_CONFIG, ctrlr_config); |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | static int espi_wait_channel_ready(uint16_t slave_reg_addr) |
| 720 | { |
| 721 | struct stopwatch sw; |
| 722 | uint32_t config; |
| 723 | |
| 724 | stopwatch_init_usecs_expire(&sw, ESPI_CH_READY_TIMEOUT_US); |
| 725 | do { |
| 726 | espi_get_configuration(slave_reg_addr, &config); |
| 727 | if (espi_slave_is_channel_ready(config)) |
| 728 | return 0; |
| 729 | } while (!stopwatch_expired(&sw)); |
| 730 | |
| 731 | printk(BIOS_ERR, "Error: Channel is not ready after %d usec (slave addr: 0x%x)\n", |
| 732 | ESPI_CH_READY_TIMEOUT_US, slave_reg_addr); |
| 733 | return -1; |
| 734 | |
| 735 | } |
| 736 | |
| 737 | static void espi_enable_ctrlr_channel(uint32_t channel_en) |
| 738 | { |
| 739 | uint32_t reg = espi_read32(ESPI_SLAVE0_CONFIG); |
| 740 | |
| 741 | reg |= channel_en; |
| 742 | |
| 743 | espi_write32(ESPI_SLAVE0_CONFIG, reg); |
| 744 | } |
| 745 | |
| 746 | static int espi_set_channel_configuration(uint32_t slave_config, uint32_t slave_reg_addr, |
| 747 | uint32_t ctrlr_enable) |
| 748 | { |
| 749 | if (espi_set_configuration(slave_reg_addr, slave_config) == -1) |
| 750 | return -1; |
| 751 | |
| 752 | if (!(slave_config & ESPI_SLAVE_CHANNEL_ENABLE)) |
| 753 | return 0; |
| 754 | |
| 755 | if (espi_wait_channel_ready(slave_reg_addr) == -1) |
| 756 | return -1; |
| 757 | |
| 758 | espi_enable_ctrlr_channel(ctrlr_enable); |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static int espi_setup_vw_channel(const struct espi_config *mb_cfg, uint32_t slave_caps) |
| 763 | { |
| 764 | uint32_t slave_vw_caps; |
| 765 | uint32_t ctrlr_vw_caps; |
| 766 | uint32_t slave_vw_count_supp; |
| 767 | uint32_t ctrlr_vw_count_supp; |
| 768 | uint32_t use_vw_count; |
| 769 | uint32_t slave_config; |
| 770 | |
| 771 | if (!mb_cfg->vw_ch_en) |
| 772 | return 0; |
| 773 | |
| 774 | if (!espi_slave_supports_vw_channel(slave_caps)) { |
| 775 | printk(BIOS_ERR, "Error: eSPI slave doesn't support VW channel!\n"); |
| 776 | return -1; |
| 777 | } |
| 778 | |
| 779 | if (espi_get_configuration(ESPI_SLAVE_VW_CFG, &slave_vw_caps) == -1) |
| 780 | return -1; |
| 781 | |
| 782 | ctrlr_vw_caps = espi_read32(ESPI_MASTER_CAP); |
| 783 | ctrlr_vw_count_supp = (ctrlr_vw_caps & ESPI_VW_MAX_SIZE_MASK) >> ESPI_VW_MAX_SIZE_SHIFT; |
| 784 | |
| 785 | slave_vw_count_supp = espi_slave_get_vw_count_supp(slave_vw_caps); |
| 786 | use_vw_count = MIN(ctrlr_vw_count_supp, slave_vw_count_supp); |
| 787 | |
| 788 | slave_config = ESPI_SLAVE_CHANNEL_ENABLE | ESPI_SLAVE_VW_COUNT_SEL_VAL(use_vw_count); |
| 789 | return espi_set_channel_configuration(slave_config, ESPI_SLAVE_VW_CFG, ESPI_VW_CH_EN); |
| 790 | } |
| 791 | |
| 792 | static int espi_setup_periph_channel(const struct espi_config *mb_cfg, uint32_t slave_caps) |
| 793 | { |
| 794 | uint32_t slave_config; |
| 795 | /* Peripheral channel requires BME bit to be set when enabling the channel. */ |
| 796 | const uint32_t slave_en_mask = ESPI_SLAVE_CHANNEL_READY | |
| 797 | ESPI_SLAVE_PERIPH_BUS_MASTER_ENABLE; |
| 798 | |
| 799 | if (espi_get_configuration(ESPI_SLAVE_PERIPH_CFG, &slave_config) == -1) |
| 800 | return -1; |
| 801 | |
| 802 | /* |
| 803 | * Peripheral channel is the only one which is enabled on reset. So, if the mainboard |
| 804 | * wants to disable it, set configuration to disable peripheral channel. It also |
| 805 | * requires that BME bit be cleared. |
| 806 | */ |
| 807 | if (mb_cfg->periph_ch_en) { |
| 808 | if (!espi_slave_supports_periph_channel(slave_caps)) { |
| 809 | printk(BIOS_ERR, "Error: eSPI slave doesn't support periph channel!\n"); |
| 810 | return -1; |
| 811 | } |
| 812 | slave_config |= slave_en_mask; |
| 813 | } else { |
| 814 | slave_config &= ~slave_en_mask; |
| 815 | } |
| 816 | |
Raul E Rangel | 7222f7e | 2021-04-09 14:15:42 -0600 | [diff] [blame^] | 817 | espi_show_slave_peripheral_channel_configuration(slave_config); |
| 818 | |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 819 | return espi_set_channel_configuration(slave_config, ESPI_SLAVE_PERIPH_CFG, |
| 820 | ESPI_PERIPH_CH_EN); |
| 821 | } |
| 822 | |
| 823 | static int espi_setup_oob_channel(const struct espi_config *mb_cfg, uint32_t slave_caps) |
| 824 | { |
| 825 | uint32_t slave_config; |
| 826 | |
| 827 | if (!mb_cfg->oob_ch_en) |
| 828 | return 0; |
| 829 | |
| 830 | if (!espi_slave_supports_oob_channel(slave_caps)) { |
| 831 | printk(BIOS_ERR, "Error: eSPI slave doesn't support OOB channel!\n"); |
| 832 | return -1; |
| 833 | } |
| 834 | |
| 835 | if (espi_get_configuration(ESPI_SLAVE_OOB_CFG, &slave_config) == -1) |
| 836 | return -1; |
| 837 | |
| 838 | slave_config |= ESPI_SLAVE_CHANNEL_ENABLE; |
| 839 | |
| 840 | return espi_set_channel_configuration(slave_config, ESPI_SLAVE_OOB_CFG, |
| 841 | ESPI_OOB_CH_EN); |
| 842 | } |
| 843 | |
| 844 | static int espi_setup_flash_channel(const struct espi_config *mb_cfg, uint32_t slave_caps) |
| 845 | { |
| 846 | uint32_t slave_config; |
| 847 | |
| 848 | if (!mb_cfg->flash_ch_en) |
| 849 | return 0; |
| 850 | |
| 851 | if (!espi_slave_supports_flash_channel(slave_caps)) { |
| 852 | printk(BIOS_ERR, "Error: eSPI slave doesn't support flash channel!\n"); |
| 853 | return -1; |
| 854 | } |
| 855 | |
| 856 | if (espi_get_configuration(ESPI_SLAVE_FLASH_CFG, &slave_config) == -1) |
| 857 | return -1; |
| 858 | |
| 859 | slave_config |= ESPI_SLAVE_CHANNEL_ENABLE; |
| 860 | |
| 861 | return espi_set_channel_configuration(slave_config, ESPI_SLAVE_FLASH_CFG, |
| 862 | ESPI_FLASH_CH_EN); |
| 863 | } |
| 864 | |
| 865 | static void espi_set_initial_config(const struct espi_config *mb_cfg) |
| 866 | { |
| 867 | uint32_t espi_initial_mode = ESPI_OP_FREQ_16_MHZ | ESPI_IO_MODE_SINGLE; |
| 868 | |
| 869 | if (mb_cfg->dedicated_alert_pin) |
| 870 | espi_initial_mode |= ESPI_ALERT_MODE; |
| 871 | |
| 872 | espi_write32(ESPI_SLAVE0_CONFIG, espi_initial_mode); |
| 873 | } |
| 874 | |
| 875 | static void espi_setup_subtractive_decode(const struct espi_config *mb_cfg) |
| 876 | { |
| 877 | uint32_t global_ctrl_reg; |
| 878 | global_ctrl_reg = espi_read32(ESPI_GLOBAL_CONTROL_1); |
| 879 | |
| 880 | if (mb_cfg->subtractive_decode) { |
| 881 | global_ctrl_reg &= ~ESPI_SUB_DECODE_SLV_MASK; |
| 882 | global_ctrl_reg |= ESPI_SUB_DECODE_EN; |
| 883 | |
| 884 | } else { |
| 885 | global_ctrl_reg &= ~ESPI_SUB_DECODE_EN; |
| 886 | } |
| 887 | espi_write32(ESPI_GLOBAL_CONTROL_1, global_ctrl_reg); |
| 888 | } |
| 889 | |
| 890 | int espi_setup(void) |
| 891 | { |
| 892 | uint32_t slave_caps; |
| 893 | const struct espi_config *cfg = espi_get_config(); |
| 894 | |
Raul E Rangel | b92383a | 2021-04-02 10:32:03 -0600 | [diff] [blame] | 895 | espi_write32(ESPI_GLOBAL_CONTROL_0, ESPI_AL_STOP_EN); |
| 896 | espi_write32(ESPI_GLOBAL_CONTROL_1, ESPI_RGCMD_INT(23) | ESPI_ERR_INT_SMI); |
| 897 | espi_write32(ESPI_SLAVE0_INT_EN, 0); |
| 898 | espi_clear_status(); |
Raul E Rangel | b95f848 | 2021-04-02 13:47:09 -0600 | [diff] [blame] | 899 | espi_clear_decodes(); |
Raul E Rangel | b92383a | 2021-04-02 10:32:03 -0600 | [diff] [blame] | 900 | |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 901 | /* |
| 902 | * Boot sequence: Step 1 |
| 903 | * Set correct initial configuration to talk to the slave: |
| 904 | * Set clock frequency to 16.7MHz and single IO mode. |
| 905 | */ |
| 906 | espi_set_initial_config(cfg); |
| 907 | |
| 908 | /* |
| 909 | * Boot sequence: Step 2 |
| 910 | * Send in-band reset |
| 911 | * The resets affects both host and slave devices, so set initial config again. |
| 912 | */ |
| 913 | if (espi_send_reset() == -1) { |
| 914 | printk(BIOS_ERR, "Error: In-band reset failed!\n"); |
| 915 | return -1; |
| 916 | } |
| 917 | espi_set_initial_config(cfg); |
| 918 | |
| 919 | /* |
| 920 | * Boot sequence: Step 3 |
| 921 | * Get configuration of slave device. |
| 922 | */ |
| 923 | if (espi_get_general_configuration(&slave_caps) == -1) { |
| 924 | printk(BIOS_ERR, "Error: Slave GET_CONFIGURATION failed!\n"); |
| 925 | return -1; |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | * Boot sequence: |
| 930 | * Step 4: Write slave device general config |
| 931 | * Step 5: Set host slave config |
| 932 | */ |
| 933 | if (espi_set_general_configuration(cfg, slave_caps) == -1) { |
| 934 | printk(BIOS_ERR, "Error: Slave SET_CONFIGURATION failed!\n"); |
| 935 | return -1; |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | * Setup polarity before enabling the VW channel so any interrupts |
| 940 | * received will have the correct polarity. |
| 941 | */ |
| 942 | espi_write32(ESPI_RXVW_POLARITY, cfg->vw_irq_polarity); |
| 943 | |
| 944 | /* |
| 945 | * Boot Sequences: Steps 6 - 9 |
| 946 | * Channel setup |
| 947 | */ |
| 948 | /* Set up VW first so we can deassert PLTRST#. */ |
| 949 | if (espi_setup_vw_channel(cfg, slave_caps) == -1) { |
| 950 | printk(BIOS_ERR, "Error: Setup VW channel failed!\n"); |
| 951 | return -1; |
| 952 | } |
| 953 | |
| 954 | /* De-assert PLTRST# if VW channel is enabled by mainboard. */ |
| 955 | if (espi_send_pltrst_deassert(cfg) == -1) { |
| 956 | printk(BIOS_ERR, "Error: PLTRST deassertion failed!\n"); |
| 957 | return -1; |
| 958 | } |
| 959 | |
| 960 | if (espi_setup_periph_channel(cfg, slave_caps) == -1) { |
| 961 | printk(BIOS_ERR, "Error: Setup Periph channel failed!\n"); |
| 962 | return -1; |
| 963 | } |
| 964 | |
| 965 | if (espi_setup_oob_channel(cfg, slave_caps) == -1) { |
| 966 | printk(BIOS_ERR, "Error: Setup OOB channel failed!\n"); |
| 967 | return -1; |
| 968 | } |
| 969 | |
| 970 | if (espi_setup_flash_channel(cfg, slave_caps) == -1) { |
| 971 | printk(BIOS_ERR, "Error: Setup Flash channel failed!\n"); |
| 972 | return -1; |
| 973 | } |
| 974 | |
Raul E Rangel | 61ac1bc | 2021-04-02 10:55:27 -0600 | [diff] [blame] | 975 | if (espi_configure_decodes(cfg) == -1) { |
| 976 | printk(BIOS_ERR, "Error: Configuring decodes failed!\n"); |
| 977 | return -1; |
| 978 | } |
| 979 | |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 980 | /* Enable subtractive decode if configured */ |
Felix Held | a2642d0 | 2021-02-17 00:32:46 +0100 | [diff] [blame] | 981 | espi_setup_subtractive_decode(cfg); |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 982 | |
Raul E Rangel | b92383a | 2021-04-02 10:32:03 -0600 | [diff] [blame] | 983 | espi_write32(ESPI_GLOBAL_CONTROL_1, |
| 984 | espi_read32(ESPI_GLOBAL_CONTROL_1) | ESPI_BUS_MASTER_EN); |
| 985 | |
Furquan Shaikh | 70063ff5 | 2020-05-11 14:28:13 -0700 | [diff] [blame] | 986 | return 0; |
| 987 | } |