3 * Vikas Manocha, <vikas.manocha@st.com>
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/arch/stm32.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 int_div, mantissa, fraction, oversampling;
24 int_div = DIV_ROUND_CLOSEST(plat->clock_rate, baudrate);
28 setbits_le32(&usart->cr1, USART_CR1_OVER8);
31 clrbits_le32(&usart->cr1, USART_CR1_OVER8);
34 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
35 fraction = int_div % oversampling;
37 writel(mantissa | fraction, &usart->brr);
42 static int stm32_serial_getc(struct udevice *dev)
44 struct stm32x7_serial_platdata *plat = dev->platdata;
45 struct stm32_usart *const usart = plat->base;
47 if ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0)
50 return readl(&usart->rd_dr);
53 static int stm32_serial_putc(struct udevice *dev, const char c)
55 struct stm32x7_serial_platdata *plat = dev->platdata;
56 struct stm32_usart *const usart = plat->base;
58 if ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0)
61 writel(c, &usart->tx_dr);
66 static int stm32_serial_pending(struct udevice *dev, bool input)
68 struct stm32x7_serial_platdata *plat = dev->platdata;
69 struct stm32_usart *const usart = plat->base;
72 return readl(&usart->sr) & USART_SR_FLAG_RXNE ? 1 : 0;
74 return readl(&usart->sr) & USART_SR_FLAG_TXE ? 0 : 1;
77 static int stm32_serial_probe(struct udevice *dev)
79 struct stm32x7_serial_platdata *plat = dev->platdata;
80 struct stm32_usart *const usart = plat->base;
86 ret = clk_get_by_index(dev, 0, &clk);
90 ret = clk_enable(&clk);
92 dev_err(dev, "failed to enable clock\n");
97 plat->clock_rate = clk_get_rate(&clk);
98 if (plat->clock_rate < 0) {
100 return plat->clock_rate;
103 /* Disable usart-> disable overrun-> enable usart */
104 clrbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
105 setbits_le32(&usart->cr3, USART_CR3_OVRDIS);
106 setbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
111 #if CONFIG_IS_ENABLED(OF_CONTROL)
112 static const struct udevice_id stm32_serial_id[] = {
113 {.compatible = "st,stm32f7-usart"},
114 {.compatible = "st,stm32f7-uart"},
118 static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
120 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
123 addr = devfdt_get_addr(dev);
124 if (addr == FDT_ADDR_T_NONE)
127 plat->base = (struct stm32_usart *)addr;
133 static const struct dm_serial_ops stm32_serial_ops = {
134 .putc = stm32_serial_putc,
135 .pending = stm32_serial_pending,
136 .getc = stm32_serial_getc,
137 .setbrg = stm32_serial_setbrg,
140 U_BOOT_DRIVER(serial_stm32) = {
141 .name = "serial_stm32x7",
143 .of_match = of_match_ptr(stm32_serial_id),
144 .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
145 .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
146 .ops = &stm32_serial_ops,
147 .probe = stm32_serial_probe,
148 .flags = DM_FLAG_PRE_RELOC,