Home | History | Annotate | Line # | Download | only in jazz
fdc_jazzio.c revision 1.10
      1 /*	$NetBSD: fdc_jazzio.c,v 1.10 2005/01/22 07:35:34 tsutsui Exp $	*/
      2 /*	$OpenBSD: fd.c,v 1.6 1998/10/03 21:18:57 millert Exp $	*/
      3 /*	NetBSD: fd.c,v 1.78 1995/07/04 07:23:09 mycroft Exp 	*/
      4 
      5 /*-
      6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by Charles M. Hannum.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*-
     42  * Copyright (c) 1990 The Regents of the University of California.
     43  * All rights reserved.
     44  *
     45  * This code is derived from software contributed to Berkeley by
     46  * Don Ahn.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: fdc_jazzio.c,v 1.10 2005/01/22 07:35:34 tsutsui Exp $");
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/callout.h>
     81 #include <sys/device.h>
     82 
     83 #include <machine/autoconf.h>
     84 #include <machine/bus.h>
     85 
     86 #include <arc/jazz/jazzdmatlbreg.h>
     87 #include <arc/jazz/fdreg.h>
     88 #include <arc/jazz/fdcvar.h>
     89 #include <arc/jazz/jazziovar.h>
     90 #include <arc/jazz/dma.h>
     91 
     92 /* controller driver configuration */
     93 int fdc_jazzio_probe(struct device *, struct cfdata *, void *);
     94 void fdc_jazzio_attach(struct device *, struct device *, void *);
     95 
     96 /* MD DMA hook functions */
     97 void fdc_jazzio_dma_start(struct fdc_softc *, caddr_t, size_t, int);
     98 void fdc_jazzio_dma_abort(struct fdc_softc *);
     99 void fdc_jazzio_dma_done(struct fdc_softc *);
    100 
    101 /* software state, per controller */
    102 struct fdc_jazzio_softc {
    103 	struct fdc_softc sc_fdc;	/* base fdc device */
    104 
    105 	bus_space_handle_t sc_baseioh;	/* base I/O handle */
    106 	bus_space_handle_t sc_dmaioh;	/* DMA I/O handle */
    107 
    108 	bus_dma_tag_t sc_dmat;		/* bus_dma tag */
    109 	bus_dmamap_t sc_dmamap;		/* bus_dma map */
    110 	int sc_datain;			/* data direction */
    111 };
    112 
    113 CFATTACH_DECL(fdc_jazzio, sizeof(struct fdc_jazzio_softc),
    114     fdc_jazzio_probe, fdc_jazzio_attach, NULL, NULL);
    115 
    116 #define FDC_NPORT 6
    117 #define FDC_OFFSET 2 /* Should we use bus_space_subregion() or not? */
    118 
    119 int
    120 fdc_jazzio_probe(struct device *parent, struct cfdata *match, void *aux)
    121 {
    122 	struct jazzio_attach_args *ja = aux;
    123 	bus_space_tag_t iot;
    124 	bus_space_handle_t base_ioh, ioh;
    125 	int rv;
    126 
    127 	if (strcmp(ja->ja_name, "I82077") != 0)
    128 		return 0;
    129 
    130 	iot = ja->ja_bust;
    131 	rv = 0;
    132 
    133 	/* Map the I/O space. */
    134 	if (bus_space_map(iot, ja->ja_addr,
    135 	    FDC_OFFSET + FDC_NPORT, 0, &base_ioh))
    136 		return 0;
    137 
    138 	if (bus_space_subregion(iot, base_ioh, FDC_OFFSET, FDC_NPORT, &ioh))
    139 		goto out;
    140 
    141 	/* reset */
    142 	bus_space_write_1(iot, ioh, FDOUT, 0);
    143 	delay(100);
    144 	bus_space_write_1(iot, ioh, FDOUT, FDO_FRST);
    145 
    146 	/* see if it can handle a command */
    147 	if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0)
    148 		goto out;
    149 	out_fdc(iot, ioh, 0xdf); /* XXX */
    150 	out_fdc(iot, ioh, 2); /* XXX */
    151 
    152 	rv = 1;
    153 
    154  out:
    155 	bus_space_unmap(iot, base_ioh, FDC_OFFSET + FDC_NPORT);
    156 	return rv;
    157 }
    158 
    159 void
    160 fdc_jazzio_attach(struct device *parent, struct device *self, void *aux)
    161 {
    162 	struct fdc_jazzio_softc *jsc = (struct fdc_jazzio_softc *)self;
    163 	struct fdc_softc *fdc = &jsc->sc_fdc;
    164 	struct jazzio_attach_args *ja = aux;
    165 
    166 	fdc->sc_iot = ja->ja_bust;
    167 
    168 	fdc->sc_maxiosize = MAXPHYS;
    169 	fdc->sc_dma_start = fdc_jazzio_dma_start;
    170 	fdc->sc_dma_abort = fdc_jazzio_dma_abort;
    171 	fdc->sc_dma_done = fdc_jazzio_dma_done;
    172 
    173 	jsc->sc_dmat = ja->ja_dmat;
    174 
    175 	if (bus_space_map(fdc->sc_iot, ja->ja_addr,
    176 	    FDC_OFFSET + FDC_NPORT, 0, &jsc->sc_baseioh)) {
    177 		printf(": unable to map I/O space\n");
    178 		return;
    179 	}
    180 
    181 	if (bus_space_subregion(fdc->sc_iot, jsc->sc_baseioh,
    182 	    FDC_OFFSET, FDC_NPORT, &fdc->sc_ioh)) {
    183 		printf(": unable to subregion I/O space\n");
    184 		goto out_unmap1;
    185 	}
    186 
    187 	if (bus_space_map(fdc->sc_iot, jazzio_conf->jc_fdcdmareg,
    188 	    R4030_DMA_RANGE, 0, &jsc->sc_dmaioh)) {
    189 		printf(": unable to map DMA I/O space\n");
    190 		goto out_unmap1;
    191 	}
    192 
    193 	if (bus_dmamap_create(jsc->sc_dmat, MAXPHYS, 1, MAXPHYS, 0,
    194 	    BUS_DMA_ALLOCNOW|BUS_DMA_NOWAIT, &jsc->sc_dmamap)) {
    195 		printf(": unable to create DMA map\n");
    196 		goto out_unmap2;
    197 	}
    198 
    199 	printf("\n");
    200 
    201 	jazzio_intr_establish(ja->ja_intr, fdcintr, fdc);
    202 
    203 	fdcattach(fdc);
    204 	return;
    205 
    206  out_unmap2:
    207 	bus_space_unmap(fdc->sc_iot, jsc->sc_dmaioh, R4030_DMA_RANGE);
    208  out_unmap1:
    209 	bus_space_unmap(fdc->sc_iot, jsc->sc_baseioh, FDC_OFFSET + FDC_NPORT);
    210 }
    211 
    212 void
    213 fdc_jazzio_dma_start(struct fdc_softc *fdc, caddr_t addr, size_t size,
    214     int datain)
    215 {
    216 	struct fdc_jazzio_softc *jsc = (void *)fdc;
    217 
    218 	/* halt DMA */
    219 	bus_space_write_4(fdc->sc_iot, jsc->sc_dmaioh, R4030_DMA_ENAB, 0);
    220 	bus_space_write_4(fdc->sc_iot, jsc->sc_dmaioh, R4030_DMA_MODE, 0);
    221 
    222 	jsc->sc_datain = datain;
    223 
    224 	bus_dmamap_load(jsc->sc_dmat, jsc->sc_dmamap, addr, size, NULL,
    225 	    BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
    226 	    (datain ? BUS_DMA_READ : BUS_DMA_WRITE));
    227 	bus_dmamap_sync(jsc->sc_dmat, jsc->sc_dmamap,
    228 	    0, jsc->sc_dmamap->dm_mapsize,
    229 	    datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    230 
    231 	/* load new transfer parameters */
    232 	bus_space_write_4(fdc->sc_iot, jsc->sc_dmaioh,
    233 	    R4030_DMA_ADDR, jsc->sc_dmamap->dm_segs[0].ds_addr);
    234 	bus_space_write_4(fdc->sc_iot, jsc->sc_dmaioh,
    235 	    R4030_DMA_COUNT, jsc->sc_dmamap->dm_segs[0].ds_len);
    236 	bus_space_write_4(fdc->sc_iot, jsc->sc_dmaioh,
    237 	    R4030_DMA_MODE, R4030_DMA_MODE_160NS | R4030_DMA_MODE_8);
    238 
    239 	/* start DMA */
    240 	bus_space_write_4(fdc->sc_iot, jsc->sc_dmaioh,
    241 	    R4030_DMA_ENAB, R4030_DMA_ENAB_RUN |
    242 	    (datain ? R4030_DMA_ENAB_READ : R4030_DMA_ENAB_WRITE));
    243 }
    244 
    245 void
    246 fdc_jazzio_dma_abort(struct fdc_softc *fdc)
    247 {
    248 	struct fdc_jazzio_softc *jsc = (void *)fdc;
    249 
    250 	/* halt DMA */
    251 	bus_space_write_4(fdc->sc_iot, jsc->sc_dmaioh, R4030_DMA_ENAB, 0);
    252 	bus_space_write_4(fdc->sc_iot, jsc->sc_dmaioh, R4030_DMA_MODE, 0);
    253 }
    254 
    255 void
    256 fdc_jazzio_dma_done(struct fdc_softc *fdc)
    257 {
    258 	struct fdc_jazzio_softc *jsc = (void *)fdc;
    259 
    260 	/* halt DMA */
    261 	bus_space_write_4(fdc->sc_iot, jsc->sc_dmaioh, R4030_DMA_COUNT, 0);
    262 	bus_space_write_4(fdc->sc_iot, jsc->sc_dmaioh, R4030_DMA_ENAB, 0);
    263 	bus_space_write_4(fdc->sc_iot, jsc->sc_dmaioh, R4030_DMA_MODE, 0);
    264 
    265 	bus_dmamap_sync(jsc->sc_dmat, jsc->sc_dmamap,
    266 	    0, jsc->sc_dmamap->dm_mapsize,
    267 	    jsc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    268 	bus_dmamap_unload(jsc->sc_dmat, jsc->sc_dmamap);
    269 }
    270