Home | History | Annotate | Line # | Download | only in dev
wdc_xsurf.c revision 1.4
      1 /*      $NetBSD: wdc_xsurf.c,v 1.4 2017/09/04 17:26:06 phx Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Radoslaw Kujawa.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /* Driver for IDE controller present on X-Surf. */
     33 
     34 #include <sys/cdefs.h>
     35 
     36 #include <sys/systm.h>
     37 #include <sys/types.h>
     38 #include <sys/device.h>
     39 #include <sys/bus.h>
     40 #include <sys/conf.h>
     41 #include <sys/kmem.h>
     42 
     43 #include <machine/cpu.h>
     44 
     45 #include <amiga/amiga/device.h>
     46 
     47 #include <dev/ata/atavar.h>
     48 #include <dev/ic/wdcvar.h>
     49 
     50 #include <amiga/dev/zbusvar.h>
     51 #include <amiga/dev/xsurfvar.h>
     52 
     53 #define WDC_XSURF_CHANNELS	2
     54 #define WDC_XSURF_CHANSIZE	0x30
     55 #define WDC_XSURF_CHANOFF	0x2000
     56 
     57 #define WDC_XSURF_ISR_OFF	0x7E
     58 #define WDC_XSURF_ISR_HIGH	0x80
     59 
     60 #define WDC_XSURF_OFF_DATA	0x0
     61 #define WDC_XSURF_OFF_ERROR	0x6
     62 #define WDC_XSURF_OFF_SECCNT	0xA
     63 #define WDC_XSURF_OFF_SECTOR	0xE
     64 #define WDC_XSURF_OFF_CYL_LO	0x12
     65 #define WDC_XSURF_OFF_CYL_HI	0x16
     66 #define WDC_XSURF_OFF_SDH	0x1A
     67 #define WDC_XSURF_OFF_COMMAND	0x1E
     68 
     69 struct wdc_xsurf_port {
     70 	struct ata_channel	channel;
     71 	struct ata_queue	queue;
     72 	struct wdc_regs		wdr;
     73 };
     74 
     75 struct wdc_xsurf_softc {
     76 	struct wdc_softc 	sc_wdcdev;
     77 	struct ata_channel 	*sc_chanarray[WDC_XSURF_CHANNELS];
     78 	struct wdc_xsurf_port	sc_ports[WDC_XSURF_CHANNELS];
     79 
     80 	struct bus_space_tag	sc_bst;
     81 	bus_space_tag_t		sc_cmdt;
     82 	bus_space_handle_t	sc_isrh;
     83 
     84 	struct isr		sc_isr;
     85 };
     86 
     87 static int	wdc_xsurf_match(device_t, cfdata_t, void *);
     88 static void	wdc_xsurf_attach(device_t, device_t, void *);
     89 void		wdc_xsurf_attach_channel(struct wdc_xsurf_softc *, int);
     90 void		wdc_xsurf_map_channel(struct wdc_xsurf_softc *, int);
     91 int		wdc_xsurf_intr(void *arg);
     92 
     93 CFATTACH_DECL_NEW(wdc_xsurf, sizeof(struct wdc_xsurf_softc),
     94     wdc_xsurf_match, wdc_xsurf_attach, NULL, NULL);
     95 
     96 static const unsigned int wdc_xsurf_wdr_offsets[] = {
     97     WDC_XSURF_OFF_DATA, WDC_XSURF_OFF_ERROR, WDC_XSURF_OFF_SECCNT,
     98     WDC_XSURF_OFF_SECTOR, WDC_XSURF_OFF_CYL_LO, WDC_XSURF_OFF_CYL_HI,
     99     WDC_XSURF_OFF_SDH, WDC_XSURF_OFF_COMMAND
    100 };
    101 
    102 static int
    103 wdc_xsurf_match(device_t parent, cfdata_t cf, void *aux)
    104 {
    105 	struct xsurfbus_attach_args *xsb_aa = aux;
    106 
    107 	if (strcmp(xsb_aa->xaa_name, "wdc_xsurf") != 0)
    108 		return 0;
    109 
    110 	return 1;
    111 }
    112 
    113 static void
    114 wdc_xsurf_attach(device_t parent, device_t self, void *aux)
    115 {
    116 	struct wdc_xsurf_softc *sc;
    117 	struct xsurfbus_attach_args *xsb_aa;
    118 	int i;
    119 
    120 	aprint_normal("\n");
    121 	aprint_naive("\n");
    122 	xsb_aa = aux;
    123 	sc = device_private(self);
    124 
    125 	sc->sc_bst.base = xsb_aa->xaa_base;
    126 	sc->sc_bst.absm = &amiga_bus_stride_1swap;
    127 	sc->sc_cmdt = &sc->sc_bst;
    128 
    129 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
    130 	sc->sc_wdcdev.sc_atac.atac_nchannels = WDC_XSURF_CHANNELS;
    131 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    132 	sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanarray;
    133 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16;
    134 	sc->sc_wdcdev.wdc_maxdrives = 2;
    135 	/* this controller has no aux control registers */
    136 	sc->sc_wdcdev.cap = WDC_CAPABILITY_NO_AUXCTL;
    137 
    138 	/* attach the channels */
    139 	wdc_allocate_regs(&sc->sc_wdcdev);
    140 	for (i = 0; i < WDC_XSURF_CHANNELS; i++) {
    141 		wdc_xsurf_attach_channel(sc, i);
    142 	}
    143 
    144 	/* map interrupt status register */
    145 	bus_space_map(sc->sc_cmdt, WDC_XSURF_ISR_OFF,
    146 	    1, 0, &sc->sc_isrh);
    147 
    148 	/* attach interrupt */
    149 	sc->sc_isr.isr_intr = wdc_xsurf_intr;
    150 	sc->sc_isr.isr_arg = sc;
    151 	sc->sc_isr.isr_ipl = 2;
    152 	add_isr(&sc->sc_isr);
    153 }
    154 
    155 void
    156 wdc_xsurf_attach_channel(struct wdc_xsurf_softc *sc, int chnum)
    157 {
    158 #ifdef WDC_XSURF_DEBUG
    159 	device_t self;
    160 
    161 	self = sc->sc_wdcdev.sc_atac.atac_dev;
    162 #endif /* WDC_XSURF_DEBUG */
    163 
    164 	sc->sc_chanarray[chnum] = &sc->sc_ports[chnum].channel;
    165 	memset(&sc->sc_ports[chnum],0,sizeof(struct wdc_xsurf_port));
    166 	sc->sc_ports[chnum].channel.ch_channel = chnum;
    167 	sc->sc_ports[chnum].channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    168 	sc->sc_ports[chnum].channel.ch_queue = &sc->sc_ports[chnum].queue;
    169 
    170 	wdc_xsurf_map_channel(sc, chnum);
    171 
    172 	wdc_init_shadow_regs(&sc->sc_ports[chnum].channel);
    173 	wdcattach(&sc->sc_ports[chnum].channel);
    174 
    175 #ifdef WDC_XSURF_DEBUG
    176 	aprint_normal_dev(self, "done init for channel %d\n", chnum);
    177 #endif /* WDC_XSURF_DEBUG */
    178 }
    179 
    180 void
    181 wdc_xsurf_map_channel(struct wdc_xsurf_softc *sc, int chnum)
    182 {
    183 	struct wdc_regs *wdr;
    184 	int i;
    185 
    186 	wdr = CHAN_TO_WDC_REGS(&sc->sc_ports[chnum].channel);
    187 
    188 	/* map the registers */
    189 	wdr->cmd_iot = sc->sc_cmdt;
    190 	bus_space_map(sc->sc_cmdt, chnum * WDC_XSURF_CHANOFF,
    191 	    WDC_XSURF_CHANSIZE, 0, &wdr->cmd_baseioh);
    192 
    193 	for (i = 0; i < WDC_NREG; i++)
    194 		bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
    195 		    wdc_xsurf_wdr_offsets[i], i == 0 ? 2 : 1,
    196 		    &wdr->cmd_iohs[i]);
    197 }
    198 
    199 int
    200 wdc_xsurf_intr(void *arg)
    201 {
    202 	struct wdc_xsurf_softc *sc;
    203 	uint8_t intreq;
    204 	int r1, r2;
    205 
    206 	sc  = (struct wdc_xsurf_softc *)arg;
    207 	r1 = r2 = 0;
    208 
    209 	intreq = bus_space_read_1(sc->sc_cmdt, sc->sc_isrh, 0);
    210 	bus_space_write_1(sc->sc_cmdt, sc->sc_isrh, 0, 0); /* pull A11 down */
    211 
    212 	/* only one register for both channels... :/ */
    213 	if (intreq & WDC_XSURF_ISR_HIGH) {
    214 		r1 = wdcintr(&sc->sc_ports[0].channel);
    215 		r2 = wdcintr(&sc->sc_ports[1].channel);
    216 	}
    217 
    218 	return r1 | r2;
    219 }
    220