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_DR 0x01 /* DATA Ready */ 47 #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */ 48 49 static vaddr_t chip_to_base_and_data(struct serial_chip *chip, 50 struct ns16550_data **pd) 51 { 52 *pd = container_of(chip, struct ns16550_data, chip); 53 54 return io_pa_or_va(&(*pd)->base, NS16550_UART_REG_SIZE); 55 } 56 57 static void ns16550_flush(struct serial_chip *chip) 58 { 59 struct ns16550_data *pd = NULL; 60 vaddr_t base = chip_to_base_and_data(chip, &pd); 61 62 while ((serial_in(base + (UART_LSR << pd->reg_shift), pd->io_width) & 63 UART_LSR_THRE) == 0) 64 ; 65 } 66 67 static void ns16550_putc(struct serial_chip *chip, int ch) 68 { 69 struct ns16550_data *pd = NULL; 70 vaddr_t base = chip_to_base_and_data(chip, &pd); 71 72 ns16550_flush(chip); 73 74 /* write out charset to Transmit-hold-register */ 75 serial_out(base + (UART_THR << pd->reg_shift), pd->io_width, ch); 76 } 77 78 static bool ns16550_have_rx_data(struct serial_chip *chip) 79 { 80 struct ns16550_data *pd = NULL; 81 vaddr_t base = chip_to_base_and_data(chip, &pd); 82 83 return serial_in(base + (UART_LSR << pd->reg_shift), pd->io_width) & 84 UART_LSR_DR; 85 } 86 87 static int ns16550_getchar(struct serial_chip *chip) 88 { 89 struct ns16550_data *pd = NULL; 90 vaddr_t base = chip_to_base_and_data(chip, &pd); 91 92 while (!ns16550_have_rx_data(chip)) { 93 /* Data is not ready, waiting again */ 94 ; 95 } 96 97 return serial_in(base + (UART_RBR << pd->reg_shift), pd->io_width) & 98 0xFF; 99 } 100 101 static const struct serial_ops ns16550_ops = { 102 .flush = ns16550_flush, 103 .putc = ns16550_putc, 104 .getchar = ns16550_getchar, 105 .have_rx_data = ns16550_have_rx_data, 106 }; 107 DECLARE_KEEP_PAGER(ns16550_ops); 108 109 void ns16550_init(struct ns16550_data *pd, paddr_t base, uint8_t io_width, 110 uint8_t reg_shift) 111 { 112 pd->base.pa = base; 113 pd->io_width = io_width; 114 pd->reg_shift = reg_shift; 115 pd->chip.ops = &ns16550_ops; 116 117 /* 118 * Do nothing, uart driver shared with normal world, 119 * everything for uart driver initialization is done in bootloader. 120 */ 121 } 122