1 /* $NetBSD: lpt_pcc.c,v 1.9 2005/12/11 12:18:17 christos 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.9 2005/12/11 12:18:17 christos 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 61 62 static int lpt_pcc_intr __P((void *)); 63 static void lpt_pcc_open __P((struct lpt_softc *, int)); 64 static void lpt_pcc_close __P((struct lpt_softc *)); 65 static void lpt_pcc_iprime __P((struct lpt_softc *)); 66 static void lpt_pcc_speed __P((struct lpt_softc *, int)); 67 static int lpt_pcc_notrdy __P((struct lpt_softc *, int)); 68 static void lpt_pcc_wr_data __P((struct lpt_softc *, u_char)); 69 70 struct lpt_funcs lpt_pcc_funcs = { 71 lpt_pcc_open, 72 lpt_pcc_close, 73 lpt_pcc_iprime, 74 lpt_pcc_speed, 75 lpt_pcc_notrdy, 76 lpt_pcc_wr_data 77 }; 78 79 /* 80 * Autoconfig stuff 81 */ 82 static int lpt_pcc_match __P((struct device *, struct cfdata *, void *)); 83 static void lpt_pcc_attach __P((struct device *, struct device *, void *)); 84 85 CFATTACH_DECL(lpt_pcc, sizeof(struct lpt_softc), 86 lpt_pcc_match, lpt_pcc_attach, NULL, NULL); 87 88 extern struct cfdriver lpt_cd; 89 90 91 /*ARGSUSED*/ 92 static int 93 lpt_pcc_match(parent, cf, args) 94 struct device *parent; 95 struct cfdata *cf; 96 void *args; 97 { 98 struct pcc_attach_args *pa; 99 100 pa = args; 101 102 if (strcmp(pa->pa_name, lpt_cd.cd_name)) 103 return (0); 104 105 pa->pa_ipl = cf->pcccf_ipl; 106 return (1); 107 } 108 109 /*ARGSUSED*/ 110 static void 111 lpt_pcc_attach(parent, self, args) 112 struct device *parent, *self; 113 void *args; 114 { 115 struct lpt_softc *sc; 116 struct pcc_attach_args *pa; 117 118 sc = (struct lpt_softc *) self; 119 pa = args; 120 121 sc->sc_bust = pa->pa_bust; 122 bus_space_map(pa->pa_bust, pa->pa_offset, LPREG_SIZE, 0, &sc->sc_bush); 123 124 sc->sc_ipl = pa->pa_ipl & PCC_IMASK; 125 sc->sc_funcs = &lpt_pcc_funcs; 126 sc->sc_laststatus = 0; 127 128 printf(": PCC Parallel Printer\n"); 129 130 /* 131 * Disable interrupts until device is opened 132 */ 133 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 0); 134 135 /* 136 * Main attachment code 137 */ 138 lpt_attach_subr(sc); 139 140 /* Register the event counter */ 141 evcnt_attach_dynamic(&sc->sc_evcnt, EVCNT_TYPE_INTR, 142 pccintr_evcnt(sc->sc_ipl), "printer", sc->sc_dev.dv_xname); 143 144 /* 145 * Hook into the printer interrupt 146 */ 147 pccintr_establish(PCCV_PRINTER, lpt_pcc_intr, sc->sc_ipl, sc, 148 &sc->sc_evcnt); 149 } 150 151 /* 152 * Handle printer interrupts which occur when the printer is ready to accept 153 * another char. 154 */ 155 int 156 lpt_pcc_intr(arg) 157 void *arg; 158 { 159 struct lpt_softc *sc; 160 int i; 161 162 sc = arg; 163 164 /* is printer online and ready for output */ 165 if (lpt_pcc_notrdy(sc, 0) && lpt_pcc_notrdy(sc, 1)) 166 return 0; 167 168 i = lpt_intr(sc); 169 170 if (pcc_reg_read(sys_pcc, PCCREG_PRNT_INTR_CTRL) & LPI_ACKINT) { 171 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 172 sc->sc_icr | LPI_ACKINT); 173 } 174 175 return (i); 176 } 177 178 179 static void 180 lpt_pcc_open(sc, int_ena) 181 struct lpt_softc *sc; 182 int int_ena; 183 { 184 int sps; 185 186 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 187 LPI_ACKINT | LPI_FAULTINT); 188 189 if (int_ena == 0) { 190 sps = splhigh(); 191 sc->sc_icr = sc->sc_ipl | LPI_ENABLE; 192 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, sc->sc_icr); 193 splx(sps); 194 } 195 } 196 197 static void 198 lpt_pcc_close(sc) 199 struct lpt_softc *sc; 200 { 201 202 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 0); 203 sc->sc_icr = sc->sc_ipl; 204 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, sc->sc_icr); 205 } 206 207 /* ARGSUSED */ 208 static void 209 lpt_pcc_iprime(sc) 210 struct lpt_softc *sc; 211 { 212 213 lpt_control_write(LPC_INPUT_PRIME); 214 delay(100); 215 } 216 217 /* ARGSUSED */ 218 static void 219 lpt_pcc_speed(sc, speed) 220 struct lpt_softc *sc; 221 int speed; 222 { 223 224 if (speed == LPT_STROBE_FAST) 225 lpt_control_write(LPC_FAST_STROBE); 226 else 227 lpt_control_write(0); 228 } 229 230 static int 231 lpt_pcc_notrdy(sc, err) 232 struct lpt_softc *sc; 233 int err; 234 { 235 u_char status; 236 u_char new; 237 238 #define LPS_INVERT (LPS_SELECT) 239 #define LPS_MASK (LPS_SELECT|LPS_FAULT|LPS_BUSY|LPS_PAPER_EMPTY) 240 241 status = (lpt_status_read(sc) ^ LPS_INVERT) & LPS_MASK; 242 243 if (err) { 244 new = status & ~sc->sc_laststatus; 245 sc->sc_laststatus = status; 246 247 if (new & LPS_SELECT) 248 log(LOG_NOTICE, "%s: offline\n", 249 sc->sc_dev.dv_xname); 250 else if (new & LPS_PAPER_EMPTY) 251 log(LOG_NOTICE, "%s: out of paper\n", 252 sc->sc_dev.dv_xname); 253 else if (new & LPS_FAULT) 254 log(LOG_NOTICE, "%s: output error\n", 255 sc->sc_dev.dv_xname); 256 } 257 258 pcc_reg_write(sys_pcc, PCCREG_PRNT_INTR_CTRL, 259 sc->sc_icr | LPI_FAULTINT); 260 261 return (status); 262 } 263 264 static void 265 lpt_pcc_wr_data(sc, data) 266 struct lpt_softc *sc; 267 u_char data; 268 { 269 270 lpt_data_write(sc, data); 271 } 272