blob: 67d47199aa4c8d1dd6060f7ff2a663aa0c038eba [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
13DECLARE_GLOBAL_DATA_PTR;
14
15/* status register */
16#define ALTERA_UART_TMT BIT(5) /* tx empty */
17#define ALTERA_UART_TRDY BIT(6) /* tx ready */
18#define ALTERA_UART_RRDY BIT(7) /* rx ready */
Scott McNutt4a889822010-03-19 19:03:28 -040019
Thomas Chou6917a5d2015-10-21 21:26:54 +080020struct altera_uart_regs {
21 u32 rxdata; /* Rx data reg */
22 u32 txdata; /* Tx data reg */
23 u32 status; /* Status reg */
24 u32 control; /* Control reg */
25 u32 divisor; /* Baud rate divisor reg */
26 u32 endofpacket; /* End-of-packet reg */
27};
Thomas Chou769cb5c2014-08-25 16:50:14 +080028
Thomas Chou6917a5d2015-10-21 21:26:54 +080029struct altera_uart_platdata {
30 struct altera_uart_regs *regs;
31 unsigned int uartclk;
32};
Thomas Chou769cb5c2014-08-25 16:50:14 +080033
Thomas Chou6917a5d2015-10-21 21:26:54 +080034static int altera_uart_setbrg(struct udevice *dev, int baudrate)
35{
36 struct altera_uart_platdata *plat = dev->platdata;
37 struct altera_uart_regs *const regs = plat->regs;
38 u32 div;
Scott McNutt4a889822010-03-19 19:03:28 -040039
Thomas Chou6917a5d2015-10-21 21:26:54 +080040 div = (plat->uartclk / baudrate) - 1;
41 writel(div, &regs->divisor);
Scott McNutt4a889822010-03-19 19:03:28 -040042
Thomas Chou6917a5d2015-10-21 21:26:54 +080043 return 0;
Marek Vasute9d0e3c2012-09-13 16:49:51 +020044}
45
Thomas Chou6917a5d2015-10-21 21:26:54 +080046static int altera_uart_putc(struct udevice *dev, const char ch)
Marek Vasute9d0e3c2012-09-13 16:49:51 +020047{
Thomas Chou6917a5d2015-10-21 21:26:54 +080048 struct altera_uart_platdata *plat = dev->platdata;
49 struct altera_uart_regs *const regs = plat->regs;
50
51 if (!(readl(&regs->status) & ALTERA_UART_TRDY))
52 return -EAGAIN;
53
54 writel(ch, &regs->txdata);
55
Marek Vasute9d0e3c2012-09-13 16:49:51 +020056 return 0;
57}
Scott McNutt4a889822010-03-19 19:03:28 -040058
Thomas Chou6917a5d2015-10-21 21:26:54 +080059static int altera_uart_pending(struct udevice *dev, bool input)
Scott McNutt4a889822010-03-19 19:03:28 -040060{
Thomas Chou6917a5d2015-10-21 21:26:54 +080061 struct altera_uart_platdata *plat = dev->platdata;
62 struct altera_uart_regs *const regs = plat->regs;
63 u32 st = readl(&regs->status);
Scott McNutt4a889822010-03-19 19:03:28 -040064
Thomas Chou6917a5d2015-10-21 21:26:54 +080065 if (input)
66 return st & ALTERA_UART_RRDY ? 1 : 0;
67 else
68 return !(st & ALTERA_UART_TMT);
Scott McNutt4a889822010-03-19 19:03:28 -040069}
70
Thomas Chou6917a5d2015-10-21 21:26:54 +080071static int altera_uart_getc(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040072{
Thomas Chou6917a5d2015-10-21 21:26:54 +080073 struct altera_uart_platdata *plat = dev->platdata;
74 struct altera_uart_regs *const regs = plat->regs;
Scott McNutt4a889822010-03-19 19:03:28 -040075
Thomas Chou6917a5d2015-10-21 21:26:54 +080076 if (!(readl(&regs->status) & ALTERA_UART_RRDY))
77 return -EAGAIN;
Scott McNutt4a889822010-03-19 19:03:28 -040078
Thomas Chou6917a5d2015-10-21 21:26:54 +080079 return readl(&regs->rxdata) & 0xff;
Scott McNutt4a889822010-03-19 19:03:28 -040080}
81
Thomas Chou6917a5d2015-10-21 21:26:54 +080082static int altera_uart_probe(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040083{
Thomas Chou6917a5d2015-10-21 21:26:54 +080084 return 0;
Scott McNutt4a889822010-03-19 19:03:28 -040085}
86
Thomas Chou6917a5d2015-10-21 21:26:54 +080087static int altera_uart_ofdata_to_platdata(struct udevice *dev)
Scott McNutt4a889822010-03-19 19:03:28 -040088{
Thomas Chou6917a5d2015-10-21 21:26:54 +080089 struct altera_uart_platdata *plat = dev_get_platdata(dev);
90
Simon Glassba1dea42017-05-17 17:18:05 -060091 plat->regs = map_physmem(devfdt_get_addr(dev),
Thomas Chou15890ca2015-11-14 10:38:09 +080092 sizeof(struct altera_uart_regs),
93 MAP_NOCACHE);
Simon Glassdd79d6e2017-01-17 16:52:55 -070094 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Thomas Chou6917a5d2015-10-21 21:26:54 +080095 "clock-frequency", 0);
96
97 return 0;
Scott McNutt4a889822010-03-19 19:03:28 -040098}
Marek Vasute9d0e3c2012-09-13 16:49:51 +020099
Thomas Chou6917a5d2015-10-21 21:26:54 +0800100static const struct dm_serial_ops altera_uart_ops = {
101 .putc = altera_uart_putc,
102 .pending = altera_uart_pending,
103 .getc = altera_uart_getc,
104 .setbrg = altera_uart_setbrg,
105};
106
107static const struct udevice_id altera_uart_ids[] = {
Thomas Chou65a50c92015-10-31 20:53:23 +0800108 { .compatible = "altr,uart-1.0" },
109 {}
Thomas Chou6917a5d2015-10-21 21:26:54 +0800110};
111
112U_BOOT_DRIVER(altera_uart) = {
113 .name = "altera_uart",
114 .id = UCLASS_SERIAL,
115 .of_match = altera_uart_ids,
116 .ofdata_to_platdata = altera_uart_ofdata_to_platdata,
117 .platdata_auto_alloc_size = sizeof(struct altera_uart_platdata),
118 .probe = altera_uart_probe,
119 .ops = &altera_uart_ops,
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200120};
121
Thomas Chou6917a5d2015-10-21 21:26:54 +0800122#ifdef CONFIG_DEBUG_UART_ALTERA_UART
123
124#include <debug_uart.h>
125
Thomas Chou083cbdd2015-11-03 14:19:02 +0800126static inline void _debug_uart_init(void)
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200127{
Thomas Chou6917a5d2015-10-21 21:26:54 +0800128 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
129 u32 div;
130
131 div = (CONFIG_DEBUG_UART_CLOCK / CONFIG_BAUDRATE) - 1;
132 writel(div, &regs->divisor);
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200133}
134
Thomas Chou6917a5d2015-10-21 21:26:54 +0800135static inline void _debug_uart_putc(int ch)
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200136{
Thomas Chou6917a5d2015-10-21 21:26:54 +0800137 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
138
139 while (1) {
140 u32 st = readl(&regs->status);
141
142 if (st & ALTERA_UART_TRDY)
143 break;
144 }
145
146 writel(ch, &regs->txdata);
Marek Vasute9d0e3c2012-09-13 16:49:51 +0200147}
Thomas Chou6917a5d2015-10-21 21:26:54 +0800148
149DEBUG_UART_FUNCS
150
151#endif