xref: /optee_os/core/drivers/stih_asc.c (revision 78b7c7c7653f8bff42fe44d31a79d7f6bbfd4d47)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2017, Linaro Limited
4  * All rights reserved.
5  */
6 #include <drivers/stih_asc.h>
7 #include <io.h>
8 #include <keep.h>
9 #include <util.h>
10 
11 #define ASC_BAUDRATE		0x00
12 #define ASC_TXBUFFER		0x04
13 #define ASC_STATUS		0x14
14 
15 #define ASC_STATUS_TX_EMPTY		BIT(1)
16 #define ASC_STATUS_TX_HALF_EMPTY	BIT(2)
17 
18 static vaddr_t chip_to_base(struct serial_chip *chip)
19 {
20 	struct stih_asc_pd *pd =
21 		container_of(chip, struct stih_asc_pd, chip);
22 
23 	return io_pa_or_va(&pd->base);
24 }
25 
26 static void stih_asc_flush(struct serial_chip *chip)
27 {
28 	vaddr_t base = chip_to_base(chip);
29 
30 	while (!(read32(base + ASC_STATUS) & ASC_STATUS_TX_EMPTY))
31 		;
32 }
33 
34 static void stih_asc_putc(struct serial_chip *chip, int ch)
35 {
36 	vaddr_t base = chip_to_base(chip);
37 
38 	while (!(read32(base + ASC_STATUS) & ASC_STATUS_TX_HALF_EMPTY))
39 		;
40 
41 	write32(ch, base + ASC_TXBUFFER);
42 }
43 
44 static const struct serial_ops stih_asc_ops = {
45 	.flush = stih_asc_flush,
46 	.putc = stih_asc_putc,
47 };
48 KEEP_PAGER(stih_asc_ops);
49 
50 void stih_asc_init(struct stih_asc_pd *pd, vaddr_t base)
51 {
52 	pd->base.pa = base;
53 	pd->chip.ops = &stih_asc_ops;
54 }
55