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 106 int sunsc_vme_options = 0; 107 108 static int sc_vme_match(struct device *, struct cfdata *, void *); 109 static void sc_vme_attach(struct device *, struct device *, void *); 110 static int sc_vme_intr(void *); 111 112 /* Auto-configuration glue. */ 113 CFATTACH_DECL(sc_vme, sizeof(struct sunscpal_softc), 114 sc_vme_match, sc_vme_attach, NULL, NULL); 115 116 static int 117 sc_vme_match(parent, cf, aux) 118 struct device *parent; 119 struct cfdata *cf; 120 void *aux; 121 { 122 struct vme_attach_args *va = aux; 123 vme_chipset_tag_t ct = va->va_vct; 124 vme_am_t mod; 125 vme_addr_t vme_addr; 126 127 /* Make sure there is something there... */ 128 mod = VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA; 129 vme_addr = va->r[0].offset; 130 131 if (vme_probe(ct, vme_addr, 1, mod, VME_D8, NULL, 0) != 0) 132 return (0); 133 134 /* 135 * If this is a VME SCSI board, we have to determine whether 136 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board. This can 137 * be determined using the fact that the "sc" board occupies 138 * 4K bytes in VME space but the "si" board occupies 2K bytes. 139 */ 140 return (vme_probe(ct, vme_addr + 0x801, 1, mod, VME_D8, NULL, 0) == 0); 141 } 142 143 static void 144 sc_vme_attach(parent, self, aux) 145 struct device *parent, *self; 146 void *aux; 147 { 148 struct sunscpal_softc *sc = (struct sunscpal_softc *) self; 149 struct vme_attach_args *va = aux; 150 vme_chipset_tag_t ct = va->va_vct; 151 bus_space_tag_t bt; 152 bus_space_handle_t bh; 153 vme_mapresc_t resc; 154 vme_intr_handle_t ih; 155 vme_am_t mod; 156 int i; 157 158 sc->sunscpal_dmat = va->va_bdt; 159 160 mod = VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA; 161 162 if (vme_space_map(ct, va->r[0].offset, SCREG_BANK_SZ, 163 mod, VME_D8, 0, &bt, &bh, &resc) != 0) 164 panic("%s: vme_space_map", device_xname(&sc->sc_dev)); 165 166 sc->sunscpal_regt = bt; 167 sc->sunscpal_regh = bh; 168 169 vme_intr_map(ct, va->ilevel, va->ivector, &ih); 170 vme_intr_establish(ct, ih, IPL_BIO, sc_vme_intr, sc); 171 172 printf("\n"); 173 174 /* 175 * Initialize fields used by the MI code 176 */ 177 178 /* PAL register bank offsets */ 179 sc->sunscpal_data = SCREG_DATA; 180 sc->sunscpal_cmd_stat = SCREG_CMD_STAT; 181 sc->sunscpal_icr = SCREG_ICR; 182 sc->sunscpal_dma_addr_h = SCREG_DMA_ADDR_H; 183 sc->sunscpal_dma_addr_l = SCREG_DMA_ADDR_L; 184 sc->sunscpal_dma_count = SCREG_DMA_COUNT; 185 sc->sunscpal_intvec = SCREG_INTVEC; 186 187 /* Miscellaneous. */ 188 sc->sc_min_dma_len = MIN_DMA_LEN; 189 sc->sc_rev = SUNSCPAL_VARIANT_501_1045; 190 191 /* 192 * Allocate DMA handles. 193 */ 194 i = SUNSCPAL_OPENINGS * sizeof(struct sunscpal_dma_handle); 195 sc->sc_dma_handles = (struct sunscpal_dma_handle *)malloc(i, M_DEVBUF, M_NOWAIT); 196 if (sc->sc_dma_handles == NULL) 197 panic("sc: DMA handle malloc failed"); 198 199 for (i = 0; i < SUNSCPAL_OPENINGS; i++) { 200 sc->sc_dma_handles[i].dh_flags = 0; 201 202 /* Allocate a DMA handle */ 203 if (vme_dmamap_create( 204 ct, /* VME chip tag */ 205 SUNSCPAL_MAX_DMA_LEN, /* size */ 206 VME_AM_A24, /* address modifier */ 207 VME_D16, /* data size */ 208 0, /* swap */ 209 1, /* nsegments */ 210 SUNSCPAL_MAX_DMA_LEN, /* maxsegsz */ 211 0, /* boundary */ 212 BUS_DMA_NOWAIT, 213 &sc->sc_dma_handles[i].dh_dmamap) != 0) { 214 215 aprint_error_dev(&sc->sc_dev, "DMA buffer map create error\n"); 216 return; 217 } 218 } 219 220 /* 221 * Set up interrupts on the board. 222 */ 223 SUNSCPAL_WRITE_1(sc, sunscpal_intvec, va->ivector & 0xFF); 224 225 /* Do the common attach stuff. */ 226 printf("%s", device_xname(&sc->sc_dev)); 227 sunscpal_attach(sc, (device_cfdata(&sc->sc_dev)->cf_flags ? 228 device_cfdata(&sc->sc_dev)->cf_flags : 229 sunsc_vme_options)); 230 } 231 232 static int 233 sc_vme_intr(void *arg) 234 { 235 struct sunscpal_softc *sc = arg; 236 int claimed; 237 238 claimed = sunscpal_intr(sc); 239 #ifdef DEBUG 240 if (!claimed) { 241 printf("sc_vme_intr: spurious from SBC\n"); 242 } 243 #endif 244 /* Yes, we DID cause this interrupt. */ 245 claimed = 1; 246 247 return (claimed); 248 } 249 250