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