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