123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- #include <stdint.h>
- #include <platform_def.h>
- #include <arch.h>
- #include <lib/mmio.h>
- #include <imx_uart.h>
- #define TX_RX_THRESH 2
- struct clk_div_factors {
- uint32_t fcr_div;
- uint32_t bmr_div;
- };
- static struct clk_div_factors clk_div[] = {
- {
- .fcr_div = IMX_UART_FCR_RFDIV1,
- .bmr_div = 1,
- },
- {
- .fcr_div = IMX_UART_FCR_RFDIV2,
- .bmr_div = 2,
- },
- {
- .fcr_div = IMX_UART_FCR_RFDIV3,
- .bmr_div = 3,
- },
- {
- .fcr_div = IMX_UART_FCR_RFDIV4,
- .bmr_div = 4,
- },
- {
- .fcr_div = IMX_UART_FCR_RFDIV5,
- .bmr_div = 5,
- },
- {
- .fcr_div = IMX_UART_FCR_RFDIV6,
- .bmr_div = 6,
- },
- {
- .fcr_div = IMX_UART_FCR_RFDIV7,
- .bmr_div = 7,
- },
- };
- static void write_reg(uintptr_t base, uint32_t offset, uint32_t val)
- {
- mmio_write_32(base + offset, val);
- }
- static uint32_t read_reg(uintptr_t base, uint32_t offset)
- {
- return mmio_read_32(base + offset);
- }
- int console_imx_uart_core_init(uintptr_t base_addr, unsigned int uart_clk,
- unsigned int baud_rate)
- {
- uint32_t val;
- uint8_t clk_idx = 1;
-
- write_reg(base_addr, IMX_UART_CR2_OFFSET, 0);
- do {
- val = read_reg(base_addr, IMX_UART_CR2_OFFSET);
- } while (!(val & IMX_UART_CR2_SRST));
-
- write_reg(base_addr, IMX_UART_CR1_OFFSET, IMX_UART_CR1_UARTEN);
-
- val = (IMX_UART_CR2_IRTS | IMX_UART_CR2_WS | IMX_UART_CR2_TXEN |
- IMX_UART_CR2_RXEN | IMX_UART_CR2_SRST);
- write_reg(base_addr, IMX_UART_CR2_OFFSET, val);
-
- val = IMX_UART_CR3_ADNIMP | IMX_UART_CR3_RXDMUXSEL;
- write_reg(base_addr, IMX_UART_CR3_OFFSET, val);
-
- write_reg(base_addr, IMX_UART_CR4_OFFSET, 0x8000);
-
- val = IMX_UART_FCR_TXTL(TX_RX_THRESH) | IMX_UART_FCR_RXTL(TX_RX_THRESH) |
- clk_div[clk_idx].fcr_div;
- #ifdef IMX_UART_DTE
-
- val |= IMX_UART_FCR_DCEDTE;
- #endif
- write_reg(base_addr, IMX_UART_FCR_OFFSET, val);
-
- write_reg(base_addr, IMX_UART_BIR_OFFSET, 0x0f);
- val = ((uart_clk / clk_div[clk_idx].bmr_div) / baud_rate) - 1;
- write_reg(base_addr, IMX_UART_BMR_OFFSET, val);
- return 0;
- }
- int console_imx_uart_core_putc(int c, uintptr_t base_addr)
- {
- uint32_t val;
- if (c == '\n')
- console_imx_uart_core_putc('\r', base_addr);
-
- write_reg(base_addr, IMX_UART_TXD_OFFSET, c);
-
- do {
- val = read_reg(base_addr, IMX_UART_STAT2_OFFSET);
- } while (!(val & IMX_UART_STAT2_TXDC));
- return 0;
- }
- int console_imx_uart_core_getc(uintptr_t base_addr)
- {
- uint32_t val;
- val = read_reg(base_addr, IMX_UART_TS_OFFSET);
- if (val & IMX_UART_TS_RXEMPTY)
- return -1;
- val = read_reg(base_addr, IMX_UART_RXD_OFFSET);
- return (int)(val & 0x000000FF);
- }
- void console_imx_uart_core_flush(uintptr_t base_addr)
- {
- }
|