1/*	$NetBSD: sc_vme.c,v 1.16 2008/04/28 20:24:01 martin Exp $	*/
2
3/*-
4 * Copyright (c) 1996,2000,2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Glass, David Jones, Gordon W. Ross, Jason R. Thorpe,
9 * Paul Kranenburg, and Matt Fredette.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * This file contains VME bus-dependent of the `sc' SCSI adapter.
35 * This hardware is frequently found on Sun 2, Sun 3, and Sun 4 machines.
36 *
37 * The SCSI machinery on this adapter is implemented by an Sun custom
38 * chipset, which is handled by the chipset driver in /sys/dev/ic/sunscpal.c
39 */
40
41/*
42 * This driver originated as an MD implementation for the `si' VME driver.
43 * The notes pertaining to that history are included below.
44 *
45 * David Jones wrote the initial version of this module for NetBSD/sun3,
46 * which included support for the VME adapter only. (no reselection).
47 *
48 * Gordon Ross added support for the Sun 3 OBIO adapter, and re-worked
49 * both the VME and OBIO code to support disconnect/reselect.
50 * (Required figuring out the hardware "features" noted above.)
51 *
52 * The autoconfiguration boilerplate came from Adam Glass.
53 *
54 * Jason R. Thorpe ported the autoconfiguration and VME portions to
55 * NetBSD/sparc, and added initial support for the 4/100 "SCSI Weird",
56 * a wacky OBIO variant of the VME SCSI-3.  Many thanks to Chuck Cranor
57 * for lots of helpful tips and suggestions.  Thanks also to Paul Kranenburg
58 * and Chris Torek for bits of insight needed along the way.  Thanks to
59 * David Gilbert and Andrew Gillham who risked filesystem life-and-limb
60 * for the sake of testing.  Andrew Gillham helped work out the bugs
61 * the 4/100 DMA code.
62 */
63
64#include <sys/cdefs.h>
65__KERNEL_RCSID(0, "$NetBSD: sc_vme.c,v 1.16 2008/04/28 20:24:01 martin Exp $");
66
67#include "opt_ddb.h"
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/kernel.h>
72#include <sys/malloc.h>
73#include <sys/errno.h>
74#include <sys/device.h>
75#include <sys/buf.h>
76
77#include <sys/bus.h>
78#include <sys/intr.h>
79
80#include <dev/vme/vmereg.h>
81#include <dev/vme/vmevar.h>
82
83#include <dev/scsipi/scsi_all.h>
84#include <dev/scsipi/scsipi_all.h>
85#include <dev/scsipi/scsipi_debug.h>
86#include <dev/scsipi/scsiconf.h>
87
88#if !defined(DDB) && !defined(Debugger)
89#define	Debugger()
90#endif
91
92#ifndef DEBUG
93#define DEBUG XXX
94#endif
95
96#include <dev/ic/sunscpalvar.h>
97
98#include <dev/vme/screg.h>
99
100/*
101 * Transfers smaller than this are done using PIO
102 * (on assumption they're not worth DMA overhead)
103 */
104#define	MIN_DMA_LEN 128
105
106int sunsc_vme_options = 0;
107
108static int	sc_vme_match(device_t, cfdata_t, void *);
109static void	sc_vme_attach(device_t, device_t, void *);
110static int	sc_vme_intr(void *);
111
112/* Auto-configuration glue. */
113CFATTACH_DECL_NEW(sc_vme, sizeof(struct sunscpal_softc),
114    sc_vme_match, sc_vme_attach, NULL, NULL);
115
116static int
117sc_vme_match(device_t parent, cfdata_t cf, void *aux)
118{
119	struct vme_attach_args	*va = aux;
120	vme_chipset_tag_t	ct = va->va_vct;
121        vme_am_t		mod;
122        vme_addr_t		vme_addr;
123
124	/* Make sure there is something there... */
125	mod = VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
126	vme_addr = va->r[0].offset;
127
128	if (vme_probe(ct, vme_addr, 1, mod, VME_D8, NULL, 0) != 0)
129		return 0;
130
131	/*
132	 * If this is a VME SCSI board, we have to determine whether
133	 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board.  This can
134	 * be determined using the fact that the "sc" board occupies
135	 * 4K bytes in VME space but the "si" board occupies 2K bytes.
136	 */
137	return vme_probe(ct, vme_addr + 0x801, 1, mod, VME_D8, NULL, 0) == 0;
138}
139
140static void
141sc_vme_attach(device_t parent, device_t self, void *aux)
142{
143	struct sunscpal_softc	*sc = device_private(self);
144	struct vme_attach_args	*va = aux;
145	vme_chipset_tag_t	ct = va->va_vct;
146	bus_space_tag_t		bt;
147	bus_space_handle_t	bh;
148	vme_mapresc_t resc;
149	vme_intr_handle_t	ih;
150	vme_am_t		mod;
151	int i;
152
153	sc->sc_dev = self;
154	sc->sunscpal_dmat = va->va_bdt;
155
156	mod = VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
157
158	if (vme_space_map(ct, va->r[0].offset, SCREG_BANK_SZ,
159	    mod, VME_D8, 0, &bt, &bh, &resc) != 0)
160		panic("%s: vme_space_map", device_xname(self));
161
162	sc->sunscpal_regt = bt;
163	sc->sunscpal_regh = bh;
164
165	vme_intr_map(ct, va->ilevel, va->ivector, &ih);
166	vme_intr_establish(ct, ih, IPL_BIO, sc_vme_intr, sc);
167
168	aprint_normal("\n");
169
170	/*
171	 * Initialize fields used by the MI code
172	 */
173
174	/* PAL register bank offsets */
175	sc->sunscpal_data = SCREG_DATA;
176	sc->sunscpal_cmd_stat = SCREG_CMD_STAT;
177	sc->sunscpal_icr = SCREG_ICR;
178	sc->sunscpal_dma_addr_h = SCREG_DMA_ADDR_H;
179	sc->sunscpal_dma_addr_l = SCREG_DMA_ADDR_L;
180	sc->sunscpal_dma_count = SCREG_DMA_COUNT;
181	sc->sunscpal_intvec = SCREG_INTVEC;
182
183	/* Miscellaneous. */
184	sc->sc_min_dma_len = MIN_DMA_LEN;
185	sc->sc_rev = SUNSCPAL_VARIANT_501_1045;
186
187	/*
188	 * Allocate DMA handles.
189	 */
190	i = SUNSCPAL_OPENINGS * sizeof(struct sunscpal_dma_handle);
191	sc->sc_dma_handles = malloc(i, M_DEVBUF, M_NOWAIT);
192	if (sc->sc_dma_handles == NULL)
193		panic("sc: DMA handle malloc failed");
194
195	for (i = 0; i < SUNSCPAL_OPENINGS; i++) {
196		sc->sc_dma_handles[i].dh_flags = 0;
197
198		/* Allocate a DMA handle */
199		if (vme_dmamap_create(
200		    ct,				/* VME chip tag */
201		    SUNSCPAL_MAX_DMA_LEN,	/* size */
202		    VME_AM_A24,			/* address modifier */
203		    VME_D16,			/* data size */
204		    0,				/* swap */
205		    1,				/* nsegments */
206		    SUNSCPAL_MAX_DMA_LEN,	/* maxsegsz */
207		    0,				/* boundary */
208		    BUS_DMA_NOWAIT,
209		    &sc->sc_dma_handles[i].dh_dmamap) != 0) {
210
211			aprint_error_dev(self, "DMA buffer map create error\n");
212			return;
213		}
214	}
215
216	/*
217	 * Set up interrupts on the board.
218	 */
219	SUNSCPAL_WRITE_1(sc, sunscpal_intvec, va->ivector & 0xFF);
220
221	/* Do the common attach stuff. */
222	printf("%s", device_xname(self));
223	sunscpal_attach(sc, (device_cfdata(self)->cf_flags ?
224	    device_cfdata(self)->cf_flags : sunsc_vme_options));
225}
226
227static int
228sc_vme_intr(void *arg)
229{
230	struct sunscpal_softc *sc = arg;
231	int claimed;
232
233	claimed = sunscpal_intr(sc);
234#ifdef  DEBUG
235	if (!claimed) {
236        	printf("%s: spurious from SBC\n", __func__);
237	}
238#endif
239	/* Yes, we DID cause this interrupt. */
240	claimed = 1;
241
242	return claimed;
243}
244