blob: 3c13ef25bb492a0a342c572b9fb3e11a6535ca54 [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
Thomas Chou6917a5d2015-10-21 21:26:54 +08007#include <dm.h>
8#include <errno.h>
Marek Vasute9d0e3c2012-09-13 16:49:51 +02009#include <serial.h>
Thomas Chou65a50c92015-10-31 20:53:23 +080010#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <linux/bitops.h>
Thomas Chou65a50c92015-10-31 20:53:23 +080012
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
Simon Glassb75b15b2020-12-03 16:55:23 -070027struct altera_uart_plat {
Thomas Chou6917a5d2015-10-21 21:26:54 +080028 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{
Simon Glass95588622020-12-22 19:30:28 -070034 struct altera_uart_plat *plat = dev_get_plat(dev);
Thomas Chou6917a5d2015-10-21 21:26:54 +080035 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{
Simon Glass95588622020-12-22 19:30:28 -070046 struct altera_uart_plat *plat = dev_get_plat(dev);
Thomas Chou6917a5d2015-10-21 21:26:54 +080047 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{
Simon Glass95588622020-12-22 19:30:28 -070059 struct altera_uart_plat *plat = dev_get_plat(dev);
Thomas Chou6917a5d2015-10-21 21:26:54 +080060 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{
Simon Glass95588622020-12-22 19:30:28 -070071 struct altera_uart_plat *plat = dev_get_plat(dev);
Thomas Chou6917a5d2015-10-21 21:26:54 +080072 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
Simon Glassaad29ae2020-12-03 16:55:21 -070085static int altera_uart_of_to_plat(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040086{
Simon Glassb75b15b2020-12-03 16:55:23 -070087 struct altera_uart_plat *plat = dev_get_plat(dev);
Thomas Chou6917a5d2015-10-21 21:26:54 +080088
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +090089 plat->regs = map_physmem(dev_read_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,
Simon Glassaad29ae2020-12-03 16:55:21 -0700113 .of_to_plat = altera_uart_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700114 .plat_auto = sizeof(struct altera_uart_plat),
Thomas Chou6917a5d2015-10-21 21:26:54 +0800115 .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{
Pali Rohár8864b352022-05-27 22:15:24 +0200125 struct altera_uart_regs *regs = (void *)CONFIG_VAL(DEBUG_UART_BASE);
Thomas Chou6917a5d2015-10-21 21:26:54 +0800126 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{
Pali Rohár8864b352022-05-27 22:15:24 +0200134 struct altera_uart_regs *regs = (void *)CONFIG_VAL(DEBUG_UART_BASE);
Thomas Chou6917a5d2015-10-21 21:26:54 +0800135
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