1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
12 #include <linux/bitops.h>
15 #define ALTERA_JTAG_RVALID BIT(15) /* Read valid */
17 /* control register */
18 #define ALTERA_JTAG_AC BIT(10) /* activity indicator */
19 #define ALTERA_JTAG_RRDY BIT(12) /* read available */
20 #define ALTERA_JTAG_WSPACE(d) ((d) >> 16) /* Write space avail */
21 /* Write fifo size. FIXME: this should be extracted with sopc2dts */
22 #define ALTERA_JTAG_WRITE_DEPTH 64
24 struct altera_jtaguart_regs {
25 u32 data; /* Data register */
26 u32 control; /* Control register */
29 struct altera_jtaguart_plat {
30 struct altera_jtaguart_regs *regs;
33 static int altera_jtaguart_setbrg(struct udevice *dev, int baudrate)
38 static int altera_jtaguart_putc(struct udevice *dev, const char ch)
40 struct altera_jtaguart_plat *plat = dev->plat;
41 struct altera_jtaguart_regs *const regs = plat->regs;
42 u32 st = readl(®s->control);
44 #ifdef CONFIG_ALTERA_JTAG_UART_BYPASS
45 if (!(st & ALTERA_JTAG_AC)) /* no connection yet */
49 if (ALTERA_JTAG_WSPACE(st) == 0)
52 writel(ch, ®s->data);
57 static int altera_jtaguart_pending(struct udevice *dev, bool input)
59 struct altera_jtaguart_plat *plat = dev->plat;
60 struct altera_jtaguart_regs *const regs = plat->regs;
61 u32 st = readl(®s->control);
64 return st & ALTERA_JTAG_RRDY ? 1 : 0;
66 return !(ALTERA_JTAG_WSPACE(st) == ALTERA_JTAG_WRITE_DEPTH);
69 static int altera_jtaguart_getc(struct udevice *dev)
71 struct altera_jtaguart_plat *plat = dev->plat;
72 struct altera_jtaguart_regs *const regs = plat->regs;
75 val = readl(®s->data);
77 if (!(val & ALTERA_JTAG_RVALID))
83 static int altera_jtaguart_probe(struct udevice *dev)
85 #ifdef CONFIG_ALTERA_JTAG_UART_BYPASS
86 struct altera_jtaguart_plat *plat = dev->plat;
87 struct altera_jtaguart_regs *const regs = plat->regs;
89 writel(ALTERA_JTAG_AC, ®s->control); /* clear AC flag */
94 static int altera_jtaguart_of_to_plat(struct udevice *dev)
96 struct altera_jtaguart_plat *plat = dev_get_plat(dev);
98 plat->regs = map_physmem(dev_read_addr(dev),
99 sizeof(struct altera_jtaguart_regs),
105 static const struct dm_serial_ops altera_jtaguart_ops = {
106 .putc = altera_jtaguart_putc,
107 .pending = altera_jtaguart_pending,
108 .getc = altera_jtaguart_getc,
109 .setbrg = altera_jtaguart_setbrg,
112 static const struct udevice_id altera_jtaguart_ids[] = {
113 { .compatible = "altr,juart-1.0" },
117 U_BOOT_DRIVER(altera_jtaguart) = {
118 .name = "altera_jtaguart",
120 .of_match = altera_jtaguart_ids,
121 .of_to_plat = altera_jtaguart_of_to_plat,
122 .plat_auto = sizeof(struct altera_jtaguart_plat),
123 .probe = altera_jtaguart_probe,
124 .ops = &altera_jtaguart_ops,
127 #ifdef CONFIG_DEBUG_UART_ALTERA_JTAGUART
129 #include <debug_uart.h>
131 static inline void _debug_uart_init(void)
135 static inline void _debug_uart_putc(int ch)
137 struct altera_jtaguart_regs *regs = (void *)CONFIG_DEBUG_UART_BASE;
140 u32 st = readl(®s->control);
142 if (ALTERA_JTAG_WSPACE(st))
146 writel(ch, ®s->data);