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
27 * $FreeBSD: src/sys/dev/ppbus/immio.c,v 1.10.2.3 2001/10/02 05:27:20 nsouch Exp $
32 * Iomega ZIP+ Matchmaker Parallel Port Interface driver
34 * Thanks to David Campbell work on the Linux driver and the Iomega specs
35 * Thanks to Thiebault Moeglin for the drive
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/module.h>
42 #include <sys/malloc.h>
48 #include <dev/ppbus/ppbio.h>
49 #include <dev/ppbus/ppbconf.h>
50 #include <dev/ppbus/ppb_msq.h>
51 #include <dev/ppbus/vpoio.h>
52 #include <dev/ppbus/ppb_1284.h>
56 #define VP0_SELTMO 5000 /* select timeout */
57 #define VP0_FAST_SPINTMO 500000 /* wait status timeout */
58 #define VP0_LOW_SPINTMO 5000000 /* wait status timeout */
60 #define VP0_SECTOR_SIZE 512
63 * Microcode to execute very fast I/O sequences at the lowest bus level.
66 #define WAIT_RET MS_PARAM(7, 2, MS_TYP_PTR)
67 #define WAIT_TMO MS_PARAM(1, 0, MS_TYP_INT)
69 #define DECLARE_WAIT_MICROSEQUENCE \
70 struct ppb_microseq wait_microseq[] = { \
74 MS_BRSET(nBUSY, 4 /* ready */), \
75 MS_DBRA(-2 /* loop */), \
77 MS_RET(1), /* timed out */ \
80 MS_RFETCH(MS_REG_STR, 0xb8, MS_UNKNOWN ), \
81 MS_RET(0) /* no error */ \
84 #define SELECT_TARGET MS_PARAM(6, 1, MS_TYP_CHA)
86 #define DECLARE_SELECT_MICROSEQUENCE \
87 struct ppb_microseq select_microseq[] = { \
89 /* first, check there is nothing holding onto the bus */ \
92 MS_BRCLEAR(0x8, 2 /* _ready */), \
93 MS_DBRA(-2 /* _loop */), \
94 MS_RET(2), /* bus busy */ \
97 MS_DASS(MS_UNKNOWN /* 0x80 | 1 << target */), \
101 /* now, wait until the drive is ready */ \
102 MS_SET(VP0_SELTMO), \
104 MS_BRSET(0x8, 3 /* ready */), \
105 MS_DBRA(-2 /* loop */), \
108 MS_RET(VP0_ESELECT_TIMEOUT), \
114 static struct ppb_microseq transfer_epilog[] = {
122 #define CPP_S1 MS_PARAM(10, 2, MS_TYP_PTR)
123 #define CPP_S2 MS_PARAM(13, 2, MS_TYP_PTR)
124 #define CPP_S3 MS_PARAM(16, 2, MS_TYP_PTR)
125 #define CPP_PARAM MS_PARAM(17, 1, MS_TYP_CHA)
127 #define DECLARE_CPP_MICROSEQ \
128 struct ppb_microseq cpp_microseq[] = { \
129 MS_CASS(0x0c), MS_DELAY(2), \
130 MS_DASS(0xaa), MS_DELAY(10), \
131 MS_DASS(0x55), MS_DELAY(10), \
132 MS_DASS(0x00), MS_DELAY(10), \
133 MS_DASS(0xff), MS_DELAY(10), \
134 MS_RFETCH(MS_REG_STR, 0xb8, MS_UNKNOWN /* &s1 */), \
135 MS_DASS(0x87), MS_DELAY(10), \
136 MS_RFETCH(MS_REG_STR, 0xb8, MS_UNKNOWN /* &s2 */), \
137 MS_DASS(0x78), MS_DELAY(10), \
138 MS_RFETCH(MS_REG_STR, 0x38, MS_UNKNOWN /* &s3 */), \
139 MS_DASS(MS_UNKNOWN /* param */), \
141 MS_CASS(0x0c), MS_DELAY(10), \
142 MS_CASS(0x0d), MS_DELAY(2), \
143 MS_CASS(0x0c), MS_DELAY(10), \
144 MS_DASS(0xff), MS_DELAY(10), \
148 #define NEGOCIATED_MODE MS_PARAM(2, 1, MS_TYP_CHA)
150 #define DECLARE_NEGOCIATE_MICROSEQ \
151 struct ppb_microseq negociate_microseq[] = { \
154 MS_DASS(MS_UNKNOWN /* mode */), \
158 MS_BRSET(0x20, 5 /* continue */), \
163 MS_RET(VP0_ENEGOCIATE), \
172 #define INB_NIBBLE_L MS_PARAM(3, 2, MS_TYP_PTR)
173 #define INB_NIBBLE_H MS_PARAM(6, 2, MS_TYP_PTR)
174 #define INB_NIBBLE_F MS_PARAM(9, 0, MS_TYP_FUN)
175 #define INB_NIBBLE_P MS_PARAM(9, 1, MS_TYP_PTR)
178 * This is the sub-microseqence for MS_GET in NIBBLE mode
179 * Retrieve the two nibbles and call the C function to generate the character
180 * and store it in the buffer (see nibble_inbyte_hook())
183 #define DECLARE_NIBBLE_INBYTE_SUBMICROSEQ \
184 struct ppb_microseq nibble_inbyte_submicroseq[] = { \
189 MS_RFETCH(MS_REG_STR, MS_FETCH_ALL, MS_UNKNOWN /* low nibble */),\
192 MS_RFETCH(MS_REG_STR, MS_FETCH_ALL, MS_UNKNOWN /* high nibble */),\
195 /* do a C call to format the received nibbles */ \
196 MS_C_CALL(MS_UNKNOWN /* C hook */, MS_UNKNOWN /* param */), \
197 MS_DBRA(-7 /* loop */), \
201 static struct ppb_microseq reset_microseq[] = {
214 * nibble_inbyte_hook()
216 * Formats high and low nibble into a character
219 nibble_inbyte_hook (void *p, char *ptr)
221 struct vpo_nibble *s = (struct vpo_nibble *)p;
223 /* increment the buffer pointer */
224 *ptr = ((s->l >> 4) & 0x0f) + (s->h & 0xf0);
230 * This is the sub-microseqence for MS_GET in PS2 mode
232 static struct ppb_microseq ps2_inbyte_submicroseq[] = {
237 MS_RFETCH_P(1, MS_REG_DTR, MS_FETCH_ALL),
239 MS_DBRA(-4 /* loop */),
245 * This is the sub-microsequence for MS_PUT in both NIBBLE and PS2 modes
247 static struct ppb_microseq spp_outbyte_submicroseq[] = {
251 MS_RASSERT_P(1, MS_REG_DTR),
253 MS_DBRA(0), /* decrement counter */
254 MS_RASSERT_P(1, MS_REG_DTR),
256 MS_DBRA(-6 /* loop */),
258 /* return from the put call */
263 /* EPP 1.7 microsequences, ptr and len set at runtime */
264 static struct ppb_microseq epp17_outstr[] = {
266 MS_RASSERT_P(MS_ACCUM, MS_REG_EPP_D),
271 static struct ppb_microseq epp17_instr[] = {
273 MS_RFETCH_P(MS_ACCUM, MS_REG_EPP_D, MS_FETCH_ALL),
279 imm_disconnect(struct vpoio_data *vpo, int *connected, int release_bus)
281 DECLARE_CPP_MICROSEQ;
283 device_t ppbus = device_get_parent(vpo->vpo_dev);
287 /* all should be ok */
291 ppb_MS_init_msq(cpp_microseq, 4, CPP_S1, (void *)&s1,
292 CPP_S2, (void *)&s2, CPP_S3, (void *)&s3,
295 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
297 if ((s1 != (char)0xb8 || s2 != (char)0x18 || s3 != (char)0x38)) {
299 printf("imm%d: (disconnect) s1=0x%x s2=0x%x, s3=0x%x\n",
300 vpo->vpo_unit, s1 & 0xff, s2 & 0xff, s3 & 0xff);
302 *connected = VP0_ECONNECT;
306 return (ppb_release_bus(ppbus, vpo->vpo_dev));
312 * how : PPB_WAIT or PPB_DONTWAIT
315 imm_connect(struct vpoio_data *vpo, int how, int *disconnected, int request_bus)
317 DECLARE_CPP_MICROSEQ;
319 device_t ppbus = device_get_parent(vpo->vpo_dev);
324 /* all should be ok */
329 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, how)))
332 ppb_MS_init_msq(cpp_microseq, 3, CPP_S1, (void *)&s1,
333 CPP_S2, (void *)&s2, CPP_S3, (void *)&s3);
335 /* select device 0 in compatible mode */
336 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0xe0);
337 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
339 /* disconnect all devices */
340 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0x30);
341 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
343 if (PPB_IN_EPP_MODE(ppbus))
344 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0x28);
346 ppb_MS_init_msq(cpp_microseq, 1, CPP_PARAM, 0xe0);
348 ppb_MS_microseq(ppbus, vpo->vpo_dev, cpp_microseq, &ret);
350 if ((s1 != (char)0xb8 || s2 != (char)0x18 || s3 != (char)0x30)) {
352 printf("imm%d: (connect) s1=0x%x s2=0x%x, s3=0x%x\n",
353 vpo->vpo_unit, s1 & 0xff, s2 & 0xff, s3 & 0xff);
355 *disconnected = VP0_ECONNECT;
364 * Detect and initialise the VP0 adapter.
367 imm_detect(struct vpoio_data *vpo)
369 device_t ppbus = device_get_parent(vpo->vpo_dev);
372 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, PPB_DONTWAIT)))
375 /* disconnect the drive, keep the bus */
376 imm_disconnect(vpo, NULL, 0);
378 vpo->vpo_mode_found = VP0_MODE_UNDEFINED;
381 /* try to enter EPP mode since vpoio failure put the bus in NIBBLE */
382 if (ppb_set_mode(ppbus, PPB_EPP) != -1) {
383 imm_connect(vpo, PPB_DONTWAIT, &error, 0);
386 /* if connection failed try PS/2 then NIBBLE modes */
388 if (ppb_set_mode(ppbus, PPB_PS2) != -1) {
389 imm_connect(vpo, PPB_DONTWAIT, &error, 0);
392 if (ppb_set_mode(ppbus, PPB_NIBBLE) != -1) {
393 imm_connect(vpo, PPB_DONTWAIT, &error, 0);
396 vpo->vpo_mode_found = VP0_MODE_NIBBLE;
398 printf("imm%d: NIBBLE mode unavailable!\n", vpo->vpo_unit);
402 vpo->vpo_mode_found = VP0_MODE_PS2;
405 vpo->vpo_mode_found = VP0_MODE_EPP;
408 /* send SCSI reset signal */
409 ppb_MS_microseq(ppbus, vpo->vpo_dev, reset_microseq, NULL);
411 /* release the bus now */
412 imm_disconnect(vpo, &error, 1);
414 /* ensure we are disconnected or daisy chained peripheral
415 * may cause serious problem to the disk */
419 printf("imm%d: can't disconnect from the drive\n",
427 ppb_release_bus(ppbus, vpo->vpo_dev);
428 return (VP0_EINITFAILED);
435 imm_outstr(struct vpoio_data *vpo, char *buffer, int size)
437 device_t ppbus = device_get_parent(vpo->vpo_dev);
440 if (PPB_IN_EPP_MODE(ppbus))
441 ppb_reset_epp_timeout(ppbus);
443 ppb_MS_exec(ppbus, vpo->vpo_dev, MS_OP_PUT, (union ppb_insarg)buffer,
444 (union ppb_insarg)size, (union ppb_insarg)MS_UNKNOWN, &error);
453 imm_instr(struct vpoio_data *vpo, char *buffer, int size)
455 device_t ppbus = device_get_parent(vpo->vpo_dev);
458 if (PPB_IN_EPP_MODE(ppbus))
459 ppb_reset_epp_timeout(ppbus);
461 ppb_MS_exec(ppbus, vpo->vpo_dev, MS_OP_GET, (union ppb_insarg)buffer,
462 (union ppb_insarg)size, (union ppb_insarg)MS_UNKNOWN, &error);
468 imm_select(struct vpoio_data *vpo, int initiator, int target)
470 DECLARE_SELECT_MICROSEQUENCE;
471 device_t ppbus = device_get_parent(vpo->vpo_dev);
474 /* initialize the select microsequence */
475 ppb_MS_init_msq(select_microseq, 1,
476 SELECT_TARGET, 1 << initiator | 1 << target);
478 ppb_MS_microseq(ppbus, vpo->vpo_dev, select_microseq, &ret);
486 * H_SELIN must be low.
490 imm_wait(struct vpoio_data *vpo, int tmo)
492 DECLARE_WAIT_MICROSEQUENCE;
494 device_t ppbus = device_get_parent(vpo->vpo_dev);
498 * Return some status information.
499 * Semantics : 0x88 = ZIP+ wants more data
500 * 0x98 = ZIP+ wants to send more data
501 * 0xa8 = ZIP+ wants command
502 * 0xb8 = end of transfer, ZIP+ is sending status
505 ppb_MS_init_msq(wait_microseq, 2,
506 WAIT_RET, (void *)&ret,
509 ppb_MS_microseq(ppbus, vpo->vpo_dev, wait_microseq, &err);
512 return (0); /* command timed out */
518 imm_negociate(struct vpoio_data *vpo)
520 DECLARE_NEGOCIATE_MICROSEQ;
521 device_t ppbus = device_get_parent(vpo->vpo_dev);
525 if (PPB_IN_NIBBLE_MODE(ppbus))
527 else if (PPB_IN_PS2_MODE(ppbus))
532 #if 0 /* XXX use standalone code not to depend on ppb_1284 code yet */
533 ret = ppb_1284_negociate(ppbus, negociate_mode);
536 return (VP0_ENEGOCIATE);
539 ppb_MS_init_msq(negociate_microseq, 1,
540 NEGOCIATED_MODE, negociate_mode);
542 ppb_MS_microseq(ppbus, vpo->vpo_dev, negociate_microseq, &ret);
550 * Low level probe of vpo device
554 imm_probe(device_t dev, struct vpoio_data *vpo)
558 /* ppbus dependent initialisation */
561 /* now, try to initialise the drive */
562 if ((error = imm_detect(vpo))) {
572 * Low level attachment of vpo device
576 imm_attach(struct vpoio_data *vpo)
578 DECLARE_NIBBLE_INBYTE_SUBMICROSEQ;
579 device_t ppbus = device_get_parent(vpo->vpo_dev);
583 * Initialize microsequence code
585 vpo->vpo_nibble_inbyte_msq = (struct ppb_microseq *)malloc(
586 sizeof(nibble_inbyte_submicroseq), M_DEVBUF, M_NOWAIT);
588 if (!vpo->vpo_nibble_inbyte_msq)
591 bcopy((void *)nibble_inbyte_submicroseq,
592 (void *)vpo->vpo_nibble_inbyte_msq,
593 sizeof(nibble_inbyte_submicroseq));
595 ppb_MS_init_msq(vpo->vpo_nibble_inbyte_msq, 4,
596 INB_NIBBLE_H, (void *)&(vpo)->vpo_nibble.h,
597 INB_NIBBLE_L, (void *)&(vpo)->vpo_nibble.l,
598 INB_NIBBLE_F, nibble_inbyte_hook,
599 INB_NIBBLE_P, (void *)&(vpo)->vpo_nibble);
602 * Initialize mode dependent in/out microsequences
604 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, PPB_WAIT)))
607 /* ppbus automatically restore the last mode entered during detection */
608 switch (vpo->vpo_mode_found) {
610 ppb_MS_GET_init(ppbus, vpo->vpo_dev, epp17_instr);
611 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, epp17_outstr);
612 printf("imm%d: EPP mode\n", vpo->vpo_unit);
615 ppb_MS_GET_init(ppbus, vpo->vpo_dev, ps2_inbyte_submicroseq);
616 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, spp_outbyte_submicroseq);
617 printf("imm%d: PS2 mode\n", vpo->vpo_unit);
619 case VP0_MODE_NIBBLE:
620 ppb_MS_GET_init(ppbus, vpo->vpo_dev, vpo->vpo_nibble_inbyte_msq);
621 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, spp_outbyte_submicroseq);
622 printf("imm%d: NIBBLE mode\n", vpo->vpo_unit);
625 panic("imm: unknown mode %d", vpo->vpo_mode_found);
628 ppb_release_bus(ppbus, vpo->vpo_dev);
638 imm_reset_bus(struct vpoio_data *vpo)
640 device_t ppbus = device_get_parent(vpo->vpo_dev);
643 /* first, connect to the drive and request the bus */
644 imm_connect(vpo, PPB_WAIT|PPB_INTR, &disconnected, 1);
648 /* reset the SCSI bus */
649 ppb_MS_microseq(ppbus, vpo->vpo_dev, reset_microseq, NULL);
651 /* then disconnect */
652 imm_disconnect(vpo, NULL, 1);
661 * Send an SCSI command
665 imm_do_scsi(struct vpoio_data *vpo, int host, int target, char *command,
666 int clen, char *buffer, int blen, int *result, int *count,
669 device_t ppbus = device_get_parent(vpo->vpo_dev);
672 int len, error = 0, not_connected = 0;
677 * enter disk state, allocate the ppbus
680 * Should we allow this call to be interruptible?
681 * The only way to report the interruption is to return
682 * EIO to upper SCSI code :^(
684 if ((error = imm_connect(vpo, PPB_WAIT|PPB_INTR, ¬_connected, 1)))
688 *ret = VP0_ECONNECT; goto error;
692 * Select the drive ...
694 if ((*ret = imm_select(vpo,host,target)))
698 * Send the command ...
700 for (k = 0; k < clen; k+=2) {
701 if (imm_wait(vpo, VP0_FAST_SPINTMO) != (char)0xa8) {
702 *ret = VP0_ECMD_TIMEOUT;
705 if (imm_outstr(vpo, &command[k], 2)) {
706 *ret = VP0_EPPDATA_TIMEOUT;
711 if (!(r = imm_wait(vpo, VP0_LOW_SPINTMO))) {
712 *ret = VP0_ESTATUS_TIMEOUT; goto error;
715 if ((r & 0x30) == 0x10) {
716 if (imm_negociate(vpo)) {
717 *ret = VP0_ENEGOCIATE;
724 * Complete transfer ...
729 if (!(r = imm_wait(vpo, VP0_LOW_SPINTMO))) {
730 *ret = VP0_ESTATUS_TIMEOUT; goto error;
733 /* stop when the ZIP+ wants to send status */
737 if (*count >= blen) {
738 *ret = VP0_EDATA_OVERFLOW;
742 /* ZIP+ wants to send data? */
743 if (r == (char)0x88) {
744 len = (((blen - *count) >= VP0_SECTOR_SIZE)) ?
747 error = imm_outstr(vpo, &buffer[*count], len);
749 if (!PPB_IN_EPP_MODE(ppbus))
752 len = (((blen - *count) >= VP0_SECTOR_SIZE)) ?
755 error = imm_instr(vpo, &buffer[*count], len);
766 if ((PPB_IN_NIBBLE_MODE(ppbus) ||
767 PPB_IN_PS2_MODE(ppbus)) && negociated)
768 ppb_MS_microseq(ppbus, vpo->vpo_dev, transfer_epilog, NULL);
771 * Retrieve status ...
773 if (imm_negociate(vpo)) {
774 *ret = VP0_ENEGOCIATE;
779 if (imm_instr(vpo, &l, 1)) {
780 *ret = VP0_EOTHER; goto error;
783 /* check if the ZIP+ wants to send more status */
784 if (imm_wait(vpo, VP0_FAST_SPINTMO) == (char)0xb8)
785 if (imm_instr(vpo, &h, 1)) {
786 *ret = VP0_EOTHER+2; goto error;
789 /* Experience showed that we should discard this */
793 *result = ((int) h << 8) | ((int) l & 0xff);
796 if ((PPB_IN_NIBBLE_MODE(ppbus) ||
797 PPB_IN_PS2_MODE(ppbus)) && negociated)
798 ppb_MS_microseq(ppbus, vpo->vpo_dev, transfer_epilog, NULL);
800 /* return to printer state, release the ppbus */
801 imm_disconnect(vpo, NULL, 1);