You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serial.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Serial console
  27. *
  28. */
  29. #include <stddef.h>
  30. #include <string.h>
  31. #include <ipxe/init.h>
  32. #include <ipxe/uart.h>
  33. #include <ipxe/console.h>
  34. #include <ipxe/serial.h>
  35. #include <config/console.h>
  36. #include <config/serial.h>
  37. /* Set default console usage if applicable */
  38. #if ! ( defined ( CONSOLE_SERIAL ) && CONSOLE_EXPLICIT ( CONSOLE_SERIAL ) )
  39. #undef CONSOLE_SERIAL
  40. #define CONSOLE_SERIAL ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
  41. #endif
  42. /* UART port number */
  43. #ifdef COMCONSOLE
  44. #define CONSOLE_PORT COMCONSOLE
  45. #else
  46. #define CONSOLE_PORT 0
  47. #endif
  48. /* UART baud rate */
  49. #ifdef COMPRESERVE
  50. #define CONSOLE_BAUD 0
  51. #else
  52. #define CONSOLE_BAUD COMSPEED
  53. #endif
  54. /* UART line control register value */
  55. #ifdef COMPRESERVE
  56. #define CONSOLE_LCR 0
  57. #else
  58. #define CONSOLE_LCR UART_LCR_WPS ( COMDATA, COMPARITY, COMSTOP )
  59. #endif
  60. /** Serial console UART */
  61. struct uart serial_console;
  62. /**
  63. * Print a character to serial console
  64. *
  65. * @v character Character to be printed
  66. */
  67. static void serial_putchar ( int character ) {
  68. /* Do nothing if we have no UART */
  69. if ( ! serial_console.base )
  70. return;
  71. /* Transmit character */
  72. uart_transmit ( &serial_console, character );
  73. }
  74. /**
  75. * Get character from serial console
  76. *
  77. * @ret character Character read from console
  78. */
  79. static int serial_getchar ( void ) {
  80. uint8_t data;
  81. /* Do nothing if we have no UART */
  82. if ( ! serial_console.base )
  83. return 0;
  84. /* Wait for data to be ready */
  85. while ( ! uart_data_ready ( &serial_console ) ) {}
  86. /* Receive data */
  87. data = uart_receive ( &serial_console );
  88. /* Strip any high bit and convert DEL to backspace */
  89. data &= 0x7f;
  90. if ( data == 0x7f )
  91. data = 0x08;
  92. return data;
  93. }
  94. /**
  95. * Check for character ready to read from serial console
  96. *
  97. * @ret True Character available to read
  98. * @ret False No character available to read
  99. */
  100. static int serial_iskey ( void ) {
  101. /* Do nothing if we have no UART */
  102. if ( ! serial_console.base )
  103. return 0;
  104. /* Check UART */
  105. return uart_data_ready ( &serial_console );
  106. }
  107. /** Serial console */
  108. struct console_driver serial_console_driver __console_driver = {
  109. .putchar = serial_putchar,
  110. .getchar = serial_getchar,
  111. .iskey = serial_iskey,
  112. .usage = CONSOLE_SERIAL,
  113. };
  114. /** Initialise serial console */
  115. static void serial_init ( void ) {
  116. int rc;
  117. /* Do nothing if we have no default port */
  118. if ( ! CONSOLE_PORT )
  119. return;
  120. /* Select UART */
  121. if ( ( rc = uart_select ( &serial_console, CONSOLE_PORT ) ) != 0 ) {
  122. DBG ( "Could not select UART %d: %s\n",
  123. CONSOLE_PORT, strerror ( rc ) );
  124. return;
  125. }
  126. /* Initialise UART */
  127. if ( ( rc = uart_init ( &serial_console, CONSOLE_BAUD,
  128. CONSOLE_LCR ) ) != 0 ) {
  129. DBG ( "Could not initialise UART %d baud %d LCR %#02x: %s\n",
  130. CONSOLE_PORT, CONSOLE_BAUD, CONSOLE_LCR, strerror ( rc ));
  131. return;
  132. }
  133. }
  134. /**
  135. * Shut down serial console
  136. *
  137. * @v flags Shutdown flags
  138. */
  139. static void serial_shutdown ( int flags __unused ) {
  140. /* Do nothing if we have no UART */
  141. if ( ! serial_console.base )
  142. return;
  143. /* Flush any pending output */
  144. uart_flush ( &serial_console );
  145. /* Leave console enabled; it's still usable */
  146. }
  147. /** Serial console initialisation function */
  148. struct init_fn serial_console_init_fn __init_fn ( INIT_CONSOLE ) = {
  149. .initialise = serial_init,
  150. };
  151. /** Serial console startup function */
  152. struct startup_fn serial_startup_fn __startup_fn ( STARTUP_EARLY ) = {
  153. .name = "serial",
  154. .shutdown = serial_shutdown,
  155. };