Home | History | Annotate | Line # | Download | only in dev
wdc_obio.c revision 1.9
      1  1.9  tsubai /*	$NetBSD: wdc_obio.c,v 1.9 2000/05/23 13:20:58 tsubai Exp $	*/
      2  1.1  tsubai 
      3  1.1  tsubai /*-
      4  1.1  tsubai  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1  tsubai  * All rights reserved.
      6  1.1  tsubai  *
      7  1.1  tsubai  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  tsubai  * by Charles M. Hannum and by Onno van der Linden.
      9  1.1  tsubai  *
     10  1.1  tsubai  * Redistribution and use in source and binary forms, with or without
     11  1.1  tsubai  * modification, are permitted provided that the following conditions
     12  1.1  tsubai  * are met:
     13  1.1  tsubai  * 1. Redistributions of source code must retain the above copyright
     14  1.1  tsubai  *    notice, this list of conditions and the following disclaimer.
     15  1.1  tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  tsubai  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  tsubai  *    documentation and/or other materials provided with the distribution.
     18  1.1  tsubai  * 3. All advertising materials mentioning features or use of this software
     19  1.1  tsubai  *    must display the following acknowledgement:
     20  1.1  tsubai  *        This product includes software developed by the NetBSD
     21  1.1  tsubai  *        Foundation, Inc. and its contributors.
     22  1.1  tsubai  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  tsubai  *    contributors may be used to endorse or promote products derived
     24  1.1  tsubai  *    from this software without specific prior written permission.
     25  1.1  tsubai  *
     26  1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  tsubai  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  tsubai  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  tsubai  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  tsubai  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  tsubai  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  tsubai  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  tsubai  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  tsubai  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  tsubai  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  tsubai  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  tsubai  */
     38  1.1  tsubai 
     39  1.1  tsubai #include <sys/param.h>
     40  1.1  tsubai #include <sys/systm.h>
     41  1.1  tsubai #include <sys/device.h>
     42  1.1  tsubai #include <sys/malloc.h>
     43  1.1  tsubai 
     44  1.1  tsubai #include <vm/vm.h>
     45  1.1  tsubai 
     46  1.1  tsubai #include <machine/bus.h>
     47  1.1  tsubai #include <machine/autoconf.h>
     48  1.1  tsubai 
     49  1.9  tsubai #include <dev/ata/atareg.h>
     50  1.1  tsubai #include <dev/ata/atavar.h>
     51  1.1  tsubai #include <dev/ic/wdcvar.h>
     52  1.1  tsubai 
     53  1.1  tsubai #include <macppc/dev/dbdma.h>
     54  1.1  tsubai 
     55  1.1  tsubai #define WDC_REG_NPORTS		8
     56  1.1  tsubai #define WDC_AUXREG_OFFSET	0x16
     57  1.1  tsubai #define WDC_DEFAULT_PIO_IRQ	13	/* XXX */
     58  1.1  tsubai #define WDC_DEFAULT_DMA_IRQ	2	/* XXX */
     59  1.1  tsubai 
     60  1.1  tsubai #define WDC_OPTIONS_DMA 0x01
     61  1.1  tsubai 
     62  1.1  tsubai /*
     63  1.1  tsubai  * XXX This code currently doesn't even try to allow 32-bit data port use.
     64  1.1  tsubai  */
     65  1.1  tsubai 
     66  1.1  tsubai struct wdc_obio_softc {
     67  1.1  tsubai 	struct wdc_softc sc_wdcdev;
     68  1.1  tsubai 	struct channel_softc *wdc_chanptr;
     69  1.1  tsubai 	struct channel_softc wdc_channel;
     70  1.1  tsubai 	dbdma_regmap_t *sc_dmareg;
     71  1.1  tsubai 	dbdma_command_t	*sc_dmacmd;
     72  1.5  tsubai 	void *sc_ih;
     73  1.1  tsubai };
     74  1.1  tsubai 
     75  1.9  tsubai int wdc_obio_probe __P((struct device *, struct cfdata *, void *));
     76  1.9  tsubai void wdc_obio_attach __P((struct device *, struct device *, void *));
     77  1.9  tsubai int wdc_obio_detach __P((struct device *, int));
     78  1.9  tsubai int wdc_obio_dma_init __P((void *, int, int, void *, size_t, int));
     79  1.9  tsubai void wdc_obio_dma_start __P((void *, int, int));
     80  1.9  tsubai int wdc_obio_dma_finish __P((void *, int, int, int));
     81  1.9  tsubai static void adjust_timing __P((struct channel_softc *));
     82  1.1  tsubai 
     83  1.1  tsubai struct cfattach wdc_obio_ca = {
     84  1.5  tsubai 	sizeof(struct wdc_obio_softc), wdc_obio_probe, wdc_obio_attach,
     85  1.5  tsubai 	wdc_obio_detach, wdcactivate
     86  1.1  tsubai };
     87  1.1  tsubai 
     88  1.1  tsubai 
     89  1.1  tsubai int
     90  1.1  tsubai wdc_obio_probe(parent, match, aux)
     91  1.1  tsubai 	struct device *parent;
     92  1.1  tsubai 	struct cfdata *match;
     93  1.1  tsubai 	void *aux;
     94  1.1  tsubai {
     95  1.1  tsubai 	struct confargs *ca = aux;
     96  1.3  tsubai 	char compat[32];
     97  1.1  tsubai 
     98  1.3  tsubai 	/* XXX should not use name */
     99  1.1  tsubai 	if (strcmp(ca->ca_name, "ATA") == 0 ||
    100  1.1  tsubai 	    strcmp(ca->ca_name, "ata") == 0 ||
    101  1.2  tsubai 	    strcmp(ca->ca_name, "ata0") == 0 ||
    102  1.1  tsubai 	    strcmp(ca->ca_name, "ide") == 0)
    103  1.3  tsubai 		return 1;
    104  1.3  tsubai 
    105  1.3  tsubai 	bzero(compat, sizeof(compat));
    106  1.3  tsubai 	OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
    107  1.6  tsubai 	if (strcmp(compat, "heathrow-ata") == 0 ||
    108  1.6  tsubai 	    strcmp(compat, "keylargo-ata") == 0)
    109  1.1  tsubai 		return 1;
    110  1.1  tsubai 
    111  1.1  tsubai 	return 0;
    112  1.1  tsubai }
    113  1.1  tsubai 
    114  1.1  tsubai void
    115  1.1  tsubai wdc_obio_attach(parent, self, aux)
    116  1.1  tsubai 	struct device *parent, *self;
    117  1.1  tsubai 	void *aux;
    118  1.1  tsubai {
    119  1.1  tsubai 	struct wdc_obio_softc *sc = (void *)self;
    120  1.1  tsubai 	struct confargs *ca = aux;
    121  1.1  tsubai 	struct channel_softc *chp = &sc->wdc_channel;
    122  1.4  tsubai 	int intr;
    123  1.1  tsubai 	int use_dma = 0;
    124  1.7  tsubai 	char path[80];
    125  1.1  tsubai 
    126  1.1  tsubai 	if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_OPTIONS_DMA) {
    127  1.1  tsubai 		if (ca->ca_nreg >= 16 || ca->ca_nintr == -1)
    128  1.1  tsubai 			use_dma = 1;	/* XXX Don't work yet. */
    129  1.1  tsubai 	}
    130  1.1  tsubai 
    131  1.1  tsubai 	if (ca->ca_nintr >= 4 && ca->ca_nreg >= 8) {
    132  1.4  tsubai 		intr = ca->ca_intr[0];
    133  1.4  tsubai 		printf(" irq %d", intr);
    134  1.4  tsubai 	} else if (ca->ca_nintr == -1) {
    135  1.4  tsubai 		intr = WDC_DEFAULT_PIO_IRQ;
    136  1.4  tsubai 		printf(" irq property not found; using %d", intr);
    137  1.4  tsubai 	} else {
    138  1.1  tsubai 		printf(": couldn't get irq property\n");
    139  1.1  tsubai 		return;
    140  1.1  tsubai 	}
    141  1.1  tsubai 
    142  1.1  tsubai 	if (use_dma)
    143  1.1  tsubai 		printf(": DMA transfer");
    144  1.1  tsubai 
    145  1.1  tsubai 	printf("\n");
    146  1.1  tsubai 
    147  1.1  tsubai 	chp->cmd_iot = chp->ctl_iot =
    148  1.1  tsubai 		macppc_make_bus_space_tag(ca->ca_baseaddr + ca->ca_reg[0], 4);
    149  1.1  tsubai 
    150  1.1  tsubai 	if (bus_space_map(chp->cmd_iot, 0, WDC_REG_NPORTS, 0, &chp->cmd_ioh) ||
    151  1.1  tsubai 	    bus_space_subregion(chp->cmd_iot, chp->cmd_ioh,
    152  1.1  tsubai 			WDC_AUXREG_OFFSET, 1, &chp->ctl_ioh)) {
    153  1.1  tsubai 		printf("%s: couldn't map registers\n",
    154  1.1  tsubai 			sc->sc_wdcdev.sc_dev.dv_xname);
    155  1.1  tsubai 		return;
    156  1.1  tsubai 	}
    157  1.1  tsubai #if 0
    158  1.1  tsubai 	chp->data32iot = chp->cmd_iot;
    159  1.1  tsubai 	chp->data32ioh = chp->cmd_ioh;
    160  1.1  tsubai #endif
    161  1.1  tsubai 
    162  1.5  tsubai 	sc->sc_ih = intr_establish(intr, IST_LEVEL, IPL_BIO, wdcintr, chp);
    163  1.1  tsubai 
    164  1.1  tsubai 	if (use_dma) {
    165  1.1  tsubai 		sc->sc_dmacmd = dbdma_alloc(sizeof(dbdma_command_t) * 20);
    166  1.1  tsubai 		sc->sc_dmareg = mapiodev(ca->ca_baseaddr + ca->ca_reg[2],
    167  1.1  tsubai 					 ca->ca_reg[3]);
    168  1.1  tsubai 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
    169  1.1  tsubai 	}
    170  1.1  tsubai 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    171  1.1  tsubai 	sc->sc_wdcdev.PIO_cap = 0;
    172  1.1  tsubai 	sc->wdc_chanptr = chp;
    173  1.1  tsubai 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    174  1.1  tsubai 	sc->sc_wdcdev.nchannels = 1;
    175  1.1  tsubai 	sc->sc_wdcdev.dma_arg = sc;
    176  1.1  tsubai 	sc->sc_wdcdev.dma_init = wdc_obio_dma_init;
    177  1.1  tsubai 	sc->sc_wdcdev.dma_start = wdc_obio_dma_start;
    178  1.1  tsubai 	sc->sc_wdcdev.dma_finish = wdc_obio_dma_finish;
    179  1.1  tsubai 	chp->channel = 0;
    180  1.1  tsubai 	chp->wdc = &sc->sc_wdcdev;
    181  1.1  tsubai 	chp->ch_queue = malloc(sizeof(struct channel_queue),
    182  1.1  tsubai 		M_DEVBUF, M_NOWAIT);
    183  1.1  tsubai 	if (chp->ch_queue == NULL) {
    184  1.1  tsubai 		printf("%s: can't allocate memory for command queue",
    185  1.1  tsubai 		sc->sc_wdcdev.sc_dev.dv_xname);
    186  1.1  tsubai 		return;
    187  1.7  tsubai 	}
    188  1.7  tsubai 
    189  1.7  tsubai #define OHARE_FEATURE_REG	0xf3000038
    190  1.7  tsubai 
    191  1.7  tsubai 	/* XXX Enable wdc1 by feature reg. */
    192  1.7  tsubai 	bzero(path, sizeof(path));
    193  1.7  tsubai 	OF_package_to_path(ca->ca_node, path, sizeof(path));
    194  1.7  tsubai 	if (strcmp(path, "/bandit@F2000000/ohare@10/ata@21000") == 0) {
    195  1.7  tsubai 		u_int x;
    196  1.7  tsubai 
    197  1.7  tsubai 		x = in32rb(OHARE_FEATURE_REG);
    198  1.7  tsubai 		x |= 8;
    199  1.7  tsubai 		out32rb(OHARE_FEATURE_REG, x);
    200  1.1  tsubai 	}
    201  1.1  tsubai 
    202  1.1  tsubai 	wdcattach(chp);
    203  1.9  tsubai 
    204  1.9  tsubai 	/* modify DMA access timings */
    205  1.9  tsubai 	if (use_dma)
    206  1.9  tsubai 		adjust_timing(chp);
    207  1.9  tsubai }
    208  1.9  tsubai 
    209  1.9  tsubai /* Multiword DMA transfer timings */
    210  1.9  tsubai static struct {
    211  1.9  tsubai 	int cycle;	/* minimum cycle time [ns] */
    212  1.9  tsubai 	int active;	/* minimum command active time [ns] */
    213  1.9  tsubai } dma_timing[3] = {
    214  1.9  tsubai 	480, 215,	/* Mode 0 */
    215  1.9  tsubai 	150,  80,	/* Mode 1 */
    216  1.9  tsubai 	120,  70,	/* Mode 2 */
    217  1.9  tsubai };
    218  1.9  tsubai 
    219  1.9  tsubai #define TIME_TO_TICK(time) howmany((time), 30)
    220  1.9  tsubai 
    221  1.9  tsubai #define CONFIG_REG (0x200 >> 4)		/* IDE access timing register */
    222  1.9  tsubai 
    223  1.9  tsubai void
    224  1.9  tsubai adjust_timing(chp)
    225  1.9  tsubai 	struct channel_softc *chp;
    226  1.9  tsubai {
    227  1.9  tsubai         struct ataparams params;
    228  1.9  tsubai 	struct ata_drive_datas *drvp = &chp->ch_drive[0];	/* XXX */
    229  1.9  tsubai 	u_int conf;
    230  1.9  tsubai 	int mode;
    231  1.9  tsubai 	int cycle, active, min_cycle, min_active;
    232  1.9  tsubai 	int cycle_tick, act_tick, inact_tick, half_tick;
    233  1.9  tsubai 
    234  1.9  tsubai 	if (ata_get_params(drvp, AT_POLL, &params) != CMD_OK)
    235  1.9  tsubai 		return;
    236  1.9  tsubai 
    237  1.9  tsubai 	for (mode = 2; mode >= 0; mode--)
    238  1.9  tsubai 		if (params.atap_dmamode_act & (1 << mode))
    239  1.9  tsubai 			goto found;
    240  1.9  tsubai 
    241  1.9  tsubai 	/* No active DMA mode is found...  Do nothing. */
    242  1.9  tsubai 	return;
    243  1.9  tsubai 
    244  1.9  tsubai found:
    245  1.9  tsubai 	min_cycle = dma_timing[mode].cycle;
    246  1.9  tsubai 	min_active = dma_timing[mode].active;
    247  1.9  tsubai 
    248  1.9  tsubai #ifdef notyet
    249  1.9  tsubai 	/* Minimum cycle time is 150ns on ohare. */
    250  1.9  tsubai 	if (ohare && params.atap_dmatiming_recom < 150)
    251  1.9  tsubai 		params.atap_dmatiming_recom = 150;
    252  1.9  tsubai #endif
    253  1.9  tsubai 	cycle = max(min_cycle, params.atap_dmatiming_recom);
    254  1.9  tsubai 	active = min_active + (cycle - min_cycle);		/* XXX */
    255  1.9  tsubai 
    256  1.9  tsubai 	cycle_tick = TIME_TO_TICK(cycle);
    257  1.9  tsubai 	act_tick = TIME_TO_TICK(active);
    258  1.9  tsubai 	inact_tick = cycle_tick - act_tick - 1;
    259  1.9  tsubai 	if (inact_tick < 1)
    260  1.9  tsubai 		inact_tick = 1;
    261  1.9  tsubai 	half_tick = 0;	/* XXX */
    262  1.9  tsubai 	conf = (half_tick << 21) | (inact_tick << 16) | (act_tick << 11);
    263  1.9  tsubai 	bus_space_write_4(chp->cmd_iot, chp->cmd_ioh, CONFIG_REG, conf);
    264  1.9  tsubai #if 0
    265  1.9  tsubai 	printf("conf = 0x%x, cyc = %d (%d ns), act = %d (%d ns), inact = %d\n",
    266  1.9  tsubai 	    conf, cycle_tick, cycle, act_tick, active, inact_tick);
    267  1.9  tsubai #endif
    268  1.5  tsubai }
    269  1.5  tsubai 
    270  1.5  tsubai int
    271  1.5  tsubai wdc_obio_detach(self, flags)
    272  1.5  tsubai 	struct device *self;
    273  1.5  tsubai 	int flags;
    274  1.5  tsubai {
    275  1.5  tsubai 	struct wdc_obio_softc *sc = (void *)self;
    276  1.5  tsubai 	struct channel_softc *chp = &sc->wdc_channel;
    277  1.5  tsubai 	int error;
    278  1.5  tsubai 
    279  1.5  tsubai 	if ((error = wdcdetach(self, flags)) != 0)
    280  1.5  tsubai 		return error;
    281  1.5  tsubai 
    282  1.5  tsubai 	intr_disestablish(sc->sc_ih);
    283  1.5  tsubai 
    284  1.5  tsubai 	free(sc->wdc_channel.ch_queue, M_DEVBUF);
    285  1.5  tsubai 
    286  1.5  tsubai 	/* Unmap our i/o space. */
    287  1.5  tsubai 	bus_space_unmap(chp->cmd_iot, chp->cmd_ioh, WDC_REG_NPORTS);
    288  1.5  tsubai 
    289  1.5  tsubai 	/* Unmap DMA registers. */
    290  1.5  tsubai 	/* XXX unmapiodev(sc->sc_dmareg); */
    291  1.5  tsubai 	/* XXX free(sc->sc_dmacmd); */
    292  1.5  tsubai 
    293  1.5  tsubai 	return 0;
    294  1.1  tsubai }
    295  1.1  tsubai 
    296  1.9  tsubai int
    297  1.1  tsubai wdc_obio_dma_init(v, channel, drive, databuf, datalen, read)
    298  1.1  tsubai 	void *v;
    299  1.1  tsubai 	void *databuf;
    300  1.1  tsubai 	size_t datalen;
    301  1.1  tsubai 	int read;
    302  1.1  tsubai {
    303  1.1  tsubai 	struct wdc_obio_softc *sc = v;
    304  1.1  tsubai 	vaddr_t va = (vaddr_t)databuf;
    305  1.1  tsubai 	dbdma_command_t *cmdp;
    306  1.4  tsubai 	u_int cmd, offset;
    307  1.1  tsubai 
    308  1.1  tsubai 	cmdp = sc->sc_dmacmd;
    309  1.1  tsubai 	cmd = read ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
    310  1.4  tsubai 
    311  1.4  tsubai 	offset = va & PGOFSET;
    312  1.4  tsubai 
    313  1.4  tsubai 	/* if va is not page-aligned, setup the first page */
    314  1.4  tsubai 	if (offset != 0) {
    315  1.4  tsubai 		int rest = NBPG - offset;	/* the rest of the page */
    316  1.4  tsubai 
    317  1.4  tsubai 		if (datalen > rest) {		/* if continues to next page */
    318  1.4  tsubai 			DBDMA_BUILD(cmdp, cmd, 0, rest, vtophys(va),
    319  1.4  tsubai 				DBDMA_INT_NEVER, DBDMA_WAIT_NEVER,
    320  1.4  tsubai 				DBDMA_BRANCH_NEVER);
    321  1.4  tsubai 			datalen -= rest;
    322  1.4  tsubai 			va += rest;
    323  1.4  tsubai 			cmdp++;
    324  1.4  tsubai 		}
    325  1.4  tsubai 	}
    326  1.4  tsubai 
    327  1.4  tsubai 	/* now va is page-aligned */
    328  1.1  tsubai 	while (datalen > NBPG) {
    329  1.1  tsubai 		DBDMA_BUILD(cmdp, cmd, 0, NBPG, vtophys(va),
    330  1.1  tsubai 			DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    331  1.1  tsubai 		datalen -= NBPG;
    332  1.1  tsubai 		va += NBPG;
    333  1.1  tsubai 		cmdp++;
    334  1.1  tsubai 	}
    335  1.1  tsubai 
    336  1.1  tsubai 	/* the last page (datalen <= NBPG here) */
    337  1.1  tsubai 	cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
    338  1.1  tsubai 	DBDMA_BUILD(cmdp, cmd, 0, datalen, vtophys(va),
    339  1.4  tsubai 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    340  1.1  tsubai 	cmdp++;
    341  1.1  tsubai 
    342  1.1  tsubai 	DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
    343  1.1  tsubai 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    344  1.1  tsubai 
    345  1.1  tsubai 	return 0;
    346  1.1  tsubai }
    347  1.1  tsubai 
    348  1.9  tsubai void
    349  1.8  tsubai wdc_obio_dma_start(v, channel, drive)
    350  1.1  tsubai 	void *v;
    351  1.1  tsubai 	int channel, drive;
    352  1.1  tsubai {
    353  1.1  tsubai 	struct wdc_obio_softc *sc = v;
    354  1.1  tsubai 
    355  1.1  tsubai 	dbdma_start(sc->sc_dmareg, sc->sc_dmacmd);
    356  1.1  tsubai }
    357  1.1  tsubai 
    358  1.9  tsubai int
    359  1.1  tsubai wdc_obio_dma_finish(v, channel, drive, read)
    360  1.1  tsubai 	void *v;
    361  1.1  tsubai 	int channel, drive;
    362  1.1  tsubai 	int read;
    363  1.1  tsubai {
    364  1.4  tsubai 	struct wdc_obio_softc *sc = v;
    365  1.4  tsubai 
    366  1.4  tsubai 	dbdma_stop(sc->sc_dmareg);
    367  1.1  tsubai 	return 0;
    368  1.1  tsubai }
    369