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