blob: c67c43532caf77fd3ed2673be0c5ff6ce5c2d267 [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
John Zhao2bb432e2019-05-21 19:32:51 -070083 if (!window_size)
84 return;
85
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070086 /* Window size must be a power of two for the AMASK to work. */
Paul Menzelfa7d2a02017-10-27 15:54:26 +020087 alignment = 1UL << (log2_ceil(window_size));
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070088 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 Zhaoe6db1892018-04-13 16:27:38 -070094 /* 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 Sarawadiefa606b2017-08-04 16:26:09 -0700111 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
122void 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
147bool 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 */
172static void lpc_set_bios_control_reg(uint8_t bios_cntl_bit)
173{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200174 pci_devfn_t dev = PCH_DEV_LPC;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700175 uint8_t bc_cntl;
176
Jonathan Neuschäfer3a182f72017-09-23 17:09:36 +0200177 assert(IS_POWER_OF_2(bios_cntl_bit));
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700178 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*/
191void 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*/
199void 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*/
207void lpc_set_eiss(void)
208{
209 lpc_set_bios_control_reg(LPC_BC_EISS);
210}
211
212/*
213* Set LPC Serial IRQ mode.
214*/
215void lpc_set_serirq_mode(enum serirq_mode mode)
216{
Elyes HAOUAS68c851b2018-06-12 22:06:09 +0200217 pci_devfn_t dev = PCH_DEV_LPC;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700218 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
239void lpc_io_setup_comm_a_b(void)
240{
Subrata Banikd83face2018-03-08 14:04:52 +0530241 /* 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 Wernercd49cce2019-03-05 16:53:33 -0800246 if (CONFIG(SOC_INTEL_COMMON_BLOCK_LPC_COMB_ENABLE)) {
Subrata Banikd83face2018-03-08 14:04:52 +0530247 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 Sarawadiefa606b2017-08-04 16:26:09 -0700253 /* Enable ComA and ComB Port */
Subrata Banikd83face2018-03-08 14:04:52 +0530254 lpc_enable_fixed_io_ranges(com_enable);
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700255}
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700256
257static 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
268static void pch_lpc_interrupt_init(void)
269{
270 const struct device *dev;
271
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300272 dev = pcidev_on_root(PCH_DEV_SLOT_LPC, 0);
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700273 if (!dev || !dev->chip_info)
274 return;
275
276 soc_pch_pirq_init(dev);
277}
278
279void 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älkkic70eed12018-05-22 02:18:00 +0300285 dev = pcidev_on_root(PCH_DEV_SLOT_LPC, 0);
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700286 if (!dev || !dev->chip_info)
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);
292 if (ENV_RAMSTAGE)
293 pch_lpc_interrupt_init();
294}
295
296void lpc_enable_pci_clk_cntl(void)
297{
298 pci_write_config8(PCH_DEV_LPC, LPC_PCCTL, LPC_PCCTL_CLKRUN_EN);
299}
Nico Huberdbcf2932018-11-28 15:29:00 +0100300
301void 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}