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