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