xref: /openbmc/linux/arch/powerpc/boot/serial.c (revision 0c176fa80fdfa9b4e0753e37223b056994c818d2)
1*0c176fa8SMark A. Greer /*
2*0c176fa8SMark A. Greer  * Generic serial console support
3*0c176fa8SMark A. Greer  *
4*0c176fa8SMark A. Greer  * Author: Mark A. Greer <mgreer@mvista.com>
5*0c176fa8SMark A. Greer  *
6*0c176fa8SMark A. Greer  * Code in serial_edit_cmdline() copied from <file:arch/ppc/boot/simple/misc.c>
7*0c176fa8SMark A. Greer  * and was written by Matt Porter <mporter@kernel.crashing.org>.
8*0c176fa8SMark A. Greer  *
9*0c176fa8SMark A. Greer  * 2001,2006 (c) MontaVista Software, Inc.  This file is licensed under
10*0c176fa8SMark A. Greer  * the terms of the GNU General Public License version 2.  This program
11*0c176fa8SMark A. Greer  * is licensed "as is" without any warranty of any kind, whether express
12*0c176fa8SMark A. Greer  * or implied.
13*0c176fa8SMark A. Greer  */
14*0c176fa8SMark A. Greer #include <stdarg.h>
15*0c176fa8SMark A. Greer #include <stddef.h>
16*0c176fa8SMark A. Greer #include "types.h"
17*0c176fa8SMark A. Greer #include "string.h"
18*0c176fa8SMark A. Greer #include "stdio.h"
19*0c176fa8SMark A. Greer #include "io.h"
20*0c176fa8SMark A. Greer #include "ops.h"
21*0c176fa8SMark A. Greer 
22*0c176fa8SMark A. Greer extern void udelay(long delay);
23*0c176fa8SMark A. Greer 
24*0c176fa8SMark A. Greer static int serial_open(void)
25*0c176fa8SMark A. Greer {
26*0c176fa8SMark A. Greer 	struct serial_console_data *scdp = console_ops.data;
27*0c176fa8SMark A. Greer 	return scdp->open();
28*0c176fa8SMark A. Greer }
29*0c176fa8SMark A. Greer 
30*0c176fa8SMark A. Greer static void serial_write(char *buf, int len)
31*0c176fa8SMark A. Greer {
32*0c176fa8SMark A. Greer 	struct serial_console_data *scdp = console_ops.data;
33*0c176fa8SMark A. Greer 
34*0c176fa8SMark A. Greer 	while (*buf != '\0')
35*0c176fa8SMark A. Greer 		scdp->putc(*buf++);
36*0c176fa8SMark A. Greer }
37*0c176fa8SMark A. Greer 
38*0c176fa8SMark A. Greer static void serial_edit_cmdline(char *buf, int len)
39*0c176fa8SMark A. Greer {
40*0c176fa8SMark A. Greer 	int timer = 0, count;
41*0c176fa8SMark A. Greer 	char ch, *cp;
42*0c176fa8SMark A. Greer 	struct serial_console_data *scdp = console_ops.data;
43*0c176fa8SMark A. Greer 
44*0c176fa8SMark A. Greer 	cp = buf;
45*0c176fa8SMark A. Greer 	count = strlen(buf);
46*0c176fa8SMark A. Greer 	cp = &buf[count];
47*0c176fa8SMark A. Greer 	count++;
48*0c176fa8SMark A. Greer 
49*0c176fa8SMark A. Greer 	while (timer++ < 5*1000) {
50*0c176fa8SMark A. Greer 		if (scdp->tstc()) {
51*0c176fa8SMark A. Greer 			while (((ch = scdp->getc()) != '\n') && (ch != '\r')) {
52*0c176fa8SMark A. Greer 				/* Test for backspace/delete */
53*0c176fa8SMark A. Greer 				if ((ch == '\b') || (ch == '\177')) {
54*0c176fa8SMark A. Greer 					if (cp != buf) {
55*0c176fa8SMark A. Greer 						cp--;
56*0c176fa8SMark A. Greer 						count--;
57*0c176fa8SMark A. Greer 						printf("\b \b");
58*0c176fa8SMark A. Greer 					}
59*0c176fa8SMark A. Greer 				/* Test for ^x/^u (and wipe the line) */
60*0c176fa8SMark A. Greer 				} else if ((ch == '\030') || (ch == '\025')) {
61*0c176fa8SMark A. Greer 					while (cp != buf) {
62*0c176fa8SMark A. Greer 						cp--;
63*0c176fa8SMark A. Greer 						count--;
64*0c176fa8SMark A. Greer 						printf("\b \b");
65*0c176fa8SMark A. Greer 					}
66*0c176fa8SMark A. Greer 				} else if (count < len) {
67*0c176fa8SMark A. Greer 						*cp++ = ch;
68*0c176fa8SMark A. Greer 						count++;
69*0c176fa8SMark A. Greer 						scdp->putc(ch);
70*0c176fa8SMark A. Greer 				}
71*0c176fa8SMark A. Greer 			}
72*0c176fa8SMark A. Greer 			break;  /* Exit 'timer' loop */
73*0c176fa8SMark A. Greer 		}
74*0c176fa8SMark A. Greer 		udelay(1000);  /* 1 msec */
75*0c176fa8SMark A. Greer 	}
76*0c176fa8SMark A. Greer 	*cp = 0;
77*0c176fa8SMark A. Greer }
78*0c176fa8SMark A. Greer 
79*0c176fa8SMark A. Greer static void serial_close(void)
80*0c176fa8SMark A. Greer {
81*0c176fa8SMark A. Greer 	struct serial_console_data *scdp = console_ops.data;
82*0c176fa8SMark A. Greer 
83*0c176fa8SMark A. Greer 	if (scdp->close)
84*0c176fa8SMark A. Greer 		scdp->close();
85*0c176fa8SMark A. Greer }
86*0c176fa8SMark A. Greer 
87*0c176fa8SMark A. Greer static void *serial_get_stdout_devp(void)
88*0c176fa8SMark A. Greer {
89*0c176fa8SMark A. Greer 	void *devp;
90*0c176fa8SMark A. Greer 	char devtype[MAX_PROP_LEN];
91*0c176fa8SMark A. Greer 	char path[MAX_PATH_LEN];
92*0c176fa8SMark A. Greer 
93*0c176fa8SMark A. Greer 	devp = finddevice("/chosen");
94*0c176fa8SMark A. Greer 	if (devp == NULL)
95*0c176fa8SMark A. Greer 		goto err_out;
96*0c176fa8SMark A. Greer 
97*0c176fa8SMark A. Greer 	if (getprop(devp, "linux,stdout-path", path, MAX_PATH_LEN) > 0) {
98*0c176fa8SMark A. Greer 		devp = finddevice(path);
99*0c176fa8SMark A. Greer 		if (devp == NULL)
100*0c176fa8SMark A. Greer 			goto err_out;
101*0c176fa8SMark A. Greer 
102*0c176fa8SMark A. Greer 		if ((getprop(devp, "device_type", devtype, sizeof(devtype)) > 0)
103*0c176fa8SMark A. Greer 				&& !strcmp(devtype, "serial"))
104*0c176fa8SMark A. Greer 			return devp;
105*0c176fa8SMark A. Greer 	}
106*0c176fa8SMark A. Greer err_out:
107*0c176fa8SMark A. Greer 	return NULL;
108*0c176fa8SMark A. Greer }
109*0c176fa8SMark A. Greer 
110*0c176fa8SMark A. Greer static struct serial_console_data serial_cd;
111*0c176fa8SMark A. Greer 
112*0c176fa8SMark A. Greer /* Node's "compatible" property determines which serial driver to use */
113*0c176fa8SMark A. Greer int serial_console_init(void)
114*0c176fa8SMark A. Greer {
115*0c176fa8SMark A. Greer 	void *devp;
116*0c176fa8SMark A. Greer 	int rc = -1;
117*0c176fa8SMark A. Greer 	char compat[MAX_PROP_LEN];
118*0c176fa8SMark A. Greer 
119*0c176fa8SMark A. Greer 	devp = serial_get_stdout_devp();
120*0c176fa8SMark A. Greer 	if (devp == NULL)
121*0c176fa8SMark A. Greer 		goto err_out;
122*0c176fa8SMark A. Greer 
123*0c176fa8SMark A. Greer 	if (getprop(devp, "compatible", compat, sizeof(compat)) < 0)
124*0c176fa8SMark A. Greer 		goto err_out;
125*0c176fa8SMark A. Greer 
126*0c176fa8SMark A. Greer 	if (!strcmp(compat, "ns16550"))
127*0c176fa8SMark A. Greer 		rc = ns16550_console_init(devp, &serial_cd);
128*0c176fa8SMark A. Greer 
129*0c176fa8SMark A. Greer 	/* Add other serial console driver calls here */
130*0c176fa8SMark A. Greer 
131*0c176fa8SMark A. Greer 	if (!rc) {
132*0c176fa8SMark A. Greer 		console_ops.open = serial_open;
133*0c176fa8SMark A. Greer 		console_ops.write = serial_write;
134*0c176fa8SMark A. Greer 		console_ops.edit_cmdline = serial_edit_cmdline;
135*0c176fa8SMark A. Greer 		console_ops.close = serial_close;
136*0c176fa8SMark A. Greer 		console_ops.data = &serial_cd;
137*0c176fa8SMark A. Greer 
138*0c176fa8SMark A. Greer 		return 0;
139*0c176fa8SMark A. Greer 	}
140*0c176fa8SMark A. Greer err_out:
141*0c176fa8SMark A. Greer 	return -1;
142*0c176fa8SMark A. Greer }
143