Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 4 | * Copyright (C) 2017-2018 Intel Corporation. |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 5 | * |
| 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 Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 15 | |
| 16 | #include <arch/acpi.h> |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 17 | #include <cbmem.h> |
| 18 | #include <console/uart.h> |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 19 | #include <device/device.h> |
| 20 | #include <device/pci.h> |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 21 | #include <device/pci_def.h> |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 22 | #include <device/pci_ids.h> |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 23 | #include <device/pci_ops.h> |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 24 | #include <intelblocks/lpss.h> |
| 25 | #include <intelblocks/uart.h> |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 26 | #include <soc/pci_devs.h> |
| 27 | #include <soc/iomap.h> |
| 28 | #include <soc/nvs.h> |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 29 | |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 30 | #define UART_PCI_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER) |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 31 | #define UART_CONSOLE_INVALID_INDEX 0xFF |
| 32 | |
| 33 | extern const struct uart_gpio_pad_config uart_gpio_pads[]; |
| 34 | extern const int uart_max_index; |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 35 | |
Christian Walter | 931e991 | 2019-07-25 09:07:32 +0000 | [diff] [blame] | 36 | static void uart_lpss_init(uintptr_t baseaddr) |
Furquan Shaikh | 3406dd6 | 2017-08-04 15:58:26 -0700 | [diff] [blame] | 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 Huber | 62ddc49 | 2019-05-29 18:39:31 +0200 | [diff] [blame] | 46 | #if CONFIG(INTEL_LPSS_UART_FOR_CONSOLE) |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 47 | uintptr_t uart_platform_base(int idx) |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 48 | { |
Nico Huber | ce8eebd | 2019-05-29 18:33:35 +0200 | [diff] [blame] | 49 | if (idx == CONFIG_UART_FOR_CONSOLE) |
Nico Huber | 9995418 | 2019-05-29 23:33:06 +0200 | [diff] [blame] | 50 | return CONFIG_CONSOLE_UART_BASE_ADDRESS; |
Nico Huber | ce8eebd | 2019-05-29 18:33:35 +0200 | [diff] [blame] | 51 | return 0; |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 52 | } |
| 53 | #endif |
| 54 | |
| 55 | static 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 | |
Aamir Bohra | 17cfba6 | 2019-07-25 20:56:54 +0530 | [diff] [blame^] | 68 | void uart_common_init(const struct device *device, uintptr_t baseaddr) |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 69 | { |
| 70 | #if defined(__SIMPLE_DEVICE__) |
Aamir Bohra | 17cfba6 | 2019-07-25 20:56:54 +0530 | [diff] [blame^] | 71 | pci_devfn_t dev = PCI_BDF(device); |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 72 | #else |
Aamir Bohra | 17cfba6 | 2019-07-25 20:56:54 +0530 | [diff] [blame^] | 73 | const struct device *dev = device; |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 74 | #endif |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 75 | |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 76 | /* Set UART base address */ |
| 77 | pci_write_config32(dev, PCI_BASE_ADDRESS_0, baseaddr); |
| 78 | |
| 79 | /* Enable memory access and bus master */ |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 80 | pci_write_config32(dev, PCI_COMMAND, UART_PCI_ENABLE); |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 81 | |
Christian Walter | 931e991 | 2019-07-25 09:07:32 +0000 | [diff] [blame] | 82 | uart_lpss_init(baseaddr); |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 83 | } |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 84 | |
Aamir Bohra | 17cfba6 | 2019-07-25 20:56:54 +0530 | [diff] [blame^] | 85 | const struct device *uart_get_device(void) |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 86 | { |
| 87 | /* |
Nico Huber | a96e66a | 2018-11-11 02:51:14 +0100 | [diff] [blame] | 88 | * This function will get called even if INTEL_LPSS_UART_FOR_CONSOLE |
| 89 | * config option is not selected. |
| 90 | * By default return NULL in this case to avoid compilation errors. |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 91 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 92 | if (!CONFIG(INTEL_LPSS_UART_FOR_CONSOLE)) |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 93 | return NULL; |
| 94 | |
| 95 | int console_index = uart_get_valid_index(); |
| 96 | |
| 97 | if (console_index != UART_CONSOLE_INVALID_INDEX) |
| 98 | return soc_uart_console_to_device(CONFIG_UART_FOR_CONSOLE); |
| 99 | else |
| 100 | return NULL; |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 103 | bool uart_is_controller_initialized(void) |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 104 | { |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 105 | uintptr_t base; |
Aamir Bohra | 17cfba6 | 2019-07-25 20:56:54 +0530 | [diff] [blame^] | 106 | const struct device *dev_uart = uart_get_device(); |
| 107 | |
| 108 | if (!dev_uart) |
| 109 | return false; |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 110 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 111 | #if defined(__SIMPLE_DEVICE__) |
Aamir Bohra | 17cfba6 | 2019-07-25 20:56:54 +0530 | [diff] [blame^] | 112 | pci_devfn_t dev = PCI_BDF(dev_uart); |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 113 | #else |
Aamir Bohra | 17cfba6 | 2019-07-25 20:56:54 +0530 | [diff] [blame^] | 114 | const struct device *dev = dev_uart; |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 115 | #endif |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 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 Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 126 | } |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 127 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 128 | static void uart_configure_gpio_pads(void) |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 129 | { |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 130 | 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 Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 135 | } |
| 136 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 137 | void uart_bootblock_init(void) |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 138 | { |
Aamir Bohra | 17cfba6 | 2019-07-25 20:56:54 +0530 | [diff] [blame^] | 139 | const struct device *dev_uart; |
| 140 | |
| 141 | dev_uart = uart_get_device(); |
| 142 | |
| 143 | if (!dev_uart) |
| 144 | return; |
| 145 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 146 | /* Program UART BAR0, command, reset and clock register */ |
Aamir Bohra | 17cfba6 | 2019-07-25 20:56:54 +0530 | [diff] [blame^] | 147 | uart_common_init(dev_uart, CONFIG_CONSOLE_UART_BASE_ADDRESS); |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 148 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 149 | /* Configure the 2 pads per UART. */ |
| 150 | uart_configure_gpio_pads(); |
| 151 | } |
| 152 | |
| 153 | #if ENV_RAMSTAGE |
| 154 | |
| 155 | static void uart_read_resources(struct device *dev) |
| 156 | { |
| 157 | pci_dev_read_resources(dev); |
| 158 | |
| 159 | /* Set the configured UART base address for the debug port */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 160 | if (CONFIG(INTEL_LPSS_UART_FOR_CONSOLE) && |
Nico Huber | a96e66a | 2018-11-11 02:51:14 +0100 | [diff] [blame] | 161 | uart_is_debug_controller(dev)) { |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 162 | struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 163 | /* Need to set the base and size for the resource allocator. */ |
Nico Huber | 9995418 | 2019-05-29 23:33:06 +0200 | [diff] [blame] | 164 | res->base = CONFIG_CONSOLE_UART_BASE_ADDRESS; |
| 165 | res->size = 0x1000; |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 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 | */ |
| 178 | static 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 Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | |
| 188 | bool uart_is_debug_controller(struct device *dev) |
| 189 | { |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 190 | return dev == uart_get_device(); |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 191 | } |
| 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 | */ |
| 202 | static bool uart_controller_needs_init(struct device *dev) |
| 203 | { |
| 204 | /* |
Jonathan Neuschäfer | 5268b76 | 2018-02-12 12:24:25 +0100 | [diff] [blame] | 205 | * If coreboot has CONSOLE_SERIAL enabled, the skip re-initializing |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 206 | * controller here. |
| 207 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 208 | if (CONFIG(CONSOLE_SERIAL)) |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 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 Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 220 | * check if SOC wants to initialize UART on resume |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 221 | */ |
| 222 | return pch_uart_init_debug_controller_on_resume(); |
| 223 | } |
| 224 | |
| 225 | static 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) |
Christian Walter | 931e991 | 2019-07-25 09:07:32 +0000 | [diff] [blame] | 234 | uart_lpss_init(base); |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 238 | static struct device_operations device_ops = { |
Elyes HAOUAS | 1d19127 | 2018-11-27 12:23:48 +0100 | [diff] [blame] | 239 | .read_resources = uart_read_resources, |
| 240 | .set_resources = pci_dev_set_resources, |
| 241 | .enable_resources = uart_common_enable_resources, |
Subrata Banik | 6bbc91a | 2017-12-07 14:55:51 +0530 | [diff] [blame] | 242 | .ops_pci = &pci_dev_ops_pci, |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | static 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 Sowmya | 7c15047 | 2018-01-23 14:44:45 +0530 | [diff] [blame] | 249 | PCI_DEVICE_ID_INTEL_SPT_H_UART0, |
| 250 | PCI_DEVICE_ID_INTEL_SPT_H_UART1, |
| 251 | PCI_DEVICE_ID_INTEL_SPT_H_UART2, |
V Sowmya | acc2a48 | 2018-01-23 15:27:23 +0530 | [diff] [blame] | 252 | PCI_DEVICE_ID_INTEL_KBP_H_UART0, |
| 253 | PCI_DEVICE_ID_INTEL_KBP_H_UART1, |
| 254 | PCI_DEVICE_ID_INTEL_KBP_H_UART2, |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 255 | 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 Zhao | bbedef9 | 2017-07-29 16:38:38 -0700 | [diff] [blame] | 259 | PCI_DEVICE_ID_INTEL_CNL_UART0, |
| 260 | PCI_DEVICE_ID_INTEL_CNL_UART1, |
| 261 | PCI_DEVICE_ID_INTEL_CNL_UART2, |
Hannah Williams | f714965 | 2017-05-13 16:18:02 -0700 | [diff] [blame] | 262 | 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, |
praveen hodagatta pranesh | e26c4a4 | 2018-09-20 03:49:45 +0800 | [diff] [blame] | 266 | PCI_DEVICE_ID_INTEL_CNP_H_UART0, |
| 267 | PCI_DEVICE_ID_INTEL_CNP_H_UART1, |
| 268 | PCI_DEVICE_ID_INTEL_CNP_H_UART2, |
Aamir Bohra | 9eac039 | 2018-06-30 12:07:04 +0530 | [diff] [blame] | 269 | PCI_DEVICE_ID_INTEL_ICP_UART0, |
| 270 | PCI_DEVICE_ID_INTEL_ICP_UART1, |
| 271 | PCI_DEVICE_ID_INTEL_ICP_UART2, |
Ronak Kanabar | da7ffb48 | 2019-02-05 01:51:13 +0530 | [diff] [blame] | 272 | PCI_DEVICE_ID_INTEL_CMP_UART0, |
| 273 | PCI_DEVICE_ID_INTEL_CMP_UART1, |
| 274 | PCI_DEVICE_ID_INTEL_CMP_UART2, |
Hannah Williams | f714965 | 2017-05-13 16:18:02 -0700 | [diff] [blame] | 275 | 0, |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | static const struct pci_driver pch_uart __pci_driver = { |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 279 | .ops = &device_ops, |
| 280 | .vendor = PCI_VENDOR_ID_INTEL, |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 281 | .devices = pci_device_ids, |
| 282 | }; |
| 283 | #endif /* ENV_RAMSTAGE */ |