xref: /freebsd-13.1/sys/dev/uart/uart_cpu.h (revision ce528519)
1098ca2bdSWarner Losh /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
42ae4f1fdSMarcel Moolenaar  * Copyright (c) 2003, 2004 Marcel Moolenaar
527d5dc18SMarcel Moolenaar  * All rights reserved.
627d5dc18SMarcel Moolenaar  *
727d5dc18SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
827d5dc18SMarcel Moolenaar  * modification, are permitted provided that the following conditions
927d5dc18SMarcel Moolenaar  * are met:
1027d5dc18SMarcel Moolenaar  *
1127d5dc18SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
1227d5dc18SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
1327d5dc18SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
1427d5dc18SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
1527d5dc18SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
1627d5dc18SMarcel Moolenaar  *
1727d5dc18SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1827d5dc18SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1927d5dc18SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2027d5dc18SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2127d5dc18SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2227d5dc18SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2327d5dc18SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2427d5dc18SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2527d5dc18SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2627d5dc18SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727d5dc18SMarcel Moolenaar  *
2827d5dc18SMarcel Moolenaar  * $FreeBSD$
2927d5dc18SMarcel Moolenaar  */
3027d5dc18SMarcel Moolenaar 
3127d5dc18SMarcel Moolenaar #ifndef _DEV_UART_CPU_H_
3227d5dc18SMarcel Moolenaar #define _DEV_UART_CPU_H_
3327d5dc18SMarcel Moolenaar 
348af03381SMarcel Moolenaar #include <sys/kdb.h>
358af03381SMarcel Moolenaar #include <sys/lock.h>
368af03381SMarcel Moolenaar #include <sys/mutex.h>
378af03381SMarcel Moolenaar 
38d76a1ef4SWarner Losh struct uart_softc;
39d76a1ef4SWarner Losh 
4027d5dc18SMarcel Moolenaar /*
4127d5dc18SMarcel Moolenaar  * Low-level operations for use by console and/or debug port support.
4227d5dc18SMarcel Moolenaar  */
4327d5dc18SMarcel Moolenaar struct uart_ops {
4427d5dc18SMarcel Moolenaar 	int (*probe)(struct uart_bas *);
4527d5dc18SMarcel Moolenaar 	void (*init)(struct uart_bas *, int, int, int, int);
4627d5dc18SMarcel Moolenaar 	void (*term)(struct uart_bas *);
4727d5dc18SMarcel Moolenaar 	void (*putc)(struct uart_bas *, int);
4897202af2SMarius Strobl 	int (*rxready)(struct uart_bas *);
49634e63c9SMarcel Moolenaar 	int (*getc)(struct uart_bas *, struct mtx *);
5027d5dc18SMarcel Moolenaar };
5127d5dc18SMarcel Moolenaar 
522ae4f1fdSMarcel Moolenaar extern bus_space_tag_t uart_bus_space_io;
532ae4f1fdSMarcel Moolenaar extern bus_space_tag_t uart_bus_space_mem;
542ae4f1fdSMarcel Moolenaar 
5527d5dc18SMarcel Moolenaar /*
56*ce528519SColin Percival  * PCI ID used for matching "unique" devices to a console.
57*ce528519SColin Percival  */
58*ce528519SColin Percival struct uart_pci_info {
59*ce528519SColin Percival 	uint16_t vendor;
60*ce528519SColin Percival 	uint16_t device;
61*ce528519SColin Percival };
62*ce528519SColin Percival 
63*ce528519SColin Percival /*
6427d5dc18SMarcel Moolenaar  * Console and debug port device info.
6527d5dc18SMarcel Moolenaar  */
6627d5dc18SMarcel Moolenaar struct uart_devinfo {
6727d5dc18SMarcel Moolenaar 	SLIST_ENTRY(uart_devinfo) next;
68f8100ce2SMarcel Moolenaar 	struct uart_ops *ops;
6927d5dc18SMarcel Moolenaar 	struct uart_bas bas;
7027d5dc18SMarcel Moolenaar 	int	baudrate;
7127d5dc18SMarcel Moolenaar 	int	databits;
7227d5dc18SMarcel Moolenaar 	int	stopbits;
7327d5dc18SMarcel Moolenaar 	int	parity;
7427d5dc18SMarcel Moolenaar 	int	type;
7527d5dc18SMarcel Moolenaar #define	UART_DEV_CONSOLE	0
7627d5dc18SMarcel Moolenaar #define	UART_DEV_DBGPORT	1
7727d5dc18SMarcel Moolenaar #define	UART_DEV_KEYBOARD	2
7827d5dc18SMarcel Moolenaar 	int	(*attach)(struct uart_softc*);
7927d5dc18SMarcel Moolenaar 	int	(*detach)(struct uart_softc*);
8027d5dc18SMarcel Moolenaar 	void	*cookie;		/* Type dependent use. */
818af03381SMarcel Moolenaar 	struct mtx *hwmtx;
82d76a1ef4SWarner Losh 	struct uart_softc *sc;		/* valid only from start of attach */
83*ce528519SColin Percival 	struct uart_pci_info pci_info;
8427d5dc18SMarcel Moolenaar };
8527d5dc18SMarcel Moolenaar 
865cc70551SMarcel Moolenaar int uart_cpu_eqres(struct uart_bas *, struct uart_bas *);
875cc70551SMarcel Moolenaar int uart_cpu_getdev(int, struct uart_devinfo *);
88f8100ce2SMarcel Moolenaar 
89f8100ce2SMarcel Moolenaar int uart_getenv(int, struct uart_devinfo *, struct uart_class *);
90f8100ce2SMarcel Moolenaar const char *uart_getname(struct uart_class *);
91f8100ce2SMarcel Moolenaar struct uart_ops *uart_getops(struct uart_class *);
92f8100ce2SMarcel Moolenaar int uart_getrange(struct uart_class *);
93405ada37SAndrew Turner u_int uart_getregshift(struct uart_class *);
94c214a270SRuslan Bukin u_int uart_getregiowidth(struct uart_class *);
9527d5dc18SMarcel Moolenaar 
9627d5dc18SMarcel Moolenaar void uart_add_sysdev(struct uart_devinfo *);
9727d5dc18SMarcel Moolenaar 
9827d5dc18SMarcel Moolenaar /*
9927d5dc18SMarcel Moolenaar  * Operations for low-level access to the UART. Primarily for use
10027d5dc18SMarcel Moolenaar  * by console and debug port logic.
10127d5dc18SMarcel Moolenaar  */
1028af03381SMarcel Moolenaar 
1038af03381SMarcel Moolenaar static __inline void
uart_lock(struct mtx * hwmtx)1048af03381SMarcel Moolenaar uart_lock(struct mtx *hwmtx)
1058af03381SMarcel Moolenaar {
1068af03381SMarcel Moolenaar 	if (!kdb_active && hwmtx != NULL)
1078af03381SMarcel Moolenaar 		mtx_lock_spin(hwmtx);
1088af03381SMarcel Moolenaar }
1098af03381SMarcel Moolenaar 
1108af03381SMarcel Moolenaar static __inline void
uart_unlock(struct mtx * hwmtx)1118af03381SMarcel Moolenaar uart_unlock(struct mtx *hwmtx)
1128af03381SMarcel Moolenaar {
1138af03381SMarcel Moolenaar 	if (!kdb_active && hwmtx != NULL)
1148af03381SMarcel Moolenaar 		mtx_unlock_spin(hwmtx);
1158af03381SMarcel Moolenaar }
1168af03381SMarcel Moolenaar 
11727d5dc18SMarcel Moolenaar static __inline int
uart_probe(struct uart_devinfo * di)11827d5dc18SMarcel Moolenaar uart_probe(struct uart_devinfo *di)
11927d5dc18SMarcel Moolenaar {
1208af03381SMarcel Moolenaar 	int res;
1218af03381SMarcel Moolenaar 
1228af03381SMarcel Moolenaar 	uart_lock(di->hwmtx);
123f8100ce2SMarcel Moolenaar 	res = di->ops->probe(&di->bas);
1248af03381SMarcel Moolenaar 	uart_unlock(di->hwmtx);
1258af03381SMarcel Moolenaar 	return (res);
12627d5dc18SMarcel Moolenaar }
12727d5dc18SMarcel Moolenaar 
12827d5dc18SMarcel Moolenaar static __inline void
uart_init(struct uart_devinfo * di)12927d5dc18SMarcel Moolenaar uart_init(struct uart_devinfo *di)
13027d5dc18SMarcel Moolenaar {
1318af03381SMarcel Moolenaar 	uart_lock(di->hwmtx);
132f8100ce2SMarcel Moolenaar 	di->ops->init(&di->bas, di->baudrate, di->databits, di->stopbits,
13327d5dc18SMarcel Moolenaar 	    di->parity);
1348af03381SMarcel Moolenaar 	uart_unlock(di->hwmtx);
13527d5dc18SMarcel Moolenaar }
13627d5dc18SMarcel Moolenaar 
13727d5dc18SMarcel Moolenaar static __inline void
uart_term(struct uart_devinfo * di)13827d5dc18SMarcel Moolenaar uart_term(struct uart_devinfo *di)
13927d5dc18SMarcel Moolenaar {
1408af03381SMarcel Moolenaar 	uart_lock(di->hwmtx);
141f8100ce2SMarcel Moolenaar 	di->ops->term(&di->bas);
1428af03381SMarcel Moolenaar 	uart_unlock(di->hwmtx);
14327d5dc18SMarcel Moolenaar }
14427d5dc18SMarcel Moolenaar 
14527d5dc18SMarcel Moolenaar static __inline void
uart_putc(struct uart_devinfo * di,int c)14627d5dc18SMarcel Moolenaar uart_putc(struct uart_devinfo *di, int c)
14727d5dc18SMarcel Moolenaar {
1488af03381SMarcel Moolenaar 	uart_lock(di->hwmtx);
149f8100ce2SMarcel Moolenaar 	di->ops->putc(&di->bas, c);
1508af03381SMarcel Moolenaar 	uart_unlock(di->hwmtx);
15127d5dc18SMarcel Moolenaar }
15227d5dc18SMarcel Moolenaar 
15327d5dc18SMarcel Moolenaar static __inline int
uart_rxready(struct uart_devinfo * di)15497202af2SMarius Strobl uart_rxready(struct uart_devinfo *di)
15597202af2SMarius Strobl {
15697202af2SMarius Strobl 	int res;
15797202af2SMarius Strobl 
15897202af2SMarius Strobl 	uart_lock(di->hwmtx);
159f8100ce2SMarcel Moolenaar 	res = di->ops->rxready(&di->bas);
16097202af2SMarius Strobl 	uart_unlock(di->hwmtx);
16197202af2SMarius Strobl 	return (res);
16297202af2SMarius Strobl }
16397202af2SMarius Strobl 
16497202af2SMarius Strobl static __inline int
uart_poll(struct uart_devinfo * di)16527d5dc18SMarcel Moolenaar uart_poll(struct uart_devinfo *di)
16627d5dc18SMarcel Moolenaar {
1678af03381SMarcel Moolenaar 	int res;
1688af03381SMarcel Moolenaar 
1698af03381SMarcel Moolenaar 	uart_lock(di->hwmtx);
170f8100ce2SMarcel Moolenaar 	if (di->ops->rxready(&di->bas))
171f8100ce2SMarcel Moolenaar 		res = di->ops->getc(&di->bas, NULL);
17297202af2SMarius Strobl 	else
17397202af2SMarius Strobl 		res = -1;
1748af03381SMarcel Moolenaar 	uart_unlock(di->hwmtx);
1758af03381SMarcel Moolenaar 	return (res);
17627d5dc18SMarcel Moolenaar }
17727d5dc18SMarcel Moolenaar 
17827d5dc18SMarcel Moolenaar static __inline int
uart_getc(struct uart_devinfo * di)17927d5dc18SMarcel Moolenaar uart_getc(struct uart_devinfo *di)
18027d5dc18SMarcel Moolenaar {
1818af03381SMarcel Moolenaar 
182f8100ce2SMarcel Moolenaar 	return (di->ops->getc(&di->bas, di->hwmtx));
18327d5dc18SMarcel Moolenaar }
18427d5dc18SMarcel Moolenaar 
185d76a1ef4SWarner Losh void uart_grab(struct uart_devinfo *di);
186d76a1ef4SWarner Losh void uart_ungrab(struct uart_devinfo *di);
187d76a1ef4SWarner Losh 
18827d5dc18SMarcel Moolenaar #endif /* _DEV_UART_CPU_H_ */
189