1b7382e09SNathan Whitehorn /*- 2b7382e09SNathan Whitehorn * Copyright 2008 by Nathan Whitehorn. All rights reserved. 3b7382e09SNathan Whitehorn * 4b7382e09SNathan Whitehorn * Redistribution and use in source and binary forms, with or without 5b7382e09SNathan Whitehorn * modification, are permitted provided that the following conditions 6b7382e09SNathan Whitehorn * are met: 7b7382e09SNathan Whitehorn * 1. Redistributions of source code must retain the above copyright 8b7382e09SNathan Whitehorn * notice, this list of conditions and the following disclaimer. 9b7382e09SNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright 10b7382e09SNathan Whitehorn * notice, this list of conditions and the following disclaimer in the 11b7382e09SNathan Whitehorn * documentation and/or other materials provided with the distribution. 12b7382e09SNathan Whitehorn * 3. The name of the author may not be used to endorse or promote products 13b7382e09SNathan Whitehorn * derived from this software without specific prior written permission. 14b7382e09SNathan Whitehorn * 15b7382e09SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16b7382e09SNathan Whitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17b7382e09SNathan Whitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18b7382e09SNathan Whitehorn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19b7382e09SNathan Whitehorn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20b7382e09SNathan Whitehorn * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21b7382e09SNathan Whitehorn * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22b7382e09SNathan Whitehorn * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23b7382e09SNathan Whitehorn * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24b7382e09SNathan Whitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25b7382e09SNathan Whitehorn * SUCH DAMAGE. 26b7382e09SNathan Whitehorn * 27b7382e09SNathan Whitehorn * $FreeBSD$ 28b7382e09SNathan Whitehorn */ 29b7382e09SNathan Whitehorn 30b7382e09SNathan Whitehorn /* 31b7382e09SNathan Whitehorn * Common routines for the DMA engine on both the Apple Kauai and MacIO 32b7382e09SNathan Whitehorn * ATA controllers. 33b7382e09SNathan Whitehorn */ 34b7382e09SNathan Whitehorn 35b7382e09SNathan Whitehorn #include "opt_ata.h" 36b7382e09SNathan Whitehorn #include <sys/param.h> 37b7382e09SNathan Whitehorn #include <sys/systm.h> 38b7382e09SNathan Whitehorn #include <sys/kernel.h> 39b7382e09SNathan Whitehorn #include <sys/module.h> 40b7382e09SNathan Whitehorn #include <sys/bus.h> 41b7382e09SNathan Whitehorn #include <sys/malloc.h> 42b7382e09SNathan Whitehorn #include <sys/sema.h> 43b7382e09SNathan Whitehorn #include <sys/taskqueue.h> 44b7382e09SNathan Whitehorn #include <vm/uma.h> 45b7382e09SNathan Whitehorn #include <machine/stdarg.h> 46b7382e09SNathan Whitehorn #include <machine/resource.h> 47b7382e09SNathan Whitehorn #include <machine/bus.h> 48b7382e09SNathan Whitehorn #include <sys/rman.h> 49b7382e09SNathan Whitehorn #include <sys/ata.h> 50b7382e09SNathan Whitehorn #include <dev/ata/ata-all.h> 51b7382e09SNathan Whitehorn #include <dev/ata/ata-pci.h> 52b7382e09SNathan Whitehorn #include <ata_if.h> 53b7382e09SNathan Whitehorn 54b7382e09SNathan Whitehorn #include "ata_dbdma.h" 55b7382e09SNathan Whitehorn 56b7382e09SNathan Whitehorn struct ata_dbdma_dmaload_args { 57b7382e09SNathan Whitehorn struct ata_dbdma_channel *sc; 58b7382e09SNathan Whitehorn 59b7382e09SNathan Whitehorn int write; 60b7382e09SNathan Whitehorn int nsegs; 61b7382e09SNathan Whitehorn }; 62b7382e09SNathan Whitehorn 63b7382e09SNathan Whitehorn static void 64b7382e09SNathan Whitehorn ata_dbdma_setprd(void *xarg, bus_dma_segment_t *segs, int nsegs, int error) 65b7382e09SNathan Whitehorn { 66b7382e09SNathan Whitehorn struct ata_dbdma_dmaload_args *arg = xarg; 67b7382e09SNathan Whitehorn struct ata_dbdma_channel *sc = arg->sc; 68b7382e09SNathan Whitehorn int branch_type, command; 69b7382e09SNathan Whitehorn int prev_stop; 70b7382e09SNathan Whitehorn int i; 71b7382e09SNathan Whitehorn 72b7382e09SNathan Whitehorn mtx_lock(&sc->dbdma_mtx); 73b7382e09SNathan Whitehorn 74b7382e09SNathan Whitehorn prev_stop = sc->next_dma_slot-1; 75b7382e09SNathan Whitehorn if (prev_stop < 0) 76b7382e09SNathan Whitehorn prev_stop = 0xff; 77b7382e09SNathan Whitehorn 78b7382e09SNathan Whitehorn for (i = 0; i < nsegs; i++) { 79b7382e09SNathan Whitehorn /* Loop back to the beginning if this is our last slot */ 80b7382e09SNathan Whitehorn if (sc->next_dma_slot == 0xff) 81b7382e09SNathan Whitehorn branch_type = DBDMA_ALWAYS; 82b7382e09SNathan Whitehorn else 83b7382e09SNathan Whitehorn branch_type = DBDMA_NEVER; 84b7382e09SNathan Whitehorn 85b7382e09SNathan Whitehorn if (arg->write) { 86b7382e09SNathan Whitehorn command = (i + 1 < nsegs) ? DBDMA_OUTPUT_MORE : 87b7382e09SNathan Whitehorn DBDMA_OUTPUT_LAST; 88b7382e09SNathan Whitehorn } else { 89b7382e09SNathan Whitehorn command = (i + 1 < nsegs) ? DBDMA_INPUT_MORE : 90b7382e09SNathan Whitehorn DBDMA_INPUT_LAST; 91b7382e09SNathan Whitehorn } 92b7382e09SNathan Whitehorn 93b7382e09SNathan Whitehorn dbdma_insert_command(sc->dbdma, sc->next_dma_slot++, 94b7382e09SNathan Whitehorn command, 0, segs[i].ds_addr, segs[i].ds_len, 95b7382e09SNathan Whitehorn DBDMA_NEVER, branch_type, DBDMA_NEVER, 0); 96b7382e09SNathan Whitehorn 97b7382e09SNathan Whitehorn if (branch_type == DBDMA_ALWAYS) 98b7382e09SNathan Whitehorn sc->next_dma_slot = 0; 99b7382e09SNathan Whitehorn } 100b7382e09SNathan Whitehorn 101b7382e09SNathan Whitehorn /* We have a corner case where the STOP command is the last slot, 102b7382e09SNathan Whitehorn * but you can't branch in STOP commands. So add a NOP branch here 103b7382e09SNathan Whitehorn * and the STOP in slot 0. */ 104b7382e09SNathan Whitehorn 105b7382e09SNathan Whitehorn if (sc->next_dma_slot == 0xff) { 106b7382e09SNathan Whitehorn dbdma_insert_branch(sc->dbdma, sc->next_dma_slot, 0); 107b7382e09SNathan Whitehorn sc->next_dma_slot = 0; 108b7382e09SNathan Whitehorn } 109b7382e09SNathan Whitehorn 110b7382e09SNathan Whitehorn #if 0 111b7382e09SNathan Whitehorn dbdma_insert_command(sc->dbdma, sc->next_dma_slot++, 112b7382e09SNathan Whitehorn DBDMA_NOP, 0, 0, 0, DBDMA_ALWAYS, DBDMA_NEVER, DBDMA_NEVER, 0); 113b7382e09SNathan Whitehorn #endif 114b7382e09SNathan Whitehorn dbdma_insert_stop(sc->dbdma, sc->next_dma_slot++); 115b7382e09SNathan Whitehorn dbdma_insert_nop(sc->dbdma, prev_stop); 116b7382e09SNathan Whitehorn 117b7382e09SNathan Whitehorn dbdma_sync_commands(sc->dbdma, BUS_DMASYNC_PREWRITE); 118b7382e09SNathan Whitehorn 119b7382e09SNathan Whitehorn mtx_unlock(&sc->dbdma_mtx); 120b7382e09SNathan Whitehorn 121b7382e09SNathan Whitehorn arg->nsegs = nsegs; 122b7382e09SNathan Whitehorn } 123b7382e09SNathan Whitehorn 124b7382e09SNathan Whitehorn static int 125b7382e09SNathan Whitehorn ata_dbdma_status(device_t dev) 126b7382e09SNathan Whitehorn { 127b7382e09SNathan Whitehorn struct ata_dbdma_channel *sc = device_get_softc(dev); 128b7382e09SNathan Whitehorn struct ata_channel *ch = device_get_softc(dev); 129b7382e09SNathan Whitehorn 130b7382e09SNathan Whitehorn if (sc->sc_ch.dma.flags & ATA_DMA_ACTIVE) { 131b7382e09SNathan Whitehorn return (!(dbdma_get_chan_status(sc->dbdma) & 132b7382e09SNathan Whitehorn DBDMA_STATUS_ACTIVE)); 133b7382e09SNathan Whitehorn } 134b7382e09SNathan Whitehorn 135b7382e09SNathan Whitehorn if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) { 136b7382e09SNathan Whitehorn DELAY(100); 137b7382e09SNathan Whitehorn if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) 138b7382e09SNathan Whitehorn return 0; 139b7382e09SNathan Whitehorn } 140b7382e09SNathan Whitehorn return 1; 141b7382e09SNathan Whitehorn } 142b7382e09SNathan Whitehorn 143b7382e09SNathan Whitehorn static int 144b7382e09SNathan Whitehorn ata_dbdma_start(struct ata_request *request) 145b7382e09SNathan Whitehorn { 146b7382e09SNathan Whitehorn struct ata_dbdma_channel *sc = device_get_softc(request->parent); 147b7382e09SNathan Whitehorn 148b7382e09SNathan Whitehorn sc->sc_ch.dma.flags |= ATA_DMA_ACTIVE; 149b7382e09SNathan Whitehorn dbdma_wake(sc->dbdma); 150b7382e09SNathan Whitehorn return 0; 151b7382e09SNathan Whitehorn } 152b7382e09SNathan Whitehorn 153b7382e09SNathan Whitehorn static void 154b7382e09SNathan Whitehorn ata_dbdma_reset(device_t dev) 155b7382e09SNathan Whitehorn { 156b7382e09SNathan Whitehorn struct ata_dbdma_channel *sc = device_get_softc(dev); 157b7382e09SNathan Whitehorn 158b7382e09SNathan Whitehorn mtx_lock(&sc->dbdma_mtx); 159b7382e09SNathan Whitehorn 160b7382e09SNathan Whitehorn dbdma_stop(sc->dbdma); 161b7382e09SNathan Whitehorn dbdma_insert_stop(sc->dbdma, 0); 162b7382e09SNathan Whitehorn sc->next_dma_slot=1; 163b7382e09SNathan Whitehorn dbdma_set_current_cmd(sc->dbdma, 0); 164b7382e09SNathan Whitehorn 165b7382e09SNathan Whitehorn sc->sc_ch.dma.flags &= ~ATA_DMA_ACTIVE; 166b7382e09SNathan Whitehorn 167b7382e09SNathan Whitehorn mtx_unlock(&sc->dbdma_mtx); 168b7382e09SNathan Whitehorn } 169b7382e09SNathan Whitehorn 170b7382e09SNathan Whitehorn static int 171b7382e09SNathan Whitehorn ata_dbdma_stop(struct ata_request *request) 172b7382e09SNathan Whitehorn { 173b7382e09SNathan Whitehorn struct ata_dbdma_channel *sc = device_get_softc(request->parent); 174b7382e09SNathan Whitehorn 175b7382e09SNathan Whitehorn uint16_t status; 176b7382e09SNathan Whitehorn 177b7382e09SNathan Whitehorn status = dbdma_get_chan_status(sc->dbdma); 178b7382e09SNathan Whitehorn 179b7382e09SNathan Whitehorn dbdma_pause(sc->dbdma); 180b7382e09SNathan Whitehorn sc->sc_ch.dma.flags &= ~ATA_DMA_ACTIVE; 181b7382e09SNathan Whitehorn 182b7382e09SNathan Whitehorn if (status & DBDMA_STATUS_DEAD) { 183b7382e09SNathan Whitehorn device_printf(request->parent,"DBDMA dead, resetting " 184b7382e09SNathan Whitehorn "channel...\n"); 185b7382e09SNathan Whitehorn ata_dbdma_reset(request->parent); 186b7382e09SNathan Whitehorn return ATA_S_ERROR; 187b7382e09SNathan Whitehorn } 188b7382e09SNathan Whitehorn 189b7382e09SNathan Whitehorn if (!(status & DBDMA_STATUS_RUN)) { 190b7382e09SNathan Whitehorn device_printf(request->parent,"DBDMA confused, stop called " 191b7382e09SNathan Whitehorn "when channel is not running!\n"); 192b7382e09SNathan Whitehorn return ATA_S_ERROR; 193b7382e09SNathan Whitehorn } 194b7382e09SNathan Whitehorn 195b7382e09SNathan Whitehorn if (status & DBDMA_STATUS_ACTIVE) { 196b7382e09SNathan Whitehorn device_printf(request->parent,"DBDMA channel stopped " 197b7382e09SNathan Whitehorn "prematurely\n"); 198b7382e09SNathan Whitehorn return ATA_S_ERROR; 199b7382e09SNathan Whitehorn } 200b7382e09SNathan Whitehorn return 0; 201b7382e09SNathan Whitehorn } 202b7382e09SNathan Whitehorn 203b7382e09SNathan Whitehorn static int 204b7382e09SNathan Whitehorn ata_dbdma_load(struct ata_request *request, void *addr, int *entries) 205b7382e09SNathan Whitehorn { 206b7382e09SNathan Whitehorn struct ata_channel *ch = device_get_softc(request->parent); 207b7382e09SNathan Whitehorn struct ata_dbdma_dmaload_args args; 208b7382e09SNathan Whitehorn 209b7382e09SNathan Whitehorn int error; 210b7382e09SNathan Whitehorn 211b7382e09SNathan Whitehorn args.sc = device_get_softc(request->parent); 212b7382e09SNathan Whitehorn args.write = !(request->flags & ATA_R_READ); 213b7382e09SNathan Whitehorn 214b7382e09SNathan Whitehorn if (!request->bytecount) { 215b7382e09SNathan Whitehorn device_printf(request->dev, 216b7382e09SNathan Whitehorn "FAILURE - zero length DMA transfer attempted\n"); 217b7382e09SNathan Whitehorn return EIO; 218b7382e09SNathan Whitehorn } 219b7382e09SNathan Whitehorn if (((uintptr_t)(request->data) & (ch->dma.alignment - 1)) || 220b7382e09SNathan Whitehorn (request->bytecount & (ch->dma.alignment - 1))) { 221b7382e09SNathan Whitehorn device_printf(request->dev, 222b7382e09SNathan Whitehorn "FAILURE - non aligned DMA transfer attempted\n"); 223b7382e09SNathan Whitehorn return EIO; 224b7382e09SNathan Whitehorn } 225b7382e09SNathan Whitehorn if (request->bytecount > ch->dma.max_iosize) { 226b7382e09SNathan Whitehorn device_printf(request->dev, 227b7382e09SNathan Whitehorn "FAILURE - oversized DMA transfer attempt %d > %d\n", 228b7382e09SNathan Whitehorn request->bytecount, ch->dma.max_iosize); 229b7382e09SNathan Whitehorn return EIO; 230b7382e09SNathan Whitehorn } 231b7382e09SNathan Whitehorn 232066f913aSAlexander Motin request->dma = &ch->dma.slot[0]; 233b7382e09SNathan Whitehorn 234b7382e09SNathan Whitehorn if ((error = bus_dmamap_load(request->dma->data_tag, 235b7382e09SNathan Whitehorn request->dma->data_map, request->data, request->bytecount, 236b7382e09SNathan Whitehorn &ata_dbdma_setprd, &args, BUS_DMA_NOWAIT))) { 237b7382e09SNathan Whitehorn device_printf(request->dev, "FAILURE - load data\n"); 238b7382e09SNathan Whitehorn goto error; 239b7382e09SNathan Whitehorn } 240b7382e09SNathan Whitehorn 241b7382e09SNathan Whitehorn if (entries) 242b7382e09SNathan Whitehorn *entries = args.nsegs; 243b7382e09SNathan Whitehorn 244b7382e09SNathan Whitehorn bus_dmamap_sync(request->dma->sg_tag, request->dma->sg_map, 245b7382e09SNathan Whitehorn BUS_DMASYNC_PREWRITE); 246b7382e09SNathan Whitehorn bus_dmamap_sync(request->dma->data_tag, request->dma->data_map, 247b7382e09SNathan Whitehorn (request->flags & ATA_R_READ) ? 248b7382e09SNathan Whitehorn BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 249b7382e09SNathan Whitehorn 250b7382e09SNathan Whitehorn return 0; 251b7382e09SNathan Whitehorn 252b7382e09SNathan Whitehorn error: 253b7382e09SNathan Whitehorn ch->dma.unload(request); 254b7382e09SNathan Whitehorn return EIO; 255b7382e09SNathan Whitehorn } 256b7382e09SNathan Whitehorn 257b7382e09SNathan Whitehorn void 258b7382e09SNathan Whitehorn ata_dbdma_dmainit(device_t dev) 259b7382e09SNathan Whitehorn { 260b7382e09SNathan Whitehorn struct ata_dbdma_channel *sc = device_get_softc(dev); 261b7382e09SNathan Whitehorn int error; 262b7382e09SNathan Whitehorn 263b7382e09SNathan Whitehorn error = dbdma_allocate_channel(sc->dbdma_regs, sc->dbdma_offset, 264b7382e09SNathan Whitehorn bus_get_dma_tag(dev), 256, &sc->dbdma); 265b7382e09SNathan Whitehorn 266b7382e09SNathan Whitehorn dbdma_set_wait_selector(sc->dbdma,1 << 7, 1 << 7); 267b7382e09SNathan Whitehorn 268b7382e09SNathan Whitehorn dbdma_insert_stop(sc->dbdma,0); 269b7382e09SNathan Whitehorn sc->next_dma_slot=1; 270b7382e09SNathan Whitehorn 271b7382e09SNathan Whitehorn ata_dmainit(dev); 272b7382e09SNathan Whitehorn sc->sc_ch.dma.start = ata_dbdma_start; 273b7382e09SNathan Whitehorn sc->sc_ch.dma.stop = ata_dbdma_stop; 274b7382e09SNathan Whitehorn sc->sc_ch.dma.load = ata_dbdma_load; 275b7382e09SNathan Whitehorn sc->sc_ch.dma.reset = ata_dbdma_reset; 276b7382e09SNathan Whitehorn 277360bf678SNathan Whitehorn /* 278360bf678SNathan Whitehorn * DBDMA's field for transfer size is 16 bits. This will overflow 279360bf678SNathan Whitehorn * if we try to do a 64K transfer, so stop short of 64K. 280360bf678SNathan Whitehorn */ 281360bf678SNathan Whitehorn sc->sc_ch.dma.segsize = 126 * DEV_BSIZE; 282360bf678SNathan Whitehorn 283b7382e09SNathan Whitehorn sc->sc_ch.hw.status = ata_dbdma_status; 284b7382e09SNathan Whitehorn 285b7382e09SNathan Whitehorn mtx_init(&sc->dbdma_mtx, "ATA DBDMA", NULL, MTX_DEF); 286b7382e09SNathan Whitehorn } 287b7382e09SNathan Whitehorn 288