2 * Copyright (c) 2016, Linaro Limited
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
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.
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.
28 #include <drivers/pl050.h>
36 #define KMI_CLKDIV 0x0c
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)
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)
54 #define KMI_IR_TXINTR (1 << 1)
55 #define KMI_IR_RXINTR (1 << 0)
57 static bool pl050_have_rx_data(struct serial_chip *chip)
59 struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
61 return !!(read8(pd->base + KMI_STAT) & KMI_STAT_RXFULL);
64 static int pl050_getchar(struct serial_chip *chip)
66 struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
68 while (!pl050_have_rx_data(chip))
70 return read8(pd->base + KMI_DATA);
73 static void pl050_flush(struct serial_chip *chip)
75 struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
77 while (!(read8(pd->base + KMI_STAT) & KMI_STAT_TXEMPTY))
81 static void pl050_putc(struct serial_chip *chip, int ch)
83 struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
86 write8(ch, pd->base + KMI_DATA);
89 static const struct serial_ops pl050_ops = {
92 .have_rx_data = pl050_have_rx_data,
93 .getchar = pl050_getchar,
95 KEEP_PAGER(pl050_ops);
97 void pl050_init(struct pl050_data *pd, vaddr_t base, uint32_t clk)
100 pd->chip.ops = &pl050_ops;
102 write8(KMI_ICR_RXINTERN | KMI_ICR_EN, pd->base + KMI_ICR);
103 write8(clk, base + KMI_CLKDIV);