Home | History | Annotate | Line # | Download | only in dev
wdc_extio.c revision 1.11
      1  1.11  jdolecek /*	$NetBSD: wdc_extio.c,v 1.11 2019/09/09 22:01:23 jdolecek 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.11  jdolecek __KERNEL_RCSID(0, "$NetBSD: wdc_extio.c,v 1.11 2019/09/09 22:01:23 jdolecek 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 wdc_regs		sc_wdc_regs;
    103   1.1    dyoung 	void			*sc_gpio;
    104   1.1    dyoung 	int			sc_map;
    105   1.1    dyoung 	struct gpio_pinmap	sc_pinmap;
    106   1.1    dyoung };
    107   1.1    dyoung 
    108   1.2      cube int	wdc_extio_match(device_t, cfdata_t, void *);
    109   1.2      cube void	wdc_extio_attach(device_t, device_t, void *);
    110   1.1    dyoung 
    111   1.2      cube CFATTACH_DECL_NEW(wdc_extio, sizeof(struct wdc_extio_softc),
    112   1.1    dyoung     wdc_extio_match, wdc_extio_attach, NULL, NULL);
    113   1.1    dyoung 
    114   1.1    dyoung #if 0
    115   1.1    dyoung void	wdc_extio_reset(struct ata_channel *, int);
    116   1.1    dyoung void
    117   1.1    dyoung wdc_extio_reset(struct ata_channel *chp, int poll)
    118   1.1    dyoung {
    119   1.1    dyoung 	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
    120   1.1    dyoung 	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
    121   1.1    dyoung 	uint8_t st0;
    122   1.1    dyoung 	int i, s = 0;
    123   1.1    dyoung 
    124   1.1    dyoung 	if (poll)
    125   1.1    dyoung 		s = splbio();
    126   1.1    dyoung 	if (wdc->select)
    127   1.1    dyoung 		wdc->select(chp,0);
    128   1.1    dyoung 	/* assert SRST, wait for reset to complete */
    129   1.1    dyoung 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_RST);
    130   1.1    dyoung 	delay(2000);
    131   1.1    dyoung 	(void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0);
    132   1.1    dyoung 	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 0);
    133   1.1    dyoung 	delay(10);	/* 400ns delay */
    134   1.1    dyoung 	for (i = 3100000; --i > 0; ) {
    135   1.1    dyoung 		st0 = bus_space_read_1(wdr->cmd_iot,
    136   1.1    dyoung 		    wdr->cmd_iohs[wd_status], 0);
    137   1.1    dyoung 		if ((st0 & WDCS_BSY) == 0)
    138   1.1    dyoung 			break;
    139   1.1    dyoung 		delay(10);
    140   1.1    dyoung 	}
    141   1.1    dyoung 	if (i == 0)
    142   1.1    dyoung 		printf("%s: reset failed\n", __func__);
    143   1.1    dyoung 	else
    144   1.1    dyoung 		WDC_EXTIO_DPRINTF("%s: reset in %d.%02dms\n", __func__,
    145   1.1    dyoung 		    (3100000 - i) / 100,
    146   1.1    dyoung 		    (3100000 - i) % 100);
    147   1.1    dyoung 
    148   1.1    dyoung 	if (poll) {
    149   1.1    dyoung 		/* ACK interrupt in case there is one pending left */
    150   1.1    dyoung 		if (wdc->irqack)
    151   1.1    dyoung 			wdc->irqack(chp);
    152   1.1    dyoung 		splx(s);
    153   1.1    dyoung 	}
    154   1.1    dyoung }
    155   1.1    dyoung #endif
    156   1.1    dyoung 
    157   1.1    dyoung static int
    158   1.1    dyoung wdc_extio_map(struct extio_attach_args *ea, struct wdc_regs *wdr,
    159  1.11  jdolecek     void **gpio, struct gpio_pinmap *pinmap)
    160   1.1    dyoung {
    161   1.1    dyoung 	int i;
    162   1.1    dyoung 
    163   1.1    dyoung 	*gpio = ea->ea_gpio;
    164   1.1    dyoung 	wdr->cmd_iot = wdr->ctl_iot = ea->ea_st;
    165   1.1    dyoung 
    166   1.1    dyoung #if 0
    167   1.1    dyoung 	if (gpio_pin_map(ea->ea_gpio, 0, ea->ea_gpio_mask, pinmap) != 0) {
    168   1.1    dyoung 		printf("%s: couldn't map GPIO\n", __func__);
    169   1.1    dyoung 		return -1;
    170   1.1    dyoung 	}
    171   1.1    dyoung #endif
    172   1.1    dyoung 
    173   1.1    dyoung 	if (bus_space_map(wdr->cmd_iot, ea->ea_addr, WDC_OBIO_CF_WINSIZE, 0,
    174   1.1    dyoung 	                  &wdr->cmd_baseioh)) {
    175   1.2      cube 		aprint_error("%s: couldn't map registers\n", __func__);
    176   1.1    dyoung 		goto post_gpio_err;
    177   1.1    dyoung 	}
    178   1.1    dyoung 
    179   1.1    dyoung 	for (i = 1; i < WDC_NREG; i++) {
    180   1.1    dyoung 		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
    181   1.1    dyoung 		                        WDC_OBIO_REG_OFFSET + i, 1,
    182   1.1    dyoung 					&wdr->cmd_iohs[i]) != 0) {
    183   1.2      cube 			aprint_error(
    184   1.2      cube 			    "%s: unable to subregion control register\n",
    185   1.1    dyoung 			    __func__);
    186   1.1    dyoung 			goto post_bus_err;
    187   1.1    dyoung 		}
    188   1.1    dyoung 	}
    189   1.1    dyoung 	if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
    190   1.1    dyoung 				WDC_OBIO_DATA_OFFSET, 2,
    191   1.1    dyoung 				&wdr->cmd_iohs[wd_data]) != 0) {
    192   1.2      cube 		aprint_error("%s: unable to subregion data register\n",
    193   1.2      cube 		    __func__);
    194   1.1    dyoung 		goto post_bus_err;
    195   1.1    dyoung 	}
    196   1.1    dyoung 	if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
    197   1.1    dyoung 				WDC_OBIO_AUXREG_OFFSET, 1, &wdr->ctl_ioh)) {
    198   1.2      cube 		aprint_error("%s: unable to subregion aux register\n",
    199   1.2      cube 		    __func__);
    200   1.1    dyoung 		goto post_bus_err;
    201   1.1    dyoung 	}
    202   1.1    dyoung 
    203  1.11  jdolecek 	wdc_init_shadow_regs(wdr);
    204   1.1    dyoung 
    205   1.1    dyoung 	return 0;
    206   1.1    dyoung post_bus_err:
    207   1.1    dyoung 	bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, WDC_OBIO_CF_WINSIZE);
    208   1.1    dyoung post_gpio_err:
    209   1.1    dyoung #if 0
    210   1.1    dyoung 	gpio_pin_unmap(ea->ea_gpio, pinmap);
    211   1.1    dyoung #endif
    212   1.1    dyoung 	return -1;
    213   1.1    dyoung }
    214   1.1    dyoung 
    215   1.1    dyoung static void
    216   1.1    dyoung wdc_extio_dataout(struct ata_channel *chp, int flags, void *bf, size_t len)
    217   1.1    dyoung {
    218   1.1    dyoung 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
    219   1.1    dyoung 
    220   1.1    dyoung 	bus_space_write_multi_1(
    221   1.1    dyoung 	    wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0, bf, len);
    222   1.1    dyoung }
    223   1.1    dyoung 
    224   1.1    dyoung static void
    225   1.1    dyoung wdc_extio_datain(struct ata_channel *chp, int flags, void *bf, size_t len)
    226   1.1    dyoung {
    227   1.1    dyoung 	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
    228   1.1    dyoung 
    229   1.1    dyoung 	bus_space_read_multi_1(
    230   1.1    dyoung 	    wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0, bf, len);
    231   1.1    dyoung }
    232   1.1    dyoung 
    233   1.1    dyoung int
    234   1.2      cube wdc_extio_match(device_t parent, cfdata_t cf, void *aux)
    235   1.1    dyoung {
    236   1.1    dyoung 	struct extio_attach_args *ea = (struct extio_attach_args *)aux;
    237   1.1    dyoung 	struct wdc_regs wdr;
    238   1.1    dyoung 	int result = 0;
    239   1.1    dyoung 	void *gpio;
    240   1.1    dyoung 	int map;
    241   1.1    dyoung 	struct gpio_pinmap pm = {.pm_map = &map};
    242   1.1    dyoung 
    243   1.1    dyoung 	if (strcmp(ea->ea_name, cf->cf_name) != 0)
    244   1.1    dyoung 		return 0;
    245   1.1    dyoung 
    246   1.1    dyoung 	WDC_EXTIO_DPRINTF("%s: enter\n", __func__);
    247   1.1    dyoung 
    248   1.1    dyoung 	memset(&wdc, 0, sizeof(wdc));
    249   1.1    dyoung 	memset(&ch, 0, sizeof(ch));
    250   1.1    dyoung 	ch.ch_atac = &wdc.sc_atac;
    251   1.1    dyoung 	wdc.regs = &wdr;
    252   1.1    dyoung 	wdc.datain_pio = wdc_extio_datain;
    253   1.1    dyoung 	wdc.dataout_pio = wdc_extio_dataout;
    254   1.1    dyoung 	if (cf->cf_flags != -1) {
    255   1.1    dyoung 		wdc.sc_atac.atac_cap |= cf->cf_flags &
    256   1.1    dyoung 		    (ATAC_CAP_NOIRQ|ATAC_CAP_DATA32|ATAC_CAP_DATA16);
    257   1.1    dyoung 		wdc.cap |= cf->cf_flags &
    258   1.1    dyoung 		    (WDC_CAPABILITY_PREATA|WDC_CAPABILITY_NO_EXTRA_RESETS);
    259   1.1    dyoung 	}
    260   1.1    dyoung 
    261  1.11  jdolecek 	if (wdc_extio_map(ea, &wdr, &gpio, &pm) == -1)
    262   1.1    dyoung 		return 0;
    263   1.1    dyoung 
    264  1.11  jdolecek 	result = wdcprobe(&wdr);
    265   1.1    dyoung 
    266   1.1    dyoung 	bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_OBIO_CF_WINSIZE);
    267   1.1    dyoung #if 0
    268   1.1    dyoung 	gpio_pin_unmap(ea->ea_gpio, &pm);
    269   1.1    dyoung #endif
    270   1.1    dyoung 
    271   1.1    dyoung 	return result;
    272   1.1    dyoung }
    273   1.1    dyoung 
    274   1.1    dyoung void
    275   1.2      cube wdc_extio_attach(device_t parent, device_t self, void *aux)
    276   1.1    dyoung {
    277   1.2      cube 	struct wdc_extio_softc *sc = device_private(self);
    278   1.1    dyoung 	struct wdc_regs *wdr;
    279   1.1    dyoung 	struct extio_attach_args *ea = aux;
    280   1.1    dyoung 	struct ata_channel *chp = &sc->sc_channel;
    281   1.1    dyoung 	struct cfdata *cf;
    282   1.1    dyoung 
    283   1.1    dyoung 	WDC_EXTIO_DPRINTF("%s: enter\n", __func__);
    284   1.1    dyoung 
    285   1.2      cube 	sc->sc_wdcdev.sc_atac.atac_dev = self;
    286   1.1    dyoung 	sc->sc_pinmap.pm_map = &sc->sc_map;
    287   1.1    dyoung 	sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
    288   1.1    dyoung 	chp->ch_atac = &sc->sc_wdcdev.sc_atac;
    289   1.1    dyoung 
    290  1.11  jdolecek 	if (wdc_extio_map(ea, wdr, &sc->sc_gpio, &sc->sc_pinmap) == -1)
    291   1.1    dyoung 		return;
    292   1.1    dyoung 
    293   1.2      cube 	cf = device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev);
    294   1.1    dyoung 	if (cf->cf_flags != -1) {
    295   1.2      cube 		aprint_normal(" flags %04x", cf->cf_flags);
    296   1.1    dyoung 		sc->sc_wdcdev.sc_atac.atac_cap |= cf->cf_flags &
    297   1.1    dyoung 		    (ATAC_CAP_NOIRQ|ATAC_CAP_DATA32|ATAC_CAP_DATA16);
    298   1.1    dyoung 		sc->sc_wdcdev.cap |= cf->cf_flags &
    299   1.1    dyoung 		    (WDC_CAPABILITY_PREATA|WDC_CAPABILITY_NO_EXTRA_RESETS);
    300   1.1    dyoung 	}
    301   1.1    dyoung 	sc->sc_wdcdev.datain_pio = wdc_extio_datain;
    302   1.1    dyoung 	sc->sc_wdcdev.dataout_pio = wdc_extio_dataout;
    303   1.1    dyoung 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
    304   1.1    dyoung 	sc->sc_chanlist[0] = chp;
    305   1.1    dyoung 	sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
    306   1.1    dyoung 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
    307   1.8    bouyer 	sc->sc_wdcdev.wdc_maxdrives = 2;
    308  1.10  jdolecek 
    309   1.1    dyoung 	chp->ch_channel = 0;
    310   1.1    dyoung 	chp->ch_atac = &sc->sc_wdcdev.sc_atac;
    311   1.1    dyoung 
    312   1.2      cube 	aprint_normal("\n");
    313   1.1    dyoung 
    314   1.1    dyoung 	wdcattach(chp);
    315   1.1    dyoung }
    316