Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / ic / atppc.c
blob06087085c42cea50126869bfa6280aa27c583f0d
1 /* $NetBSD: atppc.c,v 1.27 2008/04/18 14:56:40 cegger Exp $ */
3 /*
4 * Copyright (c) 2001 Alcove - Nicolas Souchu
5 * Copyright (c) 2003, 2004 Gary Thorpe <gathorpe@users.sourceforge.net>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * FreeBSD: src/sys/isa/ppc.c,v 1.26.2.5 2001/10/02 05:21:45 nsouch Exp
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: atppc.c,v 1.27 2008/04/18 14:56:40 cegger Exp $");
36 #include "opt_atppc.h"
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/syslog.h>
48 #include <sys/bus.h>
49 /*#include <sys/intr.h>*/
51 #include <dev/isa/isareg.h>
53 #include <dev/ic/atppcreg.h>
54 #include <dev/ic/atppcvar.h>
56 #include <dev/ppbus/ppbus_conf.h>
57 #include <dev/ppbus/ppbus_msq.h>
58 #include <dev/ppbus/ppbus_io.h>
59 #include <dev/ppbus/ppbus_var.h>
61 #ifdef ATPPC_DEBUG
62 int atppc_debug = 1;
63 #endif
65 #ifdef ATPPC_VERBOSE
66 int atppc_verbose = 1;
67 #endif
69 /* List of supported chipsets detection routines */
70 static int (*chipset_detect[])(struct atppc_softc *) = {
71 /* XXX Add these LATER: maybe as separate devices?
72 atppc_pc873xx_detect,
73 atppc_smc37c66xgt_detect,
74 atppc_w83877f_detect,
75 atppc_smc37c935_detect,
77 NULL
81 /* Prototypes for functions. */
83 /* Print function for config_found() */
84 static int atppc_print(void *, const char *);
86 /* Detection routines */
87 static int atppc_detect_fifo(struct atppc_softc *);
88 static int atppc_detect_chipset(struct atppc_softc *);
89 static int atppc_detect_generic(struct atppc_softc *);
91 /* Routines for ppbus interface (bus + device) */
92 static int atppc_read(device_t, char *, int, int, size_t *);
93 static int atppc_write(device_t, char *, int, int, size_t *);
94 static int atppc_setmode(device_t, int);
95 static int atppc_getmode(device_t);
96 static int atppc_check_epp_timeout(device_t);
97 static void atppc_reset_epp_timeout(device_t);
98 static void atppc_ecp_sync(device_t);
99 static int atppc_exec_microseq(device_t, struct ppbus_microseq * *);
100 static u_int8_t atppc_io(device_t, int, u_char *, int, u_char);
101 static int atppc_read_ivar(device_t, int, unsigned int *);
102 static int atppc_write_ivar(device_t, int, unsigned int *);
103 static int atppc_add_handler(device_t, void (*)(void *), void *);
104 static int atppc_remove_handler(device_t, void (*)(void *));
106 /* Utility functions */
108 /* Functions to read bytes into device's input buffer */
109 static void atppc_nibble_read(struct atppc_softc * const);
110 static void atppc_byte_read(struct atppc_softc * const);
111 static void atppc_epp_read(struct atppc_softc * const);
112 static void atppc_ecp_read(struct atppc_softc * const);
113 static void atppc_ecp_read_dma(struct atppc_softc *, unsigned int *,
114 unsigned char);
115 static void atppc_ecp_read_pio(struct atppc_softc *, unsigned int *,
116 unsigned char);
117 static void atppc_ecp_read_error(struct atppc_softc *);
120 /* Functions to write bytes to device's output buffer */
121 static void atppc_std_write(struct atppc_softc * const);
122 static void atppc_epp_write(struct atppc_softc * const);
123 static void atppc_fifo_write(struct atppc_softc * const);
124 static void atppc_fifo_write_dma(struct atppc_softc * const, unsigned char,
125 unsigned char);
126 static void atppc_fifo_write_pio(struct atppc_softc * const, unsigned char,
127 unsigned char);
128 static void atppc_fifo_write_error(struct atppc_softc * const,
129 const unsigned int);
131 /* Miscellaneous */
132 static int atppc_poll_str(const struct atppc_softc * const, const u_int8_t,
133 const u_int8_t);
134 static int atppc_wait_interrupt(struct atppc_softc * const, const void *,
135 const u_int8_t);
139 * Generic attach and detach functions for atppc device. If sc_dev_ok in soft
140 * configuration data is not ATPPC_ATTACHED, these should be skipped altogether.
143 /* Soft configuration attach for atppc */
144 void
145 atppc_sc_attach(struct atppc_softc *lsc)
147 /* Adapter used to configure ppbus device */
148 struct parport_adapter sc_parport_adapter;
149 char buf[64];
151 ATPPC_LOCK_INIT(lsc);
153 /* Probe and set up chipset */
154 if (atppc_detect_chipset(lsc) != 0) {
155 if (atppc_detect_generic(lsc) != 0) {
156 ATPPC_DPRINTF(("%s: Error detecting chipset\n",
157 device_xname(lsc->sc_dev)));
161 /* Probe and setup FIFO queue */
162 if (atppc_detect_fifo(lsc) == 0) {
163 printf("%s: FIFO <depth,wthr,rthr>=<%d,%d,%d>\n",
164 device_xname(lsc->sc_dev), lsc->sc_fifo, lsc->sc_wthr,
165 lsc->sc_rthr);
168 /* Print out chipset capabilities */
169 snprintb(buf, sizeof(buf), "\20\1INTR\2DMA\3FIFO\4PS2\5ECP\6EPP",
170 lsc->sc_has);
171 printf("%s: capabilities=%s\n", device_xname(lsc->sc_dev), buf);
173 /* Initialize device's buffer pointers */
174 lsc->sc_outb = lsc->sc_outbstart = lsc->sc_inb = lsc->sc_inbstart
175 = NULL;
176 lsc->sc_inb_nbytes = lsc->sc_outb_nbytes = 0;
178 /* Last configuration step: set mode to standard mode */
179 if (atppc_setmode(lsc->sc_dev, PPBUS_COMPATIBLE) != 0) {
180 ATPPC_DPRINTF(("%s: unable to initialize mode.\n",
181 device_xname(lsc->sc_dev)));
184 #if defined (MULTIPROCESSOR) || defined (LOCKDEBUG)
185 /* Initialize lock structure */
186 simple_lock_init(&(lsc->sc_lock));
187 #endif
189 /* Set up parport_adapter structure */
191 /* Set capabilites */
192 sc_parport_adapter.capabilities = 0;
193 if (lsc->sc_has & ATPPC_HAS_INTR) {
194 sc_parport_adapter.capabilities |= PPBUS_HAS_INTR;
196 if (lsc->sc_has & ATPPC_HAS_DMA) {
197 sc_parport_adapter.capabilities |= PPBUS_HAS_DMA;
199 if (lsc->sc_has & ATPPC_HAS_FIFO) {
200 sc_parport_adapter.capabilities |= PPBUS_HAS_FIFO;
202 if (lsc->sc_has & ATPPC_HAS_PS2) {
203 sc_parport_adapter.capabilities |= PPBUS_HAS_PS2;
205 if (lsc->sc_has & ATPPC_HAS_EPP) {
206 sc_parport_adapter.capabilities |= PPBUS_HAS_EPP;
208 if (lsc->sc_has & ATPPC_HAS_ECP) {
209 sc_parport_adapter.capabilities |= PPBUS_HAS_ECP;
212 /* Set function pointers */
213 sc_parport_adapter.parport_io = atppc_io;
214 sc_parport_adapter.parport_exec_microseq = atppc_exec_microseq;
215 sc_parport_adapter.parport_reset_epp_timeout =
216 atppc_reset_epp_timeout;
217 sc_parport_adapter.parport_setmode = atppc_setmode;
218 sc_parport_adapter.parport_getmode = atppc_getmode;
219 sc_parport_adapter.parport_ecp_sync = atppc_ecp_sync;
220 sc_parport_adapter.parport_read = atppc_read;
221 sc_parport_adapter.parport_write = atppc_write;
222 sc_parport_adapter.parport_read_ivar = atppc_read_ivar;
223 sc_parport_adapter.parport_write_ivar = atppc_write_ivar;
224 sc_parport_adapter.parport_dma_malloc = lsc->sc_dma_malloc;
225 sc_parport_adapter.parport_dma_free = lsc->sc_dma_free;
226 sc_parport_adapter.parport_add_handler = atppc_add_handler;
227 sc_parport_adapter.parport_remove_handler = atppc_remove_handler;
229 /* Initialize handler list, may be added to by grandchildren */
230 SLIST_INIT(&(lsc->sc_handler_listhead));
232 /* Initialize interrupt state */
233 lsc->sc_irqstat = ATPPC_IRQ_NONE;
234 lsc->sc_ecr_intr = lsc->sc_ctr_intr = lsc->sc_str_intr = 0;
236 /* Disable DMA/interrupts (each ppbus driver selects usage itself) */
237 lsc->sc_use = 0;
239 /* Configure child of the device. */
240 lsc->child = config_found(lsc->sc_dev, &(sc_parport_adapter),
241 atppc_print);
243 return;
246 /* Soft configuration detach */
248 atppc_sc_detach(struct atppc_softc *lsc, int flag)
250 device_t dev = lsc->sc_dev;
252 /* Detach children devices */
253 if (config_detach(lsc->child, flag) && !(flag & DETACH_QUIET)) {
254 aprint_error_dev(dev, "not able to detach child device, ");
256 if (!(flag & DETACH_FORCE)) {
257 printf("cannot detach\n");
258 return 1;
259 } else {
260 printf("continuing (DETACH_FORCE)\n");
264 if (!(flag & DETACH_QUIET))
265 printf("%s detached", device_xname(dev));
267 return 0;
270 /* Used by config_found() to print out device information */
271 static int
272 atppc_print(void *aux, const char *name)
274 /* Print out something on failure. */
275 if (name != NULL) {
276 printf("%s: child devices", name);
277 return UNCONF;
280 return QUIET;
284 * Machine independent detection routines for atppc driver.
287 /* Detect parallel port I/O port: taken from FreeBSD code directly. */
289 atppc_detect_port(bus_space_tag_t iot, bus_space_handle_t ioh)
292 * Much shorter than scheme used by lpt_isa_probe() and lpt_port_test()
293 * in original lpt driver.
294 * Write to data register common to all controllers and read back the
295 * values. Also tests control and status registers.
299 * Cannot use convenient macros because the device's config structure
300 * may not have been created yet: major change from FreeBSD code.
303 int rval;
304 u_int8_t ctr_sav, dtr_sav, str_sav;
306 /* Store writtable registers' values and test if they can be read */
307 str_sav = bus_space_read_1(iot, ioh, ATPPC_SPP_STR);
308 ctr_sav = bus_space_read_1(iot, ioh, ATPPC_SPP_CTR);
309 dtr_sav = bus_space_read_1(iot, ioh, ATPPC_SPP_DTR);
310 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
311 BUS_SPACE_BARRIER_READ);
314 * Ensure PS2 ports in output mode, also read back value of control
315 * register.
317 bus_space_write_1(iot, ioh, ATPPC_SPP_CTR, 0x0c);
318 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
319 BUS_SPACE_BARRIER_WRITE);
321 if (bus_space_read_1(iot, ioh, ATPPC_SPP_CTR) != 0x0c) {
322 rval = 0;
323 } else {
325 * Test if two values can be written and read from the data
326 * register.
328 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
329 BUS_SPACE_BARRIER_READ);
330 bus_space_write_1(iot, ioh, ATPPC_SPP_DTR, 0xaa);
331 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
332 BUS_SPACE_BARRIER_WRITE);
333 if (bus_space_read_1(iot, ioh, ATPPC_SPP_DTR) != 0xaa) {
334 rval = 1;
335 } else {
336 /* Second value to test */
337 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
338 BUS_SPACE_BARRIER_READ);
339 bus_space_write_1(iot, ioh, ATPPC_SPP_DTR, 0x55);
340 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
341 BUS_SPACE_BARRIER_WRITE);
342 if (bus_space_read_1(iot, ioh, ATPPC_SPP_DTR) != 0x55) {
343 rval = 1;
344 } else {
345 rval = 0;
351 /* Restore registers */
352 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
353 BUS_SPACE_BARRIER_READ);
354 bus_space_write_1(iot, ioh, ATPPC_SPP_CTR, ctr_sav);
355 bus_space_write_1(iot, ioh, ATPPC_SPP_DTR, dtr_sav);
356 bus_space_write_1(iot, ioh, ATPPC_SPP_STR, str_sav);
357 bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
358 BUS_SPACE_BARRIER_WRITE);
360 return rval;
363 /* Detect parallel port chipset. */
364 static int
365 atppc_detect_chipset(struct atppc_softc *atppc)
367 /* Try each detection routine. */
368 int i, mode;
369 for (i = 0; chipset_detect[i] != NULL; i++) {
370 if ((mode = chipset_detect[i](atppc)) != -1) {
371 atppc->sc_mode = mode;
372 return 0;
376 return 1;
379 /* Detect generic capabilities. */
380 static int
381 atppc_detect_generic(struct atppc_softc *atppc)
383 u_int8_t ecr_sav = atppc_r_ecr(atppc);
384 u_int8_t ctr_sav = atppc_r_ctr(atppc);
385 u_int8_t str_sav = atppc_r_str(atppc);
386 u_int8_t tmp;
387 atppc_barrier_r(atppc);
389 /* Default to generic */
390 atppc->sc_type = ATPPC_TYPE_GENERIC;
391 atppc->sc_model = GENERIC;
393 /* Check for ECP */
394 tmp = atppc_r_ecr(atppc);
395 atppc_barrier_r(atppc);
396 if ((tmp & ATPPC_FIFO_EMPTY) && !(tmp & ATPPC_FIFO_FULL)) {
397 atppc_w_ecr(atppc, 0x34);
398 atppc_barrier_w(atppc);
399 tmp = atppc_r_ecr(atppc);
400 atppc_barrier_r(atppc);
401 if (tmp == 0x35) {
402 atppc->sc_has |= ATPPC_HAS_ECP;
406 /* Allow search for SMC style ECP+EPP mode */
407 if (atppc->sc_has & ATPPC_HAS_ECP) {
408 atppc_w_ecr(atppc, ATPPC_ECR_EPP);
409 atppc_barrier_w(atppc);
411 /* Check for EPP by checking for timeout bit */
412 if (atppc_check_epp_timeout(atppc->sc_dev) != 0) {
413 atppc->sc_has |= ATPPC_HAS_EPP;
414 atppc->sc_epp = ATPPC_EPP_1_9;
415 if (atppc->sc_has & ATPPC_HAS_ECP) {
416 /* SMC like chipset found */
417 atppc->sc_model = SMC_LIKE;
418 atppc->sc_type = ATPPC_TYPE_SMCLIKE;
422 /* Detect PS2 mode */
423 if (atppc->sc_has & ATPPC_HAS_ECP) {
424 /* Put ECP port into PS2 mode */
425 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
426 atppc_barrier_w(atppc);
428 /* Put PS2 port in input mode: writes should not be readable */
429 atppc_w_ctr(atppc, 0x20);
430 atppc_barrier_w(atppc);
432 * Write two values to data port: if neither are read back,
433 * bidirectional mode is functional.
435 atppc_w_dtr(atppc, 0xaa);
436 atppc_barrier_w(atppc);
437 tmp = atppc_r_dtr(atppc);
438 atppc_barrier_r(atppc);
439 if (tmp != 0xaa) {
440 atppc_w_dtr(atppc, 0x55);
441 atppc_barrier_w(atppc);
442 tmp = atppc_r_dtr(atppc);
443 atppc_barrier_r(atppc);
444 if (tmp != 0x55) {
445 atppc->sc_has |= ATPPC_HAS_PS2;
449 /* Restore to previous state */
450 atppc_w_ecr(atppc, ecr_sav);
451 atppc_w_ctr(atppc, ctr_sav);
452 atppc_w_str(atppc, str_sav);
453 atppc_barrier_w(atppc);
455 return 0;
459 * Detect parallel port FIFO: taken from FreeBSD code directly.
461 static int
462 atppc_detect_fifo(struct atppc_softc *atppc)
464 #ifdef ATPPC_DEBUG
465 device_t dev = atppc->sc_dev;
466 #endif
467 u_int8_t ecr_sav;
468 u_int8_t ctr_sav;
469 u_int8_t str_sav;
470 u_int8_t cc;
471 short i;
473 /* If there is no ECP mode, we cannot config a FIFO */
474 if (!(atppc->sc_has & ATPPC_HAS_ECP)) {
475 return (EINVAL);
478 /* save registers */
479 ecr_sav = atppc_r_ecr(atppc);
480 ctr_sav = atppc_r_ctr(atppc);
481 str_sav = atppc_r_str(atppc);
482 atppc_barrier_r(atppc);
484 /* Enter ECP configuration mode, no interrupt, no DMA */
485 atppc_w_ecr(atppc, (ATPPC_ECR_CFG | ATPPC_SERVICE_INTR) &
486 ~ATPPC_ENABLE_DMA);
487 atppc_barrier_w(atppc);
489 /* read PWord size - transfers in FIFO mode must be PWord aligned */
490 atppc->sc_pword = (atppc_r_cnfgA(atppc) & ATPPC_PWORD_MASK);
491 atppc_barrier_r(atppc);
493 /* XXX 16 and 32 bits implementations not supported */
494 if (atppc->sc_pword != ATPPC_PWORD_8) {
495 ATPPC_DPRINTF(("%s(%s): FIFO PWord(%d) not supported.\n",
496 __func__, device_xname(dev), atppc->sc_pword));
497 goto error;
500 /* Byte mode, reverse direction, no interrupt, no DMA */
501 atppc_w_ecr(atppc, ATPPC_ECR_PS2 | ATPPC_SERVICE_INTR);
502 atppc_w_ctr(atppc, (ctr_sav & ~IRQENABLE) | PCD);
503 /* enter ECP test mode, no interrupt, no DMA */
504 atppc_w_ecr(atppc, ATPPC_ECR_TST | ATPPC_SERVICE_INTR);
505 atppc_barrier_w(atppc);
507 /* flush the FIFO */
508 for (i = 0; i < 1024; i++) {
509 atppc_r_fifo(atppc);
510 atppc_barrier_r(atppc);
511 cc = atppc_r_ecr(atppc);
512 atppc_barrier_r(atppc);
513 if (cc & ATPPC_FIFO_EMPTY)
514 break;
516 if (i >= 1024) {
517 ATPPC_DPRINTF(("%s(%s): cannot flush FIFO.\n", __func__,
518 device_xname(dev)));
519 goto error;
522 /* Test mode, enable interrupts, no DMA */
523 atppc_w_ecr(atppc, ATPPC_ECR_TST);
524 atppc_barrier_w(atppc);
526 /* Determine readIntrThreshold - fill FIFO until serviceIntr is set */
527 for (i = atppc->sc_rthr = atppc->sc_fifo = 0; i < 1024; i++) {
528 atppc_w_fifo(atppc, (char)i);
529 atppc_barrier_w(atppc);
530 cc = atppc_r_ecr(atppc);
531 atppc_barrier_r(atppc);
532 if ((atppc->sc_rthr == 0) && (cc & ATPPC_SERVICE_INTR)) {
533 /* readThreshold reached */
534 atppc->sc_rthr = i + 1;
536 if (cc & ATPPC_FIFO_FULL) {
537 atppc->sc_fifo = i + 1;
538 break;
541 if (i >= 1024) {
542 ATPPC_DPRINTF(("%s(%s): cannot fill FIFO.\n", __func__,
543 device_xname(dev)));
544 goto error;
547 /* Change direction */
548 atppc_w_ctr(atppc, (ctr_sav & ~IRQENABLE) & ~PCD);
549 atppc_barrier_w(atppc);
551 /* Clear the serviceIntr bit we've already set in the above loop */
552 atppc_w_ecr(atppc, ATPPC_ECR_TST);
553 atppc_barrier_w(atppc);
555 /* Determine writeIntrThreshold - empty FIFO until serviceIntr is set */
556 for (atppc->sc_wthr = 0; i > -1; i--) {
557 cc = atppc_r_fifo(atppc);
558 atppc_barrier_r(atppc);
559 if (cc != (char)(atppc->sc_fifo - i - 1)) {
560 ATPPC_DPRINTF(("%s(%s): invalid data in FIFO.\n",
561 __func__, device_xname(dev)));
562 goto error;
565 cc = atppc_r_ecr(atppc);
566 atppc_barrier_r(atppc);
567 if ((atppc->sc_wthr == 0) && (cc & ATPPC_SERVICE_INTR)) {
568 /* writeIntrThreshold reached */
569 atppc->sc_wthr = atppc->sc_fifo - i;
572 if (i > 0 && (cc & ATPPC_FIFO_EMPTY)) {
573 /* If FIFO empty before the last byte, error */
574 ATPPC_DPRINTF(("%s(%s): data lost in FIFO.\n", __func__,
575 device_xname(dev)));
576 goto error;
580 /* FIFO must be empty after the last byte */
581 cc = atppc_r_ecr(atppc);
582 atppc_barrier_r(atppc);
583 if (!(cc & ATPPC_FIFO_EMPTY)) {
584 ATPPC_DPRINTF(("%s(%s): cannot empty the FIFO.\n", __func__,
585 device_xname(dev)));
586 goto error;
589 /* Restore original registers */
590 atppc_w_ctr(atppc, ctr_sav);
591 atppc_w_str(atppc, str_sav);
592 atppc_w_ecr(atppc, ecr_sav);
593 atppc_barrier_w(atppc);
595 /* Update capabilities */
596 atppc->sc_has |= ATPPC_HAS_FIFO;
598 return 0;
600 error:
601 /* Restore original registers */
602 atppc_w_ctr(atppc, ctr_sav);
603 atppc_w_str(atppc, str_sav);
604 atppc_w_ecr(atppc, ecr_sav);
605 atppc_barrier_w(atppc);
607 return (EINVAL);
610 /* Interrupt handler for atppc device: wakes up read/write functions */
612 atppcintr(void *arg)
614 device_t dev = arg;
615 struct atppc_softc *atppc = device_private(dev);
616 int claim = 1;
617 enum { NONE, READER, WRITER } wake_up = NONE;
619 /* Record registers' status */
620 atppc->sc_str_intr = atppc_r_str(atppc);
621 atppc->sc_ctr_intr = atppc_r_ctr(atppc);
622 atppc->sc_ecr_intr = atppc_r_ecr(atppc);
623 atppc_barrier_r(atppc);
625 /* Determine cause of interrupt and wake up top half */
626 switch (atppc->sc_mode) {
627 case ATPPC_MODE_STD:
628 /* nAck pulsed for 5 usec, too fast to check reliably, assume */
629 atppc->sc_irqstat = ATPPC_IRQ_nACK;
630 if (atppc->sc_outb)
631 wake_up = WRITER;
632 else
633 claim = 0;
634 break;
636 case ATPPC_MODE_NIBBLE:
637 case ATPPC_MODE_PS2:
638 /* nAck is set low by device and then high on ack */
639 if (!(atppc->sc_str_intr & nACK)) {
640 claim = 0;
641 break;
643 atppc->sc_irqstat = ATPPC_IRQ_nACK;
644 if (atppc->sc_inb)
645 wake_up = READER;
646 break;
648 case ATPPC_MODE_ECP:
649 case ATPPC_MODE_FAST:
650 /* Confirm interrupt cause: these are not pulsed as in nAck. */
651 if (atppc->sc_ecr_intr & ATPPC_SERVICE_INTR) {
652 if (atppc->sc_ecr_intr & ATPPC_ENABLE_DMA)
653 atppc->sc_irqstat |= ATPPC_IRQ_DMA;
654 else
655 atppc->sc_irqstat |= ATPPC_IRQ_FIFO;
657 /* Decide where top half will be waiting */
658 if (atppc->sc_mode & ATPPC_MODE_ECP) {
659 if (atppc->sc_ctr_intr & PCD) {
660 if (atppc->sc_inb)
661 wake_up = READER;
662 else
663 claim = 0;
664 } else {
665 if (atppc->sc_outb)
666 wake_up = WRITER;
667 else
668 claim = 0;
670 } else {
671 if (atppc->sc_outb)
672 wake_up = WRITER;
673 else
674 claim = 0;
677 /* Determine if nFault has occurred */
678 if ((atppc->sc_mode & ATPPC_MODE_ECP) &&
679 (atppc->sc_ecr_intr & ATPPC_nFAULT_INTR) &&
680 !(atppc->sc_str_intr & nFAULT)) {
682 /* Device is requesting the channel */
683 atppc->sc_irqstat |= ATPPC_IRQ_nFAULT;
684 claim = 1;
686 break;
688 case ATPPC_MODE_EPP:
689 /* nAck pulsed for 5 usec, too fast to check reliably */
690 atppc->sc_irqstat = ATPPC_IRQ_nACK;
691 if (atppc->sc_inb)
692 wake_up = WRITER;
693 else if (atppc->sc_outb)
694 wake_up = READER;
695 else
696 claim = 0;
697 break;
699 default:
700 panic("%s: chipset is in invalid mode.", device_xname(dev));
703 if (claim) {
704 switch (wake_up) {
705 case NONE:
706 break;
708 case READER:
709 wakeup(atppc->sc_inb);
710 break;
712 case WRITER:
713 wakeup(atppc->sc_outb);
714 break;
718 /* Call all of the installed handlers */
719 if (claim) {
720 struct atppc_handler_node * callback;
721 SLIST_FOREACH(callback, &(atppc->sc_handler_listhead),
722 entries) {
723 (*callback->func)(callback->arg);
727 return claim;
731 /* Functions which support ppbus interface */
734 /* Check EPP mode timeout */
735 static int
736 atppc_check_epp_timeout(device_t dev)
738 struct atppc_softc *atppc = device_private(dev);
739 int s;
740 int error;
742 s = splatppc();
743 ATPPC_LOCK(atppc);
745 atppc_reset_epp_timeout(dev);
746 error = !(atppc_r_str(atppc) & TIMEOUT);
747 atppc_barrier_r(atppc);
749 ATPPC_UNLOCK(atppc);
750 splx(s);
752 return (error);
756 * EPP timeout, according to the PC87332 manual
757 * Semantics of clearing EPP timeout bit.
758 * PC87332 - reading SPP_STR does it...
759 * SMC - write 1 to EPP timeout bit XXX
760 * Others - (?) write 0 to EPP timeout bit
762 static void
763 atppc_reset_epp_timeout(device_t dev)
765 struct atppc_softc *atppc = device_private(dev);
766 register unsigned char r;
768 r = atppc_r_str(atppc);
769 atppc_barrier_r(atppc);
770 atppc_w_str(atppc, r | 0x1);
771 atppc_barrier_w(atppc);
772 atppc_w_str(atppc, r & 0xfe);
773 atppc_barrier_w(atppc);
775 return;
779 /* Read from atppc device: returns 0 on success. */
780 static int
781 atppc_read(device_t dev, char *buf, int len, int ioflag,
782 size_t *cnt)
784 struct atppc_softc *atppc = device_private(dev);
785 int error = 0;
786 int s;
788 s = splatppc();
789 ATPPC_LOCK(atppc);
791 *cnt = 0;
793 /* Initialize buffer */
794 atppc->sc_inb = atppc->sc_inbstart = buf;
795 atppc->sc_inb_nbytes = len;
797 /* Initialize device input error state for new operation */
798 atppc->sc_inerr = 0;
800 /* Call appropriate function to read bytes */
801 switch(atppc->sc_mode) {
802 case ATPPC_MODE_STD:
803 case ATPPC_MODE_FAST:
804 error = ENODEV;
805 break;
807 case ATPPC_MODE_NIBBLE:
808 atppc_nibble_read(atppc);
809 break;
811 case ATPPC_MODE_PS2:
812 atppc_byte_read(atppc);
813 break;
815 case ATPPC_MODE_ECP:
816 atppc_ecp_read(atppc);
817 break;
819 case ATPPC_MODE_EPP:
820 atppc_epp_read(atppc);
821 break;
823 default:
824 panic("%s(%s): chipset in invalid mode.\n", __func__,
825 device_xname(dev));
828 /* Update counter*/
829 *cnt = (atppc->sc_inbstart - atppc->sc_inb);
831 /* Reset buffer */
832 atppc->sc_inb = atppc->sc_inbstart = NULL;
833 atppc->sc_inb_nbytes = 0;
835 if (!(error))
836 error = atppc->sc_inerr;
838 ATPPC_UNLOCK(atppc);
839 splx(s);
841 return (error);
844 /* Write to atppc device: returns 0 on success. */
845 static int
846 atppc_write(device_t dev, char *buf, int len, int ioflag, size_t *cnt)
848 struct atppc_softc * const atppc = device_private(dev);
849 int error = 0;
850 int s;
852 *cnt = 0;
854 s = splatppc();
855 ATPPC_LOCK(atppc);
857 /* Set up line buffer */
858 atppc->sc_outb = atppc->sc_outbstart = buf;
859 atppc->sc_outb_nbytes = len;
861 /* Initialize device output error state for new operation */
862 atppc->sc_outerr = 0;
864 /* Call appropriate function to write bytes */
865 switch (atppc->sc_mode) {
866 case ATPPC_MODE_STD:
867 atppc_std_write(atppc);
868 break;
870 case ATPPC_MODE_NIBBLE:
871 case ATPPC_MODE_PS2:
872 error = ENODEV;
873 break;
875 case ATPPC_MODE_FAST:
876 case ATPPC_MODE_ECP:
877 atppc_fifo_write(atppc);
878 break;
880 case ATPPC_MODE_EPP:
881 atppc_epp_write(atppc);
882 break;
884 default:
885 panic("%s(%s): chipset in invalid mode.\n", __func__,
886 device_xname(dev));
889 /* Update counter*/
890 *cnt = (atppc->sc_outbstart - atppc->sc_outb);
892 /* Reset output buffer */
893 atppc->sc_outb = atppc->sc_outbstart = NULL;
894 atppc->sc_outb_nbytes = 0;
896 if (!(error))
897 error = atppc->sc_outerr;
899 ATPPC_UNLOCK(atppc);
900 splx(s);
902 return (error);
906 * Set mode of chipset to mode argument. Modes not supported are ignored. If
907 * multiple modes are flagged, the mode is not changed. Mode's are those
908 * defined for ppbus_softc.sc_mode in ppbus_conf.h. Only ECP-capable chipsets
909 * can change their mode of operation. However, ALL operation modes support
910 * centronics mode and nibble mode. Modes determine both hardware AND software
911 * behaviour.
912 * NOTE: the mode for ECP should only be changed when the channel is in
913 * forward idle mode. This function does not make sure FIFO's have flushed or
914 * any consistency checks.
916 static int
917 atppc_setmode(device_t dev, int mode)
919 struct atppc_softc *atppc = device_private(dev);
920 u_int8_t ecr;
921 u_int8_t chipset_mode;
922 int s;
923 int rval = 0;
925 s = splatppc();
926 ATPPC_LOCK(atppc);
928 /* If ECP capable, configure ecr register */
929 if (atppc->sc_has & ATPPC_HAS_ECP) {
930 /* Read ECR with mode masked out */
931 ecr = (atppc_r_ecr(atppc) & 0x1f);
932 atppc_barrier_r(atppc);
934 switch (mode) {
935 case PPBUS_ECP:
936 /* Set ECP mode */
937 ecr |= ATPPC_ECR_ECP;
938 chipset_mode = ATPPC_MODE_ECP;
939 break;
941 case PPBUS_EPP:
942 /* Set EPP mode */
943 if (atppc->sc_has & ATPPC_HAS_EPP) {
944 ecr |= ATPPC_ECR_EPP;
945 chipset_mode = ATPPC_MODE_EPP;
946 } else {
947 rval = ENODEV;
948 goto end;
950 break;
952 case PPBUS_FAST:
953 /* Set fast centronics mode */
954 ecr |= ATPPC_ECR_FIFO;
955 chipset_mode = ATPPC_MODE_FAST;
956 break;
958 case PPBUS_PS2:
959 /* Set PS2 mode */
960 ecr |= ATPPC_ECR_PS2;
961 chipset_mode = ATPPC_MODE_PS2;
962 break;
964 case PPBUS_COMPATIBLE:
965 /* Set standard mode */
966 ecr |= ATPPC_ECR_STD;
967 chipset_mode = ATPPC_MODE_STD;
968 break;
970 case PPBUS_NIBBLE:
971 /* Set nibble mode: uses chipset standard mode */
972 ecr |= ATPPC_ECR_STD;
973 chipset_mode = ATPPC_MODE_NIBBLE;
974 break;
976 default:
977 /* Invalid mode specified for ECP chip */
978 ATPPC_DPRINTF(("%s(%s): invalid mode passed as "
979 "argument.\n", __func__, device_xname(dev)));
980 rval = ENODEV;
981 goto end;
984 /* Switch to byte mode to be able to change modes. */
985 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
986 atppc_barrier_w(atppc);
988 /* Update mode */
989 atppc_w_ecr(atppc, ecr);
990 atppc_barrier_w(atppc);
991 } else {
992 switch (mode) {
993 case PPBUS_EPP:
994 if (atppc->sc_has & ATPPC_HAS_EPP) {
995 chipset_mode = ATPPC_MODE_EPP;
996 } else {
997 rval = ENODEV;
998 goto end;
1000 break;
1002 case PPBUS_PS2:
1003 if (atppc->sc_has & ATPPC_HAS_PS2) {
1004 chipset_mode = ATPPC_MODE_PS2;
1005 } else {
1006 rval = ENODEV;
1007 goto end;
1009 break;
1011 case PPBUS_NIBBLE:
1012 /* Set nibble mode (virtual) */
1013 chipset_mode = ATPPC_MODE_NIBBLE;
1014 break;
1016 case PPBUS_COMPATIBLE:
1017 chipset_mode = ATPPC_MODE_STD;
1018 break;
1020 case PPBUS_ECP:
1021 rval = ENODEV;
1022 goto end;
1024 default:
1025 ATPPC_DPRINTF(("%s(%s): invalid mode passed as "
1026 "argument.\n", __func__, device_xname(dev)));
1027 rval = ENODEV;
1028 goto end;
1032 atppc->sc_mode = chipset_mode;
1033 if (chipset_mode == ATPPC_MODE_PS2) {
1034 /* Set direction bit to reverse */
1035 ecr = atppc_r_ctr(atppc);
1036 atppc_barrier_r(atppc);
1037 ecr |= PCD;
1038 atppc_w_ctr(atppc, ecr);
1039 atppc_barrier_w(atppc);
1042 end:
1043 ATPPC_UNLOCK(atppc);
1044 splx(s);
1046 return rval;
1049 /* Get the current mode of chipset */
1050 static int
1051 atppc_getmode(device_t dev)
1053 struct atppc_softc *atppc = device_private(dev);
1054 int mode;
1055 int s;
1057 s = splatppc();
1058 ATPPC_LOCK(atppc);
1060 /* The chipset can only be in one mode at a time logically */
1061 switch (atppc->sc_mode) {
1062 case ATPPC_MODE_ECP:
1063 mode = PPBUS_ECP;
1064 break;
1066 case ATPPC_MODE_EPP:
1067 mode = PPBUS_EPP;
1068 break;
1070 case ATPPC_MODE_PS2:
1071 mode = PPBUS_PS2;
1072 break;
1074 case ATPPC_MODE_STD:
1075 mode = PPBUS_COMPATIBLE;
1076 break;
1078 case ATPPC_MODE_NIBBLE:
1079 mode = PPBUS_NIBBLE;
1080 break;
1082 case ATPPC_MODE_FAST:
1083 mode = PPBUS_FAST;
1084 break;
1086 default:
1087 panic("%s(%s): device is in invalid mode!", __func__,
1088 device_xname(dev));
1089 break;
1092 ATPPC_UNLOCK(atppc);
1093 splx(s);
1095 return mode;
1099 /* Wait for FIFO buffer to empty for ECP-capable chipset */
1100 static void
1101 atppc_ecp_sync(device_t dev)
1103 struct atppc_softc *atppc = device_private(dev);
1104 int i;
1105 int s;
1106 u_int8_t r;
1108 s = splatppc();
1109 ATPPC_LOCK(atppc);
1112 * Only wait for FIFO to empty if mode is chipset is ECP-capable AND
1113 * the mode is either ECP or Fast Centronics.
1115 r = atppc_r_ecr(atppc);
1116 atppc_barrier_r(atppc);
1117 r &= 0xe0;
1118 if (!(atppc->sc_has & ATPPC_HAS_ECP) || ((r != ATPPC_ECR_ECP)
1119 && (r != ATPPC_ECR_FIFO))) {
1120 goto end;
1123 /* Wait for FIFO to empty */
1124 for (i = 0; i < ((MAXBUSYWAIT/hz) * 1000000); i += 100) {
1125 r = atppc_r_ecr(atppc);
1126 atppc_barrier_r(atppc);
1127 if (r & ATPPC_FIFO_EMPTY) {
1128 goto end;
1130 delay(100); /* Supposed to be a 100 usec delay */
1133 ATPPC_DPRINTF(("%s: ECP sync failed, data still in FIFO.\n",
1134 device_xname(dev)));
1136 end:
1137 ATPPC_UNLOCK(atppc);
1138 splx(s);
1140 return;
1143 /* Execute a microsequence to handle fast I/O operations. */
1144 static int
1145 atppc_exec_microseq(device_t dev, struct ppbus_microseq **p_msq)
1147 struct atppc_softc *atppc = device_private(dev);
1148 struct ppbus_microseq *mi = *p_msq;
1149 char cc, *p;
1150 int i, iter, len;
1151 int error;
1152 int s;
1153 register int reg;
1154 register unsigned char mask;
1155 register int accum = 0;
1156 register char *ptr = NULL;
1157 struct ppbus_microseq *stack = NULL;
1159 s = splatppc();
1160 ATPPC_LOCK(atppc);
1162 /* microsequence registers are equivalent to PC-like port registers */
1164 #define r_reg(register,atppc) bus_space_read_1((atppc)->sc_iot, \
1165 (atppc)->sc_ioh, (register))
1166 #define w_reg(register, atppc, byte) bus_space_write_1((atppc)->sc_iot, \
1167 (atppc)->sc_ioh, (register), (byte))
1169 /* Loop until microsequence execution finishes (ending op code) */
1170 for (;;) {
1171 switch (mi->opcode) {
1172 case MS_OP_RSET:
1173 cc = r_reg(mi->arg[0].i, atppc);
1174 atppc_barrier_r(atppc);
1175 cc &= (char)mi->arg[2].i; /* clear mask */
1176 cc |= (char)mi->arg[1].i; /* assert mask */
1177 w_reg(mi->arg[0].i, atppc, cc);
1178 atppc_barrier_w(atppc);
1179 mi++;
1180 break;
1182 case MS_OP_RASSERT_P:
1183 reg = mi->arg[1].i;
1184 ptr = atppc->sc_ptr;
1186 if ((len = mi->arg[0].i) == MS_ACCUM) {
1187 accum = atppc->sc_accum;
1188 for (; accum; accum--) {
1189 w_reg(reg, atppc, *ptr++);
1190 atppc_barrier_w(atppc);
1192 atppc->sc_accum = accum;
1193 } else {
1194 for (i = 0; i < len; i++) {
1195 w_reg(reg, atppc, *ptr++);
1196 atppc_barrier_w(atppc);
1200 atppc->sc_ptr = ptr;
1201 mi++;
1202 break;
1204 case MS_OP_RFETCH_P:
1205 reg = mi->arg[1].i;
1206 mask = (char)mi->arg[2].i;
1207 ptr = atppc->sc_ptr;
1209 if ((len = mi->arg[0].i) == MS_ACCUM) {
1210 accum = atppc->sc_accum;
1211 for (; accum; accum--) {
1212 *ptr++ = r_reg(reg, atppc) & mask;
1213 atppc_barrier_r(atppc);
1215 atppc->sc_accum = accum;
1216 } else {
1217 for (i = 0; i < len; i++) {
1218 *ptr++ = r_reg(reg, atppc) & mask;
1219 atppc_barrier_r(atppc);
1223 atppc->sc_ptr = ptr;
1224 mi++;
1225 break;
1227 case MS_OP_RFETCH:
1228 *((char *)mi->arg[2].p) = r_reg(mi->arg[0].i, atppc) &
1229 (char)mi->arg[1].i;
1230 atppc_barrier_r(atppc);
1231 mi++;
1232 break;
1234 case MS_OP_RASSERT:
1235 case MS_OP_DELAY:
1236 /* let's suppose the next instr. is the same */
1237 do {
1238 for (;mi->opcode == MS_OP_RASSERT; mi++) {
1239 w_reg(mi->arg[0].i, atppc,
1240 (char)mi->arg[1].i);
1241 atppc_barrier_w(atppc);
1244 for (;mi->opcode == MS_OP_DELAY; mi++) {
1245 delay(mi->arg[0].i);
1247 } while (mi->opcode == MS_OP_RASSERT);
1248 break;
1250 case MS_OP_ADELAY:
1251 if (mi->arg[0].i) {
1252 tsleep(atppc, PPBUSPRI, "atppcdelay",
1253 mi->arg[0].i * (hz/1000));
1255 mi++;
1256 break;
1258 case MS_OP_TRIG:
1259 reg = mi->arg[0].i;
1260 iter = mi->arg[1].i;
1261 p = (char *)mi->arg[2].p;
1263 /* XXX delay limited to 255 us */
1264 for (i = 0; i < iter; i++) {
1265 w_reg(reg, atppc, *p++);
1266 atppc_barrier_w(atppc);
1267 delay((unsigned char)*p++);
1270 mi++;
1271 break;
1273 case MS_OP_SET:
1274 atppc->sc_accum = mi->arg[0].i;
1275 mi++;
1276 break;
1278 case MS_OP_DBRA:
1279 if (--atppc->sc_accum > 0) {
1280 mi += mi->arg[0].i;
1283 mi++;
1284 break;
1286 case MS_OP_BRSET:
1287 cc = atppc_r_str(atppc);
1288 atppc_barrier_r(atppc);
1289 if ((cc & (char)mi->arg[0].i) == (char)mi->arg[0].i) {
1290 mi += mi->arg[1].i;
1292 mi++;
1293 break;
1295 case MS_OP_BRCLEAR:
1296 cc = atppc_r_str(atppc);
1297 atppc_barrier_r(atppc);
1298 if ((cc & (char)mi->arg[0].i) == 0) {
1299 mi += mi->arg[1].i;
1301 mi++;
1302 break;
1304 case MS_OP_BRSTAT:
1305 cc = atppc_r_str(atppc);
1306 atppc_barrier_r(atppc);
1307 if ((cc & ((char)mi->arg[0].i | (char)mi->arg[1].i)) ==
1308 (char)mi->arg[0].i) {
1309 mi += mi->arg[2].i;
1311 mi++;
1312 break;
1314 case MS_OP_C_CALL:
1316 * If the C call returns !0 then end the microseq.
1317 * The current state of ptr is passed to the C function
1319 if ((error = mi->arg[0].f(mi->arg[1].p,
1320 atppc->sc_ptr))) {
1321 ATPPC_UNLOCK(atppc);
1322 splx(s);
1323 return (error);
1325 mi++;
1326 break;
1328 case MS_OP_PTR:
1329 atppc->sc_ptr = (char *)mi->arg[0].p;
1330 mi++;
1331 break;
1333 case MS_OP_CALL:
1334 if (stack) {
1335 panic("%s - %s: too much calls", device_xname(dev),
1336 __func__);
1339 if (mi->arg[0].p) {
1340 /* store state of the actual microsequence */
1341 stack = mi;
1343 /* jump to the new microsequence */
1344 mi = (struct ppbus_microseq *)mi->arg[0].p;
1345 } else {
1346 mi++;
1348 break;
1350 case MS_OP_SUBRET:
1351 /* retrieve microseq and pc state before the call */
1352 mi = stack;
1354 /* reset the stack */
1355 stack = 0;
1357 /* XXX return code */
1359 mi++;
1360 break;
1362 case MS_OP_PUT:
1363 case MS_OP_GET:
1364 case MS_OP_RET:
1366 * Can't return to atppc level during the execution
1367 * of a submicrosequence.
1369 if (stack) {
1370 panic("%s: cannot return to atppc level",
1371 __func__);
1373 /* update pc for atppc level of execution */
1374 *p_msq = mi;
1376 ATPPC_UNLOCK(atppc);
1377 splx(s);
1378 return (0);
1379 break;
1381 default:
1382 panic("%s: unknown microsequence "
1383 "opcode 0x%x", __func__, mi->opcode);
1384 break;
1388 /* Should not be reached! */
1389 #ifdef ATPPC_DEBUG
1390 panic("%s: unexpected code reached!\n", __func__);
1391 #endif
1394 /* General I/O routine */
1395 static u_int8_t
1396 atppc_io(device_t dev, int iop, u_char *addr, int cnt, u_char byte)
1398 struct atppc_softc *atppc = device_private(dev);
1399 u_int8_t val = 0;
1400 int s;
1402 s = splatppc();
1403 ATPPC_LOCK(atppc);
1405 switch (iop) {
1406 case PPBUS_OUTSB_EPP:
1407 bus_space_write_multi_1(atppc->sc_iot, atppc->sc_ioh,
1408 ATPPC_EPP_DATA, addr, cnt);
1409 break;
1410 case PPBUS_OUTSW_EPP:
1411 bus_space_write_multi_2(atppc->sc_iot, atppc->sc_ioh,
1412 ATPPC_EPP_DATA, (u_int16_t *)addr, cnt);
1413 break;
1414 case PPBUS_OUTSL_EPP:
1415 bus_space_write_multi_4(atppc->sc_iot, atppc->sc_ioh,
1416 ATPPC_EPP_DATA, (u_int32_t *)addr, cnt);
1417 break;
1418 case PPBUS_INSB_EPP:
1419 bus_space_read_multi_1(atppc->sc_iot, atppc->sc_ioh,
1420 ATPPC_EPP_DATA, addr, cnt);
1421 break;
1422 case PPBUS_INSW_EPP:
1423 bus_space_read_multi_2(atppc->sc_iot, atppc->sc_ioh,
1424 ATPPC_EPP_DATA, (u_int16_t *)addr, cnt);
1425 break;
1426 case PPBUS_INSL_EPP:
1427 bus_space_read_multi_4(atppc->sc_iot, atppc->sc_ioh,
1428 ATPPC_EPP_DATA, (u_int32_t *)addr, cnt);
1429 break;
1430 case PPBUS_RDTR:
1431 val = (atppc_r_dtr(atppc));
1432 break;
1433 case PPBUS_RSTR:
1434 val = (atppc_r_str(atppc));
1435 break;
1436 case PPBUS_RCTR:
1437 val = (atppc_r_ctr(atppc));
1438 break;
1439 case PPBUS_REPP_A:
1440 val = (atppc_r_eppA(atppc));
1441 break;
1442 case PPBUS_REPP_D:
1443 val = (atppc_r_eppD(atppc));
1444 break;
1445 case PPBUS_RECR:
1446 val = (atppc_r_ecr(atppc));
1447 break;
1448 case PPBUS_RFIFO:
1449 val = (atppc_r_fifo(atppc));
1450 break;
1451 case PPBUS_WDTR:
1452 atppc_w_dtr(atppc, byte);
1453 break;
1454 case PPBUS_WSTR:
1455 atppc_w_str(atppc, byte);
1456 break;
1457 case PPBUS_WCTR:
1458 atppc_w_ctr(atppc, byte);
1459 break;
1460 case PPBUS_WEPP_A:
1461 atppc_w_eppA(atppc, byte);
1462 break;
1463 case PPBUS_WEPP_D:
1464 atppc_w_eppD(atppc, byte);
1465 break;
1466 case PPBUS_WECR:
1467 atppc_w_ecr(atppc, byte);
1468 break;
1469 case PPBUS_WFIFO:
1470 atppc_w_fifo(atppc, byte);
1471 break;
1472 default:
1473 panic("%s(%s): unknown I/O operation", device_xname(dev),
1474 __func__);
1475 break;
1478 atppc_barrier(atppc);
1480 ATPPC_UNLOCK(atppc);
1481 splx(s);
1483 return val;
1486 /* Read "instance variables" of atppc device */
1487 static int
1488 atppc_read_ivar(device_t dev, int index, unsigned int *val)
1490 struct atppc_softc *atppc = device_private(dev);
1491 int rval = 0;
1492 int s;
1494 s = splatppc();
1495 ATPPC_LOCK(atppc);
1497 switch(index) {
1498 case PPBUS_IVAR_EPP_PROTO:
1499 if (atppc->sc_epp == ATPPC_EPP_1_9)
1500 *val = PPBUS_EPP_1_9;
1501 else if (atppc->sc_epp == ATPPC_EPP_1_7)
1502 *val = PPBUS_EPP_1_7;
1503 /* XXX what if not using EPP ? */
1504 break;
1506 case PPBUS_IVAR_INTR:
1507 *val = ((atppc->sc_use & ATPPC_USE_INTR) != 0);
1508 break;
1510 case PPBUS_IVAR_DMA:
1511 *val = ((atppc->sc_use & ATPPC_USE_DMA) != 0);
1512 break;
1514 default:
1515 rval = ENODEV;
1518 ATPPC_UNLOCK(atppc);
1519 splx(s);
1521 return rval;
1524 /* Write "instance varaibles" of atppc device */
1525 static int
1526 atppc_write_ivar(device_t dev, int index, unsigned int *val)
1528 struct atppc_softc *atppc = device_private(dev);
1529 int rval = 0;
1530 int s;
1532 s = splatppc();
1533 ATPPC_LOCK(atppc);
1535 switch(index) {
1536 case PPBUS_IVAR_EPP_PROTO:
1537 if (*val == PPBUS_EPP_1_9 || *val == PPBUS_EPP_1_7)
1538 atppc->sc_epp = *val;
1539 else
1540 rval = EINVAL;
1541 break;
1543 case PPBUS_IVAR_INTR:
1544 if (*val == 0)
1545 atppc->sc_use &= ~ATPPC_USE_INTR;
1546 else if (atppc->sc_has & ATPPC_HAS_INTR)
1547 atppc->sc_use |= ATPPC_USE_INTR;
1548 else
1549 rval = ENODEV;
1550 break;
1552 case PPBUS_IVAR_DMA:
1553 if (*val == 0)
1554 atppc->sc_use &= ~ATPPC_USE_DMA;
1555 else if (atppc->sc_has & ATPPC_HAS_DMA)
1556 atppc->sc_use |= ATPPC_USE_DMA;
1557 else
1558 rval = ENODEV;
1559 break;
1561 default:
1562 rval = ENODEV;
1565 ATPPC_UNLOCK(atppc);
1566 splx(s);
1568 return rval;
1571 /* Add a handler routine to be called by the interrupt handler */
1572 static int
1573 atppc_add_handler(device_t dev, void (*handler)(void *), void *arg)
1575 struct atppc_softc *atppc = device_private(dev);
1576 struct atppc_handler_node *callback;
1577 int rval = 0;
1578 int s;
1580 s = splatppc();
1581 ATPPC_LOCK(atppc);
1583 if (handler == NULL) {
1584 ATPPC_DPRINTF(("%s(%s): attempt to register NULL handler.\n",
1585 __func__, device_xname(dev)));
1586 rval = EINVAL;
1587 } else {
1588 callback = malloc(sizeof(struct atppc_handler_node), M_DEVBUF,
1589 M_NOWAIT);
1590 if (callback) {
1591 callback->func = handler;
1592 callback->arg = arg;
1593 SLIST_INSERT_HEAD(&(atppc->sc_handler_listhead),
1594 callback, entries);
1595 } else {
1596 rval = ENOMEM;
1600 ATPPC_UNLOCK(atppc);
1601 splx(s);
1603 return rval;
1606 /* Remove a handler added by atppc_add_handler() */
1607 static int
1608 atppc_remove_handler(device_t dev, void (*handler)(void *))
1610 struct atppc_softc *atppc = device_private(dev);
1611 struct atppc_handler_node *callback;
1612 int rval = EINVAL;
1613 int s;
1615 s = splatppc();
1616 ATPPC_LOCK(atppc);
1618 if (SLIST_EMPTY(&(atppc->sc_handler_listhead)))
1619 panic("%s(%s): attempt to remove handler from empty list.\n",
1620 __func__, device_xname(dev));
1622 /* Search list for handler */
1623 SLIST_FOREACH(callback, &(atppc->sc_handler_listhead), entries) {
1624 if (callback->func == handler) {
1625 SLIST_REMOVE(&(atppc->sc_handler_listhead), callback,
1626 atppc_handler_node, entries);
1627 free(callback, M_DEVBUF);
1628 rval = 0;
1629 break;
1633 ATPPC_UNLOCK(atppc);
1634 splx(s);
1636 return rval;
1639 /* Utility functions */
1643 * Functions that read bytes from port into buffer: called from interrupt
1644 * handler depending on current chipset mode and cause of interrupt. Return
1645 * value: number of bytes moved.
1648 /* Only the lower 4 bits of the final value are valid */
1649 #define nibble2char(s) ((((s) & ~nACK) >> 3) | (~(s) & nBUSY) >> 4)
1651 /* Read bytes in nibble mode */
1652 static void
1653 atppc_nibble_read(struct atppc_softc *atppc)
1655 int i;
1656 u_int8_t nibble[2];
1657 u_int8_t ctr;
1658 u_int8_t str;
1660 /* Enable interrupts if needed */
1661 if (atppc->sc_use & ATPPC_USE_INTR) {
1662 ctr = atppc_r_ctr(atppc);
1663 atppc_barrier_r(atppc);
1664 if (!(ctr & IRQENABLE)) {
1665 ctr |= IRQENABLE;
1666 atppc_w_ctr(atppc, ctr);
1667 atppc_barrier_w(atppc);
1671 while (atppc->sc_inbstart < (atppc->sc_inb + atppc->sc_inb_nbytes)) {
1672 /* Check if device has data to send in idle phase */
1673 str = atppc_r_str(atppc);
1674 atppc_barrier_r(atppc);
1675 if (str & nDATAVAIL) {
1676 return;
1679 /* Nibble-mode handshake transfer */
1680 for (i = 0; i < 2; i++) {
1681 /* Event 7 - ready to take data (HOSTBUSY low) */
1682 ctr = atppc_r_ctr(atppc);
1683 atppc_barrier_r(atppc);
1684 ctr |= HOSTBUSY;
1685 atppc_w_ctr(atppc, ctr);
1686 atppc_barrier_w(atppc);
1688 /* Event 8 - peripheral writes the first nibble */
1690 /* Event 9 - peripheral set nAck low */
1691 atppc->sc_inerr = atppc_poll_str(atppc, 0, PTRCLK);
1692 if (atppc->sc_inerr)
1693 return;
1695 /* read nibble */
1696 nibble[i] = atppc_r_str(atppc);
1698 /* Event 10 - ack, nibble received */
1699 ctr &= ~HOSTBUSY;
1700 atppc_w_ctr(atppc, ctr);
1702 /* Event 11 - wait ack from peripherial */
1703 if (atppc->sc_use & ATPPC_USE_INTR)
1704 atppc->sc_inerr = atppc_wait_interrupt(atppc,
1705 atppc->sc_inb, ATPPC_IRQ_nACK);
1706 else
1707 atppc->sc_inerr = atppc_poll_str(atppc, PTRCLK,
1708 PTRCLK);
1709 if (atppc->sc_inerr)
1710 return;
1713 /* Store byte transfered */
1714 *(atppc->sc_inbstart) = ((nibble2char(nibble[1]) << 4) & 0xf0) |
1715 (nibble2char(nibble[0]) & 0x0f);
1716 atppc->sc_inbstart++;
1720 /* Read bytes in bidirectional mode */
1721 static void
1722 atppc_byte_read(struct atppc_softc * const atppc)
1724 u_int8_t ctr;
1725 u_int8_t str;
1727 /* Check direction bit */
1728 ctr = atppc_r_ctr(atppc);
1729 atppc_barrier_r(atppc);
1730 if (!(ctr & PCD)) {
1731 ATPPC_DPRINTF(("%s: byte-mode read attempted without direction "
1732 "bit set.", device_xname(atppc->sc_dev)));
1733 atppc->sc_inerr = ENODEV;
1734 return;
1736 /* Enable interrupts if needed */
1737 if (atppc->sc_use & ATPPC_USE_INTR) {
1738 if (!(ctr & IRQENABLE)) {
1739 ctr |= IRQENABLE;
1740 atppc_w_ctr(atppc, ctr);
1741 atppc_barrier_w(atppc);
1745 /* Byte-mode handshake transfer */
1746 while (atppc->sc_inbstart < (atppc->sc_inb + atppc->sc_inb_nbytes)) {
1747 /* Check if device has data to send */
1748 str = atppc_r_str(atppc);
1749 atppc_barrier_r(atppc);
1750 if (str & nDATAVAIL) {
1751 return;
1754 /* Event 7 - ready to take data (nAUTO low) */
1755 ctr |= HOSTBUSY;
1756 atppc_w_ctr(atppc, ctr);
1757 atppc_barrier_w(atppc);
1759 /* Event 9 - peripheral set nAck low */
1760 atppc->sc_inerr = atppc_poll_str(atppc, 0, PTRCLK);
1761 if (atppc->sc_inerr)
1762 return;
1764 /* Store byte transfered */
1765 *(atppc->sc_inbstart) = atppc_r_dtr(atppc);
1766 atppc_barrier_r(atppc);
1768 /* Event 10 - data received, can't accept more */
1769 ctr &= ~HOSTBUSY;
1770 atppc_w_ctr(atppc, ctr);
1771 atppc_barrier_w(atppc);
1773 /* Event 11 - peripheral ack */
1774 if (atppc->sc_use & ATPPC_USE_INTR)
1775 atppc->sc_inerr = atppc_wait_interrupt(atppc,
1776 atppc->sc_inb, ATPPC_IRQ_nACK);
1777 else
1778 atppc->sc_inerr = atppc_poll_str(atppc, PTRCLK, PTRCLK);
1779 if (atppc->sc_inerr)
1780 return;
1782 /* Event 16 - strobe */
1783 str |= HOSTCLK;
1784 atppc_w_str(atppc, str);
1785 atppc_barrier_w(atppc);
1786 DELAY(1);
1787 str &= ~HOSTCLK;
1788 atppc_w_str(atppc, str);
1789 atppc_barrier_w(atppc);
1791 /* Update counter */
1792 atppc->sc_inbstart++;
1796 /* Read bytes in EPP mode */
1797 static void
1798 atppc_epp_read(struct atppc_softc * atppc)
1800 if (atppc->sc_epp == ATPPC_EPP_1_9) {
1802 uint8_t str;
1803 int i;
1805 atppc_reset_epp_timeout(atppc->sc_dev);
1806 for (i = 0; i < atppc->sc_inb_nbytes; i++) {
1807 *(atppc->sc_inbstart) = atppc_r_eppD(atppc);
1808 atppc_barrier_r(atppc);
1809 str = atppc_r_str(atppc);
1810 atppc_barrier_r(atppc);
1811 if (str & TIMEOUT) {
1812 atppc->sc_inerr = EIO;
1813 break;
1815 atppc->sc_inbstart++;
1818 } else {
1819 /* Read data block from EPP data register */
1820 atppc_r_eppD_multi(atppc, atppc->sc_inbstart,
1821 atppc->sc_inb_nbytes);
1822 atppc_barrier_r(atppc);
1823 /* Update buffer position, byte count and counter */
1824 atppc->sc_inbstart += atppc->sc_inb_nbytes;
1827 return;
1830 /* Read bytes in ECP mode */
1831 static void
1832 atppc_ecp_read(struct atppc_softc *atppc)
1834 u_int8_t ecr;
1835 u_int8_t ctr;
1836 u_int8_t str;
1837 const unsigned char ctr_sav = atppc_r_ctr(atppc);
1838 const unsigned char ecr_sav = atppc_r_ecr(atppc);
1839 unsigned int worklen;
1841 /* Check direction bit */
1842 ctr = ctr_sav;
1843 atppc_barrier_r(atppc);
1844 if (!(ctr & PCD)) {
1845 ATPPC_DPRINTF(("%s: ecp-mode read attempted without direction "
1846 "bit set.", device_xname(atppc->sc_dev)));
1847 atppc->sc_inerr = ENODEV;
1848 goto end;
1851 /* Clear device request if any */
1852 if (atppc->sc_use & ATPPC_USE_INTR)
1853 atppc->sc_irqstat &= ~ATPPC_IRQ_nFAULT;
1855 while (atppc->sc_inbstart < (atppc->sc_inb + atppc->sc_inb_nbytes)) {
1856 ecr = atppc_r_ecr(atppc);
1857 atppc_barrier_r(atppc);
1858 if (ecr & ATPPC_FIFO_EMPTY) {
1859 /* Check for invalid state */
1860 if (ecr & ATPPC_FIFO_FULL) {
1861 atppc_ecp_read_error(atppc);
1862 break;
1865 /* Check if device has data to send */
1866 str = atppc_r_str(atppc);
1867 atppc_barrier_r(atppc);
1868 if (str & nDATAVAIL) {
1869 break;
1872 if (atppc->sc_use & ATPPC_USE_INTR) {
1873 /* Enable interrupts */
1874 ecr &= ~ATPPC_SERVICE_INTR;
1875 atppc_w_ecr(atppc, ecr);
1876 atppc_barrier_w(atppc);
1877 /* Wait for FIFO to fill */
1878 atppc->sc_inerr = atppc_wait_interrupt(atppc,
1879 atppc->sc_inb, ATPPC_IRQ_FIFO);
1880 if (atppc->sc_inerr)
1881 break;
1882 } else {
1883 DELAY(1);
1885 continue;
1887 else if (ecr & ATPPC_FIFO_FULL) {
1888 /* Transfer sc_fifo bytes */
1889 worklen = atppc->sc_fifo;
1891 else if (ecr & ATPPC_SERVICE_INTR) {
1892 /* Transfer sc_rthr bytes */
1893 worklen = atppc->sc_rthr;
1894 } else {
1895 /* At least one byte is in the FIFO */
1896 worklen = 1;
1899 if ((atppc->sc_use & ATPPC_USE_INTR) &&
1900 (atppc->sc_use & ATPPC_USE_DMA)) {
1902 atppc_ecp_read_dma(atppc, &worklen, ecr);
1903 } else {
1904 atppc_ecp_read_pio(atppc, &worklen, ecr);
1907 if (atppc->sc_inerr) {
1908 atppc_ecp_read_error(atppc);
1909 break;
1912 /* Update counter */
1913 atppc->sc_inbstart += worklen;
1915 end:
1916 atppc_w_ctr(atppc, ctr_sav);
1917 atppc_w_ecr(atppc, ecr_sav);
1918 atppc_barrier_w(atppc);
1921 /* Read bytes in ECP mode using DMA transfers */
1922 static void
1923 atppc_ecp_read_dma(struct atppc_softc *atppc, unsigned int *length,
1924 unsigned char ecr)
1926 /* Limit transfer to maximum DMA size and start it */
1927 *length = min(*length, atppc->sc_dma_maxsize);
1928 atppc->sc_dmastat = ATPPC_DMA_INIT;
1929 atppc->sc_dma_start(atppc, atppc->sc_inbstart, *length,
1930 ATPPC_DMA_MODE_READ);
1932 atppc->sc_dmastat = ATPPC_DMA_STARTED;
1934 /* Enable interrupts, DMA */
1935 ecr &= ~ATPPC_SERVICE_INTR;
1936 ecr |= ATPPC_ENABLE_DMA;
1937 atppc_w_ecr(atppc, ecr);
1938 atppc_barrier_w(atppc);
1940 /* Wait for DMA completion */
1941 atppc->sc_inerr = atppc_wait_interrupt(atppc, atppc->sc_inb,
1942 ATPPC_IRQ_DMA);
1943 if (atppc->sc_inerr)
1944 return;
1946 /* Get register value recorded by interrupt handler */
1947 ecr = atppc->sc_ecr_intr;
1948 /* Clear DMA programming */
1949 atppc->sc_dma_finish(atppc);
1950 atppc->sc_dmastat = ATPPC_DMA_COMPLETE;
1951 /* Disable DMA */
1952 ecr &= ~ATPPC_ENABLE_DMA;
1953 atppc_w_ecr(atppc, ecr);
1954 atppc_barrier_w(atppc);
1957 /* Read bytes in ECP mode using PIO transfers */
1958 static void
1959 atppc_ecp_read_pio(struct atppc_softc *atppc, unsigned int *length,
1960 unsigned char ecr)
1962 /* Disable DMA */
1963 ecr &= ~ATPPC_ENABLE_DMA;
1964 atppc_w_ecr(atppc, ecr);
1965 atppc_barrier_w(atppc);
1967 /* Read from FIFO */
1968 atppc_r_fifo_multi(atppc, atppc->sc_inbstart, *length);
1971 /* Handle errors for ECP reads */
1972 static void
1973 atppc_ecp_read_error(struct atppc_softc *atppc)
1975 unsigned char ecr = atppc_r_ecr(atppc);
1977 /* Abort DMA if not finished */
1978 if (atppc->sc_dmastat == ATPPC_DMA_STARTED) {
1979 atppc->sc_dma_abort(atppc);
1980 ATPPC_DPRINTF(("%s: DMA interrupted.\n", __func__));
1983 /* Check for invalid states */
1984 if ((ecr & ATPPC_FIFO_EMPTY) && (ecr & ATPPC_FIFO_FULL)) {
1985 ATPPC_DPRINTF(("%s: FIFO full+empty bits set.\n", __func__));
1986 ATPPC_DPRINTF(("%s: reseting FIFO.\n", __func__));
1987 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
1988 atppc_barrier_w(atppc);
1993 * Functions that write bytes to port from buffer: called from atppc_write()
1994 * function depending on current chipset mode. Returns number of bytes moved.
1997 /* Write bytes in std/bidirectional mode */
1998 static void
1999 atppc_std_write(struct atppc_softc * const atppc)
2001 unsigned int timecount;
2002 unsigned char ctr;
2004 ctr = atppc_r_ctr(atppc);
2005 atppc_barrier_r(atppc);
2006 /* Enable interrupts if needed */
2007 if (atppc->sc_use & ATPPC_USE_INTR) {
2008 if (!(ctr & IRQENABLE)) {
2009 ctr |= IRQENABLE;
2010 atppc_w_ctr(atppc, ctr);
2011 atppc_barrier_w(atppc);
2015 while (atppc->sc_outbstart < (atppc->sc_outb + atppc->sc_outb_nbytes)) {
2016 /* Wait for peripheral to become ready for MAXBUSYWAIT */
2017 atppc->sc_outerr = atppc_poll_str(atppc, SPP_READY, SPP_MASK);
2018 if (atppc->sc_outerr)
2019 return;
2021 /* Put data in data register */
2022 atppc_w_dtr(atppc, *(atppc->sc_outbstart));
2023 atppc_barrier_w(atppc);
2024 DELAY(1);
2026 /* Pulse strobe to indicate valid data on lines */
2027 ctr |= STROBE;
2028 atppc_w_ctr(atppc, ctr);
2029 atppc_barrier_w(atppc);
2030 DELAY(1);
2031 ctr &= ~STROBE;
2032 atppc_w_ctr(atppc, ctr);
2033 atppc_barrier_w(atppc);
2035 /* Wait for nACK for MAXBUSYWAIT */
2036 timecount = 0;
2037 if (atppc->sc_use & ATPPC_USE_INTR) {
2038 atppc->sc_outerr = atppc_wait_interrupt(atppc,
2039 atppc->sc_outb, ATPPC_IRQ_nACK);
2040 if (atppc->sc_outerr)
2041 return;
2042 } else {
2043 /* Try to catch the pulsed acknowledgement */
2044 atppc->sc_outerr = atppc_poll_str(atppc, 0, nACK);
2045 if (atppc->sc_outerr)
2046 return;
2047 atppc->sc_outerr = atppc_poll_str(atppc, nACK, nACK);
2048 if (atppc->sc_outerr)
2049 return;
2052 /* Update buffer position, byte count and counter */
2053 atppc->sc_outbstart++;
2058 /* Write bytes in EPP mode */
2059 static void
2060 atppc_epp_write(struct atppc_softc *atppc)
2062 if (atppc->sc_epp == ATPPC_EPP_1_9) {
2064 uint8_t str;
2065 int i;
2067 atppc_reset_epp_timeout(atppc->sc_dev);
2068 for (i = 0; i < atppc->sc_outb_nbytes; i++) {
2069 atppc_w_eppD(atppc, *(atppc->sc_outbstart));
2070 atppc_barrier_w(atppc);
2071 str = atppc_r_str(atppc);
2072 atppc_barrier_r(atppc);
2073 if (str & TIMEOUT) {
2074 atppc->sc_outerr = EIO;
2075 break;
2077 atppc->sc_outbstart++;
2080 } else {
2081 /* Write data block to EPP data register */
2082 atppc_w_eppD_multi(atppc, atppc->sc_outbstart,
2083 atppc->sc_outb_nbytes);
2084 atppc_barrier_w(atppc);
2085 /* Update buffer position, byte count and counter */
2086 atppc->sc_outbstart += atppc->sc_outb_nbytes;
2089 return;
2093 /* Write bytes in ECP/Fast Centronics mode */
2094 static void
2095 atppc_fifo_write(struct atppc_softc * const atppc)
2097 unsigned char ctr;
2098 unsigned char ecr;
2099 const unsigned char ctr_sav = atppc_r_ctr(atppc);
2100 const unsigned char ecr_sav = atppc_r_ecr(atppc);
2102 ctr = ctr_sav;
2103 ecr = ecr_sav;
2104 atppc_barrier_r(atppc);
2106 /* Reset and flush FIFO */
2107 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
2108 atppc_barrier_w(atppc);
2109 /* Disable nAck interrupts and initialize port bits */
2110 ctr &= ~(IRQENABLE | STROBE | AUTOFEED);
2111 atppc_w_ctr(atppc, ctr);
2112 atppc_barrier_w(atppc);
2113 /* Restore mode */
2114 atppc_w_ecr(atppc, ecr);
2115 atppc_barrier_w(atppc);
2117 /* DMA or Programmed IO */
2118 if ((atppc->sc_use & ATPPC_USE_DMA) &&
2119 (atppc->sc_use & ATPPC_USE_INTR)) {
2121 atppc_fifo_write_dma(atppc, ecr, ctr);
2122 } else {
2123 atppc_fifo_write_pio(atppc, ecr, ctr);
2126 /* Restore original register values */
2127 atppc_w_ctr(atppc, ctr_sav);
2128 atppc_w_ecr(atppc, ecr_sav);
2129 atppc_barrier_w(atppc);
2132 static void
2133 atppc_fifo_write_dma(struct atppc_softc * const atppc, unsigned char ecr,
2134 unsigned char ctr)
2136 unsigned int len;
2137 unsigned int worklen;
2139 for (len = (atppc->sc_outb + atppc->sc_outb_nbytes) -
2140 atppc->sc_outbstart; len > 0; len = (atppc->sc_outb +
2141 atppc->sc_outb_nbytes) - atppc->sc_outbstart) {
2143 /* Wait for device to become ready */
2144 atppc->sc_outerr = atppc_poll_str(atppc, SPP_READY, SPP_MASK);
2145 if (atppc->sc_outerr)
2146 return;
2148 /* Reset chipset for next DMA transfer */
2149 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
2150 atppc_barrier_w(atppc);
2151 atppc_w_ecr(atppc, ecr);
2152 atppc_barrier_w(atppc);
2154 /* Limit transfer to maximum DMA size and start it */
2155 worklen = min(len, atppc->sc_dma_maxsize);
2156 atppc->sc_dmastat = ATPPC_DMA_INIT;
2157 atppc->sc_dma_start(atppc, atppc->sc_outbstart,
2158 worklen, ATPPC_DMA_MODE_WRITE);
2159 atppc->sc_dmastat = ATPPC_DMA_STARTED;
2161 /* Enable interrupts, DMA */
2162 ecr &= ~ATPPC_SERVICE_INTR;
2163 ecr |= ATPPC_ENABLE_DMA;
2164 atppc_w_ecr(atppc, ecr);
2165 atppc_barrier_w(atppc);
2167 /* Wait for DMA completion */
2168 atppc->sc_outerr = atppc_wait_interrupt(atppc, atppc->sc_outb,
2169 ATPPC_IRQ_DMA);
2170 if (atppc->sc_outerr) {
2171 atppc_fifo_write_error(atppc, worklen);
2172 return;
2174 /* Get register value recorded by interrupt handler */
2175 ecr = atppc->sc_ecr_intr;
2176 /* Clear DMA programming */
2177 atppc->sc_dma_finish(atppc);
2178 atppc->sc_dmastat = ATPPC_DMA_COMPLETE;
2179 /* Disable DMA */
2180 ecr &= ~ATPPC_ENABLE_DMA;
2181 atppc_w_ecr(atppc, ecr);
2182 atppc_barrier_w(atppc);
2184 /* Wait for FIFO to empty */
2185 for (;;) {
2186 if (ecr & ATPPC_FIFO_EMPTY) {
2187 if (ecr & ATPPC_FIFO_FULL) {
2188 atppc->sc_outerr = EIO;
2189 atppc_fifo_write_error(atppc, worklen);
2190 return;
2191 } else {
2192 break;
2196 /* Enable service interrupt */
2197 ecr &= ~ATPPC_SERVICE_INTR;
2198 atppc_w_ecr(atppc, ecr);
2199 atppc_barrier_w(atppc);
2201 atppc->sc_outerr = atppc_wait_interrupt(atppc,
2202 atppc->sc_outb, ATPPC_IRQ_FIFO);
2203 if (atppc->sc_outerr) {
2204 atppc_fifo_write_error(atppc, worklen);
2205 return;
2208 /* Get register value recorded by interrupt handler */
2209 ecr = atppc->sc_ecr_intr;
2212 /* Update pointer */
2213 atppc->sc_outbstart += worklen;
2217 static void
2218 atppc_fifo_write_pio(struct atppc_softc * const atppc, unsigned char ecr,
2219 unsigned char ctr)
2221 unsigned int len;
2222 unsigned int worklen;
2223 unsigned int timecount;
2225 /* Disable DMA */
2226 ecr &= ~ATPPC_ENABLE_DMA;
2227 atppc_w_ecr(atppc, ecr);
2228 atppc_barrier_w(atppc);
2230 for (len = (atppc->sc_outb + atppc->sc_outb_nbytes) -
2231 atppc->sc_outbstart; len > 0; len = (atppc->sc_outb +
2232 atppc->sc_outb_nbytes) - atppc->sc_outbstart) {
2234 /* Wait for device to become ready */
2235 atppc->sc_outerr = atppc_poll_str(atppc, SPP_READY, SPP_MASK);
2236 if (atppc->sc_outerr)
2237 return;
2239 /* Limit transfer to minimum of space in FIFO and buffer */
2240 worklen = min(len, atppc->sc_fifo);
2242 /* Write to FIFO */
2243 atppc_w_fifo_multi(atppc, atppc->sc_outbstart, worklen);
2245 timecount = 0;
2246 if (atppc->sc_use & ATPPC_USE_INTR) {
2247 ecr = atppc_r_ecr(atppc);
2248 atppc_barrier_w(atppc);
2250 /* Wait for interrupt */
2251 for (;;) {
2252 if (ecr & ATPPC_FIFO_EMPTY) {
2253 if (ecr & ATPPC_FIFO_FULL) {
2254 atppc->sc_outerr = EIO;
2255 atppc_fifo_write_error(atppc,
2256 worklen);
2257 return;
2258 } else {
2259 break;
2263 /* Enable service interrupt */
2264 ecr &= ~ATPPC_SERVICE_INTR;
2265 atppc_w_ecr(atppc, ecr);
2266 atppc_barrier_w(atppc);
2268 atppc->sc_outerr = atppc_wait_interrupt(atppc,
2269 atppc->sc_outb, ATPPC_IRQ_FIFO);
2270 if (atppc->sc_outerr) {
2271 atppc_fifo_write_error(atppc, worklen);
2272 return;
2275 /* Get ECR value saved by interrupt handler */
2276 ecr = atppc->sc_ecr_intr;
2278 } else {
2279 for (; timecount < ((MAXBUSYWAIT/hz)*1000000);
2280 timecount++) {
2282 ecr = atppc_r_ecr(atppc);
2283 atppc_barrier_r(atppc);
2284 if (ecr & ATPPC_FIFO_EMPTY) {
2285 if (ecr & ATPPC_FIFO_FULL) {
2286 atppc->sc_outerr = EIO;
2287 atppc_fifo_write_error(atppc,
2288 worklen);
2289 return;
2290 } else {
2291 break;
2294 DELAY(1);
2297 if (((timecount*hz)/1000000) >= MAXBUSYWAIT) {
2298 atppc->sc_outerr = EIO;
2299 atppc_fifo_write_error(atppc, worklen);
2300 return;
2304 /* Update pointer */
2305 atppc->sc_outbstart += worklen;
2309 static void
2310 atppc_fifo_write_error(struct atppc_softc * const atppc,
2311 const unsigned int worklen)
2313 unsigned char ecr = atppc_r_ecr(atppc);
2315 /* Abort DMA if not finished */
2316 if (atppc->sc_dmastat == ATPPC_DMA_STARTED) {
2317 atppc->sc_dma_abort(atppc);
2318 ATPPC_DPRINTF(("%s: DMA interrupted.\n", __func__));
2321 /* Check for invalid states */
2322 if ((ecr & ATPPC_FIFO_EMPTY) && (ecr & ATPPC_FIFO_FULL)) {
2323 ATPPC_DPRINTF(("%s: FIFO full+empty bits set.\n", __func__));
2324 } else if (!(ecr & ATPPC_FIFO_EMPTY)) {
2325 unsigned char ctr = atppc_r_ctr(atppc);
2326 int bytes_left;
2327 int i;
2329 ATPPC_DPRINTF(("%s(%s): FIFO not empty.\n", __func__,
2330 device_xname(atppc->sc_dev)));
2332 /* Drive strobe low to stop data transfer */
2333 ctr &= ~STROBE;
2334 atppc_w_ctr(atppc, ctr);
2335 atppc_barrier_w(atppc);
2337 /* Determine how many bytes remain in FIFO */
2338 for (i = 0; i < atppc->sc_fifo; i++) {
2339 atppc_w_fifo(atppc, (unsigned char)i);
2340 ecr = atppc_r_ecr(atppc);
2341 atppc_barrier_r(atppc);
2342 if (ecr & ATPPC_FIFO_FULL)
2343 break;
2345 bytes_left = (atppc->sc_fifo) - (i + 1);
2346 ATPPC_DPRINTF(("%s: %d bytes left in FIFO.\n", __func__,
2347 bytes_left));
2349 /* Update counter */
2350 atppc->sc_outbstart += (worklen - bytes_left);
2351 } else {
2352 /* Update counter */
2353 atppc->sc_outbstart += worklen;
2356 ATPPC_DPRINTF(("%s: reseting FIFO.\n", __func__));
2357 atppc_w_ecr(atppc, ATPPC_ECR_PS2);
2358 atppc_barrier_w(atppc);
2362 * Poll status register using mask and status for MAXBUSYWAIT.
2363 * Returns 0 if device ready, error value otherwise.
2365 static int
2366 atppc_poll_str(const struct atppc_softc * const atppc, const u_int8_t status,
2367 const u_int8_t mask)
2369 unsigned int timecount;
2370 u_int8_t str;
2371 int error = EIO;
2373 /* Wait for str to have status for MAXBUSYWAIT */
2374 for (timecount = 0; timecount < ((MAXBUSYWAIT/hz)*1000000);
2375 timecount++) {
2377 str = atppc_r_str(atppc);
2378 atppc_barrier_r(atppc);
2379 if ((str & mask) == status) {
2380 error = 0;
2381 break;
2383 DELAY(1);
2386 return error;
2389 /* Wait for interrupt for MAXBUSYWAIT: returns 0 if acknowledge received. */
2390 static int
2391 atppc_wait_interrupt(struct atppc_softc * const atppc, const void *where,
2392 const u_int8_t irqstat)
2394 int error = EIO;
2396 atppc->sc_irqstat &= ~irqstat;
2398 /* Wait for interrupt for MAXBUSYWAIT */
2399 error = ltsleep(where, PPBUSPRI | PCATCH, __func__, MAXBUSYWAIT,
2400 ATPPC_SC_LOCK(atppc));
2402 if (!(error) && (atppc->sc_irqstat & irqstat)) {
2403 atppc->sc_irqstat &= ~irqstat;
2404 error = 0;
2407 return error;