xref: /freebsd-12.1/sys/dev/uart/uart_cpu.h (revision d76a1ef4)
1098ca2bdSWarner Losh /*-
22ae4f1fdSMarcel Moolenaar  * Copyright (c) 2003, 2004 Marcel Moolenaar
327d5dc18SMarcel Moolenaar  * All rights reserved.
427d5dc18SMarcel Moolenaar  *
527d5dc18SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
627d5dc18SMarcel Moolenaar  * modification, are permitted provided that the following conditions
727d5dc18SMarcel Moolenaar  * are met:
827d5dc18SMarcel Moolenaar  *
927d5dc18SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
1027d5dc18SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
1127d5dc18SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
1227d5dc18SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
1327d5dc18SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
1427d5dc18SMarcel Moolenaar  *
1527d5dc18SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1627d5dc18SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1727d5dc18SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1827d5dc18SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1927d5dc18SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2027d5dc18SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2127d5dc18SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2227d5dc18SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2327d5dc18SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2427d5dc18SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2527d5dc18SMarcel Moolenaar  *
2627d5dc18SMarcel Moolenaar  * $FreeBSD$
2727d5dc18SMarcel Moolenaar  */
2827d5dc18SMarcel Moolenaar 
2927d5dc18SMarcel Moolenaar #ifndef _DEV_UART_CPU_H_
3027d5dc18SMarcel Moolenaar #define _DEV_UART_CPU_H_
3127d5dc18SMarcel Moolenaar 
328af03381SMarcel Moolenaar #include <sys/kdb.h>
338af03381SMarcel Moolenaar #include <sys/lock.h>
348af03381SMarcel Moolenaar #include <sys/mutex.h>
358af03381SMarcel Moolenaar 
36*d76a1ef4SWarner Losh struct uart_softc;
37*d76a1ef4SWarner Losh 
3827d5dc18SMarcel Moolenaar /*
3927d5dc18SMarcel Moolenaar  * Low-level operations for use by console and/or debug port support.
4027d5dc18SMarcel Moolenaar  */
4127d5dc18SMarcel Moolenaar struct uart_ops {
4227d5dc18SMarcel Moolenaar 	int (*probe)(struct uart_bas *);
4327d5dc18SMarcel Moolenaar 	void (*init)(struct uart_bas *, int, int, int, int);
4427d5dc18SMarcel Moolenaar 	void (*term)(struct uart_bas *);
4527d5dc18SMarcel Moolenaar 	void (*putc)(struct uart_bas *, int);
4697202af2SMarius Strobl 	int (*rxready)(struct uart_bas *);
47634e63c9SMarcel Moolenaar 	int (*getc)(struct uart_bas *, struct mtx *);
4827d5dc18SMarcel Moolenaar };
4927d5dc18SMarcel Moolenaar 
502ae4f1fdSMarcel Moolenaar extern bus_space_tag_t uart_bus_space_io;
512ae4f1fdSMarcel Moolenaar extern bus_space_tag_t uart_bus_space_mem;
522ae4f1fdSMarcel Moolenaar 
5327d5dc18SMarcel Moolenaar /*
5427d5dc18SMarcel Moolenaar  * Console and debug port device info.
5527d5dc18SMarcel Moolenaar  */
5627d5dc18SMarcel Moolenaar struct uart_devinfo {
5727d5dc18SMarcel Moolenaar 	SLIST_ENTRY(uart_devinfo) next;
58f8100ce2SMarcel Moolenaar 	struct uart_ops *ops;
5927d5dc18SMarcel Moolenaar 	struct uart_bas bas;
6027d5dc18SMarcel Moolenaar 	int	baudrate;
6127d5dc18SMarcel Moolenaar 	int	databits;
6227d5dc18SMarcel Moolenaar 	int	stopbits;
6327d5dc18SMarcel Moolenaar 	int	parity;
6427d5dc18SMarcel Moolenaar 	int	type;
6527d5dc18SMarcel Moolenaar #define	UART_DEV_CONSOLE	0
6627d5dc18SMarcel Moolenaar #define	UART_DEV_DBGPORT	1
6727d5dc18SMarcel Moolenaar #define	UART_DEV_KEYBOARD	2
6827d5dc18SMarcel Moolenaar 	int	(*attach)(struct uart_softc*);
6927d5dc18SMarcel Moolenaar 	int	(*detach)(struct uart_softc*);
7027d5dc18SMarcel Moolenaar 	void	*cookie;		/* Type dependent use. */
718af03381SMarcel Moolenaar 	struct mtx *hwmtx;
72*d76a1ef4SWarner Losh 	struct uart_softc *sc;		/* valid only from start of attach */
7327d5dc18SMarcel Moolenaar };
7427d5dc18SMarcel Moolenaar 
755cc70551SMarcel Moolenaar int uart_cpu_eqres(struct uart_bas *, struct uart_bas *);
765cc70551SMarcel Moolenaar int uart_cpu_getdev(int, struct uart_devinfo *);
77f8100ce2SMarcel Moolenaar 
78f8100ce2SMarcel Moolenaar int uart_getenv(int, struct uart_devinfo *, struct uart_class *);
79f8100ce2SMarcel Moolenaar const char *uart_getname(struct uart_class *);
80f8100ce2SMarcel Moolenaar struct uart_ops *uart_getops(struct uart_class *);
81f8100ce2SMarcel Moolenaar int uart_getrange(struct uart_class *);
8227d5dc18SMarcel Moolenaar 
8327d5dc18SMarcel Moolenaar void uart_add_sysdev(struct uart_devinfo *);
8427d5dc18SMarcel Moolenaar 
8527d5dc18SMarcel Moolenaar /*
8627d5dc18SMarcel Moolenaar  * Operations for low-level access to the UART. Primarily for use
8727d5dc18SMarcel Moolenaar  * by console and debug port logic.
8827d5dc18SMarcel Moolenaar  */
898af03381SMarcel Moolenaar 
908af03381SMarcel Moolenaar static __inline void
918af03381SMarcel Moolenaar uart_lock(struct mtx *hwmtx)
928af03381SMarcel Moolenaar {
938af03381SMarcel Moolenaar 	if (!kdb_active && hwmtx != NULL)
948af03381SMarcel Moolenaar 		mtx_lock_spin(hwmtx);
958af03381SMarcel Moolenaar }
968af03381SMarcel Moolenaar 
978af03381SMarcel Moolenaar static __inline void
988af03381SMarcel Moolenaar uart_unlock(struct mtx *hwmtx)
998af03381SMarcel Moolenaar {
1008af03381SMarcel Moolenaar 	if (!kdb_active && hwmtx != NULL)
1018af03381SMarcel Moolenaar 		mtx_unlock_spin(hwmtx);
1028af03381SMarcel Moolenaar }
1038af03381SMarcel Moolenaar 
10427d5dc18SMarcel Moolenaar static __inline int
10527d5dc18SMarcel Moolenaar uart_probe(struct uart_devinfo *di)
10627d5dc18SMarcel Moolenaar {
1078af03381SMarcel Moolenaar 	int res;
1088af03381SMarcel Moolenaar 
1098af03381SMarcel Moolenaar 	uart_lock(di->hwmtx);
110f8100ce2SMarcel Moolenaar 	res = di->ops->probe(&di->bas);
1118af03381SMarcel Moolenaar 	uart_unlock(di->hwmtx);
1128af03381SMarcel Moolenaar 	return (res);
11327d5dc18SMarcel Moolenaar }
11427d5dc18SMarcel Moolenaar 
11527d5dc18SMarcel Moolenaar static __inline void
11627d5dc18SMarcel Moolenaar uart_init(struct uart_devinfo *di)
11727d5dc18SMarcel Moolenaar {
1188af03381SMarcel Moolenaar 	uart_lock(di->hwmtx);
119f8100ce2SMarcel Moolenaar 	di->ops->init(&di->bas, di->baudrate, di->databits, di->stopbits,
12027d5dc18SMarcel Moolenaar 	    di->parity);
1218af03381SMarcel Moolenaar 	uart_unlock(di->hwmtx);
12227d5dc18SMarcel Moolenaar }
12327d5dc18SMarcel Moolenaar 
12427d5dc18SMarcel Moolenaar static __inline void
12527d5dc18SMarcel Moolenaar uart_term(struct uart_devinfo *di)
12627d5dc18SMarcel Moolenaar {
1278af03381SMarcel Moolenaar 	uart_lock(di->hwmtx);
128f8100ce2SMarcel Moolenaar 	di->ops->term(&di->bas);
1298af03381SMarcel Moolenaar 	uart_unlock(di->hwmtx);
13027d5dc18SMarcel Moolenaar }
13127d5dc18SMarcel Moolenaar 
13227d5dc18SMarcel Moolenaar static __inline void
13327d5dc18SMarcel Moolenaar uart_putc(struct uart_devinfo *di, int c)
13427d5dc18SMarcel Moolenaar {
1358af03381SMarcel Moolenaar 	uart_lock(di->hwmtx);
136f8100ce2SMarcel Moolenaar 	di->ops->putc(&di->bas, c);
1378af03381SMarcel Moolenaar 	uart_unlock(di->hwmtx);
13827d5dc18SMarcel Moolenaar }
13927d5dc18SMarcel Moolenaar 
14027d5dc18SMarcel Moolenaar static __inline int
14197202af2SMarius Strobl uart_rxready(struct uart_devinfo *di)
14297202af2SMarius Strobl {
14397202af2SMarius Strobl 	int res;
14497202af2SMarius Strobl 
14597202af2SMarius Strobl 	uart_lock(di->hwmtx);
146f8100ce2SMarcel Moolenaar 	res = di->ops->rxready(&di->bas);
14797202af2SMarius Strobl 	uart_unlock(di->hwmtx);
14897202af2SMarius Strobl 	return (res);
14997202af2SMarius Strobl }
15097202af2SMarius Strobl 
15197202af2SMarius Strobl static __inline int
15227d5dc18SMarcel Moolenaar uart_poll(struct uart_devinfo *di)
15327d5dc18SMarcel Moolenaar {
1548af03381SMarcel Moolenaar 	int res;
1558af03381SMarcel Moolenaar 
1568af03381SMarcel Moolenaar 	uart_lock(di->hwmtx);
157f8100ce2SMarcel Moolenaar 	if (di->ops->rxready(&di->bas))
158f8100ce2SMarcel Moolenaar 		res = di->ops->getc(&di->bas, NULL);
15997202af2SMarius Strobl 	else
16097202af2SMarius Strobl 		res = -1;
1618af03381SMarcel Moolenaar 	uart_unlock(di->hwmtx);
1628af03381SMarcel Moolenaar 	return (res);
16327d5dc18SMarcel Moolenaar }
16427d5dc18SMarcel Moolenaar 
16527d5dc18SMarcel Moolenaar static __inline int
16627d5dc18SMarcel Moolenaar uart_getc(struct uart_devinfo *di)
16727d5dc18SMarcel Moolenaar {
1688af03381SMarcel Moolenaar 
169f8100ce2SMarcel Moolenaar 	return (di->ops->getc(&di->bas, di->hwmtx));
17027d5dc18SMarcel Moolenaar }
17127d5dc18SMarcel Moolenaar 
172*d76a1ef4SWarner Losh void uart_grab(struct uart_devinfo *di);
173*d76a1ef4SWarner Losh void uart_ungrab(struct uart_devinfo *di);
174*d76a1ef4SWarner Losh 
17527d5dc18SMarcel Moolenaar #endif /* _DEV_UART_CPU_H_ */
176