Thomas Gleixner | 97873a3 | 2019-06-04 10:11:30 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 2 | /* -*- linux-c -*- ------------------------------------------------------- * |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * Copyright 2007 rPath, Inc. - All Rights Reserved |
H. Peter Anvin | df7699c5 | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 6 | * Copyright 2009 Intel Corporation; author H. Peter Anvin |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 7 | * |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 8 | * ----------------------------------------------------------------------- */ |
| 9 | |
| 10 | /* |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 11 | * Very simple screen and serial I/O |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include "boot.h" |
| 15 | |
Yinghai Lu | f4ed287 | 2010-08-02 02:17:31 -0700 | [diff] [blame] | 16 | int early_serial_base; |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 17 | |
| 18 | #define XMTRDY 0x20 |
| 19 | |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 20 | #define TXR 0 /* Transmit register (WRITE) */ |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 21 | #define LSR 5 /* Line Status */ |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 22 | |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 23 | /* |
| 24 | * These functions are in .inittext so they can be used to signal |
| 25 | * error during initialization. |
| 26 | */ |
| 27 | |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 28 | static void __attribute__((section(".inittext"))) serial_putchar(int ch) |
| 29 | { |
| 30 | unsigned timeout = 0xffff; |
| 31 | |
| 32 | while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout) |
| 33 | cpu_relax(); |
| 34 | |
| 35 | outb(ch, early_serial_base + TXR); |
| 36 | } |
| 37 | |
| 38 | static void __attribute__((section(".inittext"))) bios_putchar(int ch) |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 39 | { |
H. Peter Anvin | df7699c5 | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 40 | struct biosregs ireg; |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 41 | |
H. Peter Anvin | df7699c5 | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 42 | initregs(&ireg); |
| 43 | ireg.bx = 0x0007; |
| 44 | ireg.cx = 0x0001; |
| 45 | ireg.ah = 0x0e; |
| 46 | ireg.al = ch; |
| 47 | intcall(0x10, &ireg, NULL); |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 50 | void __attribute__((section(".inittext"))) putchar(int ch) |
| 51 | { |
| 52 | if (ch == '\n') |
| 53 | putchar('\r'); /* \n -> \r\n */ |
| 54 | |
| 55 | bios_putchar(ch); |
| 56 | |
| 57 | if (early_serial_base != 0) |
| 58 | serial_putchar(ch); |
| 59 | } |
| 60 | |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 61 | void __attribute__((section(".inittext"))) puts(const char *str) |
| 62 | { |
H. Peter Anvin | df7699c5 | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 63 | while (*str) |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 64 | putchar(*str++); |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Read the CMOS clock through the BIOS, and return the |
| 69 | * seconds in BCD. |
| 70 | */ |
| 71 | |
| 72 | static u8 gettime(void) |
| 73 | { |
H. Peter Anvin | df7699c5 | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 74 | struct biosregs ireg, oreg; |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 75 | |
H. Peter Anvin | df7699c5 | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 76 | initregs(&ireg); |
| 77 | ireg.ah = 0x02; |
| 78 | intcall(0x1a, &ireg, &oreg); |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 79 | |
H. Peter Anvin | df7699c5 | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 80 | return oreg.dh; |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Read from the keyboard |
| 85 | */ |
| 86 | int getchar(void) |
| 87 | { |
H. Peter Anvin | df7699c5 | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 88 | struct biosregs ireg, oreg; |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 89 | |
H. Peter Anvin | df7699c5 | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 90 | initregs(&ireg); |
| 91 | /* ireg.ah = 0x00; */ |
| 92 | intcall(0x16, &ireg, &oreg); |
| 93 | |
| 94 | return oreg.al; |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static int kbd_pending(void) |
| 98 | { |
H. Peter Anvin | df7699c5 | 2009-04-01 18:13:46 -0700 | [diff] [blame] | 99 | struct biosregs ireg, oreg; |
| 100 | |
| 101 | initregs(&ireg); |
| 102 | ireg.ah = 0x01; |
| 103 | intcall(0x16, &ireg, &oreg); |
| 104 | |
| 105 | return !(oreg.eflags & X86_EFLAGS_ZF); |
H. Peter Anvin | 1543610 | 2007-07-11 12:18:45 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void kbd_flush(void) |
| 109 | { |
| 110 | for (;;) { |
| 111 | if (!kbd_pending()) |
| 112 | break; |
| 113 | getchar(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | int getchar_timeout(void) |
| 118 | { |
| 119 | int cnt = 30; |
| 120 | int t0, t1; |
| 121 | |
| 122 | t0 = gettime(); |
| 123 | |
| 124 | while (cnt) { |
| 125 | if (kbd_pending()) |
| 126 | return getchar(); |
| 127 | |
| 128 | t1 = gettime(); |
| 129 | if (t0 != t1) { |
| 130 | cnt--; |
| 131 | t0 = t1; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return 0; /* Timeout! */ |
| 136 | } |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 137 | |