1*e01432baSMichael Ellerman // SPDX-License-Identifier: GPL-2.0
20c176fa8SMark A. Greer /*
30c176fa8SMark A. Greer * Generic serial console support
40c176fa8SMark A. Greer *
50c176fa8SMark A. Greer * Author: Mark A. Greer <mgreer@mvista.com>
60c176fa8SMark A. Greer *
70c176fa8SMark A. Greer * Code in serial_edit_cmdline() copied from <file:arch/ppc/boot/simple/misc.c>
80c176fa8SMark A. Greer * and was written by Matt Porter <mporter@kernel.crashing.org>.
90c176fa8SMark A. Greer *
10*e01432baSMichael Ellerman * 2001,2006 (c) MontaVista Software, Inc.
110c176fa8SMark A. Greer */
120c176fa8SMark A. Greer #include <stdarg.h>
130c176fa8SMark A. Greer #include <stddef.h>
140c176fa8SMark A. Greer #include "types.h"
150c176fa8SMark A. Greer #include "string.h"
160c176fa8SMark A. Greer #include "stdio.h"
170c176fa8SMark A. Greer #include "io.h"
180c176fa8SMark A. Greer #include "ops.h"
190c176fa8SMark A. Greer
serial_open(void)200c176fa8SMark A. Greer static int serial_open(void)
210c176fa8SMark A. Greer {
220c176fa8SMark A. Greer struct serial_console_data *scdp = console_ops.data;
230c176fa8SMark A. Greer return scdp->open();
240c176fa8SMark A. Greer }
250c176fa8SMark A. Greer
serial_write(const char * buf,int len)26b96fbb6eSGeoff Levand static void serial_write(const char *buf, int len)
270c176fa8SMark A. Greer {
280c176fa8SMark A. Greer struct serial_console_data *scdp = console_ops.data;
290c176fa8SMark A. Greer
300c176fa8SMark A. Greer while (*buf != '\0')
310c176fa8SMark A. Greer scdp->putc(*buf++);
320c176fa8SMark A. Greer }
330c176fa8SMark A. Greer
serial_edit_cmdline(char * buf,int len,unsigned int timeout)344b4b13d5SSimon Kagstrom static void serial_edit_cmdline(char *buf, int len, unsigned int timeout)
350c176fa8SMark A. Greer {
360c176fa8SMark A. Greer int timer = 0, count;
370c176fa8SMark A. Greer char ch, *cp;
380c176fa8SMark A. Greer struct serial_console_data *scdp = console_ops.data;
390c176fa8SMark A. Greer
400c176fa8SMark A. Greer cp = buf;
410c176fa8SMark A. Greer count = strlen(buf);
420c176fa8SMark A. Greer cp = &buf[count];
430c176fa8SMark A. Greer count++;
440c176fa8SMark A. Greer
454b4b13d5SSimon Kagstrom do {
460c176fa8SMark A. Greer if (scdp->tstc()) {
470c176fa8SMark A. Greer while (((ch = scdp->getc()) != '\n') && (ch != '\r')) {
480c176fa8SMark A. Greer /* Test for backspace/delete */
490c176fa8SMark A. Greer if ((ch == '\b') || (ch == '\177')) {
500c176fa8SMark A. Greer if (cp != buf) {
510c176fa8SMark A. Greer cp--;
520c176fa8SMark A. Greer count--;
530c176fa8SMark A. Greer printf("\b \b");
540c176fa8SMark A. Greer }
550c176fa8SMark A. Greer /* Test for ^x/^u (and wipe the line) */
560c176fa8SMark A. Greer } else if ((ch == '\030') || (ch == '\025')) {
570c176fa8SMark A. Greer while (cp != buf) {
580c176fa8SMark A. Greer cp--;
590c176fa8SMark A. Greer count--;
600c176fa8SMark A. Greer printf("\b \b");
610c176fa8SMark A. Greer }
620c176fa8SMark A. Greer } else if (count < len) {
630c176fa8SMark A. Greer *cp++ = ch;
640c176fa8SMark A. Greer count++;
650c176fa8SMark A. Greer scdp->putc(ch);
660c176fa8SMark A. Greer }
670c176fa8SMark A. Greer }
680c176fa8SMark A. Greer break; /* Exit 'timer' loop */
690c176fa8SMark A. Greer }
700c176fa8SMark A. Greer udelay(1000); /* 1 msec */
714b4b13d5SSimon Kagstrom } while (timer++ < timeout);
720c176fa8SMark A. Greer *cp = 0;
730c176fa8SMark A. Greer }
740c176fa8SMark A. Greer
serial_close(void)750c176fa8SMark A. Greer static void serial_close(void)
760c176fa8SMark A. Greer {
770c176fa8SMark A. Greer struct serial_console_data *scdp = console_ops.data;
780c176fa8SMark A. Greer
790c176fa8SMark A. Greer if (scdp->close)
800c176fa8SMark A. Greer scdp->close();
810c176fa8SMark A. Greer }
820c176fa8SMark A. Greer
serial_get_stdout_devp(void)830c176fa8SMark A. Greer static void *serial_get_stdout_devp(void)
840c176fa8SMark A. Greer {
850c176fa8SMark A. Greer void *devp;
860c176fa8SMark A. Greer char devtype[MAX_PROP_LEN];
870c176fa8SMark A. Greer char path[MAX_PATH_LEN];
880c176fa8SMark A. Greer
890c176fa8SMark A. Greer devp = finddevice("/chosen");
900c176fa8SMark A. Greer if (devp == NULL)
910c176fa8SMark A. Greer goto err_out;
920c176fa8SMark A. Greer
939bbc7e4cSOliver O'Halloran if (getprop(devp, "linux,stdout-path", path, MAX_PATH_LEN) > 0 ||
949bbc7e4cSOliver O'Halloran getprop(devp, "stdout-path", path, MAX_PATH_LEN) > 0) {
950c176fa8SMark A. Greer devp = finddevice(path);
960c176fa8SMark A. Greer if (devp == NULL)
970c176fa8SMark A. Greer goto err_out;
980c176fa8SMark A. Greer
990c176fa8SMark A. Greer if ((getprop(devp, "device_type", devtype, sizeof(devtype)) > 0)
1000c176fa8SMark A. Greer && !strcmp(devtype, "serial"))
1010c176fa8SMark A. Greer return devp;
1020c176fa8SMark A. Greer }
1030c176fa8SMark A. Greer err_out:
1040c176fa8SMark A. Greer return NULL;
1050c176fa8SMark A. Greer }
1060c176fa8SMark A. Greer
1070c176fa8SMark A. Greer static struct serial_console_data serial_cd;
1080c176fa8SMark A. Greer
1090c176fa8SMark A. Greer /* Node's "compatible" property determines which serial driver to use */
serial_console_init(void)1100c176fa8SMark A. Greer int serial_console_init(void)
1110c176fa8SMark A. Greer {
1120c176fa8SMark A. Greer void *devp;
1130c176fa8SMark A. Greer int rc = -1;
1140c176fa8SMark A. Greer
1150c176fa8SMark A. Greer devp = serial_get_stdout_devp();
1160c176fa8SMark A. Greer if (devp == NULL)
1170c176fa8SMark A. Greer goto err_out;
1180c176fa8SMark A. Greer
1198f23735dSGerhard Pircher if (dt_is_compatible(devp, "ns16550") ||
1208f23735dSGerhard Pircher dt_is_compatible(devp, "pnpPNP,501"))
1210c176fa8SMark A. Greer rc = ns16550_console_init(devp, &serial_cd);
122c3bb690dSMichael Ellerman #ifdef CONFIG_CPM
123d0f53fafSScott Wood else if (dt_is_compatible(devp, "fsl,cpm1-scc-uart") ||
124d0f53fafSScott Wood dt_is_compatible(devp, "fsl,cpm1-smc-uart") ||
125d0f53fafSScott Wood dt_is_compatible(devp, "fsl,cpm2-scc-uart") ||
126d0f53fafSScott Wood dt_is_compatible(devp, "fsl,cpm2-smc-uart"))
127d0f53fafSScott Wood rc = cpm_console_init(devp, &serial_cd);
128c3bb690dSMichael Ellerman #endif
129e5eff896SMichael Ellerman #ifdef CONFIG_PPC_MPC52xx
13024ce6bc4SGrant Likely else if (dt_is_compatible(devp, "fsl,mpc5200-psc-uart"))
13185498ae8SGrant Likely rc = mpc5200_psc_console_init(devp, &serial_cd);
132866bfc75SHannes Reinecke #endif
1336ffeb56eSCédric Le Goater #ifdef CONFIG_PPC_POWERNV
134656ad58eSOliver O'Halloran else if (dt_is_compatible(devp, "ibm,opal-console-raw"))
135656ad58eSOliver O'Halloran rc = opal_console_init(devp, &serial_cd);
136f8e8e69cSMichael Ellerman #endif
1370c176fa8SMark A. Greer
1380c176fa8SMark A. Greer /* Add other serial console driver calls here */
1390c176fa8SMark A. Greer
1400c176fa8SMark A. Greer if (!rc) {
1410c176fa8SMark A. Greer console_ops.open = serial_open;
1420c176fa8SMark A. Greer console_ops.write = serial_write;
1430c176fa8SMark A. Greer console_ops.close = serial_close;
1440c176fa8SMark A. Greer console_ops.data = &serial_cd;
1450c176fa8SMark A. Greer
146dc4f397dSScott Wood if (serial_cd.getc)
147dc4f397dSScott Wood console_ops.edit_cmdline = serial_edit_cmdline;
148dc4f397dSScott Wood
1490c176fa8SMark A. Greer return 0;
1500c176fa8SMark A. Greer }
1510c176fa8SMark A. Greer err_out:
1520c176fa8SMark A. Greer return -1;
1530c176fa8SMark A. Greer }
154