blob: 9d820ffd7ef2727d6b8874ffc1777abbc8230a4d [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 <cbmem.h>
18#include <console/uart.h>
Aamir Bohra83f7bae2017-04-26 19:30:41 +053019#include <device/device.h>
20#include <device/pci.h>
Aamir Bohra01d75f42017-03-30 20:12:21 +053021#include <device/pci_def.h>
Aamir Bohra83f7bae2017-04-26 19:30:41 +053022#include <device/pci_ids.h>
Furquan Shaikha8198eb2017-08-04 16:12:19 -070023#include <device/pci_ops.h>
Aamir Bohra01d75f42017-03-30 20:12:21 +053024#include <intelblocks/lpss.h>
25#include <intelblocks/uart.h>
Subrata Banikafa07f72018-05-24 12:21:06 +053026#include <soc/pci_devs.h>
27#include <soc/iomap.h>
28#include <soc/nvs.h>
Aamir Bohra01d75f42017-03-30 20:12:21 +053029
Furquan Shaikha8198eb2017-08-04 16:12:19 -070030#define UART_PCI_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER)
Subrata Banikafa07f72018-05-24 12:21:06 +053031#define UART_CONSOLE_INVALID_INDEX 0xFF
32
33extern const struct uart_gpio_pad_config uart_gpio_pads[];
34extern const int uart_max_index;
Furquan Shaikha8198eb2017-08-04 16:12:19 -070035
Furquan Shaikh3406dd62017-08-04 15:58:26 -070036static void uart_lpss_init(uintptr_t baseaddr)
37{
38 /* Take UART out of reset */
39 lpss_reset_release(baseaddr);
40
41 /* Set M and N divisor inputs and enable clock */
42 lpss_clk_update(baseaddr, CONFIG_SOC_INTEL_COMMON_LPSS_UART_CLK_M_VAL,
43 CONFIG_SOC_INTEL_COMMON_LPSS_UART_CLK_N_VAL);
44}
45
Nico Huber62ddc492019-05-29 18:39:31 +020046#if CONFIG(INTEL_LPSS_UART_FOR_CONSOLE)
Subrata Banikafa07f72018-05-24 12:21:06 +053047uintptr_t uart_platform_base(int idx)
Aamir Bohra01d75f42017-03-30 20:12:21 +053048{
Nico Huberce8eebd2019-05-29 18:33:35 +020049 if (idx == CONFIG_UART_FOR_CONSOLE)
Nico Huber99954182019-05-29 23:33:06 +020050 return CONFIG_CONSOLE_UART_BASE_ADDRESS;
Nico Huberce8eebd2019-05-29 18:33:35 +020051 return 0;
Subrata Banikafa07f72018-05-24 12:21:06 +053052}
53#endif
54
55static int uart_get_valid_index(void)
56{
57 int index;
58
59 for (index = 0; index < uart_max_index; index++) {
60 if (uart_gpio_pads[index].console_index ==
61 CONFIG_UART_FOR_CONSOLE)
62 return index;
63 }
64 /* For valid index, code should not reach here */
65 return UART_CONSOLE_INVALID_INDEX;
66}
67
68void uart_common_init(struct device *device, uintptr_t baseaddr)
69{
70#if defined(__SIMPLE_DEVICE__)
71 pci_devfn_t dev = (pci_devfn_t)(uintptr_t)device;
72#else
73 struct device *dev = device;
74#endif
75 if (!dev)
76 return;
77
Aamir Bohra01d75f42017-03-30 20:12:21 +053078 /* Set UART base address */
79 pci_write_config32(dev, PCI_BASE_ADDRESS_0, baseaddr);
80
81 /* Enable memory access and bus master */
Furquan Shaikha8198eb2017-08-04 16:12:19 -070082 pci_write_config32(dev, PCI_COMMAND, UART_PCI_ENABLE);
Aamir Bohra01d75f42017-03-30 20:12:21 +053083
Furquan Shaikh3406dd62017-08-04 15:58:26 -070084 uart_lpss_init(baseaddr);
Furquan Shaikha8198eb2017-08-04 16:12:19 -070085}
Aamir Bohra01d75f42017-03-30 20:12:21 +053086
Subrata Banikafa07f72018-05-24 12:21:06 +053087struct device *uart_get_device(void)
Furquan Shaikha8198eb2017-08-04 16:12:19 -070088{
89 /*
Nico Hubera96e66a2018-11-11 02:51:14 +010090 * This function will get called even if INTEL_LPSS_UART_FOR_CONSOLE
91 * config option is not selected.
92 * By default return NULL in this case to avoid compilation errors.
Furquan Shaikha8198eb2017-08-04 16:12:19 -070093 */
Julius Wernercd49cce2019-03-05 16:53:33 -080094 if (!CONFIG(INTEL_LPSS_UART_FOR_CONSOLE))
Subrata Banikafa07f72018-05-24 12:21:06 +053095 return NULL;
96
97 int console_index = uart_get_valid_index();
98
99 if (console_index != UART_CONSOLE_INVALID_INDEX)
100 return soc_uart_console_to_device(CONFIG_UART_FOR_CONSOLE);
101 else
102 return NULL;
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700103}
104
Subrata Banikafa07f72018-05-24 12:21:06 +0530105bool uart_is_controller_initialized(void)
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700106{
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700107 uintptr_t base;
108
Subrata Banikafa07f72018-05-24 12:21:06 +0530109#if defined(__SIMPLE_DEVICE__)
110 pci_devfn_t dev = (pci_devfn_t)(uintptr_t)uart_get_device();
111#else
112 struct device *dev = uart_get_device();
113#endif
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700114 if (!dev)
115 return false;
116
117 base = pci_read_config32(dev, PCI_BASE_ADDRESS_0) & ~0xFFF;
118 if (!base)
119 return false;
120
121 if ((pci_read_config32(dev, PCI_COMMAND) & UART_PCI_ENABLE)
122 != UART_PCI_ENABLE)
123 return false;
124
125 return !lpss_is_controller_in_reset(base);
Aamir Bohra01d75f42017-03-30 20:12:21 +0530126}
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530127
Subrata Banikafa07f72018-05-24 12:21:06 +0530128static void uart_configure_gpio_pads(void)
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530129{
Subrata Banikafa07f72018-05-24 12:21:06 +0530130 int index = uart_get_valid_index();
131
132 if (index != UART_CONSOLE_INVALID_INDEX)
133 gpio_configure_pads(uart_gpio_pads[index].gpios,
134 MAX_GPIO_PAD_PER_UART);
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530135}
136
Subrata Banikafa07f72018-05-24 12:21:06 +0530137void uart_bootblock_init(void)
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700138{
Subrata Banikafa07f72018-05-24 12:21:06 +0530139 /* Program UART BAR0, command, reset and clock register */
Nico Huber99954182019-05-29 23:33:06 +0200140 uart_common_init(uart_get_device(), CONFIG_CONSOLE_UART_BASE_ADDRESS);
Subrata Banikafa07f72018-05-24 12:21:06 +0530141
Subrata Banikafa07f72018-05-24 12:21:06 +0530142 /* Configure the 2 pads per UART. */
143 uart_configure_gpio_pads();
144}
145
146#if ENV_RAMSTAGE
147
148static void uart_read_resources(struct device *dev)
149{
150 pci_dev_read_resources(dev);
151
152 /* Set the configured UART base address for the debug port */
Julius Wernercd49cce2019-03-05 16:53:33 -0800153 if (CONFIG(INTEL_LPSS_UART_FOR_CONSOLE) &&
Nico Hubera96e66a2018-11-11 02:51:14 +0100154 uart_is_debug_controller(dev)) {
Subrata Banikafa07f72018-05-24 12:21:06 +0530155 struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0);
156 /* Need to set the base and size for the resource allocator. */
Nico Huber99954182019-05-29 23:33:06 +0200157 res->base = CONFIG_CONSOLE_UART_BASE_ADDRESS;
158 res->size = 0x1000;
Subrata Banikafa07f72018-05-24 12:21:06 +0530159 res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED |
160 IORESOURCE_FIXED;
161 }
162}
163
164/*
165 * Check if UART debug port controller needs to be initialized on resume.
166 *
167 * Returns:
168 * true = when SoC wants debug port initialization on resume
169 * false = otherwise
170 */
171static bool pch_uart_init_debug_controller_on_resume(void)
172{
173 global_nvs_t *gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
174
175 if (gnvs)
176 return !!gnvs->uior;
177
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700178 return false;
179}
180
181bool uart_is_debug_controller(struct device *dev)
182{
Subrata Banikafa07f72018-05-24 12:21:06 +0530183 return dev == uart_get_device();
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700184}
185
186/*
187 * This is a workaround to enable UART controller for the debug port if:
188 * 1. CONSOLE_SERIAL is not enabled in coreboot, and
189 * 2. This boot is S3 resume, and
190 * 3. SoC wants to initialize debug UART controller.
191 *
192 * This workaround is required because Linux kernel hangs on resume if console
193 * is not enabled in coreboot, but it is enabled in kernel and not suspended.
194 */
195static bool uart_controller_needs_init(struct device *dev)
196{
197 /*
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100198 * If coreboot has CONSOLE_SERIAL enabled, the skip re-initializing
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700199 * controller here.
200 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800201 if (CONFIG(CONSOLE_SERIAL))
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700202 return false;
203
204 /* If this device does not correspond to debug port, then skip. */
205 if (!uart_is_debug_controller(dev))
206 return false;
207
208 /* Initialize UART controller only on S3 resume. */
209 if (!acpi_is_wakeup_s3())
210 return false;
211
212 /*
Subrata Banikafa07f72018-05-24 12:21:06 +0530213 * check if SOC wants to initialize UART on resume
Furquan Shaikha8198eb2017-08-04 16:12:19 -0700214 */
215 return pch_uart_init_debug_controller_on_resume();
216}
217
218static void uart_common_enable_resources(struct device *dev)
219{
220 pci_dev_enable_resources(dev);
221
222 if (uart_controller_needs_init(dev)) {
223 uintptr_t base;
224
225 base = pci_read_config32(dev, PCI_BASE_ADDRESS_0) & ~0xFFF;
226 if (base)
227 uart_lpss_init(base);
228 }
229}
230
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530231static struct device_operations device_ops = {
Elyes HAOUAS1d191272018-11-27 12:23:48 +0100232 .read_resources = uart_read_resources,
233 .set_resources = pci_dev_set_resources,
234 .enable_resources = uart_common_enable_resources,
Subrata Banik6bbc91a2017-12-07 14:55:51 +0530235 .ops_pci = &pci_dev_ops_pci,
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530236};
237
238static const unsigned short pci_device_ids[] = {
239 PCI_DEVICE_ID_INTEL_SPT_UART0,
240 PCI_DEVICE_ID_INTEL_SPT_UART1,
241 PCI_DEVICE_ID_INTEL_SPT_UART2,
V Sowmya7c150472018-01-23 14:44:45 +0530242 PCI_DEVICE_ID_INTEL_SPT_H_UART0,
243 PCI_DEVICE_ID_INTEL_SPT_H_UART1,
244 PCI_DEVICE_ID_INTEL_SPT_H_UART2,
V Sowmyaacc2a482018-01-23 15:27:23 +0530245 PCI_DEVICE_ID_INTEL_KBP_H_UART0,
246 PCI_DEVICE_ID_INTEL_KBP_H_UART1,
247 PCI_DEVICE_ID_INTEL_KBP_H_UART2,
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530248 PCI_DEVICE_ID_INTEL_APL_UART0,
249 PCI_DEVICE_ID_INTEL_APL_UART1,
250 PCI_DEVICE_ID_INTEL_APL_UART2,
251 PCI_DEVICE_ID_INTEL_APL_UART3,
Lijian Zhaobbedef92017-07-29 16:38:38 -0700252 PCI_DEVICE_ID_INTEL_CNL_UART0,
253 PCI_DEVICE_ID_INTEL_CNL_UART1,
254 PCI_DEVICE_ID_INTEL_CNL_UART2,
Hannah Williamsf7149652017-05-13 16:18:02 -0700255 PCI_DEVICE_ID_INTEL_GLK_UART0,
256 PCI_DEVICE_ID_INTEL_GLK_UART1,
257 PCI_DEVICE_ID_INTEL_GLK_UART2,
258 PCI_DEVICE_ID_INTEL_GLK_UART3,
praveen hodagatta praneshe26c4a42018-09-20 03:49:45 +0800259 PCI_DEVICE_ID_INTEL_CNP_H_UART0,
260 PCI_DEVICE_ID_INTEL_CNP_H_UART1,
261 PCI_DEVICE_ID_INTEL_CNP_H_UART2,
Aamir Bohra9eac0392018-06-30 12:07:04 +0530262 PCI_DEVICE_ID_INTEL_ICP_UART0,
263 PCI_DEVICE_ID_INTEL_ICP_UART1,
264 PCI_DEVICE_ID_INTEL_ICP_UART2,
Ronak Kanabarda7ffb482019-02-05 01:51:13 +0530265 PCI_DEVICE_ID_INTEL_CMP_UART0,
266 PCI_DEVICE_ID_INTEL_CMP_UART1,
267 PCI_DEVICE_ID_INTEL_CMP_UART2,
Hannah Williamsf7149652017-05-13 16:18:02 -0700268 0,
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530269};
270
271static const struct pci_driver pch_uart __pci_driver = {
Subrata Banikafa07f72018-05-24 12:21:06 +0530272 .ops = &device_ops,
273 .vendor = PCI_VENDOR_ID_INTEL,
Aamir Bohra83f7bae2017-04-26 19:30:41 +0530274 .devices = pci_device_ids,
275};
276#endif /* ENV_RAMSTAGE */