serial.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. void serial_init(unsigned int port)
  10. {
  11. if (port == SERIAL_COM1)
  12. serial_dead = 0;
  13. port_outb(port + 1, 0x00);
  14. port_outb(port + 3, 0x80);
  15. port_outb(port + 0, 0x01); /* divisor low = 1 -> 115200 baud */
  16. port_outb(port + 1, 0x00); /* divisor high = 0 */
  17. port_outb(port + 3, 0x03);
  18. port_outb(port + 2, 0xC7);
  19. port_outb(port + 4, 0x03);
  20. }
  21. void serial_putc(unsigned int port, char c)
  22. {
  23. unsigned int n;
  24. /* Mirror the debug console to the network first (netlog; a no-op until
  25. * the mirror is enabled and the NIC is up). This must happen even when
  26. * COM1 itself is unusable -- boards without a serial port are exactly
  27. * the ones that use netlog. */
  28. if (port == SERIAL_COM1) {
  29. bootcon_scan_char(c);
  30. netlog_char(c);
  31. }
  32. /* A machine without a serial port can leave LSR.THRE clear forever
  33. * (the port is decoded but the UART is absent or clocked off). Never
  34. * hang the boot on it: poll with a bound, then switch COM1 off. */
  35. if (port == SERIAL_COM1 && serial_dead)
  36. return;
  37. for (n = 0; n < 100000u; n++) {
  38. if (port_inb(port + 5) & 0x20)
  39. break;
  40. }
  41. if (n == 100000u) {
  42. if (port == SERIAL_COM1)
  43. serial_dead = 1;
  44. return;
  45. }
  46. port_outb(port, (unsigned char)c);
  47. }
  48. void serial_puts(unsigned int port, const char *s)
  49. {
  50. while (*s) {
  51. if (*s == '\n')
  52. serial_putc(port, '\r');
  53. serial_putc(port, *s++);
  54. }
  55. }
  56. void serial_puthex(unsigned int port, unsigned int n)
  57. {
  58. static const char hex[] = "0123456789ABCDEF";
  59. int i;
  60. serial_puts(port, "0x");
  61. for (i = 28; i >= 0; i -= 4)
  62. serial_putc(port, hex[(n >> i) & 0xF]);
  63. }
  64. void serial_putdec(unsigned int port, unsigned int n)
  65. {
  66. char buf[12];
  67. char *p = buf + sizeof(buf) - 1;
  68. *p = '\0';
  69. if (n == 0) {
  70. serial_putc(port, '0');
  71. return;
  72. }
  73. while (n) {
  74. *--p = (char)('0' + (n % 10));
  75. n /= 10;
  76. }
  77. serial_puts(port, p);
  78. }
  79. int serial_has_data(unsigned int port)
  80. {
  81. (void)port;
  82. return serial_ring_count() > 0;
  83. }
  84. void serial_poll(void)
  85. {
  86. unsigned char c;
  87. unsigned int guard = 0;
  88. /* Bounded drain: a floating LSR (0xFF) has DR set forever. */
  89. while (((port_inb(SERIAL_COM1 + 5) & 0x01) != 0) && guard++ < 256) {
  90. c = (unsigned char)port_inb(SERIAL_COM1);
  91. serial_ring_push(c);
  92. }
  93. }
  94. int serial_ring_getc(void)
  95. {
  96. unsigned char c;
  97. if (serial_ring_pop(&c))
  98. return (int)c;
  99. return 0;
  100. }
  101. int serial_ring_wait(unsigned int timeout_ms, unsigned char *out)
  102. {
  103. unsigned int start = get_ticks_ms();
  104. int got = 0;
  105. /* A kexec/payload receive can take minutes over 115200 baud. Do not
  106. * keep the big kernel lock for that: the WM, terminals and the other
  107. * CPU's syscalls must keep running while we wait for bytes. */
  108. bkl_pause();
  109. while (get_ticks_ms() - start < timeout_ms) {
  110. if (serial_ring_pop(out)) {
  111. got = 1;
  112. break;
  113. }
  114. __asm__ volatile ("hlt");
  115. }
  116. bkl_resume();
  117. return got;
  118. }