Home | History | Annotate | Line # | Download | only in dev
wdc_mb.c revision 1.38.2.1
      1  1.38.2.1  jdolecek /*	$NetBSD: wdc_mb.c,v 1.38.2.1 2017/12/03 11:35:57 jdolecek Exp $	*/
      2       1.1       leo 
      3       1.2   mycroft /*-
      4      1.13   mycroft  * Copyright (c) 1998, 2003 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  *
     19       1.2   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.2   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.2   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.2   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.2   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.2   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.2   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.2   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.2   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.2   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.2   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1       leo  */
     31      1.12     lukem 
     32      1.12     lukem #include <sys/cdefs.h>
     33  1.38.2.1  jdolecek __KERNEL_RCSID(0, "$NetBSD: wdc_mb.c,v 1.38.2.1 2017/12/03 11:35:57 jdolecek Exp $");
     34       1.1       leo 
     35       1.1       leo #include <sys/types.h>
     36       1.1       leo #include <sys/param.h>
     37       1.1       leo #include <sys/systm.h>
     38       1.3    bouyer #include <sys/malloc.h>
     39       1.1       leo #include <sys/device.h>
     40       1.1       leo 
     41      1.28       dsl #include <sys/bswap.h>
     42       1.1       leo #include <machine/cpu.h>
     43      1.35    dyoung #include <sys/bus.h>
     44       1.1       leo #include <machine/iomap.h>
     45       1.1       leo #include <machine/mfp.h>
     46       1.1       leo #include <machine/dma.h>
     47       1.1       leo 
     48       1.3    bouyer #include <dev/ata/atavar.h>
     49       1.1       leo #include <dev/ic/wdcvar.h>
     50       1.1       leo 
     51       1.1       leo #include <m68k/asm_single.h>
     52       1.1       leo 
     53       1.1       leo #include <atari/dev/ym2149reg.h>
     54       1.1       leo #include <atari/atari/device.h>
     55       1.1       leo 
     56      1.25       jdc /* Falcon IDE register locations (base and offsets). */
     57      1.25       jdc #define FALCON_WD_BASE	0xfff00000
     58      1.25       jdc #define FALCON_WD_LEN	0x40
     59      1.29   tsutsui #define FALCON_WD_AUX	0x38
     60      1.25       jdc 
     61       1.1       leo /*
     62       1.1       leo  * XXX This code currently doesn't even try to allow 32-bit data port use.
     63       1.1       leo  */
     64      1.34   tsutsui static int	claim_hw(struct ata_channel *, int);
     65      1.34   tsutsui static void	free_hw(struct ata_channel *);
     66      1.34   tsutsui static void	read_multi_2_swap(bus_space_tag_t, bus_space_handle_t,
     67      1.34   tsutsui 		    bus_size_t, uint16_t *, bus_size_t);
     68      1.34   tsutsui static void	write_multi_2_swap(bus_space_tag_t, bus_space_handle_t,
     69      1.34   tsutsui 		    bus_size_t, const uint16_t *, bus_size_t);
     70       1.1       leo 
     71       1.1       leo struct wdc_mb_softc {
     72       1.1       leo 	struct wdc_softc sc_wdcdev;
     73      1.34   tsutsui 	struct ata_channel *sc_chanlist[1];
     74      1.34   tsutsui 	struct ata_channel sc_channel;
     75      1.34   tsutsui 	struct wdc_regs sc_wdc_regs;
     76      1.34   tsutsui 	void *sc_ih;
     77       1.1       leo };
     78       1.1       leo 
     79      1.34   tsutsui int	wdc_mb_probe(device_t, struct cfdata *, void *);
     80      1.34   tsutsui void	wdc_mb_attach(device_t, device_t, void *);
     81       1.1       leo 
     82      1.31      cube CFATTACH_DECL_NEW(wdc_mb, sizeof(struct wdc_mb_softc),
     83      1.11   thorpej     wdc_mb_probe, wdc_mb_attach, NULL, NULL);
     84       1.1       leo 
     85       1.1       leo int
     86      1.31      cube wdc_mb_probe(device_t parent, cfdata_t cfp, void *aux)
     87       1.1       leo {
     88      1.34   tsutsui 	static int wdc_matched = 0;
     89      1.23   thorpej 	struct wdc_regs wdr;
     90      1.34   tsutsui 	int result = 0, i;
     91      1.34   tsutsui 	uint8_t sv_ierb;
     92       1.1       leo 
     93       1.9       leo 	if ((machineid & ATARI_TT) || strcmp("wdc", aux) || wdc_matched)
     94       1.1       leo 		return 0;
     95       1.1       leo 	if (!atari_realconfig)
     96       1.1       leo 		return 0;
     97       1.8   thorpej 
     98      1.23   thorpej 	wdr.cmd_iot = wdr.ctl_iot = mb_alloc_bus_space_tag();
     99      1.23   thorpej 	if (wdr.cmd_iot == NULL)
    100       1.1       leo 		return 0;
    101      1.29   tsutsui 	wdr.cmd_iot->stride = 0;
    102      1.23   thorpej 	wdr.cmd_iot->wo_1   = 1;
    103       1.1       leo 
    104      1.25       jdc 	if (bus_space_map(wdr.cmd_iot, FALCON_WD_BASE, FALCON_WD_LEN, 0,
    105      1.25       jdc 	    &wdr.cmd_baseioh))
    106      1.29   tsutsui 		goto out;
    107      1.30   tsutsui 	for (i = 0; i < WDC_NREG; i++) {
    108      1.25       jdc 		if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh,
    109      1.29   tsutsui 		    i * 4, 4, &wdr.cmd_iohs[i]) != 0)
    110      1.29   tsutsui 			goto outunmap;
    111      1.29   tsutsui 	}
    112  1.38.2.1  jdolecek 	wdc_init_shadow_regs(&wdr);
    113      1.29   tsutsui 
    114      1.29   tsutsui 	if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh, FALCON_WD_AUX, 4,
    115      1.29   tsutsui 	    &wdr.ctl_ioh))
    116      1.29   tsutsui 		goto outunmap;
    117       1.1       leo 
    118       1.1       leo 	/*
    119       1.1       leo 	 * Make sure IDE interrupts are disabled during probing.
    120       1.1       leo 	 */
    121       1.1       leo 	sv_ierb = MFP->mf_ierb;
    122       1.1       leo 	MFP->mf_ierb &= ~IB_DINT;
    123       1.1       leo 
    124       1.1       leo 	/*
    125       1.1       leo 	 * Make sure that IDE is turned on on the Falcon.
    126       1.1       leo 	 */
    127       1.1       leo 	if (machineid & ATARI_FALCON)
    128       1.1       leo 		ym2149_ser2(0);
    129       1.1       leo 
    130  1.38.2.1  jdolecek 	result = wdcprobe(&wdr);
    131       1.1       leo 
    132       1.1       leo 	MFP->mf_ierb = sv_ierb;
    133       1.1       leo 
    134      1.29   tsutsui  outunmap:
    135      1.25       jdc 	bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, FALCON_WD_LEN);
    136      1.29   tsutsui  out:
    137      1.23   thorpej 	mb_free_bus_space_tag(wdr.cmd_iot);
    138       1.1       leo 
    139       1.9       leo 	if (result)
    140       1.9       leo 		wdc_matched = 1;
    141      1.34   tsutsui 	return result;
    142       1.1       leo }
    143       1.1       leo 
    144       1.1       leo void
    145      1.31      cube wdc_mb_attach(device_t parent, device_t self, void *aux)
    146       1.1       leo {
    147      1.31      cube 	struct wdc_mb_softc *sc = device_private(self);
    148      1.23   thorpej 	struct wdc_regs *wdr;
    149      1.25       jdc 	int i;
    150       1.1       leo 
    151      1.31      cube 	aprint_normal("\n");
    152       1.1       leo 
    153      1.31      cube 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    154      1.23   thorpej 	sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
    155      1.23   thorpej 	wdr->cmd_iot = wdr->ctl_iot =
    156       1.3    bouyer 	    mb_alloc_bus_space_tag();
    157      1.29   tsutsui 	wdr->cmd_iot->stride = 0;
    158      1.23   thorpej 	wdr->cmd_iot->wo_1   = 1;
    159      1.23   thorpej 	wdr->cmd_iot->abs_rms_2 = read_multi_2_swap;
    160      1.23   thorpej 	wdr->cmd_iot->abs_wms_2 = write_multi_2_swap;
    161      1.25       jdc 	if (bus_space_map(wdr->cmd_iot, FALCON_WD_BASE, FALCON_WD_LEN, 0,
    162      1.23   thorpej 			  &wdr->cmd_baseioh)) {
    163      1.31      cube 		aprint_error_dev(self, "couldn't map registers\n");
    164       1.1       leo 		return;
    165       1.1       leo 	}
    166      1.29   tsutsui 	for (i = 0; i < WDC_NREG; i++) {
    167      1.25       jdc 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
    168      1.29   tsutsui 		    i * 4, 4, &wdr->cmd_iohs[i]) != 0) {
    169      1.31      cube 			aprint_error_dev(self,
    170      1.31      cube 			    "couldn't subregion cmd reg %i\n", i);
    171      1.25       jdc 			bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
    172      1.25       jdc 			    FALCON_WD_LEN);
    173      1.25       jdc 			return;
    174      1.25       jdc 		}
    175      1.29   tsutsui 	}
    176      1.29   tsutsui 
    177      1.29   tsutsui 	if (bus_space_subregion(wdr->cmd_iot,
    178      1.29   tsutsui 	    wdr->cmd_baseioh, FALCON_WD_AUX, 4, &wdr->ctl_ioh)) {
    179      1.29   tsutsui 		bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, FALCON_WD_LEN);
    180      1.31      cube 		aprint_error_dev(self, "couldn't subregion aux reg\n");
    181      1.29   tsutsui 		return;
    182      1.29   tsutsui 	}
    183       1.1       leo 
    184       1.1       leo 	/*
    185       1.1       leo 	 * Play a nasty trick here. Normally we only manipulate the
    186       1.1       leo 	 * interrupt *mask*. However to defeat wd_get_parms(), we
    187       1.1       leo 	 * disable the interrupts here using the *enable* register.
    188       1.1       leo 	 */
    189       1.1       leo 	MFP->mf_ierb &= ~IB_DINT;
    190       1.1       leo 
    191      1.24   thorpej 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 |
    192      1.24   thorpej 	    ATAC_CAP_ATA_NOSTREAM;
    193      1.24   thorpej 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
    194      1.24   thorpej 	sc->sc_wdcdev.sc_atac.atac_claim_hw = &claim_hw;
    195      1.24   thorpej 	sc->sc_wdcdev.sc_atac.atac_free_hw  = &free_hw;
    196      1.23   thorpej 	sc->sc_chanlist[0] = &sc->sc_channel;
    197      1.24   thorpej 	sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
    198      1.24   thorpej 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
    199      1.38    bouyer 	sc->sc_wdcdev.wdc_maxdrives = 2;
    200      1.23   thorpej 	sc->sc_channel.ch_channel = 0;
    201      1.24   thorpej 	sc->sc_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
    202  1.38.2.1  jdolecek 
    203  1.38.2.1  jdolecek 	wdc_init_shadow_regs(wdr);
    204       1.1       leo 
    205       1.1       leo 	/*
    206       1.1       leo 	 * Setup & enable disk related interrupts.
    207       1.1       leo 	 */
    208       1.7       leo 	MFP->mf_ierb |= IB_DINT;
    209      1.34   tsutsui 	MFP->mf_iprb  = (uint8_t)~IB_DINT;
    210       1.7       leo 	MFP->mf_imrb |= IB_DINT;
    211      1.13   mycroft 
    212      1.23   thorpej 	wdcattach(&sc->sc_channel);
    213       1.1       leo }
    214       1.1       leo 
    215       1.1       leo /*
    216       1.1       leo  * Hardware locking
    217       1.1       leo  */
    218       1.1       leo static int	wd_lock;
    219       1.1       leo 
    220       1.1       leo static int
    221      1.34   tsutsui claim_hw(struct ata_channel *chp, int maysleep)
    222       1.1       leo {
    223  1.38.2.1  jdolecek 	ata_channel_lock_owned(chp);
    224      1.34   tsutsui 
    225       1.1       leo 	if (wd_lock != DMA_LOCK_GRANT) {
    226       1.1       leo 		if (wd_lock == DMA_LOCK_REQ) {
    227       1.1       leo 			/*
    228       1.1       leo 			 * ST_DMA access is being claimed.
    229       1.1       leo 			 */
    230       1.1       leo 			return 0;
    231       1.1       leo 		}
    232       1.1       leo 		if (!st_dmagrab((dma_farg)wdcintr,
    233      1.24   thorpej 		    (dma_farg)(maysleep ? NULL : wdcrestart), chp,
    234  1.38.2.1  jdolecek 		    &wd_lock, 1, &chp->ch_lock))
    235       1.1       leo 			return 0;
    236       1.1       leo 	}
    237       1.1       leo 	return 1;
    238       1.1       leo }
    239       1.1       leo 
    240       1.1       leo static void
    241      1.34   tsutsui free_hw(struct ata_channel *chp)
    242       1.1       leo {
    243  1.38.2.1  jdolecek 	ata_channel_lock_owned(chp);
    244      1.34   tsutsui 
    245       1.1       leo 	/*
    246       1.1       leo 	 * Flush pending interrupts before giving-up lock
    247       1.1       leo 	 */
    248      1.34   tsutsui 	MFP->mf_iprb = (uint8_t)~IB_DINT;
    249       1.1       leo 
    250       1.1       leo 	/*
    251       1.1       leo 	 * Only free the lock on a Falcon. On the Hades, keep it.
    252       1.1       leo 	 */
    253       1.1       leo /*	if (machineid & ATARI_FALCON) */
    254      1.24   thorpej 		st_dmafree(chp, &wd_lock);
    255       1.3    bouyer }
    256       1.3    bouyer 
    257       1.3    bouyer /*
    258       1.3    bouyer  * XXX
    259       1.3    bouyer  * This piece of uglyness is caused by the fact that the byte lanes of
    260       1.3    bouyer  * the data-register are swapped on the atari. This works OK for an IDE
    261       1.3    bouyer  * disk, but turns into a nightmare when used on atapi devices.
    262       1.3    bouyer  */
    263       1.3    bouyer #define calc_addr(base, off, stride, wm)	\
    264       1.3    bouyer 	((u_long)(base) + ((off) << (stride)) + (wm))
    265       1.3    bouyer 
    266       1.3    bouyer static void
    267      1.34   tsutsui read_multi_2_swap(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
    268      1.34   tsutsui     uint16_t *a, bus_size_t c)
    269       1.3    bouyer {
    270      1.34   tsutsui 	volatile uint16_t *ba;
    271       1.3    bouyer 
    272      1.34   tsutsui 	ba = (volatile uint16_t *)calc_addr(h, o, t->stride, t->wo_2);
    273       1.3    bouyer 	for (; c; a++, c--)
    274       1.3    bouyer 		*a = bswap16(*ba);
    275       1.3    bouyer }
    276       1.3    bouyer 
    277       1.3    bouyer static void
    278      1.34   tsutsui write_multi_2_swap(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
    279      1.34   tsutsui     const uint16_t *a, bus_size_t c)
    280       1.3    bouyer {
    281      1.34   tsutsui 	volatile uint16_t *ba;
    282       1.3    bouyer 
    283      1.34   tsutsui 	ba = (volatile uint16_t *)calc_addr(h, o, t->stride, t->wo_2);
    284       1.3    bouyer 	for (; c; a++, c--)
    285       1.3    bouyer 		*ba = bswap16(*a);
    286       1.1       leo }
    287