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