xref: /optee_os/core/drivers/imx_uart.c (revision 8c4a5a9a19e87b0b96b6add5e3adc23d877b06ed)
1*8c4a5a9aSPeng Fan /*
2*8c4a5a9aSPeng Fan  * Copyright (C) 2015 Freescale Semiconductor, Inc.
3*8c4a5a9aSPeng Fan  * All rights reserved.
4*8c4a5a9aSPeng Fan  *
5*8c4a5a9aSPeng Fan  * Redistribution and use in source and binary forms, with or without
6*8c4a5a9aSPeng Fan  * modification, are permitted provided that the following conditions are met:
7*8c4a5a9aSPeng Fan  *
8*8c4a5a9aSPeng Fan  * 1. Redistributions of source code must retain the above copyright notice,
9*8c4a5a9aSPeng Fan  * this list of conditions and the following disclaimer.
10*8c4a5a9aSPeng Fan  *
11*8c4a5a9aSPeng Fan  * 2. Redistributions in binary form must reproduce the above copyright notice,
12*8c4a5a9aSPeng Fan  * this list of conditions and the following disclaimer in the documentation
13*8c4a5a9aSPeng Fan  * and/or other materials provided with the distribution.
14*8c4a5a9aSPeng Fan  *
15*8c4a5a9aSPeng Fan  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16*8c4a5a9aSPeng Fan  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*8c4a5a9aSPeng Fan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*8c4a5a9aSPeng Fan  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19*8c4a5a9aSPeng Fan  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*8c4a5a9aSPeng Fan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*8c4a5a9aSPeng Fan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*8c4a5a9aSPeng Fan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*8c4a5a9aSPeng Fan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*8c4a5a9aSPeng Fan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*8c4a5a9aSPeng Fan  * POSSIBILITY OF SUCH DAMAGE.
26*8c4a5a9aSPeng Fan  */
27*8c4a5a9aSPeng Fan 
28*8c4a5a9aSPeng Fan #include <platform_config.h>
29*8c4a5a9aSPeng Fan 
30*8c4a5a9aSPeng Fan #include <drivers/imx_uart.h>
31*8c4a5a9aSPeng Fan #include <console.h>
32*8c4a5a9aSPeng Fan #include <io.h>
33*8c4a5a9aSPeng Fan #include <assert.h>
34*8c4a5a9aSPeng Fan #include <compiler.h>
35*8c4a5a9aSPeng Fan 
36*8c4a5a9aSPeng Fan /* Register definitions */
37*8c4a5a9aSPeng Fan #define URXD  0x0  /* Receiver Register */
38*8c4a5a9aSPeng Fan #define UTXD  0x40 /* Transmitter Register */
39*8c4a5a9aSPeng Fan #define UCR1  0x80 /* Control Register 1 */
40*8c4a5a9aSPeng Fan #define UCR2  0x84 /* Control Register 2 */
41*8c4a5a9aSPeng Fan #define UCR3  0x88 /* Control Register 3 */
42*8c4a5a9aSPeng Fan #define UCR4  0x8c /* Control Register 4 */
43*8c4a5a9aSPeng Fan #define UFCR  0x90 /* FIFO Control Register */
44*8c4a5a9aSPeng Fan #define USR1  0x94 /* Status Register 1 */
45*8c4a5a9aSPeng Fan #define USR2  0x98 /* Status Register 2 */
46*8c4a5a9aSPeng Fan #define UESC  0x9c /* Escape Character Register */
47*8c4a5a9aSPeng Fan #define UTIM  0xa0 /* Escape Timer Register */
48*8c4a5a9aSPeng Fan #define UBIR  0xa4 /* BRM Incremental Register */
49*8c4a5a9aSPeng Fan #define UBMR  0xa8 /* BRM Modulator Register */
50*8c4a5a9aSPeng Fan #define UBRC  0xac /* Baud Rate Count Register */
51*8c4a5a9aSPeng Fan #define UTS   0xb4 /* UART Test Register (mx31) */
52*8c4a5a9aSPeng Fan 
53*8c4a5a9aSPeng Fan /* UART Control Register Bit Fields.*/
54*8c4a5a9aSPeng Fan #define  URXD_CHARRDY    (1<<15)
55*8c4a5a9aSPeng Fan #define  URXD_ERR        (1<<14)
56*8c4a5a9aSPeng Fan #define  URXD_OVRRUN     (1<<13)
57*8c4a5a9aSPeng Fan #define  URXD_FRMERR     (1<<12)
58*8c4a5a9aSPeng Fan #define  URXD_BRK        (1<<11)
59*8c4a5a9aSPeng Fan #define  URXD_PRERR      (1<<10)
60*8c4a5a9aSPeng Fan #define  URXD_RX_DATA    (0xFF)
61*8c4a5a9aSPeng Fan #define  UCR1_ADEN       (1<<15) /* Auto dectect interrupt */
62*8c4a5a9aSPeng Fan #define  UCR1_ADBR       (1<<14) /* Auto detect baud rate */
63*8c4a5a9aSPeng Fan #define  UCR1_TRDYEN     (1<<13) /* Transmitter ready interrupt enable */
64*8c4a5a9aSPeng Fan #define  UCR1_IDEN       (1<<12) /* Idle condition interrupt */
65*8c4a5a9aSPeng Fan #define  UCR1_RRDYEN     (1<<9)	 /* Recv ready interrupt enable */
66*8c4a5a9aSPeng Fan #define  UCR1_RDMAEN     (1<<8)	 /* Recv ready DMA enable */
67*8c4a5a9aSPeng Fan #define  UCR1_IREN       (1<<7)	 /* Infrared interface enable */
68*8c4a5a9aSPeng Fan #define  UCR1_TXMPTYEN   (1<<6)	 /* Transimitter empty interrupt enable */
69*8c4a5a9aSPeng Fan #define  UCR1_RTSDEN     (1<<5)	 /* RTS delta interrupt enable */
70*8c4a5a9aSPeng Fan #define  UCR1_SNDBRK     (1<<4)	 /* Send break */
71*8c4a5a9aSPeng Fan #define  UCR1_TDMAEN     (1<<3)	 /* Transmitter ready DMA enable */
72*8c4a5a9aSPeng Fan #define  UCR1_UARTCLKEN  (1<<2)	 /* UART clock enabled */
73*8c4a5a9aSPeng Fan #define  UCR1_DOZE       (1<<1)	 /* Doze */
74*8c4a5a9aSPeng Fan #define  UCR1_UARTEN     (1<<0)	 /* UART enabled */
75*8c4a5a9aSPeng Fan 
76*8c4a5a9aSPeng Fan #define  UTS_FRCPERR	 (1<<13) /* Force parity error */
77*8c4a5a9aSPeng Fan #define  UTS_LOOP        (1<<12) /* Loop tx and rx */
78*8c4a5a9aSPeng Fan #define  UTS_TXEMPTY	 (1<<6)	 /* TxFIFO empty */
79*8c4a5a9aSPeng Fan #define  UTS_RXEMPTY	 (1<<5)	 /* RxFIFO empty */
80*8c4a5a9aSPeng Fan #define  UTS_TXFULL	 (1<<4)	 /* TxFIFO full */
81*8c4a5a9aSPeng Fan #define  UTS_RXFULL	 (1<<3)	 /* RxFIFO full */
82*8c4a5a9aSPeng Fan #define  UTS_SOFTRST	 (1<<0)	 /* Software reset */
83*8c4a5a9aSPeng Fan 
84*8c4a5a9aSPeng Fan void imx_uart_init(vaddr_t __unused vbase)
85*8c4a5a9aSPeng Fan {
86*8c4a5a9aSPeng Fan 	/*
87*8c4a5a9aSPeng Fan 	 * Do nothing, debug uart(uart0) share with normal world,
88*8c4a5a9aSPeng Fan 	 * everything for uart0 intialization is done in bootloader.
89*8c4a5a9aSPeng Fan 	 */
90*8c4a5a9aSPeng Fan }
91*8c4a5a9aSPeng Fan 
92*8c4a5a9aSPeng Fan void imx_uart_flush_tx_fifo(vaddr_t base)
93*8c4a5a9aSPeng Fan {
94*8c4a5a9aSPeng Fan 	while (!(read32(base + UTS) & UTS_TXEMPTY))
95*8c4a5a9aSPeng Fan 		;
96*8c4a5a9aSPeng Fan }
97*8c4a5a9aSPeng Fan 
98*8c4a5a9aSPeng Fan int imx_uart_getchar(vaddr_t base)
99*8c4a5a9aSPeng Fan {
100*8c4a5a9aSPeng Fan 	while (read32(base + UTS) & UTS_RXEMPTY)
101*8c4a5a9aSPeng Fan 		;
102*8c4a5a9aSPeng Fan 
103*8c4a5a9aSPeng Fan 	return (read32(base + URXD) & URXD_RX_DATA);
104*8c4a5a9aSPeng Fan }
105*8c4a5a9aSPeng Fan 
106*8c4a5a9aSPeng Fan void imx_uart_putc(const char c, vaddr_t base)
107*8c4a5a9aSPeng Fan {
108*8c4a5a9aSPeng Fan 	write32(c, base + UTXD);
109*8c4a5a9aSPeng Fan 
110*8c4a5a9aSPeng Fan 	/* wait until sent */
111*8c4a5a9aSPeng Fan 	while (!(read32(base + UTS) & UTS_TXEMPTY))
112*8c4a5a9aSPeng Fan 		;
113*8c4a5a9aSPeng Fan }
114