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