1*d1fe7992Stsutsui /* $NetBSD: wdsc.c,v 1.35 2018/09/02 16:18:50 tsutsui Exp $ */
20ac0d0d4Swdk
30ac0d0d4Swdk /*
40ac0d0d4Swdk * Copyright (c) 2001 Wayne Knowles
50ac0d0d4Swdk * All rights reserved.
60ac0d0d4Swdk *
70ac0d0d4Swdk * This code is derived from software contributed to The NetBSD Foundation
80ac0d0d4Swdk * by Wayne Knowles
90ac0d0d4Swdk *
100ac0d0d4Swdk * Redistribution and use in source and binary forms, with or without
110ac0d0d4Swdk * modification, are permitted provided that the following conditions
120ac0d0d4Swdk * are met:
130ac0d0d4Swdk * 1. Redistributions of source code must retain the above copyright
140ac0d0d4Swdk * notice, this list of conditions and the following disclaimer.
150ac0d0d4Swdk * 2. Redistributions in binary form must reproduce the above copyright
160ac0d0d4Swdk * notice, this list of conditions and the following disclaimer in the
170ac0d0d4Swdk * documentation and/or other materials provided with the distribution.
185d1469bdSmartin * 3. All advertising materials mentioning features or use of this software
195d1469bdSmartin * must display the following acknowledgement:
205d1469bdSmartin * This product includes software developed by the NetBSD
215d1469bdSmartin * Foundation, Inc. and its contributors.
225d1469bdSmartin * 4. Neither the name of The NetBSD Foundation nor the names of its
235d1469bdSmartin * contributors may be used to endorse or promote products derived
245d1469bdSmartin * from this software without specific prior written permission.
250ac0d0d4Swdk *
260ac0d0d4Swdk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
270ac0d0d4Swdk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
280ac0d0d4Swdk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
290ac0d0d4Swdk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
300ac0d0d4Swdk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
310ac0d0d4Swdk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
320ac0d0d4Swdk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
330ac0d0d4Swdk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
340ac0d0d4Swdk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
350ac0d0d4Swdk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
360ac0d0d4Swdk * POSSIBILITY OF SUCH DAMAGE.
370ac0d0d4Swdk */
380ac0d0d4Swdk
39ed517291Slukem #include <sys/cdefs.h>
40*d1fe7992Stsutsui __KERNEL_RCSID(0, "$NetBSD: wdsc.c,v 1.35 2018/09/02 16:18:50 tsutsui Exp $");
41ed517291Slukem
420ac0d0d4Swdk #include <sys/param.h>
430ac0d0d4Swdk #include <sys/systm.h>
440ac0d0d4Swdk #include <sys/kernel.h>
450ac0d0d4Swdk #include <sys/device.h>
460ac0d0d4Swdk #include <sys/buf.h>
470ac0d0d4Swdk
480ac0d0d4Swdk #include <dev/scsipi/scsi_all.h>
490ac0d0d4Swdk #include <dev/scsipi/scsipi_all.h>
500ac0d0d4Swdk #include <dev/scsipi/scsiconf.h>
510ac0d0d4Swdk
520ac0d0d4Swdk #include <machine/cpu.h>
53cf10107dSdyoung #include <sys/bus.h>
540ac0d0d4Swdk #include <machine/autoconf.h>
5575aae500Srumble #include <machine/machtype.h>
566db143f7Srumble #include <machine/sysconf.h>
570ac0d0d4Swdk
580ac0d0d4Swdk #include <sgimips/hpc/hpcvar.h>
590ac0d0d4Swdk #include <sgimips/hpc/hpcreg.h>
600ac0d0d4Swdk #include <sgimips/hpc/hpcdma.h>
610ac0d0d4Swdk
622cd3c4f0Sbjh21 #include <dev/ic/wd33c93reg.h>
632cd3c4f0Sbjh21 #include <dev/ic/wd33c93var.h>
640ac0d0d4Swdk
65de81761cSwdk #include <opt_kgdb.h>
66de81761cSwdk #include <sys/kgdb.h>
67de81761cSwdk
680ac0d0d4Swdk struct wdsc_softc {
69741c7a29Sthorpej struct wd33c93_softc sc_wd33c93; /* Must be first */
70de81761cSwdk struct evcnt sc_intrcnt; /* Interrupt counter */
71de81761cSwdk bus_dma_tag_t sc_dmat;
72de81761cSwdk bus_dmamap_t sc_dmamap;
73de81761cSwdk int sc_flags;
74de81761cSwdk #define WDSC_DMA_ACTIVE 0x1
75de81761cSwdk #define WDSC_DMA_MAPLOADED 0x2
760ac0d0d4Swdk struct hpc_dma_softc sc_hpcdma;
770ac0d0d4Swdk };
780ac0d0d4Swdk
790ac0d0d4Swdk
80398fb3fdStsutsui int wdsc_match(device_t, cfdata_t, void *);
8159f7d57cStsutsui void wdsc_attach(device_t, device_t, void *);
820ac0d0d4Swdk
8311aab16bSbjh21 CFATTACH_DECL_NEW(wdsc, sizeof(struct wdsc_softc),
84b96bc0d7Sthorpej wdsc_match, wdsc_attach, NULL, NULL);
850ac0d0d4Swdk
8659f7d57cStsutsui int wdsc_dmasetup(struct wd33c93_softc *, void ** ,size_t *, int, size_t *);
87937db2deSsekiya int wdsc_dmago(struct wd33c93_softc *);
88937db2deSsekiya void wdsc_dmastop(struct wd33c93_softc *);
89937db2deSsekiya void wdsc_reset(struct wd33c93_softc *);
90937db2deSsekiya int wdsc_dmaintr(void *);
91937db2deSsekiya int wdsc_scsiintr(void *);
920ac0d0d4Swdk
930ac0d0d4Swdk /*
946db143f7Srumble * Match for SCSI devices on the onboard and GIO32 adapter WD33C93 chips
950ac0d0d4Swdk */
960ac0d0d4Swdk int
wdsc_match(device_t parent,cfdata_t cf,void * aux)97cbab9cadSchs wdsc_match(device_t parent, cfdata_t cf, void *aux)
980ac0d0d4Swdk {
99cbab9cadSchs struct hpc_attach_args *haa = aux;
1000ac0d0d4Swdk
1016db143f7Srumble if (strcmp(haa->ha_name, cf->cf_name) == 0) {
102290a34a0Smatt vaddr_t reset, asr, reg;
1036db143f7Srumble
1046db143f7Srumble reset = MIPS_PHYS_TO_KSEG1(haa->ha_sh + haa->ha_dmaoff +
1056db143f7Srumble haa->hpc_regs->scsi0_ctl);
1066db143f7Srumble asr = MIPS_PHYS_TO_KSEG1(haa->ha_sh + haa->ha_devoff);
1076db143f7Srumble
108*d1fe7992Stsutsui /* XXX: hpc1 offset due to SGIMIPS_BUS_SPACE_HPC brain damage */
109*d1fe7992Stsutsui asr = (asr + 3) & ~0x3;
110*d1fe7992Stsutsui
1116db143f7Srumble if (platform.badaddr((void *)reset, sizeof(reset)))
11259f7d57cStsutsui return 0;
1136db143f7Srumble
1146db143f7Srumble *(volatile uint32_t *)reset = haa->hpc_regs->scsi_dmactl_reset;
1156db143f7Srumble delay(1000);
1166db143f7Srumble *(volatile uint32_t *)reset = 0x0;
1176db143f7Srumble
1186db143f7Srumble if (platform.badaddr((void *)asr, sizeof(asr)))
11959f7d57cStsutsui return 0;
1206db143f7Srumble
1216db143f7Srumble reg = *(volatile uint32_t *)asr;
1226db143f7Srumble if (haa->hpc_regs->revision == 3) {
1236db143f7Srumble if ((reg & 0xff) == SBIC_ASR_INT)
12459f7d57cStsutsui return 1;
1256db143f7Srumble } else {
1266db143f7Srumble if (((reg >> 8) & 0xff) == SBIC_ASR_INT)
12759f7d57cStsutsui return 1;
1286db143f7Srumble }
1296db143f7Srumble }
130be010c72Sthorpej
13159f7d57cStsutsui return 0;
1320ac0d0d4Swdk }
1330ac0d0d4Swdk
1340ac0d0d4Swdk /*
1350ac0d0d4Swdk * Attach the wdsc driver
1360ac0d0d4Swdk */
1370ac0d0d4Swdk void
wdsc_attach(device_t parent,device_t self,void * aux)138398fb3fdStsutsui wdsc_attach(device_t parent, device_t self, void *aux)
1390ac0d0d4Swdk {
140398fb3fdStsutsui struct wdsc_softc *wsc = device_private(self);
14111aab16bSbjh21 struct wd33c93_softc *sc = &wsc->sc_wd33c93;
142398fb3fdStsutsui struct hpc_attach_args *haa = aux;
1430ac0d0d4Swdk int err;
1440ac0d0d4Swdk
145398fb3fdStsutsui sc->sc_dev = self;
146be010c72Sthorpej sc->sc_regt = haa->ha_st;
147de81761cSwdk wsc->sc_dmat = haa->ha_dmat;
1480ac0d0d4Swdk
149af4ac18eSsekiya wsc->sc_hpcdma.hpc = haa->hpc_regs;
150af4ac18eSsekiya
151be010c72Sthorpej if ((err = bus_space_subregion(haa->ha_st, haa->ha_sh,
152eb488f67Smacallan haa->ha_devoff + 0 + 3, 1, &sc->sc_asr_regh)) != 0) {
1530e95582eSrumble printf(": unable to map asr reg, err=%d\n", err);
1540e95582eSrumble return;
1550e95582eSrumble }
1560e95582eSrumble
1570e95582eSrumble if ((err = bus_space_subregion(haa->ha_st, haa->ha_sh,
158eb488f67Smacallan haa->ha_devoff + 4 + 3, 1, &sc->sc_data_regh)) != 0) {
159eb488f67Smacallan printf(": unable to map data reg, err=%d\n", err);
160de81761cSwdk return;
1610ac0d0d4Swdk }
1620ac0d0d4Swdk
16359f7d57cStsutsui if (bus_dmamap_create(wsc->sc_dmat, MAXPHYS,
164af4ac18eSsekiya wsc->sc_hpcdma.hpc->scsi_dma_segs,
165af4ac18eSsekiya wsc->sc_hpcdma.hpc->scsi_dma_segs_size,
166af4ac18eSsekiya wsc->sc_hpcdma.hpc->scsi_dma_segs_size,
16759f7d57cStsutsui BUS_DMA_WAITOK, &wsc->sc_dmamap) != 0) {
1680ac0d0d4Swdk printf(": failed to create dmamap\n");
169de81761cSwdk return;
1700ac0d0d4Swdk }
1710ac0d0d4Swdk
1720ac0d0d4Swdk sc->sc_dmasetup = wdsc_dmasetup;
1730ac0d0d4Swdk sc->sc_dmago = wdsc_dmago;
1740ac0d0d4Swdk sc->sc_dmastop = wdsc_dmastop;
1758edf2c6dSthorpej sc->sc_reset = wdsc_reset;
1760ac0d0d4Swdk
177741c7a29Sthorpej sc->sc_adapter.adapt_request = wd33c93_scsi_request;
178de81761cSwdk sc->sc_adapter.adapt_minphys = minphys;
1790ac0d0d4Swdk
180af4ac18eSsekiya sc->sc_id = 0; /* Host ID = 0 */
1819b1a76ecSrumble sc->sc_clkfreq = 200; /* 20MHz */
182f8e6f8f8Srumble sc->sc_dmamode = SBIC_CTL_BURST_DMA;
1830ac0d0d4Swdk
184de81761cSwdk evcnt_attach_dynamic(&wsc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
18511aab16bSbjh21 device_xname(sc->sc_dev), "intr");
1860ac0d0d4Swdk
187be010c72Sthorpej if ((cpu_intr_establish(haa->ha_irq, IPL_BIO,
188398fb3fdStsutsui wdsc_scsiintr, wsc)) == NULL) {
1890ac0d0d4Swdk printf(": unable to establish interrupt!\n");
190de81761cSwdk return;
1910ac0d0d4Swdk }
1920ac0d0d4Swdk
193bc577449Ssekiya hpcdma_init(haa, &wsc->sc_hpcdma, wsc->sc_hpcdma.hpc->scsi_dma_segs);
194741c7a29Sthorpej wd33c93_attach(sc);
1950ac0d0d4Swdk }
1960ac0d0d4Swdk
1970ac0d0d4Swdk /*
1980ac0d0d4Swdk * Prime the hardware for a DMA transfer
199de81761cSwdk *
200de81761cSwdk * Requires splbio() interrupts to be disabled by the caller
2010ac0d0d4Swdk */
2020ac0d0d4Swdk int
wdsc_dmasetup(struct wd33c93_softc * sc,void ** addr,size_t * len,int datain,size_t * dmasize)20359f7d57cStsutsui wdsc_dmasetup(struct wd33c93_softc *sc, void **addr, size_t *len, int datain,
20459f7d57cStsutsui size_t *dmasize)
2050ac0d0d4Swdk {
206398fb3fdStsutsui struct wdsc_softc *wsc = (struct wdsc_softc *)sc;
207de81761cSwdk struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
2080ac0d0d4Swdk int count, err;
2090ac0d0d4Swdk void *vaddr;
2100ac0d0d4Swdk
211de81761cSwdk KASSERT((wsc->sc_flags & WDSC_DMA_ACTIVE) == 0);
2120ac0d0d4Swdk
213de81761cSwdk vaddr = *addr;
214de81761cSwdk count = dsc->sc_dlen = *len;
2150ac0d0d4Swdk if (count) {
216de81761cSwdk KASSERT((wsc->sc_flags & WDSC_DMA_MAPLOADED) == 0);
2170ac0d0d4Swdk
218de81761cSwdk /* Build list of physical addresses for this transfer */
219de81761cSwdk if ((err = bus_dmamap_load(wsc->sc_dmat, wsc->sc_dmamap,
22059f7d57cStsutsui vaddr, count, NULL /* kernel address */,
2210ac0d0d4Swdk BUS_DMA_NOWAIT)) != 0)
2220ac0d0d4Swdk panic("%s: bus_dmamap_load err=%d",
223398fb3fdStsutsui device_xname(sc->sc_dev), err);
2240ac0d0d4Swdk
225de81761cSwdk hpcdma_sglist_create(dsc, wsc->sc_dmamap);
226de81761cSwdk wsc->sc_flags |= WDSC_DMA_MAPLOADED;
2270ac0d0d4Swdk
228de81761cSwdk if (datain) {
22975aae500Srumble dsc->sc_dmacmd =
23075aae500Srumble wsc->sc_hpcdma.hpc->scsi_dma_datain_cmd;
231de81761cSwdk dsc->sc_flags |= HPCDMA_READ;
2320ac0d0d4Swdk } else {
23375aae500Srumble dsc->sc_dmacmd =
23475aae500Srumble wsc->sc_hpcdma.hpc->scsi_dma_dataout_cmd;
235de81761cSwdk dsc->sc_flags &= ~HPCDMA_READ;
2360ac0d0d4Swdk }
2370ac0d0d4Swdk }
23859f7d57cStsutsui return count;
2390ac0d0d4Swdk }
2400ac0d0d4Swdk
2410ac0d0d4Swdk /*
2420ac0d0d4Swdk * Prime the hardware for the next DMA transfer
2430ac0d0d4Swdk */
2440ac0d0d4Swdk int
wdsc_dmago(struct wd33c93_softc * sc)245398fb3fdStsutsui wdsc_dmago(struct wd33c93_softc *sc)
2460ac0d0d4Swdk {
247398fb3fdStsutsui struct wdsc_softc *wsc = (struct wdsc_softc *)sc;
248de81761cSwdk struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
2490ac0d0d4Swdk
250de81761cSwdk if (dsc->sc_dlen == 0)
25159f7d57cStsutsui return 0;
2520ac0d0d4Swdk
253de81761cSwdk KASSERT((wsc->sc_flags & WDSC_DMA_ACTIVE) == 0);
254de81761cSwdk KASSERT((wsc->sc_flags & WDSC_DMA_MAPLOADED));
2550ac0d0d4Swdk
256de81761cSwdk wsc->sc_flags |= WDSC_DMA_ACTIVE;
257de81761cSwdk
25859f7d57cStsutsui bus_dmamap_sync(wsc->sc_dmat, wsc->sc_dmamap,
25959f7d57cStsutsui 0, wsc->sc_dmamap->dm_mapsize,
260984da15cStsutsui (dsc->sc_flags & HPCDMA_READ) ?
261984da15cStsutsui BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
2620ac0d0d4Swdk
2630ac0d0d4Swdk hpcdma_cntl(dsc, dsc->sc_dmacmd); /* Thunderbirds are go! */
2640ac0d0d4Swdk
26559f7d57cStsutsui return wsc->sc_dmamap->dm_mapsize;
2660ac0d0d4Swdk }
2670ac0d0d4Swdk
2680ac0d0d4Swdk /*
269de81761cSwdk * Stop DMA and unload active DMA maps
2700ac0d0d4Swdk */
2710ac0d0d4Swdk void
wdsc_dmastop(struct wd33c93_softc * sc)272398fb3fdStsutsui wdsc_dmastop(struct wd33c93_softc *sc)
2730ac0d0d4Swdk {
274398fb3fdStsutsui struct wdsc_softc *wsc = (struct wdsc_softc *)sc;
275de81761cSwdk struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
2760ac0d0d4Swdk
277de81761cSwdk if (wsc->sc_flags & WDSC_DMA_ACTIVE) {
278de81761cSwdk if (dsc->sc_flags & HPCDMA_READ)
2790ac0d0d4Swdk hpcdma_flush(dsc);
2800ac0d0d4Swdk hpcdma_cntl(dsc, 0); /* Stop DMA */
28159f7d57cStsutsui bus_dmamap_sync(wsc->sc_dmat, wsc->sc_dmamap,
28259f7d57cStsutsui 0, wsc->sc_dmamap->dm_mapsize,
283984da15cStsutsui (dsc->sc_flags & HPCDMA_READ) ?
284984da15cStsutsui BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
2850ac0d0d4Swdk }
286de81761cSwdk if (wsc->sc_flags & WDSC_DMA_MAPLOADED)
287de81761cSwdk bus_dmamap_unload(wsc->sc_dmat, wsc->sc_dmamap);
288de81761cSwdk wsc->sc_flags &= ~(WDSC_DMA_ACTIVE | WDSC_DMA_MAPLOADED);
2890ac0d0d4Swdk }
2900ac0d0d4Swdk
2910ac0d0d4Swdk /*
2928edf2c6dSthorpej * Reset the controller.
2938edf2c6dSthorpej */
2948edf2c6dSthorpej void
wdsc_reset(struct wd33c93_softc * sc)295398fb3fdStsutsui wdsc_reset(struct wd33c93_softc *sc)
2968edf2c6dSthorpej {
297398fb3fdStsutsui struct wdsc_softc *wsc = (struct wdsc_softc *)sc;
2988edf2c6dSthorpej struct hpc_dma_softc *dsc = &wsc->sc_hpcdma;
2998edf2c6dSthorpej
3008edf2c6dSthorpej hpcdma_reset(dsc);
3018edf2c6dSthorpej }
3028edf2c6dSthorpej
3038edf2c6dSthorpej /*
304de81761cSwdk * WD33c93 SCSI controller interrupt
3050ac0d0d4Swdk */
3060ac0d0d4Swdk int
wdsc_scsiintr(void * arg)307937db2deSsekiya wdsc_scsiintr(void *arg)
3080ac0d0d4Swdk {
309de81761cSwdk struct wdsc_softc *wsc = arg;
310398fb3fdStsutsui struct wd33c93_softc *sc = &wsc->sc_wd33c93;
3110ac0d0d4Swdk int found;
3120ac0d0d4Swdk
313398fb3fdStsutsui found = wd33c93_intr(sc);
314de81761cSwdk if (found)
315de81761cSwdk wsc->sc_intrcnt.ev_count++;
31659f7d57cStsutsui return found;
3170ac0d0d4Swdk }
318