blob: f26234549c3e18ee18b7d532ac707c379dd83067 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vikas Manocha5dba05e2016-02-11 15:47:19 -08002/*
Patrice Chotard789ee0e2017-10-23 09:53:58 +02003 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
4 * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
Vikas Manocha5dba05e2016-02-11 15:47:19 -08005 */
6
7#include <common.h>
Vikas Manocha7b00ff92017-02-12 10:25:46 -08008#include <clk.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -08009#include <dm.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -080010#include <serial.h>
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020011#include <watchdog.h>
12#include <asm/io.h>
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090013#include <asm/arch/stm32.h>
Patrice Chotard9e276502018-01-12 09:23:49 +010014#include "serial_stm32.h"
Vikas Manocha5dba05e2016-02-11 15:47:19 -080015
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020016static void _stm32_serial_setbrg(fdt_addr_t base,
17 struct stm32_uart_info *uart_info,
18 u32 clock_rate,
19 int baudrate)
Vikas Manocha5dba05e2016-02-11 15:47:19 -080020{
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020021 bool stm32f4 = uart_info->stm32f4;
Patrice Chotard4809a192017-07-18 09:29:08 +020022 u32 int_div, mantissa, fraction, oversampling;
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090023
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020024 int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate);
Patrice Chotard31496322017-06-08 09:26:55 +020025
26 if (int_div < 16) {
27 oversampling = 8;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020028 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020029 } else {
30 oversampling = 16;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020031 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020032 }
33
34 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
35 fraction = int_div % oversampling;
36
Patrice Chotard5011e6f2017-09-27 15:44:50 +020037 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020038}
39
40static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
41{
42 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
43
44 _stm32_serial_setbrg(plat->base, plat->uart_info,
45 plat->clock_rate, baudrate);
Vikas Manocha5dba05e2016-02-11 15:47:19 -080046
47 return 0;
48}
49
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020050static int stm32_serial_setparity(struct udevice *dev, enum serial_par parity)
51{
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);
56 u32 config = 0;
57
58 if (stm32f4)
59 return -EINVAL; /* not supported in driver*/
60
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
67 */
68 switch (parity) {
69 default:
70 case SERIAL_PAR_NONE:
71 config = 0;
72 break;
73 case SERIAL_PAR_ODD:
74 config = USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0;
75 break;
76 case SERIAL_PAR_EVEN:
77 config = USART_CR1_PCE | USART_CR1_M0;
78 break;
79 }
80 clrsetbits_le32(cr1,
81 USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 |
82 USART_CR1_M0,
83 config);
84 setbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
85
86 return 0;
87}
88
Vikas Manocha5dba05e2016-02-11 15:47:19 -080089static int stm32_serial_getc(struct udevice *dev)
90{
Patrice Chotard5011e6f2017-09-27 15:44:50 +020091 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
92 bool stm32f4 = plat->uart_info->stm32f4;
93 fdt_addr_t base = plat->base;
Patrice Chotard24af24b2018-04-20 08:59:06 +020094 u32 isr = readl(base + ISR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -080095
Patrice Chotarddb0536e2018-05-17 14:50:43 +020096 if ((isr & USART_ISR_RXNE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -080097 return -EAGAIN;
98
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020099 if (isr & (USART_ISR_PE | USART_ISR_ORE)) {
Patrice Chotard24af24b2018-04-20 08:59:06 +0200100 if (!stm32f4)
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +0200101 setbits_le32(base + ICR_OFFSET,
102 USART_ICR_PCECF | USART_ICR_ORECF);
Patrice Chotard24af24b2018-04-20 08:59:06 +0200103 else
104 readl(base + RDR_OFFSET(stm32f4));
105 return -EIO;
106 }
107
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200108 return readl(base + RDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800109}
110
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200111static int _stm32_serial_putc(fdt_addr_t base,
112 struct stm32_uart_info *uart_info,
113 const char c)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800114{
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200115 bool stm32f4 = uart_info->stm32f4;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800116
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200117 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800118 return -EAGAIN;
119
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200120 writel(c, base + TDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800121
122 return 0;
123}
124
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200125static int stm32_serial_putc(struct udevice *dev, const char c)
126{
127 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
128
129 return _stm32_serial_putc(plat->base, plat->uart_info, c);
130}
131
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800132static int stm32_serial_pending(struct udevice *dev, bool input)
133{
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200134 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
135 bool stm32f4 = plat->uart_info->stm32f4;
136 fdt_addr_t base = plat->base;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800137
138 if (input)
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200139 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200140 USART_ISR_RXNE ? 1 : 0;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800141 else
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200142 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200143 USART_ISR_TXE ? 0 : 1;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800144}
145
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200146static void _stm32_serial_init(fdt_addr_t base,
147 struct stm32_uart_info *uart_info)
148{
149 bool stm32f4 = uart_info->stm32f4;
150 u8 uart_enable_bit = uart_info->uart_enable_bit;
151
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));
159}
160
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800161static int stm32_serial_probe(struct udevice *dev)
162{
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200163 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Patrice Chotard21aad132017-09-27 15:44:53 +0200164 struct clk clk;
Patrice Chotard21aad132017-09-27 15:44:53 +0200165 int ret;
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200166
167 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800168
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800169 ret = clk_get_by_index(dev, 0, &clk);
170 if (ret < 0)
171 return ret;
172
173 ret = clk_enable(&clk);
174 if (ret) {
175 dev_err(dev, "failed to enable clock\n");
176 return ret;
177 }
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800178
Patrice Chotard4809a192017-07-18 09:29:08 +0200179 plat->clock_rate = clk_get_rate(&clk);
180 if (plat->clock_rate < 0) {
181 clk_disable(&clk);
182 return plat->clock_rate;
183 };
184
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200185 _stm32_serial_init(plat->base, plat->uart_info);
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800186
187 return 0;
188}
189
Vikas Manocha19e22c62017-02-12 10:25:44 -0800190static const struct udevice_id stm32_serial_id[] = {
Patrice Chotardb21a69a2017-09-27 15:44:52 +0200191 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
Patrice Chotard24fc72d2017-09-27 15:44:51 +0200192 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
193 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
Vikas Manocha19e22c62017-02-12 10:25:44 -0800194 {}
195};
196
197static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
198{
199 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Vikas Manocha19e22c62017-02-12 10:25:44 -0800200
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200201 plat->base = devfdt_get_addr(dev);
202 if (plat->base == FDT_ADDR_T_NONE)
Vikas Manocha19e22c62017-02-12 10:25:44 -0800203 return -EINVAL;
204
Vikas Manocha19e22c62017-02-12 10:25:44 -0800205 return 0;
206}
Vikas Manocha19e22c62017-02-12 10:25:44 -0800207
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800208static 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,
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +0200213 .setparity = stm32_serial_setparity
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800214};
215
216U_BOOT_DRIVER(serial_stm32) = {
Patrice Chotard9e276502018-01-12 09:23:49 +0100217 .name = "serial_stm32",
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800218 .id = UCLASS_SERIAL,
Vikas Manocha19e22c62017-02-12 10:25:44 -0800219 .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),
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800222 .ops = &stm32_serial_ops,
223 .probe = stm32_serial_probe,
224 .flags = DM_FLAG_PRE_RELOC,
225};
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200226
227#ifdef CONFIG_DEBUG_UART_STM32
228#include <debug_uart.h>
229static inline struct stm32_uart_info *_debug_uart_info(void)
230{
231 struct stm32_uart_info *uart_info;
232
233#if defined(CONFIG_STM32F4)
234 uart_info = &stm32f4_info;
235#elif defined(CONFIG_STM32F7)
236 uart_info = &stm32f7_info;
237#else
238 uart_info = &stm32h7_info;
239#endif
240 return uart_info;
241}
242
243static inline void _debug_uart_init(void)
244{
245 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
246 struct stm32_uart_info *uart_info = _debug_uart_info();
247
248 _stm32_serial_init(base, uart_info);
249 _stm32_serial_setbrg(base, uart_info,
250 CONFIG_DEBUG_UART_CLOCK,
251 CONFIG_BAUDRATE);
252 printf("DEBUG done\n");
253}
254
255static inline void _debug_uart_putc(int c)
256{
257 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
258 struct stm32_uart_info *uart_info = _debug_uart_info();
259
260 while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN)
261 WATCHDOG_RESET();
262}
263
264DEBUG_UART_FUNCS
265#endif