2 * Copyright (c) 1997 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/dev/ppbus/ppb_1284.c,v 1.11 2000/01/14 08:03:14 nsouch Exp $
27 * $DragonFly: src/sys/bus/ppbus/ppb_1284.c,v 1.5 2006/12/22 23:12:17 swildner Exp $
32 * General purpose routines for the IEEE1284-1994 Standard
35 #include "opt_ppb_1284.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <machine/clock.h>
50 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
55 * Wait for the peripherial up to 40ms
58 do_1284_wait(device_t bus
, char mask
, char status
)
60 return (ppb_poll_bus(bus
, 4, mask
, status
, PPB_NOINTR
| PPB_POLL
));
64 do_peripheral_wait(device_t bus
, char mask
, char status
)
66 return (ppb_poll_bus(bus
, 100, mask
, status
, PPB_NOINTR
| PPB_POLL
));
69 #define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4)
72 * ppb_1284_reset_error()
74 * Unconditionaly reset the error field
77 ppb_1284_reset_error(device_t bus
, int state
)
79 struct ppb_data
*ppb
= DEVTOSOFTC(bus
);
81 ppb
->error
= PPB_NO_ERROR
;
88 * ppb_1284_get_state()
93 ppb_1284_get_state(device_t bus
)
95 return (DEVTOSOFTC(bus
)->state
);
99 * ppb_1284_set_state()
101 * Change IEEE1284 state if no error occured
104 ppb_1284_set_state(device_t bus
, int state
)
106 struct ppb_data
*ppb
= DEVTOSOFTC(bus
);
108 /* call ppb_1284_reset_error() if you absolutly want to change
109 * the state from PPB_ERROR to another */
110 if ((ppb
->state
!= PPB_ERROR
) &&
111 (ppb
->error
== PPB_NO_ERROR
)) {
113 ppb
->error
= PPB_NO_ERROR
;
120 ppb_1284_set_error(device_t bus
, int error
, int event
)
122 struct ppb_data
*ppb
= DEVTOSOFTC(bus
);
124 /* do not accumulate errors */
125 if ((ppb
->error
== PPB_NO_ERROR
) &&
126 (ppb
->state
!= PPB_ERROR
)) {
128 ppb
->state
= PPB_ERROR
;
132 kprintf("ppb1284: error=%d status=0x%x event=%d\n", error
,
133 ppb_rstr(bus
) & 0xff, event
);
142 * Converts mode+options into ext. value
145 ppb_request_mode(int mode
, int options
)
147 int request_mode
= 0;
149 if (options
& PPB_EXTENSIBILITY_LINK
) {
150 request_mode
= EXT_LINK_1284_NORMAL
;
155 request_mode
= (options
& PPB_REQUEST_ID
) ?
156 NIBBLE_1284_REQUEST_ID
:
160 request_mode
= (options
& PPB_REQUEST_ID
) ?
161 BYTE_1284_REQUEST_ID
:
165 if (options
& PPB_USE_RLE
)
166 request_mode
= (options
& PPB_REQUEST_ID
) ?
167 ECP_1284_RLE_REQUEST_ID
:
170 request_mode
= (options
& PPB_REQUEST_ID
) ?
171 ECP_1284_REQUEST_ID
:
175 request_mode
= EPP_1284_NORMAL
;
178 panic("%s: unsupported mode %d\n", __func__
, mode
);
182 return (request_mode
);
186 * ppb_peripheral_negociate()
188 * Negociate the peripheral side
191 ppb_peripheral_negociate(device_t bus
, int mode
, int options
)
193 int spin
, request_mode
, error
= 0;
196 ppb_set_mode(bus
, PPB_COMPATIBLE
);
197 ppb_1284_set_state(bus
, PPB_PERIPHERAL_NEGOCIATION
);
199 /* compute ext. value */
200 request_mode
= ppb_request_mode(mode
, options
);
204 while (spin
-- && (ppb_rstr(bus
) & nBUSY
))
207 /* check termination */
208 if (!(ppb_rstr(bus
) & SELECT
) || !spin
) {
213 /* Event 4 - read ext. value */
216 /* nibble mode is not supported */
217 if ((r
== (char)request_mode
) ||
218 (r
== NIBBLE_1284_NORMAL
)) {
220 /* Event 5 - restore direction bit, no data avail */
221 ppb_wctr(bus
, (STROBE
| nINIT
) & ~(SELECTIN
));
225 ppb_wctr(bus
, (nINIT
) & ~(SELECTIN
| STROBE
));
227 if (r
== NIBBLE_1284_NORMAL
) {
231 ppb_1284_set_error(bus
, PPB_MODE_UNSUPPORTED
, 4);
235 ppb_1284_set_state(bus
, PPB_PERIPHERAL_IDLE
);
237 case BYTE_1284_NORMAL
:
238 ppb_set_mode(bus
, PPB_BYTE
);
246 /* negociation succeeds */
249 /* Event 5 - mode not supported */
250 ppb_wctr(bus
, SELECTIN
);
254 ppb_wctr(bus
, (SELECTIN
) & ~(STROBE
| nINIT
));
255 ppb_1284_set_error(bus
, PPB_MODE_UNSUPPORTED
, 4);
267 ppb_peripheral_terminate(bus
, PPB_WAIT
);
272 * ppb_peripheral_terminate()
274 * Terminate peripheral transfer side
276 * Always return 0 in compatible mode
279 ppb_peripheral_terminate(device_t bus
, int how
)
287 ppb_1284_set_state(bus
, PPB_PERIPHERAL_TERMINATION
);
289 /* Event 22 - wait up to host response time (1s) */
290 if ((error
= do_peripheral_wait(bus
, SELECT
| nBUSY
, 0))) {
291 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 22);
296 ppb_wctr(bus
, (nINIT
| STROBE
) & ~(AUTOFEED
| SELECTIN
));
298 /* Event 25 - wait up to host response time (1s) */
299 if ((error
= do_peripheral_wait(bus
, nBUSY
, nBUSY
))) {
300 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 25);
305 ppb_wctr(bus
, (SELECTIN
| nINIT
| STROBE
) & ~(AUTOFEED
));
308 ppb_wctr(bus
, (SELECTIN
| nINIT
) & ~(STROBE
| AUTOFEED
));
310 /* Event 28 - wait up to host response time (1s) */
311 if ((error
= do_peripheral_wait(bus
, nBUSY
, 0))) {
312 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 28);
317 ppb_set_mode(bus
, PPB_COMPATIBLE
);
318 ppb_1284_set_state(bus
, PPB_FORWARD_IDLE
);
324 * byte_peripheral_outbyte()
326 * Write 1 byte in BYTE mode
329 byte_peripheral_outbyte(device_t bus
, char *buffer
, int last
)
334 if ((error
= do_1284_wait(bus
, nBUSY
, nBUSY
))) {
335 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 7);
339 /* check termination */
340 if (!(ppb_rstr(bus
) & SELECT
)) {
341 ppb_peripheral_terminate(bus
, PPB_WAIT
);
345 /* Event 15 - put byte on data lines */
349 ppb_wdtr(bus
, *buffer
);
352 ppb_wctr(bus
, (AUTOFEED
| STROBE
) & ~(nINIT
| SELECTIN
));
354 /* Event 10 - wait data read */
355 if ((error
= do_peripheral_wait(bus
, nBUSY
, 0))) {
356 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 16);
362 ppb_wctr(bus
, (AUTOFEED
) & ~(nINIT
| STROBE
| SELECTIN
));
364 ppb_wctr(bus
, (nINIT
) & ~(STROBE
| SELECTIN
| AUTOFEED
));
368 /* Event 16 - wait strobe */
369 if ((error
= do_peripheral_wait(bus
, nACK
| nBUSY
, 0))) {
370 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 16);
375 /* check termination */
376 if (!(ppb_rstr(bus
) & SELECT
)) {
377 ppb_peripheral_terminate(bus
, PPB_WAIT
);
386 * byte_peripheral_write()
388 * Write n bytes in BYTE mode
391 byte_peripheral_write(device_t bus
, char *buffer
, int len
, int *sent
)
396 ppb_1284_set_state(bus
, PPB_PERIPHERAL_TRANSFER
);
398 /* wait forever, the remote host is master and should initiate
401 for (i
=0; i
<len
; i
++) {
402 /* force remote nFAULT low to release the remote waiting
406 ppb_wctr(bus
, r
& ~nINIT
);
412 error
= ppb_poll_bus(bus
, PPB_FOREVER
, nBUSY
, nBUSY
,
415 if (error
&& error
!= EWOULDBLOCK
)
421 if ((error
= byte_peripheral_outbyte(bus
, buffer
+i
, (i
== len
-1))))
426 ppb_1284_set_state(bus
, PPB_PERIPHERAL_IDLE
);
435 * Read 1 byte in BYTE mode
438 byte_1284_inbyte(device_t bus
, char *buffer
)
442 /* Event 7 - ready to take data (nAUTO low) */
443 ppb_wctr(bus
, (PCD
| nINIT
| AUTOFEED
) & ~(STROBE
| SELECTIN
));
445 /* Event 9 - peripheral set nAck low */
446 if ((error
= do_1284_wait(bus
, nACK
, 0))) {
447 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 9);
452 *buffer
= ppb_rdtr(bus
);
454 /* Event 10 - data received, can't accept more */
455 ppb_wctr(bus
, (nINIT
) & ~(AUTOFEED
| STROBE
| SELECTIN
));
457 /* Event 11 - peripheral ack */
458 if ((error
= do_1284_wait(bus
, nACK
, nACK
))) {
459 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 11);
463 /* Event 16 - strobe */
464 ppb_wctr(bus
, (nINIT
| STROBE
) & ~(AUTOFEED
| SELECTIN
));
466 ppb_wctr(bus
, (nINIT
) & ~(AUTOFEED
| STROBE
| SELECTIN
));
473 * nibble_1284_inbyte()
475 * Read 1 byte in NIBBLE mode
478 nibble_1284_inbyte(device_t bus
, char *buffer
)
483 for (i
= 0; i
< 2; i
++) {
485 /* Event 7 - ready to take data (nAUTO low) */
486 ppb_wctr(bus
, (nINIT
| AUTOFEED
) & ~(STROBE
| SELECTIN
));
488 /* Event 8 - peripheral writes the first nibble */
490 /* Event 9 - peripheral set nAck low */
491 if ((error
= do_1284_wait(bus
, nACK
, 0))) {
492 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 9);
497 nibble
[i
] = ppb_rstr(bus
);
499 /* Event 10 - ack, nibble received */
500 ppb_wctr(bus
, nINIT
& ~(AUTOFEED
| STROBE
| SELECTIN
));
502 /* Event 11 - wait ack from peripherial */
503 if ((error
= do_1284_wait(bus
, nACK
, nACK
))) {
504 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 11);
509 *buffer
= ((nibble2char(nibble
[1]) << 4) & 0xf0) |
510 (nibble2char(nibble
[0]) & 0x0f);
519 * Read in IEEE1284 NIBBLE/BYTE mode
522 spp_1284_read(device_t bus
, int mode
, char *buffer
, int max
, int *read
)
524 int error
= 0, len
= 0;
525 int terminate_after_transfer
= 1;
530 state
= ppb_1284_get_state(bus
);
533 case PPB_FORWARD_IDLE
:
534 if ((error
= ppb_1284_negociate(bus
, mode
, 0)))
538 case PPB_REVERSE_IDLE
:
539 terminate_after_transfer
= 0;
543 ppb_1284_terminate(bus
);
544 if ((error
= ppb_1284_negociate(bus
, mode
, 0)))
549 while ((len
< max
) && !(ppb_rstr(bus
) & (nFAULT
))) {
551 ppb_1284_set_state(bus
, PPB_REVERSE_TRANSFER
);
559 /* read a byte, error means no more data */
560 if (nibble_1284_inbyte(bus
, buffer
+len
))
564 if (byte_1284_inbyte(bus
, buffer
+len
))
576 ppb_1284_set_state(bus
, PPB_REVERSE_IDLE
);
580 if (terminate_after_transfer
|| error
)
581 ppb_1284_terminate(bus
);
591 ppb_1284_read_id(device_t bus
, int mode
, char *buffer
,
596 /* fill the buffer with 0s */
602 if ((error
= ppb_1284_negociate(bus
, PPB_NIBBLE
, PPB_REQUEST_ID
)))
604 error
= spp_1284_read(bus
, PPB_NIBBLE
, buffer
, max
, read
);
607 if ((error
= ppb_1284_negociate(bus
, PPB_BYTE
, PPB_REQUEST_ID
)))
609 error
= spp_1284_read(bus
, PPB_BYTE
, buffer
, max
, read
);
612 panic("%s: unsupported mode %d\n", __func__
, mode
);
615 ppb_1284_terminate(bus
);
625 ppb_1284_read(device_t bus
, int mode
, char *buffer
,
633 error
= spp_1284_read(bus
, mode
, buffer
, max
, read
);
643 * ppb_1284_negociate()
645 * IEEE1284 negociation phase
647 * Normal nibble mode or request device id mode (see ppb_1284.h)
649 * After negociation, nFAULT is low if data is available
652 ppb_1284_negociate(device_t bus
, int mode
, int options
)
661 if (ppb_1284_get_state(bus
) >= PPB_PERIPHERAL_NEGOCIATION
)
662 ppb_peripheral_terminate(bus
, PPB_WAIT
);
664 if (ppb_1284_get_state(bus
) != PPB_FORWARD_IDLE
)
665 ppb_1284_terminate(bus
);
671 /* ensure the host is in compatible mode */
672 ppb_set_mode(bus
, PPB_COMPATIBLE
);
674 /* reset error to catch the actual negociation error */
675 ppb_1284_reset_error(bus
, PPB_FORWARD_IDLE
);
677 /* calculate ext. value */
678 request_mode
= ppb_request_mode(mode
, options
);
681 ppb_wctr(bus
, (nINIT
| SELECTIN
) & ~(STROBE
| AUTOFEED
));
684 /* enter negociation phase */
685 ppb_1284_set_state(bus
, PPB_NEGOCIATION
);
687 /* Event 0 - put the exten. value on the data lines */
688 ppb_wdtr(bus
, request_mode
);
691 /* request remote host attention */
692 ppb_wctr(bus
, (nINIT
| STROBE
) & ~(AUTOFEED
| SELECTIN
));
694 ppb_wctr(bus
, (nINIT
) & ~(STROBE
| AUTOFEED
| SELECTIN
));
698 #endif /* !PERIPH_1284 */
700 /* Event 1 - enter IEEE1284 mode */
701 ppb_wctr(bus
, (nINIT
| AUTOFEED
) & ~(STROBE
| SELECTIN
));
704 /* ignore the PError line, wait a bit more, remote host's
705 * interrupts don't respond fast enough */
706 if (ppb_poll_bus(bus
, 40, nACK
| SELECT
| nFAULT
,
707 SELECT
| nFAULT
, PPB_NOINTR
| PPB_POLL
)) {
708 ppb_1284_set_error(bus
, PPB_NOT_IEEE1284
, 2);
713 /* Event 2 - trying IEEE1284 dialog */
714 if (do_1284_wait(bus
, nACK
| PERROR
| SELECT
| nFAULT
,
715 PERROR
| SELECT
| nFAULT
)) {
716 ppb_1284_set_error(bus
, PPB_NOT_IEEE1284
, 2);
720 #endif /* !PERIPH_1284 */
722 /* Event 3 - latch the ext. value to the peripheral */
723 ppb_wctr(bus
, (nINIT
| STROBE
| AUTOFEED
) & ~SELECTIN
);
726 /* Event 4 - IEEE1284 device recognized */
727 ppb_wctr(bus
, nINIT
& ~(SELECTIN
| AUTOFEED
| STROBE
));
729 /* Event 6 - waiting for status lines */
730 if (do_1284_wait(bus
, nACK
, nACK
)) {
731 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 6);
736 /* Event 7 - quering result consider nACK not to misunderstand
737 * a remote computer terminate sequence */
738 if (options
& PPB_EXTENSIBILITY_LINK
) {
740 /* XXX not fully supported yet */
741 ppb_1284_terminate(bus
);
745 if (request_mode
== NIBBLE_1284_NORMAL
) {
746 if (do_1284_wait(bus
, nACK
| SELECT
, nACK
)) {
747 ppb_1284_set_error(bus
, PPB_MODE_UNSUPPORTED
, 7);
752 if (do_1284_wait(bus
, nACK
| SELECT
, SELECT
| nACK
)) {
753 ppb_1284_set_error(bus
, PPB_MODE_UNSUPPORTED
, 7);
762 /* enter reverse idle phase */
763 ppb_1284_set_state(bus
, PPB_REVERSE_IDLE
);
766 /* negociation ok, now setup the communication */
767 ppb_1284_set_state(bus
, PPB_SETUP
);
768 ppb_wctr(bus
, (nINIT
| AUTOFEED
) & ~(SELECTIN
| STROBE
));
771 /* ignore PError line */
772 if (do_1284_wait(bus
, nACK
| SELECT
| nBUSY
,
773 nACK
| SELECT
| nBUSY
)) {
774 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 30);
779 if (do_1284_wait(bus
, nACK
| SELECT
| PERROR
| nBUSY
,
780 nACK
| SELECT
| PERROR
| nBUSY
)) {
781 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 30);
785 #endif /* !PERIPH_1284 */
787 /* ok, the host enters the ForwardIdle state */
788 ppb_1284_set_state(bus
, PPB_ECP_FORWARD_IDLE
);
791 ppb_1284_set_state(bus
, PPB_EPP_IDLE
);
795 panic("%s: unknown mode (%d)!", __func__
, mode
);
797 ppb_set_mode(bus
, mode
);
802 ppb_1284_terminate(bus
);
808 * ppb_1284_terminate()
810 * IEEE1284 termination phase, return code should ignored since the host
811 * is _always_ in compatible mode after ppb_1284_terminate()
814 ppb_1284_terminate(device_t bus
)
821 /* do not reset error here to keep the error that
822 * may occured before the ppb_1284_terminate() call */
823 ppb_1284_set_state(bus
, PPB_TERMINATION
);
826 /* request remote host attention */
827 ppb_wctr(bus
, (nINIT
| STROBE
| SELECTIN
) & ~(AUTOFEED
));
829 #endif /* PERIPH_1284 */
831 /* Event 22 - set nSelectin low and nAutoFeed high */
832 ppb_wctr(bus
, (nINIT
| SELECTIN
) & ~(STROBE
| AUTOFEED
));
834 /* Event 24 - waiting for peripheral, Xflag ignored */
835 if (do_1284_wait(bus
, nACK
| nBUSY
| nFAULT
, nFAULT
)) {
836 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 24);
840 /* Event 25 - set nAutoFd low */
841 ppb_wctr(bus
, (nINIT
| SELECTIN
| AUTOFEED
) & ~STROBE
);
843 /* Event 26 - compatible mode status is set */
845 /* Event 27 - peripheral set nAck high */
846 if (do_1284_wait(bus
, nACK
, nACK
)) {
847 ppb_1284_set_error(bus
, PPB_TIMEOUT
, 27);
850 /* Event 28 - end termination, return to idle phase */
851 ppb_wctr(bus
, (nINIT
| SELECTIN
) & ~(STROBE
| AUTOFEED
));
854 /* return to compatible mode */
855 ppb_set_mode(bus
, PPB_COMPATIBLE
);
856 ppb_1284_set_state(bus
, PPB_FORWARD_IDLE
);