1 /*
2  * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3  * Scott McNutt <smcnutt@psyent.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <watchdog.h>
10 #include <asm/io.h>
11 #include <nios2-io.h>
12 #include <linux/compiler.h>
13 #include <serial.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 /*------------------------------------------------------------------
18  * JTAG acts as the serial port
19  *-----------------------------------------------------------------*/
20 static nios_jtag_t *jtag = (nios_jtag_t *)CONFIG_SYS_NIOS_CONSOLE;
21 
22 static void altera_jtag_serial_setbrg(void)
23 {
24 }
25 
26 static int altera_jtag_serial_init(void)
27 {
28 	return 0;
29 }
30 
31 static void altera_jtag_serial_putc(char c)
32 {
33 	while (1) {
34 		unsigned st = readl(&jtag->control);
35 		if (NIOS_JTAG_WSPACE(st))
36 			break;
37 #ifdef CONFIG_ALTERA_JTAG_UART_BYPASS
38 		if (!(st & NIOS_JTAG_AC)) /* no connection */
39 			return;
40 #endif
41 		WATCHDOG_RESET();
42 	}
43 	writel ((unsigned char)c, &jtag->data);
44 }
45 
46 static int altera_jtag_serial_tstc(void)
47 {
48 	return ( readl (&jtag->control) & NIOS_JTAG_RRDY);
49 }
50 
51 static int altera_jtag_serial_getc(void)
52 {
53 	int c;
54 	unsigned val;
55 
56 	while (1) {
57 		WATCHDOG_RESET ();
58 		val = readl (&jtag->data);
59 		if (val & NIOS_JTAG_RVALID)
60 			break;
61 	}
62 	c = val & 0x0ff;
63 	return (c);
64 }
65 
66 static struct serial_device altera_jtag_serial_drv = {
67 	.name	= "altera_jtag_uart",
68 	.start	= altera_jtag_serial_init,
69 	.stop	= NULL,
70 	.setbrg	= altera_jtag_serial_setbrg,
71 	.putc	= altera_jtag_serial_putc,
72 	.puts	= default_serial_puts,
73 	.getc	= altera_jtag_serial_getc,
74 	.tstc	= altera_jtag_serial_tstc,
75 };
76 
77 void altera_jtag_serial_initialize(void)
78 {
79 	serial_register(&altera_jtag_serial_drv);
80 }
81 
82 __weak struct serial_device *default_serial_console(void)
83 {
84 	return &altera_jtag_serial_drv;
85 }
86