Home | History | Annotate | Line # | Download | only in isa
wdc_isa.c revision 1.42
      1 /*	$NetBSD: wdc_isa.c,v 1.42 2004/08/14 15:08:06 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.42 2004/08/14 15:08:06 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	ata_channel *wdc_chanlist[1];
     69 	struct	ata_channel ata_channel;
     70 	struct	ata_queue wdc_chqueue;
     71 	struct	wdc_regs wdc_regs;
     72 	isa_chipset_tag_t sc_ic;
     73 	void	*sc_ih;
     74 	int	sc_drq;
     75 };
     76 
     77 int	wdc_isa_probe	__P((struct device *, struct cfdata *, void *));
     78 void	wdc_isa_attach	__P((struct device *, struct device *, void *));
     79 
     80 CFATTACH_DECL(wdc_isa, sizeof(struct wdc_isa_softc),
     81     wdc_isa_probe, wdc_isa_attach, NULL, NULL);
     82 
     83 #if 0
     84 static void	wdc_isa_dma_setup __P((struct wdc_isa_softc *));
     85 static int	wdc_isa_dma_init __P((void*, int, int, void *, size_t, int));
     86 static void 	wdc_isa_dma_start __P((void*, int, int));
     87 static int	wdc_isa_dma_finish __P((void*, int, int, int));
     88 #endif
     89 
     90 int
     91 wdc_isa_probe(parent, match, aux)
     92 	struct device *parent;
     93 	struct cfdata *match;
     94 	void *aux;
     95 {
     96 	struct ata_channel ch;
     97 	struct isa_attach_args *ia = aux;
     98 	struct wdc_softc wdc;
     99 	struct wdc_regs wdr;
    100 	int result = 0, i;
    101 
    102 	if (ia->ia_nio < 1)
    103 		return (0);
    104 	if (ia->ia_nirq < 1)
    105 		return (0);
    106 
    107 	if (ISA_DIRECT_CONFIG(ia))
    108 		return (0);
    109 
    110 	if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
    111 		return (0);
    112 	if (ia->ia_irq[0].ir_irq == ISACF_IRQ_DEFAULT)
    113 		return (0);
    114 	if (ia->ia_ndrq > 0 && ia->ia_drq[0].ir_drq == ISACF_DRQ_DEFAULT)
    115 		ia->ia_ndrq = 0;
    116 
    117 	memset(&wdc, 0, sizeof(wdc));
    118 	memset(&ch, 0, sizeof(ch));
    119 	ch.ch_wdc = &wdc;
    120 	wdc.regs = &wdr;
    121 
    122 	wdr.cmd_iot = ia->ia_iot;
    123 
    124 	if (bus_space_map(wdr.cmd_iot, ia->ia_io[0].ir_addr,
    125 	    WDC_ISA_REG_NPORTS, 0, &wdr.cmd_baseioh))
    126 		goto out;
    127 
    128 	for (i = 0; i < WDC_ISA_REG_NPORTS; i++) {
    129 		if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh, i,
    130 		    i == 0 ? 4 : 1, &wdr.cmd_iohs[i]) != 0)
    131 			goto outunmap;
    132 	}
    133 	wdc_init_shadow_regs(&ch);
    134 
    135 	wdr.ctl_iot = ia->ia_iot;
    136 	if (bus_space_map(wdr.ctl_iot, ia->ia_io[0].ir_addr +
    137 	    WDC_ISA_AUXREG_OFFSET, WDC_ISA_AUXREG_NPORTS, 0, &wdr.ctl_ioh))
    138 		goto outunmap;
    139 
    140 	result = wdcprobe(&ch);
    141 	if (result) {
    142 		ia->ia_nio = 1;
    143 		ia->ia_io[0].ir_size = WDC_ISA_REG_NPORTS;
    144 
    145 		ia->ia_nirq = 1;
    146 
    147 		ia->ia_niomem = 0;
    148 	}
    149 
    150 	bus_space_unmap(wdr.ctl_iot, wdr.ctl_ioh, WDC_ISA_AUXREG_NPORTS);
    151 outunmap:
    152 	bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_ISA_REG_NPORTS);
    153 out:
    154 	return (result);
    155 }
    156 
    157 void
    158 wdc_isa_attach(parent, self, aux)
    159 	struct device *parent, *self;
    160 	void *aux;
    161 {
    162 	struct wdc_isa_softc *sc = (void *)self;
    163 	struct wdc_regs *wdr;
    164 	struct isa_attach_args *ia = aux;
    165 	int wdc_cf_flags = self->dv_cfdata->cf_flags;
    166 	int i;
    167 
    168 	sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
    169 	wdr->cmd_iot = ia->ia_iot;
    170 	wdr->ctl_iot = ia->ia_iot;
    171 	sc->sc_ic = ia->ia_ic;
    172 	if (bus_space_map(wdr->cmd_iot, ia->ia_io[0].ir_addr,
    173 	    WDC_ISA_REG_NPORTS, 0, &wdr->cmd_baseioh) ||
    174 	    bus_space_map(wdr->ctl_iot,
    175 	      ia->ia_io[0].ir_addr + WDC_ISA_AUXREG_OFFSET,
    176 	      WDC_ISA_AUXREG_NPORTS, 0, &wdr->ctl_ioh)) {
    177 		printf(": couldn't map registers\n");
    178 		return;
    179 	}
    180 
    181 	for (i = 0; i < WDC_ISA_REG_NPORTS; i++) {
    182 		if (bus_space_subregion(wdr->cmd_iot,
    183 		      wdr->cmd_baseioh, i, i == 0 ? 4 : 1,
    184 		      &wdr->cmd_iohs[i]) != 0) {
    185 			printf(": couldn't subregion registers\n");
    186 			return;
    187 		}
    188 	}
    189 	wdc_init_shadow_regs(&sc->ata_channel);
    190 
    191 	wdr->data32iot = wdr->cmd_iot;
    192 	wdr->data32ioh = wdr->cmd_iohs[0];
    193 
    194 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
    195 	    IST_EDGE, IPL_BIO, wdcintr, &sc->ata_channel);
    196 
    197 #if 0
    198 	if (ia->ia_ndrq > 0 && ia->ia_drq[0].ir_drq != ISACF_DRQ_DEFAULT) {
    199 		sc->sc_drq = ia->ia_drq[0].ir_drq;
    200 
    201 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
    202 		sc->sc_wdcdev.dma_arg = sc;
    203 		sc->sc_wdcdev.dma_init = wdc_isa_dma_init;
    204 		sc->sc_wdcdev.dma_start = wdc_isa_dma_start;
    205 		sc->sc_wdcdev.dma_finish = wdc_isa_dma_finish;
    206 		wdc_isa_dma_setup(sc);
    207 	}
    208 #endif
    209 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_PREATA;
    210 	if (wdc_cf_flags & WDC_OPTIONS_32)
    211 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
    212 	if (wdc_cf_flags & WDC_OPTIONS_ATA_NOSTREAM)
    213 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_ATA_NOSTREAM;
    214 	if (wdc_cf_flags & WDC_OPTIONS_ATAPI_NOSTREAM)
    215 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_ATAPI_NOSTREAM;
    216 
    217 	sc->sc_wdcdev.PIO_cap = 0;
    218 	sc->wdc_chanlist[0] = &sc->ata_channel;
    219 	sc->sc_wdcdev.channels = sc->wdc_chanlist;
    220 	sc->sc_wdcdev.nchannels = 1;
    221 	sc->ata_channel.ch_channel = 0;
    222 	sc->ata_channel.ch_wdc = &sc->sc_wdcdev;
    223 	sc->ata_channel.ch_queue = &sc->wdc_chqueue;
    224 
    225 	printf("\n");
    226 
    227 	wdcattach(&sc->ata_channel);
    228 }
    229 
    230 #if 0
    231 static void
    232 wdc_isa_dma_setup(sc)
    233 	struct wdc_isa_softc *sc;
    234 {
    235 	bus_size_t maxsize;
    236 
    237 	if ((maxsize = isa_dmamaxsize(sc->sc_ic, sc->sc_drq)) < MAXPHYS) {
    238 		printf("%s: max DMA size %lu is less than required %d\n",
    239 		    sc->sc_wdcdev.sc_dev.dv_xname, (u_long)maxsize, MAXPHYS);
    240 		sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_DMA;
    241 		return;
    242 	}
    243 
    244 	if (isa_drq_alloc(sc->sc_ic, sc->sc_drq) != 0) {
    245 		printf("%s: can't reserve drq %d\n",
    246 		    sc->sc_wdcdev.sc_dev.dv_xname, sc->sc_drq);
    247 		sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_DMA;
    248 		return;
    249 	}
    250 
    251 	if (isa_dmamap_create(sc->sc_ic, sc->sc_drq,
    252 	    MAXPHYS, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    253 		printf("%s: can't create map for drq %d\n",
    254 		    sc->sc_wdcdev.sc_dev.dv_xname, sc->sc_drq);
    255 		sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_DMA;
    256 	}
    257 }
    258 
    259 static int
    260 wdc_isa_dma_init(v, channel, drive, databuf, datalen, read)
    261 	void *v;
    262 	void *databuf;
    263 	size_t datalen;
    264 	int read;
    265 {
    266 	struct wdc_isa_softc *sc = v;
    267 
    268 	isa_dmastart(sc->sc_ic, sc->sc_drq, databuf, datalen, NULL,
    269 	    (read ? DMAMODE_READ : DMAMODE_WRITE) | DMAMODE_DEMAND,
    270 	    BUS_DMA_NOWAIT);
    271 	return 0;
    272 }
    273 
    274 static void
    275 wdc_isa_dma_start(v, channel, drive)
    276 	void *v;
    277 	int channel, drive;
    278 {
    279 	/* nothing to do */
    280 }
    281 
    282 static int
    283 wdc_isa_dma_finish(v, channel, drive, read)
    284 	void *v;
    285 	int channel, drive;
    286 	int read;
    287 {
    288 	struct wdc_isa_softc *sc = v;
    289 
    290 	isa_dmadone(sc->sc_ic, sc->sc_drq);
    291 	return 0;
    292 }
    293 #endif
    294