Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 2 | /* |
Patrice Chotard | 3bc599c | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 3 | * Copyright (C) 2016, STMicroelectronics - All Rights Reserved |
| 4 | * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics. |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | a3ce8d6 | 2020-11-06 19:01:56 +0100 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_SERIAL |
| 8 | |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 9 | #include <common.h> |
Vikas Manocha | fd03b83 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 10 | #include <clk.h> |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Patrice Chotard | f828fa4 | 2018-12-04 14:11:36 +0100 | [diff] [blame] | 13 | #include <reset.h> |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 14 | #include <serial.h> |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 15 | #include <watchdog.h> |
| 16 | #include <asm/io.h> |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 17 | #include <asm/arch/stm32.h> |
Patrick Delaunay | a3ce8d6 | 2020-11-06 19:01:56 +0100 | [diff] [blame] | 18 | #include <dm/device_compat.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 19 | #include <linux/bitops.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 20 | #include <linux/delay.h> |
Patrice Chotard | b4dbc5d | 2023-05-31 08:01:30 +0200 | [diff] [blame] | 21 | #include <linux/iopoll.h> |
Patrice Chotard | ae74de0 | 2018-01-12 09:23:49 +0100 | [diff] [blame] | 22 | #include "serial_stm32.h" |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 23 | #include <dm/device_compat.h> |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 24 | |
Valentin Caron | 9e8cbea | 2023-08-04 16:09:04 +0200 | [diff] [blame] | 25 | /* |
| 26 | * At 115200 bits/s |
| 27 | * 1 bit = 1 / 115200 = 8,68 us |
| 28 | * 8 bits = 69,444 us |
| 29 | * 10 bits are needed for worst case (8 bits + 1 start + 1 stop) = 86.806 us |
| 30 | */ |
| 31 | #define ONE_BYTE_B115200_US 87 |
| 32 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 33 | static void _stm32_serial_setbrg(fdt_addr_t base, |
| 34 | struct stm32_uart_info *uart_info, |
| 35 | u32 clock_rate, |
| 36 | int baudrate) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 37 | { |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 38 | bool stm32f4 = uart_info->stm32f4; |
Patrice Chotard | 27265ce | 2017-07-18 09:29:08 +0200 | [diff] [blame] | 39 | u32 int_div, mantissa, fraction, oversampling; |
Patrice Chotard | 8ab9e8f | 2023-05-31 08:01:31 +0200 | [diff] [blame] | 40 | u8 uart_enable_bit = uart_info->uart_enable_bit; |
| 41 | |
| 42 | /* BRR register must be set when uart is disabled */ |
| 43 | clrbits_le32(base + CR1_OFFSET(stm32f4), BIT(uart_enable_bit)); |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 44 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 45 | int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate); |
Patrice Chotard | 1afcf9c | 2017-06-08 09:26:55 +0200 | [diff] [blame] | 46 | |
| 47 | if (int_div < 16) { |
| 48 | oversampling = 8; |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 49 | setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8); |
Patrice Chotard | 1afcf9c | 2017-06-08 09:26:55 +0200 | [diff] [blame] | 50 | } else { |
| 51 | oversampling = 16; |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 52 | clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8); |
Patrice Chotard | 1afcf9c | 2017-06-08 09:26:55 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT; |
| 56 | fraction = int_div % oversampling; |
| 57 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 58 | writel(mantissa | fraction, base + BRR_OFFSET(stm32f4)); |
Patrice Chotard | 8ab9e8f | 2023-05-31 08:01:31 +0200 | [diff] [blame] | 59 | |
| 60 | setbits_le32(base + CR1_OFFSET(stm32f4), BIT(uart_enable_bit)); |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static int stm32_serial_setbrg(struct udevice *dev, int baudrate) |
| 64 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 65 | struct stm32x7_serial_plat *plat = dev_get_plat(dev); |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 66 | |
| 67 | _stm32_serial_setbrg(plat->base, plat->uart_info, |
| 68 | plat->clock_rate, baudrate); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Patrice Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 73 | static int stm32_serial_setconfig(struct udevice *dev, uint serial_config) |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 74 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 75 | struct stm32x7_serial_plat *plat = dev_get_plat(dev); |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 76 | bool stm32f4 = plat->uart_info->stm32f4; |
| 77 | u8 uart_enable_bit = plat->uart_info->uart_enable_bit; |
| 78 | u32 cr1 = plat->base + CR1_OFFSET(stm32f4); |
| 79 | u32 config = 0; |
Patrice Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 80 | uint parity = SERIAL_GET_PARITY(serial_config); |
| 81 | uint bits = SERIAL_GET_BITS(serial_config); |
| 82 | uint stop = SERIAL_GET_STOP(serial_config); |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 83 | |
Patrice Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 84 | /* |
| 85 | * only parity config is implemented, check if other serial settings |
| 86 | * are the default one. |
| 87 | * (STM32F4 serial IP didn't support parity setting) |
| 88 | */ |
| 89 | if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || stm32f4) |
| 90 | return -ENOTSUPP; /* not supported in driver*/ |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 91 | |
| 92 | clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit)); |
| 93 | /* update usart configuration (uart need to be disable) |
Patrice Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 94 | * PCE: parity check enable |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 95 | * PS : '0' : Even / '1' : Odd |
| 96 | * M[1:0] = '00' : 8 Data bits |
| 97 | * M[1:0] = '01' : 9 Data bits with parity |
| 98 | */ |
| 99 | switch (parity) { |
| 100 | default: |
| 101 | case SERIAL_PAR_NONE: |
| 102 | config = 0; |
| 103 | break; |
| 104 | case SERIAL_PAR_ODD: |
| 105 | config = USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0; |
| 106 | break; |
| 107 | case SERIAL_PAR_EVEN: |
| 108 | config = USART_CR1_PCE | USART_CR1_M0; |
| 109 | break; |
| 110 | } |
Patrice Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 111 | |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 112 | clrsetbits_le32(cr1, |
| 113 | USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 | |
| 114 | USART_CR1_M0, |
| 115 | config); |
| 116 | setbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit)); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 121 | static int stm32_serial_getc(struct udevice *dev) |
| 122 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 123 | struct stm32x7_serial_plat *plat = dev_get_plat(dev); |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 124 | bool stm32f4 = plat->uart_info->stm32f4; |
| 125 | fdt_addr_t base = plat->base; |
Patrice Chotard | 7b3b74d | 2018-04-20 08:59:06 +0200 | [diff] [blame] | 126 | u32 isr = readl(base + ISR_OFFSET(stm32f4)); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 127 | |
Patrice Chotard | be1a6f7 | 2018-05-17 14:50:43 +0200 | [diff] [blame] | 128 | if ((isr & USART_ISR_RXNE) == 0) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 129 | return -EAGAIN; |
| 130 | |
Patrick Delaunay | 132518f | 2019-07-30 19:16:46 +0200 | [diff] [blame] | 131 | if (isr & (USART_ISR_PE | USART_ISR_ORE | USART_ISR_FE)) { |
Patrice Chotard | 7b3b74d | 2018-04-20 08:59:06 +0200 | [diff] [blame] | 132 | if (!stm32f4) |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 133 | setbits_le32(base + ICR_OFFSET, |
Patrick Delaunay | 132518f | 2019-07-30 19:16:46 +0200 | [diff] [blame] | 134 | USART_ICR_PCECF | USART_ICR_ORECF | |
| 135 | USART_ICR_FECF); |
Patrice Chotard | 7b3b74d | 2018-04-20 08:59:06 +0200 | [diff] [blame] | 136 | else |
| 137 | readl(base + RDR_OFFSET(stm32f4)); |
| 138 | return -EIO; |
| 139 | } |
| 140 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 141 | return readl(base + RDR_OFFSET(stm32f4)); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 144 | static int _stm32_serial_putc(fdt_addr_t base, |
| 145 | struct stm32_uart_info *uart_info, |
| 146 | const char c) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 147 | { |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 148 | bool stm32f4 = uart_info->stm32f4; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 149 | |
Patrice Chotard | be1a6f7 | 2018-05-17 14:50:43 +0200 | [diff] [blame] | 150 | if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 151 | return -EAGAIN; |
| 152 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 153 | writel(c, base + TDR_OFFSET(stm32f4)); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 158 | static int stm32_serial_putc(struct udevice *dev, const char c) |
| 159 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 160 | struct stm32x7_serial_plat *plat = dev_get_plat(dev); |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 161 | |
| 162 | return _stm32_serial_putc(plat->base, plat->uart_info, c); |
| 163 | } |
| 164 | |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 165 | static int stm32_serial_pending(struct udevice *dev, bool input) |
| 166 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 167 | struct stm32x7_serial_plat *plat = dev_get_plat(dev); |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 168 | bool stm32f4 = plat->uart_info->stm32f4; |
| 169 | fdt_addr_t base = plat->base; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 170 | |
| 171 | if (input) |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 172 | return readl(base + ISR_OFFSET(stm32f4)) & |
Patrice Chotard | be1a6f7 | 2018-05-17 14:50:43 +0200 | [diff] [blame] | 173 | USART_ISR_RXNE ? 1 : 0; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 174 | else |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 175 | return readl(base + ISR_OFFSET(stm32f4)) & |
Patrice Chotard | be1a6f7 | 2018-05-17 14:50:43 +0200 | [diff] [blame] | 176 | USART_ISR_TXE ? 0 : 1; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 179 | static void _stm32_serial_init(fdt_addr_t base, |
| 180 | struct stm32_uart_info *uart_info) |
| 181 | { |
| 182 | bool stm32f4 = uart_info->stm32f4; |
| 183 | u8 uart_enable_bit = uart_info->uart_enable_bit; |
| 184 | |
| 185 | /* Disable uart-> enable fifo -> enable uart */ |
| 186 | clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE | |
| 187 | BIT(uart_enable_bit)); |
| 188 | if (uart_info->has_fifo) |
| 189 | setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN); |
| 190 | setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE | |
| 191 | BIT(uart_enable_bit)); |
| 192 | } |
| 193 | |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 194 | static int stm32_serial_probe(struct udevice *dev) |
| 195 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 196 | struct stm32x7_serial_plat *plat = dev_get_plat(dev); |
Patrice Chotard | 9a212d7 | 2017-09-27 15:44:53 +0200 | [diff] [blame] | 197 | struct clk clk; |
Patrice Chotard | f828fa4 | 2018-12-04 14:11:36 +0100 | [diff] [blame] | 198 | struct reset_ctl reset; |
Patrice Chotard | b4dbc5d | 2023-05-31 08:01:30 +0200 | [diff] [blame] | 199 | u32 isr; |
Patrice Chotard | 9a212d7 | 2017-09-27 15:44:53 +0200 | [diff] [blame] | 200 | int ret; |
Patrice Chotard | b4dbc5d | 2023-05-31 08:01:30 +0200 | [diff] [blame] | 201 | bool stm32f4; |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 202 | |
| 203 | plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev); |
Patrice Chotard | b4dbc5d | 2023-05-31 08:01:30 +0200 | [diff] [blame] | 204 | stm32f4 = plat->uart_info->stm32f4; |
Vikas Manocha | fd03b83 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 205 | |
Vikas Manocha | fd03b83 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 206 | ret = clk_get_by_index(dev, 0, &clk); |
| 207 | if (ret < 0) |
| 208 | return ret; |
| 209 | |
| 210 | ret = clk_enable(&clk); |
| 211 | if (ret) { |
| 212 | dev_err(dev, "failed to enable clock\n"); |
| 213 | return ret; |
| 214 | } |
Vikas Manocha | fd03b83 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 215 | |
Patrice Chotard | b4dbc5d | 2023-05-31 08:01:30 +0200 | [diff] [blame] | 216 | /* |
| 217 | * before uart initialization, wait for TC bit (Transmission Complete) |
| 218 | * in case there is still chars from previous bootstage to transmit |
| 219 | */ |
Valentin Caron | 9e8cbea | 2023-08-04 16:09:04 +0200 | [diff] [blame] | 220 | ret = read_poll_timeout(readl, isr, isr & USART_ISR_TC, 50, |
| 221 | 16 * ONE_BYTE_B115200_US, plat->base + ISR_OFFSET(stm32f4)); |
| 222 | if (ret) |
| 223 | dev_dbg(dev, "FIFO not empty, some character can be lost (%d)\n", ret); |
Patrice Chotard | b4dbc5d | 2023-05-31 08:01:30 +0200 | [diff] [blame] | 224 | |
Patrice Chotard | f828fa4 | 2018-12-04 14:11:36 +0100 | [diff] [blame] | 225 | ret = reset_get_by_index(dev, 0, &reset); |
| 226 | if (!ret) { |
| 227 | reset_assert(&reset); |
| 228 | udelay(2); |
| 229 | reset_deassert(&reset); |
| 230 | } |
| 231 | |
Patrice Chotard | 27265ce | 2017-07-18 09:29:08 +0200 | [diff] [blame] | 232 | plat->clock_rate = clk_get_rate(&clk); |
Patrick Delaunay | 585289b | 2019-06-21 15:26:41 +0200 | [diff] [blame] | 233 | if (!plat->clock_rate) { |
Patrice Chotard | 27265ce | 2017-07-18 09:29:08 +0200 | [diff] [blame] | 234 | clk_disable(&clk); |
Patrick Delaunay | 585289b | 2019-06-21 15:26:41 +0200 | [diff] [blame] | 235 | return -EINVAL; |
Patrice Chotard | 27265ce | 2017-07-18 09:29:08 +0200 | [diff] [blame] | 236 | }; |
| 237 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 238 | _stm32_serial_init(plat->base, plat->uart_info); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 243 | static const struct udevice_id stm32_serial_id[] = { |
Patrice Chotard | 6c30f15 | 2017-09-27 15:44:52 +0200 | [diff] [blame] | 244 | { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info}, |
Patrice Chotard | 2a7ecc5 | 2017-09-27 15:44:51 +0200 | [diff] [blame] | 245 | { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info}, |
| 246 | { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info}, |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 247 | {} |
| 248 | }; |
| 249 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 250 | static int stm32_serial_of_to_plat(struct udevice *dev) |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 251 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 252 | struct stm32x7_serial_plat *plat = dev_get_plat(dev); |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 253 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 254 | plat->base = dev_read_addr(dev); |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 255 | if (plat->base == FDT_ADDR_T_NONE) |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 256 | return -EINVAL; |
| 257 | |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 258 | return 0; |
| 259 | } |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 260 | |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 261 | static const struct dm_serial_ops stm32_serial_ops = { |
| 262 | .putc = stm32_serial_putc, |
| 263 | .pending = stm32_serial_pending, |
| 264 | .getc = stm32_serial_getc, |
| 265 | .setbrg = stm32_serial_setbrg, |
Patrice Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 266 | .setconfig = stm32_serial_setconfig |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | U_BOOT_DRIVER(serial_stm32) = { |
Patrice Chotard | ae74de0 | 2018-01-12 09:23:49 +0100 | [diff] [blame] | 270 | .name = "serial_stm32", |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 271 | .id = UCLASS_SERIAL, |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 272 | .of_match = of_match_ptr(stm32_serial_id), |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 273 | .of_to_plat = of_match_ptr(stm32_serial_of_to_plat), |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 274 | .plat_auto = sizeof(struct stm32x7_serial_plat), |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 275 | .ops = &stm32_serial_ops, |
| 276 | .probe = stm32_serial_probe, |
Bin Meng | 4687919 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 277 | #if !CONFIG_IS_ENABLED(OF_CONTROL) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 278 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | 4687919 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 279 | #endif |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 280 | }; |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 281 | |
| 282 | #ifdef CONFIG_DEBUG_UART_STM32 |
| 283 | #include <debug_uart.h> |
| 284 | static inline struct stm32_uart_info *_debug_uart_info(void) |
| 285 | { |
| 286 | struct stm32_uart_info *uart_info; |
| 287 | |
| 288 | #if defined(CONFIG_STM32F4) |
| 289 | uart_info = &stm32f4_info; |
| 290 | #elif defined(CONFIG_STM32F7) |
| 291 | uart_info = &stm32f7_info; |
| 292 | #else |
| 293 | uart_info = &stm32h7_info; |
| 294 | #endif |
| 295 | return uart_info; |
| 296 | } |
| 297 | |
| 298 | static inline void _debug_uart_init(void) |
| 299 | { |
Pali Rohár | b62450c | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 300 | fdt_addr_t base = CONFIG_VAL(DEBUG_UART_BASE); |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 301 | struct stm32_uart_info *uart_info = _debug_uart_info(); |
| 302 | |
| 303 | _stm32_serial_init(base, uart_info); |
| 304 | _stm32_serial_setbrg(base, uart_info, |
| 305 | CONFIG_DEBUG_UART_CLOCK, |
| 306 | CONFIG_BAUDRATE); |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static inline void _debug_uart_putc(int c) |
| 310 | { |
Pali Rohár | b62450c | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 311 | fdt_addr_t base = CONFIG_VAL(DEBUG_UART_BASE); |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 312 | struct stm32_uart_info *uart_info = _debug_uart_info(); |
| 313 | |
| 314 | while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN) |
Patrick Delaunay | 66dba9a | 2019-04-18 17:32:51 +0200 | [diff] [blame] | 315 | ; |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | DEBUG_UART_FUNCS |
| 319 | #endif |