blob: 23d476fba28356b11febe2e2a840efc9235f6e41 [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
Patrick Delaunaya3ce8d62020-11-06 19:01:56 +01007#define LOG_CATEGORY UCLASS_SERIAL
8
Vikas Manocha6a12ceb2016-02-11 15:47:19 -08009#include <common.h>
Vikas Manochafd03b832017-02-12 10:25:46 -080010#include <clk.h>
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080011#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Patrice Chotardf828fa42018-12-04 14:11:36 +010013#include <reset.h>
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080014#include <serial.h>
Patrick Delaunay215c8be2018-05-17 14:50:42 +020015#include <watchdog.h>
16#include <asm/io.h>
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090017#include <asm/arch/stm32.h>
Patrick Delaunaya3ce8d62020-11-06 19:01:56 +010018#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060019#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060020#include <linux/delay.h>
Patrice Chotardb4dbc5d2023-05-31 08:01:30 +020021#include <linux/iopoll.h>
Patrice Chotardae74de02018-01-12 09:23:49 +010022#include "serial_stm32.h"
Simon Glass336d4612020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080024
Valentin Caron9e8cbea2023-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
Patrick Delaunay215c8be2018-05-17 14:50:42 +020033static void _stm32_serial_setbrg(fdt_addr_t base,
34 struct stm32_uart_info *uart_info,
35 u32 clock_rate,
36 int baudrate)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080037{
Patrick Delaunay215c8be2018-05-17 14:50:42 +020038 bool stm32f4 = uart_info->stm32f4;
Patrice Chotard27265ce2017-07-18 09:29:08 +020039 u32 int_div, mantissa, fraction, oversampling;
Patrice Chotard8ab9e8f2023-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 NISHINAGAba0a3c12016-07-08 01:02:24 +090044
Patrick Delaunay215c8be2018-05-17 14:50:42 +020045 int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020046
47 if (int_div < 16) {
48 oversampling = 8;
Patrice Chotard60a996b2017-09-27 15:44:50 +020049 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020050 } else {
51 oversampling = 16;
Patrice Chotard60a996b2017-09-27 15:44:50 +020052 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020053 }
54
55 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
56 fraction = int_div % oversampling;
57
Patrice Chotard60a996b2017-09-27 15:44:50 +020058 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
Patrice Chotard8ab9e8f2023-05-31 08:01:31 +020059
60 setbits_le32(base + CR1_OFFSET(stm32f4), BIT(uart_enable_bit));
Patrick Delaunay215c8be2018-05-17 14:50:42 +020061}
62
63static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
64{
Simon Glass8a8d24b2020-12-03 16:55:23 -070065 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunay215c8be2018-05-17 14:50:42 +020066
67 _stm32_serial_setbrg(plat->base, plat->uart_info,
68 plat->clock_rate, baudrate);
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080069
70 return 0;
71}
72
Patrice Chotardfbd5c722018-08-03 15:07:39 +020073static int stm32_serial_setconfig(struct udevice *dev, uint serial_config)
Patrick Delaunaybc709a42018-05-17 14:50:45 +020074{
Simon Glass8a8d24b2020-12-03 16:55:23 -070075 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunaybc709a42018-05-17 14:50:45 +020076 bool stm32f4 = plat->uart_info->stm32f4;
77 u8 uart_enable_bit = plat->uart_info->uart_enable_bit;
78 u32 cr1 = plat->base + CR1_OFFSET(stm32f4);
79 u32 config = 0;
Patrice Chotardfbd5c722018-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 Delaunaybc709a42018-05-17 14:50:45 +020083
Patrice Chotardfbd5c722018-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 Delaunaybc709a42018-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 Chotardfbd5c722018-08-03 15:07:39 +020094 * PCE: parity check enable
Patrick Delaunaybc709a42018-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 Chotardfbd5c722018-08-03 15:07:39 +0200111
Patrick Delaunaybc709a42018-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 Manocha6a12ceb2016-02-11 15:47:19 -0800121static int stm32_serial_getc(struct udevice *dev)
122{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700123 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard60a996b2017-09-27 15:44:50 +0200124 bool stm32f4 = plat->uart_info->stm32f4;
125 fdt_addr_t base = plat->base;
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200126 u32 isr = readl(base + ISR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800127
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200128 if ((isr & USART_ISR_RXNE) == 0)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800129 return -EAGAIN;
130
Patrick Delaunay132518f2019-07-30 19:16:46 +0200131 if (isr & (USART_ISR_PE | USART_ISR_ORE | USART_ISR_FE)) {
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200132 if (!stm32f4)
Patrick Delaunaybc709a42018-05-17 14:50:45 +0200133 setbits_le32(base + ICR_OFFSET,
Patrick Delaunay132518f2019-07-30 19:16:46 +0200134 USART_ICR_PCECF | USART_ICR_ORECF |
135 USART_ICR_FECF);
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200136 else
137 readl(base + RDR_OFFSET(stm32f4));
138 return -EIO;
139 }
140
Patrice Chotard60a996b2017-09-27 15:44:50 +0200141 return readl(base + RDR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800142}
143
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200144static int _stm32_serial_putc(fdt_addr_t base,
145 struct stm32_uart_info *uart_info,
146 const char c)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800147{
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200148 bool stm32f4 = uart_info->stm32f4;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800149
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200150 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800151 return -EAGAIN;
152
Patrice Chotard60a996b2017-09-27 15:44:50 +0200153 writel(c, base + TDR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800154
155 return 0;
156}
157
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200158static int stm32_serial_putc(struct udevice *dev, const char c)
159{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700160 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200161
162 return _stm32_serial_putc(plat->base, plat->uart_info, c);
163}
164
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800165static int stm32_serial_pending(struct udevice *dev, bool input)
166{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700167 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard60a996b2017-09-27 15:44:50 +0200168 bool stm32f4 = plat->uart_info->stm32f4;
169 fdt_addr_t base = plat->base;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800170
171 if (input)
Patrice Chotard60a996b2017-09-27 15:44:50 +0200172 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200173 USART_ISR_RXNE ? 1 : 0;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800174 else
Patrice Chotard60a996b2017-09-27 15:44:50 +0200175 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200176 USART_ISR_TXE ? 0 : 1;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800177}
178
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200179static void _stm32_serial_init(fdt_addr_t base,
180 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 Manocha6a12ceb2016-02-11 15:47:19 -0800194static int stm32_serial_probe(struct udevice *dev)
195{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700196 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard9a212d72017-09-27 15:44:53 +0200197 struct clk clk;
Patrice Chotardf828fa42018-12-04 14:11:36 +0100198 struct reset_ctl reset;
Patrice Chotardb4dbc5d2023-05-31 08:01:30 +0200199 u32 isr;
Patrice Chotard9a212d72017-09-27 15:44:53 +0200200 int ret;
Patrice Chotardb4dbc5d2023-05-31 08:01:30 +0200201 bool stm32f4;
Patrice Chotard60a996b2017-09-27 15:44:50 +0200202
203 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
Patrice Chotardb4dbc5d2023-05-31 08:01:30 +0200204 stm32f4 = plat->uart_info->stm32f4;
Vikas Manochafd03b832017-02-12 10:25:46 -0800205
Vikas Manochafd03b832017-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 Manochafd03b832017-02-12 10:25:46 -0800215
Patrice Chotardb4dbc5d2023-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 Caron9e8cbea2023-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 Chotardb4dbc5d2023-05-31 08:01:30 +0200224
Patrice Chotardf828fa42018-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 Chotard27265ce2017-07-18 09:29:08 +0200232 plat->clock_rate = clk_get_rate(&clk);
Patrick Delaunay585289b2019-06-21 15:26:41 +0200233 if (!plat->clock_rate) {
Patrice Chotard27265ce2017-07-18 09:29:08 +0200234 clk_disable(&clk);
Patrick Delaunay585289b2019-06-21 15:26:41 +0200235 return -EINVAL;
Patrice Chotard27265ce2017-07-18 09:29:08 +0200236 };
237
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200238 _stm32_serial_init(plat->base, plat->uart_info);
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800239
240 return 0;
241}
242
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800243static const struct udevice_id stm32_serial_id[] = {
Patrice Chotard6c30f152017-09-27 15:44:52 +0200244 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
Patrice Chotard2a7ecc52017-09-27 15:44:51 +0200245 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
246 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800247 {}
248};
249
Simon Glassd1998a92020-12-03 16:55:21 -0700250static int stm32_serial_of_to_plat(struct udevice *dev)
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800251{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700252 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800253
Masahiro Yamada25484932020-07-17 14:36:48 +0900254 plat->base = dev_read_addr(dev);
Patrice Chotard60a996b2017-09-27 15:44:50 +0200255 if (plat->base == FDT_ADDR_T_NONE)
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800256 return -EINVAL;
257
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800258 return 0;
259}
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800260
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800261static const struct dm_serial_ops stm32_serial_ops = {
262 .putc = stm32_serial_putc,
263 .pending = stm32_serial_pending,
264 .getc = stm32_serial_getc,
265 .setbrg = stm32_serial_setbrg,
Patrice Chotardfbd5c722018-08-03 15:07:39 +0200266 .setconfig = stm32_serial_setconfig
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800267};
268
269U_BOOT_DRIVER(serial_stm32) = {
Patrice Chotardae74de02018-01-12 09:23:49 +0100270 .name = "serial_stm32",
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800271 .id = UCLASS_SERIAL,
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800272 .of_match = of_match_ptr(stm32_serial_id),
Simon Glassd1998a92020-12-03 16:55:21 -0700273 .of_to_plat = of_match_ptr(stm32_serial_of_to_plat),
Simon Glass8a8d24b2020-12-03 16:55:23 -0700274 .plat_auto = sizeof(struct stm32x7_serial_plat),
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800275 .ops = &stm32_serial_ops,
276 .probe = stm32_serial_probe,
Bin Meng46879192018-10-24 06:36:36 -0700277#if !CONFIG_IS_ENABLED(OF_CONTROL)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800278 .flags = DM_FLAG_PRE_RELOC,
Bin Meng46879192018-10-24 06:36:36 -0700279#endif
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800280};
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200281
282#ifdef CONFIG_DEBUG_UART_STM32
283#include <debug_uart.h>
284static inline struct stm32_uart_info *_debug_uart_info(void)
285{
286 struct stm32_uart_info *uart_info;
287
288#if defined(CONFIG_STM32F4)
289 uart_info = &stm32f4_info;
290#elif defined(CONFIG_STM32F7)
291 uart_info = &stm32f7_info;
292#else
293 uart_info = &stm32h7_info;
294#endif
295 return uart_info;
296}
297
298static inline void _debug_uart_init(void)
299{
Pali Rohárb62450c2022-05-27 22:15:24 +0200300 fdt_addr_t base = CONFIG_VAL(DEBUG_UART_BASE);
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200301 struct stm32_uart_info *uart_info = _debug_uart_info();
302
303 _stm32_serial_init(base, uart_info);
304 _stm32_serial_setbrg(base, uart_info,
305 CONFIG_DEBUG_UART_CLOCK,
306 CONFIG_BAUDRATE);
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200307}
308
309static inline void _debug_uart_putc(int c)
310{
Pali Rohárb62450c2022-05-27 22:15:24 +0200311 fdt_addr_t base = CONFIG_VAL(DEBUG_UART_BASE);
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200312 struct stm32_uart_info *uart_info = _debug_uart_info();
313
314 while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN)
Patrick Delaunay66dba9a2019-04-18 17:32:51 +0200315 ;
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200316}
317
318DEBUG_UART_FUNCS
319#endif