blob: 00a8e7249b55717c172d4ed0eb4b18df73d6bf4e [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>
Patrice Chotardf828fa42018-12-04 14:11:36 +010010#include <reset.h>
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080011#include <serial.h>
Patrick Delaunay215c8be2018-05-17 14:50:42 +020012#include <watchdog.h>
13#include <asm/io.h>
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090014#include <asm/arch/stm32.h>
Patrice Chotardae74de02018-01-12 09:23:49 +010015#include "serial_stm32.h"
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080016
Patrick Delaunay215c8be2018-05-17 14:50:42 +020017static void _stm32_serial_setbrg(fdt_addr_t base,
18 struct stm32_uart_info *uart_info,
19 u32 clock_rate,
20 int baudrate)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080021{
Patrick Delaunay215c8be2018-05-17 14:50:42 +020022 bool stm32f4 = uart_info->stm32f4;
Patrice Chotard27265ce2017-07-18 09:29:08 +020023 u32 int_div, mantissa, fraction, oversampling;
Toshifumi NISHINAGAba0a3c12016-07-08 01:02:24 +090024
Patrick Delaunay215c8be2018-05-17 14:50:42 +020025 int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020026
27 if (int_div < 16) {
28 oversampling = 8;
Patrice Chotard60a996b2017-09-27 15:44:50 +020029 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020030 } else {
31 oversampling = 16;
Patrice Chotard60a996b2017-09-27 15:44:50 +020032 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard1afcf9c2017-06-08 09:26:55 +020033 }
34
35 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
36 fraction = int_div % oversampling;
37
Patrice Chotard60a996b2017-09-27 15:44:50 +020038 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
Patrick Delaunay215c8be2018-05-17 14:50:42 +020039}
40
41static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
42{
43 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
44
45 _stm32_serial_setbrg(plat->base, plat->uart_info,
46 plat->clock_rate, baudrate);
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080047
48 return 0;
49}
50
Patrice Chotardfbd5c722018-08-03 15:07:39 +020051static int stm32_serial_setconfig(struct udevice *dev, uint serial_config)
Patrick Delaunaybc709a42018-05-17 14:50:45 +020052{
53 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
54 bool stm32f4 = plat->uart_info->stm32f4;
55 u8 uart_enable_bit = plat->uart_info->uart_enable_bit;
56 u32 cr1 = plat->base + CR1_OFFSET(stm32f4);
57 u32 config = 0;
Patrice Chotardfbd5c722018-08-03 15:07:39 +020058 uint parity = SERIAL_GET_PARITY(serial_config);
59 uint bits = SERIAL_GET_BITS(serial_config);
60 uint stop = SERIAL_GET_STOP(serial_config);
Patrick Delaunaybc709a42018-05-17 14:50:45 +020061
Patrice Chotardfbd5c722018-08-03 15:07:39 +020062 /*
63 * only parity config is implemented, check if other serial settings
64 * are the default one.
65 * (STM32F4 serial IP didn't support parity setting)
66 */
67 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || stm32f4)
68 return -ENOTSUPP; /* not supported in driver*/
Patrick Delaunaybc709a42018-05-17 14:50:45 +020069
70 clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
71 /* update usart configuration (uart need to be disable)
Patrice Chotardfbd5c722018-08-03 15:07:39 +020072 * PCE: parity check enable
Patrick Delaunaybc709a42018-05-17 14:50:45 +020073 * PS : '0' : Even / '1' : Odd
74 * M[1:0] = '00' : 8 Data bits
75 * M[1:0] = '01' : 9 Data bits with parity
76 */
77 switch (parity) {
78 default:
79 case SERIAL_PAR_NONE:
80 config = 0;
81 break;
82 case SERIAL_PAR_ODD:
83 config = USART_CR1_PCE | USART_CR1_PS | USART_CR1_M0;
84 break;
85 case SERIAL_PAR_EVEN:
86 config = USART_CR1_PCE | USART_CR1_M0;
87 break;
88 }
Patrice Chotardfbd5c722018-08-03 15:07:39 +020089
Patrick Delaunaybc709a42018-05-17 14:50:45 +020090 clrsetbits_le32(cr1,
91 USART_CR1_PCE | USART_CR1_PS | USART_CR1_M1 |
92 USART_CR1_M0,
93 config);
94 setbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit));
95
96 return 0;
97}
98
Vikas Manocha6a12ceb2016-02-11 15:47:19 -080099static int stm32_serial_getc(struct udevice *dev)
100{
Patrice Chotard60a996b2017-09-27 15:44:50 +0200101 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
102 bool stm32f4 = plat->uart_info->stm32f4;
103 fdt_addr_t base = plat->base;
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200104 u32 isr = readl(base + ISR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800105
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200106 if ((isr & USART_ISR_RXNE) == 0)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800107 return -EAGAIN;
108
Patrick Delaunay132518f2019-07-30 19:16:46 +0200109 if (isr & (USART_ISR_PE | USART_ISR_ORE | USART_ISR_FE)) {
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200110 if (!stm32f4)
Patrick Delaunaybc709a42018-05-17 14:50:45 +0200111 setbits_le32(base + ICR_OFFSET,
Patrick Delaunay132518f2019-07-30 19:16:46 +0200112 USART_ICR_PCECF | USART_ICR_ORECF |
113 USART_ICR_FECF);
Patrice Chotard7b3b74d2018-04-20 08:59:06 +0200114 else
115 readl(base + RDR_OFFSET(stm32f4));
116 return -EIO;
117 }
118
Patrice Chotard60a996b2017-09-27 15:44:50 +0200119 return readl(base + RDR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800120}
121
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200122static int _stm32_serial_putc(fdt_addr_t base,
123 struct stm32_uart_info *uart_info,
124 const char c)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800125{
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200126 bool stm32f4 = uart_info->stm32f4;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800127
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200128 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800129 return -EAGAIN;
130
Patrice Chotard60a996b2017-09-27 15:44:50 +0200131 writel(c, base + TDR_OFFSET(stm32f4));
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800132
133 return 0;
134}
135
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200136static int stm32_serial_putc(struct udevice *dev, const char c)
137{
138 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
139
140 return _stm32_serial_putc(plat->base, plat->uart_info, c);
141}
142
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800143static int stm32_serial_pending(struct udevice *dev, bool input)
144{
Patrice Chotard60a996b2017-09-27 15:44:50 +0200145 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
146 bool stm32f4 = plat->uart_info->stm32f4;
147 fdt_addr_t base = plat->base;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800148
149 if (input)
Patrice Chotard60a996b2017-09-27 15:44:50 +0200150 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200151 USART_ISR_RXNE ? 1 : 0;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800152 else
Patrice Chotard60a996b2017-09-27 15:44:50 +0200153 return readl(base + ISR_OFFSET(stm32f4)) &
Patrice Chotardbe1a6f72018-05-17 14:50:43 +0200154 USART_ISR_TXE ? 0 : 1;
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800155}
156
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200157static void _stm32_serial_init(fdt_addr_t base,
158 struct stm32_uart_info *uart_info)
159{
160 bool stm32f4 = uart_info->stm32f4;
161 u8 uart_enable_bit = uart_info->uart_enable_bit;
162
163 /* Disable uart-> enable fifo -> enable uart */
164 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
165 BIT(uart_enable_bit));
166 if (uart_info->has_fifo)
167 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN);
168 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
169 BIT(uart_enable_bit));
170}
171
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800172static int stm32_serial_probe(struct udevice *dev)
173{
Patrice Chotard60a996b2017-09-27 15:44:50 +0200174 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Patrice Chotard9a212d72017-09-27 15:44:53 +0200175 struct clk clk;
Patrice Chotardf828fa42018-12-04 14:11:36 +0100176 struct reset_ctl reset;
Patrice Chotard9a212d72017-09-27 15:44:53 +0200177 int ret;
Patrice Chotard60a996b2017-09-27 15:44:50 +0200178
179 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
Vikas Manochafd03b832017-02-12 10:25:46 -0800180
Vikas Manochafd03b832017-02-12 10:25:46 -0800181 ret = clk_get_by_index(dev, 0, &clk);
182 if (ret < 0)
183 return ret;
184
185 ret = clk_enable(&clk);
186 if (ret) {
187 dev_err(dev, "failed to enable clock\n");
188 return ret;
189 }
Vikas Manochafd03b832017-02-12 10:25:46 -0800190
Patrice Chotardf828fa42018-12-04 14:11:36 +0100191 ret = reset_get_by_index(dev, 0, &reset);
192 if (!ret) {
193 reset_assert(&reset);
194 udelay(2);
195 reset_deassert(&reset);
196 }
197
Patrice Chotard27265ce2017-07-18 09:29:08 +0200198 plat->clock_rate = clk_get_rate(&clk);
Patrick Delaunay585289b2019-06-21 15:26:41 +0200199 if (!plat->clock_rate) {
Patrice Chotard27265ce2017-07-18 09:29:08 +0200200 clk_disable(&clk);
Patrick Delaunay585289b2019-06-21 15:26:41 +0200201 return -EINVAL;
Patrice Chotard27265ce2017-07-18 09:29:08 +0200202 };
203
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200204 _stm32_serial_init(plat->base, plat->uart_info);
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800205
206 return 0;
207}
208
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800209static const struct udevice_id stm32_serial_id[] = {
Patrice Chotard6c30f152017-09-27 15:44:52 +0200210 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
Patrice Chotard2a7ecc52017-09-27 15:44:51 +0200211 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
212 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800213 {}
214};
215
216static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
217{
218 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800219
Patrice Chotard60a996b2017-09-27 15:44:50 +0200220 plat->base = devfdt_get_addr(dev);
221 if (plat->base == FDT_ADDR_T_NONE)
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800222 return -EINVAL;
223
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800224 return 0;
225}
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800226
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800227static const struct dm_serial_ops stm32_serial_ops = {
228 .putc = stm32_serial_putc,
229 .pending = stm32_serial_pending,
230 .getc = stm32_serial_getc,
231 .setbrg = stm32_serial_setbrg,
Patrice Chotardfbd5c722018-08-03 15:07:39 +0200232 .setconfig = stm32_serial_setconfig
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800233};
234
235U_BOOT_DRIVER(serial_stm32) = {
Patrice Chotardae74de02018-01-12 09:23:49 +0100236 .name = "serial_stm32",
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800237 .id = UCLASS_SERIAL,
Vikas Manocha42bf5e72017-02-12 10:25:44 -0800238 .of_match = of_match_ptr(stm32_serial_id),
239 .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
240 .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800241 .ops = &stm32_serial_ops,
242 .probe = stm32_serial_probe,
Bin Meng46879192018-10-24 06:36:36 -0700243#if !CONFIG_IS_ENABLED(OF_CONTROL)
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800244 .flags = DM_FLAG_PRE_RELOC,
Bin Meng46879192018-10-24 06:36:36 -0700245#endif
Vikas Manocha6a12ceb2016-02-11 15:47:19 -0800246};
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200247
248#ifdef CONFIG_DEBUG_UART_STM32
249#include <debug_uart.h>
250static inline struct stm32_uart_info *_debug_uart_info(void)
251{
252 struct stm32_uart_info *uart_info;
253
254#if defined(CONFIG_STM32F4)
255 uart_info = &stm32f4_info;
256#elif defined(CONFIG_STM32F7)
257 uart_info = &stm32f7_info;
258#else
259 uart_info = &stm32h7_info;
260#endif
261 return uart_info;
262}
263
264static inline void _debug_uart_init(void)
265{
266 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
267 struct stm32_uart_info *uart_info = _debug_uart_info();
268
269 _stm32_serial_init(base, uart_info);
270 _stm32_serial_setbrg(base, uart_info,
271 CONFIG_DEBUG_UART_CLOCK,
272 CONFIG_BAUDRATE);
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200273}
274
275static inline void _debug_uart_putc(int c)
276{
277 fdt_addr_t base = CONFIG_DEBUG_UART_BASE;
278 struct stm32_uart_info *uart_info = _debug_uart_info();
279
280 while (_stm32_serial_putc(base, uart_info, c) == -EAGAIN)
Patrick Delaunay66dba9a2019-04-18 17:32:51 +0200281 ;
Patrick Delaunay215c8be2018-05-17 14:50:42 +0200282}
283
284DEBUG_UART_FUNCS
285#endif