1 1.34 martin /* $NetBSD: asc_tc.c,v 1.34 2008/04/28 20:23:58 martin Exp $ */ 2 1.1 jonathan 3 1.16 simonb /*- 4 1.16 simonb * Copyright (c) 2000 The NetBSD Foundation, Inc. 5 1.16 simonb * All rights reserved. 6 1.16 simonb * 7 1.16 simonb * This code is derived from software contributed to The NetBSD Foundation 8 1.16 simonb * by Tohru Nishimura. 9 1.1 jonathan * 10 1.16 simonb * Redistribution and use in source and binary forms, with or without 11 1.16 simonb * modification, are permitted provided that the following conditions 12 1.16 simonb * are met: 13 1.16 simonb * 1. Redistributions of source code must retain the above copyright 14 1.16 simonb * notice, this list of conditions and the following disclaimer. 15 1.16 simonb * 2. Redistributions in binary form must reproduce the above copyright 16 1.16 simonb * notice, this list of conditions and the following disclaimer in the 17 1.16 simonb * documentation and/or other materials provided with the distribution. 18 1.1 jonathan * 19 1.16 simonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.16 simonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.16 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.16 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.16 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.16 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.16 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.16 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.16 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.16 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.16 simonb * POSSIBILITY OF SUCH DAMAGE. 30 1.1 jonathan */ 31 1.1 jonathan 32 1.18 lukem #include <sys/cdefs.h> 33 1.34 martin __KERNEL_RCSID(0, "$NetBSD: asc_tc.c,v 1.34 2008/04/28 20:23:58 martin Exp $"); 34 1.16 simonb 35 1.1 jonathan #include <sys/param.h> 36 1.1 jonathan #include <sys/systm.h> 37 1.1 jonathan #include <sys/device.h> 38 1.16 simonb #include <sys/buf.h> 39 1.16 simonb 40 1.16 simonb #include <dev/scsipi/scsi_all.h> 41 1.16 simonb #include <dev/scsipi/scsipi_all.h> 42 1.16 simonb #include <dev/scsipi/scsiconf.h> 43 1.16 simonb #include <dev/scsipi/scsi_message.h> 44 1.16 simonb 45 1.31 ad #include <sys/bus.h> 46 1.16 simonb 47 1.16 simonb #include <dev/ic/ncr53c9xreg.h> 48 1.16 simonb #include <dev/ic/ncr53c9xvar.h> 49 1.10 mrg 50 1.1 jonathan #include <dev/tc/tcvar.h> 51 1.1 jonathan 52 1.16 simonb struct asc_softc { 53 1.16 simonb struct ncr53c9x_softc sc_ncr53c9x; /* glue to MI code */ 54 1.16 simonb bus_space_tag_t sc_bst; 55 1.16 simonb bus_space_handle_t sc_bsh; 56 1.16 simonb bus_dma_tag_t sc_dmat; 57 1.16 simonb bus_dmamap_t sc_dmamap; 58 1.33 tsutsui uint8_t **sc_dmaaddr; 59 1.16 simonb size_t *sc_dmalen; 60 1.16 simonb size_t sc_dmasize; 61 1.16 simonb int sc_active; /* DMA active ? */ 62 1.16 simonb int sc_ispullup; /* DMA into main memory? */ 63 1.1 jonathan 64 1.16 simonb /* XXX XXX XXX */ 65 1.30 yamt char *sc_base, *sc_bounce, *sc_target; 66 1.16 simonb }; 67 1.1 jonathan 68 1.33 tsutsui static int asc_tc_match(device_t, cfdata_t, void *); 69 1.33 tsutsui static void asc_tc_attach(device_t, device_t, void *); 70 1.1 jonathan 71 1.33 tsutsui CFATTACH_DECL_NEW(asc_tc, sizeof(struct asc_softc), 72 1.22 thorpej asc_tc_match, asc_tc_attach, NULL, NULL); 73 1.1 jonathan 74 1.33 tsutsui static uint8_t asc_read_reg(struct ncr53c9x_softc *, int); 75 1.33 tsutsui static void asc_write_reg(struct ncr53c9x_softc *, int, uint8_t); 76 1.24 perry static int asc_dma_isintr(struct ncr53c9x_softc *); 77 1.24 perry static void asc_tc_reset(struct ncr53c9x_softc *); 78 1.24 perry static int asc_tc_intr(struct ncr53c9x_softc *); 79 1.33 tsutsui static int asc_tc_setup(struct ncr53c9x_softc *, uint8_t **, 80 1.33 tsutsui size_t *, int, size_t *); 81 1.24 perry static void asc_tc_go(struct ncr53c9x_softc *); 82 1.24 perry static void asc_tc_stop(struct ncr53c9x_softc *); 83 1.24 perry static int asc_dma_isactive(struct ncr53c9x_softc *); 84 1.16 simonb 85 1.17 simonb static struct ncr53c9x_glue asc_tc_glue = { 86 1.33 tsutsui asc_read_reg, 87 1.33 tsutsui asc_write_reg, 88 1.33 tsutsui asc_dma_isintr, 89 1.33 tsutsui asc_tc_reset, 90 1.33 tsutsui asc_tc_intr, 91 1.33 tsutsui asc_tc_setup, 92 1.33 tsutsui asc_tc_go, 93 1.33 tsutsui asc_tc_stop, 94 1.33 tsutsui asc_dma_isactive, 95 1.33 tsutsui NULL, 96 1.1 jonathan }; 97 1.1 jonathan 98 1.1 jonathan /* 99 1.16 simonb * Parameters specific to PMAZ-A TC option card. 100 1.1 jonathan */ 101 1.16 simonb #define PMAZ_OFFSET_53C94 0x0 /* from module base */ 102 1.16 simonb #define PMAZ_OFFSET_DMAR 0x40000 /* DMA Address Register */ 103 1.16 simonb #define PMAZ_OFFSET_RAM 0x80000 /* 128KB SRAM buffer */ 104 1.16 simonb #define PMAZ_OFFSET_ROM 0xc0000 /* diagnostic ROM */ 105 1.16 simonb 106 1.16 simonb #define PMAZ_RAM_SIZE 0x20000 /* 128k (32k*32) */ 107 1.33 tsutsui #define PER_TGT_DMA_SIZE ((PMAZ_RAM_SIZE / 7) & ~(sizeof(int) - 1)) 108 1.16 simonb 109 1.16 simonb #define PMAZ_DMAR_WRITE 0x80000000 /* DMA direction bit */ 110 1.16 simonb #define PMAZ_DMAR_MASK 0x1ffff /* 17 bits, 128k */ 111 1.16 simonb #define PMAZ_DMA_ADDR(x) ((unsigned long)(x) & PMAZ_DMAR_MASK) 112 1.1 jonathan 113 1.7 mhitch static int 114 1.33 tsutsui asc_tc_match(device_t parent, cfdata_t cfdata, void *aux) 115 1.1 jonathan { 116 1.16 simonb struct tc_attach_args *d = aux; 117 1.25 perry 118 1.16 simonb if (strncmp("PMAZ-AA ", d->ta_modname, TC_ROM_LLEN)) 119 1.33 tsutsui return 0; 120 1.1 jonathan 121 1.33 tsutsui return 1; 122 1.1 jonathan } 123 1.1 jonathan 124 1.16 simonb static void 125 1.33 tsutsui asc_tc_attach(device_t parent, device_t self, void *aux) 126 1.1 jonathan { 127 1.27 thorpej struct asc_softc *asc = device_private(self); 128 1.16 simonb struct ncr53c9x_softc *sc = &asc->sc_ncr53c9x; 129 1.33 tsutsui struct tc_attach_args *ta = aux; 130 1.1 jonathan 131 1.1 jonathan /* 132 1.16 simonb * Set up glue for MI code early; we use some of it here. 133 1.1 jonathan */ 134 1.33 tsutsui sc->sc_dev = self; 135 1.17 simonb sc->sc_glue = &asc_tc_glue; 136 1.16 simonb asc->sc_bst = ta->ta_memt; 137 1.16 simonb asc->sc_dmat = ta->ta_dmat; 138 1.16 simonb if (bus_space_map(asc->sc_bst, ta->ta_addr, 139 1.33 tsutsui PMAZ_OFFSET_RAM + PMAZ_RAM_SIZE, 0, &asc->sc_bsh)) { 140 1.33 tsutsui aprint_error(": unable to map device\n"); 141 1.16 simonb return; 142 1.16 simonb } 143 1.29 christos asc->sc_base = (void *)ta->ta_addr; /* XXX XXX XXX */ 144 1.16 simonb 145 1.16 simonb tc_intr_establish(parent, ta->ta_cookie, IPL_BIO, ncr53c9x_intr, sc); 146 1.25 perry 147 1.16 simonb sc->sc_id = 7; 148 1.16 simonb sc->sc_freq = (ta->ta_busspeed) ? 25000000 : 12500000; 149 1.16 simonb 150 1.23 tsutsui /* gimme MHz */ 151 1.16 simonb sc->sc_freq /= 1000000; 152 1.1 jonathan 153 1.1 jonathan /* 154 1.16 simonb * XXX More of this should be in ncr53c9x_attach(), but 155 1.16 simonb * XXX should we really poke around the chip that much in 156 1.16 simonb * XXX the MI code? Think about this more... 157 1.1 jonathan */ 158 1.1 jonathan 159 1.1 jonathan /* 160 1.16 simonb * Set up static configuration info. 161 1.1 jonathan */ 162 1.16 simonb sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB; 163 1.16 simonb sc->sc_cfg2 = NCRCFG2_SCSI2; 164 1.16 simonb sc->sc_cfg3 = 0; 165 1.16 simonb sc->sc_rev = NCR_VARIANT_NCR53C94; 166 1.7 mhitch 167 1.7 mhitch /* 168 1.16 simonb * XXX minsync and maxxfer _should_ be set up in MI code, 169 1.16 simonb * XXX but it appears to have some dependency on what sort 170 1.16 simonb * XXX of DMA we're hooked up to, etc. 171 1.7 mhitch */ 172 1.7 mhitch 173 1.7 mhitch /* 174 1.16 simonb * This is the value used to start sync negotiations 175 1.16 simonb * Note that the NCR register "SYNCTP" is programmed 176 1.16 simonb * in "clocks per byte", and has a minimum value of 4. 177 1.16 simonb * The SCSI period used in negotiation is one-fourth 178 1.16 simonb * of the time (in nanoseconds) needed to transfer one byte. 179 1.16 simonb * Since the chip's clock is given in MHz, we have the following 180 1.16 simonb * formula: 4 * period = (1000 / freq) * 4 181 1.7 mhitch */ 182 1.16 simonb sc->sc_minsync = (1000 / sc->sc_freq) * 5 / 4; 183 1.7 mhitch 184 1.16 simonb sc->sc_maxxfer = 64 * 1024; 185 1.1 jonathan 186 1.16 simonb /* Do the common parts of attachment. */ 187 1.16 simonb sc->sc_adapter.adapt_minphys = minphys; 188 1.16 simonb sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request; 189 1.16 simonb ncr53c9x_attach(sc); 190 1.16 simonb } 191 1.16 simonb 192 1.16 simonb static void 193 1.28 thorpej asc_tc_reset(struct ncr53c9x_softc *sc) 194 1.16 simonb { 195 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc; 196 1.16 simonb 197 1.16 simonb asc->sc_active = 0; 198 1.16 simonb } 199 1.16 simonb 200 1.16 simonb static int 201 1.28 thorpej asc_tc_intr(struct ncr53c9x_softc *sc) 202 1.16 simonb { 203 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc; 204 1.16 simonb int trans, resid; 205 1.16 simonb 206 1.16 simonb resid = 0; 207 1.16 simonb if (!asc->sc_ispullup && 208 1.16 simonb (resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) { 209 1.17 simonb NCR_DMA(("asc_tc_intr: empty FIFO of %d ", resid)); 210 1.16 simonb DELAY(1); 211 1.16 simonb } 212 1.16 simonb 213 1.16 simonb resid += NCR_READ_REG(sc, NCR_TCL); 214 1.16 simonb resid += NCR_READ_REG(sc, NCR_TCM) << 8; 215 1.16 simonb 216 1.16 simonb trans = asc->sc_dmasize - resid; 217 1.16 simonb 218 1.16 simonb if (asc->sc_ispullup) 219 1.16 simonb memcpy(asc->sc_target, asc->sc_bounce, trans); 220 1.16 simonb *asc->sc_dmalen -= trans; 221 1.16 simonb *asc->sc_dmaaddr += trans; 222 1.16 simonb asc->sc_active = 0; 223 1.16 simonb 224 1.33 tsutsui return 0; 225 1.16 simonb } 226 1.16 simonb 227 1.16 simonb static int 228 1.33 tsutsui asc_tc_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len, 229 1.28 thorpej int datain, size_t *dmasize) 230 1.16 simonb { 231 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc; 232 1.33 tsutsui uint32_t tc_dmar; 233 1.16 simonb size_t size; 234 1.16 simonb 235 1.33 tsutsui asc->sc_dmaaddr = addr; 236 1.16 simonb asc->sc_dmalen = len; 237 1.16 simonb asc->sc_ispullup = datain; 238 1.16 simonb 239 1.17 simonb NCR_DMA(("asc_tc_setup: start %ld@%p, %s\n", (long)*asc->sc_dmalen, 240 1.33 tsutsui *asc->sc_dmaaddr, datain ? "IN" : "OUT")); 241 1.16 simonb 242 1.16 simonb size = *dmasize; 243 1.16 simonb if (size > PER_TGT_DMA_SIZE) 244 1.16 simonb size = PER_TGT_DMA_SIZE; 245 1.16 simonb *dmasize = asc->sc_dmasize = size; 246 1.16 simonb 247 1.17 simonb NCR_DMA(("asc_tc_setup: dmasize = %ld\n", (long)asc->sc_dmasize)); 248 1.16 simonb 249 1.16 simonb asc->sc_bounce = asc->sc_base + PMAZ_OFFSET_RAM; 250 1.16 simonb asc->sc_bounce += PER_TGT_DMA_SIZE * 251 1.16 simonb sc->sc_nexus->xs->xs_periph->periph_target; 252 1.16 simonb asc->sc_target = *addr; 253 1.16 simonb 254 1.16 simonb if (!asc->sc_ispullup) 255 1.16 simonb memcpy(asc->sc_bounce, asc->sc_target, size); 256 1.16 simonb 257 1.16 simonb #if 1 258 1.16 simonb if (asc->sc_ispullup) 259 1.16 simonb tc_dmar = PMAZ_DMA_ADDR(asc->sc_bounce); 260 1.16 simonb else 261 1.16 simonb tc_dmar = PMAZ_DMAR_WRITE | PMAZ_DMA_ADDR(asc->sc_bounce); 262 1.16 simonb bus_space_write_4(asc->sc_bst, asc->sc_bsh, PMAZ_OFFSET_DMAR, tc_dmar); 263 1.16 simonb asc->sc_active = 1; 264 1.16 simonb #endif 265 1.33 tsutsui return 0; 266 1.16 simonb } 267 1.16 simonb 268 1.16 simonb static void 269 1.28 thorpej asc_tc_go(struct ncr53c9x_softc *sc) 270 1.16 simonb { 271 1.16 simonb #if 0 272 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc; 273 1.33 tsutsui uint32_t tc_dmar; 274 1.1 jonathan 275 1.16 simonb if (asc->sc_ispullup) 276 1.16 simonb tc_dmar = PMAZ_DMA_ADDR(asc->sc_bounce); 277 1.16 simonb else 278 1.16 simonb tc_dmar = PMAZ_DMAR_WRITE | PMAZ_DMA_ADDR(asc->sc_bounce); 279 1.16 simonb bus_space_write_4(asc->sc_bst, asc->sc_bsh, PMAZ_OFFSET_DMAR, tc_dmar); 280 1.16 simonb asc->sc_active = 1; 281 1.16 simonb #endif 282 1.1 jonathan } 283 1.1 jonathan 284 1.16 simonb /* NEVER CALLED BY MI 53C9x ENGINE INDEED */ 285 1.16 simonb static void 286 1.28 thorpej asc_tc_stop(struct ncr53c9x_softc *sc) 287 1.16 simonb { 288 1.16 simonb #if 0 289 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc; 290 1.16 simonb 291 1.16 simonb if (asc->sc_ispullup) 292 1.16 simonb memcpy(asc->sc_target, asc->sc_bounce, asc->sc_dmasize); 293 1.16 simonb asc->sc_active = 0; 294 1.16 simonb #endif 295 1.16 simonb } 296 1.1 jonathan 297 1.1 jonathan /* 298 1.16 simonb * Glue functions. 299 1.1 jonathan */ 300 1.33 tsutsui static uint8_t 301 1.28 thorpej asc_read_reg(struct ncr53c9x_softc *sc, int reg) 302 1.16 simonb { 303 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc; 304 1.16 simonb 305 1.33 tsutsui return bus_space_read_4(asc->sc_bst, asc->sc_bsh, 306 1.33 tsutsui reg * sizeof(uint32_t)) & 0xff; 307 1.16 simonb } 308 1.16 simonb 309 1.16 simonb static void 310 1.33 tsutsui asc_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val) 311 1.16 simonb { 312 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc; 313 1.16 simonb 314 1.16 simonb bus_space_write_4(asc->sc_bst, asc->sc_bsh, 315 1.33 tsutsui reg * sizeof(uint32_t), val); 316 1.16 simonb } 317 1.16 simonb 318 1.16 simonb static int 319 1.28 thorpej asc_dma_isintr(struct ncr53c9x_softc *sc) 320 1.16 simonb { 321 1.33 tsutsui 322 1.33 tsutsui return (NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT) != 0; 323 1.16 simonb } 324 1.16 simonb 325 1.7 mhitch static int 326 1.28 thorpej asc_dma_isactive(struct ncr53c9x_softc *sc) 327 1.1 jonathan { 328 1.16 simonb struct asc_softc *asc = (struct asc_softc *)sc; 329 1.1 jonathan 330 1.33 tsutsui return asc->sc_active; 331 1.1 jonathan } 332