ppb_base.c revision 67164
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 * $FreeBSD: head/sys/dev/ppbus/ppb_base.c 67164 2000-10-15 14:19:01Z phk $
27 *
28 */
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/kernel.h>
32#include <sys/module.h>
33#include <sys/bus.h>
34
35
36#include <dev/ppbus/ppbconf.h>
37
38#include "ppbus_if.h"
39
40#include <dev/ppbus/ppbio.h>
41
42#define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
43
44/*
45 * ppb_poll_bus()
46 *
47 * Polls the bus
48 *
49 * max is a delay in 10-milliseconds
50 */
51int
52ppb_poll_bus(device_t bus, int max,
53	     char mask, char status, int how)
54{
55	int i, j, error;
56	char r;
57
58	/* try at least up to 10ms */
59	for (j = 0; j < ((how & PPB_POLL) ? max : 1); j++) {
60		for (i = 0; i < 10000; i++) {
61			r = ppb_rstr(bus);
62			DELAY(1);
63			if ((r & mask) == status)
64				return (0);
65		}
66	}
67
68	if (!(how & PPB_POLL)) {
69	   for (i = 0; max == PPB_FOREVER || i < max-1; i++) {
70		if ((ppb_rstr(bus) & mask) == status)
71			return (0);
72
73		switch (how) {
74		case PPB_NOINTR:
75			/* wait 10 ms */
76			tsleep((caddr_t)bus, PPBPRI, "ppbpoll", hz/100);
77			break;
78
79		case PPB_INTR:
80		default:
81			/* wait 10 ms */
82			if (((error = tsleep((caddr_t)bus, PPBPRI | PCATCH,
83			    "ppbpoll", hz/100)) != EWOULDBLOCK) != 0) {
84				return (error);
85			}
86			break;
87		}
88	   }
89	}
90
91	return (EWOULDBLOCK);
92}
93
94/*
95 * ppb_get_epp_protocol()
96 *
97 * Return the chipset EPP protocol
98 */
99int
100ppb_get_epp_protocol(device_t bus)
101{
102	uintptr_t protocol;
103
104	BUS_READ_IVAR(device_get_parent(bus), bus, PPC_IVAR_EPP_PROTO, &protocol);
105
106	return (protocol);
107}
108
109/*
110 * ppb_get_mode()
111 *
112 */
113int
114ppb_get_mode(device_t bus)
115{
116	struct ppb_data *ppb = DEVTOSOFTC(bus);
117
118	/* XXX yet device mode = ppbus mode = chipset mode */
119	return (ppb->mode);
120}
121
122/*
123 * ppb_set_mode()
124 *
125 * Set the operating mode of the chipset, return the previous mode
126 */
127int
128ppb_set_mode(device_t bus, int mode)
129{
130	struct ppb_data *ppb = DEVTOSOFTC(bus);
131	int old_mode = ppb_get_mode(bus);
132
133	if (PPBUS_SETMODE(device_get_parent(bus), mode))
134		return -1;
135
136	/* XXX yet device mode = ppbus mode = chipset mode */
137	ppb->mode = (mode & PPB_MASK);
138
139	return (old_mode);
140}
141
142/*
143 * ppb_write()
144 *
145 * Write charaters to the port
146 */
147int
148ppb_write(device_t bus, char *buf, int len, int how)
149{
150	return (PPBUS_WRITE(device_get_parent(bus), buf, len, how));
151}
152
153/*
154 * ppb_reset_epp_timeout()
155 *
156 * Reset the EPP timeout bit in the status register
157 */
158int
159ppb_reset_epp_timeout(device_t bus)
160{
161	return(PPBUS_RESET_EPP(device_get_parent(bus)));
162}
163
164/*
165 * ppb_ecp_sync()
166 *
167 * Wait for the ECP FIFO to be empty
168 */
169int
170ppb_ecp_sync(device_t bus)
171{
172	return (PPBUS_ECP_SYNC(device_get_parent(bus)));
173}
174
175/*
176 * ppb_get_status()
177 *
178 * Read the status register and update the status info
179 */
180int
181ppb_get_status(device_t bus, struct ppb_status *status)
182{
183	register char r;
184
185	r = status->status = ppb_rstr(bus);
186
187	status->timeout	= r & TIMEOUT;
188	status->error	= !(r & nFAULT);
189	status->select	= r & SELECT;
190	status->paper_end = r & PERROR;
191	status->ack	= !(r & nACK);
192	status->busy	= !(r & nBUSY);
193
194	return (0);
195}
196