Home | History | Annotate | Line # | Download | only in dev
wdc_mb.c revision 1.1.2.3
      1 /*	$NetBSD: wdc_mb.c,v 1.1.2.3 1998/06/23 08:07:28 leo Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
      5  *
      6  * DMA and multi-sector PIO handling are derived from code contributed by
      7  * Onno van der Linden.
      8  *
      9  * ISA attachment created by Christopher G. Demetriou.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by Charles M. Hannum.
     22  * 4. The name of the author may not be used to endorse or promote products
     23  *    derived from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/types.h>
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/malloc.h>
     41 #include <sys/device.h>
     42 
     43 #include <machine/cpu.h>
     44 #include <machine/bus.h>
     45 #include <machine/iomap.h>
     46 #include <machine/mfp.h>
     47 #include <machine/dma.h>
     48 
     49 #include <dev/ata/atavar.h>
     50 #include <dev/ic/wdcvar.h>
     51 
     52 #include <m68k/asm_single.h>
     53 
     54 #include <atari/dev/ym2149reg.h>
     55 #include <atari/atari/device.h>
     56 
     57 /*
     58  * XXX This code currently doesn't even try to allow 32-bit data port use.
     59  */
     60 static int	claim_hw __P((void *, int));
     61 static void	free_hw __P((void *));
     62 static void	read_multi_2_swap __P((bus_space_tag_t, bus_space_handle_t,
     63 				bus_size_t, u_int16_t *, bus_size_t));
     64 static void	write_multi_2_swap __P((bus_space_tag_t, bus_space_handle_t,
     65 				bus_size_t, const u_int16_t *, bus_size_t));
     66 
     67 struct wdc_mb_softc {
     68 	struct wdc_softc sc_wdcdev;
     69 	struct  channel_softc wdc_channel;
     70 	void	*sc_ih;
     71 };
     72 
     73 int	wdc_mb_probe	__P((struct device *, struct cfdata *, void *));
     74 void	wdc_mb_attach	__P((struct device *, struct device *, void *));
     75 
     76 struct cfattach wdc_mb_ca = {
     77 	sizeof(struct wdc_mb_softc), wdc_mb_probe, wdc_mb_attach
     78 };
     79 
     80 int
     81 wdc_mb_probe(parent, cfp, aux)
     82 	struct device *parent;
     83 	struct cfdata *cfp;
     84 	void *aux;
     85 {
     86 #if 0 /* XXX memset */
     87 	struct channel_softc ch = { 0 };
     88 #else /* XXX memset */
     89 	struct channel_softc ch;
     90 #endif /* XXX memset */
     91 	int	result = 0;
     92 	u_char	sv_ierb;
     93 
     94 #if 0 /* XXX memset */
     95 #else /* XXX memset */
     96 	bzero(&ch, sizeof ch);
     97 #endif /* XXX memset */
     98 
     99 	if ((machineid & ATARI_TT) || strcmp("wdc", aux) || cfp->cf_unit != 0)
    100 		return 0;
    101 	if (!atari_realconfig)
    102 		return 0;
    103 
    104 	ch.cmd_iot = ch.ctl_iot = mb_alloc_bus_space_tag();
    105 	if (ch.cmd_iot == NULL)
    106 		return 0;
    107 	ch.cmd_iot->stride = 2;
    108 	ch.cmd_iot->wo_1   = 1;
    109 
    110 	if (bus_space_map(ch.cmd_iot, 0xfff00000, 0x40, 0, &ch.cmd_ioh))
    111 		return 0;
    112 	if (bus_space_subregion(ch.cmd_iot, ch.cmd_ioh, 0x38, 1, &ch.ctl_ioh))
    113 		return 0;
    114 
    115 	/*
    116 	 * Make sure IDE interrupts are disabled during probing.
    117 	 */
    118 	sv_ierb = MFP->mf_ierb;
    119 	MFP->mf_ierb &= ~IB_DINT;
    120 
    121 	/*
    122 	 * Make sure that IDE is turned on on the Falcon.
    123 	 */
    124 	if (machineid & ATARI_FALCON)
    125 		ym2149_ser2(0);
    126 
    127 	result = wdcprobe(&ch);
    128 
    129 	MFP->mf_ierb = sv_ierb;
    130 
    131 	bus_space_unmap(ch.cmd_iot,  ch.cmd_ioh, 0x40);
    132 	mb_free_bus_space_tag(ch.cmd_iot);
    133 
    134 	return (result);
    135 }
    136 
    137 void
    138 wdc_mb_attach(parent, self, aux)
    139 	struct device *parent, *self;
    140 	void *aux;
    141 {
    142 	struct wdc_mb_softc *sc = (void *)self;
    143 
    144 	printf("\n");
    145 
    146 	sc->wdc_channel.cmd_iot = sc->wdc_channel.ctl_iot =
    147 	    mb_alloc_bus_space_tag();
    148 	sc->wdc_channel.cmd_iot->stride = 2;
    149 	sc->wdc_channel.cmd_iot->wo_1   = 1;
    150 	sc->wdc_channel.cmd_iot->abs_rms_2 = read_multi_2_swap;
    151 	sc->wdc_channel.cmd_iot->abs_wms_2 = write_multi_2_swap;
    152 	if (bus_space_map(sc->wdc_channel.cmd_iot, 0xfff00000, 0x40, 0,
    153 			  &sc->wdc_channel.cmd_ioh)) {
    154 		printf("%s: couldn't map registers\n",
    155 		    sc->sc_wdcdev.sc_dev.dv_xname);
    156 		return;
    157 	}
    158 	if (bus_space_subregion(sc->wdc_channel.cmd_iot,
    159 	    sc->wdc_channel.cmd_ioh, 0x38, 1, &sc->wdc_channel.ctl_ioh))
    160 		return;
    161 
    162 	/*
    163 	 * Play a nasty trick here. Normally we only manipulate the
    164 	 * interrupt *mask*. However to defeat wd_get_parms(), we
    165 	 * disable the interrupts here using the *enable* register.
    166 	 */
    167 	MFP->mf_ierb &= ~IB_DINT;
    168 
    169 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_HWLOCK|WDC_CAPABILITY_ATA_NOSTREAM;
    170 	sc->sc_wdcdev.pio_mode = 0;
    171 	sc->sc_wdcdev.claim_hw = &claim_hw;
    172 	sc->sc_wdcdev.free_hw  = &free_hw;
    173 	sc->sc_wdcdev.channels = &sc->wdc_channel;
    174 	sc->sc_wdcdev.nchannels = 1;
    175 	sc->wdc_channel.channel = 0;
    176 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
    177 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
    178 	    M_DEVBUF, M_NOWAIT);
    179 	if (sc->wdc_channel.ch_queue == NULL) {
    180 	    printf("%s: can't allocate memory for command queue",
    181 		sc->sc_wdcdev.sc_dev.dv_xname);
    182 	    return;
    183 	}
    184 	wdcattach(&sc->wdc_channel);
    185 
    186 	/*
    187 	 * Setup & enable disk related interrupts.
    188 	 */
    189 	MFP->mf_ierb  |= IB_DINT;
    190 	MFP->mf_iprb  &= ~IB_DINT;
    191 	MFP->mf_imrb  |= IB_DINT;
    192 }
    193 
    194 /*
    195  * Hardware locking
    196  */
    197 static int	wd_lock;
    198 
    199 static int
    200 claim_hw(softc, maysleep)
    201 void *softc;
    202 int  maysleep;
    203 {
    204 	if (wd_lock != DMA_LOCK_GRANT) {
    205 		if (wd_lock == DMA_LOCK_REQ) {
    206 			/*
    207 			 * ST_DMA access is being claimed.
    208 			 */
    209 			return 0;
    210 		}
    211 		if (!st_dmagrab((dma_farg)wdcintr,
    212 		    (dma_farg)(maysleep ? NULL : wdcrestart), softc,
    213 		    &wd_lock, 1))
    214 			return 0;
    215 	}
    216 	return 1;
    217 }
    218 
    219 static void
    220 free_hw(softc)
    221 void *softc;
    222 {
    223 	/*
    224 	 * Flush pending interrupts before giving-up lock
    225 	 */
    226 	single_inst_bclr_b(MFP->mf_iprb, IB_DINT);
    227 
    228 	/*
    229 	 * Only free the lock on a Falcon. On the Hades, keep it.
    230 	 */
    231 /*	if (machineid & ATARI_FALCON) */
    232 		st_dmafree(softc, &wd_lock);
    233 }
    234 
    235 /*
    236  * XXX
    237  * This piece of uglyness is caused by the fact that the byte lanes of
    238  * the data-register are swapped on the atari. This works OK for an IDE
    239  * disk, but turns into a nightmare when used on atapi devices.
    240  */
    241 #define calc_addr(base, off, stride, wm)	\
    242 	((u_long)(base) + ((off) << (stride)) + (wm))
    243 
    244 static void
    245 read_multi_2_swap(t, h, o, a, c)
    246 	bus_space_tag_t		t;
    247 	bus_space_handle_t	h;
    248 	bus_size_t		o, c;
    249 	u_int16_t		*a;
    250 {
    251 	u_int16_t	*ba;
    252 
    253 	ba = (u_int16_t *)calc_addr(h, o, t->stride, t->wo_2);
    254 	for (; c; a++, c--)
    255 		*a = bswap16(*ba);
    256 }
    257 
    258 static void
    259 write_multi_2_swap(t, h, o, a, c)
    260 	bus_space_tag_t		t;
    261 	bus_space_handle_t	h;
    262 	bus_size_t		o, c;
    263 	const u_int16_t		*a;
    264 {
    265 	u_int16_t	*ba;
    266 
    267 	ba = (u_int16_t *)calc_addr(h, o, t->stride, t->wo_2);
    268 	for (; c; a++, c--)
    269 		*ba = bswap16(*a);
    270 }
    271