2016-02-11 23:47:19 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2016
|
|
|
|
* Vikas Manocha, <vikas.manocha@st.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2017-02-12 18:25:46 +00:00
|
|
|
#include <clk.h>
|
2016-02-11 23:47:19 +00:00
|
|
|
#include <dm.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <serial.h>
|
2016-07-07 16:02:24 +00:00
|
|
|
#include <asm/arch/stm32.h>
|
2016-02-11 23:47:19 +00:00
|
|
|
#include "serial_stm32x7.h"
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
static int stm32_serial_setbrg(struct udevice *dev, int baudrate)
|
|
|
|
{
|
|
|
|
struct stm32x7_serial_platdata *plat = dev->platdata;
|
|
|
|
struct stm32_usart *const usart = plat->base;
|
2017-07-18 07:29:08 +00:00
|
|
|
u32 int_div, mantissa, fraction, oversampling;
|
2016-07-07 16:02:24 +00:00
|
|
|
|
2017-07-18 07:29:08 +00:00
|
|
|
int_div = DIV_ROUND_CLOSEST(plat->clock_rate, baudrate);
|
2017-06-08 07:26:55 +00:00
|
|
|
|
|
|
|
if (int_div < 16) {
|
|
|
|
oversampling = 8;
|
|
|
|
setbits_le32(&usart->cr1, USART_CR1_OVER8);
|
|
|
|
} else {
|
|
|
|
oversampling = 16;
|
|
|
|
clrbits_le32(&usart->cr1, USART_CR1_OVER8);
|
|
|
|
}
|
|
|
|
|
|
|
|
mantissa = (int_div / oversampling) << USART_BRR_M_SHIFT;
|
|
|
|
fraction = int_div % oversampling;
|
|
|
|
|
|
|
|
writel(mantissa | fraction, &usart->brr);
|
2016-02-11 23:47:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_serial_getc(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct stm32x7_serial_platdata *plat = dev->platdata;
|
|
|
|
struct stm32_usart *const usart = plat->base;
|
|
|
|
|
|
|
|
if ((readl(&usart->sr) & USART_SR_FLAG_RXNE) == 0)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
return readl(&usart->rd_dr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_serial_putc(struct udevice *dev, const char c)
|
|
|
|
{
|
|
|
|
struct stm32x7_serial_platdata *plat = dev->platdata;
|
|
|
|
struct stm32_usart *const usart = plat->base;
|
|
|
|
|
|
|
|
if ((readl(&usart->sr) & USART_SR_FLAG_TXE) == 0)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
writel(c, &usart->tx_dr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_serial_pending(struct udevice *dev, bool input)
|
|
|
|
{
|
|
|
|
struct stm32x7_serial_platdata *plat = dev->platdata;
|
|
|
|
struct stm32_usart *const usart = plat->base;
|
|
|
|
|
|
|
|
if (input)
|
|
|
|
return readl(&usart->sr) & USART_SR_FLAG_RXNE ? 1 : 0;
|
|
|
|
else
|
|
|
|
return readl(&usart->sr) & USART_SR_FLAG_TXE ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32_serial_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct stm32x7_serial_platdata *plat = dev->platdata;
|
|
|
|
struct stm32_usart *const usart = plat->base;
|
2017-02-12 18:25:46 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_CLK
|
|
|
|
int ret;
|
|
|
|
struct clk clk;
|
|
|
|
|
|
|
|
ret = clk_get_by_index(dev, 0, &clk);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = clk_enable(&clk);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "failed to enable clock\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-07-18 07:29:08 +00:00
|
|
|
plat->clock_rate = clk_get_rate(&clk);
|
|
|
|
if (plat->clock_rate < 0) {
|
|
|
|
clk_disable(&clk);
|
|
|
|
return plat->clock_rate;
|
|
|
|
};
|
|
|
|
|
2017-05-28 19:55:12 +00:00
|
|
|
/* Disable usart-> disable overrun-> enable usart */
|
|
|
|
clrbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
|
|
|
|
setbits_le32(&usart->cr3, USART_CR3_OVRDIS);
|
2016-02-11 23:47:19 +00:00
|
|
|
setbits_le32(&usart->cr1, USART_CR1_RE | USART_CR1_TE | USART_CR1_UE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-12 18:25:44 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL)
|
|
|
|
static const struct udevice_id stm32_serial_id[] = {
|
serial: stm32x7: align compatible with kernel one
stm32x7.c driver is dedicated for STM32F7.
In kernel, "st,stm32-usart" and "st,stm32-uart" compatible
strings are dedicated for STM32F4.
To keep U-boot and kernel aligned, replace the serial compatible
string from "st,stm32-usart", "st,stm32-uart" to
"st,stm32f7-usart", "st,stm32f7-uart" specific for STM32F7.
Signed-off-by: Patrice Chotard <patrice.chotard@st.com>
Reviewed-by: Christophe KERELLO <christophe.kerello@st.com>
Reviewed-by: Patrick DELAUNAY <patrick.delaunay@st.com>
Acked-by: Vikas MANOCHA <vikas.manocha@st.com>
2017-06-08 07:26:54 +00:00
|
|
|
{.compatible = "st,stm32f7-usart"},
|
|
|
|
{.compatible = "st,stm32f7-uart"},
|
2017-02-12 18:25:44 +00:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int stm32_serial_ofdata_to_platdata(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct stm32x7_serial_platdata *plat = dev_get_platdata(dev);
|
|
|
|
fdt_addr_t addr;
|
|
|
|
|
2017-05-17 23:18:05 +00:00
|
|
|
addr = devfdt_get_addr(dev);
|
2017-02-12 18:25:44 +00:00
|
|
|
if (addr == FDT_ADDR_T_NONE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
plat->base = (struct stm32_usart *)addr;
|
2017-02-12 18:25:46 +00:00
|
|
|
|
2017-02-12 18:25:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-11 23:47:19 +00:00
|
|
|
static const struct dm_serial_ops stm32_serial_ops = {
|
|
|
|
.putc = stm32_serial_putc,
|
|
|
|
.pending = stm32_serial_pending,
|
|
|
|
.getc = stm32_serial_getc,
|
|
|
|
.setbrg = stm32_serial_setbrg,
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(serial_stm32) = {
|
|
|
|
.name = "serial_stm32x7",
|
|
|
|
.id = UCLASS_SERIAL,
|
2017-02-12 18:25:44 +00:00
|
|
|
.of_match = of_match_ptr(stm32_serial_id),
|
|
|
|
.ofdata_to_platdata = of_match_ptr(stm32_serial_ofdata_to_platdata),
|
|
|
|
.platdata_auto_alloc_size = sizeof(struct stm32x7_serial_platdata),
|
2016-02-11 23:47:19 +00:00
|
|
|
.ops = &stm32_serial_ops,
|
|
|
|
.probe = stm32_serial_probe,
|
|
|
|
.flags = DM_FLAG_PRE_RELOC,
|
|
|
|
};
|