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