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" 210c176fa8SMark A. Greer 220c176fa8SMark A. Greer static int serial_open(void) 230c176fa8SMark A. Greer { 240c176fa8SMark A. Greer struct serial_console_data *scdp = console_ops.data; 250c176fa8SMark A. Greer return scdp->open(); 260c176fa8SMark A. Greer } 270c176fa8SMark A. Greer 28b96fbb6eSGeoff Levand static void serial_write(const char *buf, int len) 290c176fa8SMark A. Greer { 300c176fa8SMark A. Greer struct serial_console_data *scdp = console_ops.data; 310c176fa8SMark A. Greer 320c176fa8SMark A. Greer while (*buf != '\0') 330c176fa8SMark A. Greer scdp->putc(*buf++); 340c176fa8SMark A. Greer } 350c176fa8SMark A. Greer 360c176fa8SMark A. Greer static void serial_edit_cmdline(char *buf, int len) 370c176fa8SMark A. Greer { 380c176fa8SMark A. Greer int timer = 0, count; 390c176fa8SMark A. Greer char ch, *cp; 400c176fa8SMark A. Greer struct serial_console_data *scdp = console_ops.data; 410c176fa8SMark A. Greer 420c176fa8SMark A. Greer cp = buf; 430c176fa8SMark A. Greer count = strlen(buf); 440c176fa8SMark A. Greer cp = &buf[count]; 450c176fa8SMark A. Greer count++; 460c176fa8SMark A. Greer 470c176fa8SMark A. Greer while (timer++ < 5*1000) { 480c176fa8SMark A. Greer if (scdp->tstc()) { 490c176fa8SMark A. Greer while (((ch = scdp->getc()) != '\n') && (ch != '\r')) { 500c176fa8SMark A. Greer /* Test for backspace/delete */ 510c176fa8SMark A. Greer if ((ch == '\b') || (ch == '\177')) { 520c176fa8SMark A. Greer if (cp != buf) { 530c176fa8SMark A. Greer cp--; 540c176fa8SMark A. Greer count--; 550c176fa8SMark A. Greer printf("\b \b"); 560c176fa8SMark A. Greer } 570c176fa8SMark A. Greer /* Test for ^x/^u (and wipe the line) */ 580c176fa8SMark A. Greer } else if ((ch == '\030') || (ch == '\025')) { 590c176fa8SMark A. Greer while (cp != buf) { 600c176fa8SMark A. Greer cp--; 610c176fa8SMark A. Greer count--; 620c176fa8SMark A. Greer printf("\b \b"); 630c176fa8SMark A. Greer } 640c176fa8SMark A. Greer } else if (count < len) { 650c176fa8SMark A. Greer *cp++ = ch; 660c176fa8SMark A. Greer count++; 670c176fa8SMark A. Greer scdp->putc(ch); 680c176fa8SMark A. Greer } 690c176fa8SMark A. Greer } 700c176fa8SMark A. Greer break; /* Exit 'timer' loop */ 710c176fa8SMark A. Greer } 720c176fa8SMark A. Greer udelay(1000); /* 1 msec */ 730c176fa8SMark A. Greer } 740c176fa8SMark A. Greer *cp = 0; 750c176fa8SMark A. Greer } 760c176fa8SMark A. Greer 770c176fa8SMark A. Greer static void serial_close(void) 780c176fa8SMark A. Greer { 790c176fa8SMark A. Greer struct serial_console_data *scdp = console_ops.data; 800c176fa8SMark A. Greer 810c176fa8SMark A. Greer if (scdp->close) 820c176fa8SMark A. Greer scdp->close(); 830c176fa8SMark A. Greer } 840c176fa8SMark A. Greer 850c176fa8SMark A. Greer static void *serial_get_stdout_devp(void) 860c176fa8SMark A. Greer { 870c176fa8SMark A. Greer void *devp; 880c176fa8SMark A. Greer char devtype[MAX_PROP_LEN]; 890c176fa8SMark A. Greer char path[MAX_PATH_LEN]; 900c176fa8SMark A. Greer 910c176fa8SMark A. Greer devp = finddevice("/chosen"); 920c176fa8SMark A. Greer if (devp == NULL) 930c176fa8SMark A. Greer goto err_out; 940c176fa8SMark A. Greer 950c176fa8SMark A. Greer if (getprop(devp, "linux,stdout-path", path, MAX_PATH_LEN) > 0) { 960c176fa8SMark A. Greer devp = finddevice(path); 970c176fa8SMark A. Greer if (devp == NULL) 980c176fa8SMark A. Greer goto err_out; 990c176fa8SMark A. Greer 1000c176fa8SMark A. Greer if ((getprop(devp, "device_type", devtype, sizeof(devtype)) > 0) 1010c176fa8SMark A. Greer && !strcmp(devtype, "serial")) 1020c176fa8SMark A. Greer return devp; 1030c176fa8SMark A. Greer } 1040c176fa8SMark A. Greer err_out: 1050c176fa8SMark A. Greer return NULL; 1060c176fa8SMark A. Greer } 1070c176fa8SMark A. Greer 1080c176fa8SMark A. Greer static struct serial_console_data serial_cd; 1090c176fa8SMark A. Greer 1100c176fa8SMark A. Greer /* Node's "compatible" property determines which serial driver to use */ 1110c176fa8SMark A. Greer int serial_console_init(void) 1120c176fa8SMark A. Greer { 1130c176fa8SMark A. Greer void *devp; 1140c176fa8SMark A. Greer int rc = -1; 1150c176fa8SMark A. Greer 1160c176fa8SMark A. Greer devp = serial_get_stdout_devp(); 1170c176fa8SMark A. Greer if (devp == NULL) 1180c176fa8SMark A. Greer goto err_out; 1190c176fa8SMark A. Greer 120dc4f397dSScott Wood if (dt_is_compatible(devp, "ns16550")) 1210c176fa8SMark A. Greer rc = ns16550_console_init(devp, &serial_cd); 122dc4f397dSScott Wood else if (dt_is_compatible(devp, "marvell,mpsc")) 123e12deb84SMark A. Greer rc = mpsc_console_init(devp, &serial_cd); 124d0f53fafSScott Wood else if (dt_is_compatible(devp, "fsl,cpm1-scc-uart") || 125d0f53fafSScott Wood dt_is_compatible(devp, "fsl,cpm1-smc-uart") || 126d0f53fafSScott Wood dt_is_compatible(devp, "fsl,cpm2-scc-uart") || 127d0f53fafSScott Wood dt_is_compatible(devp, "fsl,cpm2-smc-uart")) 128d0f53fafSScott Wood rc = cpm_console_init(devp, &serial_cd); 12985498ae8SGrant Likely else if (dt_is_compatible(devp, "mpc5200-psc-uart")) 13085498ae8SGrant Likely rc = mpc5200_psc_console_init(devp, &serial_cd); 131*7ddc5f97SGrant Likely else if (dt_is_compatible(devp, "xilinx,uartlite")) 132*7ddc5f97SGrant Likely rc = uartlite_console_init(devp, &serial_cd); 1330c176fa8SMark A. Greer 1340c176fa8SMark A. Greer /* Add other serial console driver calls here */ 1350c176fa8SMark A. Greer 1360c176fa8SMark A. Greer if (!rc) { 1370c176fa8SMark A. Greer console_ops.open = serial_open; 1380c176fa8SMark A. Greer console_ops.write = serial_write; 1390c176fa8SMark A. Greer console_ops.close = serial_close; 1400c176fa8SMark A. Greer console_ops.data = &serial_cd; 1410c176fa8SMark A. Greer 142dc4f397dSScott Wood if (serial_cd.getc) 143dc4f397dSScott Wood console_ops.edit_cmdline = serial_edit_cmdline; 144dc4f397dSScott Wood 1450c176fa8SMark A. Greer return 0; 1460c176fa8SMark A. Greer } 1470c176fa8SMark A. Greer err_out: 1480c176fa8SMark A. Greer return -1; 1490c176fa8SMark A. Greer } 150