Patrick Georgi | 02363b5 | 2020-05-05 20:48:50 +0200 | [diff] [blame^] | 1 | /* This file is part of the coreboot project. */ |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 2 | /* |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #define __SIMPLE_DEVICE__ |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <console/console.h> |
| 19 | #include <device/pci.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 20 | #include <device/pci_ops.h> |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 21 | #include <intelblocks/lpc_lib.h> |
| 22 | #include <lib.h> |
| 23 | #include "lpc_def.h" |
| 24 | #include <soc/pci_devs.h> |
| 25 | |
Subrata Banik | d83face | 2018-03-08 14:04:52 +0530 | [diff] [blame] | 26 | uint16_t lpc_enable_fixed_io_ranges(uint16_t io_enables) |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 27 | { |
| 28 | uint16_t reg_io_enables; |
| 29 | |
| 30 | reg_io_enables = pci_read_config16(PCH_DEV_LPC, LPC_IO_ENABLES); |
| 31 | io_enables |= reg_io_enables; |
| 32 | pci_write_config16(PCH_DEV_LPC, LPC_IO_ENABLES, io_enables); |
Subrata Banik | d83face | 2018-03-08 14:04:52 +0530 | [diff] [blame] | 33 | |
| 34 | return io_enables; |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Wim Vervoorn | e6db910 | 2020-02-03 14:57:40 +0100 | [diff] [blame] | 37 | uint16_t lpc_get_fixed_io_decode(void) |
| 38 | { |
| 39 | return pci_read_config16(PCH_DEV_LPC, LPC_IO_DECODE); |
| 40 | } |
| 41 | |
Wim Vervoorn | 5f2adfe | 2020-02-03 15:32:54 +0100 | [diff] [blame] | 42 | uint16_t lpc_set_fixed_io_ranges(uint16_t io_ranges, uint16_t mask) |
| 43 | { |
| 44 | uint16_t reg_io_ranges; |
| 45 | |
| 46 | reg_io_ranges = lpc_get_fixed_io_decode() & ~mask; |
| 47 | io_ranges |= reg_io_ranges & mask; |
| 48 | pci_write_config16(PCH_DEV_LPC, LPC_IO_DECODE, io_ranges); |
| 49 | |
| 50 | return io_ranges; |
| 51 | } |
| 52 | |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 53 | /* |
| 54 | * Find the first unused IO window. |
| 55 | * Returns -1 if not found, 0 for reg 0x84, 1 for reg 0x88 ... |
| 56 | */ |
| 57 | static int find_unused_pmio_window(void) |
| 58 | { |
| 59 | int i; |
| 60 | uint32_t lgir; |
| 61 | |
| 62 | for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) { |
| 63 | lgir = pci_read_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i)); |
| 64 | |
| 65 | if (!(lgir & LPC_LGIR_EN)) |
| 66 | return i; |
| 67 | } |
| 68 | |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | void lpc_close_pmio_windows(void) |
| 73 | { |
| 74 | size_t i; |
| 75 | |
| 76 | for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) |
| 77 | pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i), 0); |
| 78 | } |
| 79 | |
| 80 | void lpc_open_pmio_window(uint16_t base, uint16_t size) |
| 81 | { |
Lijian Zhao | e6db189 | 2018-04-13 16:27:38 -0700 | [diff] [blame] | 82 | int i, lgir_reg_num; |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 83 | uint32_t lgir_reg_offset, lgir, window_size, alignment; |
| 84 | resource_t bridged_size, bridge_base; |
| 85 | |
| 86 | printk(BIOS_SPEW, "LPC: Trying to open IO window from %x size %x\n", |
| 87 | base, size); |
| 88 | |
| 89 | bridged_size = 0; |
| 90 | bridge_base = base; |
| 91 | |
| 92 | while (bridged_size < size) { |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 93 | /* Each IO range register can only open a 256-byte window. */ |
| 94 | window_size = MIN(size, LPC_LGIR_MAX_WINDOW_SIZE); |
| 95 | |
John Zhao | 1ceac4e | 2019-07-09 14:27:28 -0700 | [diff] [blame] | 96 | if (window_size <= 0) |
John Zhao | 2bb432e | 2019-05-21 19:32:51 -0700 | [diff] [blame] | 97 | return; |
| 98 | |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 99 | /* Window size must be a power of two for the AMASK to work. */ |
Paul Menzel | fa7d2a0 | 2017-10-27 15:54:26 +0200 | [diff] [blame] | 100 | alignment = 1UL << (log2_ceil(window_size)); |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 101 | window_size = ALIGN_UP(window_size, alignment); |
| 102 | |
| 103 | /* Address[15:2] in LGIR[15:12] and Mask[7:2] in LGIR[23:18]. */ |
| 104 | lgir = (bridge_base & LPC_LGIR_ADDR_MASK) | LPC_LGIR_EN; |
| 105 | lgir |= ((window_size - 1) << 16) & LPC_LGIR_AMASK_MASK; |
| 106 | |
Lijian Zhao | e6db189 | 2018-04-13 16:27:38 -0700 | [diff] [blame] | 107 | /* Skip programming if same range already programmed. */ |
| 108 | for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) { |
| 109 | if (lgir == pci_read_config32(PCH_DEV_LPC, |
| 110 | LPC_GENERIC_IO_RANGE(i))) |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | lgir_reg_num = find_unused_pmio_window(); |
| 115 | if (lgir_reg_num < 0) { |
| 116 | printk(BIOS_ERR, |
| 117 | "LPC: Cannot open IO window: %llx size %llx\n", |
| 118 | bridge_base, size - bridged_size); |
| 119 | printk(BIOS_ERR, "No more IO windows\n"); |
| 120 | return; |
| 121 | } |
| 122 | lgir_reg_offset = LPC_GENERIC_IO_RANGE(lgir_reg_num); |
| 123 | |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 124 | pci_write_config32(PCH_DEV_LPC, lgir_reg_offset, lgir); |
| 125 | |
| 126 | printk(BIOS_DEBUG, |
| 127 | "LPC: Opened IO window LGIR%d: base %llx size %x\n", |
| 128 | lgir_reg_num, bridge_base, window_size); |
| 129 | |
| 130 | bridged_size += window_size; |
| 131 | bridge_base += window_size; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void lpc_open_mmio_window(uintptr_t base, size_t size) |
| 136 | { |
| 137 | uint32_t lgmr; |
| 138 | |
| 139 | lgmr = pci_read_config32(PCH_DEV_LPC, LPC_GENERIC_MEM_RANGE); |
| 140 | |
| 141 | if (lgmr & LPC_LGMR_EN) { |
| 142 | printk(BIOS_ERR, |
| 143 | "LPC: Cannot open window to resource %lx size %zx\n", |
| 144 | base, size); |
| 145 | printk(BIOS_ERR, "LPC: MMIO window already in use\n"); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | if (size > LPC_LGMR_WINDOW_SIZE) { |
| 150 | printk(BIOS_WARNING, |
| 151 | "LPC: Resource %lx size %zx larger than window(%x)\n", |
| 152 | base, size, LPC_LGMR_WINDOW_SIZE); |
| 153 | } |
| 154 | |
| 155 | lgmr = (base & LPC_LGMR_ADDR_MASK) | LPC_LGMR_EN; |
| 156 | |
| 157 | pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_MEM_RANGE, lgmr); |
| 158 | } |
| 159 | |
| 160 | bool lpc_fits_fixed_mmio_window(uintptr_t base, size_t size) |
| 161 | { |
| 162 | resource_t res_end, range_end; |
| 163 | const struct lpc_mmio_range *range; |
| 164 | const struct lpc_mmio_range *lpc_fixed_mmio_ranges = |
| 165 | soc_get_fixed_mmio_ranges(); |
| 166 | |
| 167 | for (range = lpc_fixed_mmio_ranges; range->size; range++) { |
| 168 | range_end = range->base + range->size; |
| 169 | res_end = base + size; |
| 170 | |
| 171 | if ((base >= range->base) && (res_end <= range_end)) { |
| 172 | printk(BIOS_DEBUG, |
| 173 | "Resource %lx size %zx fits in fixed window" |
| 174 | " %lx size %zx\n", |
| 175 | base, size, range->base, range->size); |
| 176 | return true; |
| 177 | } |
| 178 | } |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Set FAST_SPIBAR BIOS Control register based on input bit field. |
| 184 | */ |
| 185 | static void lpc_set_bios_control_reg(uint8_t bios_cntl_bit) |
| 186 | { |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 187 | pci_devfn_t dev = PCH_DEV_LPC; |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 188 | uint8_t bc_cntl; |
| 189 | |
Jonathan Neuschäfer | 3a182f7 | 2017-09-23 17:09:36 +0200 | [diff] [blame] | 190 | assert(IS_POWER_OF_2(bios_cntl_bit)); |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 191 | bc_cntl = pci_read_config8(dev, LPC_BIOS_CNTL); |
| 192 | bc_cntl |= bios_cntl_bit; |
| 193 | pci_write_config8(dev, LPC_BIOS_CNTL, bc_cntl); |
| 194 | |
| 195 | /* |
| 196 | * Ensure an additional read back after performing lock down |
| 197 | */ |
| 198 | pci_read_config8(PCH_DEV_LPC, LPC_BIOS_CNTL); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Set LPC BIOS Control BILD bit. |
| 203 | */ |
| 204 | void lpc_set_bios_interface_lock_down(void) |
| 205 | { |
| 206 | lpc_set_bios_control_reg(LPC_BC_BILD); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Set LPC BIOS Control LE bit. |
| 211 | */ |
| 212 | void lpc_set_lock_enable(void) |
| 213 | { |
| 214 | lpc_set_bios_control_reg(LPC_BC_LE); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Set LPC BIOS Control EISS bit. |
| 219 | */ |
| 220 | void lpc_set_eiss(void) |
| 221 | { |
| 222 | lpc_set_bios_control_reg(LPC_BC_EISS); |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Set LPC Serial IRQ mode. |
| 227 | */ |
| 228 | void lpc_set_serirq_mode(enum serirq_mode mode) |
| 229 | { |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 230 | pci_devfn_t dev = PCH_DEV_LPC; |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 231 | uint8_t scnt; |
| 232 | |
| 233 | scnt = pci_read_config8(dev, LPC_SERIRQ_CTL); |
| 234 | scnt &= ~(LPC_SCNT_EN | LPC_SCNT_MODE); |
| 235 | |
| 236 | switch (mode) { |
| 237 | case SERIRQ_QUIET: |
| 238 | scnt |= LPC_SCNT_EN; |
| 239 | break; |
| 240 | case SERIRQ_CONTINUOUS: |
| 241 | scnt |= LPC_SCNT_EN | LPC_SCNT_MODE; |
| 242 | break; |
| 243 | case SERIRQ_OFF: |
| 244 | default: |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | pci_write_config8(dev, LPC_SERIRQ_CTL, scnt); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | void lpc_io_setup_comm_a_b(void) |
| 253 | { |
Subrata Banik | d83face | 2018-03-08 14:04:52 +0530 | [diff] [blame] | 254 | /* ComA Range 3F8h-3FFh [2:0] */ |
| 255 | uint16_t com_ranges = LPC_IOD_COMA_RANGE; |
| 256 | uint16_t com_enable = LPC_IOE_COMA_EN; |
| 257 | |
| 258 | /* ComB Range 2F8h-2FFh [6:4] */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 259 | if (CONFIG(SOC_INTEL_COMMON_BLOCK_LPC_COMB_ENABLE)) { |
Subrata Banik | d83face | 2018-03-08 14:04:52 +0530 | [diff] [blame] | 260 | com_ranges |= LPC_IOD_COMB_RANGE; |
| 261 | com_enable |= LPC_IOE_COMB_EN; |
| 262 | } |
| 263 | |
| 264 | /* Setup I/O Decode Range Register for LPC */ |
| 265 | pci_write_config16(PCH_DEV_LPC, LPC_IO_DECODE, com_ranges); |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 266 | /* Enable ComA and ComB Port */ |
Subrata Banik | d83face | 2018-03-08 14:04:52 +0530 | [diff] [blame] | 267 | lpc_enable_fixed_io_ranges(com_enable); |
Ravi Sarawadi | efa606b | 2017-08-04 16:26:09 -0700 | [diff] [blame] | 268 | } |
Ravi Sarawadi | a9b5a39 | 2017-09-20 13:46:19 -0700 | [diff] [blame] | 269 | |
| 270 | static void lpc_set_gen_decode_range( |
| 271 | uint32_t gen_io_dec[LPC_NUM_GENERIC_IO_RANGES]) |
| 272 | { |
| 273 | size_t i; |
| 274 | |
| 275 | /* Set in PCI generic decode range registers */ |
| 276 | for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) |
| 277 | pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i), |
| 278 | gen_io_dec[i]); |
| 279 | } |
| 280 | |
Ravi Sarawadi | a9b5a39 | 2017-09-20 13:46:19 -0700 | [diff] [blame] | 281 | void pch_enable_lpc(void) |
| 282 | { |
| 283 | /* Lookup device tree in romstage */ |
| 284 | const struct device *dev; |
| 285 | uint32_t gen_io_dec[LPC_NUM_GENERIC_IO_RANGES]; |
| 286 | |
Kyösti Mälkki | c70eed1 | 2018-05-22 02:18:00 +0300 | [diff] [blame] | 287 | dev = pcidev_on_root(PCH_DEV_SLOT_LPC, 0); |
Kyösti Mälkki | 8950cfb | 2019-07-13 22:16:25 +0300 | [diff] [blame] | 288 | if (!dev) |
Ravi Sarawadi | a9b5a39 | 2017-09-20 13:46:19 -0700 | [diff] [blame] | 289 | return; |
| 290 | |
| 291 | soc_get_gen_io_dec_range(dev, gen_io_dec); |
| 292 | lpc_set_gen_decode_range(gen_io_dec); |
| 293 | soc_setup_dmi_pcr_io_dec(gen_io_dec); |
Subrata Banik | 42c44c2 | 2019-05-15 20:27:04 +0530 | [diff] [blame] | 294 | if (ENV_PAYLOAD_LOADER) |
Subrata Banik | 0d866f8 | 2020-02-18 11:20:30 +0530 | [diff] [blame] | 295 | soc_pch_pirq_init(dev); |
Ravi Sarawadi | a9b5a39 | 2017-09-20 13:46:19 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void lpc_enable_pci_clk_cntl(void) |
| 299 | { |
| 300 | pci_write_config8(PCH_DEV_LPC, LPC_PCCTL, LPC_PCCTL_CLKRUN_EN); |
| 301 | } |
Nico Huber | dbcf293 | 2018-11-28 15:29:00 +0100 | [diff] [blame] | 302 | |
| 303 | void lpc_disable_clkrun(void) |
| 304 | { |
| 305 | const uint8_t pcctl = pci_read_config8(PCH_DEV_LPC, LPC_PCCTL); |
| 306 | pci_write_config8(PCH_DEV_LPC, LPC_PCCTL, pcctl & ~LPC_PCCTL_CLKRUN_EN); |
| 307 | } |