Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 1 | void outb(unsigned char value, unsigned short port) |
| 2 | { |
| 3 | __builtin_outb(value, port); |
| 4 | } |
| 5 | |
| 6 | unsigned char inb(unsigned short port) |
| 7 | { |
| 8 | return __builtin_inb(port); |
| 9 | } |
| 10 | |
| 11 | /* Base Address */ |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 12 | #ifndef CONFIG_TTYS0_BASE |
| 13 | #define CONFIG_TTYS0_BASE 0x3f8 |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 14 | #endif |
| 15 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 16 | #ifndef CONFIG_TTYS0_BAUD |
| 17 | #define CONFIG_TTYS0_BAUD 115200 |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 18 | #endif |
| 19 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 20 | #if ((115200%CONFIG_TTYS0_BAUD) != 0) |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 21 | #error Bad ttys0 baud rate |
| 22 | #endif |
| 23 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 24 | #if CONFIG_TTYS0_BAUD == 115200 |
| 25 | #define CONFIG_TTYS0_DIV (1) |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 26 | #else |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 27 | #define CONFIG_TTYS0_DIV (115200/CONFIG_TTYS0_BAUD) |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
| 30 | /* Line Control Settings */ |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 31 | #ifndef CONFIG_TTYS0_LCS |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 32 | /* Set 8bit, 1 stop bit, no parity */ |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 33 | #define CONFIG_TTYS0_LCS 0x3 |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 36 | #define UART_LCS CONFIG_TTYS0_LCS |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 37 | |
| 38 | /* Data */ |
| 39 | #define UART_RBR 0x00 |
| 40 | #define UART_TBR 0x00 |
| 41 | |
| 42 | /* Control */ |
| 43 | #define UART_IER 0x01 |
| 44 | #define UART_IIR 0x02 |
| 45 | #define UART_FCR 0x02 |
| 46 | #define UART_LCR 0x03 |
| 47 | #define UART_MCR 0x04 |
| 48 | #define UART_DLL 0x00 |
| 49 | #define UART_DLM 0x01 |
| 50 | |
| 51 | /* Status */ |
| 52 | #define UART_LSR 0x05 |
| 53 | #define UART_MSR 0x06 |
| 54 | #define UART_SCR 0x07 |
| 55 | |
| 56 | int uart_can_tx_byte(void) |
| 57 | { |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 58 | return inb(CONFIG_TTYS0_BASE + UART_LSR) & 0x20; |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void uart_wait_to_tx_byte(void) |
| 62 | { |
| 63 | while(!uart_can_tx_byte()) |
| 64 | ; |
| 65 | } |
| 66 | |
| 67 | void uart_wait_until_sent(void) |
| 68 | { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 69 | while(!(inb(CONFIG_TTYS0_BASE + UART_LSR) & 0x40)) |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 70 | ; |
| 71 | } |
| 72 | |
| 73 | static void uart_tx_byte(unsigned char data) |
| 74 | { |
| 75 | uart_wait_to_tx_byte(); |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 76 | outb(data, CONFIG_TTYS0_BASE + UART_TBR); |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 77 | /* Make certain the data clears the fifos */ |
| 78 | uart_wait_until_sent(); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void uart_init(void) |
| 83 | { |
| 84 | /* disable interrupts */ |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 85 | outb(0x0, CONFIG_TTYS0_BASE + UART_IER); |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 86 | /* enable fifo's */ |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 87 | outb(0x01, CONFIG_TTYS0_BASE + UART_FCR); |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 88 | /* Set Baud Rate Divisor to 12 ==> 115200 Baud */ |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 89 | outb(0x80 | UART_LCS, CONFIG_TTYS0_BASE + UART_LCR); |
| 90 | outb(CONFIG_TTYS0_DIV & 0xFF, CONFIG_TTYS0_BASE + UART_DLL); |
| 91 | outb((CONFIG_TTYS0_DIV >> 8) & 0xFF, CONFIG_TTYS0_BASE + UART_DLM); |
| 92 | outb(UART_LCS, CONFIG_TTYS0_BASE + UART_LCR); |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | void __console_tx_char(unsigned char byte) |
| 97 | { |
| 98 | uart_tx_byte(byte); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 99 | |
Eric Biederman | 6aa31cc | 2003-06-10 21:22:07 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void __console_tx_string(char *str) |
| 103 | { |
| 104 | unsigned char ch; |
| 105 | while((ch = *str++) != '\0') { |
| 106 | __console_tx_char(ch); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | void print_debug_char(unsigned char byte) { __console_tx_char(byte); } |
| 112 | void print_debug(char *str) { __console_tx_string(str); } |
| 113 | |
| 114 | void main(void) |
| 115 | { |
| 116 | static const char msg[] = "hello world\r\n"; |
| 117 | uart_init(); |
| 118 | #if 0 |
| 119 | print_debug(msg); |
| 120 | #endif |
| 121 | #if 1 |
| 122 | print_debug("hello world\r\n"); |
| 123 | #endif |
| 124 | while(1) { |
| 125 | ; |
| 126 | } |
| 127 | } |