Home | History | Annotate | Line # | Download | only in dev
wdc_acafh.c revision 1.2
      1 /*	$NetBSD: wdc_acafh.c,v 1.2 2013/12/22 23:02:38 rkujawa Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2000, 2003, 2013 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  * This code is derived from software contributed to The NetBSD Foundation
     11  * by Michael L. Hitch.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * ACA500 CF (IDE) controller driver.
     37  *
     38  * The hardware emulates original A600/A1200 Gayle interface. However, it has
     39  * two channels, second channel placed instead of ctl registers (so software
     40  * reset of the bus is not possible, duh). There are no slave devices.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: wdc_acafh.c,v 1.2 2013/12/22 23:02:38 rkujawa Exp $");
     45 
     46 #include <sys/types.h>
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/malloc.h>
     50 #include <sys/device.h>
     51 #include <sys/bus.h>
     52 
     53 #include <machine/cpu.h>
     54 #include <machine/intr.h>
     55 #include <sys/bswap.h>
     56 
     57 #include <amiga/amiga/cia.h>
     58 #include <amiga/amiga/custom.h>
     59 #include <amiga/amiga/device.h>
     60 #include <amiga/amiga/gayle.h>
     61 #include <amiga/dev/zbusvar.h>
     62 #include <amiga/dev/acafhvar.h>
     63 #include <amiga/dev/acafhreg.h>
     64 
     65 #include <dev/ata/atavar.h>
     66 #include <dev/ic/wdcvar.h>
     67 
     68 #define WDC_ACAFH_SLOTS 2
     69 
     70 struct wdc_acafh_slot {
     71 	struct ata_channel	channel;
     72 	struct ata_queue	chqueue;
     73 	struct wdc_regs		wdr;
     74 };
     75 
     76 struct wdc_acafh_softc {
     77 	device_t		sc_dev;
     78 
     79 	struct wdc_softc	sc_wdcdev;
     80 	struct ata_channel	*sc_chanlist[WDC_ACAFH_SLOTS];
     81 	struct wdc_acafh_slot	sc_slots[WDC_ACAFH_SLOTS];
     82 
     83 	struct isr		sc_isr;
     84 	volatile u_char		*sc_intreg;
     85 
     86 	struct bus_space_tag	cmd_iot;
     87 
     88 	struct acafh_softc	*aca_sc;
     89 };
     90 
     91 int		wdc_acafh_match(device_t, cfdata_t, void *);
     92 void		wdc_acafh_attach(device_t, device_t, void *);
     93 int		wdc_acafh_intr(void *);
     94 static void	wdc_acafh_attach_channel(struct wdc_acafh_softc *, int);
     95 static void	wdc_acafh_map_channel(struct wdc_acafh_softc *, int);
     96 
     97 CFATTACH_DECL_NEW(wdc_acafh, sizeof(struct wdc_acafh_softc),
     98     wdc_acafh_match, wdc_acafh_attach, NULL, NULL);
     99 
    100 int
    101 wdc_acafh_match(device_t parent, cfdata_t cfp, void *aux)
    102 {
    103 	struct acafhbus_attach_args *aap = aux;
    104 
    105 	if (strcmp(aap->aaa_name, "wdc_acafh") != 0)
    106 		return(0);
    107 	return 1;
    108 }
    109 
    110 void
    111 wdc_acafh_attach(device_t parent, device_t self, void *aux)
    112 {
    113 	struct wdc_acafh_softc *sc = device_private(self);
    114 	int i;
    115 
    116 	aprint_normal(": ACA500 CompactFlash interface\n");
    117 
    118 	sc->sc_dev = device_private(self);
    119 	sc->aca_sc = device_private(parent);
    120 
    121 	gayle_init();
    122 	sc->sc_intreg = &gayle.intreq;
    123 
    124 	/* XXX: take addr from attach args? */
    125 	sc->cmd_iot.base = (u_long) ztwomap(GAYLE_IDE_BASE + 2);
    126 	sc->cmd_iot.absm = &amiga_bus_stride_4swap;
    127 
    128 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    129 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16;
    130 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
    131 	sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
    132 	sc->sc_wdcdev.sc_atac.atac_nchannels = WDC_ACAFH_SLOTS;
    133 	sc->sc_wdcdev.wdc_maxdrives = 2;
    134 	sc->sc_wdcdev.cap = WDC_CAPABILITY_NO_AUXCTL;
    135 
    136 	wdc_allocate_regs(&sc->sc_wdcdev);
    137 	for (i = 0; i < WDC_ACAFH_SLOTS; i++) {
    138 		wdc_acafh_attach_channel(sc, i);
    139 	}
    140 
    141 	sc->sc_isr.isr_intr = wdc_acafh_intr;
    142 	sc->sc_isr.isr_arg = sc;
    143 	sc->sc_isr.isr_ipl = 2;
    144 	add_isr (&sc->sc_isr);
    145 
    146 	gayle.intena |= GAYLE_INT_IDE;
    147 
    148 }
    149 
    150 void
    151 wdc_acafh_attach_channel(struct wdc_acafh_softc *sc, int chnum)
    152 {
    153 	sc->sc_chanlist[chnum] = &sc->sc_slots[chnum].channel;
    154 	memset(&sc->sc_slots[chnum],0,sizeof(struct wdc_acafh_slot));
    155 	sc->sc_slots[chnum].channel.ch_channel = chnum;
    156 	sc->sc_slots[chnum].channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    157 	sc->sc_slots[chnum].channel.ch_queue = &sc->sc_slots[chnum].chqueue;
    158 
    159 	wdc_acafh_map_channel(sc, chnum);
    160 
    161 	wdc_init_shadow_regs(&sc->sc_slots[chnum].channel);
    162 	wdcattach(&sc->sc_slots[chnum].channel);
    163 
    164 #ifdef WDC_ACAFH_DEBUG
    165 	aprint_normal_dev(sc->sc_dev, "done init for channel %d\n", chnum);
    166 #endif /* WDC_ACAFH_DEBUG */
    167 
    168 }
    169 
    170 void
    171 wdc_acafh_map_channel(struct wdc_acafh_softc *sc, int chnum)
    172 {
    173 	struct wdc_regs *wdr;
    174 	int i;
    175 	bus_addr_t off;
    176 
    177 	wdr = CHAN_TO_WDC_REGS(&sc->sc_slots[chnum].channel);
    178 	wdr->cmd_iot = &sc->cmd_iot;
    179 
    180 	if (chnum == 0)
    181 		off = 0;
    182 	else
    183 		off = 0x400;
    184 
    185 	if (bus_space_map(wdr->cmd_iot, off, 0x40, 0,
    186 			  &wdr->cmd_baseioh)) {
    187 		aprint_error_dev(sc->sc_dev, "couldn't map regs\n");
    188 		return;
    189 	}
    190 
    191 	for (i = 0; i < WDC_NREG; i++) {
    192 		if (bus_space_subregion(wdr->cmd_iot,
    193 		    wdr->cmd_baseioh, i, i == 0 ? 4 : 1,
    194 		    &wdr->cmd_iohs[i]) != 0) {
    195 
    196 			bus_space_unmap(wdr->cmd_iot,
    197 			    wdr->cmd_baseioh, 0x40);
    198 			aprint_error_dev(sc->sc_dev, "couldn't map regs\n");
    199 			return;
    200 		}
    201 	}
    202 
    203 }
    204 
    205 int
    206 wdc_acafh_intr(void *arg)
    207 {
    208 	struct wdc_acafh_softc *sc = (struct wdc_acafh_softc *)arg;
    209 	int ret = 0;
    210 	u_char intreq = *sc->sc_intreg;
    211 
    212 	if (intreq & GAYLE_INT_IDE) {
    213 		if (acafh_cf_intr_status(sc->aca_sc, 1) == 1) {
    214 			ret = wdcintr(&sc->sc_slots[1].channel);
    215 		}
    216 		if (acafh_cf_intr_status(sc->aca_sc, 0) == 1) {
    217 			ret = wdcintr(&sc->sc_slots[0].channel);
    218 		}
    219 		gayle.intreq = 0x7c | (intreq & 0x03);
    220 	}
    221 
    222 	return ret;
    223 }
    224 
    225