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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "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. static struct console_driver serial_console;
  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. divisor = COMBRD;
  130. lcs = UART_LCS;
  131. #ifdef COMPRESERVE
  132. lcs = uart_readb(UART_BASE + UART_LCR) & 0x7f;
  133. uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
  134. divisor = (uart_readb(UART_BASE + UART_DLM) << 8) | uart_readb(UART_BASE + UART_DLL);
  135. uart_writeb(lcs, UART_BASE + UART_LCR);
  136. #endif
  137. /* Set Baud Rate Divisor to COMSPEED, and test to see if the
  138. * serial port appears to be present.
  139. */
  140. uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
  141. uart_writeb(0xaa, UART_BASE + UART_DLL);
  142. if (uart_readb(UART_BASE + UART_DLL) != 0xaa)
  143. goto out;
  144. uart_writeb(0x55, UART_BASE + UART_DLL);
  145. if (uart_readb(UART_BASE + UART_DLL) != 0x55)
  146. goto out;
  147. uart_writeb(divisor & 0xff, UART_BASE + UART_DLL);
  148. if (uart_readb(UART_BASE + UART_DLL) != (divisor & 0xff))
  149. goto out;
  150. uart_writeb(0xaa, UART_BASE + UART_DLM);
  151. if (uart_readb(UART_BASE + UART_DLM) != 0xaa)
  152. goto out;
  153. uart_writeb(0x55, UART_BASE + UART_DLM);
  154. if (uart_readb(UART_BASE + UART_DLM) != 0x55)
  155. goto out;
  156. uart_writeb((divisor >> 8) & 0xff, UART_BASE + UART_DLM);
  157. if (uart_readb(UART_BASE + UART_DLM) != ((divisor >> 8) & 0xff))
  158. goto out;
  159. uart_writeb(lcs, UART_BASE + UART_LCR);
  160. /* disable interrupts */
  161. uart_writeb(0x0, UART_BASE + UART_IER);
  162. /* disable fifo's */
  163. uart_writeb(0x00, UART_BASE + UART_FCR);
  164. /* Set clear to send, so flow control works... */
  165. uart_writeb((1<<1), UART_BASE + UART_MCR);
  166. /* Flush the input buffer. */
  167. do {
  168. /* rx buffer reg
  169. * throw away (unconditionally the first time)
  170. */
  171. uart_readb(UART_BASE + UART_RBR);
  172. /* line status reg */
  173. status = uart_readb(UART_BASE + UART_LSR);
  174. } while(status & UART_LSR_DR);
  175. serial_console.disabled = 0;
  176. out:
  177. return;
  178. }
  179. /*
  180. * void serial_fini(void);
  181. * Cleanup our use of the serial port, in particular flush the
  182. * output buffer so we don't accidentially loose characters.
  183. */
  184. static void serial_fini ( void ) {
  185. int i, status;
  186. if (serial_console.disabled) {
  187. /* no serial interface */
  188. return;
  189. }
  190. /* Flush the output buffer to avoid dropping characters,
  191. * if we are reinitializing the serial port.
  192. */
  193. i = 10000; /* timeout */
  194. do {
  195. status = uart_readb(UART_BASE + UART_LSR);
  196. } while((--i > 0) && !(status & UART_LSR_TEMPT));
  197. /* Don't mark it as disabled; it's still usable */
  198. }
  199. static struct console_driver serial_console __console_driver = {
  200. .putchar = serial_putc,
  201. .getchar = serial_getc,
  202. .iskey = serial_ischar,
  203. .disabled = 1,
  204. };
  205. INIT_FN ( INIT_CONSOLE, serial_init, NULL, serial_fini );