bpp.c revision 1.3 1 /* $NetBSD: bpp.c,v 1.3 1999/11/21 15:01:50 pk Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
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 #include <sys/param.h>
40 #include <sys/ioctl.h>
41 #include <sys/fcntl.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/vnode.h>
45 #include <sys/poll.h>
46 #include <sys/select.h>
47 #include <sys/malloc.h>
48 #include <sys/proc.h>
49 #include <sys/signalvar.h>
50 #include <sys/conf.h>
51 #include <sys/errno.h>
52 #include <sys/device.h>
53
54 #include <machine/conf.h>
55 #include <machine/bus.h>
56 #include <machine/autoconf.h>
57
58 #include <dev/ic/lsi64854reg.h>
59 #include <dev/ic/lsi64854var.h>
60
61 #include <dev/sbus/sbusvar.h>
62 #include <dev/sbus/bppreg.h>
63
64 #define splbpp() spltty() /* XXX */
65
66 #if 0
67 struct bpp_param {
68 int bpp_dss; /* data setup to strobe */
69 int bpp_dsw; /* data strobe width */
70 int bpp_outputpins; /* Select/Autofeed/Init pins */
71 int bpp_inputpins; /* Error/Select/Paperout pins */
72 };
73 #endif
74
75 struct hwstate {
76 u_int16_t hw_hcr; /* Hardware config register */
77 u_int16_t hw_ocr; /* Operation config register */
78 u_int8_t hw_tcr; /* Transfer Control register */
79 u_int8_t hw_or; /* Output register */
80 u_int16_t hw_irq; /* IRQ; polarity bits only */
81 };
82
83 struct bpp_softc {
84 struct lsi64854_softc sc_lsi64854; /* base device */
85 struct sbusdev sc_sd; /* sbus device */
86
87 size_t sc_bufsz; /* temp buffer */
88 caddr_t sc_buf;
89
90 int sc_error; /* bottom-half error */
91 int sc_flags;
92 #define BPP_OPEN 0x01 /* Device is open */
93 #define BPP_XCLUDE 0x02 /* Exclusive-open mode */
94 #define BPP_ASYNC 0x04 /* Asynchronous I/O mode */
95 #define BPP_LOCKED 0x08 /* DMA in progress */
96 #define BPP_WANT 0x10 /* Waiting for DMA */
97
98 struct selinfo sc_rsel;
99 struct selinfo sc_wsel;
100 struct proc *sc_asyncproc; /* Process to notify if async */
101
102 /* Hardware state */
103 struct hwstate sc_hwdefault;
104 struct hwstate sc_hwcurrent;
105 };
106
107 static int bppmatch __P((struct device *, struct cfdata *, void *));
108 static void bppattach __P((struct device *, struct device *, void *));
109 static int bppintr __P((void *));
110 static void bpp_setparams __P((struct bpp_softc *, struct hwstate *));
111
112 struct cfattach bpp_ca = {
113 sizeof(struct bpp_softc), bppmatch, bppattach
114 };
115
116 extern struct cfdriver bpp_cd;
117 #define BPPUNIT(dev) (minor(dev))
118
119
120 int
121 bppmatch(parent, cf, aux)
122 struct device *parent;
123 struct cfdata *cf;
124 void *aux;
125 {
126 struct sbus_attach_args *sa = aux;
127
128 return (strcmp("SUNW,bpp", sa->sa_name) == 0);
129 }
130
131 void
132 bppattach(parent, self, aux)
133 struct device *parent, *self;
134 void *aux;
135 {
136 struct sbus_attach_args *sa = aux;
137 struct bpp_softc *dsc = (void *)self;
138 struct lsi64854_softc *sc = &dsc->sc_lsi64854;
139 int burst, sbusburst;
140 int node;
141
142 sc->sc_bustag = sa->sa_bustag;
143 sc->sc_dmatag = sa->sa_dmatag;
144 node = sa->sa_node;
145
146 /* Map device registers */
147 if (bus_space_map2(sa->sa_bustag,
148 sa->sa_slot,
149 sa->sa_offset,
150 sa->sa_size,
151 BUS_SPACE_MAP_LINEAR,
152 0, &sc->sc_regs) != 0) {
153 printf("%s: cannot map registers\n", self->dv_xname);
154 return;
155 }
156
157 /*
158 * Get transfer burst size from PROM and plug it into the
159 * controller registers. This is needed on the Sun4m; do
160 * others need it too?
161 */
162 sbusburst = ((struct sbus_softc *)parent)->sc_burst;
163 if (sbusburst == 0)
164 sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
165
166 burst = getpropint(node, "burst-sizes", -1);
167 if (burst == -1)
168 /* take SBus burst sizes */
169 burst = sbusburst;
170
171 /* Clamp at parent's burst sizes */
172 burst &= sbusburst;
173 sc->sc_burst = (burst & SBUS_BURST_32) ? 32 :
174 (burst & SBUS_BURST_16) ? 16 : 0;
175
176 /* Join the Sbus device family */
177 dsc->sc_sd.sd_reset = (void *)0;
178 sbus_establish(&dsc->sc_sd, self);
179
180 /* Initialize the DMA channel */
181 sc->sc_channel = L64854_CHANNEL_PP;
182 lsi64854_attach(sc);
183
184 /* Establish interrupt handler */
185 if (sa->sa_nintr) {
186 sc->sc_intrchain = bppintr;
187 sc->sc_intrchainarg = dsc;
188 (void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, 0,
189 lsi64854_pp_intr, sc);
190 }
191
192 /* Allocate buffer XXX - should actually use dmamap_uio() */
193 dsc->sc_bufsz = 1024;
194 dsc->sc_buf = malloc(dsc->sc_bufsz, M_DEVBUF, M_NOWAIT);
195
196 /* XXX read default state */
197 {
198 bus_space_handle_t h = sc->sc_regs;
199 struct hwstate *hw = &dsc->sc_hwdefault;
200 hw->hw_hcr = bus_space_read_2(sc->sc_bustag, h, L64854_REG_HCR);
201 hw->hw_ocr = bus_space_read_2(sc->sc_bustag, h, L64854_REG_OCR);
202 hw->hw_tcr = bus_space_read_1(sc->sc_bustag, h, L64854_REG_TCR);
203 hw->hw_or = bus_space_read_1(sc->sc_bustag, h, L64854_REG_OR);
204 }
205 }
206
207 void
208 bpp_setparams(sc, hw)
209 struct bpp_softc *sc;
210 struct hwstate *hw;
211 {
212 u_int16_t irq;
213 bus_space_tag_t t = sc->sc_lsi64854.sc_bustag;
214 bus_space_handle_t h = sc->sc_lsi64854.sc_regs;
215
216 bus_space_write_2(t, h, L64854_REG_HCR, hw->hw_hcr);
217 bus_space_write_2(t, h, L64854_REG_OCR, hw->hw_ocr);
218 bus_space_write_1(t, h, L64854_REG_TCR, hw->hw_tcr);
219 bus_space_write_1(t, h, L64854_REG_OR, hw->hw_or);
220
221 /* Only change IRP settings in interrupt status register */
222 irq = bus_space_read_2(t, h, L64854_REG_ICR);
223 irq &= ~BPP_ALLIRP;
224 irq |= (hw->hw_irq & BPP_ALLIRP);
225 bus_space_write_2(t, h, L64854_REG_ICR, irq);
226 }
227
228 int
229 bppopen(dev, flags, mode, p)
230 dev_t dev;
231 int flags, mode;
232 struct proc *p;
233 {
234 int unit = BPPUNIT(dev);
235 struct bpp_softc *sc;
236 struct lsi64854_softc *lsi;
237 u_int16_t irq;
238 int s;
239
240 if (unit >= bpp_cd.cd_ndevs)
241 return (ENXIO);
242 sc = bpp_cd.cd_devs[unit];
243
244 if ((sc->sc_flags & (BPP_OPEN|BPP_XCLUDE)) == (BPP_OPEN|BPP_XCLUDE))
245 return (EBUSY);
246
247 lsi = &sc->sc_lsi64854;
248
249 /* Set default parameters */
250 sc->sc_hwcurrent = sc->sc_hwdefault;
251 s = splbpp();
252 bpp_setparams(sc, &sc->sc_hwdefault);
253 splx(s);
254
255 /* Enable interrupts */
256 irq = BPP_ALLEN;
257 irq |= sc->sc_hwdefault.hw_irq;
258 bus_space_write_2(lsi->sc_bustag, lsi->sc_regs, L64854_REG_ICR, irq);
259 return (0);
260 }
261
262 int
263 bppclose(dev, flags, mode, p)
264 dev_t dev;
265 int flags, mode;
266 struct proc *p;
267 {
268 struct bpp_softc *sc = bpp_cd.cd_devs[BPPUNIT(dev)];
269 struct lsi64854_softc *lsi = &sc->sc_lsi64854;
270 u_int16_t irq;
271
272 /* Turn off all interrupt enables */
273 irq = sc->sc_hwdefault.hw_irq | BPP_ALLIRQ;
274 irq &= ~BPP_ALLEN;
275 bus_space_write_2(lsi->sc_bustag, lsi->sc_regs, L64854_REG_ICR, irq);
276
277 sc->sc_asyncproc = NULL;
278 sc->sc_flags = 0;
279 return (0);
280 }
281
282 int
283 bppread(dev, uio, flags)
284 dev_t dev;
285 struct uio *uio;
286 int flags;
287 {
288
289 return (ENXIO);
290 }
291
292 int
293 bppwrite(dev, uio, flags)
294 dev_t dev;
295 struct uio *uio;
296 int flags;
297 {
298 struct bpp_softc *sc = bpp_cd.cd_devs[BPPUNIT(dev)];
299 struct lsi64854_softc *lsi = &sc->sc_lsi64854;
300 int error = 0;
301 int s;
302
303 /*
304 * Wait until the DMA engibe is free.
305 */
306 s = splbpp();
307 while ((sc->sc_flags & BPP_LOCKED) != 0) {
308 if ((flags & IO_NDELAY) != 0) {
309 splx(s);
310 return (EWOULDBLOCK);
311 }
312
313 sc->sc_flags |= BPP_WANT;
314 error = tsleep(sc->sc_buf, PZERO|PCATCH, "bppwrite", 0);
315 if (error != 0) {
316 splx(s);
317 return (error);
318 }
319 }
320 sc->sc_flags |= BPP_LOCKED;
321 splx(s);
322
323 /*
324 * Move data from user space into our private buffer
325 * and start DMA.
326 */
327 while (uio->uio_resid > 0) {
328 caddr_t bp = sc->sc_buf;
329 size_t len = min(sc->sc_bufsz, uio->uio_resid);
330
331 if ((error = uiomove(bp, len, uio)) != 0)
332 break;
333
334 while (len > 0) {
335 u_int8_t tcr;
336 size_t size = len;
337 DMA_SETUP(lsi, &bp, &len, 0, &size);
338
339 /* Clear direction control bit */
340 tcr = bus_space_read_1(lsi->sc_bustag, lsi->sc_regs,
341 L64854_REG_TCR);
342 tcr &= ~BPP_TCR_DIR;
343 bus_space_write_1(lsi->sc_bustag, lsi->sc_regs,
344 L64854_REG_TCR, tcr);
345
346 /* Enable DMA */
347 DMA_GO(lsi);
348 error = tsleep(sc, PZERO|PCATCH, "bppdma", 0);
349 if (error != 0)
350 goto out;
351
352 /* Bail out if bottom half reported an error */
353 if ((error = sc->sc_error) != 0)
354 goto out;
355 }
356 }
357
358 out:
359 s = splbpp();
360 sc->sc_flags &= ~BPP_LOCKED;
361 if ((sc->sc_flags & BPP_WANT) != 0) {
362 sc->sc_flags &= ~BPP_WANT;
363 wakeup(sc->sc_buf);
364 }
365 splx(s);
366 return (error);
367 }
368
369 /* move to header: */
370 #define BPPIOCSPARAM _IOW('P', 0x1, struct hwstate)
371 #define BPPIOCGPARAM _IOR('P', 0x2, struct hwstate)
372
373 int
374 bppioctl(dev, cmd, data, flag, p)
375 dev_t dev;
376 u_long cmd;
377 caddr_t data;
378 int flag;
379 struct proc *p;
380 {
381 struct bpp_softc *sc = bpp_cd.cd_devs[BPPUNIT(dev)];
382 struct hwstate *hw, *chw;
383 int error = 0;
384 int s;
385
386 switch(cmd) {
387 case BPPIOCSPARAM:
388 chw = &sc->sc_hwcurrent;
389 hw = (struct hwstate *)data;
390
391 /*
392 * Extract and store user-settable bits.
393 */
394 #define _bpp_set(reg,mask) do { \
395 chw->reg &= ~(mask); \
396 chw->reg |= (hw->reg & (mask)); \
397 } while (0)
398 _bpp_set(hw_hcr, BPP_HCR_DSS_MASK|BPP_HCR_DSW_MASK);
399 _bpp_set(hw_ocr, BPP_OCR_USER);
400 _bpp_set(hw_tcr, BPP_TCR_USER);
401 _bpp_set(hw_or, BPP_OR_USER);
402 _bpp_set(hw_irq, BPP_IRQ_USER);
403 #undef _bpp_set
404
405 /* Apply settings */
406 s = splbpp();
407 bpp_setparams(sc, chw);
408 splx(s);
409 break;
410 case BPPIOCGPARAM:
411 *((struct hwstate *)data) = sc->sc_hwcurrent;
412 break;
413 case TIOCEXCL:
414 s = splbpp();
415 sc->sc_flags |= BPP_XCLUDE;
416 splx(s);
417 break;
418 case TIOCNXCL:
419 s = splbpp();
420 sc->sc_flags &= ~BPP_XCLUDE;
421 splx(s);
422 break;
423 case FIOASYNC:
424 s = splbpp();
425 if (*(int *)data) {
426 if (sc->sc_asyncproc != NULL)
427 error = EBUSY;
428 else
429 sc->sc_asyncproc = p;
430 } else
431 sc->sc_asyncproc = NULL;
432 splx(s);
433 break;
434 default:
435 break;
436 }
437
438 return (error);
439 }
440
441 int
442 bpppoll(dev, events, p)
443 dev_t dev;
444 int events;
445 struct proc *p;
446 {
447 struct bpp_softc *sc = bpp_cd.cd_devs[BPPUNIT(dev)];
448 int revents = 0;
449
450 if (events & (POLLIN | POLLRDNORM)) {
451 /* read is not yet implemented */
452 }
453
454 if (events & (POLLOUT | POLLWRNORM)) {
455 if ((sc->sc_flags & BPP_LOCKED) == 0)
456 revents |= (POLLOUT | POLLWRNORM);
457 }
458
459 if (revents == 0) {
460 if (events & (POLLIN | POLLRDNORM))
461 selrecord(p, &sc->sc_rsel);
462 if (events & (POLLOUT | POLLWRNORM))
463 selrecord(p, &sc->sc_wsel);
464 }
465
466 return (revents);
467 }
468
469 int
470 bppintr(arg)
471 void *arg;
472 {
473 struct bpp_softc *sc = arg;
474 struct lsi64854_softc *lsi = &sc->sc_lsi64854;
475 u_int16_t irq;
476
477 irq = bus_space_read_2(lsi->sc_bustag, lsi->sc_regs, L64854_REG_ICR);
478 /* Ack all interrupts */
479 bus_space_write_2(lsi->sc_bustag, lsi->sc_regs, L64854_REG_ICR,
480 irq | BPP_ALLIRQ);
481
482 /* Did our device interrupt? */
483 if ((irq & BPP_ALLIRQ) == 0)
484 return (0);
485
486 if ((sc->sc_flags & BPP_LOCKED) != 0)
487 wakeup(sc);
488 else if ((sc->sc_flags & BPP_WANT) != 0) {
489 sc->sc_flags &= ~BPP_WANT;
490 wakeup(sc->sc_buf);
491 } else {
492 selwakeup(&sc->sc_wsel);
493 if (sc->sc_asyncproc != NULL)
494 psignal(sc->sc_asyncproc, SIGIO);
495 }
496 return (1);
497 }
498