Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / util / romcc / tests / simple_test27.c
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 */
12 #ifndef CONFIG_TTYS0_BASE
13 #define CONFIG_TTYS0_BASE 0x3f8
14 #endif
15
16 #ifndef CONFIG_TTYS0_BAUD
17 #define CONFIG_TTYS0_BAUD 115200
18 #endif
19
20 #if ((115200%CONFIG_TTYS0_BAUD) != 0)
21 #error Bad ttys0 baud rate
22 #endif
23
24 #if CONFIG_TTYS0_BAUD == 115200
25 #define CONFIG_TTYS0_DIV (1)
26 #else
27 #define CONFIG_TTYS0_DIV        (115200/CONFIG_TTYS0_BAUD)
28 #endif
29
30 /* Line Control Settings */
31 #ifndef CONFIG_TTYS0_LCS
32 /* Set 8bit, 1 stop bit, no parity */
33 #define CONFIG_TTYS0_LCS        0x3
34 #endif
35
36 #define UART_LCS        CONFIG_TTYS0_LCS
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 {
58         return inb(CONFIG_TTYS0_BASE + UART_LSR) & 0x20;
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 {
69         while(!(inb(CONFIG_TTYS0_BASE + UART_LSR) & 0x40))
70                 ;
71 }
72
73 static void uart_tx_byte(unsigned char data)
74 {
75         uart_wait_to_tx_byte();
76         outb(data, CONFIG_TTYS0_BASE + UART_TBR);
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 */
85         outb(0x0, CONFIG_TTYS0_BASE + UART_IER);
86         /* enable fifo's */
87         outb(0x01, CONFIG_TTYS0_BASE + UART_FCR);
88         /* Set Baud Rate Divisor to 12 ==> 115200 Baud */
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);
93 }
94
95
96 void __console_tx_char(unsigned char byte)
97 {
98         uart_tx_byte(byte);
99
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         print_debug("how are you today\r\n");
124 #endif
125         while(1) {
126                 ;
127         }
128 }
129
130 void main2(void)
131 {
132         main();
133 }