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