blob: 0085113f6746a4cfc2977bc07458dbeb91d50e12 [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
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020025static void _stm32_serial_setbrg(fdt_addr_t base,
26 struct stm32_uart_info *uart_info,
27 u32 clock_rate,
28 int baudrate)
Vikas Manocha5dba05e2016-02-11 15:47:19 -080029{
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020030 bool stm32f4 = uart_info->stm32f4;
Patrice Chotard4809a192017-07-18 09:29:08 +020031 u32 int_div, mantissa, fraction, oversampling;
Patrice Chotard45bc3c42023-05-31 08:01:31 +020032 u8 uart_enable_bit = uart_info->uart_enable_bit;
33
34 /* BRR register must be set when uart is disabled */
35 clrbits_le32(base + CR1_OFFSET(stm32f4), BIT(uart_enable_bit));
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090036
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020037 int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate);
Patrice Chotard31496322017-06-08 09:26:55 +020038
39 if (int_div < 16) {
40 oversampling = 8;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020041 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020042 } else {
43 oversampling = 16;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020044 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020045 }
46
47 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
48 fraction = int_div % oversampling;
49
Patrice Chotard5011e6f2017-09-27 15:44:50 +020050 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
Patrice Chotard45bc3c42023-05-31 08:01:31 +020051
52 setbits_le32(base + CR1_OFFSET(stm32f4), BIT(uart_enable_bit));
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020053}
54
55static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
56{
Simon Glassb75b15b2020-12-03 16:55:23 -070057 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020058
59 _stm32_serial_setbrg(plat->base, plat->uart_info,
60 plat->clock_rate, baudrate);
Vikas Manocha5dba05e2016-02-11 15:47:19 -080061
62 return 0;
63}
64
Patrice Chotard34e64c02018-08-03 15:07:39 +020065static int stm32_serial_setconfig(struct udevice *dev, uint serial_config)
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020066{
Simon Glassb75b15b2020-12-03 16:55:23 -070067 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020068 bool stm32f4 = plat->uart_info->stm32f4;
69 u8 uart_enable_bit = plat->uart_info->uart_enable_bit;
70 u32 cr1 = plat->base + CR1_OFFSET(stm32f4);
71 u32 config = 0;
Patrice Chotard34e64c02018-08-03 15:07:39 +020072 uint parity = SERIAL_GET_PARITY(serial_config);
73 uint bits = SERIAL_GET_BITS(serial_config);
74 uint stop = SERIAL_GET_STOP(serial_config);
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020075
Patrice Chotard34e64c02018-08-03 15:07:39 +020076 /*
77 * only parity config is implemented, check if other serial settings
78 * are the default one.
79 * (STM32F4 serial IP didn't support parity setting)
80 */
81 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || stm32f4)
82 return -ENOTSUPP; /* not supported in driver*/
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020083
84 clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
85 /* update usart configuration (uart need to be disable)
Patrice Chotard34e64c02018-08-03 15:07:39 +020086 * PCE: parity check enable
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020087 * PS : '0' : Even / '1' : Odd
88 * M[1:0] = '00' : 8 Data bits
89 * M[1:0] = '01' : 9 Data bits with parity
90 */
91 switch (parity) {
92 default:
93 case SERIAL_PAR_NONE:
94 config = 0;
95 break;
96 case SERIAL_PAR_ODD:
97 config = USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0;
98 break;
99 case SERIAL_PAR_EVEN:
100 config = USART_CR1_PCE | USART_CR1_M0;
101 break;
102 }
Patrice Chotard34e64c02018-08-03 15:07:39 +0200103
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +0200104 clrsetbits_le32(cr1,
105 USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 |
106 USART_CR1_M0,
107 config);
108 setbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
109
110 return 0;
111}
112
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800113static int stm32_serial_getc(struct udevice *dev)
114{
Simon Glassb75b15b2020-12-03 16:55:23 -0700115 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200116 bool stm32f4 = plat->uart_info->stm32f4;
117 fdt_addr_t base = plat->base;
Patrice Chotard24af24b2018-04-20 08:59:06 +0200118 u32 isr = readl(base + ISR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800119
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200120 if ((isr & USART_ISR_RXNE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800121 return -EAGAIN;
122
Patrick Delaunay1a103bf2019-07-30 19:16:46 +0200123 if (isr & (USART_ISR_PE | USART_ISR_ORE | USART_ISR_FE)) {
Patrice Chotard24af24b2018-04-20 08:59:06 +0200124 if (!stm32f4)
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +0200125 setbits_le32(base + ICR_OFFSET,
Patrick Delaunay1a103bf2019-07-30 19:16:46 +0200126 USART_ICR_PCECF | USART_ICR_ORECF |
127 USART_ICR_FECF);
Patrice Chotard24af24b2018-04-20 08:59:06 +0200128 else
129 readl(base + RDR_OFFSET(stm32f4));
130 return -EIO;
131 }
132
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200133 return readl(base + RDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800134}
135
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200136static int _stm32_serial_putc(fdt_addr_t base,
137 struct stm32_uart_info *uart_info,
138 const char c)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800139{
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200140 bool stm32f4 = uart_info->stm32f4;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800141
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200142 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800143 return -EAGAIN;
144
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200145 writel(c, base + TDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800146
147 return 0;
148}
149
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200150static int stm32_serial_putc(struct udevice *dev, const char c)
151{
Simon Glassb75b15b2020-12-03 16:55:23 -0700152 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200153
154 return _stm32_serial_putc(plat->base, plat->uart_info, c);
155}
156
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800157static int stm32_serial_pending(struct udevice *dev, bool input)
158{
Simon Glassb75b15b2020-12-03 16:55:23 -0700159 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200160 bool stm32f4 = plat->uart_info->stm32f4;
161 fdt_addr_t base = plat->base;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800162
163 if (input)
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200164 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200165 USART_ISR_RXNE ? 1 : 0;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800166 else
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200167 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200168 USART_ISR_TXE ? 0 : 1;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800169}
170
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200171static void _stm32_serial_init(fdt_addr_t base,
172 struct stm32_uart_info *uart_info)
173{
174 bool stm32f4 = uart_info->stm32f4;
175 u8 uart_enable_bit = uart_info->uart_enable_bit;
176
177 /* Disable uart-> enable fifo -> enable uart */
178 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
179 BIT(uart_enable_bit));
180 if (uart_info->has_fifo)
181 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN);
182 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
183 BIT(uart_enable_bit));
184}
185
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800186static int stm32_serial_probe(struct udevice *dev)
187{
Simon Glassb75b15b2020-12-03 16:55:23 -0700188 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Patrice Chotard21aad132017-09-27 15:44:53 +0200189 struct clk clk;
Patrice Chotard4d6701e2018-12-04 14:11:36 +0100190 struct reset_ctl reset;
Patrice Chotarde6262b12023-05-31 08:01:30 +0200191 u32 isr;
Patrice Chotard21aad132017-09-27 15:44:53 +0200192 int ret;
Patrice Chotarde6262b12023-05-31 08:01:30 +0200193 bool stm32f4;
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200194
195 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
Patrice Chotarde6262b12023-05-31 08:01:30 +0200196 stm32f4 = plat->uart_info->stm32f4;
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800197
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800198 ret = clk_get_by_index(dev, 0, &clk);
199 if (ret < 0)
200 return ret;
201
202 ret = clk_enable(&clk);
203 if (ret) {
204 dev_err(dev, "failed to enable clock\n");
205 return ret;
206 }
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800207
Patrice Chotarde6262b12023-05-31 08:01:30 +0200208 /*
209 * before uart initialization, wait for TC bit (Transmission Complete)
210 * in case there is still chars from previous bootstage to transmit
211 */
212 ret = read_poll_timeout(readl, isr, isr & USART_ISR_TC, 10, 150,
213 plat->base + ISR_OFFSET(stm32f4));
214 if (ret) {
215 clk_disable(&clk);
216 return ret;
217 }
218
Patrice Chotard4d6701e2018-12-04 14:11:36 +0100219 ret = reset_get_by_index(dev, 0, &reset);
220 if (!ret) {
221 reset_assert(&reset);
222 udelay(2);
223 reset_deassert(&reset);
224 }
225
Patrice Chotard4809a192017-07-18 09:29:08 +0200226 plat->clock_rate = clk_get_rate(&clk);
Patrick Delaunay3f4afd32019-06-21 15:26:41 +0200227 if (!plat->clock_rate) {
Patrice Chotard4809a192017-07-18 09:29:08 +0200228 clk_disable(&clk);
Patrick Delaunay3f4afd32019-06-21 15:26:41 +0200229 return -EINVAL;
Patrice Chotard4809a192017-07-18 09:29:08 +0200230 };
231
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200232 _stm32_serial_init(plat->base, plat->uart_info);
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800233
234 return 0;
235}
236
Vikas Manocha19e22c62017-02-12 10:25:44 -0800237static const struct udevice_id stm32_serial_id[] = {
Patrice Chotardb21a69a2017-09-27 15:44:52 +0200238 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
Patrice Chotard24fc72d2017-09-27 15:44:51 +0200239 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
240 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
Vikas Manocha19e22c62017-02-12 10:25:44 -0800241 {}
242};
243
Simon Glassaad29ae2020-12-03 16:55:21 -0700244static int stm32_serial_of_to_plat(struct udevice *dev)
Vikas Manocha19e22c62017-02-12 10:25:44 -0800245{
Simon Glassb75b15b2020-12-03 16:55:23 -0700246 struct stm32x7_serial_plat *plat = dev_get_plat(dev);
Vikas Manocha19e22c62017-02-12 10:25:44 -0800247
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900248 plat->base = dev_read_addr(dev);
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200249 if (plat->base == FDT_ADDR_T_NONE)
Vikas Manocha19e22c62017-02-12 10:25:44 -0800250 return -EINVAL;
251
Vikas Manocha19e22c62017-02-12 10:25:44 -0800252 return 0;
253}
Vikas Manocha19e22c62017-02-12 10:25:44 -0800254
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800255static const struct dm_serial_ops stm32_serial_ops = {
256 .putc = stm32_serial_putc,
257 .pending = stm32_serial_pending,
258 .getc = stm32_serial_getc,
259 .setbrg = stm32_serial_setbrg,
Patrice Chotard34e64c02018-08-03 15:07:39 +0200260 .setconfig = stm32_serial_setconfig
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800261};
262
263U_BOOT_DRIVER(serial_stm32) = {
Patrice Chotard9e276502018-01-12 09:23:49 +0100264 .name = "serial_stm32",
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800265 .id = UCLASS_SERIAL,
Vikas Manocha19e22c62017-02-12 10:25:44 -0800266 .of_match = of_match_ptr(stm32_serial_id),
Simon Glassaad29ae2020-12-03 16:55:21 -0700267 .of_to_plat = of_match_ptr(stm32_serial_of_to_plat),
Simon Glassb75b15b2020-12-03 16:55:23 -0700268 .plat_auto = sizeof(struct stm32x7_serial_plat),
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800269 .ops = &stm32_serial_ops,
270 .probe = stm32_serial_probe,
Bin Mengbdb33d82018-10-24 06:36:36 -0700271#if !CONFIG_IS_ENABLED(OF_CONTROL)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800272 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700273#endif
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800274};
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200275
276#ifdef CONFIG_DEBUG_UART_STM32
277#include <debug_uart.h>
278static inline struct stm32_uart_info *_debug_uart_info(void)
279{
280 struct stm32_uart_info *uart_info;
281
282#if defined(CONFIG_STM32F4)
283 uart_info = &stm32f4_info;
284#elif defined(CONFIG_STM32F7)
285 uart_info = &stm32f7_info;
286#else
287 uart_info = &stm32h7_info;
288#endif
289 return uart_info;
290}
291
292static inline void _debug_uart_init(void)
293{
Pali Rohár8864b352022-05-27 22:15:24 +0200294 fdt_addr_t base = CONFIG_VAL(DEBUG_UART_BASE);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200295 struct stm32_uart_info *uart_info = _debug_uart_info();
296
297 _stm32_serial_init(base, uart_info);
298 _stm32_serial_setbrg(base, uart_info,
299 CONFIG_DEBUG_UART_CLOCK,
300 CONFIG_BAUDRATE);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200301}
302
303static inline void _debug_uart_putc(int c)
304{
Pali Rohár8864b352022-05-27 22:15:24 +0200305 fdt_addr_t base = CONFIG_VAL(DEBUG_UART_BASE);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200306 struct stm32_uart_info *uart_info = _debug_uart_info();
307
308 while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN)
Patrick Delaunaye093b1c2019-04-18 17:32:51 +0200309 ;
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200310}
311
312DEBUG_UART_FUNCS
313#endif