early_serial_console.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Serial port routines for use during early boot reporting. This code is
  3. * included from both the compressed kernel and the regular kernel.
  4. */
  5. #include "boot.h"
  6. #define DEFAULT_SERIAL_PORT 0x3f8 /* ttyS0 */
  7. #define DLAB 0x80
  8. #define TXR 0 /* Transmit register (WRITE) */
  9. #define RXR 0 /* Receive register (READ) */
  10. #define IER 1 /* Interrupt Enable */
  11. #define IIR 2 /* Interrupt ID */
  12. #define FCR 2 /* FIFO control */
  13. #define LCR 3 /* Line control */
  14. #define MCR 4 /* Modem control */
  15. #define LSR 5 /* Line Status */
  16. #define MSR 6 /* Modem Status */
  17. #define DLL 0 /* Divisor Latch Low */
  18. #define DLH 1 /* Divisor latch High */
  19. #define DEFAULT_BAUD 9600
  20. static void early_serial_init(int port, int baud)
  21. {
  22. unsigned char c;
  23. unsigned divisor;
  24. outb(0x3, port + LCR); /* 8n1 */
  25. outb(0, port + IER); /* no interrupt */
  26. outb(0, port + FCR); /* no fifo */
  27. outb(0x3, port + MCR); /* DTR + RTS */
  28. divisor = 115200 / baud;
  29. c = inb(port + LCR);
  30. outb(c | DLAB, port + LCR);
  31. outb(divisor & 0xff, port + DLL);
  32. outb((divisor >> 8) & 0xff, port + DLH);
  33. outb(c & ~DLAB, port + LCR);
  34. early_serial_base = port;
  35. }
  36. static void parse_earlyprintk(void)
  37. {
  38. int baud = DEFAULT_BAUD;
  39. char arg[32];
  40. int pos = 0;
  41. int port = 0;
  42. if (cmdline_find_option("earlyprintk", arg, sizeof arg) > 0) {
  43. char *e;
  44. if (!strncmp(arg, "serial", 6)) {
  45. port = DEFAULT_SERIAL_PORT;
  46. pos += 6;
  47. }
  48. if (arg[pos] == ',')
  49. pos++;
  50. /*
  51. * make sure we have
  52. * "serial,0x3f8,115200"
  53. * "serial,ttyS0,115200"
  54. * "ttyS0,115200"
  55. */
  56. if (pos == 7 && !strncmp(arg + pos, "0x", 2)) {
  57. port = simple_strtoull(arg + pos, &e, 16);
  58. if (port == 0 || arg + pos == e)
  59. port = DEFAULT_SERIAL_PORT;
  60. else
  61. pos = e - arg;
  62. } else if (!strncmp(arg + pos, "ttyS", 4)) {
  63. static const int bases[] = { 0x3f8, 0x2f8 };
  64. int idx = 0;
  65. /* += strlen("ttyS"); */
  66. pos += 4;
  67. if (arg[pos++] == '1')
  68. idx = 1;
  69. port = bases[idx];
  70. }
  71. if (arg[pos] == ',')
  72. pos++;
  73. baud = simple_strtoull(arg + pos, &e, 0);
  74. if (baud == 0 || arg + pos == e)
  75. baud = DEFAULT_BAUD;
  76. }
  77. if (port)
  78. early_serial_init(port, baud);
  79. }
  80. #define BASE_BAUD (1843200/16)
  81. static unsigned int probe_baud(int port)
  82. {
  83. unsigned char lcr, dll, dlh;
  84. unsigned int quot;
  85. lcr = inb(port + LCR);
  86. outb(lcr | DLAB, port + LCR);
  87. dll = inb(port + DLL);
  88. dlh = inb(port + DLH);
  89. outb(lcr, port + LCR);
  90. quot = (dlh << 8) | dll;
  91. return BASE_BAUD / quot;
  92. }
  93. static void parse_console_uart8250(void)
  94. {
  95. char optstr[64], *options;
  96. int baud = DEFAULT_BAUD;
  97. int port = 0;
  98. /*
  99. * console=uart8250,io,0x3f8,115200n8
  100. * need to make sure it is last one console !
  101. */
  102. if (cmdline_find_option("console", optstr, sizeof optstr) <= 0)
  103. return;
  104. options = optstr;
  105. if (!strncmp(options, "uart8250,io,", 12))
  106. port = simple_strtoull(options + 12, &options, 0);
  107. else if (!strncmp(options, "uart,io,", 8))
  108. port = simple_strtoull(options + 8, &options, 0);
  109. else
  110. return;
  111. if (options && (options[0] == ','))
  112. baud = simple_strtoull(options + 1, &options, 0);
  113. else
  114. baud = probe_baud(port);
  115. if (port)
  116. early_serial_init(port, baud);
  117. }
  118. void console_init(void)
  119. {
  120. parse_earlyprintk();
  121. if (!early_serial_base)
  122. parse_console_uart8250();
  123. }