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