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