xref: /openbmc/u-boot/drivers/serial/serial_arc.c (revision 87f78478a4a1bf574db0b0e575ca37cf91fb187c)
122a240c3SAlexey Brodkin /*
222a240c3SAlexey Brodkin  * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
322a240c3SAlexey Brodkin  *
422a240c3SAlexey Brodkin  * This program is free software; you can redistribute it and/or modify
522a240c3SAlexey Brodkin  * it under the terms of the GNU General Public License version 2 as
622a240c3SAlexey Brodkin  * published by the Free Software Foundation.
722a240c3SAlexey Brodkin  *
822a240c3SAlexey Brodkin  */
922a240c3SAlexey Brodkin 
1022a240c3SAlexey Brodkin #include <common.h>
1101496c4fSAlexey Brodkin #include <dm.h>
1222a240c3SAlexey Brodkin #include <serial.h>
1322a240c3SAlexey Brodkin 
1422a240c3SAlexey Brodkin DECLARE_GLOBAL_DATA_PTR;
1522a240c3SAlexey Brodkin 
1622a240c3SAlexey Brodkin struct arc_serial_regs {
1722a240c3SAlexey Brodkin 	unsigned int id0;
1822a240c3SAlexey Brodkin 	unsigned int id1;
1922a240c3SAlexey Brodkin 	unsigned int id2;
2022a240c3SAlexey Brodkin 	unsigned int id3;
2122a240c3SAlexey Brodkin 	unsigned int data;
2222a240c3SAlexey Brodkin 	unsigned int status;
2322a240c3SAlexey Brodkin 	unsigned int baudl;
2422a240c3SAlexey Brodkin 	unsigned int baudh;
2522a240c3SAlexey Brodkin };
2622a240c3SAlexey Brodkin 
2701496c4fSAlexey Brodkin 
2801496c4fSAlexey Brodkin struct arc_serial_platdata {
2901496c4fSAlexey Brodkin 	struct arc_serial_regs *reg;
3001496c4fSAlexey Brodkin 	unsigned int uartclk;
3101496c4fSAlexey Brodkin };
3201496c4fSAlexey Brodkin 
3322a240c3SAlexey Brodkin /* Bit definitions of STATUS register */
3422a240c3SAlexey Brodkin #define UART_RXEMPTY		(1 << 5)
3522a240c3SAlexey Brodkin #define UART_OVERFLOW_ERR	(1 << 1)
3622a240c3SAlexey Brodkin #define UART_TXEMPTY		(1 << 7)
3722a240c3SAlexey Brodkin 
arc_serial_setbrg(struct udevice * dev,int baudrate)3801496c4fSAlexey Brodkin static int arc_serial_setbrg(struct udevice *dev, int baudrate)
3922a240c3SAlexey Brodkin {
4001496c4fSAlexey Brodkin 	struct arc_serial_platdata *plat = dev->platdata;
4101496c4fSAlexey Brodkin 	struct arc_serial_regs *const regs = plat->reg;
4201496c4fSAlexey Brodkin 	int arc_console_baud = gd->cpu_clk / (baudrate * 4) - 1;
4322a240c3SAlexey Brodkin 
4494b5400eSAlexey Brodkin 	writeb(arc_console_baud & 0xff, &regs->baudl);
4594b5400eSAlexey Brodkin 	writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
4622a240c3SAlexey Brodkin 
4722a240c3SAlexey Brodkin 	return 0;
4822a240c3SAlexey Brodkin }
4922a240c3SAlexey Brodkin 
arc_serial_putc(struct udevice * dev,const char c)5001496c4fSAlexey Brodkin static int arc_serial_putc(struct udevice *dev, const char c)
5122a240c3SAlexey Brodkin {
5201496c4fSAlexey Brodkin 	struct arc_serial_platdata *plat = dev->platdata;
5301496c4fSAlexey Brodkin 	struct arc_serial_regs *const regs = plat->reg;
5401496c4fSAlexey Brodkin 
5594b5400eSAlexey Brodkin 	while (!(readb(&regs->status) & UART_TXEMPTY))
5622a240c3SAlexey Brodkin 		;
5722a240c3SAlexey Brodkin 
5894b5400eSAlexey Brodkin 	writeb(c, &regs->data);
5901496c4fSAlexey Brodkin 
6001496c4fSAlexey Brodkin 	return 0;
6122a240c3SAlexey Brodkin }
6222a240c3SAlexey Brodkin 
arc_serial_tstc(struct arc_serial_regs * const regs)6301496c4fSAlexey Brodkin static int arc_serial_tstc(struct arc_serial_regs *const regs)
6422a240c3SAlexey Brodkin {
6594b5400eSAlexey Brodkin 	return !(readb(&regs->status) & UART_RXEMPTY);
6622a240c3SAlexey Brodkin }
6722a240c3SAlexey Brodkin 
arc_serial_pending(struct udevice * dev,bool input)6801496c4fSAlexey Brodkin static int arc_serial_pending(struct udevice *dev, bool input)
6922a240c3SAlexey Brodkin {
7001496c4fSAlexey Brodkin 	struct arc_serial_platdata *plat = dev->platdata;
7101496c4fSAlexey Brodkin 	struct arc_serial_regs *const regs = plat->reg;
7201496c4fSAlexey Brodkin 	uint32_t status = readb(&regs->status);
7301496c4fSAlexey Brodkin 
7401496c4fSAlexey Brodkin 	if (input)
7501496c4fSAlexey Brodkin 		return status & UART_RXEMPTY ? 0 : 1;
7601496c4fSAlexey Brodkin 	else
7701496c4fSAlexey Brodkin 		return status & UART_TXEMPTY ? 0 : 1;
7801496c4fSAlexey Brodkin }
7901496c4fSAlexey Brodkin 
arc_serial_getc(struct udevice * dev)8001496c4fSAlexey Brodkin static int arc_serial_getc(struct udevice *dev)
8101496c4fSAlexey Brodkin {
8201496c4fSAlexey Brodkin 	struct arc_serial_platdata *plat = dev->platdata;
8301496c4fSAlexey Brodkin 	struct arc_serial_regs *const regs = plat->reg;
8401496c4fSAlexey Brodkin 
8501496c4fSAlexey Brodkin 	while (!arc_serial_tstc(regs))
8622a240c3SAlexey Brodkin 		;
8722a240c3SAlexey Brodkin 
8822a240c3SAlexey Brodkin 	/* Check for overflow errors */
8994b5400eSAlexey Brodkin 	if (readb(&regs->status) & UART_OVERFLOW_ERR)
9022a240c3SAlexey Brodkin 		return 0;
9122a240c3SAlexey Brodkin 
9294b5400eSAlexey Brodkin 	return readb(&regs->data) & 0xFF;
9322a240c3SAlexey Brodkin }
9422a240c3SAlexey Brodkin 
arc_serial_probe(struct udevice * dev)9501496c4fSAlexey Brodkin static int arc_serial_probe(struct udevice *dev)
9601496c4fSAlexey Brodkin {
9701496c4fSAlexey Brodkin 	return 0;
9801496c4fSAlexey Brodkin }
9901496c4fSAlexey Brodkin 
10001496c4fSAlexey Brodkin static const struct dm_serial_ops arc_serial_ops = {
10122a240c3SAlexey Brodkin 	.putc = arc_serial_putc,
10201496c4fSAlexey Brodkin 	.pending = arc_serial_pending,
10322a240c3SAlexey Brodkin 	.getc = arc_serial_getc,
10401496c4fSAlexey Brodkin 	.setbrg = arc_serial_setbrg,
10522a240c3SAlexey Brodkin };
10622a240c3SAlexey Brodkin 
10701496c4fSAlexey Brodkin static const struct udevice_id arc_serial_ids[] = {
10801496c4fSAlexey Brodkin 	{ .compatible = "snps,arc-uart" },
10901496c4fSAlexey Brodkin 	{ }
11001496c4fSAlexey Brodkin };
11101496c4fSAlexey Brodkin 
arc_serial_ofdata_to_platdata(struct udevice * dev)11201496c4fSAlexey Brodkin static int arc_serial_ofdata_to_platdata(struct udevice *dev)
11322a240c3SAlexey Brodkin {
11401496c4fSAlexey Brodkin 	struct arc_serial_platdata *plat = dev_get_platdata(dev);
11501496c4fSAlexey Brodkin 	DECLARE_GLOBAL_DATA_PTR;
11601496c4fSAlexey Brodkin 
117a821c4afSSimon Glass 	plat->reg = (struct arc_serial_regs *)devfdt_get_addr(dev);
118e160f7d4SSimon Glass 	plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
11901496c4fSAlexey Brodkin 				       "clock-frequency", 0);
12001496c4fSAlexey Brodkin 
12101496c4fSAlexey Brodkin 	return 0;
12222a240c3SAlexey Brodkin }
12322a240c3SAlexey Brodkin 
12401496c4fSAlexey Brodkin U_BOOT_DRIVER(serial_arc) = {
12501496c4fSAlexey Brodkin 	.name	= "serial_arc",
12601496c4fSAlexey Brodkin 	.id	= UCLASS_SERIAL,
12701496c4fSAlexey Brodkin 	.of_match = arc_serial_ids,
12801496c4fSAlexey Brodkin 	.ofdata_to_platdata = arc_serial_ofdata_to_platdata,
129*d771dd53SAlexey Brodkin 	.platdata_auto_alloc_size = sizeof(struct arc_serial_platdata),
13001496c4fSAlexey Brodkin 	.probe = arc_serial_probe,
13101496c4fSAlexey Brodkin 	.ops	= &arc_serial_ops,
13201496c4fSAlexey Brodkin };
13354705016SAlexey Brodkin 
13454705016SAlexey Brodkin #ifdef CONFIG_DEBUG_ARC_SERIAL
13554705016SAlexey Brodkin #include <debug_uart.h>
13654705016SAlexey Brodkin 
_debug_uart_init(void)13754705016SAlexey Brodkin static inline void _debug_uart_init(void)
13854705016SAlexey Brodkin {
13954705016SAlexey Brodkin 	struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_DEBUG_UART_BASE;
14054705016SAlexey Brodkin 	int arc_console_baud = CONFIG_DEBUG_UART_CLOCK / (CONFIG_BAUDRATE * 4) - 1;
14154705016SAlexey Brodkin 
14254705016SAlexey Brodkin 	writeb(arc_console_baud & 0xff, &regs->baudl);
14354705016SAlexey Brodkin 	writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
14454705016SAlexey Brodkin }
14554705016SAlexey Brodkin 
_debug_uart_putc(int c)14654705016SAlexey Brodkin static inline void _debug_uart_putc(int c)
14754705016SAlexey Brodkin {
14854705016SAlexey Brodkin 	struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_DEBUG_UART_BASE;
14954705016SAlexey Brodkin 
15054705016SAlexey Brodkin 	while (!(readb(&regs->status) & UART_TXEMPTY))
15154705016SAlexey Brodkin 		;
15254705016SAlexey Brodkin 
15354705016SAlexey Brodkin 	writeb(c, &regs->data);
15454705016SAlexey Brodkin }
15554705016SAlexey Brodkin 
15654705016SAlexey Brodkin DEBUG_UART_FUNCS
15754705016SAlexey Brodkin 
15854705016SAlexey Brodkin #endif
159