blob: a9f5c3c3ea91eb5e44ed627e84c625f55ecfcec9 [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
7#include <common.h>
Vikas Manochafd03b832017-02-12 10:25:46 -08008#include <clk.h>
Vikas Manocha6a12ceb2016-02-11 15:47:19 -08009#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Patrice Chotardf828fa42018-12-04 14:11:36 +010011#include <reset.h>
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080012#include <serial.h>
Patrick Delaunay215c8be2018-05-17 14:50:42 +020013#include <watchdog.h>
14#include <asm/io.h>
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090015#include <asm/arch/stm32.h>
Patrice Chotardae74de02018-01-12 09:23:49 +010016#include "serial_stm32.h"
Simon Glass336d4612020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080018
Patrick Delaunay215c8be2018-05-17 14:50:42 +020019static void _stm32_serial_setbrg(fdt_addr_t base,
20 struct stm32_uart_info *uart_info,
21 u32 clock_rate,
22 int baudrate)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080023{
Patrick Delaunay215c8be2018-05-17 14:50:42 +020024 bool stm32f4 = uart_info->stm32f4;
Patrice Chotard27265ce2017-07-18 09:29:08 +020025 u32 int_div, mantissa, fraction, oversampling;
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090026
Patrick Delaunay215c8be2018-05-17 14:50:42 +020027 int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020028
29 if (int_div < 16) {
30 oversampling = 8;
Patrice Chotard60a996b2017-09-27 15:44:50 +020031 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020032 } else {
33 oversampling = 16;
Patrice Chotard60a996b2017-09-27 15:44:50 +020034 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020035 }
36
37 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
38 fraction = int_div % oversampling;
39
Patrice Chotard60a996b2017-09-27 15:44:50 +020040 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
Patrick Delaunay215c8be2018-05-17 14:50:42 +020041}
42
43static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
44{
45 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
46
47 _stm32_serial_setbrg(plat->base, plat->uart_info,
48 plat->clock_rate, baudrate);
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080049
50 return 0;
51}
52
Patrice Chotardfbd5c722018-08-03 15:07:39 +020053static int stm32_serial_setconfig(struct udevice *dev, uint serial_config)
Patrick Delaunaybc709a42018-05-17 14:50:45 +020054{
55 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
56 bool stm32f4 = plat->uart_info->stm32f4;
57 u8 uart_enable_bit = plat->uart_info->uart_enable_bit;
58 u32 cr1 = plat->base + CR1_OFFSET(stm32f4);
59 u32 config = 0;
Patrice Chotardfbd5c722018-08-03 15:07:39 +020060 uint parity = SERIAL_GET_PARITY(serial_config);
61 uint bits = SERIAL_GET_BITS(serial_config);
62 uint stop = SERIAL_GET_STOP(serial_config);
Patrick Delaunaybc709a42018-05-17 14:50:45 +020063
Patrice Chotardfbd5c722018-08-03 15:07:39 +020064 /*
65 * only parity config is implemented, check if other serial settings
66 * are the default one.
67 * (STM32F4 serial IP didn't support parity setting)
68 */
69 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || stm32f4)
70 return -ENOTSUPP; /* not supported in driver*/
Patrick Delaunaybc709a42018-05-17 14:50:45 +020071
72 clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
73 /* update usart configuration (uart need to be disable)
Patrice Chotardfbd5c722018-08-03 15:07:39 +020074 * PCE: parity check enable
Patrick Delaunaybc709a42018-05-17 14:50:45 +020075 * PS : '0' : Even / '1' : Odd
76 * M[1:0] = '00' : 8 Data bits
77 * M[1:0] = '01' : 9 Data bits with parity
78 */
79 switch (parity) {
80 default:
81 case SERIAL_PAR_NONE:
82 config = 0;
83 break;
84 case SERIAL_PAR_ODD:
85 config = USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0;
86 break;
87 case SERIAL_PAR_EVEN:
88 config = USART_CR1_PCE | USART_CR1_M0;
89 break;
90 }
Patrice Chotardfbd5c722018-08-03 15:07:39 +020091
Patrick Delaunaybc709a42018-05-17 14:50:45 +020092 clrsetbits_le32(cr1,
93 USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 |
94 USART_CR1_M0,
95 config);
96 setbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
97
98 return 0;
99}
100
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800101static int stm32_serial_getc(struct udevice *dev)
102{
Patrice Chotard60a996b2017-09-27 15:44:50 +0200103 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
104 bool stm32f4 = plat->uart_info->stm32f4;
105 fdt_addr_t base = plat->base;
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200106 u32 isr = readl(base + ISR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800107
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200108 if ((isr & USART_ISR_RXNE) == 0)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800109 return -EAGAIN;
110
Patrick Delaunay132518f2019-07-30 19:16:46 +0200111 if (isr & (USART_ISR_PE | USART_ISR_ORE | USART_ISR_FE)) {
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200112 if (!stm32f4)
Patrick Delaunaybc709a42018-05-17 14:50:45 +0200113 setbits_le32(base + ICR_OFFSET,
Patrick Delaunay132518f2019-07-30 19:16:46 +0200114 USART_ICR_PCECF | USART_ICR_ORECF |
115 USART_ICR_FECF);
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200116 else
117 readl(base + RDR_OFFSET(stm32f4));
118 return -EIO;
119 }
120
Patrice Chotard60a996b2017-09-27 15:44:50 +0200121 return readl(base + RDR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800122}
123
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200124static int _stm32_serial_putc(fdt_addr_t base,
125 struct stm32_uart_info *uart_info,
126 const char c)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800127{
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200128 bool stm32f4 = uart_info->stm32f4;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800129
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200130 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800131 return -EAGAIN;
132
Patrice Chotard60a996b2017-09-27 15:44:50 +0200133 writel(c, base + TDR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800134
135 return 0;
136}
137
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200138static int stm32_serial_putc(struct udevice *dev, const char c)
139{
140 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
141
142 return _stm32_serial_putc(plat->base, plat->uart_info, c);
143}
144
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800145static int stm32_serial_pending(struct udevice *dev, bool input)
146{
Patrice Chotard60a996b2017-09-27 15:44:50 +0200147 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
148 bool stm32f4 = plat->uart_info->stm32f4;
149 fdt_addr_t base = plat->base;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800150
151 if (input)
Patrice Chotard60a996b2017-09-27 15:44:50 +0200152 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200153 USART_ISR_RXNE ? 1 : 0;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800154 else
Patrice Chotard60a996b2017-09-27 15:44:50 +0200155 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200156 USART_ISR_TXE ? 0 : 1;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800157}
158
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200159static void _stm32_serial_init(fdt_addr_t base,
160 struct stm32_uart_info *uart_info)
161{
162 bool stm32f4 = uart_info->stm32f4;
163 u8 uart_enable_bit = uart_info->uart_enable_bit;
164
165 /* Disable uart-> enable fifo -> enable uart */
166 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
167 BIT(uart_enable_bit));
168 if (uart_info->has_fifo)
169 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN);
170 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
171 BIT(uart_enable_bit));
172}
173
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800174static int stm32_serial_probe(struct udevice *dev)
175{
Patrice Chotard60a996b2017-09-27 15:44:50 +0200176 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Patrice Chotard9a212d72017-09-27 15:44:53 +0200177 struct clk clk;
Patrice Chotardf828fa42018-12-04 14:11:36 +0100178 struct reset_ctl reset;
Patrice Chotard9a212d72017-09-27 15:44:53 +0200179 int ret;
Patrice Chotard60a996b2017-09-27 15:44:50 +0200180
181 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
Vikas Manochafd03b832017-02-12 10:25:46 -0800182
Vikas Manochafd03b832017-02-12 10:25:46 -0800183 ret = clk_get_by_index(dev, 0, &clk);
184 if (ret < 0)
185 return ret;
186
187 ret = clk_enable(&clk);
188 if (ret) {
189 dev_err(dev, "failed to enable clock\n");
190 return ret;
191 }
Vikas Manochafd03b832017-02-12 10:25:46 -0800192
Patrice Chotardf828fa42018-12-04 14:11:36 +0100193 ret = reset_get_by_index(dev, 0, &reset);
194 if (!ret) {
195 reset_assert(&reset);
196 udelay(2);
197 reset_deassert(&reset);
198 }
199
Patrice Chotard27265ce2017-07-18 09:29:08 +0200200 plat->clock_rate = clk_get_rate(&clk);
Patrick Delaunay585289b2019-06-21 15:26:41 +0200201 if (!plat->clock_rate) {
Patrice Chotard27265ce2017-07-18 09:29:08 +0200202 clk_disable(&clk);
Patrick Delaunay585289b2019-06-21 15:26:41 +0200203 return -EINVAL;
Patrice Chotard27265ce2017-07-18 09:29:08 +0200204 };
205
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200206 _stm32_serial_init(plat->base, plat->uart_info);
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800207
208 return 0;
209}
210
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800211static const struct udevice_id stm32_serial_id[] = {
Patrice Chotard6c30f152017-09-27 15:44:52 +0200212 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
Patrice Chotard2a7ecc52017-09-27 15:44:51 +0200213 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
214 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800215 {}
216};
217
218static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
219{
220 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800221
Patrice Chotard60a996b2017-09-27 15:44:50 +0200222 plat->base = devfdt_get_addr(dev);
223 if (plat->base == FDT_ADDR_T_NONE)
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800224 return -EINVAL;
225
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800226 return 0;
227}
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800228
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800229static const struct dm_serial_ops stm32_serial_ops = {
230 .putc = stm32_serial_putc,
231 .pending = stm32_serial_pending,
232 .getc = stm32_serial_getc,
233 .setbrg = stm32_serial_setbrg,
Patrice Chotardfbd5c722018-08-03 15:07:39 +0200234 .setconfig = stm32_serial_setconfig
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800235};
236
237U_BOOT_DRIVER(serial_stm32) = {
Patrice Chotardae74de02018-01-12 09:23:49 +0100238 .name = "serial_stm32",
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800239 .id = UCLASS_SERIAL,
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800240 .of_match = of_match_ptr(stm32_serial_id),
241 .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
242 .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800243 .ops = &stm32_serial_ops,
244 .probe = stm32_serial_probe,
Bin Meng46879192018-10-24 06:36:36 -0700245#if !CONFIG_IS_ENABLED(OF_CONTROL)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800246 .flags = DM_FLAG_PRE_RELOC,
Bin Meng46879192018-10-24 06:36:36 -0700247#endif
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800248};
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200249
250#ifdef CONFIG_DEBUG_UART_STM32
251#include <debug_uart.h>
252static inline struct stm32_uart_info *_debug_uart_info(void)
253{
254 struct stm32_uart_info *uart_info;
255
256#if defined(CONFIG_STM32F4)
257 uart_info = &stm32f4_info;
258#elif defined(CONFIG_STM32F7)
259 uart_info = &stm32f7_info;
260#else
261 uart_info = &stm32h7_info;
262#endif
263 return uart_info;
264}
265
266static inline void _debug_uart_init(void)
267{
268 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
269 struct stm32_uart_info *uart_info = _debug_uart_info();
270
271 _stm32_serial_init(base, uart_info);
272 _stm32_serial_setbrg(base, uart_info,
273 CONFIG_DEBUG_UART_CLOCK,
274 CONFIG_BAUDRATE);
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200275}
276
277static inline void _debug_uart_putc(int c)
278{
279 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
280 struct stm32_uart_info *uart_info = _debug_uart_info();
281
282 while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN)
Patrick Delaunay66dba9a2019-04-18 17:32:51 +0200283 ;
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200284}
285
286DEBUG_UART_FUNCS
287#endif