early_serial_console.c 3.5 KB

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