wdc_obio.c revision 1.62 1 1.62 thorpej /* $NetBSD: wdc_obio.c,v 1.62 2021/01/26 14:49:41 thorpej Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*-
4 1.27 mycroft * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
5 1.1 tsubai * All rights reserved.
6 1.1 tsubai *
7 1.1 tsubai * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tsubai * by Charles M. Hannum and by Onno van der Linden.
9 1.1 tsubai *
10 1.1 tsubai * Redistribution and use in source and binary forms, with or without
11 1.1 tsubai * modification, are permitted provided that the following conditions
12 1.1 tsubai * are met:
13 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
14 1.1 tsubai * notice, this list of conditions and the following disclaimer.
15 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
17 1.1 tsubai * documentation and/or other materials provided with the distribution.
18 1.1 tsubai *
19 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 tsubai * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 tsubai * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 tsubai * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 tsubai * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 tsubai * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 tsubai * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 tsubai * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 tsubai * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 tsubai * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 tsubai * POSSIBILITY OF SUCH DAMAGE.
30 1.1 tsubai */
31 1.26 lukem
32 1.26 lukem #include <sys/cdefs.h>
33 1.62 thorpej __KERNEL_RCSID(0, "$NetBSD: wdc_obio.c,v 1.62 2021/01/26 14:49:41 thorpej Exp $");
34 1.1 tsubai
35 1.1 tsubai #include <sys/param.h>
36 1.1 tsubai #include <sys/systm.h>
37 1.1 tsubai #include <sys/device.h>
38 1.59 macallan #include <sys/kmem.h>
39 1.1 tsubai
40 1.10 mrg #include <uvm/uvm_extern.h>
41 1.1 tsubai
42 1.53 dyoung #include <sys/bus.h>
43 1.1 tsubai #include <machine/autoconf.h>
44 1.47 garbled #include <machine/pio.h>
45 1.1 tsubai
46 1.9 tsubai #include <dev/ata/atareg.h>
47 1.1 tsubai #include <dev/ata/atavar.h>
48 1.1 tsubai #include <dev/ic/wdcvar.h>
49 1.1 tsubai
50 1.12 matt #include <dev/ofw/openfirm.h>
51 1.12 matt
52 1.1 tsubai #include <macppc/dev/dbdma.h>
53 1.1 tsubai
54 1.1 tsubai #define WDC_REG_NPORTS 8
55 1.1 tsubai #define WDC_AUXREG_OFFSET 0x16
56 1.1 tsubai #define WDC_DEFAULT_PIO_IRQ 13 /* XXX */
57 1.1 tsubai #define WDC_DEFAULT_DMA_IRQ 2 /* XXX */
58 1.1 tsubai
59 1.1 tsubai #define WDC_OPTIONS_DMA 0x01
60 1.1 tsubai
61 1.1 tsubai /*
62 1.1 tsubai * XXX This code currently doesn't even try to allow 32-bit data port use.
63 1.1 tsubai */
64 1.1 tsubai
65 1.1 tsubai struct wdc_obio_softc {
66 1.1 tsubai struct wdc_softc sc_wdcdev;
67 1.39 thorpej struct ata_channel *sc_chanptr;
68 1.39 thorpej struct ata_channel sc_channel;
69 1.39 thorpej struct wdc_regs sc_wdc_regs;
70 1.47 garbled bus_space_handle_t sc_dmaregh;
71 1.1 tsubai dbdma_regmap_t *sc_dmareg;
72 1.1 tsubai dbdma_command_t *sc_dmacmd;
73 1.18 dbj u_int sc_dmaconf[2]; /* per target value of CONFIG_REG */
74 1.59 macallan void *sc_ih, *sc_dma;
75 1.1 tsubai };
76 1.1 tsubai
77 1.48 matt static int wdc_obio_match(device_t, cfdata_t, void *);
78 1.48 matt static void wdc_obio_attach(device_t, device_t, void *);
79 1.48 matt static int wdc_obio_detach(device_t, int);
80 1.48 matt static int wdc_obio_dma_init(void *, int, int, void *, size_t, int);
81 1.48 matt static void wdc_obio_dma_start(void *, int, int);
82 1.48 matt static int wdc_obio_dma_finish(void *, int, int, int);
83 1.48 matt
84 1.48 matt static void wdc_obio_select(struct ata_channel *, int);
85 1.48 matt static void adjust_timing(struct ata_channel *);
86 1.48 matt static void ata4_adjust_timing(struct ata_channel *);
87 1.1 tsubai
88 1.49 cube CFATTACH_DECL_NEW(wdc_obio, sizeof(struct wdc_obio_softc),
89 1.52 dyoung wdc_obio_match, wdc_obio_attach, wdc_obio_detach, NULL);
90 1.1 tsubai
91 1.48 matt static const char * const ata_names[] = {
92 1.47 garbled "heathrow-ata",
93 1.47 garbled "keylargo-ata",
94 1.47 garbled "ohare-ata",
95 1.47 garbled NULL
96 1.47 garbled };
97 1.47 garbled
98 1.1 tsubai int
99 1.48 matt wdc_obio_match(device_t parent, cfdata_t match, void *aux)
100 1.1 tsubai {
101 1.1 tsubai struct confargs *ca = aux;
102 1.1 tsubai
103 1.3 tsubai /* XXX should not use name */
104 1.1 tsubai if (strcmp(ca->ca_name, "ATA") == 0 ||
105 1.1 tsubai strcmp(ca->ca_name, "ata") == 0 ||
106 1.2 tsubai strcmp(ca->ca_name, "ata0") == 0 ||
107 1.1 tsubai strcmp(ca->ca_name, "ide") == 0)
108 1.3 tsubai return 1;
109 1.3 tsubai
110 1.62 thorpej if (of_compatible(ca->ca_node, ata_names))
111 1.1 tsubai return 1;
112 1.1 tsubai
113 1.1 tsubai return 0;
114 1.1 tsubai }
115 1.1 tsubai
116 1.1 tsubai void
117 1.48 matt wdc_obio_attach(device_t parent, device_t self, void *aux)
118 1.1 tsubai {
119 1.48 matt struct wdc_obio_softc *sc = device_private(self);
120 1.39 thorpej struct wdc_regs *wdr;
121 1.1 tsubai struct confargs *ca = aux;
122 1.39 thorpej struct ata_channel *chp = &sc->sc_channel;
123 1.47 garbled int intr, i, type = IST_EDGE;
124 1.1 tsubai int use_dma = 0;
125 1.47 garbled char path[80];
126 1.1 tsubai
127 1.49 cube sc->sc_wdcdev.sc_atac.atac_dev = self;
128 1.49 cube if (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
129 1.44 thorpej WDC_OPTIONS_DMA) {
130 1.1 tsubai if (ca->ca_nreg >= 16 || ca->ca_nintr == -1)
131 1.1 tsubai use_dma = 1; /* XXX Don't work yet. */
132 1.1 tsubai }
133 1.1 tsubai
134 1.1 tsubai if (ca->ca_nintr >= 4 && ca->ca_nreg >= 8) {
135 1.4 tsubai intr = ca->ca_intr[0];
136 1.48 matt aprint_normal(" irq %d", intr);
137 1.47 garbled if (ca->ca_nintr > 8) {
138 1.47 garbled type = ca->ca_intr[1] ? IST_LEVEL : IST_EDGE;
139 1.47 garbled }
140 1.48 matt aprint_normal(", %s triggered", (type == IST_EDGE) ? "edge" : "level");
141 1.4 tsubai } else if (ca->ca_nintr == -1) {
142 1.4 tsubai intr = WDC_DEFAULT_PIO_IRQ;
143 1.48 matt aprint_normal(" irq property not found; using %d", intr);
144 1.4 tsubai } else {
145 1.48 matt aprint_error(": couldn't get irq property\n");
146 1.1 tsubai return;
147 1.1 tsubai }
148 1.1 tsubai
149 1.1 tsubai if (use_dma)
150 1.48 matt aprint_normal(": DMA transfer");
151 1.1 tsubai
152 1.48 matt aprint_normal("\n");
153 1.1 tsubai
154 1.39 thorpej sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
155 1.39 thorpej
156 1.47 garbled wdr->cmd_iot = wdr->ctl_iot = ca->ca_tag;
157 1.1 tsubai
158 1.47 garbled if (bus_space_map(wdr->cmd_iot, ca->ca_baseaddr + ca->ca_reg[0],
159 1.47 garbled WDC_REG_NPORTS << 4, 0, &wdr->cmd_baseioh) ||
160 1.39 thorpej bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
161 1.47 garbled WDC_AUXREG_OFFSET << 4, 1, &wdr->ctl_ioh)) {
162 1.48 matt aprint_error_dev(self, "couldn't map registers\n");
163 1.1 tsubai return;
164 1.1 tsubai }
165 1.47 garbled
166 1.30 bouyer for (i = 0; i < WDC_NREG; i++) {
167 1.47 garbled if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i << 4,
168 1.39 thorpej i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
169 1.39 thorpej bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
170 1.47 garbled WDC_REG_NPORTS << 4);
171 1.48 matt aprint_error_dev(self,
172 1.48 matt "couldn't subregion registers\n");
173 1.30 bouyer return;
174 1.30 bouyer }
175 1.30 bouyer }
176 1.1 tsubai #if 0
177 1.39 thorpej wdr->data32iot = wdr->cmd_iot;
178 1.39 thorpej wdr->data32ioh = wdr->cmd_ioh;
179 1.1 tsubai #endif
180 1.1 tsubai
181 1.47 garbled sc->sc_ih = intr_establish(intr, type, IPL_BIO, wdcintr, chp);
182 1.1 tsubai
183 1.1 tsubai if (use_dma) {
184 1.59 macallan sc->sc_dmacmd = dbdma_alloc(sizeof(dbdma_command_t) * 20,
185 1.59 macallan &sc->sc_dma);
186 1.47 garbled /*
187 1.47 garbled * XXX
188 1.47 garbled * we don't use ca->ca_reg[3] for size here because at least
189 1.47 garbled * on the PB3400c it says 0x200 for both IDE channels ( the
190 1.47 garbled * one on the mainboard and the other on the mediabay ) but
191 1.47 garbled * their start addresses are only 0x100 apart. Since those
192 1.47 garbled * DMA registers are always 0x100 or less we don't really
193 1.47 garbled * have to care though
194 1.47 garbled */
195 1.47 garbled if (bus_space_map(wdr->cmd_iot, ca->ca_baseaddr + ca->ca_reg[2],
196 1.47 garbled 0x100, BUS_SPACE_MAP_LINEAR, &sc->sc_dmaregh)) {
197 1.47 garbled
198 1.48 matt aprint_error_dev(self,
199 1.48 matt "unable to map DMA registers (%08x)\n",
200 1.47 garbled ca->ca_reg[2]);
201 1.47 garbled /* should unmap stuff here */
202 1.47 garbled return;
203 1.47 garbled }
204 1.47 garbled sc->sc_dmareg = bus_space_vaddr(wdr->cmd_iot, sc->sc_dmaregh);
205 1.47 garbled
206 1.40 thorpej sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
207 1.40 thorpej sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
208 1.16 bouyer if (strcmp(ca->ca_name, "ata-4") == 0) {
209 1.40 thorpej sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_UDMA;
210 1.40 thorpej sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
211 1.45 macallan sc->sc_wdcdev.sc_atac.atac_set_modes =
212 1.45 macallan ata4_adjust_timing;
213 1.16 bouyer } else {
214 1.40 thorpej sc->sc_wdcdev.sc_atac.atac_set_modes = adjust_timing;
215 1.16 bouyer }
216 1.13 bouyer #ifdef notyet
217 1.13 bouyer /* Minimum cycle time is 150ns (DMA MODE 1) on ohare. */
218 1.13 bouyer if (ohare) {
219 1.40 thorpej sc->sc_wdcdev.sc_atac.atac_pio_cap = 3;
220 1.40 thorpej sc->sc_wdcdev.sc_atac.atac_dma_cap = 1;
221 1.13 bouyer }
222 1.13 bouyer #endif
223 1.17 bouyer } else {
224 1.24 wiz /* all non-DMA controllers can use adjust_timing */
225 1.40 thorpej sc->sc_wdcdev.sc_atac.atac_set_modes = adjust_timing;
226 1.59 macallan sc->sc_dmacmd = NULL;
227 1.1 tsubai }
228 1.17 bouyer
229 1.40 thorpej sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
230 1.59 macallan sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 /*| ATAC_CAP_DATA32*/;
231 1.39 thorpej sc->sc_chanptr = chp;
232 1.40 thorpej sc->sc_wdcdev.sc_atac.atac_channels = &sc->sc_chanptr;
233 1.40 thorpej sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
234 1.58 bouyer sc->sc_wdcdev.wdc_maxdrives = 2;
235 1.1 tsubai sc->sc_wdcdev.dma_arg = sc;
236 1.1 tsubai sc->sc_wdcdev.dma_init = wdc_obio_dma_init;
237 1.1 tsubai sc->sc_wdcdev.dma_start = wdc_obio_dma_start;
238 1.1 tsubai sc->sc_wdcdev.dma_finish = wdc_obio_dma_finish;
239 1.61 jdolecek
240 1.35 thorpej chp->ch_channel = 0;
241 1.40 thorpej chp->ch_atac = &sc->sc_wdcdev.sc_atac;
242 1.7 tsubai
243 1.60 jdolecek wdc_init_shadow_regs(wdr);
244 1.41 aymeric
245 1.7 tsubai #define OHARE_FEATURE_REG 0xf3000038
246 1.7 tsubai
247 1.7 tsubai /* XXX Enable wdc1 by feature reg. */
248 1.14 wiz memset(path, 0, sizeof(path));
249 1.7 tsubai OF_package_to_path(ca->ca_node, path, sizeof(path));
250 1.7 tsubai if (strcmp(path, "/bandit@F2000000/ohare@10/ata@21000") == 0) {
251 1.7 tsubai u_int x;
252 1.7 tsubai
253 1.7 tsubai x = in32rb(OHARE_FEATURE_REG);
254 1.7 tsubai x |= 8;
255 1.7 tsubai out32rb(OHARE_FEATURE_REG, x);
256 1.1 tsubai }
257 1.1 tsubai
258 1.29 bouyer wdcattach(chp);
259 1.9 tsubai }
260 1.9 tsubai
261 1.9 tsubai /* Multiword DMA transfer timings */
262 1.13 bouyer struct ide_timings {
263 1.9 tsubai int cycle; /* minimum cycle time [ns] */
264 1.9 tsubai int active; /* minimum command active time [ns] */
265 1.13 bouyer };
266 1.48 matt static const struct ide_timings pio_timing[5] = {
267 1.19 dbj { 600, 180 }, /* Mode 0 */
268 1.19 dbj { 390, 150 }, /* 1 */
269 1.19 dbj { 240, 105 }, /* 2 */
270 1.19 dbj { 180, 90 }, /* 3 */
271 1.19 dbj { 120, 75 } /* 4 */
272 1.13 bouyer };
273 1.48 matt static const struct ide_timings dma_timing[3] = {
274 1.19 dbj { 480, 240 }, /* Mode 0 */
275 1.19 dbj { 165, 90 }, /* Mode 1 */
276 1.19 dbj { 120, 75 } /* Mode 2 */
277 1.9 tsubai };
278 1.9 tsubai
279 1.48 matt static const struct ide_timings udma_timing[5] = {
280 1.48 matt { 120, 180 }, /* Mode 0 */
281 1.48 matt { 90, 150 }, /* Mode 1 */
282 1.48 matt { 60, 120 }, /* Mode 2 */
283 1.48 matt { 45, 90 }, /* Mode 3 */
284 1.48 matt { 30, 90 } /* Mode 4 */
285 1.16 bouyer };
286 1.16 bouyer
287 1.9 tsubai #define TIME_TO_TICK(time) howmany((time), 30)
288 1.16 bouyer #define PIO_REC_OFFSET 4
289 1.16 bouyer #define PIO_REC_MIN 1
290 1.16 bouyer #define PIO_ACT_MIN 1
291 1.16 bouyer #define DMA_REC_OFFSET 1
292 1.16 bouyer #define DMA_REC_MIN 1
293 1.16 bouyer #define DMA_ACT_MIN 1
294 1.16 bouyer
295 1.18 dbj #define ATA4_TIME_TO_TICK(time) howmany((time), 15) /* 15 ns clock */
296 1.16 bouyer
297 1.47 garbled #define CONFIG_REG (0x200) /* IDE access timing register */
298 1.9 tsubai
299 1.18 dbj void
300 1.48 matt wdc_obio_select(struct ata_channel *chp, int drive)
301 1.18 dbj {
302 1.40 thorpej struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->ch_atac;
303 1.40 thorpej struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
304 1.39 thorpej
305 1.39 thorpej bus_space_write_4(wdr->cmd_iot, wdr->cmd_baseioh,
306 1.18 dbj CONFIG_REG, sc->sc_dmaconf[drive]);
307 1.18 dbj }
308 1.9 tsubai
309 1.9 tsubai void
310 1.48 matt adjust_timing(struct ata_channel *chp)
311 1.9 tsubai {
312 1.40 thorpej struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->ch_atac;
313 1.13 bouyer int drive;
314 1.31 mjl int min_cycle = 0, min_active = 0;
315 1.31 mjl int cycle_tick = 0, act_tick = 0, inact_tick = 0, half_tick;
316 1.9 tsubai
317 1.13 bouyer for (drive = 0; drive < 2; drive++) {
318 1.18 dbj u_int conf = 0;
319 1.18 dbj struct ata_drive_datas *drvp;
320 1.18 dbj
321 1.13 bouyer drvp = &chp->ch_drive[drive];
322 1.18 dbj /* set up pio mode timings */
323 1.58 bouyer if (drvp->drive_type != ATA_DRIVET_NONE) {
324 1.18 dbj int piomode = drvp->PIO_mode;
325 1.18 dbj min_cycle = pio_timing[piomode].cycle;
326 1.18 dbj min_active = pio_timing[piomode].active;
327 1.18 dbj
328 1.18 dbj cycle_tick = TIME_TO_TICK(min_cycle);
329 1.18 dbj act_tick = TIME_TO_TICK(min_active);
330 1.18 dbj if (act_tick < PIO_ACT_MIN)
331 1.18 dbj act_tick = PIO_ACT_MIN;
332 1.18 dbj inact_tick = cycle_tick - act_tick - PIO_REC_OFFSET;
333 1.18 dbj if (inact_tick < PIO_REC_MIN)
334 1.18 dbj inact_tick = PIO_REC_MIN;
335 1.18 dbj /* mask: 0x000007ff */
336 1.18 dbj conf |= (inact_tick << 5) | act_tick;
337 1.18 dbj }
338 1.24 wiz /* Set up DMA mode timings */
339 1.58 bouyer if (drvp->drive_flags & ATA_DRIVE_DMA) {
340 1.18 dbj int dmamode = drvp->DMA_mode;
341 1.18 dbj min_cycle = dma_timing[dmamode].cycle;
342 1.18 dbj min_active = dma_timing[dmamode].active;
343 1.18 dbj cycle_tick = TIME_TO_TICK(min_cycle);
344 1.18 dbj act_tick = TIME_TO_TICK(min_active);
345 1.18 dbj inact_tick = cycle_tick - act_tick - DMA_REC_OFFSET;
346 1.18 dbj if (inact_tick < DMA_REC_MIN)
347 1.18 dbj inact_tick = DMA_REC_MIN;
348 1.18 dbj half_tick = 0; /* XXX */
349 1.18 dbj /* mask: 0xfffff800 */
350 1.18 dbj conf |=
351 1.18 dbj (half_tick << 21) |
352 1.18 dbj (inact_tick << 16) | (act_tick << 11);
353 1.13 bouyer }
354 1.20 bouyer #ifdef DEBUG
355 1.18 dbj if (conf) {
356 1.18 dbj printf("conf[%d] = 0x%x, cyc = %d (%d ns), act = %d (%d ns), inact = %d\n",
357 1.18 dbj drive, conf, cycle_tick, min_cycle, act_tick, min_active, inact_tick);
358 1.18 dbj }
359 1.20 bouyer #endif
360 1.18 dbj sc->sc_dmaconf[drive] = conf;
361 1.13 bouyer }
362 1.18 dbj sc->sc_wdcdev.select = 0;
363 1.18 dbj if (sc->sc_dmaconf[0]) {
364 1.18 dbj wdc_obio_select(chp,0);
365 1.38 thorpej if (sc->sc_dmaconf[1] &&
366 1.38 thorpej (sc->sc_dmaconf[0] != sc->sc_dmaconf[1])) {
367 1.18 dbj sc->sc_wdcdev.select = wdc_obio_select;
368 1.13 bouyer }
369 1.18 dbj } else if (sc->sc_dmaconf[1]) {
370 1.18 dbj wdc_obio_select(chp,1);
371 1.13 bouyer }
372 1.16 bouyer }
373 1.16 bouyer
374 1.16 bouyer void
375 1.48 matt ata4_adjust_timing(struct ata_channel *chp)
376 1.16 bouyer {
377 1.40 thorpej struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->ch_atac;
378 1.16 bouyer int drive;
379 1.31 mjl int min_cycle = 0, min_active = 0;
380 1.31 mjl int cycle_tick = 0, act_tick = 0, inact_tick = 0;
381 1.16 bouyer
382 1.18 dbj for (drive = 0; drive < 2; drive++) {
383 1.18 dbj u_int conf = 0;
384 1.18 dbj struct ata_drive_datas *drvp;
385 1.16 bouyer
386 1.16 bouyer drvp = &chp->ch_drive[drive];
387 1.18 dbj /* set up pio mode timings */
388 1.18 dbj
389 1.58 bouyer if (drvp->drive_type != ATA_DRIVET_NONE) {
390 1.18 dbj int piomode = drvp->PIO_mode;
391 1.18 dbj min_cycle = pio_timing[piomode].cycle;
392 1.18 dbj min_active = pio_timing[piomode].active;
393 1.18 dbj
394 1.18 dbj cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
395 1.18 dbj act_tick = ATA4_TIME_TO_TICK(min_active);
396 1.18 dbj inact_tick = cycle_tick - act_tick;
397 1.18 dbj /* mask: 0x000003ff */
398 1.18 dbj conf |= (inact_tick << 5) | act_tick;
399 1.18 dbj }
400 1.18 dbj /* set up dma mode timings */
401 1.58 bouyer if (drvp->drive_flags & ATA_DRIVE_DMA) {
402 1.18 dbj int dmamode = drvp->DMA_mode;
403 1.18 dbj min_cycle = dma_timing[dmamode].cycle;
404 1.18 dbj min_active = dma_timing[dmamode].active;
405 1.18 dbj cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
406 1.18 dbj act_tick = ATA4_TIME_TO_TICK(min_active);
407 1.18 dbj inact_tick = cycle_tick - act_tick;
408 1.18 dbj /* mask: 0x001ffc00 */
409 1.18 dbj conf |= (act_tick << 10) | (inact_tick << 15);
410 1.16 bouyer }
411 1.18 dbj /* set up udma mode timings */
412 1.58 bouyer if (drvp->drive_flags & ATA_DRIVE_UDMA) {
413 1.18 dbj int udmamode = drvp->UDMA_mode;
414 1.18 dbj min_cycle = udma_timing[udmamode].cycle;
415 1.18 dbj min_active = udma_timing[udmamode].active;
416 1.18 dbj act_tick = ATA4_TIME_TO_TICK(min_active);
417 1.18 dbj cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
418 1.18 dbj /* mask: 0x1ff00000 */
419 1.18 dbj conf |= (cycle_tick << 21) | (act_tick << 25) | 0x100000;
420 1.18 dbj }
421 1.20 bouyer #ifdef DEBUG
422 1.18 dbj if (conf) {
423 1.18 dbj printf("ata4 conf[%d] = 0x%x, cyc = %d (%d ns), act = %d (%d ns), inact = %d\n",
424 1.18 dbj drive, conf, cycle_tick, min_cycle, act_tick, min_active, inact_tick);
425 1.16 bouyer }
426 1.20 bouyer #endif
427 1.18 dbj sc->sc_dmaconf[drive] = conf;
428 1.16 bouyer }
429 1.18 dbj sc->sc_wdcdev.select = 0;
430 1.18 dbj if (sc->sc_dmaconf[0]) {
431 1.18 dbj wdc_obio_select(chp,0);
432 1.38 thorpej if (sc->sc_dmaconf[1] &&
433 1.38 thorpej (sc->sc_dmaconf[0] != sc->sc_dmaconf[1])) {
434 1.18 dbj sc->sc_wdcdev.select = wdc_obio_select;
435 1.16 bouyer }
436 1.18 dbj } else if (sc->sc_dmaconf[1]) {
437 1.18 dbj wdc_obio_select(chp,1);
438 1.16 bouyer }
439 1.5 tsubai }
440 1.5 tsubai
441 1.5 tsubai int
442 1.48 matt wdc_obio_detach(device_t self, int flags)
443 1.5 tsubai {
444 1.48 matt struct wdc_obio_softc *sc = device_private(self);
445 1.5 tsubai int error;
446 1.5 tsubai
447 1.5 tsubai if ((error = wdcdetach(self, flags)) != 0)
448 1.5 tsubai return error;
449 1.5 tsubai
450 1.5 tsubai intr_disestablish(sc->sc_ih);
451 1.5 tsubai
452 1.5 tsubai /* Unmap our i/o space. */
453 1.39 thorpej bus_space_unmap(sc->sc_wdcdev.regs->cmd_iot,
454 1.47 garbled sc->sc_wdcdev.regs->cmd_baseioh, WDC_REG_NPORTS << 4);
455 1.5 tsubai
456 1.5 tsubai /* Unmap DMA registers. */
457 1.59 macallan if (sc->sc_dmacmd != NULL) {
458 1.5 tsubai
459 1.59 macallan bus_space_unmap(sc->sc_wdcdev.regs->cmd_iot,
460 1.59 macallan sc->sc_dmaregh, 0x100);
461 1.59 macallan dbdma_free(sc->sc_dma, sizeof(dbdma_command_t) * 20);
462 1.59 macallan }
463 1.5 tsubai return 0;
464 1.1 tsubai }
465 1.1 tsubai
466 1.9 tsubai int
467 1.48 matt wdc_obio_dma_init(void *v, int channel, int drive, void *databuf,
468 1.48 matt size_t datalen, int flags)
469 1.1 tsubai {
470 1.1 tsubai struct wdc_obio_softc *sc = v;
471 1.1 tsubai vaddr_t va = (vaddr_t)databuf;
472 1.1 tsubai dbdma_command_t *cmdp;
473 1.4 tsubai u_int cmd, offset;
474 1.25 hamajima int read = flags & WDC_DMA_READ;
475 1.1 tsubai
476 1.1 tsubai cmdp = sc->sc_dmacmd;
477 1.1 tsubai cmd = read ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
478 1.4 tsubai
479 1.4 tsubai offset = va & PGOFSET;
480 1.4 tsubai
481 1.4 tsubai /* if va is not page-aligned, setup the first page */
482 1.4 tsubai if (offset != 0) {
483 1.23 thorpej int rest = PAGE_SIZE - offset; /* the rest of the page */
484 1.4 tsubai
485 1.4 tsubai if (datalen > rest) { /* if continues to next page */
486 1.4 tsubai DBDMA_BUILD(cmdp, cmd, 0, rest, vtophys(va),
487 1.4 tsubai DBDMA_INT_NEVER, DBDMA_WAIT_NEVER,
488 1.4 tsubai DBDMA_BRANCH_NEVER);
489 1.4 tsubai datalen -= rest;
490 1.4 tsubai va += rest;
491 1.4 tsubai cmdp++;
492 1.4 tsubai }
493 1.4 tsubai }
494 1.4 tsubai
495 1.4 tsubai /* now va is page-aligned */
496 1.23 thorpej while (datalen > PAGE_SIZE) {
497 1.23 thorpej DBDMA_BUILD(cmdp, cmd, 0, PAGE_SIZE, vtophys(va),
498 1.1 tsubai DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
499 1.23 thorpej datalen -= PAGE_SIZE;
500 1.23 thorpej va += PAGE_SIZE;
501 1.1 tsubai cmdp++;
502 1.1 tsubai }
503 1.1 tsubai
504 1.23 thorpej /* the last page (datalen <= PAGE_SIZE here) */
505 1.1 tsubai cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
506 1.1 tsubai DBDMA_BUILD(cmdp, cmd, 0, datalen, vtophys(va),
507 1.4 tsubai DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
508 1.1 tsubai cmdp++;
509 1.1 tsubai
510 1.1 tsubai DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
511 1.1 tsubai DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
512 1.1 tsubai
513 1.1 tsubai return 0;
514 1.1 tsubai }
515 1.1 tsubai
516 1.9 tsubai void
517 1.48 matt wdc_obio_dma_start(void *v, int channel, int drive)
518 1.1 tsubai {
519 1.1 tsubai struct wdc_obio_softc *sc = v;
520 1.1 tsubai
521 1.1 tsubai dbdma_start(sc->sc_dmareg, sc->sc_dmacmd);
522 1.1 tsubai }
523 1.1 tsubai
524 1.9 tsubai int
525 1.48 matt wdc_obio_dma_finish(void *v, int channel, int drive, int read)
526 1.1 tsubai {
527 1.4 tsubai struct wdc_obio_softc *sc = v;
528 1.4 tsubai
529 1.4 tsubai dbdma_stop(sc->sc_dmareg);
530 1.1 tsubai return 0;
531 1.1 tsubai }
532