wdc_obio.c revision 1.38 1 /* $NetBSD: wdc_obio.c,v 1.38 2004/08/13 03:12:59 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2003 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/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: wdc_obio.c,v 1.38 2004/08/13 03:12:59 thorpej Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/malloc.h>
46
47 #include <uvm/uvm_extern.h>
48
49 #include <machine/bus.h>
50 #include <machine/autoconf.h>
51
52 #include <dev/ata/atareg.h>
53 #include <dev/ata/atavar.h>
54 #include <dev/ic/wdcvar.h>
55
56 #include <dev/ofw/openfirm.h>
57
58 #include <macppc/dev/dbdma.h>
59
60 #define WDC_REG_NPORTS 8
61 #define WDC_AUXREG_OFFSET 0x16
62 #define WDC_DEFAULT_PIO_IRQ 13 /* XXX */
63 #define WDC_DEFAULT_DMA_IRQ 2 /* XXX */
64
65 #define WDC_OPTIONS_DMA 0x01
66
67 /*
68 * XXX This code currently doesn't even try to allow 32-bit data port use.
69 */
70
71 struct wdc_obio_softc {
72 struct wdc_softc sc_wdcdev;
73 struct wdc_channel *wdc_chanptr;
74 struct wdc_channel wdc_channel;
75 struct ata_queue wdc_chqueue;
76 dbdma_regmap_t *sc_dmareg;
77 dbdma_command_t *sc_dmacmd;
78 u_int sc_dmaconf[2]; /* per target value of CONFIG_REG */
79 void *sc_ih;
80 };
81
82 int wdc_obio_probe __P((struct device *, struct cfdata *, void *));
83 void wdc_obio_attach __P((struct device *, struct device *, void *));
84 int wdc_obio_detach __P((struct device *, int));
85 int wdc_obio_dma_init __P((void *, int, int, void *, size_t, int));
86 void wdc_obio_dma_start __P((void *, int, int));
87 int wdc_obio_dma_finish __P((void *, int, int, int));
88
89 static void wdc_obio_select __P((struct wdc_channel *, int));
90 static void adjust_timing __P((struct wdc_channel *));
91 static void ata4_adjust_timing __P((struct wdc_channel *));
92
93 CFATTACH_DECL(wdc_obio, sizeof(struct wdc_obio_softc),
94 wdc_obio_probe, wdc_obio_attach, wdc_obio_detach, wdcactivate);
95
96 int
97 wdc_obio_probe(parent, match, aux)
98 struct device *parent;
99 struct cfdata *match;
100 void *aux;
101 {
102 struct confargs *ca = aux;
103 char compat[32];
104
105 /* XXX should not use name */
106 if (strcmp(ca->ca_name, "ATA") == 0 ||
107 strcmp(ca->ca_name, "ata") == 0 ||
108 strcmp(ca->ca_name, "ata0") == 0 ||
109 strcmp(ca->ca_name, "ide") == 0)
110 return 1;
111
112 memset(compat, 0, sizeof(compat));
113 OF_getprop(ca->ca_node, "compatible", compat, sizeof(compat));
114 if (strcmp(compat, "heathrow-ata") == 0 ||
115 strcmp(compat, "keylargo-ata") == 0)
116 return 1;
117
118 return 0;
119 }
120
121 void
122 wdc_obio_attach(parent, self, aux)
123 struct device *parent, *self;
124 void *aux;
125 {
126 struct wdc_obio_softc *sc = (void *)self;
127 struct confargs *ca = aux;
128 struct wdc_channel *chp = &sc->wdc_channel;
129 int intr, i;
130 int use_dma = 0;
131 char path[80];
132
133 if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_OPTIONS_DMA) {
134 if (ca->ca_nreg >= 16 || ca->ca_nintr == -1)
135 use_dma = 1; /* XXX Don't work yet. */
136 }
137
138 if (ca->ca_nintr >= 4 && ca->ca_nreg >= 8) {
139 intr = ca->ca_intr[0];
140 printf(" irq %d", intr);
141 } else if (ca->ca_nintr == -1) {
142 intr = WDC_DEFAULT_PIO_IRQ;
143 printf(" irq property not found; using %d", intr);
144 } else {
145 printf(": couldn't get irq property\n");
146 return;
147 }
148
149 if (use_dma)
150 printf(": DMA transfer");
151
152 printf("\n");
153
154 chp->cmd_iot = chp->ctl_iot =
155 macppc_make_bus_space_tag(ca->ca_baseaddr + ca->ca_reg[0], 4);
156
157 if (bus_space_map(chp->cmd_iot, 0, WDC_REG_NPORTS, 0,
158 &chp->cmd_baseioh) ||
159 bus_space_subregion(chp->cmd_iot, chp->cmd_baseioh,
160 WDC_AUXREG_OFFSET, 1, &chp->ctl_ioh)) {
161 printf("%s: couldn't map registers\n",
162 sc->sc_wdcdev.sc_dev.dv_xname);
163 return;
164 }
165 for (i = 0; i < WDC_NREG; i++) {
166 if (bus_space_subregion(chp->cmd_iot, chp->cmd_baseioh, i,
167 i == 0 ? 4 : 1, &chp->cmd_iohs[i]) != 0) {
168 bus_space_unmap(chp->cmd_iot, chp->cmd_baseioh,
169 WDC_REG_NPORTS);
170 printf("%s: couldn't subregion registers\n",
171 sc->sc_wdcdev.sc_dev.dv_xname);
172 return;
173 }
174 }
175 wdc_init_shadow_regs(chp);
176 #if 0
177 chp->data32iot = chp->cmd_iot;
178 chp->data32ioh = chp->cmd_ioh;
179 #endif
180
181 sc->sc_ih = intr_establish(intr, IST_LEVEL, IPL_BIO, wdcintr, chp);
182
183 if (use_dma) {
184 sc->sc_dmacmd = dbdma_alloc(sizeof(dbdma_command_t) * 20);
185 sc->sc_dmareg = mapiodev(ca->ca_baseaddr + ca->ca_reg[2],
186 ca->ca_reg[3]);
187 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
188 sc->sc_wdcdev.DMA_cap = 2;
189 if (strcmp(ca->ca_name, "ata-4") == 0) {
190 sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
191 sc->sc_wdcdev.UDMA_cap = 4;
192 sc->sc_wdcdev.set_modes = ata4_adjust_timing;
193 } else {
194 sc->sc_wdcdev.set_modes = adjust_timing;
195 }
196 #ifdef notyet
197 /* Minimum cycle time is 150ns (DMA MODE 1) on ohare. */
198 if (ohare) {
199 sc->sc_wdcdev.PIO_cap = 3;
200 sc->sc_wdcdev.DMA_cap = 1;
201 }
202 #endif
203 } else {
204 /* all non-DMA controllers can use adjust_timing */
205 sc->sc_wdcdev.set_modes = adjust_timing;
206 }
207
208 sc->sc_wdcdev.PIO_cap = 4;
209 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
210 sc->wdc_chanptr = chp;
211 sc->sc_wdcdev.channels = &sc->wdc_chanptr;
212 sc->sc_wdcdev.nchannels = 1;
213 sc->sc_wdcdev.dma_arg = sc;
214 sc->sc_wdcdev.dma_init = wdc_obio_dma_init;
215 sc->sc_wdcdev.dma_start = wdc_obio_dma_start;
216 sc->sc_wdcdev.dma_finish = wdc_obio_dma_finish;
217 chp->ch_channel = 0;
218 chp->ch_wdc = &sc->sc_wdcdev;
219 chp->ch_queue = &sc->wdc_chqueue;
220
221 #define OHARE_FEATURE_REG 0xf3000038
222
223 /* XXX Enable wdc1 by feature reg. */
224 memset(path, 0, sizeof(path));
225 OF_package_to_path(ca->ca_node, path, sizeof(path));
226 if (strcmp(path, "/bandit@F2000000/ohare@10/ata@21000") == 0) {
227 u_int x;
228
229 x = in32rb(OHARE_FEATURE_REG);
230 x |= 8;
231 out32rb(OHARE_FEATURE_REG, x);
232 }
233
234 wdcattach(chp);
235 }
236
237 /* Multiword DMA transfer timings */
238 struct ide_timings {
239 int cycle; /* minimum cycle time [ns] */
240 int active; /* minimum command active time [ns] */
241 };
242 static struct ide_timings pio_timing[5] = {
243 { 600, 180 }, /* Mode 0 */
244 { 390, 150 }, /* 1 */
245 { 240, 105 }, /* 2 */
246 { 180, 90 }, /* 3 */
247 { 120, 75 } /* 4 */
248 };
249 static struct ide_timings dma_timing[3] = {
250 { 480, 240 }, /* Mode 0 */
251 { 165, 90 }, /* Mode 1 */
252 { 120, 75 } /* Mode 2 */
253 };
254
255 static struct ide_timings udma_timing[5] = {
256 {120, 180}, /* Mode 0 */
257 { 90, 150}, /* Mode 1 */
258 { 60, 120}, /* Mode 2 */
259 { 45, 90}, /* Mode 3 */
260 { 30, 90} /* Mode 4 */
261 };
262
263 #define TIME_TO_TICK(time) howmany((time), 30)
264 #define PIO_REC_OFFSET 4
265 #define PIO_REC_MIN 1
266 #define PIO_ACT_MIN 1
267 #define DMA_REC_OFFSET 1
268 #define DMA_REC_MIN 1
269 #define DMA_ACT_MIN 1
270
271 #define ATA4_TIME_TO_TICK(time) howmany((time), 15) /* 15 ns clock */
272
273 #define CONFIG_REG (0x200 >> 4) /* IDE access timing register */
274
275 void
276 wdc_obio_select(chp, drive)
277 struct wdc_channel *chp;
278 int drive;
279 {
280 struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->ch_wdc;
281 bus_space_write_4(chp->cmd_iot, chp->cmd_baseioh,
282 CONFIG_REG, sc->sc_dmaconf[drive]);
283 }
284
285 void
286 adjust_timing(chp)
287 struct wdc_channel *chp;
288 {
289 struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->ch_wdc;
290 int drive;
291 int min_cycle = 0, min_active = 0;
292 int cycle_tick = 0, act_tick = 0, inact_tick = 0, half_tick;
293
294 for (drive = 0; drive < 2; drive++) {
295 u_int conf = 0;
296 struct ata_drive_datas *drvp;
297
298 drvp = &chp->ch_drive[drive];
299 /* set up pio mode timings */
300 if (drvp->drive_flags & DRIVE) {
301 int piomode = drvp->PIO_mode;
302 min_cycle = pio_timing[piomode].cycle;
303 min_active = pio_timing[piomode].active;
304
305 cycle_tick = TIME_TO_TICK(min_cycle);
306 act_tick = TIME_TO_TICK(min_active);
307 if (act_tick < PIO_ACT_MIN)
308 act_tick = PIO_ACT_MIN;
309 inact_tick = cycle_tick - act_tick - PIO_REC_OFFSET;
310 if (inact_tick < PIO_REC_MIN)
311 inact_tick = PIO_REC_MIN;
312 /* mask: 0x000007ff */
313 conf |= (inact_tick << 5) | act_tick;
314 }
315 /* Set up DMA mode timings */
316 if (drvp->drive_flags & DRIVE_DMA) {
317 int dmamode = drvp->DMA_mode;
318 min_cycle = dma_timing[dmamode].cycle;
319 min_active = dma_timing[dmamode].active;
320 cycle_tick = TIME_TO_TICK(min_cycle);
321 act_tick = TIME_TO_TICK(min_active);
322 inact_tick = cycle_tick - act_tick - DMA_REC_OFFSET;
323 if (inact_tick < DMA_REC_MIN)
324 inact_tick = DMA_REC_MIN;
325 half_tick = 0; /* XXX */
326 /* mask: 0xfffff800 */
327 conf |=
328 (half_tick << 21) |
329 (inact_tick << 16) | (act_tick << 11);
330 }
331 #ifdef DEBUG
332 if (conf) {
333 printf("conf[%d] = 0x%x, cyc = %d (%d ns), act = %d (%d ns), inact = %d\n",
334 drive, conf, cycle_tick, min_cycle, act_tick, min_active, inact_tick);
335 }
336 #endif
337 sc->sc_dmaconf[drive] = conf;
338 }
339 sc->sc_wdcdev.select = 0;
340 if (sc->sc_dmaconf[0]) {
341 wdc_obio_select(chp,0);
342 if (sc->sc_dmaconf[1] &&
343 (sc->sc_dmaconf[0] != sc->sc_dmaconf[1])) {
344 sc->sc_wdcdev.select = wdc_obio_select;
345 }
346 } else if (sc->sc_dmaconf[1]) {
347 wdc_obio_select(chp,1);
348 }
349 }
350
351 void
352 ata4_adjust_timing(chp)
353 struct wdc_channel *chp;
354 {
355 struct wdc_obio_softc *sc = (struct wdc_obio_softc *)chp->ch_wdc;
356 int drive;
357 int min_cycle = 0, min_active = 0;
358 int cycle_tick = 0, act_tick = 0, inact_tick = 0;
359
360 for (drive = 0; drive < 2; drive++) {
361 u_int conf = 0;
362 struct ata_drive_datas *drvp;
363
364 drvp = &chp->ch_drive[drive];
365 /* set up pio mode timings */
366
367 if (drvp->drive_flags & DRIVE) {
368 int piomode = drvp->PIO_mode;
369 min_cycle = pio_timing[piomode].cycle;
370 min_active = pio_timing[piomode].active;
371
372 cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
373 act_tick = ATA4_TIME_TO_TICK(min_active);
374 inact_tick = cycle_tick - act_tick;
375 /* mask: 0x000003ff */
376 conf |= (inact_tick << 5) | act_tick;
377 }
378 /* set up dma mode timings */
379 if (drvp->drive_flags & DRIVE_DMA) {
380 int dmamode = drvp->DMA_mode;
381 min_cycle = dma_timing[dmamode].cycle;
382 min_active = dma_timing[dmamode].active;
383 cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
384 act_tick = ATA4_TIME_TO_TICK(min_active);
385 inact_tick = cycle_tick - act_tick;
386 /* mask: 0x001ffc00 */
387 conf |= (act_tick << 10) | (inact_tick << 15);
388 }
389 /* set up udma mode timings */
390 if (drvp->drive_flags & DRIVE_UDMA) {
391 int udmamode = drvp->UDMA_mode;
392 min_cycle = udma_timing[udmamode].cycle;
393 min_active = udma_timing[udmamode].active;
394 act_tick = ATA4_TIME_TO_TICK(min_active);
395 cycle_tick = ATA4_TIME_TO_TICK(min_cycle);
396 /* mask: 0x1ff00000 */
397 conf |= (cycle_tick << 21) | (act_tick << 25) | 0x100000;
398 }
399 #ifdef DEBUG
400 if (conf) {
401 printf("ata4 conf[%d] = 0x%x, cyc = %d (%d ns), act = %d (%d ns), inact = %d\n",
402 drive, conf, cycle_tick, min_cycle, act_tick, min_active, inact_tick);
403 }
404 #endif
405 sc->sc_dmaconf[drive] = conf;
406 }
407 sc->sc_wdcdev.select = 0;
408 if (sc->sc_dmaconf[0]) {
409 wdc_obio_select(chp,0);
410 if (sc->sc_dmaconf[1] &&
411 (sc->sc_dmaconf[0] != sc->sc_dmaconf[1])) {
412 sc->sc_wdcdev.select = wdc_obio_select;
413 }
414 } else if (sc->sc_dmaconf[1]) {
415 wdc_obio_select(chp,1);
416 }
417 }
418
419 int
420 wdc_obio_detach(self, flags)
421 struct device *self;
422 int flags;
423 {
424 struct wdc_obio_softc *sc = (void *)self;
425 int error;
426
427 if ((error = wdcdetach(self, flags)) != 0)
428 return error;
429
430 intr_disestablish(sc->sc_ih);
431
432 /* Unmap our i/o space. */
433 bus_space_unmap(chp->cmd_iot, chp->cmd_ioh, WDC_REG_NPORTS);
434
435 /* Unmap DMA registers. */
436 /* XXX unmapiodev(sc->sc_dmareg); */
437 /* XXX free(sc->sc_dmacmd); */
438
439 return 0;
440 }
441
442 int
443 wdc_obio_dma_init(v, channel, drive, databuf, datalen, flags)
444 void *v;
445 void *databuf;
446 size_t datalen;
447 int flags;
448 {
449 struct wdc_obio_softc *sc = v;
450 vaddr_t va = (vaddr_t)databuf;
451 dbdma_command_t *cmdp;
452 u_int cmd, offset;
453 int read = flags & WDC_DMA_READ;
454
455 cmdp = sc->sc_dmacmd;
456 cmd = read ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
457
458 offset = va & PGOFSET;
459
460 /* if va is not page-aligned, setup the first page */
461 if (offset != 0) {
462 int rest = PAGE_SIZE - offset; /* the rest of the page */
463
464 if (datalen > rest) { /* if continues to next page */
465 DBDMA_BUILD(cmdp, cmd, 0, rest, vtophys(va),
466 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER,
467 DBDMA_BRANCH_NEVER);
468 datalen -= rest;
469 va += rest;
470 cmdp++;
471 }
472 }
473
474 /* now va is page-aligned */
475 while (datalen > PAGE_SIZE) {
476 DBDMA_BUILD(cmdp, cmd, 0, PAGE_SIZE, vtophys(va),
477 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
478 datalen -= PAGE_SIZE;
479 va += PAGE_SIZE;
480 cmdp++;
481 }
482
483 /* the last page (datalen <= PAGE_SIZE here) */
484 cmd = read ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
485 DBDMA_BUILD(cmdp, cmd, 0, datalen, vtophys(va),
486 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
487 cmdp++;
488
489 DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
490 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
491
492 return 0;
493 }
494
495 void
496 wdc_obio_dma_start(v, channel, drive)
497 void *v;
498 int channel, drive;
499 {
500 struct wdc_obio_softc *sc = v;
501
502 dbdma_start(sc->sc_dmareg, sc->sc_dmacmd);
503 }
504
505 int
506 wdc_obio_dma_finish(v, channel, drive, read)
507 void *v;
508 int channel, drive;
509 int read;
510 {
511 struct wdc_obio_softc *sc = v;
512
513 dbdma_stop(sc->sc_dmareg);
514 return 0;
515 }
516