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