1*1bb92983SJerome Forissier /* SPDX-License-Identifier: BSD-2-Clause */ 2974c85b1SJens Wiklander /* 3974c85b1SJens Wiklander * Copyright (c) 2016, Linaro Limited 4974c85b1SJens Wiklander * All rights reserved. 5974c85b1SJens Wiklander * 6974c85b1SJens Wiklander * Redistribution and use in source and binary forms, with or without 7974c85b1SJens Wiklander * modification, are permitted provided that the following conditions are met: 8974c85b1SJens Wiklander * 9974c85b1SJens Wiklander * 1. Redistributions of source code must retain the above copyright notice, 10974c85b1SJens Wiklander * this list of conditions and the following disclaimer. 11974c85b1SJens Wiklander * 12974c85b1SJens Wiklander * 2. Redistributions in binary form must reproduce the above copyright notice, 13974c85b1SJens Wiklander * this list of conditions and the following disclaimer in the documentation 14974c85b1SJens Wiklander * and/or other materials provided with the distribution. 15974c85b1SJens Wiklander * 16974c85b1SJens Wiklander * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17974c85b1SJens Wiklander * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18974c85b1SJens Wiklander * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19974c85b1SJens Wiklander * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20974c85b1SJens Wiklander * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21974c85b1SJens Wiklander * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22974c85b1SJens Wiklander * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23974c85b1SJens Wiklander * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24974c85b1SJens Wiklander * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25974c85b1SJens Wiklander * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26974c85b1SJens Wiklander * POSSIBILITY OF SUCH DAMAGE. 27974c85b1SJens Wiklander */ 28974c85b1SJens Wiklander #ifndef __DRIVERS_SERIAL_H 29974c85b1SJens Wiklander #define __DRIVERS_SERIAL_H 30974c85b1SJens Wiklander 31a26e2529SJerome Forissier #include <assert.h> 32ae8a86d9SJerome Forissier #include <stdbool.h> 33a26e2529SJerome Forissier #include <types_ext.h> 34a26e2529SJerome Forissier #include <mm/core_memprot.h> 35a26e2529SJerome Forissier #include <mm/core_mmu.h> 36ae8a86d9SJerome Forissier 37974c85b1SJens Wiklander struct serial_chip { 38974c85b1SJens Wiklander const struct serial_ops *ops; 39974c85b1SJens Wiklander }; 40974c85b1SJens Wiklander 41974c85b1SJens Wiklander struct serial_ops { 42974c85b1SJens Wiklander void (*putc)(struct serial_chip *chip, int ch); 43974c85b1SJens Wiklander void (*flush)(struct serial_chip *chip); 44974c85b1SJens Wiklander bool (*have_rx_data)(struct serial_chip *chip); 45974c85b1SJens Wiklander int (*getchar)(struct serial_chip *chip); 46974c85b1SJens Wiklander }; 47974c85b1SJens Wiklander 489e30051eSJerome Forissier struct serial_driver { 499e30051eSJerome Forissier /* Allocate device data and return the inner serial_chip */ 509e30051eSJerome Forissier struct serial_chip *(*dev_alloc)(void); 519e30051eSJerome Forissier /* 529e30051eSJerome Forissier * Initialize device from FDT node. @parms is device-specific, 539e30051eSJerome Forissier * its meaning is as defined by the DT bindings for the characters 549e30051eSJerome Forissier * following the ":" in /chosen/stdout-path. Typically for UART 559e30051eSJerome Forissier * devices this is <baud>{<parity>{<bits>{<flow>}}} where: 569e30051eSJerome Forissier * baud - baud rate in decimal 579e30051eSJerome Forissier * parity - 'n' (none), 'o', (odd) or 'e' (even) 589e30051eSJerome Forissier * bits - number of data bits 599e30051eSJerome Forissier * flow - 'r' (rts) 609e30051eSJerome Forissier * For example: 115200n8r 619e30051eSJerome Forissier */ 629e30051eSJerome Forissier int (*dev_init)(struct serial_chip *dev, const void *fdt, 639e30051eSJerome Forissier int offset, const char *parms); 649e30051eSJerome Forissier void (*dev_free)(struct serial_chip *dev); 659e30051eSJerome Forissier }; 669e30051eSJerome Forissier 67a26e2529SJerome Forissier struct io_pa_va { 68a26e2529SJerome Forissier paddr_t pa; 69a26e2529SJerome Forissier vaddr_t va; 70a26e2529SJerome Forissier }; 71a26e2529SJerome Forissier 72a26e2529SJerome Forissier /* 73a26e2529SJerome Forissier * Helper function to return a physical or virtual address for a device, 74a26e2529SJerome Forissier * depending on whether the MMU is enabled or not 75a26e2529SJerome Forissier */ 76a26e2529SJerome Forissier static inline vaddr_t io_pa_or_va(struct io_pa_va *p) 77a26e2529SJerome Forissier { 78a26e2529SJerome Forissier assert(p->pa); 79a26e2529SJerome Forissier if (cpu_mmu_enabled()) { 80a26e2529SJerome Forissier if (!p->va) 81a26e2529SJerome Forissier p->va = (vaddr_t)phys_to_virt_io(p->pa); 82a26e2529SJerome Forissier assert(p->va); 83a26e2529SJerome Forissier return p->va; 84a26e2529SJerome Forissier } 85a26e2529SJerome Forissier return p->pa; 86a26e2529SJerome Forissier } 87a26e2529SJerome Forissier 88974c85b1SJens Wiklander #endif /*__DRIVERS_SERIASERIAL_H*/ 89