serial.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. getprop(devp, "stdout-path", path, MAX_PATH_LEN) > 0) {
  86. devp = finddevice(path);
  87. if (devp == NULL)
  88. goto err_out;
  89. if ((getprop(devp, "device_type", devtype, sizeof(devtype)) > 0)
  90. && !strcmp(devtype, "serial"))
  91. return devp;
  92. }
  93. err_out:
  94. return NULL;
  95. }
  96. static struct serial_console_data serial_cd;
  97. /* Node's "compatible" property determines which serial driver to use */
  98. int serial_console_init(void)
  99. {
  100. void *devp;
  101. int rc = -1;
  102. devp = serial_get_stdout_devp();
  103. if (devp == NULL)
  104. goto err_out;
  105. if (dt_is_compatible(devp, "ns16550") ||
  106. dt_is_compatible(devp, "pnpPNP,501"))
  107. rc = ns16550_console_init(devp, &serial_cd);
  108. #ifdef CONFIG_CPM
  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. #endif
  115. #ifdef CONFIG_PPC_MPC52xx
  116. else if (dt_is_compatible(devp, "fsl,mpc5200-psc-uart"))
  117. rc = mpc5200_psc_console_init(devp, &serial_cd);
  118. #endif
  119. #ifdef CONFIG_PPC64_BOOT_WRAPPER
  120. else if (dt_is_compatible(devp, "ibm,opal-console-raw"))
  121. rc = opal_console_init(devp, &serial_cd);
  122. #endif
  123. /* Add other serial console driver calls here */
  124. if (!rc) {
  125. console_ops.open = serial_open;
  126. console_ops.write = serial_write;
  127. console_ops.close = serial_close;
  128. console_ops.data = &serial_cd;
  129. if (serial_cd.getc)
  130. console_ops.edit_cmdline = serial_edit_cmdline;
  131. return 0;
  132. }
  133. err_out:
  134. return -1;
  135. }