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 void altera_jtag_serial_puts(const char *s)
63 {
64 	while (*s != 0)
65 		serial_putc (*s++);
66 }
67 
68 static int altera_jtag_serial_tstc(void)
69 {
70 	return ( readl (&jtag->control) & NIOS_JTAG_RRDY);
71 }
72 
73 static int altera_jtag_serial_getc(void)
74 {
75 	int c;
76 	unsigned val;
77 
78 	while (1) {
79 		WATCHDOG_RESET ();
80 		val = readl (&jtag->data);
81 		if (val & NIOS_JTAG_RVALID)
82 			break;
83 	}
84 	c = val & 0x0ff;
85 	return (c);
86 }
87 
88 static struct serial_device altera_jtag_serial_drv = {
89 	.name	= "altera_jtag_uart",
90 	.start	= altera_jtag_serial_init,
91 	.stop	= NULL,
92 	.setbrg	= altera_jtag_serial_setbrg,
93 	.putc	= altera_jtag_serial_putc,
94 	.puts	= altera_jtag_serial_puts,
95 	.getc	= altera_jtag_serial_getc,
96 	.tstc	= altera_jtag_serial_tstc,
97 };
98 
99 void altera_jtag_serial_initialize(void)
100 {
101 	serial_register(&altera_jtag_serial_drv);
102 }
103 
104 __weak struct serial_device *default_serial_console(void)
105 {
106 	return &altera_jtag_serial_drv;
107 }
108