blob: 1ee58142b3f82535281460f1ebc901fef831e220 [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
Patrick Delaunay80bd23b2020-11-06 19:01:56 +01007#define LOG_CATEGORY UCLASS_SERIAL
8
Vikas Manocha7b00ff92017-02-12 10:25:46 -08009#include <clk.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -080010#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Patrice Chotard4d6701e2018-12-04 14:11:36 +010012#include <reset.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -080013#include <serial.h>
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020014#include <watchdog.h>
15#include <asm/io.h>
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090016#include <asm/arch/stm32.h>
Patrick Delaunay80bd23b2020-11-06 19:01:56 +010017#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Patrice Chotarde6262b12023-05-31 08:01:30 +020020#include <linux/iopoll.h>
Patrice Chotard9e276502018-01-12 09:23:49 +010021#include "serial_stm32.h"
Simon Glass9bc15642020-02-03 07:36:16 -070022#include <dm/device_compat.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -080023
Valentin Caronaa74b112023-08-04 16:09:04 +020024/*
25 * At 115200 bits/s
26 * 1 bit = 1 / 115200 = 8,68 us
27 * 8 bits = 69,444 us
28 * 10 bits are needed for worst case (8 bits + 1 start + 1 stop) = 86.806 us
29 */
30#define ONE_BYTE_B115200_US 87
31
Patrice Chotardf93f92b2023-10-27 16:43:01 +020032static void _stm32_serial_setbrg(void __iomem *base,
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020033 struct stm32_uart_info *uart_info,
34 u32 clock_rate,
35 int baudrate)
Vikas Manocha5dba05e2016-02-11 15:47:19 -080036{
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020037 bool stm32f4 = uart_info->stm32f4;
Patrice Chotard4809a192017-07-18 09:29:08 +020038 u32 int_div, mantissa, fraction, oversampling;
Patrice Chotard45bc3c42023-05-31 08:01:31 +020039 u8 uart_enable_bit = uart_info->uart_enable_bit;
40
41 /* BRR register must be set when uart is disabled */
42 clrbits_le32(base + CR1_OFFSET(stm32f4), BIT(uart_enable_bit));
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090043
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020044 int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate);
Patrice Chotard31496322017-06-08 09:26:55 +020045
46 if (int_div < 16) {
47 oversampling = 8;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020048 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020049 } else {
50 oversampling = 16;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020051 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020052 }
53
54 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
55 fraction = int_div % oversampling;
56
Patrice Chotard5011e6f2017-09-27 15:44:50 +020057 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
Patrice Chotard45bc3c42023-05-31 08:01:31 +020058
59 setbits_le32(base + CR1_OFFSET(stm32f4), BIT(uart_enable_bit));
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020060}
61
62static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
63{
Simon Glassb75b15b2020-12-03 16:55:23 -070064 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020065
66 _stm32_serial_setbrg(plat->base, plat->uart_info,
67 plat->clock_rate, baudrate);
Vikas Manocha5dba05e2016-02-11 15:47:19 -080068
69 return 0;
70}
71
Patrice Chotard34e64c02018-08-03 15:07:39 +020072static int stm32_serial_setconfig(struct udevice *dev, uint serial_config)
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020073{
Simon Glassb75b15b2020-12-03 16:55:23 -070074 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020075 bool stm32f4 = plat->uart_info->stm32f4;
76 u8 uart_enable_bit = plat->uart_info->uart_enable_bit;
Patrice Chotardf93f92b2023-10-27 16:43:01 +020077 void __iomem *cr1 = plat->base + CR1_OFFSET(stm32f4);
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020078 u32 config = 0;
Patrice Chotard34e64c02018-08-03 15:07:39 +020079 uint parity = SERIAL_GET_PARITY(serial_config);
80 uint bits = SERIAL_GET_BITS(serial_config);
81 uint stop = SERIAL_GET_STOP(serial_config);
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020082
Patrice Chotard34e64c02018-08-03 15:07:39 +020083 /*
84 * only parity config is implemented, check if other serial settings
85 * are the default one.
86 * (STM32F4 serial IP didn't support parity setting)
87 */
88 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || stm32f4)
89 return -ENOTSUPP; /* not supported in driver*/
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020090
91 clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
92 /* update usart configuration (uart need to be disable)
Patrice Chotard34e64c02018-08-03 15:07:39 +020093 * PCE: parity check enable
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020094 * PS : '0' : Even / '1' : Odd
95 * M[1:0] = '00' : 8 Data bits
96 * M[1:0] = '01' : 9 Data bits with parity
97 */
98 switch (parity) {
99 default:
100 case SERIAL_PAR_NONE:
101 config = 0;
102 break;
103 case SERIAL_PAR_ODD:
104 config = USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0;
105 break;
106 case SERIAL_PAR_EVEN:
107 config = USART_CR1_PCE | USART_CR1_M0;
108 break;
109 }
Patrice Chotard34e64c02018-08-03 15:07:39 +0200110
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +0200111 clrsetbits_le32(cr1,
112 USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 |
113 USART_CR1_M0,
114 config);
115 setbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
116
117 return 0;
118}
119
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800120static int stm32_serial_getc(struct udevice *dev)
121{
Simon Glassb75b15b2020-12-03 16:55:23 -0700122 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200123 bool stm32f4 = plat->uart_info->stm32f4;
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200124 void __iomem *base = plat->base;
Patrice Chotard24af24b2018-04-20 08:59:06 +0200125 u32 isr = readl(base + ISR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800126
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200127 if ((isr & USART_ISR_RXNE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800128 return -EAGAIN;
129
Patrick Delaunay1a103bf2019-07-30 19:16:46 +0200130 if (isr & (USART_ISR_PE | USART_ISR_ORE | USART_ISR_FE)) {
Patrice Chotard24af24b2018-04-20 08:59:06 +0200131 if (!stm32f4)
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +0200132 setbits_le32(base + ICR_OFFSET,
Patrick Delaunay1a103bf2019-07-30 19:16:46 +0200133 USART_ICR_PCECF | USART_ICR_ORECF |
134 USART_ICR_FECF);
Patrice Chotard24af24b2018-04-20 08:59:06 +0200135 else
136 readl(base + RDR_OFFSET(stm32f4));
137 return -EIO;
138 }
139
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200140 return readl(base + RDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800141}
142
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200143static int _stm32_serial_putc(void __iomem *base,
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200144 struct stm32_uart_info *uart_info,
145 const char c)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800146{
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200147 bool stm32f4 = uart_info->stm32f4;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800148
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200149 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800150 return -EAGAIN;
151
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200152 writel(c, base + TDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800153
154 return 0;
155}
156
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200157static int stm32_serial_putc(struct udevice *dev, const char c)
158{
Simon Glassb75b15b2020-12-03 16:55:23 -0700159 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200160
161 return _stm32_serial_putc(plat->base, plat->uart_info, c);
162}
163
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800164static int stm32_serial_pending(struct udevice *dev, bool input)
165{
Simon Glassb75b15b2020-12-03 16:55:23 -0700166 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200167 bool stm32f4 = plat->uart_info->stm32f4;
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200168 void __iomem *base = plat->base;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800169
170 if (input)
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200171 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200172 USART_ISR_RXNE ? 1 : 0;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800173 else
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200174 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200175 USART_ISR_TXE ? 0 : 1;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800176}
177
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200178static void _stm32_serial_init(void __iomem *base,
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200179 struct stm32_uart_info *uart_info)
180{
181 bool stm32f4 = uart_info->stm32f4;
182 u8 uart_enable_bit = uart_info->uart_enable_bit;
183
184 /* Disable uart-> enable fifo -> enable uart */
185 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
186 BIT(uart_enable_bit));
187 if (uart_info->has_fifo)
188 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN);
189 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
190 BIT(uart_enable_bit));
191}
192
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800193static int stm32_serial_probe(struct udevice *dev)
194{
Simon Glassb75b15b2020-12-03 16:55:23 -0700195 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard21aad132017-09-27 15:44:53 +0200196 struct clk clk;
Patrice Chotard4d6701e2018-12-04 14:11:36 +0100197 struct reset_ctl reset;
Patrice Chotarde6262b12023-05-31 08:01:30 +0200198 u32 isr;
Patrice Chotard21aad132017-09-27 15:44:53 +0200199 int ret;
Patrice Chotarde6262b12023-05-31 08:01:30 +0200200 bool stm32f4;
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200201
202 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
Patrice Chotarde6262b12023-05-31 08:01:30 +0200203 stm32f4 = plat->uart_info->stm32f4;
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800204
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800205 ret = clk_get_by_index(dev, 0, &clk);
206 if (ret < 0)
207 return ret;
208
209 ret = clk_enable(&clk);
210 if (ret) {
211 dev_err(dev, "failed to enable clock\n");
212 return ret;
213 }
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800214
Patrice Chotarde6262b12023-05-31 08:01:30 +0200215 /*
216 * before uart initialization, wait for TC bit (Transmission Complete)
217 * in case there is still chars from previous bootstage to transmit
218 */
Valentin Caronaa74b112023-08-04 16:09:04 +0200219 ret = read_poll_timeout(readl, isr, isr & USART_ISR_TC, 50,
220 16 * ONE_BYTE_B115200_US, plat->base + ISR_OFFSET(stm32f4));
221 if (ret)
222 dev_dbg(dev, "FIFO not empty, some character can be lost (%d)\n", ret);
Patrice Chotarde6262b12023-05-31 08:01:30 +0200223
Patrice Chotard4d6701e2018-12-04 14:11:36 +0100224 ret = reset_get_by_index(dev, 0, &reset);
225 if (!ret) {
226 reset_assert(&reset);
227 udelay(2);
228 reset_deassert(&reset);
229 }
230
Patrice Chotard4809a192017-07-18 09:29:08 +0200231 plat->clock_rate = clk_get_rate(&clk);
Patrick Delaunay3f4afd32019-06-21 15:26:41 +0200232 if (!plat->clock_rate) {
Patrice Chotard4809a192017-07-18 09:29:08 +0200233 clk_disable(&clk);
Patrick Delaunay3f4afd32019-06-21 15:26:41 +0200234 return -EINVAL;
Patrice Chotard4809a192017-07-18 09:29:08 +0200235 };
236
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200237 _stm32_serial_init(plat->base, plat->uart_info);
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800238
239 return 0;
240}
241
Vikas Manocha19e22c62017-02-12 10:25:44 -0800242static const struct udevice_id stm32_serial_id[] = {
Patrice Chotardb21a69a2017-09-27 15:44:52 +0200243 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
Patrice Chotard24fc72d2017-09-27 15:44:51 +0200244 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
245 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
Vikas Manocha19e22c62017-02-12 10:25:44 -0800246 {}
247};
248
Simon Glassaad29ae2020-12-03 16:55:21 -0700249static int stm32_serial_of_to_plat(struct udevice *dev)
Vikas Manocha19e22c62017-02-12 10:25:44 -0800250{
Simon Glassb75b15b2020-12-03 16:55:23 -0700251 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200252 fdt_addr_t addr;
Vikas Manocha19e22c62017-02-12 10:25:44 -0800253
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200254 addr = dev_read_addr(dev);
255 if (addr == FDT_ADDR_T_NONE)
Vikas Manocha19e22c62017-02-12 10:25:44 -0800256 return -EINVAL;
257
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200258 plat->base = (void __iomem *)addr;
259
Vikas Manocha19e22c62017-02-12 10:25:44 -0800260 return 0;
261}
Vikas Manocha19e22c62017-02-12 10:25:44 -0800262
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800263static const struct dm_serial_ops stm32_serial_ops = {
264 .putc = stm32_serial_putc,
265 .pending = stm32_serial_pending,
266 .getc = stm32_serial_getc,
267 .setbrg = stm32_serial_setbrg,
Patrice Chotard34e64c02018-08-03 15:07:39 +0200268 .setconfig = stm32_serial_setconfig
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800269};
270
271U_BOOT_DRIVER(serial_stm32) = {
Patrice Chotard9e276502018-01-12 09:23:49 +0100272 .name = "serial_stm32",
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800273 .id = UCLASS_SERIAL,
Vikas Manocha19e22c62017-02-12 10:25:44 -0800274 .of_match = of_match_ptr(stm32_serial_id),
Simon Glassaad29ae2020-12-03 16:55:21 -0700275 .of_to_plat = of_match_ptr(stm32_serial_of_to_plat),
Simon Glassb75b15b2020-12-03 16:55:23 -0700276 .plat_auto = sizeof(struct stm32x7_serial_plat),
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800277 .ops = &stm32_serial_ops,
278 .probe = stm32_serial_probe,
Bin Mengbdb33d82018-10-24 06:36:36 -0700279#if !CONFIG_IS_ENABLED(OF_CONTROL)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800280 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700281#endif
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800282};
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200283
284#ifdef CONFIG_DEBUG_UART_STM32
285#include <debug_uart.h>
286static inline struct stm32_uart_info *_debug_uart_info(void)
287{
288 struct stm32_uart_info *uart_info;
289
290#if defined(CONFIG_STM32F4)
291 uart_info = &stm32f4_info;
292#elif defined(CONFIG_STM32F7)
293 uart_info = &stm32f7_info;
294#else
295 uart_info = &stm32h7_info;
296#endif
297 return uart_info;
298}
299
300static inline void _debug_uart_init(void)
301{
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200302 void __iomem *base = (void __iomem *)CONFIG_VAL(DEBUG_UART_BASE);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200303 struct stm32_uart_info *uart_info = _debug_uart_info();
304
305 _stm32_serial_init(base, uart_info);
306 _stm32_serial_setbrg(base, uart_info,
307 CONFIG_DEBUG_UART_CLOCK,
308 CONFIG_BAUDRATE);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200309}
310
311static inline void _debug_uart_putc(int c)
312{
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200313 void __iomem *base = (void __iomem *)CONFIG_VAL(DEBUG_UART_BASE);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200314 struct stm32_uart_info *uart_info = _debug_uart_info();
315
316 while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN)
Patrick Delaunaye093b1c2019-04-18 17:32:51 +0200317 ;
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200318}
319
320DEBUG_UART_FUNCS
321#endif