lpt_pcc.c revision 1.7 1 /* $NetBSD: lpt_pcc.c,v 1.7 2002/10/02 05:28:13 thorpej 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 <dev/mvme/lptvar.h>
52
53 #include <mvme68k/dev/lpt_pccreg.h>
54 #include <mvme68k/dev/pccreg.h>
55 #include <mvme68k/dev/pccvar.h>
56
57
58
59 static int lpt_pcc_intr __P((void *));
60 static void lpt_pcc_open __P((struct lpt_softc *, int));
61 static void lpt_pcc_close __P((struct lpt_softc *));
62 static void lpt_pcc_iprime __P((struct lpt_softc *));
63 static void lpt_pcc_speed __P((struct lpt_softc *, int));
64 static int lpt_pcc_notrdy __P((struct lpt_softc *, int));
65 static void lpt_pcc_wr_data __P((struct lpt_softc *, u_char));
66
67 struct lpt_funcs lpt_pcc_funcs = {
68 lpt_pcc_open,
69 lpt_pcc_close,
70 lpt_pcc_iprime,
71 lpt_pcc_speed,
72 lpt_pcc_notrdy,
73 lpt_pcc_wr_data
74 };
75
76 /*
77 * Autoconfig stuff
78 */
79 static int lpt_pcc_match __P((struct device *, struct cfdata *, void *));
80 static void lpt_pcc_attach __P((struct device *, struct device *, void *));
81
82 CFATTACH_DECL(lpt_pcc, sizeof(struct lpt_softc),
83 lpt_pcc_match, lpt_pcc_attach, NULL, NULL);
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 /* Register the event counter */
138 evcnt_attach_dynamic(&sc->sc_evcnt, EVCNT_TYPE_INTR,
139 pccintr_evcnt(sc->sc_ipl), "printer", sc->sc_dev.dv_xname);
140
141 /*
142 * Hook into the printer interrupt
143 */
144 pccintr_establish(PCCV_PRINTER, lpt_pcc_intr, sc->sc_ipl, sc,
145 &sc->sc_evcnt);
146 }
147
148 /*
149 * Handle printer interrupts which occur when the printer is ready to accept
150 * another char.
151 */
152 int
153 lpt_pcc_intr(arg)
154 void *arg;
155 {
156 struct lpt_softc *sc;
157 int i;
158
159 sc = arg;
160
161 /* is printer online and ready for output */
162 if (lpt_pcc_notrdy(sc, 0) && lpt_pcc_notrdy(sc, 1))
163 return 0;
164
165 i = lpt_intr(sc);
166
167 if (pcc_reg_read(sys_pcc, PCCREG_PRNT_INTR_CTRL) & LPI_ACKINT) {
168 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
169 sc->sc_icr | LPI_ACKINT);
170 }
171
172 return (i);
173 }
174
175
176 static void
177 lpt_pcc_open(sc, int_ena)
178 struct lpt_softc *sc;
179 int int_ena;
180 {
181 int sps;
182
183 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
184 LPI_ACKINT | LPI_FAULTINT);
185
186 if (int_ena == 0) {
187 sps = splhigh();
188 sc->sc_icr = sc->sc_ipl | LPI_ENABLE;
189 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, sc->sc_icr);
190 splx(sps);
191 }
192 }
193
194 static void
195 lpt_pcc_close(sc)
196 struct lpt_softc *sc;
197 {
198
199 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 0);
200 sc->sc_icr = sc->sc_ipl;
201 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, sc->sc_icr);
202 }
203
204 /* ARGSUSED */
205 static void
206 lpt_pcc_iprime(sc)
207 struct lpt_softc *sc;
208 {
209
210 lpt_control_write(LPC_INPUT_PRIME);
211 delay(100);
212 }
213
214 /* ARGSUSED */
215 static void
216 lpt_pcc_speed(sc, speed)
217 struct lpt_softc *sc;
218 int speed;
219 {
220
221 if (speed == LPT_STROBE_FAST)
222 lpt_control_write(LPC_FAST_STROBE);
223 else
224 lpt_control_write(0);
225 }
226
227 static int
228 lpt_pcc_notrdy(sc, err)
229 struct lpt_softc *sc;
230 int err;
231 {
232 u_char status;
233 u_char new;
234
235 #define LPS_INVERT (LPS_SELECT)
236 #define LPS_MASK (LPS_SELECT|LPS_FAULT|LPS_BUSY|LPS_PAPER_EMPTY)
237
238 status = (lpt_status_read(sc) ^ LPS_INVERT) & LPS_MASK;
239
240 if (err) {
241 new = status & ~sc->sc_laststatus;
242 sc->sc_laststatus = status;
243
244 if (new & LPS_SELECT)
245 log(LOG_NOTICE, "%s: offline\n",
246 sc->sc_dev.dv_xname);
247 else if (new & LPS_PAPER_EMPTY)
248 log(LOG_NOTICE, "%s: out of paper\n",
249 sc->sc_dev.dv_xname);
250 else if (new & LPS_FAULT)
251 log(LOG_NOTICE, "%s: output error\n",
252 sc->sc_dev.dv_xname);
253 }
254
255 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
256 sc->sc_icr | LPI_FAULTINT);
257
258 return (status);
259 }
260
261 static void
262 lpt_pcc_wr_data(sc, data)
263 struct lpt_softc *sc;
264 u_char data;
265 {
266
267 lpt_data_write(sc, data);
268 }
269