console.c revision 245450
1/*-
2 * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@gmail.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1.	Redistributions of source code must retain the above copyright
9 *	notice, this list of conditions and the following disclaimer.
10 * 2.	Redistributions in binary form must reproduce the above copyright
11 *	notice, this list of conditions and the following disclaimer in the
12 *	documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/* Simple UART console driver for Allwinner A10 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/arm/allwinner/console.c 245450 2013-01-15 08:26:16Z ganbold $");
31
32#include <sys/types.h>
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/cons.h>
36#include <sys/consio.h>
37#include <sys/kernel.h>
38
39#ifndef	A10_UART_BASE
40#define	A10_UART_BASE	0xe1c28000 	/* UART0 */
41#endif
42
43int reg_shift = 2;
44
45#define UART_DLL        0       /* Out: Divisor Latch Low */
46#define UART_DLM        1       /* Out: Divisor Latch High */
47#define UART_FCR        2       /* Out: FIFO Control Register */
48#define UART_LCR        3       /* Out: Line Control Register */
49#define UART_MCR        4       /* Out: Modem Control Register */
50#define UART_LSR        5       /* In:  Line Status Register */
51#define UART_LSR_THRE   0x20    /* Transmit-hold-register empty */
52#define UART_LSR_DR     0x01    /* Receiver data ready */
53#define UART_MSR        6       /* In:  Modem Status Register */
54#define UART_SCR        7       /* I/O: Scratch Register */
55
56
57/*
58 * uart related funcs
59 */
60static u_int32_t
61uart_getreg(u_int32_t *bas)
62{
63	return *((volatile u_int32_t *)(bas)) & 0xff;
64}
65
66static void
67uart_setreg(u_int32_t *bas, u_int32_t val)
68{
69	*((volatile u_int32_t *)(bas)) = (u_int32_t)val;
70}
71
72static int
73ub_getc(void)
74{
75	while ((uart_getreg((u_int32_t *)(A10_UART_BASE +
76	    (UART_LSR << reg_shift))) & UART_LSR_DR) == 0);
77		__asm __volatile("nop");
78
79	return (uart_getreg((u_int32_t *)A10_UART_BASE) & 0xff);
80}
81
82static void
83ub_putc(unsigned char c)
84{
85	if (c == '\n')
86		ub_putc('\r');
87
88	while ((uart_getreg((u_int32_t *)(A10_UART_BASE +
89	    (UART_LSR << reg_shift))) & UART_LSR_THRE) == 0)
90		__asm __volatile("nop");
91
92	uart_setreg((u_int32_t *)A10_UART_BASE, c);
93}
94
95static cn_probe_t	uart_cnprobe;
96static cn_init_t	uart_cninit;
97static cn_term_t	uart_cnterm;
98static cn_getc_t	uart_cngetc;
99static cn_putc_t	uart_cnputc;
100static cn_grab_t	uart_cngrab;
101static cn_ungrab_t	uart_cnungrab;
102
103static void
104uart_cngrab(struct consdev *cp)
105{
106}
107
108static void
109uart_cnungrab(struct consdev *cp)
110{
111}
112
113
114static void
115uart_cnprobe(struct consdev *cp)
116{
117	sprintf(cp->cn_name, "uart");
118	cp->cn_pri = CN_NORMAL;
119}
120
121static void
122uart_cninit(struct consdev *cp)
123{
124	uart_setreg((u_int32_t *)(A10_UART_BASE +
125	    (UART_FCR << reg_shift)), 0x06);
126}
127
128void
129uart_cnputc(struct consdev *cp, int c)
130{
131	ub_putc(c);
132}
133
134int
135uart_cngetc(struct consdev * cp)
136{
137	return ub_getc();
138}
139
140static void
141uart_cnterm(struct consdev * cp)
142{
143}
144
145CONSOLE_DRIVER(uart);
146
147