1 /* $NetBSD: uha_eisa.c,v 1.18 2002/04/05 18:27:48 bouyer Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 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: uha_eisa.c,v 1.18 2002/04/05 18:27:48 bouyer Exp $"); 41 42 #include "opt_ddb.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/device.h> 47 #include <sys/kernel.h> 48 #include <sys/proc.h> 49 #include <sys/user.h> 50 51 #include <machine/bus.h> 52 #include <machine/intr.h> 53 54 #include <dev/scsipi/scsi_all.h> 55 #include <dev/scsipi/scsipi_all.h> 56 #include <dev/scsipi/scsiconf.h> 57 58 #include <dev/eisa/eisavar.h> 59 #include <dev/eisa/eisadevs.h> 60 61 #include <dev/ic/uhareg.h> 62 #include <dev/ic/uhavar.h> 63 64 #define UHA_EISA_SLOT_OFFSET 0xc80 65 #define UHA_EISA_IOSIZE 0x020 66 67 int uha_eisa_match __P((struct device *, struct cfdata *, void *)); 68 void uha_eisa_attach __P((struct device *, struct device *, void *)); 69 70 struct cfattach uha_eisa_ca = { 71 sizeof(struct uha_softc), uha_eisa_match, uha_eisa_attach 72 }; 73 74 #ifndef DDB 75 #define Debugger() panic("should call debugger here (uha_eisa.c)") 76 #endif /* ! DDB */ 77 78 int u24_find __P((bus_space_tag_t, bus_space_handle_t, 79 struct uha_probe_data *)); 80 void u24_start_mbox __P((struct uha_softc *, struct uha_mscp *)); 81 int u24_poll __P((struct uha_softc *, struct scsipi_xfer *, int)); 82 int u24_intr __P((void *)); 83 void u24_init __P((struct uha_softc *)); 84 85 /* 86 * Check the slots looking for a board we recognise 87 * If we find one, note it's address (slot) and call 88 * the actual probe routine to check it out. 89 */ 90 int 91 uha_eisa_match(parent, match, aux) 92 struct device *parent; 93 struct cfdata *match; 94 void *aux; 95 { 96 struct eisa_attach_args *ea = aux; 97 bus_space_tag_t iot = ea->ea_iot; 98 bus_space_handle_t ioh; 99 int rv; 100 101 /* must match one of our known ID strings */ 102 if (strncmp(ea->ea_idstring, "USC024", 6)) 103 return (0); 104 105 if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) + 106 UHA_EISA_SLOT_OFFSET, UHA_EISA_IOSIZE, 0, &ioh)) 107 return (0); 108 109 rv = u24_find(iot, ioh, NULL); 110 111 bus_space_unmap(iot, ioh, UHA_EISA_IOSIZE); 112 113 return (rv); 114 } 115 116 /* 117 * Attach all the sub-devices we can find 118 */ 119 void 120 uha_eisa_attach(parent, self, aux) 121 struct device *parent, *self; 122 void *aux; 123 { 124 struct eisa_attach_args *ea = aux; 125 struct uha_softc *sc = (void *)self; 126 bus_space_tag_t iot = ea->ea_iot; 127 bus_dma_tag_t dmat = ea->ea_dmat; 128 bus_space_handle_t ioh; 129 struct uha_probe_data upd; 130 eisa_chipset_tag_t ec = ea->ea_ec; 131 eisa_intr_handle_t ih; 132 const char *model, *intrstr; 133 134 if (!strncmp(ea->ea_idstring, "USC024", 6)) 135 model = EISA_PRODUCT_USC0240; 136 else 137 model = "unknown model!"; 138 printf(": %s\n", model); 139 140 if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) + 141 UHA_EISA_SLOT_OFFSET, UHA_EISA_IOSIZE, 0, &ioh)) 142 panic("uha_eisa_attach: could not map I/O addresses"); 143 144 sc->sc_iot = iot; 145 sc->sc_ioh = ioh; 146 sc->sc_dmat = dmat; 147 if (!u24_find(iot, ioh, &upd)) 148 panic("uha_eisa_attach: u24_find failed!"); 149 150 sc->sc_dmaflags = 0; 151 152 if (eisa_intr_map(ec, upd.sc_irq, &ih)) { 153 printf("%s: couldn't map interrupt (%d)\n", 154 sc->sc_dev.dv_xname, upd.sc_irq); 155 return; 156 } 157 intrstr = eisa_intr_string(ec, ih); 158 sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO, 159 u24_intr, sc); 160 if (sc->sc_ih == NULL) { 161 printf("%s: couldn't establish interrupt", 162 sc->sc_dev.dv_xname); 163 if (intrstr != NULL) 164 printf(" at %s", intrstr); 165 printf("\n"); 166 return; 167 } 168 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); 169 170 /* Save function pointers for later use. */ 171 sc->start_mbox = u24_start_mbox; 172 sc->poll = u24_poll; 173 sc->init = u24_init; 174 175 uha_attach(sc, &upd); 176 } 177 178 int 179 u24_find(iot, ioh, sc) 180 bus_space_tag_t iot; 181 bus_space_handle_t ioh; 182 struct uha_probe_data *sc; 183 { 184 u_int8_t config0, config1, config2; 185 int irq, drq; 186 int resetcount = 4000; /* 4 secs? */ 187 188 config0 = bus_space_read_1(iot, ioh, U24_CONFIG + 0); 189 config1 = bus_space_read_1(iot, ioh, U24_CONFIG + 1); 190 config2 = bus_space_read_1(iot, ioh, U24_CONFIG + 2); 191 if ((config0 & U24_MAGIC1) == 0 || 192 (config1 & U24_MAGIC2) == 0) 193 return (0); 194 195 drq = -1; 196 197 switch (config0 & U24_IRQ_MASK) { 198 case U24_IRQ10: 199 irq = 10; 200 break; 201 case U24_IRQ11: 202 irq = 11; 203 break; 204 case U24_IRQ14: 205 irq = 14; 206 break; 207 case U24_IRQ15: 208 irq = 15; 209 break; 210 default: 211 printf("u24_find: illegal irq setting %x\n", 212 config0 & U24_IRQ_MASK); 213 return (0); 214 } 215 216 bus_space_write_1(iot, ioh, U24_LINT, UHA_ASRST); 217 218 while (--resetcount) { 219 if (bus_space_read_1(iot, ioh, U24_LINT)) 220 break; 221 delay(1000); /* 1 mSec per loop */ 222 } 223 if (!resetcount) { 224 printf("u24_find: board timed out during reset\n"); 225 return (0); 226 } 227 228 /* if we want to fill in softc, do so now */ 229 if (sc) { 230 sc->sc_irq = irq; 231 sc->sc_drq = drq; 232 sc->sc_scsi_dev = config2 & U24_HOSTID_MASK; 233 } 234 235 return (1); 236 } 237 238 void 239 u24_start_mbox(sc, mscp) 240 struct uha_softc *sc; 241 struct uha_mscp *mscp; 242 { 243 bus_space_tag_t iot = sc->sc_iot; 244 bus_space_handle_t ioh = sc->sc_ioh; 245 int spincount = 100000; /* 1s should be enough */ 246 247 while (--spincount) { 248 if ((bus_space_read_1(iot, ioh, U24_LINT) & U24_LDIP) == 0) 249 break; 250 delay(100); 251 } 252 if (!spincount) { 253 printf("%s: uha_start_mbox, board not responding\n", 254 sc->sc_dev.dv_xname); 255 Debugger(); 256 } 257 258 bus_space_write_4(iot, ioh, U24_OGMPTR, 259 sc->sc_dmamap_mscp->dm_segs[0].ds_addr + UHA_MSCP_OFF(mscp)); 260 if (mscp->flags & MSCP_ABORT) 261 bus_space_write_1(iot, ioh, U24_OGMCMD, 0x80); 262 else 263 bus_space_write_1(iot, ioh, U24_OGMCMD, 0x01); 264 bus_space_write_1(iot, ioh, U24_LINT, U24_OGMFULL); 265 266 if ((mscp->xs->xs_control & XS_CTL_POLL) == 0) 267 callout_reset(&mscp->xs->xs_callout, 268 mstohz(mscp->timeout), uha_timeout, mscp); 269 } 270 271 int 272 u24_poll(sc, xs, count) 273 struct uha_softc *sc; 274 struct scsipi_xfer *xs; 275 int count; 276 { 277 bus_space_tag_t iot = sc->sc_iot; 278 bus_space_handle_t ioh = sc->sc_ioh; 279 280 while (count) { 281 /* 282 * If we had interrupts enabled, would we 283 * have got an interrupt? 284 */ 285 if (bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP) 286 u24_intr(sc); 287 if (xs->xs_status & XS_STS_DONE) 288 return (0); 289 delay(1000); 290 count--; 291 } 292 return (1); 293 } 294 295 int 296 u24_intr(arg) 297 void *arg; 298 { 299 struct uha_softc *sc = arg; 300 bus_space_tag_t iot = sc->sc_iot; 301 bus_space_handle_t ioh = sc->sc_ioh; 302 struct uha_mscp *mscp; 303 u_char uhastat; 304 u_long mboxval; 305 306 #ifdef UHADEBUG 307 printf("%s: uhaintr ", sc->sc_dev.dv_xname); 308 #endif /*UHADEBUG */ 309 310 if ((bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP) == 0) 311 return (0); 312 313 for (;;) { 314 /* 315 * First get all the information and then 316 * acknowledge the interrupt 317 */ 318 uhastat = bus_space_read_1(iot, ioh, U24_SINT); 319 mboxval = bus_space_read_4(iot, ioh, U24_ICMPTR); 320 bus_space_write_1(iot, ioh, U24_SINT, U24_ICM_ACK); 321 bus_space_write_1(iot, ioh, U24_ICMCMD, 0); 322 323 #ifdef UHADEBUG 324 printf("status = 0x%x ", uhastat); 325 #endif /*UHADEBUG*/ 326 327 /* 328 * Process the completed operation 329 */ 330 mscp = uha_mscp_phys_kv(sc, mboxval); 331 if (!mscp) { 332 printf("%s: BAD MSCP RETURNED!\n", 333 sc->sc_dev.dv_xname); 334 continue; /* whatever it was, it'll timeout */ 335 } 336 callout_stop(&mscp->xs->xs_callout); 337 uha_done(sc, mscp); 338 339 if ((bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP) == 0) 340 return (1); 341 } 342 } 343 344 void 345 u24_init(sc) 346 struct uha_softc *sc; 347 { 348 bus_space_tag_t iot = sc->sc_iot; 349 bus_space_handle_t ioh = sc->sc_ioh; 350 351 /* free OGM and ICM */ 352 bus_space_write_1(iot, ioh, U24_OGMCMD, 0); 353 bus_space_write_1(iot, ioh, U24_ICMCMD, 0); 354 /* make sure interrupts are enabled */ 355 #ifdef UHADEBUG 356 printf("u24_init: lmask=%02x, smask=%02x\n", 357 bus_space_read_1(iot, ioh, U24_LMASK), 358 bus_space_read_1(iot, ioh, U24_SMASK)); 359 #endif 360 bus_space_write_1(iot, ioh, U24_LMASK, 0xd2); /* XXX */ 361 bus_space_write_1(iot, ioh, U24_SMASK, 0x92); /* XXX */ 362 } 363