blob: 436cf2331df952cc92e0ca0e9486ab96f0401f42 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Scott McNutt4a889822010-03-19 19:03:28 -04002/*
3 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
4 * Scott McNutt <smcnutt@psyent.com>
Scott McNutt4a889822010-03-19 19:03:28 -04005 */
6
Scott McNutt4a889822010-03-19 19:03:28 -04007#include <common.h>
Thomas Chou6917a5d2015-10-21 21:26:54 +08008#include <dm.h>
9#include <errno.h>
Marek Vasute9d0e3c2012-09-13 16:49:51 +020010#include <serial.h>
Thomas Chou65a50c92015-10-31 20:53:23 +080011#include <asm/io.h>
12
Thomas Chou65a50c92015-10-31 20:53:23 +080013/* status register */
14#define ALTERA_UART_TMT BIT(5) /* tx empty */
15#define ALTERA_UART_TRDY BIT(6) /* tx ready */
16#define ALTERA_UART_RRDY BIT(7) /* rx ready */
Scott McNutt4a889822010-03-19 19:03:28 -040017
Thomas Chou6917a5d2015-10-21 21:26:54 +080018struct altera_uart_regs {
19 u32 rxdata; /* Rx data reg */
20 u32 txdata; /* Tx data reg */
21 u32 status; /* Status reg */
22 u32 control; /* Control reg */
23 u32 divisor; /* Baud rate divisor reg */
24 u32 endofpacket; /* End-of-packet reg */
25};
Thomas Chou769cb5c2014-08-25 16:50:14 +080026
Thomas Chou6917a5d2015-10-21 21:26:54 +080027struct altera_uart_platdata {
28 struct altera_uart_regs *regs;
29 unsigned int uartclk;
30};
Thomas Chou769cb5c2014-08-25 16:50:14 +080031
Thomas Chou6917a5d2015-10-21 21:26:54 +080032static int altera_uart_setbrg(struct udevice *dev, int baudrate)
33{
34 struct altera_uart_platdata *plat = dev->platdata;
35 struct altera_uart_regs *const regs = plat->regs;
36 u32 div;
Scott McNutt4a889822010-03-19 19:03:28 -040037
Thomas Chou6917a5d2015-10-21 21:26:54 +080038 div = (plat->uartclk / baudrate) - 1;
39 writel(div, &regs->divisor);
Scott McNutt4a889822010-03-19 19:03:28 -040040
Thomas Chou6917a5d2015-10-21 21:26:54 +080041 return 0;
Marek Vasute9d0e3c2012-09-13 16:49:51 +020042}
43
Thomas Chou6917a5d2015-10-21 21:26:54 +080044static int altera_uart_putc(struct udevice *dev, const char ch)
Marek Vasute9d0e3c2012-09-13 16:49:51 +020045{
Thomas Chou6917a5d2015-10-21 21:26:54 +080046 struct altera_uart_platdata *plat = dev->platdata;
47 struct altera_uart_regs *const regs = plat->regs;
48
49 if (!(readl(&regs->status) & ALTERA_UART_TRDY))
50 return -EAGAIN;
51
52 writel(ch, &regs->txdata);
53
Marek Vasute9d0e3c2012-09-13 16:49:51 +020054 return 0;
55}
Scott McNutt4a889822010-03-19 19:03:28 -040056
Thomas Chou6917a5d2015-10-21 21:26:54 +080057static int altera_uart_pending(struct udevice *dev, bool input)
Scott McNutt4a889822010-03-19 19:03:28 -040058{
Thomas Chou6917a5d2015-10-21 21:26:54 +080059 struct altera_uart_platdata *plat = dev->platdata;
60 struct altera_uart_regs *const regs = plat->regs;
61 u32 st = readl(&regs->status);
Scott McNutt4a889822010-03-19 19:03:28 -040062
Thomas Chou6917a5d2015-10-21 21:26:54 +080063 if (input)
64 return st & ALTERA_UART_RRDY ? 1 : 0;
65 else
66 return !(st & ALTERA_UART_TMT);
Scott McNutt4a889822010-03-19 19:03:28 -040067}
68
Thomas Chou6917a5d2015-10-21 21:26:54 +080069static int altera_uart_getc(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040070{
Thomas Chou6917a5d2015-10-21 21:26:54 +080071 struct altera_uart_platdata *plat = dev->platdata;
72 struct altera_uart_regs *const regs = plat->regs;
Scott McNutt4a889822010-03-19 19:03:28 -040073
Thomas Chou6917a5d2015-10-21 21:26:54 +080074 if (!(readl(&regs->status) & ALTERA_UART_RRDY))
75 return -EAGAIN;
Scott McNutt4a889822010-03-19 19:03:28 -040076
Thomas Chou6917a5d2015-10-21 21:26:54 +080077 return readl(&regs->rxdata) & 0xff;
Scott McNutt4a889822010-03-19 19:03:28 -040078}
79
Thomas Chou6917a5d2015-10-21 21:26:54 +080080static int altera_uart_probe(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040081{
Thomas Chou6917a5d2015-10-21 21:26:54 +080082 return 0;
Scott McNutt4a889822010-03-19 19:03:28 -040083}
84
Thomas Chou6917a5d2015-10-21 21:26:54 +080085static int altera_uart_ofdata_to_platdata(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040086{
Thomas Chou6917a5d2015-10-21 21:26:54 +080087 struct altera_uart_platdata *plat = dev_get_platdata(dev);
88
Simon Glassba1dea42017-05-17 17:18:05 -060089 plat->regs = map_physmem(devfdt_get_addr(dev),
Thomas Chou15890ca2015-11-14 10:38:09 +080090 sizeof(struct altera_uart_regs),
91 MAP_NOCACHE);
Simon Goldschmidt36dc0cc2019-05-09 22:11:58 +020092 plat->uartclk = dev_read_u32_default(dev, "clock-frequency", 0);
Thomas Chou6917a5d2015-10-21 21:26:54 +080093
94 return 0;
Scott McNutt4a889822010-03-19 19:03:28 -040095}
Marek Vasute9d0e3c2012-09-13 16:49:51 +020096
Thomas Chou6917a5d2015-10-21 21:26:54 +080097static const struct dm_serial_ops altera_uart_ops = {
98 .putc = altera_uart_putc,
99 .pending = altera_uart_pending,
100 .getc = altera_uart_getc,
101 .setbrg = altera_uart_setbrg,
102};
103
104static const struct udevice_id altera_uart_ids[] = {
Thomas Chou65a50c92015-10-31 20:53:23 +0800105 { .compatible = "altr,uart-1.0" },
106 {}
Thomas Chou6917a5d2015-10-21 21:26:54 +0800107};
108
109U_BOOT_DRIVER(altera_uart) = {
110 .name = "altera_uart",
111 .id = UCLASS_SERIAL,
112 .of_match = altera_uart_ids,
113 .ofdata_to_platdata = altera_uart_ofdata_to_platdata,
114 .platdata_auto_alloc_size = sizeof(struct altera_uart_platdata),
115 .probe = altera_uart_probe,
116 .ops = &altera_uart_ops,
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200117};
118
Thomas Chou6917a5d2015-10-21 21:26:54 +0800119#ifdef CONFIG_DEBUG_UART_ALTERA_UART
120
121#include <debug_uart.h>
122
Thomas Chou083cbdd2015-11-03 14:19:02 +0800123static inline void _debug_uart_init(void)
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200124{
Thomas Chou6917a5d2015-10-21 21:26:54 +0800125 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
126 u32 div;
127
128 div = (CONFIG_DEBUG_UART_CLOCK / CONFIG_BAUDRATE) - 1;
129 writel(div, &regs->divisor);
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200130}
131
Thomas Chou6917a5d2015-10-21 21:26:54 +0800132static inline void _debug_uart_putc(int ch)
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200133{
Thomas Chou6917a5d2015-10-21 21:26:54 +0800134 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
135
136 while (1) {
137 u32 st = readl(&regs->status);
138
139 if (st & ALTERA_UART_TRDY)
140 break;
141 }
142
143 writel(ch, &regs->txdata);
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200144}
Thomas Chou6917a5d2015-10-21 21:26:54 +0800145
146DEBUG_UART_FUNCS
147
148#endif