lpt_pcc.c revision 1.11 1 /* $NetBSD: lpt_pcc.c,v 1.11 2008/03/07 17:15:51 cube 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/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: lpt_pcc.c,v 1.11 2008/03/07 17:15:51 cube Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/device.h>
50 #include <sys/syslog.h>
51
52 #include <machine/bus.h>
53
54 #include <dev/mvme/lptvar.h>
55
56 #include <mvme68k/dev/lpt_pccreg.h>
57 #include <mvme68k/dev/pccreg.h>
58 #include <mvme68k/dev/pccvar.h>
59
60 #include "ioconf.h"
61
62
63 static int lpt_pcc_intr(void *);
64 static void lpt_pcc_open(struct lpt_softc *, int);
65 static void lpt_pcc_close(struct lpt_softc *);
66 static void lpt_pcc_iprime(struct lpt_softc *);
67 static void lpt_pcc_speed(struct lpt_softc *, int);
68 static int lpt_pcc_notrdy(struct lpt_softc *, int);
69 static void lpt_pcc_wr_data(struct lpt_softc *, u_char);
70
71 struct lpt_funcs lpt_pcc_funcs = {
72 lpt_pcc_open,
73 lpt_pcc_close,
74 lpt_pcc_iprime,
75 lpt_pcc_speed,
76 lpt_pcc_notrdy,
77 lpt_pcc_wr_data
78 };
79
80 /*
81 * Autoconfig stuff
82 */
83 static int lpt_pcc_match(device_t, cfdata_t , void *);
84 static void lpt_pcc_attach(device_t, device_t, void *);
85
86 CFATTACH_DECL_NEW(lpt_pcc, sizeof(struct lpt_softc),
87 lpt_pcc_match, lpt_pcc_attach, NULL, NULL);
88
89
90 /*ARGSUSED*/
91 static int
92 lpt_pcc_match(device_t parent, cfdata_t cf, void *args)
93 {
94 struct pcc_attach_args *pa;
95
96 pa = args;
97
98 if (strcmp(pa->pa_name, lpt_cd.cd_name))
99 return 0;
100
101 pa->pa_ipl = cf->pcccf_ipl;
102 return 1;
103 }
104
105 /*ARGSUSED*/
106 static void
107 lpt_pcc_attach(device_t parent, device_t self, void *args)
108 {
109 struct lpt_softc *sc;
110 struct pcc_attach_args *pa;
111
112 sc = device_private(self);
113 sc->sc_dev = self;
114 pa = args;
115
116 sc->sc_bust = pa->pa_bust;
117 bus_space_map(pa->pa_bust, pa->pa_offset, LPREG_SIZE, 0, &sc->sc_bush);
118
119 sc->sc_ipl = pa->pa_ipl & PCC_IMASK;
120 sc->sc_funcs = &lpt_pcc_funcs;
121 sc->sc_laststatus = 0;
122
123 aprint_normal(": PCC Parallel Printer\n");
124
125 /*
126 * Disable interrupts until device is opened
127 */
128 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 0);
129
130 /*
131 * Main attachment code
132 */
133 lpt_attach_subr(sc);
134
135 /* Register the event counter */
136 evcnt_attach_dynamic(&sc->sc_evcnt, EVCNT_TYPE_INTR,
137 pccintr_evcnt(sc->sc_ipl), "printer", device_xname(sc->sc_dev));
138
139 /*
140 * Hook into the printer interrupt
141 */
142 pccintr_establish(PCCV_PRINTER, lpt_pcc_intr, sc->sc_ipl, sc,
143 &sc->sc_evcnt);
144 }
145
146 /*
147 * Handle printer interrupts which occur when the printer is ready to accept
148 * another char.
149 */
150 int
151 lpt_pcc_intr(void *arg)
152 {
153 struct lpt_softc *sc;
154 int i;
155
156 sc = arg;
157
158 /* is printer online and ready for output */
159 if (lpt_pcc_notrdy(sc, 0) && lpt_pcc_notrdy(sc, 1))
160 return 0;
161
162 i = lpt_intr(sc);
163
164 if (pcc_reg_read(sys_pcc, PCCREG_PRNT_INTR_CTRL) & LPI_ACKINT) {
165 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
166 sc->sc_icr | LPI_ACKINT);
167 }
168
169 return i;
170 }
171
172
173 static void
174 lpt_pcc_open(struct lpt_softc *sc, 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(struct lpt_softc *sc)
191 {
192
193 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 0);
194 sc->sc_icr = sc->sc_ipl;
195 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, sc->sc_icr);
196 }
197
198 /* ARGSUSED */
199 static void
200 lpt_pcc_iprime(struct lpt_softc *sc)
201 {
202
203 lpt_control_write(LPC_INPUT_PRIME);
204 delay(100);
205 }
206
207 /* ARGSUSED */
208 static void
209 lpt_pcc_speed(struct lpt_softc *sc, int speed)
210 {
211
212 if (speed == LPT_STROBE_FAST)
213 lpt_control_write(LPC_FAST_STROBE);
214 else
215 lpt_control_write(0);
216 }
217
218 static int
219 lpt_pcc_notrdy(struct lpt_softc *sc, int err)
220 {
221 u_char status;
222 u_char new;
223
224 #define LPS_INVERT (LPS_SELECT)
225 #define LPS_MASK (LPS_SELECT|LPS_FAULT|LPS_BUSY|LPS_PAPER_EMPTY)
226
227 status = (lpt_status_read(sc) ^ LPS_INVERT) & LPS_MASK;
228
229 if (err) {
230 new = status & ~sc->sc_laststatus;
231 sc->sc_laststatus = status;
232
233 if (new & LPS_SELECT)
234 log(LOG_NOTICE, "%s: offline\n",
235 device_xname(sc->sc_dev));
236 else if (new & LPS_PAPER_EMPTY)
237 log(LOG_NOTICE, "%s: out of paper\n",
238 device_xname(sc->sc_dev));
239 else if (new & LPS_FAULT)
240 log(LOG_NOTICE, "%s: output error\n",
241 device_xname(sc->sc_dev));
242 }
243
244 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
245 sc->sc_icr | LPI_FAULTINT);
246
247 return status;
248 }
249
250 static void
251 lpt_pcc_wr_data(struct lpt_softc *sc, u_char data)
252 {
253
254 lpt_data_write(sc, data);
255 }
256