xref: /optee_os/core/drivers/ns16550.c (revision 817466cb476de705a8e3dabe1ef165fe27a18c2f)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (C) 2015 Freescale Semiconductor, Inc.
4  * Copyright (c) 2017, Linaro Limited
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <drivers/ns16550.h>
31 #include <io.h>
32 #include <keep.h>
33 #include <util.h>
34 
35 /* uart register defines */
36 #define UART_RBR	0x0
37 #define UART_THR	0x0
38 #define UART_IER	0x1
39 #define UART_FCR	0x2
40 #define UART_LCR	0x3
41 #define UART_MCR	0x4
42 #define UART_LSR	0x5
43 #define UART_MSR	0x6
44 #define UART_SPR	0x7
45 
46 /* uart status register bits */
47 #define UART_LSR_THRE	0x20 /* Transmit-hold-register empty */
48 
49 static vaddr_t chip_to_base(struct serial_chip *chip)
50 {
51 	struct ns16550_data *pd =
52 		container_of(chip, struct ns16550_data, chip);
53 
54 	return io_pa_or_va(&pd->base);
55 }
56 
57 static void ns16550_flush(struct serial_chip *chip)
58 {
59 	vaddr_t base = chip_to_base(chip);
60 
61 	while ((read8(base + UART_LSR) & UART_LSR_THRE) == 0)
62 		;
63 }
64 
65 static void ns16550_putc(struct serial_chip *chip, int ch)
66 {
67 	vaddr_t base = chip_to_base(chip);
68 
69 	ns16550_flush(chip);
70 
71 	/* write out charset to Transmit-hold-register */
72 	write8(ch, base + UART_THR);
73 }
74 
75 static const struct serial_ops ns16550_ops = {
76 	.flush = ns16550_flush,
77 	.putc = ns16550_putc,
78 };
79 KEEP_PAGER(ns16550_ops);
80 
81 void ns16550_init(struct ns16550_data *pd, paddr_t base)
82 {
83 	pd->base.pa = base;
84 	pd->chip.ops = &ns16550_ops;
85 
86 	/*
87 	 * Do nothing, uart driver shared with normal world,
88 	 * everything for uart driver initialization is done in bootloader.
89 	 */
90 }
91