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_64_FLOPPY_H
11 #define __ASM_X86_64_FLOPPY_H
12 
13 #include <linux/vmalloc.h>
14 
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) && ((unsigned long)(a)/K_64 != ((unsigned long)(a) + (s) - 1) / K_64))
26 
27 #define CROSS_64KB(a,s) _CROSS_64KB(a,s,use_virtual_dma & 1)
28 
29 
30 #define SW fd_routine[use_virtual_dma&1]
31 #define CSW fd_routine[can_use_virtual_dma & 1]
32 
33 
34 #define fd_inb(port)			inb_p(port)
35 #define fd_outb(port,value)		outb_p(port,value)
36 
37 #define fd_request_dma()        CSW._request_dma(FLOPPY_DMA,"floppy")
38 #define fd_free_dma()           CSW._free_dma(FLOPPY_DMA)
39 #define fd_enable_irq()         enable_irq(FLOPPY_IRQ)
40 #define fd_disable_irq()        disable_irq(FLOPPY_IRQ)
41 #define fd_free_irq()		free_irq(FLOPPY_IRQ, NULL)
42 #define fd_get_dma_residue()    SW._get_dma_residue(FLOPPY_DMA)
43 #define fd_dma_mem_alloc(size)	SW._dma_mem_alloc(size)
44 #define fd_dma_setup(addr, size, mode, io) SW._dma_setup(addr, size, mode, io)
45 
46 #define FLOPPY_CAN_FALLBACK_ON_NODMA
47 
48 static int virtual_dma_count;
49 static int virtual_dma_residue;
50 static char *virtual_dma_addr;
51 static int virtual_dma_mode;
52 static int doing_pdma;
53 
floppy_hardint(int irq,void * dev_id,struct pt_regs * regs)54 static void floppy_hardint(int irq, void *dev_id, struct pt_regs * regs)
55 {
56 	register unsigned char st;
57 
58 #undef TRACE_FLPY_INT
59 
60 #ifdef TRACE_FLPY_INT
61 	static int calls=0;
62 	static int bytes=0;
63 	static int dma_wait=0;
64 #endif
65 	if(!doing_pdma) {
66 		floppy_interrupt(irq, dev_id, regs);
67 		return;
68 	}
69 
70 #ifdef TRACE_FLPY_INT
71 	if(!calls)
72 		bytes = virtual_dma_count;
73 #endif
74 
75 	{
76 		register int lcount;
77 		register char *lptr;
78 
79 		st = 1;
80 		for(lcount=virtual_dma_count, lptr=virtual_dma_addr;
81 		    lcount; lcount--, lptr++) {
82 			st=inb(virtual_dma_port+4) & 0xa0 ;
83 			if(st != 0xa0)
84 				break;
85 			if(virtual_dma_mode)
86 				outb_p(*lptr, virtual_dma_port+5);
87 			else
88 				*lptr = inb_p(virtual_dma_port+5);
89 		}
90 		virtual_dma_count = lcount;
91 		virtual_dma_addr = lptr;
92 		st = inb(virtual_dma_port+4);
93 	}
94 
95 #ifdef TRACE_FLPY_INT
96 	calls++;
97 #endif
98 	if(st == 0x20)
99 		return;
100 	if(!(st & 0x20)) {
101 		virtual_dma_residue += virtual_dma_count;
102 		virtual_dma_count=0;
103 #ifdef TRACE_FLPY_INT
104 		printk("count=%x, residue=%x calls=%d bytes=%d dma_wait=%d\n",
105 		       virtual_dma_count, virtual_dma_residue, calls, bytes,
106 		       dma_wait);
107 		calls = 0;
108 		dma_wait=0;
109 #endif
110 		doing_pdma = 0;
111 		floppy_interrupt(irq, dev_id, regs);
112 		return;
113 	}
114 #ifdef TRACE_FLPY_INT
115 	if(!virtual_dma_count)
116 		dma_wait++;
117 #endif
118 }
119 
fd_disable_dma(void)120 static void fd_disable_dma(void)
121 {
122 	if(! (can_use_virtual_dma & 1))
123 		disable_dma(FLOPPY_DMA);
124 	doing_pdma = 0;
125 	virtual_dma_residue += virtual_dma_count;
126 	virtual_dma_count=0;
127 }
128 
vdma_request_dma(unsigned int dmanr,const char * device_id)129 static int vdma_request_dma(unsigned int dmanr, const char * device_id)
130 {
131 	return 0;
132 }
133 
vdma_nop(unsigned int dummy)134 static void vdma_nop(unsigned int dummy)
135 {
136 }
137 
138 
vdma_get_dma_residue(unsigned int dummy)139 static int vdma_get_dma_residue(unsigned int dummy)
140 {
141 	return virtual_dma_count + virtual_dma_residue;
142 }
143 
144 
fd_request_irq(void)145 static int fd_request_irq(void)
146 {
147 	if(can_use_virtual_dma)
148 		return request_irq(FLOPPY_IRQ, floppy_hardint,SA_INTERRUPT,
149 						   "floppy", NULL);
150 	else
151 		return request_irq(FLOPPY_IRQ, floppy_interrupt,
152 						   SA_INTERRUPT|SA_SAMPLE_RANDOM,
153 						   "floppy", NULL);
154 
155 }
156 
dma_mem_alloc(unsigned long size)157 static unsigned long dma_mem_alloc(unsigned long size)
158 {
159 	return __get_dma_pages(GFP_KERNEL,get_order(size));
160 }
161 
162 
vdma_mem_alloc(unsigned long size)163 static unsigned long vdma_mem_alloc(unsigned long size)
164 {
165 	return (unsigned long) vmalloc(size);
166 
167 }
168 
169 #define nodma_mem_alloc(size) vdma_mem_alloc(size)
170 
_fd_dma_mem_free(unsigned long addr,unsigned long size)171 static void _fd_dma_mem_free(unsigned long addr, unsigned long size)
172 {
173 	if((unsigned long) addr >= (unsigned long) high_memory)
174 		return vfree((void *)addr);
175 	else
176 		free_pages(addr, get_order(size));
177 }
178 
179 #define fd_dma_mem_free(addr, size)  _fd_dma_mem_free(addr, size)
180 
_fd_chose_dma_mode(char * addr,unsigned long size)181 static void _fd_chose_dma_mode(char *addr, unsigned long size)
182 {
183 	if(can_use_virtual_dma == 2) {
184 		if((unsigned long) addr >= (unsigned long) high_memory ||
185 		   virt_to_bus(addr) >= 0x1000000 ||
186 		   _CROSS_64KB(addr, size, 0))
187 			use_virtual_dma = 1;
188 		else
189 			use_virtual_dma = 0;
190 	} else {
191 		use_virtual_dma = can_use_virtual_dma & 1;
192 	}
193 }
194 
195 #define fd_chose_dma_mode(addr, size) _fd_chose_dma_mode(addr, size)
196 
197 
vdma_dma_setup(char * addr,unsigned long size,int mode,int io)198 static int vdma_dma_setup(char *addr, unsigned long size, int mode, int io)
199 {
200 	doing_pdma = 1;
201 	virtual_dma_port = io;
202 	virtual_dma_mode = (mode  == DMA_MODE_WRITE);
203 	virtual_dma_addr = addr;
204 	virtual_dma_count = size;
205 	virtual_dma_residue = 0;
206 	return 0;
207 }
208 
hard_dma_setup(char * addr,unsigned long size,int mode,int io)209 static int hard_dma_setup(char *addr, unsigned long size, int mode, int io)
210 {
211 #ifdef FLOPPY_SANITY_CHECK
212 	if (CROSS_64KB(addr, size)) {
213 		printk("DMA crossing 64-K boundary %p-%p\n", addr, addr+size);
214 		return -1;
215 	}
216 #endif
217 	/* actual, physical DMA */
218 	doing_pdma = 0;
219 	clear_dma_ff(FLOPPY_DMA);
220 	set_dma_mode(FLOPPY_DMA,mode);
221 	set_dma_addr(FLOPPY_DMA,virt_to_bus(addr));
222 	set_dma_count(FLOPPY_DMA,size);
223 	enable_dma(FLOPPY_DMA);
224 	return 0;
225 }
226 
227 struct fd_routine_l {
228 	int (*_request_dma)(unsigned int dmanr, const char * device_id);
229 	void (*_free_dma)(unsigned int dmanr);
230 	int (*_get_dma_residue)(unsigned int dummy);
231 	unsigned long (*_dma_mem_alloc) (unsigned long size);
232 	int (*_dma_setup)(char *addr, unsigned long size, int mode, int io);
233 } fd_routine[] = {
234 	{
235 		request_dma,
236 		free_dma,
237 		get_dma_residue,
238 		dma_mem_alloc,
239 		hard_dma_setup
240 	},
241 	{
242 		vdma_request_dma,
243 		vdma_nop,
244 		vdma_get_dma_residue,
245 		vdma_mem_alloc,
246 		vdma_dma_setup
247 	}
248 };
249 
250 
251 static int FDC1 = 0x3f0;
252 static int FDC2 = -1;
253 
254 /*
255  * Floppy types are stored in the rtc's CMOS RAM and so rtc_lock
256  * is needed to prevent corrupted CMOS RAM in case "insmod floppy"
257  * coincides with another rtc CMOS user.		Paul G.
258  */
259 #define FLOPPY0_TYPE	({				\
260 	unsigned long flags;				\
261 	unsigned char val;				\
262 	spin_lock_irqsave(&rtc_lock, flags);		\
263 	val = (CMOS_READ(0x10) >> 4) & 15;		\
264 	spin_unlock_irqrestore(&rtc_lock, flags);	\
265 	val;						\
266 })
267 
268 #define FLOPPY1_TYPE	({				\
269 	unsigned long flags;				\
270 	unsigned char val;				\
271 	spin_lock_irqsave(&rtc_lock, flags);		\
272 	val = CMOS_READ(0x10) & 15;			\
273 	spin_unlock_irqrestore(&rtc_lock, flags);	\
274 	val;						\
275 })
276 
277 #define N_FDC 2
278 #define N_DRIVE 8
279 
280 #define FLOPPY_MOTOR_MASK 0xf0
281 
282 #define AUTO_DMA
283 
284 #define EXTRA_FLOPPY_PARAMS
285 
286 #endif /* __ASM_X86_64_FLOPPY_H */
287