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
27 * $FreeBSD: src/sys/dev/ppbus/vpoio.c,v 1.10.2.3 2001/10/02 05:27:20 nsouch Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
35 #include <sys/malloc.h>
37 #include <machine/clock.h>
46 #include <bus/ppbus/ppbio.h>
47 #include <bus/ppbus/ppbconf.h>
48 #include <bus/ppbus/ppb_msq.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].c = d_pulse; \
120 connect_epp_microseq[i].arg[2].c = \
121 connect_spp_microseq[i].arg[2].c = 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 kprintf("%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 kprintf("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 kprintf("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 union ppb_insarg buffer = { .c = _buffer };
444 union ppb_insarg size = { .i = _size };
445 union ppb_insarg unknown = { .i = MS_UNKNOWN };
446 device_t ppbus = device_get_parent(vpo->vpo_dev);
449 ppb_MS_exec(ppbus, vpo->vpo_dev, MS_OP_PUT, buffer, size, unknown,
461 vpoio_instr(struct vpoio_data *vpo, char *_buffer, int _size)
463 union ppb_insarg buffer = { .c = _buffer };
464 union ppb_insarg size = { .i = _size };
465 union ppb_insarg unknown = { .i = MS_UNKNOWN };
466 device_t ppbus = device_get_parent(vpo->vpo_dev);
469 ppb_MS_exec(ppbus, vpo->vpo_dev, MS_OP_GET, buffer, size, unknown,
478 vpoio_select(struct vpoio_data *vpo, int initiator, int target)
480 device_t ppbus = device_get_parent(vpo->vpo_dev);
483 struct ppb_microseq select_microseq[] = {
487 #define SELECT_TARGET MS_PARAM(0, 1, MS_TYP_INT)
488 #define SELECT_INITIATOR MS_PARAM(3, 1, MS_TYP_INT)
490 /* send the select command to the drive */
492 MS_CASS(H_nAUTO | H_nSELIN | H_INIT | H_STROBE),
493 MS_CASS( H_AUTO | H_nSELIN | H_INIT | H_STROBE),
495 MS_CASS( H_AUTO | H_nSELIN | H_nINIT | H_STROBE),
497 /* now, wait until the drive is ready */
499 /* loop: */ MS_BRSET(H_ACK, 2 /* ready */),
500 MS_DBRA(-2 /* loop */),
501 /* error: */ MS_RET(1),
502 /* ready: */ MS_RET(0)
505 /* initialize the select microsequence */
506 ppb_MS_init_msq(select_microseq, 2,
507 SELECT_TARGET, 1 << target,
508 SELECT_INITIATOR, 1 << initiator);
510 ppb_MS_microseq(ppbus, vpo->vpo_dev, select_microseq, &ret);
513 return (VP0_ESELECT_TIMEOUT);
521 * H_SELIN must be low.
523 * XXX should be ported to microseq
526 vpoio_wait(struct vpoio_data *vpo, int tmo)
528 DECLARE_WAIT_MICROSEQUENCE;
530 device_t ppbus = device_get_parent(vpo->vpo_dev);
534 if (ppb_poll_device(ppbus, 150, nBUSY, nBUSY, PPB_INTR))
537 return (ppb_rstr(ppbus) & 0xf0);
541 * Return some status information.
542 * Semantics : 0xc0 = ZIP wants more data
543 * 0xd0 = ZIP wants to send more data
544 * 0xe0 = ZIP wants command
545 * 0xf0 = end of transfer, ZIP is sending status
548 ppb_MS_init_msq(wait_microseq, 2,
549 WAIT_RET, (void *)&ret,
552 ppb_MS_microseq(ppbus, vpo->vpo_dev, wait_microseq, &err);
555 return (0); /* command timed out */
563 * Low level probe of vpo device
567 vpoio_probe(device_t dev, struct vpoio_data *vpo)
571 /* ppbus dependent initialisation */
575 * Initialize microsequence code
579 /* now, try to initialise the drive */
580 if ((error = vpoio_detect(vpo))) {
590 * Low level attachment of vpo device
594 vpoio_attach(struct vpoio_data *vpo)
596 DECLARE_NIBBLE_INBYTE_SUBMICROSEQ;
597 device_t ppbus = device_get_parent(vpo->vpo_dev);
600 vpo->vpo_nibble_inbyte_msq = kmalloc(sizeof(nibble_inbyte_submicroseq),
603 bcopy((void *)nibble_inbyte_submicroseq,
604 (void *)vpo->vpo_nibble_inbyte_msq,
605 sizeof(nibble_inbyte_submicroseq));
607 ppb_MS_init_msq(vpo->vpo_nibble_inbyte_msq, 4,
608 INB_NIBBLE_H, (void *)&(vpo)->vpo_nibble.h,
609 INB_NIBBLE_L, (void *)&(vpo)->vpo_nibble.l,
610 INB_NIBBLE_F, nibble_inbyte_hook,
611 INB_NIBBLE_P, (void *)&(vpo)->vpo_nibble);
614 * Initialize mode dependent in/out microsequences
616 if ((error = ppb_request_bus(ppbus, vpo->vpo_dev, PPB_WAIT)))
619 /* ppbus sets automatically the last mode entered during detection */
620 switch (vpo->vpo_mode_found) {
622 ppb_MS_GET_init(ppbus, vpo->vpo_dev, epp17_instr_body);
623 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, epp17_outstr_body);
624 kprintf("vpo%d: EPP mode\n", vpo->vpo_unit);
627 ppb_MS_GET_init(ppbus, vpo->vpo_dev, ps2_inbyte_submicroseq);
628 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, spp_outbyte_submicroseq);
629 kprintf("vpo%d: PS2 mode\n", vpo->vpo_unit);
631 case VP0_MODE_NIBBLE:
632 ppb_MS_GET_init(ppbus, vpo->vpo_dev, vpo->vpo_nibble_inbyte_msq);
633 ppb_MS_PUT_init(ppbus, vpo->vpo_dev, spp_outbyte_submicroseq);
634 kprintf("vpo%d: NIBBLE mode\n", vpo->vpo_unit);
637 panic("vpo: unknown mode %d", vpo->vpo_mode_found);
640 ppb_release_bus(ppbus, vpo->vpo_dev);
651 vpoio_reset_bus(struct vpoio_data *vpo)
653 /* first, connect to the drive */
654 if (vpoio_connect(vpo, PPB_WAIT|PPB_INTR) || !vpoio_in_disk_mode(vpo)) {
657 kprintf("%s: not in disk mode!\n", __func__);
660 vpoio_disconnect(vpo);
664 /* reset the SCSI bus */
667 /* then disconnect */
668 vpoio_disconnect(vpo);
676 * Send an SCSI command
680 vpoio_do_scsi(struct vpoio_data *vpo, int host, int target, char *command,
681 int clen, char *buffer, int blen, int *result, int *count,
684 device_t ppbus = device_get_parent(vpo->vpo_dev);
691 * enter disk state, allocate the ppbus
694 * Should we allow this call to be interruptible?
695 * The only way to report the interruption is to return
696 * EIO do upper SCSI code :^(
698 if ((error = vpoio_connect(vpo, PPB_WAIT|PPB_INTR)))
701 if (!vpoio_in_disk_mode(vpo)) {
702 *ret = VP0_ECONNECT; goto error;
705 if ((*ret = vpoio_select(vpo,host,target)))
709 * Send the command ...
711 * set H_SELIN low for vpoio_wait().
713 ppb_wctr(ppbus, H_AUTO | H_nSELIN | H_INIT | H_STROBE);
715 for (k = 0; k < clen; k++) {
716 if (vpoio_wait(vpo, VP0_FAST_SPINTMO) != (char)0xe0) {
717 *ret = VP0_ECMD_TIMEOUT;
720 if (vpoio_outstr(vpo, &command[k], 1)) {
721 *ret = VP0_EPPDATA_TIMEOUT;
733 if (!(r = vpoio_wait(vpo, VP0_LOW_SPINTMO))) {
734 *ret = VP0_ESTATUS_TIMEOUT; goto error;
737 /* stop when the ZIP wants to send status */
741 if (*count >= blen) {
742 *ret = VP0_EDATA_OVERFLOW;
746 /* if in EPP mode or writing bytes, try to transfer a sector
747 * otherwise, just send one byte
749 if (PPB_IN_EPP_MODE(ppbus) || r == (char)0xc0)
750 len = (((blen - *count) >= VP0_SECTOR_SIZE)) ?
755 /* ZIP wants to send data? */
757 error = vpoio_outstr(vpo, &buffer[*count], len);
759 error = vpoio_instr(vpo, &buffer[*count], len);
769 if (vpoio_instr(vpo, &l, 1)) {
770 *ret = VP0_EOTHER; goto error;
773 /* check if the ZIP wants to send more status */
774 if (vpoio_wait(vpo, VP0_FAST_SPINTMO) == (char)0xf0)
775 if (vpoio_instr(vpo, &h, 1)) {
776 *ret = VP0_EOTHER+2; goto error;
779 *result = ((int) h << 8) | ((int) l & 0xff);
782 /* return to printer state, release the ppbus */
783 vpoio_disconnect(vpo);