1 /* $NetBSD: dma.c,v 1.15 2002/09/27 15:35:52 provos Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Leo Weppelman. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Leo Weppelman. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * This file contains special code dealing with the DMA interface 35 * on the Atari ST. 36 * 37 * The DMA circuitry requires some special treatment for the peripheral 38 * devices which make use of the ST's DMA feature (the hard disk and the 39 * floppy drive). 40 * All devices using DMA need mutually exclusive access and can follow some 41 * standard pattern which will be provided in this file. 42 * 43 * The file contains the following entry points: 44 * 45 * st_dmagrab: ensure exclusive access to the DMA circuitry 46 * st_dmafree: free exclusive access to the DMA circuitry 47 * st_dmawanted: somebody is queued waiting for DMA-access 48 * dmaint: DMA interrupt routine, switches to the current driver 49 * st_dmaaddr_set: specify 24 bit RAM address 50 * st_dmaaddr_get: get address of last DMA-op 51 * st_dmacomm: program DMA, flush FIFO first 52 */ 53 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/kernel.h> 57 #include <sys/proc.h> 58 #include <sys/queue.h> 59 60 #include <machine/cpu.h> 61 #include <machine/iomap.h> 62 #include <machine/dma.h> 63 64 #include <atari/atari/intr.h> 65 66 #define NDMA_DEV 10 /* Max 2 floppy's, 8 hard-disks */ 67 typedef struct dma_entry { 68 TAILQ_ENTRY(dma_entry) entries; /* List pointers */ 69 void (*call_func)(void *); /* Call when lock granted */ 70 void (*int_func)(void *); /* Call on DMA interrupt */ 71 void *softc; /* Arg. to int_func */ 72 int *lock_stat; /* status of DMA lock */ 73 } DMA_ENTRY; 74 75 /* 76 * Preallocated entries. An allocator seem an overkill here. 77 */ 78 static DMA_ENTRY dmatable[NDMA_DEV]; /* preallocated entries */ 79 80 /* 81 * Heads of free and active lists: 82 */ 83 static TAILQ_HEAD(freehead, dma_entry) dma_free; 84 static TAILQ_HEAD(acthead, dma_entry) dma_active; 85 86 static int must_init = 1; /* Must initialize */ 87 88 int cdmaint __P((void *, int)); 89 90 static void st_dma_init __P((void)); 91 92 static void 93 st_dma_init() 94 { 95 int i; 96 97 TAILQ_INIT(&dma_free); 98 TAILQ_INIT(&dma_active); 99 100 for(i = 0; i < NDMA_DEV; i++) 101 TAILQ_INSERT_HEAD(&dma_free, &dmatable[i], entries); 102 103 if (intr_establish(7, USER_VEC, 0, cdmaint, NULL) == NULL) 104 panic("st_dma_init: Can't establish interrupt"); 105 } 106 107 int 108 st_dmagrab(int_func, call_func, softc, lock_stat, rcaller) 109 dma_farg int_func; 110 dma_farg call_func; 111 void *softc; 112 int *lock_stat; 113 int rcaller; 114 { 115 int sps; 116 DMA_ENTRY *req; 117 118 if(must_init) { 119 st_dma_init(); 120 must_init = 0; 121 } 122 *lock_stat = DMA_LOCK_REQ; 123 124 sps = splhigh(); 125 126 /* 127 * Create a request... 128 */ 129 if(dma_free.tqh_first == NULL) 130 panic("st_dmagrab: Too many outstanding requests"); 131 req = dma_free.tqh_first; 132 TAILQ_REMOVE(&dma_free, dma_free.tqh_first, entries); 133 req->call_func = call_func; 134 req->int_func = int_func; 135 req->softc = softc; 136 req->lock_stat = lock_stat; 137 TAILQ_INSERT_TAIL(&dma_active, req, entries); 138 139 if(dma_active.tqh_first != req) { 140 if (call_func == NULL) { 141 do { 142 tsleep(&dma_active, PRIBIO, "dmalck", 0); 143 } while (*req->lock_stat != DMA_LOCK_GRANT); 144 splx(sps); 145 return(1); 146 } 147 splx(sps); 148 return(0); 149 } 150 splx(sps); 151 152 /* 153 * We're at the head of the queue, ergo: we got the lock. 154 */ 155 *lock_stat = DMA_LOCK_GRANT; 156 157 if(rcaller || (call_func == NULL)) { 158 /* 159 * Just return to caller immediately without going 160 * through 'call_func' first. 161 */ 162 return(1); 163 } 164 165 (*call_func)(softc); /* Call followup function */ 166 return(0); 167 } 168 169 void 170 st_dmafree(softc, lock_stat) 171 void *softc; 172 int *lock_stat; 173 { 174 int sps; 175 DMA_ENTRY *req; 176 177 sps = splhigh(); 178 179 /* 180 * Some validity checks first. 181 */ 182 if((req = dma_active.tqh_first) == NULL) 183 panic("st_dmafree: empty active queue"); 184 if(req->softc != softc) 185 printf("Caller of st_dmafree is not lock-owner!\n"); 186 187 /* 188 * Clear lock status, move request from active to free queue. 189 */ 190 *lock_stat = 0; 191 TAILQ_REMOVE(&dma_active, req, entries); 192 TAILQ_INSERT_HEAD(&dma_free, req, entries); 193 194 if((req = dma_active.tqh_first) != NULL) { 195 *req->lock_stat = DMA_LOCK_GRANT; 196 197 if (req->call_func == NULL) 198 wakeup((caddr_t)&dma_active); 199 else { 200 /* 201 * Call next request through softint handler. This avoids 202 * spl-conflicts. 203 */ 204 add_sicallback((si_farg)req->call_func, req->softc, 0); 205 } 206 } 207 splx(sps); 208 return; 209 } 210 211 int 212 st_dmawanted() 213 { 214 return(dma_active.tqh_first->entries.tqe_next != NULL); 215 } 216 217 int 218 cdmaint(unused, sr) 219 void *unused; 220 int sr; /* sr at time of interrupt */ 221 { 222 dma_farg int_func; 223 void *softc; 224 225 if(dma_active.tqh_first != NULL) { 226 /* 227 * Due to the logic of the ST-DMA chip, it is not possible to 228 * check for stray interrupts here... 229 */ 230 int_func = dma_active.tqh_first->int_func; 231 softc = dma_active.tqh_first->softc; 232 233 if(!BASEPRI(sr)) 234 add_sicallback((si_farg)int_func, softc, 0); 235 else { 236 spl1(); 237 (*int_func)(softc); 238 spl0(); 239 } 240 return 1; 241 } 242 return 0; 243 } 244 245 /* 246 * Setup address for DMA-transfer. 247 * Note: The order _is_ important! 248 */ 249 void 250 st_dmaaddr_set(address) 251 caddr_t address; 252 { 253 register u_long ad = (u_long)address; 254 255 DMA->dma_addr[AD_LOW ] = (ad ) & 0xff; 256 DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff; 257 DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff; 258 } 259 260 /* 261 * Get address from DMA unit. 262 */ 263 u_long 264 st_dmaaddr_get() 265 { 266 register u_long ad = 0; 267 268 ad = (DMA->dma_addr[AD_LOW ] & 0xff); 269 ad |= (DMA->dma_addr[AD_MID ] & 0xff) << 8; 270 ad |= (DMA->dma_addr[AD_HIGH] & 0xff) <<16; 271 return(ad); 272 } 273 274 /* 275 * Program the DMA-controller to transfer 'nblk' blocks of 512 bytes. 276 * The DMA_WRBIT trick flushes the FIFO before doing DMA. 277 */ 278 void 279 st_dmacomm(mode, nblk) 280 int mode, nblk; 281 { 282 DMA->dma_mode = mode; 283 DMA->dma_mode = mode ^ DMA_WRBIT; 284 DMA->dma_mode = mode; 285 DMA->dma_data = nblk; 286 delay(2); /* Needed for Falcon */ 287 DMA->dma_mode = DMA_SCREG | (mode & DMA_WRBIT); 288 } 289