ninjaata32.c revision 1.1 1 1.1 itohy /* $Id: ninjaata32.c,v 1.1 2006/09/07 14:22:07 itohy Exp $ */
2 1.1 itohy
3 1.1 itohy /*
4 1.1 itohy * Copyright (c) 2006 ITOH Yasufumi <itohy (at) NetBSD.org>.
5 1.1 itohy * All rights reserved.
6 1.1 itohy *
7 1.1 itohy * Redistribution and use in source and binary forms, with or without
8 1.1 itohy * modification, are permitted provided that the following conditions
9 1.1 itohy * are met:
10 1.1 itohy * 1. Redistributions of source code must retain the above copyright
11 1.1 itohy * notice, this list of conditions and the following disclaimer.
12 1.1 itohy * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 itohy * notice, this list of conditions and the following disclaimer in the
14 1.1 itohy * documentation and/or other materials provided with the distribution.
15 1.1 itohy *
16 1.1 itohy * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS''
17 1.1 itohy * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 1.1 itohy * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 itohy * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS
20 1.1 itohy * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 itohy * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 itohy * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 itohy * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 itohy * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 itohy * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 1.1 itohy * THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 itohy */
28 1.1 itohy
29 1.1 itohy #include <sys/cdefs.h>
30 1.1 itohy __KERNEL_RCSID(0, "$NetBSD: ninjaata32.c,v 1.1 2006/09/07 14:22:07 itohy Exp $");
31 1.1 itohy
32 1.1 itohy #include <sys/param.h>
33 1.1 itohy #include <sys/kernel.h>
34 1.1 itohy #include <sys/device.h>
35 1.1 itohy
36 1.1 itohy #include <machine/bus.h>
37 1.1 itohy #include <machine/intr.h>
38 1.1 itohy
39 1.1 itohy #include <uvm/uvm_extern.h>
40 1.1 itohy
41 1.1 itohy #include <dev/ata/atavar.h>
42 1.1 itohy #include <dev/ic/wdcreg.h>
43 1.1 itohy #include <dev/ic/wdcvar.h>
44 1.1 itohy
45 1.1 itohy #include <dev/ic/ninjaata32reg.h>
46 1.1 itohy #include <dev/ic/ninjaata32var.h>
47 1.1 itohy
48 1.1 itohy #ifdef NJATA32_DEBUG
49 1.1 itohy #define DPRINTF(x) printf x
50 1.1 itohy #else
51 1.1 itohy #define DPRINTF(x)
52 1.1 itohy #endif
53 1.1 itohy
54 1.1 itohy static void njata32_init(struct njata32_softc *, int nosleep);
55 1.1 itohy static void njata32_irqack(struct ata_channel *);
56 1.1 itohy static void njata32_clearirq(struct ata_channel *, int);
57 1.1 itohy static void njata32_setup_channel(struct ata_channel *);
58 1.1 itohy static int njata32_dma_init(void *, int channel, int drive,
59 1.1 itohy void *databuf, size_t datalen, int flags);
60 1.1 itohy static void njata32_piobm_start(void *, int channel, int drive, int skip,
61 1.1 itohy int xferlen, int flags);
62 1.1 itohy static int njata32_dma_finish(void *, int channel, int drive, int force);
63 1.1 itohy static void njata32_piobm_done(void *, int channel, int drive);
64 1.1 itohy
65 1.1 itohy #if 0 /* ATA DMA is currently unused */
66 1.1 itohy static const uint8_t njata32_timing_dma[NJATA32_MODE_MAX_DMA + 1] = {
67 1.1 itohy NJATA32_TIMING_DMA0, NJATA32_TIMING_DMA1, NJATA32_TIMING_DMA2
68 1.1 itohy };
69 1.1 itohy #endif
70 1.1 itohy static const uint8_t njata32_timing_pio[NJATA32_MODE_MAX_PIO + 1] = {
71 1.1 itohy NJATA32_TIMING_PIO0, NJATA32_TIMING_PIO1, NJATA32_TIMING_PIO2,
72 1.1 itohy NJATA32_TIMING_PIO3, NJATA32_TIMING_PIO4
73 1.1 itohy };
74 1.1 itohy
75 1.1 itohy static void
76 1.1 itohy njata32_init(sc, nosleep)
77 1.1 itohy struct njata32_softc *sc;
78 1.1 itohy int nosleep; /* can't sleep (during cold boot and in interrupt) */
79 1.1 itohy {
80 1.1 itohy
81 1.1 itohy /* disable interrupts */
82 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
83 1.1 itohy NJATA32_REG_IRQ_SELECT, 0);
84 1.1 itohy
85 1.1 itohy /* bus reset */
86 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc), NJATA32_REG_AS,
87 1.1 itohy NJATA32_AS_WAIT0 | NJATA32_AS_BUS_RESET);
88 1.1 itohy if (nosleep)
89 1.1 itohy delay(50000);
90 1.1 itohy else
91 1.1 itohy tsleep(sc, PRIBIO, "njaini", mstohz(50));
92 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc), NJATA32_REG_AS,
93 1.1 itohy NJATA32_AS_WAIT0);
94 1.1 itohy
95 1.1 itohy /* initial transfer speed */
96 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
97 1.1 itohy NJATA32_REG_TIMING, NJATA32_TIMING_PIO0);
98 1.1 itohy
99 1.1 itohy /* setup busmaster mode */
100 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc), NJATA32_REG_IOBM,
101 1.1 itohy NJATA32_IOBM_DEFAULT);
102 1.1 itohy
103 1.1 itohy /* enable interrupts */
104 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
105 1.1 itohy NJATA32_REG_IRQ_SELECT, NJATA32_IRQ_XFER | NJATA32_IRQ_DEV);
106 1.1 itohy }
107 1.1 itohy
108 1.1 itohy void
109 1.1 itohy njata32_attach(sc)
110 1.1 itohy struct njata32_softc *sc;
111 1.1 itohy {
112 1.1 itohy bus_addr_t dmaaddr;
113 1.1 itohy int i, devno, error;
114 1.1 itohy struct wdc_regs *wdr;
115 1.1 itohy
116 1.1 itohy /*
117 1.1 itohy * allocate DMA resource
118 1.1 itohy */
119 1.1 itohy if ((error = bus_dmamem_alloc(sc->sc_dmat,
120 1.1 itohy sizeof(struct njata32_dma_page), PAGE_SIZE, 0,
121 1.1 itohy &sc->sc_sgt_seg, 1, &sc->sc_sgt_nsegs, BUS_DMA_NOWAIT)) != 0) {
122 1.1 itohy printf("%s: unable to allocate sgt page, error = %d\n",
123 1.1 itohy NJATA32NAME(sc), error);
124 1.1 itohy return;
125 1.1 itohy }
126 1.1 itohy if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_sgt_seg,
127 1.1 itohy sc->sc_sgt_nsegs, sizeof(struct njata32_dma_page),
128 1.1 itohy (caddr_t *)&sc->sc_sgtpg,
129 1.1 itohy BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
130 1.1 itohy printf("%s: unable to map sgt page, error = %d\n",
131 1.1 itohy NJATA32NAME(sc), error);
132 1.1 itohy goto fail1;
133 1.1 itohy }
134 1.1 itohy if ((error = bus_dmamap_create(sc->sc_dmat,
135 1.1 itohy sizeof(struct njata32_dma_page), 1,
136 1.1 itohy sizeof(struct njata32_dma_page), 0, BUS_DMA_NOWAIT,
137 1.1 itohy &sc->sc_dmamap_sgt)) != 0) {
138 1.1 itohy printf("%s: unable to create sgt DMA map, error = %d\n",
139 1.1 itohy NJATA32NAME(sc), error);
140 1.1 itohy goto fail2;
141 1.1 itohy }
142 1.1 itohy if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_sgt,
143 1.1 itohy sc->sc_sgtpg, sizeof(struct njata32_dma_page),
144 1.1 itohy NULL, BUS_DMA_NOWAIT)) != 0) {
145 1.1 itohy printf("%s: unable to load sgt DMA map, error = %d\n",
146 1.1 itohy NJATA32NAME(sc), error);
147 1.1 itohy goto fail3;
148 1.1 itohy }
149 1.1 itohy
150 1.1 itohy dmaaddr = sc->sc_dmamap_sgt->dm_segs[0].ds_addr;
151 1.1 itohy
152 1.1 itohy for (devno = 0; devno < NJATA32_NUM_DEV; devno++) {
153 1.1 itohy sc->sc_dev[devno].d_sgt = sc->sc_sgtpg->dp_sg[devno];
154 1.1 itohy sc->sc_dev[devno].d_sgt_dma = dmaaddr +
155 1.1 itohy offsetof(struct njata32_dma_page, dp_sg[devno]);
156 1.1 itohy
157 1.1 itohy error = bus_dmamap_create(sc->sc_dmat,
158 1.1 itohy NJATA32_MAX_XFER, /* max total map size */
159 1.1 itohy NJATA32_NUM_SG, /* max number of segments */
160 1.1 itohy NJATA32_SGT_MAXSEGLEN, /* max size of a segment */
161 1.1 itohy 0, /* boundary */
162 1.1 itohy BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
163 1.1 itohy &sc->sc_dev[devno].d_dmamap_xfer);
164 1.1 itohy if (error) {
165 1.1 itohy printf("%s: failed to create DMA map (error = %d)\n",
166 1.1 itohy NJATA32NAME(sc), error);
167 1.1 itohy goto fail4;
168 1.1 itohy }
169 1.1 itohy }
170 1.1 itohy
171 1.1 itohy /* device properties */
172 1.1 itohy sc->sc_wdcdev.sc_atac.atac_cap =
173 1.1 itohy ATAC_CAP_DATA16 | ATAC_CAP_DATA32 | ATAC_CAP_PIOBM;
174 1.1 itohy sc->sc_wdcdev.irqack = njata32_irqack;
175 1.1 itohy sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_wdc_chanarray;
176 1.1 itohy sc->sc_wdcdev.sc_atac.atac_nchannels = NJATA32_NCHAN; /* 1 */
177 1.1 itohy sc->sc_wdcdev.sc_atac.atac_pio_cap = NJATA32_MODE_MAX_PIO;
178 1.1 itohy #if 0 /* ATA DMA is currently unused */
179 1.1 itohy sc->sc_wdcdev.sc_atac.atac_dma_cap = NJATA32_MODE_MAX_DMA;
180 1.1 itohy #endif
181 1.1 itohy sc->sc_wdcdev.sc_atac.atac_set_modes = njata32_setup_channel;
182 1.1 itohy
183 1.1 itohy /* DMA control functions */
184 1.1 itohy sc->sc_wdcdev.dma_arg = sc;
185 1.1 itohy sc->sc_wdcdev.dma_init = njata32_dma_init;
186 1.1 itohy sc->sc_wdcdev.piobm_start = njata32_piobm_start;
187 1.1 itohy sc->sc_wdcdev.dma_finish = njata32_dma_finish;
188 1.1 itohy sc->sc_wdcdev.piobm_done = njata32_piobm_done;
189 1.1 itohy
190 1.1 itohy sc->sc_wdcdev.cap |= WDC_CAPABILITY_NO_EXTRA_RESETS;
191 1.1 itohy
192 1.1 itohy sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
193 1.1 itohy
194 1.1 itohy /* only one channel */
195 1.1 itohy sc->sc_wdc_chanarray[0] = &sc->sc_ch[0].ch_ata_channel;
196 1.1 itohy sc->sc_ch[0].ch_ata_channel.ch_channel = 0;
197 1.1 itohy sc->sc_ch[0].ch_ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
198 1.1 itohy sc->sc_ch[0].ch_ata_channel.ch_queue = &sc->sc_wdc_chqueue;
199 1.1 itohy sc->sc_ch[0].ch_ata_channel.ch_ndrive = 2; /* max number of drives */
200 1.1 itohy
201 1.1 itohy /* map ATA registers */
202 1.1 itohy for (i = 0; i < WDC_NREG; i++) {
203 1.1 itohy if (bus_space_subregion(NJATA32_REGT(sc), NJATA32_REGH(sc),
204 1.1 itohy NJATA32_OFFSET_WDCREGS + i,
205 1.1 itohy i == wd_data ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
206 1.1 itohy aprint_error("%s: couldn't subregion cmd regs\n",
207 1.1 itohy NJATA32NAME(sc));
208 1.1 itohy goto fail4;
209 1.1 itohy }
210 1.1 itohy }
211 1.1 itohy wdc_init_shadow_regs(&sc->sc_ch[0].ch_ata_channel);
212 1.1 itohy wdr->data32iot = NJATA32_REGT(sc);
213 1.1 itohy wdr->data32ioh = wdr->cmd_iohs[wd_data];
214 1.1 itohy
215 1.1 itohy /* map ATA ctl reg */
216 1.1 itohy wdr->ctl_iot = NJATA32_REGT(sc);
217 1.1 itohy if (bus_space_subregion(NJATA32_REGT(sc), NJATA32_REGH(sc),
218 1.1 itohy NJATA32_REG_WD_ALTSTATUS, 1, &wdr->ctl_ioh) != 0) {
219 1.1 itohy aprint_error("%s: couldn't subregion ctl regs\n",
220 1.1 itohy NJATA32NAME(sc));
221 1.1 itohy goto fail4;
222 1.1 itohy }
223 1.1 itohy
224 1.1 itohy sc->sc_flags |= NJATA32_CMDPG_MAPPED;
225 1.1 itohy
226 1.1 itohy /* use flags value as busmaster wait */
227 1.1 itohy if ((sc->sc_bmwait =
228 1.1 itohy device_cfdata(&sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
229 1.1 itohy (NJATA32_BM_WAIT_MASK >> NJATA32_BM_WAIT_SHIFT)) > 0)
230 1.1 itohy aprint_normal("%s: busmaster wait = %d\n",
231 1.1 itohy NJATA32NAME(sc), sc->sc_bmwait);
232 1.1 itohy
233 1.1 itohy njata32_init(sc, cold);
234 1.1 itohy
235 1.1 itohy wdcattach(&sc->sc_ch[0].ch_ata_channel);
236 1.1 itohy
237 1.1 itohy return;
238 1.1 itohy
239 1.1 itohy /*
240 1.1 itohy * cleanup
241 1.1 itohy */
242 1.1 itohy fail4: while (--devno >= 0) {
243 1.1 itohy bus_dmamap_destroy(sc->sc_dmat,
244 1.1 itohy sc->sc_dev[devno].d_dmamap_xfer);
245 1.1 itohy }
246 1.1 itohy bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_sgt);
247 1.1 itohy fail3: bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_sgt);
248 1.1 itohy fail2: bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_sgtpg,
249 1.1 itohy sizeof(struct njata32_dma_page));
250 1.1 itohy fail1: bus_dmamem_free(sc->sc_dmat, &sc->sc_sgt_seg, sc->sc_sgt_nsegs);
251 1.1 itohy }
252 1.1 itohy
253 1.1 itohy int
254 1.1 itohy njata32_detach(sc, flags)
255 1.1 itohy struct njata32_softc *sc;
256 1.1 itohy int flags;
257 1.1 itohy {
258 1.1 itohy int rv, devno;
259 1.1 itohy
260 1.1 itohy if (sc->sc_flags & NJATA32_CMDPG_MAPPED) {
261 1.1 itohy if ((rv = wdcdetach(&sc->sc_wdcdev.sc_atac.atac_dev, flags)))
262 1.1 itohy return rv;
263 1.1 itohy
264 1.1 itohy /* free DMA resource */
265 1.1 itohy for (devno = 0; devno < NJATA32_NUM_DEV; devno++) {
266 1.1 itohy bus_dmamap_destroy(sc->sc_dmat,
267 1.1 itohy sc->sc_dev[devno].d_dmamap_xfer);
268 1.1 itohy }
269 1.1 itohy bus_dmamap_unload(sc->sc_dmat, sc->sc_dmamap_sgt);
270 1.1 itohy bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_sgt);
271 1.1 itohy bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_sgtpg,
272 1.1 itohy sizeof(struct njata32_dma_page));
273 1.1 itohy bus_dmamem_free(sc->sc_dmat, &sc->sc_sgt_seg, sc->sc_sgt_nsegs);
274 1.1 itohy }
275 1.1 itohy
276 1.1 itohy return 0;
277 1.1 itohy }
278 1.1 itohy
279 1.1 itohy static void
280 1.1 itohy njata32_irqack(chp)
281 1.1 itohy struct ata_channel *chp;
282 1.1 itohy {
283 1.1 itohy struct njata32_softc *sc = (void *)chp->ch_atac;
284 1.1 itohy
285 1.1 itohy /* disable busmaster */
286 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
287 1.1 itohy NJATA32_REG_BM, (sc->sc_bmwait << NJATA32_BM_WAIT_SHIFT));
288 1.1 itohy }
289 1.1 itohy
290 1.1 itohy static void
291 1.1 itohy njata32_clearirq(chp, irq)
292 1.1 itohy struct ata_channel *chp;
293 1.1 itohy int irq;
294 1.1 itohy {
295 1.1 itohy struct njata32_softc *sc = (void *)chp->ch_atac;
296 1.1 itohy
297 1.1 itohy printf("%s: unhandled intr: irq %#x, bm %#x, ",
298 1.1 itohy NJATA32NAME(sc), irq,
299 1.1 itohy bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
300 1.1 itohy NJATA32_REG_BM));
301 1.1 itohy
302 1.1 itohy /* disable busmaster */
303 1.1 itohy njata32_irqack(chp);
304 1.1 itohy
305 1.1 itohy /* clear device interrupt */
306 1.1 itohy printf("err %#x, seccnt %#x, cyl %#x, sdh %#x, ",
307 1.1 itohy bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
308 1.1 itohy NJATA32_REG_WD_ERROR),
309 1.1 itohy bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
310 1.1 itohy NJATA32_REG_WD_SECCNT),
311 1.1 itohy bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
312 1.1 itohy NJATA32_REG_WD_CYL_LO) |
313 1.1 itohy (bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
314 1.1 itohy NJATA32_REG_WD_CYL_HI) << 8),
315 1.1 itohy bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
316 1.1 itohy NJATA32_REG_WD_SDH));
317 1.1 itohy printf("status %#x\n",
318 1.1 itohy bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
319 1.1 itohy NJATA32_REG_WD_STATUS));
320 1.1 itohy }
321 1.1 itohy
322 1.1 itohy static void
323 1.1 itohy njata32_setup_channel(chp)
324 1.1 itohy struct ata_channel *chp;
325 1.1 itohy {
326 1.1 itohy struct njata32_softc *sc = (void *)chp->ch_atac;
327 1.1 itohy struct ata_drive_datas *drvp;
328 1.1 itohy int drive;
329 1.1 itohy uint8_t mode;
330 1.1 itohy
331 1.1 itohy KASSERT(chp->ch_ndrive != 0);
332 1.1 itohy
333 1.1 itohy sc->sc_timing_pio = 0;
334 1.1 itohy #if 0 /* ATA DMA is currently unused */
335 1.1 itohy sc->sc_timing_dma = 0;
336 1.1 itohy #endif
337 1.1 itohy
338 1.1 itohy for (drive = 0; drive < chp->ch_ndrive; drive++) {
339 1.1 itohy drvp = &chp->ch_drive[drive];
340 1.1 itohy if ((drvp->drive_flags & DRIVE) == 0)
341 1.1 itohy continue; /* no drive */
342 1.1 itohy
343 1.1 itohy #if 0 /* ATA DMA is currently unused */
344 1.1 itohy if ((drvp->drive_flags & DRIVE_DMA) != 0) {
345 1.1 itohy /*
346 1.1 itohy * Multiword DMA
347 1.1 itohy */
348 1.1 itohy if ((mode = drvp->DMA_mode) > NJATA32_MODE_MAX_DMA)
349 1.1 itohy mode = NJATA32_MODE_MAX_DMA;
350 1.1 itohy if (sc->sc_timing_dma < njata32_timing_dma[mode])
351 1.1 itohy sc->sc_timing_dma = njata32_timing_dma[mode];
352 1.1 itohy }
353 1.1 itohy #endif
354 1.1 itohy /*
355 1.1 itohy * PIO
356 1.1 itohy */
357 1.1 itohy if ((mode = drvp->PIO_mode) > NJATA32_MODE_MAX_PIO)
358 1.1 itohy mode = NJATA32_MODE_MAX_PIO;
359 1.1 itohy if (sc->sc_timing_pio < njata32_timing_pio[mode])
360 1.1 itohy sc->sc_timing_pio = njata32_timing_pio[mode];
361 1.1 itohy }
362 1.1 itohy
363 1.1 itohy /* set timing for PIO */
364 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
365 1.1 itohy NJATA32_REG_TIMING, sc->sc_timing_pio);
366 1.1 itohy }
367 1.1 itohy
368 1.1 itohy /*
369 1.1 itohy * map DMA buffer
370 1.1 itohy */
371 1.1 itohy int
372 1.1 itohy njata32_dma_init(v, channel, drive, databuf, datalen, flags)
373 1.1 itohy void *v;
374 1.1 itohy int channel, drive;
375 1.1 itohy void *databuf;
376 1.1 itohy size_t datalen;
377 1.1 itohy int flags;
378 1.1 itohy {
379 1.1 itohy struct njata32_softc *sc = v;
380 1.1 itohy int error;
381 1.1 itohy struct njata32_device *dev = &sc->sc_dev[drive];
382 1.1 itohy
383 1.1 itohy KASSERT(channel == 0);
384 1.1 itohy KASSERT((dev->d_flags & NJATA32_DEV_DMA_MAPPED) == 0);
385 1.1 itohy KASSERT((dev->d_flags & NJATA32_DEV_DMA_STARTED) == 0);
386 1.1 itohy
387 1.1 itohy KASSERT(flags & (WDC_DMA_PIOBM_ATA | WDC_DMA_PIOBM_ATAPI));
388 1.1 itohy
389 1.1 itohy /* use PIO for short transfer */
390 1.1 itohy if (datalen < 64 /* needs tune */) {
391 1.1 itohy DPRINTF(("%s: njata32_dma_init: short transfer (%u)\n",
392 1.1 itohy NJATA32NAME(sc), (unsigned)datalen));
393 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
394 1.1 itohy NJATA32_REG_TIMING, sc->sc_timing_pio);
395 1.1 itohy return EINVAL;
396 1.1 itohy }
397 1.1 itohy
398 1.1 itohy /* use PIO for unaligned transfer (word alignment seems OK) */
399 1.1 itohy if (((uintptr_t)databuf & 1) || (datalen & 1)) {
400 1.1 itohy DPRINTF(("%s: njata32_dma_init: unaligned: buf %p, len %u\n",
401 1.1 itohy NJATA32NAME(sc), databuf, (unsigned)datalen));
402 1.1 itohy return EINVAL;
403 1.1 itohy }
404 1.1 itohy
405 1.1 itohy DPRINTF(("%s: njata32_dma_init: %s: databuf %p, datalen %u\n",
406 1.1 itohy NJATA32NAME(sc), (flags & WDC_DMA_READ) ? "read" : "write",
407 1.1 itohy databuf, (unsigned)datalen));
408 1.1 itohy
409 1.1 itohy error = bus_dmamap_load(sc->sc_dmat, dev->d_dmamap_xfer,
410 1.1 itohy databuf, datalen, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
411 1.1 itohy ((flags & WDC_DMA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE));
412 1.1 itohy if (error) {
413 1.1 itohy printf("%s: load xfer failed, error %d\n",
414 1.1 itohy NJATA32NAME(sc), error);
415 1.1 itohy return error;
416 1.1 itohy }
417 1.1 itohy
418 1.1 itohy bus_dmamap_sync(sc->sc_dmat, dev->d_dmamap_xfer, 0,
419 1.1 itohy dev->d_dmamap_xfer->dm_mapsize,
420 1.1 itohy (flags & WDC_DMA_READ) ?
421 1.1 itohy BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
422 1.1 itohy
423 1.1 itohy dev->d_flags =
424 1.1 itohy ((flags & WDC_DMA_READ) ? NJATA32_DEV_DMA_READ : 0) |
425 1.1 itohy ((flags & WDC_DMA_PIOBM_ATAPI) ? NJATA32_DEV_DMA_ATAPI : 0) |
426 1.1 itohy NJATA32_DEV_DMA_MAPPED;
427 1.1 itohy
428 1.1 itohy return 0;
429 1.1 itohy }
430 1.1 itohy
431 1.1 itohy /*
432 1.1 itohy * start DMA
433 1.1 itohy *
434 1.1 itohy * top: databuf + skip
435 1.1 itohy * size: xferlen
436 1.1 itohy */
437 1.1 itohy void
438 1.1 itohy njata32_piobm_start(v, channel, drive, skip, xferlen, flags)
439 1.1 itohy void *v;
440 1.1 itohy int channel, drive;
441 1.1 itohy int skip, xferlen; /* offset/size in mapped DMA buffer */
442 1.1 itohy int flags;
443 1.1 itohy {
444 1.1 itohy struct njata32_softc *sc = v;
445 1.1 itohy struct njata32_device *dev = &sc->sc_dev[drive];
446 1.1 itohy int i, nsegs, seglen;
447 1.1 itohy uint8_t bmreg;
448 1.1 itohy
449 1.1 itohy DPRINTF(("%s: njata32_piobm_start: ch%d, dv%d, skip %d, xferlen %d\n",
450 1.1 itohy NJATA32NAME(sc), channel, drive, skip, xferlen));
451 1.1 itohy
452 1.1 itohy KASSERT(channel == 0);
453 1.1 itohy KASSERT(dev->d_flags & NJATA32_DEV_DMA_MAPPED);
454 1.1 itohy KASSERT((dev->d_flags & NJATA32_DEV_DMA_STARTED) == 0);
455 1.1 itohy
456 1.1 itohy /*
457 1.1 itohy * create scatter/gather table
458 1.1 itohy * XXX this code may be slow
459 1.1 itohy */
460 1.1 itohy for (i = nsegs = 0;
461 1.1 itohy i < dev->d_dmamap_xfer->dm_nsegs && xferlen > 0; i++) {
462 1.1 itohy if (dev->d_dmamap_xfer->dm_segs[i].ds_len <= skip) {
463 1.1 itohy skip -= dev->d_dmamap_xfer->dm_segs[i].ds_len;
464 1.1 itohy continue;
465 1.1 itohy }
466 1.1 itohy
467 1.1 itohy seglen = dev->d_dmamap_xfer->dm_segs[i].ds_len - skip;
468 1.1 itohy if (seglen > xferlen)
469 1.1 itohy seglen = xferlen;
470 1.1 itohy
471 1.1 itohy dev->d_sgt[nsegs].sg_addr =
472 1.1 itohy htole32(dev->d_dmamap_xfer->dm_segs[i].ds_addr + skip);
473 1.1 itohy dev->d_sgt[nsegs].sg_len = htole32(seglen);
474 1.1 itohy
475 1.1 itohy xferlen -= seglen;
476 1.1 itohy nsegs++;
477 1.1 itohy skip = 0;
478 1.1 itohy }
479 1.1 itohy sc->sc_piobm_nsegs = nsegs;
480 1.1 itohy /* end mark */
481 1.1 itohy dev->d_sgt[nsegs - 1].sg_len |= htole32(NJATA32_SGT_ENDMARK);
482 1.1 itohy
483 1.1 itohy #ifdef DIAGNOSTIC
484 1.1 itohy if (xferlen)
485 1.1 itohy panic("%s: njata32_piobm_start: xferlen residue %d\n",
486 1.1 itohy NJATA32NAME(sc), xferlen);
487 1.1 itohy #endif
488 1.1 itohy
489 1.1 itohy bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_sgt,
490 1.1 itohy (char *)dev->d_sgt - (char *)sc->sc_sgtpg,
491 1.1 itohy sizeof(struct njata32_sgtable) * nsegs,
492 1.1 itohy BUS_DMASYNC_PREWRITE);
493 1.1 itohy
494 1.1 itohy /* set timing for PIO */
495 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
496 1.1 itohy NJATA32_REG_TIMING, sc->sc_timing_pio);
497 1.1 itohy
498 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc), NJATA32_REG_IOBM,
499 1.1 itohy NJATA32_IOBM_DEFAULT);
500 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc), NJATA32_REG_AS,
501 1.1 itohy NJATA32_AS_WAIT0);
502 1.1 itohy
503 1.1 itohy /*
504 1.1 itohy * interrupt configuration
505 1.1 itohy */
506 1.1 itohy if ((dev->d_flags & (NJATA32_DEV_DMA_READ | NJATA32_DEV_DMA_ATAPI)) ==
507 1.1 itohy NJATA32_DEV_DMA_READ) {
508 1.1 itohy /*
509 1.1 itohy * ATA piobm read is executed while device interrupt is active,
510 1.1 itohy * so disable device interrupt here
511 1.1 itohy */
512 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
513 1.1 itohy NJATA32_REG_IRQ_SELECT, NJATA32_IRQ_XFER);
514 1.1 itohy }
515 1.1 itohy
516 1.1 itohy /* enable scatter/gather busmaster transfer */
517 1.1 itohy bmreg = NJATA32_BM_EN | NJATA32_BM_SG |
518 1.1 itohy (sc->sc_bmwait << NJATA32_BM_WAIT_SHIFT) |
519 1.1 itohy ((dev->d_flags & NJATA32_DEV_DMA_READ) ? NJATA32_BM_RD : 0);
520 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc), NJATA32_REG_BM,
521 1.1 itohy bmreg);
522 1.1 itohy
523 1.1 itohy /* load scatter/gather table */
524 1.1 itohy bus_space_write_4(NJATA32_REGT(sc), NJATA32_REGH(sc),
525 1.1 itohy NJATA32_REG_DMAADDR, dev->d_sgt_dma);
526 1.1 itohy bus_space_write_4(NJATA32_REGT(sc), NJATA32_REGH(sc),
527 1.1 itohy NJATA32_REG_DMALENGTH, sizeof(struct njata32_sgtable) * nsegs);
528 1.1 itohy
529 1.1 itohy /* start transfer */
530 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc), NJATA32_REG_BM,
531 1.1 itohy (bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
532 1.1 itohy NJATA32_REG_BM)
533 1.1 itohy & ~(NJATA32_BM_RD|NJATA32_BM_SG|NJATA32_BM_WAIT_MASK)) |
534 1.1 itohy bmreg | NJATA32_BM_GO);
535 1.1 itohy
536 1.1 itohy sc->sc_devflags = dev->d_flags;
537 1.1 itohy if (flags & WDC_PIOBM_XFER_IRQ)
538 1.1 itohy sc->sc_devflags |= NJATA32_DEV_XFER_INTR;
539 1.1 itohy #ifdef DIAGNOSTIC
540 1.1 itohy dev->d_flags |= NJATA32_DEV_DMA_STARTED;
541 1.1 itohy #endif
542 1.1 itohy }
543 1.1 itohy
544 1.1 itohy /*
545 1.1 itohy * end of DMA
546 1.1 itohy */
547 1.1 itohy int
548 1.1 itohy njata32_dma_finish(v, channel, drive, force)
549 1.1 itohy void *v;
550 1.1 itohy int channel, drive;
551 1.1 itohy int force;
552 1.1 itohy {
553 1.1 itohy struct njata32_softc *sc = v;
554 1.1 itohy int bm;
555 1.1 itohy int error = 0;
556 1.1 itohy
557 1.1 itohy DPRINTF(("%s: njata32_dma_finish: bm = %#x\n", NJATA32NAME(sc),
558 1.1 itohy bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
559 1.1 itohy NJATA32_REG_BM)));
560 1.1 itohy
561 1.1 itohy KASSERT(channel == 0);
562 1.1 itohy KASSERT(sc->sc_dev[drive].d_flags & NJATA32_DEV_DMA_MAPPED);
563 1.1 itohy KASSERT(sc->sc_dev[drive].d_flags & NJATA32_DEV_DMA_STARTED);
564 1.1 itohy
565 1.1 itohy bm = bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
566 1.1 itohy NJATA32_REG_BM);
567 1.1 itohy
568 1.1 itohy #ifdef NJATA32_DEBUG
569 1.1 itohy printf("%s: irq %#x, bm %#x, 18 %#x, 1c %#x\n", NJATA32NAME(sc),
570 1.1 itohy bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
571 1.1 itohy NJATA32_REG_IRQ_STAT),
572 1.1 itohy bm,
573 1.1 itohy bus_space_read_4(NJATA32_REGT(sc), NJATA32_REGH(sc), 0x18),
574 1.1 itohy bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc), 0x1c));
575 1.1 itohy #endif
576 1.1 itohy
577 1.1 itohy /* check if DMA is active */
578 1.1 itohy if (bm & NJATA32_BM_GO) {
579 1.1 itohy error = WDC_DMAST_NOIRQ;
580 1.1 itohy
581 1.1 itohy switch (force) {
582 1.1 itohy case WDC_DMAEND_END:
583 1.1 itohy return error;
584 1.1 itohy
585 1.1 itohy case WDC_DMAEND_ABRT:
586 1.1 itohy printf("%s: aborting DMA\n", NJATA32NAME(sc));
587 1.1 itohy break;
588 1.1 itohy }
589 1.1 itohy }
590 1.1 itohy
591 1.1 itohy /*
592 1.1 itohy * ???
593 1.1 itohy * For unknown reason, PIOBM transfer sometimes fails in the middle,
594 1.1 itohy * in which case the bit #7 of BM register becomes 0.
595 1.1 itohy * Increasing the wait value seems to improve the situation.
596 1.1 itohy */
597 1.1 itohy if ((bm & NJATA32_BM_DONE) == 0) {
598 1.1 itohy error |= WDC_DMAST_ERR;
599 1.1 itohy printf("%s: busmaster error", NJATA32NAME(sc));
600 1.1 itohy if (sc->sc_bmwait < 1 /* XXX */) {
601 1.1 itohy sc->sc_bmwait++;
602 1.1 itohy printf(", new busmaster wait = %d", sc->sc_bmwait);
603 1.1 itohy }
604 1.1 itohy printf("\n");
605 1.1 itohy }
606 1.1 itohy
607 1.1 itohy /* stop command */
608 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc), NJATA32_REG_AS,
609 1.1 itohy NJATA32_AS_WAIT0);
610 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc), NJATA32_REG_BM,
611 1.1 itohy (sc->sc_bmwait << NJATA32_BM_WAIT_SHIFT));
612 1.1 itohy
613 1.1 itohy /* set timing for PIO */
614 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
615 1.1 itohy NJATA32_REG_TIMING, sc->sc_timing_pio);
616 1.1 itohy
617 1.1 itohy /*
618 1.1 itohy * reenable device interrupt in case it was disabled for
619 1.1 itohy * this transfer
620 1.1 itohy */
621 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
622 1.1 itohy NJATA32_REG_IRQ_SELECT, NJATA32_IRQ_XFER | NJATA32_IRQ_DEV);
623 1.1 itohy
624 1.1 itohy #if 1 /* should be? */
625 1.1 itohy if ((sc->sc_devflags & NJATA32_DEV_GOT_XFER_INTR) == 0)
626 1.1 itohy error |= WDC_DMAST_ERR;
627 1.1 itohy #endif
628 1.1 itohy sc->sc_devflags = 0;
629 1.1 itohy
630 1.1 itohy #ifdef DIAGNOSTIC
631 1.1 itohy sc->sc_dev[drive].d_flags &= ~NJATA32_DEV_DMA_STARTED;
632 1.1 itohy #endif
633 1.1 itohy
634 1.1 itohy return error;
635 1.1 itohy }
636 1.1 itohy
637 1.1 itohy /*
638 1.1 itohy * unmap DMA buffer
639 1.1 itohy */
640 1.1 itohy void
641 1.1 itohy njata32_piobm_done(v, channel, drive)
642 1.1 itohy void *v;
643 1.1 itohy int channel, drive;
644 1.1 itohy {
645 1.1 itohy struct njata32_softc *sc = v;
646 1.1 itohy struct njata32_device *dev = &sc->sc_dev[drive];
647 1.1 itohy
648 1.1 itohy DPRINTF(("%s: njata32_piobm_done: ch%d dv%d\n",
649 1.1 itohy NJATA32NAME(sc), channel, drive));
650 1.1 itohy
651 1.1 itohy KASSERT(channel == 0);
652 1.1 itohy KASSERT(dev->d_flags & NJATA32_DEV_DMA_MAPPED);
653 1.1 itohy KASSERT((dev->d_flags & NJATA32_DEV_DMA_STARTED) == 0);
654 1.1 itohy
655 1.1 itohy bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_sgt,
656 1.1 itohy (char *)dev->d_sgt - (char *)sc->sc_sgtpg,
657 1.1 itohy sizeof(struct njata32_sgtable) * sc->sc_piobm_nsegs,
658 1.1 itohy BUS_DMASYNC_POSTWRITE);
659 1.1 itohy
660 1.1 itohy /* unload dma map */
661 1.1 itohy bus_dmamap_sync(sc->sc_dmat, dev->d_dmamap_xfer,
662 1.1 itohy 0, dev->d_dmamap_xfer->dm_mapsize,
663 1.1 itohy (dev->d_flags & NJATA32_DEV_DMA_READ) ?
664 1.1 itohy BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
665 1.1 itohy
666 1.1 itohy bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_sgt,
667 1.1 itohy (char *)dev->d_sgt - (char *)sc->sc_sgtpg,
668 1.1 itohy sizeof(struct njata32_sgtable) * NJATA32_NUM_SG,
669 1.1 itohy BUS_DMASYNC_POSTWRITE);
670 1.1 itohy
671 1.1 itohy bus_dmamap_unload(sc->sc_dmat, dev->d_dmamap_xfer);
672 1.1 itohy dev->d_flags &= ~NJATA32_DEV_DMA_MAPPED;
673 1.1 itohy }
674 1.1 itohy
675 1.1 itohy int
676 1.1 itohy njata32_intr(arg)
677 1.1 itohy void *arg;
678 1.1 itohy {
679 1.1 itohy struct njata32_softc *sc = arg;
680 1.1 itohy struct ata_channel *chp;
681 1.1 itohy int irq;
682 1.1 itohy
683 1.1 itohy irq = bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
684 1.1 itohy NJATA32_REG_IRQ_STAT);
685 1.1 itohy if ((irq & (NJATA32_IRQ_XFER | NJATA32_IRQ_DEV)) == 0)
686 1.1 itohy return 0; /* not mine */
687 1.1 itohy
688 1.1 itohy DPRINTF(("%s: njata32_intr: irq = %#x, altstatus = %#x\n",
689 1.1 itohy NJATA32NAME(sc), irq,
690 1.1 itohy bus_space_read_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
691 1.1 itohy NJATA32_REG_WD_ALTSTATUS)));
692 1.1 itohy
693 1.1 itohy chp = &sc->sc_ch[0].ch_ata_channel;
694 1.1 itohy
695 1.1 itohy if (irq & NJATA32_IRQ_XFER)
696 1.1 itohy sc->sc_devflags |= NJATA32_DEV_GOT_XFER_INTR;
697 1.1 itohy
698 1.1 itohy if ((irq & (NJATA32_IRQ_XFER | NJATA32_IRQ_DEV)) == NJATA32_IRQ_XFER &&
699 1.1 itohy (sc->sc_devflags & NJATA32_DEV_XFER_INTR) == 0) {
700 1.1 itohy /*
701 1.1 itohy * transfer done, wait for device interrupt
702 1.1 itohy */
703 1.1 itohy bus_space_write_1(NJATA32_REGT(sc), NJATA32_REGH(sc),
704 1.1 itohy NJATA32_REG_BM, (sc->sc_bmwait << NJATA32_BM_WAIT_SHIFT));
705 1.1 itohy return 1;
706 1.1 itohy }
707 1.1 itohy
708 1.1 itohy /*
709 1.1 itohy * If both transfer done interrupt and device interrupt are
710 1.1 itohy * active for ATAPI transfer, call wdcintr() twice.
711 1.1 itohy */
712 1.1 itohy if ((sc->sc_devflags & NJATA32_DEV_DMA_ATAPI) &&
713 1.1 itohy (irq & (NJATA32_IRQ_XFER | NJATA32_IRQ_DEV)) ==
714 1.1 itohy (NJATA32_IRQ_XFER | NJATA32_IRQ_DEV) &&
715 1.1 itohy (sc->sc_devflags & NJATA32_DEV_XFER_INTR)) {
716 1.1 itohy if (wdcintr(chp) == 0) {
717 1.1 itohy njata32_clearirq(&sc->sc_ch[0].ch_ata_channel, irq);
718 1.1 itohy }
719 1.1 itohy }
720 1.1 itohy
721 1.1 itohy if (wdcintr(chp) == 0) {
722 1.1 itohy njata32_clearirq(&sc->sc_ch[0].ch_ata_channel, irq);
723 1.1 itohy }
724 1.1 itohy
725 1.1 itohy return 1;
726 1.1 itohy }
727