summaryrefslogtreecommitdiff
path: root/src/cpu/allwinner/a10/uart.c
blob: f7c2db99b3375d64933e482f10ae990d711273cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2013  Alexandru Gagniuc <mr.nuke.me@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Uart setup helpers for Allwinner SoCs
 *
 *
 */

#include "uart.h"
#include <arch/io.h>
#include <console/uart.h>
#include <drivers/uart/uart8250reg.h>

/**
 * \brief Configure line control settings for UART
 */
static void a10_uart_configure(struct a10_uart *uart, u32 baud_rate, u8 data_bits,
			enum uart_parity parity, u8 stop_bits)
{
	u32 reg32;
	u16 div;

	div = (u16) uart_baudrate_divisor(baud_rate,
		uart_platform_refclk(), 16);
	/* Enable access to Divisor Latch register */
	write32(&uart->lcr, UART8250_LCR_DLAB);
	/* Set baudrate */
	write32(&uart->dlh, (div >> 8) & 0xff);
	write32(&uart->dll, div & 0xff);
	/* Set line control */
	reg32 = (data_bits - 5) & UART8250_LCR_WLS_MSK;
	switch (parity) {
	case UART_PARITY_ODD:
		reg32 |= UART8250_LCR_PEN;
		break;
	case UART_PARITY_EVEN:
		reg32 |= UART8250_LCR_PEN;
		reg32 |= UART8250_LCR_EPS;
		break;
	case UART_PARITY_NONE:	/* Fall through */
	default:
		break;
	}
	write32(&uart->lcr, reg32);
}

static void a10_uart_enable_fifos(struct a10_uart *uart)
{
	write32(&uart->fcr, UART8250_FCR_FIFO_EN);
}

static int tx_fifo_full(struct a10_uart *uart)
{
	/* This may be a misnomer, or a typo in the datasheet. THRE indicates
	 * that the TX register is empty, not that the FIFO is not full, but
	 * this may be due to a datasheet typo. Keep the current name to signal
	 * intent. */
	return !(read32(&uart->lsr) & UART8250_LSR_THRE);
}

static int rx_fifo_empty(struct a10_uart *uart)
{
	return !(read32(&uart->lsr) & UART8250_LSR_DR);
}

/**
 * \brief Read a single byte from the UART.
 *
 * Blocks until at least a byte is available.
 */
static u8 a10_uart_rx_blocking(struct a10_uart *uart)
{
	while (rx_fifo_empty(uart));

	return read32(&uart->rbr);
}

/**
 * \brief Write a single byte to the UART.
 *
 * Blocks until there is space in the FIFO.
 */
static void a10_uart_tx_blocking(struct a10_uart *uart, u8 data)
{
	while (tx_fifo_full(uart));

	return write32(&uart->thr, data);
}


void uart_init(int idx)
{
	struct a10_uart *uart_base = uart_platform_baseptr(idx);

	/* Use default 8N1 encoding */
	a10_uart_configure(uart_base, CONFIG_TTYS0_BAUD,
		8, UART_PARITY_NONE, 1);
	a10_uart_enable_fifos(uart_base);
}

unsigned char uart_rx_byte(int idx)
{
	return a10_uart_rx_blocking(uart_platform_baseptr(idx));
}

void uart_tx_byte(int idx, unsigned char data)
{
	a10_uart_tx_blocking(uart_platform_baseptr(idx), data);
}

void uart_tx_flush(int idx)
{
}