2 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3 * Scott McNutt <smcnutt@psyent.com>
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/compiler.h>
15 struct altera_uart_regs {
16 u32 rxdata; /* Rx data reg */
17 u32 txdata; /* Tx data reg */
18 u32 status; /* Status reg */
19 u32 control; /* Control reg */
20 u32 divisor; /* Baud rate divisor reg */
21 u32 endofpacket; /* End-of-packet reg */
24 struct altera_uart_platdata {
25 struct altera_uart_regs *regs;
30 #define ALTERA_UART_TMT BIT(5) /* tx empty */
31 #define ALTERA_UART_TRDY BIT(6) /* tx ready */
32 #define ALTERA_UART_RRDY BIT(7) /* rx ready */
34 DECLARE_GLOBAL_DATA_PTR;
36 static int altera_uart_setbrg(struct udevice *dev, int baudrate)
38 struct altera_uart_platdata *plat = dev->platdata;
39 struct altera_uart_regs *const regs = plat->regs;
42 div = (plat->uartclk / baudrate) - 1;
43 writel(div, ®s->divisor);
48 static int altera_uart_putc(struct udevice *dev, const char ch)
50 struct altera_uart_platdata *plat = dev->platdata;
51 struct altera_uart_regs *const regs = plat->regs;
53 if (!(readl(®s->status) & ALTERA_UART_TRDY))
56 writel(ch, ®s->txdata);
61 static int altera_uart_pending(struct udevice *dev, bool input)
63 struct altera_uart_platdata *plat = dev->platdata;
64 struct altera_uart_regs *const regs = plat->regs;
65 u32 st = readl(®s->status);
68 return st & ALTERA_UART_RRDY ? 1 : 0;
70 return !(st & ALTERA_UART_TMT);
73 static int altera_uart_getc(struct udevice *dev)
75 struct altera_uart_platdata *plat = dev->platdata;
76 struct altera_uart_regs *const regs = plat->regs;
78 if (!(readl(®s->status) & ALTERA_UART_RRDY))
81 return readl(®s->rxdata) & 0xff;
84 static int altera_uart_probe(struct udevice *dev)
89 static int altera_uart_ofdata_to_platdata(struct udevice *dev)
91 struct altera_uart_platdata *plat = dev_get_platdata(dev);
93 plat->regs = ioremap(dev_get_addr(dev),
94 sizeof(struct altera_uart_regs));
95 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
96 "clock-frequency", 0);
101 static 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,
108 static const struct udevice_id altera_uart_ids[] = {
109 { .compatible = "altr,uart-1.0", },
113 U_BOOT_DRIVER(altera_uart) = {
114 .name = "altera_uart",
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,
124 #ifdef CONFIG_DEBUG_UART_ALTERA_UART
126 #include <debug_uart.h>
128 void debug_uart_init(void)
130 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
133 div = (CONFIG_DEBUG_UART_CLOCK / CONFIG_BAUDRATE) - 1;
134 writel(div, ®s->divisor);
137 static inline void _debug_uart_putc(int ch)
139 struct altera_uart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
142 u32 st = readl(®s->status);
144 if (st & ALTERA_UART_TRDY)
148 writel(ch, ®s->txdata);