blob: 016082814f6a7be333ebd6249fc036bf876c4e60 [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
7#include <common.h>
Vikas Manocha7b00ff92017-02-12 10:25:46 -08008#include <clk.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -08009#include <dm.h>
Patrice Chotard4d6701e2018-12-04 14:11:36 +010010#include <reset.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -080011#include <serial.h>
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020012#include <watchdog.h>
13#include <asm/io.h>
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090014#include <asm/arch/stm32.h>
Patrice Chotard9e276502018-01-12 09:23:49 +010015#include "serial_stm32.h"
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -080017
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020018static void _stm32_serial_setbrg(fdt_addr_t base,
19 struct stm32_uart_info *uart_info,
20 u32 clock_rate,
21 int baudrate)
Vikas Manocha5dba05e2016-02-11 15:47:19 -080022{
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020023 bool stm32f4 = uart_info->stm32f4;
Patrice Chotard4809a192017-07-18 09:29:08 +020024 u32 int_div, mantissa, fraction, oversampling;
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090025
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020026 int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate);
Patrice Chotard31496322017-06-08 09:26:55 +020027
28 if (int_div < 16) {
29 oversampling = 8;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020030 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020031 } else {
32 oversampling = 16;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020033 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020034 }
35
36 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
37 fraction = int_div % oversampling;
38
Patrice Chotard5011e6f2017-09-27 15:44:50 +020039 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
Patrick Delaunayab8e5d22018-05-17 14:50:42 +020040}
41
42static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
43{
44 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
45
46 _stm32_serial_setbrg(plat->base, plat->uart_info,
47 plat->clock_rate, baudrate);
Vikas Manocha5dba05e2016-02-11 15:47:19 -080048
49 return 0;
50}
51
Patrice Chotard34e64c02018-08-03 15:07:39 +020052static int stm32_serial_setconfig(struct udevice *dev, uint serial_config)
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020053{
54 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
55 bool stm32f4 = plat->uart_info->stm32f4;
56 u8 uart_enable_bit = plat->uart_info->uart_enable_bit;
57 u32 cr1 = plat->base + CR1_OFFSET(stm32f4);
58 u32 config = 0;
Patrice Chotard34e64c02018-08-03 15:07:39 +020059 uint parity = SERIAL_GET_PARITY(serial_config);
60 uint bits = SERIAL_GET_BITS(serial_config);
61 uint stop = SERIAL_GET_STOP(serial_config);
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020062
Patrice Chotard34e64c02018-08-03 15:07:39 +020063 /*
64 * only parity config is implemented, check if other serial settings
65 * are the default one.
66 * (STM32F4 serial IP didn't support parity setting)
67 */
68 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || stm32f4)
69 return -ENOTSUPP; /* not supported in driver*/
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020070
71 clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
72 /* update usart configuration (uart need to be disable)
Patrice Chotard34e64c02018-08-03 15:07:39 +020073 * PCE: parity check enable
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020074 * PS : '0' : Even / '1' : Odd
75 * M[1:0] = '00' : 8 Data bits
76 * M[1:0] = '01' : 9 Data bits with parity
77 */
78 switch (parity) {
79 default:
80 case SERIAL_PAR_NONE:
81 config = 0;
82 break;
83 case SERIAL_PAR_ODD:
84 config = USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0;
85 break;
86 case SERIAL_PAR_EVEN:
87 config = USART_CR1_PCE | USART_CR1_M0;
88 break;
89 }
Patrice Chotard34e64c02018-08-03 15:07:39 +020090
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +020091 clrsetbits_le32(cr1,
92 USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 |
93 USART_CR1_M0,
94 config);
95 setbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
96
97 return 0;
98}
99
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800100static int stm32_serial_getc(struct udevice *dev)
101{
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200102 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
103 bool stm32f4 = plat->uart_info->stm32f4;
104 fdt_addr_t base = plat->base;
Patrice Chotard24af24b2018-04-20 08:59:06 +0200105 u32 isr = readl(base + ISR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800106
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200107 if ((isr & USART_ISR_RXNE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800108 return -EAGAIN;
109
Patrick Delaunay1a103bf2019-07-30 19:16:46 +0200110 if (isr & (USART_ISR_PE | USART_ISR_ORE | USART_ISR_FE)) {
Patrice Chotard24af24b2018-04-20 08:59:06 +0200111 if (!stm32f4)
Patrick Delaunay39ffe0e2018-05-17 14:50:45 +0200112 setbits_le32(base + ICR_OFFSET,
Patrick Delaunay1a103bf2019-07-30 19:16:46 +0200113 USART_ICR_PCECF | USART_ICR_ORECF |
114 USART_ICR_FECF);
Patrice Chotard24af24b2018-04-20 08:59:06 +0200115 else
116 readl(base + RDR_OFFSET(stm32f4));
117 return -EIO;
118 }
119
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200120 return readl(base + RDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800121}
122
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200123static int _stm32_serial_putc(fdt_addr_t base,
124 struct stm32_uart_info *uart_info,
125 const char c)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800126{
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200127 bool stm32f4 = uart_info->stm32f4;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800128
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200129 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800130 return -EAGAIN;
131
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200132 writel(c, base + TDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800133
134 return 0;
135}
136
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200137static int stm32_serial_putc(struct udevice *dev, const char c)
138{
139 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
140
141 return _stm32_serial_putc(plat->base, plat->uart_info, c);
142}
143
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800144static int stm32_serial_pending(struct udevice *dev, bool input)
145{
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200146 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
147 bool stm32f4 = plat->uart_info->stm32f4;
148 fdt_addr_t base = plat->base;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800149
150 if (input)
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200151 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200152 USART_ISR_RXNE ? 1 : 0;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800153 else
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200154 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotarddb0536e2018-05-17 14:50:43 +0200155 USART_ISR_TXE ? 0 : 1;
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800156}
157
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200158static void _stm32_serial_init(fdt_addr_t base,
159 struct stm32_uart_info *uart_info)
160{
161 bool stm32f4 = uart_info->stm32f4;
162 u8 uart_enable_bit = uart_info->uart_enable_bit;
163
164 /* Disable uart-> enable fifo -> enable uart */
165 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
166 BIT(uart_enable_bit));
167 if (uart_info->has_fifo)
168 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN);
169 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
170 BIT(uart_enable_bit));
171}
172
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800173static int stm32_serial_probe(struct udevice *dev)
174{
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200175 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Patrice Chotard21aad132017-09-27 15:44:53 +0200176 struct clk clk;
Patrice Chotard4d6701e2018-12-04 14:11:36 +0100177 struct reset_ctl reset;
Patrice Chotard21aad132017-09-27 15:44:53 +0200178 int ret;
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200179
180 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800181
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800182 ret = clk_get_by_index(dev, 0, &clk);
183 if (ret < 0)
184 return ret;
185
186 ret = clk_enable(&clk);
187 if (ret) {
188 dev_err(dev, "failed to enable clock\n");
189 return ret;
190 }
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800191
Patrice Chotard4d6701e2018-12-04 14:11:36 +0100192 ret = reset_get_by_index(dev, 0, &reset);
193 if (!ret) {
194 reset_assert(&reset);
195 udelay(2);
196 reset_deassert(&reset);
197 }
198
Patrice Chotard4809a192017-07-18 09:29:08 +0200199 plat->clock_rate = clk_get_rate(&clk);
Patrick Delaunay3f4afd32019-06-21 15:26:41 +0200200 if (!plat->clock_rate) {
Patrice Chotard4809a192017-07-18 09:29:08 +0200201 clk_disable(&clk);
Patrick Delaunay3f4afd32019-06-21 15:26:41 +0200202 return -EINVAL;
Patrice Chotard4809a192017-07-18 09:29:08 +0200203 };
204
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200205 _stm32_serial_init(plat->base, plat->uart_info);
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800206
207 return 0;
208}
209
Vikas Manocha19e22c62017-02-12 10:25:44 -0800210static const struct udevice_id stm32_serial_id[] = {
Patrice Chotardb21a69a2017-09-27 15:44:52 +0200211 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
Patrice Chotard24fc72d2017-09-27 15:44:51 +0200212 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
213 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
Vikas Manocha19e22c62017-02-12 10:25:44 -0800214 {}
215};
216
217static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
218{
219 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Vikas Manocha19e22c62017-02-12 10:25:44 -0800220
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200221 plat->base = devfdt_get_addr(dev);
222 if (plat->base == FDT_ADDR_T_NONE)
Vikas Manocha19e22c62017-02-12 10:25:44 -0800223 return -EINVAL;
224
Vikas Manocha19e22c62017-02-12 10:25:44 -0800225 return 0;
226}
Vikas Manocha19e22c62017-02-12 10:25:44 -0800227
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800228static const struct dm_serial_ops stm32_serial_ops = {
229 .putc = stm32_serial_putc,
230 .pending = stm32_serial_pending,
231 .getc = stm32_serial_getc,
232 .setbrg = stm32_serial_setbrg,
Patrice Chotard34e64c02018-08-03 15:07:39 +0200233 .setconfig = stm32_serial_setconfig
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800234};
235
236U_BOOT_DRIVER(serial_stm32) = {
Patrice Chotard9e276502018-01-12 09:23:49 +0100237 .name = "serial_stm32",
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800238 .id = UCLASS_SERIAL,
Vikas Manocha19e22c62017-02-12 10:25:44 -0800239 .of_match = of_match_ptr(stm32_serial_id),
240 .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
241 .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800242 .ops = &stm32_serial_ops,
243 .probe = stm32_serial_probe,
Bin Mengbdb33d82018-10-24 06:36:36 -0700244#if !CONFIG_IS_ENABLED(OF_CONTROL)
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800245 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700246#endif
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800247};
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200248
249#ifdef CONFIG_DEBUG_UART_STM32
250#include <debug_uart.h>
251static inline struct stm32_uart_info *_debug_uart_info(void)
252{
253 struct stm32_uart_info *uart_info;
254
255#if defined(CONFIG_STM32F4)
256 uart_info = &stm32f4_info;
257#elif defined(CONFIG_STM32F7)
258 uart_info = &stm32f7_info;
259#else
260 uart_info = &stm32h7_info;
261#endif
262 return uart_info;
263}
264
265static inline void _debug_uart_init(void)
266{
267 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
268 struct stm32_uart_info *uart_info = _debug_uart_info();
269
270 _stm32_serial_init(base, uart_info);
271 _stm32_serial_setbrg(base, uart_info,
272 CONFIG_DEBUG_UART_CLOCK,
273 CONFIG_BAUDRATE);
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200274}
275
276static inline void _debug_uart_putc(int c)
277{
278 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
279 struct stm32_uart_info *uart_info = _debug_uart_info();
280
281 while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN)
Patrick Delaunaye093b1c2019-04-18 17:32:51 +0200282 ;
Patrick Delaunayab8e5d22018-05-17 14:50:42 +0200283}
284
285DEBUG_UART_FUNCS
286#endif