3 * Vikas Manocha, <vikas.manocha@st.com>
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <dm/platform_data/serial_stm32x7.h>
13 #include "serial_stm32x7.h"
15 DECLARE_GLOBAL_DATA_PTR;
17 static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
19 struct stm32x7_serial_platdata *plat = dev->platdata;
20 struct stm32_usart *const usart = plat->base;
21 writel(plat->clock/baudrate, &usart->brr);
26 static int stm32_serial_getc(struct udevice *dev)
28 struct stm32x7_serial_platdata *plat = dev->platdata;
29 struct stm32_usart *const usart = plat->base;
31 if ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0)
34 return readl(&usart->rd_dr);
37 static int stm32_serial_putc(struct udevice *dev, const char c)
39 struct stm32x7_serial_platdata *plat = dev->platdata;
40 struct stm32_usart *const usart = plat->base;
42 if ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0)
45 writel(c, &usart->tx_dr);
50 static int stm32_serial_pending(struct udevice *dev, bool input)
52 struct stm32x7_serial_platdata *plat = dev->platdata;
53 struct stm32_usart *const usart = plat->base;
56 return readl(&usart->sr) & USART_SR_FLAG_RXNE ? 1 : 0;
58 return readl(&usart->sr) & USART_SR_FLAG_TXE ? 0 : 1;
61 static int stm32_serial_probe(struct udevice *dev)
63 struct stm32x7_serial_platdata *plat = dev->platdata;
64 struct stm32_usart *const usart = plat->base;
65 setbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
70 static const struct dm_serial_ops stm32_serial_ops = {
71 .putc = stm32_serial_putc,
72 .pending = stm32_serial_pending,
73 .getc = stm32_serial_getc,
74 .setbrg = stm32_serial_setbrg,
77 U_BOOT_DRIVER(serial_stm32) = {
78 .name = "serial_stm32x7",
80 .ops = &stm32_serial_ops,
81 .probe = stm32_serial_probe,
82 .flags = DM_FLAG_PRE_RELOC,