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