vserver 1.9.5.x5
[linux-2.6.git] / drivers / char / ftape / lowlevel / fdc-io.c
1 /*
2  * Copyright (C) 1993-1996 Bas Laarhoven,
3  *           (C) 1996-1997 Claus-Justus Heine.
4
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2, or (at your option)
8  any later version.
9
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING.  If not, write to
17  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18
19  *
20  * $Source: /homes/cvs/ftape-stacked/ftape/lowlevel/fdc-io.c,v $
21  * $Revision: 1.7.4.2 $
22  * $Date: 1997/11/16 14:48:17 $
23  *
24  *      This file contains the low-level floppy disk interface code
25  *      for the QIC-40/80/3010/3020 floppy-tape driver "ftape" for
26  *      Linux.
27  */
28
29 #include <linux/config.h> /* for CONFIG_FT_* */
30 #include <linux/errno.h>
31 #include <linux/sched.h>
32 #include <linux/ioport.h>
33 #include <linux/interrupt.h>
34 #include <linux/kernel.h>
35 #include <asm/system.h>
36 #include <asm/io.h>
37 #include <asm/dma.h>
38 #include <asm/irq.h>
39
40 #include <linux/ftape.h>
41 #include <linux/qic117.h>
42 #include "../lowlevel/ftape-tracing.h"
43 #include "../lowlevel/fdc-io.h"
44 #include "../lowlevel/fdc-isr.h"
45 #include "../lowlevel/ftape-io.h"
46 #include "../lowlevel/ftape-rw.h"
47 #include "../lowlevel/ftape-ctl.h"
48 #include "../lowlevel/ftape-calibr.h"
49 #include "../lowlevel/fc-10.h"
50
51 /*      Global vars.
52  */
53 static int ftape_motor;
54 volatile int ftape_current_cylinder = -1;
55 volatile fdc_mode_enum fdc_mode = fdc_idle;
56 fdc_config_info fdc;
57 DECLARE_WAIT_QUEUE_HEAD(ftape_wait_intr);
58
59 unsigned int ft_fdc_base       = CONFIG_FT_FDC_BASE;
60 unsigned int ft_fdc_irq        = CONFIG_FT_FDC_IRQ;
61 unsigned int ft_fdc_dma        = CONFIG_FT_FDC_DMA;
62 unsigned int ft_fdc_threshold  = CONFIG_FT_FDC_THR;  /* bytes */
63 unsigned int ft_fdc_rate_limit = CONFIG_FT_FDC_MAX_RATE; /* bits/sec */
64 int ft_probe_fc10        = CONFIG_FT_PROBE_FC10;
65 int ft_mach2             = CONFIG_FT_MACH2;
66
67 /*      Local vars.
68  */
69 static spinlock_t fdc_io_lock; 
70 static unsigned int fdc_calibr_count;
71 static unsigned int fdc_calibr_time;
72 static int fdc_status;
73 volatile __u8 fdc_head;         /* FDC head from sector id */
74 volatile __u8 fdc_cyl;          /* FDC track from sector id */
75 volatile __u8 fdc_sect;         /* FDC sector from sector id */
76 static int fdc_data_rate = 500; /* data rate (Kbps) */
77 static int fdc_rate_code;       /* data rate code (0 == 500 Kbps) */
78 static int fdc_seek_rate = 2;   /* step rate (msec) */
79 static void (*do_ftape) (void);
80 static int fdc_fifo_state;      /* original fifo setting - fifo enabled */
81 static int fdc_fifo_thr;        /* original fifo setting - threshold */
82 static int fdc_lock_state;      /* original lock setting - locked */
83 static int fdc_fifo_locked;     /* has fifo && lock set ? */
84 static __u8 fdc_precomp;        /* default precomp. value (nsec) */
85 static __u8 fdc_prec_code;      /* fdc precomp. select code */
86
87 static char ftape_id[] = "ftape";  /* used by request irq and free irq */
88
89 static int fdc_set_seek_rate(int seek_rate);
90
91 void fdc_catch_stray_interrupts(int count)
92 {
93         unsigned long flags;
94
95         spin_lock_irqsave(&fdc_io_lock, flags);
96         if (count == 0) {
97                 ft_expected_stray_interrupts = 0;
98         } else {
99                 ft_expected_stray_interrupts += count;
100         }
101         spin_unlock_irqrestore(&fdc_io_lock, flags);
102 }
103
104 /*  Wait during a timeout period for a given FDC status.
105  *  If usecs == 0 then just test status, else wait at least for usecs.
106  *  Returns -ETIME on timeout. Function must be calibrated first !
107  */
108 static int fdc_wait(unsigned int usecs, __u8 mask, __u8 state)
109 {
110         int count_1 = (fdc_calibr_count * usecs +
111                        fdc_calibr_count - 1) / fdc_calibr_time;
112
113         do {
114                 fdc_status = inb_p(fdc.msr);
115                 if ((fdc_status & mask) == state) {
116                         return 0;
117                 }
118         } while (count_1-- >= 0);
119         return -ETIME;
120 }
121
122 int fdc_ready_wait(unsigned int usecs)
123 {
124         return fdc_wait(usecs, FDC_DATA_READY | FDC_BUSY, FDC_DATA_READY);
125 }
126
127 /* Why can't we just use udelay()?
128  */
129 static void fdc_usec_wait(unsigned int usecs)
130 {
131         fdc_wait(usecs, 0, 1);  /* will always timeout ! */
132 }
133
134 static int fdc_ready_out_wait(unsigned int usecs)
135 {
136         fdc_usec_wait(FT_RQM_DELAY);    /* wait for valid RQM status */
137         return fdc_wait(usecs, FDC_DATA_OUT_READY, FDC_DATA_OUT_READY);
138 }
139
140 void fdc_wait_calibrate(void)
141 {
142         ftape_calibrate("fdc_wait",
143                         fdc_usec_wait, &fdc_calibr_count, &fdc_calibr_time); 
144 }
145
146 /*  Wait for a (short) while for the FDC to become ready
147  *  and transfer the next command byte.
148  *  Return -ETIME on timeout on getting ready (depends on hardware!).
149  */
150 static int fdc_write(const __u8 data)
151 {
152         fdc_usec_wait(FT_RQM_DELAY);    /* wait for valid RQM status */
153         if (fdc_wait(150, FDC_DATA_READY_MASK, FDC_DATA_IN_READY) < 0) {
154                 return -ETIME;
155         } else {
156                 outb(data, fdc.fifo);
157                 return 0;
158         }
159 }
160
161 /*  Wait for a (short) while for the FDC to become ready
162  *  and transfer the next result byte.
163  *  Return -ETIME if timeout on getting ready (depends on hardware!).
164  */
165 static int fdc_read(__u8 * data)
166 {
167         fdc_usec_wait(FT_RQM_DELAY);    /* wait for valid RQM status */
168         if (fdc_wait(150, FDC_DATA_READY_MASK, FDC_DATA_OUT_READY) < 0) {
169                 return -ETIME;
170         } else {
171                 *data = inb(fdc.fifo);
172                 return 0;
173         }
174 }
175
176 /*  Output a cmd_len long command string to the FDC.
177  *  The FDC should be ready to receive a new command or
178  *  an error (EBUSY or ETIME) will occur.
179  */
180 int fdc_command(const __u8 * cmd_data, int cmd_len)
181 {
182         int result = 0;
183         unsigned long flags;
184         int count = cmd_len;
185         int retry = 0;
186 #ifdef TESTING
187         static unsigned int last_time;
188         unsigned int time;
189 #endif
190         TRACE_FUN(ft_t_any);
191
192         fdc_usec_wait(FT_RQM_DELAY);    /* wait for valid RQM status */
193         spin_lock_irqsave(&fdc_io_lock, flags);
194         if (!in_interrupt())
195                 /* Yes, I know, too much comments inside this function
196                  * ...
197                  * 
198                  * Yet another bug in the original driver. All that
199                  * havoc is caused by the fact that the isr() sends
200                  * itself a command to the floppy tape driver (pause,
201                  * micro step pause).  Now, the problem is that
202                  * commands are transmitted via the fdc_seek
203                  * command. But: the fdc performs seeks in the
204                  * background i.e. it doesn't signal busy while
205                  * sending the step pulses to the drive. Therefore the
206                  * non-interrupt level driver has no chance to tell
207                  * whether the isr() just has issued a seek. Therefore
208                  * we HAVE TO have a look at the ft_hide_interrupt
209                  * flag: it signals the non-interrupt level part of
210                  * the driver that it has to wait for the fdc until it
211                  * has completet seeking.
212                  *
213                  * THIS WAS PRESUMABLY THE REASON FOR ALL THAT
214                  * "fdc_read timeout" errors, I HOPE :-)
215                  */
216                 if (ft_hide_interrupt) {
217                         restore_flags(flags);
218                         TRACE(ft_t_info,
219                               "Waiting for the isr() completing fdc_seek()");
220                         if (fdc_interrupt_wait(2 * FT_SECOND) < 0) {
221                                 TRACE(ft_t_warn,
222                       "Warning: timeout waiting for isr() seek to complete");
223                         }
224                         if (ft_hide_interrupt || !ft_seek_completed) {
225                                 /* There cannot be another
226                                  * interrupt. The isr() only stops
227                                  * the tape and the next interrupt
228                                  * won't come until we have send our
229                                  * command to the drive.
230                                  */
231                                 TRACE_ABORT(-EIO, ft_t_bug,
232                                             "BUG? isr() is still seeking?\n"
233                                             KERN_INFO "hide: %d\n"
234                                             KERN_INFO "seek: %d",
235                                             ft_hide_interrupt,
236                                             ft_seek_completed);
237
238                         }
239                         fdc_usec_wait(FT_RQM_DELAY);    /* wait for valid RQM status */
240                         spin_lock_irqsave(&fdc_io_lock, flags);
241                 }
242         fdc_status = inb(fdc.msr);
243         if ((fdc_status & FDC_DATA_READY_MASK) != FDC_DATA_IN_READY) {
244                 spin_unlock_irqrestore(&fdc_io_lock, flags);
245                 TRACE_ABORT(-EBUSY, ft_t_err, "fdc not ready");
246         } 
247         fdc_mode = *cmd_data;   /* used by isr */
248 #ifdef TESTING
249         if (fdc_mode == FDC_SEEK) {
250                 time = ftape_timediff(last_time, ftape_timestamp());
251                 if (time < 6000) {
252         TRACE(ft_t_bug,"Warning: short timeout between seek commands: %d",
253               time);
254                 }
255         }
256 #endif
257         if (!in_interrupt()) {
258                 /* shouldn't be cleared if called from isr
259                  */
260                 ft_interrupt_seen = 0;
261         }
262         while (count) {
263                 result = fdc_write(*cmd_data);
264                 if (result < 0) {
265                         TRACE(ft_t_fdc_dma,
266                               "fdc_mode = %02x, status = %02x at index %d",
267                               (int) fdc_mode, (int) fdc_status,
268                               cmd_len - count);
269                         if (++retry <= 3) {
270                                 TRACE(ft_t_warn, "fdc_write timeout, retry");
271                         } else {
272                                 TRACE(ft_t_err, "fdc_write timeout, fatal");
273                                 /* recover ??? */
274                                 break;
275                         }
276                 } else {
277                         --count;
278                         ++cmd_data;
279                 }
280         }
281 #ifdef TESTING
282         if (fdc_mode == FDC_SEEK) {
283                 last_time = ftape_timestamp();
284         }
285 #endif
286         spin_unlock_irqrestore(&fdc_io_lock, flags);
287         TRACE_EXIT result;
288 }
289
290 /*  Input a res_len long result string from the FDC.
291  *  The FDC should be ready to send the result or an error
292  *  (EBUSY or ETIME) will occur.
293  */
294 int fdc_result(__u8 * res_data, int res_len)
295 {
296         int result = 0;
297         unsigned long flags;
298         int count = res_len;
299         int retry = 0;
300         TRACE_FUN(ft_t_any);
301
302         spin_lock_irqsave(&fdc_io_lock, flags);
303         fdc_status = inb(fdc.msr);
304         if ((fdc_status & FDC_DATA_READY_MASK) != FDC_DATA_OUT_READY) {
305                 TRACE(ft_t_err, "fdc not ready");
306                 result = -EBUSY;
307         } else while (count) {
308                 if (!(fdc_status & FDC_BUSY)) {
309                         spin_unlock_irqrestore(&fdc_io_lock, flags);
310                         TRACE_ABORT(-EIO, ft_t_err, "premature end of result phase");
311                 }
312                 result = fdc_read(res_data);
313                 if (result < 0) {
314                         TRACE(ft_t_fdc_dma,
315                               "fdc_mode = %02x, status = %02x at index %d",
316                               (int) fdc_mode,
317                               (int) fdc_status,
318                               res_len - count);
319                         if (++retry <= 3) {
320                                 TRACE(ft_t_warn, "fdc_read timeout, retry");
321                         } else {
322                                 TRACE(ft_t_err, "fdc_read timeout, fatal");
323                                 /* recover ??? */
324                                 break;
325                                 ++retry;
326                         }
327                 } else {
328                         --count;
329                         ++res_data;
330                 }
331         }
332         spin_unlock_irqrestore(&fdc_io_lock, flags);
333         fdc_usec_wait(FT_RQM_DELAY);    /* allow FDC to negate BSY */
334         TRACE_EXIT result;
335 }
336
337 /*      Handle command and result phases for
338  *      commands without data phase.
339  */
340 static int fdc_issue_command(const __u8 * out_data, int out_count,
341                       __u8 * in_data, int in_count)
342 {
343         TRACE_FUN(ft_t_any);
344
345         if (out_count > 0) {
346                 TRACE_CATCH(fdc_command(out_data, out_count),);
347         }
348         /* will take 24 - 30 usec for fdc_sense_drive_status and
349          * fdc_sense_interrupt_status commands.
350          *    35 fails sometimes (5/9/93 SJL)
351          * On a loaded system it incidentally takes longer than
352          * this for the fdc to get ready ! ?????? WHY ??????
353          * So until we know what's going on use a very long timeout.
354          */
355         TRACE_CATCH(fdc_ready_out_wait(500 /* usec */),);
356         if (in_count > 0) {
357                 TRACE_CATCH(fdc_result(in_data, in_count),
358                             TRACE(ft_t_err, "result phase aborted"));
359         }
360         TRACE_EXIT 0;
361 }
362
363 /*      Wait for FDC interrupt with timeout (in milliseconds).
364  *      Signals are blocked so the wait will not be aborted.
365  *      Note: interrupts must be enabled ! (23/05/93 SJL)
366  */
367 int fdc_interrupt_wait(unsigned int time)
368 {
369         DECLARE_WAITQUEUE(wait,current);
370         sigset_t old_sigmask;   
371         static int resetting;
372         long timeout;
373
374         TRACE_FUN(ft_t_fdc_dma);
375
376         if (waitqueue_active(&ftape_wait_intr)) {
377                 TRACE_ABORT(-EIO, ft_t_err, "error: nested call");
378         }
379         /* timeout time will be up to USPT microseconds too long ! */
380         timeout = (1000 * time + FT_USPT - 1) / FT_USPT;
381
382         spin_lock_irq(&current->sighand->siglock);
383         old_sigmask = current->blocked;
384         sigfillset(&current->blocked);
385         recalc_sigpending();
386         spin_unlock_irq(&current->sighand->siglock);
387
388         set_current_state(TASK_INTERRUPTIBLE);
389         add_wait_queue(&ftape_wait_intr, &wait);
390         while (!ft_interrupt_seen && (current->state == TASK_INTERRUPTIBLE)) {
391                 timeout = schedule_timeout(timeout);
392         }
393
394         spin_lock_irq(&current->sighand->siglock);
395         current->blocked = old_sigmask;
396         recalc_sigpending();
397         spin_unlock_irq(&current->sighand->siglock);
398         
399         remove_wait_queue(&ftape_wait_intr, &wait);
400         /*  the following IS necessary. True: as well
401          *  wake_up_interruptible() as the schedule() set TASK_RUNNING
402          *  when they wakeup a task, BUT: it may very well be that
403          *  ft_interrupt_seen is already set to 1 when we enter here
404          *  in which case schedule() gets never called, and
405          *  TASK_RUNNING never set. This has the funny effect that we
406          *  execute all the code until we leave kernel space, but then
407          *  the task is stopped (a task CANNOT be preempted while in
408          *  kernel mode. Sending a pair of SIGSTOP/SIGCONT to the
409          *  tasks wakes it up again. Funny! :-)
410          */
411         current->state = TASK_RUNNING; 
412         if (ft_interrupt_seen) { /* woken up by interrupt */
413                 ft_interrupt_seen = 0;
414                 TRACE_EXIT 0;
415         }
416         /*  Original comment:
417          *  In first instance, next statement seems unnecessary since
418          *  it will be cleared in fdc_command. However, a small part of
419          *  the software seems to rely on this being cleared here
420          *  (ftape_close might fail) so stick to it until things get fixed !
421          */
422         /*  My deeply sought of knowledge:
423          *  Behold NO! It is obvious. fdc_reset() doesn't call fdc_command()
424          *  but nevertheless uses fdc_interrupt_wait(). OF COURSE this needs to
425          *  be reset here.
426          */
427         ft_interrupt_seen = 0;  /* clear for next call */
428         if (!resetting) {
429                 resetting = 1;  /* break infinite recursion if reset fails */
430                 TRACE(ft_t_any, "cleanup reset");
431                 fdc_reset();
432                 resetting = 0;
433         }
434         TRACE_EXIT (signal_pending(current)) ? -EINTR : -ETIME;
435 }
436
437 /*      Start/stop drive motor. Enable DMA mode.
438  */
439 void fdc_motor(int motor)
440 {
441         int unit = ft_drive_sel;
442         int data = unit | FDC_RESET_NOT | FDC_DMA_MODE;
443         TRACE_FUN(ft_t_any);
444
445         ftape_motor = motor;
446         if (ftape_motor) {
447                 data |= FDC_MOTOR_0 << unit;
448                 TRACE(ft_t_noise, "turning motor %d on", unit);
449         } else {
450                 TRACE(ft_t_noise, "turning motor %d off", unit);
451         }
452         if (ft_mach2) {
453                 outb_p(data, fdc.dor2);
454         } else {
455                 outb_p(data, fdc.dor);
456         }
457         ftape_sleep(10 * FT_MILLISECOND);
458         TRACE_EXIT;
459 }
460
461 static void fdc_update_dsr(void)
462 {
463         TRACE_FUN(ft_t_any);
464
465         TRACE(ft_t_flow, "rate = %d Kbps, precomp = %d ns",
466               fdc_data_rate, fdc_precomp);
467         if (fdc.type >= i82077) {
468                 outb_p((fdc_rate_code & 0x03) | fdc_prec_code, fdc.dsr);
469         } else {
470                 outb_p(fdc_rate_code & 0x03, fdc.ccr);
471         }
472         TRACE_EXIT;
473 }
474
475 void fdc_set_write_precomp(int precomp)
476 {
477         TRACE_FUN(ft_t_any);
478
479         TRACE(ft_t_noise, "New precomp: %d nsec", precomp);
480         fdc_precomp = precomp;
481         /*  write precompensation can be set in multiples of 41.67 nsec.
482          *  round the parameter to the nearest multiple and convert it
483          *  into a fdc setting. Note that 0 means default to the fdc,
484          *  7 is used instead of that.
485          */
486         fdc_prec_code = ((fdc_precomp + 21) / 42) << 2;
487         if (fdc_prec_code == 0 || fdc_prec_code > (6 << 2)) {
488                 fdc_prec_code = 7 << 2;
489         }
490         fdc_update_dsr();
491         TRACE_EXIT;
492 }
493
494 /*  Reprogram the 82078 registers to use Data Rate Table 1 on all drives.
495  */
496 static void fdc_set_drive_specs(void)
497 {
498         __u8 cmd[] = { FDC_DRIVE_SPEC, 0x00, 0x00, 0x00, 0x00, 0xc0};
499         int result;
500         TRACE_FUN(ft_t_any);
501
502         TRACE(ft_t_flow, "Setting of drive specs called");
503         if (fdc.type >= i82078_1) {
504                 cmd[1] = (0 << 5) | (2 << 2);
505                 cmd[2] = (1 << 5) | (2 << 2);
506                 cmd[3] = (2 << 5) | (2 << 2);
507                 cmd[4] = (3 << 5) | (2 << 2);
508                 result = fdc_command(cmd, NR_ITEMS(cmd));
509                 if (result < 0) {
510                         TRACE(ft_t_err, "Setting of drive specs failed");
511                 }
512         }
513         TRACE_EXIT;
514 }
515
516 /* Select clock for fdc, must correspond with tape drive setting !
517  * This also influences the fdc timing so we must adjust some values.
518  */
519 int fdc_set_data_rate(int rate)
520 {
521         int bad_rate = 0;
522         TRACE_FUN(ft_t_any);
523
524         /* Select clock for fdc, must correspond with tape drive setting !
525          * This also influences the fdc timing so we must adjust some values.
526          */
527         TRACE(ft_t_fdc_dma, "new rate = %d", rate);
528         switch (rate) {
529         case 250:
530                 fdc_rate_code = fdc_data_rate_250;
531                 break;
532         case 500:
533                 fdc_rate_code = fdc_data_rate_500;
534                 break;
535         case 1000:
536                 if (fdc.type < i82077) {
537                         bad_rate = 1;
538                 } else {
539                         fdc_rate_code = fdc_data_rate_1000;
540                 }
541                 break;
542         case 2000:
543                 if (fdc.type < i82078_1) {
544                         bad_rate = 1;
545                 } else {
546                         fdc_rate_code = fdc_data_rate_2000;
547                 }
548                 break;
549         default:
550                 bad_rate = 1;
551         }
552         if (bad_rate) {
553                 TRACE_ABORT(-EIO,
554                             ft_t_fdc_dma, "%d is not a valid data rate", rate);
555         }
556         fdc_data_rate = rate;
557         fdc_update_dsr();
558         fdc_set_seek_rate(fdc_seek_rate);  /* clock changed! */
559         ftape_udelay(1000);
560         TRACE_EXIT 0;
561 }
562
563 /*  keep the unit select if keep_select is != 0,
564  */
565 static void fdc_dor_reset(int keep_select)
566 {
567         __u8 fdc_ctl = ft_drive_sel;
568
569         if (keep_select != 0) {
570                 fdc_ctl |= FDC_DMA_MODE;
571                 if (ftape_motor) {
572                         fdc_ctl |= FDC_MOTOR_0 << ft_drive_sel;
573                 }
574         }
575         ftape_udelay(10); /* ??? but seems to be necessary */
576         if (ft_mach2) {
577                 outb_p(fdc_ctl & 0x0f, fdc.dor);
578                 outb_p(fdc_ctl, fdc.dor2);
579         } else {
580                 outb_p(fdc_ctl, fdc.dor);
581         }
582         fdc_usec_wait(10); /* delay >= 14 fdc clocks */
583         if (keep_select == 0) {
584                 fdc_ctl = 0;
585         }
586         fdc_ctl |= FDC_RESET_NOT;
587         if (ft_mach2) {
588                 outb_p(fdc_ctl & 0x0f, fdc.dor);
589                 outb_p(fdc_ctl, fdc.dor2);
590         } else {
591                 outb_p(fdc_ctl, fdc.dor);
592         }
593 }
594
595 /*      Reset the floppy disk controller. Leave the ftape_unit selected.
596  */
597 void fdc_reset(void)
598 {
599         int st0;
600         int i;
601         int dummy;
602         unsigned long flags;
603         TRACE_FUN(ft_t_any);
604
605         spin_lock_irqsave(&fdc_io_lock, flags);
606
607         fdc_dor_reset(1); /* keep unit selected */
608
609         fdc_mode = fdc_idle;
610
611         /*  maybe the cli()/sti() pair is not necessary, BUT:
612          *  the following line MUST be here. Otherwise fdc_interrupt_wait()
613          *  won't wait. Note that fdc_reset() is called from 
614          *  ftape_dumb_stop() when the fdc is busy transferring data. In this
615          *  case fdc_isr() MOST PROBABLY sets ft_interrupt_seen, and tries
616          *  to get the result bytes from the fdc etc. CLASH.
617          */
618         ft_interrupt_seen = 0;
619         
620         /*  Program data rate
621          */
622         fdc_update_dsr();               /* restore data rate and precomp */
623
624         spin_unlock_irqrestore(&fdc_io_lock, flags);
625
626         /*
627          *      Wait for first polling cycle to complete
628          */
629         if (fdc_interrupt_wait(1 * FT_SECOND) < 0) {
630                 TRACE(ft_t_err, "no drive polling interrupt!");
631         } else {        /* clear all disk-changed statuses */
632                 for (i = 0; i < 4; ++i) {
633                         if(fdc_sense_interrupt_status(&st0, &dummy) != 0) {
634                                 TRACE(ft_t_err, "sense failed for %d", i);
635                         }
636                         if (i == ft_drive_sel) {
637                                 ftape_current_cylinder = dummy;
638                         }
639                 }
640                 TRACE(ft_t_noise, "drive polling completed");
641         }
642         /*
643          *      SPECIFY COMMAND
644          */
645         fdc_set_seek_rate(fdc_seek_rate);
646         /*
647          *      DRIVE SPECIFICATION COMMAND (if fdc type known)
648          */
649         if (fdc.type >= i82078_1) {
650                 fdc_set_drive_specs();
651         }
652         TRACE_EXIT;
653 }
654
655 #if !defined(CLK_48MHZ)
656 # define CLK_48MHZ 1
657 #endif
658
659 /*  When we're done, put the fdc into reset mode so that the regular
660  *  floppy disk driver will figure out that something is wrong and
661  *  initialize the controller the way it wants.
662  */
663 void fdc_disable(void)
664 {
665         __u8 cmd1[] = {FDC_CONFIGURE, 0x00, 0x00, 0x00};
666         __u8 cmd2[] = {FDC_LOCK};
667         __u8 cmd3[] = {FDC_UNLOCK};
668         __u8 stat[1];
669         TRACE_FUN(ft_t_flow);
670
671         if (!fdc_fifo_locked) {
672                 fdc_reset();
673                 TRACE_EXIT;
674         }
675         if (fdc_issue_command(cmd3, 1, stat, 1) < 0 || stat[0] != 0x00) {
676                 fdc_dor_reset(0);
677                 TRACE_ABORT(/**/, ft_t_bug, 
678                 "couldn't unlock fifo, configuration remains changed");
679         }
680         fdc_fifo_locked = 0;
681         if (CLK_48MHZ && fdc.type >= i82078) {
682                 cmd1[0] |= FDC_CLK48_BIT;
683         }
684         cmd1[2] = ((fdc_fifo_state) ? 0 : 0x20) + (fdc_fifo_thr - 1);
685         if (fdc_command(cmd1, NR_ITEMS(cmd1)) < 0) {
686                 fdc_dor_reset(0);
687                 TRACE_ABORT(/**/, ft_t_bug,
688                 "couldn't reconfigure fifo to old state");
689         }
690         if (fdc_lock_state &&
691             fdc_issue_command(cmd2, 1, stat, 1) < 0) {
692                 fdc_dor_reset(0);
693                 TRACE_ABORT(/**/, ft_t_bug, "couldn't lock old state again");
694         }
695         TRACE(ft_t_noise, "fifo restored: %sabled, thr. %d, %slocked",
696               fdc_fifo_state ? "en" : "dis",
697               fdc_fifo_thr, (fdc_lock_state) ? "" : "not ");
698         fdc_dor_reset(0);
699         TRACE_EXIT;
700 }
701
702 /*      Specify FDC seek-rate (milliseconds)
703  */
704 static int fdc_set_seek_rate(int seek_rate)
705 {
706         /* set step rate, dma mode, and minimal head load and unload times
707          */
708         __u8 in[3] = { FDC_SPECIFY, 1, (1 << 1)};
709  
710         fdc_seek_rate = seek_rate;
711         in[1] |= (16 - (fdc_data_rate * fdc_seek_rate) / 500) << 4;
712
713         return fdc_command(in, 3);
714 }
715
716 /*      Sense drive status: get unit's drive status (ST3)
717  */
718 int fdc_sense_drive_status(int *st3)
719 {
720         __u8 out[2];
721         __u8 in[1];
722         TRACE_FUN(ft_t_any);
723
724         out[0] = FDC_SENSED;
725         out[1] = ft_drive_sel;
726         TRACE_CATCH(fdc_issue_command(out, 2, in, 1),);
727         *st3 = in[0];
728         TRACE_EXIT 0;
729 }
730
731 /*      Sense Interrupt Status command:
732  *      should be issued at the end of each seek.
733  *      get ST0 and current cylinder.
734  */
735 int fdc_sense_interrupt_status(int *st0, int *current_cylinder)
736 {
737         __u8 out[1];
738         __u8 in[2];
739         TRACE_FUN(ft_t_any);
740
741         out[0] = FDC_SENSEI;
742         TRACE_CATCH(fdc_issue_command(out, 1, in, 2),);
743         *st0 = in[0];
744         *current_cylinder = in[1];
745         TRACE_EXIT 0;
746 }
747
748 /*      step to track
749  */
750 int fdc_seek(int track)
751 {
752         __u8 out[3];
753         int st0, pcn;
754 #ifdef TESTING
755         unsigned int time;
756 #endif
757         TRACE_FUN(ft_t_any);
758
759         out[0] = FDC_SEEK;
760         out[1] = ft_drive_sel;
761         out[2] = track;
762 #ifdef TESTING
763         time = ftape_timestamp();
764 #endif
765         /*  We really need this command to work !
766          */
767         ft_seek_completed = 0;
768         TRACE_CATCH(fdc_command(out, 3),
769                     fdc_reset();
770                     TRACE(ft_t_noise, "destination was: %d, resetting FDC...",
771                           track));
772         /*    Handle interrupts until ft_seek_completed or timeout.
773          */
774         for (;;) {
775                 TRACE_CATCH(fdc_interrupt_wait(2 * FT_SECOND),);
776                 if (ft_seek_completed) {
777                         TRACE_CATCH(fdc_sense_interrupt_status(&st0, &pcn),);
778                         if ((st0 & ST0_SEEK_END) == 0) {
779                                 TRACE_ABORT(-EIO, ft_t_err,
780                                       "no seek-end after seek completion !??");
781                         }
782                         break;
783                 }
784         }
785 #ifdef TESTING
786         time = ftape_timediff(time, ftape_timestamp()) / abs(track - ftape_current_cylinder);
787         if ((time < 900 || time > 3100) && abs(track - ftape_current_cylinder) > 5) {
788                 TRACE(ft_t_warn, "Wrong FDC STEP interval: %d usecs (%d)",
789                          time, track - ftape_current_cylinder);
790         }
791 #endif
792         /*    Verify whether we issued the right tape command.
793          */
794         /* Verify that we seek to the proper track. */
795         if (pcn != track) {
796                 TRACE_ABORT(-EIO, ft_t_err, "bad seek..");
797         }
798         ftape_current_cylinder = track;
799         TRACE_EXIT 0;
800 }
801
802 static int perpend_mode; /* set if fdc is in perpendicular mode */
803
804 static int perpend_off(void)
805 {
806         __u8 perpend[] = {FDC_PERPEND, 0x00};
807         TRACE_FUN(ft_t_any);
808         
809         if (perpend_mode) {
810                 /* Turn off perpendicular mode */
811                 perpend[1] = 0x80;
812                 TRACE_CATCH(fdc_command(perpend, 2),
813                             TRACE(ft_t_err,"Perpendicular mode exit failed!"));
814                 perpend_mode = 0;
815         }
816         TRACE_EXIT 0;
817 }
818
819 static int handle_perpend(int segment_id)
820 {
821         __u8 perpend[] = {FDC_PERPEND, 0x00};
822         TRACE_FUN(ft_t_any);
823
824         /* When writing QIC-3020 tapes, turn on perpendicular mode
825          * if tape is moving in forward direction (even tracks).
826          */
827         if (ft_qic_std == QIC_TAPE_QIC3020 &&
828             ((segment_id / ft_segments_per_track) & 1) == 0) {
829 /*  FIXME: some i82077 seem to support perpendicular mode as
830  *  well. 
831  */
832 #if 0
833                 if (fdc.type < i82077AA) {}
834 #else
835                 if (fdc.type < i82077 && ft_data_rate < 1000) {
836 #endif
837                         /*  fdc does not support perpendicular mode: complain 
838                          */
839                         TRACE_ABORT(-EIO, ft_t_err,
840                                     "Your FDC does not support QIC-3020.");
841                 }
842                 perpend[1] = 0x03 /* 0x83 + (0x4 << ft_drive_sel) */ ;
843                 TRACE_CATCH(fdc_command(perpend, 2),
844                            TRACE(ft_t_err,"Perpendicular mode entry failed!"));
845                 TRACE(ft_t_flow, "Perpendicular mode set");
846                 perpend_mode = 1;
847                 TRACE_EXIT 0;
848         }
849         TRACE_EXIT perpend_off();
850 }
851
852 static inline void fdc_setup_dma(char mode,
853                                  volatile void *addr, unsigned int count)
854 {
855         /* Program the DMA controller.
856          */
857         disable_dma(fdc.dma);
858         clear_dma_ff(fdc.dma);
859         set_dma_mode(fdc.dma, mode);
860         set_dma_addr(fdc.dma, virt_to_bus((void*)addr));
861         set_dma_count(fdc.dma, count);
862         enable_dma(fdc.dma);
863 }
864
865 /*  Setup fdc and dma for formatting the next segment
866  */
867 int fdc_setup_formatting(buffer_struct * buff)
868 {
869         unsigned long flags;
870         __u8 out[6] = {
871                 FDC_FORMAT, 0x00, 3, 4 * FT_SECTORS_PER_SEGMENT, 0x00, 0x6b
872         };
873         TRACE_FUN(ft_t_any);
874         
875         TRACE_CATCH(handle_perpend(buff->segment_id),);
876         /* Program the DMA controller.
877          */
878         TRACE(ft_t_fdc_dma,
879               "phys. addr. = %lx", virt_to_bus((void*) buff->ptr));
880         spin_lock_irqsave(&fdc_io_lock, flags);
881         fdc_setup_dma(DMA_MODE_WRITE, buff->ptr, FT_SECTORS_PER_SEGMENT * 4);
882         /* Issue FDC command to start reading/writing.
883          */
884         out[1] = ft_drive_sel;
885         out[4] = buff->gap3;
886         TRACE_CATCH(fdc_setup_error = fdc_command(out, sizeof(out)),
887                     restore_flags(flags); fdc_mode = fdc_idle);
888         spin_unlock_irqrestore(&fdc_io_lock, flags);
889         TRACE_EXIT 0;
890 }
891
892
893 /*      Setup Floppy Disk Controller and DMA to read or write the next cluster
894  *      of good sectors from or to the current segment.
895  */
896 int fdc_setup_read_write(buffer_struct * buff, __u8 operation)
897 {
898         unsigned long flags;
899         __u8 out[9];
900         int dma_mode;
901         TRACE_FUN(ft_t_any);
902
903         switch(operation) {
904         case FDC_VERIFY:
905                 if (fdc.type < i82077) {
906                         operation = FDC_READ;
907                 }
908         case FDC_READ:
909         case FDC_READ_DELETED:
910                 dma_mode = DMA_MODE_READ;
911                 TRACE(ft_t_fdc_dma, "xfer %d sectors to 0x%p",
912                       buff->sector_count, buff->ptr);
913                 TRACE_CATCH(perpend_off(),);
914                 break;
915         case FDC_WRITE_DELETED:
916                 TRACE(ft_t_noise, "deleting segment %d", buff->segment_id);
917         case FDC_WRITE:
918                 dma_mode = DMA_MODE_WRITE;
919                 /* When writing QIC-3020 tapes, turn on perpendicular mode
920                  * if tape is moving in forward direction (even tracks).
921                  */
922                 TRACE_CATCH(handle_perpend(buff->segment_id),);
923                 TRACE(ft_t_fdc_dma, "xfer %d sectors from 0x%p",
924                       buff->sector_count, buff->ptr);
925                 break;
926         default:
927                 TRACE_ABORT(-EIO,
928                             ft_t_bug, "bug: invalid operation parameter");
929         }
930         TRACE(ft_t_fdc_dma, "phys. addr. = %lx",virt_to_bus((void*)buff->ptr));
931         spin_lock_irqsave(&fdc_io_lock, flags);
932         if (operation != FDC_VERIFY) {
933                 fdc_setup_dma(dma_mode, buff->ptr,
934                               FT_SECTOR_SIZE * buff->sector_count);
935         }
936         /* Issue FDC command to start reading/writing.
937          */
938         out[0] = operation;
939         out[1] = ft_drive_sel;
940         out[2] = buff->cyl;
941         out[3] = buff->head;
942         out[4] = buff->sect + buff->sector_offset;
943         out[5] = 3;             /* Sector size of 1K. */
944         out[6] = out[4] + buff->sector_count - 1;       /* last sector */
945         out[7] = 109;           /* Gap length. */
946         out[8] = 0xff;          /* No limit to transfer size. */
947         TRACE(ft_t_fdc_dma, "C: 0x%02x, H: 0x%02x, R: 0x%02x, cnt: 0x%02x",
948                 out[2], out[3], out[4], out[6] - out[4] + 1);
949         spin_unlock_irqrestore(&fdc_io_lock, flags);
950         TRACE_CATCH(fdc_setup_error = fdc_command(out, 9),fdc_mode = fdc_idle);
951         TRACE_EXIT 0;
952 }
953
954 int fdc_fifo_threshold(__u8 threshold,
955                        int *fifo_state, int *lock_state, int *fifo_thr)
956 {
957         const __u8 cmd0[] = {FDC_DUMPREGS};
958         __u8 cmd1[] = {FDC_CONFIGURE, 0, (0x0f & (threshold - 1)), 0};
959         const __u8 cmd2[] = {FDC_LOCK};
960         const __u8 cmd3[] = {FDC_UNLOCK};
961         __u8 reg[10];
962         __u8 stat;
963         int i;
964         int result;
965         TRACE_FUN(ft_t_any);
966
967         if (CLK_48MHZ && fdc.type >= i82078) {
968                 cmd1[0] |= FDC_CLK48_BIT;
969         }
970         /*  Dump fdc internal registers for examination
971          */
972         TRACE_CATCH(fdc_command(cmd0, NR_ITEMS(cmd0)),
973                     TRACE(ft_t_warn, "dumpreg cmd failed, fifo unchanged"));
974         /*  Now read fdc internal registers from fifo
975          */
976         for (i = 0; i < (int)NR_ITEMS(reg); ++i) {
977                 fdc_read(&reg[i]);
978                 TRACE(ft_t_fdc_dma, "Register %d = 0x%02x", i, reg[i]);
979         }
980         if (fifo_state && lock_state && fifo_thr) {
981                 *fifo_state = (reg[8] & 0x20) == 0;
982                 *lock_state = reg[7] & 0x80;
983                 *fifo_thr = 1 + (reg[8] & 0x0f);
984         }
985         TRACE(ft_t_noise,
986               "original fifo state: %sabled, threshold %d, %slocked",
987               ((reg[8] & 0x20) == 0) ? "en" : "dis",
988               1 + (reg[8] & 0x0f), (reg[7] & 0x80) ? "" : "not ");
989         /*  If fdc is already locked, unlock it first ! */
990         if (reg[7] & 0x80) {
991                 fdc_ready_wait(100);
992                 TRACE_CATCH(fdc_issue_command(cmd3, NR_ITEMS(cmd3), &stat, 1),
993                             TRACE(ft_t_bug, "FDC unlock command failed, "
994                                   "configuration unchanged"));
995         }
996         fdc_fifo_locked = 0;
997         /*  Enable fifo and set threshold at xx bytes to allow a
998          *  reasonably large latency and reduce number of dma bursts.
999          */
1000         fdc_ready_wait(100);
1001         if ((result = fdc_command(cmd1, NR_ITEMS(cmd1))) < 0) {
1002                 TRACE(ft_t_bug, "configure cmd failed, fifo unchanged");
1003         }
1004         /*  Now lock configuration so reset will not change it
1005          */
1006         if(fdc_issue_command(cmd2, NR_ITEMS(cmd2), &stat, 1) < 0 ||
1007            stat != 0x10) {
1008                 TRACE_ABORT(-EIO, ft_t_bug,
1009                             "FDC lock command failed, stat = 0x%02x", stat);
1010         }
1011         fdc_fifo_locked = 1;
1012         TRACE_EXIT result;
1013 }
1014
1015 static int fdc_fifo_enable(void)
1016 {
1017         TRACE_FUN(ft_t_any);
1018
1019         if (fdc_fifo_locked) {
1020                 TRACE_ABORT(0, ft_t_warn, "Fifo not enabled because locked");
1021         }
1022         TRACE_CATCH(fdc_fifo_threshold(ft_fdc_threshold /* bytes */,
1023                                        &fdc_fifo_state,
1024                                        &fdc_lock_state,
1025                                        &fdc_fifo_thr),);
1026         TRACE_CATCH(fdc_fifo_threshold(ft_fdc_threshold /* bytes */,
1027                                        NULL, NULL, NULL),);
1028         TRACE_EXIT 0;
1029 }
1030
1031 /*   Determine fd controller type 
1032  */
1033 static __u8 fdc_save_state[2];
1034
1035 static int fdc_probe(void)
1036 {
1037         __u8 cmd[1];
1038         __u8 stat[16]; /* must be able to hold dumpregs & save results */
1039         int i;
1040         TRACE_FUN(ft_t_any);
1041
1042         /*  Try to find out what kind of fd controller we have to deal with
1043          *  Scheme borrowed from floppy driver:
1044          *  first try if FDC_DUMPREGS command works
1045          *  (this indicates that we have a 82072 or better)
1046          *  then try the FDC_VERSION command (82072 doesn't support this)
1047          *  then try the FDC_UNLOCK command (some older 82077's don't support this)
1048          *  then try the FDC_PARTID command (82078's support this)
1049          */
1050         cmd[0] = FDC_DUMPREGS;
1051         if (fdc_issue_command(cmd, 1, stat, 1) != 0) {
1052                 TRACE_ABORT(no_fdc, ft_t_bug, "No FDC found");
1053         }
1054         if (stat[0] == 0x80) {
1055                 /* invalid command: must be pre 82072 */
1056                 TRACE_ABORT(i8272,
1057                             ft_t_warn, "Type 8272A/765A compatible FDC found");
1058         }
1059         fdc_result(&stat[1], 9);
1060         fdc_save_state[0] = stat[7];
1061         fdc_save_state[1] = stat[8];
1062         cmd[0] = FDC_VERSION;
1063         if (fdc_issue_command(cmd, 1, stat, 1) < 0 || stat[0] == 0x80) {
1064                 TRACE_ABORT(i8272, ft_t_warn, "Type 82072 FDC found");
1065         }
1066         if (*stat != 0x90) {
1067                 TRACE_ABORT(i8272, ft_t_warn, "Unknown FDC found");
1068         }
1069         cmd[0] = FDC_UNLOCK;
1070         if(fdc_issue_command(cmd, 1, stat, 1) < 0 || stat[0] != 0x00) {
1071                 TRACE_ABORT(i8272, ft_t_warn,
1072                             "Type pre-1991 82077 FDC found, "
1073                             "treating it like a 82072");
1074         }
1075         if (fdc_save_state[0] & 0x80) { /* was locked */
1076                 cmd[0] = FDC_LOCK; /* restore lock */
1077                 (void)fdc_issue_command(cmd, 1, stat, 1);
1078                 TRACE(ft_t_warn, "FDC is already locked");
1079         }
1080         /* Test for a i82078 FDC */
1081         cmd[0] = FDC_PARTID;
1082         if (fdc_issue_command(cmd, 1, stat, 1) < 0 || stat[0] == 0x80) {
1083                 /* invalid command: not a i82078xx type FDC */
1084                 for (i = 0; i < 4; ++i) {
1085                         outb_p(i, fdc.tdr);
1086                         if ((inb_p(fdc.tdr) & 0x03) != i) {
1087                                 TRACE_ABORT(i82077,
1088                                             ft_t_warn, "Type 82077 FDC found");
1089                         }
1090                 }
1091                 TRACE_ABORT(i82077AA, ft_t_warn, "Type 82077AA FDC found");
1092         }
1093         /* FDC_PARTID cmd succeeded */
1094         switch (stat[0] >> 5) {
1095         case 0x0:
1096                 /* i82078SL or i82078-1.  The SL part cannot run at
1097                  * 2Mbps (the SL and -1 dies are identical; they are
1098                  * speed graded after production, according to Intel).
1099                  * Some SL's can be detected by doing a SAVE cmd and
1100                  * look at bit 7 of the first byte (the SEL3V# bit).
1101                  * If it is 0, the part runs off 3Volts, and hence it
1102                  * is a SL.
1103                  */
1104                 cmd[0] = FDC_SAVE;
1105                 if(fdc_issue_command(cmd, 1, stat, 16) < 0) {
1106                         TRACE(ft_t_err, "FDC_SAVE failed. Dunno why");
1107                         /* guess we better claim the fdc to be a i82078 */
1108                         TRACE_ABORT(i82078,
1109                                     ft_t_warn,
1110                                     "Type i82078 FDC (i suppose) found");
1111                 }
1112                 if ((stat[0] & FDC_SEL3V_BIT)) {
1113                         /* fdc running off 5Volts; Pray that it's a i82078-1
1114                          */
1115                         TRACE_ABORT(i82078_1, ft_t_warn,
1116                                   "Type i82078-1 or 5Volt i82078SL FDC found");
1117                 }
1118                 TRACE_ABORT(i82078, ft_t_warn,
1119                             "Type 3Volt i82078SL FDC (1Mbps) found");
1120         case 0x1:
1121         case 0x2: /* S82078B  */
1122                 /* The '78B  isn't '78 compatible.  Detect it as a '77AA */
1123                 TRACE_ABORT(i82077AA, ft_t_warn, "Type i82077AA FDC found");
1124         case 0x3: /* NSC PC8744 core; used in several super-IO chips */
1125                 TRACE_ABORT(i82077AA,
1126                             ft_t_warn, "Type 82077AA compatible FDC found");
1127         default:
1128                 TRACE(ft_t_warn, "A previously undetected FDC found");
1129                 TRACE_ABORT(i82077AA, ft_t_warn,
1130                           "Treating it as a 82077AA. Please report partid= %d",
1131                             stat[0]);
1132         } /* switch(stat[ 0] >> 5) */
1133         TRACE_EXIT no_fdc;
1134 }
1135
1136 static int fdc_request_regions(void)
1137 {
1138         TRACE_FUN(ft_t_flow);
1139
1140         if (ft_mach2 || ft_probe_fc10) {
1141                 if (!request_region(fdc.sra, 8, "fdc (ft)")) {
1142 #ifndef BROKEN_FLOPPY_DRIVER
1143                         TRACE_EXIT -EBUSY;
1144 #else
1145                         TRACE(ft_t_warn,
1146 "address 0x%03x occupied (by floppy driver?), using it anyway", fdc.sra);
1147 #endif
1148                 }
1149         } else {
1150                 if (!request_region(fdc.sra, 6, "fdc (ft)")) {
1151 #ifndef BROKEN_FLOPPY_DRIVER
1152                         TRACE_EXIT -EBUSY;
1153 #else
1154                         TRACE(ft_t_warn,
1155 "address 0x%03x occupied (by floppy driver?), using it anyway", fdc.sra);
1156 #endif
1157                 }
1158                 if (!request_region(fdc.sra + 7, 1, "fdc (ft)")) {
1159 #ifndef BROKEN_FLOPPY_DRIVER
1160                         release_region(fdc.sra, 6);
1161                         TRACE_EXIT -EBUSY;
1162 #else
1163                         TRACE(ft_t_warn,
1164 "address 0x%03x occupied (by floppy driver?), using it anyway", fdc.sra + 7);
1165 #endif
1166                 }
1167         }
1168         TRACE_EXIT 0;
1169 }
1170
1171 void fdc_release_regions(void)
1172 {
1173         TRACE_FUN(ft_t_flow);
1174
1175         if (fdc.sra != 0) {
1176                 if (fdc.dor2 != 0) {
1177                         release_region(fdc.sra, 8);
1178                 } else {
1179                         release_region(fdc.sra, 6);
1180                         release_region(fdc.dir, 1);
1181                 }
1182         }
1183         TRACE_EXIT;
1184 }
1185
1186 static int fdc_config_regs(unsigned int fdc_base, 
1187                            unsigned int fdc_irq, 
1188                            unsigned int fdc_dma)
1189 {
1190         TRACE_FUN(ft_t_flow);
1191
1192         fdc.irq = fdc_irq;
1193         fdc.dma = fdc_dma;
1194         fdc.sra = fdc_base;
1195         fdc.srb = fdc_base + 1;
1196         fdc.dor = fdc_base + 2;
1197         fdc.tdr = fdc_base + 3;
1198         fdc.msr = fdc.dsr = fdc_base + 4;
1199         fdc.fifo = fdc_base + 5;
1200         fdc.dir = fdc.ccr = fdc_base + 7;
1201         fdc.dor2 = (ft_mach2 || ft_probe_fc10) ? fdc_base + 6 : 0;
1202         TRACE_CATCH(fdc_request_regions(), fdc.sra = 0);
1203         TRACE_EXIT 0;
1204 }
1205
1206 static int fdc_config(void)
1207 {
1208         static int already_done;
1209         TRACE_FUN(ft_t_any);
1210
1211         if (already_done) {
1212                 TRACE_CATCH(fdc_request_regions(),);
1213                 *(fdc.hook) = fdc_isr;  /* hook our handler in */
1214                 TRACE_EXIT 0;
1215         }
1216         if (ft_probe_fc10) {
1217                 int fc_type;
1218                 
1219                 TRACE_CATCH(fdc_config_regs(ft_fdc_base,
1220                                             ft_fdc_irq, ft_fdc_dma),);
1221                 fc_type = fc10_enable();
1222                 if (fc_type != 0) {
1223                         TRACE(ft_t_warn, "FC-%c0 controller found", '0' + fc_type);
1224                         fdc.type = fc10;
1225                         fdc.hook = &do_ftape;
1226                         *(fdc.hook) = fdc_isr;  /* hook our handler in */
1227                         already_done = 1;
1228                         TRACE_EXIT 0;
1229                 } else {
1230                         TRACE(ft_t_warn, "FC-10/20 controller not found");
1231                         fdc_release_regions();
1232                         fdc.type = no_fdc;
1233                         ft_probe_fc10 = 0;
1234                         ft_fdc_base   = 0x3f0;
1235                         ft_fdc_irq    = 6;
1236                         ft_fdc_dma    = 2;
1237                 }
1238         }
1239         TRACE(ft_t_warn, "fdc base: 0x%x, irq: %d, dma: %d", 
1240               ft_fdc_base, ft_fdc_irq, ft_fdc_dma);
1241         TRACE_CATCH(fdc_config_regs(ft_fdc_base, ft_fdc_irq, ft_fdc_dma),);
1242         fdc.hook = &do_ftape;
1243         *(fdc.hook) = fdc_isr;  /* hook our handler in */
1244         already_done = 1;
1245         TRACE_EXIT 0;
1246 }
1247
1248 static irqreturn_t ftape_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1249 {
1250         void (*handler) (void) = *fdc.hook;
1251         int handled = 0;
1252         TRACE_FUN(ft_t_any);
1253
1254         *fdc.hook = NULL;
1255         if (handler) {
1256                 handled = 1;
1257                 handler();
1258         } else {
1259                 TRACE(ft_t_bug, "Unexpected ftape interrupt");
1260         }
1261         TRACE_EXIT IRQ_RETVAL(handled);
1262 }
1263
1264 static int fdc_grab_irq_and_dma(void)
1265 {
1266         TRACE_FUN(ft_t_any);
1267
1268         if (fdc.hook == &do_ftape) {
1269                 /*  Get fast interrupt handler.
1270                  */
1271                 if (request_irq(fdc.irq, ftape_interrupt,
1272                                 SA_INTERRUPT, "ft", ftape_id)) {
1273                         TRACE_ABORT(-EIO, ft_t_bug,
1274                                     "Unable to grab IRQ%d for ftape driver",
1275                                     fdc.irq);
1276                 }
1277                 if (request_dma(fdc.dma, ftape_id)) {
1278                         free_irq(fdc.irq, ftape_id);
1279                         TRACE_ABORT(-EIO, ft_t_bug,
1280                               "Unable to grab DMA%d for ftape driver",
1281                               fdc.dma);
1282                 }
1283         }
1284         if (ft_fdc_base != 0x3f0 && (ft_fdc_dma == 2 || ft_fdc_irq == 6)) {
1285                 /* Using same dma channel or irq as standard fdc, need
1286                  * to disable the dma-gate on the std fdc. This
1287                  * couldn't be done in the floppy driver as some
1288                  * laptops are using the dma-gate to enter a low power
1289                  * or even suspended state :-(
1290                  */
1291                 outb_p(FDC_RESET_NOT, 0x3f2);
1292                 TRACE(ft_t_noise, "DMA-gate on standard fdc disabled");
1293         }
1294         TRACE_EXIT 0;
1295 }
1296
1297 int fdc_release_irq_and_dma(void)
1298 {
1299         TRACE_FUN(ft_t_any);
1300
1301         if (fdc.hook == &do_ftape) {
1302                 disable_dma(fdc.dma);   /* just in case... */
1303                 free_dma(fdc.dma);
1304                 free_irq(fdc.irq, ftape_id);
1305         }
1306         if (ft_fdc_base != 0x3f0 && (ft_fdc_dma == 2 || ft_fdc_irq == 6)) {
1307                 /* Using same dma channel as standard fdc, need to
1308                  * disable the dma-gate on the std fdc. This couldn't
1309                  * be done in the floppy driver as some laptops are
1310                  * using the dma-gate to enter a low power or even
1311                  * suspended state :-(
1312                  */
1313                 outb_p(FDC_RESET_NOT | FDC_DMA_MODE, 0x3f2);
1314                 TRACE(ft_t_noise, "DMA-gate on standard fdc enabled again");
1315         }
1316         TRACE_EXIT 0;
1317 }
1318
1319 int fdc_init(void)
1320 {
1321         TRACE_FUN(ft_t_any);
1322
1323         /* find a FDC to use */
1324         TRACE_CATCH(fdc_config(),);
1325         TRACE_CATCH(fdc_grab_irq_and_dma(), fdc_release_regions());
1326         ftape_motor = 0;
1327         fdc_catch_stray_interrupts(0);  /* clear number of awainted
1328                                          * stray interrupte 
1329                                          */
1330         fdc_catch_stray_interrupts(1);  /* one always comes (?) */
1331         TRACE(ft_t_flow, "resetting fdc");
1332         fdc_set_seek_rate(2);           /* use nominal QIC step rate */
1333         fdc_reset();                    /* init fdc & clear track counters */
1334         if (fdc.type == no_fdc) {       /* no FC-10 or FC-20 found */
1335                 fdc.type = fdc_probe();
1336                 fdc_reset();            /* update with new knowledge */
1337         }
1338         if (fdc.type == no_fdc) {
1339                 fdc_release_irq_and_dma();
1340                 fdc_release_regions();
1341                 TRACE_EXIT -ENXIO;
1342         }
1343         if (fdc.type >= i82077) {
1344                 if (fdc_fifo_enable() < 0) {
1345                         TRACE(ft_t_warn, "couldn't enable fdc fifo !");
1346                 } else {
1347                         TRACE(ft_t_flow, "fdc fifo enabled and locked");
1348                 }
1349         }
1350         TRACE_EXIT 0;
1351 }