blob: 436cf2331df952cc92e0ca0e9486ab96f0401f42 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Scott McNuttc9d4f462010-03-19 19:03:28 -04002/*
3 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
4 * Scott McNutt <smcnutt@psyent.com>
Scott McNuttc9d4f462010-03-19 19:03:28 -04005 */
6
Scott McNuttc9d4f462010-03-19 19:03:28 -04007#include <common.h>
Thomas Chouda2f8382015-10-21 21:26:54 +08008#include <dm.h>
9#include <errno.h>
Marek Vasutb207d642012-09-13 16:49:51 +020010#include <serial.h>
Thomas Chou89241482015-10-31 20:53:23 +080011#include <asm/io.h>
12
Thomas Chou89241482015-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 McNuttc9d4f462010-03-19 19:03:28 -040017
Thomas Chouda2f8382015-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};
26
27struct altera_uart_platdata {
28 struct altera_uart_regs *regs;
29 unsigned int uartclk;
30};
Thomas Chou86450712014-08-25 16:50:14 +080031
Thomas Chouda2f8382015-10-21 21:26:54 +080032static int altera_uart_setbrg(struct udevice *dev, int baudrate)
Marek Vasutb207d642012-09-13 16:49:51 +020033{
Thomas Chouda2f8382015-10-21 21:26:54 +080034 struct altera_uart_platdata *plat = dev->platdata;
35 struct altera_uart_regs *const regs = plat->regs;
36 u32 div;
37
38 div = (plat->uartclk / baudrate) - 1;
39 writel(div, &regs->divisor);
40
41 return 0;
Marek Vasutb207d642012-09-13 16:49:51 +020042}
43
Thomas Chouda2f8382015-10-21 21:26:54 +080044static int altera_uart_putc(struct udevice *dev, const char ch)
45{
46 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
54 return 0;
55}
56
57static int altera_uart_pending(struct udevice *dev, bool input)
58{
59 struct altera_uart_platdata *plat = dev->platdata;
60 struct altera_uart_regs *const regs = plat->regs;
61 u32 st = readl(&regs->status);
62
63 if (input)
64 return st & ALTERA_UART_RRDY ? 1 : 0;
65 else
66 return !(st & ALTERA_UART_TMT);
67}
68
69static int altera_uart_getc(struct udevice *dev)
70{
71 struct altera_uart_platdata *plat = dev->platdata;
72 struct altera_uart_regs *const regs = plat->regs;
73
74 if (!(readl(&regs->status) & ALTERA_UART_RRDY))
75 return -EAGAIN;
76
77 return readl(&regs->rxdata) & 0xff;
78}
79
80static int altera_uart_probe(struct udevice *dev)
Marek Vasutb207d642012-09-13 16:49:51 +020081{
82 return 0;
83}
Scott McNuttc9d4f462010-03-19 19:03:28 -040084
Thomas Chouda2f8382015-10-21 21:26:54 +080085static int altera_uart_ofdata_to_platdata(struct udevice *dev)
Scott McNuttc9d4f462010-03-19 19:03:28 -040086{
Thomas Chouda2f8382015-10-21 21:26:54 +080087 struct altera_uart_platdata *plat = dev_get_platdata(dev);
Scott McNuttc9d4f462010-03-19 19:03:28 -040088
Simon Glassa821c4a2017-05-17 17:18:05 -060089 plat->regs = map_physmem(devfdt_get_addr(dev),
Thomas Chou1ec60b92015-11-14 10:38:09 +080090 sizeof(struct altera_uart_regs),
91 MAP_NOCACHE);
Simon Goldschmidt41b22c02019-05-09 22:11:58 +020092 plat->uartclk = dev_read_u32_default(dev, "clock-frequency", 0);
Scott McNuttc9d4f462010-03-19 19:03:28 -040093
Marek Vasutb207d642012-09-13 16:49:51 +020094 return 0;
Scott McNuttc9d4f462010-03-19 19:03:28 -040095}
96
Thomas Chouda2f8382015-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,
Marek Vasutb207d642012-09-13 16:49:51 +0200102};
103
Thomas Chouda2f8382015-10-21 21:26:54 +0800104static const struct udevice_id altera_uart_ids[] = {
Thomas Chou89241482015-10-31 20:53:23 +0800105 { .compatible = "altr,uart-1.0" },
106 {}
Thomas Chouda2f8382015-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,
Thomas Chouda2f8382015-10-21 21:26:54 +0800117};
118
119#ifdef CONFIG_DEBUG_UART_ALTERA_UART
120
121#include <debug_uart.h>
122
Thomas Choue03c17d2015-11-03 14:19:02 +0800123static inline void _debug_uart_init(void)
Marek Vasutb207d642012-09-13 16:49:51 +0200124{
Thomas Chouda2f8382015-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 Vasutb207d642012-09-13 16:49:51 +0200130}
131
Thomas Chouda2f8382015-10-21 21:26:54 +0800132static inline void _debug_uart_putc(int ch)
Marek Vasutb207d642012-09-13 16:49:51 +0200133{
Thomas Chouda2f8382015-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 Vasutb207d642012-09-13 16:49:51 +0200144}
Thomas Chouda2f8382015-10-21 21:26:54 +0800145
146DEBUG_UART_FUNCS
147
148#endif