early_serial_console.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if (!strncmp(arg, "ttyS", 4)) {
  48. static const int bases[] = { 0x3f8, 0x2f8 };
  49. int idx = 0;
  50. if (!strncmp(arg + pos, "ttyS", 4))
  51. pos += 4;
  52. if (arg[pos++] == '1')
  53. idx = 1;
  54. port = bases[idx];
  55. }
  56. if (arg[pos] == ',')
  57. pos++;
  58. baud = simple_strtoull(arg + pos, &e, 0);
  59. if (baud == 0 || arg + pos == e)
  60. baud = DEFAULT_BAUD;
  61. }
  62. if (port)
  63. early_serial_init(port, baud);
  64. }
  65. #define BASE_BAUD (1843200/16)
  66. static unsigned int probe_baud(int port)
  67. {
  68. unsigned char lcr, dll, dlh;
  69. unsigned int quot;
  70. lcr = inb(port + LCR);
  71. outb(lcr | DLAB, port + LCR);
  72. dll = inb(port + DLL);
  73. dlh = inb(port + DLH);
  74. outb(lcr, port + LCR);
  75. quot = (dlh << 8) | dll;
  76. return BASE_BAUD / quot;
  77. }
  78. static void parse_console_uart8250(void)
  79. {
  80. char optstr[64], *options;
  81. int baud = DEFAULT_BAUD;
  82. int port = 0;
  83. /*
  84. * console=uart8250,io,0x3f8,115200n8
  85. * need to make sure it is last one console !
  86. */
  87. if (cmdline_find_option("console", optstr, sizeof optstr) <= 0)
  88. return;
  89. options = optstr;
  90. if (!strncmp(options, "uart8250,io,", 12))
  91. port = simple_strtoull(options + 12, &options, 0);
  92. else if (!strncmp(options, "uart,io,", 8))
  93. port = simple_strtoull(options + 8, &options, 0);
  94. else
  95. return;
  96. if (options && (options[0] == ','))
  97. baud = simple_strtoull(options + 1, &options, 0);
  98. else
  99. baud = probe_baud(port);
  100. if (port)
  101. early_serial_init(port, baud);
  102. }
  103. void console_init(void)
  104. {
  105. parse_earlyprintk();
  106. if (!early_serial_base)
  107. parse_console_uart8250();
  108. }