2 * drivers/block/ataflop.c
4 * Copyright (C) 1993 Greg Harp
5 * Atari Support by Bjoern Brauel, Roman Hodek
7 * Big cleanup Sep 11..14 1994 Roman Hodek:
8 * - Driver now works interrupt driven
9 * - Support for two drives; should work, but I cannot test that :-(
10 * - Reading is done in whole tracks and buffered to speed up things
11 * - Disk change detection and drive deselecting after motor-off
13 * - Autodetection of disk format (DD/HD); untested yet, because I
14 * don't have an HD drive :-(
16 * Fixes Nov 13 1994 Martin Schaller:
17 * - Autodetection works now
18 * - Support for 5 1/4'' disks
19 * - Removed drive type (unknown on atari)
20 * - Do seeks with 8 Mhz
22 * Changes by Andreas Schwab:
23 * - After errors in multiple read mode try again reading single sectors
25 * - Clean up error handling
26 * - Set blk_size for proper size checking
27 * - Initialize track register when testing presence of floppy
28 * - Implement some ioctl's
30 * Changes by Torsten Lang:
31 * - When probing the floppies we should add the FDCCMDADD_H flag since
32 * the FDC will otherwise wait forever when no disk is inserted...
34 * ++ Freddi Aschwanden (fa) 20.9.95 fixes for medusa:
35 * - MFPDELAY() after each FDC access -> atari
36 * - more/other disk formats
37 * - DMA to the block buffer directly if we have a 32bit DMA
38 * - for medusa, the step rate is always 3ms
39 * - on medusa, use only cache_push()
41 * - Make disk format numbering independent from minors
42 * - Let user set max. supported drive type (speeds up format
43 * detection, saves buffer space)
46 * - implement some more ioctls
50 * - increase gap size at start of track for HD/ED disks
52 * Michael (MSch) 11/07/96:
53 * - implemented FDSETPRM and FDDEFPRM ioctl
56 * - implemented missing BLK* ioctls
60 * - Maybe a better strategy for disk change detection (does anyone
64 #include <linux/module.h>
66 #include <linux/sched.h>
67 #include <linux/string.h>
69 #include <linux/fcntl.h>
70 #include <linux/kernel.h>
71 #include <linux/timer.h>
73 #include <linux/errno.h>
74 #include <linux/types.h>
75 #include <linux/delay.h>
77 #include <linux/slab.h>
78 #include <linux/init.h>
80 #include <asm/setup.h>
81 #include <asm/system.h>
82 #include <asm/bitops.h>
84 #include <asm/pgtable.h>
85 #include <asm/uaccess.h>
87 #include <asm/atafd.h>
88 #include <asm/atafdreg.h>
89 #include <asm/atarihw.h>
90 #include <asm/atariints.h>
91 #include <asm/atari_stdma.h>
92 #include <asm/atari_stram.h>
94 #define MAJOR_NR FLOPPY_MAJOR
95 #include <linux/blk.h>
96 #include <linux/blkpg.h>
98 #define FD_MAX_UNITS 2
102 /* Disk types: DD, HD, ED */
103 static struct atari_disk_type {
105 unsigned spt; /* sectors per track */
106 unsigned blocks; /* total number of blocks */
107 unsigned fdc_speed; /* fdc_speed setting */
108 unsigned stretch; /* track doubling ? */
110 { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
111 { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
112 { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
113 { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
114 /* formats above are probed for type DD */
115 #define MAX_TYPE_DD 3
116 { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
117 { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
118 { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
119 /* formats above are probed for types DD and HD */
120 #define MAX_TYPE_HD 6
121 { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
122 { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
123 /* formats above are probed for types DD, HD and ED */
124 #define MAX_TYPE_ED 8
125 /* types below are never autoprobed */
126 { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
127 { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
128 { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
129 { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
130 { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
131 { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
132 { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
133 { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
134 { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
135 { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
136 { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
137 { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
138 { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
139 { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
140 { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
141 { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
142 { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
143 { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
144 { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
147 static int StartDiskType[] = {
157 static int DriveType = TYPE_HD;
159 /* Array for translating minors into disk formats */
162 unsigned drive_types;
163 } minor2disktype[] = {
164 { 0, TYPE_DD }, /* 1: d360 */
165 { 4, TYPE_HD }, /* 2: h1200 */
166 { 1, TYPE_DD }, /* 3: D360 */
167 { 2, TYPE_DD }, /* 4: D720 */
168 { 1, TYPE_DD }, /* 5: h360 = D360 */
169 { 2, TYPE_DD }, /* 6: h720 = D720 */
170 { 5, TYPE_HD }, /* 7: H1440 */
171 { 7, TYPE_ED }, /* 8: E2880 */
172 /* some PC formats :-) */
173 { 8, TYPE_ED }, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
174 { 5, TYPE_HD }, /* 10: h1440 = H1440 */
175 { 9, TYPE_HD }, /* 11: H1680 */
176 { 10, TYPE_DD }, /* 12: h410 */
177 { 3, TYPE_DD }, /* 13: H820 <- == D820, 82x10 */
178 { 11, TYPE_HD }, /* 14: h1476 */
179 { 12, TYPE_HD }, /* 15: H1722 */
180 { 13, TYPE_DD }, /* 16: h420 */
181 { 14, TYPE_DD }, /* 17: H830 */
182 { 15, TYPE_HD }, /* 18: h1494 */
183 { 16, TYPE_HD }, /* 19: H1743 */
184 { 17, TYPE_DD }, /* 20: h880 */
185 { 18, TYPE_DD }, /* 21: D1040 */
186 { 19, TYPE_DD }, /* 22: D1120 */
187 { 20, TYPE_HD }, /* 23: h1600 */
188 { 21, TYPE_HD }, /* 24: H1760 */
189 { 22, TYPE_HD }, /* 25: H1920 */
190 { 23, TYPE_ED }, /* 26: E3200 */
191 { 24, TYPE_ED }, /* 27: E3520 */
192 { 25, TYPE_ED }, /* 28: E3840 */
193 { 26, TYPE_HD }, /* 29: H1840 */
194 { 27, TYPE_DD }, /* 30: D800 */
195 { 6, TYPE_HD }, /* 31: H1640 <- was H1600 == h1600 for PC */
198 #define NUM_DISK_MINORS (sizeof(minor2disktype)/sizeof(*minor2disktype))
201 * Maximum disk size (in kilobytes). This default is used whenever the
202 * current disk size is unknown.
204 #define MAX_DISK_SIZE 3280
207 * MSch: User-provided type information. 'drive' points to
208 * the respective entry of this array. Set by FDSETPRM ioctls.
210 static struct atari_disk_type user_params[FD_MAX_UNITS];
213 * User-provided permanent type information. 'drive' points to
214 * the respective entry of this array. Set by FDDEFPRM ioctls,
215 * restored upon disk change by floppy_revalidate() if valid (as seen by
216 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
218 static struct atari_disk_type default_params[FD_MAX_UNITS];
220 static int floppy_sizes[256];
221 static int floppy_blocksizes[256];
223 /* current info on each unit */
224 static struct atari_floppy_struct {
225 int connected; /* !=0 : drive is connected */
226 int autoprobe; /* !=0 : do autoprobe */
228 struct atari_disk_type *disktype; /* current type of disk */
230 int track; /* current head position or -1 if
232 unsigned int steprate; /* steprate setting */
233 unsigned int wpstat; /* current state of WP signal (for
234 disk change detection) */
235 int flags; /* flags */
236 } unit[FD_MAX_UNITS];
238 #define UD unit[drive]
239 #define UDT unit[drive].disktype
240 #define SUD unit[SelectedDrive]
241 #define SUDT unit[SelectedDrive].disktype
244 #define FDC_READ(reg) ({ \
245 /* unsigned long __flags; */ \
246 unsigned short __val; \
247 /* save_flags(__flags); cli(); */ \
248 dma_wd.dma_mode_status = 0x80 | (reg); \
250 __val = dma_wd.fdc_acces_seccount; \
252 /* restore_flags(__flags); */ \
256 #define FDC_WRITE(reg,val) \
258 /* unsigned long __flags; */ \
259 /* save_flags(__flags); cli(); */ \
260 dma_wd.dma_mode_status = 0x80 | (reg); \
262 dma_wd.fdc_acces_seccount = (val); \
264 /* restore_flags(__flags); */ \
268 /* Buffering variables:
269 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
270 * operations. Second, a track buffer is used to cache a whole track
271 * of the disk to save read operations. These are two separate buffers
272 * because that allows write operations without clearing the track buffer.
275 static int MaxSectors[] = {
278 static int BufferSize[] = {
279 15*512, 30*512, 60*512
282 #define BUFFER_SIZE (BufferSize[DriveType])
284 unsigned char *DMABuffer; /* buffer for writes */
285 static unsigned long PhysDMABuffer; /* physical address */
287 static int UseTrackbuffer = -1; /* Do track buffering? */
288 MODULE_PARM(UseTrackbuffer, "i");
290 unsigned char *TrackBuffer; /* buffer for reads */
291 static unsigned long PhysTrackBuffer; /* physical address */
292 static int BufferDrive, BufferSide, BufferTrack;
293 static int read_track; /* non-zero if we are reading whole tracks */
295 #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
296 #define IS_BUFFERED(drive,side,track) \
297 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
300 * These are global variables, as that's the easiest way to give
301 * information to interrupts. They are the data used for the current
304 static int SelectedDrive = 0;
305 static int ReqCmd, ReqBlock;
306 static int ReqSide, ReqTrack, ReqSector, ReqCnt;
307 static int HeadSettleFlag = 0;
308 static unsigned char *ReqData, *ReqBuffer;
309 static int MotorOn = 0, MotorOffTrys;
310 static int IsFormatting = 0, FormatError;
312 static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
313 MODULE_PARM(UserSteprate, "1-" __MODULE_STRING(FD_MAX_UNITS) "i");
315 /* Synchronization of FDC access. */
316 static volatile int fdc_busy = 0;
317 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
318 static DECLARE_WAIT_QUEUE_HEAD(format_wait);
320 static unsigned long changed_floppies = 0xff, fake_change = 0;
321 #define CHECK_CHANGE_DELAY HZ/2
323 #define FD_MOTOR_OFF_DELAY (3*HZ)
324 #define FD_MOTOR_OFF_MAXTRY (10*20)
326 #define FLOPPY_TIMEOUT (6*HZ)
327 #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
328 * will be recalibrated. */
329 #define MAX_ERRORS 8 /* After this many errors the driver
334 * The driver is trying to determine the correct media format
335 * while Probing is set. fd_rwsec_done() clears it after a
338 static int Probing = 0;
340 /* This flag is set when a dummy seek is necessary to make the WP
341 * status bit accessible.
343 static int NeedSeek = 0;
347 #define DPRINT(a) printk a
352 /***************************** Prototypes *****************************/
354 static void fd_select_side( int side );
355 static void fd_select_drive( int drive );
356 static void fd_deselect( void );
357 static void fd_motor_off_timer( unsigned long dummy );
358 static void check_change( unsigned long dummy );
359 static __inline__ void set_head_settle_flag( void );
360 static __inline__ int get_head_settle_flag( void );
361 static void floppy_irq (int irq, void *dummy, struct pt_regs *fp);
362 static void fd_error( void );
363 static int do_format(kdev_t drive, struct atari_format_descr *desc);
364 static void do_fd_action( int drive );
365 static void fd_calibrate( void );
366 static void fd_calibrate_done( int status );
367 static void fd_seek( void );
368 static void fd_seek_done( int status );
369 static void fd_rwsec( void );
370 static void fd_readtrack_check( unsigned long dummy );
371 static void fd_rwsec_done( int status );
372 static void fd_rwsec_done1(int status);
373 static void fd_writetrack( void );
374 static void fd_writetrack_done( int status );
375 static void fd_times_out( unsigned long dummy );
376 static void finish_fdc( void );
377 static void finish_fdc_done( int dummy );
378 static void floppy_off( unsigned int nr);
379 static __inline__ void copy_buffer( void *from, void *to);
380 static void setup_req_params( int drive );
381 static void redo_fd_request( void);
382 static int invalidate_drive(kdev_t rdev);
383 static int fd_ioctl( struct inode *inode, struct file *filp, unsigned int
384 cmd, unsigned long param);
385 static void fd_probe( int drive );
386 static int fd_test_drive_present( int drive );
387 static void config_types( void );
388 static int floppy_open( struct inode *inode, struct file *filp );
389 static int floppy_release( struct inode * inode, struct file * filp );
391 /************************* End of Prototypes **************************/
393 static struct timer_list motor_off_timer =
394 { function: fd_motor_off_timer };
395 static struct timer_list readtrack_timer =
396 { function: fd_readtrack_check };
398 static struct timer_list timeout_timer =
399 { function: fd_times_out };
401 static struct timer_list fd_timer =
402 { function: check_change };
405 start_motor_off_timer(void)
407 mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
412 start_check_change_timer( void )
414 mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
420 mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
426 del_timer(&timeout_timer);
429 /* Select the side to use. */
431 static void fd_select_side( int side )
436 cli(); /* protect against various other ints mucking around with the PSG */
438 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
439 sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
440 sound_ym.rd_data_reg_sel & 0xfe;
442 restore_flags(flags);
446 /* Select a drive, update the FDC's track register and set the correct
447 * clock speed for this disk's type.
450 static void fd_select_drive( int drive )
455 if (drive == SelectedDrive)
459 cli(); /* protect against various other ints mucking around with the PSG */
460 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
461 tmp = sound_ym.rd_data_reg_sel;
462 sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
463 atari_dont_touch_floppy_select = 1;
464 restore_flags(flags);
466 /* restore track register to saved value */
467 FDC_WRITE( FDCREG_TRACK, UD.track );
470 /* select 8/16 MHz */
472 if (ATARIHW_PRESENT(FDCSPEED))
473 dma_wd.fdc_speed = UDT->fdc_speed;
475 SelectedDrive = drive;
479 /* Deselect both drives. */
481 static void fd_deselect( void )
486 cli(); /* protect against various other ints mucking around with the PSG */
487 atari_dont_touch_floppy_select = 0;
488 sound_ym.rd_data_reg_sel=14; /* Select PSG Port A */
489 sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
490 (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
491 /* On Falcon, the drive B select line is used on the printer port, so
492 * leave it alone... */
494 restore_flags(flags);
498 /* This timer function deselects the drives when the FDC switched the
499 * motor off. The deselection cannot happen earlier because the FDC
500 * counts the index signals, which arrive only if one drive is selected.
503 static void fd_motor_off_timer( unsigned long dummy )
505 unsigned char status;
507 if (SelectedDrive < 0)
508 /* no drive selected, needn't deselect anyone */
511 if (stdma_islocked())
514 status = FDC_READ( FDCREG_STATUS );
516 if (!(status & 0x80)) {
517 /* motor already turned off by FDC -> deselect drives */
522 /* not yet off, try again */
525 /* Test again later; if tested too often, it seems there is no disk
526 * in the drive and the FDC will leave the motor on forever (or,
527 * at least until a disk is inserted). So we'll test only twice
528 * per second from then on...
530 mod_timer(&motor_off_timer,
531 jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
535 /* This function is repeatedly called to detect disk changes (as good
536 * as possible) and keep track of the current state of the write protection.
539 static void check_change( unsigned long dummy )
541 static int drive = 0;
544 unsigned char old_porta;
547 if (++drive > 1 || !UD.connected)
551 cli(); /* protect against various other ints mucking around with the PSG */
553 if (!stdma_islocked()) {
554 sound_ym.rd_data_reg_sel = 14;
555 old_porta = sound_ym.rd_data_reg_sel;
556 sound_ym.wd_data = (old_porta | DSKDRVNONE) &
557 ~(drive == 0 ? DSKDRV0 : DSKDRV1);
558 stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
559 sound_ym.wd_data = old_porta;
561 if (stat != UD.wpstat) {
562 DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
564 set_bit (drive, &changed_floppies);
567 restore_flags(flags);
569 start_check_change_timer();
573 /* Handling of the Head Settling Flag: This flag should be set after each
574 * seek operation, because we don't use seeks with verify.
577 static __inline__ void set_head_settle_flag( void )
579 HeadSettleFlag = FDCCMDADD_E;
582 static __inline__ int get_head_settle_flag( void )
584 int tmp = HeadSettleFlag;
592 /* General Interrupt Handling */
594 static void (*FloppyIRQHandler)( int status ) = NULL;
596 static void floppy_irq (int irq, void *dummy, struct pt_regs *fp)
598 unsigned char status;
599 void (*handler)( int );
601 handler = xchg(&FloppyIRQHandler, NULL);
605 status = FDC_READ( FDCREG_STATUS );
606 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
610 DPRINT(("FDC irq, no handler\n"));
615 /* Error handling: If some error happened, retry some times, then
616 * recalibrate, then try again, and fail after MAX_ERRORS.
619 static void fd_error( void )
624 wake_up( &format_wait );
628 if (QUEUE_EMPTY) return;
630 if (CURRENT->errors >= MAX_ERRORS) {
631 printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
634 else if (CURRENT->errors == RECALIBRATE_ERRORS) {
635 printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
636 if (SelectedDrive != -1)
644 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
647 /* ---------- Formatting ---------- */
649 #define FILL(n,val) \
651 memset( p, val, n ); \
655 static int do_format(kdev_t device, struct atari_format_descr *desc)
660 int type, drive = MINOR(device) & 3;
662 DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
663 drive, desc->track, desc->head, desc->sect_offset ));
667 while( fdc_busy ) sleep_on( &fdc_wait );
669 stdma_lock(floppy_irq, NULL);
670 atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
671 restore_flags(flags);
673 type = MINOR(device) >> 2;
675 if (--type >= NUM_DISK_MINORS ||
676 minor2disktype[type].drive_types > DriveType) {
680 type = minor2disktype[type].index;
681 UDT = &disk_type[type];
684 if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
691 /* The track buffer is used for the raw track data, so its
692 contents become invalid! */
694 /* stop deselect timer */
695 del_timer( &motor_off_timer );
697 FILL( 60 * (nsect / 9), 0x4e );
698 for( sect = 0; sect < nsect; ++sect ) {
704 *p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
715 FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
719 ReqTrack = desc->track;
720 ReqSide = desc->head;
721 do_fd_action( drive );
723 sleep_on( &format_wait );
726 return( FormatError ? -EIO : 0 );
730 /* do_fd_action() is the general procedure for a fd request: All
731 * required parameter settings (drive select, side select, track
732 * position) are checked and set if needed. For each of these
733 * parameters and the actual reading or writing exist two functions:
734 * one that starts the setting (or skips it if possible) and one
735 * callback for the "done" interrupt. Each done func calls the next
736 * set function to propagate the request down to fd_rwsec_done().
739 static void do_fd_action( int drive )
741 DPRINT(("do_fd_action\n"));
743 if (UseTrackbuffer && !IsFormatting) {
745 if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
746 if (ReqCmd == READ) {
747 copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
748 if (++ReqCnt < CURRENT->current_nr_sectors) {
749 /* read next sector */
750 setup_req_params( drive );
754 /* all sectors finished */
755 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
756 CURRENT->sector += CURRENT->current_nr_sectors;
763 /* cmd == WRITE, pay attention to track buffer
765 copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
770 if (SelectedDrive != drive)
771 fd_select_drive( drive );
775 else if (UD.track != ReqTrack << UDT->stretch)
777 else if (IsFormatting)
784 /* Seek to track 0 if the current track is unknown */
786 static void fd_calibrate( void )
788 if (SUD.track >= 0) {
789 fd_calibrate_done( 0 );
793 if (ATARIHW_PRESENT(FDCSPEED))
794 dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
795 DPRINT(("fd_calibrate\n"));
796 SET_IRQ_HANDLER( fd_calibrate_done );
797 /* we can't verify, since the speed may be incorrect */
798 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
807 static void fd_calibrate_done( int status )
809 DPRINT(("fd_calibrate_done()\n"));
812 /* set the correct speed now */
813 if (ATARIHW_PRESENT(FDCSPEED))
814 dma_wd.fdc_speed = SUDT->fdc_speed;
815 if (status & FDCSTAT_RECNF) {
816 printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
826 /* Seek the drive to the requested track. The drive must have been
827 * calibrated at some point before this.
830 static void fd_seek( void )
832 if (SUD.track == ReqTrack << SUDT->stretch) {
837 if (ATARIHW_PRESENT(FDCSPEED)) {
838 dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
842 DPRINT(("fd_seek() to track %d\n",ReqTrack));
843 FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
845 SET_IRQ_HANDLER( fd_seek_done );
846 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
849 set_head_settle_flag();
855 static void fd_seek_done( int status )
857 DPRINT(("fd_seek_done()\n"));
860 /* set the correct speed */
861 if (ATARIHW_PRESENT(FDCSPEED))
862 dma_wd.fdc_speed = SUDT->fdc_speed;
863 if (status & FDCSTAT_RECNF) {
864 printk(KERN_ERR "fd%d: seek error (to track %d)\n",
865 SelectedDrive, ReqTrack );
866 /* we don't know exactly which track we are on now! */
871 SUD.track = ReqTrack << SUDT->stretch;
881 /* This does the actual reading/writing after positioning the head
882 * over the correct track.
885 static int MultReadInProgress = 0;
888 static void fd_rwsec( void )
890 unsigned long paddr, flags;
891 unsigned int rwflag, old_motoron;
894 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
895 if (ReqCmd == WRITE) {
896 if (ATARIHW_PRESENT(EXTD_DMA)) {
897 paddr = virt_to_phys(ReqData);
900 copy_buffer( ReqData, DMABuffer );
901 paddr = PhysDMABuffer;
903 dma_cache_maintenance( paddr, 512, 1 );
908 paddr = PhysTrackBuffer;
910 paddr = ATARIHW_PRESENT(EXTD_DMA) ?
911 virt_to_phys(ReqData) : PhysDMABuffer;
915 fd_select_side( ReqSide );
917 /* Start sector of this operation */
918 FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
920 /* Cheat for track if stretch != 0 */
922 track = FDC_READ( FDCREG_TRACK);
924 FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
931 dma_wd.dma_lo = (unsigned char)paddr;
934 dma_wd.dma_md = (unsigned char)paddr;
937 if (ATARIHW_PRESENT(EXTD_DMA))
938 st_dma_ext_dmahi = (unsigned short)paddr;
940 dma_wd.dma_hi = (unsigned char)paddr;
942 restore_flags(flags);
944 /* Clear FIFO and switch DMA to correct mode */
945 dma_wd.dma_mode_status = 0x90 | rwflag;
947 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
949 dma_wd.dma_mode_status = 0x90 | rwflag;
952 /* How many sectors for DMA */
953 dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
957 /* Start operation */
958 dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
960 SET_IRQ_HANDLER( fd_rwsec_done );
961 dma_wd.fdc_acces_seccount =
962 (get_head_settle_flag() |
963 (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
965 old_motoron = MotorOn;
968 /* wait for interrupt */
971 /* If reading a whole track, wait about one disk rotation and
972 * then check if all sectors are read. The FDC will even
973 * search for the first non-existent sector and need 1 sec to
974 * recognise that it isn't present :-(
976 MultReadInProgress = 1;
977 mod_timer(&readtrack_timer,
978 /* 1 rot. + 5 rot.s if motor was off */
979 jiffies + HZ/5 + (old_motoron ? 0 : HZ));
985 static void fd_readtrack_check( unsigned long dummy )
987 unsigned long flags, addr, addr2;
992 if (!MultReadInProgress) {
993 /* This prevents a race condition that could arise if the
994 * interrupt is triggered while the calling of this timer
995 * callback function takes place. The IRQ function then has
996 * already cleared 'MultReadInProgress' when flow of control
999 restore_flags(flags);
1003 /* get the current DMA address */
1004 /* ++ f.a. read twice to avoid being fooled by switcher */
1008 addr = dma_wd.dma_lo & 0xff;
1010 addr |= (dma_wd.dma_md & 0xff) << 8;
1012 if (ATARIHW_PRESENT( EXTD_DMA ))
1013 addr |= (st_dma_ext_dmahi & 0xffff) << 16;
1015 addr |= (dma_wd.dma_hi & 0xff) << 16;
1017 } while(addr != addr2);
1019 if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1020 /* already read enough data, force an FDC interrupt to stop
1021 * the read operation
1023 SET_IRQ_HANDLER( NULL );
1024 MultReadInProgress = 0;
1025 restore_flags(flags);
1026 DPRINT(("fd_readtrack_check(): done\n"));
1027 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1030 /* No error until now -- the FDC would have interrupted
1036 /* not yet finished, wait another tenth rotation */
1037 restore_flags(flags);
1038 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1039 mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1044 static void fd_rwsec_done( int status )
1046 DPRINT(("fd_rwsec_done()\n"));
1049 del_timer(&readtrack_timer);
1050 if (!MultReadInProgress)
1052 MultReadInProgress = 0;
1054 fd_rwsec_done1(status);
1057 static void fd_rwsec_done1(int status)
1063 /* Correct the track if stretch != 0 */
1064 if (SUDT->stretch) {
1065 track = FDC_READ( FDCREG_TRACK);
1067 FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1070 if (!UseTrackbuffer) {
1071 dma_wd.dma_mode_status = 0x90;
1073 if (!(dma_wd.dma_mode_status & 0x01)) {
1074 printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1080 if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1081 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1084 if ((status & FDCSTAT_RECNF) &&
1085 /* RECNF is no error after a multiple read when the FDC
1086 searched for a non-existent sector! */
1087 !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1089 if (SUDT > disk_type) {
1090 if (SUDT[-1].blocks > ReqBlock) {
1091 /* try another disk type */
1093 floppy_sizes[SelectedDrive] = SUDT->blocks >> 1;
1098 if (SUD.flags & FTD_MSG)
1099 printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1100 SelectedDrive, SUDT->name );
1104 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1105 if (SUD.autoprobe) {
1106 SUDT = disk_type + StartDiskType[DriveType];
1107 floppy_sizes[SelectedDrive] = SUDT->blocks >> 1;
1112 if (ATARIHW_PRESENT(FDCSPEED)) {
1113 dma_wd.fdc_speed = SUDT->fdc_speed;
1116 setup_req_params( SelectedDrive );
1118 do_fd_action( SelectedDrive );
1122 printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1123 SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1126 if (status & FDCSTAT_CRC) {
1127 printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1128 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1131 if (status & FDCSTAT_LOST) {
1132 printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1133 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1139 if (ReqCmd == READ) {
1142 addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1143 dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1144 if (!ATARIHW_PRESENT( EXTD_DMA ))
1145 copy_buffer (addr, ReqData);
1147 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1148 BufferDrive = SelectedDrive;
1149 BufferSide = ReqSide;
1150 BufferTrack = ReqTrack;
1151 copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1155 if (++ReqCnt < CURRENT->current_nr_sectors) {
1156 /* read next sector */
1157 setup_req_params( SelectedDrive );
1158 do_fd_action( SelectedDrive );
1161 /* all sectors finished */
1162 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
1163 CURRENT->sector += CURRENT->current_nr_sectors;
1175 static void fd_writetrack( void )
1177 unsigned long paddr, flags;
1180 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1182 paddr = PhysTrackBuffer;
1183 dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1185 fd_select_side( ReqSide );
1187 /* Cheat for track if stretch != 0 */
1188 if (SUDT->stretch) {
1189 track = FDC_READ( FDCREG_TRACK);
1191 FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1198 dma_wd.dma_lo = (unsigned char)paddr;
1201 dma_wd.dma_md = (unsigned char)paddr;
1204 if (ATARIHW_PRESENT( EXTD_DMA ))
1205 st_dma_ext_dmahi = (unsigned short)paddr;
1207 dma_wd.dma_hi = (unsigned char)paddr;
1209 restore_flags(flags);
1211 /* Clear FIFO and switch DMA to correct mode */
1212 dma_wd.dma_mode_status = 0x190;
1214 dma_wd.dma_mode_status = 0x90;
1216 dma_wd.dma_mode_status = 0x190;
1219 /* How many sectors for DMA */
1220 dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1223 /* Start operation */
1224 dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1226 SET_IRQ_HANDLER( fd_writetrack_done );
1227 dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1231 /* wait for interrupt */
1235 static void fd_writetrack_done( int status )
1237 DPRINT(("fd_writetrack_done()\n"));
1241 if (status & FDCSTAT_WPROT) {
1242 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1245 if (status & FDCSTAT_LOST) {
1246 printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1247 SelectedDrive, ReqSide, ReqTrack );
1251 wake_up( &format_wait );
1258 static void fd_times_out( unsigned long dummy )
1260 atari_disable_irq( IRQ_MFP_FDC );
1261 if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1262 * before we came here... */
1264 SET_IRQ_HANDLER( NULL );
1265 /* If the timeout occurred while the readtrack_check timer was
1266 * active, we need to cancel it, else bad things will happen */
1268 del_timer( &readtrack_timer );
1269 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1272 printk(KERN_ERR "floppy timeout\n" );
1275 atari_enable_irq( IRQ_MFP_FDC );
1279 /* The (noop) seek operation here is needed to make the WP bit in the
1280 * FDC status register accessible for check_change. If the last disk
1281 * operation would have been a RDSEC, this bit would always read as 0
1282 * no matter what :-( To save time, the seek goes to the track we're
1286 static void finish_fdc( void )
1289 finish_fdc_done( 0 );
1292 DPRINT(("finish_fdc: dummy seek started\n"));
1293 FDC_WRITE (FDCREG_DATA, SUD.track);
1294 SET_IRQ_HANDLER( finish_fdc_done );
1295 FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1298 /* we must wait for the IRQ here, because the ST-DMA
1299 is released immediately afterwards and the interrupt
1300 may be delivered to the wrong driver. */
1305 static void finish_fdc_done( int dummy )
1307 unsigned long flags;
1309 DPRINT(("finish_fdc_done entered\n"));
1313 if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1314 /* If the check for a disk change is done too early after this
1315 * last seek command, the WP bit still reads wrong :-((
1317 mod_timer(&fd_timer, jiffies + 5);
1319 start_check_change_timer();
1320 start_motor_off_timer();
1326 wake_up( &fdc_wait );
1327 restore_flags(flags);
1329 DPRINT(("finish_fdc() finished\n"));
1333 /* Prevent "aliased" accesses. */
1334 static int fd_ref[4] = { 0,0,0,0 };
1335 static int fd_device[4] = { 0,0,0,0 };
1338 * Current device number. Taken either from the block header or from the
1339 * format request descriptor.
1341 #define CURRENT_DEVICE (CURRENT->rq_dev)
1343 /* Current error count. */
1344 #define CURRENT_ERRORS (CURRENT->errors)
1347 /* dummy for blk.h */
1348 static void floppy_off( unsigned int nr) {}
1351 /* The detection of disk changes is a dark chapter in Atari history :-(
1352 * Because the "Drive ready" signal isn't present in the Atari
1353 * hardware, one has to rely on the "Write Protect". This works fine,
1354 * as long as no write protected disks are used. TOS solves this
1355 * problem by introducing tri-state logic ("maybe changed") and
1356 * looking at the serial number in block 0. This isn't possible for
1357 * Linux, since the floppy driver can't make assumptions about the
1358 * filesystem used on the disk and thus the contents of block 0. I've
1359 * chosen the method to always say "The disk was changed" if it is
1360 * unsure whether it was. This implies that every open or mount
1361 * invalidates the disk buffers if you work with write protected
1362 * disks. But at least this is better than working with incorrect data
1363 * due to unrecognised disk changes.
1366 static int check_floppy_change (kdev_t dev)
1368 unsigned int drive = MINOR(dev) & 0x03;
1370 if (MAJOR(dev) != MAJOR_NR) {
1371 printk(KERN_ERR "floppy_changed: not a floppy\n");
1375 if (test_bit (drive, &fake_change)) {
1376 /* simulated change (e.g. after formatting) */
1379 if (test_bit (drive, &changed_floppies)) {
1380 /* surely changed (the WP signal changed at least once) */
1384 /* WP is on -> could be changed: to be sure, buffers should be
1393 static int floppy_revalidate (kdev_t dev)
1395 int drive = MINOR(dev) & 3;
1397 if (test_bit(drive, &changed_floppies) ||
1398 test_bit(drive, &fake_change) ||
1399 unit[drive].disktype == 0) {
1400 if (UD.flags & FTD_MSG)
1401 printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1403 clear_bit(drive, &fake_change);
1404 clear_bit(drive, &changed_floppies);
1405 /* MSch: clearing geometry makes sense only for autoprobe
1406 formats, for 'permanent user-defined' parameter:
1407 restore default_params[] here if flagged valid! */
1408 if (default_params[drive].blocks == 0)
1411 UDT = &default_params[drive];
1416 static __inline__ void copy_buffer(void *from, void *to)
1418 ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
1421 for( cnt = 512/4; cnt; cnt-- )
1426 /* This sets up the global variables describing the current request. */
1428 static void setup_req_params( int drive )
1430 int block = ReqBlock + ReqCnt;
1432 ReqTrack = block / UDT->spt;
1433 ReqSector = block - ReqTrack * UDT->spt + 1;
1434 ReqSide = ReqTrack & 1;
1436 ReqData = ReqBuffer + 512 * ReqCnt;
1439 read_track = (ReqCmd == READ && CURRENT_ERRORS == 0);
1443 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1444 ReqTrack, ReqSector, (unsigned long)ReqData ));
1448 static void redo_fd_request(void)
1450 int device, drive, type;
1452 DPRINT(("redo_fd_request: CURRENT=%08lx CURRENT->dev=%04x CURRENT->sector=%ld\n",
1453 (unsigned long)CURRENT, !QUEUE_EMPTY ? CURRENT->rq_dev : 0,
1454 !QUEUE_EMPTY ? CURRENT->sector : 0 ));
1458 if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE){
1467 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1468 panic(DEVICE_NAME ": request list destroyed");
1470 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
1471 panic(DEVICE_NAME ": block not locked");
1473 device = MINOR(CURRENT_DEVICE);
1477 if (!UD.connected) {
1478 /* drive not connected */
1479 printk(KERN_ERR "Unknown Device: fd%d\n", drive );
1487 UDT = disk_type + StartDiskType[DriveType];
1488 floppy_sizes[drive] = UDT->blocks >> 1;
1493 /* user supplied disk type */
1494 if (--type >= NUM_DISK_MINORS) {
1495 printk(KERN_WARNING "fd%d: invalid disk format", drive );
1499 if (minor2disktype[type].drive_types > DriveType) {
1500 printk(KERN_WARNING "fd%d: unsupported disk format", drive );
1504 type = minor2disktype[type].index;
1505 UDT = &disk_type[type];
1506 floppy_sizes[drive] = UDT->blocks >> 1;
1510 if (CURRENT->sector + 1 > UDT->blocks) {
1515 /* stop deselect timer */
1516 del_timer( &motor_off_timer );
1519 ReqCmd = CURRENT->cmd;
1520 ReqBlock = CURRENT->sector;
1521 ReqBuffer = CURRENT->buffer;
1522 setup_req_params( drive );
1523 do_fd_action( drive );
1532 void do_fd_request(request_queue_t * q)
1534 unsigned long flags;
1536 DPRINT(("do_fd_request for pid %d\n",current->pid));
1537 while( fdc_busy ) sleep_on( &fdc_wait );
1539 stdma_lock(floppy_irq, NULL);
1541 atari_disable_irq( IRQ_MFP_FDC );
1542 save_flags(flags); /* The request function is called with ints
1543 sti(); * disabled... so must save the IPL for later */
1545 restore_flags(flags);
1546 atari_enable_irq( IRQ_MFP_FDC );
1550 static int invalidate_drive(kdev_t rdev)
1552 /* invalidate the buffer track to force a reread */
1554 set_bit(MINOR(rdev) & 3, &fake_change);
1555 check_disk_change(rdev);
1559 static int fd_ioctl(struct inode *inode, struct file *filp,
1560 unsigned int cmd, unsigned long param)
1564 struct atari_format_descr fmt_desc;
1565 struct atari_disk_type *dtp;
1566 struct floppy_struct getprm;
1568 struct floppy_struct setprm;
1570 device = inode->i_rdev;
1577 return blk_ioctl(device, cmd, param);
1579 drive = MINOR (device);
1586 if (--type >= NUM_DISK_MINORS)
1588 if (minor2disktype[type].drive_types > DriveType)
1590 type = minor2disktype[type].index;
1591 dtp = &disk_type[type];
1592 if (UD.flags & FTD_MSG)
1593 printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1594 drive, dtp, dtp->name);
1602 if (cmd == BLKGETSIZE)
1603 return put_user(dtp->blocks, (unsigned long *)param);
1605 memset((void *)&getprm, 0, sizeof(getprm));
1606 getprm.size = dtp->blocks;
1607 getprm.sect = dtp->spt;
1609 getprm.track = dtp->blocks/dtp->spt/2;
1610 getprm.stretch = dtp->stretch;
1611 if (copy_to_user((void *)param, &getprm, sizeof(getprm)))
1619 * MSch 7/96: simple 'set geometry' case: just set the
1620 * 'default' device params (minor == 0).
1621 * Currently, the drive geometry is cleared after each
1622 * disk change and subsequent revalidate()! simple
1623 * implementation of FDDEFPRM: save geometry from a
1624 * FDDEFPRM call and restore it in floppy_revalidate() !
1627 /* get the parameters from user space */
1628 if (fd_ref[drive] != 1 && fd_ref[drive] != -1)
1630 if (copy_from_user(&setprm, (void *) param, sizeof(setprm)))
1633 * first of all: check for floppy change and revalidate,
1634 * or the next access will revalidate - and clear UDT :-(
1637 if (check_floppy_change(device))
1638 floppy_revalidate(device);
1640 if (UD.flags & FTD_MSG)
1641 printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1642 drive, setprm.size, setprm.sect, setprm.stretch);
1644 /* what if type > 0 here? Overwrite specified entry ? */
1646 /* refuse to re-set a predefined type for now */
1652 * type == 0: first look for a matching entry in the type list,
1653 * and set the UD.disktype field to use the perdefined entry.
1654 * TODO: add user-defined format to head of autoprobe list ?
1655 * Useful to include the user-type for future autodetection!
1658 for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1660 if (minor2disktype[settype].drive_types > DriveType) {
1661 /* skip this one, invalid for drive ... */
1664 setidx = minor2disktype[settype].index;
1665 dtp = &disk_type[setidx];
1667 /* found matching entry ?? */
1668 if ( dtp->blocks == setprm.size
1669 && dtp->spt == setprm.sect
1670 && dtp->stretch == setprm.stretch ) {
1671 if (UD.flags & FTD_MSG)
1672 printk (KERN_INFO "floppy%d: setting %s %p!\n",
1673 drive, dtp->name, dtp);
1675 floppy_sizes[drive] = UDT->blocks >> 1;
1677 if (cmd == FDDEFPRM) {
1678 /* save settings as permanent default type */
1679 default_params[drive].name = dtp->name;
1680 default_params[drive].spt = dtp->spt;
1681 default_params[drive].blocks = dtp->blocks;
1682 default_params[drive].fdc_speed = dtp->fdc_speed;
1683 default_params[drive].stretch = dtp->stretch;
1691 /* no matching disk type found above - setting user_params */
1693 if (cmd == FDDEFPRM) {
1694 /* set permanent type */
1695 dtp = &default_params[drive];
1697 /* set user type (reset by disk change!) */
1698 dtp = &user_params[drive];
1700 dtp->name = "user format";
1701 dtp->blocks = setprm.size;
1702 dtp->spt = setprm.sect;
1703 if (setprm.sect > 14)
1707 dtp->stretch = setprm.stretch;
1709 if (UD.flags & FTD_MSG)
1710 printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1711 drive, dtp->blocks, dtp->spt, dtp->stretch);
1714 if (!dtp || setprm.track != dtp->blocks/dtp->spt/2 ||
1721 floppy_sizes[drive] = UDT->blocks >> 1;
1725 UD.flags |= FTD_MSG;
1728 UD.flags &= ~FTD_MSG;
1730 case FDSETEMSGTRESH:
1735 if (fd_ref[drive] != 1 && fd_ref[drive] != -1)
1737 if (copy_from_user(&fmt_desc, (void *) param, sizeof(fmt_desc)))
1739 return do_format(device, &fmt_desc);
1742 /* MSch: invalidate default_params */
1743 default_params[drive].blocks = 0;
1744 floppy_sizes[drive] = MAX_DISK_SIZE;
1745 return invalidate_drive (device);
1748 return invalidate_drive(device);
1754 /* Initialize the 'unit' variable for drive 'drive' */
1756 static void __init fd_probe( int drive )
1761 if (!fd_test_drive_present( drive ))
1766 switch( UserSteprate[drive] ) {
1768 UD.steprate = FDCSTEP_2;
1771 UD.steprate = FDCSTEP_3;
1774 UD.steprate = FDCSTEP_6;
1777 UD.steprate = FDCSTEP_12;
1779 default: /* should be -1 for "not set by user" */
1780 if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1781 UD.steprate = FDCSTEP_3;
1783 UD.steprate = FDCSTEP_6;
1786 MotorOn = 1; /* from probe restore operation! */
1790 /* This function tests the physical presence of a floppy drive (not
1791 * whether a disk is inserted). This is done by issuing a restore
1792 * command, waiting max. 2 seconds (that should be enough to move the
1793 * head across the whole disk) and looking at the state of the "TR00"
1794 * signal. This should now be raised if there is a drive connected
1795 * (and there is no hardware failure :-) Otherwise, the drive is
1799 static int __init fd_test_drive_present( int drive )
1801 unsigned long timeout;
1802 unsigned char status;
1805 if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1806 fd_select_drive( drive );
1808 /* disable interrupt temporarily */
1809 atari_turnoff_irq( IRQ_MFP_FDC );
1810 FDC_WRITE (FDCREG_TRACK, 0xff00);
1811 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1813 timeout = jiffies + 2*HZ+HZ/2;
1814 while (time_before(jiffies, timeout))
1815 if (!(mfp.par_dt_reg & 0x20))
1818 status = FDC_READ( FDCREG_STATUS );
1819 ok = (status & FDCSTAT_TR00) != 0;
1821 /* force interrupt to abort restore operation (FDC would try
1822 * about 50 seconds!) */
1823 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1825 status = FDC_READ( FDCREG_STATUS );
1829 /* dummy seek command to make WP bit accessible */
1830 FDC_WRITE( FDCREG_DATA, 0 );
1831 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
1832 while( mfp.par_dt_reg & 0x20 )
1834 status = FDC_READ( FDCREG_STATUS );
1837 atari_turnon_irq( IRQ_MFP_FDC );
1842 /* Look how many and which kind of drives are connected. If there are
1843 * floppies, additionally start the disk-change and motor-off timers.
1846 static void __init config_types( void )
1850 /* for probing drives, set the FDC speed to 8 MHz */
1851 if (ATARIHW_PRESENT(FDCSPEED))
1852 dma_wd.fdc_speed = 0;
1854 printk(KERN_INFO "Probing floppy drive(s):\n");
1855 for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1858 printk(KERN_INFO "fd%d\n", drive);
1863 if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1864 /* If FDC is still busy from probing, give it another FORCI
1865 * command to abort the operation. If this isn't done, the FDC
1866 * will interrupt later and its IRQ line stays low, because
1867 * the status register isn't read. And this will block any
1868 * interrupts on this IRQ line :-(
1870 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1872 FDC_READ( FDCREG_STATUS );
1877 start_motor_off_timer();
1878 if (cnt == 1) fd_select_drive( 0 );
1879 start_check_change_timer();
1884 * floppy_open check for aliasing (/dev/fd0 can be the same as
1885 * /dev/PS0 etc), and disallows simultaneous access to the same
1886 * drive with different device numbers.
1889 static int floppy_open( struct inode *inode, struct file *filp )
1895 DPRINT (("Weird, open called with filp=0\n"));
1899 drive = MINOR(inode->i_rdev) & 3;
1900 type = MINOR(inode->i_rdev) >> 2;
1901 DPRINT(("fd_open: type=%d\n",type));
1902 if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1905 old_dev = fd_device[drive];
1907 if (fd_ref[drive] && old_dev != MINOR(inode->i_rdev))
1910 if (fd_ref[drive] == -1 || (fd_ref[drive] && filp->f_flags & O_EXCL))
1913 if (filp->f_flags & O_EXCL)
1918 fd_device[drive] = MINOR(inode->i_rdev);
1920 if (old_dev && old_dev != MINOR(inode->i_rdev))
1921 invalidate_buffers(MKDEV(FLOPPY_MAJOR, old_dev));
1923 if (filp->f_flags & O_NDELAY)
1926 if (filp->f_mode & 3) {
1927 check_disk_change(inode->i_rdev);
1928 if (filp->f_mode & 2) {
1930 floppy_release(inode, filp);
1940 static int floppy_release( struct inode * inode, struct file * filp )
1942 int drive = MINOR(inode->i_rdev) & 3;
1944 if (fd_ref[drive] < 0)
1946 else if (!fd_ref[drive]--) {
1947 printk(KERN_ERR "floppy_release with fd_ref == 0");
1954 static struct block_device_operations floppy_fops = {
1957 release: floppy_release,
1959 check_media_change: check_floppy_change,
1960 revalidate: floppy_revalidate,
1963 int __init atari_floppy_init (void)
1968 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
1972 /* Hades doesn't have Atari-compatible floppy */
1975 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
1976 printk(KERN_ERR "Unable to get major %d for floppy\n",MAJOR_NR);
1980 if (UseTrackbuffer < 0)
1981 /* not set by user -> use default: for now, we turn
1982 track buffering off for all Medusas, though it
1983 could be used with ones that have a counter
1984 card. But the test is too hard :-( */
1985 UseTrackbuffer = !MACH_IS_MEDUSA;
1987 /* initialize variables */
1991 DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
1993 printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
1994 unregister_blkdev(MAJOR_NR, "fd");
1997 TrackBuffer = DMABuffer + 512;
1998 PhysDMABuffer = virt_to_phys(DMABuffer);
1999 PhysTrackBuffer = virt_to_phys(TrackBuffer);
2000 BufferDrive = BufferSide = BufferTrack = -1;
2002 for (i = 0; i < FD_MAX_UNITS; i++) {
2007 for (i = 0; i < 256; i++)
2008 if ((i >> 2) > 0 && (i >> 2) <= NUM_DISK_MINORS) {
2009 int type = minor2disktype[(i >> 2) - 1].index;
2010 floppy_sizes[i] = disk_type[type].blocks >> 1;
2012 floppy_sizes[i] = MAX_DISK_SIZE;
2014 blk_size[MAJOR_NR] = floppy_sizes;
2015 blksize_size[MAJOR_NR] = floppy_blocksizes;
2016 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
2018 printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
2019 DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
2020 UseTrackbuffer ? "" : "no ");
2023 (void)do_floppy; /* avoid warning about unused variable */
2028 void __init atari_floppy_setup( char *str, int *ints )
2033 printk(KERN_ERR "ataflop_setup: no arguments!\n" );
2036 else if (ints[0] > 2+FD_MAX_UNITS) {
2037 printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2040 if (ints[1] < 0 || ints[1] > 2)
2041 printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2043 DriveType = ints[1];
2046 UseTrackbuffer = (ints[2] > 0);
2048 for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2049 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2050 printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2052 UserSteprate[i-3] = ints[i];
2058 MODULE_LICENSE("GPL");
2060 int init_module (void)
2064 return atari_floppy_init ();
2067 void cleanup_module (void)
2069 unregister_blkdev(MAJOR_NR, "fd");
2071 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
2072 del_timer_sync(&fd_timer);
2073 atari_stram_free( DMABuffer );