2 * linux/kernel/floppy.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * 02.12.91 - Changed to static variables to indicate need for reset
9 * and recalibrate. This makes some things easier (output_byte reset
10 * checking etc), and means less interrupt jumping in case of errors,
11 * so the code is hopefully easier to understand.
15 * This file is certainly a mess. I've tried my best to get it working,
16 * but I don't like programming floppies, and I have only one anyway.
17 * Urgel. I should check for more errors, and do more graceful error
18 * recovery. Seems there are problems with several drives. I've tried to
19 * correct them. No promises.
23 * As with hd.c, all routines within this file can (and will) be called
24 * by interrupts, so extreme caution is needed. A hardware interrupt
25 * handler may not sleep, or a kernel panic will happen. Thus I cannot
26 * call "floppy-on" directly, but have to set a special timer interrupt
31 * 28.02.92 - made track-buffering routines, based on the routines written
32 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
36 * Automatic floppy-detection and formatting written by Werner Almesberger
37 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
38 * the floppy-change signal detection.
42 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
43 * FDC data overrun bug, added some preliminary stuff for vertical
46 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
48 * TODO: Errors are still not counted properly.
52 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
53 * modelled after the freeware MS/DOS program fdformat/88 V1.8 by
54 * Christoph H. Hochst\"atter.
55 * I have fixed the shift values to the ones I always use. Maybe a new
56 * ioctl() should be created to be able to modify them.
57 * There is a bug in the driver that makes it impossible to format a
58 * floppy as the first thing after bootup.
62 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
63 * this helped the floppy driver as well. Much cleaner, and still seems to
67 #define REALLY_SLOW_IO
71 #include <linux/sched.h>
73 #include <linux/kernel.h>
74 #include <linux/timer.h>
75 #include <linux/fdreg.h>
77 #include <linux/errno.h>
80 #include <asm/system.h>
82 #include <asm/segment.h>
84 #define MAJOR_NR FLOPPY_MAJOR
87 static unsigned int changed_floppies
= 0, fake_change
= 0;
89 static int initial_reset_flag
= 0;
90 static int need_configure
= 1; /* for 82077 */
91 static int recalibrate
= 0;
93 static int recover
= 0; /* recalibrate immediately after resetting */
96 static unsigned char current_DOR
= 0x0C;
97 static unsigned char running
= 0;
99 #define TYPE(x) ((x)>>2)
100 #define DRIVE(x) ((x)&0x03)
103 * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
104 * max X times - some types of errors increase the errorcount by 2 or
105 * even 3, so we might actually retry only X/2 times before giving up.
107 #define MAX_ERRORS 12
110 * Maximum disk size (in kilobytes). This default is used whenever the
111 * current disk size is unknown.
113 #define MAX_DISK_SIZE 1440
116 * Maximum number of sectors in a track buffer. Track buffering is disabled
117 * if tracks are bigger.
119 #define MAX_BUFFER_SECTORS 18
122 * The DMA channel used by the floppy controller cannot access data at
125 * Went back to the 1MB limit, as some people had problems with the floppy
126 * driver otherwise. It doesn't matter much for performance anyway, as most
127 * floppy accesses go through the track buffer.
129 #define LAST_DMA_ADDR (0x100000 - BLOCK_SIZE)
132 * globals used by 'result()'
134 #define MAX_REPLIES 7
135 static unsigned char reply_buffer
[MAX_REPLIES
];
136 #define ST0 (reply_buffer[0])
137 #define ST1 (reply_buffer[1])
138 #define ST2 (reply_buffer[2])
139 #define ST3 (reply_buffer[3])
142 * This struct defines the different floppy types.
144 * The 'stretch' tells if the tracks need to be doubled for some
145 * types (ie 360kB diskette in 1.2MB drive etc). Others should
146 * be self-explanatory.
148 static struct floppy_struct floppy_type
[] = {
149 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL
}, /* no testing */
150 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,NULL
}, /* 360kB PC diskettes */
151 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,NULL
}, /* 1.2 MB AT-diskettes */
152 { 720, 9,2,40,1,0x2A,0x02,0xDF,0x50,NULL
}, /* 360kB in 720kB drive */
153 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,NULL
}, /* 3.5" 720kB diskette */
154 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,NULL
}, /* 360kB in 1.2MB drive */
155 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,NULL
}, /* 720kB in 1.2MB drive */
156 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL
}, /* 1.44MB diskette */
160 * Auto-detection. Each drive type has a pair of formats which are
161 * used in succession to try to read the disk. If the FDC cannot lock onto
162 * the disk, the next format is tried. This uses the variable 'probing'.
164 static struct floppy_struct floppy_types
[] = {
165 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"360k/PC" }, /* 360kB PC diskettes */
166 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"360k/PC" }, /* 360kB PC diskettes */
167 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"1.2M" }, /* 1.2 MB AT-diskettes */
168 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"360k/AT" }, /* 360kB in 1.2MB drive */
169 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k" }, /* 3.5" 720kB diskette */
170 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k" }, /* 3.5" 720kB diskette */
171 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"1.44M" }, /* 1.44MB diskette */
172 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"720k/AT" }, /* 3.5" 720kB diskette */
175 /* Auto-detection: Disk type used until the next media change occurs. */
176 struct floppy_struct
*current_type
[4] = { NULL
, NULL
, NULL
, NULL
};
178 /* This type is tried first. */
179 struct floppy_struct
*base_type
[4];
182 * User-provided type information. current_type points to
183 * the respective entry of this array.
185 struct floppy_struct user_params
[4];
187 static int floppy_sizes
[] ={
188 MAX_DISK_SIZE
, MAX_DISK_SIZE
, MAX_DISK_SIZE
, MAX_DISK_SIZE
,
199 * The driver is trying to determine the correct media format
200 * while probing is set. rw_interrupt() clears it after a
203 static int probing
= 0;
206 * (User-provided) media information is _not_ discarded after a media change
207 * if the corresponding keep_data flag is non-zero. Positive values are
208 * decremented after each probe.
210 static int keep_data
[4] = { 0,0,0,0 };
213 * Announce successful media type detection and media information loss after
215 * Also used to enable/disable printing of overrun warnings.
217 static ftd_msg
[4] = { 0,0,0,0 };
219 /* Prevent "aliased" accesses. */
221 static fd_ref
[4] = { 0,0,0,0 };
222 static fd_device
[4] = { 0,0,0,0 };
224 /* Synchronization of FDC access. */
225 static volatile int format_status
= FORMAT_NONE
, fdc_busy
= 0;
226 static struct wait_queue
*fdc_wait
= NULL
, *format_done
= NULL
;
228 /* Errors during formatting are counted here. */
229 static int format_errors
;
231 /* Format request descriptor. */
232 static struct format_descr format_req
;
235 * Current device number. Taken either from the block header or from the
236 * format request descriptor.
238 #define CURRENT_DEVICE (format_status == FORMAT_BUSY ? format_req.device : \
241 /* Current error count. */
242 #define CURRENT_ERRORS (format_status == FORMAT_BUSY ? format_errors : \
246 * Threshold for reporting FDC errors to the console.
247 * Setting this to zero may flood your screen when using
248 * ultra cheap floppies ;-)
250 static unsigned short min_report_error_cnt
[4] = {2, 2, 2, 2};
253 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
254 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
255 * H is head unload time (1=16ms, 2=32ms, etc)
257 * Spec2 is (HLD<<1 | ND), where HLD is head load time (1=2ms, 2=4 ms etc)
258 * and ND is set means no DMA. Hardcoded to 6 (HLD=6ms, use DMA).
262 * Track buffer and block buffer (in case track buffering doesn't work).
263 * Because these are written to by the DMA controller, they must
264 * not contain a 64k byte boundary crossing, or data will be
265 * corrupted/lost. Alignment of these is enforced in boot/head.s.
266 * Note that you must not change the sizes below without updating head.s.
268 extern char tmp_floppy_area
[BLOCK_SIZE
];
269 extern char floppy_track_buffer
[512*2*MAX_BUFFER_SECTORS
];
271 static void redo_fd_request(void);
274 * These are global variables, as that's the easiest way to give
275 * information to interrupts. They are the data used for the current
280 static int read_track
= 0; /* flag to indicate if we want to read entire track */
281 static int buffer_track
= -1;
282 static int buffer_drive
= -1;
283 static int cur_spec1
= -1;
284 static int cur_rate
= -1;
285 static struct floppy_struct
* floppy
= floppy_type
;
286 static unsigned char current_drive
= 255;
287 static unsigned char sector
= 0;
288 static unsigned char head
= 0;
289 static unsigned char track
= 0;
290 static unsigned char seek_track
= 0;
291 static unsigned char current_track
= NO_TRACK
;
292 static unsigned char command
= 0;
293 static unsigned char fdc_version
= FDC_TYPE_STD
; /* FDC version code */
295 static void floppy_ready(void);
297 static void select_callback(unsigned long unused
)
302 static void floppy_select(unsigned int nr
)
304 static struct timer_list select
= { NULL
, 0, 0, select_callback
};
306 if (current_drive
== (current_DOR
& 3)) {
311 current_track
= NO_TRACK
;
313 current_DOR
|= current_drive
;
314 outb(current_DOR
,FD_DOR
);
320 static void motor_on_callback(unsigned long nr
)
322 running
|= 0x10 << nr
;
326 static struct timer_list motor_on_timer
[4] = {
327 { NULL
, 0, 0, motor_on_callback
},
328 { NULL
, 0, 1, motor_on_callback
},
329 { NULL
, 0, 2, motor_on_callback
},
330 { NULL
, 0, 3, motor_on_callback
}
333 static void motor_off_callback(unsigned long nr
)
335 unsigned char mask
= ~(0x10 << nr
);
339 outb(current_DOR
,FD_DOR
);
343 static struct timer_list motor_off_timer
[4] = {
344 { NULL
, 0, 0, motor_off_callback
},
345 { NULL
, 0, 1, motor_off_callback
},
346 { NULL
, 0, 2, motor_off_callback
},
347 { NULL
, 0, 3, motor_off_callback
}
350 static void floppy_on(unsigned int nr
)
352 unsigned char mask
= 0x10 << nr
;
354 del_timer(motor_off_timer
+ nr
);
357 if (!(mask
& current_DOR
)) {
358 del_timer(motor_on_timer
+ nr
);
359 motor_on_timer
[nr
].expires
= HZ
;
360 add_timer(motor_on_timer
+ nr
);
365 outb(current_DOR
,FD_DOR
);
368 static void floppy_off(unsigned int nr
)
370 del_timer(motor_off_timer
+nr
);
371 motor_off_timer
[nr
].expires
= 3*HZ
;
372 add_timer(motor_off_timer
+nr
);
375 void request_done(int uptodate
)
377 timer_active
&= ~(1 << FLOPPY_TIMER
);
378 if (format_status
!= FORMAT_BUSY
)
379 end_request(uptodate
);
381 format_status
= uptodate
? FORMAT_OKAY
: FORMAT_ERROR
;
382 wake_up(&format_done
);
387 * floppy-change is never called from an interrupt, so we can relax a bit
388 * here, sleep etc. Note that floppy-on tries to set current_DOR to point
389 * to the desired drive, but it will probably not survive the sleep if
390 * several floppies are used at the same time: thus the loop.
392 int floppy_change(struct buffer_head
* bh
)
394 unsigned int mask
= 1 << (bh
->b_dev
& 0x03);
396 if (MAJOR(bh
->b_dev
) != MAJOR_NR
) {
397 printk("floppy_changed: not a floppy\n");
400 if (fake_change
& mask
) {
402 fake_change
&= ~mask
;
403 /* omitting the next line breaks formatting in a horrible way ... */
404 changed_floppies
&= ~mask
;
407 if (changed_floppies
& mask
) {
409 changed_floppies
&= ~mask
;
416 ll_rw_block(WRITE
, 1, &bh
);
420 ll_rw_block(READ
, 1, &bh
);
423 if (changed_floppies
& mask
) {
424 changed_floppies
&= ~mask
;
431 #define copy_buffer(from,to) \
432 __asm__("cld ; rep ; movsl" \
434 :"c" (BLOCK_SIZE/4),"S" ((long)(from)),"D" ((long)(to)) \
437 static void setup_DMA(void)
439 unsigned long addr
,count
;
440 unsigned char dma_code
;
442 dma_code
= DMA_WRITE
;
443 if (command
== FD_READ
)
445 if (command
== FD_FORMAT
) {
446 addr
= (long) tmp_floppy_area
;
447 count
= floppy
->sect
*4;
449 addr
= (long) CURRENT
->buffer
;
453 /* mark buffer-track bad, in case all this fails.. */
454 buffer_drive
= buffer_track
= -1;
455 count
= floppy
->sect
*floppy
->head
*512;
456 addr
= (long) floppy_track_buffer
;
457 } else if (addr
>= LAST_DMA_ADDR
) {
458 addr
= (long) tmp_floppy_area
;
459 if (command
== FD_WRITE
)
460 copy_buffer(CURRENT
->buffer
,tmp_floppy_area
);
463 disable_dma(FLOPPY_DMA
);
464 clear_dma_ff(FLOPPY_DMA
);
465 set_dma_mode(FLOPPY_DMA
, (command
== FD_READ
)? DMA_MODE_READ
: DMA_MODE_WRITE
);
466 set_dma_addr(FLOPPY_DMA
, addr
);
467 set_dma_count(FLOPPY_DMA
, count
);
468 enable_dma(FLOPPY_DMA
);
472 static void output_byte(char byte
)
475 unsigned char status
;
479 for(counter
= 0 ; counter
< 10000 ; counter
++) {
480 status
= inb_p(FD_STATUS
) & (STATUS_READY
| STATUS_DIR
);
481 if (status
== STATUS_READY
) {
486 current_track
= NO_TRACK
;
488 printk("Unable to send byte to FDC\n");
491 static int result(void)
493 int i
= 0, counter
, status
;
497 for (counter
= 0 ; counter
< 10000 ; counter
++) {
498 status
= inb_p(FD_STATUS
)&(STATUS_DIR
|STATUS_READY
|STATUS_BUSY
);
499 if (status
== STATUS_READY
) {
502 if (status
== (STATUS_DIR
|STATUS_READY
|STATUS_BUSY
)) {
503 if (i
>= MAX_REPLIES
) {
504 printk("floppy_stat reply overrun\n");
507 reply_buffer
[i
++] = inb_p(FD_DATA
);
511 current_track
= NO_TRACK
;
512 printk("Getstatus times out\n");
516 static void bad_flp_intr(void)
520 current_track
= NO_TRACK
;
521 if (format_status
== FORMAT_BUSY
)
522 errors
= ++format_errors
;
524 printk(DEVICE_NAME
": no current request\n");
525 reset
= recalibrate
= 1;
528 errors
= ++CURRENT
->errors
;
529 if (errors
> MAX_ERRORS
) {
532 if (errors
> MAX_ERRORS
/2)
539 /* Set perpendicular mode as required, based on data rate, if supported.
540 * 82077 Untested! 1Mbps data rate only possible with 82077-1.
541 * TODO: increase MAX_BUFFER_SECTORS, add floppy_type entries.
543 static inline void perpendicular_mode(unsigned char rate
)
545 if (fdc_version
== FDC_TYPE_82077
) {
546 output_byte(FD_PERPENDICULAR
);
548 unsigned char r
= rate
& 0x03;
550 output_byte(2); /* perpendicular, 500 kbps */
552 output_byte(3); /* perpendicular, 1Mbps */
554 printk(DEVICE_NAME
": Invalid data rate for perpendicular mode!\n");
558 output_byte(0); /* conventional mode */
561 printk(DEVICE_NAME
": perpendicular mode not supported by this FDC.\n");
565 } /* perpendicular_mode */
569 * This has only been tested for the case fdc_version == FDC_TYPE_STD.
570 * In case you have a 82077 and want to test it, you'll have to compile
571 * with `FDC_FIFO_UNTESTED' defined. You may also want to add support for
572 * recognizing drives with vertical recording support.
574 static void configure_fdc_mode(void)
576 if (need_configure
&& (fdc_version
== FDC_TYPE_82077
)) {
577 /* Enhanced version with FIFO & vertical recording. */
578 output_byte(FD_CONFIGURE
);
580 output_byte(0x1A); /* FIFO on, polling off, 10 byte threshold */
581 output_byte(0); /* precompensation from track 0 upwards */
583 printk(DEVICE_NAME
": FIFO enabled\n");
585 if (cur_spec1
!= floppy
->spec1
) {
586 cur_spec1
= floppy
->spec1
;
587 output_byte(FD_SPECIFY
);
588 output_byte(cur_spec1
); /* hut etc */
589 output_byte(6); /* Head load time =6ms, DMA */
591 if (cur_rate
!= floppy
->rate
) {
592 /* use bit 6 of floppy->rate to indicate perpendicular mode */
593 perpendicular_mode(floppy
->rate
);
594 outb_p((cur_rate
= (floppy
->rate
)) & ~0x40, FD_DCR
);
596 } /* configure_fdc_mode */
599 static void tell_sector(int nr
)
602 printk(" -- FDC reply errror");
605 printk(": track %d, head %d, sector %d", reply_buffer
[3],
606 reply_buffer
[4], reply_buffer
[5]);
611 * Ok, this interrupt is called after a DMA read/write has succeeded
612 * or failed, so we check the results, and copy any buffers.
613 * hhb: Added better error reporting.
615 static void rw_interrupt(void)
622 /* check IC to find cause of interrupt */
623 switch ((ST0
& ST0_INTR
)>>6) {
624 case 1: /* error occured during command execution */
627 printk(DEVICE_NAME
": Drive %d is write protected\n", current_drive
);
630 } else if (ST1
& ST1_OR
) {
631 if (ftd_msg
[ST0
& ST0_DS
])
632 printk(DEVICE_NAME
": Over/Underrun - retrying\n");
633 /* could continue from where we stopped, but ... */
635 } else if (CURRENT_ERRORS
> min_report_error_cnt
[ST0
& ST0_DS
]) {
636 printk(DEVICE_NAME
" %d: ", ST0
& ST0_DS
);
638 printk("Recalibrate failed!");
639 } else if (ST2
& ST2_CRC
) {
640 printk("data CRC error");
642 } else if (ST1
& ST1_CRC
) {
645 } else if ((ST1
& (ST1_MAM
|ST1_ND
)) || (ST2
& ST2_MAM
)) {
647 printk("sector not found");
650 printk("probe failed...");
651 } else if (ST2
& ST2_WC
) { /* seek error */
652 printk("wrong cylinder");
653 } else if (ST2
& ST2_BC
) { /* cylinder marked as bad */
654 printk("bad cylinder");
656 printk("unknown error. ST[0..3] are: 0x%x 0x%x 0x%x 0x%x\n", ST0
, ST1
, ST2
, ST3
);
665 case 2: /* invalid command given */
666 printk(DEVICE_NAME
": Invalid FDC command given!\n");
670 printk(DEVICE_NAME
": Abnormal termination caused by polling\n");
674 default: /* (0) Normal command termination */
679 int drive
= MINOR(CURRENT
->dev
);
682 printk("Auto-detected floppy type %s in fd%d\n",
684 current_type
[drive
] = floppy
;
685 floppy_sizes
[drive
] = floppy
->size
>> 1;
689 buffer_track
= seek_track
;
690 buffer_drive
= current_drive
;
691 buffer_area
= floppy_track_buffer
+
692 ((sector
-1 + head
*floppy
->sect
)<<9);
693 copy_buffer(buffer_area
,CURRENT
->buffer
);
694 } else if (command
== FD_READ
&&
695 (unsigned long)(CURRENT
->buffer
) >= LAST_DMA_ADDR
)
696 copy_buffer(tmp_floppy_area
,CURRENT
->buffer
);
702 * We try to read tracks, but if we get too many errors, we
703 * go back to reading just one sector at a time.
705 * This means we should be able to read a sector even if there
706 * are other bad sectors on this track.
708 inline void setup_rw_floppy(void)
711 do_floppy
= rw_interrupt
;
712 output_byte(command
);
713 if (command
!= FD_FORMAT
) {
715 output_byte(current_drive
);
720 output_byte(head
<<2 | current_drive
);
725 output_byte(2); /* sector size = 512 */
726 output_byte(floppy
->sect
);
727 output_byte(floppy
->gap
);
728 output_byte(0xFF); /* sector size (0xff when n!=0 ?) */
730 output_byte(head
<<2 | current_drive
);
732 output_byte(floppy
->sect
);
733 output_byte(floppy
->fmt_gap
);
734 output_byte(FD_FILL_BYTE
);
741 * This is the routine called after every seek (or recalibrate) interrupt
742 * from the floppy controller. Note that the "unexpected interrupt" routine
743 * also does a recalibrate, but doesn't come here.
745 static void seek_interrupt(void)
747 /* sense drive status */
748 output_byte(FD_SENSEI
);
749 if (result() != 2 || (ST0
& 0xF8) != 0x20 || ST1
!= seek_track
) {
750 printk(DEVICE_NAME
": seek failed\n");
762 * This routine is called when everything should be correctly set up
763 * for the transfer (ie floppy motor is on and the correct floppy is
766 static void transfer(void)
768 read_track
= (command
== FD_READ
) && (CURRENT_ERRORS
< 4) &&
769 (floppy
->sect
<= MAX_BUFFER_SECTORS
);
771 configure_fdc_mode();
782 do_floppy
= seek_interrupt
;
783 output_byte(FD_SEEK
);
785 output_byte(current_drive
);
787 output_byte((head
<<2) | current_drive
);
788 output_byte(seek_track
);
794 * Special case - used after a unexpected interrupt (or reset)
797 static void recalibrate_floppy(void);
799 static void recal_interrupt(void)
801 output_byte(FD_SENSEI
);
802 current_track
= NO_TRACK
;
803 if (result()!=2 || (ST0
& 0xE0) == 0x60)
805 /* Recalibrate until track 0 is reached. Might help on some errors. */
806 if ((ST0
& 0x10) == 0x10)
807 recalibrate_floppy(); /* FIXME: should limit nr of recalibrates */
812 static void unexpected_floppy_interrupt(void)
814 current_track
= NO_TRACK
;
815 output_byte(FD_SENSEI
);
816 printk(DEVICE_NAME
": unexpected interrupt\n");
817 if (result()!=2 || (ST0
& 0xE0) == 0x60)
823 static void recalibrate_floppy(void)
827 do_floppy
= recal_interrupt
;
828 output_byte(FD_RECALIBRATE
);
829 output_byte(head
<<2 | current_drive
);
835 * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
837 static void reset_interrupt(void)
841 for (i
=0; i
<4; i
++) {
842 output_byte(FD_SENSEI
);
845 output_byte(FD_SPECIFY
);
846 output_byte(cur_spec1
); /* hut etc */
847 output_byte(6); /* Head load time =6ms, DMA */
848 configure_fdc_mode(); /* reprogram fdc */
849 if (initial_reset_flag
) {
850 initial_reset_flag
= 0;
858 recalibrate_floppy();
864 * reset is done by pulling bit 2 of DOR low for a while.
866 static void reset_floppy(void)
870 do_floppy
= reset_interrupt
;
872 current_track
= NO_TRACK
;
877 if (!initial_reset_flag
)
878 printk("Reset-floppy called\n");
880 outb_p(current_DOR
& ~0x04, FD_DOR
);
881 for (i
=0 ; i
<1000 ; i
++)
883 outb(current_DOR
, FD_DOR
);
887 static void floppy_shutdown(void)
898 static void shake_done(void)
900 current_track
= NO_TRACK
;
901 if (inb(FD_DIR
) & 0x80)
906 static int retry_recal(void (*proc
)(void))
908 output_byte(FD_SENSEI
);
909 if (result() == 2 && (ST0
& 0x10) != 0x10) return 0;
911 output_byte(FD_RECALIBRATE
);
912 output_byte(head
<<2 | current_drive
);
916 static void shake_zero(void)
918 if (!retry_recal(shake_zero
)) shake_done();
921 static void shake_one(void)
923 if (retry_recal(shake_one
)) return;
924 do_floppy
= shake_done
;
925 output_byte(FD_SEEK
);
926 output_byte(head
<< 2 | current_drive
);
930 static void floppy_ready(void)
932 if (inb(FD_DIR
) & 0x80) {
933 changed_floppies
|= 1<<current_drive
;
935 if (keep_data
[current_drive
]) {
936 if (keep_data
[current_drive
] > 0)
937 keep_data
[current_drive
]--;
939 if (ftd_msg
[current_drive
] && current_type
[current_drive
] != NULL
)
940 printk("Disk type is undefined after disk "
941 "change in fd%d\n",current_drive
);
942 current_type
[current_drive
] = NULL
;
943 floppy_sizes
[current_drive
] = MAX_DISK_SIZE
;
945 /* Forcing the drive to seek makes the "media changed" condition go away.
946 * There should be a cleaner solution for that ...
948 if (!reset
&& !recalibrate
) {
949 if (current_track
&& current_track
!= NO_TRACK
)
950 do_floppy
= shake_zero
;
952 do_floppy
= shake_one
;
953 output_byte(FD_RECALIBRATE
);
954 output_byte(head
<<2 | current_drive
);
963 recalibrate_floppy();
969 static void setup_format_params(void)
971 unsigned char *here
= (unsigned char *) tmp_floppy_area
;
972 int count
,head_shift
,track_shift
,total_shift
;
974 /* allow for about 30ms for data transport per track */
975 head_shift
= floppy
->sect
/ 6;
976 /* a ``cylinder'' is two tracks plus a little stepping time */
977 track_shift
= 2 * head_shift
+ 1;
978 /* count backwards */
979 total_shift
= floppy
->sect
-
980 ((track_shift
* track
+ head_shift
* head
) % floppy
->sect
);
982 /* XXX: should do a check to see this fits in tmp_floppy_area!! */
983 for (count
= 0; count
< floppy
->sect
; count
++) {
986 *here
++ = 1 + (( count
+ total_shift
) % floppy
->sect
);
987 *here
++ = 2; /* 512 bytes */
991 static void redo_fd_request(void)
997 if (CURRENT
&& CURRENT
->dev
< 0) return;
1000 if (format_status
== FORMAT_WAIT
)
1001 format_status
= FORMAT_BUSY
;
1002 if (format_status
!= FORMAT_BUSY
) {
1005 printk("FDC access conflict!");
1011 if (MAJOR(CURRENT
->dev
) != MAJOR_NR
)
1012 panic(DEVICE_NAME
": request list destroyed"); \
1014 if (!CURRENT
->bh
->b_lock
)
1015 panic(DEVICE_NAME
": block not locked");
1020 device
= MINOR(CURRENT_DEVICE
);
1022 floppy
= (device
>> 2) + floppy_type
;
1023 else { /* Auto-detection */
1024 floppy
= current_type
[device
& 3];
1027 floppy
= base_type
[device
& 3];
1032 if (CURRENT_ERRORS
& 1)
1036 if (format_status
!= FORMAT_BUSY
) {
1037 if (current_drive
!= CURRENT_DEV
) {
1038 current_track
= NO_TRACK
;
1039 current_drive
= CURRENT_DEV
;
1041 block
= CURRENT
->sector
;
1042 if (block
+2 > floppy
->size
) {
1046 sector
= block
% floppy
->sect
;
1047 block
/= floppy
->sect
;
1048 head
= block
% floppy
->head
;
1049 track
= block
/ floppy
->head
;
1050 seek_track
= track
<< floppy
->stretch
;
1051 if (CURRENT
->cmd
== READ
)
1053 else if (CURRENT
->cmd
== WRITE
)
1056 printk("do_fd_request: unknown command\n");
1061 if (current_drive
!= (format_req
.device
& 3))
1062 current_track
= NO_TRACK
;
1063 current_drive
= format_req
.device
& 3;
1064 if (((unsigned) format_req
.track
) >= floppy
->track
||
1065 (format_req
.head
& 0xfffe) || probing
) {
1069 head
= format_req
.head
;
1070 track
= format_req
.track
;
1071 seek_track
= track
<< floppy
->stretch
;
1072 if (seek_track
== buffer_track
) buffer_track
= -1;
1073 command
= FD_FORMAT
;
1074 setup_format_params();
1076 timer_table
[FLOPPY_TIMER
].expires
= jiffies
+10*HZ
;
1077 timer_active
|= 1 << FLOPPY_TIMER
;
1078 if ((seek_track
== buffer_track
) &&
1079 (current_drive
== buffer_drive
)) {
1080 buffer_area
= floppy_track_buffer
+
1081 ((sector
+ head
*floppy
->sect
)<<9);
1082 if (command
== FD_READ
) {
1083 copy_buffer(buffer_area
,CURRENT
->buffer
);
1086 } else if (command
== FD_WRITE
)
1087 copy_buffer(CURRENT
->buffer
,buffer_area
);
1089 if (seek_track
!= current_track
)
1092 del_timer(motor_off_timer
+ current_drive
);
1093 floppy_on(current_drive
);
1096 void do_fd_request(void)
1099 while (fdc_busy
) sleep_on(&fdc_wait
);
1105 static int fd_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
1106 unsigned long param
)
1108 int i
,drive
,cnt
,okay
;
1109 struct floppy_struct
*this_floppy
;
1112 RO_IOCTLS(inode
->i_rdev
,param
);
1114 drive
= MINOR(inode
->i_rdev
);
1124 fake_change
|= 1 << (drive
& 3);
1130 if (drive
> 3) this_floppy
= &floppy_type
[drive
>> 2];
1131 else if ((this_floppy
= current_type
[drive
& 3]) == NULL
)
1133 i
= verify_area(VERIFY_WRITE
,(void *) param
,sizeof(struct floppy_struct
));
1136 for (cnt
= 0; cnt
< sizeof(struct floppy_struct
); cnt
++)
1137 put_fs_byte(((char *) this_floppy
)[cnt
],
1138 (char *) param
+cnt
);
1143 if (fd_ref
[drive
& 3] != 1)
1146 while (format_status
!= FORMAT_NONE
)
1147 sleep_on(&format_done
);
1148 for (cnt
= 0; cnt
< sizeof(struct format_descr
); cnt
++)
1149 ((char *) &format_req
)[cnt
] = get_fs_byte(
1150 (char *) param
+cnt
);
1151 format_req
.device
= drive
;
1152 format_status
= FORMAT_WAIT
;
1154 while (format_status
!= FORMAT_OKAY
&& format_status
!=
1156 if (fdc_busy
) sleep_on(&fdc_wait
);
1162 while (format_status
!= FORMAT_OKAY
&& format_status
!=
1164 sleep_on(&format_done
);
1166 okay
= format_status
== FORMAT_OKAY
;
1167 format_status
= FORMAT_NONE
;
1168 floppy_off(drive
& 3);
1169 wake_up(&format_done
);
1170 return okay
? 0 : -EIO
;
1172 if (!permission(inode
, 2))
1175 fake_change
|= 1 << (drive
& 3);
1177 check_disk_change(inode
->i_rdev
);
1182 if (drive
< 0 || drive
> 3)
1186 current_type
[drive
] = NULL
;
1187 floppy_sizes
[drive
] = MAX_DISK_SIZE
;
1188 keep_data
[drive
] = 0;
1192 memcpy_fromfs(user_params
+drive
,
1194 sizeof(struct floppy_struct
));
1195 current_type
[drive
] = &user_params
[drive
];
1196 floppy_sizes
[drive
] = user_params
[drive
].size
>> 1;
1197 if (cmd
== FDDEFPRM
)
1198 keep_data
[drive
] = -1;
1201 while (fdc_busy
) sleep_on(&fdc_wait
);
1204 outb_p((current_DOR
& 0xfc) | drive
|
1205 (0x10 << drive
),FD_DOR
);
1206 for (cnt
= 0; cnt
< 1000; cnt
++) __asm__("nop");
1207 if (inb(FD_DIR
) & 0x80)
1208 keep_data
[drive
] = 1;
1210 keep_data
[drive
] = 0;
1211 outb_p(current_DOR
,FD_DOR
);
1222 case FDSETEMSGTRESH
:
1223 min_report_error_cnt
[drive
] = (unsigned short) (param
& 0x0f);
1231 #define CMOS_READ(addr) ({ \
1232 outb_p(addr,0x70); \
1236 static struct floppy_struct
*find_base(int drive
,int code
)
1238 struct floppy_struct
*base
;
1240 if (code
> 0 && code
< 5) {
1241 base
= &floppy_types
[(code
-1)*2];
1242 printk("fd%d is %s",drive
,base
->name
);
1245 printk("fd%d is unknown type %d",drive
,code
);
1249 static void config_types(void)
1251 printk("Floppy drive(s): ");
1252 base_type
[0] = find_base(0,(CMOS_READ(0x10) >> 4) & 15);
1253 if (((CMOS_READ(0x14) >> 6) & 1) == 0)
1254 base_type
[1] = NULL
;
1257 base_type
[1] = find_base(1,CMOS_READ(0x10) & 15);
1259 base_type
[2] = base_type
[3] = NULL
;
1264 * floppy_open check for aliasing (/dev/fd0 can be the same as
1265 * /dev/PS0 etc), and disallows simultaneous access to the same
1266 * drive with different device numbers.
1268 static int floppy_open(struct inode
* inode
, struct file
* filp
)
1273 drive
= inode
->i_rdev
& 3;
1274 old_dev
= fd_device
[drive
];
1276 if (old_dev
!= inode
->i_rdev
)
1279 fd_device
[drive
] = inode
->i_rdev
;
1280 buffer_drive
= buffer_track
= -1;
1281 if (old_dev
&& old_dev
!= inode
->i_rdev
)
1282 invalidate_buffers(old_dev
);
1283 if (filp
&& filp
->f_mode
)
1284 check_disk_change(inode
->i_rdev
);
1288 static void floppy_release(struct inode
* inode
, struct file
* filp
)
1290 sync_dev(inode
->i_rdev
);
1291 if (!fd_ref
[inode
->i_rdev
& 3]--) {
1292 printk("floppy_release with fd_ref == 0");
1293 fd_ref
[inode
->i_rdev
& 3] = 0;
1297 static struct file_operations floppy_fops
= {
1298 NULL
, /* lseek - default */
1299 block_read
, /* read - general block-dev read */
1300 block_write
, /* write - general block-dev write */
1301 NULL
, /* readdir - bad */
1303 fd_ioctl
, /* ioctl */
1305 floppy_open
, /* open */
1306 floppy_release
, /* release */
1307 block_fsync
/* fsync */
1312 * The version command is not supposed to generate an interrupt, but
1313 * my FDC does, except when booting in SVGA screen mode.
1314 * When it does generate an interrupt, it doesn't return any status bytes.
1315 * It appears to have something to do with the version command...
1317 * This should never be called, because of the reset after the version check.
1319 static void ignore_interrupt(void)
1321 printk(DEVICE_NAME
": weird interrupt ignored (%d)\n", result());
1323 CLEAR_INTR
; /* ignore only once */
1327 static void floppy_interrupt(int unused
)
1329 void (*handler
)(void) = DEVICE_INTR
;
1333 handler
= unexpected_floppy_interrupt
;
1338 * This is the floppy IRQ description. The SA_INTERRUPT in sa_flags
1339 * means we run the IRQ-handler with interrupts disabled.
1341 static struct sigaction floppy_sigaction
= {
1348 void floppy_init(void)
1350 outb(current_DOR
,FD_DOR
);
1351 if (register_blkdev(MAJOR_NR
,"fd",&floppy_fops
)) {
1352 printk("Unable to get major %d for floppy\n",MAJOR_NR
);
1355 blk_size
[MAJOR_NR
] = floppy_sizes
;
1356 blk_dev
[MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
1357 timer_table
[FLOPPY_TIMER
].fn
= floppy_shutdown
;
1358 timer_active
&= ~(1 << FLOPPY_TIMER
);
1360 if (irqaction(FLOPPY_IRQ
,&floppy_sigaction
))
1361 printk("Unable to grab IRQ%d for the floppy driver\n", FLOPPY_IRQ
);
1362 if (request_dma(FLOPPY_DMA
))
1363 printk("Unable to grab DMA%d for the floppy driver\n", FLOPPY_DMA
);
1364 /* Try to determine the floppy controller type */
1365 DEVICE_INTR
= ignore_interrupt
; /* don't ask ... */
1366 output_byte(FD_VERSION
); /* get FDC version code */
1367 if (result() != 1) {
1368 printk(DEVICE_NAME
": FDC failed to return version byte\n");
1369 fdc_version
= FDC_TYPE_STD
;
1371 fdc_version
= reply_buffer
[0];
1372 if (fdc_version
!= FDC_TYPE_STD
)
1373 printk(DEVICE_NAME
": FDC version 0x%x\n", fdc_version
);
1374 #ifndef FDC_FIFO_UNTESTED
1375 fdc_version
= FDC_TYPE_STD
; /* force std fdc type; can't test other. */
1378 /* Not all FDCs seem to be able to handle the version command
1379 * properly, so force a reset for the standard FDC clones,
1380 * to avoid interrupt garbage.
1383 if (fdc_version
== FDC_TYPE_STD
) {
1384 initial_reset_flag
= 1;