blob: b3836377363bf8cab95370f913b972968df10df9 [file] [log] [blame]
Ravi Sarawadiefa606b2017-08-04 16:26:09 -07001/*
2 * This file is part of the coreboot project.
3 *
Subrata Banikd83face2018-03-08 14:04:52 +05304 * Copyright (C) 2016-2018 Intel Corp.
Ravi Sarawadiefa606b2017-08-04 16:26:09 -07005 * (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älkkif1b58b72019-03-01 13:43:02 +020023#include <device/pci_ops.h>
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070024#include <intelblocks/lpc_lib.h>
25#include <lib.h>
26#include "lpc_def.h"
27#include <soc/pci_devs.h>
28
Subrata Banikd83face2018-03-08 14:04:52 +053029uint16_t lpc_enable_fixed_io_ranges(uint16_t io_enables)
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070030{
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 Banikd83face2018-03-08 14:04:52 +053036
37 return io_enables;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070038}
39
40/*
41 * Find the first unused IO window.
42 * Returns -1 if not found, 0 for reg 0x84, 1 for reg 0x88 ...
43 */
44static 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
59void 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
67void lpc_open_pmio_window(uint16_t base, uint16_t size)
68{
Lijian Zhaoe6db1892018-04-13 16:27:38 -070069 int i, lgir_reg_num;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070070 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 Sarawadiefa606b2017-08-04 16:26:09 -070080 /* Each IO range register can only open a 256-byte window. */
81 window_size = MIN(size, LPC_LGIR_MAX_WINDOW_SIZE);
82
83 /* Window size must be a power of two for the AMASK to work. */
Paul Menzelfa7d2a02017-10-27 15:54:26 +020084 alignment = 1UL << (log2_ceil(window_size));
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070085 window_size = ALIGN_UP(window_size, alignment);
86
87 /* Address[15:2] in LGIR[15:12] and Mask[7:2] in LGIR[23:18]. */
88 lgir = (bridge_base & LPC_LGIR_ADDR_MASK) | LPC_LGIR_EN;
89 lgir |= ((window_size - 1) << 16) & LPC_LGIR_AMASK_MASK;
90
Lijian Zhaoe6db1892018-04-13 16:27:38 -070091 /* Skip programming if same range already programmed. */
92 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) {
93 if (lgir == pci_read_config32(PCH_DEV_LPC,
94 LPC_GENERIC_IO_RANGE(i)))
95 return;
96 }
97
98 lgir_reg_num = find_unused_pmio_window();
99 if (lgir_reg_num < 0) {
100 printk(BIOS_ERR,
101 "LPC: Cannot open IO window: %llx size %llx\n",
102 bridge_base, size - bridged_size);
103 printk(BIOS_ERR, "No more IO windows\n");
104 return;
105 }
106 lgir_reg_offset = LPC_GENERIC_IO_RANGE(lgir_reg_num);
107
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700108 pci_write_config32(PCH_DEV_LPC, lgir_reg_offset, lgir);
109
110 printk(BIOS_DEBUG,
111 "LPC: Opened IO window LGIR%d: base %llx size %x\n",
112 lgir_reg_num, bridge_base, window_size);
113
114 bridged_size += window_size;
115 bridge_base += window_size;
116 }
117}
118
119void lpc_open_mmio_window(uintptr_t base, size_t size)
120{
121 uint32_t lgmr;
122
123 lgmr = pci_read_config32(PCH_DEV_LPC, LPC_GENERIC_MEM_RANGE);
124
125 if (lgmr & LPC_LGMR_EN) {
126 printk(BIOS_ERR,
127 "LPC: Cannot open window to resource %lx size %zx\n",
128 base, size);
129 printk(BIOS_ERR, "LPC: MMIO window already in use\n");
130 return;
131 }
132
133 if (size > LPC_LGMR_WINDOW_SIZE) {
134 printk(BIOS_WARNING,
135 "LPC: Resource %lx size %zx larger than window(%x)\n",
136 base, size, LPC_LGMR_WINDOW_SIZE);
137 }
138
139 lgmr = (base & LPC_LGMR_ADDR_MASK) | LPC_LGMR_EN;
140
141 pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_MEM_RANGE, lgmr);
142}
143
144bool lpc_fits_fixed_mmio_window(uintptr_t base, size_t size)
145{
146 resource_t res_end, range_end;
147 const struct lpc_mmio_range *range;
148 const struct lpc_mmio_range *lpc_fixed_mmio_ranges =
149 soc_get_fixed_mmio_ranges();
150
151 for (range = lpc_fixed_mmio_ranges; range->size; range++) {
152 range_end = range->base + range->size;
153 res_end = base + size;
154
155 if ((base >= range->base) && (res_end <= range_end)) {
156 printk(BIOS_DEBUG,
157 "Resource %lx size %zx fits in fixed window"
158 " %lx size %zx\n",
159 base, size, range->base, range->size);
160 return true;
161 }
162 }
163 return false;
164}
165
166/*
167 * Set FAST_SPIBAR BIOS Control register based on input bit field.
168 */
169static void lpc_set_bios_control_reg(uint8_t bios_cntl_bit)
170{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200171 pci_devfn_t dev = PCH_DEV_LPC;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700172 uint8_t bc_cntl;
173
Jonathan Neuschäfer3a182f72017-09-23 17:09:36 +0200174 assert(IS_POWER_OF_2(bios_cntl_bit));
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700175 bc_cntl = pci_read_config8(dev, LPC_BIOS_CNTL);
176 bc_cntl |= bios_cntl_bit;
177 pci_write_config8(dev, LPC_BIOS_CNTL, bc_cntl);
178
179 /*
180 * Ensure an additional read back after performing lock down
181 */
182 pci_read_config8(PCH_DEV_LPC, LPC_BIOS_CNTL);
183}
184
185/*
186* Set LPC BIOS Control BILD bit.
187*/
188void lpc_set_bios_interface_lock_down(void)
189{
190 lpc_set_bios_control_reg(LPC_BC_BILD);
191}
192
193/*
194* Set LPC BIOS Control LE bit.
195*/
196void lpc_set_lock_enable(void)
197{
198 lpc_set_bios_control_reg(LPC_BC_LE);
199}
200
201/*
202* Set LPC BIOS Control EISS bit.
203*/
204void lpc_set_eiss(void)
205{
206 lpc_set_bios_control_reg(LPC_BC_EISS);
207}
208
209/*
210* Set LPC Serial IRQ mode.
211*/
212void lpc_set_serirq_mode(enum serirq_mode mode)
213{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200214 pci_devfn_t dev = PCH_DEV_LPC;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700215 uint8_t scnt;
216
217 scnt = pci_read_config8(dev, LPC_SERIRQ_CTL);
218 scnt &= ~(LPC_SCNT_EN | LPC_SCNT_MODE);
219
220 switch (mode) {
221 case SERIRQ_QUIET:
222 scnt |= LPC_SCNT_EN;
223 break;
224 case SERIRQ_CONTINUOUS:
225 scnt |= LPC_SCNT_EN | LPC_SCNT_MODE;
226 break;
227 case SERIRQ_OFF:
228 default:
229 break;
230 }
231
232 pci_write_config8(dev, LPC_SERIRQ_CTL, scnt);
233}
234
235
236void lpc_io_setup_comm_a_b(void)
237{
Subrata Banikd83face2018-03-08 14:04:52 +0530238 /* ComA Range 3F8h-3FFh [2:0] */
239 uint16_t com_ranges = LPC_IOD_COMA_RANGE;
240 uint16_t com_enable = LPC_IOE_COMA_EN;
241
242 /* ComB Range 2F8h-2FFh [6:4] */
Julius Wernercd49cce2019-03-05 16:53:33 -0800243 if (CONFIG(SOC_INTEL_COMMON_BLOCK_LPC_COMB_ENABLE)) {
Subrata Banikd83face2018-03-08 14:04:52 +0530244 com_ranges |= LPC_IOD_COMB_RANGE;
245 com_enable |= LPC_IOE_COMB_EN;
246 }
247
248 /* Setup I/O Decode Range Register for LPC */
249 pci_write_config16(PCH_DEV_LPC, LPC_IO_DECODE, com_ranges);
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700250 /* Enable ComA and ComB Port */
Subrata Banikd83face2018-03-08 14:04:52 +0530251 lpc_enable_fixed_io_ranges(com_enable);
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700252}
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700253
254static void lpc_set_gen_decode_range(
255 uint32_t gen_io_dec[LPC_NUM_GENERIC_IO_RANGES])
256{
257 size_t i;
258
259 /* Set in PCI generic decode range registers */
260 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++)
261 pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i),
262 gen_io_dec[i]);
263}
264
265static void pch_lpc_interrupt_init(void)
266{
267 const struct device *dev;
268
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300269 dev = pcidev_on_root(PCH_DEV_SLOT_LPC, 0);
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700270 if (!dev || !dev->chip_info)
271 return;
272
273 soc_pch_pirq_init(dev);
274}
275
276void pch_enable_lpc(void)
277{
278 /* Lookup device tree in romstage */
279 const struct device *dev;
280 uint32_t gen_io_dec[LPC_NUM_GENERIC_IO_RANGES];
281
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300282 dev = pcidev_on_root(PCH_DEV_SLOT_LPC, 0);
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700283 if (!dev || !dev->chip_info)
284 return;
285
286 soc_get_gen_io_dec_range(dev, gen_io_dec);
287 lpc_set_gen_decode_range(gen_io_dec);
288 soc_setup_dmi_pcr_io_dec(gen_io_dec);
289 if (ENV_RAMSTAGE)
290 pch_lpc_interrupt_init();
291}
292
293void lpc_enable_pci_clk_cntl(void)
294{
295 pci_write_config8(PCH_DEV_LPC, LPC_PCCTL, LPC_PCCTL_CLKRUN_EN);
296}
Nico Huberdbcf2932018-11-28 15:29:00 +0100297
298void lpc_disable_clkrun(void)
299{
300 const uint8_t pcctl = pci_read_config8(PCH_DEV_LPC, LPC_PCCTL);
301 pci_write_config8(PCH_DEV_LPC, LPC_PCCTL, pcctl & ~LPC_PCCTL_CLKRUN_EN);
302}