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