No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / newsmips / apbus / spifi.c
blob3a0af10426ac2115544b7330fcfbf2d39b198400
1 /* $NetBSD: spifi.c,v 1.16 2008/04/09 15:40:30 tsutsui Exp $ */
3 /*-
4 * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: spifi.c,v 1.16 2008/04/09 15:40:30 tsutsui Exp $");
32 #include <sys/param.h>
33 #include <sys/buf.h>
34 #include <sys/device.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
37 #include <sys/queue.h>
38 #include <sys/systm.h>
40 #include <uvm/uvm_extern.h>
42 #include <dev/scsipi/scsi_all.h>
43 #include <dev/scsipi/scsi_message.h>
44 #include <dev/scsipi/scsipi_all.h>
45 #include <dev/scsipi/scsiconf.h>
47 #include <newsmips/apbus/apbusvar.h>
48 #include <newsmips/apbus/spifireg.h>
49 #include <newsmips/apbus/dmac3reg.h>
50 #include <newsmips/apbus/dmac3var.h>
52 #include <machine/adrsmap.h>
54 /* #define SPIFI_DEBUG */
56 #ifdef SPIFI_DEBUG
57 # define DPRINTF printf
58 #else
59 # define DPRINTF while (0) printf
60 #endif
62 struct spifi_scb {
63 TAILQ_ENTRY(spifi_scb) chain;
64 int flags;
65 struct scsipi_xfer *xs;
66 struct scsipi_generic cmd;
67 int cmdlen;
68 int resid;
69 vaddr_t daddr;
70 uint8_t target;
71 uint8_t lun;
72 uint8_t lun_targ;
73 uint8_t status;
75 /* scb flags */
76 #define SPIFI_READ 0x80
77 #define SPIFI_DMA 0x01
79 struct spifi_softc {
80 device_t sc_dev;
81 struct scsipi_channel sc_channel;
82 struct scsipi_adapter sc_adapter;
84 struct spifi_reg *sc_reg;
85 struct spifi_scb *sc_nexus;
86 void *sc_dma; /* attached DMA softc */
87 int sc_id; /* my SCSI ID */
88 int sc_msgout;
89 uint8_t sc_omsg[16];
90 struct spifi_scb sc_scb[16];
91 TAILQ_HEAD(, spifi_scb) free_scb;
92 TAILQ_HEAD(, spifi_scb) ready_scb;
95 #define SPIFI_SYNC_OFFSET_MAX 7
97 #define SEND_REJECT 1
98 #define SEND_IDENTIFY 2
99 #define SEND_SDTR 4
101 #define SPIFI_DATAOUT 0
102 #define SPIFI_DATAIN PRS_IO
103 #define SPIFI_COMMAND PRS_CD
104 #define SPIFI_STATUS (PRS_CD | PRS_IO)
105 #define SPIFI_MSGOUT (PRS_MSG | PRS_CD)
106 #define SPIFI_MSGIN (PRS_MSG | PRS_CD | PRS_IO)
108 int spifi_match(device_t, cfdata_t, void *);
109 void spifi_attach(device_t, device_t, void *);
111 void spifi_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
112 void *);
113 struct spifi_scb *spifi_get_scb(struct spifi_softc *);
114 void spifi_free_scb(struct spifi_softc *, struct spifi_scb *);
115 int spifi_poll(struct spifi_softc *);
116 void spifi_minphys(struct buf *);
118 void spifi_sched(struct spifi_softc *);
119 int spifi_intr(void *);
120 void spifi_pmatch(struct spifi_softc *);
122 void spifi_select(struct spifi_softc *);
123 void spifi_sendmsg(struct spifi_softc *, int);
124 void spifi_command(struct spifi_softc *);
125 void spifi_data_io(struct spifi_softc *);
126 void spifi_status(struct spifi_softc *);
127 int spifi_done(struct spifi_softc *);
128 void spifi_fifo_drain(struct spifi_softc *);
129 void spifi_reset(struct spifi_softc *);
130 void spifi_bus_reset(struct spifi_softc *);
132 static int spifi_read_count(struct spifi_reg *);
133 static void spifi_write_count(struct spifi_reg *, int);
135 #define DMAC3_FASTACCESS(sc) dmac3_misc((sc)->sc_dma, DMAC3_CONF_FASTACCESS)
136 #define DMAC3_SLOWACCESS(sc) dmac3_misc((sc)->sc_dma, DMAC3_CONF_SLOWACCESS)
138 CFATTACH_DECL_NEW(spifi, sizeof(struct spifi_softc),
139 spifi_match, spifi_attach, NULL, NULL);
142 spifi_match(device_t parent, cfdata_t cf, void *aux)
144 struct apbus_attach_args *apa = aux;
146 if (strcmp(apa->apa_name, "spifi") == 0)
147 return 1;
149 return 0;
152 void
153 spifi_attach(device_t parent, device_t self, void *aux)
155 struct spifi_softc *sc = device_private(self);
156 struct apbus_attach_args *apa = aux;
157 struct dmac3_softc *dma;
158 int intr, i;
160 sc->sc_dev = self;
162 /* Initialize scbs. */
163 TAILQ_INIT(&sc->free_scb);
164 TAILQ_INIT(&sc->ready_scb);
165 for (i = 0; i < __arraycount(sc->sc_scb); i++)
166 TAILQ_INSERT_TAIL(&sc->free_scb, &sc->sc_scb[i], chain);
168 sc->sc_reg = (struct spifi_reg *)apa->apa_hwbase;
169 sc->sc_id = 7; /* XXX */
171 /* Find my dmac3. */
172 dma = dmac3_link(apa->apa_ctlnum);
173 if (dma == NULL) {
174 aprint_error(": cannot find slave dmac\n");
175 return;
177 sc->sc_dma = dma;
179 aprint_normal(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
180 aprint_normal(": SCSI ID = %d, using %s\n",
181 sc->sc_id, device_xname(dma->sc_dev));
183 dmac3_reset(sc->sc_dma);
185 DMAC3_SLOWACCESS(sc);
186 spifi_reset(sc);
187 DMAC3_FASTACCESS(sc);
189 sc->sc_adapter.adapt_dev = self;
190 sc->sc_adapter.adapt_nchannels = 1;
191 sc->sc_adapter.adapt_openings = 7;
192 sc->sc_adapter.adapt_max_periph = 1;
193 sc->sc_adapter.adapt_ioctl = NULL;
194 sc->sc_adapter.adapt_minphys = minphys;
195 sc->sc_adapter.adapt_request = spifi_scsipi_request;
197 memset(&sc->sc_channel, 0, sizeof(sc->sc_channel));
198 sc->sc_channel.chan_adapter = &sc->sc_adapter;
199 sc->sc_channel.chan_bustype = &scsi_bustype;
200 sc->sc_channel.chan_channel = 0;
201 sc->sc_channel.chan_ntargets = 8;
202 sc->sc_channel.chan_nluns = 8;
203 sc->sc_channel.chan_id = sc->sc_id;
205 if (apa->apa_slotno == 0)
206 intr = NEWS5000_INT0_DMAC;
207 else
208 intr = SLOTTOMASK(apa->apa_slotno);
209 apbus_intr_establish(0, intr, 0, spifi_intr, sc, apa->apa_name,
210 apa->apa_ctlnum);
212 config_found(self, &sc->sc_channel, scsiprint);
215 void
216 spifi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
217 void *arg)
219 struct scsipi_xfer *xs;
220 struct scsipi_periph *periph;
221 struct spifi_softc *sc = device_private(chan->chan_adapter->adapt_dev);
222 struct spifi_scb *scb;
223 u_int flags;
224 int s;
226 switch (req) {
227 case ADAPTER_REQ_RUN_XFER:
228 xs = arg;
229 periph = xs->xs_periph;
231 DPRINTF("spifi_scsi_cmd\n");
233 flags = xs->xs_control;
235 scb = spifi_get_scb(sc);
236 if (scb == NULL) {
237 panic("spifi_scsipi_request: no scb");
240 scb->xs = xs;
241 scb->flags = 0;
242 scb->status = 0;
243 scb->daddr = (vaddr_t)xs->data;
244 scb->resid = xs->datalen;
245 memcpy(&scb->cmd, xs->cmd, xs->cmdlen);
246 scb->cmdlen = xs->cmdlen;
248 scb->target = periph->periph_target;
249 scb->lun = periph->periph_lun;
250 scb->lun_targ = scb->target | (scb->lun << 3);
252 if (flags & XS_CTL_DATA_IN)
253 scb->flags |= SPIFI_READ;
255 s = splbio();
257 TAILQ_INSERT_TAIL(&sc->ready_scb, scb, chain);
259 if (sc->sc_nexus == NULL) /* IDLE */
260 spifi_sched(sc);
262 splx(s);
264 if (flags & XS_CTL_POLL) {
265 if (spifi_poll(sc)) {
266 printf("spifi: timeout\n");
267 if (spifi_poll(sc))
268 printf("spifi: timeout again\n");
271 return;
272 case ADAPTER_REQ_GROW_RESOURCES:
273 /* XXX Not supported. */
274 return;
275 case ADAPTER_REQ_SET_XFER_MODE:
276 /* XXX Not supported. */
277 return;
281 struct spifi_scb *
282 spifi_get_scb(struct spifi_softc *sc)
284 struct spifi_scb *scb;
285 int s;
287 s = splbio();
288 scb = TAILQ_FIRST(&sc->free_scb);
289 if (scb)
290 TAILQ_REMOVE(&sc->free_scb, scb, chain);
291 splx(s);
293 return scb;
296 void
297 spifi_free_scb(struct spifi_softc *sc, struct spifi_scb *scb)
299 int s;
301 s = splbio();
302 TAILQ_INSERT_HEAD(&sc->free_scb, scb, chain);
303 splx(s);
307 spifi_poll(struct spifi_softc *sc)
309 struct spifi_scb *scb = sc->sc_nexus;
310 struct scsipi_xfer *xs;
311 int count;
313 printf("%s: not implemented yet\n", __func__);
314 delay(10000);
315 scb->status = SCSI_OK;
316 scb->resid = 0;
317 spifi_done(sc);
318 return 0;
320 if (xs == NULL)
321 return 0;
323 xs = scb->xs;
324 count = xs->timeout;
326 while (count > 0) {
327 if (dmac3_intr(sc->sc_dma) != 0)
328 spifi_intr(sc);
330 if (xs->xs_status & XS_STS_DONE)
331 return 0;
332 DELAY(1000);
333 count--;
335 return 1;
338 void
339 spifi_minphys(struct buf *bp)
342 if (bp->b_bcount > 64 * 1024)
343 bp->b_bcount = 64 * 1024;
345 minphys(bp);
348 void
349 spifi_sched(struct spifi_softc *sc)
351 struct spifi_scb *scb;
353 scb = TAILQ_FIRST(&sc->ready_scb);
354 start:
355 if (scb == NULL || sc->sc_nexus != NULL)
356 return;
357 #if 0
358 if (sc->sc_targets[scb->target] & (1 << scb->lun))
359 goto next;
360 #endif
361 TAILQ_REMOVE(&sc->ready_scb, scb, chain);
363 #ifdef SPIFI_DEBUG
365 int i;
367 printf("spifi_sched: ID:LUN = %d:%d, ", scb->target, scb->lun);
368 printf("cmd = 0x%x", scb->cmd.opcode);
369 for (i = 0; i < 5; i++)
370 printf(" 0x%x", scb->cmd.bytes[i]);
371 printf("\n");
373 #endif
375 DMAC3_SLOWACCESS(sc);
376 sc->sc_nexus = scb;
377 spifi_select(sc);
378 DMAC3_FASTACCESS(sc);
380 scb = scb->chain.tqe_next;
381 goto start;
384 static inline int
385 spifi_read_count(struct spifi_reg *reg)
387 int count;
389 count = (reg->count_hi & 0xff) << 16 |
390 (reg->count_mid & 0xff) << 8 |
391 (reg->count_low & 0xff);
392 return count;
395 static inline void
396 spifi_write_count(struct spifi_reg *reg, int count)
399 reg->count_hi = count >> 16;
400 reg->count_mid = count >> 8;
401 reg->count_low = count;
405 #ifdef SPIFI_DEBUG
406 static const char scsi_phase_name[][8] = {
407 "DATAOUT", "DATAIN", "COMMAND", "STATUS",
408 "", "", "MSGOUT", "MSGIN"
410 #endif
413 spifi_intr(void *v)
415 struct spifi_softc *sc = v;
416 struct spifi_reg *reg = sc->sc_reg;
417 int intr, state, icond;
418 struct spifi_scb *scb;
419 struct scsipi_xfer *xs;
420 #ifdef SPIFI_DEBUG
421 char bitmask[64];
422 #endif
424 switch (dmac3_intr(sc->sc_dma)) {
425 case 0:
426 DPRINTF("spurious DMA intr\n");
427 return 0;
428 case -1:
429 printf("DMAC parity error, data PAD\n");
431 DMAC3_SLOWACCESS(sc);
432 reg->prcmd = PRC_TRPAD;
433 DMAC3_FASTACCESS(sc);
434 return 1;
436 default:
437 break;
439 DMAC3_SLOWACCESS(sc);
441 intr = reg->intr & 0xff;
442 if (intr == 0) {
443 DMAC3_FASTACCESS(sc);
444 DPRINTF("spurious intr (not me)\n");
445 return 0;
448 scb = sc->sc_nexus;
449 xs = scb->xs;
450 state = reg->spstat;
451 icond = reg->icond;
453 /* clear interrupt */
454 reg->intr = ~intr;
456 #ifdef SPIFI_DEBUG
457 snprintb(bitmask, sizeof bitmask, INTR_BITMASK, intr);
458 printf("spifi_intr intr = 0x%s (%s), ", bitmask,
459 scsi_phase_name[(reg->prstat >> 3) & 7]);
460 printf("state = 0x%x, icond = 0x%x\n", state, icond);
461 #endif
463 if (intr & INTR_FCOMP) {
464 spifi_fifo_drain(sc);
465 scb->status = reg->cmbuf[scb->target].status;
466 scb->resid = spifi_read_count(reg);
468 DPRINTF("datalen = %d, resid = %d, status = 0x%x\n",
469 xs->datalen, scb->resid, scb->status);
470 DPRINTF("msg = 0x%x\n", reg->cmbuf[sc->sc_id].cdb[0]);
472 DMAC3_FASTACCESS(sc);
473 spifi_done(sc);
474 return 1;
476 if (intr & INTR_DISCON)
477 panic("%s: disconnect", __func__);
479 if (intr & INTR_TIMEO) {
480 xs->error = XS_SELTIMEOUT;
481 DMAC3_FASTACCESS(sc);
482 spifi_done(sc);
483 return 1;
485 if (intr & INTR_BSRQ) {
486 if (scb == NULL)
487 panic("%s: reconnect?", __func__);
489 if (intr & INTR_PERR) {
490 printf("%s: %d:%d parity error\n",
491 device_xname(sc->sc_dev),
492 scb->target, scb->lun);
494 /* XXX reset */
495 xs->error = XS_DRIVER_STUFFUP;
496 spifi_done(sc);
497 return 1;
500 if (state >> 4 == SPS_MSGIN && icond == ICOND_NXTREQ)
501 panic("%s: NXTREQ", __func__);
502 if (reg->fifoctrl & FIFOC_RQOVRN)
503 panic("%s: RQOVRN", __func__);
504 if (icond == ICOND_UXPHASEZ)
505 panic("ICOND_UXPHASEZ");
507 if ((icond & 0x0f) == ICOND_ADATAOFF) {
508 spifi_data_io(sc);
509 goto done;
511 if ((icond & 0xf0) == ICOND_UBF) {
512 reg->exstat = reg->exstat & ~EXS_UBF;
513 spifi_pmatch(sc);
514 goto done;
518 * XXX Work around the SPIFI bug that interrupts during
519 * XXX dataout phase.
521 if (state == ((SPS_DATAOUT << 4) | SPS_INTR) &&
522 (reg->prstat & PRS_PHASE) == SPIFI_DATAOUT) {
523 reg->prcmd = PRC_DATAOUT;
524 goto done;
526 if ((reg->prstat & PRS_Z) == 0) {
527 spifi_pmatch(sc);
528 goto done;
531 panic("%s: unknown intr state", __func__);
534 done:
535 DMAC3_FASTACCESS(sc);
536 return 1;
539 void
540 spifi_pmatch(struct spifi_softc *sc)
542 struct spifi_reg *reg = sc->sc_reg;
543 int phase;
545 phase = (reg->prstat & PRS_PHASE);
547 #ifdef SPIFI_DEBUG
548 printf("%s (%s)\n", __func__, scsi_phase_name[phase >> 3]);
549 #endif
551 switch (phase) {
553 case SPIFI_COMMAND:
554 spifi_command(sc);
555 break;
556 case SPIFI_DATAIN:
557 case SPIFI_DATAOUT:
558 spifi_data_io(sc);
559 break;
560 case SPIFI_STATUS:
561 spifi_status(sc);
562 break;
564 case SPIFI_MSGIN: /* XXX */
565 case SPIFI_MSGOUT: /* XXX */
566 default:
567 printf("spifi: unknown phase %d\n", phase);
571 void
572 spifi_select(struct spifi_softc *sc)
574 struct spifi_reg *reg = sc->sc_reg;
575 struct spifi_scb *scb = sc->sc_nexus;
576 int sel;
578 #if 0
579 if (reg->loopdata || reg->intr)
580 return;
581 #endif
583 if (scb == NULL) {
584 printf("%s: spifi_select: NULL nexus\n",
585 device_xname(sc->sc_dev));
586 return;
589 reg->exctrl = EXC_IPLOCK;
591 dmac3_reset(sc->sc_dma);
592 sel = scb->target << 4 | SEL_ISTART | SEL_IRESELEN | SEL_WATN;
593 spifi_sendmsg(sc, SEND_IDENTIFY);
594 reg->select = sel;
597 void
598 spifi_sendmsg(struct spifi_softc *sc, int msg)
600 struct spifi_scb *scb = sc->sc_nexus;
601 /* struct mesh_tinfo *ti; */
602 int lun, len, i;
604 int id = sc->sc_id;
605 struct spifi_reg *reg = sc->sc_reg;
607 DPRINTF("%s: sending", __func__);
608 sc->sc_msgout = msg;
609 len = 0;
611 if (msg & SEND_REJECT) {
612 DPRINTF(" REJECT");
613 sc->sc_omsg[len++] = MSG_MESSAGE_REJECT;
615 if (msg & SEND_IDENTIFY) {
616 DPRINTF(" IDENTIFY");
617 lun = scb->xs->xs_periph->periph_lun;
618 sc->sc_omsg[len++] = MSG_IDENTIFY(lun, 0);
620 if (msg & SEND_SDTR) {
621 DPRINTF(" SDTR");
622 #if 0
623 ti = &sc->sc_tinfo[scb->target];
624 sc->sc_omsg[len++] = MSG_EXTENDED;
625 sc->sc_omsg[len++] = 3;
626 sc->sc_omsg[len++] = MSG_EXT_SDTR;
627 sc->sc_omsg[len++] = ti->period;
628 sc->sc_omsg[len++] = ti->offset;
629 #endif
631 DPRINTF("\n");
633 reg->cmlen = CML_AMSG_EN | len;
634 for (i = 0; i < len; i++)
635 reg->cmbuf[id].cdb[i] = sc->sc_omsg[i];
638 void
639 spifi_command(struct spifi_softc *sc)
641 struct spifi_scb *scb = sc->sc_nexus;
642 struct spifi_reg *reg = sc->sc_reg;
643 int len = scb->cmdlen;
644 uint8_t *cmdp = (uint8_t *)&scb->cmd;
645 int i;
647 DPRINTF("%s\n", __func__);
649 reg->cmdpage = scb->lun_targ;
651 if (reg->init_status & IST_ACK) {
652 /* Negate ACK. */
653 reg->prcmd = PRC_NJMP | PRC_CLRACK | PRC_COMMAND;
654 reg->prcmd = PRC_NJMP | PRC_COMMAND;
657 reg->cmlen = CML_AMSG_EN | len;
659 for (i = 0; i < len; i++)
660 reg->cmbuf[sc->sc_id].cdb[i] = *cmdp++;
662 reg->prcmd = PRC_COMMAND;
665 void
666 spifi_data_io(struct spifi_softc *sc)
668 struct spifi_scb *scb = sc->sc_nexus;
669 struct spifi_reg *reg = sc->sc_reg;
670 int phase;
672 DPRINTF("%s\n", __func__);
674 phase = reg->prstat & PRS_PHASE;
675 dmac3_reset(sc->sc_dma);
677 spifi_write_count(reg, scb->resid);
678 reg->cmlen = CML_AMSG_EN | 1;
679 reg->data_xfer = 0;
681 scb->flags |= SPIFI_DMA;
682 if (phase == SPIFI_DATAIN) {
683 if (reg->fifoctrl & FIFOC_SSTKACT) {
685 * Clear FIFO and load the contents of synchronous
686 * stack into the FIFO.
688 reg->fifoctrl = FIFOC_CLREVEN;
689 reg->fifoctrl = FIFOC_LOAD;
691 reg->autodata = ADATA_IN | scb->lun_targ;
692 dmac3_start(sc->sc_dma, scb->daddr, scb->resid, DMAC3_CSR_RECV);
693 reg->prcmd = PRC_DATAIN;
694 } else {
695 reg->fifoctrl = FIFOC_CLREVEN;
696 reg->autodata = scb->lun_targ;
697 dmac3_start(sc->sc_dma, scb->daddr, scb->resid, DMAC3_CSR_SEND);
698 reg->prcmd = PRC_DATAOUT;
702 void
703 spifi_status(struct spifi_softc *sc)
705 struct spifi_reg *reg = sc->sc_reg;
707 DPRINTF("%s\n", __func__);
708 spifi_fifo_drain(sc);
709 reg->cmlen = CML_AMSG_EN | 1;
710 reg->prcmd = PRC_STATUS;
714 spifi_done(struct spifi_softc *sc)
716 struct spifi_scb *scb = sc->sc_nexus;
717 struct scsipi_xfer *xs = scb->xs;
719 DPRINTF("%s\n", __func__);
721 xs->status = scb->status;
722 if (xs->status == SCSI_CHECK) {
723 DPRINTF("%s: CHECK CONDITION\n", __func__);
724 if (xs->error == XS_NOERROR)
725 xs->error = XS_BUSY;
728 xs->resid = scb->resid;
730 scsipi_done(xs);
731 spifi_free_scb(sc, scb);
733 sc->sc_nexus = NULL;
734 spifi_sched(sc);
736 return false;
739 void
740 spifi_fifo_drain(struct spifi_softc *sc)
742 struct spifi_scb *scb = sc->sc_nexus;
743 struct spifi_reg *reg = sc->sc_reg;
744 int fifoctrl, fifo_count;
746 DPRINTF("%s\n", __func__);
748 if ((scb->flags & SPIFI_READ) == 0)
749 return;
751 fifoctrl = reg->fifoctrl;
752 if (fifoctrl & FIFOC_SSTKACT)
753 return;
755 fifo_count = 8 - (fifoctrl & FIFOC_FSLOT);
756 if (fifo_count > 0 && (scb->flags & SPIFI_DMA)) {
757 /* Flush data still in FIFO. */
758 reg->fifoctrl = FIFOC_FLUSH;
759 return;
762 reg->fifoctrl = FIFOC_CLREVEN;
765 void
766 spifi_reset(struct spifi_softc *sc)
768 struct spifi_reg *reg = sc->sc_reg;
769 int id = sc->sc_id;
771 DPRINTF("%s\n", __func__);
773 reg->auxctrl = AUXCTRL_SRST;
774 reg->auxctrl = AUXCTRL_CRST;
776 dmac3_reset(sc->sc_dma);
778 reg->auxctrl = AUXCTRL_SRST;
779 reg->auxctrl = AUXCTRL_CRST;
780 reg->auxctrl = AUXCTRL_DMAEDGE;
782 /* Mask (only) target mode interrupts. */
783 reg->imask = INTR_TGSEL | INTR_COMRECV;
785 reg->config = CONFIG_DMABURST | CONFIG_PCHKEN | CONFIG_PGENEN | id;
786 reg->fastwide = FAST_FASTEN;
787 reg->prctrl = 0;
788 reg->loopctrl = 0;
790 /* Enable automatic status input except the initiator. */
791 reg->autostat = ~(1 << id);
793 reg->fifoctrl = FIFOC_CLREVEN;
794 spifi_write_count(reg, 0);
796 /* Flush write buffer. */
797 (void)reg->spstat;
800 void
801 spifi_bus_reset(struct spifi_softc *sc)
803 struct spifi_reg *reg = sc->sc_reg;
805 printf("%s: bus reset\n", device_xname(sc->sc_dev));
807 sc->sc_nexus = NULL;
809 reg->auxctrl = AUXCTRL_SETRST;
810 delay(100);
811 reg->auxctrl = 0;
814 #if 0
815 static uint8_t spifi_sync_period[] = {
816 /* 0 1 2 3 4 5 6 7 8 9 10 11 */
817 137, 125, 112, 100, 87, 75, 62, 50, 43, 37, 31, 25
820 void
821 spifi_setsync(struct spifi_softc *sc, struct spifi_tinfo *ti)
824 if ((ti->flags & T_SYNCMODE) == 0)
825 reg->data_xfer = 0;
826 else {
827 uint8_t period = ti->period;
828 uint8_t offset = ti->offset;
829 int v;
831 for (v = sizeof(spifi_sync_period) - 1; v >= 0; v--)
832 if (spifi_sync_period[v] >= period)
833 break;
834 if (v == -1)
835 reg->data_xfer = 0; /* XXX */
836 else
837 reg->data_xfer = v << 4 | offset;
840 #endif