2 * Copyright (c) 1998, 1999 Nicolas Souchu
3 * Copyright (c) 2000 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 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/module.h>
38 #include <sys/malloc.h>
40 #include <machine/clock.h>
46 #include <dev/ppbus/ppbio.h>
47 #include <dev/ppbus/ppbconf.h>
48 #include <dev/ppbus/ppb_msq.h>
49 #include <dev/ppbus/vpoio.h>
54 * The driver pools the drive. We may add a timeout queue to avoid
55 * active polling on nACK. I've tried this but it leads to unreliable
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 */
63 * Actually, VP0 timings are more accurate (about few 16MHZ cycles),
64 * but succeeding in respecting such timings leads to architecture
65 * dependent considerations.
69 #define VP0_SECTOR_SIZE 512
70 #define VP0_BUFFER_SIZE 0x12000
72 #define n(flags) (~(flags) & (flags))
77 #define H_AUTO n(AUTOFEED)
78 #define H_nAUTO AUTOFEED
79 #define H_STROBE n(STROBE)
80 #define H_nSTROBE STROBE
81 #define H_BSY n(nBUSY)
84 #define H_nSEL n(SELECT)
86 #define H_nERR n(PERROR)
88 #define H_nACK n(nACK)
90 #define H_nFLT n(nFAULT)
91 #define H_SELIN n(SELECTIN)
92 #define H_nSELIN SELECTIN
94 #define H_nINIT n(nINIT)
97 * Microcode to execute very fast I/O sequences at the lowest bus level.
100 #define WAIT_RET MS_PARAM(4, 2, MS_TYP_PTR)
101 #define WAIT_TMO MS_PARAM(0, 0, MS_TYP_INT)
103 #define DECLARE_WAIT_MICROSEQUENCE \
104 struct ppb_microseq wait_microseq[] = { \
105 MS_SET(MS_UNKNOWN), \
107 MS_BRSET(nBUSY, 2 /* ready */), \
108 MS_DBRA(-2 /* loop */), \
109 MS_RET(1), /* timed out */ \
111 MS_RFETCH(MS_REG_STR, 0xf0, MS_UNKNOWN), \
112 MS_RET(0) /* no error */ \
115 /* call this macro to initialize connect/disconnect microsequences */
116 #define INIT_TRIG_MICROSEQ { \
118 for (i=1; i <= 7; i+=2) { \
119 disconnect_microseq[i].arg[2] = (union ppb_insarg)d_pulse; \
120 connect_epp_microseq[i].arg[2] = \
121 connect_spp_microseq[i].arg[2] = (union ppb_insarg)c_pulse; \
125 #define trig_d_pulse MS_TRIG(MS_REG_CTR,5,MS_UNKNOWN /* d_pulse */)
126 static char d_pulse[] = {
127 H_AUTO | H_nSELIN | H_INIT | H_STROBE, 0,
128 H_nAUTO | H_nSELIN | H_INIT | H_STROBE, VP0_PULSE,
129 H_AUTO | H_nSELIN | H_INIT | H_STROBE, 0,
130 H_AUTO | H_SELIN | H_INIT | H_STROBE, VP0_PULSE,
131 H_AUTO | H_nSELIN | H_INIT | H_STROBE, VP0_PULSE
134 #define trig_c_pulse MS_TRIG(MS_REG_CTR,5,MS_UNKNOWN /* c_pulse */)
135 static char c_pulse[] = {
136 H_AUTO | H_nSELIN | H_INIT | H_STROBE, 0,
137 H_AUTO | H_SELIN | H_INIT | H_STROBE, 0,
138 H_nAUTO | H_SELIN | H_INIT | H_STROBE, VP0_PULSE,
139 H_AUTO | H_SELIN | H_INIT | H_STROBE, 0,
140 H_AUTO | H_nSELIN | H_INIT | H_STROBE, VP0_PULSE
143 static struct ppb_microseq disconnect_microseq[] = {
144 MS_DASS(0x0), trig_d_pulse, MS_DASS(0x3c), trig_d_pulse,
145 MS_DASS(0x20), trig_d_pulse, MS_DASS(0xf), trig_d_pulse, MS_RET(0)
148 static struct ppb_microseq connect_epp_microseq[] = {
149 MS_DASS(0x0), trig_c_pulse, MS_DASS(0x3c), trig_c_pulse,
150 MS_DASS(0x20), trig_c_pulse, MS_DASS(0xcf), trig_c_pulse, MS_RET(0)
153 static struct ppb_microseq connect_spp_microseq[] = {
154 MS_DASS(0x0), trig_c_pulse, MS_DASS(0x3c), trig_c_pulse,
155 MS_DASS(0x20), trig_c_pulse, MS_DASS(0x8f), trig_c_pulse, MS_RET(0)
159 * nibble_inbyte_hook()
161 * Formats high and low nibble into a character
164 nibble_inbyte_hook (void *p, char *ptr)
166 struct vpo_nibble *s = (struct vpo_nibble *)p;
168 /* increment the buffer pointer */
169 *ptr++ = ((s->l >> 4) & 0x0f) + (s->h & 0xf0);
174 #define INB_NIBBLE_H MS_PARAM(2, 2, MS_TYP_PTR)
175 #define INB_NIBBLE_L MS_PARAM(4, 2, MS_TYP_PTR)
176 #define INB_NIBBLE_F MS_PARAM(5, 0, MS_TYP_FUN)
177 #define INB_NIBBLE_P MS_PARAM(5, 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[] = { \
188 MS_CASS( H_AUTO | H_SELIN | H_INIT | H_STROBE), \
189 MS_DELAY(VP0_PULSE), \
190 MS_RFETCH(MS_REG_STR, MS_FETCH_ALL, MS_UNKNOWN /* high nibble */),\
191 MS_CASS(H_nAUTO | H_SELIN | H_INIT | H_STROBE), \
192 MS_RFETCH(MS_REG_STR, MS_FETCH_ALL, MS_UNKNOWN /* low nibble */),\
193 /* do a C call to format the received nibbles */ \
194 MS_C_CALL(MS_UNKNOWN /* C hook */, MS_UNKNOWN /* param */),\
195 MS_DBRA(-7 /* loop */), \
196 MS_CASS(H_AUTO | H_nSELIN | H_INIT | H_STROBE), \
201 * This is the sub-microseqence for MS_GET in PS2 mode
203 static struct ppb_microseq ps2_inbyte_submicroseq[] = {
204 MS_CASS(PCD | H_AUTO | H_SELIN | H_INIT | H_nSTROBE),
207 MS_RFETCH_P(1, MS_REG_DTR, MS_FETCH_ALL),
208 MS_CASS(PCD | H_nAUTO | H_SELIN | H_INIT | H_nSTROBE),
209 MS_CASS(PCD | H_AUTO | H_SELIN | H_INIT | H_nSTROBE),
210 MS_DBRA(-4 /* loop */),
212 MS_CASS(H_AUTO | H_nSELIN | H_INIT | H_STROBE),
217 * This is the sub-microsequence for MS_PUT in both NIBBLE and PS2 modes
219 static struct ppb_microseq spp_outbyte_submicroseq[] = {
222 MS_RASSERT_P(1, MS_REG_DTR),
223 MS_CASS(H_nAUTO | H_nSELIN | H_INIT | H_STROBE),
224 MS_CASS( H_AUTO | H_nSELIN | H_INIT | H_STROBE),
226 MS_DBRA(-5 /* loop */),
228 /* return from the put call */
232 /* EPP 1.7 microsequences, ptr and len set at runtime */
233 static struct ppb_microseq epp17_outstr_body[] = {
234 MS_CASS(H_AUTO | H_SELIN | H_INIT | H_STROBE),
237 MS_RASSERT_P(1, MS_REG_EPP_D),
238 MS_BRSET(TIMEOUT, 3 /* error */), /* EPP timeout? */
239 MS_DBRA(-3 /* loop */),
241 MS_CASS(H_AUTO | H_nSELIN | H_INIT | H_STROBE),
244 MS_CASS(H_AUTO | H_nSELIN | H_INIT | H_STROBE),
248 static struct ppb_microseq epp17_instr_body[] = {
249 MS_CASS(PCD | H_AUTO | H_SELIN | H_INIT | H_STROBE),
252 MS_RFETCH_P(1, MS_REG_EPP_D, MS_FETCH_ALL),
253 MS_BRSET(TIMEOUT, 3 /* error */), /* EPP timeout? */
254 MS_DBRA(-3 /* loop */),
256 MS_CASS(PCD | H_AUTO | H_nSELIN | H_INIT | H_STROBE),
259 MS_CASS(PCD | H_AUTO | H_nSELIN | H_INIT | H_STROBE),
263 static struct ppb_microseq in_disk_mode[] = {
264 MS_CASS( H_AUTO | H_nSELIN | H_INIT | H_STROBE),
265 MS_CASS(H_nAUTO | H_nSELIN | H_INIT | H_STROBE),
267 MS_BRCLEAR(H_FLT, 3 /* error */),
268 MS_CASS( H_AUTO | H_nSELIN | H_INIT | H_STROBE),
269 MS_BRSET(H_FLT, 1 /* error */),
277 vpoio_disconnect(struct vpoio_data *vpo)
279 device_t ppbus = device_get_parent(vpo->vpo_dev);
282 ppb_MS_microseq(ppbus, vpo->vpo_dev, disconnect_microseq, &ret);
283 return (ppb_release_bus(ppbus, vpo->vpo_dev));
287 * how : PPB_WAIT or PPB_DONTWAIT
290 vpoio_connect(struct vpoio_data *vpo, int how)
292 device_t ppbus = device_get_parent(vpo->vpo_dev);
296 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, how))) {
299 printf("%s: can't request bus!\n", __func__);
304 if (PPB_IN_EPP_MODE(ppbus))
305 ppb_MS_microseq(ppbus, vpo->vpo_dev, connect_epp_microseq, &ret);
307 ppb_MS_microseq(ppbus, vpo->vpo_dev, connect_spp_microseq, &ret);
315 * SCSI reset signal, the drive must be in disk mode
318 vpoio_reset (struct vpoio_data *vpo)
320 device_t ppbus = device_get_parent(vpo->vpo_dev);
323 struct ppb_microseq reset_microseq[] = {
325 #define INITIATOR MS_PARAM(0, 1, MS_TYP_INT)
328 MS_CASS(H_AUTO | H_nSELIN | H_nINIT | H_STROBE),
330 MS_CASS(H_AUTO | H_nSELIN | H_INIT | H_STROBE),
334 ppb_MS_init_msq(reset_microseq, 1, INITIATOR, 1 << VP0_INITIATOR);
335 ppb_MS_microseq(ppbus, vpo->vpo_dev, reset_microseq, &ret);
341 * vpoio_in_disk_mode()
344 vpoio_in_disk_mode(struct vpoio_data *vpo)
346 device_t ppbus = device_get_parent(vpo->vpo_dev);
349 ppb_MS_microseq(ppbus, vpo->vpo_dev, in_disk_mode, &ret);
357 * Detect and initialise the VP0 adapter.
360 vpoio_detect(struct vpoio_data *vpo)
362 device_t ppbus = device_get_parent(vpo->vpo_dev);
365 /* allocate the bus, then apply microsequences */
366 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, PPB_DONTWAIT)))
369 /* Force disconnection */
370 ppb_MS_microseq(ppbus, vpo->vpo_dev, disconnect_microseq, &ret);
372 /* Try to enter EPP mode, then connect to the drive in EPP mode */
373 if (ppb_set_mode(ppbus, PPB_EPP) != -1) {
374 /* call manually the microseq instead of using the appropriate function
375 * since we already requested the ppbus */
376 ppb_MS_microseq(ppbus, vpo->vpo_dev, connect_epp_microseq, &ret);
379 /* If EPP mode switch failed or ZIP connection in EPP mode failed,
380 * try to connect in NIBBLE mode */
381 if (!vpoio_in_disk_mode(vpo)) {
383 /* The interface must be at least PS/2 or NIBBLE capable.
384 * There is no way to know if the ZIP will work with
385 * PS/2 mode since PS/2 and SPP both use the same connect
386 * sequence. One must supress PS/2 with boot flags if
387 * PS/2 mode fails (see ppc(4)).
389 if (ppb_set_mode(ppbus, PPB_PS2) != -1) {
390 vpo->vpo_mode_found = VP0_MODE_PS2;
392 if (ppb_set_mode(ppbus, PPB_NIBBLE) == -1)
395 vpo->vpo_mode_found = VP0_MODE_NIBBLE;
398 /* Can't know if the interface is capable of PS/2 yet */
399 ppb_MS_microseq(ppbus, vpo->vpo_dev, connect_spp_microseq, &ret);
400 if (!vpoio_in_disk_mode(vpo)) {
401 vpo->vpo_mode_found = VP0_MODE_UNDEFINED;
403 printf("vpo%d: can't connect to the drive\n",
406 /* disconnect and release the bus */
407 ppb_MS_microseq(ppbus, vpo->vpo_dev, disconnect_microseq,
412 vpo->vpo_mode_found = VP0_MODE_EPP;
415 /* send SCSI reset signal */
418 ppb_MS_microseq(ppbus, vpo->vpo_dev, disconnect_microseq, &ret);
420 /* ensure we are disconnected or daisy chained peripheral
421 * may cause serious problem to the disk */
422 if (vpoio_in_disk_mode(vpo)) {
424 printf("vpo%d: can't disconnect from the drive\n",
429 ppb_release_bus(ppbus, vpo->vpo_dev);
433 ppb_release_bus(ppbus, vpo->vpo_dev);
434 return (VP0_EINITFAILED);
441 vpoio_outstr(struct vpoio_data *vpo, char *buffer, int size)
443 device_t ppbus = device_get_parent(vpo->vpo_dev);
446 ppb_MS_exec(ppbus, vpo->vpo_dev, MS_OP_PUT, (union ppb_insarg)buffer,
447 (union ppb_insarg)size, (union ppb_insarg)MS_UNKNOWN, &error);
458 vpoio_instr(struct vpoio_data *vpo, char *buffer, int size)
460 device_t ppbus = device_get_parent(vpo->vpo_dev);
463 ppb_MS_exec(ppbus, vpo->vpo_dev, MS_OP_GET, (union ppb_insarg)buffer,
464 (union ppb_insarg)size, (union ppb_insarg)MS_UNKNOWN, &error);
472 vpoio_select(struct vpoio_data *vpo, int initiator, int target)
474 device_t ppbus = device_get_parent(vpo->vpo_dev);
477 struct ppb_microseq select_microseq[] = {
481 #define SELECT_TARGET MS_PARAM(0, 1, MS_TYP_INT)
482 #define SELECT_INITIATOR MS_PARAM(3, 1, MS_TYP_INT)
484 /* send the select command to the drive */
486 MS_CASS(H_nAUTO | H_nSELIN | H_INIT | H_STROBE),
487 MS_CASS( H_AUTO | H_nSELIN | H_INIT | H_STROBE),
489 MS_CASS( H_AUTO | H_nSELIN | H_nINIT | H_STROBE),
491 /* now, wait until the drive is ready */
493 /* loop: */ MS_BRSET(H_ACK, 2 /* ready */),
494 MS_DBRA(-2 /* loop */),
495 /* error: */ MS_RET(1),
496 /* ready: */ MS_RET(0)
499 /* initialize the select microsequence */
500 ppb_MS_init_msq(select_microseq, 2,
501 SELECT_TARGET, 1 << target,
502 SELECT_INITIATOR, 1 << initiator);
504 ppb_MS_microseq(ppbus, vpo->vpo_dev, select_microseq, &ret);
507 return (VP0_ESELECT_TIMEOUT);
515 * H_SELIN must be low.
517 * XXX should be ported to microseq
520 vpoio_wait(struct vpoio_data *vpo, int tmo)
522 DECLARE_WAIT_MICROSEQUENCE;
524 device_t ppbus = device_get_parent(vpo->vpo_dev);
528 if (ppb_poll_device(ppbus, 150, nBUSY, nBUSY, PPB_INTR))
531 return (ppb_rstr(ppbus) & 0xf0);
535 * Return some status information.
536 * Semantics : 0xc0 = ZIP wants more data
537 * 0xd0 = ZIP wants to send more data
538 * 0xe0 = ZIP wants command
539 * 0xf0 = end of transfer, ZIP is sending status
542 ppb_MS_init_msq(wait_microseq, 2,
543 WAIT_RET, (void *)&ret,
546 ppb_MS_microseq(ppbus, vpo->vpo_dev, wait_microseq, &err);
549 return (0); /* command timed out */
557 * Low level probe of vpo device
561 vpoio_probe(device_t dev, struct vpoio_data *vpo)
565 /* ppbus dependent initialisation */
569 * Initialize microsequence code
573 /* now, try to initialise the drive */
574 if ((error = vpoio_detect(vpo))) {
584 * Low level attachment of vpo device
588 vpoio_attach(struct vpoio_data *vpo)
590 DECLARE_NIBBLE_INBYTE_SUBMICROSEQ;
591 device_t ppbus = device_get_parent(vpo->vpo_dev);
594 vpo->vpo_nibble_inbyte_msq = (struct ppb_microseq *)malloc(
595 sizeof(nibble_inbyte_submicroseq), M_DEVBUF, M_NOWAIT);
597 if (!vpo->vpo_nibble_inbyte_msq)
600 bcopy((void *)nibble_inbyte_submicroseq,
601 (void *)vpo->vpo_nibble_inbyte_msq,
602 sizeof(nibble_inbyte_submicroseq));
604 ppb_MS_init_msq(vpo->vpo_nibble_inbyte_msq, 4,
605 INB_NIBBLE_H, (void *)&(vpo)->vpo_nibble.h,
606 INB_NIBBLE_L, (void *)&(vpo)->vpo_nibble.l,
607 INB_NIBBLE_F, nibble_inbyte_hook,
608 INB_NIBBLE_P, (void *)&(vpo)->vpo_nibble);
611 * Initialize mode dependent in/out microsequences
613 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, PPB_WAIT)))
616 /* ppbus sets automatically the last mode entered during detection */
617 switch (vpo->vpo_mode_found) {
619 ppb_MS_GET_init(ppbus, vpo->vpo_dev, epp17_instr_body);
620 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, epp17_outstr_body);
621 printf("vpo%d: EPP mode\n", vpo->vpo_unit);
624 ppb_MS_GET_init(ppbus, vpo->vpo_dev, ps2_inbyte_submicroseq);
625 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, spp_outbyte_submicroseq);
626 printf("vpo%d: PS2 mode\n", vpo->vpo_unit);
628 case VP0_MODE_NIBBLE:
629 ppb_MS_GET_init(ppbus, vpo->vpo_dev, vpo->vpo_nibble_inbyte_msq);
630 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, spp_outbyte_submicroseq);
631 printf("vpo%d: NIBBLE mode\n", vpo->vpo_unit);
634 panic("vpo: unknown mode %d", vpo->vpo_mode_found);
637 ppb_release_bus(ppbus, vpo->vpo_dev);
648 vpoio_reset_bus(struct vpoio_data *vpo)
650 /* first, connect to the drive */
651 if (vpoio_connect(vpo, PPB_WAIT|PPB_INTR) || !vpoio_in_disk_mode(vpo)) {
654 printf("%s: not in disk mode!\n", __func__);
657 vpoio_disconnect(vpo);
661 /* reset the SCSI bus */
664 /* then disconnect */
665 vpoio_disconnect(vpo);
673 * Send an SCSI command
677 vpoio_do_scsi(struct vpoio_data *vpo, int host, int target, char *command,
678 int clen, char *buffer, int blen, int *result, int *count,
681 device_t ppbus = device_get_parent(vpo->vpo_dev);
688 * enter disk state, allocate the ppbus
691 * Should we allow this call to be interruptible?
692 * The only way to report the interruption is to return
693 * EIO do upper SCSI code :^(
695 if ((error = vpoio_connect(vpo, PPB_WAIT|PPB_INTR)))
698 if (!vpoio_in_disk_mode(vpo)) {
699 *ret = VP0_ECONNECT; goto error;
702 if ((*ret = vpoio_select(vpo,host,target)))
706 * Send the command ...
708 * set H_SELIN low for vpoio_wait().
710 ppb_wctr(ppbus, H_AUTO | H_nSELIN | H_INIT | H_STROBE);
712 for (k = 0; k < clen; k++) {
713 if (vpoio_wait(vpo, VP0_FAST_SPINTMO) != (char)0xe0) {
714 *ret = VP0_ECMD_TIMEOUT;
717 if (vpoio_outstr(vpo, &command[k], 1)) {
718 *ret = VP0_EPPDATA_TIMEOUT;
730 if (!(r = vpoio_wait(vpo, VP0_LOW_SPINTMO))) {
731 *ret = VP0_ESTATUS_TIMEOUT; goto error;
734 /* stop when the ZIP wants to send status */
738 if (*count >= blen) {
739 *ret = VP0_EDATA_OVERFLOW;
743 /* if in EPP mode or writing bytes, try to transfer a sector
744 * otherwise, just send one byte
746 if (PPB_IN_EPP_MODE(ppbus) || r == (char)0xc0)
747 len = (((blen - *count) >= VP0_SECTOR_SIZE)) ?
752 /* ZIP wants to send data? */
754 error = vpoio_outstr(vpo, &buffer[*count], len);
756 error = vpoio_instr(vpo, &buffer[*count], len);
766 if (vpoio_instr(vpo, &l, 1)) {
767 *ret = VP0_EOTHER; goto error;
770 /* check if the ZIP wants to send more status */
771 if (vpoio_wait(vpo, VP0_FAST_SPINTMO) == (char)0xf0)
772 if (vpoio_instr(vpo, &h, 1)) {
773 *ret = VP0_EOTHER+2; goto error;
776 *result = ((int) h << 8) | ((int) l & 0xff);
779 /* return to printer state, release the ppbus */
780 vpoio_disconnect(vpo);