Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / util / romcc / tests / simple_test20.c
1 static void outb(unsigned char value, unsigned short port)
2 {
3         __builtin_outb(value, port);
4 }
5
6 static void outl(unsigned int value, unsigned short port)
7 {
8         __builtin_outl(value, port);
9 }
10
11 static unsigned char inb(unsigned short port)
12 {
13         return __builtin_inb(port);
14 }
15
16 static unsigned char inl(unsigned short port)
17 {
18         return __builtin_inl(port);
19 }
20
21 static unsigned int config_cmd(unsigned char bus, unsigned devfn, unsigned where)
22 {
23         return 0x80000000 | (bus << 16) | (devfn << 8) | (where & ~3);
24 }
25
26 static unsigned int pcibios_read_config_dword(
27         unsigned char bus, unsigned devfn, unsigned where)
28 {
29         outl(config_cmd(bus, devfn, where), 0xCF8);
30         return inl(0xCFC);
31 }
32
33
34
35 /* Base Address */
36 #ifndef CONFIG_TTYS0_BASE
37 #define CONFIG_TTYS0_BASE 0x3f8
38 #endif
39
40 #ifndef CONFIG_TTYS0_BAUD
41 #define CONFIG_TTYS0_BAUD 115200
42 #endif
43
44 #if ((115200%CONFIG_TTYS0_BAUD) != 0)
45 #error Bad ttys0 baud rate
46 #endif
47
48 #define CONFIG_TTYS0_DIV        (115200/CONFIG_TTYS0_BAUD)
49
50 /* Line Control Settings */
51 #ifndef CONFIG_TTYS0_LCS
52 /* Set 8bit, 1 stop bit, no parity */
53 #define CONFIG_TTYS0_LCS        0x3
54 #endif
55
56 #define UART_LCS        CONFIG_TTYS0_LCS
57
58 /* Data */
59 #define UART_RBR 0x00
60 #define UART_TBR 0x00
61
62 /* Control */
63 #define UART_IER 0x01
64 #define UART_IIR 0x02
65 #define UART_FCR 0x02
66 #define UART_LCR 0x03
67 #define UART_MCR 0x04
68 #define UART_DLL 0x00
69 #define UART_DLM 0x01
70
71 /* Status */
72 #define UART_LSR 0x05
73 #define UART_MSR 0x06
74 #define UART_SCR 0x07
75
76 int uart_can_tx_byte(void)
77 {
78         return inb(CONFIG_TTYS0_BASE + UART_LSR) & 0x20;
79 }
80
81 void uart_wait_to_tx_byte(void)
82 {
83         while(!uart_can_tx_byte())
84                 ;
85 }
86
87 void uart_wait_until_sent(void)
88 {
89         while(!(inb(CONFIG_TTYS0_BASE + UART_LSR) & 0x40))
90                 ;
91 }
92
93 void uart_tx_byte(unsigned char data)
94 {
95         uart_wait_to_tx_byte();
96         outb(data, CONFIG_TTYS0_BASE + UART_TBR);
97         /* Make certain the data clears the fifos */
98         uart_wait_until_sent();
99 }
100
101 void uart_init(void)
102 {
103         /* disable interrupts */
104         outb(0x0, CONFIG_TTYS0_BASE + UART_IER);
105         /* enable fifo's */
106         outb(0x01, CONFIG_TTYS0_BASE + UART_FCR);
107         /* Set Baud Rate Divisor to 12 ==> 115200 Baud */
108         outb(0x80 | UART_LCS, CONFIG_TTYS0_BASE + UART_LCR);
109         outb(CONFIG_TTYS0_DIV & 0xFF,   CONFIG_TTYS0_BASE + UART_DLL);
110         outb((CONFIG_TTYS0_DIV >> 8) & 0xFF,    CONFIG_TTYS0_BASE + UART_DLM);
111         outb(UART_LCS, CONFIG_TTYS0_BASE + UART_LCR);
112 }
113
114 void __console_tx_char(unsigned char byte)
115 {
116         uart_tx_byte(byte);
117 }
118 void __console_tx_nibble(unsigned nibble)
119 {
120         unsigned char digit;
121         digit = nibble + '0';
122         if (digit > '9') {
123                 digit += 39;
124         }
125         __console_tx_char(digit);
126 }
127
128 void __console_tx_hex32(unsigned int value)
129 {
130         __console_tx_nibble((value >> 28) & 0x0f);
131         __console_tx_nibble((value >> 24) & 0x0f);
132         __console_tx_nibble((value >> 20) & 0x0f);
133         __console_tx_nibble((value >> 16) & 0x0f);
134         __console_tx_nibble((value >> 12) & 0x0f);
135         __console_tx_nibble((value >>  8) & 0x0f);
136         __console_tx_nibble((value >>  4) & 0x0f);
137         __console_tx_nibble(value & 0x0f);
138 }
139
140 void print_debug_hex32(unsigned int value) { __console_tx_hex32(value); }
141
142
143 void main(void)
144 {
145         unsigned long htic;
146         htic = pcibios_read_config_dword(0, 0xc0, 0x6c);
147         print_debug_hex32(htic);
148 }