1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
4 * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
14 #include <asm/arch/stm32.h>
15 #include "serial_stm32.h"
16 #include <dm/device_compat.h>
18 static void _stm32_serial_setbrg(fdt_addr_t base,
19 struct stm32_uart_info *uart_info,
23 bool stm32f4 = uart_info->stm32f4;
24 u32 int_div, mantissa, fraction, oversampling;
26 int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate);
30 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
33 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
36 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
37 fraction = int_div % oversampling;
39 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
42 static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
44 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
46 _stm32_serial_setbrg(plat->base, plat->uart_info,
47 plat->clock_rate, baudrate);
52 static int stm32_serial_setconfig(struct udevice *dev, uint serial_config)
54 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
55 bool stm32f4 = plat->uart_info->stm32f4;
56 u8 uart_enable_bit = plat->uart_info->uart_enable_bit;
57 u32 cr1 = plat->base + CR1_OFFSET(stm32f4);
59 uint parity = SERIAL_GET_PARITY(serial_config);
60 uint bits = SERIAL_GET_BITS(serial_config);
61 uint stop = SERIAL_GET_STOP(serial_config);
64 * only parity config is implemented, check if other serial settings
65 * are the default one.
66 * (STM32F4 serial IP didn't support parity setting)
68 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || stm32f4)
69 return -ENOTSUPP; /* not supported in driver*/
71 clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
72 /* update usart configuration (uart need to be disable)
73 * PCE: parity check enable
74 * PS : '0' : Even / '1' : Odd
75 * M[1:0] = '00' : 8 Data bits
76 * M[1:0] = '01' : 9 Data bits with parity
84 config = USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0;
87 config = USART_CR1_PCE | USART_CR1_M0;
92 USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 |
95 setbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
100 static int stm32_serial_getc(struct udevice *dev)
102 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
103 bool stm32f4 = plat->uart_info->stm32f4;
104 fdt_addr_t base = plat->base;
105 u32 isr = readl(base + ISR_OFFSET(stm32f4));
107 if ((isr & USART_ISR_RXNE) == 0)
110 if (isr & (USART_ISR_PE | USART_ISR_ORE | USART_ISR_FE)) {
112 setbits_le32(base + ICR_OFFSET,
113 USART_ICR_PCECF | USART_ICR_ORECF |
116 readl(base + RDR_OFFSET(stm32f4));
120 return readl(base + RDR_OFFSET(stm32f4));
123 static int _stm32_serial_putc(fdt_addr_t base,
124 struct stm32_uart_info *uart_info,
127 bool stm32f4 = uart_info->stm32f4;
129 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0)
132 writel(c, base + TDR_OFFSET(stm32f4));
137 static int stm32_serial_putc(struct udevice *dev, const char c)
139 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
141 return _stm32_serial_putc(plat->base, plat->uart_info, c);
144 static int stm32_serial_pending(struct udevice *dev, bool input)
146 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
147 bool stm32f4 = plat->uart_info->stm32f4;
148 fdt_addr_t base = plat->base;
151 return readl(base + ISR_OFFSET(stm32f4)) &
152 USART_ISR_RXNE ? 1 : 0;
154 return readl(base + ISR_OFFSET(stm32f4)) &
155 USART_ISR_TXE ? 0 : 1;
158 static void _stm32_serial_init(fdt_addr_t base,
159 struct stm32_uart_info *uart_info)
161 bool stm32f4 = uart_info->stm32f4;
162 u8 uart_enable_bit = uart_info->uart_enable_bit;
164 /* Disable uart-> enable fifo -> enable uart */
165 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
166 BIT(uart_enable_bit));
167 if (uart_info->has_fifo)
168 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN);
169 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
170 BIT(uart_enable_bit));
173 static int stm32_serial_probe(struct udevice *dev)
175 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
177 struct reset_ctl reset;
180 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
182 ret = clk_get_by_index(dev, 0, &clk);
186 ret = clk_enable(&clk);
188 dev_err(dev, "failed to enable clock\n");
192 ret = reset_get_by_index(dev, 0, &reset);
194 reset_assert(&reset);
196 reset_deassert(&reset);
199 plat->clock_rate = clk_get_rate(&clk);
200 if (!plat->clock_rate) {
205 _stm32_serial_init(plat->base, plat->uart_info);
210 static const struct udevice_id stm32_serial_id[] = {
211 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
212 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
213 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
217 static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
219 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
221 plat->base = devfdt_get_addr(dev);
222 if (plat->base == FDT_ADDR_T_NONE)
228 static const struct dm_serial_ops stm32_serial_ops = {
229 .putc = stm32_serial_putc,
230 .pending = stm32_serial_pending,
231 .getc = stm32_serial_getc,
232 .setbrg = stm32_serial_setbrg,
233 .setconfig = stm32_serial_setconfig
236 U_BOOT_DRIVER(serial_stm32) = {
237 .name = "serial_stm32",
239 .of_match = of_match_ptr(stm32_serial_id),
240 .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
241 .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
242 .ops = &stm32_serial_ops,
243 .probe = stm32_serial_probe,
244 #if !CONFIG_IS_ENABLED(OF_CONTROL)
245 .flags = DM_FLAG_PRE_RELOC,
249 #ifdef CONFIG_DEBUG_UART_STM32
250 #include <debug_uart.h>
251 static inline struct stm32_uart_info *_debug_uart_info(void)
253 struct stm32_uart_info *uart_info;
255 #if defined(CONFIG_STM32F4)
256 uart_info = &stm32f4_info;
257 #elif defined(CONFIG_STM32F7)
258 uart_info = &stm32f7_info;
260 uart_info = &stm32h7_info;
265 static inline void _debug_uart_init(void)
267 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
268 struct stm32_uart_info *uart_info = _debug_uart_info();
270 _stm32_serial_init(base, uart_info);
271 _stm32_serial_setbrg(base, uart_info,
272 CONFIG_DEBUG_UART_CLOCK,
276 static inline void _debug_uart_putc(int c)
278 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
279 struct stm32_uart_info *uart_info = _debug_uart_info();
281 while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN)