serial.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "serial.h"
  2. #include "bootcon.h"
  3. #include "serial_ring_buf.h"
  4. #include "netlog.h"
  5. #include "pci.h"
  6. #include "sched.h"
  7. #include "timer.h"
  8. static int serial_dead; /* COM1 stopped answering (no UART present) */
  9. /* An absent 16550 sits on a floating bus that returns 0xFF from every
  10. * register. A real one cannot: LSR bit 7 is reserved-zero and a 16550's IIR
  11. * is 0x01/0xC1, never 0xFF. Detect it before programming the port and before
  12. * the PIT starts polling it. */
  13. static int serial_absent(unsigned int port)
  14. {
  15. return port_inb(port + 5) == 0xFF && port_inb(port + 2) == 0xFF;
  16. }
  17. void serial_init(unsigned int port)
  18. {
  19. if (port == SERIAL_COM1) {
  20. serial_dead = 0;
  21. if (serial_absent(port)) {
  22. /* Keep the boot console and netlog mirrors working (they
  23. * are fed by serial_putc before the dead check), but
  24. * never touch the floating port again. */
  25. serial_dead = 1;
  26. bootcon_puts("com1: absent\n");
  27. return;
  28. }
  29. }
  30. port_outb(port + 1, 0x00);
  31. port_outb(port + 3, 0x80);
  32. port_outb(port + 0, 0x01); /* divisor low = 1 -> 115200 baud */
  33. port_outb(port + 1, 0x00); /* divisor high = 0 */
  34. port_outb(port + 3, 0x03);
  35. port_outb(port + 2, 0xC7);
  36. port_outb(port + 4, 0x03);
  37. }
  38. void serial_putc(unsigned int port, char c)
  39. {
  40. unsigned int n;
  41. /* Mirror the debug console to the network first (netlog; a no-op until
  42. * the mirror is enabled and the NIC is up). This must happen even when
  43. * COM1 itself is unusable -- boards without a serial port are exactly
  44. * the ones that use netlog. */
  45. if (port == SERIAL_COM1) {
  46. bootcon_scan_char(c);
  47. netlog_char(c);
  48. }
  49. /* A machine without a serial port can leave LSR.THRE clear forever
  50. * (the port is decoded but the UART is absent or clocked off). Never
  51. * hang the boot on it: poll with a bound, then switch COM1 off. */
  52. if (port == SERIAL_COM1 && serial_dead)
  53. return;
  54. for (n = 0; n < 100000u; n++) {
  55. if (port_inb(port + 5) & 0x20)
  56. break;
  57. }
  58. if (n == 100000u) {
  59. if (port == SERIAL_COM1)
  60. serial_dead = 1;
  61. return;
  62. }
  63. port_outb(port, (unsigned char)c);
  64. }
  65. void serial_puts(unsigned int port, const char *s)
  66. {
  67. while (*s) {
  68. if (*s == '\n')
  69. serial_putc(port, '\r');
  70. serial_putc(port, *s++);
  71. }
  72. }
  73. void serial_puthex(unsigned int port, unsigned int n)
  74. {
  75. static const char hex[] = "0123456789ABCDEF";
  76. int i;
  77. serial_puts(port, "0x");
  78. for (i = 28; i >= 0; i -= 4)
  79. serial_putc(port, hex[(n >> i) & 0xF]);
  80. }
  81. void serial_putdec(unsigned int port, unsigned int n)
  82. {
  83. char buf[12];
  84. char *p = buf + sizeof(buf) - 1;
  85. *p = '\0';
  86. if (n == 0) {
  87. serial_putc(port, '0');
  88. return;
  89. }
  90. while (n) {
  91. *--p = (char)('0' + (n % 10));
  92. n /= 10;
  93. }
  94. serial_puts(port, p);
  95. }
  96. int serial_has_data(unsigned int port)
  97. {
  98. (void)port;
  99. return serial_ring_count() > 0;
  100. }
  101. void serial_poll(void)
  102. {
  103. unsigned char c, lsr;
  104. unsigned int guard = 0;
  105. if (serial_dead)
  106. return;
  107. /* Read the status before the data. On a serial-less machine (the
  108. * floating bus returns 0xFF) the old drain loop did 256 LSR + 256 data
  109. * reads on every PIT tick. Over eSPI those cost more than the 1 ms
  110. * tick itself and starved the boot after [process]; mark the port dead
  111. * instead. A real UART is only polled while DR is set. */
  112. lsr = (unsigned char)port_inb(SERIAL_COM1 + 5);
  113. if (lsr == 0xFF) {
  114. serial_dead = 1;
  115. return;
  116. }
  117. while ((lsr & 0x01) != 0 && guard++ < 256) {
  118. c = (unsigned char)port_inb(SERIAL_COM1);
  119. serial_ring_push(c);
  120. lsr = (unsigned char)port_inb(SERIAL_COM1 + 5);
  121. }
  122. }
  123. int serial_ring_getc(void)
  124. {
  125. unsigned char c;
  126. if (serial_ring_pop(&c))
  127. return (int)c;
  128. return 0;
  129. }
  130. int serial_ring_wait(unsigned int timeout_ms, unsigned char *out)
  131. {
  132. unsigned int start = get_ticks_ms();
  133. int got = 0;
  134. /* A kexec/payload receive can take minutes over 115200 baud. Do not
  135. * keep the big kernel lock for that: the WM, terminals and the other
  136. * CPU's syscalls must keep running while we wait for bytes. */
  137. bkl_pause();
  138. while (get_ticks_ms() - start < timeout_ms) {
  139. if (serial_ring_pop(out)) {
  140. got = 1;
  141. break;
  142. }
  143. __asm__ volatile ("hlt");
  144. }
  145. bkl_resume();
  146. return got;
  147. }