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