serial.c 3.7 KB

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