blob: a5d529cab28d65fbc1d92a6c8cac5edaf8075b54 [file] [log] [blame]
Vikas Manocha5dba05e2016-02-11 15:47:19 -08001/*
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 Manocha7b00ff92017-02-12 10:25:46 -08009#include <clk.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -080010#include <dm.h>
11#include <asm/io.h>
12#include <serial.h>
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090013#include <asm/arch/stm32.h>
Vikas Manocha5dba05e2016-02-11 15:47:19 -080014#include "serial_stm32x7.h"
15
16DECLARE_GLOBAL_DATA_PTR;
17
18static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
19{
Patrice Chotard5011e6f2017-09-27 15:44:50 +020020 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
21 bool stm32f4 = plat->uart_info->stm32f4;
22 fdt_addr_t base = plat->base;
Patrice Chotard4809a192017-07-18 09:29:08 +020023 u32 int_div, mantissa, fraction, oversampling;
Toshifumi NISHINAGA65bfb9c2016-07-08 01:02:24 +090024
Patrice Chotard4809a192017-07-18 09:29:08 +020025 int_div = DIV_ROUND_CLOSEST(plat->clock_rate, baudrate);
Patrice Chotard31496322017-06-08 09:26:55 +020026
27 if (int_div < 16) {
28 oversampling = 8;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020029 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020030 } else {
31 oversampling = 16;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020032 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_OVER8);
Patrice Chotard31496322017-06-08 09:26:55 +020033 }
34
35 mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
36 fraction = int_div % oversampling;
37
Patrice Chotard5011e6f2017-09-27 15:44:50 +020038 writel(mantissa | fraction, base + BRR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -080039
40 return 0;
41}
42
43static int stm32_serial_getc(struct udevice *dev)
44{
Patrice Chotard5011e6f2017-09-27 15:44:50 +020045 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
46 bool stm32f4 = plat->uart_info->stm32f4;
47 fdt_addr_t base = plat->base;
Vikas Manocha5dba05e2016-02-11 15:47:19 -080048
Patrice Chotard5011e6f2017-09-27 15:44:50 +020049 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_SR_FLAG_RXNE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -080050 return -EAGAIN;
51
Patrice Chotard5011e6f2017-09-27 15:44:50 +020052 return readl(base + RDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -080053}
54
55static int stm32_serial_putc(struct udevice *dev, const char c)
56{
Patrice Chotard5011e6f2017-09-27 15:44:50 +020057 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
58 bool stm32f4 = plat->uart_info->stm32f4;
59 fdt_addr_t base = plat->base;
Vikas Manocha5dba05e2016-02-11 15:47:19 -080060
Patrice Chotard5011e6f2017-09-27 15:44:50 +020061 if ((readl(base + ISR_OFFSET(stm32f4)) & USART_SR_FLAG_TXE) == 0)
Vikas Manocha5dba05e2016-02-11 15:47:19 -080062 return -EAGAIN;
63
Patrice Chotard5011e6f2017-09-27 15:44:50 +020064 writel(c, base + TDR_OFFSET(stm32f4));
Vikas Manocha5dba05e2016-02-11 15:47:19 -080065
66 return 0;
67}
68
69static int stm32_serial_pending(struct udevice *dev, bool input)
70{
Patrice Chotard5011e6f2017-09-27 15:44:50 +020071 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
72 bool stm32f4 = plat->uart_info->stm32f4;
73 fdt_addr_t base = plat->base;
Vikas Manocha5dba05e2016-02-11 15:47:19 -080074
75 if (input)
Patrice Chotard5011e6f2017-09-27 15:44:50 +020076 return readl(base + ISR_OFFSET(stm32f4)) &
77 USART_SR_FLAG_RXNE ? 1 : 0;
Vikas Manocha5dba05e2016-02-11 15:47:19 -080078 else
Patrice Chotard5011e6f2017-09-27 15:44:50 +020079 return readl(base + ISR_OFFSET(stm32f4)) &
80 USART_SR_FLAG_TXE ? 0 : 1;
Vikas Manocha5dba05e2016-02-11 15:47:19 -080081}
82
83static int stm32_serial_probe(struct udevice *dev)
84{
Patrice Chotard5011e6f2017-09-27 15:44:50 +020085 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Patrice Chotard21aad132017-09-27 15:44:53 +020086 struct clk clk;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020087 fdt_addr_t base = plat->base;
Patrice Chotard21aad132017-09-27 15:44:53 +020088 int ret;
Patrice Chotard5011e6f2017-09-27 15:44:50 +020089 bool stm32f4;
90 u8 uart_enable_bit;
91
92 plat->uart_info = (struct stm32_uart_info *)dev_get_driver_data(dev);
93 stm32f4 = plat->uart_info->stm32f4;
94 uart_enable_bit = plat->uart_info->uart_enable_bit;
Vikas Manocha7b00ff92017-02-12 10:25:46 -080095
Vikas Manocha7b00ff92017-02-12 10:25:46 -080096 ret = clk_get_by_index(dev, 0, &clk);
97 if (ret < 0)
98 return ret;
99
100 ret = clk_enable(&clk);
101 if (ret) {
102 dev_err(dev, "failed to enable clock\n");
103 return ret;
104 }
Vikas Manocha7b00ff92017-02-12 10:25:46 -0800105
Patrice Chotard4809a192017-07-18 09:29:08 +0200106 plat->clock_rate = clk_get_rate(&clk);
107 if (plat->clock_rate < 0) {
108 clk_disable(&clk);
109 return plat->clock_rate;
110 };
111
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200112 /* Disable uart-> disable overrun-> enable uart */
113 clrbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
114 BIT(uart_enable_bit));
115 if (plat->uart_info->has_overrun_disable)
116 setbits_le32(base + CR3_OFFSET(stm32f4), USART_CR3_OVRDIS);
Patrice Chotard24fc72d2017-09-27 15:44:51 +0200117 if (plat->uart_info->has_fifo)
118 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_FIFOEN);
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200119 setbits_le32(base + CR1_OFFSET(stm32f4), USART_CR1_RE | USART_CR1_TE |
120 BIT(uart_enable_bit));
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800121
122 return 0;
123}
124
Vikas Manocha19e22c62017-02-12 10:25:44 -0800125static const struct udevice_id stm32_serial_id[] = {
Patrice Chotardb21a69a2017-09-27 15:44:52 +0200126 { .compatible = "st,stm32-uart", .data = (ulong)&stm32f4_info},
Patrice Chotard24fc72d2017-09-27 15:44:51 +0200127 { .compatible = "st,stm32f7-uart", .data = (ulong)&stm32f7_info},
128 { .compatible = "st,stm32h7-uart", .data = (ulong)&stm32h7_info},
Vikas Manocha19e22c62017-02-12 10:25:44 -0800129 {}
130};
131
132static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
133{
134 struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
Vikas Manocha19e22c62017-02-12 10:25:44 -0800135
Patrice Chotard5011e6f2017-09-27 15:44:50 +0200136 plat->base = devfdt_get_addr(dev);
137 if (plat->base == FDT_ADDR_T_NONE)
Vikas Manocha19e22c62017-02-12 10:25:44 -0800138 return -EINVAL;
139
Vikas Manocha19e22c62017-02-12 10:25:44 -0800140 return 0;
141}
Vikas Manocha19e22c62017-02-12 10:25:44 -0800142
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800143static const struct dm_serial_ops stm32_serial_ops = {
144 .putc = stm32_serial_putc,
145 .pending = stm32_serial_pending,
146 .getc = stm32_serial_getc,
147 .setbrg = stm32_serial_setbrg,
148};
149
150U_BOOT_DRIVER(serial_stm32) = {
151 .name = "serial_stm32x7",
152 .id = UCLASS_SERIAL,
Vikas Manocha19e22c62017-02-12 10:25:44 -0800153 .of_match = of_match_ptr(stm32_serial_id),
154 .ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
155 .platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
Vikas Manocha5dba05e2016-02-11 15:47:19 -0800156 .ops = &stm32_serial_ops,
157 .probe = stm32_serial_probe,
158 .flags = DM_FLAG_PRE_RELOC,
159};