1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (C) 2015 Freescale Semiconductor, Inc. 4 * Copyright (c) 2017, 2020, 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 <keep.h> 32 #include <util.h> 33 34 /* uart register defines */ 35 #define UART_RBR 0x0 36 #define UART_THR 0x0 37 #define UART_IER 0x1 38 #define UART_FCR 0x2 39 #define UART_LCR 0x3 40 #define UART_MCR 0x4 41 #define UART_LSR 0x5 42 #define UART_MSR 0x6 43 #define UART_SPR 0x7 44 45 /* uart status register bits */ 46 #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */ 47 48 static void ns16550_flush(struct serial_chip *chip) 49 { 50 struct ns16550_data *pd = 51 container_of(chip, struct ns16550_data, chip); 52 vaddr_t base = io_pa_or_va(&pd->base, 53 (UART_LSR << pd->reg_shift) + pd->io_width); 54 55 while ((serial_in(base + (UART_LSR << pd->reg_shift), pd->io_width) & 56 UART_LSR_THRE) == 0) 57 ; 58 } 59 60 static void ns16550_putc(struct serial_chip *chip, int ch) 61 { 62 struct ns16550_data *pd = 63 container_of(chip, struct ns16550_data, chip); 64 vaddr_t base = io_pa_or_va(&pd->base, (UART_THR << pd->reg_shift) + 65 pd->io_width); 66 67 ns16550_flush(chip); 68 69 /* write out charset to Transmit-hold-register */ 70 serial_out(base + (UART_THR << pd->reg_shift), pd->io_width, ch); 71 } 72 73 static const struct serial_ops ns16550_ops = { 74 .flush = ns16550_flush, 75 .putc = ns16550_putc, 76 }; 77 DECLARE_KEEP_PAGER(ns16550_ops); 78 79 void ns16550_init(struct ns16550_data *pd, paddr_t base, uint8_t io_width, 80 uint8_t reg_shift) 81 { 82 pd->base.pa = base; 83 pd->io_width = io_width; 84 pd->reg_shift = reg_shift; 85 pd->chip.ops = &ns16550_ops; 86 87 /* 88 * Do nothing, uart driver shared with normal world, 89 * everything for uart driver initialization is done in bootloader. 90 */ 91 } 92