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