1 /*
2  * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3  * Scott McNutt <smcnutt@psyent.com>
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 #include <common.h>
25 #include <watchdog.h>
26 #include <asm/io.h>
27 #include <nios2-io.h>
28 #include <linux/compiler.h>
29 #include <serial.h>
30 
31 DECLARE_GLOBAL_DATA_PTR;
32 
33 /*------------------------------------------------------------------
34  * JTAG acts as the serial port
35  *-----------------------------------------------------------------*/
36 static nios_jtag_t *jtag = (nios_jtag_t *)CONFIG_SYS_NIOS_CONSOLE;
37 
38 static void altera_jtag_serial_setbrg(void)
39 {
40 }
41 
42 static int altera_jtag_serial_init(void)
43 {
44 	return 0;
45 }
46 
47 static void altera_jtag_serial_putc(char c)
48 {
49 	while (1) {
50 		unsigned st = readl(&jtag->control);
51 		if (NIOS_JTAG_WSPACE(st))
52 			break;
53 #ifdef CONFIG_ALTERA_JTAG_UART_BYPASS
54 		if (!(st & NIOS_JTAG_AC)) /* no connection */
55 			return;
56 #endif
57 		WATCHDOG_RESET();
58 	}
59 	writel ((unsigned char)c, &jtag->data);
60 }
61 
62 static int altera_jtag_serial_tstc(void)
63 {
64 	return ( readl (&jtag->control) & NIOS_JTAG_RRDY);
65 }
66 
67 static int altera_jtag_serial_getc(void)
68 {
69 	int c;
70 	unsigned val;
71 
72 	while (1) {
73 		WATCHDOG_RESET ();
74 		val = readl (&jtag->data);
75 		if (val & NIOS_JTAG_RVALID)
76 			break;
77 	}
78 	c = val & 0x0ff;
79 	return (c);
80 }
81 
82 static struct serial_device altera_jtag_serial_drv = {
83 	.name	= "altera_jtag_uart",
84 	.start	= altera_jtag_serial_init,
85 	.stop	= NULL,
86 	.setbrg	= altera_jtag_serial_setbrg,
87 	.putc	= altera_jtag_serial_putc,
88 	.puts	= default_serial_puts,
89 	.getc	= altera_jtag_serial_getc,
90 	.tstc	= altera_jtag_serial_tstc,
91 };
92 
93 void altera_jtag_serial_initialize(void)
94 {
95 	serial_register(&altera_jtag_serial_drv);
96 }
97 
98 __weak struct serial_device *default_serial_console(void)
99 {
100 	return &altera_jtag_serial_drv;
101 }
102