early_serial_console.c 3.3 KB

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