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