Home | History | Annotate | Line # | Download | only in dev
wdc_extio.c revision 1.8
      1  1.8  bouyer /*	$NetBSD: wdc_extio.c,v 1.8 2012/07/31 15:50:33 bouyer Exp $ */
      2  1.1  dyoung 
      3  1.1  dyoung /*-
      4  1.1  dyoung  * Copyright (c) 2007 David Young.  All rights reserved.
      5  1.1  dyoung  *
      6  1.1  dyoung  * CompactFlash attachment for RouterBoard 153.  Derived from
      7  1.1  dyoung  * the NetBSD/mac68k OBIO-IDE attachment.
      8  1.1  dyoung  *
      9  1.1  dyoung  * Redistribution and use in source and binary forms, with or
     10  1.1  dyoung  * without modification, are permitted provided that the following
     11  1.1  dyoung  * conditions are met:
     12  1.1  dyoung  * 1. Redistributions of source code must retain the above copyright
     13  1.1  dyoung  *    notice, this list of conditions and the following disclaimer.
     14  1.1  dyoung  * 2. Redistributions in binary form must reproduce the above
     15  1.1  dyoung  *    copyright notice, this list of conditions and the following
     16  1.1  dyoung  *    disclaimer in the documentation and/or other materials provided
     17  1.1  dyoung  *    with the distribution.
     18  1.1  dyoung  *
     19  1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
     20  1.1  dyoung  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     21  1.1  dyoung  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     22  1.1  dyoung  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
     23  1.1  dyoung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     24  1.1  dyoung  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     25  1.1  dyoung  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
     26  1.1  dyoung  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.1  dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     28  1.1  dyoung  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     29  1.1  dyoung  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     30  1.1  dyoung  * OF SUCH DAMAGE.
     31  1.1  dyoung  */
     32  1.1  dyoung /*
     33  1.1  dyoung  * Copyright (c) 2002 Takeshi Shibagaki  All rights reserved.
     34  1.1  dyoung  *
     35  1.1  dyoung  * mac68k OBIO-IDE attachment created by Takeshi Shibagaki
     36  1.1  dyoung  *
     37  1.1  dyoung  * Redistribution and use in source and binary forms, with or without
     38  1.1  dyoung  * modification, are permitted provided that the following conditions
     39  1.1  dyoung  * are met:
     40  1.1  dyoung  * 1. Redistributions of source code must retain the above copyright
     41  1.1  dyoung  *    notice, this list of conditions and the following disclaimer.
     42  1.1  dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     43  1.1  dyoung  *    notice, this list of conditions and the following disclaimer in the
     44  1.1  dyoung  *    documentation and/or other materials provided with the distribution.
     45  1.1  dyoung  * 3. All advertising materials mentioning features or use of this software
     46  1.1  dyoung  *    must display the following acknowledgement:
     47  1.1  dyoung  *	This product includes software developed by Charles M. Hannum.
     48  1.1  dyoung  * 4. The name of the author may not be used to endorse or promote products
     49  1.1  dyoung  *    derived from this software without specific prior written permission.
     50  1.1  dyoung  *
     51  1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     52  1.1  dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53  1.1  dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54  1.1  dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     55  1.1  dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     56  1.1  dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     57  1.1  dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     58  1.1  dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     59  1.1  dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     60  1.1  dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61  1.1  dyoung  */
     62  1.1  dyoung 
     63  1.1  dyoung #include <sys/cdefs.h>
     64  1.8  bouyer __KERNEL_RCSID(0, "$NetBSD: wdc_extio.c,v 1.8 2012/07/31 15:50:33 bouyer Exp $");
     65  1.1  dyoung 
     66  1.1  dyoung #include <sys/param.h>
     67  1.5    matt #include <sys/bus.h>
     68  1.5    matt #include <sys/callout.h>
     69  1.5    matt #include <sys/cpu.h>
     70  1.1  dyoung #include <sys/device.h>
     71  1.5    matt #include <sys/intr.h>
     72  1.5    matt #include <sys/kernel.h>
     73  1.1  dyoung #include <sys/malloc.h>
     74  1.5    matt #include <sys/systm.h>
     75  1.1  dyoung 
     76  1.1  dyoung #include <mips/adm5120/include/adm5120_extiovar.h>
     77  1.1  dyoung 
     78  1.1  dyoung #include <dev/ata/atareg.h>
     79  1.1  dyoung #include <dev/ata/atavar.h>
     80  1.1  dyoung #include <dev/ic/wdcvar.h>
     81  1.1  dyoung 
     82  1.1  dyoung #ifdef WDC_EXTIO_DEBUG
     83  1.1  dyoung int wdc_extio_debug = 1;
     84  1.1  dyoung #define	WDC_EXTIO_DPRINTF(__fmt, ...)		\
     85  1.1  dyoung do {						\
     86  1.1  dyoung 	if (wdc_extio_debug)			\
     87  1.1  dyoung 		printf((__fmt), __VA_ARGS__);	\
     88  1.1  dyoung } while (/*CONSTCOND*/0)
     89  1.1  dyoung #else /* !WDC_EXTIO_DEBUG */
     90  1.1  dyoung #define	WDC_EXTIO_DPRINTF(__fmt, ...)	do { } while (/*CONSTCOND*/0)
     91  1.1  dyoung #endif /* WDC_EXTIO_DEBUG */
     92  1.1  dyoung 
     93  1.1  dyoung #define	WDC_OBIO_CF_WINSIZE	0x1000
     94  1.1  dyoung #define	WDC_OBIO_REG_OFFSET	0x0800
     95  1.1  dyoung #define	WDC_OBIO_DATA_OFFSET	0x0808
     96  1.1  dyoung #define	WDC_OBIO_AUXREG_OFFSET	0x080e
     97  1.1  dyoung 
     98  1.1  dyoung struct wdc_extio_softc {
     99  1.1  dyoung 	struct wdc_softc	sc_wdcdev;
    100  1.1  dyoung 	struct ata_channel	*sc_chanlist[1];
    101  1.1  dyoung 	struct ata_channel	sc_channel;
    102  1.1  dyoung 	struct ata_queue	sc_chqueue;
    103  1.1  dyoung 	struct wdc_regs		sc_wdc_regs;
    104  1.1  dyoung 	void			*sc_gpio;
    105  1.1  dyoung 	int			sc_map;
    106  1.1  dyoung 	struct gpio_pinmap	sc_pinmap;
    107  1.1  dyoung };
    108  1.1  dyoung 
    109  1.2    cube int	wdc_extio_match(device_t, cfdata_t, void *);
    110  1.2    cube void	wdc_extio_attach(device_t, device_t, void *);
    111  1.1  dyoung 
    112  1.2    cube CFATTACH_DECL_NEW(wdc_extio, sizeof(struct wdc_extio_softc),
    113  1.1  dyoung     wdc_extio_match, wdc_extio_attach, NULL, NULL);
    114  1.1  dyoung 
    115  1.1  dyoung #if 0
    116  1.1  dyoung void	wdc_extio_reset(struct ata_channel *, int);
    117  1.1  dyoung void
    118  1.1  dyoung wdc_extio_reset(struct ata_channel *chp, int poll)
    119  1.1  dyoung {
    120  1.1  dyoung 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
    121  1.1  dyoung 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
    122  1.1  dyoung 	uint8_t st0;
    123  1.1  dyoung 	int i, s = 0;
    124  1.1  dyoung 
    125  1.1  dyoung 	if (poll)
    126  1.1  dyoung 		s = splbio();
    127  1.1  dyoung 	if (wdc->select)
    128  1.1  dyoung 		wdc->select(chp,0);
    129  1.1  dyoung 	/* assert SRST, wait for reset to complete */
    130  1.1  dyoung 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_RST);
    131  1.1  dyoung 	delay(2000);
    132  1.1  dyoung 	(void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0);
    133  1.1  dyoung 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 0);
    134  1.1  dyoung 	delay(10);	/* 400ns delay */
    135  1.1  dyoung 	for (i = 3100000; --i > 0; ) {
    136  1.1  dyoung 		st0 = bus_space_read_1(wdr->cmd_iot,
    137  1.1  dyoung 		    wdr->cmd_iohs[wd_status], 0);
    138  1.1  dyoung 		if ((st0 & WDCS_BSY) == 0)
    139  1.1  dyoung 			break;
    140  1.1  dyoung 		delay(10);
    141  1.1  dyoung 	}
    142  1.1  dyoung 	if (i == 0)
    143  1.1  dyoung 		printf("%s: reset failed\n", __func__);
    144  1.1  dyoung 	else
    145  1.1  dyoung 		WDC_EXTIO_DPRINTF("%s: reset in %d.%02dms\n", __func__,
    146  1.1  dyoung 		    (3100000 - i) / 100,
    147  1.1  dyoung 		    (3100000 - i) % 100);
    148  1.1  dyoung 
    149  1.1  dyoung 	if (poll) {
    150  1.1  dyoung 		/* ACK interrupt in case there is one pending left */
    151  1.1  dyoung 		if (wdc->irqack)
    152  1.1  dyoung 			wdc->irqack(chp);
    153  1.1  dyoung 		splx(s);
    154  1.1  dyoung 	}
    155  1.1  dyoung }
    156  1.1  dyoung #endif
    157  1.1  dyoung 
    158  1.1  dyoung static int
    159  1.1  dyoung wdc_extio_map(struct extio_attach_args *ea, struct wdc_regs *wdr,
    160  1.1  dyoung     struct ata_channel *chp, void **gpio, struct gpio_pinmap *pinmap)
    161  1.1  dyoung {
    162  1.1  dyoung 	int i;
    163  1.1  dyoung 
    164  1.1  dyoung 	*gpio = ea->ea_gpio;
    165  1.1  dyoung 	wdr->cmd_iot = wdr->ctl_iot = ea->ea_st;
    166  1.1  dyoung 
    167  1.1  dyoung #if 0
    168  1.1  dyoung 	if (gpio_pin_map(ea->ea_gpio, 0, ea->ea_gpio_mask, pinmap) != 0) {
    169  1.1  dyoung 		printf("%s: couldn't map GPIO\n", __func__);
    170  1.1  dyoung 		return -1;
    171  1.1  dyoung 	}
    172  1.1  dyoung #endif
    173  1.1  dyoung 
    174  1.1  dyoung 	if (bus_space_map(wdr->cmd_iot, ea->ea_addr, WDC_OBIO_CF_WINSIZE, 0,
    175  1.1  dyoung 	                  &wdr->cmd_baseioh)) {
    176  1.2    cube 		aprint_error("%s: couldn't map registers\n", __func__);
    177  1.1  dyoung 		goto post_gpio_err;
    178  1.1  dyoung 	}
    179  1.1  dyoung 
    180  1.1  dyoung 	for (i = 1; i < WDC_NREG; i++) {
    181  1.1  dyoung 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
    182  1.1  dyoung 		                        WDC_OBIO_REG_OFFSET + i, 1,
    183  1.1  dyoung 					&wdr->cmd_iohs[i]) != 0) {
    184  1.2    cube 			aprint_error(
    185  1.2    cube 			    "%s: unable to subregion control register\n",
    186  1.1  dyoung 			    __func__);
    187  1.1  dyoung 			goto post_bus_err;
    188  1.1  dyoung 		}
    189  1.1  dyoung 	}
    190  1.1  dyoung 	if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
    191  1.1  dyoung 				WDC_OBIO_DATA_OFFSET, 2,
    192  1.1  dyoung 				&wdr->cmd_iohs[wd_data]) != 0) {
    193  1.2    cube 		aprint_error("%s: unable to subregion data register\n",
    194  1.2    cube 		    __func__);
    195  1.1  dyoung 		goto post_bus_err;
    196  1.1  dyoung 	}
    197  1.1  dyoung 	if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
    198  1.1  dyoung 				WDC_OBIO_AUXREG_OFFSET, 1, &wdr->ctl_ioh)) {
    199  1.2    cube 		aprint_error("%s: unable to subregion aux register\n",
    200  1.2    cube 		    __func__);
    201  1.1  dyoung 		goto post_bus_err;
    202  1.1  dyoung 	}
    203  1.1  dyoung 
    204  1.1  dyoung 	wdc_init_shadow_regs(chp);
    205  1.1  dyoung 
    206  1.1  dyoung 	return 0;
    207  1.1  dyoung post_bus_err:
    208  1.1  dyoung 	bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, WDC_OBIO_CF_WINSIZE);
    209  1.1  dyoung post_gpio_err:
    210  1.1  dyoung #if 0
    211  1.1  dyoung 	gpio_pin_unmap(ea->ea_gpio, pinmap);
    212  1.1  dyoung #endif
    213  1.1  dyoung 	return -1;
    214  1.1  dyoung }
    215  1.1  dyoung 
    216  1.1  dyoung static void
    217  1.1  dyoung wdc_extio_dataout(struct ata_channel *chp, int flags, void *bf, size_t len)
    218  1.1  dyoung {
    219  1.1  dyoung 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
    220  1.1  dyoung 
    221  1.1  dyoung 	bus_space_write_multi_1(
    222  1.1  dyoung 	    wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0, bf, len);
    223  1.1  dyoung }
    224  1.1  dyoung 
    225  1.1  dyoung static void
    226  1.1  dyoung wdc_extio_datain(struct ata_channel *chp, int flags, void *bf, size_t len)
    227  1.1  dyoung {
    228  1.1  dyoung 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
    229  1.1  dyoung 
    230  1.1  dyoung 	bus_space_read_multi_1(
    231  1.1  dyoung 	    wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0, bf, len);
    232  1.1  dyoung }
    233  1.1  dyoung 
    234  1.1  dyoung int
    235  1.2    cube wdc_extio_match(device_t parent, cfdata_t cf, void *aux)
    236  1.1  dyoung {
    237  1.1  dyoung 	struct extio_attach_args *ea = (struct extio_attach_args *)aux;
    238  1.1  dyoung 	struct ata_channel ch;
    239  1.1  dyoung 	struct wdc_softc wdc;
    240  1.1  dyoung 	struct wdc_regs wdr;
    241  1.1  dyoung 	int result = 0;
    242  1.1  dyoung 	void *gpio;
    243  1.1  dyoung 	int map;
    244  1.1  dyoung 	struct gpio_pinmap pm = {.pm_map = &map};
    245  1.1  dyoung 
    246  1.1  dyoung 	if (strcmp(ea->ea_name, cf->cf_name) != 0)
    247  1.1  dyoung 		return 0;
    248  1.1  dyoung 
    249  1.1  dyoung 	WDC_EXTIO_DPRINTF("%s: enter\n", __func__);
    250  1.1  dyoung 
    251  1.1  dyoung 	memset(&wdc, 0, sizeof(wdc));
    252  1.1  dyoung 	memset(&ch, 0, sizeof(ch));
    253  1.1  dyoung 	ch.ch_atac = &wdc.sc_atac;
    254  1.1  dyoung 	wdc.regs = &wdr;
    255  1.1  dyoung 	wdc.datain_pio = wdc_extio_datain;
    256  1.1  dyoung 	wdc.dataout_pio = wdc_extio_dataout;
    257  1.1  dyoung 	if (cf->cf_flags != -1) {
    258  1.1  dyoung 		wdc.sc_atac.atac_cap |= cf->cf_flags &
    259  1.1  dyoung 		    (ATAC_CAP_NOIRQ|ATAC_CAP_DATA32|ATAC_CAP_DATA16);
    260  1.1  dyoung 		wdc.cap |= cf->cf_flags &
    261  1.1  dyoung 		    (WDC_CAPABILITY_PREATA|WDC_CAPABILITY_NO_EXTRA_RESETS);
    262  1.1  dyoung 	}
    263  1.1  dyoung 
    264  1.1  dyoung 	if (wdc_extio_map(ea, &wdr, &ch, &gpio, &pm) == -1)
    265  1.1  dyoung 		return 0;
    266  1.1  dyoung 
    267  1.1  dyoung 	result = wdcprobe(&ch);
    268  1.1  dyoung 
    269  1.1  dyoung 	bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_OBIO_CF_WINSIZE);
    270  1.1  dyoung #if 0
    271  1.1  dyoung 	gpio_pin_unmap(ea->ea_gpio, &pm);
    272  1.1  dyoung #endif
    273  1.1  dyoung 
    274  1.1  dyoung 	return result;
    275  1.1  dyoung }
    276  1.1  dyoung 
    277  1.1  dyoung void
    278  1.2    cube wdc_extio_attach(device_t parent, device_t self, void *aux)
    279  1.1  dyoung {
    280  1.2    cube 	struct wdc_extio_softc *sc = device_private(self);
    281  1.1  dyoung 	struct wdc_regs *wdr;
    282  1.1  dyoung 	struct extio_attach_args *ea = aux;
    283  1.1  dyoung 	struct ata_channel *chp = &sc->sc_channel;
    284  1.1  dyoung 	struct cfdata *cf;
    285  1.1  dyoung 
    286  1.1  dyoung 	WDC_EXTIO_DPRINTF("%s: enter\n", __func__);
    287  1.1  dyoung 
    288  1.2    cube 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    289  1.1  dyoung 	sc->sc_pinmap.pm_map = &sc->sc_map;
    290  1.1  dyoung 	sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
    291  1.1  dyoung 	chp->ch_atac = &sc->sc_wdcdev.sc_atac;
    292  1.1  dyoung 
    293  1.1  dyoung 	if (wdc_extio_map(ea, wdr, chp, &sc->sc_gpio, &sc->sc_pinmap) == -1)
    294  1.1  dyoung 		return;
    295  1.1  dyoung 
    296  1.2    cube 	cf = device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev);
    297  1.1  dyoung 	if (cf->cf_flags != -1) {
    298  1.2    cube 		aprint_normal(" flags %04x", cf->cf_flags);
    299  1.1  dyoung 		sc->sc_wdcdev.sc_atac.atac_cap |= cf->cf_flags &
    300  1.1  dyoung 		    (ATAC_CAP_NOIRQ|ATAC_CAP_DATA32|ATAC_CAP_DATA16);
    301  1.1  dyoung 		sc->sc_wdcdev.cap |= cf->cf_flags &
    302  1.1  dyoung 		    (WDC_CAPABILITY_PREATA|WDC_CAPABILITY_NO_EXTRA_RESETS);
    303  1.1  dyoung 	}
    304  1.1  dyoung 	sc->sc_wdcdev.datain_pio = wdc_extio_datain;
    305  1.1  dyoung 	sc->sc_wdcdev.dataout_pio = wdc_extio_dataout;
    306  1.1  dyoung 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
    307  1.1  dyoung 	sc->sc_chanlist[0] = chp;
    308  1.1  dyoung 	sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
    309  1.1  dyoung 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
    310  1.8  bouyer 	sc->sc_wdcdev.wdc_maxdrives = 2;
    311  1.1  dyoung 	chp->ch_channel = 0;
    312  1.1  dyoung 	chp->ch_atac = &sc->sc_wdcdev.sc_atac;
    313  1.1  dyoung 	chp->ch_queue = &sc->sc_chqueue;
    314  1.1  dyoung 
    315  1.2    cube 	aprint_normal("\n");
    316  1.1  dyoung 
    317  1.1  dyoung 	wdcattach(chp);
    318  1.1  dyoung }
    319