1 1.30 tsutsui /* $NetBSD: esp.c,v 1.30 2024/12/20 23:52:00 tsutsui Exp $ */ 2 1.1 jeremy 3 1.3 gwr /*- 4 1.3 gwr * Copyright (c) 1997 The NetBSD Foundation, Inc. 5 1.3 gwr * All rights reserved. 6 1.1 jeremy * 7 1.3 gwr * This code is derived from software contributed to The NetBSD Foundation 8 1.3 gwr * by Jeremy Cooper and Gordon W. Ross 9 1.1 jeremy * 10 1.1 jeremy * Redistribution and use in source and binary forms, with or without 11 1.1 jeremy * modification, are permitted provided that the following conditions 12 1.1 jeremy * are met: 13 1.1 jeremy * 1. Redistributions of source code must retain the above copyright 14 1.1 jeremy * notice, this list of conditions and the following disclaimer. 15 1.1 jeremy * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 jeremy * notice, this list of conditions and the following disclaimer in the 17 1.1 jeremy * documentation and/or other materials provided with the distribution. 18 1.1 jeremy * 19 1.3 gwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.3 gwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.3 gwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.3 gwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.3 gwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.3 gwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.3 gwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.3 gwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.3 gwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.3 gwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 jeremy * POSSIBILITY OF SUCH DAMAGE. 30 1.1 jeremy */ 31 1.1 jeremy 32 1.1 jeremy /* 33 1.3 gwr * "Front end" glue for the ncr53c9x chip, formerly known as the 34 1.3 gwr * Emulex SCSI Processor (ESP) which is what we actually have. 35 1.1 jeremy */ 36 1.18 lukem 37 1.18 lukem #include <sys/cdefs.h> 38 1.30 tsutsui __KERNEL_RCSID(0, "$NetBSD: esp.c,v 1.30 2024/12/20 23:52:00 tsutsui Exp $"); 39 1.1 jeremy 40 1.1 jeremy #include <sys/types.h> 41 1.1 jeremy #include <sys/param.h> 42 1.1 jeremy #include <sys/systm.h> 43 1.1 jeremy #include <sys/kernel.h> 44 1.1 jeremy #include <sys/errno.h> 45 1.1 jeremy #include <sys/device.h> 46 1.1 jeremy #include <sys/buf.h> 47 1.1 jeremy 48 1.5 bouyer #include <dev/scsipi/scsi_all.h> 49 1.5 bouyer #include <dev/scsipi/scsipi_all.h> 50 1.5 bouyer #include <dev/scsipi/scsiconf.h> 51 1.5 bouyer #include <dev/scsipi/scsi_message.h> 52 1.1 jeremy 53 1.1 jeremy #include <machine/autoconf.h> 54 1.24 tsutsui #include <machine/bus.h> 55 1.3 gwr 56 1.3 gwr #include <dev/ic/ncr53c9xreg.h> 57 1.3 gwr #include <dev/ic/ncr53c9xvar.h> 58 1.3 gwr 59 1.7 gwr #include <sun3/dev/dmareg.h> 60 1.7 gwr #include <sun3/dev/dmavar.h> 61 1.1 jeremy 62 1.1 jeremy #define ESP_REG_SIZE (12*4) 63 1.1 jeremy 64 1.3 gwr struct esp_softc { 65 1.3 gwr struct ncr53c9x_softc sc_ncr53c9x; /* glue to MI code */ 66 1.24 tsutsui bus_space_tag_t sc_bst; /* bus space tag */ 67 1.24 tsutsui bus_space_handle_t sc_bsh; /* bus space handle */ 68 1.3 gwr struct dma_softc *sc_dma; /* pointer to my dma */ 69 1.3 gwr }; 70 1.1 jeremy 71 1.27 tsutsui static int espmatch(device_t, cfdata_t, void *); 72 1.27 tsutsui static void espattach(device_t, device_t, void *); 73 1.1 jeremy 74 1.27 tsutsui CFATTACH_DECL_NEW(esp, sizeof(struct esp_softc), 75 1.17 thorpej espmatch, espattach, NULL, NULL); 76 1.1 jeremy 77 1.3 gwr /* 78 1.3 gwr * Functions and the switch for the MI code. 79 1.3 gwr */ 80 1.27 tsutsui static uint8_t esp_read_reg(struct ncr53c9x_softc *, int); 81 1.27 tsutsui static void esp_write_reg(struct ncr53c9x_softc *, int, uint8_t); 82 1.19 chs static int esp_dma_isintr(struct ncr53c9x_softc *); 83 1.19 chs static void esp_dma_reset(struct ncr53c9x_softc *); 84 1.19 chs static int esp_dma_intr(struct ncr53c9x_softc *); 85 1.27 tsutsui static int esp_dma_setup(struct ncr53c9x_softc *, uint8_t **, size_t *, 86 1.27 tsutsui int, size_t *); 87 1.19 chs static void esp_dma_go(struct ncr53c9x_softc *); 88 1.19 chs static void esp_dma_stop(struct ncr53c9x_softc *); 89 1.19 chs static int esp_dma_isactive(struct ncr53c9x_softc *); 90 1.3 gwr 91 1.3 gwr static struct ncr53c9x_glue esp_glue = { 92 1.3 gwr esp_read_reg, 93 1.3 gwr esp_write_reg, 94 1.3 gwr esp_dma_isintr, 95 1.3 gwr esp_dma_reset, 96 1.3 gwr esp_dma_intr, 97 1.3 gwr esp_dma_setup, 98 1.3 gwr esp_dma_go, 99 1.3 gwr esp_dma_stop, 100 1.3 gwr esp_dma_isactive, 101 1.3 gwr NULL, /* gl_clear_latched_intr */ 102 1.3 gwr }; 103 1.3 gwr 104 1.30 tsutsui static int 105 1.27 tsutsui espmatch(device_t parent, struct cfdata *cf, void *aux) 106 1.1 jeremy { 107 1.1 jeremy struct confargs *ca = aux; 108 1.1 jeremy 109 1.1 jeremy /* 110 1.1 jeremy * Check for the esp registers. 111 1.1 jeremy */ 112 1.1 jeremy if (bus_peek(ca->ca_bustype, 113 1.3 gwr ca->ca_paddr + (NCR_STAT * 4), 1) == -1) 114 1.27 tsutsui return 0; 115 1.1 jeremy 116 1.1 jeremy /* If default ipl, fill it in. */ 117 1.1 jeremy if (ca->ca_intpri == -1) 118 1.1 jeremy ca->ca_intpri = 2; 119 1.1 jeremy 120 1.27 tsutsui return 1; 121 1.1 jeremy } 122 1.1 jeremy 123 1.30 tsutsui static void 124 1.27 tsutsui espattach(device_t parent, device_t self, void *aux) 125 1.1 jeremy { 126 1.27 tsutsui struct esp_softc *esc = device_private(self); 127 1.27 tsutsui struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x; 128 1.10 gwr struct confargs *ca = aux; 129 1.1 jeremy 130 1.1 jeremy /* 131 1.3 gwr * Set up glue for MI code early; we use some of it here. 132 1.1 jeremy */ 133 1.27 tsutsui sc->sc_dev = self; 134 1.3 gwr sc->sc_glue = &esp_glue; 135 1.3 gwr 136 1.3 gwr /* 137 1.24 tsutsui * Map the ESP registers. 138 1.3 gwr */ 139 1.24 tsutsui esc->sc_bst = ca->ca_bustag; 140 1.24 tsutsui if (bus_space_map(esc->sc_bst, ca->ca_paddr, ESP_REG_SIZE, 0, 141 1.24 tsutsui &esc->sc_bsh) != 0) { 142 1.27 tsutsui aprint_error(": can't map register\n"); 143 1.24 tsutsui return; 144 1.24 tsutsui } 145 1.1 jeremy 146 1.3 gwr /* Other settings */ 147 1.1 jeremy sc->sc_id = 7; 148 1.22 lukem sc->sc_freq = 20; /* The 3/80 esp runs at 20 MHz */ 149 1.1 jeremy 150 1.1 jeremy /* 151 1.3 gwr * Hook up the DMA driver. 152 1.3 gwr */ 153 1.27 tsutsui esc->sc_dma = espdmafind(device_unit(self)); 154 1.25 tsutsui esc->sc_dma->sc_client = sc; /* Point back to us */ 155 1.3 gwr 156 1.3 gwr /* 157 1.3 gwr * XXX More of this should be in ncr53c9x_attach(), but 158 1.3 gwr * XXX should we really poke around the chip that much in 159 1.3 gwr * XXX the MI code? Think about this more... 160 1.3 gwr */ 161 1.3 gwr 162 1.3 gwr /* 163 1.1 jeremy * It is necessary to try to load the 2nd config register here, 164 1.3 gwr * to find out what rev the esp chip is, else the ncr53c9x_reset 165 1.1 jeremy * will not set up the defaults correctly. 166 1.1 jeremy */ 167 1.3 gwr sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB; 168 1.3 gwr sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE; 169 1.3 gwr sc->sc_cfg3 = NCRCFG3_CDB; 170 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2); 171 1.3 gwr 172 1.3 gwr if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) != 173 1.3 gwr (NCRCFG2_SCSI2 | NCRCFG2_RPE)) { 174 1.3 gwr sc->sc_rev = NCR_VARIANT_ESP100; 175 1.1 jeremy } else { 176 1.3 gwr sc->sc_cfg2 = NCRCFG2_SCSI2; 177 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2); 178 1.1 jeremy sc->sc_cfg3 = 0; 179 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3); 180 1.3 gwr sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK); 181 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3); 182 1.3 gwr if (NCR_READ_REG(sc, NCR_CFG3) != 183 1.3 gwr (NCRCFG3_CDB | NCRCFG3_FCLK)) { 184 1.3 gwr sc->sc_rev = NCR_VARIANT_ESP100A; 185 1.1 jeremy } else { 186 1.3 gwr /* NCRCFG2_FE enables > 64K transfers */ 187 1.3 gwr sc->sc_cfg2 |= NCRCFG2_FE; 188 1.1 jeremy sc->sc_cfg3 = 0; 189 1.3 gwr NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3); 190 1.3 gwr sc->sc_rev = NCR_VARIANT_ESP200; 191 1.1 jeremy } 192 1.1 jeremy } 193 1.1 jeremy 194 1.1 jeremy /* 195 1.3 gwr * XXX minsync and maxxfer _should_ be set up in MI code, 196 1.3 gwr * XXX but it appears to have some dependency on what sort 197 1.3 gwr * XXX of DMA we're hooked up to, etc. 198 1.1 jeremy */ 199 1.1 jeremy 200 1.1 jeremy /* 201 1.1 jeremy * This is the value used to start sync negotiations 202 1.3 gwr * Note that the NCR register "SYNCTP" is programmed 203 1.1 jeremy * in "clocks per byte", and has a minimum value of 4. 204 1.1 jeremy * The SCSI period used in negotiation is one-fourth 205 1.1 jeremy * of the time (in nanoseconds) needed to transfer one byte. 206 1.1 jeremy * Since the chip's clock is given in MHz, we have the following 207 1.1 jeremy * formula: 4 * period = (1000 / freq) * 4 208 1.1 jeremy */ 209 1.1 jeremy sc->sc_minsync = 1000 / sc->sc_freq; 210 1.1 jeremy 211 1.1 jeremy /* 212 1.1 jeremy * Alas, we must now modify the value a bit, because it's 213 1.1 jeremy * only valid when can switch on FASTCLK and FASTSCSI bits 214 1.1 jeremy * in config register 3... 215 1.1 jeremy */ 216 1.1 jeremy switch (sc->sc_rev) { 217 1.3 gwr case NCR_VARIANT_ESP100: 218 1.3 gwr sc->sc_maxxfer = 64 * 1024; 219 1.1 jeremy sc->sc_minsync = 0; /* No synch on old chip? */ 220 1.1 jeremy break; 221 1.3 gwr 222 1.3 gwr case NCR_VARIANT_ESP100A: 223 1.1 jeremy sc->sc_maxxfer = 64 * 1024; 224 1.3 gwr /* Min clocks/byte is 5 */ 225 1.3 gwr sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5); 226 1.1 jeremy break; 227 1.3 gwr 228 1.3 gwr case NCR_VARIANT_ESP200: 229 1.1 jeremy sc->sc_maxxfer = 16 * 1024 * 1024; 230 1.1 jeremy /* XXX - do actually set FAST* bits */ 231 1.3 gwr break; 232 1.1 jeremy } 233 1.1 jeremy 234 1.29 andvar /* and the interrupts */ 235 1.12 nisimura isr_add_autovect(ncr53c9x_intr, sc, ca->ca_intpri); 236 1.11 cgd evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL, 237 1.27 tsutsui device_xname(self), "intr"); 238 1.1 jeremy 239 1.3 gwr /* Do the common parts of attachment. */ 240 1.14 bouyer sc->sc_adapter.adapt_minphys = minphys; 241 1.14 bouyer sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request; 242 1.14 bouyer ncr53c9x_attach(sc); 243 1.10 gwr 244 1.10 gwr /* Turn on target selection using the `dma' method */ 245 1.20 tsutsui sc->sc_features |= NCR_F_DMASELECT; 246 1.1 jeremy } 247 1.1 jeremy 248 1.1 jeremy 249 1.1 jeremy /* 250 1.3 gwr * Glue functions. 251 1.1 jeremy */ 252 1.1 jeremy 253 1.27 tsutsui uint8_t 254 1.19 chs esp_read_reg(struct ncr53c9x_softc *sc, int reg) 255 1.1 jeremy { 256 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc; 257 1.1 jeremy 258 1.24 tsutsui return bus_space_read_1(esc->sc_bst, esc->sc_bsh, reg * 4); 259 1.1 jeremy } 260 1.1 jeremy 261 1.1 jeremy void 262 1.27 tsutsui esp_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val) 263 1.1 jeremy { 264 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc; 265 1.1 jeremy 266 1.24 tsutsui bus_space_write_1(esc->sc_bst, esc->sc_bsh, reg * 4, val); 267 1.1 jeremy } 268 1.1 jeremy 269 1.30 tsutsui int 270 1.19 chs esp_dma_isintr(struct ncr53c9x_softc *sc) 271 1.1 jeremy { 272 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc; 273 1.1 jeremy 274 1.25 tsutsui return DMA_ISINTR(esc->sc_dma); 275 1.1 jeremy } 276 1.1 jeremy 277 1.30 tsutsui void 278 1.19 chs esp_dma_reset(struct ncr53c9x_softc *sc) 279 1.1 jeremy { 280 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc; 281 1.1 jeremy 282 1.3 gwr dma_reset(esc->sc_dma); 283 1.1 jeremy } 284 1.1 jeremy 285 1.30 tsutsui int 286 1.19 chs esp_dma_intr(struct ncr53c9x_softc *sc) 287 1.1 jeremy { 288 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc; 289 1.1 jeremy 290 1.25 tsutsui return espdmaintr(esc->sc_dma); 291 1.1 jeremy } 292 1.1 jeremy 293 1.30 tsutsui int 294 1.27 tsutsui esp_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len, 295 1.27 tsutsui int datain, size_t *dmasize) 296 1.1 jeremy { 297 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc; 298 1.1 jeremy 299 1.25 tsutsui return dma_setup(esc->sc_dma, addr, len, datain, dmasize); 300 1.1 jeremy } 301 1.1 jeremy 302 1.30 tsutsui void 303 1.19 chs esp_dma_go(struct ncr53c9x_softc *sc) 304 1.1 jeremy { 305 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc; 306 1.1 jeremy 307 1.25 tsutsui DMA_GO(esc->sc_dma); 308 1.1 jeremy } 309 1.1 jeremy 310 1.30 tsutsui void 311 1.19 chs esp_dma_stop(struct ncr53c9x_softc *sc) 312 1.1 jeremy { 313 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc; 314 1.1 jeremy 315 1.25 tsutsui DMA_STOP(esc->sc_dma); 316 1.1 jeremy } 317 1.1 jeremy 318 1.30 tsutsui int 319 1.19 chs esp_dma_isactive(struct ncr53c9x_softc *sc) 320 1.1 jeremy { 321 1.3 gwr struct esp_softc *esc = (struct esp_softc *)sc; 322 1.1 jeremy 323 1.25 tsutsui return DMA_ISACTIVE(esc->sc_dma); 324 1.1 jeremy } 325