blob: 8344940282eb431455f12c042d497970a1dfd0e4 [file] [log] [blame]
Scott McNutt4a889822010-03-19 19:03:28 -04001/*
2 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3 * Scott McNutt <smcnutt@psyent.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Scott McNutt4a889822010-03-19 19:03:28 -04006 */
7
Scott McNutt4a889822010-03-19 19:03:28 -04008#include <common.h>
Thomas Chou6917a5d2015-10-21 21:26:54 +08009#include <dm.h>
10#include <errno.h>
Marek Vasute9d0e3c2012-09-13 16:49:51 +020011#include <serial.h>
Thomas Chou65a50c92015-10-31 20:53:23 +080012#include <asm/io.h>
13
14DECLARE_GLOBAL_DATA_PTR;
15
16/* status register */
17#define ALTERA_UART_TMT BIT(5) /* tx empty */
18#define ALTERA_UART_TRDY BIT(6) /* tx ready */
19#define ALTERA_UART_RRDY BIT(7) /* rx ready */
Scott McNutt4a889822010-03-19 19:03:28 -040020
Thomas Chou6917a5d2015-10-21 21:26:54 +080021struct altera_uart_regs {
22 u32 rxdata; /* Rx data reg */
23 u32 txdata; /* Tx data reg */
24 u32 status; /* Status reg */
25 u32 control; /* Control reg */
26 u32 divisor; /* Baud rate divisor reg */
27 u32 endofpacket; /* End-of-packet reg */
28};
Thomas Chou769cb5c2014-08-25 16:50:14 +080029
Thomas Chou6917a5d2015-10-21 21:26:54 +080030struct altera_uart_platdata {
31 struct altera_uart_regs *regs;
32 unsigned int uartclk;
33};
Thomas Chou769cb5c2014-08-25 16:50:14 +080034
Thomas Chou6917a5d2015-10-21 21:26:54 +080035static int altera_uart_setbrg(struct udevice *dev, int baudrate)
36{
37 struct altera_uart_platdata *plat = dev->platdata;
38 struct altera_uart_regs *const regs = plat->regs;
39 u32 div;
Scott McNutt4a889822010-03-19 19:03:28 -040040
Thomas Chou6917a5d2015-10-21 21:26:54 +080041 div = (plat->uartclk / baudrate) - 1;
42 writel(div, &regs->divisor);
Scott McNutt4a889822010-03-19 19:03:28 -040043
Thomas Chou6917a5d2015-10-21 21:26:54 +080044 return 0;
Marek Vasute9d0e3c2012-09-13 16:49:51 +020045}
46
Thomas Chou6917a5d2015-10-21 21:26:54 +080047static int altera_uart_putc(struct udevice *dev, const char ch)
Marek Vasute9d0e3c2012-09-13 16:49:51 +020048{
Thomas Chou6917a5d2015-10-21 21:26:54 +080049 struct altera_uart_platdata *plat = dev->platdata;
50 struct altera_uart_regs *const regs = plat->regs;
51
52 if (!(readl(&regs->status) & ALTERA_UART_TRDY))
53 return -EAGAIN;
54
55 writel(ch, &regs->txdata);
56
Marek Vasute9d0e3c2012-09-13 16:49:51 +020057 return 0;
58}
Scott McNutt4a889822010-03-19 19:03:28 -040059
Thomas Chou6917a5d2015-10-21 21:26:54 +080060static int altera_uart_pending(struct udevice *dev, bool input)
Scott McNutt4a889822010-03-19 19:03:28 -040061{
Thomas Chou6917a5d2015-10-21 21:26:54 +080062 struct altera_uart_platdata *plat = dev->platdata;
63 struct altera_uart_regs *const regs = plat->regs;
64 u32 st = readl(&regs->status);
Scott McNutt4a889822010-03-19 19:03:28 -040065
Thomas Chou6917a5d2015-10-21 21:26:54 +080066 if (input)
67 return st & ALTERA_UART_RRDY ? 1 : 0;
68 else
69 return !(st & ALTERA_UART_TMT);
Scott McNutt4a889822010-03-19 19:03:28 -040070}
71
Thomas Chou6917a5d2015-10-21 21:26:54 +080072static int altera_uart_getc(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040073{
Thomas Chou6917a5d2015-10-21 21:26:54 +080074 struct altera_uart_platdata *plat = dev->platdata;
75 struct altera_uart_regs *const regs = plat->regs;
Scott McNutt4a889822010-03-19 19:03:28 -040076
Thomas Chou6917a5d2015-10-21 21:26:54 +080077 if (!(readl(&regs->status) & ALTERA_UART_RRDY))
78 return -EAGAIN;
Scott McNutt4a889822010-03-19 19:03:28 -040079
Thomas Chou6917a5d2015-10-21 21:26:54 +080080 return readl(&regs->rxdata) & 0xff;
Scott McNutt4a889822010-03-19 19:03:28 -040081}
82
Thomas Chou6917a5d2015-10-21 21:26:54 +080083static int altera_uart_probe(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040084{
Thomas Chou6917a5d2015-10-21 21:26:54 +080085 return 0;
Scott McNutt4a889822010-03-19 19:03:28 -040086}
87
Thomas Chou6917a5d2015-10-21 21:26:54 +080088static int altera_uart_ofdata_to_platdata(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040089{
Thomas Chou6917a5d2015-10-21 21:26:54 +080090 struct altera_uart_platdata *plat = dev_get_platdata(dev);
91
Thomas Chou15890ca2015-11-14 10:38:09 +080092 plat->regs = map_physmem(dev_get_addr(dev),
93 sizeof(struct altera_uart_regs),
94 MAP_NOCACHE);
Simon Glassdd79d6e2017-01-17 16:52:55 -070095 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Thomas Chou6917a5d2015-10-21 21:26:54 +080096 "clock-frequency", 0);
97
98 return 0;
Scott McNutt4a889822010-03-19 19:03:28 -040099}
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200100
Thomas Chou6917a5d2015-10-21 21:26:54 +0800101static const struct dm_serial_ops altera_uart_ops = {
102 .putc = altera_uart_putc,
103 .pending = altera_uart_pending,
104 .getc = altera_uart_getc,
105 .setbrg = altera_uart_setbrg,
106};
107
108static const struct udevice_id altera_uart_ids[] = {
Thomas Chou65a50c92015-10-31 20:53:23 +0800109 { .compatible = "altr,uart-1.0" },
110 {}
Thomas Chou6917a5d2015-10-21 21:26:54 +0800111};
112
113U_BOOT_DRIVER(altera_uart) = {
114 .name = "altera_uart",
115 .id = UCLASS_SERIAL,
116 .of_match = altera_uart_ids,
117 .ofdata_to_platdata = altera_uart_ofdata_to_platdata,
118 .platdata_auto_alloc_size = sizeof(struct altera_uart_platdata),
119 .probe = altera_uart_probe,
120 .ops = &altera_uart_ops,
121 .flags = DM_FLAG_PRE_RELOC,
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200122};
123
Thomas Chou6917a5d2015-10-21 21:26:54 +0800124#ifdef CONFIG_DEBUG_UART_ALTERA_UART
125
126#include <debug_uart.h>
127
Thomas Chou083cbdd2015-11-03 14:19:02 +0800128static inline void _debug_uart_init(void)
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200129{
Thomas Chou6917a5d2015-10-21 21:26:54 +0800130 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
131 u32 div;
132
133 div = (CONFIG_DEBUG_UART_CLOCK / CONFIG_BAUDRATE) - 1;
134 writel(div, &regs->divisor);
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200135}
136
Thomas Chou6917a5d2015-10-21 21:26:54 +0800137static inline void _debug_uart_putc(int ch)
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200138{
Thomas Chou6917a5d2015-10-21 21:26:54 +0800139 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
140
141 while (1) {
142 u32 st = readl(&regs->status);
143
144 if (st & ALTERA_UART_TRDY)
145 break;
146 }
147
148 writel(ch, &regs->txdata);
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200149}
Thomas Chou6917a5d2015-10-21 21:26:54 +0800150
151DEBUG_UART_FUNCS
152
153#endif