3 * Vikas Manocha, <vikas.manocha@st.com>
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/arch/stm32.h>
13 #include <dm/platform_data/serial_stm32x7.h>
14 #include "serial_stm32x7.h"
16 DECLARE_GLOBAL_DATA_PTR;
18 static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
20 struct stm32x7_serial_platdata *plat = dev->platdata;
21 struct stm32_usart *const usart = plat->base;
22 u32 clock, int_div, frac_div, tmp;
24 if (((u32)usart & STM32_BUS_MASK) == APB1_PERIPH_BASE)
25 clock = clock_get(CLOCK_APB1);
26 else if (((u32)usart & STM32_BUS_MASK) == APB2_PERIPH_BASE)
27 clock = clock_get(CLOCK_APB2);
31 int_div = (25 * clock) / (4 * baudrate);
32 tmp = ((int_div / 100) << USART_BRR_M_SHIFT) & USART_BRR_M_MASK;
33 frac_div = int_div - (100 * (tmp >> USART_BRR_M_SHIFT));
34 tmp |= (((frac_div * 16) + 50) / 100) & USART_BRR_F_MASK;
35 writel(tmp, &usart->brr);
40 static int stm32_serial_getc(struct udevice *dev)
42 struct stm32x7_serial_platdata *plat = dev->platdata;
43 struct stm32_usart *const usart = plat->base;
45 if ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0)
48 return readl(&usart->rd_dr);
51 static int stm32_serial_putc(struct udevice *dev, const char c)
53 struct stm32x7_serial_platdata *plat = dev->platdata;
54 struct stm32_usart *const usart = plat->base;
56 if ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0)
59 writel(c, &usart->tx_dr);
64 static int stm32_serial_pending(struct udevice *dev, bool input)
66 struct stm32x7_serial_platdata *plat = dev->platdata;
67 struct stm32_usart *const usart = plat->base;
70 return readl(&usart->sr) & USART_SR_FLAG_RXNE ? 1 : 0;
72 return readl(&usart->sr) & USART_SR_FLAG_TXE ? 0 : 1;
75 static int stm32_serial_probe(struct udevice *dev)
77 struct stm32x7_serial_platdata *plat = dev->platdata;
78 struct stm32_usart *const usart = plat->base;
79 setbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
84 static const struct dm_serial_ops stm32_serial_ops = {
85 .putc = stm32_serial_putc,
86 .pending = stm32_serial_pending,
87 .getc = stm32_serial_getc,
88 .setbrg = stm32_serial_setbrg,
91 U_BOOT_DRIVER(serial_stm32) = {
92 .name = "serial_stm32x7",
94 .ops = &stm32_serial_ops,
95 .probe = stm32_serial_probe,
96 .flags = DM_FLAG_PRE_RELOC,