lpt_pcc.c revision 1.3 1 /* $NetBSD: lpt_pcc.c,v 1.3 2000/03/18 22:33:03 scw Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Steve C. Woodford.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Device Driver back-end for the MVME147's parallel printer port
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/device.h>
47 #include <sys/syslog.h>
48
49 #include <machine/bus.h>
50
51 #include <mvme68k/dev/lptvar.h>
52 #include <mvme68k/dev/lpt_pccreg.h>
53 #include <mvme68k/dev/pccreg.h>
54 #include <mvme68k/dev/pccvar.h>
55
56
57
58 static int lpt_pcc_intr __P((void *));
59 static void lpt_pcc_open __P((struct lpt_softc *, int));
60 static void lpt_pcc_close __P((struct lpt_softc *));
61 static void lpt_pcc_iprime __P((struct lpt_softc *));
62 static void lpt_pcc_speed __P((struct lpt_softc *, int));
63 static int lpt_pcc_notrdy __P((struct lpt_softc *, int));
64 static void lpt_pcc_wr_data __P((struct lpt_softc *, u_char));
65
66 struct lpt_funcs lpt_pcc_funcs = {
67 lpt_pcc_open,
68 lpt_pcc_close,
69 lpt_pcc_iprime,
70 lpt_pcc_speed,
71 lpt_pcc_notrdy,
72 lpt_pcc_wr_data
73 };
74
75 /*
76 * Autoconfig stuff
77 */
78 static int lpt_pcc_match __P((struct device *, struct cfdata *, void *));
79 static void lpt_pcc_attach __P((struct device *, struct device *, void *));
80
81 struct cfattach lpt_pcc_ca = {
82 sizeof(struct lpt_softc), lpt_pcc_match, lpt_pcc_attach
83 };
84
85 extern struct cfdriver lpt_cd;
86
87
88 /*ARGSUSED*/
89 static int
90 lpt_pcc_match(parent, cf, args)
91 struct device *parent;
92 struct cfdata *cf;
93 void *args;
94 {
95 struct pcc_attach_args *pa;
96
97 pa = args;
98
99 if (strcmp(pa->pa_name, lpt_cd.cd_name))
100 return (0);
101
102 pa->pa_ipl = cf->pcccf_ipl;
103 return (1);
104 }
105
106 /*ARGSUSED*/
107 static void
108 lpt_pcc_attach(parent, self, args)
109 struct device *parent, *self;
110 void *args;
111 {
112 struct lpt_softc *sc;
113 struct pcc_attach_args *pa;
114
115 sc = (struct lpt_softc *) self;
116 pa = args;
117
118 sc->sc_bust = pa->pa_bust;
119 bus_space_map(pa->pa_bust, pa->pa_offset, LPREG_SIZE, 0, &sc->sc_bush);
120
121 sc->sc_ipl = pa->pa_ipl & PCC_IMASK;
122 sc->sc_funcs = &lpt_pcc_funcs;
123 sc->sc_laststatus = 0;
124
125 printf(": PCC Parallel Printer\n");
126
127 /*
128 * Disable interrupts until device is opened
129 */
130 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 0);
131
132 /*
133 * Main attachment code
134 */
135 lpt_attach_subr(sc);
136
137 /*
138 * Hook into the printer interrupt
139 */
140 pccintr_establish(PCCV_PRINTER, lpt_pcc_intr, sc->sc_ipl, sc);
141 }
142
143 /*
144 * Handle printer interrupts which occur when the printer is ready to accept
145 * another char.
146 */
147 int
148 lpt_pcc_intr(arg)
149 void *arg;
150 {
151 struct lpt_softc *sc;
152 int i;
153
154 sc = arg;
155
156 /* is printer online and ready for output */
157 if (lpt_pcc_notrdy(sc, 0) && lpt_pcc_notrdy(sc, 1))
158 return 0;
159
160 i = lpt_intr(sc);
161
162 if (pcc_reg_read(sys_pcc, PCCREG_PRNT_INTR_CTRL) & LPI_ACKINT) {
163 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
164 sc->sc_icr | LPI_ACKINT);
165 }
166
167 return (i);
168 }
169
170
171 static void
172 lpt_pcc_open(sc, int_ena)
173 struct lpt_softc *sc;
174 int int_ena;
175 {
176 int sps;
177
178 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
179 LPI_ACKINT | LPI_FAULTINT);
180
181 if (int_ena == 0) {
182 sps = splhigh();
183 sc->sc_icr = sc->sc_ipl | LPI_ENABLE;
184 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, sc->sc_icr);
185 splx(sps);
186 }
187 }
188
189 static void
190 lpt_pcc_close(sc)
191 struct lpt_softc *sc;
192 {
193
194 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 0);
195 sc->sc_icr = sc->sc_ipl;
196 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, sc->sc_icr);
197 }
198
199 /* ARGSUSED */
200 static void
201 lpt_pcc_iprime(sc)
202 struct lpt_softc *sc;
203 {
204
205 lpt_control_write(LPC_INPUT_PRIME);
206 delay(100);
207 }
208
209 /* ARGSUSED */
210 static void
211 lpt_pcc_speed(sc, speed)
212 struct lpt_softc *sc;
213 int speed;
214 {
215
216 if (speed == LPT_STROBE_FAST)
217 lpt_control_write(LPC_FAST_STROBE);
218 else
219 lpt_control_write(0);
220 }
221
222 static int
223 lpt_pcc_notrdy(sc, err)
224 struct lpt_softc *sc;
225 int err;
226 {
227 u_char status;
228 u_char new;
229
230 #define LPS_INVERT (LPS_SELECT)
231 #define LPS_MASK (LPS_SELECT|LPS_FAULT|LPS_BUSY|LPS_PAPER_EMPTY)
232
233 status = (lpt_status_read(sc) ^ LPS_INVERT) & LPS_MASK;
234
235 if (err) {
236 new = status & ~sc->sc_laststatus;
237 sc->sc_laststatus = status;
238
239 if (new & LPS_SELECT)
240 log(LOG_NOTICE, "%s: offline\n",
241 sc->sc_dev.dv_xname);
242 else if (new & LPS_PAPER_EMPTY)
243 log(LOG_NOTICE, "%s: out of paper\n",
244 sc->sc_dev.dv_xname);
245 else if (new & LPS_FAULT)
246 log(LOG_NOTICE, "%s: output error\n",
247 sc->sc_dev.dv_xname);
248 }
249
250 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
251 sc->sc_icr | LPI_FAULTINT);
252
253 return (status);
254 }
255
256 static void
257 lpt_pcc_wr_data(sc, data)
258 struct lpt_softc *sc;
259 u_char data;
260 {
261
262 lpt_data_write(sc, data);
263 }
264