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)
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
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>
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"
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
;
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
)
95 ft_expected_stray_interrupts
= 0;
97 ft_expected_stray_interrupts
+= count
;
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
;
112 fdc_status
= inb_p(fdc
.msr
);
113 if ((fdc_status
& mask
) == state
) {
116 } while (count_1
-- >= 0);
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) {
160 outb(data
, fdc
.fifo
);
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) {
175 *data
= inb(fdc
.fifo
);
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
)
191 static unsigned int last_time
= 0;
196 fdc_usec_wait(FT_RQM_DELAY
); /* wait for valid RQM status */
199 #if LINUX_VERSION_CODE >= KERNEL_VER(2,1,30)
204 /* Yes, I know, too much comments inside this function
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
);
228 "Waiting for the isr() completing fdc_seek()");
229 if (fdc_interrupt_wait(2 * FT_SECOND
) < 0) {
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",
248 fdc_usec_wait(FT_RQM_DELAY
); /* wait for valid RQM status */
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 */
259 if (fdc_mode
== FDC_SEEK
) {
260 time
= ftape_timediff(last_time
, ftape_timestamp());
262 TRACE(ft_t_bug
,"Warning: short timeout between seek commands: %d",
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;
275 /* shouldn't be cleared if called from isr
277 ft_interrupt_seen
= 0;
281 result
= fdc_write(*cmd_data
);
284 "fdc_mode = %02x, status = %02x at index %d",
285 (int) fdc_mode
, (int) fdc_status
,
288 TRACE(ft_t_warn
, "fdc_write timeout, retry");
290 TRACE(ft_t_err
, "fdc_write timeout, fatal");
300 if (fdc_mode
== FDC_SEEK
) {
301 last_time
= ftape_timestamp();
304 restore_flags(flags
);
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
)
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");
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
);
334 "fdc_mode = %02x, status = %02x at index %d",
339 TRACE(ft_t_warn
, "fdc_read timeout, retry");
341 TRACE(ft_t_err
, "fdc_read timeout, fatal");
351 restore_flags(flags
);
352 fdc_usec_wait(FT_RQM_DELAY
); /* allow FDC to negate BSY */
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
)
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 */),);
376 TRACE_CATCH(fdc_result(in_data
, in_count
),
377 TRACE(ft_t_err
, "result phase aborted"));
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
);
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");
399 if (ftape_wait_intr
) {
400 TRACE_ABORT(-EIO
, ft_t_err
, "error: nested call");
403 /* timeout time will be up to USPT microseconds too long ! */
404 timeout
= (1000 * time
+ FT_USPT
- 1) / FT_USPT
;
406 spin_lock_irq(¤t
->sigmask_lock
);
407 old_sigmask
= current
->blocked
;
408 sigfillset(¤t
->blocked
);
409 recalc_sigpending(current
);
410 spin_unlock_irq(¤t
->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(¤t
->sigmask_lock
);
419 current
->blocked
= old_sigmask
;
420 recalc_sigpending(current
);
421 spin_unlock_irq(¤t
->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;
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
451 ft_interrupt_seen
= 0; /* clear for next call */
453 resetting
= 1; /* break infinite recursion if reset fails */
454 TRACE(ft_t_any
, "cleanup reset");
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
;
471 data
|= FDC_MOTOR_0
<< unit
;
472 TRACE(ft_t_noise
, "turning motor %d on", unit
);
474 TRACE(ft_t_noise
, "turning motor %d off", unit
);
477 outb_p(data
, fdc
.dor2
);
479 outb_p(data
, fdc
.dor
);
481 ftape_sleep(10 * FT_MILLISECOND
);
485 static void fdc_update_dsr(void)
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
);
494 outb_p(fdc_rate_code
& 0x03, fdc
.ccr
);
499 void fdc_set_write_precomp(int precomp
)
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;
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};
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
));
534 TRACE(ft_t_err
, "Setting of drive specs failed");
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
)
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
);
554 fdc_rate_code
= fdc_data_rate_250
;
557 fdc_rate_code
= fdc_data_rate_500
;
560 if (fdc
.type
< i82077
) {
563 fdc_rate_code
= fdc_data_rate_1000
;
567 if (fdc
.type
< i82078_1
) {
570 fdc_rate_code
= fdc_data_rate_2000
;
578 ft_t_fdc_dma
, "%d is not a valid data rate", rate
);
580 fdc_data_rate
= rate
;
582 fdc_set_seek_rate(fdc_seek_rate
); /* clock changed! */
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
;
596 fdc_ctl
|= FDC_MOTOR_0
<< ft_drive_sel
;
599 ftape_udelay(10); /* ??? but seems to be necessary */
601 outb_p(fdc_ctl
& 0x0f, fdc
.dor
);
602 outb_p(fdc_ctl
, fdc
.dor2
);
604 outb_p(fdc_ctl
, fdc
.dor
);
606 fdc_usec_wait(10); /* delay >= 14 fdc clocks */
607 if (keep_select
== 0) {
610 fdc_ctl
|= FDC_RESET_NOT
;
612 outb_p(fdc_ctl
& 0x0f, fdc
.dor
);
613 outb_p(fdc_ctl
, fdc
.dor2
);
615 outb_p(fdc_ctl
, fdc
.dor
);
619 /* Reset the floppy disk controller. Leave the ftape_unit selected.
632 fdc_dor_reset(1); /* keep unit selected */
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;
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");
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();
680 #if !defined(CLK_48MHZ)
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
};
694 TRACE_FUN(ft_t_flow
);
696 if (!fdc_fifo_locked
) {
700 if (fdc_issue_command(cmd3
, 1, stat
, 1) < 0 || stat
[0] != 0x00) {
702 TRACE_ABORT(/**/, ft_t_bug
,
703 "couldn't unlock fifo, configuration remains changed");
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) {
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) {
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 ");
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
)
750 out
[1] = ft_drive_sel
;
751 TRACE_CATCH(fdc_issue_command(out
, 2, in
, 1),);
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
)
767 TRACE_CATCH(fdc_issue_command(out
, 1, in
, 2),);
769 *current_cylinder
= in
[1];
775 int fdc_seek(int track
)
785 out
[1] = ft_drive_sel
;
788 time
= ftape_timestamp();
790 /* We really need this command to work !
792 ft_seek_completed
= 0;
793 TRACE_CATCH(fdc_command(out
, 3),
795 TRACE(ft_t_noise
, "destination was: %d, resetting FDC...",
797 /* Handle interrupts until ft_seek_completed or timeout.
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 !??");
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
);
817 /* Verify whether we issued the right tape command.
819 /* Verify that we seek to the proper track. */
821 TRACE_ABORT(-EIO
, ft_t_err
, "bad seek..");
823 ftape_current_cylinder
= track
;
827 /* Recalibrate and wait until home.
829 int fdc_recalibrate(void)
835 int old_seek_rate
= fdc_seek_rate
;
838 TRACE_CATCH(fdc_set_seek_rate(6),);
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) {
851 continue; /* some drives/fdc's
852 * give an extra interrupt
855 TRACE_ABORT(-EIO
, ft_t_err
,
856 "no seek-end after seek completion !??");
862 ftape_current_cylinder
= pcn
;
864 TRACE(ft_t_err
, "failed: resulting track = %d", pcn
);
866 TRACE_CATCH(fdc_set_seek_rate(old_seek_rate
),);
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};
878 /* Turn off perpendicular mode */
880 TRACE_CATCH(fdc_command(perpend
, 2),
881 TRACE(ft_t_err
,"Perpendicular mode exit failed!"));
887 static int handle_perpend(int segment_id
)
889 __u8 perpend
[] = {FDC_PERPEND
, 0x00};
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
901 if (fdc
.type
< i82077AA
) {}
903 if (fdc
.type
< i82077
&& ft_data_rate
< 1000) {
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");
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
);
931 /* This seemingly stupid construction confuses the gcc-2.4.5
932 * code generator enough to create correct code.
937 for (i
= 0; i
< 1; ++i
) {
945 /* Setup fdc and dma for formatting the next segment
947 int fdc_setup_formatting(buffer_struct
* buff
)
951 FDC_FORMAT
, 0x00, 3, 4 * FT_SECTORS_PER_SEGMENT
, 0x00, 0x6b
955 TRACE_CATCH(handle_perpend(buff
->segment_id
),);
956 /* Program the DMA controller.
959 "phys. addr. = %lx", virt_to_bus((void*) buff
->ptr
));
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
;
967 TRACE_CATCH(fdc_setup_error
= fdc_command(out
, sizeof(out
)),
968 restore_flags(flags
); fdc_mode
= fdc_idle
);
969 restore_flags(flags
);
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
)
986 if (fdc
.type
< i82077
) {
987 operation
= 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(),);
996 case FDC_WRITE_DELETED
:
997 TRACE(ft_t_noise
, "deleting segment %d", buff
->segment_id
);
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
);
1009 ft_t_bug
, "bug: illegal operation parameter");
1011 TRACE(ft_t_fdc_dma
, "phys. addr. = %lx",virt_to_bus((void*)buff
->ptr
));
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.
1021 out
[1] = ft_drive_sel
;
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
);
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
};
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
) {
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);
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 ||
1090 TRACE_ABORT(-EIO
, ft_t_bug
,
1091 "FDC lock command failed, stat = 0x%02x", stat
);
1093 fdc_fifo_locked
= 1;
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 */,
1108 TRACE_CATCH(fdc_fifo_threshold(ft_fdc_threshold
/* bytes */,
1109 NULL
, NULL
, NULL
),);
1113 /* Determine fd controller type
1115 static __u8 fdc_save_state
[2] = {0, 0};
1120 __u8 stat
[16]; /* must be able to hold dumpregs & save results */
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 */
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
) {
1168 if ((inb_p(fdc
.tdr
) & 0x03) != i
) {
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) {
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
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 */
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");
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");
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",
1214 } /* switch(stat[ 0] >> 5) */
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
1228 "address 0x%03x occupied (by floppy driver?), using it anyway", fdc
.sra
);
1231 request_region(fdc
.sra
, 8, "fdc (ft)");
1233 if (check_region(fdc
.sra
, 6) < 0 ||
1234 check_region(fdc
.dir
, 1) < 0) {
1235 #ifndef BROKEN_FLOPPY_DRIVER
1239 "address 0x%03x occupied (by floppy driver?), using it anyway", fdc
.sra
);
1242 request_region(fdc
.sra
, 6, "fdc (ft)");
1243 request_region(fdc
.sra
+ 7, 1, "fdc (ft)");
1248 void fdc_release_regions(void)
1250 TRACE_FUN(ft_t_flow
);
1253 if (fdc
.dor2
!= 0) {
1254 release_region(fdc
.sra
, 8);
1256 release_region(fdc
.sra
, 6);
1257 release_region(fdc
.dir
, 1);
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
);
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);
1283 static int fdc_config(void)
1285 static int already_done
= 0;
1286 TRACE_FUN(ft_t_any
);
1289 TRACE_CATCH(fdc_request_regions(),);
1290 *(fdc
.hook
) = fdc_isr
; /* hook our handler in */
1293 if (ft_probe_fc10
) {
1296 TRACE_CATCH(fdc_config_regs(ft_fdc_base
,
1297 ft_fdc_irq
, ft_fdc_dma
),);
1298 fc_type
= fc10_enable();
1300 TRACE(ft_t_warn
, "FC-%c0 controller found", '0' + fc_type
);
1302 fdc
.hook
= &do_ftape
;
1303 *(fdc
.hook
) = fdc_isr
; /* hook our handler in */
1307 TRACE(ft_t_warn
, "FC-10/20 controller not found");
1308 fdc_release_regions();
1311 ft_fdc_base
= 0x3f0;
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 */
1325 #if LINUX_VERSION_CODE >= KERNEL_VER(1,3,70)
1326 static void ftape_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
1328 static void ftape_interrupt(int irq
, struct pt_regs
*regs
)
1331 void (*handler
) (void) = *fdc
.hook
;
1332 TRACE_FUN(ft_t_any
);
1338 TRACE(ft_t_bug
, "Unexpected ftape interrupt");
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",
1358 if (request_irq(fdc
.irq
, ftape_interrupt
, SA_INTERRUPT
,
1360 TRACE_ABORT(-EIO
, ft_t_bug
,
1361 "Unable to grab IRQ%d for ftape driver",
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
);
1371 TRACE_ABORT(-EIO
, ft_t_bug
,
1372 "Unable to grab DMA%d for ftape driver",
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");
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... */
1397 disable_irq(fdc
.irq
);
1398 #if LINUX_VERSION_CODE >= KERNEL_VER(1,3,70)
1399 free_irq(fdc
.irq
, ftape_id
);
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");
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());
1425 fdc_catch_stray_interrupts(0); /* clear number of awainted
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();
1441 if (fdc
.type
>= i82077
) {
1442 if (fdc_fifo_enable() < 0) {
1443 TRACE(ft_t_warn
, "couldn't enable fdc fifo !");
1445 TRACE(ft_t_flow
, "fdc fifo enabled and locked");