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