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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /* Set default values if none specified */
  19. #ifndef COMCONSOLE
  20. #define COMCONSOLE ( 0x3f8 )
  21. #endif
  22. #ifndef CONSPEED
  23. #define CONSPEED ( 9600 )
  24. #endif
  25. #undef UART_BASE
  26. #define UART_BASE COMCONSOLE
  27. #undef UART_BAUD
  28. #define UART_BAUD CONSPEED
  29. #if ((115200%UART_BAUD) != 0)
  30. #error Bad ttys0 baud rate
  31. #endif
  32. #define COMBRD (115200/UART_BAUD)
  33. /* Line Control Settings */
  34. #ifndef COMPARM
  35. /* Set 8bit, 1 stop bit, no parity */
  36. #define COMPARM 0x03
  37. #endif
  38. #define UART_LCS COMPARM
  39. /* Data */
  40. #define UART_RBR 0x00
  41. #define UART_TBR 0x00
  42. /* Control */
  43. #define UART_IER 0x01
  44. #define UART_IIR 0x02
  45. #define UART_FCR 0x02
  46. #define UART_LCR 0x03
  47. #define UART_MCR 0x04
  48. #define UART_DLL 0x00
  49. #define UART_DLM 0x01
  50. /* Status */
  51. #define UART_LSR 0x05
  52. #define UART_LSR_TEMPT 0x40 /* Transmitter empty */
  53. #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
  54. #define UART_LSR_BI 0x10 /* Break interrupt indicator */
  55. #define UART_LSR_FE 0x08 /* Frame error indicator */
  56. #define UART_LSR_PE 0x04 /* Parity error indicator */
  57. #define UART_LSR_OE 0x02 /* Overrun error indicator */
  58. #define UART_LSR_DR 0x01 /* Receiver data ready */
  59. #define UART_MSR 0x06
  60. #define UART_SCR 0x07
  61. #if defined(UART_MEM)
  62. #define uart_readb(addr) readb((addr))
  63. #define uart_writeb(val,addr) writeb((val),(addr))
  64. #else
  65. #define uart_readb(addr) inb((addr))
  66. #define uart_writeb(val,addr) outb((val),(addr))
  67. #endif
  68. static struct console_driver serial_console;
  69. /*
  70. * void serial_putc(int ch);
  71. * Write character `ch' to port UART_BASE.
  72. */
  73. static void serial_putc ( int ch ) {
  74. int i;
  75. int status;
  76. i = 1000; /* timeout */
  77. while(--i > 0) {
  78. status = uart_readb(UART_BASE + UART_LSR);
  79. if (status & UART_LSR_THRE) {
  80. /* TX buffer emtpy */
  81. uart_writeb(ch, UART_BASE + UART_TBR);
  82. break;
  83. }
  84. mdelay(2);
  85. }
  86. }
  87. /*
  88. * int serial_getc(void);
  89. * Read a character from port UART_BASE.
  90. */
  91. static int serial_getc ( void ) {
  92. int status;
  93. int ch;
  94. do {
  95. status = uart_readb(UART_BASE + UART_LSR);
  96. } while((status & 1) == 0);
  97. ch = uart_readb(UART_BASE + UART_RBR); /* fetch (first) character */
  98. ch &= 0x7f; /* remove any parity bits we get */
  99. if (ch == 0x7f) { /* Make DEL... look like BS */
  100. ch = 0x08;
  101. }
  102. return ch;
  103. }
  104. /*
  105. * int serial_ischar(void);
  106. * If there is a character in the input buffer of port UART_BASE,
  107. * return nonzero; otherwise return 0.
  108. */
  109. static int serial_ischar ( void ) {
  110. int status;
  111. status = uart_readb(UART_BASE + UART_LSR); /* line status reg; */
  112. return status & 1; /* rx char available */
  113. }
  114. /*
  115. * int serial_init(void);
  116. * Initialize port UART_BASE to speed CONSPEED, line settings 8N1.
  117. */
  118. static void serial_init ( void ) {
  119. int status;
  120. int divisor, lcs;
  121. divisor = COMBRD;
  122. lcs = UART_LCS;
  123. #ifdef COMPRESERVE
  124. lcs = uart_readb(UART_BASE + UART_LCR) & 0x7f;
  125. uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
  126. divisor = (uart_readb(UART_BASE + UART_DLM) << 8) | uart_readb(UART_BASE + UART_DLL);
  127. uart_writeb(lcs, UART_BASE + UART_LCR);
  128. #endif
  129. /* Set Baud Rate Divisor to CONSPEED, and test to see if the
  130. * serial port appears to be present.
  131. */
  132. uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
  133. uart_writeb(0xaa, UART_BASE + UART_DLL);
  134. if (uart_readb(UART_BASE + UART_DLL) != 0xaa)
  135. goto out;
  136. uart_writeb(0x55, UART_BASE + UART_DLL);
  137. if (uart_readb(UART_BASE + UART_DLL) != 0x55)
  138. goto out;
  139. uart_writeb(divisor & 0xff, UART_BASE + UART_DLL);
  140. if (uart_readb(UART_BASE + UART_DLL) != (divisor & 0xff))
  141. goto out;
  142. uart_writeb(0xaa, UART_BASE + UART_DLM);
  143. if (uart_readb(UART_BASE + UART_DLM) != 0xaa)
  144. goto out;
  145. uart_writeb(0x55, UART_BASE + UART_DLM);
  146. if (uart_readb(UART_BASE + UART_DLM) != 0x55)
  147. goto out;
  148. uart_writeb((divisor >> 8) & 0xff, UART_BASE + UART_DLM);
  149. if (uart_readb(UART_BASE + UART_DLM) != ((divisor >> 8) & 0xff))
  150. goto out;
  151. uart_writeb(lcs, UART_BASE + UART_LCR);
  152. /* disable interrupts */
  153. uart_writeb(0x0, UART_BASE + UART_IER);
  154. /* disable fifo's */
  155. uart_writeb(0x00, UART_BASE + UART_FCR);
  156. /* Set clear to send, so flow control works... */
  157. uart_writeb((1<<1), UART_BASE + UART_MCR);
  158. /* Flush the input buffer. */
  159. do {
  160. /* rx buffer reg
  161. * throw away (unconditionally the first time)
  162. */
  163. uart_readb(UART_BASE + UART_RBR);
  164. /* line status reg */
  165. status = uart_readb(UART_BASE + UART_LSR);
  166. } while(status & UART_LSR_DR);
  167. serial_console.disabled = 0;
  168. out:
  169. return;
  170. }
  171. /*
  172. * void serial_fini(void);
  173. * Cleanup our use of the serial port, in particular flush the
  174. * output buffer so we don't accidentially loose characters.
  175. */
  176. static void serial_fini ( void ) {
  177. int i, status;
  178. if (serial_console.disabled) {
  179. /* no serial interface */
  180. return;
  181. }
  182. /* Flush the output buffer to avoid dropping characters,
  183. * if we are reinitializing the serial port.
  184. */
  185. i = 10000; /* timeout */
  186. do {
  187. status = uart_readb(UART_BASE + UART_LSR);
  188. } while((--i > 0) && !(status & UART_LSR_TEMPT));
  189. /* Don't mark it as disabled; it's still usable */
  190. }
  191. static struct console_driver serial_console __console_driver = {
  192. .putchar = serial_putc,
  193. .getchar = serial_getc,
  194. .iskey = serial_ischar,
  195. .disabled = 1,
  196. };
  197. INIT_FN ( INIT_CONSOLE, serial_init, NULL, serial_fini );