Update from upstream to 2.4.0 version
[platform/core/security/tef-optee_os.git] / core / drivers / cdns_uart.c
1 /*
2  * Copyright (c) 2016, Xilinx Inc.
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 are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <assert.h>
28 #include <drivers/cdns_uart.h>
29 #include <io.h>
30 #include <keep.h>
31 #include <mm/core_mmu.h>
32 #include <util.h>
33
34 #define CDNS_UART_CONTROL               0
35 #define CDNS_UART_MODE                  4
36 #define CDNS_UART_IEN                   8
37 #define CDNS_UART_IRQ_STATUS            0x14
38 #define CDNS_UART_CHANNEL_STATUS        0x2c
39 #define CDNS_UART_FIFO                  0x30
40
41 #define CDNS_UART_CONTROL_RXRES         BIT(0)
42 #define CDNS_UART_CONTROL_TXRES         BIT(1)
43 #define CDNS_UART_CONTROL_RXEN          BIT(2)
44 #define CDNS_UART_CONTROL_TXEN          BIT(4)
45
46 #define CDNS_UART_MODE_8BIT             (0 << 1)
47 #define CDNS_UART_MODE_PARITY_NONE      (0x4 << 3)
48 #define CDNS_UART_MODE_1STP             (0 << 6)
49
50 #define CDNS_UART_CHANNEL_STATUS_TFUL   BIT(4)
51 #define CDNS_UART_CHANNEL_STATUS_TEMPTY BIT(3)
52 #define CDNS_UART_CHANNEL_STATUS_REMPTY BIT(1)
53
54 #define CDNS_UART_IRQ_RXTRIG            BIT(0)
55 #define CDNS_UART_IRQ_RXTOUT            BIT(8)
56
57 static vaddr_t chip_to_base(struct serial_chip *chip)
58 {
59         struct cdns_uart_data *pd =
60                 container_of(chip, struct cdns_uart_data, chip);
61
62         return io_pa_or_va(&pd->base);
63 }
64
65 static void cdns_uart_flush(struct serial_chip *chip)
66 {
67         vaddr_t base = chip_to_base(chip);
68
69         while (!(read32(base + CDNS_UART_CHANNEL_STATUS) &
70                  CDNS_UART_CHANNEL_STATUS_TEMPTY))
71                 ;
72 }
73
74 static bool cdns_uart_have_rx_data(struct serial_chip *chip)
75 {
76         vaddr_t base = chip_to_base(chip);
77
78         return !(read32(base + CDNS_UART_CHANNEL_STATUS) &
79                         CDNS_UART_CHANNEL_STATUS_REMPTY);
80 }
81
82 static int cdns_uart_getchar(struct serial_chip *chip)
83 {
84         vaddr_t base = chip_to_base(chip);
85
86         while (!cdns_uart_have_rx_data(chip))
87                 ;
88         return read32(base + CDNS_UART_FIFO) & 0xff;
89 }
90
91 static void cdns_uart_putc(struct serial_chip *chip, int ch)
92 {
93         vaddr_t base = chip_to_base(chip);
94
95         /* Wait until there is space in the FIFO */
96         while (read32(base + CDNS_UART_CHANNEL_STATUS) &
97                         CDNS_UART_CHANNEL_STATUS_TFUL)
98                 ;
99
100         /* Send the character */
101         write32(ch, base + CDNS_UART_FIFO);
102 }
103
104
105 static const struct serial_ops cdns_uart_ops = {
106         .flush = cdns_uart_flush,
107         .getchar = cdns_uart_getchar,
108         .have_rx_data = cdns_uart_have_rx_data,
109         .putc = cdns_uart_putc,
110 };
111 KEEP_PAGER(cdns_uart_ops);
112
113 /*
114  * we rely on the bootloader having set up the HW correctly, we just enable
115  * transmitter/receiver here, just in case.
116  */
117 void cdns_uart_init(struct cdns_uart_data *pd, paddr_t base, uint32_t uart_clk,
118                 uint32_t baud_rate)
119 {
120         pd->base.pa = base;
121         pd->chip.ops = &cdns_uart_ops;
122
123         if (!uart_clk || !baud_rate)
124                 return;
125
126         /* Enable UART and RX/TX */
127         write32(CDNS_UART_CONTROL_RXEN | CDNS_UART_CONTROL_TXEN,
128                 base + CDNS_UART_CONTROL);
129
130         cdns_uart_flush(&pd->chip);
131 }