1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
4 * Scott McNutt <smcnutt@psyent.com>
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 */
18 struct 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 */
27 struct altera_uart_platdata {
28 struct altera_uart_regs *regs;
32 static int altera_uart_setbrg(struct udevice *dev, int baudrate)
34 struct altera_uart_platdata *plat = dev->platdata;
35 struct altera_uart_regs *const regs = plat->regs;
38 div = (plat->uartclk / baudrate) - 1;
39 writel(div, ®s->divisor);
44 static int altera_uart_putc(struct udevice *dev, const char ch)
46 struct altera_uart_platdata *plat = dev->platdata;
47 struct altera_uart_regs *const regs = plat->regs;
49 if (!(readl(®s->status) & ALTERA_UART_TRDY))
52 writel(ch, ®s->txdata);
57 static int altera_uart_pending(struct udevice *dev, bool input)
59 struct altera_uart_platdata *plat = dev->platdata;
60 struct altera_uart_regs *const regs = plat->regs;
61 u32 st = readl(®s->status);
64 return st & ALTERA_UART_RRDY ? 1 : 0;
66 return !(st & ALTERA_UART_TMT);
69 static int altera_uart_getc(struct udevice *dev)
71 struct altera_uart_platdata *plat = dev->platdata;
72 struct altera_uart_regs *const regs = plat->regs;
74 if (!(readl(®s->status) & ALTERA_UART_RRDY))
77 return readl(®s->rxdata) & 0xff;
80 static int altera_uart_probe(struct udevice *dev)
85 static int altera_uart_ofdata_to_platdata(struct udevice *dev)
87 struct altera_uart_platdata *plat = dev_get_platdata(dev);
89 plat->regs = map_physmem(devfdt_get_addr(dev),
90 sizeof(struct altera_uart_regs),
92 plat->uartclk = dev_read_u32_default(dev, "clock-frequency", 0);
97 static 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,
104 static const struct udevice_id altera_uart_ids[] = {
105 { .compatible = "altr,uart-1.0" },
109 U_BOOT_DRIVER(altera_uart) = {
110 .name = "altera_uart",
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,
119 #ifdef CONFIG_DEBUG_UART_ALTERA_UART
121 #include <debug_uart.h>
123 static inline void _debug_uart_init(void)
125 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
128 div = (CONFIG_DEBUG_UART_CLOCK / CONFIG_BAUDRATE) - 1;
129 writel(div, ®s->divisor);
132 static inline void _debug_uart_putc(int ch)
134 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
137 u32 st = readl(®s->status);
139 if (st & ALTERA_UART_TRDY)
143 writel(ch, ®s->txdata);