early_serial_console.c 3.4 KB

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