2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2009-10-01 08:20:28 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2009 SAMSUNG Electronics
|
|
|
|
* Minkyu Kang <mk7.kang@samsung.com>
|
|
|
|
* Heungjun Kim <riverful.kim@samsung.com>
|
|
|
|
*
|
|
|
|
* based on drivers/serial/s3c64xx.c
|
|
|
|
*/
|
|
|
|
|
2014-09-14 22:36:17 +00:00
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
2013-06-24 11:17:22 +00:00
|
|
|
#include <fdtdec.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2011-04-29 18:03:29 +00:00
|
|
|
#include <linux/compiler.h>
|
2009-10-01 08:20:28 +00:00
|
|
|
#include <asm/io.h>
|
2023-01-26 13:44:09 +00:00
|
|
|
#if !IS_ENABLED(CONFIG_ARCH_APPLE)
|
2009-10-01 08:20:28 +00:00
|
|
|
#include <asm/arch/clk.h>
|
2021-10-23 14:58:04 +00:00
|
|
|
#endif
|
2015-07-03 00:15:53 +00:00
|
|
|
#include <asm/arch/uart.h>
|
2009-10-01 08:20:28 +00:00
|
|
|
#include <serial.h>
|
2016-04-23 16:48:11 +00:00
|
|
|
#include <clk.h>
|
2009-10-01 08:20:28 +00:00
|
|
|
|
2021-10-23 14:58:04 +00:00
|
|
|
enum {
|
|
|
|
PORT_S5P = 0,
|
|
|
|
PORT_S5L
|
|
|
|
};
|
|
|
|
|
2023-11-07 19:06:00 +00:00
|
|
|
#define UFCON_FIFO_EN BIT(0)
|
|
|
|
#define UFCON_RX_FIFO_RESET BIT(1)
|
|
|
|
#define UMCON_RESET_VAL 0x0
|
|
|
|
#define ULCON_WORD_8_BIT 0x3
|
|
|
|
#define UCON_RX_IRQ_OR_POLLING BIT(0)
|
|
|
|
#define UCON_TX_IRQ_OR_POLLING BIT(2)
|
|
|
|
#define UCON_RX_ERR_IRQ_EN BIT(6)
|
|
|
|
#define UCON_TX_IRQ_LEVEL BIT(9)
|
|
|
|
|
2021-10-23 14:58:04 +00:00
|
|
|
#define S5L_RX_FIFO_COUNT_SHIFT 0
|
|
|
|
#define S5L_RX_FIFO_COUNT_MASK (0xf << S5L_RX_FIFO_COUNT_SHIFT)
|
2023-11-07 19:06:00 +00:00
|
|
|
#define S5L_RX_FIFO_FULL BIT(8)
|
2021-10-23 14:58:04 +00:00
|
|
|
#define S5L_TX_FIFO_COUNT_SHIFT 4
|
|
|
|
#define S5L_TX_FIFO_COUNT_MASK (0xf << S5L_TX_FIFO_COUNT_SHIFT)
|
2023-11-07 19:06:00 +00:00
|
|
|
#define S5L_TX_FIFO_FULL BIT(9)
|
2021-10-23 14:58:04 +00:00
|
|
|
|
|
|
|
#define S5P_RX_FIFO_COUNT_SHIFT 0
|
|
|
|
#define S5P_RX_FIFO_COUNT_MASK (0xff << S5P_RX_FIFO_COUNT_SHIFT)
|
2023-11-07 19:06:00 +00:00
|
|
|
#define S5P_RX_FIFO_FULL BIT(8)
|
2021-10-23 14:58:04 +00:00
|
|
|
#define S5P_TX_FIFO_COUNT_SHIFT 16
|
|
|
|
#define S5P_TX_FIFO_COUNT_MASK (0xff << S5P_TX_FIFO_COUNT_SHIFT)
|
2023-11-07 19:06:00 +00:00
|
|
|
#define S5P_TX_FIFO_FULL BIT(24)
|
2013-03-21 20:33:04 +00:00
|
|
|
|
2013-06-24 11:17:22 +00:00
|
|
|
/* Information about a serial port */
|
2020-12-03 23:55:23 +00:00
|
|
|
struct s5p_serial_plat {
|
2023-11-07 19:06:01 +00:00
|
|
|
struct s5p_uart *reg; /* address of registers in physical memory */
|
|
|
|
u8 reg_width; /* register width */
|
|
|
|
u8 port_id; /* uart port number */
|
2021-10-23 14:58:04 +00:00
|
|
|
u8 rx_fifo_count_shift;
|
|
|
|
u8 tx_fifo_count_shift;
|
|
|
|
u32 rx_fifo_count_mask;
|
|
|
|
u32 tx_fifo_count_mask;
|
|
|
|
u32 rx_fifo_full;
|
|
|
|
u32 tx_fifo_full;
|
2014-09-14 22:36:17 +00:00
|
|
|
};
|
2009-10-01 08:20:28 +00:00
|
|
|
|
|
|
|
/*
|
2010-03-24 07:59:30 +00:00
|
|
|
* The coefficient, used to calculate the baudrate on S5P UARTs is
|
2009-10-01 08:20:28 +00:00
|
|
|
* calculated as
|
|
|
|
* C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
|
2023-11-07 19:06:01 +00:00
|
|
|
* however, section 31.6.11 of the datasheet doesn't recommend using 1 for 1,
|
2009-10-01 08:20:28 +00:00
|
|
|
* 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
|
|
|
|
*/
|
|
|
|
static const int udivslot[] = {
|
|
|
|
0,
|
|
|
|
0x0080,
|
|
|
|
0x0808,
|
|
|
|
0x0888,
|
|
|
|
0x2222,
|
|
|
|
0x4924,
|
|
|
|
0x4a52,
|
|
|
|
0x54aa,
|
|
|
|
0x5555,
|
|
|
|
0xd555,
|
|
|
|
0xd5d5,
|
|
|
|
0xddd5,
|
|
|
|
0xdddd,
|
|
|
|
0xdfdd,
|
|
|
|
0xdfdf,
|
|
|
|
0xffdf,
|
|
|
|
};
|
|
|
|
|
2015-07-03 00:15:53 +00:00
|
|
|
static void __maybe_unused s5p_serial_init(struct s5p_uart *uart)
|
|
|
|
{
|
2023-11-07 19:06:00 +00:00
|
|
|
/* Enable FIFOs, auto clear Rx FIFO */
|
|
|
|
writel(UFCON_FIFO_EN | UFCON_RX_FIFO_RESET, &uart->ufcon);
|
|
|
|
/* No auto flow control, disable nRTS signal */
|
|
|
|
writel(UMCON_RESET_VAL, &uart->umcon);
|
|
|
|
/* 8N1, no parity bit */
|
|
|
|
writel(ULCON_WORD_8_BIT, &uart->ulcon);
|
2015-07-03 00:15:53 +00:00
|
|
|
/* No interrupts, no DMA, pure polling */
|
2023-11-07 19:06:00 +00:00
|
|
|
writel(UCON_RX_IRQ_OR_POLLING | UCON_TX_IRQ_OR_POLLING |
|
|
|
|
UCON_RX_ERR_IRQ_EN | UCON_TX_IRQ_LEVEL, &uart->ucon);
|
2015-07-03 00:15:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 14:58:04 +00:00
|
|
|
static void __maybe_unused s5p_serial_baud(struct s5p_uart *uart, u8 reg_width,
|
|
|
|
uint uclk, int baudrate)
|
2009-10-01 08:20:28 +00:00
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
|
2010-08-24 06:51:55 +00:00
|
|
|
val = uclk / baudrate;
|
2009-10-01 08:20:28 +00:00
|
|
|
|
|
|
|
writel(val / 16 - 1, &uart->ubrdiv);
|
2010-09-28 05:35:02 +00:00
|
|
|
|
2011-01-24 05:43:25 +00:00
|
|
|
if (s5p_uart_divslot())
|
2010-09-28 05:35:02 +00:00
|
|
|
writew(udivslot[val % 16], &uart->rest.slot);
|
2021-10-23 14:58:04 +00:00
|
|
|
else if (reg_width == 4)
|
|
|
|
writel(val % 16, &uart->rest.value);
|
2010-09-28 05:35:02 +00:00
|
|
|
else
|
|
|
|
writeb(val % 16, &uart->rest.value);
|
2015-07-03 00:15:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:15:55 +00:00
|
|
|
#ifndef CONFIG_SPL_BUILD
|
2015-07-03 00:15:53 +00:00
|
|
|
int s5p_serial_setbrg(struct udevice *dev, int baudrate)
|
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct s5p_serial_plat *plat = dev_get_plat(dev);
|
2015-07-03 00:15:53 +00:00
|
|
|
struct s5p_uart *const uart = plat->reg;
|
2016-04-23 16:48:11 +00:00
|
|
|
u32 uclk;
|
|
|
|
|
2023-01-26 13:44:09 +00:00
|
|
|
#if IS_ENABLED(CONFIG_CLK_EXYNOS) || IS_ENABLED(CONFIG_ARCH_APPLE)
|
2016-06-17 15:44:00 +00:00
|
|
|
struct clk clk;
|
2023-11-07 17:34:17 +00:00
|
|
|
int ret;
|
2016-04-23 16:48:11 +00:00
|
|
|
|
2016-06-17 15:44:00 +00:00
|
|
|
ret = clk_get_by_index(dev, 1, &clk);
|
2016-04-23 16:48:11 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-06-17 15:44:00 +00:00
|
|
|
uclk = clk_get_rate(&clk);
|
2016-04-23 16:48:11 +00:00
|
|
|
#else
|
|
|
|
uclk = get_uart_clk(plat->port_id);
|
|
|
|
#endif
|
2015-07-03 00:15:53 +00:00
|
|
|
|
2021-10-23 14:58:04 +00:00
|
|
|
s5p_serial_baud(uart, plat->reg_width, uclk, baudrate);
|
2014-09-14 22:36:17 +00:00
|
|
|
|
|
|
|
return 0;
|
2009-10-01 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 22:36:17 +00:00
|
|
|
static int s5p_serial_probe(struct udevice *dev)
|
2009-10-01 08:20:28 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct s5p_serial_plat *plat = dev_get_plat(dev);
|
2014-09-14 22:36:17 +00:00
|
|
|
struct s5p_uart *const uart = plat->reg;
|
2009-10-01 08:20:28 +00:00
|
|
|
|
2015-07-03 00:15:53 +00:00
|
|
|
s5p_serial_init(uart);
|
2009-10-01 08:20:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-14 22:36:17 +00:00
|
|
|
static int serial_err_check(const struct s5p_uart *const uart, int op)
|
2009-10-01 08:20:28 +00:00
|
|
|
{
|
2009-11-10 11:23:50 +00:00
|
|
|
unsigned int mask;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UERSTAT
|
|
|
|
* Break Detect [3]
|
|
|
|
* Frame Err [2] : receive operation
|
|
|
|
* Parity Err [1] : receive operation
|
|
|
|
* Overrun Err [0] : receive operation
|
|
|
|
*/
|
|
|
|
if (op)
|
|
|
|
mask = 0x8;
|
|
|
|
else
|
|
|
|
mask = 0xf;
|
2009-10-01 08:20:28 +00:00
|
|
|
|
2009-11-10 11:23:50 +00:00
|
|
|
return readl(&uart->uerstat) & mask;
|
2009-10-01 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 22:36:17 +00:00
|
|
|
static int s5p_serial_getc(struct udevice *dev)
|
2009-10-01 08:20:28 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct s5p_serial_plat *plat = dev_get_plat(dev);
|
2014-09-14 22:36:17 +00:00
|
|
|
struct s5p_uart *const uart = plat->reg;
|
2013-06-24 11:17:22 +00:00
|
|
|
|
2021-10-23 14:58:04 +00:00
|
|
|
if (!(readl(&uart->ufstat) & plat->rx_fifo_count_mask))
|
2014-09-14 22:36:17 +00:00
|
|
|
return -EAGAIN;
|
2009-10-01 08:20:28 +00:00
|
|
|
|
2014-09-14 22:36:17 +00:00
|
|
|
serial_err_check(uart, 0);
|
2021-10-23 14:58:04 +00:00
|
|
|
if (plat->reg_width == 4)
|
|
|
|
return (int)(readl(&uart->urxh) & 0xff);
|
|
|
|
else
|
|
|
|
return (int)(readb(&uart->urxh) & 0xff);
|
2009-10-01 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 22:36:17 +00:00
|
|
|
static int s5p_serial_putc(struct udevice *dev, const char ch)
|
2009-10-01 08:20:28 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct s5p_serial_plat *plat = dev_get_plat(dev);
|
2014-09-14 22:36:17 +00:00
|
|
|
struct s5p_uart *const uart = plat->reg;
|
2009-10-01 08:20:28 +00:00
|
|
|
|
2021-10-23 14:58:04 +00:00
|
|
|
if (readl(&uart->ufstat) & plat->tx_fifo_full)
|
2014-09-14 22:36:17 +00:00
|
|
|
return -EAGAIN;
|
2009-10-01 08:20:28 +00:00
|
|
|
|
2021-10-23 14:58:04 +00:00
|
|
|
if (plat->reg_width == 4)
|
|
|
|
writel(ch, &uart->utxh);
|
|
|
|
else
|
|
|
|
writeb(ch, &uart->utxh);
|
2014-09-14 22:36:17 +00:00
|
|
|
serial_err_check(uart, 1);
|
2009-10-01 08:20:28 +00:00
|
|
|
|
2014-09-14 22:36:17 +00:00
|
|
|
return 0;
|
2009-10-01 08:20:28 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 22:36:17 +00:00
|
|
|
static int s5p_serial_pending(struct udevice *dev, bool input)
|
2009-10-01 08:20:28 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct s5p_serial_plat *plat = dev_get_plat(dev);
|
2014-09-14 22:36:17 +00:00
|
|
|
struct s5p_uart *const uart = plat->reg;
|
|
|
|
uint32_t ufstat = readl(&uart->ufstat);
|
2009-10-01 08:20:28 +00:00
|
|
|
|
2021-10-23 14:58:04 +00:00
|
|
|
if (input) {
|
|
|
|
return (ufstat & plat->rx_fifo_count_mask) >>
|
|
|
|
plat->rx_fifo_count_shift;
|
|
|
|
} else {
|
|
|
|
return (ufstat & plat->tx_fifo_count_mask) >>
|
|
|
|
plat->tx_fifo_count_shift;
|
|
|
|
}
|
2012-09-09 16:48:28 +00:00
|
|
|
}
|
2009-10-01 08:20:28 +00:00
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int s5p_serial_of_to_plat(struct udevice *dev)
|
2013-06-24 11:17:22 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct s5p_serial_plat *plat = dev_get_plat(dev);
|
2021-10-23 14:58:04 +00:00
|
|
|
const ulong port_type = dev_get_driver_data(dev);
|
2013-06-24 11:17:22 +00:00
|
|
|
|
2023-11-07 20:13:49 +00:00
|
|
|
plat->reg = dev_read_addr_ptr(dev);
|
|
|
|
if (!plat->reg)
|
2014-09-14 22:36:17 +00:00
|
|
|
return -EINVAL;
|
2013-06-24 11:17:22 +00:00
|
|
|
|
2021-10-23 14:58:04 +00:00
|
|
|
plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
|
2023-11-07 19:05:59 +00:00
|
|
|
plat->port_id = dev_read_u8_default(dev, "id", dev_seq(dev));
|
2021-10-23 14:58:04 +00:00
|
|
|
|
|
|
|
if (port_type == PORT_S5L) {
|
|
|
|
plat->rx_fifo_count_shift = S5L_RX_FIFO_COUNT_SHIFT;
|
|
|
|
plat->rx_fifo_count_mask = S5L_RX_FIFO_COUNT_MASK;
|
|
|
|
plat->rx_fifo_full = S5L_RX_FIFO_FULL;
|
|
|
|
plat->tx_fifo_count_shift = S5L_TX_FIFO_COUNT_SHIFT;
|
|
|
|
plat->tx_fifo_count_mask = S5L_TX_FIFO_COUNT_MASK;
|
|
|
|
plat->tx_fifo_full = S5L_TX_FIFO_FULL;
|
|
|
|
} else {
|
|
|
|
plat->rx_fifo_count_shift = S5P_RX_FIFO_COUNT_SHIFT;
|
|
|
|
plat->rx_fifo_count_mask = S5P_RX_FIFO_COUNT_MASK;
|
|
|
|
plat->rx_fifo_full = S5P_RX_FIFO_FULL;
|
|
|
|
plat->tx_fifo_count_shift = S5P_TX_FIFO_COUNT_SHIFT;
|
|
|
|
plat->tx_fifo_count_mask = S5P_TX_FIFO_COUNT_MASK;
|
|
|
|
plat->tx_fifo_full = S5P_TX_FIFO_FULL;
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:17:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-14 22:36:17 +00:00
|
|
|
static const struct dm_serial_ops s5p_serial_ops = {
|
2023-11-07 19:06:01 +00:00
|
|
|
.putc = s5p_serial_putc,
|
|
|
|
.pending = s5p_serial_pending,
|
|
|
|
.getc = s5p_serial_getc,
|
|
|
|
.setbrg = s5p_serial_setbrg,
|
2014-09-14 22:36:17 +00:00
|
|
|
};
|
2012-09-12 17:39:57 +00:00
|
|
|
|
2014-09-14 22:36:17 +00:00
|
|
|
static const struct udevice_id s5p_serial_ids[] = {
|
2021-10-23 14:58:04 +00:00
|
|
|
{ .compatible = "samsung,exynos4210-uart", .data = PORT_S5P },
|
2024-01-11 03:09:06 +00:00
|
|
|
{ .compatible = "samsung,exynos850-uart", .data = PORT_S5P },
|
2021-10-23 14:58:04 +00:00
|
|
|
{ .compatible = "apple,s5l-uart", .data = PORT_S5L },
|
2014-09-14 22:36:17 +00:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(serial_s5p) = {
|
2023-11-07 19:06:01 +00:00
|
|
|
.name = "serial_s5p",
|
|
|
|
.id = UCLASS_SERIAL,
|
|
|
|
.of_match = s5p_serial_ids,
|
|
|
|
.of_to_plat = s5p_serial_of_to_plat,
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct s5p_serial_plat),
|
2023-11-07 19:06:01 +00:00
|
|
|
.probe = s5p_serial_probe,
|
|
|
|
.ops = &s5p_serial_ops,
|
2014-09-14 22:36:17 +00:00
|
|
|
};
|
2015-07-03 00:15:55 +00:00
|
|
|
#endif
|
2015-07-03 00:15:54 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_UART_S5P
|
|
|
|
|
|
|
|
#include <debug_uart.h>
|
|
|
|
|
2015-10-19 01:51:23 +00:00
|
|
|
static inline void _debug_uart_init(void)
|
2015-07-03 00:15:54 +00:00
|
|
|
{
|
2021-10-17 10:45:39 +00:00
|
|
|
if (IS_ENABLED(CONFIG_DEBUG_UART_SKIP_INIT))
|
|
|
|
return;
|
|
|
|
|
2022-05-27 20:15:24 +00:00
|
|
|
struct s5p_uart *uart = (struct s5p_uart *)CONFIG_VAL(DEBUG_UART_BASE);
|
2015-07-03 00:15:54 +00:00
|
|
|
|
|
|
|
s5p_serial_init(uart);
|
2023-01-26 13:44:09 +00:00
|
|
|
#if IS_ENABLED(CONFIG_ARCH_APPLE)
|
2021-10-23 14:58:04 +00:00
|
|
|
s5p_serial_baud(uart, 4, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
|
|
|
|
#else
|
|
|
|
s5p_serial_baud(uart, 1, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
|
|
|
|
#endif
|
2015-07-03 00:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void _debug_uart_putc(int ch)
|
|
|
|
{
|
2022-05-27 20:15:24 +00:00
|
|
|
struct s5p_uart *uart = (struct s5p_uart *)CONFIG_VAL(DEBUG_UART_BASE);
|
2015-07-03 00:15:54 +00:00
|
|
|
|
2023-01-26 13:44:09 +00:00
|
|
|
#if IS_ENABLED(CONFIG_ARCH_APPLE)
|
2023-11-07 19:06:01 +00:00
|
|
|
while (readl(&uart->ufstat) & S5L_TX_FIFO_FULL)
|
|
|
|
;
|
2021-10-23 14:58:04 +00:00
|
|
|
writel(ch, &uart->utxh);
|
|
|
|
#else
|
2023-11-07 19:06:01 +00:00
|
|
|
while (readl(&uart->ufstat) & S5P_TX_FIFO_FULL)
|
|
|
|
;
|
2015-07-03 00:15:54 +00:00
|
|
|
writeb(ch, &uart->utxh);
|
2021-10-23 14:58:04 +00:00
|
|
|
#endif
|
2015-07-03 00:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_UART_FUNCS
|
|
|
|
|
|
|
|
#endif
|