xref: /freebsd/sys/dev/uart/uart_cpu.h (revision 7bd6fde3)
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 /*
37  * Low-level operations for use by console and/or debug port support.
38  */
39 struct uart_ops {
40 	int (*probe)(struct uart_bas *);
41 	void (*init)(struct uart_bas *, int, int, int, int);
42 	void (*term)(struct uart_bas *);
43 	void (*putc)(struct uart_bas *, int);
44 	int (*rxready)(struct uart_bas *);
45 	int (*getc)(struct uart_bas *, struct mtx *);
46 };
47 
48 extern struct uart_ops uart_i8251_ops;
49 extern struct uart_ops uart_ns8250_ops;
50 extern struct uart_ops uart_sab82532_ops;
51 extern struct uart_ops uart_z8530_ops;
52 
53 extern bus_space_tag_t uart_bus_space_io;
54 extern bus_space_tag_t uart_bus_space_mem;
55 
56 /*
57  * Console and debug port device info.
58  */
59 struct uart_softc;
60 struct uart_devinfo {
61 	SLIST_ENTRY(uart_devinfo) next;
62 	struct uart_ops ops;
63 	struct uart_bas bas;
64 	int	baudrate;
65 	int	databits;
66 	int	stopbits;
67 	int	parity;
68 	int	type;
69 #define	UART_DEV_CONSOLE	0
70 #define	UART_DEV_DBGPORT	1
71 #define	UART_DEV_KEYBOARD	2
72 	int	(*attach)(struct uart_softc*);
73 	int	(*detach)(struct uart_softc*);
74 	void	*cookie;		/* Type dependent use. */
75 	struct mtx *hwmtx;
76 };
77 
78 int uart_cpu_eqres(struct uart_bas *, struct uart_bas *);
79 int uart_cpu_getdev(int, struct uart_devinfo *);
80 int uart_getenv(int, struct uart_devinfo *);
81 
82 void uart_add_sysdev(struct uart_devinfo *);
83 
84 /*
85  * Operations for low-level access to the UART. Primarily for use
86  * by console and debug port logic.
87  */
88 
89 static __inline void
90 uart_lock(struct mtx *hwmtx)
91 {
92 	if (!kdb_active && hwmtx != NULL)
93 		mtx_lock_spin(hwmtx);
94 }
95 
96 static __inline void
97 uart_unlock(struct mtx *hwmtx)
98 {
99 	if (!kdb_active && hwmtx != NULL)
100 		mtx_unlock_spin(hwmtx);
101 }
102 
103 static __inline int
104 uart_probe(struct uart_devinfo *di)
105 {
106 	int res;
107 
108 	uart_lock(di->hwmtx);
109 	res = di->ops.probe(&di->bas);
110 	uart_unlock(di->hwmtx);
111 	return (res);
112 }
113 
114 static __inline void
115 uart_init(struct uart_devinfo *di)
116 {
117 	uart_lock(di->hwmtx);
118 	di->ops.init(&di->bas, di->baudrate, di->databits, di->stopbits,
119 	    di->parity);
120 	uart_unlock(di->hwmtx);
121 }
122 
123 static __inline void
124 uart_term(struct uart_devinfo *di)
125 {
126 	uart_lock(di->hwmtx);
127 	di->ops.term(&di->bas);
128 	uart_unlock(di->hwmtx);
129 }
130 
131 static __inline void
132 uart_putc(struct uart_devinfo *di, int c)
133 {
134 	uart_lock(di->hwmtx);
135 	di->ops.putc(&di->bas, c);
136 	uart_unlock(di->hwmtx);
137 }
138 
139 static __inline int
140 uart_rxready(struct uart_devinfo *di)
141 {
142 	int res;
143 
144 	uart_lock(di->hwmtx);
145 	res = di->ops.rxready(&di->bas);
146 	uart_unlock(di->hwmtx);
147 	return (res);
148 }
149 
150 static __inline int
151 uart_poll(struct uart_devinfo *di)
152 {
153 	int res;
154 
155 	uart_lock(di->hwmtx);
156 	if (di->ops.rxready(&di->bas))
157 		res = di->ops.getc(&di->bas, NULL);
158 	else
159 		res = -1;
160 	uart_unlock(di->hwmtx);
161 	return (res);
162 }
163 
164 static __inline int
165 uart_getc(struct uart_devinfo *di)
166 {
167 
168 	return (di->ops.getc(&di->bas, di->hwmtx));
169 }
170 
171 #endif /* _DEV_UART_CPU_H_ */
172