xref: /netbsd-src/sys/arch/sun2/dev/sc_mbmem.c (revision d8d32193539cbf46c50d85202560be2cda7dfab5)
1*d8d32193Srin /*	$NetBSD: sc_mbmem.c,v 1.16 2023/08/04 11:18:18 rin Exp $	*/
202672011Sfredette 
302672011Sfredette /*-
402672011Sfredette  * Copyright (c) 1996 The NetBSD Foundation, Inc.
502672011Sfredette  * All rights reserved.
602672011Sfredette  *
702672011Sfredette  * This code is derived from software contributed to The NetBSD Foundation
802672011Sfredette  * by Adam Glass, David Jones, Gordon W. Ross, and Matthew Fredette.
902672011Sfredette  *
1002672011Sfredette  * Redistribution and use in source and binary forms, with or without
1102672011Sfredette  * modification, are permitted provided that the following conditions
1202672011Sfredette  * are met:
1302672011Sfredette  * 1. Redistributions of source code must retain the above copyright
1402672011Sfredette  *    notice, this list of conditions and the following disclaimer.
1502672011Sfredette  * 2. Redistributions in binary form must reproduce the above copyright
1602672011Sfredette  *    notice, this list of conditions and the following disclaimer in the
1702672011Sfredette  *    documentation and/or other materials provided with the distribution.
1802672011Sfredette  *
1902672011Sfredette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2002672011Sfredette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2102672011Sfredette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2202672011Sfredette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2302672011Sfredette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2402672011Sfredette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2502672011Sfredette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2602672011Sfredette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2702672011Sfredette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2802672011Sfredette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2902672011Sfredette  * POSSIBILITY OF SUCH DAMAGE.
3002672011Sfredette  */
3102672011Sfredette 
3202672011Sfredette /*
3302672011Sfredette  * This file contains only the machine-dependent parts of the
3402672011Sfredette  * Sun-2 SCSI driver.  (Autoconfig stuff and DMA functions.)
3502672011Sfredette  * The machine-independent parts are in sunscpal.c
3602672011Sfredette  *
3702672011Sfredette  * Supported hardware includes:
3802672011Sfredette  * Sun SCSI-2 on Multibus (Sun2/120,Sun2/170)
3902672011Sfredette  * Sun SCSI-2 on VME (Sun2/50,Sun3/160,Sun3/260,others?)
4002672011Sfredette  *
4102672011Sfredette  * Credits, history:
4202672011Sfredette  *
4302672011Sfredette  * Matthew Fredette took revision 1.15 of si_vme.c, and then heavily
4402672011Sfredette  * modified it to support the Sun sc.  The remaining credits
4502672011Sfredette  * are from si_vme.c:
4602672011Sfredette  *
4702672011Sfredette  * David Jones wrote the initial version of this module, which
4802672011Sfredette  * included support for the VME adapter only. (no reselection).
4902672011Sfredette  *
5002672011Sfredette  * Gordon Ross added support for the OBIO adapter, and re-worked
5102672011Sfredette  * both the VME and OBIO code to support disconnect/reselect.
5202672011Sfredette  * (Required figuring out the hardware "features" noted above.)
5302672011Sfredette  *
5402672011Sfredette  * The autoconfiguration boilerplate came from Adam Glass.
5502672011Sfredette  */
5602672011Sfredette 
5702672011Sfredette /*****************************************************************
5802672011Sfredette  * Multibus functions for sc
5902672011Sfredette  ****************************************************************/
6002672011Sfredette 
61ed517291Slukem #include <sys/cdefs.h>
62*d8d32193Srin __KERNEL_RCSID(0, "$NetBSD: sc_mbmem.c,v 1.16 2023/08/04 11:18:18 rin Exp $");
63ed517291Slukem 
6402672011Sfredette #include <sys/param.h>
6502672011Sfredette #include <sys/systm.h>
6602672011Sfredette #include <sys/errno.h>
6702672011Sfredette #include <sys/kernel.h>
68fd5889cbSthorpej #include <sys/kmem.h>
6902672011Sfredette #include <sys/device.h>
7002672011Sfredette #include <sys/buf.h>
7102672011Sfredette #include <sys/proc.h>
7202672011Sfredette 
7302672011Sfredette #include <dev/scsipi/scsi_all.h>
7402672011Sfredette #include <dev/scsipi/scsipi_all.h>
7502672011Sfredette #include <dev/scsipi/scsipi_debug.h>
7602672011Sfredette #include <dev/scsipi/scsiconf.h>
7702672011Sfredette 
7802672011Sfredette #include <machine/autoconf.h>
7902672011Sfredette #include <machine/bus.h>
8002672011Sfredette 
8102672011Sfredette /* #define DEBUG XXX */
8202672011Sfredette 
8302672011Sfredette #include <dev/ic/sunscpalreg.h>
8402672011Sfredette #include <dev/ic/sunscpalvar.h>
8502672011Sfredette 
8675eb3514Sfredette /* Yes, we use a VME header file. */
8775eb3514Sfredette #include <dev/vme/screg.h>
8802672011Sfredette 
8902672011Sfredette /*
9002672011Sfredette  * Transfers smaller than this are done using PIO
9102672011Sfredette  * (on assumption they're not worth DMA overhead)
9202672011Sfredette  */
9302672011Sfredette #define	MIN_DMA_LEN 128
9402672011Sfredette 
9502672011Sfredette /*
9602672011Sfredette  * New-style autoconfig attachment
9702672011Sfredette  */
9802672011Sfredette 
99bc6f8d14Stsutsui static int	sunsc_mbmem_match(device_t, cfdata_t, void *);
100bc6f8d14Stsutsui static void	sunsc_mbmem_attach(device_t, device_t, void *);
10110b1a7beSchs static int	sunsc_mbmem_intr(void *);
10202672011Sfredette 
103bc6f8d14Stsutsui CFATTACH_DECL_NEW(sc_mbmem, sizeof(struct sunscpal_softc),
1044bf871a7Sthorpej     sunsc_mbmem_match, sunsc_mbmem_attach, NULL, NULL);
10502672011Sfredette 
10602672011Sfredette /*
10702672011Sfredette  * Options for parity, DMA, and interrupts.
10802672011Sfredette  */
10902672011Sfredette int sunsc_mbmem_options = 0x00;
11002672011Sfredette 
11102672011Sfredette static int
sunsc_mbmem_match(device_t parent,cfdata_t cf,void * aux)112bc6f8d14Stsutsui sunsc_mbmem_match(device_t parent, cfdata_t cf, void *aux)
11302672011Sfredette {
11475eb3514Sfredette 	struct mbmem_attach_args *mbma = aux;
11575eb3514Sfredette 	bus_space_handle_t bh;
116bc6f8d14Stsutsui 	bool matched;
11702672011Sfredette 
11802672011Sfredette 	/* No default Multibus address. */
11975eb3514Sfredette 	if (mbma->mbma_paddr == -1)
120bc6f8d14Stsutsui 		return 0;
12102672011Sfredette 
12275eb3514Sfredette 	/* Make sure there is something there... */
12375eb3514Sfredette 	if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, SCREG_BANK_SZ,
12475eb3514Sfredette 	    0, &bh))
125bc6f8d14Stsutsui 		return 0;
126bc6f8d14Stsutsui 	matched =
127bc6f8d14Stsutsui 	    (bus_space_peek_2(mbma->mbma_bustag, bh, SCREG_ICR, NULL) == 0);
12875eb3514Sfredette 	bus_space_unmap(mbma->mbma_bustag, bh, SCREG_BANK_SZ);
12975eb3514Sfredette 	if (!matched)
130bc6f8d14Stsutsui 		return 0;
13102672011Sfredette 
13202672011Sfredette 	/* Default interrupt priority. */
13375eb3514Sfredette 	if (mbma->mbma_pri == -1)
13475eb3514Sfredette 		mbma->mbma_pri = 2;
13502672011Sfredette 
136bc6f8d14Stsutsui 	return 1;
13702672011Sfredette }
13802672011Sfredette 
13902672011Sfredette static void
sunsc_mbmem_attach(device_t parent,device_t self,void * args)140bc6f8d14Stsutsui sunsc_mbmem_attach(device_t parent, device_t self, void *args)
14102672011Sfredette {
142bc6f8d14Stsutsui 	struct sunscpal_softc *sc = device_private(self);
1432be6494fSthorpej 	struct cfdata *cf = device_cfdata(self);
14475eb3514Sfredette 	struct mbmem_attach_args *mbma = args;
14575eb3514Sfredette 	int i;
14602672011Sfredette 
147bc6f8d14Stsutsui 	sc->sc_dev = self;
148bc6f8d14Stsutsui 
14902672011Sfredette 	/* Map in the device. */
15075eb3514Sfredette 	sc->sunscpal_regt = mbma->mbma_bustag;
15175eb3514Sfredette 	sc->sunscpal_dmat = mbma->mbma_dmatag;
15275eb3514Sfredette 	if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, SCREG_BANK_SZ,
15302672011Sfredette 	    0, &sc->sunscpal_regh))
154bc6f8d14Stsutsui 		panic("%s: can't map", __func__);
15502672011Sfredette 
15602672011Sfredette 	/* Device register offsets. */
15775eb3514Sfredette 	sc->sunscpal_data = SCREG_DATA;
15875eb3514Sfredette 	sc->sunscpal_cmd_stat = SCREG_CMD_STAT;
15975eb3514Sfredette 	sc->sunscpal_icr = SCREG_ICR;
16075eb3514Sfredette 	sc->sunscpal_dma_addr_h = SCREG_DMA_ADDR_H;
16175eb3514Sfredette 	sc->sunscpal_dma_addr_l = SCREG_DMA_ADDR_L;
16275eb3514Sfredette 	sc->sunscpal_dma_count = SCREG_DMA_COUNT;
16375eb3514Sfredette 	sc->sunscpal_intvec = SCREG_INTVEC;
16475eb3514Sfredette 
16575eb3514Sfredette 	/* Allocate DMA handles. */
16675eb3514Sfredette 	i = SUNSCPAL_OPENINGS * sizeof(struct sunscpal_dma_handle);
167*d8d32193Srin 	sc->sc_dma_handles = kmem_zalloc(i, KM_SLEEP);
16875eb3514Sfredette 	for (i = 0; i < SUNSCPAL_OPENINGS; i++)
16975eb3514Sfredette 		if (bus_dmamap_create(sc->sunscpal_dmat, SUNSCPAL_MAX_DMA_LEN,
17075eb3514Sfredette 		    1, SUNSCPAL_MAX_DMA_LEN,
171bc6f8d14Stsutsui 		    0, BUS_DMA_WAITOK, &sc->sc_dma_handles[i].dh_dmamap) != 0)
1721ffa7b76Swiz 			panic("sc: DMA map create failed");
17302672011Sfredette 
17402672011Sfredette 	/* Miscellaneous. */
17502672011Sfredette 	sc->sc_min_dma_len = MIN_DMA_LEN;
17602672011Sfredette 	sc->sc_rev = SUNSCPAL_VARIANT_501_1006;
17702672011Sfredette 
17802672011Sfredette 	/* Attach interrupt handler. */
17975eb3514Sfredette 	bus_intr_establish(mbma->mbma_bustag, mbma->mbma_pri, IPL_BIO, 0,
18002672011Sfredette 	    sunsc_mbmem_intr, sc);
18102672011Sfredette 
18202672011Sfredette 	/* Do the common attach stuff. */
183bc6f8d14Stsutsui 	sunscpal_attach(sc, cf->cf_flags ? cf->cf_flags : sunsc_mbmem_options);
18402672011Sfredette }
18502672011Sfredette 
18602672011Sfredette static int
sunsc_mbmem_intr(void * arg)18702672011Sfredette sunsc_mbmem_intr(void *arg)
18802672011Sfredette {
18902672011Sfredette 	struct sunscpal_softc *sc = arg;
19002672011Sfredette 	int claimed;
19102672011Sfredette 
19202672011Sfredette 	claimed = sunscpal_intr(sc);
19302672011Sfredette #ifdef	DEBUG
194bc6f8d14Stsutsui 	if (claimed == 0) {
195bc6f8d14Stsutsui 		printf("%s: spurious from SBC\n", __func__);
19602672011Sfredette 	}
19702672011Sfredette #endif
19802672011Sfredette 	/* Yes, we DID cause this interrupt. */
19902672011Sfredette 	claimed = 1;
20002672011Sfredette 
201bc6f8d14Stsutsui 	return claimed;
20202672011Sfredette }
203