1 /* $NetBSD: pi1ppc.c,v 1.6 2008/04/22 14:02:04 cegger Exp $ */
4 * Copyright (c) 2001 Alcove - Nicolas Souchu
5 * Copyright (c) 2003, 2004 Gary Thorpe <gathorpe@users.sourceforge.net>
6 * Copyright (c) 2005 Joe Britt <britt@danger.com> - SGI PI1 version
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * FreeBSD: src/sys/isa/ppc.c,v 1.26.2.5 2001/10/02 05:21:45 nsouch Exp
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: pi1ppc.c,v 1.6 2008/04/22 14:02:04 cegger Exp $");
38 #include "opt_pi1ppc.h"
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
46 #include <sys/systm.h>
47 #include <sys/vnode.h>
48 #include <sys/syslog.h>
50 #include <machine/bus.h>
51 /*#include <machine/intr.h>*/
53 #include <dev/ppbus/ppbus_conf.h>
54 #include <dev/ppbus/ppbus_msq.h>
55 #include <dev/ppbus/ppbus_io.h>
56 #include <dev/ppbus/ppbus_var.h>
58 #include <machine/autoconf.h>
59 #include <machine/machtype.h>
61 #include <sgimips/ioc/iocreg.h>
63 #include <sgimips/hpc/hpcvar.h>
64 #include <sgimips/hpc/hpcreg.h>
66 #include <sgimips/hpc/pi1ppcreg.h>
67 #include <sgimips/hpc/pi1ppcvar.h>
74 int pi1ppc_verbose
= 1;
78 /* Prototypes for functions. */
80 /* PC-style register emulation */
81 static u_int8_t
r_reg(int reg
, struct pi1ppc_softc
*pi1ppc
);
82 static void w_reg(int reg
, struct pi1ppc_softc
*pi1ppc
, u_int8_t byte
);
88 #define pi1ppc_r_str(_x) r_reg(AT_STAT_REG,_x)
89 #define pi1ppc_r_ctr(_x) r_reg(AT_CTL_REG,_x)
90 #define pi1ppc_r_dtr(_x) r_reg(AT_DATA_REG,_x)
92 #define pi1ppc_w_str(_x,_y)
93 #define pi1ppc_w_ctr(_x,_y) w_reg(AT_CTL_REG,_x,_y)
94 #define pi1ppc_w_dtr(_x,_y) w_reg(AT_DATA_REG,_x,_y)
96 /* do we need to do these? */
97 #define pi1ppc_barrier_r(_x) bus_space_barrier(_x->sc_iot,_x->sc_ioh, \
98 0,4,BUS_SPACE_BARRIER_READ)
99 #define pi1ppc_barrier_w(_x) bus_space_barrier(_x->sc_iot,_x->sc_ioh, \
100 0,4,BUS_SPACE_BARRIER_WRITE)
101 #define pi1ppc_barrier(_x) pi1ppc_barrier_r(_x)
104 /* Print function for config_found() */
105 static int pi1ppc_print(void *, const char *);
107 /* Routines for ppbus interface (bus + device) */
108 static int pi1ppc_read(device_t
, char *, int, int, size_t *);
109 static int pi1ppc_write(device_t
, char *, int, int, size_t *);
110 static int pi1ppc_setmode(device_t
, int);
111 static int pi1ppc_getmode(device_t
);
112 static int pi1ppc_exec_microseq(device_t
, struct ppbus_microseq
* *);
113 static u_int8_t
pi1ppc_io(device_t
, int, u_char
*, int, u_char
);
114 static int pi1ppc_read_ivar(device_t
, int, unsigned int *);
115 static int pi1ppc_write_ivar(device_t
, int, unsigned int *);
116 static int pi1ppc_add_handler(device_t
, void (*)(void *), void *);
117 static int pi1ppc_remove_handler(device_t
, void (*)(void *));
119 /* no-ops, do any IOC machines have ECP/EPP-capable ports? */
120 static void pi1ppc_reset_epp_timeout(device_t
);
121 static void pi1ppc_ecp_sync(device_t
);
123 /* Utility functions */
125 /* Functions to read bytes into device's input buffer */
126 static void pi1ppc_nibble_read(struct pi1ppc_softc
* const);
127 static void pi1ppc_byte_read(struct pi1ppc_softc
* const);
129 /* Functions to write bytes to device's output buffer */
130 static void pi1ppc_std_write(struct pi1ppc_softc
* const);
133 static void pi1ppc_set_intr_mask(struct pi1ppc_softc
* const, u_int8_t
);
134 static u_int8_t
pi1ppc_get_intr_stat(struct pi1ppc_softc
* const);
136 #ifdef USE_INDY_ACK_HACK
137 static u_int8_t
pi1ppc_get_intr_mask(struct pi1ppc_softc
* const);
140 static int pi1ppc_poll_str(struct pi1ppc_softc
* const, const u_int8_t
,
142 static int pi1ppc_wait_interrupt(struct pi1ppc_softc
* const, const void *,
145 static int pi1ppc_poll_interrupt_stat(struct pi1ppc_softc
* const,
148 static int pi1ppc_match(device_t parent
, cfdata_t match
, void *aux
);
149 static void pi1ppc_attach(device_t parent
, device_t self
, void *aux
);
151 CFATTACH_DECL_NEW(pi1ppc
, sizeof(struct pi1ppc_softc
),
157 /* Currently only matching on Indy, though I think the Indigo1 also
158 uses PI1. If it does, then the driver should work (if it is attached
159 at the appropriate base addr).
163 pi1ppc_match(device_t parent
, cfdata_t match
, void *aux
)
165 if (mach_type
== MACH_SGI_IP22
)
172 pi1ppc_attach(device_t parent
, device_t self
, void *aux
)
174 struct pi1ppc_softc
*sc
;
175 struct hpc_attach_args
*haa
;
177 sc
= device_private(self
);
180 sc
->sc_iot
= haa
->ha_st
;
182 if (bus_space_subregion(haa
->ha_st
, haa
->ha_sh
, haa
->ha_devoff
,
183 0x28, /* # bytes in par port regs */
185 aprint_error(": unable to map control registers\n");
189 pi1ppc_sc_attach(sc
);
193 * Generic attach and detach functions for pi1ppc device.
195 * If sc_dev_ok in soft configuration data is not ATPPC_ATTACHED, these should
196 * be skipped altogether.
199 /* Soft configuration attach for pi1ppc */
201 pi1ppc_sc_attach(struct pi1ppc_softc
*lsc
)
203 /* Adapter used to configure ppbus device */
204 struct parport_adapter sc_parport_adapter
;
207 PI1PPC_LOCK_INIT(lsc
);
209 /* For a PC, this is where the installed chipset is probed.
210 * We *know* what we have, no need to probe.
212 lsc
->sc_type
= PI1PPC_TYPE_INDY
;
213 lsc
->sc_model
= GENERIC
;
215 /* XXX Once we support Interrupts & DMA, update this */
216 lsc
->sc_has
= PI1PPC_HAS_PS2
;
218 /* Print out chipset capabilities */
219 snprintb(buf
, sizeof(buf
), "\20\1INTR\2DMA\3FIFO\4PS2\5ECP\6EPP",
221 printf("\n%s: capabilities=%s\n", device_xname(lsc
->sc_dev
), buf
);
223 /* Initialize device's buffer pointers */
224 lsc
->sc_outb
= lsc
->sc_outbstart
= lsc
->sc_inb
= lsc
->sc_inbstart
226 lsc
->sc_inb_nbytes
= lsc
->sc_outb_nbytes
= 0;
228 /* Last configuration step: set mode to standard mode */
229 if (pi1ppc_setmode(lsc
->sc_dev
, PPBUS_COMPATIBLE
) != 0) {
230 PI1PPC_DPRINTF(("%s: unable to initialize mode.\n",
231 device_xname(lsc
->sc_dev
)));
234 #if defined (MULTIPROCESSOR) || defined (LOCKDEBUG)
235 /* Initialize lock structure */
236 simple_lock_init(&(lsc
->sc_lock
));
239 /* Set up parport_adapter structure */
241 /* Set capabilites */
242 sc_parport_adapter
.capabilities
= 0;
243 if (lsc
->sc_has
& PI1PPC_HAS_INTR
) {
244 sc_parport_adapter
.capabilities
|= PPBUS_HAS_INTR
;
246 if (lsc
->sc_has
& PI1PPC_HAS_DMA
) {
247 sc_parport_adapter
.capabilities
|= PPBUS_HAS_DMA
;
249 if (lsc
->sc_has
& PI1PPC_HAS_FIFO
) {
250 sc_parport_adapter
.capabilities
|= PPBUS_HAS_FIFO
;
252 if (lsc
->sc_has
& PI1PPC_HAS_PS2
) {
253 sc_parport_adapter
.capabilities
|= PPBUS_HAS_PS2
;
256 /* Set function pointers */
257 sc_parport_adapter
.parport_io
= pi1ppc_io
;
258 sc_parport_adapter
.parport_exec_microseq
= pi1ppc_exec_microseq
;
259 sc_parport_adapter
.parport_setmode
= pi1ppc_setmode
;
260 sc_parport_adapter
.parport_getmode
= pi1ppc_getmode
;
261 sc_parport_adapter
.parport_read
= pi1ppc_read
;
262 sc_parport_adapter
.parport_write
= pi1ppc_write
;
263 sc_parport_adapter
.parport_read_ivar
= pi1ppc_read_ivar
;
264 sc_parport_adapter
.parport_write_ivar
= pi1ppc_write_ivar
;
265 sc_parport_adapter
.parport_dma_malloc
= lsc
->sc_dma_malloc
;
266 sc_parport_adapter
.parport_dma_free
= lsc
->sc_dma_free
;
267 sc_parport_adapter
.parport_add_handler
= pi1ppc_add_handler
;
268 sc_parport_adapter
.parport_remove_handler
= pi1ppc_remove_handler
;
270 /* these are no-ops (does later machines have ECP/EPP support?) */
271 sc_parport_adapter
.parport_ecp_sync
= pi1ppc_ecp_sync
;
272 sc_parport_adapter
.parport_reset_epp_timeout
=
273 pi1ppc_reset_epp_timeout
;
275 /* Initialize handler list, may be added to by grandchildren */
276 SLIST_INIT(&(lsc
->sc_handler_listhead
));
278 /* Initialize interrupt state */
279 lsc
->sc_irqstat
= PI1PPC_IRQ_NONE
;
280 lsc
->sc_ecr_intr
= lsc
->sc_ctr_intr
= lsc
->sc_str_intr
= 0;
282 /* Disable DMA/interrupts (each ppbus driver selects usage itself) */
285 /* Configure child of the device. */
286 lsc
->child
= config_found(lsc
->sc_dev
, &(sc_parport_adapter
),
292 /* Soft configuration detach */
294 pi1ppc_sc_detach(struct pi1ppc_softc
*lsc
, int flag
)
296 device_t dev
= lsc
->sc_dev
;
298 /* Detach children devices */
299 if (config_detach(lsc
->child
, flag
) && !(flag
& DETACH_QUIET
)) {
300 printf("%s not able to detach child device, ", device_xname(dev
));
302 if (!(flag
& DETACH_FORCE
)) {
303 printf("cannot detach\n");
306 printf("continuing (DETACH_FORCE)\n");
310 if (!(flag
& DETACH_QUIET
))
311 printf("%s detached", device_xname(dev
));
316 /* Used by config_found() to print out device information */
318 pi1ppc_print(void *aux
, const char *name
)
320 /* Print out something on failure. */
322 printf("%s: child devices", name
);
329 /* Interrupt handler for pi1ppc device: wakes up read/write functions */
331 pi1ppcintr(void *arg
)
333 /* NO INTERRUPTS YET */
336 struct pi1ppc_softc
*pi1ppc
= device_private(dev
);
338 enum { NONE
, READER
, WRITER
} wake_up
= NONE
;
344 /* Record registers' status */
345 pi1ppc
->sc_str_intr
= pi1ppc_r_str(pi1ppc
);
346 pi1ppc
->sc_ctr_intr
= pi1ppc_r_ctr(pi1ppc
);
347 pi1ppc_barrier_r(pi1ppc
);
349 /* Determine cause of interrupt and wake up top half */
350 switch (atppc
->sc_mode
) {
352 /* nAck pulsed for 5 usec, too fast to check reliably, assume */
353 atppc
->sc_irqstat
= ATPPC_IRQ_nACK
;
360 case ATPPC_MODE_NIBBLE
:
362 /* nAck is set low by device and then high on ack */
363 if (!(atppc
->sc_str_intr
& nACK
)) {
367 atppc
->sc_irqstat
= ATPPC_IRQ_nACK
;
373 case ATPPC_MODE_FAST
:
374 /* Confirm interrupt cause: these are not pulsed as in nAck. */
375 if (atppc
->sc_ecr_intr
& ATPPC_SERVICE_INTR
) {
376 if (atppc
->sc_ecr_intr
& ATPPC_ENABLE_DMA
)
377 atppc
->sc_irqstat
|= ATPPC_IRQ_DMA
;
379 atppc
->sc_irqstat
|= ATPPC_IRQ_FIFO
;
381 /* Decide where top half will be waiting */
382 if (atppc
->sc_mode
& ATPPC_MODE_ECP
) {
383 if (atppc
->sc_ctr_intr
& PCD
) {
401 /* Determine if nFault has occurred */
402 if ((atppc
->sc_mode
& ATPPC_MODE_ECP
) &&
403 (atppc
->sc_ecr_intr
& ATPPC_nFAULT_INTR
) &&
404 !(atppc
->sc_str_intr
& nFAULT
)) {
406 /* Device is requesting the channel */
407 atppc
->sc_irqstat
|= ATPPC_IRQ_nFAULT
;
413 /* nAck pulsed for 5 usec, too fast to check reliably */
414 atppc
->sc_irqstat
= ATPPC_IRQ_nACK
;
417 else if (atppc
->sc_outb
)
424 panic("%s: chipset is in invalid mode.", device_xname(dev
));
433 wakeup(atppc
->sc_inb
);
437 wakeup(atppc
->sc_outb
);
442 PI1PPC_UNLOCK(atppc
);
444 /* Call all of the installed handlers */
446 struct atppc_handler_node
* callback
;
447 SLIST_FOREACH(callback
, &(atppc
->sc_handler_listhead
),
449 (*callback
->func
)(callback
->arg
);
457 return 0; /* NO INTERRUPTS YET */
461 /* Functions which support ppbus interface */
464 pi1ppc_reset_epp_timeout(device_t dev
)
469 /* Read from pi1ppc device: returns 0 on success. */
471 pi1ppc_read(device_t dev
, char *buf
, int len
, int ioflag
,
474 struct pi1ppc_softc
*pi1ppc
= device_private(dev
);
483 /* Initialize buffer */
484 pi1ppc
->sc_inb
= pi1ppc
->sc_inbstart
= buf
;
485 pi1ppc
->sc_inb_nbytes
= len
;
487 /* Initialize device input error state for new operation */
488 pi1ppc
->sc_inerr
= 0;
490 /* Call appropriate function to read bytes */
491 switch(pi1ppc
->sc_mode
) {
492 case PI1PPC_MODE_STD
:
496 case PI1PPC_MODE_NIBBLE
:
497 pi1ppc_nibble_read(pi1ppc
);
500 case PI1PPC_MODE_PS2
:
501 pi1ppc_byte_read(pi1ppc
);
505 panic("%s(%s): chipset in invalid mode.\n", __func__
,
510 *cnt
= (pi1ppc
->sc_inbstart
- pi1ppc
->sc_inb
);
513 pi1ppc
->sc_inb
= pi1ppc
->sc_inbstart
= NULL
;
514 pi1ppc
->sc_inb_nbytes
= 0;
517 error
= pi1ppc
->sc_inerr
;
519 PI1PPC_UNLOCK(pi1ppc
);
525 /* Write to pi1ppc device: returns 0 on success. */
527 pi1ppc_write(device_t dev
, char *buf
, int len
, int ioflag
, size_t *cnt
)
529 struct pi1ppc_softc
* const pi1ppc
= device_private(dev
);
538 /* Set up line buffer */
539 pi1ppc
->sc_outb
= pi1ppc
->sc_outbstart
= buf
;
540 pi1ppc
->sc_outb_nbytes
= len
;
542 /* Initialize device output error state for new operation */
543 pi1ppc
->sc_outerr
= 0;
545 /* Call appropriate function to write bytes */
546 switch (pi1ppc
->sc_mode
) {
547 case PI1PPC_MODE_STD
:
548 pi1ppc_std_write(pi1ppc
);
551 case PI1PPC_MODE_NIBBLE
:
552 case PI1PPC_MODE_PS2
:
557 panic("%s(%s): chipset in invalid mode.\n", __func__
,
562 *cnt
= (pi1ppc
->sc_outbstart
- pi1ppc
->sc_outb
);
564 /* Reset output buffer */
565 pi1ppc
->sc_outb
= pi1ppc
->sc_outbstart
= NULL
;
566 pi1ppc
->sc_outb_nbytes
= 0;
569 error
= pi1ppc
->sc_outerr
;
571 PI1PPC_UNLOCK(pi1ppc
);
578 * Set mode of chipset to mode argument. Modes not supported are ignored. If
579 * multiple modes are flagged, the mode is not changed. Modes are those
580 * defined for ppbus_softc.sc_mode in ppbus_conf.h. Only ECP-capable chipsets
581 * can change their mode of operation. However, ALL operation modes support
582 * centronics mode and nibble mode. Modes determine both hardware AND software
584 * NOTE: the mode for ECP should only be changed when the channel is in
585 * forward idle mode. This function does not make sure FIFO's have flushed or
586 * any consistency checks.
589 pi1ppc_setmode(device_t dev
, int mode
)
591 struct pi1ppc_softc
*pi1ppc
= device_private(dev
);
593 u_int8_t chipset_mode
;
602 /* Indy has this, other PI1 machines do too? */
603 chipset_mode
= PI1PPC_MODE_PS2
;
607 /* Set nibble mode (virtual) */
608 chipset_mode
= PI1PPC_MODE_NIBBLE
;
611 case PPBUS_COMPATIBLE
:
612 chipset_mode
= PI1PPC_MODE_STD
;
621 PI1PPC_DPRINTF(("%s(%s): invalid mode passed as "
622 "argument.\n", __func__
, device_xname(dev
)));
627 pi1ppc
->sc_mode
= chipset_mode
;
628 if (chipset_mode
== PI1PPC_MODE_PS2
) {
629 /* Set direction bit to reverse */
630 ecr
= pi1ppc_r_ctr(pi1ppc
);
631 pi1ppc_barrier_r(pi1ppc
);
632 ecr
|= PCD
; /* data is INPUT */
633 pi1ppc_w_ctr(pi1ppc
, ecr
);
634 pi1ppc_barrier_w(pi1ppc
);
638 PI1PPC_UNLOCK(pi1ppc
);
644 /* Get the current mode of chipset */
646 pi1ppc_getmode(device_t dev
)
648 struct pi1ppc_softc
*pi1ppc
= device_private(dev
);
655 /* The chipset can only be in one mode at a time logically */
656 switch (pi1ppc
->sc_mode
) {
657 case PI1PPC_MODE_PS2
:
661 case PI1PPC_MODE_STD
:
662 mode
= PPBUS_COMPATIBLE
;
665 case PI1PPC_MODE_NIBBLE
:
670 panic("%s(%s): device is in invalid mode!", __func__
,
675 PI1PPC_UNLOCK(pi1ppc
);
682 /* Wait for FIFO buffer to empty for ECP-capable chipset */
684 pi1ppc_ecp_sync(device_t dev
)
689 /* Execute a microsequence to handle fast I/O operations. */
691 /* microsequence registers are equivalent to PC-like port registers */
692 /* therefore, translate bit positions & polarities */
694 /* Bit 4 of ctl_reg_int_en is used to emulate the PC's int enable
695 bit. Without it, lpt doesn't like the port.
697 static u_int8_t ctl_reg_int_en
= 0;
700 r_reg(int reg
, struct pi1ppc_softc
*pi1ppc
)
704 /* if we read the status reg, make it look like the PC */
705 if(reg
== AT_STAT_REG
) {
706 val
= bus_space_read_4((pi1ppc
)->sc_iot
,
707 (pi1ppc
)->sc_ioh
, IOC_PLP_STAT
);
713 /* bit 2 reads as '1' on Indy (why?) */
719 /* if we read the ctl reg, make it look like the PC */
720 if(reg
== AT_CTL_REG
) {
721 val
= bus_space_read_4((pi1ppc
)->sc_iot
,
722 (pi1ppc
)->sc_ioh
, IOC_PLP_CTL
);
725 /* get the dir bit in the right place */
726 val
= ((val
>> 1) & 0x20) | (val
& 0x0f);
728 /* invert /SEL, /AUTOFD, and /STB */
731 /* emulate the PC's int enable ctl bit */
732 val
|= (ctl_reg_int_en
& 0x10);
737 if(reg
== AT_DATA_REG
) {
738 val
= bus_space_read_4((pi1ppc
)->sc_iot
,
739 (pi1ppc
)->sc_ioh
, IOC_PLP_DATA
);
749 w_reg(int reg
, struct pi1ppc_softc
*pi1ppc
, u_int8_t byte
)
751 /* don't try to write to the status reg */
753 /* if we are writing the ctl reg, adjust PC style -> IOC style */
754 if(reg
== AT_CTL_REG
) {
755 /* preserve pc-style int enable bit */
756 ctl_reg_int_en
= (byte
& 0x10);
758 /* get the dir bit in the right place */
759 byte
= ((byte
<< 1) & 0x40) | (byte
& 0x0f);
761 /* invert /SEL, /AUTOFD, and /STB */
764 bus_space_write_4((pi1ppc
)->sc_iot
,
765 (pi1ppc
)->sc_ioh
, IOC_PLP_CTL
, byte
);
768 if(reg
== AT_DATA_REG
) {
769 bus_space_write_4((pi1ppc
)->sc_iot
,
770 (pi1ppc
)->sc_ioh
, IOC_PLP_DATA
, byte
);
775 pi1ppc_exec_microseq(device_t dev
, struct ppbus_microseq
**p_msq
)
777 struct pi1ppc_softc
*pi1ppc
= device_private(dev
);
778 struct ppbus_microseq
*mi
= *p_msq
;
784 register unsigned char mask
;
785 register int accum
= 0;
786 register char *ptr
= NULL
;
787 struct ppbus_microseq
*stack
= NULL
;
792 /* Loop until microsequence execution finishes (ending op code) */
794 switch (mi
->opcode
) {
796 cc
= r_reg(mi
->arg
[0].i
, pi1ppc
);
797 pi1ppc_barrier_r(pi1ppc
);
798 cc
&= (char)mi
->arg
[2].i
; /* clear mask */
799 cc
|= (char)mi
->arg
[1].i
; /* assert mask */
800 w_reg(mi
->arg
[0].i
, pi1ppc
, cc
);
801 pi1ppc_barrier_w(pi1ppc
);
805 case MS_OP_RASSERT_P
:
807 ptr
= pi1ppc
->sc_ptr
;
809 if ((len
= mi
->arg
[0].i
) == MS_ACCUM
) {
810 accum
= pi1ppc
->sc_accum
;
811 for (; accum
; accum
--) {
812 w_reg(reg
, pi1ppc
, *ptr
++);
813 pi1ppc_barrier_w(pi1ppc
);
815 pi1ppc
->sc_accum
= accum
;
817 for (i
= 0; i
< len
; i
++) {
818 w_reg(reg
, pi1ppc
, *ptr
++);
819 pi1ppc_barrier_w(pi1ppc
);
823 pi1ppc
->sc_ptr
= ptr
;
829 mask
= (char)mi
->arg
[2].i
;
830 ptr
= pi1ppc
->sc_ptr
;
832 if ((len
= mi
->arg
[0].i
) == MS_ACCUM
) {
833 accum
= pi1ppc
->sc_accum
;
834 for (; accum
; accum
--) {
835 *ptr
++ = r_reg(reg
, pi1ppc
) & mask
;
836 pi1ppc_barrier_r(pi1ppc
);
838 pi1ppc
->sc_accum
= accum
;
840 for (i
= 0; i
< len
; i
++) {
841 *ptr
++ = r_reg(reg
, pi1ppc
) & mask
;
842 pi1ppc_barrier_r(pi1ppc
);
846 pi1ppc
->sc_ptr
= ptr
;
851 *((char *)mi
->arg
[2].p
) = r_reg(mi
->arg
[0].i
, pi1ppc
) &
853 pi1ppc_barrier_r(pi1ppc
);
859 /* let's suppose the next instr. is the same */
861 for (;mi
->opcode
== MS_OP_RASSERT
; mi
++) {
862 w_reg(mi
->arg
[0].i
, pi1ppc
,
864 pi1ppc_barrier_w(pi1ppc
);
867 for (;mi
->opcode
== MS_OP_DELAY
; mi
++) {
870 } while (mi
->opcode
== MS_OP_RASSERT
);
875 tsleep(pi1ppc
, PPBUSPRI
, "pi1ppcdelay",
876 mi
->arg
[0].i
* (hz
/1000));
884 p
= (char *)mi
->arg
[2].p
;
886 /* XXX delay limited to 255 us */
887 for (i
= 0; i
< iter
; i
++) {
888 w_reg(reg
, pi1ppc
, *p
++);
889 pi1ppc_barrier_w(pi1ppc
);
890 delay((unsigned char)*p
++);
897 pi1ppc
->sc_accum
= mi
->arg
[0].i
;
902 if (--pi1ppc
->sc_accum
> 0) {
910 cc
= pi1ppc_r_str(pi1ppc
);
911 pi1ppc_barrier_r(pi1ppc
);
912 if ((cc
& (char)mi
->arg
[0].i
) == (char)mi
->arg
[0].i
) {
919 cc
= pi1ppc_r_str(pi1ppc
);
920 pi1ppc_barrier_r(pi1ppc
);
921 if ((cc
& (char)mi
->arg
[0].i
) == 0) {
928 cc
= pi1ppc_r_str(pi1ppc
);
929 pi1ppc_barrier_r(pi1ppc
);
930 if ((cc
& ((char)mi
->arg
[0].i
| (char)mi
->arg
[1].i
)) ==
931 (char)mi
->arg
[0].i
) {
939 * If the C call returns !0 then end the microseq.
940 * The current state of ptr is passed to the C function
942 if ((error
= mi
->arg
[0].f(mi
->arg
[1].p
,
944 PI1PPC_UNLOCK(pi1ppc
);
952 pi1ppc
->sc_ptr
= (char *)mi
->arg
[0].p
;
958 panic("%s - %s: too many calls", device_xname(dev
),
963 /* store state of the actual microsequence */
966 /* jump to the new microsequence */
967 mi
= (struct ppbus_microseq
*)mi
->arg
[0].p
;
974 /* retrieve microseq and pc state before the call */
977 /* reset the stack */
980 /* XXX return code */
989 * Can't return to pi1ppc level during the execution
990 * of a submicrosequence.
993 panic("%s: cannot return to pi1ppc level",
996 /* update pc for pi1ppc level of execution */
999 PI1PPC_UNLOCK(pi1ppc
);
1005 panic("%s: unknown microsequence "
1006 "opcode 0x%x", __func__
, mi
->opcode
);
1011 /* Should not be reached! */
1013 panic("%s: unexpected code reached!\n", __func__
);
1017 /* General I/O routine */
1019 pi1ppc_io(device_t dev
, int iop
, u_char
*addr
, int cnt
, u_char byte
)
1021 struct pi1ppc_softc
*pi1ppc
= device_private(dev
);
1026 PI1PPC_LOCK(pi1ppc
);
1030 val
= r_reg(AT_DATA_REG
, pi1ppc
);
1033 val
= r_reg(AT_STAT_REG
, pi1ppc
);
1036 val
= r_reg(AT_CTL_REG
, pi1ppc
);
1039 w_reg(AT_DATA_REG
, pi1ppc
, byte
);
1042 /* writing to the status register is weird */
1045 w_reg(AT_CTL_REG
, pi1ppc
, byte
);
1048 panic("%s(%s): unknown I/O operation", device_xname(dev
),
1053 pi1ppc_barrier(pi1ppc
);
1055 PI1PPC_UNLOCK(pi1ppc
);
1061 /* Read "instance variables" of pi1ppc device */
1063 pi1ppc_read_ivar(device_t dev
, int index
, unsigned int *val
)
1065 struct pi1ppc_softc
*pi1ppc
= device_private(dev
);
1070 PI1PPC_LOCK(pi1ppc
);
1073 case PPBUS_IVAR_INTR
:
1074 *val
= ((pi1ppc
->sc_use
& PI1PPC_USE_INTR
) != 0);
1077 case PPBUS_IVAR_DMA
:
1078 *val
= ((pi1ppc
->sc_use
& PI1PPC_USE_DMA
) != 0);
1085 PI1PPC_UNLOCK(pi1ppc
);
1091 /* Write "instance varaibles" of pi1ppc device */
1093 pi1ppc_write_ivar(device_t dev
, int index
, unsigned int *val
)
1095 struct pi1ppc_softc
*pi1ppc
= device_private(dev
);
1100 PI1PPC_LOCK(pi1ppc
);
1103 case PPBUS_IVAR_INTR
:
1105 pi1ppc
->sc_use
&= ~PI1PPC_USE_INTR
;
1106 else if (pi1ppc
->sc_has
& PI1PPC_HAS_INTR
)
1107 pi1ppc
->sc_use
|= PI1PPC_USE_INTR
;
1112 case PPBUS_IVAR_DMA
:
1114 pi1ppc
->sc_use
&= ~PI1PPC_USE_DMA
;
1115 else if (pi1ppc
->sc_has
& PI1PPC_HAS_DMA
)
1116 pi1ppc
->sc_use
|= PI1PPC_USE_DMA
;
1125 PI1PPC_UNLOCK(pi1ppc
);
1131 /* Add a handler routine to be called by the interrupt handler */
1133 pi1ppc_add_handler(device_t dev
, void (*handler
)(void *), void *arg
)
1135 struct pi1ppc_softc
*pi1ppc
= device_private(dev
);
1136 struct pi1ppc_handler_node
*callback
;
1141 PI1PPC_LOCK(pi1ppc
);
1143 if (handler
== NULL
) {
1144 PI1PPC_DPRINTF(("%s(%s): attempt to register NULL handler.\n",
1145 __func__
, device_xname(dev
)));
1148 callback
= malloc(sizeof(struct pi1ppc_handler_node
), M_DEVBUF
,
1151 callback
->func
= handler
;
1152 callback
->arg
= arg
;
1153 SLIST_INSERT_HEAD(&(pi1ppc
->sc_handler_listhead
),
1160 PI1PPC_UNLOCK(pi1ppc
);
1166 /* Remove a handler added by pi1ppc_add_handler() */
1168 pi1ppc_remove_handler(device_t dev
, void (*handler
)(void *))
1170 struct pi1ppc_softc
*pi1ppc
= device_private(dev
);
1171 struct pi1ppc_handler_node
*callback
;
1176 PI1PPC_LOCK(pi1ppc
);
1178 if (SLIST_EMPTY(&(pi1ppc
->sc_handler_listhead
)))
1179 panic("%s(%s): attempt to remove handler from empty list.\n",
1180 __func__
, device_xname(dev
));
1182 /* Search list for handler */
1183 SLIST_FOREACH(callback
, &(pi1ppc
->sc_handler_listhead
), entries
) {
1184 if (callback
->func
== handler
) {
1185 SLIST_REMOVE(&(pi1ppc
->sc_handler_listhead
), callback
,
1186 pi1ppc_handler_node
, entries
);
1187 free(callback
, M_DEVBUF
);
1193 PI1PPC_UNLOCK(pi1ppc
);
1199 /* Utility functions */
1202 * Functions that read bytes from port into buffer: called from interrupt
1203 * handler depending on current chipset mode and cause of interrupt. Return
1204 * value: number of bytes moved.
1207 /* note: BUSY is inverted in the PC world, but not on Indy, but the r_reg()
1208 and w_reg() functions make the Indy look like the PC. */
1210 /* Only the lower 4 bits of the final value are valid */
1211 #define nibble2char(s) ((((s) & ~nACK) >> 3) | (~(s) & nBUSY) >> 4)
1214 /* Read bytes in nibble mode */
1216 pi1ppc_nibble_read(struct pi1ppc_softc
*pi1ppc
)
1223 /* Enable interrupts if needed */
1224 if (pi1ppc
->sc_use
& PI1PPC_USE_INTR
) {
1226 /* XXX JOE - need code to enable interrupts
1227 --> emulate PC behavior in r_reg/w_reg
1230 ctr
= pi1ppc_r_ctr(pi1ppc
);
1231 pi1ppc_barrier_r(ioppc
);
1232 if (!(ctr
& IRQENABLE
)) {
1234 pi1ppc_w_ctr(pi1ppc
, ctr
);
1235 pi1ppc_barrier_w(pi1ppc
);
1240 while (pi1ppc
->sc_inbstart
< (pi1ppc
->sc_inb
+ pi1ppc
->sc_inb_nbytes
)) {
1241 /* Check if device has data to send in idle phase */
1242 str
= pi1ppc_r_str(pi1ppc
);
1243 pi1ppc_barrier_r(pi1ppc
);
1244 if (str
& nDATAVAIL
) {
1248 /* Nibble-mode handshake transfer */
1249 for (i
= 0; i
< 2; i
++) {
1250 /* Event 7 - ready to take data (HOSTBUSY low) */
1251 ctr
= pi1ppc_r_ctr(pi1ppc
);
1252 pi1ppc_barrier_r(pi1ppc
);
1254 pi1ppc_w_ctr(pi1ppc
, ctr
);
1255 pi1ppc_barrier_w(pi1ppc
);
1257 /* Event 8 - peripheral writes the first nibble */
1259 /* Event 9 - peripheral set nAck low */
1260 pi1ppc
->sc_inerr
= pi1ppc_poll_str(pi1ppc
, 0, PTRCLK
);
1261 if (pi1ppc
->sc_inerr
)
1265 nibble
[i
] = pi1ppc_r_str(pi1ppc
);
1267 /* Event 10 - ack, nibble received */
1269 pi1ppc_w_ctr(pi1ppc
, ctr
);
1271 /* Event 11 - wait ack from peripherial */
1272 if (pi1ppc
->sc_use
& PI1PPC_USE_INTR
)
1273 pi1ppc
->sc_inerr
= pi1ppc_wait_interrupt(pi1ppc
,
1274 pi1ppc
->sc_inb
, PI1PPC_IRQ_nACK
);
1276 pi1ppc
->sc_inerr
= pi1ppc_poll_str(pi1ppc
, PTRCLK
,
1278 if (pi1ppc
->sc_inerr
)
1282 /* Store byte transfered */
1283 *(pi1ppc
->sc_inbstart
) = ((nibble2char(nibble
[1]) << 4) & 0xf0) |
1284 (nibble2char(nibble
[0]) & 0x0f);
1285 pi1ppc
->sc_inbstart
++;
1289 /* Read bytes in bidirectional mode */
1291 pi1ppc_byte_read(struct pi1ppc_softc
* const pi1ppc
)
1296 /* Check direction bit */
1297 ctr
= pi1ppc_r_ctr(pi1ppc
);
1298 pi1ppc_barrier_r(pi1ppc
);
1300 PI1PPC_DPRINTF(("%s: byte-mode read attempted without direction "
1301 "bit set.", device_xname(pi1ppc
->sc_dev
)));
1302 pi1ppc
->sc_inerr
= ENODEV
;
1305 /* Enable interrupts if needed */
1307 /* XXX JOE - need code to enable interrupts */
1309 if (pi1ppc
->sc_use
& PI1PPC_USE_INTR
) {
1310 if (!(ctr
& IRQENABLE
)) {
1312 pi1ppc_w_ctr(pi1ppc
, ctr
);
1313 pi1ppc_barrier_w(pi1ppc
);
1318 /* Byte-mode handshake transfer */
1319 while (pi1ppc
->sc_inbstart
< (pi1ppc
->sc_inb
+ pi1ppc
->sc_inb_nbytes
)) {
1320 /* Check if device has data to send */
1321 str
= pi1ppc_r_str(pi1ppc
);
1322 pi1ppc_barrier_r(pi1ppc
);
1323 if (str
& nDATAVAIL
) {
1327 /* Event 7 - ready to take data (nAUTO low) */
1329 pi1ppc_w_ctr(pi1ppc
, ctr
);
1330 pi1ppc_barrier_w(pi1ppc
);
1332 /* Event 9 - peripheral set nAck low */
1333 pi1ppc
->sc_inerr
= pi1ppc_poll_str(pi1ppc
, 0, PTRCLK
);
1334 if (pi1ppc
->sc_inerr
)
1337 /* Store byte transfered */
1338 *(pi1ppc
->sc_inbstart
) = pi1ppc_r_dtr(pi1ppc
);
1339 pi1ppc_barrier_r(pi1ppc
);
1341 /* Event 10 - data received, can't accept more */
1343 pi1ppc_w_ctr(pi1ppc
, ctr
);
1344 pi1ppc_barrier_w(pi1ppc
);
1346 /* Event 11 - peripheral ack */
1347 if (pi1ppc
->sc_use
& PI1PPC_USE_INTR
)
1348 pi1ppc
->sc_inerr
= pi1ppc_wait_interrupt(pi1ppc
,
1349 pi1ppc
->sc_inb
, PI1PPC_IRQ_nACK
);
1351 pi1ppc
->sc_inerr
= pi1ppc_poll_str(pi1ppc
, PTRCLK
, PTRCLK
);
1352 if (pi1ppc
->sc_inerr
)
1355 /* Event 16 - strobe */
1357 pi1ppc_w_str(pi1ppc
, str
);
1358 pi1ppc_barrier_w(pi1ppc
);
1361 pi1ppc_w_str(pi1ppc
, str
);
1362 pi1ppc_barrier_w(pi1ppc
);
1364 /* Update counter */
1365 pi1ppc
->sc_inbstart
++;
1370 * Functions that write bytes to port from buffer: called from pi1ppc_write()
1371 * function depending on current chipset mode. Returns number of bytes moved.
1375 pi1ppc_set_intr_mask(struct pi1ppc_softc
* const pi1ppc
, u_int8_t mask
)
1377 /* invert valid bits (0 = enabled) */
1381 bus_space_write_4((pi1ppc
)->sc_iot
, (pi1ppc
)->sc_ioh
, IOC_PLP_INTMASK
, mask
);
1382 pi1ppc_barrier_w(pi1ppc
);
1386 #ifdef USE_INDY_ACK_HACK
1388 pi1ppc_get_intr_mask(struct pi1ppc_softc
* const pi1ppc
)
1391 val
= bus_space_read_4((pi1ppc
)->sc_iot
, (pi1ppc
)->sc_ioh
, IOC_PLP_INTMASK
);
1392 pi1ppc_barrier_r(pi1ppc
);
1394 /* invert (0 = enabled) */
1397 return (val
& 0xfc);
1402 pi1ppc_get_intr_stat(struct pi1ppc_softc
* const pi1ppc
)
1405 val
= bus_space_read_4((pi1ppc
)->sc_iot
, (pi1ppc
)->sc_ioh
, IOC_PLP_INTSTAT
);
1406 pi1ppc_barrier_r(pi1ppc
);
1408 return (val
& 0xfc);
1411 /* Write bytes in std/bidirectional mode */
1413 pi1ppc_std_write(struct pi1ppc_softc
* const pi1ppc
)
1417 ctr
= pi1ppc_r_ctr(pi1ppc
);
1418 pi1ppc_barrier_r(pi1ppc
);
1420 /* Ensure that the data lines are in OUTPUT mode */
1422 pi1ppc_w_ctr(pi1ppc
, ctr
);
1423 pi1ppc_barrier_w(pi1ppc
);
1425 /* XXX JOE - need code to enable interrupts */
1427 /* Enable interrupts if needed */
1428 if (pi1ppc
->sc_use
& PI1PPC_USE_INTR
) {
1429 if (!(ctr
& IRQENABLE
)) {
1431 pi1ppc_w_ctr(pi1ppc
, ctr
);
1432 pi1ppc_barrier_w(pi1ppc
);
1437 while (pi1ppc
->sc_outbstart
< (pi1ppc
->sc_outb
+ pi1ppc
->sc_outb_nbytes
)) {
1439 /* Wait for peripheral to become ready for MAXBUSYWAIT */
1440 pi1ppc
->sc_outerr
= pi1ppc_poll_str(pi1ppc
, SPP_READY
, SPP_MASK
);
1441 if (pi1ppc
->sc_outerr
) {
1442 printf("pi1ppc: timeout waiting for peripheral to become ready\n");
1446 /* Put data in data register */
1447 pi1ppc_w_dtr(pi1ppc
, *(pi1ppc
->sc_outbstart
));
1448 pi1ppc_barrier_w(pi1ppc
);
1451 /* If no intr, prepare to catch the rising edge of nACK */
1452 if (!(pi1ppc
->sc_use
& PI1PPC_USE_INTR
)) {
1453 pi1ppc_get_intr_stat(pi1ppc
); /* clear any pending intr */
1454 pi1ppc_set_intr_mask(pi1ppc
, PI1_PLP_ACK_INTR
);
1457 /* Pulse strobe to indicate valid data on lines */
1459 pi1ppc_w_ctr(pi1ppc
, ctr
);
1460 pi1ppc_barrier_w(pi1ppc
);
1463 pi1ppc_w_ctr(pi1ppc
, ctr
);
1464 pi1ppc_barrier_w(pi1ppc
);
1466 /* Wait for nACK for MAXBUSYWAIT */
1467 if (pi1ppc
->sc_use
& PI1PPC_USE_INTR
) {
1468 pi1ppc
->sc_outerr
= pi1ppc_wait_interrupt(pi1ppc
,
1469 pi1ppc
->sc_outb
, PI1PPC_IRQ_nACK
);
1470 if (pi1ppc
->sc_outerr
)
1473 /* Try to catch the pulsed acknowledgement */
1474 pi1ppc
->sc_outerr
= pi1ppc_poll_interrupt_stat(pi1ppc
,
1477 if (pi1ppc
->sc_outerr
) {
1478 printf("pi1ppc: timeout waiting for ACK: %02x\n",pi1ppc_r_str(pi1ppc
));
1483 /* Update buffer position, byte count and counter */
1484 pi1ppc
->sc_outbstart
++;
1490 * Poll status register using mask and status for MAXBUSYWAIT.
1491 * Returns 0 if device ready, error value otherwise.
1494 pi1ppc_poll_str(struct pi1ppc_softc
* const pi1ppc
, const u_int8_t status
,
1495 const u_int8_t mask
)
1497 unsigned int timecount
;
1501 /* Wait for str to have status for MAXBUSYWAIT */
1502 for (timecount
= 0; timecount
< ((MAXBUSYWAIT
/hz
)*1000000);
1505 str
= pi1ppc_r_str(pi1ppc
);
1506 pi1ppc_barrier_r(pi1ppc
);
1507 if ((str
& mask
) == status
) {
1517 /* Wait for interrupt for MAXBUSYWAIT: returns 0 if acknowledge received. */
1519 pi1ppc_wait_interrupt(struct pi1ppc_softc
* const pi1ppc
, const void *where
,
1520 const u_int8_t irqstat
)
1524 pi1ppc
->sc_irqstat
&= ~irqstat
;
1526 /* Wait for interrupt for MAXBUSYWAIT */
1527 error
= ltsleep(where
, PPBUSPRI
| PCATCH
, __func__
, MAXBUSYWAIT
,
1528 PI1PPC_SC_LOCK(pi1ppc
));
1530 if (!(error
) && (pi1ppc
->sc_irqstat
& irqstat
)) {
1531 pi1ppc
->sc_irqstat
&= ~irqstat
;
1539 INDY ACK HACK DESCRIPTION
1541 There appears to be a bug in the Indy's PI1 hardware - it sometimes
1542 *misses* the rising edge of /ACK. Ugh!
1544 (Also, unlike the other status bits, /ACK doesn't generate an
1545 interrupt on its falling edge.)
1547 So, we do something kind of skanky here. We use a shorter timeout,
1548 and, if we timeout, we first check BUSY. If BUSY is high, we go
1549 back to waiting for /ACK (because maybe this really is just a slow
1552 If it's a normal printer, it will raise BUSY from when it sees our
1553 /STROBE until it raises its /ACK:
1554 _____ _____________________
1556 ________________ __________
1559 BUSY ______/ \__________
1561 So, if we time out and see BUSY low, then we probably just missed
1564 In that case, we then check /ERROR and SELECTIN. If both are hi,
1565 (the peripheral thinks it is selected, and is not asserting /ERROR)
1566 we assume that the Indy's parallel port missed the /ACK, and return
1570 #ifdef USE_INDY_ACK_HACK
1571 #define ACK_TIMEOUT_SCALER 1000
1573 #define ACK_TIMEOUT_SCALER 1000000
1577 pi1ppc_poll_interrupt_stat(struct pi1ppc_softc
* const pi1ppc
,
1578 const u_int8_t match
)
1580 unsigned int timecount
;
1584 #ifdef USE_INDY_ACK_HACK
1586 int retry_count
= 10000;
1591 /* Wait for intr status to have match bits set for MAXBUSYWAIT */
1592 for (timecount
= 0; timecount
< ((MAXBUSYWAIT
/hz
)*ACK_TIMEOUT_SCALER
);
1594 cur
= pi1ppc_get_intr_stat(pi1ppc
);
1595 if ((cur
& match
) == match
) {
1602 #ifdef USE_INDY_ACK_HACK
1604 cur
= pi1ppc_r_str(pi1ppc
);
1606 /* retry if BUSY is hi (inverted, so lo) and we haven't
1607 waited the usual amt */
1609 if(((cur
&nBUSY
) == 0) && retry_count
) {
1614 /* if /ERROR and SELECT are high, and the peripheral isn't
1615 BUSY, assume that we just missed the /ACK.
1616 (Remember, we emulate the PC's inverted BUSY!)
1619 if((cur
&(nFAULT
|SELECT
|nBUSY
)) == (nFAULT
|SELECT
|nBUSY
))
1622 /* if things still look bad, print out some info */
1624 printf("int mask=%02x, int stat=%02x, str=%02x\n",
1625 pi1ppc_get_intr_mask(pi1ppc
),
1626 pi1ppc_get_intr_stat(pi1ppc
),