blob: 12b99e737e866800df0e8c2b0de297c58f4ad6ee [file] [log] [blame]
Aamir Bohra01d75f42017-03-30 20:12:21 +05301/*
2 * This file is part of the coreboot project.
3 *
Subrata Banikafa07f72018-05-24 12:21:06 +05304 * Copyright (C) 2017-2018 Intel Corporation.
Aamir Bohra01d75f42017-03-30 20:12:21 +05305 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
Furquan Shaikha8198eb2017-08-04 16:12:19 -070015
16#include <arch/acpi.h>
Subrata Banikafa07f72018-05-24 12:21:06 +053017#include <assert.h>
Aaron Durbin64031672018-04-21 14:45:32 -060018#include <compiler.h>
Subrata Banikafa07f72018-05-24 12:21:06 +053019#include <cbmem.h>
20#include <console/uart.h>
Aamir Bohra83f7bae2017-04-26 19:30:41 +053021#include <device/device.h>
22#include <device/pci.h>
Aamir Bohra01d75f42017-03-30 20:12:21 +053023#include <device/pci_def.h>
Aamir Bohra83f7bae2017-04-26 19:30:41 +053024#include <device/pci_ids.h>
Furquan Shaikha8198eb2017-08-04 16:12:19 -070025#include <device/pci_ops.h>
Aamir Bohra01d75f42017-03-30 20:12:21 +053026#include <intelblocks/lpss.h>
27#include <intelblocks/uart.h>
Subrata Banikafa07f72018-05-24 12:21:06 +053028#include <soc/pci_devs.h>
29#include <soc/iomap.h>
30#include <soc/nvs.h>
31#include <string.h>
Aamir Bohra01d75f42017-03-30 20:12:21 +053032
Furquan Shaikha8198eb2017-08-04 16:12:19 -070033#define UART_PCI_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER)
Subrata Banikafa07f72018-05-24 12:21:06 +053034#define UART_CONSOLE_INVALID_INDEX 0xFF
35
36extern const struct uart_gpio_pad_config uart_gpio_pads[];
37extern const int uart_max_index;
Furquan Shaikha8198eb2017-08-04 16:12:19 -070038
Furquan Shaikh3406dd62017-08-04 15:58:26 -070039static void uart_lpss_init(uintptr_t baseaddr)
40{
41 /* Take UART out of reset */
42 lpss_reset_release(baseaddr);
43
44 /* Set M and N divisor inputs and enable clock */
45 lpss_clk_update(baseaddr, CONFIG_SOC_INTEL_COMMON_LPSS_UART_CLK_M_VAL,
46 CONFIG_SOC_INTEL_COMMON_LPSS_UART_CLK_N_VAL);
47}
48
Subrata Banikafa07f72018-05-24 12:21:06 +053049#if IS_ENABLED(CONFIG_DRIVERS_UART_8250MEM)
50uintptr_t uart_platform_base(int idx)
Aamir Bohra01d75f42017-03-30 20:12:21 +053051{
Subrata Banikafa07f72018-05-24 12:21:06 +053052 /* return Base address for UART console index */
53 return UART_BASE_0_ADDR(idx);
54}
55#endif
56
57static int uart_get_valid_index(void)
58{
59 int index;
60
61 for (index = 0; index < uart_max_index; index++) {
62 if (uart_gpio_pads[index].console_index ==
63 CONFIG_UART_FOR_CONSOLE)
64 return index;
65 }
66 /* For valid index, code should not reach here */
67 return UART_CONSOLE_INVALID_INDEX;
68}
69
70void uart_common_init(struct device *device, uintptr_t baseaddr)
71{
72#if defined(__SIMPLE_DEVICE__)
73 pci_devfn_t dev = (pci_devfn_t)(uintptr_t)device;
74#else
75 struct device *dev = device;
76#endif
77 if (!dev)
78 return;
79
Aamir Bohra01d75f42017-03-30 20:12:21 +053080 /* Set UART base address */
81 pci_write_config32(dev, PCI_BASE_ADDRESS_0, baseaddr);
82
83 /* Enable memory access and bus master */
Furquan Shaikha8198eb2017-08-04 16:12:19 -070084 pci_write_config32(dev, PCI_COMMAND, UART_PCI_ENABLE);
Aamir Bohra01d75f42017-03-30 20:12:21 +053085
Furquan Shaikh3406dd62017-08-04 15:58:26 -070086 uart_lpss_init(baseaddr);
Furquan Shaikha8198eb2017-08-04 16:12:19 -070087}
Aamir Bohra01d75f42017-03-30 20:12:21 +053088
Subrata Banikafa07f72018-05-24 12:21:06 +053089struct device *uart_get_device(void)
Furquan Shaikha8198eb2017-08-04 16:12:19 -070090{
91 /*
Subrata Banikafa07f72018-05-24 12:21:06 +053092 * This function will get called even if UART_DEBUG config options is
93 * not selected.
94 * By default returning NULL in case CONFIG_UART_DEBUG option is not
95 * selected to avoid compilation errors.
Furquan Shaikha8198eb2017-08-04 16:12:19 -070096 */
Subrata Banikafa07f72018-05-24 12:21:06 +053097 if (!IS_ENABLED(CONFIG_UART_DEBUG))
98 return NULL;
99
100 int console_index = uart_get_valid_index();
101
102 if (console_index != UART_CONSOLE_INVALID_INDEX)
103 return soc_uart_console_to_device(CONFIG_UART_FOR_CONSOLE);
104 else
105 return NULL;
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700106}
107
Subrata Banikafa07f72018-05-24 12:21:06 +0530108bool uart_is_controller_initialized(void)
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700109{
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700110 uintptr_t base;
111
Subrata Banikafa07f72018-05-24 12:21:06 +0530112#if defined(__SIMPLE_DEVICE__)
113 pci_devfn_t dev = (pci_devfn_t)(uintptr_t)uart_get_device();
114#else
115 struct device *dev = uart_get_device();
116#endif
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700117 if (!dev)
118 return false;
119
120 base = pci_read_config32(dev, PCI_BASE_ADDRESS_0) & ~0xFFF;
121 if (!base)
122 return false;
123
124 if ((pci_read_config32(dev, PCI_COMMAND) & UART_PCI_ENABLE)
125 != UART_PCI_ENABLE)
126 return false;
127
128 return !lpss_is_controller_in_reset(base);
Aamir Bohra01d75f42017-03-30 20:12:21 +0530129}
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530130
Subrata Banikafa07f72018-05-24 12:21:06 +0530131static void uart_configure_gpio_pads(void)
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530132{
Subrata Banikafa07f72018-05-24 12:21:06 +0530133 int index = uart_get_valid_index();
134
135 if (index != UART_CONSOLE_INVALID_INDEX)
136 gpio_configure_pads(uart_gpio_pads[index].gpios,
137 MAX_GPIO_PAD_PER_UART);
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530138}
139
Subrata Banikafa07f72018-05-24 12:21:06 +0530140void uart_bootblock_init(void)
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700141{
Subrata Banikafa07f72018-05-24 12:21:06 +0530142 /* Program UART BAR0, command, reset and clock register */
143 uart_common_init(uart_get_device(),
144 UART_BASE(CONFIG_UART_FOR_CONSOLE));
145
146 if (!IS_ENABLED(CONFIG_DRIVERS_UART_8250MEM_32))
147 /* Put UART in byte access mode for 16550 compatibility */
148 soc_uart_set_legacy_mode();
149
150 /* Configure the 2 pads per UART. */
151 uart_configure_gpio_pads();
152}
153
154#if ENV_RAMSTAGE
155
156static void uart_read_resources(struct device *dev)
157{
158 pci_dev_read_resources(dev);
159
160 /* Set the configured UART base address for the debug port */
161 if (IS_ENABLED(CONFIG_UART_DEBUG) && uart_is_debug_controller(dev)) {
162 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
163 /* Need to set the base and size for the resource allocator. */
164 res->base = UART_BASE(CONFIG_UART_FOR_CONSOLE);
165 res->size = UART_BASE_SIZE;
166 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED |
167 IORESOURCE_FIXED;
168 }
169}
170
171/*
172 * Check if UART debug port controller needs to be initialized on resume.
173 *
174 * Returns:
175 * true = when SoC wants debug port initialization on resume
176 * false = otherwise
177 */
178static bool pch_uart_init_debug_controller_on_resume(void)
179{
180 global_nvs_t *gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
181
182 if (gnvs)
183 return !!gnvs->uior;
184
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700185 return false;
186}
187
188bool uart_is_debug_controller(struct device *dev)
189{
Subrata Banikafa07f72018-05-24 12:21:06 +0530190 return dev == uart_get_device();
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700191}
192
193/*
194 * This is a workaround to enable UART controller for the debug port if:
195 * 1. CONSOLE_SERIAL is not enabled in coreboot, and
196 * 2. This boot is S3 resume, and
197 * 3. SoC wants to initialize debug UART controller.
198 *
199 * This workaround is required because Linux kernel hangs on resume if console
200 * is not enabled in coreboot, but it is enabled in kernel and not suspended.
201 */
202static bool uart_controller_needs_init(struct device *dev)
203{
204 /*
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100205 * If coreboot has CONSOLE_SERIAL enabled, the skip re-initializing
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700206 * controller here.
207 */
208 if (IS_ENABLED(CONFIG_CONSOLE_SERIAL))
209 return false;
210
211 /* If this device does not correspond to debug port, then skip. */
212 if (!uart_is_debug_controller(dev))
213 return false;
214
215 /* Initialize UART controller only on S3 resume. */
216 if (!acpi_is_wakeup_s3())
217 return false;
218
219 /*
Subrata Banikafa07f72018-05-24 12:21:06 +0530220 * check if SOC wants to initialize UART on resume
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700221 */
222 return pch_uart_init_debug_controller_on_resume();
223}
224
225static void uart_common_enable_resources(struct device *dev)
226{
227 pci_dev_enable_resources(dev);
228
229 if (uart_controller_needs_init(dev)) {
230 uintptr_t base;
231
232 base = pci_read_config32(dev, PCI_BASE_ADDRESS_0) & ~0xFFF;
233 if (base)
234 uart_lpss_init(base);
235 }
236}
237
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530238static struct device_operations device_ops = {
Subrata Banikafa07f72018-05-24 12:21:06 +0530239 .read_resources = &uart_read_resources,
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530240 .set_resources = &pci_dev_set_resources,
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700241 .enable_resources = &uart_common_enable_resources,
Subrata Banik6bbc91a2017-12-07 14:55:51 +0530242 .ops_pci = &pci_dev_ops_pci,
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530243};
244
245static const unsigned short pci_device_ids[] = {
246 PCI_DEVICE_ID_INTEL_SPT_UART0,
247 PCI_DEVICE_ID_INTEL_SPT_UART1,
248 PCI_DEVICE_ID_INTEL_SPT_UART2,
V Sowmya7c150472018-01-23 14:44:45 +0530249 PCI_DEVICE_ID_INTEL_SPT_H_UART0,
250 PCI_DEVICE_ID_INTEL_SPT_H_UART1,
251 PCI_DEVICE_ID_INTEL_SPT_H_UART2,
V Sowmyaacc2a482018-01-23 15:27:23 +0530252 PCI_DEVICE_ID_INTEL_KBP_H_UART0,
253 PCI_DEVICE_ID_INTEL_KBP_H_UART1,
254 PCI_DEVICE_ID_INTEL_KBP_H_UART2,
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530255 PCI_DEVICE_ID_INTEL_APL_UART0,
256 PCI_DEVICE_ID_INTEL_APL_UART1,
257 PCI_DEVICE_ID_INTEL_APL_UART2,
258 PCI_DEVICE_ID_INTEL_APL_UART3,
Lijian Zhaobbedef92017-07-29 16:38:38 -0700259 PCI_DEVICE_ID_INTEL_CNL_UART0,
260 PCI_DEVICE_ID_INTEL_CNL_UART1,
261 PCI_DEVICE_ID_INTEL_CNL_UART2,
Hannah Williamsf7149652017-05-13 16:18:02 -0700262 PCI_DEVICE_ID_INTEL_GLK_UART0,
263 PCI_DEVICE_ID_INTEL_GLK_UART1,
264 PCI_DEVICE_ID_INTEL_GLK_UART2,
265 PCI_DEVICE_ID_INTEL_GLK_UART3,
266 0,
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530267};
268
269static const struct pci_driver pch_uart __pci_driver = {
Subrata Banikafa07f72018-05-24 12:21:06 +0530270 .ops = &device_ops,
271 .vendor = PCI_VENDOR_ID_INTEL,
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530272 .devices = pci_device_ids,
273};
274#endif /* ENV_RAMSTAGE */