console.c revision 250357
1248557Sray/*-
2250357Sray * Copyright (c) 2012, 2013 The FreeBSD Foundation
3248557Sray * All rights reserved.
4248557Sray *
5248557Sray * This software was developed by Oleksandr Rybalko under sponsorship
6248557Sray * from the FreeBSD Foundation.
7248557Sray *
8248557Sray * Redistribution and use in source and binary forms, with or without
9248557Sray * modification, are permitted provided that the following conditions
10248557Sray * are met:
11248557Sray * 1.	Redistributions of source code must retain the above copyright
12248557Sray *	notice, this list of conditions and the following disclaimer.
13248557Sray * 2.	Redistributions in binary form must reproduce the above copyright
14248557Sray *	notice, this list of conditions and the following disclaimer in the
15248557Sray *	documentation and/or other materials provided with the distribution.
16248557Sray *
17248557Sray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18248557Sray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19248557Sray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20248557Sray * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21248557Sray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22248557Sray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23248557Sray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24248557Sray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25248557Sray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26248557Sray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27248557Sray * SUCH DAMAGE.
28248557Sray */
29248557Sray
30248557Sray/* Simple UART console driver for Freescale i.MX515 */
31248557Sray
32248557Sray#include <sys/cdefs.h>
33248557Sray__FBSDID("$FreeBSD: head/sys/arm/freescale/imx/console.c 250357 2013-05-08 09:42:50Z ray $");
34248557Sray
35248557Sray#include <sys/types.h>
36248557Sray#include <sys/param.h>
37248557Sray#include <sys/systm.h>
38248557Sray#include <sys/cons.h>
39248557Sray#include <sys/consio.h>
40248557Sray#include <sys/kernel.h>
41248557Sray
42248557Sray/* Allow it to be predefined, to be able to use another UART for console */
43248557Sray#ifndef	IMX_UART_BASE
44248557Sray#define	IMX_UART_BASE	0xe3fbc000 /* UART1 */
45248557Sray#endif
46248557Sray
47248557Sray#define	IMX_RXD			(u_int32_t *)(IMX_UART_BASE + 0x00)
48248557Sray#define	IMX_TXD			(u_int32_t *)(IMX_UART_BASE + 0x40)
49248557Sray
50248557Sray#define	IMX_UFCR		(u_int32_t *)(IMX_UART_BASE + 0x90)
51248557Sray#define	IMX_USR1		(u_int32_t *)(IMX_UART_BASE + 0x94)
52248557Sray#define	IMX_USR1_TRDY		(1 << 13)
53248557Sray
54248557Sray#define	IMX_USR2		(u_int32_t *)(IMX_UART_BASE + 0x98)
55248557Sray#define	IMX_USR2_RDR		(1 << 0)
56248557Sray#define	IMX_USR2_TXFE		(1 << 14)
57248557Sray#define	IMX_USR2_TXDC		(1 << 3)
58248557Sray
59248557Sray#define	IMX_UTS			(u_int32_t *)(IMX_UART_BASE + 0xb4)
60248557Sray#define	IMX_UTS_TXFULL		(1 << 4)
61248557Sray
62248557Sray/*
63248557Sray * uart related funcs
64248557Sray */
65248557Sraystatic u_int32_t
66248557Srayuart_getreg(u_int32_t *bas)
67248557Sray{
68248557Sray
69248557Sray	return *((volatile u_int32_t *)(bas)) & 0xff;
70248557Sray}
71248557Sray
72248557Sraystatic void
73248557Srayuart_setreg(u_int32_t *bas, u_int32_t val)
74248557Sray{
75248557Sray
76248557Sray	*((volatile u_int32_t *)(bas)) = (u_int32_t)val;
77248557Sray}
78248557Sray
79248557Sraystatic int
80248557Srayub_tstc(void)
81248557Sray{
82248557Sray
83248557Sray	return ((uart_getreg(IMX_USR2) & IMX_USR2_RDR) ? 1 : 0);
84248557Sray}
85248557Sray
86248557Sraystatic int
87248557Srayub_getc(void)
88248557Sray{
89248557Sray
90248557Sray	while (!ub_tstc());
91248557Sray		__asm __volatile("nop");
92248557Sray
93248557Sray	return (uart_getreg(IMX_RXD) & 0xff);
94248557Sray}
95248557Sray
96248557Sraystatic void
97248557Srayub_putc(unsigned char c)
98248557Sray{
99248557Sray
100248557Sray	if (c == '\n')
101248557Sray		ub_putc('\r');
102248557Sray
103248557Sray	while (uart_getreg(IMX_UTS) & IMX_UTS_TXFULL)
104248557Sray		__asm __volatile("nop");
105248557Sray
106248557Sray	uart_setreg(IMX_TXD, c);
107248557Sray}
108248557Sray
109248557Sraystatic cn_probe_t	uart_cnprobe;
110248557Sraystatic cn_init_t	uart_cninit;
111248557Sraystatic cn_term_t	uart_cnterm;
112248557Sraystatic cn_getc_t	uart_cngetc;
113248557Sraystatic cn_putc_t	uart_cnputc;
114248557Sraystatic cn_grab_t	uart_cngrab;
115248557Sraystatic cn_ungrab_t	uart_cnungrab;
116248557Sray
117248557Sraystatic void
118248557Srayuart_cngrab(struct consdev *cp)
119248557Sray{
120248557Sray
121248557Sray}
122248557Sray
123248557Sraystatic void
124248557Srayuart_cnungrab(struct consdev *cp)
125248557Sray{
126248557Sray
127248557Sray}
128248557Sray
129248557Sray
130248557Sraystatic void
131248557Srayuart_cnprobe(struct consdev *cp)
132248557Sray{
133248557Sray
134248557Sray        sprintf(cp->cn_name, "uart");
135248557Sray        cp->cn_pri = CN_NORMAL;
136248557Sray}
137248557Sray
138248557Sraystatic void
139248557Srayuart_cninit(struct consdev *cp)
140248557Sray{
141248557Sray	uart_setreg(IMX_UFCR, 0x00004210);
142248557Sray}
143248557Sray
144248557Srayvoid
145248557Srayuart_cnputc(struct consdev *cp, int c)
146248557Sray{
147248557Sray
148248557Sray	ub_putc(c);
149248557Sray}
150248557Sray
151248557Srayint
152248557Srayuart_cngetc(struct consdev * cp)
153248557Sray{
154248557Sray
155248557Sray	return ub_getc();
156248557Sray}
157248557Sray
158248557Sraystatic void
159248557Srayuart_cnterm(struct consdev * cp)
160248557Sray{
161248557Sray
162248557Sray}
163248557Sray
164248557SrayCONSOLE_DRIVER(uart);
165