serial.c 2.9 KB

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