ppb_base.c revision 187576
1/*-
2 * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
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 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/ppbus/ppb_base.c 187576 2009-01-21 23:10:06Z jhb $");
29
30#include <sys/param.h>
31#include <sys/lock.h>
32#include <sys/kernel.h>
33#include <sys/module.h>
34#include <sys/mutex.h>
35#include <sys/systm.h>
36#include <sys/bus.h>
37
38#include <dev/ppbus/ppbconf.h>
39
40#include "ppbus_if.h"
41
42#include <dev/ppbus/ppbio.h>
43
44MODULE_VERSION(ppbus, 1);
45
46#define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
47
48/*
49 * ppb_poll_bus()
50 *
51 * Polls the bus
52 *
53 * max is a delay in 10-milliseconds
54 */
55int
56ppb_poll_bus(device_t bus, int max,
57	     char mask, char status, int how)
58{
59	struct ppb_data *ppb = DEVTOSOFTC(bus);
60	int i, j, error;
61	char r;
62
63	mtx_assert(ppb->ppc_lock, MA_OWNED);
64
65	/* try at least up to 10ms */
66	for (j = 0; j < ((how & PPB_POLL) ? max : 1); j++) {
67		for (i = 0; i < 10000; i++) {
68			r = ppb_rstr(bus);
69			DELAY(1);
70			if ((r & mask) == status)
71				return (0);
72		}
73	}
74
75	if (!(how & PPB_POLL)) {
76	   for (i = 0; max == PPB_FOREVER || i < max-1; i++) {
77		if ((ppb_rstr(bus) & mask) == status)
78			return (0);
79
80		/* wait 10 ms */
81		error = mtx_sleep((caddr_t)bus, ppb->ppc_lock, PPBPRI |
82		    (how == PPB_NOINTR ? 0 : PCATCH), "ppbpoll", hz/100);
83		if (error != EWOULDBLOCK)
84			return (error);
85	   }
86	}
87
88	return (EWOULDBLOCK);
89}
90
91/*
92 * ppb_get_epp_protocol()
93 *
94 * Return the chipset EPP protocol
95 */
96int
97ppb_get_epp_protocol(device_t bus)
98{
99#ifdef INVARIANTS
100	struct ppb_data *ppb = DEVTOSOFTC(bus);
101#endif
102	uintptr_t protocol;
103
104	mtx_assert(ppb->ppc_lock, MA_OWNED);
105	BUS_READ_IVAR(device_get_parent(bus), bus, PPC_IVAR_EPP_PROTO, &protocol);
106
107	return (protocol);
108}
109
110/*
111 * ppb_get_mode()
112 *
113 */
114int
115ppb_get_mode(device_t bus)
116{
117	struct ppb_data *ppb = DEVTOSOFTC(bus);
118
119	/* XXX yet device mode = ppbus mode = chipset mode */
120	mtx_assert(ppb->ppc_lock, MA_OWNED);
121	return (ppb->mode);
122}
123
124/*
125 * ppb_set_mode()
126 *
127 * Set the operating mode of the chipset, return the previous mode
128 */
129int
130ppb_set_mode(device_t bus, int mode)
131{
132	struct ppb_data *ppb = DEVTOSOFTC(bus);
133	int old_mode = ppb_get_mode(bus);
134
135	mtx_assert(ppb->ppc_lock, MA_OWNED);
136	if (PPBUS_SETMODE(device_get_parent(bus), mode))
137		return (-1);
138
139	/* XXX yet device mode = ppbus mode = chipset mode */
140	ppb->mode = (mode & PPB_MASK);
141
142	return (old_mode);
143}
144
145/*
146 * ppb_write()
147 *
148 * Write charaters to the port
149 */
150int
151ppb_write(device_t bus, char *buf, int len, int how)
152{
153#ifdef INVARIANTS
154	struct ppb_data *ppb = DEVTOSOFTC(bus);
155#endif
156
157	mtx_assert(ppb->ppc_lock, MA_OWNED);
158	return (PPBUS_WRITE(device_get_parent(bus), buf, len, how));
159}
160
161/*
162 * ppb_reset_epp_timeout()
163 *
164 * Reset the EPP timeout bit in the status register
165 */
166int
167ppb_reset_epp_timeout(device_t bus)
168{
169#ifdef INVARIANTS
170	struct ppb_data *ppb = DEVTOSOFTC(bus);
171#endif
172
173	mtx_assert(ppb->ppc_lock, MA_OWNED);
174	return(PPBUS_RESET_EPP(device_get_parent(bus)));
175}
176
177/*
178 * ppb_ecp_sync()
179 *
180 * Wait for the ECP FIFO to be empty
181 */
182int
183ppb_ecp_sync(device_t bus)
184{
185#ifdef INVARIANTS
186	struct ppb_data *ppb = DEVTOSOFTC(bus);
187#endif
188
189	mtx_assert(ppb->ppc_lock, MA_OWNED);
190	return (PPBUS_ECP_SYNC(device_get_parent(bus)));
191}
192
193/*
194 * ppb_get_status()
195 *
196 * Read the status register and update the status info
197 */
198int
199ppb_get_status(device_t bus, struct ppb_status *status)
200{
201#ifdef INVARIANTS
202	struct ppb_data *ppb = DEVTOSOFTC(bus);
203#endif
204	register char r;
205
206	mtx_assert(ppb->ppc_lock, MA_OWNED);
207
208	r = status->status = ppb_rstr(bus);
209
210	status->timeout	= r & TIMEOUT;
211	status->error	= !(r & nFAULT);
212	status->select	= r & SELECT;
213	status->paper_end = r & PERROR;
214	status->ack	= !(r & nACK);
215	status->busy	= !(r & nBUSY);
216
217	return (0);
218}
219
220void
221ppb_lock(device_t bus)
222{
223	struct ppb_data *ppb = DEVTOSOFTC(bus);
224
225	mtx_lock(ppb->ppc_lock);
226}
227
228void
229ppb_unlock(device_t bus)
230{
231	struct ppb_data *ppb = DEVTOSOFTC(bus);
232
233	mtx_unlock(ppb->ppc_lock);
234}
235
236void
237_ppb_assert_locked(device_t bus, const char *file, int line)
238{
239#ifdef INVARIANTS
240	struct ppb_data *ppb = DEVTOSOFTC(bus);
241
242	_mtx_assert(ppb->ppc_lock, MA_OWNED, file, line);
243#endif
244}
245
246void
247ppb_init_callout(device_t bus, struct callout *c, int flags)
248{
249	struct ppb_data *ppb = DEVTOSOFTC(bus);
250
251	callout_init_mtx(c, ppb->ppc_lock, flags);
252}
253
254int
255ppb_sleep(device_t bus, void *wchan, int priority, const char *wmesg, int timo)
256{
257	struct ppb_data *ppb = DEVTOSOFTC(bus);
258
259	return (mtx_sleep(wchan, ppb->ppc_lock, priority, wmesg, timo));
260}
261