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