1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2016, Linaro Limited 4 * All rights reserved. 5 */ 6 #ifndef __DRIVERS_SERIAL_H 7 #define __DRIVERS_SERIAL_H 8 9 #include <assert.h> 10 #include <stdbool.h> 11 #include <types_ext.h> 12 #include <mm/core_memprot.h> 13 #include <mm/core_mmu.h> 14 15 struct serial_chip { 16 const struct serial_ops *ops; 17 }; 18 19 struct serial_ops { 20 void (*putc)(struct serial_chip *chip, int ch); 21 void (*flush)(struct serial_chip *chip); 22 bool (*have_rx_data)(struct serial_chip *chip); 23 int (*getchar)(struct serial_chip *chip); 24 }; 25 26 struct serial_driver { 27 /* Allocate device data and return the inner serial_chip */ 28 struct serial_chip *(*dev_alloc)(void); 29 /* 30 * Initialize device from FDT node. @parms is device-specific, 31 * its meaning is as defined by the DT bindings for the characters 32 * following the ":" in /chosen/stdout-path. Typically for UART 33 * devices this is <baud>{<parity>{<bits>{<flow>}}} where: 34 * baud - baud rate in decimal 35 * parity - 'n' (none), 'o', (odd) or 'e' (even) 36 * bits - number of data bits 37 * flow - 'r' (rts) 38 * For example: 115200n8r 39 */ 40 int (*dev_init)(struct serial_chip *dev, const void *fdt, 41 int offset, const char *parms); 42 void (*dev_free)(struct serial_chip *dev); 43 }; 44 45 struct io_pa_va { 46 paddr_t pa; 47 vaddr_t va; 48 }; 49 50 /* 51 * Helper function to return a physical or virtual address for a device, 52 * depending on whether the MMU is enabled or not 53 */ 54 static inline vaddr_t io_pa_or_va(struct io_pa_va *p) 55 { 56 assert(p->pa); 57 if (cpu_mmu_enabled()) { 58 if (!p->va) 59 p->va = (vaddr_t)phys_to_virt_io(p->pa); 60 assert(p->va); 61 return p->va; 62 } 63 return p->pa; 64 } 65 66 #endif /*__DRIVERS_SERIASERIAL_H*/ 67