blob: 66e02d5689d09ddc052b3d184c008a5b384ac50e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vikas Manocha6a12ceb2016-02-11 15:47:19 -08002/*
Patrice Chotard3bc599c2017-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 Manocha6a12ceb2016-02-11 15:47:19 -08005 */
6
7#include <common.h>
Vikas Manochafd03b832017-02-12 10:25:46 -08008#include <clk.h>
Vikas Manocha6a12ceb2016-02-11 15:47:19 -08009#include <dm.h>
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080010#include <serial.h>
Patrick Delaunay215c8be2018-05-17 14:50:42 +020011#include <watchdog.h>
12#include <asm/io.h>
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090013#include <asm/arch/stm32.h>
Patrice Chotardae74de02018-01-12 09:23:49 +010014#include "serial_stm32.h"
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080015
Patrick Delaunay215c8be2018-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 Manocha6a12ceb2016-02-11 15:47:19 -080020{
Patrick Delaunay215c8be2018-05-17 14:50:42 +020021 bool stm32f4 = uart_info->stm32f4;
Patrice Chotard27265ce2017-07-18 09:29:08 +020022 u32 int_div, mantissa, fraction, oversampling;
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090023
Patrick Delaunay215c8be2018-05-17 14:50:42 +020024 int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020025
26 if (int_div < 16) {
27 oversampling = 8;
Patrice Chotard60a996b2017-09-27 15:44:50 +020028 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020029 } else {
30 oversampling = 16;
Patrice Chotard60a996b2017-09-27 15:44:50 +020031 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020032 }
33
34 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
35 fraction = int_div % oversampling;
36
Patrice Chotard60a996b2017-09-27 15:44:50 +020037 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
Patrick Delaunay215c8be2018-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 Manocha6a12ceb2016-02-11 15:47:19 -080046
47 return 0;
48}
49
Patrice Chotardfbd5c722018-08-03 15:07:39 +020050static int stm32_serial_setconfig(struct udevice *dev, uint serial_config)
Patrick Delaunaybc709a42018-05-17 14:50:45 +020051{
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;
Patrice Chotardfbd5c722018-08-03 15:07:39 +020057 uint parity = SERIAL_GET_PARITY(serial_config);
58 uint bits = SERIAL_GET_BITS(serial_config);
59 uint stop = SERIAL_GET_STOP(serial_config);
Patrick Delaunaybc709a42018-05-17 14:50:45 +020060
Patrice Chotardfbd5c722018-08-03 15:07:39 +020061 /*
62 * only parity config is implemented, check if other serial settings
63 * are the default one.
64 * (STM32F4 serial IP didn't support parity setting)
65 */
66 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || stm32f4)
67 return -ENOTSUPP; /* not supported in driver*/
Patrick Delaunaybc709a42018-05-17 14:50:45 +020068
69 clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
70 /* update usart configuration (uart need to be disable)
Patrice Chotardfbd5c722018-08-03 15:07:39 +020071 * PCE: parity check enable
Patrick Delaunaybc709a42018-05-17 14:50:45 +020072 * PS : '0' : Even / '1' : Odd
73 * M[1:0] = '00' : 8 Data bits
74 * M[1:0] = '01' : 9 Data bits with parity
75 */
76 switch (parity) {
77 default:
78 case SERIAL_PAR_NONE:
79 config = 0;
80 break;
81 case SERIAL_PAR_ODD:
82 config = USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0;
83 break;
84 case SERIAL_PAR_EVEN:
85 config = USART_CR1_PCE | USART_CR1_M0;
86 break;
87 }
Patrice Chotardfbd5c722018-08-03 15:07:39 +020088
Patrick Delaunaybc709a42018-05-17 14:50:45 +020089 clrsetbits_le32(cr1,
90 USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 |
91 USART_CR1_M0,
92 config);
93 setbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
94
95 return 0;
96}
97
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080098static int stm32_serial_getc(struct udevice *dev)
99{
Patrice Chotard60a996b2017-09-27 15:44:50 +0200100 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
101 bool stm32f4 = plat->uart_info->stm32f4;
102 fdt_addr_t base = plat->base;
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200103 u32 isr = readl(base + ISR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800104
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200105 if ((isr & USART_ISR_RXNE) == 0)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800106 return -EAGAIN;
107
Patrick Delaunaybc709a42018-05-17 14:50:45 +0200108 if (isr & (USART_ISR_PE | USART_ISR_ORE)) {
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200109 if (!stm32f4)
Patrick Delaunaybc709a42018-05-17 14:50:45 +0200110 setbits_le32(base + ICR_OFFSET,
111 USART_ICR_PCECF | USART_ICR_ORECF);
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200112 else
113 readl(base + RDR_OFFSET(stm32f4));
114 return -EIO;
115 }
116
Patrice Chotard60a996b2017-09-27 15:44:50 +0200117 return readl(base + RDR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800118}
119
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200120static int _stm32_serial_putc(fdt_addr_t base,
121 struct stm32_uart_info *uart_info,
122 const char c)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800123{
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200124 bool stm32f4 = uart_info->stm32f4;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800125
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200126 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800127 return -EAGAIN;
128
Patrice Chotard60a996b2017-09-27 15:44:50 +0200129 writel(c, base + TDR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800130
131 return 0;
132}
133
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200134static int stm32_serial_putc(struct udevice *dev, const char c)
135{
136 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
137
138 return _stm32_serial_putc(plat->base, plat->uart_info, c);
139}
140
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800141static int stm32_serial_pending(struct udevice *dev, bool input)
142{
Patrice Chotard60a996b2017-09-27 15:44:50 +0200143 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
144 bool stm32f4 = plat->uart_info->stm32f4;
145 fdt_addr_t base = plat->base;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800146
147 if (input)
Patrice Chotard60a996b2017-09-27 15:44:50 +0200148 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200149 USART_ISR_RXNE ? 1 : 0;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800150 else
Patrice Chotard60a996b2017-09-27 15:44:50 +0200151 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200152 USART_ISR_TXE ? 0 : 1;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800153}
154
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200155static void _stm32_serial_init(fdt_addr_t base,
156 struct stm32_uart_info *uart_info)
157{
158 bool stm32f4 = uart_info->stm32f4;
159 u8 uart_enable_bit = uart_info->uart_enable_bit;
160
161 /* Disable uart-> enable fifo -> enable uart */
162 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
163 BIT(uart_enable_bit));
164 if (uart_info->has_fifo)
165 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN);
166 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
167 BIT(uart_enable_bit));
168}
169
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800170static int stm32_serial_probe(struct udevice *dev)
171{
Patrice Chotard60a996b2017-09-27 15:44:50 +0200172 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Patrice Chotard9a212d72017-09-27 15:44:53 +0200173 struct clk clk;
Patrice Chotard9a212d72017-09-27 15:44:53 +0200174 int ret;
Patrice Chotard60a996b2017-09-27 15:44:50 +0200175
176 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
Vikas Manochafd03b832017-02-12 10:25:46 -0800177
Vikas Manochafd03b832017-02-12 10:25:46 -0800178 ret = clk_get_by_index(dev, 0, &clk);
179 if (ret < 0)
180 return ret;
181
182 ret = clk_enable(&clk);
183 if (ret) {
184 dev_err(dev, "failed to enable clock\n");
185 return ret;
186 }
Vikas Manochafd03b832017-02-12 10:25:46 -0800187
Patrice Chotard27265ce2017-07-18 09:29:08 +0200188 plat->clock_rate = clk_get_rate(&clk);
189 if (plat->clock_rate < 0) {
190 clk_disable(&clk);
191 return plat->clock_rate;
192 };
193
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200194 _stm32_serial_init(plat->base, plat->uart_info);
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800195
196 return 0;
197}
198
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800199static const struct udevice_id stm32_serial_id[] = {
Patrice Chotard6c30f152017-09-27 15:44:52 +0200200 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
Patrice Chotard2a7ecc52017-09-27 15:44:51 +0200201 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
202 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800203 {}
204};
205
206static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
207{
208 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800209
Patrice Chotard60a996b2017-09-27 15:44:50 +0200210 plat->base = devfdt_get_addr(dev);
211 if (plat->base == FDT_ADDR_T_NONE)
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800212 return -EINVAL;
213
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800214 return 0;
215}
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800216
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800217static const struct dm_serial_ops stm32_serial_ops = {
218 .putc = stm32_serial_putc,
219 .pending = stm32_serial_pending,
220 .getc = stm32_serial_getc,
221 .setbrg = stm32_serial_setbrg,
Patrice Chotardfbd5c722018-08-03 15:07:39 +0200222 .setconfig = stm32_serial_setconfig
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800223};
224
225U_BOOT_DRIVER(serial_stm32) = {
Patrice Chotardae74de02018-01-12 09:23:49 +0100226 .name = "serial_stm32",
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800227 .id = UCLASS_SERIAL,
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800228 .of_match = of_match_ptr(stm32_serial_id),
229 .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
230 .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800231 .ops = &stm32_serial_ops,
232 .probe = stm32_serial_probe,
233 .flags = DM_FLAG_PRE_RELOC,
234};
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200235
236#ifdef CONFIG_DEBUG_UART_STM32
237#include <debug_uart.h>
238static inline struct stm32_uart_info *_debug_uart_info(void)
239{
240 struct stm32_uart_info *uart_info;
241
242#if defined(CONFIG_STM32F4)
243 uart_info = &stm32f4_info;
244#elif defined(CONFIG_STM32F7)
245 uart_info = &stm32f7_info;
246#else
247 uart_info = &stm32h7_info;
248#endif
249 return uart_info;
250}
251
252static inline void _debug_uart_init(void)
253{
254 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
255 struct stm32_uart_info *uart_info = _debug_uart_info();
256
257 _stm32_serial_init(base, uart_info);
258 _stm32_serial_setbrg(base, uart_info,
259 CONFIG_DEBUG_UART_CLOCK,
260 CONFIG_BAUDRATE);
261 printf("DEBUG done\n");
262}
263
264static inline void _debug_uart_putc(int c)
265{
266 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
267 struct stm32_uart_info *uart_info = _debug_uart_info();
268
269 while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN)
270 WATCHDOG_RESET();
271}
272
273DEBUG_UART_FUNCS
274#endif