Home | History | Annotate | Line # | Download | only in dev
spc.c revision 1.4
      1  1.4  tsutsui /* $NetBSD: spc.c,v 1.4 2005/01/02 12:03:13 tsutsui Exp $ */
      2  1.1  tsutsui 
      3  1.1  tsutsui /*
      4  1.1  tsutsui  * Copyright (c) 2003 Izumi Tsutsui.
      5  1.1  tsutsui  * All rights reserved.
      6  1.1  tsutsui  *
      7  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
      8  1.1  tsutsui  * modification, are permitted provided that the following conditions
      9  1.1  tsutsui  * are met:
     10  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     12  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     15  1.1  tsutsui  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  tsutsui  *    derived from this software without specific prior written permission.
     17  1.1  tsutsui  *
     18  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  tsutsui  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  tsutsui  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  tsutsui  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1  tsutsui  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1  tsutsui  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1  tsutsui  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1  tsutsui  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1  tsutsui  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  tsutsui  */
     29  1.1  tsutsui 
     30  1.1  tsutsui #include "opt_ddb.h"
     31  1.1  tsutsui 
     32  1.1  tsutsui #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     33  1.1  tsutsui 
     34  1.4  tsutsui __KERNEL_RCSID(0, "$NetBSD: spc.c,v 1.4 2005/01/02 12:03:13 tsutsui Exp $");
     35  1.1  tsutsui 
     36  1.1  tsutsui #include <sys/param.h>
     37  1.1  tsutsui #include <sys/systm.h>
     38  1.1  tsutsui #include <sys/device.h>
     39  1.1  tsutsui 
     40  1.1  tsutsui #include <machine/autoconf.h>
     41  1.1  tsutsui #include <machine/bus.h>
     42  1.1  tsutsui #include <machine/cpu.h>
     43  1.1  tsutsui #include <machine/intr.h>
     44  1.1  tsutsui 
     45  1.1  tsutsui #include <hp300/dev/dioreg.h>
     46  1.1  tsutsui #include <hp300/dev/diovar.h>
     47  1.1  tsutsui #include <hp300/dev/diodevs.h>
     48  1.1  tsutsui 
     49  1.1  tsutsui #include <dev/scsipi/scsi_all.h>
     50  1.1  tsutsui #include <dev/scsipi/scsipi_all.h>
     51  1.2  tsutsui #include <dev/scsipi/scsi_message.h>
     52  1.1  tsutsui #include <dev/scsipi/scsiconf.h>
     53  1.1  tsutsui 
     54  1.1  tsutsui #include <dev/ic/mb89352reg.h>
     55  1.1  tsutsui #include <dev/ic/mb89352var.h>
     56  1.1  tsutsui 
     57  1.1  tsutsui #include <hp300/dev/hp98265reg.h>
     58  1.1  tsutsui #include <hp300/dev/dmareg.h>
     59  1.1  tsutsui #include <hp300/dev/dmavar.h>
     60  1.1  tsutsui 
     61  1.3  thorpej static int	spc_dio_match(struct device *, struct cfdata *, void *);
     62  1.3  thorpej static void	spc_dio_attach(struct device *, struct device *, void *);
     63  1.3  thorpej static void	spc_dio_dmastart(struct spc_softc *, void *, size_t, int);
     64  1.3  thorpej static void	spc_dio_dmadone(struct spc_softc *);
     65  1.3  thorpej static void	spc_dio_dmago(void *);
     66  1.3  thorpej static void	spc_dio_dmastop(void *);
     67  1.1  tsutsui 
     68  1.1  tsutsui struct spc_dio_softc {
     69  1.1  tsutsui 	struct spc_softc sc_spc;	/* MI spc softc */
     70  1.1  tsutsui 
     71  1.1  tsutsui 	/* DIO specific goo. */
     72  1.1  tsutsui 	struct bus_space_tag sc_tag;	/* bus space tag with oddbyte func */
     73  1.1  tsutsui 	bus_space_handle_t sc_iohsc;	/* bus space handle for HPSCSI */
     74  1.1  tsutsui 	struct dmaqueue sc_dq;		/* DMA job queue */
     75  1.1  tsutsui 	u_int sc_dflags;		/* DMA flags */
     76  1.1  tsutsui #define SCSI_DMA32	0x01		/* 32-bit DMA should be used */
     77  1.1  tsutsui #define SCSI_HAVEDMA	0x02		/* controller has DMA channel */
     78  1.1  tsutsui #define SCSI_DATAIN	0x04		/* DMA direction */
     79  1.1  tsutsui };
     80  1.1  tsutsui 
     81  1.1  tsutsui CFATTACH_DECL(spc, sizeof(struct spc_dio_softc),
     82  1.1  tsutsui     spc_dio_match, spc_dio_attach, NULL, NULL);
     83  1.1  tsutsui 
     84  1.1  tsutsui static int
     85  1.3  thorpej spc_dio_match(struct device *parent, struct cfdata *cf, void *aux)
     86  1.1  tsutsui {
     87  1.1  tsutsui 	struct dio_attach_args *da = aux;
     88  1.1  tsutsui 
     89  1.1  tsutsui 	switch (da->da_id) {
     90  1.1  tsutsui 	case DIO_DEVICE_ID_SCSI0:
     91  1.1  tsutsui 	case DIO_DEVICE_ID_SCSI1:
     92  1.1  tsutsui 	case DIO_DEVICE_ID_SCSI2:
     93  1.1  tsutsui 	case DIO_DEVICE_ID_SCSI3:
     94  1.1  tsutsui 		return 1;
     95  1.1  tsutsui 	}
     96  1.1  tsutsui 
     97  1.1  tsutsui 	return 0;
     98  1.1  tsutsui }
     99  1.1  tsutsui 
    100  1.1  tsutsui static void
    101  1.3  thorpej spc_dio_attach(struct device *parent, struct device *self, void *aux)
    102  1.1  tsutsui {
    103  1.1  tsutsui 	struct spc_dio_softc *dsc = (struct spc_dio_softc *)self;
    104  1.1  tsutsui 	struct spc_softc *sc = &dsc->sc_spc;
    105  1.1  tsutsui 	struct dio_attach_args *da = aux;
    106  1.1  tsutsui 	bus_space_tag_t iot = &dsc->sc_tag;
    107  1.1  tsutsui 	bus_space_handle_t iohsc, iohspc;
    108  1.4  tsutsui 	uint8_t id;
    109  1.1  tsutsui 
    110  1.1  tsutsui 	memcpy(iot, da->da_bst, sizeof(struct bus_space_tag));
    111  1.1  tsutsui 	dio_set_bus_space_oddbyte(iot);
    112  1.1  tsutsui 
    113  1.1  tsutsui 	if (bus_space_map(iot, da->da_addr, da->da_size, 0, &iohsc)) {
    114  1.1  tsutsui 		printf(": can't map SCSI registers\n");
    115  1.1  tsutsui 		return;
    116  1.1  tsutsui 	}
    117  1.1  tsutsui 
    118  1.1  tsutsui 	if (bus_space_subregion(iot, iohsc, SPC_OFFSET, SPC_SIZE, &iohspc)) {
    119  1.1  tsutsui 		printf(": can't map SPC registers\n");
    120  1.1  tsutsui 		return;
    121  1.1  tsutsui 	}
    122  1.1  tsutsui 
    123  1.1  tsutsui 	printf(": 98265A SCSI");
    124  1.1  tsutsui 
    125  1.1  tsutsui 	bus_space_write_1(iot, iohsc, HPSCSI_ID, 0xff);
    126  1.1  tsutsui 	DELAY(100);
    127  1.1  tsutsui 	id = bus_space_read_1(iot, iohsc, HPSCSI_ID);
    128  1.1  tsutsui 	if ((id & ID_WORD_DMA) == 0) {
    129  1.1  tsutsui 		printf(", 32-bit DMA");
    130  1.1  tsutsui 		dsc->sc_dflags |= SCSI_DMA32;
    131  1.1  tsutsui 	}
    132  1.1  tsutsui 	id &= ID_MASK;
    133  1.1  tsutsui 	printf(", SCSI ID %d\n", id);
    134  1.1  tsutsui 
    135  1.1  tsutsui 	sc->sc_iot = iot;
    136  1.1  tsutsui 	sc->sc_ioh = iohspc;
    137  1.1  tsutsui 	sc->sc_initiator = id;
    138  1.1  tsutsui 
    139  1.1  tsutsui 	sc->sc_dma_start = spc_dio_dmastart;
    140  1.1  tsutsui 	sc->sc_dma_done  = spc_dio_dmadone;
    141  1.1  tsutsui 
    142  1.1  tsutsui 	dsc->sc_iohsc = iohsc;
    143  1.1  tsutsui 	dsc->sc_dq.dq_softc = dsc;
    144  1.1  tsutsui 	dsc->sc_dq.dq_start = spc_dio_dmago;
    145  1.1  tsutsui 	dsc->sc_dq.dq_done  = spc_dio_dmastop;
    146  1.1  tsutsui 
    147  1.1  tsutsui 	bus_space_write_1(iot, iohsc, HPSCSI_CSR, 0x00);
    148  1.1  tsutsui 	bus_space_write_1(iot, iohsc, HPSCSI_HCONF, 0x00);
    149  1.1  tsutsui 
    150  1.1  tsutsui 	dio_intr_establish(spc_intr, (void *)sc, da->da_ipl, IPL_BIO);
    151  1.1  tsutsui 
    152  1.1  tsutsui 	spc_attach(sc);
    153  1.1  tsutsui 
    154  1.1  tsutsui 	/* Enable SPC interrupts. */
    155  1.1  tsutsui 	bus_space_write_1(iot, iohsc, HPSCSI_CSR, CSR_IE);
    156  1.1  tsutsui }
    157  1.1  tsutsui 
    158  1.3  thorpej static void
    159  1.3  thorpej spc_dio_dmastart(struct spc_softc *sc, void *addr, size_t size, int datain)
    160  1.1  tsutsui {
    161  1.1  tsutsui 	struct spc_dio_softc *dsc = (struct spc_dio_softc *)sc;
    162  1.1  tsutsui 
    163  1.1  tsutsui 	dsc->sc_dq.dq_chan = DMA0 | DMA1;
    164  1.1  tsutsui 	dsc->sc_dflags |= SCSI_HAVEDMA;
    165  1.1  tsutsui 	if (datain)
    166  1.1  tsutsui 		dsc->sc_dflags |= SCSI_DATAIN;
    167  1.1  tsutsui 	else
    168  1.1  tsutsui 		dsc->sc_dflags &= ~SCSI_DATAIN;
    169  1.1  tsutsui 
    170  1.1  tsutsui 	if (dmareq(&dsc->sc_dq) != 0)
    171  1.1  tsutsui 		/* DMA channel is available, so start DMA immediately */
    172  1.1  tsutsui 		spc_dio_dmago((void *)dsc);
    173  1.1  tsutsui 	/* else dma start function will be called later from dmafree(). */
    174  1.1  tsutsui }
    175  1.1  tsutsui 
    176  1.3  thorpej static void
    177  1.3  thorpej spc_dio_dmago(void *arg)
    178  1.1  tsutsui {
    179  1.1  tsutsui 	struct spc_dio_softc *dsc = (struct spc_dio_softc *)arg;
    180  1.1  tsutsui 	struct spc_softc *sc = &dsc->sc_spc;
    181  1.1  tsutsui 	bus_space_tag_t iot;
    182  1.1  tsutsui 	bus_space_handle_t iohsc, iohspc;
    183  1.1  tsutsui 	int len, chan;
    184  1.4  tsutsui 	uint32_t dmaflags;
    185  1.4  tsutsui 	uint8_t cmd;
    186  1.1  tsutsui 
    187  1.1  tsutsui 	iot = sc->sc_iot;
    188  1.1  tsutsui 	iohspc = sc->sc_ioh;
    189  1.1  tsutsui 	iohsc = dsc->sc_iohsc;
    190  1.1  tsutsui 
    191  1.1  tsutsui 	bus_space_write_1(iot, iohsc, HPSCSI_HCONF, 0);
    192  1.1  tsutsui 
    193  1.1  tsutsui 	cmd = CSR_IE;
    194  1.1  tsutsui 	dmaflags = DMAGO_NOINT;
    195  1.1  tsutsui 	chan = dsc->sc_dq.dq_chan;
    196  1.1  tsutsui 	if ((dsc->sc_dflags & SCSI_DATAIN) != 0) {
    197  1.1  tsutsui 		cmd |= CSR_DMAIN;
    198  1.1  tsutsui 		dmaflags |= DMAGO_READ;
    199  1.1  tsutsui 	}
    200  1.1  tsutsui 	if ((dsc->sc_dflags & SCSI_DMA32) != 0 &&
    201  1.1  tsutsui 	    ((u_int)sc->sc_dp & 3) == 0 &&
    202  1.1  tsutsui 	    (sc->sc_dleft & 3) == 0) {
    203  1.1  tsutsui 		cmd |= CSR_DMA32;
    204  1.1  tsutsui 		dmaflags |= DMAGO_LWORD;
    205  1.1  tsutsui 	} else
    206  1.1  tsutsui 		dmaflags |= DMAGO_WORD;
    207  1.1  tsutsui 
    208  1.1  tsutsui 	dmago(chan, sc->sc_dp, sc->sc_dleft, dmaflags);
    209  1.1  tsutsui 
    210  1.1  tsutsui 	bus_space_write_1(iot, iohsc, HPSCSI_CSR, cmd);
    211  1.1  tsutsui 	cmd |= (chan == 0) ? CSR_DE0 : CSR_DE1;
    212  1.1  tsutsui 	bus_space_write_1(iot, iohsc, HPSCSI_CSR, cmd);
    213  1.1  tsutsui 
    214  1.1  tsutsui 	cmd = SCMD_XFR;
    215  1.1  tsutsui 	len = sc->sc_dleft;
    216  1.1  tsutsui 
    217  1.1  tsutsui 	if ((len & (DEV_BSIZE - 1)) != 0) /* XXX ??? */ {
    218  1.1  tsutsui 		cmd |= SCMD_PAD;
    219  1.1  tsutsui #if 0
    220  1.1  tsutsui 		if ((dsc->sc_dflags & SCSI_DATAIN) != 0)
    221  1.1  tsutsui 			len += 2; /* XXX ??? */
    222  1.1  tsutsui #endif
    223  1.1  tsutsui 	}
    224  1.1  tsutsui 
    225  1.1  tsutsui 	bus_space_write_1(iot, iohspc, TCH, len >> 16);
    226  1.1  tsutsui 	bus_space_write_1(iot, iohspc, TCM, len >>  8);
    227  1.1  tsutsui 	bus_space_write_1(iot, iohspc, TCL, len);
    228  1.1  tsutsui 	bus_space_write_1(iot, iohspc, PCTL, sc->sc_phase | PCTL_BFINT_ENAB);
    229  1.1  tsutsui 	bus_space_write_1(iot, iohspc, SCMD, cmd);
    230  1.1  tsutsui 
    231  1.1  tsutsui 	sc->sc_flags |= SPC_DOINGDMA;
    232  1.1  tsutsui }
    233  1.1  tsutsui 
    234  1.3  thorpej static void
    235  1.3  thorpej spc_dio_dmadone(struct spc_softc *sc)
    236  1.1  tsutsui {
    237  1.1  tsutsui 	struct spc_dio_softc *dsc = (struct spc_dio_softc *)sc;
    238  1.1  tsutsui 	bus_space_tag_t iot;
    239  1.1  tsutsui 	bus_space_handle_t ioh, iohsc;
    240  1.1  tsutsui 	int resid, trans;
    241  1.4  tsutsui 	uint8_t cmd;
    242  1.1  tsutsui 
    243  1.1  tsutsui 	iot = sc->sc_iot;
    244  1.1  tsutsui 	ioh = sc->sc_ioh;
    245  1.1  tsutsui 	iohsc = dsc->sc_iohsc;
    246  1.1  tsutsui 
    247  1.1  tsutsui 	/* wait DMA complete */
    248  1.1  tsutsui 	if ((bus_space_read_1(iot, ioh, SSTS) & SSTS_BUSY) != 0) {
    249  1.1  tsutsui 		int timeout = 1000; /* XXX how long? */
    250  1.1  tsutsui 		while ((bus_space_read_1(iot, ioh, SSTS) & SSTS_BUSY) != 0) {
    251  1.1  tsutsui 			if (--timeout < 0)
    252  1.1  tsutsui 				printf("%s: DMA complete timeout\n",
    253  1.1  tsutsui 				    sc->sc_dev.dv_xname);
    254  1.1  tsutsui 			DELAY(1);
    255  1.1  tsutsui 		}
    256  1.1  tsutsui 	}
    257  1.1  tsutsui 
    258  1.1  tsutsui 	if ((dsc->sc_dflags & SCSI_HAVEDMA) != 0) {
    259  1.1  tsutsui 		dmafree(&dsc->sc_dq);
    260  1.1  tsutsui 		dsc->sc_dflags &= ~SCSI_HAVEDMA;
    261  1.1  tsutsui 	}
    262  1.1  tsutsui 
    263  1.1  tsutsui 	cmd = bus_space_read_1(iot, iohsc, HPSCSI_CSR);
    264  1.1  tsutsui 	cmd &= ~(CSR_DE1|CSR_DE0);
    265  1.1  tsutsui 	bus_space_write_1(iot, iohsc, HPSCSI_CSR, cmd);
    266  1.1  tsutsui 
    267  1.1  tsutsui 	resid = bus_space_read_1(iot, ioh, TCH) << 16 |
    268  1.1  tsutsui 	    bus_space_read_1(iot, ioh, TCM) << 8 |
    269  1.1  tsutsui 	    bus_space_read_1(iot, ioh, TCL);
    270  1.1  tsutsui 	trans = sc->sc_dleft - resid;
    271  1.1  tsutsui 	sc->sc_dp += trans;
    272  1.1  tsutsui 	sc->sc_dleft -= trans;
    273  1.1  tsutsui 
    274  1.1  tsutsui 	sc->sc_flags &= ~SPC_DOINGDMA;
    275  1.1  tsutsui }
    276  1.1  tsutsui 
    277  1.3  thorpej static void
    278  1.3  thorpej spc_dio_dmastop(void *arg)
    279  1.1  tsutsui {
    280  1.1  tsutsui 	struct spc_dio_softc *dsc = (struct spc_dio_softc *)arg;
    281  1.1  tsutsui 	struct spc_softc *sc = &dsc->sc_spc;
    282  1.4  tsutsui 	uint8_t cmd;
    283  1.1  tsutsui 
    284  1.1  tsutsui 	/* XXX When is this function called? */
    285  1.1  tsutsui 	cmd = bus_space_read_1(sc->sc_iot, dsc->sc_iohsc, HPSCSI_CSR);
    286  1.1  tsutsui 	cmd &= ~(CSR_DE1|CSR_DE0);
    287  1.1  tsutsui 	bus_space_write_1(sc->sc_iot, dsc->sc_iohsc, HPSCSI_CSR, cmd);
    288  1.1  tsutsui 
    289  1.1  tsutsui 	dsc->sc_dflags &= ~SCSI_HAVEDMA;
    290  1.1  tsutsui 	sc->sc_flags &= ~SPC_DOINGDMA;
    291  1.1  tsutsui }
    292