2013-12-13 06:35:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2015-03-17 11:55:14 +00:00
|
|
|
#include <dm.h>
|
2013-12-13 06:35:11 +00:00
|
|
|
#include <serial.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2013-12-13 06:35:11 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
struct arc_serial_regs {
|
|
|
|
unsigned int id0;
|
|
|
|
unsigned int id1;
|
|
|
|
unsigned int id2;
|
|
|
|
unsigned int id3;
|
|
|
|
unsigned int data;
|
|
|
|
unsigned int status;
|
|
|
|
unsigned int baudl;
|
|
|
|
unsigned int baudh;
|
|
|
|
};
|
|
|
|
|
2015-03-17 11:55:14 +00:00
|
|
|
|
2020-12-03 23:55:23 +00:00
|
|
|
struct arc_serial_plat {
|
2015-03-17 11:55:14 +00:00
|
|
|
struct arc_serial_regs *reg;
|
|
|
|
unsigned int uartclk;
|
|
|
|
};
|
|
|
|
|
2013-12-13 06:35:11 +00:00
|
|
|
/* Bit definitions of STATUS register */
|
|
|
|
#define UART_RXEMPTY (1 << 5)
|
|
|
|
#define UART_OVERFLOW_ERR (1 << 1)
|
|
|
|
#define UART_TXEMPTY (1 << 7)
|
|
|
|
|
2015-03-17 11:55:14 +00:00
|
|
|
static int arc_serial_setbrg(struct udevice *dev, int baudrate)
|
2013-12-13 06:35:11 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct arc_serial_plat *plat = dev_get_plat(dev);
|
2015-03-17 11:55:14 +00:00
|
|
|
struct arc_serial_regs *const regs = plat->reg;
|
|
|
|
int arc_console_baud = gd->cpu_clk / (baudrate * 4) - 1;
|
2013-12-13 06:35:11 +00:00
|
|
|
|
2014-02-08 06:10:02 +00:00
|
|
|
writeb(arc_console_baud & 0xff, ®s->baudl);
|
|
|
|
writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh);
|
2013-12-13 06:35:11 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-17 11:55:14 +00:00
|
|
|
static int arc_serial_putc(struct udevice *dev, const char c)
|
2013-12-13 06:35:11 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct arc_serial_plat *plat = dev_get_plat(dev);
|
2015-03-17 11:55:14 +00:00
|
|
|
struct arc_serial_regs *const regs = plat->reg;
|
|
|
|
|
2022-12-10 23:31:21 +00:00
|
|
|
if (!(readb(®s->status) & UART_TXEMPTY))
|
|
|
|
return -EAGAIN;
|
2013-12-13 06:35:11 +00:00
|
|
|
|
2014-02-08 06:10:02 +00:00
|
|
|
writeb(c, ®s->data);
|
2015-03-17 11:55:14 +00:00
|
|
|
|
|
|
|
return 0;
|
2013-12-13 06:35:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:55:14 +00:00
|
|
|
static int arc_serial_tstc(struct arc_serial_regs *const regs)
|
2013-12-13 06:35:11 +00:00
|
|
|
{
|
2014-02-08 06:10:02 +00:00
|
|
|
return !(readb(®s->status) & UART_RXEMPTY);
|
2013-12-13 06:35:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:55:14 +00:00
|
|
|
static int arc_serial_pending(struct udevice *dev, bool input)
|
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct arc_serial_plat *plat = dev_get_plat(dev);
|
2015-03-17 11:55:14 +00:00
|
|
|
struct arc_serial_regs *const regs = plat->reg;
|
|
|
|
uint32_t status = readb(®s->status);
|
|
|
|
|
|
|
|
if (input)
|
|
|
|
return status & UART_RXEMPTY ? 0 : 1;
|
|
|
|
else
|
|
|
|
return status & UART_TXEMPTY ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int arc_serial_getc(struct udevice *dev)
|
2013-12-13 06:35:11 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct arc_serial_plat *plat = dev_get_plat(dev);
|
2015-03-17 11:55:14 +00:00
|
|
|
struct arc_serial_regs *const regs = plat->reg;
|
|
|
|
|
2022-12-10 23:31:21 +00:00
|
|
|
if (!arc_serial_tstc(regs))
|
|
|
|
return -EAGAIN;
|
2013-12-13 06:35:11 +00:00
|
|
|
|
|
|
|
/* Check for overflow errors */
|
2014-02-08 06:10:02 +00:00
|
|
|
if (readb(®s->status) & UART_OVERFLOW_ERR)
|
2013-12-13 06:35:11 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-02-08 06:10:02 +00:00
|
|
|
return readb(®s->data) & 0xFF;
|
2013-12-13 06:35:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:55:14 +00:00
|
|
|
static int arc_serial_probe(struct udevice *dev)
|
2013-12-13 06:35:11 +00:00
|
|
|
{
|
2015-03-17 11:55:14 +00:00
|
|
|
return 0;
|
2013-12-13 06:35:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:55:14 +00:00
|
|
|
static const struct dm_serial_ops arc_serial_ops = {
|
|
|
|
.putc = arc_serial_putc,
|
|
|
|
.pending = arc_serial_pending,
|
|
|
|
.getc = arc_serial_getc,
|
|
|
|
.setbrg = arc_serial_setbrg,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id arc_serial_ids[] = {
|
|
|
|
{ .compatible = "snps,arc-uart" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int arc_serial_of_to_plat(struct udevice *dev)
|
2013-12-13 06:35:11 +00:00
|
|
|
{
|
2020-12-03 23:55:23 +00:00
|
|
|
struct arc_serial_plat *plat = dev_get_plat(dev);
|
2015-03-17 11:55:14 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2020-07-17 05:36:46 +00:00
|
|
|
plat->reg = dev_read_addr_ptr(dev);
|
2017-01-17 23:52:55 +00:00
|
|
|
plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
|
2015-03-17 11:55:14 +00:00
|
|
|
"clock-frequency", 0);
|
|
|
|
|
|
|
|
return 0;
|
2013-12-13 06:35:11 +00:00
|
|
|
}
|
2015-03-17 11:55:14 +00:00
|
|
|
|
|
|
|
U_BOOT_DRIVER(serial_arc) = {
|
|
|
|
.name = "serial_arc",
|
|
|
|
.id = UCLASS_SERIAL,
|
|
|
|
.of_match = arc_serial_ids,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = arc_serial_of_to_plat,
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct arc_serial_plat),
|
2015-03-17 11:55:14 +00:00
|
|
|
.probe = arc_serial_probe,
|
|
|
|
.ops = &arc_serial_ops,
|
|
|
|
};
|
2018-05-21 13:42:07 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_ARC_SERIAL
|
|
|
|
#include <debug_uart.h>
|
|
|
|
|
|
|
|
static inline void _debug_uart_init(void)
|
|
|
|
{
|
2022-05-27 20:15:24 +00:00
|
|
|
struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_VAL(DEBUG_UART_BASE);
|
2018-05-21 13:42:07 +00:00
|
|
|
int arc_console_baud = CONFIG_DEBUG_UART_CLOCK / (CONFIG_BAUDRATE * 4) - 1;
|
|
|
|
|
|
|
|
writeb(arc_console_baud & 0xff, ®s->baudl);
|
|
|
|
writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void _debug_uart_putc(int c)
|
|
|
|
{
|
2022-05-27 20:15:24 +00:00
|
|
|
struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_VAL(DEBUG_UART_BASE);
|
2018-05-21 13:42:07 +00:00
|
|
|
|
|
|
|
while (!(readb(®s->status) & UART_TXEMPTY))
|
|
|
|
;
|
|
|
|
|
|
|
|
writeb(c, ®s->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_UART_FUNCS
|
|
|
|
|
|
|
|
#endif
|