Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 2 | /* |
Patrice Chotard | 789ee0e | 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 | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Vikas Manocha | 7b00ff9 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 8 | #include <clk.h> |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <serial.h> |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 12 | #include <asm/arch/stm32.h> |
Patrice Chotard | 9e27650 | 2018-01-12 09:23:49 +0100 | [diff] [blame] | 13 | #include "serial_stm32.h" |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 14 | |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 15 | static int stm32_serial_setbrg(struct udevice *dev, int baudrate) |
| 16 | { |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 17 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 18 | bool stm32f4 = plat->uart_info->stm32f4; |
| 19 | fdt_addr_t base = plat->base; |
Patrice Chotard | 4809a19 | 2017-07-18 09:29:08 +0200 | [diff] [blame] | 20 | u32 int_div, mantissa, fraction, oversampling; |
Toshifumi NISHINAGA | 65bfb9c | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 21 | |
Patrice Chotard | 4809a19 | 2017-07-18 09:29:08 +0200 | [diff] [blame] | 22 | int_div = DIV_ROUND_CLOSEST(plat->clock_rate, baudrate); |
Patrice Chotard | 3149632 | 2017-06-08 09:26:55 +0200 | [diff] [blame] | 23 | |
| 24 | if (int_div < 16) { |
| 25 | oversampling = 8; |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 26 | setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8); |
Patrice Chotard | 3149632 | 2017-06-08 09:26:55 +0200 | [diff] [blame] | 27 | } else { |
| 28 | oversampling = 16; |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 29 | clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8); |
Patrice Chotard | 3149632 | 2017-06-08 09:26:55 +0200 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT; |
| 33 | fraction = int_div % oversampling; |
| 34 | |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 35 | writel(mantissa | fraction, base + BRR_OFFSET(stm32f4)); |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static int stm32_serial_getc(struct udevice *dev) |
| 41 | { |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 42 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 43 | bool stm32f4 = plat->uart_info->stm32f4; |
| 44 | fdt_addr_t base = plat->base; |
Patrice Chotard | 24af24b | 2018-04-20 08:59:06 +0200 | [diff] [blame] | 45 | u32 isr = readl(base + ISR_OFFSET(stm32f4)); |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 46 | |
Patrice Chotard | 75f2c86 | 2018-04-20 08:59:07 +0200 | [diff] [blame] | 47 | if ((isr & USART_ISR_FLAG_RXNE) == 0) |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 48 | return -EAGAIN; |
| 49 | |
Patrice Chotard | 75f2c86 | 2018-04-20 08:59:07 +0200 | [diff] [blame] | 50 | if (isr & USART_ISR_FLAG_ORE) { |
Patrice Chotard | 24af24b | 2018-04-20 08:59:06 +0200 | [diff] [blame] | 51 | if (!stm32f4) |
| 52 | setbits_le32(base + ICR_OFFSET, USART_ICR_OREF); |
| 53 | else |
| 54 | readl(base + RDR_OFFSET(stm32f4)); |
| 55 | return -EIO; |
| 56 | } |
| 57 | |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 58 | return readl(base + RDR_OFFSET(stm32f4)); |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static int stm32_serial_putc(struct udevice *dev, const char c) |
| 62 | { |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 63 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 64 | bool stm32f4 = plat->uart_info->stm32f4; |
| 65 | fdt_addr_t base = plat->base; |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 66 | |
Patrice Chotard | 75f2c86 | 2018-04-20 08:59:07 +0200 | [diff] [blame] | 67 | if ((readl(base + ISR_OFFSET(stm32f4)) & USART_ISR_FLAG_TXE) == 0) |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 68 | return -EAGAIN; |
| 69 | |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 70 | writel(c, base + TDR_OFFSET(stm32f4)); |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int stm32_serial_pending(struct udevice *dev, bool input) |
| 76 | { |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 77 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 78 | bool stm32f4 = plat->uart_info->stm32f4; |
| 79 | fdt_addr_t base = plat->base; |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 80 | |
| 81 | if (input) |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 82 | return readl(base + ISR_OFFSET(stm32f4)) & |
Patrice Chotard | 75f2c86 | 2018-04-20 08:59:07 +0200 | [diff] [blame] | 83 | USART_ISR_FLAG_RXNE ? 1 : 0; |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 84 | else |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 85 | return readl(base + ISR_OFFSET(stm32f4)) & |
Patrice Chotard | 75f2c86 | 2018-04-20 08:59:07 +0200 | [diff] [blame] | 86 | USART_ISR_FLAG_TXE ? 0 : 1; |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int stm32_serial_probe(struct udevice *dev) |
| 90 | { |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 91 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
Patrice Chotard | 21aad13 | 2017-09-27 15:44:53 +0200 | [diff] [blame] | 92 | struct clk clk; |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 93 | fdt_addr_t base = plat->base; |
Patrice Chotard | 21aad13 | 2017-09-27 15:44:53 +0200 | [diff] [blame] | 94 | int ret; |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 95 | bool stm32f4; |
| 96 | u8 uart_enable_bit; |
| 97 | |
| 98 | plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev); |
| 99 | stm32f4 = plat->uart_info->stm32f4; |
| 100 | uart_enable_bit = plat->uart_info->uart_enable_bit; |
Vikas Manocha | 7b00ff9 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 101 | |
Vikas Manocha | 7b00ff9 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 102 | ret = clk_get_by_index(dev, 0, &clk); |
| 103 | if (ret < 0) |
| 104 | return ret; |
| 105 | |
| 106 | ret = clk_enable(&clk); |
| 107 | if (ret) { |
| 108 | dev_err(dev, "failed to enable clock\n"); |
| 109 | return ret; |
| 110 | } |
Vikas Manocha | 7b00ff9 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 111 | |
Patrice Chotard | 4809a19 | 2017-07-18 09:29:08 +0200 | [diff] [blame] | 112 | plat->clock_rate = clk_get_rate(&clk); |
| 113 | if (plat->clock_rate < 0) { |
| 114 | clk_disable(&clk); |
| 115 | return plat->clock_rate; |
| 116 | }; |
| 117 | |
Patrice Chotard | 24af24b | 2018-04-20 08:59:06 +0200 | [diff] [blame] | 118 | /* Disable uart-> enable fifo-> enable uart */ |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 119 | clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE | |
| 120 | BIT(uart_enable_bit)); |
Patrice Chotard | 24fc72d | 2017-09-27 15:44:51 +0200 | [diff] [blame] | 121 | if (plat->uart_info->has_fifo) |
| 122 | setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN); |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 123 | setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE | |
| 124 | BIT(uart_enable_bit)); |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
Vikas Manocha | 19e22c6 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 129 | static const struct udevice_id stm32_serial_id[] = { |
Patrice Chotard | b21a69a | 2017-09-27 15:44:52 +0200 | [diff] [blame] | 130 | { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info}, |
Patrice Chotard | 24fc72d | 2017-09-27 15:44:51 +0200 | [diff] [blame] | 131 | { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info}, |
| 132 | { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info}, |
Vikas Manocha | 19e22c6 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 133 | {} |
| 134 | }; |
| 135 | |
| 136 | static int stm32_serial_ofdata_to_platdata(struct udevice *dev) |
| 137 | { |
| 138 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
Vikas Manocha | 19e22c6 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 139 | |
Patrice Chotard | 5011e6f | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 140 | plat->base = devfdt_get_addr(dev); |
| 141 | if (plat->base == FDT_ADDR_T_NONE) |
Vikas Manocha | 19e22c6 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 142 | return -EINVAL; |
| 143 | |
Vikas Manocha | 19e22c6 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 144 | return 0; |
| 145 | } |
Vikas Manocha | 19e22c6 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 146 | |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 147 | static const struct dm_serial_ops stm32_serial_ops = { |
| 148 | .putc = stm32_serial_putc, |
| 149 | .pending = stm32_serial_pending, |
| 150 | .getc = stm32_serial_getc, |
| 151 | .setbrg = stm32_serial_setbrg, |
| 152 | }; |
| 153 | |
| 154 | U_BOOT_DRIVER(serial_stm32) = { |
Patrice Chotard | 9e27650 | 2018-01-12 09:23:49 +0100 | [diff] [blame] | 155 | .name = "serial_stm32", |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 156 | .id = UCLASS_SERIAL, |
Vikas Manocha | 19e22c6 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 157 | .of_match = of_match_ptr(stm32_serial_id), |
| 158 | .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata), |
| 159 | .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata), |
Vikas Manocha | 5dba05e | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 160 | .ops = &stm32_serial_ops, |
| 161 | .probe = stm32_serial_probe, |
| 162 | .flags = DM_FLAG_PRE_RELOC, |
| 163 | }; |