1 /* 2 * Architecture specific parts of the Floppy driver 3 * 4 * This file is subject to the terms and conditions of the GNU General Public 5 * License. See the file "COPYING" in the main directory of this archive 6 * for more details. 7 * 8 * Copyright (C) 1995 9 */ 10 #ifndef _ASM_X86_FLOPPY_H 11 #define _ASM_X86_FLOPPY_H 12 13 #include <linux/sizes.h> 14 #include <linux/vmalloc.h> 15 16 /* 17 * The DMA channel used by the floppy controller cannot access data at 18 * addresses >= 16MB 19 * 20 * Went back to the 1MB limit, as some people had problems with the floppy 21 * driver otherwise. It doesn't matter much for performance anyway, as most 22 * floppy accesses go through the track buffer. 23 */ 24 #define _CROSS_64KB(a, s, vdma) \ 25 (!(vdma) && \ 26 ((unsigned long)(a) / SZ_64K != ((unsigned long)(a) + (s) - 1) / SZ_64K)) 27 28 #define SW fd_routine[use_virtual_dma & 1] 29 #define CSW fd_routine[can_use_virtual_dma & 1] 30 31 32 #define fd_request_dma() CSW._request_dma(FLOPPY_DMA, "floppy") 33 #define fd_free_dma() CSW._free_dma(FLOPPY_DMA) 34 #define fd_enable_irq() enable_irq(FLOPPY_IRQ) 35 #define fd_disable_irq() disable_irq(FLOPPY_IRQ) 36 #define fd_free_irq() free_irq(FLOPPY_IRQ, NULL) 37 #define fd_get_dma_residue() SW._get_dma_residue(FLOPPY_DMA) 38 #define fd_dma_mem_alloc(size) SW._dma_mem_alloc(size) 39 #define fd_dma_setup(addr, size, mode, io) SW._dma_setup(addr, size, mode, io) 40 41 #define FLOPPY_CAN_FALLBACK_ON_NODMA 42 43 static int virtual_dma_count; 44 static int virtual_dma_residue; 45 static char *virtual_dma_addr; 46 static int virtual_dma_mode; 47 static int doing_pdma; 48 49 static inline u8 fd_inb(u16 base, u16 reg) 50 { 51 u8 ret = inb_p(base + reg); 52 53 native_io_delay(); 54 native_io_delay(); 55 native_io_delay(); 56 57 return ret; 58 } 59 60 static inline void fd_outb(u8 value, u16 base, u16 reg) 61 { 62 outb_p(value, base + reg); 63 64 native_io_delay(); 65 native_io_delay(); 66 native_io_delay(); 67 } 68 69 static irqreturn_t floppy_hardint(int irq, void *dev_id) 70 { 71 unsigned char st; 72 73 #undef TRACE_FLPY_INT 74 75 #ifdef TRACE_FLPY_INT 76 static int calls; 77 static int bytes; 78 static int dma_wait; 79 #endif 80 if (!doing_pdma) 81 return floppy_interrupt(irq, dev_id); 82 83 #ifdef TRACE_FLPY_INT 84 if (!calls) 85 bytes = virtual_dma_count; 86 #endif 87 88 { 89 int lcount; 90 char *lptr; 91 92 for (lcount = virtual_dma_count, lptr = virtual_dma_addr; 93 lcount; lcount--, lptr++) { 94 st = inb(virtual_dma_port + FD_STATUS); 95 st &= STATUS_DMA | STATUS_READY; 96 if (st != (STATUS_DMA | STATUS_READY)) 97 break; 98 if (virtual_dma_mode) 99 fd_outb(*lptr, virtual_dma_port, FD_DATA); 100 else 101 *lptr = fd_inb(virtual_dma_port, FD_DATA); 102 } 103 virtual_dma_count = lcount; 104 virtual_dma_addr = lptr; 105 st = inb(virtual_dma_port + FD_STATUS); 106 } 107 108 #ifdef TRACE_FLPY_INT 109 calls++; 110 #endif 111 if (st == STATUS_DMA) 112 return IRQ_HANDLED; 113 if (!(st & STATUS_DMA)) { 114 virtual_dma_residue += virtual_dma_count; 115 virtual_dma_count = 0; 116 #ifdef TRACE_FLPY_INT 117 printk(KERN_DEBUG "count=%x, residue=%x calls=%d bytes=%d dma_wait=%d\n", 118 virtual_dma_count, virtual_dma_residue, calls, bytes, 119 dma_wait); 120 calls = 0; 121 dma_wait = 0; 122 #endif 123 doing_pdma = 0; 124 floppy_interrupt(irq, dev_id); 125 return IRQ_HANDLED; 126 } 127 #ifdef TRACE_FLPY_INT 128 if (!virtual_dma_count) 129 dma_wait++; 130 #endif 131 return IRQ_HANDLED; 132 } 133 134 static void fd_disable_dma(void) 135 { 136 if (!(can_use_virtual_dma & 1)) 137 disable_dma(FLOPPY_DMA); 138 doing_pdma = 0; 139 virtual_dma_residue += virtual_dma_count; 140 virtual_dma_count = 0; 141 } 142 143 static int vdma_request_dma(unsigned int dmanr, const char *device_id) 144 { 145 return 0; 146 } 147 148 static void vdma_nop(unsigned int dummy) 149 { 150 } 151 152 153 static int vdma_get_dma_residue(unsigned int dummy) 154 { 155 return virtual_dma_count + virtual_dma_residue; 156 } 157 158 159 static int fd_request_irq(void) 160 { 161 if (can_use_virtual_dma) 162 return request_irq(FLOPPY_IRQ, floppy_hardint, 163 0, "floppy", NULL); 164 else 165 return request_irq(FLOPPY_IRQ, floppy_interrupt, 166 0, "floppy", NULL); 167 } 168 169 static unsigned long dma_mem_alloc(unsigned long size) 170 { 171 return __get_dma_pages(GFP_KERNEL|__GFP_NORETRY, get_order(size)); 172 } 173 174 175 static unsigned long vdma_mem_alloc(unsigned long size) 176 { 177 return (unsigned long)vmalloc(size); 178 179 } 180 181 #define nodma_mem_alloc(size) vdma_mem_alloc(size) 182 183 static void _fd_dma_mem_free(unsigned long addr, unsigned long size) 184 { 185 if ((unsigned long)addr >= (unsigned long)high_memory) 186 vfree((void *)addr); 187 else 188 free_pages(addr, get_order(size)); 189 } 190 191 #define fd_dma_mem_free(addr, size) _fd_dma_mem_free(addr, size) 192 193 static void _fd_chose_dma_mode(char *addr, unsigned long size) 194 { 195 if (can_use_virtual_dma == 2) { 196 if ((unsigned long)addr >= (unsigned long)high_memory || 197 isa_virt_to_bus(addr) >= 0x1000000 || 198 _CROSS_64KB(addr, size, 0)) 199 use_virtual_dma = 1; 200 else 201 use_virtual_dma = 0; 202 } else { 203 use_virtual_dma = can_use_virtual_dma & 1; 204 } 205 } 206 207 #define fd_chose_dma_mode(addr, size) _fd_chose_dma_mode(addr, size) 208 209 210 static int vdma_dma_setup(char *addr, unsigned long size, int mode, int io) 211 { 212 doing_pdma = 1; 213 virtual_dma_port = io; 214 virtual_dma_mode = (mode == DMA_MODE_WRITE); 215 virtual_dma_addr = addr; 216 virtual_dma_count = size; 217 virtual_dma_residue = 0; 218 return 0; 219 } 220 221 static int hard_dma_setup(char *addr, unsigned long size, int mode, int io) 222 { 223 #ifdef FLOPPY_SANITY_CHECK 224 if (_CROSS_64KB(addr, size, use_virtual_dma & 1)) { 225 printk("DMA crossing 64-K boundary %p-%p\n", addr, addr+size); 226 return -1; 227 } 228 #endif 229 /* actual, physical DMA */ 230 doing_pdma = 0; 231 clear_dma_ff(FLOPPY_DMA); 232 set_dma_mode(FLOPPY_DMA, mode); 233 set_dma_addr(FLOPPY_DMA, isa_virt_to_bus(addr)); 234 set_dma_count(FLOPPY_DMA, size); 235 enable_dma(FLOPPY_DMA); 236 return 0; 237 } 238 239 static struct fd_routine_l { 240 int (*_request_dma)(unsigned int dmanr, const char *device_id); 241 void (*_free_dma)(unsigned int dmanr); 242 int (*_get_dma_residue)(unsigned int dummy); 243 unsigned long (*_dma_mem_alloc)(unsigned long size); 244 int (*_dma_setup)(char *addr, unsigned long size, int mode, int io); 245 } fd_routine[] = { 246 { 247 ._request_dma = request_dma, 248 ._free_dma = free_dma, 249 ._get_dma_residue = get_dma_residue, 250 ._dma_mem_alloc = dma_mem_alloc, 251 ._dma_setup = hard_dma_setup 252 }, 253 { 254 ._request_dma = vdma_request_dma, 255 ._free_dma = vdma_nop, 256 ._get_dma_residue = vdma_get_dma_residue, 257 ._dma_mem_alloc = vdma_mem_alloc, 258 ._dma_setup = vdma_dma_setup 259 } 260 }; 261 262 263 static int FDC1 = 0x3f0; 264 static int FDC2 = -1; 265 266 /* 267 * Floppy types are stored in the rtc's CMOS RAM and so rtc_lock 268 * is needed to prevent corrupted CMOS RAM in case "insmod floppy" 269 * coincides with another rtc CMOS user. Paul G. 270 */ 271 #define FLOPPY0_TYPE \ 272 ({ \ 273 unsigned long flags; \ 274 unsigned char val; \ 275 spin_lock_irqsave(&rtc_lock, flags); \ 276 val = (CMOS_READ(0x10) >> 4) & 15; \ 277 spin_unlock_irqrestore(&rtc_lock, flags); \ 278 val; \ 279 }) 280 281 #define FLOPPY1_TYPE \ 282 ({ \ 283 unsigned long flags; \ 284 unsigned char val; \ 285 spin_lock_irqsave(&rtc_lock, flags); \ 286 val = CMOS_READ(0x10) & 15; \ 287 spin_unlock_irqrestore(&rtc_lock, flags); \ 288 val; \ 289 }) 290 291 #define N_FDC 2 292 #define N_DRIVE 8 293 294 #define EXTRA_FLOPPY_PARAMS 295 296 #endif /* _ASM_X86_FLOPPY_H */ 297