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 <assert.h> |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 18 | #include <cbmem.h> |
| 19 | #include <console/uart.h> |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 20 | #include <device/device.h> |
| 21 | #include <device/pci.h> |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 22 | #include <device/pci_def.h> |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 23 | #include <device/pci_ids.h> |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 24 | #include <device/pci_ops.h> |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 25 | #include <intelblocks/lpss.h> |
| 26 | #include <intelblocks/uart.h> |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 27 | #include <soc/pci_devs.h> |
| 28 | #include <soc/iomap.h> |
| 29 | #include <soc/nvs.h> |
| 30 | #include <string.h> |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 31 | |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 32 | #define UART_PCI_ENABLE (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER) |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 33 | #define UART_CONSOLE_INVALID_INDEX 0xFF |
| 34 | |
| 35 | extern const struct uart_gpio_pad_config uart_gpio_pads[]; |
| 36 | extern const int uart_max_index; |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 37 | |
Furquan Shaikh | 3406dd6 | 2017-08-04 15:58:26 -0700 | [diff] [blame] | 38 | static void uart_lpss_init(uintptr_t baseaddr) |
| 39 | { |
| 40 | /* Take UART out of reset */ |
| 41 | lpss_reset_release(baseaddr); |
| 42 | |
| 43 | /* Set M and N divisor inputs and enable clock */ |
| 44 | lpss_clk_update(baseaddr, CONFIG_SOC_INTEL_COMMON_LPSS_UART_CLK_M_VAL, |
| 45 | CONFIG_SOC_INTEL_COMMON_LPSS_UART_CLK_N_VAL); |
| 46 | } |
| 47 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 48 | #if IS_ENABLED(CONFIG_DRIVERS_UART_8250MEM) |
| 49 | uintptr_t uart_platform_base(int idx) |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 50 | { |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 51 | /* return Base address for UART console index */ |
| 52 | return UART_BASE_0_ADDR(idx); |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | static int uart_get_valid_index(void) |
| 57 | { |
| 58 | int index; |
| 59 | |
| 60 | for (index = 0; index < uart_max_index; index++) { |
| 61 | if (uart_gpio_pads[index].console_index == |
| 62 | CONFIG_UART_FOR_CONSOLE) |
| 63 | return index; |
| 64 | } |
| 65 | /* For valid index, code should not reach here */ |
| 66 | return UART_CONSOLE_INVALID_INDEX; |
| 67 | } |
| 68 | |
| 69 | void uart_common_init(struct device *device, uintptr_t baseaddr) |
| 70 | { |
| 71 | #if defined(__SIMPLE_DEVICE__) |
| 72 | pci_devfn_t dev = (pci_devfn_t)(uintptr_t)device; |
| 73 | #else |
| 74 | struct device *dev = device; |
| 75 | #endif |
| 76 | if (!dev) |
| 77 | return; |
| 78 | |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 79 | /* Set UART base address */ |
| 80 | pci_write_config32(dev, PCI_BASE_ADDRESS_0, baseaddr); |
| 81 | |
| 82 | /* Enable memory access and bus master */ |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 83 | pci_write_config32(dev, PCI_COMMAND, UART_PCI_ENABLE); |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 84 | |
Furquan Shaikh | 3406dd6 | 2017-08-04 15:58:26 -0700 | [diff] [blame] | 85 | uart_lpss_init(baseaddr); |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 86 | } |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 87 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 88 | struct device *uart_get_device(void) |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 89 | { |
| 90 | /* |
Nico Huber | a96e66a | 2018-11-11 02:51:14 +0100 | [diff] [blame] | 91 | * This function will get called even if INTEL_LPSS_UART_FOR_CONSOLE |
| 92 | * config option is not selected. |
| 93 | * By default return NULL in this case to avoid compilation errors. |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 94 | */ |
Nico Huber | a96e66a | 2018-11-11 02:51:14 +0100 | [diff] [blame] | 95 | if (!IS_ENABLED(CONFIG_INTEL_LPSS_UART_FOR_CONSOLE)) |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 96 | return NULL; |
| 97 | |
| 98 | int console_index = uart_get_valid_index(); |
| 99 | |
| 100 | if (console_index != UART_CONSOLE_INVALID_INDEX) |
| 101 | return soc_uart_console_to_device(CONFIG_UART_FOR_CONSOLE); |
| 102 | else |
| 103 | return NULL; |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 106 | bool uart_is_controller_initialized(void) |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 107 | { |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 108 | uintptr_t base; |
| 109 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 110 | #if defined(__SIMPLE_DEVICE__) |
| 111 | pci_devfn_t dev = (pci_devfn_t)(uintptr_t)uart_get_device(); |
| 112 | #else |
| 113 | struct device *dev = uart_get_device(); |
| 114 | #endif |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 115 | if (!dev) |
| 116 | return false; |
| 117 | |
| 118 | base = pci_read_config32(dev, PCI_BASE_ADDRESS_0) & ~0xFFF; |
| 119 | if (!base) |
| 120 | return false; |
| 121 | |
| 122 | if ((pci_read_config32(dev, PCI_COMMAND) & UART_PCI_ENABLE) |
| 123 | != UART_PCI_ENABLE) |
| 124 | return false; |
| 125 | |
| 126 | return !lpss_is_controller_in_reset(base); |
Aamir Bohra | 01d75f4 | 2017-03-30 20:12:21 +0530 | [diff] [blame] | 127 | } |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 128 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 129 | static void uart_configure_gpio_pads(void) |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 130 | { |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 131 | int index = uart_get_valid_index(); |
| 132 | |
| 133 | if (index != UART_CONSOLE_INVALID_INDEX) |
| 134 | gpio_configure_pads(uart_gpio_pads[index].gpios, |
| 135 | MAX_GPIO_PAD_PER_UART); |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 136 | } |
| 137 | |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 138 | void uart_bootblock_init(void) |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 139 | { |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 140 | /* Program UART BAR0, command, reset and clock register */ |
| 141 | uart_common_init(uart_get_device(), |
| 142 | UART_BASE(CONFIG_UART_FOR_CONSOLE)); |
| 143 | |
| 144 | if (!IS_ENABLED(CONFIG_DRIVERS_UART_8250MEM_32)) |
| 145 | /* Put UART in byte access mode for 16550 compatibility */ |
| 146 | soc_uart_set_legacy_mode(); |
| 147 | |
| 148 | /* Configure the 2 pads per UART. */ |
| 149 | uart_configure_gpio_pads(); |
| 150 | } |
| 151 | |
| 152 | #if ENV_RAMSTAGE |
| 153 | |
| 154 | static void uart_read_resources(struct device *dev) |
| 155 | { |
| 156 | pci_dev_read_resources(dev); |
| 157 | |
| 158 | /* Set the configured UART base address for the debug port */ |
Nico Huber | a96e66a | 2018-11-11 02:51:14 +0100 | [diff] [blame] | 159 | if (IS_ENABLED(CONFIG_INTEL_LPSS_UART_FOR_CONSOLE) && |
| 160 | uart_is_debug_controller(dev)) { |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 161 | struct resource *res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 162 | /* Need to set the base and size for the resource allocator. */ |
| 163 | res->base = UART_BASE(CONFIG_UART_FOR_CONSOLE); |
| 164 | res->size = UART_BASE_SIZE; |
| 165 | res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | |
| 166 | IORESOURCE_FIXED; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Check if UART debug port controller needs to be initialized on resume. |
| 172 | * |
| 173 | * Returns: |
| 174 | * true = when SoC wants debug port initialization on resume |
| 175 | * false = otherwise |
| 176 | */ |
| 177 | static bool pch_uart_init_debug_controller_on_resume(void) |
| 178 | { |
| 179 | global_nvs_t *gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS); |
| 180 | |
| 181 | if (gnvs) |
| 182 | return !!gnvs->uior; |
| 183 | |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | |
| 187 | bool uart_is_debug_controller(struct device *dev) |
| 188 | { |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 189 | return dev == uart_get_device(); |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
| 193 | * This is a workaround to enable UART controller for the debug port if: |
| 194 | * 1. CONSOLE_SERIAL is not enabled in coreboot, and |
| 195 | * 2. This boot is S3 resume, and |
| 196 | * 3. SoC wants to initialize debug UART controller. |
| 197 | * |
| 198 | * This workaround is required because Linux kernel hangs on resume if console |
| 199 | * is not enabled in coreboot, but it is enabled in kernel and not suspended. |
| 200 | */ |
| 201 | static bool uart_controller_needs_init(struct device *dev) |
| 202 | { |
| 203 | /* |
Jonathan Neuschäfer | 5268b76 | 2018-02-12 12:24:25 +0100 | [diff] [blame] | 204 | * If coreboot has CONSOLE_SERIAL enabled, the skip re-initializing |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 205 | * controller here. |
| 206 | */ |
| 207 | if (IS_ENABLED(CONFIG_CONSOLE_SERIAL)) |
| 208 | return false; |
| 209 | |
| 210 | /* If this device does not correspond to debug port, then skip. */ |
| 211 | if (!uart_is_debug_controller(dev)) |
| 212 | return false; |
| 213 | |
| 214 | /* Initialize UART controller only on S3 resume. */ |
| 215 | if (!acpi_is_wakeup_s3()) |
| 216 | return false; |
| 217 | |
| 218 | /* |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 219 | * check if SOC wants to initialize UART on resume |
Furquan Shaikh | a8198eb | 2017-08-04 16:12:19 -0700 | [diff] [blame] | 220 | */ |
| 221 | return pch_uart_init_debug_controller_on_resume(); |
| 222 | } |
| 223 | |
| 224 | static void uart_common_enable_resources(struct device *dev) |
| 225 | { |
| 226 | pci_dev_enable_resources(dev); |
| 227 | |
| 228 | if (uart_controller_needs_init(dev)) { |
| 229 | uintptr_t base; |
| 230 | |
| 231 | base = pci_read_config32(dev, PCI_BASE_ADDRESS_0) & ~0xFFF; |
| 232 | if (base) |
| 233 | uart_lpss_init(base); |
| 234 | } |
| 235 | } |
| 236 | |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 237 | static struct device_operations device_ops = { |
Elyes HAOUAS | 1d19127 | 2018-11-27 12:23:48 +0100 | [diff] [blame^] | 238 | .read_resources = uart_read_resources, |
| 239 | .set_resources = pci_dev_set_resources, |
| 240 | .enable_resources = uart_common_enable_resources, |
Subrata Banik | 6bbc91a | 2017-12-07 14:55:51 +0530 | [diff] [blame] | 241 | .ops_pci = &pci_dev_ops_pci, |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | static const unsigned short pci_device_ids[] = { |
| 245 | PCI_DEVICE_ID_INTEL_SPT_UART0, |
| 246 | PCI_DEVICE_ID_INTEL_SPT_UART1, |
| 247 | PCI_DEVICE_ID_INTEL_SPT_UART2, |
V Sowmya | 7c15047 | 2018-01-23 14:44:45 +0530 | [diff] [blame] | 248 | PCI_DEVICE_ID_INTEL_SPT_H_UART0, |
| 249 | PCI_DEVICE_ID_INTEL_SPT_H_UART1, |
| 250 | PCI_DEVICE_ID_INTEL_SPT_H_UART2, |
V Sowmya | acc2a48 | 2018-01-23 15:27:23 +0530 | [diff] [blame] | 251 | PCI_DEVICE_ID_INTEL_KBP_H_UART0, |
| 252 | PCI_DEVICE_ID_INTEL_KBP_H_UART1, |
| 253 | PCI_DEVICE_ID_INTEL_KBP_H_UART2, |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 254 | PCI_DEVICE_ID_INTEL_APL_UART0, |
| 255 | PCI_DEVICE_ID_INTEL_APL_UART1, |
| 256 | PCI_DEVICE_ID_INTEL_APL_UART2, |
| 257 | PCI_DEVICE_ID_INTEL_APL_UART3, |
Lijian Zhao | bbedef9 | 2017-07-29 16:38:38 -0700 | [diff] [blame] | 258 | PCI_DEVICE_ID_INTEL_CNL_UART0, |
| 259 | PCI_DEVICE_ID_INTEL_CNL_UART1, |
| 260 | PCI_DEVICE_ID_INTEL_CNL_UART2, |
Hannah Williams | f714965 | 2017-05-13 16:18:02 -0700 | [diff] [blame] | 261 | PCI_DEVICE_ID_INTEL_GLK_UART0, |
| 262 | PCI_DEVICE_ID_INTEL_GLK_UART1, |
| 263 | PCI_DEVICE_ID_INTEL_GLK_UART2, |
| 264 | PCI_DEVICE_ID_INTEL_GLK_UART3, |
praveen hodagatta pranesh | e26c4a4 | 2018-09-20 03:49:45 +0800 | [diff] [blame] | 265 | PCI_DEVICE_ID_INTEL_CNP_H_UART0, |
| 266 | PCI_DEVICE_ID_INTEL_CNP_H_UART1, |
| 267 | PCI_DEVICE_ID_INTEL_CNP_H_UART2, |
Aamir Bohra | 9eac039 | 2018-06-30 12:07:04 +0530 | [diff] [blame] | 268 | PCI_DEVICE_ID_INTEL_ICP_UART0, |
| 269 | PCI_DEVICE_ID_INTEL_ICP_UART1, |
| 270 | PCI_DEVICE_ID_INTEL_ICP_UART2, |
Hannah Williams | f714965 | 2017-05-13 16:18:02 -0700 | [diff] [blame] | 271 | 0, |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | static const struct pci_driver pch_uart __pci_driver = { |
Subrata Banik | afa07f7 | 2018-05-24 12:21:06 +0530 | [diff] [blame] | 275 | .ops = &device_ops, |
| 276 | .vendor = PCI_VENDOR_ID_INTEL, |
Aamir Bohra | 83f7bae | 2017-04-26 19:30:41 +0530 | [diff] [blame] | 277 | .devices = pci_device_ids, |
| 278 | }; |
| 279 | #endif /* ENV_RAMSTAGE */ |