Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2016 |
| 3 | * Vikas Manocha, <vikas.manocha@st.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Vikas Manocha | fd03b83 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 9 | #include <clk.h> |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <serial.h> |
Toshifumi NISHINAGA | ba0a3c1 | 2016-07-08 01:02:24 +0900 | [diff] [blame] | 13 | #include <asm/arch/stm32.h> |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 14 | #include "serial_stm32x7.h" |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | static int stm32_serial_setbrg(struct udevice *dev, int baudrate) |
| 19 | { |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 20 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 21 | bool stm32f4 = plat->uart_info->stm32f4; |
| 22 | fdt_addr_t base = plat->base; |
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 | |
Patrice Chotard | 27265ce | 2017-07-18 09:29:08 +0200 | [diff] [blame] | 25 | int_div = DIV_ROUND_CLOSEST(plat->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)); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int stm32_serial_getc(struct udevice *dev) |
| 44 | { |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 45 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 46 | bool stm32f4 = plat->uart_info->stm32f4; |
| 47 | fdt_addr_t base = plat->base; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 48 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 49 | if ((readl(base + ISR_OFFSET(stm32f4)) & USART_SR_FLAG_RXNE) == 0) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 50 | return -EAGAIN; |
| 51 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 52 | return readl(base + RDR_OFFSET(stm32f4)); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int stm32_serial_putc(struct udevice *dev, const char c) |
| 56 | { |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 57 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 58 | bool stm32f4 = plat->uart_info->stm32f4; |
| 59 | fdt_addr_t base = plat->base; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 60 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 61 | if ((readl(base + ISR_OFFSET(stm32f4)) & USART_SR_FLAG_TXE) == 0) |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 62 | return -EAGAIN; |
| 63 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 64 | writel(c, base + TDR_OFFSET(stm32f4)); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int stm32_serial_pending(struct udevice *dev, bool input) |
| 70 | { |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 71 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 72 | bool stm32f4 = plat->uart_info->stm32f4; |
| 73 | fdt_addr_t base = plat->base; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 74 | |
| 75 | if (input) |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 76 | return readl(base + ISR_OFFSET(stm32f4)) & |
| 77 | USART_SR_FLAG_RXNE ? 1 : 0; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 78 | else |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 79 | return readl(base + ISR_OFFSET(stm32f4)) & |
| 80 | USART_SR_FLAG_TXE ? 0 : 1; |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static int stm32_serial_probe(struct udevice *dev) |
| 84 | { |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 85 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
| 86 | fdt_addr_t base = plat->base; |
| 87 | bool stm32f4; |
| 88 | u8 uart_enable_bit; |
| 89 | |
| 90 | plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev); |
| 91 | stm32f4 = plat->uart_info->stm32f4; |
| 92 | uart_enable_bit = plat->uart_info->uart_enable_bit; |
Vikas Manocha | fd03b83 | 2017-02-12 10:25:46 -0800 | [diff] [blame] | 93 | |
| 94 | #ifdef CONFIG_CLK |
| 95 | int ret; |
| 96 | struct clk clk; |
| 97 | |
| 98 | ret = clk_get_by_index(dev, 0, &clk); |
| 99 | if (ret < 0) |
| 100 | return ret; |
| 101 | |
| 102 | ret = clk_enable(&clk); |
| 103 | if (ret) { |
| 104 | dev_err(dev, "failed to enable clock\n"); |
| 105 | return ret; |
| 106 | } |
| 107 | #endif |
| 108 | |
Patrice Chotard | 27265ce | 2017-07-18 09:29:08 +0200 | [diff] [blame] | 109 | plat->clock_rate = clk_get_rate(&clk); |
| 110 | if (plat->clock_rate < 0) { |
| 111 | clk_disable(&clk); |
| 112 | return plat->clock_rate; |
| 113 | }; |
| 114 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 115 | /* Disable uart-> disable overrun-> enable uart */ |
| 116 | clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE | |
| 117 | BIT(uart_enable_bit)); |
| 118 | if (plat->uart_info->has_overrun_disable) |
| 119 | setbits_le32(base + CR3_OFFSET(stm32f4), USART_CR3_OVRDIS); |
Patrice Chotard | 2a7ecc5 | 2017-09-27 15:44:51 +0200 | [diff] [blame^] | 120 | if (plat->uart_info->has_fifo) |
| 121 | setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN); |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 122 | setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE | |
| 123 | BIT(uart_enable_bit)); |
Vikas Manocha | 6a12ceb | 2016-02-11 15:47:19 -0800 | [diff] [blame] | 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 128 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 129 | static const struct udevice_id stm32_serial_id[] = { |
Patrice Chotard | 2a7ecc5 | 2017-09-27 15:44:51 +0200 | [diff] [blame^] | 130 | { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info}, |
| 131 | { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info}, |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 132 | {} |
| 133 | }; |
| 134 | |
| 135 | static int stm32_serial_ofdata_to_platdata(struct udevice *dev) |
| 136 | { |
| 137 | struct stm32x7_serial_platdata *plat = dev_get_platdata(dev); |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 138 | |
Patrice Chotard | 60a996b | 2017-09-27 15:44:50 +0200 | [diff] [blame] | 139 | plat->base = devfdt_get_addr(dev); |
| 140 | if (plat->base == FDT_ADDR_T_NONE) |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 141 | return -EINVAL; |
| 142 | |
Vikas Manocha | 42bf5e7 | 2017-02-12 10:25:44 -0800 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | #endif |
| 146 | |
Vikas Manocha | 6a12ceb | 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) = { |
| 155 | .name = "serial_stm32x7", |
| 156 | .id = UCLASS_SERIAL, |
Vikas Manocha | 42bf5e7 | 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 | 6a12ceb | 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 | }; |