blob: aeac441c23020f5754e1f818bf9db9d0ac229de7 [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>
23#include <intelblocks/lpc_lib.h>
24#include <lib.h>
25#include "lpc_def.h"
26#include <soc/pci_devs.h>
27
Subrata Banikd83face2018-03-08 14:04:52 +053028uint16_t lpc_enable_fixed_io_ranges(uint16_t io_enables)
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070029{
30 uint16_t reg_io_enables;
31
32 reg_io_enables = pci_read_config16(PCH_DEV_LPC, LPC_IO_ENABLES);
33 io_enables |= reg_io_enables;
34 pci_write_config16(PCH_DEV_LPC, LPC_IO_ENABLES, io_enables);
Subrata Banikd83face2018-03-08 14:04:52 +053035
36 return io_enables;
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070037}
38
39/*
40 * Find the first unused IO window.
41 * Returns -1 if not found, 0 for reg 0x84, 1 for reg 0x88 ...
42 */
43static int find_unused_pmio_window(void)
44{
45 int i;
46 uint32_t lgir;
47
48 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) {
49 lgir = pci_read_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i));
50
51 if (!(lgir & LPC_LGIR_EN))
52 return i;
53 }
54
55 return -1;
56}
57
58void lpc_close_pmio_windows(void)
59{
60 size_t i;
61
62 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++)
63 pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i), 0);
64}
65
66void lpc_open_pmio_window(uint16_t base, uint16_t size)
67{
68 int lgir_reg_num;
69 uint32_t lgir_reg_offset, lgir, window_size, alignment;
70 resource_t bridged_size, bridge_base;
71
72 printk(BIOS_SPEW, "LPC: Trying to open IO window from %x size %x\n",
73 base, size);
74
75 bridged_size = 0;
76 bridge_base = base;
77
78 while (bridged_size < size) {
79 lgir_reg_num = find_unused_pmio_window();
80 if (lgir_reg_num < 0) {
81 printk(BIOS_ERR,
82 "LPC: Cannot open IO window: %llx size %llx\n",
83 bridge_base, size - bridged_size);
84 printk(BIOS_ERR, "No more IO windows\n");
85 return;
86 }
87 lgir_reg_offset = LPC_GENERIC_IO_RANGE(lgir_reg_num);
88
89 /* Each IO range register can only open a 256-byte window. */
90 window_size = MIN(size, LPC_LGIR_MAX_WINDOW_SIZE);
91
92 /* Window size must be a power of two for the AMASK to work. */
Paul Menzelfa7d2a02017-10-27 15:54:26 +020093 alignment = 1UL << (log2_ceil(window_size));
Ravi Sarawadiefa606b2017-08-04 16:26:09 -070094 window_size = ALIGN_UP(window_size, alignment);
95
96 /* Address[15:2] in LGIR[15:12] and Mask[7:2] in LGIR[23:18]. */
97 lgir = (bridge_base & LPC_LGIR_ADDR_MASK) | LPC_LGIR_EN;
98 lgir |= ((window_size - 1) << 16) & LPC_LGIR_AMASK_MASK;
99
100 pci_write_config32(PCH_DEV_LPC, lgir_reg_offset, lgir);
101
102 printk(BIOS_DEBUG,
103 "LPC: Opened IO window LGIR%d: base %llx size %x\n",
104 lgir_reg_num, bridge_base, window_size);
105
106 bridged_size += window_size;
107 bridge_base += window_size;
108 }
109}
110
111void lpc_open_mmio_window(uintptr_t base, size_t size)
112{
113 uint32_t lgmr;
114
115 lgmr = pci_read_config32(PCH_DEV_LPC, LPC_GENERIC_MEM_RANGE);
116
117 if (lgmr & LPC_LGMR_EN) {
118 printk(BIOS_ERR,
119 "LPC: Cannot open window to resource %lx size %zx\n",
120 base, size);
121 printk(BIOS_ERR, "LPC: MMIO window already in use\n");
122 return;
123 }
124
125 if (size > LPC_LGMR_WINDOW_SIZE) {
126 printk(BIOS_WARNING,
127 "LPC: Resource %lx size %zx larger than window(%x)\n",
128 base, size, LPC_LGMR_WINDOW_SIZE);
129 }
130
131 lgmr = (base & LPC_LGMR_ADDR_MASK) | LPC_LGMR_EN;
132
133 pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_MEM_RANGE, lgmr);
134}
135
136bool lpc_fits_fixed_mmio_window(uintptr_t base, size_t size)
137{
138 resource_t res_end, range_end;
139 const struct lpc_mmio_range *range;
140 const struct lpc_mmio_range *lpc_fixed_mmio_ranges =
141 soc_get_fixed_mmio_ranges();
142
143 for (range = lpc_fixed_mmio_ranges; range->size; range++) {
144 range_end = range->base + range->size;
145 res_end = base + size;
146
147 if ((base >= range->base) && (res_end <= range_end)) {
148 printk(BIOS_DEBUG,
149 "Resource %lx size %zx fits in fixed window"
150 " %lx size %zx\n",
151 base, size, range->base, range->size);
152 return true;
153 }
154 }
155 return false;
156}
157
158/*
159 * Set FAST_SPIBAR BIOS Control register based on input bit field.
160 */
161static void lpc_set_bios_control_reg(uint8_t bios_cntl_bit)
162{
163 device_t dev = PCH_DEV_LPC;
164 uint8_t bc_cntl;
165
Jonathan Neuschäfer3a182f72017-09-23 17:09:36 +0200166 assert(IS_POWER_OF_2(bios_cntl_bit));
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700167 bc_cntl = pci_read_config8(dev, LPC_BIOS_CNTL);
168 bc_cntl |= bios_cntl_bit;
169 pci_write_config8(dev, LPC_BIOS_CNTL, bc_cntl);
170
171 /*
172 * Ensure an additional read back after performing lock down
173 */
174 pci_read_config8(PCH_DEV_LPC, LPC_BIOS_CNTL);
175}
176
177/*
178* Set LPC BIOS Control BILD bit.
179*/
180void lpc_set_bios_interface_lock_down(void)
181{
182 lpc_set_bios_control_reg(LPC_BC_BILD);
183}
184
185/*
186* Set LPC BIOS Control LE bit.
187*/
188void lpc_set_lock_enable(void)
189{
190 lpc_set_bios_control_reg(LPC_BC_LE);
191}
192
193/*
194* Set LPC BIOS Control EISS bit.
195*/
196void lpc_set_eiss(void)
197{
198 lpc_set_bios_control_reg(LPC_BC_EISS);
199}
200
201/*
202* Set LPC Serial IRQ mode.
203*/
204void lpc_set_serirq_mode(enum serirq_mode mode)
205{
206 device_t dev = PCH_DEV_LPC;
207 uint8_t scnt;
208
209 scnt = pci_read_config8(dev, LPC_SERIRQ_CTL);
210 scnt &= ~(LPC_SCNT_EN | LPC_SCNT_MODE);
211
212 switch (mode) {
213 case SERIRQ_QUIET:
214 scnt |= LPC_SCNT_EN;
215 break;
216 case SERIRQ_CONTINUOUS:
217 scnt |= LPC_SCNT_EN | LPC_SCNT_MODE;
218 break;
219 case SERIRQ_OFF:
220 default:
221 break;
222 }
223
224 pci_write_config8(dev, LPC_SERIRQ_CTL, scnt);
225}
226
227
228void lpc_io_setup_comm_a_b(void)
229{
Subrata Banikd83face2018-03-08 14:04:52 +0530230 /* ComA Range 3F8h-3FFh [2:0] */
231 uint16_t com_ranges = LPC_IOD_COMA_RANGE;
232 uint16_t com_enable = LPC_IOE_COMA_EN;
233
234 /* ComB Range 2F8h-2FFh [6:4] */
235 if (IS_ENABLED(CONFIG_SOC_INTEL_COMMON_BLOCK_LPC_COMB_ENABLE)) {
236 com_ranges |= LPC_IOD_COMB_RANGE;
237 com_enable |= LPC_IOE_COMB_EN;
238 }
239
240 /* Setup I/O Decode Range Register for LPC */
241 pci_write_config16(PCH_DEV_LPC, LPC_IO_DECODE, com_ranges);
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700242 /* Enable ComA and ComB Port */
Subrata Banikd83face2018-03-08 14:04:52 +0530243 lpc_enable_fixed_io_ranges(com_enable);
Ravi Sarawadiefa606b2017-08-04 16:26:09 -0700244}
Ravi Sarawadia9b5a392017-09-20 13:46:19 -0700245
246static void lpc_set_gen_decode_range(
247 uint32_t gen_io_dec[LPC_NUM_GENERIC_IO_RANGES])
248{
249 size_t i;
250
251 /* Set in PCI generic decode range registers */
252 for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++)
253 pci_write_config32(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i),
254 gen_io_dec[i]);
255}
256
257static void pch_lpc_interrupt_init(void)
258{
259 const struct device *dev;
260
261 dev = dev_find_slot(0, PCI_DEVFN(PCH_DEV_SLOT_LPC, 0));
262 if (!dev || !dev->chip_info)
263 return;
264
265 soc_pch_pirq_init(dev);
266}
267
268void pch_enable_lpc(void)
269{
270 /* Lookup device tree in romstage */
271 const struct device *dev;
272 uint32_t gen_io_dec[LPC_NUM_GENERIC_IO_RANGES];
273
274 dev = dev_find_slot(0, PCI_DEVFN(PCH_DEV_SLOT_LPC, 0));
275 if (!dev || !dev->chip_info)
276 return;
277
278 soc_get_gen_io_dec_range(dev, gen_io_dec);
279 lpc_set_gen_decode_range(gen_io_dec);
280 soc_setup_dmi_pcr_io_dec(gen_io_dec);
281 if (ENV_RAMSTAGE)
282 pch_lpc_interrupt_init();
283}
284
285void lpc_enable_pci_clk_cntl(void)
286{
287 pci_write_config8(PCH_DEV_LPC, LPC_PCCTL, LPC_PCCTL_CLKRUN_EN);
288}