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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <gpxe/init.h>
  16. #include <gpxe/io.h>
  17. #include <unistd.h>
  18. #include <gpxe/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. /*
  78. * void serial_putc(int ch);
  79. * Write character `ch' to port UART_BASE.
  80. */
  81. 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. 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. 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. out:
  189. return;
  190. }
  191. /*
  192. * void serial_fini(void);
  193. * Cleanup our use of the serial port, in particular flush the
  194. * output buffer so we don't accidentially lose characters.
  195. */
  196. static void serial_fini ( int flags __unused ) {
  197. int i, status;
  198. /* Flush the output buffer to avoid dropping characters,
  199. * if we are reinitializing the serial port.
  200. */
  201. i = 10000; /* timeout */
  202. do {
  203. status = uart_readb(UART_BASE + UART_LSR);
  204. } while((--i > 0) && !(status & UART_LSR_TEMPT));
  205. /* Don't mark it as disabled; it's still usable */
  206. }
  207. /**
  208. * Serial driver initialisation function
  209. *
  210. * Initialise serial port early on so that it is available to capture
  211. * early debug messages.
  212. */
  213. struct init_fn serial_init_fn __init_fn ( INIT_SERIAL ) = {
  214. .initialise = serial_init,
  215. };
  216. /** Serial driver startup function */
  217. struct startup_fn serial_startup_fn __startup_fn ( STARTUP_EARLY ) = {
  218. .shutdown = serial_fini,
  219. };