xref: /freebsd/sys/arm/freescale/imx/imx_console.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * Copyright (c) 2012, 2013 The FreeBSD Foundation
3  *
4  * This software was developed by Oleksandr Rybalko under sponsorship
5  * from the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1.	Redistributions of source code must retain the above copyright
11  *	notice, this list of conditions and the following disclaimer.
12  * 2.	Redistributions in binary form must reproduce the above copyright
13  *	notice, this list of conditions and the following disclaimer in the
14  *	documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /* Simple UART console driver for Freescale i.MX515 */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/cons.h>
38 #include <sys/consio.h>
39 #include <sys/kernel.h>
40 
41 /* Allow it to be predefined, to be able to use another UART for console */
42 #ifndef	IMX_UART_BASE
43 #define	IMX_UART_BASE	0xe3fbc000 /* imx51 UART1 */
44 #endif
45 
46 #define	IMX_RXD			0x00
47 #define	IMX_TXD			0x40
48 
49 #define	IMX_UFCR		0x90
50 #define	IMX_USR1		0x94
51 #define	IMX_USR1_TRDY		(1 << 13)
52 
53 #define	IMX_USR2		0x98
54 #define	IMX_USR2_RDR		(1 << 0)
55 #define	IMX_USR2_TXFE		(1 << 14)
56 #define	IMX_USR2_TXDC		(1 << 3)
57 
58 #define	IMX_UTS			0xb4
59 #define	IMX_UTS_TXFULL		(1 << 4)
60 
61 /*
62  * The base address of the uart registers.
63  *
64  * This is global so that it can be changed on the fly from the outside.  For
65  * example, set imx_uart_base=physaddr and then call cninit() as the first two
66  * lines of initarm() and enjoy printf() availability through the tricky bits of
67  * startup.  After initarm() switches from physical to virtual addressing, just
68  * set imx_uart_base=virtaddr and printf keeps working.
69  */
70 uint32_t imx_uart_base = IMX_UART_BASE;
71 
72 /*
73  * uart related funcs
74  */
75 static uint32_t
76 ub_getreg(uint32_t off)
77 {
78 
79 	return *((volatile uint32_t *)(imx_uart_base + off));
80 }
81 
82 static void
83 ub_setreg(uint32_t off, uint32_t val)
84 {
85 
86 	*((volatile uint32_t *)(imx_uart_base + off)) = val;
87 }
88 
89 static int
90 ub_tstc(void)
91 {
92 
93 	return ((ub_getreg(IMX_USR2) & IMX_USR2_RDR) ? 1 : 0);
94 }
95 
96 static int
97 ub_getc(void)
98 {
99 
100 	while (!ub_tstc());
101 		__asm __volatile("nop");
102 
103 	return (ub_getreg(IMX_RXD) & 0xff);
104 }
105 
106 static void
107 ub_putc(unsigned char c)
108 {
109 
110 	if (c == '\n')
111 		ub_putc('\r');
112 
113 	while (ub_getreg(IMX_UTS) & IMX_UTS_TXFULL)
114 		__asm __volatile("nop");
115 
116 	ub_setreg(IMX_TXD, c);
117 }
118 
119 static cn_probe_t	uart_cnprobe;
120 static cn_init_t	uart_cninit;
121 static cn_term_t	uart_cnterm;
122 static cn_getc_t	uart_cngetc;
123 static cn_putc_t	uart_cnputc;
124 static cn_grab_t	uart_cngrab;
125 static cn_ungrab_t	uart_cnungrab;
126 
127 static void
128 uart_cngrab(struct consdev *cp)
129 {
130 
131 }
132 
133 static void
134 uart_cnungrab(struct consdev *cp)
135 {
136 
137 }
138 
139 static void
140 uart_cnprobe(struct consdev *cp)
141 {
142 
143         sprintf(cp->cn_name, "uart");
144         cp->cn_pri = CN_NORMAL;
145 }
146 
147 static void
148 uart_cninit(struct consdev *cp)
149 {
150 
151         /* Init fifo trigger levels to 32 bytes, refclock div to 2. */
152 	ub_setreg(IMX_UFCR, 0x00004210);
153 }
154 
155 static void
156 uart_cnputc(struct consdev *cp, int c)
157 {
158 
159 	ub_putc(c);
160 }
161 
162 static int
163 uart_cngetc(struct consdev * cp)
164 {
165 
166 	return ub_getc();
167 }
168 
169 static void
170 uart_cnterm(struct consdev * cp)
171 {
172 
173 }
174 
175 CONSOLE_DRIVER(uart);
176