1 /* $NetBSD: asc_tc.c,v 1.33 2008/04/13 04:55:53 tsutsui 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> 40 __KERNEL_RCSID(0, "$NetBSD: asc_tc.c,v 1.33 2008/04/13 04:55:53 tsutsui Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/device.h> 45 #include <sys/buf.h> 46 47 #include <dev/scsipi/scsi_all.h> 48 #include <dev/scsipi/scsipi_all.h> 49 #include <dev/scsipi/scsiconf.h> 50 #include <dev/scsipi/scsi_message.h> 51 52 #include <sys/bus.h> 53 54 #include <dev/ic/ncr53c9xreg.h> 55 #include <dev/ic/ncr53c9xvar.h> 56 57 #include <dev/tc/tcvar.h> 58 59 struct asc_softc { 60 struct ncr53c9x_softc sc_ncr53c9x; /* glue to MI code */ 61 bus_space_tag_t sc_bst; 62 bus_space_handle_t sc_bsh; 63 bus_dma_tag_t sc_dmat; 64 bus_dmamap_t sc_dmamap; 65 uint8_t **sc_dmaaddr; 66 size_t *sc_dmalen; 67 size_t sc_dmasize; 68 int sc_active; /* DMA active ? */ 69 int sc_ispullup; /* DMA into main memory? */ 70 71 /* XXX XXX XXX */ 72 char *sc_base, *sc_bounce, *sc_target; 73 }; 74 75 static int asc_tc_match(device_t, cfdata_t, void *); 76 static void asc_tc_attach(device_t, device_t, void *); 77 78 CFATTACH_DECL_NEW(asc_tc, sizeof(struct asc_softc), 79 asc_tc_match, asc_tc_attach, NULL, NULL); 80 81 static uint8_t asc_read_reg(struct ncr53c9x_softc *, int); 82 static void asc_write_reg(struct ncr53c9x_softc *, int, uint8_t); 83 static int asc_dma_isintr(struct ncr53c9x_softc *); 84 static void asc_tc_reset(struct ncr53c9x_softc *); 85 static int asc_tc_intr(struct ncr53c9x_softc *); 86 static int asc_tc_setup(struct ncr53c9x_softc *, uint8_t **, 87 size_t *, int, size_t *); 88 static void asc_tc_go(struct ncr53c9x_softc *); 89 static void asc_tc_stop(struct ncr53c9x_softc *); 90 static int asc_dma_isactive(struct ncr53c9x_softc *); 91 92 static struct ncr53c9x_glue asc_tc_glue = { 93 asc_read_reg, 94 asc_write_reg, 95 asc_dma_isintr, 96 asc_tc_reset, 97 asc_tc_intr, 98 asc_tc_setup, 99 asc_tc_go, 100 asc_tc_stop, 101 asc_dma_isactive, 102 NULL, 103 }; 104 105 /* 106 * Parameters specific to PMAZ-A TC option card. 107 */ 108 #define PMAZ_OFFSET_53C94 0x0 /* from module base */ 109 #define PMAZ_OFFSET_DMAR 0x40000 /* DMA Address Register */ 110 #define PMAZ_OFFSET_RAM 0x80000 /* 128KB SRAM buffer */ 111 #define PMAZ_OFFSET_ROM 0xc0000 /* diagnostic ROM */ 112 113 #define PMAZ_RAM_SIZE 0x20000 /* 128k (32k*32) */ 114 #define PER_TGT_DMA_SIZE ((PMAZ_RAM_SIZE / 7) & ~(sizeof(int) - 1)) 115 116 #define PMAZ_DMAR_WRITE 0x80000000 /* DMA direction bit */ 117 #define PMAZ_DMAR_MASK 0x1ffff /* 17 bits, 128k */ 118 #define PMAZ_DMA_ADDR(x) ((unsigned long)(x) & PMAZ_DMAR_MASK) 119 120 static int 121 asc_tc_match(device_t parent, cfdata_t cfdata, void *aux) 122 { 123 struct tc_attach_args *d = aux; 124 125 if (strncmp("PMAZ-AA ", d->ta_modname, TC_ROM_LLEN)) 126 return 0; 127 128 return 1; 129 } 130 131 static void 132 asc_tc_attach(device_t parent, device_t self, void *aux) 133 { 134 struct asc_softc *asc = device_private(self); 135 struct ncr53c9x_softc *sc = &asc->sc_ncr53c9x; 136 struct tc_attach_args *ta = aux; 137 138 /* 139 * Set up glue for MI code early; we use some of it here. 140 */ 141 sc->sc_dev = self; 142 sc->sc_glue = &asc_tc_glue; 143 asc->sc_bst = ta->ta_memt; 144 asc->sc_dmat = ta->ta_dmat; 145 if (bus_space_map(asc->sc_bst, ta->ta_addr, 146 PMAZ_OFFSET_RAM + PMAZ_RAM_SIZE, 0, &asc->sc_bsh)) { 147 aprint_error(": unable to map device\n"); 148 return; 149 } 150 asc->sc_base = (void *)ta->ta_addr; /* XXX XXX XXX */ 151 152 tc_intr_establish(parent, ta->ta_cookie, IPL_BIO, ncr53c9x_intr, sc); 153 154 sc->sc_id = 7; 155 sc->sc_freq = (ta->ta_busspeed) ? 25000000 : 12500000; 156 157 /* gimme MHz */ 158 sc->sc_freq /= 1000000; 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 191 sc->sc_maxxfer = 64 * 1024; 192 193 /* Do the common parts of attachment. */ 194 sc->sc_adapter.adapt_minphys = minphys; 195 sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request; 196 ncr53c9x_attach(sc); 197 } 198 199 static void 200 asc_tc_reset(struct ncr53c9x_softc *sc) 201 { 202 struct asc_softc *asc = (struct asc_softc *)sc; 203 204 asc->sc_active = 0; 205 } 206 207 static int 208 asc_tc_intr(struct ncr53c9x_softc *sc) 209 { 210 struct asc_softc *asc = (struct asc_softc *)sc; 211 int trans, resid; 212 213 resid = 0; 214 if (!asc->sc_ispullup && 215 (resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) { 216 NCR_DMA(("asc_tc_intr: empty FIFO of %d ", resid)); 217 DELAY(1); 218 } 219 220 resid += NCR_READ_REG(sc, NCR_TCL); 221 resid += NCR_READ_REG(sc, NCR_TCM) << 8; 222 223 trans = asc->sc_dmasize - resid; 224 225 if (asc->sc_ispullup) 226 memcpy(asc->sc_target, asc->sc_bounce, trans); 227 *asc->sc_dmalen -= trans; 228 *asc->sc_dmaaddr += trans; 229 asc->sc_active = 0; 230 231 return 0; 232 } 233 234 static int 235 asc_tc_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len, 236 int datain, size_t *dmasize) 237 { 238 struct asc_softc *asc = (struct asc_softc *)sc; 239 uint32_t tc_dmar; 240 size_t size; 241 242 asc->sc_dmaaddr = addr; 243 asc->sc_dmalen = len; 244 asc->sc_ispullup = datain; 245 246 NCR_DMA(("asc_tc_setup: start %ld@%p, %s\n", (long)*asc->sc_dmalen, 247 *asc->sc_dmaaddr, datain ? "IN" : "OUT")); 248 249 size = *dmasize; 250 if (size > PER_TGT_DMA_SIZE) 251 size = PER_TGT_DMA_SIZE; 252 *dmasize = asc->sc_dmasize = size; 253 254 NCR_DMA(("asc_tc_setup: dmasize = %ld\n", (long)asc->sc_dmasize)); 255 256 asc->sc_bounce = asc->sc_base + PMAZ_OFFSET_RAM; 257 asc->sc_bounce += PER_TGT_DMA_SIZE * 258 sc->sc_nexus->xs->xs_periph->periph_target; 259 asc->sc_target = *addr; 260 261 if (!asc->sc_ispullup) 262 memcpy(asc->sc_bounce, asc->sc_target, size); 263 264 #if 1 265 if (asc->sc_ispullup) 266 tc_dmar = PMAZ_DMA_ADDR(asc->sc_bounce); 267 else 268 tc_dmar = PMAZ_DMAR_WRITE | PMAZ_DMA_ADDR(asc->sc_bounce); 269 bus_space_write_4(asc->sc_bst, asc->sc_bsh, PMAZ_OFFSET_DMAR, tc_dmar); 270 asc->sc_active = 1; 271 #endif 272 return 0; 273 } 274 275 static void 276 asc_tc_go(struct ncr53c9x_softc *sc) 277 { 278 #if 0 279 struct asc_softc *asc = (struct asc_softc *)sc; 280 uint32_t tc_dmar; 281 282 if (asc->sc_ispullup) 283 tc_dmar = PMAZ_DMA_ADDR(asc->sc_bounce); 284 else 285 tc_dmar = PMAZ_DMAR_WRITE | PMAZ_DMA_ADDR(asc->sc_bounce); 286 bus_space_write_4(asc->sc_bst, asc->sc_bsh, PMAZ_OFFSET_DMAR, tc_dmar); 287 asc->sc_active = 1; 288 #endif 289 } 290 291 /* NEVER CALLED BY MI 53C9x ENGINE INDEED */ 292 static void 293 asc_tc_stop(struct ncr53c9x_softc *sc) 294 { 295 #if 0 296 struct asc_softc *asc = (struct asc_softc *)sc; 297 298 if (asc->sc_ispullup) 299 memcpy(asc->sc_target, asc->sc_bounce, asc->sc_dmasize); 300 asc->sc_active = 0; 301 #endif 302 } 303 304 /* 305 * Glue functions. 306 */ 307 static uint8_t 308 asc_read_reg(struct ncr53c9x_softc *sc, int reg) 309 { 310 struct asc_softc *asc = (struct asc_softc *)sc; 311 312 return bus_space_read_4(asc->sc_bst, asc->sc_bsh, 313 reg * sizeof(uint32_t)) & 0xff; 314 } 315 316 static void 317 asc_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val) 318 { 319 struct asc_softc *asc = (struct asc_softc *)sc; 320 321 bus_space_write_4(asc->sc_bst, asc->sc_bsh, 322 reg * sizeof(uint32_t), val); 323 } 324 325 static int 326 asc_dma_isintr(struct ncr53c9x_softc *sc) 327 { 328 329 return (NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT) != 0; 330 } 331 332 static int 333 asc_dma_isactive(struct ncr53c9x_softc *sc) 334 { 335 struct asc_softc *asc = (struct asc_softc *)sc; 336 337 return asc->sc_active; 338 } 339