Home | History | Annotate | Line # | Download | only in dev
wdc_obio.c revision 1.12
      1 /*	$NetBSD: wdc_obio.c,v 1.12 2001/06/08 00:32:02 matt 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 <dev/ofw/openfirm.h>
     54 
     55 #include <macppc/dev/dbdma.h>
     56 
     57 #define WDC_REG_NPORTS		8
     58 #define WDC_AUXREG_OFFSET	0x16
     59 #define WDC_DEFAULT_PIO_IRQ	13	/* XXX */
     60 #define WDC_DEFAULT_DMA_IRQ	2	/* XXX */
     61 
     62 #define WDC_OPTIONS_DMA 0x01
     63 
     64 /*
     65  * XXX This code currently doesn't even try to allow 32-bit data port use.
     66  */
     67 
     68 struct wdc_obio_softc {
     69 	struct wdc_softc sc_wdcdev;
     70 	struct channel_softc *wdc_chanptr;
     71 	struct channel_softc wdc_channel;
     72 	dbdma_regmap_t *sc_dmareg;
     73 	dbdma_command_t	*sc_dmacmd;
     74 	void *sc_ih;
     75 };
     76 
     77 int wdc_obio_probe __P((struct device *, struct cfdata *, void *));
     78 void wdc_obio_attach __P((struct device *, struct device *, void *));
     79 int wdc_obio_detach __P((struct device *, int));
     80 int wdc_obio_dma_init __P((void *, int, int, void *, size_t, int));
     81 void wdc_obio_dma_start __P((void *, int, int));
     82 int wdc_obio_dma_finish __P((void *, int, int, int));
     83 static void adjust_timing __P((struct channel_softc *));
     84 
     85 struct cfattach wdc_obio_ca = {
     86 	sizeof(struct wdc_obio_softc), wdc_obio_probe, wdc_obio_attach,
     87 	wdc_obio_detach, wdcactivate
     88 };
     89 
     90 
     91 int
     92 wdc_obio_probe(parent, match, aux)
     93 	struct device *parent;
     94 	struct cfdata *match;
     95 	void *aux;
     96 {
     97 	struct confargs *ca = aux;
     98 	char compat[32];
     99 
    100 	/* XXX should not use name */
    101 	if (strcmp(ca->ca_name, "ATA") == 0 ||
    102 	    strcmp(ca->ca_name, "ata") == 0 ||
    103 	    strcmp(ca->ca_name, "ata0") == 0 ||
    104 	    strcmp(ca->ca_name, "ide") == 0)
    105 		return 1;
    106 
    107 	bzero(compat, sizeof(compat));
    108 	OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
    109 	if (strcmp(compat, "heathrow-ata") == 0 ||
    110 	    strcmp(compat, "keylargo-ata") == 0)
    111 		return 1;
    112 
    113 	return 0;
    114 }
    115 
    116 void
    117 wdc_obio_attach(parent, self, aux)
    118 	struct device *parent, *self;
    119 	void *aux;
    120 {
    121 	struct wdc_obio_softc *sc = (void *)self;
    122 	struct confargs *ca = aux;
    123 	struct channel_softc *chp = &sc->wdc_channel;
    124 	int intr;
    125 	int use_dma = 0;
    126 	char path[80];
    127 
    128 	if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_OPTIONS_DMA) {
    129 		if (ca->ca_nreg >= 16 || ca->ca_nintr == -1)
    130 			use_dma = 1;	/* XXX Don't work yet. */
    131 	}
    132 
    133 	if (ca->ca_nintr >= 4 && ca->ca_nreg >= 8) {
    134 		intr = ca->ca_intr[0];
    135 		printf(" irq %d", intr);
    136 	} else if (ca->ca_nintr == -1) {
    137 		intr = WDC_DEFAULT_PIO_IRQ;
    138 		printf(" irq property not found; using %d", intr);
    139 	} else {
    140 		printf(": couldn't get irq property\n");
    141 		return;
    142 	}
    143 
    144 	if (use_dma)
    145 		printf(": DMA transfer");
    146 
    147 	printf("\n");
    148 
    149 	chp->cmd_iot = chp->ctl_iot =
    150 		macppc_make_bus_space_tag(ca->ca_baseaddr + ca->ca_reg[0], 4);
    151 
    152 	if (bus_space_map(chp->cmd_iot, 0, WDC_REG_NPORTS, 0, &chp->cmd_ioh) ||
    153 	    bus_space_subregion(chp->cmd_iot, chp->cmd_ioh,
    154 			WDC_AUXREG_OFFSET, 1, &chp->ctl_ioh)) {
    155 		printf("%s: couldn't map registers\n",
    156 			sc->sc_wdcdev.sc_dev.dv_xname);
    157 		return;
    158 	}
    159 #if 0
    160 	chp->data32iot = chp->cmd_iot;
    161 	chp->data32ioh = chp->cmd_ioh;
    162 #endif
    163 
    164 	sc->sc_ih = intr_establish(intr, IST_LEVEL, IPL_BIO, wdcintr, chp);
    165 
    166 	if (use_dma) {
    167 		sc->sc_dmacmd = dbdma_alloc(sizeof(dbdma_command_t) * 20);
    168 		sc->sc_dmareg = mapiodev(ca->ca_baseaddr + ca->ca_reg[2],
    169 					 ca->ca_reg[3]);
    170 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
    171 	}
    172 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
    173 	sc->sc_wdcdev.PIO_cap = 0;
    174 	sc->wdc_chanptr = chp;
    175 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
    176 	sc->sc_wdcdev.nchannels = 1;
    177 	sc->sc_wdcdev.dma_arg = sc;
    178 	sc->sc_wdcdev.dma_init = wdc_obio_dma_init;
    179 	sc->sc_wdcdev.dma_start = wdc_obio_dma_start;
    180 	sc->sc_wdcdev.dma_finish = wdc_obio_dma_finish;
    181 	chp->channel = 0;
    182 	chp->wdc = &sc->sc_wdcdev;
    183 	chp->ch_queue = malloc(sizeof(struct channel_queue),
    184 		M_DEVBUF, M_NOWAIT);
    185 	if (chp->ch_queue == NULL) {
    186 		printf("%s: can't allocate memory for command queue",
    187 		sc->sc_wdcdev.sc_dev.dv_xname);
    188 		return;
    189 	}
    190 
    191 #define OHARE_FEATURE_REG	0xf3000038
    192 
    193 	/* XXX Enable wdc1 by feature reg. */
    194 	bzero(path, sizeof(path));
    195 	OF_package_to_path(ca->ca_node, path, sizeof(path));
    196 	if (strcmp(path, "/bandit@F2000000/ohare@10/ata@21000") == 0) {
    197 		u_int x;
    198 
    199 		x = in32rb(OHARE_FEATURE_REG);
    200 		x |= 8;
    201 		out32rb(OHARE_FEATURE_REG, x);
    202 	}
    203 
    204 	wdcattach(chp);
    205 
    206 	/* modify DMA access timings */
    207 	if (use_dma)
    208 		adjust_timing(chp);
    209 
    210 	wdc_print_modes(chp);
    211 }
    212 
    213 /* Multiword DMA transfer timings */
    214 static struct {
    215 	int cycle;	/* minimum cycle time [ns] */
    216 	int active;	/* minimum command active time [ns] */
    217 } dma_timing[3] = {
    218 	{ 480, 215 },	/* Mode 0 */
    219 	{ 150,  80 },	/* Mode 1 */
    220 	{ 120,  70 },	/* Mode 2 */
    221 };
    222 
    223 #define TIME_TO_TICK(time) howmany((time), 30)
    224 
    225 #define CONFIG_REG (0x200 >> 4)		/* IDE access timing register */
    226 
    227 void
    228 adjust_timing(chp)
    229 	struct channel_softc *chp;
    230 {
    231         struct ataparams params;
    232 	struct ata_drive_datas *drvp = &chp->ch_drive[0];	/* XXX */
    233 	u_int conf;
    234 	int mode;
    235 	int cycle, active, min_cycle, min_active;
    236 	int cycle_tick, act_tick, inact_tick, half_tick;
    237 
    238 	if (ata_get_params(drvp, AT_POLL, &params) != CMD_OK)
    239 		return;
    240 
    241 	for (mode = 2; mode >= 0; mode--)
    242 		if (params.atap_dmamode_act & (1 << mode))
    243 			goto found;
    244 
    245 	/* No active DMA mode is found...  Do nothing. */
    246 	return;
    247 
    248 found:
    249 	min_cycle = dma_timing[mode].cycle;
    250 	min_active = dma_timing[mode].active;
    251 
    252 #ifdef notyet
    253 	/* Minimum cycle time is 150ns on ohare. */
    254 	if (ohare && params.atap_dmatiming_recom < 150)
    255 		params.atap_dmatiming_recom = 150;
    256 #endif
    257 	cycle = max(min_cycle, params.atap_dmatiming_recom);
    258 	active = min_active + (cycle - min_cycle);		/* XXX */
    259 
    260 	cycle_tick = TIME_TO_TICK(cycle);
    261 	act_tick = TIME_TO_TICK(active);
    262 	inact_tick = cycle_tick - act_tick - 1;
    263 	if (inact_tick < 1)
    264 		inact_tick = 1;
    265 	half_tick = 0;	/* XXX */
    266 	conf = (half_tick << 21) | (inact_tick << 16) | (act_tick << 11);
    267 	bus_space_write_4(chp->cmd_iot, chp->cmd_ioh, CONFIG_REG, conf);
    268 #if 0
    269 	printf("conf = 0x%x, cyc = %d (%d ns), act = %d (%d ns), inact = %d\n",
    270 	    conf, cycle_tick, cycle, act_tick, active, inact_tick);
    271 #endif
    272 }
    273 
    274 int
    275 wdc_obio_detach(self, flags)
    276 	struct device *self;
    277 	int flags;
    278 {
    279 	struct wdc_obio_softc *sc = (void *)self;
    280 	int error;
    281 
    282 	if ((error = wdcdetach(self, flags)) != 0)
    283 		return error;
    284 
    285 	intr_disestablish(sc->sc_ih);
    286 
    287 	free(sc->wdc_channel.ch_queue, M_DEVBUF);
    288 
    289 	/* Unmap our i/o space. */
    290 	bus_space_unmap(chp->cmd_iot, chp->cmd_ioh, WDC_REG_NPORTS);
    291 
    292 	/* Unmap DMA registers. */
    293 	/* XXX unmapiodev(sc->sc_dmareg); */
    294 	/* XXX free(sc->sc_dmacmd); */
    295 
    296 	return 0;
    297 }
    298 
    299 int
    300 wdc_obio_dma_init(v, channel, drive, databuf, datalen, read)
    301 	void *v;
    302 	void *databuf;
    303 	size_t datalen;
    304 	int read;
    305 {
    306 	struct wdc_obio_softc *sc = v;
    307 	vaddr_t va = (vaddr_t)databuf;
    308 	dbdma_command_t *cmdp;
    309 	u_int cmd, offset;
    310 
    311 	cmdp = sc->sc_dmacmd;
    312 	cmd = read ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
    313 
    314 	offset = va & PGOFSET;
    315 
    316 	/* if va is not page-aligned, setup the first page */
    317 	if (offset != 0) {
    318 		int rest = NBPG - offset;	/* the rest of the page */
    319 
    320 		if (datalen > rest) {		/* if continues to next page */
    321 			DBDMA_BUILD(cmdp, cmd, 0, rest, vtophys(va),
    322 				DBDMA_INT_NEVER, DBDMA_WAIT_NEVER,
    323 				DBDMA_BRANCH_NEVER);
    324 			datalen -= rest;
    325 			va += rest;
    326 			cmdp++;
    327 		}
    328 	}
    329 
    330 	/* now va is page-aligned */
    331 	while (datalen > NBPG) {
    332 		DBDMA_BUILD(cmdp, cmd, 0, NBPG, vtophys(va),
    333 			DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    334 		datalen -= NBPG;
    335 		va += NBPG;
    336 		cmdp++;
    337 	}
    338 
    339 	/* the last page (datalen <= NBPG here) */
    340 	cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
    341 	DBDMA_BUILD(cmdp, cmd, 0, datalen, vtophys(va),
    342 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    343 	cmdp++;
    344 
    345 	DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
    346 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    347 
    348 	return 0;
    349 }
    350 
    351 void
    352 wdc_obio_dma_start(v, channel, drive)
    353 	void *v;
    354 	int channel, drive;
    355 {
    356 	struct wdc_obio_softc *sc = v;
    357 
    358 	dbdma_start(sc->sc_dmareg, sc->sc_dmacmd);
    359 }
    360 
    361 int
    362 wdc_obio_dma_finish(v, channel, drive, read)
    363 	void *v;
    364 	int channel, drive;
    365 	int read;
    366 {
    367 	struct wdc_obio_softc *sc = v;
    368 
    369 	dbdma_stop(sc->sc_dmareg);
    370 	return 0;
    371 }
    372