1 /* $NetBSD: dma.c,v 1.23 2009/07/08 12:23:09 tsutsui 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/cdefs.h> 55 __KERNEL_RCSID(0, "$NetBSD: dma.c,v 1.23 2009/07/08 12:23:09 tsutsui Exp $"); 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/kernel.h> 60 #include <sys/proc.h> 61 #include <sys/queue.h> 62 63 #include <machine/cpu.h> 64 #include <machine/iomap.h> 65 #include <machine/dma.h> 66 #include <machine/intr.h> 67 68 #define NDMA_DEV 10 /* Max 2 floppy's, 8 hard-disks */ 69 typedef struct dma_entry { 70 TAILQ_ENTRY(dma_entry) entries; /* List pointers */ 71 void (*call_func)(void *); /* Call when lock granted */ 72 void (*int_func)(void *); /* Call on DMA interrupt */ 73 void *softc; /* Arg. to int_func */ 74 int *lock_stat; /* status of DMA lock */ 75 } DMA_ENTRY; 76 77 /* 78 * Preallocated entries. An allocator seem an overkill here. 79 */ 80 static DMA_ENTRY dmatable[NDMA_DEV]; /* preallocated entries */ 81 82 /* 83 * Heads of free and active lists: 84 */ 85 static TAILQ_HEAD(freehead, dma_entry) dma_free; 86 static TAILQ_HEAD(acthead, dma_entry) dma_active; 87 88 static int must_init = 1; /* Must initialize */ 89 90 int cdmaint(void *, int); 91 92 static void st_dma_init(void); 93 94 static void 95 st_dma_init(void) 96 { 97 int i; 98 99 TAILQ_INIT(&dma_free); 100 TAILQ_INIT(&dma_active); 101 102 for(i = 0; i < NDMA_DEV; i++) 103 TAILQ_INSERT_HEAD(&dma_free, &dmatable[i], entries); 104 105 if (intr_establish(7, USER_VEC, 0, cdmaint, NULL) == NULL) 106 panic("st_dma_init: Can't establish interrupt"); 107 } 108 109 int 110 st_dmagrab(dma_farg int_func, dma_farg call_func, void *softc, int *lock_stat, int rcaller) 111 { 112 int sps; 113 DMA_ENTRY *req; 114 115 if(must_init) { 116 st_dma_init(); 117 must_init = 0; 118 } 119 *lock_stat = DMA_LOCK_REQ; 120 121 sps = splhigh(); 122 123 /* 124 * Create a request... 125 */ 126 if(dma_free.tqh_first == NULL) 127 panic("st_dmagrab: Too many outstanding requests"); 128 req = dma_free.tqh_first; 129 TAILQ_REMOVE(&dma_free, dma_free.tqh_first, entries); 130 req->call_func = call_func; 131 req->int_func = int_func; 132 req->softc = softc; 133 req->lock_stat = lock_stat; 134 TAILQ_INSERT_TAIL(&dma_active, req, entries); 135 136 if(dma_active.tqh_first != req) { 137 if (call_func == NULL) { 138 do { 139 tsleep(&dma_active, PRIBIO, "dmalck", 0); 140 } while (*req->lock_stat != DMA_LOCK_GRANT); 141 splx(sps); 142 return(1); 143 } 144 splx(sps); 145 return(0); 146 } 147 splx(sps); 148 149 /* 150 * We're at the head of the queue, ergo: we got the lock. 151 */ 152 *lock_stat = DMA_LOCK_GRANT; 153 154 if(rcaller || (call_func == NULL)) { 155 /* 156 * Just return to caller immediately without going 157 * through 'call_func' first. 158 */ 159 return(1); 160 } 161 162 (*call_func)(softc); /* Call followup function */ 163 return(0); 164 } 165 166 void 167 st_dmafree(void *softc, int *lock_stat) 168 { 169 int sps; 170 DMA_ENTRY *req; 171 172 sps = splhigh(); 173 174 /* 175 * Some validity checks first. 176 */ 177 if((req = dma_active.tqh_first) == NULL) 178 panic("st_dmafree: empty active queue"); 179 if(req->softc != softc) 180 printf("Caller of st_dmafree is not lock-owner!\n"); 181 182 /* 183 * Clear lock status, move request from active to free queue. 184 */ 185 *lock_stat = 0; 186 TAILQ_REMOVE(&dma_active, req, entries); 187 TAILQ_INSERT_HEAD(&dma_free, req, entries); 188 189 if((req = dma_active.tqh_first) != NULL) { 190 *req->lock_stat = DMA_LOCK_GRANT; 191 192 if (req->call_func == NULL) 193 wakeup((void *)&dma_active); 194 else { 195 /* 196 * Call next request through softint handler. This avoids 197 * spl-conflicts. 198 */ 199 add_sicallback((si_farg)req->call_func, req->softc, 0); 200 } 201 } 202 splx(sps); 203 return; 204 } 205 206 int 207 st_dmawanted(void) 208 { 209 return(dma_active.tqh_first->entries.tqe_next != NULL); 210 } 211 212 int 213 cdmaint(void *unused, int sr) 214 /* sr: sr at time of interrupt */ 215 { 216 dma_farg int_func; 217 void *softc; 218 219 if(dma_active.tqh_first != NULL) { 220 /* 221 * Due to the logic of the ST-DMA chip, it is not possible to 222 * check for stray interrupts here... 223 */ 224 int_func = dma_active.tqh_first->int_func; 225 softc = dma_active.tqh_first->softc; 226 227 if(!BASEPRI(sr)) 228 add_sicallback((si_farg)int_func, softc, 0); 229 else { 230 spl1(); 231 (*int_func)(softc); 232 spl0(); 233 } 234 return 1; 235 } 236 return 0; 237 } 238 239 /* 240 * Setup address for DMA-transfer. 241 * Note: The order _is_ important! 242 */ 243 void 244 st_dmaaddr_set(void * address) 245 { 246 register u_long ad = (u_long)address; 247 248 DMA->dma_addr[AD_LOW ] = (ad ) & 0xff; 249 DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff; 250 DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff; 251 } 252 253 /* 254 * Get address from DMA unit. 255 */ 256 u_long 257 st_dmaaddr_get(void) 258 { 259 register u_long ad = 0; 260 261 ad = (DMA->dma_addr[AD_LOW ] & 0xff); 262 ad |= (DMA->dma_addr[AD_MID ] & 0xff) << 8; 263 ad |= (DMA->dma_addr[AD_HIGH] & 0xff) <<16; 264 return(ad); 265 } 266 267 /* 268 * Program the DMA-controller to transfer 'nblk' blocks of 512 bytes. 269 * The DMA_WRBIT trick flushes the FIFO before doing DMA. 270 */ 271 void 272 st_dmacomm(int mode, int nblk) 273 { 274 DMA->dma_mode = mode; 275 DMA->dma_mode = mode ^ DMA_WRBIT; 276 DMA->dma_mode = mode; 277 DMA->dma_data = nblk; 278 delay(2); /* Needed for Falcon */ 279 DMA->dma_mode = DMA_SCREG | (mode & DMA_WRBIT); 280 } 281