1 /* $NetBSD: wdc_isa.c,v 1.41 2004/05/25 20:42:41 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2003 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 and by Onno van der Linden. 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: wdc_isa.c,v 1.41 2004/05/25 20:42:41 thorpej Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/device.h> 45 #include <sys/malloc.h> 46 47 #include <machine/bus.h> 48 #include <machine/intr.h> 49 50 #include <dev/isa/isavar.h> 51 #include <dev/isa/isadmavar.h> 52 53 #include <dev/ic/wdcreg.h> 54 #include <dev/ata/atavar.h> 55 #include <dev/ic/wdcvar.h> 56 57 #define WDC_ISA_REG_NPORTS 8 58 #define WDC_ISA_AUXREG_OFFSET 0x206 59 #define WDC_ISA_AUXREG_NPORTS 1 /* XXX "fdc" owns ports 0x3f7/0x377 */ 60 61 /* options passed via the 'flags' config keyword */ 62 #define WDC_OPTIONS_32 0x01 /* try to use 32bit data I/O */ 63 #define WDC_OPTIONS_ATA_NOSTREAM 0x04 64 #define WDC_OPTIONS_ATAPI_NOSTREAM 0x08 65 66 struct wdc_isa_softc { 67 struct wdc_softc sc_wdcdev; 68 struct wdc_channel *wdc_chanlist[1]; 69 struct wdc_channel wdc_channel; 70 struct ata_queue wdc_chqueue; 71 isa_chipset_tag_t sc_ic; 72 void *sc_ih; 73 int sc_drq; 74 }; 75 76 int wdc_isa_probe __P((struct device *, struct cfdata *, void *)); 77 void wdc_isa_attach __P((struct device *, struct device *, void *)); 78 79 CFATTACH_DECL(wdc_isa, sizeof(struct wdc_isa_softc), 80 wdc_isa_probe, wdc_isa_attach, NULL, NULL); 81 82 #if 0 83 static void wdc_isa_dma_setup __P((struct wdc_isa_softc *)); 84 static int wdc_isa_dma_init __P((void*, int, int, void *, size_t, int)); 85 static void wdc_isa_dma_start __P((void*, int, int)); 86 static int wdc_isa_dma_finish __P((void*, int, int, int)); 87 #endif 88 89 int 90 wdc_isa_probe(parent, match, aux) 91 struct device *parent; 92 struct cfdata *match; 93 void *aux; 94 { 95 struct wdc_channel ch; 96 struct isa_attach_args *ia = aux; 97 int result = 0, i; 98 99 if (ia->ia_nio < 1) 100 return (0); 101 if (ia->ia_nirq < 1) 102 return (0); 103 104 if (ISA_DIRECT_CONFIG(ia)) 105 return (0); 106 107 if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT) 108 return (0); 109 if (ia->ia_irq[0].ir_irq == ISACF_IRQ_DEFAULT) 110 return (0); 111 if (ia->ia_ndrq > 0 && ia->ia_drq[0].ir_drq == ISACF_DRQ_DEFAULT) 112 ia->ia_ndrq = 0; 113 114 memset(&ch, 0, sizeof(ch)); 115 116 ch.cmd_iot = ia->ia_iot; 117 118 if (bus_space_map(ch.cmd_iot, ia->ia_io[0].ir_addr, 119 WDC_ISA_REG_NPORTS, 0, &ch.cmd_baseioh)) 120 goto out; 121 122 for (i = 0; i < WDC_ISA_REG_NPORTS; i++) { 123 if (bus_space_subregion(ch.cmd_iot, ch.cmd_baseioh, i, 124 i == 0 ? 4 : 1, &ch.cmd_iohs[i]) != 0) 125 goto outunmap; 126 } 127 wdc_init_shadow_regs(&ch); 128 129 ch.ctl_iot = ia->ia_iot; 130 if (bus_space_map(ch.ctl_iot, ia->ia_io[0].ir_addr + 131 WDC_ISA_AUXREG_OFFSET, WDC_ISA_AUXREG_NPORTS, 0, &ch.ctl_ioh)) 132 goto outunmap; 133 134 result = wdcprobe(&ch); 135 if (result) { 136 ia->ia_nio = 1; 137 ia->ia_io[0].ir_size = WDC_ISA_REG_NPORTS; 138 139 ia->ia_nirq = 1; 140 141 ia->ia_niomem = 0; 142 } 143 144 bus_space_unmap(ch.ctl_iot, ch.ctl_ioh, WDC_ISA_AUXREG_NPORTS); 145 outunmap: 146 bus_space_unmap(ch.cmd_iot, ch.cmd_baseioh, WDC_ISA_REG_NPORTS); 147 out: 148 return (result); 149 } 150 151 void 152 wdc_isa_attach(parent, self, aux) 153 struct device *parent, *self; 154 void *aux; 155 { 156 struct wdc_isa_softc *sc = (void *)self; 157 struct isa_attach_args *ia = aux; 158 int wdc_cf_flags = self->dv_cfdata->cf_flags; 159 int i; 160 161 sc->wdc_channel.cmd_iot = ia->ia_iot; 162 sc->wdc_channel.ctl_iot = ia->ia_iot; 163 sc->sc_ic = ia->ia_ic; 164 if (bus_space_map(sc->wdc_channel.cmd_iot, ia->ia_io[0].ir_addr, 165 WDC_ISA_REG_NPORTS, 0, &sc->wdc_channel.cmd_baseioh) || 166 bus_space_map(sc->wdc_channel.ctl_iot, 167 ia->ia_io[0].ir_addr + WDC_ISA_AUXREG_OFFSET, 168 WDC_ISA_AUXREG_NPORTS, 0, &sc->wdc_channel.ctl_ioh)) { 169 printf(": couldn't map registers\n"); 170 return; 171 } 172 173 for (i = 0; i < WDC_ISA_REG_NPORTS; i++) { 174 if (bus_space_subregion(sc->wdc_channel.cmd_iot, 175 sc->wdc_channel.cmd_baseioh, i, i == 0 ? 4 : 1, 176 &sc->wdc_channel.cmd_iohs[i]) != 0) { 177 printf(": couldn't subregion registers\n"); 178 return; 179 } 180 } 181 wdc_init_shadow_regs(&sc->wdc_channel); 182 183 sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot; 184 sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_iohs[0]; 185 186 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq, 187 IST_EDGE, IPL_BIO, wdcintr, &sc->wdc_channel); 188 189 #if 0 190 if (ia->ia_ndrq > 0 && ia->ia_drq[0].ir_drq != ISACF_DRQ_DEFAULT) { 191 sc->sc_drq = ia->ia_drq[0].ir_drq; 192 193 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA; 194 sc->sc_wdcdev.dma_arg = sc; 195 sc->sc_wdcdev.dma_init = wdc_isa_dma_init; 196 sc->sc_wdcdev.dma_start = wdc_isa_dma_start; 197 sc->sc_wdcdev.dma_finish = wdc_isa_dma_finish; 198 wdc_isa_dma_setup(sc); 199 } 200 #endif 201 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_PREATA; 202 if (wdc_cf_flags & WDC_OPTIONS_32) 203 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32; 204 if (wdc_cf_flags & WDC_OPTIONS_ATA_NOSTREAM) 205 sc->sc_wdcdev.cap |= WDC_CAPABILITY_ATA_NOSTREAM; 206 if (wdc_cf_flags & WDC_OPTIONS_ATAPI_NOSTREAM) 207 sc->sc_wdcdev.cap |= WDC_CAPABILITY_ATAPI_NOSTREAM; 208 209 sc->sc_wdcdev.PIO_cap = 0; 210 sc->wdc_chanlist[0] = &sc->wdc_channel; 211 sc->sc_wdcdev.channels = sc->wdc_chanlist; 212 sc->sc_wdcdev.nchannels = 1; 213 sc->wdc_channel.ch_channel = 0; 214 sc->wdc_channel.ch_wdc = &sc->sc_wdcdev; 215 sc->wdc_channel.ch_queue = &sc->wdc_chqueue; 216 217 printf("\n"); 218 219 wdcattach(&sc->wdc_channel); 220 } 221 222 #if 0 223 static void 224 wdc_isa_dma_setup(sc) 225 struct wdc_isa_softc *sc; 226 { 227 bus_size_t maxsize; 228 229 if ((maxsize = isa_dmamaxsize(sc->sc_ic, sc->sc_drq)) < MAXPHYS) { 230 printf("%s: max DMA size %lu is less than required %d\n", 231 sc->sc_wdcdev.sc_dev.dv_xname, (u_long)maxsize, MAXPHYS); 232 sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_DMA; 233 return; 234 } 235 236 if (isa_drq_alloc(sc->sc_ic, sc->sc_drq) != 0) { 237 printf("%s: can't reserve drq %d\n", 238 sc->sc_wdcdev.sc_dev.dv_xname, sc->sc_drq); 239 sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_DMA; 240 return; 241 } 242 243 if (isa_dmamap_create(sc->sc_ic, sc->sc_drq, 244 MAXPHYS, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) { 245 printf("%s: can't create map for drq %d\n", 246 sc->sc_wdcdev.sc_dev.dv_xname, sc->sc_drq); 247 sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_DMA; 248 } 249 } 250 251 static int 252 wdc_isa_dma_init(v, channel, drive, databuf, datalen, read) 253 void *v; 254 void *databuf; 255 size_t datalen; 256 int read; 257 { 258 struct wdc_isa_softc *sc = v; 259 260 isa_dmastart(sc->sc_ic, sc->sc_drq, databuf, datalen, NULL, 261 (read ? DMAMODE_READ : DMAMODE_WRITE) | DMAMODE_DEMAND, 262 BUS_DMA_NOWAIT); 263 return 0; 264 } 265 266 static void 267 wdc_isa_dma_start(v, channel, drive) 268 void *v; 269 int channel, drive; 270 { 271 /* nothing to do */ 272 } 273 274 static int 275 wdc_isa_dma_finish(v, channel, drive, read) 276 void *v; 277 int channel, drive; 278 int read; 279 { 280 struct wdc_isa_softc *sc = v; 281 282 isa_dmadone(sc->sc_ic, sc->sc_drq); 283 return 0; 284 } 285 #endif 286