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 $
30 * General purpose routines for the IEEE1284-1994 Standard
33 #include "opt_ppb_1284.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <machine/clock.h>
48 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
53 * Wait for the peripherial up to 40ms
56 do_1284_wait(device_t bus, char mask, char status)
58 return (ppb_poll_bus(bus, 4, mask, status, PPB_NOINTR | PPB_POLL));
62 do_peripheral_wait(device_t bus, char mask, char status)
64 return (ppb_poll_bus(bus, 100, mask, status, PPB_NOINTR | PPB_POLL));
67 #define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4)
70 * ppb_1284_reset_error()
72 * Unconditionaly reset the error field
75 ppb_1284_reset_error(device_t bus, int state)
77 struct ppb_data *ppb = DEVTOSOFTC(bus);
79 ppb->error = PPB_NO_ERROR;
86 * ppb_1284_get_state()
91 ppb_1284_get_state(device_t bus)
93 return (DEVTOSOFTC(bus)->state);
97 * ppb_1284_set_state()
99 * Change IEEE1284 state if no error occured
102 ppb_1284_set_state(device_t bus, int state)
104 struct ppb_data *ppb = DEVTOSOFTC(bus);
106 /* call ppb_1284_reset_error() if you absolutly want to change
107 * the state from PPB_ERROR to another */
108 if ((ppb->state != PPB_ERROR) &&
109 (ppb->error == PPB_NO_ERROR)) {
111 ppb->error = PPB_NO_ERROR;
118 ppb_1284_set_error(device_t bus, int error, int event)
120 struct ppb_data *ppb = DEVTOSOFTC(bus);
122 /* do not accumulate errors */
123 if ((ppb->error == PPB_NO_ERROR) &&
124 (ppb->state != PPB_ERROR)) {
126 ppb->state = PPB_ERROR;
130 kprintf("ppb1284: error=%d status=0x%x event=%d\n", error,
131 ppb_rstr(bus) & 0xff, event);
140 * Converts mode+options into ext. value
143 ppb_request_mode(int mode, int options)
145 int request_mode = 0;
147 if (options & PPB_EXTENSIBILITY_LINK) {
148 request_mode = EXT_LINK_1284_NORMAL;
153 request_mode = (options & PPB_REQUEST_ID) ?
154 NIBBLE_1284_REQUEST_ID :
158 request_mode = (options & PPB_REQUEST_ID) ?
159 BYTE_1284_REQUEST_ID :
163 if (options & PPB_USE_RLE)
164 request_mode = (options & PPB_REQUEST_ID) ?
165 ECP_1284_RLE_REQUEST_ID :
168 request_mode = (options & PPB_REQUEST_ID) ?
169 ECP_1284_REQUEST_ID :
173 request_mode = EPP_1284_NORMAL;
176 panic("%s: unsupported mode %d", __func__, mode);
180 return (request_mode);
184 * ppb_peripheral_negociate()
186 * Negociate the peripheral side
189 ppb_peripheral_negociate(device_t bus, int mode, int options)
191 int spin, request_mode, error = 0;
194 ppb_set_mode(bus, PPB_COMPATIBLE);
195 ppb_1284_set_state(bus, PPB_PERIPHERAL_NEGOCIATION);
197 /* compute ext. value */
198 request_mode = ppb_request_mode(mode, options);
202 while (spin-- && (ppb_rstr(bus) & nBUSY))
205 /* check termination */
206 if (!(ppb_rstr(bus) & SELECT) || !spin) {
211 /* Event 4 - read ext. value */
214 /* nibble mode is not supported */
215 if ((r == (char)request_mode) ||
216 (r == NIBBLE_1284_NORMAL)) {
218 /* Event 5 - restore direction bit, no data avail */
219 ppb_wctr(bus, (STROBE | nINIT) & ~(SELECTIN));
223 ppb_wctr(bus, (nINIT) & ~(SELECTIN | STROBE));
225 if (r == NIBBLE_1284_NORMAL) {
229 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
233 ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
235 case BYTE_1284_NORMAL:
236 ppb_set_mode(bus, PPB_BYTE);
244 /* negociation succeeds */
247 /* Event 5 - mode not supported */
248 ppb_wctr(bus, SELECTIN);
252 ppb_wctr(bus, (SELECTIN) & ~(STROBE | nINIT));
253 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
265 ppb_peripheral_terminate(bus, PPB_WAIT);
270 * ppb_peripheral_terminate()
272 * Terminate peripheral transfer side
274 * Always return 0 in compatible mode
277 ppb_peripheral_terminate(device_t bus, int how)
285 ppb_1284_set_state(bus, PPB_PERIPHERAL_TERMINATION);
287 /* Event 22 - wait up to host response time (1s) */
288 if ((error = do_peripheral_wait(bus, SELECT | nBUSY, 0))) {
289 ppb_1284_set_error(bus, PPB_TIMEOUT, 22);
294 ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
296 /* Event 25 - wait up to host response time (1s) */
297 if ((error = do_peripheral_wait(bus, nBUSY, nBUSY))) {
298 ppb_1284_set_error(bus, PPB_TIMEOUT, 25);
303 ppb_wctr(bus, (SELECTIN | nINIT | STROBE) & ~(AUTOFEED));
306 ppb_wctr(bus, (SELECTIN | nINIT) & ~(STROBE | AUTOFEED));
308 /* Event 28 - wait up to host response time (1s) */
309 if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
310 ppb_1284_set_error(bus, PPB_TIMEOUT, 28);
315 ppb_set_mode(bus, PPB_COMPATIBLE);
316 ppb_1284_set_state(bus, PPB_FORWARD_IDLE);
322 * byte_peripheral_outbyte()
324 * Write 1 byte in BYTE mode
327 byte_peripheral_outbyte(device_t bus, char *buffer, int last)
332 if ((error = do_1284_wait(bus, nBUSY, nBUSY))) {
333 ppb_1284_set_error(bus, PPB_TIMEOUT, 7);
337 /* check termination */
338 if (!(ppb_rstr(bus) & SELECT)) {
339 ppb_peripheral_terminate(bus, PPB_WAIT);
343 /* Event 15 - put byte on data lines */
347 ppb_wdtr(bus, *buffer);
350 ppb_wctr(bus, (AUTOFEED | STROBE) & ~(nINIT | SELECTIN));
352 /* Event 10 - wait data read */
353 if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
354 ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
360 ppb_wctr(bus, (AUTOFEED) & ~(nINIT | STROBE | SELECTIN));
362 ppb_wctr(bus, (nINIT) & ~(STROBE | SELECTIN | AUTOFEED));
366 /* Event 16 - wait strobe */
367 if ((error = do_peripheral_wait(bus, nACK | nBUSY, 0))) {
368 ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
373 /* check termination */
374 if (!(ppb_rstr(bus) & SELECT)) {
375 ppb_peripheral_terminate(bus, PPB_WAIT);
384 * byte_peripheral_write()
386 * Write n bytes in BYTE mode
389 byte_peripheral_write(device_t bus, char *buffer, int len, int *sent)
394 ppb_1284_set_state(bus, PPB_PERIPHERAL_TRANSFER);
396 /* wait forever, the remote host is master and should initiate
399 for (i=0; i<len; i++) {
400 /* force remote nFAULT low to release the remote waiting
404 ppb_wctr(bus, r & ~nINIT);
410 error = ppb_poll_bus(bus, PPB_FOREVER, nBUSY, nBUSY,
413 if (error && error != EWOULDBLOCK)
419 if ((error = byte_peripheral_outbyte(bus, buffer+i, (i == len-1))))
424 ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
433 * Read 1 byte in BYTE mode
436 byte_1284_inbyte(device_t bus, char *buffer)
440 /* Event 7 - ready to take data (nAUTO low) */
441 ppb_wctr(bus, (PCD | nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
443 /* Event 9 - peripheral set nAck low */
444 if ((error = do_1284_wait(bus, nACK, 0))) {
445 ppb_1284_set_error(bus, PPB_TIMEOUT, 9);
450 *buffer = ppb_rdtr(bus);
452 /* Event 10 - data received, can't accept more */
453 ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
455 /* Event 11 - peripheral ack */
456 if ((error = do_1284_wait(bus, nACK, nACK))) {
457 ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
461 /* Event 16 - strobe */
462 ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
464 ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
471 * nibble_1284_inbyte()
473 * Read 1 byte in NIBBLE mode
476 nibble_1284_inbyte(device_t bus, char *buffer)
481 for (i = 0; i < 2; i++) {
483 /* Event 7 - ready to take data (nAUTO low) */
484 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
486 /* Event 8 - peripheral writes the first nibble */
488 /* Event 9 - peripheral set nAck low */
489 if ((error = do_1284_wait(bus, nACK, 0))) {
490 ppb_1284_set_error(bus, PPB_TIMEOUT, 9);
495 nibble[i] = ppb_rstr(bus);
497 /* Event 10 - ack, nibble received */
498 ppb_wctr(bus, nINIT & ~(AUTOFEED | STROBE | SELECTIN));
500 /* Event 11 - wait ack from peripherial */
501 if ((error = do_1284_wait(bus, nACK, nACK))) {
502 ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
507 *buffer = ((nibble2char(nibble[1]) << 4) & 0xf0) |
508 (nibble2char(nibble[0]) & 0x0f);
517 * Read in IEEE1284 NIBBLE/BYTE mode
520 spp_1284_read(device_t bus, int mode, char *buffer, int max, int *read)
522 int error = 0, len = 0;
523 int terminate_after_transfer = 1;
528 state = ppb_1284_get_state(bus);
531 case PPB_FORWARD_IDLE:
532 if ((error = ppb_1284_negociate(bus, mode, 0)))
536 case PPB_REVERSE_IDLE:
537 terminate_after_transfer = 0;
541 ppb_1284_terminate(bus);
542 if ((error = ppb_1284_negociate(bus, mode, 0)))
547 while ((len < max) && !(ppb_rstr(bus) & (nFAULT))) {
549 ppb_1284_set_state(bus, PPB_REVERSE_TRANSFER);
557 /* read a byte, error means no more data */
558 if (nibble_1284_inbyte(bus, buffer+len))
562 if (byte_1284_inbyte(bus, buffer+len))
574 ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
578 if (terminate_after_transfer || error)
579 ppb_1284_terminate(bus);
589 ppb_1284_read_id(device_t bus, int mode, char *buffer,
594 /* fill the buffer with 0s */
600 if ((error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID)))
602 error = spp_1284_read(bus, PPB_NIBBLE, buffer, max, read);
605 if ((error = ppb_1284_negociate(bus, PPB_BYTE, PPB_REQUEST_ID)))
607 error = spp_1284_read(bus, PPB_BYTE, buffer, max, read);
610 panic("%s: unsupported mode %d", __func__, mode);
613 ppb_1284_terminate(bus);
623 ppb_1284_read(device_t bus, int mode, char *buffer,
631 error = spp_1284_read(bus, mode, buffer, max, read);
641 * ppb_1284_negociate()
643 * IEEE1284 negociation phase
645 * Normal nibble mode or request device id mode (see ppb_1284.h)
647 * After negociation, nFAULT is low if data is available
650 ppb_1284_negociate(device_t bus, int mode, int options)
659 if (ppb_1284_get_state(bus) >= PPB_PERIPHERAL_NEGOCIATION)
660 ppb_peripheral_terminate(bus, PPB_WAIT);
662 if (ppb_1284_get_state(bus) != PPB_FORWARD_IDLE)
663 ppb_1284_terminate(bus);
669 /* ensure the host is in compatible mode */
670 ppb_set_mode(bus, PPB_COMPATIBLE);
672 /* reset error to catch the actual negociation error */
673 ppb_1284_reset_error(bus, PPB_FORWARD_IDLE);
675 /* calculate ext. value */
676 request_mode = ppb_request_mode(mode, options);
679 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
682 /* enter negociation phase */
683 ppb_1284_set_state(bus, PPB_NEGOCIATION);
685 /* Event 0 - put the exten. value on the data lines */
686 ppb_wdtr(bus, request_mode);
689 /* request remote host attention */
690 ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
692 ppb_wctr(bus, (nINIT) & ~(STROBE | AUTOFEED | SELECTIN));
696 #endif /* !PERIPH_1284 */
698 /* Event 1 - enter IEEE1284 mode */
699 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
702 /* ignore the PError line, wait a bit more, remote host's
703 * interrupts don't respond fast enough */
704 if (ppb_poll_bus(bus, 40, nACK | SELECT | nFAULT,
705 SELECT | nFAULT, PPB_NOINTR | PPB_POLL)) {
706 ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
711 /* Event 2 - trying IEEE1284 dialog */
712 if (do_1284_wait(bus, nACK | PERROR | SELECT | nFAULT,
713 PERROR | SELECT | nFAULT)) {
714 ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
718 #endif /* !PERIPH_1284 */
720 /* Event 3 - latch the ext. value to the peripheral */
721 ppb_wctr(bus, (nINIT | STROBE | AUTOFEED) & ~SELECTIN);
724 /* Event 4 - IEEE1284 device recognized */
725 ppb_wctr(bus, nINIT & ~(SELECTIN | AUTOFEED | STROBE));
727 /* Event 6 - waiting for status lines */
728 if (do_1284_wait(bus, nACK, nACK)) {
729 ppb_1284_set_error(bus, PPB_TIMEOUT, 6);
734 /* Event 7 - quering result consider nACK not to misunderstand
735 * a remote computer terminate sequence */
736 if (options & PPB_EXTENSIBILITY_LINK) {
738 /* XXX not fully supported yet */
739 ppb_1284_terminate(bus);
743 if (request_mode == NIBBLE_1284_NORMAL) {
744 if (do_1284_wait(bus, nACK | SELECT, nACK)) {
745 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
750 if (do_1284_wait(bus, nACK | SELECT, SELECT | nACK)) {
751 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
760 /* enter reverse idle phase */
761 ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
764 /* negociation ok, now setup the communication */
765 ppb_1284_set_state(bus, PPB_SETUP);
766 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(SELECTIN | STROBE));
769 /* ignore PError line */
770 if (do_1284_wait(bus, nACK | SELECT | nBUSY,
771 nACK | SELECT | nBUSY)) {
772 ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
777 if (do_1284_wait(bus, nACK | SELECT | PERROR | nBUSY,
778 nACK | SELECT | PERROR | nBUSY)) {
779 ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
783 #endif /* !PERIPH_1284 */
785 /* ok, the host enters the ForwardIdle state */
786 ppb_1284_set_state(bus, PPB_ECP_FORWARD_IDLE);
789 ppb_1284_set_state(bus, PPB_EPP_IDLE);
793 panic("%s: unknown mode (%d)!", __func__, mode);
795 ppb_set_mode(bus, mode);
800 ppb_1284_terminate(bus);
806 * ppb_1284_terminate()
808 * IEEE1284 termination phase, return code should ignored since the host
809 * is _always_ in compatible mode after ppb_1284_terminate()
812 ppb_1284_terminate(device_t bus)
819 /* do not reset error here to keep the error that
820 * may occured before the ppb_1284_terminate() call */
821 ppb_1284_set_state(bus, PPB_TERMINATION);
824 /* request remote host attention */
825 ppb_wctr(bus, (nINIT | STROBE | SELECTIN) & ~(AUTOFEED));
827 #endif /* PERIPH_1284 */
829 /* Event 22 - set nSelectin low and nAutoFeed high */
830 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
832 /* Event 24 - waiting for peripheral, Xflag ignored */
833 if (do_1284_wait(bus, nACK | nBUSY | nFAULT, nFAULT)) {
834 ppb_1284_set_error(bus, PPB_TIMEOUT, 24);
838 /* Event 25 - set nAutoFd low */
839 ppb_wctr(bus, (nINIT | SELECTIN | AUTOFEED) & ~STROBE);
841 /* Event 26 - compatible mode status is set */
843 /* Event 27 - peripheral set nAck high */
844 if (do_1284_wait(bus, nACK, nACK)) {
845 ppb_1284_set_error(bus, PPB_TIMEOUT, 27);
848 /* Event 28 - end termination, return to idle phase */
849 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
852 /* return to compatible mode */
853 ppb_set_mode(bus, PPB_COMPATIBLE);
854 ppb_1284_set_state(bus, PPB_FORWARD_IDLE);