lpt_pcc.c revision 1.10 1 /* $NetBSD: lpt_pcc.c,v 1.10 2008/01/12 09:54:21 tsutsui 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.10 2008/01/12 09:54:21 tsutsui 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(struct device *, struct cfdata *, void *);
84 static void lpt_pcc_attach(struct device *, struct device *, void *);
85
86 CFATTACH_DECL(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(struct device *parent, struct cfdata *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(struct device *parent, struct device *self, void *args)
108 {
109 struct lpt_softc *sc;
110 struct pcc_attach_args *pa;
111
112 sc = (struct lpt_softc *)self;
113 pa = args;
114
115 sc->sc_bust = pa->pa_bust;
116 bus_space_map(pa->pa_bust, pa->pa_offset, LPREG_SIZE, 0, &sc->sc_bush);
117
118 sc->sc_ipl = pa->pa_ipl & PCC_IMASK;
119 sc->sc_funcs = &lpt_pcc_funcs;
120 sc->sc_laststatus = 0;
121
122 printf(": PCC Parallel Printer\n");
123
124 /*
125 * Disable interrupts until device is opened
126 */
127 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 0);
128
129 /*
130 * Main attachment code
131 */
132 lpt_attach_subr(sc);
133
134 /* Register the event counter */
135 evcnt_attach_dynamic(&sc->sc_evcnt, EVCNT_TYPE_INTR,
136 pccintr_evcnt(sc->sc_ipl), "printer", sc->sc_dev.dv_xname);
137
138 /*
139 * Hook into the printer interrupt
140 */
141 pccintr_establish(PCCV_PRINTER, lpt_pcc_intr, sc->sc_ipl, sc,
142 &sc->sc_evcnt);
143 }
144
145 /*
146 * Handle printer interrupts which occur when the printer is ready to accept
147 * another char.
148 */
149 int
150 lpt_pcc_intr(void *arg)
151 {
152 struct lpt_softc *sc;
153 int i;
154
155 sc = arg;
156
157 /* is printer online and ready for output */
158 if (lpt_pcc_notrdy(sc, 0) && lpt_pcc_notrdy(sc, 1))
159 return 0;
160
161 i = lpt_intr(sc);
162
163 if (pcc_reg_read(sys_pcc, PCCREG_PRNT_INTR_CTRL) & LPI_ACKINT) {
164 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
165 sc->sc_icr | LPI_ACKINT);
166 }
167
168 return i;
169 }
170
171
172 static void
173 lpt_pcc_open(struct lpt_softc *sc, int int_ena)
174 {
175 int sps;
176
177 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
178 LPI_ACKINT | LPI_FAULTINT);
179
180 if (int_ena == 0) {
181 sps = splhigh();
182 sc->sc_icr = sc->sc_ipl | LPI_ENABLE;
183 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, sc->sc_icr);
184 splx(sps);
185 }
186 }
187
188 static void
189 lpt_pcc_close(struct lpt_softc *sc)
190 {
191
192 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 0);
193 sc->sc_icr = sc->sc_ipl;
194 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, sc->sc_icr);
195 }
196
197 /* ARGSUSED */
198 static void
199 lpt_pcc_iprime(struct lpt_softc *sc)
200 {
201
202 lpt_control_write(LPC_INPUT_PRIME);
203 delay(100);
204 }
205
206 /* ARGSUSED */
207 static void
208 lpt_pcc_speed(struct lpt_softc *sc, int speed)
209 {
210
211 if (speed == LPT_STROBE_FAST)
212 lpt_control_write(LPC_FAST_STROBE);
213 else
214 lpt_control_write(0);
215 }
216
217 static int
218 lpt_pcc_notrdy(struct lpt_softc *sc, int err)
219 {
220 u_char status;
221 u_char new;
222
223 #define LPS_INVERT (LPS_SELECT)
224 #define LPS_MASK (LPS_SELECT|LPS_FAULT|LPS_BUSY|LPS_PAPER_EMPTY)
225
226 status = (lpt_status_read(sc) ^ LPS_INVERT) & LPS_MASK;
227
228 if (err) {
229 new = status & ~sc->sc_laststatus;
230 sc->sc_laststatus = status;
231
232 if (new & LPS_SELECT)
233 log(LOG_NOTICE, "%s: offline\n",
234 sc->sc_dev.dv_xname);
235 else if (new & LPS_PAPER_EMPTY)
236 log(LOG_NOTICE, "%s: out of paper\n",
237 sc->sc_dev.dv_xname);
238 else if (new & LPS_FAULT)
239 log(LOG_NOTICE, "%s: output error\n",
240 sc->sc_dev.dv_xname);
241 }
242
243 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL,
244 sc->sc_icr | LPI_FAULTINT);
245
246 return status;
247 }
248
249 static void
250 lpt_pcc_wr_data(struct lpt_softc *sc, u_char data)
251 {
252
253 lpt_data_write(sc, data);
254 }
255