Update from upstream to 2.4.0 version
[platform/core/security/tef-optee_os.git] / core / drivers / pl050.c
1 /*
2  * Copyright (c) 2016, Linaro Limited
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 <compiler.h>
28 #include <drivers/pl050.h>
29 #include <io.h>
30 #include <keep.h>
31 #include <util.h>
32
33 #define KMI_ICR         0x00
34 #define KMI_STAT        0x04
35 #define KMI_DATA        0x08
36 #define KMI_CLKDIV      0x0c
37 #define KMI_IR          0x10
38
39 #define KMI_ICR_TYPE            (1 << 5)
40 #define KMI_ICR_RXINTERN        (1 << 4)
41 #define KMI_ICR_TXINTERN        (1 << 3)
42 #define KMI_ICR_EN              (1 << 2)
43 #define KMI_ICR_FKMID           (1 << 1)
44 #define KMI_ICR_FKMIC           (1 << 0)
45
46 #define KMI_STAT_TXEMPTY        (1 << 6)
47 #define KMI_STAT_TXBUSY         (1 << 5)
48 #define KMI_STAT_RXFULL         (1 << 4)
49 #define KMI_STAT_RXBUSY         (1 << 3)
50 #define KMI_STAT_RXPARITY       (1 << 2)
51 #define KMI_STAT_KMIC           (1 << 1)
52 #define KMI_STAT_KMID           (1 << 0)
53
54 #define KMI_IR_TXINTR           (1 << 1)
55 #define KMI_IR_RXINTR           (1 << 0)
56
57 static bool pl050_have_rx_data(struct serial_chip *chip)
58 {
59         struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
60
61         return !!(read8(pd->base + KMI_STAT) & KMI_STAT_RXFULL);
62 }
63
64 static int pl050_getchar(struct serial_chip *chip)
65 {
66         struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
67
68         while (!pl050_have_rx_data(chip))
69                 ;
70         return read8(pd->base + KMI_DATA);
71 }
72
73 static void pl050_flush(struct serial_chip *chip)
74 {
75         struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
76
77         while (!(read8(pd->base + KMI_STAT) & KMI_STAT_TXEMPTY))
78                 ;
79 }
80
81 static void pl050_putc(struct serial_chip *chip, int ch)
82 {
83         struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
84
85         pl050_flush(chip);
86         write8(ch, pd->base + KMI_DATA);
87 }
88
89 static const struct serial_ops pl050_ops = {
90         .putc = pl050_putc,
91         .flush = pl050_flush,
92         .have_rx_data = pl050_have_rx_data,
93         .getchar = pl050_getchar,
94 };
95 KEEP_PAGER(pl050_ops);
96
97 void pl050_init(struct pl050_data *pd, vaddr_t base, uint32_t clk)
98 {
99         pd->base = base;
100         pd->chip.ops = &pl050_ops;
101
102         write8(KMI_ICR_RXINTERN | KMI_ICR_EN, pd->base + KMI_ICR);
103         write8(clk, base + KMI_CLKDIV);
104 }