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