2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2012-09-14 00:55:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
|
|
|
|
* Copyright (C) 2011-2012 Xilinx, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2016-07-14 12:40:03 +00:00
|
|
|
#include <clk.h>
|
2012-09-14 00:55:24 +00:00
|
|
|
#include <common.h>
|
2015-10-18 01:41:27 +00:00
|
|
|
#include <debug_uart.h>
|
|
|
|
#include <dm.h>
|
2015-10-18 01:41:22 +00:00
|
|
|
#include <errno.h>
|
2014-02-24 10:16:33 +00:00
|
|
|
#include <fdtdec.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2012-09-14 00:55:24 +00:00
|
|
|
#include <watchdog.h>
|
|
|
|
#include <asm/io.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <dm/device_compat.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2012-09-14 00:55:24 +00:00
|
|
|
#include <linux/compiler.h>
|
|
|
|
#include <serial.h>
|
2020-02-03 14:36:15 +00:00
|
|
|
#include <linux/err.h>
|
2012-09-14 00:55:24 +00:00
|
|
|
|
2018-06-14 09:13:41 +00:00
|
|
|
#define ZYNQ_UART_SR_TXACTIVE BIT(11) /* TX active */
|
2018-06-14 07:43:34 +00:00
|
|
|
#define ZYNQ_UART_SR_TXFULL BIT(4) /* TX FIFO full */
|
2022-03-25 10:50:07 +00:00
|
|
|
#define ZYNQ_UART_SR_TXEMPTY BIT(3) /* TX FIFO empty */
|
2018-06-14 09:13:41 +00:00
|
|
|
#define ZYNQ_UART_SR_RXEMPTY BIT(1) /* RX FIFO empty */
|
|
|
|
|
|
|
|
#define ZYNQ_UART_CR_TX_EN BIT(4) /* TX enabled */
|
|
|
|
#define ZYNQ_UART_CR_RX_EN BIT(2) /* RX enabled */
|
|
|
|
#define ZYNQ_UART_CR_TXRST BIT(1) /* TX logic reset */
|
|
|
|
#define ZYNQ_UART_CR_RXRST BIT(0) /* RX logic reset */
|
2012-09-14 00:55:24 +00:00
|
|
|
|
2021-06-25 11:19:11 +00:00
|
|
|
#define ZYNQ_UART_MR_STOPMODE_2_BIT 0x00000080 /* 2 stop bits */
|
|
|
|
#define ZYNQ_UART_MR_STOPMODE_1_5_BIT 0x00000040 /* 1.5 stop bits */
|
|
|
|
#define ZYNQ_UART_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
|
|
|
|
|
2012-09-14 00:55:24 +00:00
|
|
|
#define ZYNQ_UART_MR_PARITY_NONE 0x00000020 /* No parity mode */
|
2021-06-25 11:19:11 +00:00
|
|
|
#define ZYNQ_UART_MR_PARITY_ODD 0x00000008 /* Odd parity mode */
|
|
|
|
#define ZYNQ_UART_MR_PARITY_EVEN 0x00000000 /* Even parity mode */
|
|
|
|
|
|
|
|
#define ZYNQ_UART_MR_CHARLEN_6_BIT 0x00000006 /* 6 bits data */
|
|
|
|
#define ZYNQ_UART_MR_CHARLEN_7_BIT 0x00000004 /* 7 bits data */
|
|
|
|
#define ZYNQ_UART_MR_CHARLEN_8_BIT 0x00000000 /* 8 bits data */
|
2012-09-14 00:55:24 +00:00
|
|
|
|
|
|
|
struct uart_zynq {
|
2015-01-07 14:00:47 +00:00
|
|
|
u32 control; /* 0x0 - Control Register [8:0] */
|
|
|
|
u32 mode; /* 0x4 - Mode Register [10:0] */
|
2012-09-14 00:55:24 +00:00
|
|
|
u32 reserved1[4];
|
2015-01-07 14:00:47 +00:00
|
|
|
u32 baud_rate_gen; /* 0x18 - Baud Rate Generator [15:0] */
|
2012-09-14 00:55:24 +00:00
|
|
|
u32 reserved2[4];
|
2015-01-07 14:00:47 +00:00
|
|
|
u32 channel_sts; /* 0x2c - Channel Status [11:0] */
|
|
|
|
u32 tx_rx_fifo; /* 0x30 - FIFO [15:0] or [7:0] */
|
|
|
|
u32 baud_rate_divider; /* 0x34 - Baud Rate Divider [7:0] */
|
2012-09-14 00:55:24 +00:00
|
|
|
};
|
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
struct zynq_uart_plat {
|
2015-10-18 01:41:27 +00:00
|
|
|
struct uart_zynq *regs;
|
2013-12-19 18:08:58 +00:00
|
|
|
};
|
|
|
|
|
2020-03-24 10:31:42 +00:00
|
|
|
/* Set up the baud rate */
|
2015-10-18 01:41:22 +00:00
|
|
|
static void _uart_zynq_serial_setbrg(struct uart_zynq *regs,
|
|
|
|
unsigned long clock, unsigned long baud)
|
2012-09-14 00:55:24 +00:00
|
|
|
{
|
|
|
|
/* Calculation results. */
|
|
|
|
unsigned int calc_bauderror, bdiv, bgen;
|
|
|
|
unsigned long calc_baud = 0;
|
|
|
|
|
2015-04-15 11:05:06 +00:00
|
|
|
/* Covering case where input clock is so slow */
|
2015-10-18 01:41:22 +00:00
|
|
|
if (clock < 1000000 && baud > 4800)
|
|
|
|
baud = 4800;
|
2015-04-15 11:05:06 +00:00
|
|
|
|
2012-09-14 00:55:24 +00:00
|
|
|
/* master clock
|
|
|
|
* Baud rate = ------------------
|
|
|
|
* bgen * (bdiv + 1)
|
|
|
|
*
|
|
|
|
* Find acceptable values for baud generation.
|
|
|
|
*/
|
|
|
|
for (bdiv = 4; bdiv < 255; bdiv++) {
|
|
|
|
bgen = clock / (baud * (bdiv + 1));
|
|
|
|
if (bgen < 2 || bgen > 65535)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
calc_baud = clock / (bgen * (bdiv + 1));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use first calculated baudrate with
|
|
|
|
* an acceptable (<3%) error
|
|
|
|
*/
|
|
|
|
if (baud > calc_baud)
|
|
|
|
calc_bauderror = baud - calc_baud;
|
|
|
|
else
|
|
|
|
calc_bauderror = calc_baud - baud;
|
|
|
|
if (((calc_bauderror * 100) / baud) < 3)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
writel(bdiv, ®s->baud_rate_divider);
|
|
|
|
writel(bgen, ®s->baud_rate_gen);
|
|
|
|
}
|
|
|
|
|
2015-10-18 01:41:22 +00:00
|
|
|
/* Initialize the UART, with...some settings. */
|
|
|
|
static void _uart_zynq_serial_init(struct uart_zynq *regs)
|
|
|
|
{
|
2012-09-14 00:55:24 +00:00
|
|
|
/* RX/TX enabled & reset */
|
|
|
|
writel(ZYNQ_UART_CR_TX_EN | ZYNQ_UART_CR_RX_EN | ZYNQ_UART_CR_TXRST | \
|
|
|
|
ZYNQ_UART_CR_RXRST, ®s->control);
|
|
|
|
writel(ZYNQ_UART_MR_PARITY_NONE, ®s->mode); /* 8 bit, no parity */
|
2015-10-18 01:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _uart_zynq_serial_putc(struct uart_zynq *regs, const char c)
|
|
|
|
{
|
2022-03-25 10:50:07 +00:00
|
|
|
if (CONFIG_IS_ENABLED(DEBUG_UART_ZYNQ)) {
|
|
|
|
if (!(readl(®s->channel_sts) & ZYNQ_UART_SR_TXEMPTY))
|
|
|
|
return -EAGAIN;
|
|
|
|
} else {
|
|
|
|
if (readl(®s->channel_sts) & ZYNQ_UART_SR_TXFULL)
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2015-10-18 01:41:22 +00:00
|
|
|
|
|
|
|
writel(c, ®s->tx_rx_fifo);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-14 09:19:57 +00:00
|
|
|
static int zynq_serial_setbrg(struct udevice *dev, int baudrate)
|
2012-09-14 00:55:24 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct zynq_uart_plat *plat = dev_get_plat(dev);
|
2016-07-14 12:40:03 +00:00
|
|
|
unsigned long clock;
|
2012-09-14 00:55:24 +00:00
|
|
|
|
2016-07-14 12:40:03 +00:00
|
|
|
int ret;
|
|
|
|
struct clk clk;
|
|
|
|
|
|
|
|
ret = clk_get_by_index(dev, 0, &clk);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(dev, "failed to get clock\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
clock = clk_get_rate(&clk);
|
|
|
|
if (IS_ERR_VALUE(clock)) {
|
|
|
|
dev_err(dev, "failed to get rate\n");
|
|
|
|
return clock;
|
|
|
|
}
|
|
|
|
debug("%s: CLK %ld\n", __func__, clock);
|
|
|
|
|
|
|
|
ret = clk_enable(&clk);
|
2021-02-09 14:28:15 +00:00
|
|
|
if (ret) {
|
2016-07-14 12:40:03 +00:00
|
|
|
dev_err(dev, "failed to enable clock\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-17 15:27:30 +00:00
|
|
|
|
2020-12-03 23:55:18 +00:00
|
|
|
_uart_zynq_serial_setbrg(plat->regs, clock, baudrate);
|
2012-09-14 00:55:24 +00:00
|
|
|
|
2015-10-18 01:41:27 +00:00
|
|
|
return 0;
|
2012-09-14 00:55:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 11:19:11 +00:00
|
|
|
#if !defined(CONFIG_SPL_BUILD)
|
|
|
|
static int zynq_serial_setconfig(struct udevice *dev, uint serial_config)
|
|
|
|
{
|
|
|
|
struct zynq_uart_plat *plat = dev_get_plat(dev);
|
|
|
|
struct uart_zynq *regs = plat->regs;
|
|
|
|
u32 val = 0;
|
|
|
|
|
|
|
|
switch (SERIAL_GET_BITS(serial_config)) {
|
|
|
|
case SERIAL_6_BITS:
|
|
|
|
val |= ZYNQ_UART_MR_CHARLEN_6_BIT;
|
|
|
|
break;
|
|
|
|
case SERIAL_7_BITS:
|
|
|
|
val |= ZYNQ_UART_MR_CHARLEN_7_BIT;
|
|
|
|
break;
|
|
|
|
case SERIAL_8_BITS:
|
|
|
|
val |= ZYNQ_UART_MR_CHARLEN_8_BIT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOTSUPP; /* not supported in driver */
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (SERIAL_GET_STOP(serial_config)) {
|
|
|
|
case SERIAL_ONE_STOP:
|
|
|
|
val |= ZYNQ_UART_MR_STOPMODE_1_BIT;
|
|
|
|
break;
|
|
|
|
case SERIAL_ONE_HALF_STOP:
|
|
|
|
val |= ZYNQ_UART_MR_STOPMODE_1_5_BIT;
|
|
|
|
break;
|
|
|
|
case SERIAL_TWO_STOP:
|
|
|
|
val |= ZYNQ_UART_MR_STOPMODE_2_BIT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOTSUPP; /* not supported in driver */
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (SERIAL_GET_PARITY(serial_config)) {
|
|
|
|
case SERIAL_PAR_NONE:
|
|
|
|
val |= ZYNQ_UART_MR_PARITY_NONE;
|
|
|
|
break;
|
|
|
|
case SERIAL_PAR_ODD:
|
|
|
|
val |= ZYNQ_UART_MR_PARITY_ODD;
|
|
|
|
break;
|
|
|
|
case SERIAL_PAR_EVEN:
|
|
|
|
val |= ZYNQ_UART_MR_PARITY_EVEN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -ENOTSUPP; /* not supported in driver */
|
|
|
|
}
|
|
|
|
|
|
|
|
writel(val, ®s->mode);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define zynq_serial_setconfig NULL
|
|
|
|
#endif
|
|
|
|
|
2015-10-18 01:41:27 +00:00
|
|
|
static int zynq_serial_probe(struct udevice *dev)
|
2012-09-14 00:55:24 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct zynq_uart_plat *plat = dev_get_plat(dev);
|
2020-12-03 23:55:18 +00:00
|
|
|
struct uart_zynq *regs = plat->regs;
|
2020-03-24 10:31:42 +00:00
|
|
|
u32 val;
|
2012-09-14 00:55:24 +00:00
|
|
|
|
2020-03-24 10:31:42 +00:00
|
|
|
/* No need to reinitialize the UART if TX already enabled */
|
|
|
|
val = readl(®s->control);
|
|
|
|
if (val & ZYNQ_UART_CR_TX_EN)
|
2018-06-14 08:41:35 +00:00
|
|
|
return 0;
|
|
|
|
|
2020-12-03 23:55:18 +00:00
|
|
|
_uart_zynq_serial_init(plat->regs);
|
2012-09-14 00:55:24 +00:00
|
|
|
|
2015-10-18 01:41:27 +00:00
|
|
|
return 0;
|
2012-09-14 00:55:24 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 01:41:27 +00:00
|
|
|
static int zynq_serial_getc(struct udevice *dev)
|
2012-09-14 00:55:24 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct zynq_uart_plat *plat = dev_get_plat(dev);
|
2020-12-03 23:55:18 +00:00
|
|
|
struct uart_zynq *regs = plat->regs;
|
2015-10-18 01:41:27 +00:00
|
|
|
|
|
|
|
if (readl(®s->channel_sts) & ZYNQ_UART_SR_RXEMPTY)
|
|
|
|
return -EAGAIN;
|
2012-09-14 00:55:24 +00:00
|
|
|
|
|
|
|
return readl(®s->tx_rx_fifo);
|
|
|
|
}
|
|
|
|
|
2015-10-18 01:41:27 +00:00
|
|
|
static int zynq_serial_putc(struct udevice *dev, const char ch)
|
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct zynq_uart_plat *plat = dev_get_plat(dev);
|
2012-09-14 00:55:24 +00:00
|
|
|
|
2020-12-03 23:55:18 +00:00
|
|
|
return _uart_zynq_serial_putc(plat->regs, ch);
|
2015-10-18 01:41:27 +00:00
|
|
|
}
|
2012-09-14 00:55:24 +00:00
|
|
|
|
2015-10-18 01:41:27 +00:00
|
|
|
static int zynq_serial_pending(struct udevice *dev, bool input)
|
2014-02-24 10:16:33 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct zynq_uart_plat *plat = dev_get_plat(dev);
|
2020-12-03 23:55:18 +00:00
|
|
|
struct uart_zynq *regs = plat->regs;
|
2014-02-24 10:16:33 +00:00
|
|
|
|
2015-10-18 01:41:27 +00:00
|
|
|
if (input)
|
|
|
|
return !(readl(®s->channel_sts) & ZYNQ_UART_SR_RXEMPTY);
|
|
|
|
else
|
|
|
|
return !!(readl(®s->channel_sts) & ZYNQ_UART_SR_TXACTIVE);
|
|
|
|
}
|
2014-02-24 10:16:33 +00:00
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int zynq_serial_of_to_plat(struct udevice *dev)
|
2015-10-18 01:41:27 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct zynq_uart_plat *plat = dev_get_plat(dev);
|
2014-02-24 10:16:33 +00:00
|
|
|
|
2020-12-03 23:55:18 +00:00
|
|
|
plat->regs = (struct uart_zynq *)dev_read_addr(dev);
|
|
|
|
if (IS_ERR(plat->regs))
|
|
|
|
return PTR_ERR(plat->regs);
|
2014-02-24 10:16:33 +00:00
|
|
|
|
2015-10-18 01:41:27 +00:00
|
|
|
return 0;
|
2014-02-24 10:16:33 +00:00
|
|
|
}
|
2012-10-08 21:46:23 +00:00
|
|
|
|
2015-10-18 01:41:27 +00:00
|
|
|
static const struct dm_serial_ops zynq_serial_ops = {
|
|
|
|
.putc = zynq_serial_putc,
|
|
|
|
.pending = zynq_serial_pending,
|
|
|
|
.getc = zynq_serial_getc,
|
|
|
|
.setbrg = zynq_serial_setbrg,
|
2021-06-25 11:19:11 +00:00
|
|
|
.setconfig = zynq_serial_setconfig,
|
2015-10-18 01:41:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id zynq_serial_ids[] = {
|
|
|
|
{ .compatible = "xlnx,xuartps" },
|
|
|
|
{ .compatible = "cdns,uart-r1p8" },
|
2016-01-14 10:45:52 +00:00
|
|
|
{ .compatible = "cdns,uart-r1p12" },
|
2022-01-11 12:55:19 +00:00
|
|
|
{ .compatible = "xlnx,zynqmp-uart" },
|
2015-10-18 01:41:27 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2015-12-01 13:29:34 +00:00
|
|
|
U_BOOT_DRIVER(serial_zynq) = {
|
2015-10-18 01:41:27 +00:00
|
|
|
.name = "serial_zynq",
|
|
|
|
.id = UCLASS_SERIAL,
|
|
|
|
.of_match = zynq_serial_ids,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = zynq_serial_of_to_plat,
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct zynq_uart_plat),
|
2015-10-18 01:41:27 +00:00
|
|
|
.probe = zynq_serial_probe,
|
|
|
|
.ops = &zynq_serial_ops,
|
|
|
|
};
|
2015-10-18 01:41:22 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_UART_ZYNQ
|
2016-01-05 11:49:21 +00:00
|
|
|
static inline void _debug_uart_init(void)
|
2015-10-18 01:41:22 +00:00
|
|
|
{
|
2022-05-27 20:15:24 +00:00
|
|
|
struct uart_zynq *regs = (struct uart_zynq *)CONFIG_VAL(DEBUG_UART_BASE);
|
2015-10-18 01:41:22 +00:00
|
|
|
|
|
|
|
_uart_zynq_serial_init(regs);
|
|
|
|
_uart_zynq_serial_setbrg(regs, CONFIG_DEBUG_UART_CLOCK,
|
|
|
|
CONFIG_BAUDRATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void _debug_uart_putc(int ch)
|
|
|
|
{
|
2022-05-27 20:15:24 +00:00
|
|
|
struct uart_zynq *regs = (struct uart_zynq *)CONFIG_VAL(DEBUG_UART_BASE);
|
2015-10-18 01:41:22 +00:00
|
|
|
|
|
|
|
while (_uart_zynq_serial_putc(regs, ch) == -EAGAIN)
|
|
|
|
WATCHDOG_RESET();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_UART_FUNCS
|
|
|
|
|
|
|
|
#endif
|