Kevin O'Connor | 4cd522e | 2013-11-29 12:14:34 -0500 | [diff] [blame] | 1 | // Low-level serial (and serial-like) device access. |
| 2 | // |
| 3 | // Copyright (C) 2008-1013 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // |
| 5 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
| 6 | |
| 7 | #include "config.h" // CONFIG_DEBUG_SERIAL |
| 8 | #include "fw/paravirt.h" // RunningOnQEMU |
| 9 | #include "output.h" // dprintf |
| 10 | #include "serialio.h" // serial_debug_preinit |
| 11 | #include "x86.h" // outb |
| 12 | |
| 13 | |
| 14 | /**************************************************************** |
| 15 | * Serial port debug output |
| 16 | ****************************************************************/ |
| 17 | |
| 18 | #define DEBUG_TIMEOUT 100000 |
| 19 | |
| 20 | // Setup the debug serial port for output. |
| 21 | void |
| 22 | serial_debug_preinit(void) |
| 23 | { |
| 24 | if (!CONFIG_DEBUG_SERIAL) |
| 25 | return; |
| 26 | // setup for serial logging: 8N1 |
| 27 | u8 oldparam, newparam = 0x03; |
| 28 | oldparam = inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_LCR); |
| 29 | outb(newparam, CONFIG_DEBUG_SERIAL_PORT+SEROFF_LCR); |
| 30 | // Disable irqs |
| 31 | u8 oldier, newier = 0; |
| 32 | oldier = inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_IER); |
| 33 | outb(newier, CONFIG_DEBUG_SERIAL_PORT+SEROFF_IER); |
| 34 | |
| 35 | if (oldparam != newparam || oldier != newier) |
| 36 | dprintf(1, "Changing serial settings was %x/%x now %x/%x\n" |
| 37 | , oldparam, oldier, newparam, newier); |
| 38 | } |
| 39 | |
| 40 | // Write a character to the serial port. |
| 41 | static void |
| 42 | serial_debug(char c) |
| 43 | { |
| 44 | if (!CONFIG_DEBUG_SERIAL) |
| 45 | return; |
| 46 | int timeout = DEBUG_TIMEOUT; |
| 47 | while ((inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_LSR) & 0x20) != 0x20) |
| 48 | if (!timeout--) |
| 49 | // Ran out of time. |
| 50 | return; |
| 51 | outb(c, CONFIG_DEBUG_SERIAL_PORT+SEROFF_DATA); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | serial_debug_putc(char c) |
| 56 | { |
| 57 | if (c == '\n') |
| 58 | serial_debug('\r'); |
| 59 | serial_debug(c); |
| 60 | } |
| 61 | |
| 62 | // Make sure all serial port writes have been completely sent. |
| 63 | void |
| 64 | serial_debug_flush(void) |
| 65 | { |
| 66 | if (!CONFIG_DEBUG_SERIAL) |
| 67 | return; |
| 68 | int timeout = DEBUG_TIMEOUT; |
| 69 | while ((inb(CONFIG_DEBUG_SERIAL_PORT+SEROFF_LSR) & 0x60) != 0x60) |
| 70 | if (!timeout--) |
| 71 | // Ran out of time. |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /**************************************************************** |
| 77 | * QEMU debug port |
| 78 | ****************************************************************/ |
| 79 | |
| 80 | u16 DebugOutputPort VARFSEG = 0x402; |
| 81 | |
| 82 | // Write a character to the special debugging port. |
| 83 | void |
| 84 | qemu_debug_putc(char c) |
| 85 | { |
| 86 | if (CONFIG_DEBUG_IO && runningOnQEMU()) |
| 87 | // Send character to debug port. |
| 88 | outb(c, GET_GLOBAL(DebugOutputPort)); |
| 89 | } |