src/[arch-lib]: change "unsigned" to "unsigned int"
[coreboot.git] / src / drivers / uart / uart8250io.c
blob58e014170a42dafe39298320910ea501e06d61fb
1 /*
2 * This file is part of the coreboot project.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <stdlib.h>
15 #include <arch/io.h>
16 #include <boot/coreboot_tables.h>
17 #include <console/uart.h>
18 #include <trace.h>
19 #include "uart8250reg.h"
21 /* Should support 8250, 16450, 16550, 16550A type UARTs */
23 /* Expected character delay at 1200bps is 9ms for a working UART
24 * and no flow-control. Assume UART as stuck if shift register
25 * or FIFO takes more than 50ms per character to appear empty.
27 * Estimated that inb() from UART takes 1 microsecond.
29 #define SINGLE_CHAR_TIMEOUT (50 * 1000)
30 #define FIFO_TIMEOUT (16 * SINGLE_CHAR_TIMEOUT)
32 static int uart8250_can_tx_byte(unsigned int base_port)
34 return inb(base_port + UART8250_LSR) & UART8250_LSR_THRE;
37 static void uart8250_tx_byte(unsigned int base_port, unsigned char data)
39 unsigned long int i = SINGLE_CHAR_TIMEOUT;
40 while (i-- && !uart8250_can_tx_byte(base_port));
41 outb(data, base_port + UART8250_TBR);
44 static void uart8250_tx_flush(unsigned int base_port)
46 unsigned long int i = FIFO_TIMEOUT;
47 while (i-- && !(inb(base_port + UART8250_LSR) & UART8250_LSR_TEMT));
50 static int uart8250_can_rx_byte(unsigned int base_port)
52 return inb(base_port + UART8250_LSR) & UART8250_LSR_DR;
55 static unsigned char uart8250_rx_byte(unsigned int base_port)
57 unsigned long int i = SINGLE_CHAR_TIMEOUT;
58 while (i && !uart8250_can_rx_byte(base_port))
59 i--;
61 if (i)
62 return inb(base_port + UART8250_RBR);
63 else
64 return 0x0;
67 static void uart8250_init(unsigned int base_port, unsigned int divisor)
69 DISABLE_TRACE;
70 /* Disable interrupts */
71 outb(0x0, base_port + UART8250_IER);
72 /* Enable FIFOs */
73 outb(UART8250_FCR_FIFO_EN, base_port + UART8250_FCR);
75 /* assert DTR and RTS so the other end is happy */
76 outb(UART8250_MCR_DTR | UART8250_MCR_RTS, base_port + UART8250_MCR);
78 /* DLAB on */
79 outb(UART8250_LCR_DLAB | CONFIG_TTYS0_LCS, base_port + UART8250_LCR);
81 /* Set Baud Rate Divisor. 12 ==> 9600 Baud */
82 outb(divisor & 0xFF, base_port + UART8250_DLL);
83 outb((divisor >> 8) & 0xFF, base_port + UART8250_DLM);
85 /* Set to 3 for 8N1 */
86 outb(CONFIG_TTYS0_LCS, base_port + UART8250_LCR);
87 ENABLE_TRACE;
90 static const unsigned int bases[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
92 uintptr_t uart_platform_base(int idx)
94 if (idx < ARRAY_SIZE(bases))
95 return bases[idx];
96 return 0;
99 void uart_init(int idx)
101 if (!CONFIG(DRIVERS_UART_8250IO_SKIP_INIT)) {
102 unsigned int div;
103 div = uart_baudrate_divisor(get_uart_baudrate(),
104 uart_platform_refclk(), uart_input_clock_divider());
105 uart8250_init(uart_platform_base(idx), div);
109 void uart_tx_byte(int idx, unsigned char data)
111 uart8250_tx_byte(uart_platform_base(idx), data);
114 unsigned char uart_rx_byte(int idx)
116 return uart8250_rx_byte(uart_platform_base(idx));
119 void uart_tx_flush(int idx)
121 uart8250_tx_flush(uart_platform_base(idx));
124 void uart_fill_lb(void *data)
126 struct lb_serial serial;
127 serial.type = LB_SERIAL_TYPE_IO_MAPPED;
128 serial.baseaddr = uart_platform_base(CONFIG_UART_FOR_CONSOLE);
129 serial.baud = get_uart_baudrate();
130 serial.regwidth = 1;
131 serial.input_hertz = uart_platform_refclk();
132 serial.uart_pci_addr = CONFIG_UART_PCI_ADDR;
133 lb_add_serial(&serial, data);
135 lb_add_console(LB_TAG_CONSOLE_SERIAL8250, data);