1 /* $NetBSD: asc_ioasic.c,v 1.12 2001/08/26 11:47:25 simonb Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Tohru Nishimura. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 40 __KERNEL_RCSID(0, "$NetBSD: asc_ioasic.c,v 1.12 2001/08/26 11:47:25 simonb Exp $"); 41 42 #include <sys/types.h> 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/device.h> 46 #include <sys/buf.h> 47 48 #include <dev/scsipi/scsi_all.h> 49 #include <dev/scsipi/scsipi_all.h> 50 #include <dev/scsipi/scsiconf.h> 51 #include <dev/scsipi/scsi_message.h> 52 53 #include <machine/bus.h> 54 55 #include <dev/ic/ncr53c9xreg.h> 56 #include <dev/ic/ncr53c9xvar.h> 57 58 #include <dev/tc/tcvar.h> 59 #include <dev/tc/ioasicvar.h> 60 #include <dev/tc/ioasicreg.h> 61 62 struct asc_softc { 63 struct ncr53c9x_softc sc_ncr53c9x; /* glue to MI code */ 64 bus_space_tag_t sc_bst; /* bus space tag */ 65 bus_space_handle_t sc_bsh; /* bus space handle */ 66 bus_space_handle_t sc_scsi_bsh; /* ASC register handle */ 67 bus_dma_tag_t sc_dmat; /* bus dma tag */ 68 bus_dmamap_t sc_dmamap; /* bus dmamap */ 69 caddr_t *sc_dmaaddr; 70 size_t *sc_dmalen; 71 size_t sc_dmasize; 72 unsigned sc_flags; 73 #define ASC_ISPULLUP 0x0001 74 #define ASC_DMAACTIVE 0x0002 75 #define ASC_MAPLOADED 0x0004 76 }; 77 78 static int asc_ioasic_match __P((struct device *, struct cfdata *, void *)); 79 static void asc_ioasic_attach __P((struct device *, struct device *, void *)); 80 81 struct cfattach asc_ioasic_ca = { 82 sizeof(struct asc_softc), asc_ioasic_match, asc_ioasic_attach 83 }; 84 85 static u_char asc_read_reg __P((struct ncr53c9x_softc *, int)); 86 static void asc_write_reg __P((struct ncr53c9x_softc *, int, u_char)); 87 static int asc_dma_isintr __P((struct ncr53c9x_softc *sc)); 88 static void asc_ioasic_reset __P((struct ncr53c9x_softc *)); 89 static int asc_ioasic_intr __P((struct ncr53c9x_softc *)); 90 static int asc_ioasic_setup __P((struct ncr53c9x_softc *, 91 caddr_t *, size_t *, int, size_t *)); 92 static void asc_ioasic_go __P((struct ncr53c9x_softc *)); 93 static void asc_ioasic_stop __P((struct ncr53c9x_softc *)); 94 static int asc_dma_isactive __P((struct ncr53c9x_softc *)); 95 static void asc_clear_latched_intr __P((struct ncr53c9x_softc *)); 96 97 static struct ncr53c9x_glue asc_ioasic_glue = { 98 asc_read_reg, 99 asc_write_reg, 100 asc_dma_isintr, 101 asc_ioasic_reset, 102 asc_ioasic_intr, 103 asc_ioasic_setup, 104 asc_ioasic_go, 105 asc_ioasic_stop, 106 asc_dma_isactive, 107 asc_clear_latched_intr, 108 }; 109 110 static int 111 asc_ioasic_match(parent, cfdata, aux) 112 struct device *parent; 113 struct cfdata *cfdata; 114 void *aux; 115 { 116 struct ioasicdev_attach_args *d = aux; 117 118 if (strncmp("asc", d->iada_modname, TC_ROM_LLEN)) 119 return 0; 120 121 return 1; 122 } 123 124 static void 125 asc_ioasic_attach(parent, self, aux) 126 struct device *parent, *self; 127 void *aux; 128 { 129 struct ioasicdev_attach_args *d = aux; 130 struct asc_softc *asc = (struct asc_softc *)self; 131 struct ncr53c9x_softc *sc = &asc->sc_ncr53c9x; 132 133 /* 134 * Set up glue for MI code early; we use some of it here. 135 */ 136 sc->sc_glue = &asc_ioasic_glue; 137 asc->sc_bst = ((struct ioasic_softc *)parent)->sc_bst; 138 asc->sc_bsh = ((struct ioasic_softc *)parent)->sc_bsh; 139 if (bus_space_subregion(asc->sc_bst, asc->sc_bsh, 140 IOASIC_SLOT_12_START, 0x100, &asc->sc_scsi_bsh)) { 141 printf(": failed to map device registers\n"); 142 return; 143 } 144 asc->sc_dmat = ((struct ioasic_softc *)parent)->sc_dmat; 145 if (bus_dmamap_create(asc->sc_dmat, NBPG * 2, 146 2, NBPG, NBPG, BUS_DMA_NOWAIT, &asc->sc_dmamap)) { 147 printf(": failed to create DMA map\n"); 148 return; 149 } 150 151 sc->sc_id = 7; 152 sc->sc_freq = 25000000; 153 154 /* gimme Mhz */ 155 sc->sc_freq /= 1000000; 156 157 ioasic_intr_establish(parent, d->iada_cookie, TC_IPL_BIO, 158 ncr53c9x_intr, sc); 159 160 /* 161 * XXX More of this should be in ncr53c9x_attach(), but 162 * XXX should we really poke around the chip that much in 163 * XXX the MI code? Think about this more... 164 */ 165 166 /* 167 * Set up static configuration info. 168 */ 169 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB; 170 sc->sc_cfg2 = NCRCFG2_SCSI2; 171 sc->sc_cfg3 = 0; 172 sc->sc_rev = NCR_VARIANT_NCR53C94; 173 174 /* 175 * XXX minsync and maxxfer _should_ be set up in MI code, 176 * XXX but it appears to have some dependency on what sort 177 * XXX of DMA we're hooked up to, etc. 178 */ 179 180 /* 181 * This is the value used to start sync negotiations 182 * Note that the NCR register "SYNCTP" is programmed 183 * in "clocks per byte", and has a minimum value of 4. 184 * The SCSI period used in negotiation is one-fourth 185 * of the time (in nanoseconds) needed to transfer one byte. 186 * Since the chip's clock is given in MHz, we have the following 187 * formula: 4 * period = (1000 / freq) * 4 188 */ 189 sc->sc_minsync = (1000 / sc->sc_freq) * 5 / 4 ; 190 sc->sc_maxxfer = 64 * 1024; 191 192 /* Do the common parts of attachment. */ 193 sc->sc_adapter.adapt_minphys = minphys; 194 sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request; 195 ncr53c9x_attach(sc); 196 } 197 198 void 199 asc_ioasic_reset(sc) 200 struct ncr53c9x_softc *sc; 201 { 202 struct asc_softc *asc = (struct asc_softc *)sc; 203 u_int32_t ssr; 204 205 ssr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR); 206 ssr &= ~IOASIC_CSR_DMAEN_SCSI; 207 bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR, ssr); 208 bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_SCSI_SCR, 0); 209 210 if (asc->sc_flags & ASC_MAPLOADED) 211 bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap); 212 asc->sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED); 213 } 214 215 #define TWOPAGE(a) (NBPG*2 - ((a) & (NBPG-1))) 216 217 int 218 asc_ioasic_setup(sc, addr, len, ispullup, dmasize) 219 struct ncr53c9x_softc *sc; 220 caddr_t *addr; 221 size_t *len; 222 int ispullup; 223 size_t *dmasize; 224 { 225 struct asc_softc *asc = (struct asc_softc *)sc; 226 u_int32_t ssr, scr, *p; 227 size_t size; 228 vaddr_t cp; 229 230 NCR_DMA(("%s: start %d@%p,%s\n", sc->sc_dev.dv_xname, 231 *asc->sc_dmalen, *asc->sc_dmaaddr, ispullup ? "IN" : "OUT")); 232 233 /* upto two 4KB pages */ 234 size = min(*dmasize, TWOPAGE((size_t)*addr)); 235 asc->sc_dmaaddr = addr; 236 asc->sc_dmalen = len; 237 asc->sc_dmasize = size; 238 asc->sc_flags = (ispullup) ? ASC_ISPULLUP : 0; 239 *dmasize = size; /* return trimmed transfer size */ 240 241 /* stop DMA engine first */ 242 ssr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR); 243 ssr &= ~IOASIC_CSR_DMAEN_SCSI; 244 bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR, ssr); 245 246 /* have dmamap for the transfering addresses */ 247 if (bus_dmamap_load(asc->sc_dmat, asc->sc_dmamap, 248 *addr, size, 249 NULL /* kernel address */, BUS_DMA_NOWAIT)) 250 panic("%s: cannot allocate DMA address", sc->sc_dev.dv_xname); 251 252 /* take care of 8B constraint on starting address */ 253 cp = (vaddr_t)*addr; 254 if ((cp & 7) == 0) { 255 /* comfortably aligned to 8B boundary */ 256 scr = 0; 257 } 258 else { 259 /* truncate to the boundary */ 260 p = (u_int32_t *)(cp & ~7); 261 /* how many 16bit quantities in subject */ 262 scr = (cp & 7) >> 1; 263 /* trim down physical address too */ 264 asc->sc_dmamap->dm_segs[0].ds_addr &= ~7; 265 asc->sc_dmamap->dm_segs[0].ds_len += (cp & 6); 266 if ((asc->sc_flags & ASC_ISPULLUP) == 0) { 267 /* push down to SCSI device */ 268 scr |= 4; 269 /* round up physical address in this case */ 270 asc->sc_dmamap->dm_segs[0].ds_addr += 8; 271 /* don't care excess cache flush */ 272 } 273 /* pack fixup data in SDR0/SDR1 pair and instruct SCR */ 274 bus_space_write_4(asc->sc_bst, asc->sc_bsh, 275 IOASIC_SCSI_SDR0, p[0]); 276 bus_space_write_4(asc->sc_bst, asc->sc_bsh, 277 IOASIC_SCSI_SDR1, p[1]); 278 } 279 bus_space_write_4(asc->sc_bst, asc->sc_bsh, 280 IOASIC_SCSI_DMAPTR, 281 IOASIC_DMA_ADDR(asc->sc_dmamap->dm_segs[0].ds_addr)); 282 bus_space_write_4(asc->sc_bst, asc->sc_bsh, 283 IOASIC_SCSI_NEXTPTR, 284 (asc->sc_dmamap->dm_nsegs == 1) 285 ? ~0 : IOASIC_DMA_ADDR(asc->sc_dmamap->dm_segs[1].ds_addr)); 286 bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_SCSI_SCR, scr); 287 288 /* synchronize dmamap contents with memory image */ 289 bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap, 290 0, size, 291 (ispullup) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 292 293 asc->sc_flags |= ASC_MAPLOADED; 294 return 0; 295 } 296 297 void 298 asc_ioasic_go(sc) 299 struct ncr53c9x_softc *sc; 300 { 301 struct asc_softc *asc = (struct asc_softc *)sc; 302 u_int32_t ssr; 303 304 ssr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR); 305 if (asc->sc_flags & ASC_ISPULLUP) 306 ssr |= IOASIC_CSR_SCSI_DIR; 307 else { 308 /* ULTRIX does in this way */ 309 ssr &= ~IOASIC_CSR_SCSI_DIR; 310 bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR, ssr); 311 ssr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR); 312 } 313 ssr |= IOASIC_CSR_DMAEN_SCSI; 314 bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR, ssr); 315 asc->sc_flags |= ASC_DMAACTIVE; 316 } 317 318 static int 319 asc_ioasic_intr(sc) 320 struct ncr53c9x_softc *sc; 321 { 322 struct asc_softc *asc = (struct asc_softc *)sc; 323 int trans, resid; 324 u_int tcl, tcm, ssr, scr, intr; 325 326 if ((asc->sc_flags & ASC_DMAACTIVE) == 0) 327 panic("ioasic_intr: DMA wasn't active"); 328 329 #define IOASIC_ASC_ERRORS \ 330 (IOASIC_INTR_SCSI_PTR_LOAD|IOASIC_INTR_SCSI_OVRUN|IOASIC_INTR_SCSI_READ_E) 331 /* 332 * When doing polled I/O, the SCSI bits in the interrupt register won't 333 * get cleared by the interrupt processing. This will cause the DMA 334 * address registers to not load on the next DMA transfer. 335 * Check for these bits here, and clear them if needed. 336 */ 337 intr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_INTR); 338 if ((intr & IOASIC_ASC_ERRORS) != 0) { 339 intr &= ~IOASIC_ASC_ERRORS; 340 bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_INTR, intr); 341 } 342 343 /* DMA has stopped */ 344 ssr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR); 345 ssr &= ~IOASIC_CSR_DMAEN_SCSI; 346 bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR, ssr); 347 348 asc->sc_flags &= ~ASC_DMAACTIVE; 349 350 if (asc->sc_dmasize == 0) { 351 /* A "Transfer Pad" operation completed */ 352 tcl = NCR_READ_REG(sc, NCR_TCL); 353 tcm = NCR_READ_REG(sc, NCR_TCM); 354 NCR_DMA(("ioasic_intr: discarded %d bytes (tcl=%d, tcm=%d)\n", 355 tcl | (tcm << 8), tcl, tcm)); 356 return 0; 357 } 358 359 resid = 0; 360 if ((asc->sc_flags & ASC_ISPULLUP) == 0 && 361 (resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) { 362 NCR_DMA(("ioasic_intr: empty FIFO of %d ", resid)); 363 DELAY(1); 364 } 365 366 resid += (tcl = NCR_READ_REG(sc, NCR_TCL)); 367 resid += (tcm = NCR_READ_REG(sc, NCR_TCM)) << 8; 368 369 trans = asc->sc_dmasize - resid; 370 if (trans < 0) { /* transferred < 0 ? */ 371 printf("ioasic_intr: xfer (%d) > req (%d)\n", 372 trans, asc->sc_dmasize); 373 trans = asc->sc_dmasize; 374 } 375 NCR_DMA(("ioasic_intr: tcl=%d, tcm=%d; trans=%d, resid=%d\n", 376 tcl, tcm, trans, resid)); 377 378 bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap, 379 0, asc->sc_dmasize, 380 (asc->sc_flags & ASC_ISPULLUP) 381 ? BUS_DMASYNC_POSTREAD 382 : BUS_DMASYNC_POSTWRITE); 383 384 scr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_SCSI_SCR); 385 if ((asc->sc_flags & ASC_ISPULLUP) && scr != 0) { 386 u_int32_t sdr[2], ptr; 387 388 sdr[0] = bus_space_read_4(asc->sc_bst, asc->sc_bsh, 389 IOASIC_SCSI_SDR0); 390 sdr[1] = bus_space_read_4(asc->sc_bst, asc->sc_bsh, 391 IOASIC_SCSI_SDR1); 392 ptr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, 393 IOASIC_SCSI_DMAPTR); 394 ptr = (ptr >> 3) & 0x1ffffffc; 395 /* 396 * scr: 1 -> short[0] 397 * 2 -> short[0] + short[1] 398 * 3 -> short[0] + short[1] + short[2] 399 */ 400 scr &= IOASIC_SCR_WORD; 401 memcpy((void *)MIPS_PHYS_TO_KSEG0(ptr), sdr, scr << 1); 402 } 403 404 bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap); 405 asc->sc_flags &= ~ASC_MAPLOADED; 406 407 *asc->sc_dmalen -= trans; 408 *asc->sc_dmaaddr += trans; 409 410 return 0; 411 } 412 413 414 void 415 asc_ioasic_stop(sc) 416 struct ncr53c9x_softc *sc; 417 { 418 struct asc_softc *asc = (struct asc_softc *)sc; 419 420 if (asc->sc_flags & ASC_MAPLOADED) { 421 bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap, 422 0, asc->sc_dmasize, 423 (asc->sc_flags & ASC_ISPULLUP) 424 ? BUS_DMASYNC_POSTREAD 425 : BUS_DMASYNC_POSTWRITE); 426 bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap); 427 } 428 429 asc->sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED); 430 } 431 432 static u_char 433 asc_read_reg(sc, reg) 434 struct ncr53c9x_softc *sc; 435 int reg; 436 { 437 struct asc_softc *asc = (struct asc_softc *)sc; 438 u_int32_t v; 439 440 v = bus_space_read_4(asc->sc_bst, 441 asc->sc_scsi_bsh, reg * sizeof(u_int32_t)); 442 443 return v & 0xff; 444 } 445 446 static void 447 asc_write_reg(sc, reg, val) 448 struct ncr53c9x_softc *sc; 449 int reg; 450 u_char val; 451 { 452 struct asc_softc *asc = (struct asc_softc *)sc; 453 454 bus_space_write_4(asc->sc_bst, 455 asc->sc_scsi_bsh, reg * sizeof(u_int32_t), val); 456 } 457 458 static int 459 asc_dma_isintr(sc) 460 struct ncr53c9x_softc *sc; 461 { 462 return !!(NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT); 463 } 464 465 static int 466 asc_dma_isactive(sc) 467 struct ncr53c9x_softc *sc; 468 { 469 struct asc_softc *asc = (struct asc_softc *)sc; 470 471 return !!(asc->sc_flags & ASC_DMAACTIVE); 472 } 473 474 static void 475 asc_clear_latched_intr(sc) 476 struct ncr53c9x_softc *sc; 477 { 478 } 479