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