Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/m68k/atari/stmda.c |
| 3 | * |
| 4 | * Copyright (C) 1994 Roman Hodek |
| 5 | * |
| 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
| 8 | * License. See the file COPYING in the main directory of this archive |
| 9 | * for more details. |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | /* This file contains some function for controlling the access to the */ |
| 14 | /* ST-DMA chip that may be shared between devices. Currently we have: */ |
| 15 | /* TT: Floppy and ACSI bus */ |
| 16 | /* Falcon: Floppy and SCSI */ |
| 17 | /* */ |
| 18 | /* The controlling functions set up a wait queue for access to the */ |
| 19 | /* ST-DMA chip. Callers to stdma_lock() that cannot granted access are */ |
| 20 | /* put onto a queue and waked up later if the owner calls */ |
| 21 | /* stdma_release(). Additionally, the caller gives his interrupt */ |
| 22 | /* service routine to stdma_lock(). */ |
| 23 | /* */ |
| 24 | /* On the Falcon, the IDE bus uses just the ACSI/Floppy interrupt, but */ |
| 25 | /* not the ST-DMA chip itself. So falhd.c needs not to lock the */ |
| 26 | /* chip. The interrupt is routed to falhd.c if IDE is configured, the */ |
| 27 | /* model is a Falcon and the interrupt was caused by the HD controller */ |
| 28 | /* (can be determined by looking at its status register). */ |
| 29 | |
| 30 | |
| 31 | #include <linux/types.h> |
| 32 | #include <linux/kdev_t.h> |
| 33 | #include <linux/genhd.h> |
| 34 | #include <linux/sched.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/interrupt.h> |
| 37 | #include <linux/wait.h> |
Adrian Bunk | a3b2004 | 2008-02-04 22:30:26 -0800 | [diff] [blame] | 38 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include <asm/atari_stdma.h> |
| 41 | #include <asm/atariints.h> |
| 42 | #include <asm/atarihw.h> |
| 43 | #include <asm/io.h> |
| 44 | #include <asm/irq.h> |
| 45 | |
| 46 | static int stdma_locked; /* the semaphore */ |
| 47 | /* int func to be called */ |
David Howells | 40220c1 | 2006-10-09 12:19:47 +0100 | [diff] [blame] | 48 | static irq_handler_t stdma_isr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | static void *stdma_isr_data; /* data passed to isr */ |
| 50 | static DECLARE_WAIT_QUEUE_HEAD(stdma_wait); /* wait queue for ST-DMA */ |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | /***************************** Prototypes *****************************/ |
| 56 | |
Al Viro | 2850bc2 | 2006-10-07 14:16:45 +0100 | [diff] [blame] | 57 | static irqreturn_t stdma_int (int irq, void *dummy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | /************************* End of Prototypes **************************/ |
| 60 | |
| 61 | |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 62 | /** |
| 63 | * stdma_try_lock - attempt to acquire ST DMA interrupt "lock" |
| 64 | * @handler: interrupt handler to use after acquisition |
| 65 | * |
| 66 | * Returns !0 if lock was acquired; otherwise 0. |
| 67 | */ |
| 68 | |
| 69 | int stdma_try_lock(irq_handler_t handler, void *data) |
| 70 | { |
| 71 | unsigned long flags; |
| 72 | |
| 73 | local_irq_save(flags); |
| 74 | if (stdma_locked) { |
| 75 | local_irq_restore(flags); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | stdma_locked = 1; |
| 80 | stdma_isr = handler; |
| 81 | stdma_isr_data = data; |
| 82 | local_irq_restore(flags); |
| 83 | return 1; |
| 84 | } |
| 85 | EXPORT_SYMBOL(stdma_try_lock); |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * Function: void stdma_lock( isrfunc isr, void *data ) |
| 90 | * |
| 91 | * Purpose: Tries to get a lock on the ST-DMA chip that is used by more |
| 92 | * then one device driver. Waits on stdma_wait until lock is free. |
| 93 | * stdma_lock() may not be called from an interrupt! You have to |
| 94 | * get the lock in your main routine and release it when your |
| 95 | * request is finished. |
| 96 | * |
| 97 | * Inputs: A interrupt function that is called until the lock is |
| 98 | * released. |
| 99 | * |
| 100 | * Returns: nothing |
| 101 | * |
| 102 | */ |
| 103 | |
David Howells | 40220c1 | 2006-10-09 12:19:47 +0100 | [diff] [blame] | 104 | void stdma_lock(irq_handler_t handler, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | /* Since the DMA is used for file system purposes, we |
| 107 | have to sleep uninterruptible (there may be locked |
| 108 | buffers) */ |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 109 | wait_event(stdma_wait, stdma_try_lock(handler, data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
Adrian Bunk | a3b2004 | 2008-02-04 22:30:26 -0800 | [diff] [blame] | 111 | EXPORT_SYMBOL(stdma_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | |
| 114 | /* |
| 115 | * Function: void stdma_release( void ) |
| 116 | * |
| 117 | * Purpose: Releases the lock on the ST-DMA chip. |
| 118 | * |
| 119 | * Inputs: none |
| 120 | * |
| 121 | * Returns: nothing |
| 122 | * |
| 123 | */ |
| 124 | |
| 125 | void stdma_release(void) |
| 126 | { |
| 127 | unsigned long flags; |
| 128 | |
| 129 | local_irq_save(flags); |
| 130 | |
| 131 | stdma_locked = 0; |
| 132 | stdma_isr = NULL; |
| 133 | stdma_isr_data = NULL; |
| 134 | wake_up(&stdma_wait); |
| 135 | |
| 136 | local_irq_restore(flags); |
| 137 | } |
Adrian Bunk | a3b2004 | 2008-02-04 22:30:26 -0800 | [diff] [blame] | 138 | EXPORT_SYMBOL(stdma_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 141 | /** |
| 142 | * stdma_is_locked_by - allow lock holder to check whether it needs to release. |
| 143 | * @handler: interrupt handler previously used to acquire lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | * |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 145 | * Returns !0 if locked for the given handler; 0 otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | */ |
| 147 | |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 148 | int stdma_is_locked_by(irq_handler_t handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 150 | unsigned long flags; |
| 151 | int result; |
| 152 | |
| 153 | local_irq_save(flags); |
| 154 | result = stdma_locked && (stdma_isr == handler); |
| 155 | local_irq_restore(flags); |
| 156 | |
| 157 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 159 | EXPORT_SYMBOL(stdma_is_locked_by); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | |
| 162 | /* |
| 163 | * Function: int stdma_islocked( void ) |
| 164 | * |
| 165 | * Purpose: Check if the ST-DMA is currently locked. |
| 166 | * Note: Returned status is only valid if ints are disabled while calling and |
| 167 | * as long as they remain disabled. |
| 168 | * If called with ints enabled, status can change only from locked to |
| 169 | * unlocked, because ints may not lock the ST-DMA. |
| 170 | * |
| 171 | * Inputs: none |
| 172 | * |
| 173 | * Returns: != 0 if locked, 0 otherwise |
| 174 | * |
| 175 | */ |
| 176 | |
| 177 | int stdma_islocked(void) |
| 178 | { |
| 179 | return stdma_locked; |
| 180 | } |
Adrian Bunk | a3b2004 | 2008-02-04 22:30:26 -0800 | [diff] [blame] | 181 | EXPORT_SYMBOL(stdma_islocked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 183 | |
| 184 | /* |
| 185 | * Function: void stdma_init( void ) |
| 186 | * |
| 187 | * Purpose: Initialize the ST-DMA chip access controlling. |
| 188 | * It sets up the interrupt and its service routine. The int is registered |
| 189 | * as slow int, client devices have to live with that (no problem |
| 190 | * currently). |
| 191 | * |
| 192 | * Inputs: none |
| 193 | * |
| 194 | * Return: nothing |
| 195 | * |
| 196 | */ |
| 197 | |
| 198 | void __init stdma_init(void) |
| 199 | { |
| 200 | stdma_isr = NULL; |
Geert Uytterhoeven | 87511d0 | 2014-10-05 22:34:40 +0200 | [diff] [blame] | 201 | if (request_irq(IRQ_MFP_FDC, stdma_int, IRQF_SHARED, |
Geert Uytterhoeven | 79abeed | 2011-05-04 14:55:41 +0200 | [diff] [blame] | 202 | "ST-DMA floppy,ACSI,IDE,Falcon-SCSI", stdma_int)) |
Geert Uytterhoeven | 5b8b4c3 | 2008-12-30 14:01:32 +0100 | [diff] [blame] | 203 | pr_err("Couldn't register ST-DMA interrupt\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | |
| 207 | /* |
| 208 | * Function: void stdma_int() |
| 209 | * |
| 210 | * Purpose: The interrupt routine for the ST-DMA. It calls the isr |
| 211 | * registered by stdma_lock(). |
| 212 | * |
| 213 | */ |
| 214 | |
Al Viro | 2850bc2 | 2006-10-07 14:16:45 +0100 | [diff] [blame] | 215 | static irqreturn_t stdma_int(int irq, void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | if (stdma_isr) |
Al Viro | 2850bc2 | 2006-10-07 14:16:45 +0100 | [diff] [blame] | 218 | (*stdma_isr)(irq, stdma_isr_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return IRQ_HANDLED; |
| 220 | } |