serial: stm32x7: migrate serial struct to driver
[oweals/u-boot.git] / drivers / serial / serial_stm32x7.c
1 /*
2  * (C) Copyright 2016
3  * Vikas Manocha, <vikas.manocha@st.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <clk.h>
10 #include <dm.h>
11 #include <asm/io.h>
12 #include <serial.h>
13 #include <asm/arch/stm32.h>
14 #include "serial_stm32x7.h"
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
19 {
20         struct stm32x7_serial_platdata *plat = dev->platdata;
21         struct stm32_usart *const usart = plat->base;
22         u32  clock, int_div, mantissa, fraction, oversampling;
23
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);
28         else
29                 return -EINVAL;
30
31         int_div = DIV_ROUND_CLOSEST(clock, baudrate);
32
33         if (int_div < 16) {
34                 oversampling = 8;
35                 setbits_le32(&usart->cr1, USART_CR1_OVER8);
36         } else {
37                 oversampling = 16;
38                 clrbits_le32(&usart->cr1, USART_CR1_OVER8);
39         }
40
41         mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
42         fraction = int_div % oversampling;
43
44         writel(mantissa | fraction, &usart->brr);
45
46         return 0;
47 }
48
49 static int stm32_serial_getc(struct udevice *dev)
50 {
51         struct stm32x7_serial_platdata *plat = dev->platdata;
52         struct stm32_usart *const usart = plat->base;
53
54         if ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0)
55                 return -EAGAIN;
56
57         return readl(&usart->rd_dr);
58 }
59
60 static int stm32_serial_putc(struct udevice *dev, const char c)
61 {
62         struct stm32x7_serial_platdata *plat = dev->platdata;
63         struct stm32_usart *const usart = plat->base;
64
65         if ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0)
66                 return -EAGAIN;
67
68         writel(c, &usart->tx_dr);
69
70         return 0;
71 }
72
73 static int stm32_serial_pending(struct udevice *dev, bool input)
74 {
75         struct stm32x7_serial_platdata *plat = dev->platdata;
76         struct stm32_usart *const usart = plat->base;
77
78         if (input)
79                 return readl(&usart->sr) & USART_SR_FLAG_RXNE ? 1 : 0;
80         else
81                 return readl(&usart->sr) & USART_SR_FLAG_TXE ? 0 : 1;
82 }
83
84 static int stm32_serial_probe(struct udevice *dev)
85 {
86         struct stm32x7_serial_platdata *plat = dev->platdata;
87         struct stm32_usart *const usart = plat->base;
88
89 #ifdef CONFIG_CLK
90         int ret;
91         struct clk clk;
92
93         ret = clk_get_by_index(dev, 0, &clk);
94         if (ret < 0)
95                 return ret;
96
97         ret = clk_enable(&clk);
98         if (ret) {
99                 dev_err(dev, "failed to enable clock\n");
100                 return ret;
101         }
102 #endif
103
104         /* Disable usart-> disable overrun-> enable usart */
105         clrbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
106         setbits_le32(&usart->cr3, USART_CR3_OVRDIS);
107         setbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
108
109         return 0;
110 }
111
112 #if CONFIG_IS_ENABLED(OF_CONTROL)
113 static const struct udevice_id stm32_serial_id[] = {
114         {.compatible = "st,stm32f7-usart"},
115         {.compatible = "st,stm32f7-uart"},
116         {}
117 };
118
119 static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
120 {
121         struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
122         fdt_addr_t addr;
123
124         addr = devfdt_get_addr(dev);
125         if (addr == FDT_ADDR_T_NONE)
126                 return -EINVAL;
127
128         plat->base = (struct stm32_usart *)addr;
129
130         return 0;
131 }
132 #endif
133
134 static const struct dm_serial_ops stm32_serial_ops = {
135         .putc = stm32_serial_putc,
136         .pending = stm32_serial_pending,
137         .getc = stm32_serial_getc,
138         .setbrg = stm32_serial_setbrg,
139 };
140
141 U_BOOT_DRIVER(serial_stm32) = {
142         .name = "serial_stm32x7",
143         .id = UCLASS_SERIAL,
144         .of_match = of_match_ptr(stm32_serial_id),
145         .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
146         .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
147         .ops = &stm32_serial_ops,
148         .probe = stm32_serial_probe,
149         .flags = DM_FLAG_PRE_RELOC,
150 };