xref: /openbmc/linux/arch/powerpc/boot/serial.c (revision 9bbc7e4ce47ecefa142fcba55eef7754af1b0213)
10c176fa8SMark A. Greer /*
20c176fa8SMark A. Greer  * Generic serial console support
30c176fa8SMark A. Greer  *
40c176fa8SMark A. Greer  * Author: Mark A. Greer <mgreer@mvista.com>
50c176fa8SMark A. Greer  *
60c176fa8SMark A. Greer  * Code in serial_edit_cmdline() copied from <file:arch/ppc/boot/simple/misc.c>
70c176fa8SMark A. Greer  * and was written by Matt Porter <mporter@kernel.crashing.org>.
80c176fa8SMark A. Greer  *
90c176fa8SMark A. Greer  * 2001,2006 (c) MontaVista Software, Inc.  This file is licensed under
100c176fa8SMark A. Greer  * the terms of the GNU General Public License version 2.  This program
110c176fa8SMark A. Greer  * is licensed "as is" without any warranty of any kind, whether express
120c176fa8SMark A. Greer  * or implied.
130c176fa8SMark A. Greer  */
140c176fa8SMark A. Greer #include <stdarg.h>
150c176fa8SMark A. Greer #include <stddef.h>
160c176fa8SMark A. Greer #include "types.h"
170c176fa8SMark A. Greer #include "string.h"
180c176fa8SMark A. Greer #include "stdio.h"
190c176fa8SMark A. Greer #include "io.h"
200c176fa8SMark A. Greer #include "ops.h"
215e9dcb61SJoel Stanley #include "autoconf.h"
220c176fa8SMark A. Greer 
230c176fa8SMark A. Greer static int serial_open(void)
240c176fa8SMark A. Greer {
250c176fa8SMark A. Greer 	struct serial_console_data *scdp = console_ops.data;
260c176fa8SMark A. Greer 	return scdp->open();
270c176fa8SMark A. Greer }
280c176fa8SMark A. Greer 
29b96fbb6eSGeoff Levand static void serial_write(const char *buf, int len)
300c176fa8SMark A. Greer {
310c176fa8SMark A. Greer 	struct serial_console_data *scdp = console_ops.data;
320c176fa8SMark A. Greer 
330c176fa8SMark A. Greer 	while (*buf != '\0')
340c176fa8SMark A. Greer 		scdp->putc(*buf++);
350c176fa8SMark A. Greer }
360c176fa8SMark A. Greer 
374b4b13d5SSimon Kagstrom static void serial_edit_cmdline(char *buf, int len, unsigned int timeout)
380c176fa8SMark A. Greer {
390c176fa8SMark A. Greer 	int timer = 0, count;
400c176fa8SMark A. Greer 	char ch, *cp;
410c176fa8SMark A. Greer 	struct serial_console_data *scdp = console_ops.data;
420c176fa8SMark A. Greer 
430c176fa8SMark A. Greer 	cp = buf;
440c176fa8SMark A. Greer 	count = strlen(buf);
450c176fa8SMark A. Greer 	cp = &buf[count];
460c176fa8SMark A. Greer 	count++;
470c176fa8SMark A. Greer 
484b4b13d5SSimon Kagstrom 	do {
490c176fa8SMark A. Greer 		if (scdp->tstc()) {
500c176fa8SMark A. Greer 			while (((ch = scdp->getc()) != '\n') && (ch != '\r')) {
510c176fa8SMark A. Greer 				/* Test for backspace/delete */
520c176fa8SMark A. Greer 				if ((ch == '\b') || (ch == '\177')) {
530c176fa8SMark A. Greer 					if (cp != buf) {
540c176fa8SMark A. Greer 						cp--;
550c176fa8SMark A. Greer 						count--;
560c176fa8SMark A. Greer 						printf("\b \b");
570c176fa8SMark A. Greer 					}
580c176fa8SMark A. Greer 				/* Test for ^x/^u (and wipe the line) */
590c176fa8SMark A. Greer 				} else if ((ch == '\030') || (ch == '\025')) {
600c176fa8SMark A. Greer 					while (cp != buf) {
610c176fa8SMark A. Greer 						cp--;
620c176fa8SMark A. Greer 						count--;
630c176fa8SMark A. Greer 						printf("\b \b");
640c176fa8SMark A. Greer 					}
650c176fa8SMark A. Greer 				} else if (count < len) {
660c176fa8SMark A. Greer 						*cp++ = ch;
670c176fa8SMark A. Greer 						count++;
680c176fa8SMark A. Greer 						scdp->putc(ch);
690c176fa8SMark A. Greer 				}
700c176fa8SMark A. Greer 			}
710c176fa8SMark A. Greer 			break;  /* Exit 'timer' loop */
720c176fa8SMark A. Greer 		}
730c176fa8SMark A. Greer 		udelay(1000);  /* 1 msec */
744b4b13d5SSimon Kagstrom 	} while (timer++ < timeout);
750c176fa8SMark A. Greer 	*cp = 0;
760c176fa8SMark A. Greer }
770c176fa8SMark A. Greer 
780c176fa8SMark A. Greer static void serial_close(void)
790c176fa8SMark A. Greer {
800c176fa8SMark A. Greer 	struct serial_console_data *scdp = console_ops.data;
810c176fa8SMark A. Greer 
820c176fa8SMark A. Greer 	if (scdp->close)
830c176fa8SMark A. Greer 		scdp->close();
840c176fa8SMark A. Greer }
850c176fa8SMark A. Greer 
860c176fa8SMark A. Greer static void *serial_get_stdout_devp(void)
870c176fa8SMark A. Greer {
880c176fa8SMark A. Greer 	void *devp;
890c176fa8SMark A. Greer 	char devtype[MAX_PROP_LEN];
900c176fa8SMark A. Greer 	char path[MAX_PATH_LEN];
910c176fa8SMark A. Greer 
920c176fa8SMark A. Greer 	devp = finddevice("/chosen");
930c176fa8SMark A. Greer 	if (devp == NULL)
940c176fa8SMark A. Greer 		goto err_out;
950c176fa8SMark A. Greer 
96*9bbc7e4cSOliver O'Halloran 	if (getprop(devp, "linux,stdout-path", path, MAX_PATH_LEN) > 0 ||
97*9bbc7e4cSOliver O'Halloran 		getprop(devp, "stdout-path", path, MAX_PATH_LEN) > 0) {
980c176fa8SMark A. Greer 		devp = finddevice(path);
990c176fa8SMark A. Greer 		if (devp == NULL)
1000c176fa8SMark A. Greer 			goto err_out;
1010c176fa8SMark A. Greer 
1020c176fa8SMark A. Greer 		if ((getprop(devp, "device_type", devtype, sizeof(devtype)) > 0)
1030c176fa8SMark A. Greer 				&& !strcmp(devtype, "serial"))
1040c176fa8SMark A. Greer 			return devp;
1050c176fa8SMark A. Greer 	}
1060c176fa8SMark A. Greer err_out:
1070c176fa8SMark A. Greer 	return NULL;
1080c176fa8SMark A. Greer }
1090c176fa8SMark A. Greer 
1100c176fa8SMark A. Greer static struct serial_console_data serial_cd;
1110c176fa8SMark A. Greer 
1120c176fa8SMark A. Greer /* Node's "compatible" property determines which serial driver to use */
1130c176fa8SMark A. Greer int serial_console_init(void)
1140c176fa8SMark A. Greer {
1150c176fa8SMark A. Greer 	void *devp;
1160c176fa8SMark A. Greer 	int rc = -1;
1170c176fa8SMark A. Greer 
1180c176fa8SMark A. Greer 	devp = serial_get_stdout_devp();
1190c176fa8SMark A. Greer 	if (devp == NULL)
1200c176fa8SMark A. Greer 		goto err_out;
1210c176fa8SMark A. Greer 
1228f23735dSGerhard Pircher 	if (dt_is_compatible(devp, "ns16550") ||
1238f23735dSGerhard Pircher 	    dt_is_compatible(devp, "pnpPNP,501"))
1240c176fa8SMark A. Greer 		rc = ns16550_console_init(devp, &serial_cd);
125c3bb690dSMichael Ellerman #ifdef CONFIG_CPM
126d0f53fafSScott Wood 	else if (dt_is_compatible(devp, "fsl,cpm1-scc-uart") ||
127d0f53fafSScott Wood 	         dt_is_compatible(devp, "fsl,cpm1-smc-uart") ||
128d0f53fafSScott Wood 	         dt_is_compatible(devp, "fsl,cpm2-scc-uart") ||
129d0f53fafSScott Wood 	         dt_is_compatible(devp, "fsl,cpm2-smc-uart"))
130d0f53fafSScott Wood 		rc = cpm_console_init(devp, &serial_cd);
131c3bb690dSMichael Ellerman #endif
132866bfc75SHannes Reinecke #ifdef CONFIG_PPC_MPC52XX
13324ce6bc4SGrant Likely 	else if (dt_is_compatible(devp, "fsl,mpc5200-psc-uart"))
13485498ae8SGrant Likely 		rc = mpc5200_psc_console_init(devp, &serial_cd);
135866bfc75SHannes Reinecke #endif
1363d6bf693SMichael Ellerman #ifdef CONFIG_XILINX_VIRTEX
137c35a8fb2SStephen Neuendorffer 	else if (dt_is_compatible(devp, "xlnx,opb-uartlite-1.00.b") ||
138c35a8fb2SStephen Neuendorffer 		 dt_is_compatible(devp, "xlnx,xps-uartlite-1.00.a"))
1397ddc5f97SGrant Likely 		rc = uartlite_console_init(devp, &serial_cd);
1403d6bf693SMichael Ellerman #endif
141f8e8e69cSMichael Ellerman #ifdef CONFIG_PPC64_BOOT_WRAPPER
142656ad58eSOliver O'Halloran 	else if (dt_is_compatible(devp, "ibm,opal-console-raw"))
143656ad58eSOliver O'Halloran 		rc = opal_console_init(devp, &serial_cd);
144f8e8e69cSMichael Ellerman #endif
1450c176fa8SMark A. Greer 
1460c176fa8SMark A. Greer 	/* Add other serial console driver calls here */
1470c176fa8SMark A. Greer 
1480c176fa8SMark A. Greer 	if (!rc) {
1490c176fa8SMark A. Greer 		console_ops.open = serial_open;
1500c176fa8SMark A. Greer 		console_ops.write = serial_write;
1510c176fa8SMark A. Greer 		console_ops.close = serial_close;
1520c176fa8SMark A. Greer 		console_ops.data = &serial_cd;
1530c176fa8SMark A. Greer 
154dc4f397dSScott Wood 		if (serial_cd.getc)
155dc4f397dSScott Wood 			console_ops.edit_cmdline = serial_edit_cmdline;
156dc4f397dSScott Wood 
1570c176fa8SMark A. Greer 		return 0;
1580c176fa8SMark A. Greer 	}
1590c176fa8SMark A. Greer err_out:
1600c176fa8SMark A. Greer 	return -1;
1610c176fa8SMark A. Greer }
162