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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <ipxe/init.h>
  31. #include <ipxe/uart.h>
  32. #include <ipxe/console.h>
  33. #include <ipxe/serial.h>
  34. #include <config/console.h>
  35. #include <config/serial.h>
  36. /* Set default console usage if applicable */
  37. #if ! ( defined ( CONSOLE_SERIAL ) && CONSOLE_EXPLICIT ( CONSOLE_SERIAL ) )
  38. #undef CONSOLE_SERIAL
  39. #define CONSOLE_SERIAL ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
  40. #endif
  41. /* UART port number */
  42. #ifdef COMCONSOLE
  43. #define CONSOLE_PORT COMCONSOLE
  44. #else
  45. #define CONSOLE_PORT 0
  46. #endif
  47. /* UART baud rate */
  48. #ifdef COMPRESERVE
  49. #define CONSOLE_BAUD 0
  50. #else
  51. #define CONSOLE_BAUD COMSPEED
  52. #endif
  53. /* UART line control register value */
  54. #ifdef COMPRESERVE
  55. #define CONSOLE_LCR 0
  56. #else
  57. #define CONSOLE_LCR UART_LCR_WPS ( COMDATA, COMPARITY, COMSTOP )
  58. #endif
  59. /** Serial console UART */
  60. struct uart serial_console;
  61. /**
  62. * Print a character to serial console
  63. *
  64. * @v character Character to be printed
  65. */
  66. static void serial_putchar ( int character ) {
  67. /* Do nothing if we have no UART */
  68. if ( ! serial_console.base )
  69. return;
  70. /* Transmit character */
  71. uart_transmit ( &serial_console, character );
  72. }
  73. /**
  74. * Get character from serial console
  75. *
  76. * @ret character Character read from console
  77. */
  78. static int serial_getchar ( void ) {
  79. uint8_t data;
  80. /* Do nothing if we have no UART */
  81. if ( ! serial_console.base )
  82. return 0;
  83. /* Wait for data to be ready */
  84. while ( ! uart_data_ready ( &serial_console ) ) {}
  85. /* Receive data */
  86. data = uart_receive ( &serial_console );
  87. /* Strip any high bit and convert DEL to backspace */
  88. data &= 0x7f;
  89. if ( data == 0x7f )
  90. data = 0x08;
  91. return data;
  92. }
  93. /**
  94. * Check for character ready to read from serial console
  95. *
  96. * @ret True Character available to read
  97. * @ret False No character available to read
  98. */
  99. static int serial_iskey ( void ) {
  100. /* Do nothing if we have no UART */
  101. if ( ! serial_console.base )
  102. return 0;
  103. /* Check UART */
  104. return uart_data_ready ( &serial_console );
  105. }
  106. /** Serial console */
  107. struct console_driver serial_console_driver __console_driver = {
  108. .putchar = serial_putchar,
  109. .getchar = serial_getchar,
  110. .iskey = serial_iskey,
  111. .usage = CONSOLE_SERIAL,
  112. };
  113. /** Initialise serial console */
  114. static void serial_init ( void ) {
  115. int rc;
  116. /* Do nothing if we have no default port */
  117. if ( ! CONSOLE_PORT )
  118. return;
  119. /* Select UART */
  120. if ( ( rc = uart_select ( &serial_console, CONSOLE_PORT ) ) != 0 ) {
  121. DBG ( "Could not select UART %d: %s\n",
  122. CONSOLE_PORT, strerror ( rc ) );
  123. return;
  124. }
  125. /* Initialise UART */
  126. if ( ( rc = uart_init ( &serial_console, CONSOLE_BAUD,
  127. CONSOLE_LCR ) ) != 0 ) {
  128. DBG ( "Could not initialise UART %d baud %d LCR %#02x: %s\n",
  129. CONSOLE_PORT, CONSOLE_BAUD, CONSOLE_LCR, strerror ( rc ));
  130. return;
  131. }
  132. }
  133. /**
  134. * Shut down serial console
  135. *
  136. * @v flags Shutdown flags
  137. */
  138. static void serial_shutdown ( int flags __unused ) {
  139. /* Do nothing if we have no UART */
  140. if ( ! serial_console.base )
  141. return;
  142. /* Flush any pending output */
  143. uart_flush ( &serial_console );
  144. /* Leave console enabled; it's still usable */
  145. }
  146. /** Serial console initialisation function */
  147. struct init_fn serial_console_init_fn __init_fn ( INIT_CONSOLE ) = {
  148. .initialise = serial_init,
  149. };
  150. /** Serial console startup function */
  151. struct startup_fn serial_startup_fn __startup_fn ( STARTUP_EARLY ) = {
  152. .shutdown = serial_shutdown,
  153. };