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>
35 #define KMI_CLKDIV 0x0c
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)
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)
53 #define KMI_IR_TXINTR (1 << 1)
54 #define KMI_IR_RXINTR (1 << 0)
56 static bool pl050_have_rx_data(struct serial_chip *chip)
58 struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
60 return !!(read8(pd->base + KMI_STAT) & KMI_STAT_RXFULL);
63 static int pl050_getchar(struct serial_chip *chip)
65 struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
67 while (!pl050_have_rx_data(chip))
69 return read8(pd->base + KMI_DATA);
72 static void pl050_flush(struct serial_chip *chip)
74 struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
76 while (!(read8(pd->base + KMI_STAT) & KMI_STAT_TXEMPTY))
80 static void pl050_putc(struct serial_chip *chip, int ch)
82 struct pl050_data *pd = container_of(chip, struct pl050_data, chip);
85 write8(ch, pd->base + KMI_DATA);
88 static const struct serial_ops pl050_ops = {
91 .have_rx_data = pl050_have_rx_data,
92 .getchar = pl050_getchar,
95 void pl050_init(struct pl050_data *pd, vaddr_t base, uint32_t clk)
98 pd->chip.ops = &pl050_ops;
100 write8(KMI_ICR_RXINTERN | KMI_ICR_EN, pd->base + KMI_ICR);
101 write8(clk, base + KMI_CLKDIV);