2 * Copyright (c) 1998, 1999 Nicolas Souchu
3 * Copyright (c) 2001 Alcove - Nicolas Souchu
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Iomega ZIP+ Matchmaker Parallel Port Interface driver
36 * Thanks to David Campbell work on the Linux driver and the Iomega specs
37 * Thanks to Thiebault Moeglin for the drive
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/module.h>
44 #include <sys/malloc.h>
50 #include <dev/ppbus/ppbio.h>
51 #include <dev/ppbus/ppbconf.h>
52 #include <dev/ppbus/ppb_msq.h>
53 #include <dev/ppbus/vpoio.h>
54 #include <dev/ppbus/ppb_1284.h>
58 #define VP0_SELTMO 5000 /* select timeout */
59 #define VP0_FAST_SPINTMO 500000 /* wait status timeout */
60 #define VP0_LOW_SPINTMO 5000000 /* wait status timeout */
62 #define VP0_SECTOR_SIZE 512
65 * Microcode to execute very fast I/O sequences at the lowest bus level.
68 #define WAIT_RET MS_PARAM(7, 2, MS_TYP_PTR)
69 #define WAIT_TMO MS_PARAM(1, 0, MS_TYP_INT)
71 #define DECLARE_WAIT_MICROSEQUENCE \
72 struct ppb_microseq wait_microseq[] = { \
76 MS_BRSET(nBUSY, 4 /* ready */), \
77 MS_DBRA(-2 /* loop */), \
79 MS_RET(1), /* timed out */ \
82 MS_RFETCH(MS_REG_STR, 0xb8, MS_UNKNOWN ), \
83 MS_RET(0) /* no error */ \
86 #define SELECT_TARGET MS_PARAM(6, 1, MS_TYP_CHA)
88 #define DECLARE_SELECT_MICROSEQUENCE \
89 struct ppb_microseq select_microseq[] = { \
91 /* first, check there is nothing holding onto the bus */ \
94 MS_BRCLEAR(0x8, 2 /* _ready */), \
95 MS_DBRA(-2 /* _loop */), \
96 MS_RET(2), /* bus busy */ \
99 MS_DASS(MS_UNKNOWN /* 0x80 | 1 << target */), \
103 /* now, wait until the drive is ready */ \
104 MS_SET(VP0_SELTMO), \
106 MS_BRSET(0x8, 3 /* ready */), \
107 MS_DBRA(-2 /* loop */), \
110 MS_RET(VP0_ESELECT_TIMEOUT), \
116 static struct ppb_microseq transfer_epilog[] = {
124 #define CPP_S1 MS_PARAM(10, 2, MS_TYP_PTR)
125 #define CPP_S2 MS_PARAM(13, 2, MS_TYP_PTR)
126 #define CPP_S3 MS_PARAM(16, 2, MS_TYP_PTR)
127 #define CPP_PARAM MS_PARAM(17, 1, MS_TYP_CHA)
129 #define DECLARE_CPP_MICROSEQ \
130 struct ppb_microseq cpp_microseq[] = { \
131 MS_CASS(0x0c), MS_DELAY(2), \
132 MS_DASS(0xaa), MS_DELAY(10), \
133 MS_DASS(0x55), MS_DELAY(10), \
134 MS_DASS(0x00), MS_DELAY(10), \
135 MS_DASS(0xff), MS_DELAY(10), \
136 MS_RFETCH(MS_REG_STR, 0xb8, MS_UNKNOWN /* &s1 */), \
137 MS_DASS(0x87), MS_DELAY(10), \
138 MS_RFETCH(MS_REG_STR, 0xb8, MS_UNKNOWN /* &s2 */), \
139 MS_DASS(0x78), MS_DELAY(10), \
140 MS_RFETCH(MS_REG_STR, 0x38, MS_UNKNOWN /* &s3 */), \
141 MS_DASS(MS_UNKNOWN /* param */), \
143 MS_CASS(0x0c), MS_DELAY(10), \
144 MS_CASS(0x0d), MS_DELAY(2), \
145 MS_CASS(0x0c), MS_DELAY(10), \
146 MS_DASS(0xff), MS_DELAY(10), \
150 #define NEGOCIATED_MODE MS_PARAM(2, 1, MS_TYP_CHA)
152 #define DECLARE_NEGOCIATE_MICROSEQ \
153 struct ppb_microseq negociate_microseq[] = { \
156 MS_DASS(MS_UNKNOWN /* mode */), \
160 MS_BRSET(0x20, 5 /* continue */), \
165 MS_RET(VP0_ENEGOCIATE), \
174 #define INB_NIBBLE_L MS_PARAM(3, 2, MS_TYP_PTR)
175 #define INB_NIBBLE_H MS_PARAM(6, 2, MS_TYP_PTR)
176 #define INB_NIBBLE_F MS_PARAM(9, 0, MS_TYP_FUN)
177 #define INB_NIBBLE_P MS_PARAM(9, 1, MS_TYP_PTR)
180 * This is the sub-microseqence for MS_GET in NIBBLE mode
181 * Retrieve the two nibbles and call the C function to generate the character
182 * and store it in the buffer (see nibble_inbyte_hook())
185 #define DECLARE_NIBBLE_INBYTE_SUBMICROSEQ \
186 struct ppb_microseq nibble_inbyte_submicroseq[] = { \
191 MS_RFETCH(MS_REG_STR, MS_FETCH_ALL, MS_UNKNOWN /* low nibble */),\
194 MS_RFETCH(MS_REG_STR, MS_FETCH_ALL, MS_UNKNOWN /* high nibble */),\
197 /* do a C call to format the received nibbles */ \
198 MS_C_CALL(MS_UNKNOWN /* C hook */, MS_UNKNOWN /* param */), \
199 MS_DBRA(-7 /* loop */), \
203 static struct ppb_microseq reset_microseq[] = {
216 * nibble_inbyte_hook()
218 * Formats high and low nibble into a character
221 nibble_inbyte_hook (void *p, char *ptr)
223 struct vpo_nibble *s = (struct vpo_nibble *)p;
225 /* increment the buffer pointer */
226 *ptr = ((s->l >> 4) & 0x0f) + (s->h & 0xf0);
232 * This is the sub-microseqence for MS_GET in PS2 mode
234 static struct ppb_microseq ps2_inbyte_submicroseq[] = {
239 MS_RFETCH_P(1, MS_REG_DTR, MS_FETCH_ALL),
241 MS_DBRA(-4 /* loop */),
247 * This is the sub-microsequence for MS_PUT in both NIBBLE and PS2 modes
249 static struct ppb_microseq spp_outbyte_submicroseq[] = {
253 MS_RASSERT_P(1, MS_REG_DTR),
255 MS_DBRA(0), /* decrement counter */
256 MS_RASSERT_P(1, MS_REG_DTR),
258 MS_DBRA(-6 /* loop */),
260 /* return from the put call */
265 /* EPP 1.7 microsequences, ptr and len set at runtime */
266 static struct ppb_microseq epp17_outstr[] = {
268 MS_RASSERT_P(MS_ACCUM, MS_REG_EPP_D),
273 static struct ppb_microseq epp17_instr[] = {
275 MS_RFETCH_P(MS_ACCUM, MS_REG_EPP_D, MS_FETCH_ALL),
281 imm_disconnect(struct vpoio_data *vpo, int *connected, int release_bus)
283 DECLARE_CPP_MICROSEQ;
285 device_t ppbus = device_get_parent(vpo->vpo_dev);
289 /* all should be ok */
293 ppb_MS_init_msq(cpp_microseq, 4, CPP_S1, (void *)&s1,
294 CPP_S2, (void *)&s2, CPP_S3, (void *)&s3,
297 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
299 if ((s1 != (char)0xb8 || s2 != (char)0x18 || s3 != (char)0x38)) {
301 device_printf(vpo->vpo_dev,
302 "(disconnect) s1=0x%x s2=0x%x, s3=0x%x\n",
303 s1 & 0xff, s2 & 0xff, s3 & 0xff);
305 *connected = VP0_ECONNECT;
309 return (ppb_release_bus(ppbus, vpo->vpo_dev));
315 * how : PPB_WAIT or PPB_DONTWAIT
318 imm_connect(struct vpoio_data *vpo, int how, int *disconnected, int request_bus)
320 DECLARE_CPP_MICROSEQ;
322 device_t ppbus = device_get_parent(vpo->vpo_dev);
327 /* all should be ok */
332 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, how)))
335 ppb_MS_init_msq(cpp_microseq, 3, CPP_S1, (void *)&s1,
336 CPP_S2, (void *)&s2, CPP_S3, (void *)&s3);
338 /* select device 0 in compatible mode */
339 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0xe0);
340 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
342 /* disconnect all devices */
343 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0x30);
344 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
346 if (PPB_IN_EPP_MODE(ppbus))
347 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0x28);
349 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0xe0);
351 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
353 if ((s1 != (char)0xb8 || s2 != (char)0x18 || s3 != (char)0x30)) {
355 device_printf(vpo->vpo_dev,
356 "(connect) s1=0x%x s2=0x%x, s3=0x%x\n",
357 s1 & 0xff, s2 & 0xff, s3 & 0xff);
359 *disconnected = VP0_ECONNECT;
368 * Detect and initialise the VP0 adapter.
371 imm_detect(struct vpoio_data *vpo)
373 device_t ppbus = device_get_parent(vpo->vpo_dev);
376 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, PPB_DONTWAIT)))
379 /* disconnect the drive, keep the bus */
380 imm_disconnect(vpo, NULL, 0);
382 vpo->vpo_mode_found = VP0_MODE_UNDEFINED;
385 /* try to enter EPP mode since vpoio failure put the bus in NIBBLE */
386 if (ppb_set_mode(ppbus, PPB_EPP) != -1) {
387 imm_connect(vpo, PPB_DONTWAIT, &error, 0);
390 /* if connection failed try PS/2 then NIBBLE modes */
392 if (ppb_set_mode(ppbus, PPB_PS2) != -1) {
393 imm_connect(vpo, PPB_DONTWAIT, &error, 0);
396 if (ppb_set_mode(ppbus, PPB_NIBBLE) != -1) {
397 imm_connect(vpo, PPB_DONTWAIT, &error, 0);
400 vpo->vpo_mode_found = VP0_MODE_NIBBLE;
402 device_printf(vpo->vpo_dev,
403 "NIBBLE mode unavailable!\n");
407 vpo->vpo_mode_found = VP0_MODE_PS2;
410 vpo->vpo_mode_found = VP0_MODE_EPP;
413 /* send SCSI reset signal */
414 ppb_MS_microseq(ppbus, vpo->vpo_dev, reset_microseq, NULL);
416 /* release the bus now */
417 imm_disconnect(vpo, &error, 1);
419 /* ensure we are disconnected or daisy chained peripheral
420 * may cause serious problem to the disk */
424 device_printf(vpo->vpo_dev,
425 "can't disconnect from the drive\n");
432 ppb_release_bus(ppbus, vpo->vpo_dev);
433 return (VP0_EINITFAILED);
440 imm_outstr(struct vpoio_data *vpo, char *buffer, int size)
442 device_t ppbus = device_get_parent(vpo->vpo_dev);
445 if (PPB_IN_EPP_MODE(ppbus))
446 ppb_reset_epp_timeout(ppbus);
448 ppb_MS_exec(ppbus, vpo->vpo_dev, MS_OP_PUT, (union ppb_insarg)buffer,
449 (union ppb_insarg)size, (union ppb_insarg)MS_UNKNOWN, &error);
458 imm_instr(struct vpoio_data *vpo, char *buffer, int size)
460 device_t ppbus = device_get_parent(vpo->vpo_dev);
463 if (PPB_IN_EPP_MODE(ppbus))
464 ppb_reset_epp_timeout(ppbus);
466 ppb_MS_exec(ppbus, vpo->vpo_dev, MS_OP_GET, (union ppb_insarg)buffer,
467 (union ppb_insarg)size, (union ppb_insarg)MS_UNKNOWN, &error);
473 imm_select(struct vpoio_data *vpo, int initiator, int target)
475 DECLARE_SELECT_MICROSEQUENCE;
476 device_t ppbus = device_get_parent(vpo->vpo_dev);
479 /* initialize the select microsequence */
480 ppb_MS_init_msq(select_microseq, 1,
481 SELECT_TARGET, 1 << initiator | 1 << target);
483 ppb_MS_microseq(ppbus, vpo->vpo_dev, select_microseq, &ret);
491 * H_SELIN must be low.
495 imm_wait(struct vpoio_data *vpo, int tmo)
497 DECLARE_WAIT_MICROSEQUENCE;
499 device_t ppbus = device_get_parent(vpo->vpo_dev);
503 * Return some status information.
504 * Semantics : 0x88 = ZIP+ wants more data
505 * 0x98 = ZIP+ wants to send more data
506 * 0xa8 = ZIP+ wants command
507 * 0xb8 = end of transfer, ZIP+ is sending status
510 ppb_MS_init_msq(wait_microseq, 2,
511 WAIT_RET, (void *)&ret,
514 ppb_MS_microseq(ppbus, vpo->vpo_dev, wait_microseq, &err);
517 return (0); /* command timed out */
523 imm_negociate(struct vpoio_data *vpo)
525 DECLARE_NEGOCIATE_MICROSEQ;
526 device_t ppbus = device_get_parent(vpo->vpo_dev);
530 if (PPB_IN_NIBBLE_MODE(ppbus))
532 else if (PPB_IN_PS2_MODE(ppbus))
537 #if 0 /* XXX use standalone code not to depend on ppb_1284 code yet */
538 ret = ppb_1284_negociate(ppbus, negociate_mode);
541 return (VP0_ENEGOCIATE);
544 ppb_MS_init_msq(negociate_microseq, 1,
545 NEGOCIATED_MODE, negociate_mode);
547 ppb_MS_microseq(ppbus, vpo->vpo_dev, negociate_microseq, &ret);
555 * Low level probe of vpo device
559 imm_probe(device_t dev, struct vpoio_data *vpo)
563 /* ppbus dependent initialisation */
566 /* now, try to initialise the drive */
567 if ((error = imm_detect(vpo))) {
577 * Low level attachment of vpo device
581 imm_attach(struct vpoio_data *vpo)
583 DECLARE_NIBBLE_INBYTE_SUBMICROSEQ;
584 device_t ppbus = device_get_parent(vpo->vpo_dev);
588 * Initialize microsequence code
590 vpo->vpo_nibble_inbyte_msq = (struct ppb_microseq *)malloc(
591 sizeof(nibble_inbyte_submicroseq), M_DEVBUF, M_NOWAIT);
593 if (!vpo->vpo_nibble_inbyte_msq)
596 bcopy((void *)nibble_inbyte_submicroseq,
597 (void *)vpo->vpo_nibble_inbyte_msq,
598 sizeof(nibble_inbyte_submicroseq));
600 ppb_MS_init_msq(vpo->vpo_nibble_inbyte_msq, 4,
601 INB_NIBBLE_H, (void *)&(vpo)->vpo_nibble.h,
602 INB_NIBBLE_L, (void *)&(vpo)->vpo_nibble.l,
603 INB_NIBBLE_F, nibble_inbyte_hook,
604 INB_NIBBLE_P, (void *)&(vpo)->vpo_nibble);
607 * Initialize mode dependent in/out microsequences
610 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, PPB_WAIT)))
613 /* ppbus automatically restore the last mode entered during detection */
614 switch (vpo->vpo_mode_found) {
616 ppb_MS_GET_init(ppbus, vpo->vpo_dev, epp17_instr);
617 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, epp17_outstr);
618 device_printf(vpo->vpo_dev, "EPP mode\n");
621 ppb_MS_GET_init(ppbus, vpo->vpo_dev, ps2_inbyte_submicroseq);
622 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, spp_outbyte_submicroseq);
623 device_printf(vpo->vpo_dev, "PS2 mode\n");
625 case VP0_MODE_NIBBLE:
626 ppb_MS_GET_init(ppbus, vpo->vpo_dev, vpo->vpo_nibble_inbyte_msq);
627 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, spp_outbyte_submicroseq);
628 device_printf(vpo->vpo_dev, "NIBBLE mode\n");
631 panic("imm: unknown mode %d", vpo->vpo_mode_found);
634 ppb_release_bus(ppbus, vpo->vpo_dev);
645 imm_reset_bus(struct vpoio_data *vpo)
647 device_t ppbus = device_get_parent(vpo->vpo_dev);
650 /* first, connect to the drive and request the bus */
651 imm_connect(vpo, PPB_WAIT|PPB_INTR, &disconnected, 1);
655 /* reset the SCSI bus */
656 ppb_MS_microseq(ppbus, vpo->vpo_dev, reset_microseq, NULL);
658 /* then disconnect */
659 imm_disconnect(vpo, NULL, 1);
668 * Send an SCSI command
672 imm_do_scsi(struct vpoio_data *vpo, int host, int target, char *command,
673 int clen, char *buffer, int blen, int *result, int *count,
676 device_t ppbus = device_get_parent(vpo->vpo_dev);
679 int len, error = 0, not_connected = 0;
684 * enter disk state, allocate the ppbus
687 * Should we allow this call to be interruptible?
688 * The only way to report the interruption is to return
689 * EIO to upper SCSI code :^(
691 if ((error = imm_connect(vpo, PPB_WAIT|PPB_INTR, ¬_connected, 1)))
700 * Select the drive ...
702 if ((*ret = imm_select(vpo,host,target)))
706 * Send the command ...
708 for (k = 0; k < clen; k+=2) {
709 if (imm_wait(vpo, VP0_FAST_SPINTMO) != (char)0xa8) {
710 *ret = VP0_ECMD_TIMEOUT;
713 if (imm_outstr(vpo, &command[k], 2)) {
714 *ret = VP0_EPPDATA_TIMEOUT;
719 if (!(r = imm_wait(vpo, VP0_LOW_SPINTMO))) {
720 *ret = VP0_ESTATUS_TIMEOUT;
724 if ((r & 0x30) == 0x10) {
725 if (imm_negociate(vpo)) {
726 *ret = VP0_ENEGOCIATE;
733 * Complete transfer ...
738 if (!(r = imm_wait(vpo, VP0_LOW_SPINTMO))) {
739 *ret = VP0_ESTATUS_TIMEOUT;
743 /* stop when the ZIP+ wants to send status */
747 if (*count >= blen) {
748 *ret = VP0_EDATA_OVERFLOW;
752 /* ZIP+ wants to send data? */
753 if (r == (char)0x88) {
754 len = (((blen - *count) >= VP0_SECTOR_SIZE)) ?
757 error = imm_outstr(vpo, &buffer[*count], len);
759 if (!PPB_IN_EPP_MODE(ppbus))
762 len = (((blen - *count) >= VP0_SECTOR_SIZE)) ?
765 error = imm_instr(vpo, &buffer[*count], len);
776 if ((PPB_IN_NIBBLE_MODE(ppbus) ||
777 PPB_IN_PS2_MODE(ppbus)) && negociated)
778 ppb_MS_microseq(ppbus, vpo->vpo_dev, transfer_epilog, NULL);
781 * Retrieve status ...
783 if (imm_negociate(vpo)) {
784 *ret = VP0_ENEGOCIATE;
789 if (imm_instr(vpo, &l, 1)) {
794 /* check if the ZIP+ wants to send more status */
795 if (imm_wait(vpo, VP0_FAST_SPINTMO) == (char)0xb8)
796 if (imm_instr(vpo, &h, 1)) {
797 *ret = VP0_EOTHER + 2;
801 /* Experience showed that we should discard this */
805 *result = ((int) h << 8) | ((int) l & 0xff);
808 if ((PPB_IN_NIBBLE_MODE(ppbus) ||
809 PPB_IN_PS2_MODE(ppbus)) && negociated)
810 ppb_MS_microseq(ppbus, vpo->vpo_dev, transfer_epilog, NULL);
812 /* return to printer state, release the ppbus */
813 imm_disconnect(vpo, NULL, 1);