Home | History | Annotate | Line # | Download | only in isa
wdc_isa.c revision 1.53.6.1
      1  1.53.6.1       mjf /*	$NetBSD: wdc_isa.c,v 1.53.6.1 2008/04/03 12:42:45 mjf Exp $ */
      2       1.1       cgd 
      3       1.9   mycroft /*-
      4      1.32   mycroft  * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
      5       1.9   mycroft  * All rights reserved.
      6       1.1       cgd  *
      7       1.9   mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8       1.9   mycroft  * by Charles M. Hannum and by Onno van der Linden.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19       1.1       cgd  *    must display the following acknowledgement:
     20       1.9   mycroft  *        This product includes software developed by the NetBSD
     21       1.9   mycroft  *        Foundation, Inc. and its contributors.
     22       1.9   mycroft  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.9   mycroft  *    contributors may be used to endorse or promote products derived
     24       1.9   mycroft  *    from this software without specific prior written permission.
     25       1.1       cgd  *
     26       1.9   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.9   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.9   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.9   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.9   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.9   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.9   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.9   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.9   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.9   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.9   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1       cgd  */
     38      1.22     lukem 
     39      1.22     lukem #include <sys/cdefs.h>
     40  1.53.6.1       mjf __KERNEL_RCSID(0, "$NetBSD: wdc_isa.c,v 1.53.6.1 2008/04/03 12:42:45 mjf Exp $");
     41       1.1       cgd 
     42       1.1       cgd #include <sys/param.h>
     43       1.1       cgd #include <sys/systm.h>
     44       1.1       cgd #include <sys/device.h>
     45      1.10    bouyer #include <sys/malloc.h>
     46       1.1       cgd 
     47      1.52        ad #include <sys/bus.h>
     48      1.52        ad #include <sys/intr.h>
     49       1.1       cgd 
     50       1.1       cgd #include <dev/isa/isavar.h>
     51       1.1       cgd #include <dev/isa/isadmavar.h>
     52       1.3   mycroft 
     53      1.37      fvdl #include <dev/ic/wdcreg.h>
     54      1.10    bouyer #include <dev/ata/atavar.h>
     55       1.1       cgd #include <dev/ic/wdcvar.h>
     56       1.1       cgd 
     57       1.1       cgd #define	WDC_ISA_REG_NPORTS	8
     58       1.1       cgd #define	WDC_ISA_AUXREG_OFFSET	0x206
     59       1.5  drochner #define	WDC_ISA_AUXREG_NPORTS	1 /* XXX "fdc" owns ports 0x3f7/0x377 */
     60       1.1       cgd 
     61      1.15    bouyer /* options passed via the 'flags' config keyword */
     62      1.20  takemura #define WDC_OPTIONS_32			0x01 /* try to use 32bit data I/O */
     63      1.21       leo #define WDC_OPTIONS_ATA_NOSTREAM	0x04
     64      1.21       leo #define WDC_OPTIONS_ATAPI_NOSTREAM	0x08
     65       1.1       cgd 
     66       1.1       cgd struct wdc_isa_softc {
     67      1.10    bouyer 	struct	wdc_softc sc_wdcdev;
     68      1.42   thorpej 	struct	ata_channel *wdc_chanlist[1];
     69      1.42   thorpej 	struct	ata_channel ata_channel;
     70      1.38   thorpej 	struct	ata_queue wdc_chqueue;
     71      1.42   thorpej 	struct	wdc_regs wdc_regs;
     72       1.7   thorpej 	isa_chipset_tag_t sc_ic;
     73       1.1       cgd 	void	*sc_ih;
     74       1.1       cgd 	int	sc_drq;
     75       1.1       cgd };
     76       1.1       cgd 
     77  1.53.6.1       mjf static int	wdc_isa_probe(device_t , cfdata_t, void *);
     78      1.53    dyoung static void	wdc_isa_attach(device_t, device_t, void *);
     79      1.53    dyoung static int	wdc_isa_detach(device_t, int);
     80      1.53    dyoung 
     81  1.53.6.1       mjf CFATTACH_DECL2_NEW(wdc_isa, sizeof(struct wdc_isa_softc),
     82      1.53    dyoung     wdc_isa_probe, wdc_isa_attach, wdc_isa_detach, NULL, NULL,
     83      1.53    dyoung     wdc_childdetached);
     84       1.1       cgd 
     85      1.29   mycroft #if 0
     86      1.44   thorpej static void	wdc_isa_dma_setup(struct wdc_isa_softc *);
     87      1.44   thorpej static int	wdc_isa_dma_init(void*, int, int, void *, size_t, int);
     88      1.44   thorpej static void 	wdc_isa_dma_start(void*, int, int);
     89      1.44   thorpej static int	wdc_isa_dma_finish(void*, int, int, int);
     90      1.29   mycroft #endif
     91       1.1       cgd 
     92      1.44   thorpej static int
     93  1.53.6.1       mjf wdc_isa_probe(device_t parent, cfdata_t match, void *aux)
     94       1.1       cgd {
     95      1.42   thorpej 	struct ata_channel ch;
     96       1.1       cgd 	struct isa_attach_args *ia = aux;
     97      1.42   thorpej 	struct wdc_softc wdc;
     98      1.42   thorpej 	struct wdc_regs wdr;
     99      1.37      fvdl 	int result = 0, i;
    100      1.18   thorpej 
    101      1.24   thorpej 	if (ia->ia_nio < 1)
    102      1.24   thorpej 		return (0);
    103      1.24   thorpej 	if (ia->ia_nirq < 1)
    104      1.24   thorpej 		return (0);
    105      1.24   thorpej 
    106      1.24   thorpej 	if (ISA_DIRECT_CONFIG(ia))
    107      1.24   thorpej 		return (0);
    108      1.24   thorpej 
    109      1.46  drochner 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    110      1.24   thorpej 		return (0);
    111      1.46  drochner 	if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
    112      1.24   thorpej 		return (0);
    113      1.46  drochner 	if (ia->ia_ndrq > 0 && ia->ia_drq[0].ir_drq == ISA_UNKNOWN_DRQ)
    114      1.25  gmcgarry 		ia->ia_ndrq = 0;
    115      1.24   thorpej 
    116      1.42   thorpej 	memset(&wdc, 0, sizeof(wdc));
    117      1.18   thorpej 	memset(&ch, 0, sizeof(ch));
    118      1.45   thorpej 	ch.ch_atac = &wdc.sc_atac;
    119      1.42   thorpej 	wdc.regs = &wdr;
    120       1.1       cgd 
    121      1.42   thorpej 	wdr.cmd_iot = ia->ia_iot;
    122      1.24   thorpej 
    123      1.42   thorpej 	if (bus_space_map(wdr.cmd_iot, ia->ia_io[0].ir_addr,
    124      1.42   thorpej 	    WDC_ISA_REG_NPORTS, 0, &wdr.cmd_baseioh))
    125       1.1       cgd 		goto out;
    126       1.1       cgd 
    127      1.37      fvdl 	for (i = 0; i < WDC_ISA_REG_NPORTS; i++) {
    128      1.42   thorpej 		if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh, i,
    129      1.42   thorpej 		    i == 0 ? 4 : 1, &wdr.cmd_iohs[i]) != 0)
    130      1.37      fvdl 			goto outunmap;
    131      1.37      fvdl 	}
    132      1.41   thorpej 	wdc_init_shadow_regs(&ch);
    133      1.37      fvdl 
    134      1.42   thorpej 	wdr.ctl_iot = ia->ia_iot;
    135      1.42   thorpej 	if (bus_space_map(wdr.ctl_iot, ia->ia_io[0].ir_addr +
    136      1.42   thorpej 	    WDC_ISA_AUXREG_OFFSET, WDC_ISA_AUXREG_NPORTS, 0, &wdr.ctl_ioh))
    137       1.1       cgd 		goto outunmap;
    138       1.1       cgd 
    139      1.10    bouyer 	result = wdcprobe(&ch);
    140       1.1       cgd 	if (result) {
    141      1.24   thorpej 		ia->ia_nio = 1;
    142      1.24   thorpej 		ia->ia_io[0].ir_size = WDC_ISA_REG_NPORTS;
    143      1.24   thorpej 
    144      1.24   thorpej 		ia->ia_nirq = 1;
    145      1.24   thorpej 
    146      1.24   thorpej 		ia->ia_niomem = 0;
    147       1.1       cgd 	}
    148       1.1       cgd 
    149      1.42   thorpej 	bus_space_unmap(wdr.ctl_iot, wdr.ctl_ioh, WDC_ISA_AUXREG_NPORTS);
    150       1.1       cgd outunmap:
    151      1.42   thorpej 	bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_ISA_REG_NPORTS);
    152       1.1       cgd out:
    153       1.1       cgd 	return (result);
    154       1.1       cgd }
    155       1.1       cgd 
    156      1.53    dyoung static int
    157      1.53    dyoung wdc_isa_detach(device_t self, int flags)
    158      1.53    dyoung {
    159      1.53    dyoung 	struct wdc_isa_softc *sc = device_private(self);
    160      1.53    dyoung 	struct wdc_regs *wdr = &sc->wdc_regs;
    161      1.53    dyoung 	int rc;
    162      1.53    dyoung 
    163      1.53    dyoung 	if ((rc = wdcdetach(self, flags)) != 0)
    164      1.53    dyoung 		return rc;
    165      1.53    dyoung 
    166      1.53    dyoung 	isa_intr_disestablish(sc->sc_ic, sc->sc_ih);
    167      1.53    dyoung 
    168      1.53    dyoung 	bus_space_unmap(wdr->ctl_iot, wdr->ctl_ioh, WDC_ISA_AUXREG_NPORTS);
    169      1.53    dyoung 	bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, WDC_ISA_REG_NPORTS);
    170      1.53    dyoung 
    171      1.53    dyoung 	return 0;
    172      1.53    dyoung }
    173      1.53    dyoung 
    174      1.44   thorpej static void
    175      1.53    dyoung wdc_isa_attach(device_t parent, device_t self, void *aux)
    176       1.1       cgd {
    177      1.53    dyoung 	struct wdc_isa_softc *sc = device_private(self);
    178      1.42   thorpej 	struct wdc_regs *wdr;
    179       1.1       cgd 	struct isa_attach_args *ia = aux;
    180      1.49   thorpej 	int wdc_cf_flags = device_cfdata(self)->cf_flags;
    181      1.37      fvdl 	int i;
    182       1.4   mycroft 
    183  1.53.6.1       mjf 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    184      1.42   thorpej 	sc->sc_wdcdev.regs = wdr = &sc->wdc_regs;
    185      1.42   thorpej 	wdr->cmd_iot = ia->ia_iot;
    186      1.42   thorpej 	wdr->ctl_iot = ia->ia_iot;
    187       1.7   thorpej 	sc->sc_ic = ia->ia_ic;
    188      1.42   thorpej 	if (bus_space_map(wdr->cmd_iot, ia->ia_io[0].ir_addr,
    189      1.42   thorpej 	    WDC_ISA_REG_NPORTS, 0, &wdr->cmd_baseioh) ||
    190      1.42   thorpej 	    bus_space_map(wdr->ctl_iot,
    191      1.24   thorpej 	      ia->ia_io[0].ir_addr + WDC_ISA_AUXREG_OFFSET,
    192      1.42   thorpej 	      WDC_ISA_AUXREG_NPORTS, 0, &wdr->ctl_ioh)) {
    193  1.53.6.1       mjf 		aprint_error(": couldn't map registers\n");
    194      1.30      matt 		return;
    195       1.1       cgd 	}
    196      1.37      fvdl 
    197      1.37      fvdl 	for (i = 0; i < WDC_ISA_REG_NPORTS; i++) {
    198      1.42   thorpej 		if (bus_space_subregion(wdr->cmd_iot,
    199      1.42   thorpej 		      wdr->cmd_baseioh, i, i == 0 ? 4 : 1,
    200      1.42   thorpej 		      &wdr->cmd_iohs[i]) != 0) {
    201  1.53.6.1       mjf 			aprint_error(": couldn't subregion registers\n");
    202      1.37      fvdl 			return;
    203      1.37      fvdl 		}
    204      1.37      fvdl 	}
    205      1.37      fvdl 
    206      1.42   thorpej 	wdr->data32iot = wdr->cmd_iot;
    207      1.42   thorpej 	wdr->data32ioh = wdr->cmd_iohs[0];
    208       1.1       cgd 
    209      1.29   mycroft #if 0
    210      1.46  drochner 	if (ia->ia_ndrq > 0 && ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ) {
    211      1.24   thorpej 		sc->sc_drq = ia->ia_drq[0].ir_drq;
    212       1.1       cgd 
    213      1.45   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
    214      1.10    bouyer 		sc->sc_wdcdev.dma_arg = sc;
    215      1.10    bouyer 		sc->sc_wdcdev.dma_init = wdc_isa_dma_init;
    216      1.10    bouyer 		sc->sc_wdcdev.dma_start = wdc_isa_dma_start;
    217      1.10    bouyer 		sc->sc_wdcdev.dma_finish = wdc_isa_dma_finish;
    218      1.10    bouyer 		wdc_isa_dma_setup(sc);
    219       1.6       cgd 	}
    220      1.29   mycroft #endif
    221      1.45   thorpej 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_PREATA;
    222      1.45   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
    223      1.30      matt 	if (wdc_cf_flags & WDC_OPTIONS_32)
    224      1.45   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA32;
    225      1.30      matt 	if (wdc_cf_flags & WDC_OPTIONS_ATA_NOSTREAM)
    226      1.45   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_ATA_NOSTREAM;
    227      1.30      matt 	if (wdc_cf_flags & WDC_OPTIONS_ATAPI_NOSTREAM)
    228      1.45   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_ATAPI_NOSTREAM;
    229      1.30      matt 
    230      1.45   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
    231      1.42   thorpej 	sc->wdc_chanlist[0] = &sc->ata_channel;
    232      1.45   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanlist;
    233      1.45   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
    234      1.42   thorpej 	sc->ata_channel.ch_channel = 0;
    235      1.45   thorpej 	sc->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    236      1.42   thorpej 	sc->ata_channel.ch_queue = &sc->wdc_chqueue;
    237      1.48    bouyer 	sc->ata_channel.ch_ndrive = 2;
    238      1.43   mycroft 	wdc_init_shadow_regs(&sc->ata_channel);
    239      1.30      matt 
    240  1.53.6.1       mjf 	aprint_normal("\n");
    241      1.30      matt 
    242      1.43   mycroft 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
    243      1.43   mycroft 	    IST_EDGE, IPL_BIO, wdcintr, &sc->ata_channel);
    244      1.43   mycroft 
    245      1.42   thorpej 	wdcattach(&sc->ata_channel);
    246       1.1       cgd }
    247       1.1       cgd 
    248      1.29   mycroft #if 0
    249       1.1       cgd static void
    250      1.44   thorpej wdc_isa_dma_setup(struct wdc_isa_softc *sc)
    251       1.1       cgd {
    252      1.16   thorpej 	bus_size_t maxsize;
    253      1.16   thorpej 
    254      1.16   thorpej 	if ((maxsize = isa_dmamaxsize(sc->sc_ic, sc->sc_drq)) < MAXPHYS) {
    255  1.53.6.1       mjf 		aprint_error_dev(sc_wdcdev.sc_atac.atac_dev,
    256  1.53.6.1       mjf 		    "max DMA size %lu is less than required %d\n",
    257  1.53.6.1       mjf 		    (u_long)maxsize, MAXPHYS);
    258      1.45   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap &= ~ATAC_CAP_DMA;
    259      1.31      fvdl 		return;
    260      1.31      fvdl 	}
    261      1.31      fvdl 
    262      1.31      fvdl 	if (isa_drq_alloc(sc->sc_ic, sc->sc_drq) != 0) {
    263  1.53.6.1       mjf 		aprint_error_dev(sc_wdcdev.sc_atac.atac_dev,
    264  1.53.6.1       mjf 		    "can't reserve drq %d\n", sc->sc_drq);
    265      1.45   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap &= ~ATAC_CAP_DMA;
    266      1.16   thorpej 		return;
    267      1.16   thorpej 	}
    268      1.16   thorpej 
    269       1.7   thorpej 	if (isa_dmamap_create(sc->sc_ic, sc->sc_drq,
    270       1.1       cgd 	    MAXPHYS, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    271  1.53.6.1       mjf 		aprint_error_dev(sc_wdcdev.sc_atac.atac_dev,
    272  1.53.6.1       mjf 		    "can't create map for drq %d\n", sc->sc_drq);
    273      1.45   thorpej 		sc->sc_wdcdev.sc_atac.atac_cap &= ~ATAC_CAP_DMA;
    274       1.1       cgd 	}
    275       1.1       cgd }
    276       1.1       cgd 
    277      1.10    bouyer static int
    278      1.44   thorpej wdc_isa_dma_init(void *v, int channel, int drive, void *databuf,
    279      1.44   thorpej     size_t datalen, int read)
    280       1.1       cgd {
    281      1.10    bouyer 	struct wdc_isa_softc *sc = v;
    282       1.1       cgd 
    283      1.13   mycroft 	isa_dmastart(sc->sc_ic, sc->sc_drq, databuf, datalen, NULL,
    284      1.13   mycroft 	    (read ? DMAMODE_READ : DMAMODE_WRITE) | DMAMODE_DEMAND,
    285       1.1       cgd 	    BUS_DMA_NOWAIT);
    286      1.10    bouyer 	return 0;
    287       1.1       cgd }
    288       1.1       cgd 
    289       1.1       cgd static void
    290      1.44   thorpej wdc_isa_dma_start(void *v, int channel, int drive)
    291      1.10    bouyer {
    292      1.10    bouyer 	/* nothing to do */
    293      1.10    bouyer }
    294      1.10    bouyer 
    295      1.10    bouyer static int
    296      1.44   thorpej wdc_isa_dma_finish(void *v, int channel, int drive, int read)
    297       1.1       cgd {
    298      1.10    bouyer 	struct wdc_isa_softc *sc = v;
    299       1.1       cgd 
    300       1.7   thorpej 	isa_dmadone(sc->sc_ic, sc->sc_drq);
    301      1.10    bouyer 	return 0;
    302       1.1       cgd }
    303      1.29   mycroft #endif
    304