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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * The serial port interface routines implement a simple polled i/o
  3. * interface to a standard serial port. Due to the space restrictions
  4. * for the boot blocks, no BIOS support is used (since BIOS requires
  5. * expensive real/protected mode switches), instead the rudimentary
  6. * BIOS support is duplicated here.
  7. *
  8. * The base address and speed for the i/o port are passed from the
  9. * Makefile in the COMCONSOLE and CONSPEED preprocessor macros. The
  10. * line control parameters are currently hard-coded to 8 bits, no
  11. * parity, 1 stop bit (8N1). This can be changed in init_serial().
  12. */
  13. #include "stddef.h"
  14. #include "console.h"
  15. #include <gpxe/init.h>
  16. #include "io.h"
  17. #include "timer.h"
  18. #include "config/serial.h"
  19. /* Set default values if none specified */
  20. #ifndef COMCONSOLE
  21. #define COMCONSOLE 0x3f8
  22. #endif
  23. #ifndef COMSPEED
  24. #define COMSPEED 9600
  25. #endif
  26. #ifndef COMDATA
  27. #define COMDATA 8
  28. #endif
  29. #ifndef COMPARITY
  30. #define COMPARITY 0
  31. #endif
  32. #ifndef COMSTOP
  33. #define COMSTOP 1
  34. #endif
  35. #undef UART_BASE
  36. #define UART_BASE ( COMCONSOLE )
  37. #undef UART_BAUD
  38. #define UART_BAUD ( COMSPEED )
  39. #if ((115200%UART_BAUD) != 0)
  40. #error Bad ttys0 baud rate
  41. #endif
  42. #define COMBRD (115200/UART_BAUD)
  43. /* Line Control Settings */
  44. #define UART_LCS ( ( ( (COMDATA) - 5 ) << 0 ) | \
  45. ( ( (COMPARITY) ) << 3 ) | \
  46. ( ( (COMSTOP) - 1 ) << 2 ) )
  47. /* Data */
  48. #define UART_RBR 0x00
  49. #define UART_TBR 0x00
  50. /* Control */
  51. #define UART_IER 0x01
  52. #define UART_IIR 0x02
  53. #define UART_FCR 0x02
  54. #define UART_LCR 0x03
  55. #define UART_MCR 0x04
  56. #define UART_DLL 0x00
  57. #define UART_DLM 0x01
  58. /* Status */
  59. #define UART_LSR 0x05
  60. #define UART_LSR_TEMPT 0x40 /* Transmitter empty */
  61. #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
  62. #define UART_LSR_BI 0x10 /* Break interrupt indicator */
  63. #define UART_LSR_FE 0x08 /* Frame error indicator */
  64. #define UART_LSR_PE 0x04 /* Parity error indicator */
  65. #define UART_LSR_OE 0x02 /* Overrun error indicator */
  66. #define UART_LSR_DR 0x01 /* Receiver data ready */
  67. #define UART_MSR 0x06
  68. #define UART_SCR 0x07
  69. #if defined(UART_MEM)
  70. #define uart_readb(addr) readb((addr))
  71. #define uart_writeb(val,addr) writeb((val),(addr))
  72. #else
  73. #define uart_readb(addr) inb((addr))
  74. #define uart_writeb(val,addr) outb((val),(addr))
  75. #endif
  76. struct console_driver serial_console __console_driver;
  77. /*
  78. * void serial_putc(int ch);
  79. * Write character `ch' to port UART_BASE.
  80. */
  81. static void serial_putc ( int ch ) {
  82. int i;
  83. int status;
  84. i = 1000; /* timeout */
  85. while(--i > 0) {
  86. status = uart_readb(UART_BASE + UART_LSR);
  87. if (status & UART_LSR_THRE) {
  88. /* TX buffer emtpy */
  89. uart_writeb(ch, UART_BASE + UART_TBR);
  90. break;
  91. }
  92. mdelay(2);
  93. }
  94. }
  95. /*
  96. * int serial_getc(void);
  97. * Read a character from port UART_BASE.
  98. */
  99. static int serial_getc ( void ) {
  100. int status;
  101. int ch;
  102. do {
  103. status = uart_readb(UART_BASE + UART_LSR);
  104. } while((status & 1) == 0);
  105. ch = uart_readb(UART_BASE + UART_RBR); /* fetch (first) character */
  106. ch &= 0x7f; /* remove any parity bits we get */
  107. if (ch == 0x7f) { /* Make DEL... look like BS */
  108. ch = 0x08;
  109. }
  110. return ch;
  111. }
  112. /*
  113. * int serial_ischar(void);
  114. * If there is a character in the input buffer of port UART_BASE,
  115. * return nonzero; otherwise return 0.
  116. */
  117. static int serial_ischar ( void ) {
  118. int status;
  119. status = uart_readb(UART_BASE + UART_LSR); /* line status reg; */
  120. return status & 1; /* rx char available */
  121. }
  122. /*
  123. * int serial_init(void);
  124. * Initialize port UART_BASE to speed COMSPEED, line settings 8N1.
  125. */
  126. static void serial_init ( void ) {
  127. int status;
  128. int divisor, lcs;
  129. DBG ( "Serial port %#x initialising\n", UART_BASE );
  130. divisor = COMBRD;
  131. lcs = UART_LCS;
  132. #ifdef COMPRESERVE
  133. lcs = uart_readb(UART_BASE + UART_LCR) & 0x7f;
  134. uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
  135. divisor = (uart_readb(UART_BASE + UART_DLM) << 8) | uart_readb(UART_BASE + UART_DLL);
  136. uart_writeb(lcs, UART_BASE + UART_LCR);
  137. #endif
  138. /* Set Baud Rate Divisor to COMSPEED, and test to see if the
  139. * serial port appears to be present.
  140. */
  141. uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
  142. uart_writeb(0xaa, UART_BASE + UART_DLL);
  143. if (uart_readb(UART_BASE + UART_DLL) != 0xaa) {
  144. DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
  145. goto out;
  146. }
  147. uart_writeb(0x55, UART_BASE + UART_DLL);
  148. if (uart_readb(UART_BASE + UART_DLL) != 0x55) {
  149. DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
  150. goto out;
  151. }
  152. uart_writeb(divisor & 0xff, UART_BASE + UART_DLL);
  153. if (uart_readb(UART_BASE + UART_DLL) != (divisor & 0xff)) {
  154. DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
  155. goto out;
  156. }
  157. uart_writeb(0xaa, UART_BASE + UART_DLM);
  158. if (uart_readb(UART_BASE + UART_DLM) != 0xaa) {
  159. DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
  160. goto out;
  161. }
  162. uart_writeb(0x55, UART_BASE + UART_DLM);
  163. if (uart_readb(UART_BASE + UART_DLM) != 0x55) {
  164. DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
  165. goto out;
  166. }
  167. uart_writeb((divisor >> 8) & 0xff, UART_BASE + UART_DLM);
  168. if (uart_readb(UART_BASE + UART_DLM) != ((divisor >> 8) & 0xff)) {
  169. DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
  170. goto out;
  171. }
  172. uart_writeb(lcs, UART_BASE + UART_LCR);
  173. /* disable interrupts */
  174. uart_writeb(0x0, UART_BASE + UART_IER);
  175. /* disable fifo's */
  176. uart_writeb(0x00, UART_BASE + UART_FCR);
  177. /* Set clear to send, so flow control works... */
  178. uart_writeb((1<<1), UART_BASE + UART_MCR);
  179. /* Flush the input buffer. */
  180. do {
  181. /* rx buffer reg
  182. * throw away (unconditionally the first time)
  183. */
  184. (void) uart_readb(UART_BASE + UART_RBR);
  185. /* line status reg */
  186. status = uart_readb(UART_BASE + UART_LSR);
  187. } while(status & UART_LSR_DR);
  188. serial_console.disabled = 0;
  189. out:
  190. return;
  191. }
  192. /*
  193. * void serial_fini(void);
  194. * Cleanup our use of the serial port, in particular flush the
  195. * output buffer so we don't accidentially lose characters.
  196. */
  197. static void serial_fini ( void ) {
  198. int i, status;
  199. if (serial_console.disabled) {
  200. /* no serial interface */
  201. return;
  202. }
  203. /* Flush the output buffer to avoid dropping characters,
  204. * if we are reinitializing the serial port.
  205. */
  206. i = 10000; /* timeout */
  207. do {
  208. status = uart_readb(UART_BASE + UART_LSR);
  209. } while((--i > 0) && !(status & UART_LSR_TEMPT));
  210. /* Don't mark it as disabled; it's still usable */
  211. }
  212. struct console_driver serial_console __console_driver = {
  213. .putchar = serial_putc,
  214. .getchar = serial_getc,
  215. .iskey = serial_ischar,
  216. .disabled = 1,
  217. };
  218. /** Serial console startup function */
  219. struct startup_fn serial_startup_fn __startup_fn ( STARTUP_NORMAL ) = {
  220. .startup = serial_init,
  221. .shutdown = serial_fini,
  222. };
  223. /**
  224. * Serial console initialisation function
  225. *
  226. * Initialise console early on so that it is available to capture
  227. * early debug messages. It is safe to call serial_init() multiple
  228. * times.
  229. */
  230. struct init_fn serial_init_fn __init_fn ( INIT_EARLY ) = {
  231. .initialise = serial_init,
  232. };