1 /* $NetBSD: atppc.c,v 1.27 2008/04/18 14:56:40 cegger Exp $ */
4 * Copyright (c) 2001 Alcove - Nicolas Souchu
5 * Copyright (c) 2003, 2004 Gary Thorpe <gathorpe@users.sourceforge.net>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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>
44 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/syslog.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>
66 int atppc_verbose
= 1;
69 /* List of supported chipsets detection routines */
70 static int (*chipset_detect
[])(struct atppc_softc
*) = {
71 /* XXX Add these LATER: maybe as separate devices?
73 atppc_smc37c66xgt_detect,
75 atppc_smc37c935_detect,
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 *,
115 static void atppc_ecp_read_pio(struct atppc_softc
*, unsigned int *,
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,
126 static void atppc_fifo_write_pio(struct atppc_softc
* const, unsigned char,
128 static void atppc_fifo_write_error(struct atppc_softc
* const,
132 static int atppc_poll_str(const struct atppc_softc
* const, const u_int8_t
,
134 static int atppc_wait_interrupt(struct atppc_softc
* const, const void *,
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 */
145 atppc_sc_attach(struct atppc_softc
*lsc
)
147 /* Adapter used to configure ppbus device */
148 struct parport_adapter sc_parport_adapter
;
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
,
168 /* Print out chipset capabilities */
169 snprintb(buf
, sizeof(buf
), "\20\1INTR\2DMA\3FIFO\4PS2\5ECP\6EPP",
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
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
));
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) */
239 /* Configure child of the device. */
240 lsc
->child
= config_found(lsc
->sc_dev
, &(sc_parport_adapter
),
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");
260 printf("continuing (DETACH_FORCE)\n");
264 if (!(flag
& DETACH_QUIET
))
265 printf("%s detached", device_xname(dev
));
270 /* Used by config_found() to print out device information */
272 atppc_print(void *aux
, const char *name
)
274 /* Print out something on failure. */
276 printf("%s: child devices", name
);
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.
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
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) {
325 * Test if two values can be written and read from the data
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) {
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) {
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
);
363 /* Detect parallel port chipset. */
365 atppc_detect_chipset(struct atppc_softc
*atppc
)
367 /* Try each detection routine. */
369 for (i
= 0; chipset_detect
[i
] != NULL
; i
++) {
370 if ((mode
= chipset_detect
[i
](atppc
)) != -1) {
371 atppc
->sc_mode
= mode
;
379 /* Detect generic capabilities. */
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
);
387 atppc_barrier_r(atppc
);
389 /* Default to generic */
390 atppc
->sc_type
= ATPPC_TYPE_GENERIC
;
391 atppc
->sc_model
= GENERIC
;
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
);
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
);
440 atppc_w_dtr(atppc
, 0x55);
441 atppc_barrier_w(atppc
);
442 tmp
= atppc_r_dtr(atppc
);
443 atppc_barrier_r(atppc
);
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
);
459 * Detect parallel port FIFO: taken from FreeBSD code directly.
462 atppc_detect_fifo(struct atppc_softc
*atppc
)
465 device_t dev
= atppc
->sc_dev
;
473 /* If there is no ECP mode, we cannot config a FIFO */
474 if (!(atppc
->sc_has
& ATPPC_HAS_ECP
)) {
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
) &
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
));
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
);
508 for (i
= 0; i
< 1024; i
++) {
510 atppc_barrier_r(atppc
);
511 cc
= atppc_r_ecr(atppc
);
512 atppc_barrier_r(atppc
);
513 if (cc
& ATPPC_FIFO_EMPTY
)
517 ATPPC_DPRINTF(("%s(%s): cannot flush FIFO.\n", __func__
,
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;
542 ATPPC_DPRINTF(("%s(%s): cannot fill FIFO.\n", __func__
,
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
)));
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__
,
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__
,
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
;
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
);
610 /* Interrupt handler for atppc device: wakes up read/write functions */
615 struct atppc_softc
*atppc
= device_private(dev
);
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
) {
628 /* nAck pulsed for 5 usec, too fast to check reliably, assume */
629 atppc
->sc_irqstat
= ATPPC_IRQ_nACK
;
636 case ATPPC_MODE_NIBBLE
:
638 /* nAck is set low by device and then high on ack */
639 if (!(atppc
->sc_str_intr
& nACK
)) {
643 atppc
->sc_irqstat
= ATPPC_IRQ_nACK
;
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
;
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
) {
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
;
689 /* nAck pulsed for 5 usec, too fast to check reliably */
690 atppc
->sc_irqstat
= ATPPC_IRQ_nACK
;
693 else if (atppc
->sc_outb
)
700 panic("%s: chipset is in invalid mode.", device_xname(dev
));
709 wakeup(atppc
->sc_inb
);
713 wakeup(atppc
->sc_outb
);
718 /* Call all of the installed handlers */
720 struct atppc_handler_node
* callback
;
721 SLIST_FOREACH(callback
, &(atppc
->sc_handler_listhead
),
723 (*callback
->func
)(callback
->arg
);
731 /* Functions which support ppbus interface */
734 /* Check EPP mode timeout */
736 atppc_check_epp_timeout(device_t dev
)
738 struct atppc_softc
*atppc
= device_private(dev
);
745 atppc_reset_epp_timeout(dev
);
746 error
= !(atppc_r_str(atppc
) & TIMEOUT
);
747 atppc_barrier_r(atppc
);
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
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
);
779 /* Read from atppc device: returns 0 on success. */
781 atppc_read(device_t dev
, char *buf
, int len
, int ioflag
,
784 struct atppc_softc
*atppc
= device_private(dev
);
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 */
800 /* Call appropriate function to read bytes */
801 switch(atppc
->sc_mode
) {
803 case ATPPC_MODE_FAST
:
807 case ATPPC_MODE_NIBBLE
:
808 atppc_nibble_read(atppc
);
812 atppc_byte_read(atppc
);
816 atppc_ecp_read(atppc
);
820 atppc_epp_read(atppc
);
824 panic("%s(%s): chipset in invalid mode.\n", __func__
,
829 *cnt
= (atppc
->sc_inbstart
- atppc
->sc_inb
);
832 atppc
->sc_inb
= atppc
->sc_inbstart
= NULL
;
833 atppc
->sc_inb_nbytes
= 0;
836 error
= atppc
->sc_inerr
;
844 /* Write to atppc device: returns 0 on success. */
846 atppc_write(device_t dev
, char *buf
, int len
, int ioflag
, size_t *cnt
)
848 struct atppc_softc
* const atppc
= device_private(dev
);
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
) {
867 atppc_std_write(atppc
);
870 case ATPPC_MODE_NIBBLE
:
875 case ATPPC_MODE_FAST
:
877 atppc_fifo_write(atppc
);
881 atppc_epp_write(atppc
);
885 panic("%s(%s): chipset in invalid mode.\n", __func__
,
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;
897 error
= atppc
->sc_outerr
;
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
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.
917 atppc_setmode(device_t dev
, int mode
)
919 struct atppc_softc
*atppc
= device_private(dev
);
921 u_int8_t chipset_mode
;
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
);
937 ecr
|= ATPPC_ECR_ECP
;
938 chipset_mode
= ATPPC_MODE_ECP
;
943 if (atppc
->sc_has
& ATPPC_HAS_EPP
) {
944 ecr
|= ATPPC_ECR_EPP
;
945 chipset_mode
= ATPPC_MODE_EPP
;
953 /* Set fast centronics mode */
954 ecr
|= ATPPC_ECR_FIFO
;
955 chipset_mode
= ATPPC_MODE_FAST
;
960 ecr
|= ATPPC_ECR_PS2
;
961 chipset_mode
= ATPPC_MODE_PS2
;
964 case PPBUS_COMPATIBLE
:
965 /* Set standard mode */
966 ecr
|= ATPPC_ECR_STD
;
967 chipset_mode
= ATPPC_MODE_STD
;
971 /* Set nibble mode: uses chipset standard mode */
972 ecr
|= ATPPC_ECR_STD
;
973 chipset_mode
= ATPPC_MODE_NIBBLE
;
977 /* Invalid mode specified for ECP chip */
978 ATPPC_DPRINTF(("%s(%s): invalid mode passed as "
979 "argument.\n", __func__
, device_xname(dev
)));
984 /* Switch to byte mode to be able to change modes. */
985 atppc_w_ecr(atppc
, ATPPC_ECR_PS2
);
986 atppc_barrier_w(atppc
);
989 atppc_w_ecr(atppc
, ecr
);
990 atppc_barrier_w(atppc
);
994 if (atppc
->sc_has
& ATPPC_HAS_EPP
) {
995 chipset_mode
= ATPPC_MODE_EPP
;
1003 if (atppc
->sc_has
& ATPPC_HAS_PS2
) {
1004 chipset_mode
= ATPPC_MODE_PS2
;
1012 /* Set nibble mode (virtual) */
1013 chipset_mode
= ATPPC_MODE_NIBBLE
;
1016 case PPBUS_COMPATIBLE
:
1017 chipset_mode
= ATPPC_MODE_STD
;
1025 ATPPC_DPRINTF(("%s(%s): invalid mode passed as "
1026 "argument.\n", __func__
, device_xname(dev
)));
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
);
1038 atppc_w_ctr(atppc
, ecr
);
1039 atppc_barrier_w(atppc
);
1043 ATPPC_UNLOCK(atppc
);
1049 /* Get the current mode of chipset */
1051 atppc_getmode(device_t dev
)
1053 struct atppc_softc
*atppc
= device_private(dev
);
1060 /* The chipset can only be in one mode at a time logically */
1061 switch (atppc
->sc_mode
) {
1062 case ATPPC_MODE_ECP
:
1066 case ATPPC_MODE_EPP
:
1070 case ATPPC_MODE_PS2
:
1074 case ATPPC_MODE_STD
:
1075 mode
= PPBUS_COMPATIBLE
;
1078 case ATPPC_MODE_NIBBLE
:
1079 mode
= PPBUS_NIBBLE
;
1082 case ATPPC_MODE_FAST
:
1087 panic("%s(%s): device is in invalid mode!", __func__
,
1092 ATPPC_UNLOCK(atppc
);
1099 /* Wait for FIFO buffer to empty for ECP-capable chipset */
1101 atppc_ecp_sync(device_t dev
)
1103 struct atppc_softc
*atppc
= device_private(dev
);
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
);
1118 if (!(atppc
->sc_has
& ATPPC_HAS_ECP
) || ((r
!= ATPPC_ECR_ECP
)
1119 && (r
!= ATPPC_ECR_FIFO
))) {
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
) {
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
)));
1137 ATPPC_UNLOCK(atppc
);
1143 /* Execute a microsequence to handle fast I/O operations. */
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
;
1154 register unsigned char mask
;
1155 register int accum
= 0;
1156 register char *ptr
= NULL
;
1157 struct ppbus_microseq
*stack
= NULL
;
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) */
1171 switch (mi
->opcode
) {
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
);
1182 case MS_OP_RASSERT_P
:
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
;
1194 for (i
= 0; i
< len
; i
++) {
1195 w_reg(reg
, atppc
, *ptr
++);
1196 atppc_barrier_w(atppc
);
1200 atppc
->sc_ptr
= ptr
;
1204 case MS_OP_RFETCH_P
:
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
;
1217 for (i
= 0; i
< len
; i
++) {
1218 *ptr
++ = r_reg(reg
, atppc
) & mask
;
1219 atppc_barrier_r(atppc
);
1223 atppc
->sc_ptr
= ptr
;
1228 *((char *)mi
->arg
[2].p
) = r_reg(mi
->arg
[0].i
, atppc
) &
1230 atppc_barrier_r(atppc
);
1236 /* let's suppose the next instr. is the same */
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
);
1252 tsleep(atppc
, PPBUSPRI
, "atppcdelay",
1253 mi
->arg
[0].i
* (hz
/1000));
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
++);
1274 atppc
->sc_accum
= mi
->arg
[0].i
;
1279 if (--atppc
->sc_accum
> 0) {
1287 cc
= atppc_r_str(atppc
);
1288 atppc_barrier_r(atppc
);
1289 if ((cc
& (char)mi
->arg
[0].i
) == (char)mi
->arg
[0].i
) {
1296 cc
= atppc_r_str(atppc
);
1297 atppc_barrier_r(atppc
);
1298 if ((cc
& (char)mi
->arg
[0].i
) == 0) {
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
) {
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
,
1321 ATPPC_UNLOCK(atppc
);
1329 atppc
->sc_ptr
= (char *)mi
->arg
[0].p
;
1335 panic("%s - %s: too much calls", device_xname(dev
),
1340 /* store state of the actual microsequence */
1343 /* jump to the new microsequence */
1344 mi
= (struct ppbus_microseq
*)mi
->arg
[0].p
;
1351 /* retrieve microseq and pc state before the call */
1354 /* reset the stack */
1357 /* XXX return code */
1366 * Can't return to atppc level during the execution
1367 * of a submicrosequence.
1370 panic("%s: cannot return to atppc level",
1373 /* update pc for atppc level of execution */
1376 ATPPC_UNLOCK(atppc
);
1382 panic("%s: unknown microsequence "
1383 "opcode 0x%x", __func__
, mi
->opcode
);
1388 /* Should not be reached! */
1390 panic("%s: unexpected code reached!\n", __func__
);
1394 /* General I/O routine */
1396 atppc_io(device_t dev
, int iop
, u_char
*addr
, int cnt
, u_char byte
)
1398 struct atppc_softc
*atppc
= device_private(dev
);
1406 case PPBUS_OUTSB_EPP
:
1407 bus_space_write_multi_1(atppc
->sc_iot
, atppc
->sc_ioh
,
1408 ATPPC_EPP_DATA
, addr
, cnt
);
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
);
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
);
1418 case PPBUS_INSB_EPP
:
1419 bus_space_read_multi_1(atppc
->sc_iot
, atppc
->sc_ioh
,
1420 ATPPC_EPP_DATA
, addr
, cnt
);
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
);
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
);
1431 val
= (atppc_r_dtr(atppc
));
1434 val
= (atppc_r_str(atppc
));
1437 val
= (atppc_r_ctr(atppc
));
1440 val
= (atppc_r_eppA(atppc
));
1443 val
= (atppc_r_eppD(atppc
));
1446 val
= (atppc_r_ecr(atppc
));
1449 val
= (atppc_r_fifo(atppc
));
1452 atppc_w_dtr(atppc
, byte
);
1455 atppc_w_str(atppc
, byte
);
1458 atppc_w_ctr(atppc
, byte
);
1461 atppc_w_eppA(atppc
, byte
);
1464 atppc_w_eppD(atppc
, byte
);
1467 atppc_w_ecr(atppc
, byte
);
1470 atppc_w_fifo(atppc
, byte
);
1473 panic("%s(%s): unknown I/O operation", device_xname(dev
),
1478 atppc_barrier(atppc
);
1480 ATPPC_UNLOCK(atppc
);
1486 /* Read "instance variables" of atppc device */
1488 atppc_read_ivar(device_t dev
, int index
, unsigned int *val
)
1490 struct atppc_softc
*atppc
= device_private(dev
);
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 ? */
1506 case PPBUS_IVAR_INTR
:
1507 *val
= ((atppc
->sc_use
& ATPPC_USE_INTR
) != 0);
1510 case PPBUS_IVAR_DMA
:
1511 *val
= ((atppc
->sc_use
& ATPPC_USE_DMA
) != 0);
1518 ATPPC_UNLOCK(atppc
);
1524 /* Write "instance varaibles" of atppc device */
1526 atppc_write_ivar(device_t dev
, int index
, unsigned int *val
)
1528 struct atppc_softc
*atppc
= device_private(dev
);
1536 case PPBUS_IVAR_EPP_PROTO
:
1537 if (*val
== PPBUS_EPP_1_9
|| *val
== PPBUS_EPP_1_7
)
1538 atppc
->sc_epp
= *val
;
1543 case PPBUS_IVAR_INTR
:
1545 atppc
->sc_use
&= ~ATPPC_USE_INTR
;
1546 else if (atppc
->sc_has
& ATPPC_HAS_INTR
)
1547 atppc
->sc_use
|= ATPPC_USE_INTR
;
1552 case PPBUS_IVAR_DMA
:
1554 atppc
->sc_use
&= ~ATPPC_USE_DMA
;
1555 else if (atppc
->sc_has
& ATPPC_HAS_DMA
)
1556 atppc
->sc_use
|= ATPPC_USE_DMA
;
1565 ATPPC_UNLOCK(atppc
);
1571 /* Add a handler routine to be called by the interrupt handler */
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
;
1583 if (handler
== NULL
) {
1584 ATPPC_DPRINTF(("%s(%s): attempt to register NULL handler.\n",
1585 __func__
, device_xname(dev
)));
1588 callback
= malloc(sizeof(struct atppc_handler_node
), M_DEVBUF
,
1591 callback
->func
= handler
;
1592 callback
->arg
= arg
;
1593 SLIST_INSERT_HEAD(&(atppc
->sc_handler_listhead
),
1600 ATPPC_UNLOCK(atppc
);
1606 /* Remove a handler added by atppc_add_handler() */
1608 atppc_remove_handler(device_t dev
, void (*handler
)(void *))
1610 struct atppc_softc
*atppc
= device_private(dev
);
1611 struct atppc_handler_node
*callback
;
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
);
1633 ATPPC_UNLOCK(atppc
);
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 */
1653 atppc_nibble_read(struct atppc_softc
*atppc
)
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
)) {
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
) {
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
);
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
)
1696 nibble
[i
] = atppc_r_str(atppc
);
1698 /* Event 10 - ack, nibble received */
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
);
1707 atppc
->sc_inerr
= atppc_poll_str(atppc
, PTRCLK
,
1709 if (atppc
->sc_inerr
)
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 */
1722 atppc_byte_read(struct atppc_softc
* const atppc
)
1727 /* Check direction bit */
1728 ctr
= atppc_r_ctr(atppc
);
1729 atppc_barrier_r(atppc
);
1731 ATPPC_DPRINTF(("%s: byte-mode read attempted without direction "
1732 "bit set.", device_xname(atppc
->sc_dev
)));
1733 atppc
->sc_inerr
= ENODEV
;
1736 /* Enable interrupts if needed */
1737 if (atppc
->sc_use
& ATPPC_USE_INTR
) {
1738 if (!(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
) {
1754 /* Event 7 - ready to take data (nAUTO low) */
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
)
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 */
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
);
1778 atppc
->sc_inerr
= atppc_poll_str(atppc
, PTRCLK
, PTRCLK
);
1779 if (atppc
->sc_inerr
)
1782 /* Event 16 - strobe */
1784 atppc_w_str(atppc
, str
);
1785 atppc_barrier_w(atppc
);
1788 atppc_w_str(atppc
, str
);
1789 atppc_barrier_w(atppc
);
1791 /* Update counter */
1792 atppc
->sc_inbstart
++;
1796 /* Read bytes in EPP mode */
1798 atppc_epp_read(struct atppc_softc
* atppc
)
1800 if (atppc
->sc_epp
== ATPPC_EPP_1_9
) {
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
;
1815 atppc
->sc_inbstart
++;
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
;
1830 /* Read bytes in ECP mode */
1832 atppc_ecp_read(struct atppc_softc
*atppc
)
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 */
1843 atppc_barrier_r(atppc
);
1845 ATPPC_DPRINTF(("%s: ecp-mode read attempted without direction "
1846 "bit set.", device_xname(atppc
->sc_dev
)));
1847 atppc
->sc_inerr
= ENODEV
;
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
);
1865 /* Check if device has data to send */
1866 str
= atppc_r_str(atppc
);
1867 atppc_barrier_r(atppc
);
1868 if (str
& nDATAVAIL
) {
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
)
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
;
1895 /* At least one byte is in the FIFO */
1899 if ((atppc
->sc_use
& ATPPC_USE_INTR
) &&
1900 (atppc
->sc_use
& ATPPC_USE_DMA
)) {
1902 atppc_ecp_read_dma(atppc
, &worklen
, ecr
);
1904 atppc_ecp_read_pio(atppc
, &worklen
, ecr
);
1907 if (atppc
->sc_inerr
) {
1908 atppc_ecp_read_error(atppc
);
1912 /* Update counter */
1913 atppc
->sc_inbstart
+= worklen
;
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 */
1923 atppc_ecp_read_dma(struct atppc_softc
*atppc
, unsigned int *length
,
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
,
1943 if (atppc
->sc_inerr
)
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
;
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 */
1959 atppc_ecp_read_pio(struct atppc_softc
*atppc
, unsigned int *length
,
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 */
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 */
1999 atppc_std_write(struct atppc_softc
* const atppc
)
2001 unsigned int timecount
;
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
)) {
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
)
2021 /* Put data in data register */
2022 atppc_w_dtr(atppc
, *(atppc
->sc_outbstart
));
2023 atppc_barrier_w(atppc
);
2026 /* Pulse strobe to indicate valid data on lines */
2028 atppc_w_ctr(atppc
, ctr
);
2029 atppc_barrier_w(atppc
);
2032 atppc_w_ctr(atppc
, ctr
);
2033 atppc_barrier_w(atppc
);
2035 /* Wait for nACK for MAXBUSYWAIT */
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
)
2043 /* Try to catch the pulsed acknowledgement */
2044 atppc
->sc_outerr
= atppc_poll_str(atppc
, 0, nACK
);
2045 if (atppc
->sc_outerr
)
2047 atppc
->sc_outerr
= atppc_poll_str(atppc
, nACK
, nACK
);
2048 if (atppc
->sc_outerr
)
2052 /* Update buffer position, byte count and counter */
2053 atppc
->sc_outbstart
++;
2058 /* Write bytes in EPP mode */
2060 atppc_epp_write(struct atppc_softc
*atppc
)
2062 if (atppc
->sc_epp
== ATPPC_EPP_1_9
) {
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
;
2077 atppc
->sc_outbstart
++;
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
;
2093 /* Write bytes in ECP/Fast Centronics mode */
2095 atppc_fifo_write(struct atppc_softc
* const atppc
)
2099 const unsigned char ctr_sav
= atppc_r_ctr(atppc
);
2100 const unsigned char ecr_sav
= atppc_r_ecr(atppc
);
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
);
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
);
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
);
2133 atppc_fifo_write_dma(struct atppc_softc
* const atppc
, unsigned char ecr
,
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
)
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
,
2170 if (atppc
->sc_outerr
) {
2171 atppc_fifo_write_error(atppc
, worklen
);
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
;
2180 ecr
&= ~ATPPC_ENABLE_DMA
;
2181 atppc_w_ecr(atppc
, ecr
);
2182 atppc_barrier_w(atppc
);
2184 /* Wait for FIFO to empty */
2186 if (ecr
& ATPPC_FIFO_EMPTY
) {
2187 if (ecr
& ATPPC_FIFO_FULL
) {
2188 atppc
->sc_outerr
= EIO
;
2189 atppc_fifo_write_error(atppc
, worklen
);
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
);
2208 /* Get register value recorded by interrupt handler */
2209 ecr
= atppc
->sc_ecr_intr
;
2212 /* Update pointer */
2213 atppc
->sc_outbstart
+= worklen
;
2218 atppc_fifo_write_pio(struct atppc_softc
* const atppc
, unsigned char ecr
,
2222 unsigned int worklen
;
2223 unsigned int timecount
;
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
)
2239 /* Limit transfer to minimum of space in FIFO and buffer */
2240 worklen
= min(len
, atppc
->sc_fifo
);
2243 atppc_w_fifo_multi(atppc
, atppc
->sc_outbstart
, worklen
);
2246 if (atppc
->sc_use
& ATPPC_USE_INTR
) {
2247 ecr
= atppc_r_ecr(atppc
);
2248 atppc_barrier_w(atppc
);
2250 /* Wait for interrupt */
2252 if (ecr
& ATPPC_FIFO_EMPTY
) {
2253 if (ecr
& ATPPC_FIFO_FULL
) {
2254 atppc
->sc_outerr
= EIO
;
2255 atppc_fifo_write_error(atppc
,
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
);
2275 /* Get ECR value saved by interrupt handler */
2276 ecr
= atppc
->sc_ecr_intr
;
2279 for (; timecount
< ((MAXBUSYWAIT
/hz
)*1000000);
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
,
2297 if (((timecount
*hz
)/1000000) >= MAXBUSYWAIT
) {
2298 atppc
->sc_outerr
= EIO
;
2299 atppc_fifo_write_error(atppc
, worklen
);
2304 /* Update pointer */
2305 atppc
->sc_outbstart
+= worklen
;
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
);
2329 ATPPC_DPRINTF(("%s(%s): FIFO not empty.\n", __func__
,
2330 device_xname(atppc
->sc_dev
)));
2332 /* Drive strobe low to stop data transfer */
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
)
2345 bytes_left
= (atppc
->sc_fifo
) - (i
+ 1);
2346 ATPPC_DPRINTF(("%s: %d bytes left in FIFO.\n", __func__
,
2349 /* Update counter */
2350 atppc
->sc_outbstart
+= (worklen
- bytes_left
);
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.
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
;
2373 /* Wait for str to have status for MAXBUSYWAIT */
2374 for (timecount
= 0; timecount
< ((MAXBUSYWAIT
/hz
)*1000000);
2377 str
= atppc_r_str(atppc
);
2378 atppc_barrier_r(atppc
);
2379 if ((str
& mask
) == status
) {
2389 /* Wait for interrupt for MAXBUSYWAIT: returns 0 if acknowledge received. */
2391 atppc_wait_interrupt(struct atppc_softc
* const atppc
, const void *where
,
2392 const u_int8_t irqstat
)
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
;