Linux 2.1.127
[davej-history.git] / drivers / char / ftape / lowlevel / fdc-io.c
blobc126d05a838439d1454aa4c86faf779e4387f6fb
1 /*
2 * Copyright (C) 1993-1996 Bas Laarhoven,
3 * (C) 1996-1997 Claus-Justus Heine.
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.
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.
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.
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 $
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.
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/version.h>
34 #include <linux/interrupt.h>
35 #include <asm/system.h>
36 #include <asm/io.h>
37 #include <asm/dma.h>
38 #include <asm/irq.h>
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"
51 /* Global vars.
53 int ftape_motor = 0;
54 volatile int ftape_current_cylinder = -1;
55 volatile fdc_mode_enum fdc_mode = fdc_idle;
56 fdc_config_info fdc = {0};
57 struct wait_queue *ftape_wait_intr = NULL;
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;
67 /* Local vars.
69 static unsigned int fdc_calibr_count;
70 static unsigned int fdc_calibr_time;
71 static int fdc_status;
72 volatile __u8 fdc_head; /* FDC head from sector id */
73 volatile __u8 fdc_cyl; /* FDC track from sector id */
74 volatile __u8 fdc_sect; /* FDC sector from sector id */
75 static int fdc_data_rate = 500; /* data rate (Kbps) */
76 static int fdc_rate_code = 0; /* data rate code (0 == 500 Kbps) */
77 static int fdc_seek_rate = 2; /* step rate (msec) */
78 static void (*do_ftape) (void);
79 static int fdc_fifo_state; /* original fifo setting - fifo enabled */
80 static int fdc_fifo_thr; /* original fifo setting - threshold */
81 static int fdc_lock_state; /* original lock setting - locked */
82 static int fdc_fifo_locked = 0; /* has fifo && lock set ? */
83 static __u8 fdc_precomp = 0; /* default precomp. value (nsec) */
84 static __u8 fdc_prec_code = 0; /* fdc precomp. select code */
86 static char ftape_id[] = "ftape"; /* used by request irq and free irq */
88 void fdc_catch_stray_interrupts(int count)
90 unsigned long flags;
92 save_flags(flags);
93 cli();
94 if (count == 0) {
95 ft_expected_stray_interrupts = 0;
96 } else {
97 ft_expected_stray_interrupts += count;
99 restore_flags(flags);
102 /* Wait during a timeout period for a given FDC status.
103 * If usecs == 0 then just test status, else wait at least for usecs.
104 * Returns -ETIME on timeout. Function must be calibrated first !
106 int fdc_wait(unsigned int usecs, __u8 mask, __u8 state)
108 int count_1 = (fdc_calibr_count * usecs +
109 fdc_calibr_count - 1) / fdc_calibr_time;
111 do {
112 fdc_status = inb_p(fdc.msr);
113 if ((fdc_status & mask) == state) {
114 return 0;
116 } while (count_1-- >= 0);
117 return -ETIME;
120 int fdc_ready_wait(unsigned int usecs)
122 return fdc_wait(usecs, FDC_DATA_READY | FDC_BUSY, FDC_DATA_READY);
125 /* Why can't we just use udelay()?
127 static void fdc_usec_wait(unsigned int usecs)
129 fdc_wait(usecs, 0, 1); /* will always timeout ! */
132 int fdc_ready_out_wait(unsigned int usecs)
134 fdc_usec_wait(FT_RQM_DELAY); /* wait for valid RQM status */
135 return fdc_wait(usecs, FDC_DATA_OUT_READY, FDC_DATA_OUT_READY);
138 int fdc_ready_in_wait(unsigned int usecs)
140 fdc_usec_wait(FT_RQM_DELAY); /* wait for valid RQM status */
141 return fdc_wait(usecs, FDC_DATA_OUT_READY, FDC_DATA_IN_READY);
144 void fdc_wait_calibrate(void)
146 ftape_calibrate("fdc_wait",
147 fdc_usec_wait, &fdc_calibr_count, &fdc_calibr_time);
150 /* Wait for a (short) while for the FDC to become ready
151 * and transfer the next command byte.
152 * Return -ETIME on timeout on getting ready (depends on hardware!).
154 static int fdc_write(const __u8 data)
156 fdc_usec_wait(FT_RQM_DELAY); /* wait for valid RQM status */
157 if (fdc_wait(150, FDC_DATA_READY_MASK, FDC_DATA_IN_READY) < 0) {
158 return -ETIME;
159 } else {
160 outb(data, fdc.fifo);
161 return 0;
165 /* Wait for a (short) while for the FDC to become ready
166 * and transfer the next result byte.
167 * Return -ETIME if timeout on getting ready (depends on hardware!).
169 static int fdc_read(__u8 * data)
171 fdc_usec_wait(FT_RQM_DELAY); /* wait for valid RQM status */
172 if (fdc_wait(150, FDC_DATA_READY_MASK, FDC_DATA_OUT_READY) < 0) {
173 return -ETIME;
174 } else {
175 *data = inb(fdc.fifo);
176 return 0;
180 /* Output a cmd_len long command string to the FDC.
181 * The FDC should be ready to receive a new command or
182 * an error (EBUSY or ETIME) will occur.
184 int fdc_command(const __u8 * cmd_data, int cmd_len)
186 int result = 0;
187 unsigned long flags;
188 int count = cmd_len;
189 int retry = 0;
190 #ifdef TESTING
191 static unsigned int last_time = 0;
192 unsigned int time;
193 #endif
194 TRACE_FUN(ft_t_any);
196 fdc_usec_wait(FT_RQM_DELAY); /* wait for valid RQM status */
197 save_flags(flags);
198 cli();
199 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,30)
200 if (!in_interrupt())
201 #else
202 if (!intr_count)
203 #endif
204 /* Yes, I know, too much comments inside this function
205 * ...
207 * Yet another bug in the original driver. All that
208 * havoc is caused by the fact that the isr() sends
209 * itself a command to the floppy tape driver (pause,
210 * micro step pause). Now, the problem is that
211 * commands are transmitted via the fdc_seek
212 * command. But: the fdc performs seeks in the
213 * background i.e. it doesn't signal busy while
214 * sending the step pulses to the drive. Therefore the
215 * non-interrupt level driver has no chance to tell
216 * whether the isr() just has issued a seek. Therefore
217 * we HAVE TO have a look at the ft_hide_interrupt
218 * flag: it signals the non-interrupt level part of
219 * the driver that it has to wait for the fdc until it
220 * has completet seeking.
222 * THIS WAS PRESUMABLY THE REASON FOR ALL THAT
223 * "fdc_read timeout" errors, I HOPE :-)
225 if (ft_hide_interrupt) {
226 restore_flags(flags);
227 TRACE(ft_t_info,
228 "Waiting for the isr() completing fdc_seek()");
229 if (fdc_interrupt_wait(2 * FT_SECOND) < 0) {
230 TRACE(ft_t_warn,
231 "Warning: timeout waiting for isr() seek to complete");
233 if (ft_hide_interrupt || !ft_seek_completed) {
234 /* There cannot be another
235 * interrupt. The isr() only stops
236 * the tape and the next interrupt
237 * won't come until we have send our
238 * command to the drive.
240 TRACE_ABORT(-EIO, ft_t_bug,
241 "BUG? isr() is still seeking?\n"
242 KERN_INFO "hide: %d\n"
243 KERN_INFO "seek: %d",
244 ft_hide_interrupt,
245 ft_seek_completed);
248 fdc_usec_wait(FT_RQM_DELAY); /* wait for valid RQM status */
249 save_flags(flags);
250 cli();
252 fdc_status = inb(fdc.msr);
253 if ((fdc_status & FDC_DATA_READY_MASK) != FDC_DATA_IN_READY) {
254 restore_flags(flags);
255 TRACE_ABORT(-EBUSY, ft_t_err, "fdc not ready");
257 fdc_mode = *cmd_data; /* used by isr */
258 #ifdef TESTING
259 if (fdc_mode == FDC_SEEK) {
260 time = ftape_timediff(last_time, ftape_timestamp());
261 if (time < 6000) {
262 TRACE(ft_t_bug,"Warning: short timeout between seek commands: %d",
263 time);
266 #endif
267 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,30)
268 if (!in_interrupt()) {
269 /* shouldn't be cleared if called from isr
271 ft_interrupt_seen = 0;
273 #else
274 if (!intr_count) {
275 /* shouldn't be cleared if called from isr
277 ft_interrupt_seen = 0;
279 #endif
280 while (count) {
281 result = fdc_write(*cmd_data);
282 if (result < 0) {
283 TRACE(ft_t_fdc_dma,
284 "fdc_mode = %02x, status = %02x at index %d",
285 (int) fdc_mode, (int) fdc_status,
286 cmd_len - count);
287 if (++retry <= 3) {
288 TRACE(ft_t_warn, "fdc_write timeout, retry");
289 } else {
290 TRACE(ft_t_err, "fdc_write timeout, fatal");
291 /* recover ??? */
292 break;
294 } else {
295 --count;
296 ++cmd_data;
299 #ifdef TESTING
300 if (fdc_mode == FDC_SEEK) {
301 last_time = ftape_timestamp();
303 #endif
304 restore_flags(flags);
305 TRACE_EXIT result;
308 /* Input a res_len long result string from the FDC.
309 * The FDC should be ready to send the result or an error
310 * (EBUSY or ETIME) will occur.
312 int fdc_result(__u8 * res_data, int res_len)
314 int result = 0;
315 unsigned long flags;
316 int count = res_len;
317 int retry = 0;
318 TRACE_FUN(ft_t_any);
320 save_flags(flags);
321 cli();
322 fdc_status = inb(fdc.msr);
323 if ((fdc_status & FDC_DATA_READY_MASK) != FDC_DATA_OUT_READY) {
324 TRACE(ft_t_err, "fdc not ready");
325 result = -EBUSY;
326 } else while (count) {
327 if (!(fdc_status & FDC_BUSY)) {
328 restore_flags(flags);
329 TRACE_ABORT(-EIO, ft_t_err, "premature end of result phase");
331 result = fdc_read(res_data);
332 if (result < 0) {
333 TRACE(ft_t_fdc_dma,
334 "fdc_mode = %02x, status = %02x at index %d",
335 (int) fdc_mode,
336 (int) fdc_status,
337 res_len - count);
338 if (++retry <= 3) {
339 TRACE(ft_t_warn, "fdc_read timeout, retry");
340 } else {
341 TRACE(ft_t_err, "fdc_read timeout, fatal");
342 /* recover ??? */
343 break;
344 ++retry;
346 } else {
347 --count;
348 ++res_data;
351 restore_flags(flags);
352 fdc_usec_wait(FT_RQM_DELAY); /* allow FDC to negate BSY */
353 TRACE_EXIT result;
356 /* Handle command and result phases for
357 * commands without data phase.
359 int fdc_issue_command(const __u8 * out_data, int out_count,
360 __u8 * in_data, int in_count)
362 TRACE_FUN(ft_t_any);
364 if (out_count > 0) {
365 TRACE_CATCH(fdc_command(out_data, out_count),);
367 /* will take 24 - 30 usec for fdc_sense_drive_status and
368 * fdc_sense_interrupt_status commands.
369 * 35 fails sometimes (5/9/93 SJL)
370 * On a loaded system it incidentally takes longer than
371 * this for the fdc to get ready ! ?????? WHY ??????
372 * So until we know what's going on use a very long timeout.
374 TRACE_CATCH(fdc_ready_out_wait(500 /* usec */),);
375 if (in_count > 0) {
376 TRACE_CATCH(fdc_result(in_data, in_count),
377 TRACE(ft_t_err, "result phase aborted"));
379 TRACE_EXIT 0;
382 /* Wait for FDC interrupt with timeout (in milliseconds).
383 * Signals are blocked so the wait will not be aborted.
384 * Note: interrupts must be enabled ! (23/05/93 SJL)
386 int fdc_interrupt_wait(unsigned int time)
388 struct wait_queue wait = {current, NULL};
389 sigset_t old_sigmask;
390 static int resetting = 0;
391 TRACE_FUN(ft_t_fdc_dma);
392 long timeout;
394 #if LINUX_VERSION_CODE >= KERNEL_VER(2,0,16)
395 if (waitqueue_active(&ftape_wait_intr)) {
396 TRACE_ABORT(-EIO, ft_t_err, "error: nested call");
398 #else
399 if (ftape_wait_intr) {
400 TRACE_ABORT(-EIO, ft_t_err, "error: nested call");
402 #endif
403 /* timeout time will be up to USPT microseconds too long ! */
404 timeout = (1000 * time + FT_USPT - 1) / FT_USPT;
406 spin_lock_irq(&current->sigmask_lock);
407 old_sigmask = current->blocked;
408 sigfillset(&current->blocked);
409 recalc_sigpending(current);
410 spin_unlock_irq(&current->sigmask_lock);
412 current->state = TASK_INTERRUPTIBLE;
413 add_wait_queue(&ftape_wait_intr, &wait);
414 while (!ft_interrupt_seen && (current->state == TASK_INTERRUPTIBLE)) {
415 timeout = schedule_timeout(timeout);
418 spin_lock_irq(&current->sigmask_lock);
419 current->blocked = old_sigmask;
420 recalc_sigpending(current);
421 spin_unlock_irq(&current->sigmask_lock);
423 remove_wait_queue(&ftape_wait_intr, &wait);
424 /* the following IS necessary. True: as well
425 * wake_up_interruptible() as the schedule() set TASK_RUNNING
426 * when they wakeup a task, BUT: it may very well be that
427 * ft_interrupt_seen is already set to 1 when we enter here
428 * in which case schedule() gets never called, and
429 * TASK_RUNNING never set. This has the funny effect that we
430 * execute all the code until we leave kernel space, but then
431 * the task is stopped (a task CANNOT be preempted while in
432 * kernel mode. Sending a pair of SIGSTOP/SIGCONT to the
433 * tasks wakes it up again. Funny! :-)
435 current->state = TASK_RUNNING;
436 if (ft_interrupt_seen) { /* woken up by interrupt */
437 ft_interrupt_seen = 0;
438 TRACE_EXIT 0;
440 /* Original comment:
441 * In first instance, next statement seems unnecessary since
442 * it will be cleared in fdc_command. However, a small part of
443 * the software seems to rely on this being cleared here
444 * (ftape_close might fail) so stick to it until things get fixed !
446 /* My deeply sought of knowledge:
447 * Behold NO! It is obvious. fdc_reset() doesn't call fdc_command()
448 * but nevertheless uses fdc_interrupt_wait(). OF COURSE this needs to
449 * be reset here.
451 ft_interrupt_seen = 0; /* clear for next call */
452 if (!resetting) {
453 resetting = 1; /* break infinite recursion if reset fails */
454 TRACE(ft_t_any, "cleanup reset");
455 fdc_reset();
456 resetting = 0;
458 TRACE_EXIT (signal_pending(current)) ? -EINTR : -ETIME;
461 /* Start/stop drive motor. Enable DMA mode.
463 void fdc_motor(int motor)
465 int unit = ft_drive_sel;
466 int data = unit | FDC_RESET_NOT | FDC_DMA_MODE;
467 TRACE_FUN(ft_t_any);
469 ftape_motor = motor;
470 if (ftape_motor) {
471 data |= FDC_MOTOR_0 << unit;
472 TRACE(ft_t_noise, "turning motor %d on", unit);
473 } else {
474 TRACE(ft_t_noise, "turning motor %d off", unit);
476 if (ft_mach2) {
477 outb_p(data, fdc.dor2);
478 } else {
479 outb_p(data, fdc.dor);
481 ftape_sleep(10 * FT_MILLISECOND);
482 TRACE_EXIT;
485 static void fdc_update_dsr(void)
487 TRACE_FUN(ft_t_any);
489 TRACE(ft_t_flow, "rate = %d Kbps, precomp = %d ns",
490 fdc_data_rate, fdc_precomp);
491 if (fdc.type >= i82077) {
492 outb_p((fdc_rate_code & 0x03) | fdc_prec_code, fdc.dsr);
493 } else {
494 outb_p(fdc_rate_code & 0x03, fdc.ccr);
496 TRACE_EXIT;
499 void fdc_set_write_precomp(int precomp)
501 TRACE_FUN(ft_t_any);
503 TRACE(ft_t_noise, "New precomp: %d nsec", precomp);
504 fdc_precomp = precomp;
505 /* write precompensation can be set in multiples of 41.67 nsec.
506 * round the parameter to the nearest multiple and convert it
507 * into a fdc setting. Note that 0 means default to the fdc,
508 * 7 is used instead of that.
510 fdc_prec_code = ((fdc_precomp + 21) / 42) << 2;
511 if (fdc_prec_code == 0 || fdc_prec_code > (6 << 2)) {
512 fdc_prec_code = 7 << 2;
514 fdc_update_dsr();
515 TRACE_EXIT;
518 /* Reprogram the 82078 registers to use Data Rate Table 1 on all drives.
520 void fdc_set_drive_specs(void)
522 __u8 cmd[] = { FDC_DRIVE_SPEC, 0x00, 0x00, 0x00, 0x00, 0xc0};
523 int result;
524 TRACE_FUN(ft_t_any);
526 TRACE(ft_t_flow, "Setting of drive specs called");
527 if (fdc.type >= i82078_1) {
528 cmd[1] = (0 << 5) | (2 << 2);
529 cmd[2] = (1 << 5) | (2 << 2);
530 cmd[3] = (2 << 5) | (2 << 2);
531 cmd[4] = (3 << 5) | (2 << 2);
532 result = fdc_command(cmd, NR_ITEMS(cmd));
533 if (result < 0) {
534 TRACE(ft_t_err, "Setting of drive specs failed");
537 TRACE_EXIT;
540 /* Select clock for fdc, must correspond with tape drive setting !
541 * This also influences the fdc timing so we must adjust some values.
543 int fdc_set_data_rate(int rate)
545 int bad_rate = 0;
546 TRACE_FUN(ft_t_any);
548 /* Select clock for fdc, must correspond with tape drive setting !
549 * This also influences the fdc timing so we must adjust some values.
551 TRACE(ft_t_fdc_dma, "new rate = %d", rate);
552 switch (rate) {
553 case 250:
554 fdc_rate_code = fdc_data_rate_250;
555 break;
556 case 500:
557 fdc_rate_code = fdc_data_rate_500;
558 break;
559 case 1000:
560 if (fdc.type < i82077) {
561 bad_rate = 1;
562 } else {
563 fdc_rate_code = fdc_data_rate_1000;
565 break;
566 case 2000:
567 if (fdc.type < i82078_1) {
568 bad_rate = 1;
569 } else {
570 fdc_rate_code = fdc_data_rate_2000;
572 break;
573 default:
574 bad_rate = 1;
576 if (bad_rate) {
577 TRACE_ABORT(-EIO,
578 ft_t_fdc_dma, "%d is not a valid data rate", rate);
580 fdc_data_rate = rate;
581 fdc_update_dsr();
582 fdc_set_seek_rate(fdc_seek_rate); /* clock changed! */
583 ftape_udelay(1000);
584 TRACE_EXIT 0;
587 /* keep the unit select if keep_select is != 0,
589 static void fdc_dor_reset(int keep_select)
591 __u8 fdc_ctl = ft_drive_sel;
593 if (keep_select != 0) {
594 fdc_ctl |= FDC_DMA_MODE;
595 if (ftape_motor) {
596 fdc_ctl |= FDC_MOTOR_0 << ft_drive_sel;
599 ftape_udelay(10); /* ??? but seems to be necessary */
600 if (ft_mach2) {
601 outb_p(fdc_ctl & 0x0f, fdc.dor);
602 outb_p(fdc_ctl, fdc.dor2);
603 } else {
604 outb_p(fdc_ctl, fdc.dor);
606 fdc_usec_wait(10); /* delay >= 14 fdc clocks */
607 if (keep_select == 0) {
608 fdc_ctl = 0;
610 fdc_ctl |= FDC_RESET_NOT;
611 if (ft_mach2) {
612 outb_p(fdc_ctl & 0x0f, fdc.dor);
613 outb_p(fdc_ctl, fdc.dor2);
614 } else {
615 outb_p(fdc_ctl, fdc.dor);
619 /* Reset the floppy disk controller. Leave the ftape_unit selected.
621 void fdc_reset(void)
623 int st0;
624 int i;
625 int dummy;
626 unsigned long flags;
627 TRACE_FUN(ft_t_any);
629 save_flags(flags);
630 cli();
632 fdc_dor_reset(1); /* keep unit selected */
634 fdc_mode = fdc_idle;
636 /* maybe the cli()/sti() pair is not necessary, BUT:
637 * the following line MUST be here. Otherwise fdc_interrupt_wait()
638 * won't wait. Note that fdc_reset() is called from
639 * ftape_dumb_stop() when the fdc is busy transferring data. In this
640 * case fdc_isr() MOST PROBABLY sets ft_interrupt_seen, and tries
641 * to get the result bytes from the fdc etc. CLASH.
643 ft_interrupt_seen = 0;
645 /* Program data rate
647 fdc_update_dsr(); /* restore data rate and precomp */
649 restore_flags(flags);
652 * Wait for first polling cycle to complete
654 if (fdc_interrupt_wait(1 * FT_SECOND) < 0) {
655 TRACE(ft_t_err, "no drive polling interrupt!");
656 } else { /* clear all disk-changed statuses */
657 for (i = 0; i < 4; ++i) {
658 if(fdc_sense_interrupt_status(&st0, &dummy) != 0) {
659 TRACE(ft_t_err, "sense failed for %d", i);
661 if (i == ft_drive_sel) {
662 ftape_current_cylinder = dummy;
665 TRACE(ft_t_noise, "drive polling completed");
668 * SPECIFY COMMAND
670 fdc_set_seek_rate(fdc_seek_rate);
672 * DRIVE SPECIFICATION COMMAND (if fdc type known)
674 if (fdc.type >= i82078_1) {
675 fdc_set_drive_specs();
677 TRACE_EXIT;
680 #if !defined(CLK_48MHZ)
681 # define CLK_48MHZ 1
682 #endif
684 /* When we're done, put the fdc into reset mode so that the regular
685 * floppy disk driver will figure out that something is wrong and
686 * initialize the controller the way it wants.
688 void fdc_disable(void)
690 __u8 cmd1[] = {FDC_CONFIGURE, 0x00, 0x00, 0x00};
691 __u8 cmd2[] = {FDC_LOCK};
692 __u8 cmd3[] = {FDC_UNLOCK};
693 __u8 stat[1];
694 TRACE_FUN(ft_t_flow);
696 if (!fdc_fifo_locked) {
697 fdc_reset();
698 TRACE_EXIT;
700 if (fdc_issue_command(cmd3, 1, stat, 1) < 0 || stat[0] != 0x00) {
701 fdc_dor_reset(0);
702 TRACE_ABORT(/**/, ft_t_bug,
703 "couldn't unlock fifo, configuration remains changed");
705 fdc_fifo_locked = 0;
706 if (CLK_48MHZ && fdc.type >= i82078) {
707 cmd1[0] |= FDC_CLK48_BIT;
709 cmd1[2] = ((fdc_fifo_state) ? 0 : 0x20) + (fdc_fifo_thr - 1);
710 if (fdc_command(cmd1, NR_ITEMS(cmd1)) < 0) {
711 fdc_dor_reset(0);
712 TRACE_ABORT(/**/, ft_t_bug,
713 "couldn't reconfigure fifo to old state");
715 if (fdc_lock_state &&
716 fdc_issue_command(cmd2, 1, stat, 1) < 0) {
717 fdc_dor_reset(0);
718 TRACE_ABORT(/**/, ft_t_bug, "couldn't lock old state again");
720 TRACE(ft_t_noise, "fifo restored: %sabled, thr. %d, %slocked",
721 fdc_fifo_state ? "en" : "dis",
722 fdc_fifo_thr, (fdc_lock_state) ? "" : "not ");
723 fdc_dor_reset(0);
724 TRACE_EXIT;
727 /* Specify FDC seek-rate (milliseconds)
729 int fdc_set_seek_rate(int seek_rate)
731 /* set step rate, dma mode, and minimal head load and unload times
733 __u8 in[3] = { FDC_SPECIFY, 1, (1 << 1)};
735 fdc_seek_rate = seek_rate;
736 in[1] |= (16 - (fdc_data_rate * fdc_seek_rate) / 500) << 4;
738 return fdc_command(in, 3);
741 /* Sense drive status: get unit's drive status (ST3)
743 int fdc_sense_drive_status(int *st3)
745 __u8 out[2];
746 __u8 in[1];
747 TRACE_FUN(ft_t_any);
749 out[0] = FDC_SENSED;
750 out[1] = ft_drive_sel;
751 TRACE_CATCH(fdc_issue_command(out, 2, in, 1),);
752 *st3 = in[0];
753 TRACE_EXIT 0;
756 /* Sense Interrupt Status command:
757 * should be issued at the end of each seek.
758 * get ST0 and current cylinder.
760 int fdc_sense_interrupt_status(int *st0, int *current_cylinder)
762 __u8 out[1];
763 __u8 in[2];
764 TRACE_FUN(ft_t_any);
766 out[0] = FDC_SENSEI;
767 TRACE_CATCH(fdc_issue_command(out, 1, in, 2),);
768 *st0 = in[0];
769 *current_cylinder = in[1];
770 TRACE_EXIT 0;
773 /* step to track
775 int fdc_seek(int track)
777 __u8 out[3];
778 int st0, pcn;
779 #ifdef TESTING
780 unsigned int time;
781 #endif
782 TRACE_FUN(ft_t_any);
784 out[0] = FDC_SEEK;
785 out[1] = ft_drive_sel;
786 out[2] = track;
787 #ifdef TESTING
788 time = ftape_timestamp();
789 #endif
790 /* We really need this command to work !
792 ft_seek_completed = 0;
793 TRACE_CATCH(fdc_command(out, 3),
794 fdc_reset();
795 TRACE(ft_t_noise, "destination was: %d, resetting FDC...",
796 track));
797 /* Handle interrupts until ft_seek_completed or timeout.
799 for (;;) {
800 TRACE_CATCH(fdc_interrupt_wait(2 * FT_SECOND),);
801 if (ft_seek_completed) {
802 TRACE_CATCH(fdc_sense_interrupt_status(&st0, &pcn),);
803 if ((st0 & ST0_SEEK_END) == 0) {
804 TRACE_ABORT(-EIO, ft_t_err,
805 "no seek-end after seek completion !??");
807 break;
810 #ifdef TESTING
811 time = ftape_timediff(time, ftape_timestamp()) / ABS(track - ftape_current_cylinder);
812 if ((time < 900 || time > 3100) && ABS(track - ftape_current_cylinder) > 5) {
813 TRACE(ft_t_warn, "Wrong FDC STEP interval: %d usecs (%d)",
814 time, track - ftape_current_cylinder);
816 #endif
817 /* Verify whether we issued the right tape command.
819 /* Verify that we seek to the proper track. */
820 if (pcn != track) {
821 TRACE_ABORT(-EIO, ft_t_err, "bad seek..");
823 ftape_current_cylinder = track;
824 TRACE_EXIT 0;
827 /* Recalibrate and wait until home.
829 int fdc_recalibrate(void)
831 __u8 out[2];
832 int st0;
833 int pcn;
834 int retry;
835 int old_seek_rate = fdc_seek_rate;
836 TRACE_FUN(ft_t_any);
838 TRACE_CATCH(fdc_set_seek_rate(6),);
839 out[0] = FDC_RECAL;
840 out[1] = ft_drive_sel;
841 ft_seek_completed = 0;
842 TRACE_CATCH(fdc_command(out, 2),);
843 /* Handle interrupts until ft_seek_completed or timeout.
845 for (retry = 0;; ++retry) {
846 TRACE_CATCH(fdc_interrupt_wait(2 * FT_SECOND),);
847 if (ft_seek_completed) {
848 TRACE_CATCH(fdc_sense_interrupt_status(&st0, &pcn),);
849 if ((st0 & ST0_SEEK_END) == 0) {
850 if (retry < 1) {
851 continue; /* some drives/fdc's
852 * give an extra interrupt
854 } else {
855 TRACE_ABORT(-EIO, ft_t_err,
856 "no seek-end after seek completion !??");
859 break;
862 ftape_current_cylinder = pcn;
863 if (pcn != 0) {
864 TRACE(ft_t_err, "failed: resulting track = %d", pcn);
866 TRACE_CATCH(fdc_set_seek_rate(old_seek_rate),);
867 TRACE_EXIT 0;
870 static int perpend_mode = 0; /* set if fdc is in perpendicular mode */
872 static int perpend_off(void)
874 __u8 perpend[] = {FDC_PERPEND, 0x00};
875 TRACE_FUN(ft_t_any);
877 if (perpend_mode) {
878 /* Turn off perpendicular mode */
879 perpend[1] = 0x80;
880 TRACE_CATCH(fdc_command(perpend, 2),
881 TRACE(ft_t_err,"Perpendicular mode exit failed!"));
882 perpend_mode = 0;
884 TRACE_EXIT 0;
887 static int handle_perpend(int segment_id)
889 __u8 perpend[] = {FDC_PERPEND, 0x00};
890 TRACE_FUN(ft_t_any);
892 /* When writing QIC-3020 tapes, turn on perpendicular mode
893 * if tape is moving in forward direction (even tracks).
895 if (ft_qic_std == QIC_TAPE_QIC3020 &&
896 ((segment_id / ft_segments_per_track) & 1) == 0) {
897 /* FIXME: some i82077 seem to support perpendicular mode as
898 * well.
900 #if 0
901 if (fdc.type < i82077AA) {}
902 #else
903 if (fdc.type < i82077 && ft_data_rate < 1000) {
904 #endif
905 /* fdc does not support perpendicular mode: complain
907 TRACE_ABORT(-EIO, ft_t_err,
908 "Your FDC does not support QIC-3020.");
910 perpend[1] = 0x03 /* 0x83 + (0x4 << ft_drive_sel) */ ;
911 TRACE_CATCH(fdc_command(perpend, 2),
912 TRACE(ft_t_err,"Perpendicular mode entry failed!"));
913 TRACE(ft_t_flow, "Perpendicular mode set");
914 perpend_mode = 1;
915 TRACE_EXIT 0;
917 TRACE_EXIT perpend_off();
920 static inline void fdc_setup_dma(char mode,
921 volatile void *addr, unsigned int count)
923 /* Program the DMA controller.
925 disable_dma(fdc.dma);
926 clear_dma_ff(fdc.dma);
927 set_dma_mode(fdc.dma, mode);
928 set_dma_addr(fdc.dma, virt_to_bus((void*)addr));
929 set_dma_count(fdc.dma, count);
930 #ifdef GCC_2_4_5_BUG
931 /* This seemingly stupid construction confuses the gcc-2.4.5
932 * code generator enough to create correct code.
934 if (1) {
935 int i;
937 for (i = 0; i < 1; ++i) {
938 ftape_udelay(1);
941 #endif
942 enable_dma(fdc.dma);
945 /* Setup fdc and dma for formatting the next segment
947 int fdc_setup_formatting(buffer_struct * buff)
949 unsigned long flags;
950 __u8 out[6] = {
951 FDC_FORMAT, 0x00, 3, 4 * FT_SECTORS_PER_SEGMENT, 0x00, 0x6b
953 TRACE_FUN(ft_t_any);
955 TRACE_CATCH(handle_perpend(buff->segment_id),);
956 /* Program the DMA controller.
958 TRACE(ft_t_fdc_dma,
959 "phys. addr. = %lx", virt_to_bus((void*) buff->ptr));
960 save_flags(flags);
961 cli(); /* could be called from ISR ! */
962 fdc_setup_dma(DMA_MODE_WRITE, buff->ptr, FT_SECTORS_PER_SEGMENT * 4);
963 /* Issue FDC command to start reading/writing.
965 out[1] = ft_drive_sel;
966 out[4] = buff->gap3;
967 TRACE_CATCH(fdc_setup_error = fdc_command(out, sizeof(out)),
968 restore_flags(flags); fdc_mode = fdc_idle);
969 restore_flags(flags);
970 TRACE_EXIT 0;
974 /* Setup Floppy Disk Controller and DMA to read or write the next cluster
975 * of good sectors from or to the current segment.
977 int fdc_setup_read_write(buffer_struct * buff, __u8 operation)
979 unsigned long flags;
980 __u8 out[9];
981 int dma_mode;
982 TRACE_FUN(ft_t_any);
984 switch(operation) {
985 case FDC_VERIFY:
986 if (fdc.type < i82077) {
987 operation = FDC_READ;
989 case FDC_READ:
990 case FDC_READ_DELETED:
991 dma_mode = DMA_MODE_READ;
992 TRACE(ft_t_fdc_dma, "xfer %d sectors to 0x%p",
993 buff->sector_count, buff->ptr);
994 TRACE_CATCH(perpend_off(),);
995 break;
996 case FDC_WRITE_DELETED:
997 TRACE(ft_t_noise, "deleting segment %d", buff->segment_id);
998 case FDC_WRITE:
999 dma_mode = DMA_MODE_WRITE;
1000 /* When writing QIC-3020 tapes, turn on perpendicular mode
1001 * if tape is moving in forward direction (even tracks).
1003 TRACE_CATCH(handle_perpend(buff->segment_id),);
1004 TRACE(ft_t_fdc_dma, "xfer %d sectors from 0x%p",
1005 buff->sector_count, buff->ptr);
1006 break;
1007 default:
1008 TRACE_ABORT(-EIO,
1009 ft_t_bug, "bug: illegal operation parameter");
1011 TRACE(ft_t_fdc_dma, "phys. addr. = %lx",virt_to_bus((void*)buff->ptr));
1012 save_flags(flags);
1013 cli(); /* could be called from ISR ! */
1014 if (operation != FDC_VERIFY) {
1015 fdc_setup_dma(dma_mode, buff->ptr,
1016 FT_SECTOR_SIZE * buff->sector_count);
1018 /* Issue FDC command to start reading/writing.
1020 out[0] = operation;
1021 out[1] = ft_drive_sel;
1022 out[2] = buff->cyl;
1023 out[3] = buff->head;
1024 out[4] = buff->sect + buff->sector_offset;
1025 out[5] = 3; /* Sector size of 1K. */
1026 out[6] = out[4] + buff->sector_count - 1; /* last sector */
1027 out[7] = 109; /* Gap length. */
1028 out[8] = 0xff; /* No limit to transfer size. */
1029 TRACE(ft_t_fdc_dma, "C: 0x%02x, H: 0x%02x, R: 0x%02x, cnt: 0x%02x",
1030 out[2], out[3], out[4], out[6] - out[4] + 1);
1031 restore_flags(flags);
1032 TRACE_CATCH(fdc_setup_error = fdc_command(out, 9),fdc_mode = fdc_idle);
1033 TRACE_EXIT 0;
1036 int fdc_fifo_threshold(__u8 threshold,
1037 int *fifo_state, int *lock_state, int *fifo_thr)
1039 const __u8 cmd0[] = {FDC_DUMPREGS};
1040 __u8 cmd1[] = {FDC_CONFIGURE, 0, (0x0f & (threshold - 1)), 0};
1041 const __u8 cmd2[] = {FDC_LOCK};
1042 const __u8 cmd3[] = {FDC_UNLOCK};
1043 __u8 reg[10];
1044 __u8 stat;
1045 int i;
1046 int result;
1047 TRACE_FUN(ft_t_any);
1049 if (CLK_48MHZ && fdc.type >= i82078) {
1050 cmd1[0] |= FDC_CLK48_BIT;
1052 /* Dump fdc internal registers for examination
1054 TRACE_CATCH(fdc_command(cmd0, NR_ITEMS(cmd0)),
1055 TRACE(ft_t_warn, "dumpreg cmd failed, fifo unchanged"));
1056 /* Now read fdc internal registers from fifo
1058 for (i = 0; i < (int)NR_ITEMS(reg); ++i) {
1059 fdc_read(&reg[i]);
1060 TRACE(ft_t_fdc_dma, "Register %d = 0x%02x", i, reg[i]);
1062 if (fifo_state && lock_state && fifo_thr) {
1063 *fifo_state = (reg[8] & 0x20) == 0;
1064 *lock_state = reg[7] & 0x80;
1065 *fifo_thr = 1 + (reg[8] & 0x0f);
1067 TRACE(ft_t_noise,
1068 "original fifo state: %sabled, threshold %d, %slocked",
1069 ((reg[8] & 0x20) == 0) ? "en" : "dis",
1070 1 + (reg[8] & 0x0f), (reg[7] & 0x80) ? "" : "not ");
1071 /* If fdc is already locked, unlock it first ! */
1072 if (reg[7] & 0x80) {
1073 fdc_ready_wait(100);
1074 TRACE_CATCH(fdc_issue_command(cmd3, NR_ITEMS(cmd3), &stat, 1),
1075 TRACE(ft_t_bug, "FDC unlock command failed, "
1076 "configuration unchanged"));
1078 fdc_fifo_locked = 0;
1079 /* Enable fifo and set threshold at xx bytes to allow a
1080 * reasonably large latency and reduce number of dma bursts.
1082 fdc_ready_wait(100);
1083 if ((result = fdc_command(cmd1, NR_ITEMS(cmd1))) < 0) {
1084 TRACE(ft_t_bug, "configure cmd failed, fifo unchanged");
1086 /* Now lock configuration so reset will not change it
1088 if(fdc_issue_command(cmd2, NR_ITEMS(cmd2), &stat, 1) < 0 ||
1089 stat != 0x10) {
1090 TRACE_ABORT(-EIO, ft_t_bug,
1091 "FDC lock command failed, stat = 0x%02x", stat);
1093 fdc_fifo_locked = 1;
1094 TRACE_EXIT result;
1097 static int fdc_fifo_enable(void)
1099 TRACE_FUN(ft_t_any);
1101 if (fdc_fifo_locked) {
1102 TRACE_ABORT(0, ft_t_warn, "Fifo not enabled because locked");
1104 TRACE_CATCH(fdc_fifo_threshold(ft_fdc_threshold /* bytes */,
1105 &fdc_fifo_state,
1106 &fdc_lock_state,
1107 &fdc_fifo_thr),);
1108 TRACE_CATCH(fdc_fifo_threshold(ft_fdc_threshold /* bytes */,
1109 NULL, NULL, NULL),);
1110 TRACE_EXIT 0;
1113 /* Determine fd controller type
1115 static __u8 fdc_save_state[2] = {0, 0};
1117 int fdc_probe(void)
1119 __u8 cmd[1];
1120 __u8 stat[16]; /* must be able to hold dumpregs & save results */
1121 int i;
1122 TRACE_FUN(ft_t_any);
1124 /* Try to find out what kind of fd controller we have to deal with
1125 * Scheme borrowed from floppy driver:
1126 * first try if FDC_DUMPREGS command works
1127 * (this indicates that we have a 82072 or better)
1128 * then try the FDC_VERSION command (82072 doesn't support this)
1129 * then try the FDC_UNLOCK command (some older 82077's don't support this)
1130 * then try the FDC_PARTID command (82078's support this)
1132 cmd[0] = FDC_DUMPREGS;
1133 if (fdc_issue_command(cmd, 1, stat, 1) != 0) {
1134 TRACE_ABORT(no_fdc, ft_t_bug, "No FDC found");
1136 if (stat[0] == 0x80) {
1137 /* invalid command: must be pre 82072 */
1138 TRACE_ABORT(i8272,
1139 ft_t_warn, "Type 8272A/765A compatible FDC found");
1141 fdc_result(&stat[1], 9);
1142 fdc_save_state[0] = stat[7];
1143 fdc_save_state[1] = stat[8];
1144 cmd[0] = FDC_VERSION;
1145 if (fdc_issue_command(cmd, 1, stat, 1) < 0 || stat[0] == 0x80) {
1146 TRACE_ABORT(i8272, ft_t_warn, "Type 82072 FDC found");
1148 if (*stat != 0x90) {
1149 TRACE_ABORT(i8272, ft_t_warn, "Unknown FDC found");
1151 cmd[0] = FDC_UNLOCK;
1152 if(fdc_issue_command(cmd, 1, stat, 1) < 0 || stat[0] != 0x00) {
1153 TRACE_ABORT(i8272, ft_t_warn,
1154 "Type pre-1991 82077 FDC found, "
1155 "treating it like a 82072");
1157 if (fdc_save_state[0] & 0x80) { /* was locked */
1158 cmd[0] = FDC_LOCK; /* restore lock */
1159 (void)fdc_issue_command(cmd, 1, stat, 1);
1160 TRACE(ft_t_warn, "FDC is already locked");
1162 /* Test for a i82078 FDC */
1163 cmd[0] = FDC_PARTID;
1164 if (fdc_issue_command(cmd, 1, stat, 1) < 0 || stat[0] == 0x80) {
1165 /* invalid command: not a i82078xx type FDC */
1166 for (i = 0; i < 4; ++i) {
1167 outb_p(i, fdc.tdr);
1168 if ((inb_p(fdc.tdr) & 0x03) != i) {
1169 TRACE_ABORT(i82077,
1170 ft_t_warn, "Type 82077 FDC found");
1173 TRACE_ABORT(i82077AA, ft_t_warn, "Type 82077AA FDC found");
1175 /* FDC_PARTID cmd succeeded */
1176 switch (stat[0] >> 5) {
1177 case 0x0:
1178 /* i82078SL or i82078-1. The SL part cannot run at
1179 * 2Mbps (the SL and -1 dies are identical; they are
1180 * speed graded after production, according to Intel).
1181 * Some SL's can be detected by doing a SAVE cmd and
1182 * look at bit 7 of the first byte (the SEL3V# bit).
1183 * If it is 0, the part runs off 3Volts, and hence it
1184 * is a SL.
1186 cmd[0] = FDC_SAVE;
1187 if(fdc_issue_command(cmd, 1, stat, 16) < 0) {
1188 TRACE(ft_t_err, "FDC_SAVE failed. Dunno why");
1189 /* guess we better claim the fdc to be a i82078 */
1190 TRACE_ABORT(i82078,
1191 ft_t_warn,
1192 "Type i82078 FDC (i suppose) found");
1194 if ((stat[0] & FDC_SEL3V_BIT)) {
1195 /* fdc running off 5Volts; Pray that it's a i82078-1
1197 TRACE_ABORT(i82078_1, ft_t_warn,
1198 "Type i82078-1 or 5Volt i82078SL FDC found");
1200 TRACE_ABORT(i82078, ft_t_warn,
1201 "Type 3Volt i82078SL FDC (1Mbps) found");
1202 case 0x1:
1203 case 0x2: /* S82078B */
1204 /* The '78B isn't '78 compatible. Detect it as a '77AA */
1205 TRACE_ABORT(i82077AA, ft_t_warn, "Type i82077AA FDC found");
1206 case 0x3: /* NSC PC8744 core; used in several super-IO chips */
1207 TRACE_ABORT(i82077AA,
1208 ft_t_warn, "Type 82077AA compatible FDC found");
1209 default:
1210 TRACE(ft_t_warn, "A previously undetected FDC found");
1211 TRACE_ABORT(i82077AA, ft_t_warn,
1212 "Treating it as a 82077AA. Please report partid= %d",
1213 stat[0]);
1214 } /* switch(stat[ 0] >> 5) */
1215 TRACE_EXIT no_fdc;
1218 static int fdc_request_regions(void)
1220 TRACE_FUN(ft_t_flow);
1222 if (ft_mach2 || ft_probe_fc10) {
1223 if (check_region(fdc.sra, 8) < 0) {
1224 #ifndef BROKEN_FLOPPY_DRIVER
1225 TRACE_EXIT -EBUSY;
1226 #else
1227 TRACE(ft_t_warn,
1228 "address 0x%03x occupied (by floppy driver?), using it anyway", fdc.sra);
1229 #endif
1231 request_region(fdc.sra, 8, "fdc (ft)");
1232 } else {
1233 if (check_region(fdc.sra, 6) < 0 ||
1234 check_region(fdc.dir, 1) < 0) {
1235 #ifndef BROKEN_FLOPPY_DRIVER
1236 TRACE_EXIT -EBUSY;
1237 #else
1238 TRACE(ft_t_warn,
1239 "address 0x%03x occupied (by floppy driver?), using it anyway", fdc.sra);
1240 #endif
1242 request_region(fdc.sra, 6, "fdc (ft)");
1243 request_region(fdc.sra + 7, 1, "fdc (ft)");
1245 TRACE_EXIT 0;
1248 void fdc_release_regions(void)
1250 TRACE_FUN(ft_t_flow);
1252 if (fdc.sra != 0) {
1253 if (fdc.dor2 != 0) {
1254 release_region(fdc.sra, 8);
1255 } else {
1256 release_region(fdc.sra, 6);
1257 release_region(fdc.dir, 1);
1260 TRACE_EXIT;
1263 static int fdc_config_regs(unsigned int fdc_base,
1264 unsigned int fdc_irq,
1265 unsigned int fdc_dma)
1267 TRACE_FUN(ft_t_flow);
1269 fdc.irq = fdc_irq;
1270 fdc.dma = fdc_dma;
1271 fdc.sra = fdc_base;
1272 fdc.srb = fdc_base + 1;
1273 fdc.dor = fdc_base + 2;
1274 fdc.tdr = fdc_base + 3;
1275 fdc.msr = fdc.dsr = fdc_base + 4;
1276 fdc.fifo = fdc_base + 5;
1277 fdc.dir = fdc.ccr = fdc_base + 7;
1278 fdc.dor2 = (ft_mach2 || ft_probe_fc10) ? fdc_base + 6 : 0;
1279 TRACE_CATCH(fdc_request_regions(), fdc.sra = 0);
1280 TRACE_EXIT 0;
1283 static int fdc_config(void)
1285 static int already_done = 0;
1286 TRACE_FUN(ft_t_any);
1288 if (already_done) {
1289 TRACE_CATCH(fdc_request_regions(),);
1290 *(fdc.hook) = fdc_isr; /* hook our handler in */
1291 TRACE_EXIT 0;
1293 if (ft_probe_fc10) {
1294 int fc_type;
1296 TRACE_CATCH(fdc_config_regs(ft_fdc_base,
1297 ft_fdc_irq, ft_fdc_dma),);
1298 fc_type = fc10_enable();
1299 if (fc_type != 0) {
1300 TRACE(ft_t_warn, "FC-%c0 controller found", '0' + fc_type);
1301 fdc.type = fc10;
1302 fdc.hook = &do_ftape;
1303 *(fdc.hook) = fdc_isr; /* hook our handler in */
1304 already_done = 1;
1305 TRACE_EXIT 0;
1306 } else {
1307 TRACE(ft_t_warn, "FC-10/20 controller not found");
1308 fdc_release_regions();
1309 fdc.type = no_fdc;
1310 ft_probe_fc10 = 0;
1311 ft_fdc_base = 0x3f0;
1312 ft_fdc_irq = 6;
1313 ft_fdc_dma = 2;
1316 TRACE(ft_t_warn, "fdc base: 0x%x, irq: %d, dma: %d",
1317 ft_fdc_base, ft_fdc_irq, ft_fdc_dma);
1318 TRACE_CATCH(fdc_config_regs(ft_fdc_base, ft_fdc_irq, ft_fdc_dma),);
1319 fdc.hook = &do_ftape;
1320 *(fdc.hook) = fdc_isr; /* hook our handler in */
1321 already_done = 1;
1322 TRACE_EXIT 0;
1325 #if LINUX_VERSION_CODE >= KERNEL_VER(1,3,70)
1326 static void ftape_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1327 #else
1328 static void ftape_interrupt(int irq, struct pt_regs *regs)
1329 #endif
1331 void (*handler) (void) = *fdc.hook;
1332 TRACE_FUN(ft_t_any);
1334 *fdc.hook = NULL;
1335 if (handler) {
1336 handler();
1337 } else {
1338 TRACE(ft_t_bug, "Unexpected ftape interrupt");
1340 TRACE_EXIT;
1343 int fdc_grab_irq_and_dma(void)
1345 TRACE_FUN(ft_t_any);
1347 if (fdc.hook == &do_ftape) {
1348 /* Get fast interrupt handler.
1350 #if LINUX_VERSION_CODE >= KERNEL_VER(1,3,70)
1351 if (request_irq(fdc.irq, ftape_interrupt,
1352 SA_INTERRUPT, "ft", ftape_id)) {
1353 TRACE_ABORT(-EIO, ft_t_bug,
1354 "Unable to grab IRQ%d for ftape driver",
1355 fdc.irq);
1357 #else
1358 if (request_irq(fdc.irq, ftape_interrupt, SA_INTERRUPT,
1359 ftape_id)) {
1360 TRACE_ABORT(-EIO, ft_t_bug,
1361 "Unable to grab IRQ%d for ftape driver",
1362 fdc.irq);
1364 #endif
1365 if (request_dma(fdc.dma, ftape_id)) {
1366 #if LINUX_VERSION_CODE >= KERNEL_VER(1,3,70)
1367 free_irq(fdc.irq, ftape_id);
1368 #else
1369 free_irq(fdc.irq);
1370 #endif
1371 TRACE_ABORT(-EIO, ft_t_bug,
1372 "Unable to grab DMA%d for ftape driver",
1373 fdc.dma);
1375 enable_irq(fdc.irq);
1377 if (ft_fdc_base != 0x3f0 && (ft_fdc_dma == 2 || ft_fdc_irq == 6)) {
1378 /* Using same dma channel or irq as standard fdc, need
1379 * to disable the dma-gate on the std fdc. This
1380 * couldn't be done in the floppy driver as some
1381 * laptops are using the dma-gate to enter a low power
1382 * or even suspended state :-(
1384 outb_p(FDC_RESET_NOT, 0x3f2);
1385 TRACE(ft_t_noise, "DMA-gate on standard fdc disabled");
1387 TRACE_EXIT 0;
1390 int fdc_release_irq_and_dma(void)
1392 TRACE_FUN(ft_t_any);
1394 if (fdc.hook == &do_ftape) {
1395 disable_dma(fdc.dma); /* just in case... */
1396 free_dma(fdc.dma);
1397 disable_irq(fdc.irq);
1398 #if LINUX_VERSION_CODE >= KERNEL_VER(1,3,70)
1399 free_irq(fdc.irq, ftape_id);
1400 #else
1401 free_irq(fdc.irq);
1402 #endif
1404 if (ft_fdc_base != 0x3f0 && (ft_fdc_dma == 2 || ft_fdc_irq == 6)) {
1405 /* Using same dma channel as standard fdc, need to
1406 * disable the dma-gate on the std fdc. This couldn't
1407 * be done in the floppy driver as some laptops are
1408 * using the dma-gate to enter a low power or even
1409 * suspended state :-(
1411 outb_p(FDC_RESET_NOT | FDC_DMA_MODE, 0x3f2);
1412 TRACE(ft_t_noise, "DMA-gate on standard fdc enabled again");
1414 TRACE_EXIT 0;
1417 int fdc_init(void)
1419 TRACE_FUN(ft_t_any);
1421 /* find a FDC to use */
1422 TRACE_CATCH(fdc_config(),);
1423 TRACE_CATCH(fdc_grab_irq_and_dma(), fdc_release_regions());
1424 ftape_motor = 0;
1425 fdc_catch_stray_interrupts(0); /* clear number of awainted
1426 * stray interrupte
1428 fdc_catch_stray_interrupts(1); /* one always comes (?) */
1429 TRACE(ft_t_flow, "resetting fdc");
1430 fdc_set_seek_rate(2); /* use nominal QIC step rate */
1431 fdc_reset(); /* init fdc & clear track counters */
1432 if (fdc.type == no_fdc) { /* no FC-10 or FC-20 found */
1433 fdc.type = fdc_probe();
1434 fdc_reset(); /* update with new knowledge */
1436 if (fdc.type == no_fdc) {
1437 fdc_release_irq_and_dma();
1438 fdc_release_regions();
1439 TRACE_EXIT -ENXIO;
1441 if (fdc.type >= i82077) {
1442 if (fdc_fifo_enable() < 0) {
1443 TRACE(ft_t_warn, "couldn't enable fdc fifo !");
1444 } else {
1445 TRACE(ft_t_flow, "fdc fifo enabled and locked");
1448 TRACE_EXIT 0;