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