Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 2 | /* |
Patrice Chotard | 3bc599c | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 3 | * Copyright (C) 2016, STMicroelectronics - All Rights Reserved |
| 4 | * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics. |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Vikas Manocha | fd03b83 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 8 | #include <clk.h> |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 9 | #include <dm.h> |
Patrice Chotard | f828fa4 | 2018-12-04 14:11:36 +0100 | [diff] [blame] | 10 | #include <reset.h> |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 11 | #include <serial.h> |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 12 | #include <watchdog.h> |
| 13 | #include <asm/io.h> |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 14 | #include <asm/arch/stm32.h> |
Patrice Chotard | ae74de0 | 2018-01-12 09:23:49 +0100 | [diff] [blame] | 15 | #include "serial_stm32.h" |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 16 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 17 | static void _stm32_serial_setbrg(fdt_addr_t base, |
| 18 | struct stm32_uart_info *uart_info, |
| 19 | u32 clock_rate, |
| 20 | int baudrate) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 21 | { |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 22 | bool stm32f4 = uart_info->stm32f4; |
Patrice Chotard | 27265ce | 2017-07-18 09:29:08 +0200 | [diff] [blame] | 23 | u32 int_div, mantissa, fraction, oversampling; |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 24 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 25 | int_div = DIV_ROUND_CLOSEST(clock_rate, baudrate); |
Patrice Chotard | 1afcf9c | 2017-06-08 09:26:55 +0200 | [diff] [blame] | 26 | |
| 27 | if (int_div < 16) { |
| 28 | oversampling = 8; |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 29 | setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8); |
Patrice Chotard | 1afcf9c | 2017-06-08 09:26:55 +0200 | [diff] [blame] | 30 | } else { |
| 31 | oversampling = 16; |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 32 | clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8); |
Patrice Chotard | 1afcf9c | 2017-06-08 09:26:55 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT; |
| 36 | fraction = int_div % oversampling; |
| 37 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 38 | writel(mantissa | fraction, base + BRR_OFFSET(stm32f4)); |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static 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 Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
Patrice Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 51 | static int stm32_serial_setconfig(struct udevice *dev, uint serial_config) |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 52 | { |
| 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 Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 58 | uint parity = SERIAL_GET_PARITY(serial_config); |
| 59 | uint bits = SERIAL_GET_BITS(serial_config); |
| 60 | uint stop = SERIAL_GET_STOP(serial_config); |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 61 | |
Patrice Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 62 | /* |
| 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 Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 69 | |
| 70 | clrbits_le32(cr1, USART_CR1_RE | USART_CR1_TE | BIT(uart_enable_bit)); |
| 71 | /* update usart configuration (uart need to be disable) |
Patrice Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 72 | * PCE: parity check enable |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 73 | * 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 Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 89 | |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 90 | 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 Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 99 | static int stm32_serial_getc(struct udevice *dev) |
| 100 | { |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 101 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 102 | bool stm32f4 = plat->uart_info->stm32f4; |
| 103 | fdt_addr_t base = plat->base; |
Patrice Chotard | 7b3b74d | 2018-04-20 08:59:06 +0200 | [diff] [blame] | 104 | u32 isr = readl(base + ISR_OFFSET(stm32f4)); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 105 | |
Patrice Chotard | be1a6f7 | 2018-05-17 14:50:43 +0200 | [diff] [blame] | 106 | if ((isr & USART_ISR_RXNE) == 0) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 107 | return -EAGAIN; |
| 108 | |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 109 | if (isr & (USART_ISR_PE | USART_ISR_ORE)) { |
Patrice Chotard | 7b3b74d | 2018-04-20 08:59:06 +0200 | [diff] [blame] | 110 | if (!stm32f4) |
Patrick Delaunay | bc709a4 | 2018-05-17 14:50:45 +0200 | [diff] [blame] | 111 | setbits_le32(base + ICR_OFFSET, |
| 112 | USART_ICR_PCECF | USART_ICR_ORECF); |
Patrice Chotard | 7b3b74d | 2018-04-20 08:59:06 +0200 | [diff] [blame] | 113 | else |
| 114 | readl(base + RDR_OFFSET(stm32f4)); |
| 115 | return -EIO; |
| 116 | } |
| 117 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 118 | return readl(base + RDR_OFFSET(stm32f4)); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 121 | static int _stm32_serial_putc(fdt_addr_t base, |
| 122 | struct stm32_uart_info *uart_info, |
| 123 | const char c) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 124 | { |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 125 | bool stm32f4 = uart_info->stm32f4; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 126 | |
Patrice Chotard | be1a6f7 | 2018-05-17 14:50:43 +0200 | [diff] [blame] | 127 | if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_TXE) == 0) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 128 | return -EAGAIN; |
| 129 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 130 | writel(c, base + TDR_OFFSET(stm32f4)); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 135 | static int stm32_serial_putc(struct udevice *dev, const char c) |
| 136 | { |
| 137 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 138 | |
| 139 | return _stm32_serial_putc(plat->base, plat->uart_info, c); |
| 140 | } |
| 141 | |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 142 | static int stm32_serial_pending(struct udevice *dev, bool input) |
| 143 | { |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 144 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 145 | bool stm32f4 = plat->uart_info->stm32f4; |
| 146 | fdt_addr_t base = plat->base; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 147 | |
| 148 | if (input) |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 149 | return readl(base + ISR_OFFSET(stm32f4)) & |
Patrice Chotard | be1a6f7 | 2018-05-17 14:50:43 +0200 | [diff] [blame] | 150 | USART_ISR_RXNE ? 1 : 0; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 151 | else |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 152 | return readl(base + ISR_OFFSET(stm32f4)) & |
Patrice Chotard | be1a6f7 | 2018-05-17 14:50:43 +0200 | [diff] [blame] | 153 | USART_ISR_TXE ? 0 : 1; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 156 | static void _stm32_serial_init(fdt_addr_t base, |
| 157 | struct stm32_uart_info *uart_info) |
| 158 | { |
| 159 | bool stm32f4 = uart_info->stm32f4; |
| 160 | u8 uart_enable_bit = uart_info->uart_enable_bit; |
| 161 | |
| 162 | /* Disable uart-> enable fifo -> enable uart */ |
| 163 | clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE | |
| 164 | BIT(uart_enable_bit)); |
| 165 | if (uart_info->has_fifo) |
| 166 | setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN); |
| 167 | setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE | |
| 168 | BIT(uart_enable_bit)); |
| 169 | } |
| 170 | |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 171 | static int stm32_serial_probe(struct udevice *dev) |
| 172 | { |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 173 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
Patrice Chotard | 9a212d7 | 2017-09-27 15:44:53 +0200 | [diff] [blame] | 174 | struct clk clk; |
Patrice Chotard | f828fa4 | 2018-12-04 14:11:36 +0100 | [diff] [blame] | 175 | struct reset_ctl reset; |
Patrice Chotard | 9a212d7 | 2017-09-27 15:44:53 +0200 | [diff] [blame] | 176 | int ret; |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 177 | |
| 178 | plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev); |
Vikas Manocha | fd03b83 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 179 | |
Vikas Manocha | fd03b83 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 180 | ret = clk_get_by_index(dev, 0, &clk); |
| 181 | if (ret < 0) |
| 182 | return ret; |
| 183 | |
| 184 | ret = clk_enable(&clk); |
| 185 | if (ret) { |
| 186 | dev_err(dev, "failed to enable clock\n"); |
| 187 | return ret; |
| 188 | } |
Vikas Manocha | fd03b83 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 189 | |
Patrice Chotard | f828fa4 | 2018-12-04 14:11:36 +0100 | [diff] [blame] | 190 | ret = reset_get_by_index(dev, 0, &reset); |
| 191 | if (!ret) { |
| 192 | reset_assert(&reset); |
| 193 | udelay(2); |
| 194 | reset_deassert(&reset); |
| 195 | } |
| 196 | |
Patrice Chotard | 27265ce | 2017-07-18 09:29:08 +0200 | [diff] [blame] | 197 | plat->clock_rate = clk_get_rate(&clk); |
| 198 | if (plat->clock_rate < 0) { |
| 199 | clk_disable(&clk); |
| 200 | return plat->clock_rate; |
| 201 | }; |
| 202 | |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 203 | _stm32_serial_init(plat->base, plat->uart_info); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 208 | static const struct udevice_id stm32_serial_id[] = { |
Patrice Chotard | 6c30f15 | 2017-09-27 15:44:52 +0200 | [diff] [blame] | 209 | { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info}, |
Patrice Chotard | 2a7ecc5 | 2017-09-27 15:44:51 +0200 | [diff] [blame] | 210 | { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info}, |
| 211 | { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info}, |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 212 | {} |
| 213 | }; |
| 214 | |
| 215 | static int stm32_serial_ofdata_to_platdata(struct udevice *dev) |
| 216 | { |
| 217 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 218 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 219 | plat->base = devfdt_get_addr(dev); |
| 220 | if (plat->base == FDT_ADDR_T_NONE) |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 221 | return -EINVAL; |
| 222 | |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 223 | return 0; |
| 224 | } |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 225 | |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 226 | static const struct dm_serial_ops stm32_serial_ops = { |
| 227 | .putc = stm32_serial_putc, |
| 228 | .pending = stm32_serial_pending, |
| 229 | .getc = stm32_serial_getc, |
| 230 | .setbrg = stm32_serial_setbrg, |
Patrice Chotard | fbd5c72 | 2018-08-03 15:07:39 +0200 | [diff] [blame] | 231 | .setconfig = stm32_serial_setconfig |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | U_BOOT_DRIVER(serial_stm32) = { |
Patrice Chotard | ae74de0 | 2018-01-12 09:23:49 +0100 | [diff] [blame] | 235 | .name = "serial_stm32", |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 236 | .id = UCLASS_SERIAL, |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 237 | .of_match = of_match_ptr(stm32_serial_id), |
| 238 | .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata), |
| 239 | .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata), |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 240 | .ops = &stm32_serial_ops, |
| 241 | .probe = stm32_serial_probe, |
Bin Meng | 4687919 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 242 | #if !CONFIG_IS_ENABLED(OF_CONTROL) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 243 | .flags = DM_FLAG_PRE_RELOC, |
Bin Meng | 4687919 | 2018-10-24 06:36:36 -0700 | [diff] [blame] | 244 | #endif |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 245 | }; |
Patrick Delaunay | 215c8be | 2018-05-17 14:50:42 +0200 | [diff] [blame] | 246 | |
| 247 | #ifdef CONFIG_DEBUG_UART_STM32 |
| 248 | #include <debug_uart.h> |
| 249 | static inline struct stm32_uart_info *_debug_uart_info(void) |
| 250 | { |
| 251 | struct stm32_uart_info *uart_info; |
| 252 | |
| 253 | #if defined(CONFIG_STM32F4) |
| 254 | uart_info = &stm32f4_info; |
| 255 | #elif defined(CONFIG_STM32F7) |
| 256 | uart_info = &stm32f7_info; |
| 257 | #else |
| 258 | uart_info = &stm32h7_info; |
| 259 | #endif |
| 260 | return uart_info; |
| 261 | } |
| 262 | |
| 263 | static inline void _debug_uart_init(void) |
| 264 | { |
| 265 | fdt_addr_t base = CONFIG_DEBUG_UART_BASE; |
| 266 | struct stm32_uart_info *uart_info = _debug_uart_info(); |
| 267 | |
| 268 | _stm32_serial_init(base, uart_info); |
| 269 | _stm32_serial_setbrg(base, uart_info, |
| 270 | CONFIG_DEBUG_UART_CLOCK, |
| 271 | CONFIG_BAUDRATE); |
| 272 | printf("DEBUG done\n"); |
| 273 | } |
| 274 | |
| 275 | static 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) |
| 281 | WATCHDOG_RESET(); |
| 282 | } |
| 283 | |
| 284 | DEBUG_UART_FUNCS |
| 285 | #endif |