xref: /linux/arch/m68k/atari/stdma.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/arch/m68k/atari/stmda.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1994 Roman Hodek
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * This file is subject to the terms and conditions of the GNU General Public
81da177e4SLinus Torvalds  * License.  See the file COPYING in the main directory of this archive
91da177e4SLinus Torvalds  * for more details.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds /* This file contains some function for controlling the access to the  */
141da177e4SLinus Torvalds /* ST-DMA chip that may be shared between devices. Currently we have:  */
151da177e4SLinus Torvalds /*   TT:     Floppy and ACSI bus                                       */
161da177e4SLinus Torvalds /*   Falcon: Floppy and SCSI                                           */
171da177e4SLinus Torvalds /*                                                                     */
181da177e4SLinus Torvalds /* The controlling functions set up a wait queue for access to the     */
191da177e4SLinus Torvalds /* ST-DMA chip. Callers to stdma_lock() that cannot granted access are */
201da177e4SLinus Torvalds /* put onto a queue and waked up later if the owner calls              */
211da177e4SLinus Torvalds /* stdma_release(). Additionally, the caller gives his interrupt       */
221da177e4SLinus Torvalds /* service routine to stdma_lock().                                    */
231da177e4SLinus Torvalds /*                                                                     */
241da177e4SLinus Torvalds /* On the Falcon, the IDE bus uses just the ACSI/Floppy interrupt, but */
251da177e4SLinus Torvalds /* not the ST-DMA chip itself. So falhd.c needs not to lock the        */
261da177e4SLinus Torvalds /* chip. The interrupt is routed to falhd.c if IDE is configured, the  */
271da177e4SLinus Torvalds /* model is a Falcon and the interrupt was caused by the HD controller */
281da177e4SLinus Torvalds /* (can be determined by looking at its status register).              */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <linux/types.h>
321da177e4SLinus Torvalds #include <linux/kdev_t.h>
331da177e4SLinus Torvalds #include <linux/sched.h>
341da177e4SLinus Torvalds #include <linux/init.h>
351da177e4SLinus Torvalds #include <linux/interrupt.h>
361da177e4SLinus Torvalds #include <linux/wait.h>
37a3b2004aSAdrian Bunk #include <linux/module.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <asm/atari_stdma.h>
401da177e4SLinus Torvalds #include <asm/atariints.h>
411da177e4SLinus Torvalds #include <asm/atarihw.h>
421da177e4SLinus Torvalds #include <asm/io.h>
431da177e4SLinus Torvalds #include <asm/irq.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds static int stdma_locked;			/* the semaphore */
461da177e4SLinus Torvalds 						/* int func to be called */
4740220c1aSDavid Howells static irq_handler_t stdma_isr;
481da177e4SLinus Torvalds static void *stdma_isr_data;			/* data passed to isr */
491da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(stdma_wait);	/* wait queue for ST-DMA */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /***************************** Prototypes *****************************/
551da177e4SLinus Torvalds 
562850bc27SAl Viro static irqreturn_t stdma_int (int irq, void *dummy);
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /************************* End of Prototypes **************************/
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 
6116b29e75SFinn Thain /**
6216b29e75SFinn Thain  * stdma_try_lock - attempt to acquire ST DMA interrupt "lock"
6316b29e75SFinn Thain  * @handler: interrupt handler to use after acquisition
64*4ad69503SGeert Uytterhoeven  * @data: cookie passed to the interrupt handler function
6516b29e75SFinn Thain  *
6616b29e75SFinn Thain  * Returns !0 if lock was acquired; otherwise 0.
6716b29e75SFinn Thain  */
6816b29e75SFinn Thain 
stdma_try_lock(irq_handler_t handler,void * data)6916b29e75SFinn Thain int stdma_try_lock(irq_handler_t handler, void *data)
7016b29e75SFinn Thain {
7116b29e75SFinn Thain 	unsigned long flags;
7216b29e75SFinn Thain 
7316b29e75SFinn Thain 	local_irq_save(flags);
7416b29e75SFinn Thain 	if (stdma_locked) {
7516b29e75SFinn Thain 		local_irq_restore(flags);
7616b29e75SFinn Thain 		return 0;
7716b29e75SFinn Thain 	}
7816b29e75SFinn Thain 
7916b29e75SFinn Thain 	stdma_locked   = 1;
8016b29e75SFinn Thain 	stdma_isr      = handler;
8116b29e75SFinn Thain 	stdma_isr_data = data;
8216b29e75SFinn Thain 	local_irq_restore(flags);
8316b29e75SFinn Thain 	return 1;
8416b29e75SFinn Thain }
8516b29e75SFinn Thain EXPORT_SYMBOL(stdma_try_lock);
8616b29e75SFinn Thain 
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /*
891da177e4SLinus Torvalds  * Function: void stdma_lock( isrfunc isr, void *data )
901da177e4SLinus Torvalds  *
911da177e4SLinus Torvalds  * Purpose: Tries to get a lock on the ST-DMA chip that is used by more
921da177e4SLinus Torvalds  *   then one device driver. Waits on stdma_wait until lock is free.
931da177e4SLinus Torvalds  *   stdma_lock() may not be called from an interrupt! You have to
941da177e4SLinus Torvalds  *   get the lock in your main routine and release it when your
951da177e4SLinus Torvalds  *   request is finished.
961da177e4SLinus Torvalds  *
971da177e4SLinus Torvalds  * Inputs: A interrupt function that is called until the lock is
981da177e4SLinus Torvalds  *   released.
991da177e4SLinus Torvalds  *
1001da177e4SLinus Torvalds  * Returns: nothing
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  */
1031da177e4SLinus Torvalds 
stdma_lock(irq_handler_t handler,void * data)10440220c1aSDavid Howells void stdma_lock(irq_handler_t handler, void *data)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	/* Since the DMA is used for file system purposes, we
1071da177e4SLinus Torvalds 	 have to sleep uninterruptible (there may be locked
1081da177e4SLinus Torvalds 	 buffers) */
10916b29e75SFinn Thain 	wait_event(stdma_wait, stdma_try_lock(handler, data));
1101da177e4SLinus Torvalds }
111a3b2004aSAdrian Bunk EXPORT_SYMBOL(stdma_lock);
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /*
1151da177e4SLinus Torvalds  * Function: void stdma_release( void )
1161da177e4SLinus Torvalds  *
1171da177e4SLinus Torvalds  * Purpose: Releases the lock on the ST-DMA chip.
1181da177e4SLinus Torvalds  *
1191da177e4SLinus Torvalds  * Inputs: none
1201da177e4SLinus Torvalds  *
1211da177e4SLinus Torvalds  * Returns: nothing
1221da177e4SLinus Torvalds  *
1231da177e4SLinus Torvalds  */
1241da177e4SLinus Torvalds 
stdma_release(void)1251da177e4SLinus Torvalds void stdma_release(void)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	unsigned long flags;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	local_irq_save(flags);
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	stdma_locked   = 0;
1321da177e4SLinus Torvalds 	stdma_isr      = NULL;
1331da177e4SLinus Torvalds 	stdma_isr_data = NULL;
1341da177e4SLinus Torvalds 	wake_up(&stdma_wait);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	local_irq_restore(flags);
1371da177e4SLinus Torvalds }
138a3b2004aSAdrian Bunk EXPORT_SYMBOL(stdma_release);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 
14116b29e75SFinn Thain /**
14216b29e75SFinn Thain  * stdma_is_locked_by - allow lock holder to check whether it needs to release.
14316b29e75SFinn Thain  * @handler: interrupt handler previously used to acquire lock.
1441da177e4SLinus Torvalds  *
14516b29e75SFinn Thain  * Returns !0 if locked for the given handler; 0 otherwise.
1461da177e4SLinus Torvalds  */
1471da177e4SLinus Torvalds 
stdma_is_locked_by(irq_handler_t handler)14816b29e75SFinn Thain int stdma_is_locked_by(irq_handler_t handler)
1491da177e4SLinus Torvalds {
15016b29e75SFinn Thain 	unsigned long flags;
15116b29e75SFinn Thain 	int result;
15216b29e75SFinn Thain 
15316b29e75SFinn Thain 	local_irq_save(flags);
15416b29e75SFinn Thain 	result = stdma_locked && (stdma_isr == handler);
15516b29e75SFinn Thain 	local_irq_restore(flags);
15616b29e75SFinn Thain 
15716b29e75SFinn Thain 	return result;
1581da177e4SLinus Torvalds }
15916b29e75SFinn Thain EXPORT_SYMBOL(stdma_is_locked_by);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /*
1631da177e4SLinus Torvalds  * Function: int stdma_islocked( void )
1641da177e4SLinus Torvalds  *
1651da177e4SLinus Torvalds  * Purpose: Check if the ST-DMA is currently locked.
1661da177e4SLinus Torvalds  * Note: Returned status is only valid if ints are disabled while calling and
1671da177e4SLinus Torvalds  *       as long as they remain disabled.
1681da177e4SLinus Torvalds  *       If called with ints enabled, status can change only from locked to
1691da177e4SLinus Torvalds  *       unlocked, because ints may not lock the ST-DMA.
1701da177e4SLinus Torvalds  *
1711da177e4SLinus Torvalds  * Inputs: none
1721da177e4SLinus Torvalds  *
1731da177e4SLinus Torvalds  * Returns: != 0 if locked, 0 otherwise
1741da177e4SLinus Torvalds  *
1751da177e4SLinus Torvalds  */
1761da177e4SLinus Torvalds 
stdma_islocked(void)1771da177e4SLinus Torvalds int stdma_islocked(void)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	return stdma_locked;
1801da177e4SLinus Torvalds }
181a3b2004aSAdrian Bunk EXPORT_SYMBOL(stdma_islocked);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds /*
1851da177e4SLinus Torvalds  * Function: void stdma_init( void )
1861da177e4SLinus Torvalds  *
1871da177e4SLinus Torvalds  * Purpose: Initialize the ST-DMA chip access controlling.
1881da177e4SLinus Torvalds  *   It sets up the interrupt and its service routine. The int is registered
1891da177e4SLinus Torvalds  *   as slow int, client devices have to live with that (no problem
1901da177e4SLinus Torvalds  *   currently).
1911da177e4SLinus Torvalds  *
1921da177e4SLinus Torvalds  * Inputs: none
1931da177e4SLinus Torvalds  *
1941da177e4SLinus Torvalds  * Return: nothing
1951da177e4SLinus Torvalds  *
1961da177e4SLinus Torvalds  */
1971da177e4SLinus Torvalds 
stdma_init(void)1981da177e4SLinus Torvalds void __init stdma_init(void)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	stdma_isr = NULL;
20187511d09SGeert Uytterhoeven 	if (request_irq(IRQ_MFP_FDC, stdma_int, IRQF_SHARED,
20279abeed6SGeert Uytterhoeven 			"ST-DMA floppy,ACSI,IDE,Falcon-SCSI", stdma_int))
2035b8b4c3dSGeert Uytterhoeven 		pr_err("Couldn't register ST-DMA interrupt\n");
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /*
2081da177e4SLinus Torvalds  * Function: void stdma_int()
2091da177e4SLinus Torvalds  *
2101da177e4SLinus Torvalds  * Purpose: The interrupt routine for the ST-DMA. It calls the isr
2111da177e4SLinus Torvalds  *   registered by stdma_lock().
2121da177e4SLinus Torvalds  *
2131da177e4SLinus Torvalds  */
2141da177e4SLinus Torvalds 
stdma_int(int irq,void * dummy)2152850bc27SAl Viro static irqreturn_t stdma_int(int irq, void *dummy)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds   if (stdma_isr)
2182850bc27SAl Viro       (*stdma_isr)(irq, stdma_isr_data);
2191da177e4SLinus Torvalds   return IRQ_HANDLED;
2201da177e4SLinus Torvalds }
221