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.
13 #include <asm/arch/stm32.h>
14 #include "serial_stm32.h"
16 static void _stm32_serial_setbrg(fdt_addr_t base,
17 struct stm32_uart_info *uart_info,
21 bool stm32f4 = uart_info->stm32f4;
22 u32 int_div, mantissa, fraction, oversampling;
24 int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate);
28 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
31 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
34 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
35 fraction = int_div % oversampling;
37 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
40 static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
42 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
44 _stm32_serial_setbrg(plat->base, plat->uart_info,
45 plat->clock_rate, baudrate);
50 static int stm32_serial_setparity(struct udevice *dev, enum serial_par parity)
52 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
53 bool stm32f4 = plat->uart_info->stm32f4;
54 u8 uart_enable_bit = plat->uart_info->uart_enable_bit;
55 u32 cr1 = plat->base + CR1_OFFSET(stm32f4);
59 return -EINVAL; /* not supported in driver*/
61 clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
62 /* update usart configuration (uart need to be disable)
63 * PCE: parity check control
64 * PS : '0' : Even / '1' : Odd
65 * M[1:0] = '00' : 8 Data bits
66 * M[1:0] = '01' : 9 Data bits with parity
74 config = USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0;
77 config = USART_CR1_PCE | USART_CR1_M0;
81 USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 |
84 setbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
89 static int stm32_serial_getc(struct udevice *dev)
91 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
92 bool stm32f4 = plat->uart_info->stm32f4;
93 fdt_addr_t base = plat->base;
94 u32 isr = readl(base + ISR_OFFSET(stm32f4));
96 if ((isr & USART_ISR_RXNE) == 0)
99 if (isr & (USART_ISR_PE | USART_ISR_ORE)) {
101 setbits_le32(base + ICR_OFFSET,
102 USART_ICR_PCECF | USART_ICR_ORECF);
104 readl(base + RDR_OFFSET(stm32f4));
108 return readl(base + RDR_OFFSET(stm32f4));
111 static int _stm32_serial_putc(fdt_addr_t base,
112 struct stm32_uart_info *uart_info,
115 bool stm32f4 = uart_info->stm32f4;
117 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0)
120 writel(c, base + TDR_OFFSET(stm32f4));
125 static int stm32_serial_putc(struct udevice *dev, const char c)
127 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
129 return _stm32_serial_putc(plat->base, plat->uart_info, c);
132 static int stm32_serial_pending(struct udevice *dev, bool input)
134 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
135 bool stm32f4 = plat->uart_info->stm32f4;
136 fdt_addr_t base = plat->base;
139 return readl(base + ISR_OFFSET(stm32f4)) &
140 USART_ISR_RXNE ? 1 : 0;
142 return readl(base + ISR_OFFSET(stm32f4)) &
143 USART_ISR_TXE ? 0 : 1;
146 static void _stm32_serial_init(fdt_addr_t base,
147 struct stm32_uart_info *uart_info)
149 bool stm32f4 = uart_info->stm32f4;
150 u8 uart_enable_bit = uart_info->uart_enable_bit;
152 /* Disable uart-> enable fifo -> enable uart */
153 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
154 BIT(uart_enable_bit));
155 if (uart_info->has_fifo)
156 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN);
157 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
158 BIT(uart_enable_bit));
161 static int stm32_serial_probe(struct udevice *dev)
163 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
167 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
169 ret = clk_get_by_index(dev, 0, &clk);
173 ret = clk_enable(&clk);
175 dev_err(dev, "failed to enable clock\n");
179 plat->clock_rate = clk_get_rate(&clk);
180 if (plat->clock_rate < 0) {
182 return plat->clock_rate;
185 _stm32_serial_init(plat->base, plat->uart_info);
190 static const struct udevice_id stm32_serial_id[] = {
191 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
192 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
193 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
197 static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
199 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
201 plat->base = devfdt_get_addr(dev);
202 if (plat->base == FDT_ADDR_T_NONE)
208 static const struct dm_serial_ops stm32_serial_ops = {
209 .putc = stm32_serial_putc,
210 .pending = stm32_serial_pending,
211 .getc = stm32_serial_getc,
212 .setbrg = stm32_serial_setbrg,
213 .setparity = stm32_serial_setparity
216 U_BOOT_DRIVER(serial_stm32) = {
217 .name = "serial_stm32",
219 .of_match = of_match_ptr(stm32_serial_id),
220 .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
221 .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
222 .ops = &stm32_serial_ops,
223 .probe = stm32_serial_probe,
224 .flags = DM_FLAG_PRE_RELOC,
227 #ifdef CONFIG_DEBUG_UART_STM32
228 #include <debug_uart.h>
229 static inline struct stm32_uart_info *_debug_uart_info(void)
231 struct stm32_uart_info *uart_info;
233 #if defined(CONFIG_STM32F4)
234 uart_info = &stm32f4_info;
235 #elif defined(CONFIG_STM32F7)
236 uart_info = &stm32f7_info;
238 uart_info = &stm32h7_info;
243 static inline void _debug_uart_init(void)
245 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
246 struct stm32_uart_info *uart_info = _debug_uart_info();
248 _stm32_serial_init(base, uart_info);
249 _stm32_serial_setbrg(base, uart_info,
250 CONFIG_DEBUG_UART_CLOCK,
252 printf("DEBUG done\n");
255 static inline void _debug_uart_putc(int c)
257 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
258 struct stm32_uart_info *uart_info = _debug_uart_info();
260 while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN)