1/*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2003, 2004 Marcel Moolenaar 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 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#ifndef _DEV_UART_CPU_H_ 30#define _DEV_UART_CPU_H_ 31 32#include <sys/kdb.h> 33#include <sys/lock.h> 34#include <sys/mutex.h> 35 36struct uart_softc; 37 38/* 39 * Low-level operations for use by console and/or debug port support. 40 */ 41struct uart_ops { 42 int (*probe)(struct uart_bas *); 43 void (*init)(struct uart_bas *, int, int, int, int); 44 void (*term)(struct uart_bas *); 45 void (*putc)(struct uart_bas *, int); 46 int (*rxready)(struct uart_bas *); 47 int (*getc)(struct uart_bas *, struct mtx *); 48}; 49 50extern bus_space_tag_t uart_bus_space_io; 51extern bus_space_tag_t uart_bus_space_mem; 52 53/* 54 * PCI ID used for matching "unique" devices to a console. 55 */ 56struct uart_pci_info { 57 uint16_t vendor; 58 uint16_t device; 59}; 60 61/* 62 * Console and debug port device info. 63 */ 64struct uart_devinfo { 65 SLIST_ENTRY(uart_devinfo) next; 66 struct uart_ops *ops; 67 struct uart_bas bas; 68 int baudrate; 69 int databits; 70 int stopbits; 71 int parity; 72 int type; 73#define UART_DEV_CONSOLE 0 74#define UART_DEV_DBGPORT 1 75#define UART_DEV_KEYBOARD 2 76 int (*attach)(struct uart_softc*); 77 int (*detach)(struct uart_softc*); 78 void *cookie; /* Type dependent use. */ 79 struct mtx *hwmtx; 80 struct uart_softc *sc; /* valid only from start of attach */ 81 struct uart_pci_info pci_info; 82}; 83 84int uart_cpu_eqres(struct uart_bas *, struct uart_bas *); 85int uart_cpu_getdev(int, struct uart_devinfo *); 86 87int uart_getenv(int, struct uart_devinfo *, struct uart_class *); 88const char *uart_getname(struct uart_class *); 89struct uart_ops *uart_getops(struct uart_class *); 90int uart_getrange(struct uart_class *); 91u_int uart_getregshift(struct uart_class *); 92u_int uart_getregiowidth(struct uart_class *); 93 94void uart_add_sysdev(struct uart_devinfo *); 95 96/* 97 * Operations for low-level access to the UART. Primarily for use 98 * by console and debug port logic. 99 */ 100 101static __inline void 102uart_lock(struct mtx *hwmtx) 103{ 104 if (!kdb_active && hwmtx != NULL) 105 mtx_lock_spin(hwmtx); 106} 107 108static __inline void 109uart_unlock(struct mtx *hwmtx) 110{ 111 if (!kdb_active && hwmtx != NULL) 112 mtx_unlock_spin(hwmtx); 113} 114 115static __inline int 116uart_probe(struct uart_devinfo *di) 117{ 118 int res; 119 120 uart_lock(di->hwmtx); 121 res = di->ops->probe(&di->bas); 122 uart_unlock(di->hwmtx); 123 return (res); 124} 125 126static __inline void 127uart_init(struct uart_devinfo *di) 128{ 129 uart_lock(di->hwmtx); 130 di->ops->init(&di->bas, di->baudrate, di->databits, di->stopbits, 131 di->parity); 132 uart_unlock(di->hwmtx); 133} 134 135static __inline void 136uart_term(struct uart_devinfo *di) 137{ 138 uart_lock(di->hwmtx); 139 di->ops->term(&di->bas); 140 uart_unlock(di->hwmtx); 141} 142 143static __inline void 144uart_putc(struct uart_devinfo *di, int c) 145{ 146 uart_lock(di->hwmtx); 147 di->ops->putc(&di->bas, c); 148 uart_unlock(di->hwmtx); 149} 150 151static __inline int 152uart_rxready(struct uart_devinfo *di) 153{ 154 int res; 155 156 uart_lock(di->hwmtx); 157 res = di->ops->rxready(&di->bas); 158 uart_unlock(di->hwmtx); 159 return (res); 160} 161 162static __inline int 163uart_poll(struct uart_devinfo *di) 164{ 165 int res; 166 167 uart_lock(di->hwmtx); 168 if (di->ops->rxready(&di->bas)) 169 res = di->ops->getc(&di->bas, NULL); 170 else 171 res = -1; 172 uart_unlock(di->hwmtx); 173 return (res); 174} 175 176static __inline int 177uart_getc(struct uart_devinfo *di) 178{ 179 180 return (di->ops->getc(&di->bas, di->hwmtx)); 181} 182 183void uart_grab(struct uart_devinfo *di); 184void uart_ungrab(struct uart_devinfo *di); 185 186#endif /* _DEV_UART_CPU_H_ */ 187