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