Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

serial.c 6.5KB

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