blob: fb039546a41b73a3ba656e1ca12eaff3b628734d [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 Manocha5dba05e2016-02-11 15:47:19 -08009#include <common.h>
Vikas Manocha7b00ff92017-02-12 10:25:46 -080010#include <clk.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -080011#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Patrice Chotard4d6701e2018-12-04 14:11:36 +010013#include <reset.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -080014#include <serial.h>
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020015#include <watchdog.h>
16#include <asm/io.h>
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090017#include <asm/arch/stm32.h>
Patrick Delaunay80bd23b2020-11-06 19:01:56 +010018#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Patrice Chotarde6262b12023-05-31 08:01:30 +020021#include <linux/iopoll.h>
Patrice Chotard9e276502018-01-12 09:23:49 +010022#include "serial_stm32.h"
Simon Glass9bc15642020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -080024
Valentin Caronaa74b112023-08-04 16:09:04 +020025/*
26 * At 115200 bits/s
27 * 1 bit = 1 / 115200 = 8,68 us
28 * 8 bits = 69,444 us
29 * 10 bits are needed for worst case (8 bits + 1 start + 1 stop) = 86.806 us
30 */
31#define ONE_BYTE_B115200_US 87
32
Patrice Chotardf93f92b2023-10-27 16:43:01 +020033static void _stm32_serial_setbrg(void __iomem *base,
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020034 struct stm32_uart_info *uart_info,
35 u32 clock_rate,
36 int baudrate)
Vikas Manocha5dba05e2016-02-11 15:47:19 -080037{
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020038 bool stm32f4 = uart_info->stm32f4;
Patrice Chotard4809a192017-07-18 09:29:08 +020039 u32 int_div, mantissa, fraction, oversampling;
Patrice Chotard45bc3c42023-05-31 08:01:31 +020040 u8 uart_enable_bit = uart_info->uart_enable_bit;
41
42 /* BRR register must be set when uart is disabled */
43 clrbits_le32(base + CR1_OFFSET(stm32f4), BIT(uart_enable_bit));
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090044
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020045 int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate);
Patrice Chotard31496322017-06-08 09:26:55 +020046
47 if (int_div < 16) {
48 oversampling = 8;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020049 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020050 } else {
51 oversampling = 16;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020052 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020053 }
54
55 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
56 fraction = int_div % oversampling;
57
Patrice Chotard5011e6f2017-09-27 15:44:50 +020058 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
Patrice Chotard45bc3c42023-05-31 08:01:31 +020059
60 setbits_le32(base + CR1_OFFSET(stm32f4), BIT(uart_enable_bit));
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020061}
62
63static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
64{
Simon Glassb75b15b2020-12-03 16:55:23 -070065 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020066
67 _stm32_serial_setbrg(plat->base, plat->uart_info,
68 plat->clock_rate, baudrate);
Vikas Manocha5dba05e2016-02-11 15:47:19 -080069
70 return 0;
71}
72
Patrice Chotard34e64c02018-08-03 15:07:39 +020073static int stm32_serial_setconfig(struct udevice *dev, uint serial_config)
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020074{
Simon Glassb75b15b2020-12-03 16:55:23 -070075 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020076 bool stm32f4 = plat->uart_info->stm32f4;
77 u8 uart_enable_bit = plat->uart_info->uart_enable_bit;
Patrice Chotardf93f92b2023-10-27 16:43:01 +020078 void __iomem *cr1 = plat->base + CR1_OFFSET(stm32f4);
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020079 u32 config = 0;
Patrice Chotard34e64c02018-08-03 15:07:39 +020080 uint parity = SERIAL_GET_PARITY(serial_config);
81 uint bits = SERIAL_GET_BITS(serial_config);
82 uint stop = SERIAL_GET_STOP(serial_config);
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020083
Patrice Chotard34e64c02018-08-03 15:07:39 +020084 /*
85 * only parity config is implemented, check if other serial settings
86 * are the default one.
87 * (STM32F4 serial IP didn't support parity setting)
88 */
89 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || stm32f4)
90 return -ENOTSUPP; /* not supported in driver*/
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020091
92 clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
93 /* update usart configuration (uart need to be disable)
Patrice Chotard34e64c02018-08-03 15:07:39 +020094 * PCE: parity check enable
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020095 * PS : '0' : Even / '1' : Odd
96 * M[1:0] = '00' : 8 Data bits
97 * M[1:0] = '01' : 9 Data bits with parity
98 */
99 switch (parity) {
100 default:
101 case SERIAL_PAR_NONE:
102 config = 0;
103 break;
104 case SERIAL_PAR_ODD:
105 config = USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0;
106 break;
107 case SERIAL_PAR_EVEN:
108 config = USART_CR1_PCE | USART_CR1_M0;
109 break;
110 }
Patrice Chotard34e64c02018-08-03 15:07:39 +0200111
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +0200112 clrsetbits_le32(cr1,
113 USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 |
114 USART_CR1_M0,
115 config);
116 setbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
117
118 return 0;
119}
120
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800121static int stm32_serial_getc(struct udevice *dev)
122{
Simon Glassb75b15b2020-12-03 16:55:23 -0700123 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200124 bool stm32f4 = plat->uart_info->stm32f4;
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200125 void __iomem *base = plat->base;
Patrice Chotard24af24b2018-04-20 08:59:06 +0200126 u32 isr = readl(base + ISR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800127
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200128 if ((isr & USART_ISR_RXNE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800129 return -EAGAIN;
130
Patrick Delaunay1a103bf2019-07-30 19:16:46 +0200131 if (isr & (USART_ISR_PE | USART_ISR_ORE | USART_ISR_FE)) {
Patrice Chotard24af24b2018-04-20 08:59:06 +0200132 if (!stm32f4)
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +0200133 setbits_le32(base + ICR_OFFSET,
Patrick Delaunay1a103bf2019-07-30 19:16:46 +0200134 USART_ICR_PCECF | USART_ICR_ORECF |
135 USART_ICR_FECF);
Patrice Chotard24af24b2018-04-20 08:59:06 +0200136 else
137 readl(base + RDR_OFFSET(stm32f4));
138 return -EIO;
139 }
140
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200141 return readl(base + RDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800142}
143
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200144static int _stm32_serial_putc(void __iomem *base,
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200145 struct stm32_uart_info *uart_info,
146 const char c)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800147{
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200148 bool stm32f4 = uart_info->stm32f4;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800149
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200150 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800151 return -EAGAIN;
152
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200153 writel(c, base + TDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800154
155 return 0;
156}
157
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200158static int stm32_serial_putc(struct udevice *dev, const char c)
159{
Simon Glassb75b15b2020-12-03 16:55:23 -0700160 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200161
162 return _stm32_serial_putc(plat->base, plat->uart_info, c);
163}
164
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800165static int stm32_serial_pending(struct udevice *dev, bool input)
166{
Simon Glassb75b15b2020-12-03 16:55:23 -0700167 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200168 bool stm32f4 = plat->uart_info->stm32f4;
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200169 void __iomem *base = plat->base;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800170
171 if (input)
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200172 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200173 USART_ISR_RXNE ? 1 : 0;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800174 else
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200175 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200176 USART_ISR_TXE ? 0 : 1;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800177}
178
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200179static void _stm32_serial_init(void __iomem *base,
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200180 struct stm32_uart_info *uart_info)
181{
182 bool stm32f4 = uart_info->stm32f4;
183 u8 uart_enable_bit = uart_info->uart_enable_bit;
184
185 /* Disable uart-> enable fifo -> enable uart */
186 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
187 BIT(uart_enable_bit));
188 if (uart_info->has_fifo)
189 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN);
190 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
191 BIT(uart_enable_bit));
192}
193
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800194static int stm32_serial_probe(struct udevice *dev)
195{
Simon Glassb75b15b2020-12-03 16:55:23 -0700196 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard21aad132017-09-27 15:44:53 +0200197 struct clk clk;
Patrice Chotard4d6701e2018-12-04 14:11:36 +0100198 struct reset_ctl reset;
Patrice Chotarde6262b12023-05-31 08:01:30 +0200199 u32 isr;
Patrice Chotard21aad132017-09-27 15:44:53 +0200200 int ret;
Patrice Chotarde6262b12023-05-31 08:01:30 +0200201 bool stm32f4;
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200202
203 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
Patrice Chotarde6262b12023-05-31 08:01:30 +0200204 stm32f4 = plat->uart_info->stm32f4;
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800205
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800206 ret = clk_get_by_index(dev, 0, &clk);
207 if (ret < 0)
208 return ret;
209
210 ret = clk_enable(&clk);
211 if (ret) {
212 dev_err(dev, "failed to enable clock\n");
213 return ret;
214 }
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800215
Patrice Chotarde6262b12023-05-31 08:01:30 +0200216 /*
217 * before uart initialization, wait for TC bit (Transmission Complete)
218 * in case there is still chars from previous bootstage to transmit
219 */
Valentin Caronaa74b112023-08-04 16:09:04 +0200220 ret = read_poll_timeout(readl, isr, isr & USART_ISR_TC, 50,
221 16 * ONE_BYTE_B115200_US, plat->base + ISR_OFFSET(stm32f4));
222 if (ret)
223 dev_dbg(dev, "FIFO not empty, some character can be lost (%d)\n", ret);
Patrice Chotarde6262b12023-05-31 08:01:30 +0200224
Patrice Chotard4d6701e2018-12-04 14:11:36 +0100225 ret = reset_get_by_index(dev, 0, &reset);
226 if (!ret) {
227 reset_assert(&reset);
228 udelay(2);
229 reset_deassert(&reset);
230 }
231
Patrice Chotard4809a192017-07-18 09:29:08 +0200232 plat->clock_rate = clk_get_rate(&clk);
Patrick Delaunay3f4afd32019-06-21 15:26:41 +0200233 if (!plat->clock_rate) {
Patrice Chotard4809a192017-07-18 09:29:08 +0200234 clk_disable(&clk);
Patrick Delaunay3f4afd32019-06-21 15:26:41 +0200235 return -EINVAL;
Patrice Chotard4809a192017-07-18 09:29:08 +0200236 };
237
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200238 _stm32_serial_init(plat->base, plat->uart_info);
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800239
240 return 0;
241}
242
Vikas Manocha19e22c62017-02-12 10:25:44 -0800243static const struct udevice_id stm32_serial_id[] = {
Patrice Chotardb21a69a2017-09-27 15:44:52 +0200244 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
Patrice Chotard24fc72d2017-09-27 15:44:51 +0200245 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
246 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
Vikas Manocha19e22c62017-02-12 10:25:44 -0800247 {}
248};
249
Simon Glassaad29ae2020-12-03 16:55:21 -0700250static int stm32_serial_of_to_plat(struct udevice *dev)
Vikas Manocha19e22c62017-02-12 10:25:44 -0800251{
Simon Glassb75b15b2020-12-03 16:55:23 -0700252 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200253 fdt_addr_t addr;
Vikas Manocha19e22c62017-02-12 10:25:44 -0800254
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200255 addr = dev_read_addr(dev);
256 if (addr == FDT_ADDR_T_NONE)
Vikas Manocha19e22c62017-02-12 10:25:44 -0800257 return -EINVAL;
258
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200259 plat->base = (void __iomem *)addr;
260
Vikas Manocha19e22c62017-02-12 10:25:44 -0800261 return 0;
262}
Vikas Manocha19e22c62017-02-12 10:25:44 -0800263
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800264static const struct dm_serial_ops stm32_serial_ops = {
265 .putc = stm32_serial_putc,
266 .pending = stm32_serial_pending,
267 .getc = stm32_serial_getc,
268 .setbrg = stm32_serial_setbrg,
Patrice Chotard34e64c02018-08-03 15:07:39 +0200269 .setconfig = stm32_serial_setconfig
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800270};
271
272U_BOOT_DRIVER(serial_stm32) = {
Patrice Chotard9e276502018-01-12 09:23:49 +0100273 .name = "serial_stm32",
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800274 .id = UCLASS_SERIAL,
Vikas Manocha19e22c62017-02-12 10:25:44 -0800275 .of_match = of_match_ptr(stm32_serial_id),
Simon Glassaad29ae2020-12-03 16:55:21 -0700276 .of_to_plat = of_match_ptr(stm32_serial_of_to_plat),
Simon Glassb75b15b2020-12-03 16:55:23 -0700277 .plat_auto = sizeof(struct stm32x7_serial_plat),
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800278 .ops = &stm32_serial_ops,
279 .probe = stm32_serial_probe,
Bin Mengbdb33d82018-10-24 06:36:36 -0700280#if !CONFIG_IS_ENABLED(OF_CONTROL)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800281 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700282#endif
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800283};
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200284
285#ifdef CONFIG_DEBUG_UART_STM32
286#include <debug_uart.h>
287static inline struct stm32_uart_info *_debug_uart_info(void)
288{
289 struct stm32_uart_info *uart_info;
290
291#if defined(CONFIG_STM32F4)
292 uart_info = &stm32f4_info;
293#elif defined(CONFIG_STM32F7)
294 uart_info = &stm32f7_info;
295#else
296 uart_info = &stm32h7_info;
297#endif
298 return uart_info;
299}
300
301static inline void _debug_uart_init(void)
302{
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200303 void __iomem *base = (void __iomem *)CONFIG_VAL(DEBUG_UART_BASE);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200304 struct stm32_uart_info *uart_info = _debug_uart_info();
305
306 _stm32_serial_init(base, uart_info);
307 _stm32_serial_setbrg(base, uart_info,
308 CONFIG_DEBUG_UART_CLOCK,
309 CONFIG_BAUDRATE);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200310}
311
312static inline void _debug_uart_putc(int c)
313{
Patrice Chotardf93f92b2023-10-27 16:43:01 +0200314 void __iomem *base = (void __iomem *)CONFIG_VAL(DEBUG_UART_BASE);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200315 struct stm32_uart_info *uart_info = _debug_uart_info();
316
317 while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN)
Patrick Delaunaye093b1c2019-04-18 17:32:51 +0200318 ;
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200319}
320
321DEBUG_UART_FUNCS
322#endif