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>
67 #include <linux/delay.h>
68 #include <linux/init.h>
69 #include <linux/blkdev.h>
71 #include <asm/atafd.h>
72 #include <asm/atafdreg.h>
73 #include <asm/atariints.h>
74 #include <asm/atari_stdma.h>
75 #include <asm/atari_stram.h>
77 #define FD_MAX_UNITS 2
81 static struct request_queue *floppy_queue;
83 #define QUEUE (floppy_queue)
84 #define CURRENT elv_next_request(floppy_queue)
86 /* Disk types: DD, HD, ED */
87 static struct atari_disk_type {
89 unsigned spt; /* sectors per track */
90 unsigned blocks; /* total number of blocks */
91 unsigned fdc_speed; /* fdc_speed setting */
92 unsigned stretch; /* track doubling ? */
94 { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
95 { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
96 { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
97 { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
98 /* formats above are probed for type DD */
100 { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
101 { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
102 { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
103 /* formats above are probed for types DD and HD */
104 #define MAX_TYPE_HD 6
105 { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
106 { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
107 /* formats above are probed for types DD, HD and ED */
108 #define MAX_TYPE_ED 8
109 /* types below are never autoprobed */
110 { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
111 { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
112 { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
113 { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
114 { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
115 { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
116 { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
117 { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
118 { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
119 { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
120 { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
121 { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
122 { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
123 { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
124 { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
125 { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
126 { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
127 { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
128 { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
131 static int StartDiskType[] = {
141 static int DriveType = TYPE_HD;
143 static DEFINE_SPINLOCK(ataflop_lock);
145 /* Array for translating minors into disk formats */
148 unsigned drive_types;
149 } minor2disktype[] = {
150 { 0, TYPE_DD }, /* 1: d360 */
151 { 4, TYPE_HD }, /* 2: h1200 */
152 { 1, TYPE_DD }, /* 3: D360 */
153 { 2, TYPE_DD }, /* 4: D720 */
154 { 1, TYPE_DD }, /* 5: h360 = D360 */
155 { 2, TYPE_DD }, /* 6: h720 = D720 */
156 { 5, TYPE_HD }, /* 7: H1440 */
157 { 7, TYPE_ED }, /* 8: E2880 */
158 /* some PC formats :-) */
159 { 8, TYPE_ED }, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
160 { 5, TYPE_HD }, /* 10: h1440 = H1440 */
161 { 9, TYPE_HD }, /* 11: H1680 */
162 { 10, TYPE_DD }, /* 12: h410 */
163 { 3, TYPE_DD }, /* 13: H820 <- == D820, 82x10 */
164 { 11, TYPE_HD }, /* 14: h1476 */
165 { 12, TYPE_HD }, /* 15: H1722 */
166 { 13, TYPE_DD }, /* 16: h420 */
167 { 14, TYPE_DD }, /* 17: H830 */
168 { 15, TYPE_HD }, /* 18: h1494 */
169 { 16, TYPE_HD }, /* 19: H1743 */
170 { 17, TYPE_DD }, /* 20: h880 */
171 { 18, TYPE_DD }, /* 21: D1040 */
172 { 19, TYPE_DD }, /* 22: D1120 */
173 { 20, TYPE_HD }, /* 23: h1600 */
174 { 21, TYPE_HD }, /* 24: H1760 */
175 { 22, TYPE_HD }, /* 25: H1920 */
176 { 23, TYPE_ED }, /* 26: E3200 */
177 { 24, TYPE_ED }, /* 27: E3520 */
178 { 25, TYPE_ED }, /* 28: E3840 */
179 { 26, TYPE_HD }, /* 29: H1840 */
180 { 27, TYPE_DD }, /* 30: D800 */
181 { 6, TYPE_HD }, /* 31: H1640 <- was H1600 == h1600 for PC */
184 #define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
187 * Maximum disk size (in kilobytes). This default is used whenever the
188 * current disk size is unknown.
190 #define MAX_DISK_SIZE 3280
193 * MSch: User-provided type information. 'drive' points to
194 * the respective entry of this array. Set by FDSETPRM ioctls.
196 static struct atari_disk_type user_params[FD_MAX_UNITS];
199 * User-provided permanent type information. 'drive' points to
200 * the respective entry of this array. Set by FDDEFPRM ioctls,
201 * restored upon disk change by floppy_revalidate() if valid (as seen by
202 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
204 static struct atari_disk_type default_params[FD_MAX_UNITS];
206 /* current info on each unit */
207 static struct atari_floppy_struct {
208 int connected; /* !=0 : drive is connected */
209 int autoprobe; /* !=0 : do autoprobe */
211 struct atari_disk_type *disktype; /* current type of disk */
213 int track; /* current head position or -1 if
215 unsigned int steprate; /* steprate setting */
216 unsigned int wpstat; /* current state of WP signal (for
217 disk change detection) */
218 int flags; /* flags */
219 struct gendisk *disk;
222 } unit[FD_MAX_UNITS];
224 #define UD unit[drive]
225 #define UDT unit[drive].disktype
226 #define SUD unit[SelectedDrive]
227 #define SUDT unit[SelectedDrive].disktype
230 #define FDC_READ(reg) ({ \
231 /* unsigned long __flags; */ \
232 unsigned short __val; \
233 /* local_irq_save(__flags); */ \
234 dma_wd.dma_mode_status = 0x80 | (reg); \
236 __val = dma_wd.fdc_acces_seccount; \
238 /* local_irq_restore(__flags); */ \
242 #define FDC_WRITE(reg,val) \
244 /* unsigned long __flags; */ \
245 /* local_irq_save(__flags); */ \
246 dma_wd.dma_mode_status = 0x80 | (reg); \
248 dma_wd.fdc_acces_seccount = (val); \
250 /* local_irq_restore(__flags); */ \
254 /* Buffering variables:
255 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
256 * operations. Second, a track buffer is used to cache a whole track
257 * of the disk to save read operations. These are two separate buffers
258 * because that allows write operations without clearing the track buffer.
261 static int MaxSectors[] = {
264 static int BufferSize[] = {
265 15*512, 30*512, 60*512
268 #define BUFFER_SIZE (BufferSize[DriveType])
270 unsigned char *DMABuffer; /* buffer for writes */
271 static unsigned long PhysDMABuffer; /* physical address */
273 static int UseTrackbuffer = -1; /* Do track buffering? */
274 module_param(UseTrackbuffer, int, 0);
276 unsigned char *TrackBuffer; /* buffer for reads */
277 static unsigned long PhysTrackBuffer; /* physical address */
278 static int BufferDrive, BufferSide, BufferTrack;
279 static int read_track; /* non-zero if we are reading whole tracks */
281 #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
282 #define IS_BUFFERED(drive,side,track) \
283 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
286 * These are global variables, as that's the easiest way to give
287 * information to interrupts. They are the data used for the current
290 static int SelectedDrive = 0;
291 static int ReqCmd, ReqBlock;
292 static int ReqSide, ReqTrack, ReqSector, ReqCnt;
293 static int HeadSettleFlag = 0;
294 static unsigned char *ReqData, *ReqBuffer;
295 static int MotorOn = 0, MotorOffTrys;
296 static int IsFormatting = 0, FormatError;
298 static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
299 module_param_array(UserSteprate, int, NULL, 0);
301 /* Synchronization of FDC access. */
302 static volatile int fdc_busy = 0;
303 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
304 static DECLARE_WAIT_QUEUE_HEAD(format_wait);
306 static unsigned long changed_floppies = 0xff, fake_change = 0;
307 #define CHECK_CHANGE_DELAY HZ/2
309 #define FD_MOTOR_OFF_DELAY (3*HZ)
310 #define FD_MOTOR_OFF_MAXTRY (10*20)
312 #define FLOPPY_TIMEOUT (6*HZ)
313 #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
314 * will be recalibrated. */
315 #define MAX_ERRORS 8 /* After this many errors the driver
320 * The driver is trying to determine the correct media format
321 * while Probing is set. fd_rwsec_done() clears it after a
324 static int Probing = 0;
326 /* This flag is set when a dummy seek is necessary to make the WP
327 * status bit accessible.
329 static int NeedSeek = 0;
333 #define DPRINT(a) printk a
338 /***************************** Prototypes *****************************/
340 static void fd_select_side( int side );
341 static void fd_select_drive( int drive );
342 static void fd_deselect( void );
343 static void fd_motor_off_timer( unsigned long dummy );
344 static void check_change( unsigned long dummy );
345 static irqreturn_t floppy_irq (int irq, void *dummy, struct pt_regs *fp);
346 static void fd_error( void );
347 static int do_format(int drive, int type, struct atari_format_descr *desc);
348 static void do_fd_action( int drive );
349 static void fd_calibrate( void );
350 static void fd_calibrate_done( int status );
351 static void fd_seek( void );
352 static void fd_seek_done( int status );
353 static void fd_rwsec( void );
354 static void fd_readtrack_check( unsigned long dummy );
355 static void fd_rwsec_done( int status );
356 static void fd_rwsec_done1(int status);
357 static void fd_writetrack( void );
358 static void fd_writetrack_done( int status );
359 static void fd_times_out( unsigned long dummy );
360 static void finish_fdc( void );
361 static void finish_fdc_done( int dummy );
362 static void setup_req_params( int drive );
363 static void redo_fd_request( void);
364 static int fd_ioctl( struct inode *inode, struct file *filp, unsigned int
365 cmd, unsigned long param);
366 static void fd_probe( int drive );
367 static int fd_test_drive_present( int drive );
368 static void config_types( void );
369 static int floppy_open( struct inode *inode, struct file *filp );
370 static int floppy_release( struct inode * inode, struct file * filp );
372 /************************* End of Prototypes **************************/
374 static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer, 0, 0);
375 static DEFINE_TIMER(readtrack_timer, fd_readtrack_check, 0, 0);
376 static DEFINE_TIMER(timeout_timer, fd_times_out, 0, 0);
377 static DEFINE_TIMER(fd_timer, check_change, 0, 0);
379 static inline void start_motor_off_timer(void)
381 mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
385 static inline void start_check_change_timer( void )
387 mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
390 static inline void start_timeout(void)
392 mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
395 static inline void stop_timeout(void)
397 del_timer(&timeout_timer);
400 /* Select the side to use. */
402 static void fd_select_side( int side )
406 /* protect against various other ints mucking around with the PSG */
407 local_irq_save(flags);
409 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
410 sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
411 sound_ym.rd_data_reg_sel & 0xfe;
413 local_irq_restore(flags);
417 /* Select a drive, update the FDC's track register and set the correct
418 * clock speed for this disk's type.
421 static void fd_select_drive( int drive )
426 if (drive == SelectedDrive)
429 /* protect against various other ints mucking around with the PSG */
430 local_irq_save(flags);
431 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
432 tmp = sound_ym.rd_data_reg_sel;
433 sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
434 atari_dont_touch_floppy_select = 1;
435 local_irq_restore(flags);
437 /* restore track register to saved value */
438 FDC_WRITE( FDCREG_TRACK, UD.track );
441 /* select 8/16 MHz */
443 if (ATARIHW_PRESENT(FDCSPEED))
444 dma_wd.fdc_speed = UDT->fdc_speed;
446 SelectedDrive = drive;
450 /* Deselect both drives. */
452 static void fd_deselect( void )
456 /* protect against various other ints mucking around with the PSG */
457 local_irq_save(flags);
458 atari_dont_touch_floppy_select = 0;
459 sound_ym.rd_data_reg_sel=14; /* Select PSG Port A */
460 sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
461 (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
462 /* On Falcon, the drive B select line is used on the printer port, so
463 * leave it alone... */
465 local_irq_restore(flags);
469 /* This timer function deselects the drives when the FDC switched the
470 * motor off. The deselection cannot happen earlier because the FDC
471 * counts the index signals, which arrive only if one drive is selected.
474 static void fd_motor_off_timer( unsigned long dummy )
476 unsigned char status;
478 if (SelectedDrive < 0)
479 /* no drive selected, needn't deselect anyone */
482 if (stdma_islocked())
485 status = FDC_READ( FDCREG_STATUS );
487 if (!(status & 0x80)) {
488 /* motor already turned off by FDC -> deselect drives */
493 /* not yet off, try again */
496 /* Test again later; if tested too often, it seems there is no disk
497 * in the drive and the FDC will leave the motor on forever (or,
498 * at least until a disk is inserted). So we'll test only twice
499 * per second from then on...
501 mod_timer(&motor_off_timer,
502 jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
506 /* This function is repeatedly called to detect disk changes (as good
507 * as possible) and keep track of the current state of the write protection.
510 static void check_change( unsigned long dummy )
512 static int drive = 0;
515 unsigned char old_porta;
518 if (++drive > 1 || !UD.connected)
521 /* protect against various other ints mucking around with the PSG */
522 local_irq_save(flags);
524 if (!stdma_islocked()) {
525 sound_ym.rd_data_reg_sel = 14;
526 old_porta = sound_ym.rd_data_reg_sel;
527 sound_ym.wd_data = (old_porta | DSKDRVNONE) &
528 ~(drive == 0 ? DSKDRV0 : DSKDRV1);
529 stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
530 sound_ym.wd_data = old_porta;
532 if (stat != UD.wpstat) {
533 DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
535 set_bit (drive, &changed_floppies);
538 local_irq_restore(flags);
540 start_check_change_timer();
544 /* Handling of the Head Settling Flag: This flag should be set after each
545 * seek operation, because we don't use seeks with verify.
548 static inline void set_head_settle_flag(void)
550 HeadSettleFlag = FDCCMDADD_E;
553 static inline int get_head_settle_flag(void)
555 int tmp = HeadSettleFlag;
560 static inline void copy_buffer(void *from, void *to)
562 ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
565 for (cnt = 512/4; cnt; cnt--)
572 /* General Interrupt Handling */
574 static void (*FloppyIRQHandler)( int status ) = NULL;
576 static irqreturn_t floppy_irq (int irq, void *dummy, struct pt_regs *fp)
578 unsigned char status;
579 void (*handler)( int );
581 handler = xchg(&FloppyIRQHandler, NULL);
585 status = FDC_READ( FDCREG_STATUS );
586 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
590 DPRINT(("FDC irq, no handler\n"));
596 /* Error handling: If some error happened, retry some times, then
597 * recalibrate, then try again, and fail after MAX_ERRORS.
600 static void fd_error( void )
605 wake_up( &format_wait );
613 if (CURRENT->errors >= MAX_ERRORS) {
614 printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
615 end_request(CURRENT, 0);
617 else if (CURRENT->errors == RECALIBRATE_ERRORS) {
618 printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
619 if (SelectedDrive != -1)
627 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
630 /* ---------- Formatting ---------- */
632 #define FILL(n,val) \
634 memset( p, val, n ); \
638 static int do_format(int drive, int type, struct atari_format_descr *desc)
644 DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
645 drive, desc->track, desc->head, desc->sect_offset ));
647 local_irq_save(flags);
648 while( fdc_busy ) sleep_on( &fdc_wait );
650 stdma_lock(floppy_irq, NULL);
651 atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
652 local_irq_restore(flags);
655 if (--type >= NUM_DISK_MINORS ||
656 minor2disktype[type].drive_types > DriveType) {
660 type = minor2disktype[type].index;
661 UDT = &disk_type[type];
664 if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
671 /* The track buffer is used for the raw track data, so its
672 contents become invalid! */
674 /* stop deselect timer */
675 del_timer( &motor_off_timer );
677 FILL( 60 * (nsect / 9), 0x4e );
678 for( sect = 0; sect < nsect; ++sect ) {
684 *p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
695 FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
699 ReqTrack = desc->track;
700 ReqSide = desc->head;
701 do_fd_action( drive );
703 sleep_on( &format_wait );
706 return( FormatError ? -EIO : 0 );
710 /* do_fd_action() is the general procedure for a fd request: All
711 * required parameter settings (drive select, side select, track
712 * position) are checked and set if needed. For each of these
713 * parameters and the actual reading or writing exist two functions:
714 * one that starts the setting (or skips it if possible) and one
715 * callback for the "done" interrupt. Each done func calls the next
716 * set function to propagate the request down to fd_rwsec_done().
719 static void do_fd_action( int drive )
721 DPRINT(("do_fd_action\n"));
723 if (UseTrackbuffer && !IsFormatting) {
725 if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
726 if (ReqCmd == READ) {
727 copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
728 if (++ReqCnt < CURRENT->current_nr_sectors) {
729 /* read next sector */
730 setup_req_params( drive );
734 /* all sectors finished */
735 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
736 CURRENT->sector += CURRENT->current_nr_sectors;
737 end_request(CURRENT, 1);
743 /* cmd == WRITE, pay attention to track buffer
745 copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
750 if (SelectedDrive != drive)
751 fd_select_drive( drive );
755 else if (UD.track != ReqTrack << UDT->stretch)
757 else if (IsFormatting)
764 /* Seek to track 0 if the current track is unknown */
766 static void fd_calibrate( void )
768 if (SUD.track >= 0) {
769 fd_calibrate_done( 0 );
773 if (ATARIHW_PRESENT(FDCSPEED))
774 dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
775 DPRINT(("fd_calibrate\n"));
776 SET_IRQ_HANDLER( fd_calibrate_done );
777 /* we can't verify, since the speed may be incorrect */
778 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
787 static void fd_calibrate_done( int status )
789 DPRINT(("fd_calibrate_done()\n"));
792 /* set the correct speed now */
793 if (ATARIHW_PRESENT(FDCSPEED))
794 dma_wd.fdc_speed = SUDT->fdc_speed;
795 if (status & FDCSTAT_RECNF) {
796 printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
806 /* Seek the drive to the requested track. The drive must have been
807 * calibrated at some point before this.
810 static void fd_seek( void )
812 if (SUD.track == ReqTrack << SUDT->stretch) {
817 if (ATARIHW_PRESENT(FDCSPEED)) {
818 dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
822 DPRINT(("fd_seek() to track %d\n",ReqTrack));
823 FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
825 SET_IRQ_HANDLER( fd_seek_done );
826 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
829 set_head_settle_flag();
835 static void fd_seek_done( int status )
837 DPRINT(("fd_seek_done()\n"));
840 /* set the correct speed */
841 if (ATARIHW_PRESENT(FDCSPEED))
842 dma_wd.fdc_speed = SUDT->fdc_speed;
843 if (status & FDCSTAT_RECNF) {
844 printk(KERN_ERR "fd%d: seek error (to track %d)\n",
845 SelectedDrive, ReqTrack );
846 /* we don't know exactly which track we are on now! */
851 SUD.track = ReqTrack << SUDT->stretch;
861 /* This does the actual reading/writing after positioning the head
862 * over the correct track.
865 static int MultReadInProgress = 0;
868 static void fd_rwsec( void )
870 unsigned long paddr, flags;
871 unsigned int rwflag, old_motoron;
874 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
875 if (ReqCmd == WRITE) {
876 if (ATARIHW_PRESENT(EXTD_DMA)) {
877 paddr = virt_to_phys(ReqData);
880 copy_buffer( ReqData, DMABuffer );
881 paddr = PhysDMABuffer;
883 dma_cache_maintenance( paddr, 512, 1 );
888 paddr = PhysTrackBuffer;
890 paddr = ATARIHW_PRESENT(EXTD_DMA) ?
891 virt_to_phys(ReqData) : PhysDMABuffer;
895 fd_select_side( ReqSide );
897 /* Start sector of this operation */
898 FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
900 /* Cheat for track if stretch != 0 */
902 track = FDC_READ( FDCREG_TRACK);
904 FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
909 local_irq_save(flags);
910 dma_wd.dma_lo = (unsigned char)paddr;
913 dma_wd.dma_md = (unsigned char)paddr;
916 if (ATARIHW_PRESENT(EXTD_DMA))
917 st_dma_ext_dmahi = (unsigned short)paddr;
919 dma_wd.dma_hi = (unsigned char)paddr;
921 local_irq_restore(flags);
923 /* Clear FIFO and switch DMA to correct mode */
924 dma_wd.dma_mode_status = 0x90 | rwflag;
926 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
928 dma_wd.dma_mode_status = 0x90 | rwflag;
931 /* How many sectors for DMA */
932 dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
936 /* Start operation */
937 dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
939 SET_IRQ_HANDLER( fd_rwsec_done );
940 dma_wd.fdc_acces_seccount =
941 (get_head_settle_flag() |
942 (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
944 old_motoron = MotorOn;
947 /* wait for interrupt */
950 /* If reading a whole track, wait about one disk rotation and
951 * then check if all sectors are read. The FDC will even
952 * search for the first non-existent sector and need 1 sec to
953 * recognise that it isn't present :-(
955 MultReadInProgress = 1;
956 mod_timer(&readtrack_timer,
957 /* 1 rot. + 5 rot.s if motor was off */
958 jiffies + HZ/5 + (old_motoron ? 0 : HZ));
964 static void fd_readtrack_check( unsigned long dummy )
966 unsigned long flags, addr, addr2;
968 local_irq_save(flags);
970 if (!MultReadInProgress) {
971 /* This prevents a race condition that could arise if the
972 * interrupt is triggered while the calling of this timer
973 * callback function takes place. The IRQ function then has
974 * already cleared 'MultReadInProgress' when flow of control
977 local_irq_restore(flags);
981 /* get the current DMA address */
982 /* ++ f.a. read twice to avoid being fooled by switcher */
986 addr = dma_wd.dma_lo & 0xff;
988 addr |= (dma_wd.dma_md & 0xff) << 8;
990 if (ATARIHW_PRESENT( EXTD_DMA ))
991 addr |= (st_dma_ext_dmahi & 0xffff) << 16;
993 addr |= (dma_wd.dma_hi & 0xff) << 16;
995 } while(addr != addr2);
997 if (addr >= PhysTrackBuffer + SUDT->spt*512) {
998 /* already read enough data, force an FDC interrupt to stop
1001 SET_IRQ_HANDLER( NULL );
1002 MultReadInProgress = 0;
1003 local_irq_restore(flags);
1004 DPRINT(("fd_readtrack_check(): done\n"));
1005 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1008 /* No error until now -- the FDC would have interrupted
1014 /* not yet finished, wait another tenth rotation */
1015 local_irq_restore(flags);
1016 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1017 mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1022 static void fd_rwsec_done( int status )
1024 DPRINT(("fd_rwsec_done()\n"));
1027 del_timer(&readtrack_timer);
1028 if (!MultReadInProgress)
1030 MultReadInProgress = 0;
1032 fd_rwsec_done1(status);
1035 static void fd_rwsec_done1(int status)
1041 /* Correct the track if stretch != 0 */
1042 if (SUDT->stretch) {
1043 track = FDC_READ( FDCREG_TRACK);
1045 FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1048 if (!UseTrackbuffer) {
1049 dma_wd.dma_mode_status = 0x90;
1051 if (!(dma_wd.dma_mode_status & 0x01)) {
1052 printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1058 if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1059 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1062 if ((status & FDCSTAT_RECNF) &&
1063 /* RECNF is no error after a multiple read when the FDC
1064 searched for a non-existent sector! */
1065 !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1067 if (SUDT > disk_type) {
1068 if (SUDT[-1].blocks > ReqBlock) {
1069 /* try another disk type */
1071 set_capacity(unit[SelectedDrive].disk,
1077 if (SUD.flags & FTD_MSG)
1078 printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1079 SelectedDrive, SUDT->name );
1083 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1084 if (SUD.autoprobe) {
1085 SUDT = disk_type + StartDiskType[DriveType];
1086 set_capacity(unit[SelectedDrive].disk,
1092 if (ATARIHW_PRESENT(FDCSPEED)) {
1093 dma_wd.fdc_speed = SUDT->fdc_speed;
1096 setup_req_params( SelectedDrive );
1098 do_fd_action( SelectedDrive );
1102 printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1103 SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1106 if (status & FDCSTAT_CRC) {
1107 printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1108 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1111 if (status & FDCSTAT_LOST) {
1112 printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1113 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1119 if (ReqCmd == READ) {
1122 addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1123 dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1124 if (!ATARIHW_PRESENT( EXTD_DMA ))
1125 copy_buffer (addr, ReqData);
1127 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1128 BufferDrive = SelectedDrive;
1129 BufferSide = ReqSide;
1130 BufferTrack = ReqTrack;
1131 copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1135 if (++ReqCnt < CURRENT->current_nr_sectors) {
1136 /* read next sector */
1137 setup_req_params( SelectedDrive );
1138 do_fd_action( SelectedDrive );
1141 /* all sectors finished */
1142 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
1143 CURRENT->sector += CURRENT->current_nr_sectors;
1144 end_request(CURRENT, 1);
1155 static void fd_writetrack( void )
1157 unsigned long paddr, flags;
1160 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1162 paddr = PhysTrackBuffer;
1163 dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1165 fd_select_side( ReqSide );
1167 /* Cheat for track if stretch != 0 */
1168 if (SUDT->stretch) {
1169 track = FDC_READ( FDCREG_TRACK);
1171 FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1176 local_irq_save(flags);
1177 dma_wd.dma_lo = (unsigned char)paddr;
1180 dma_wd.dma_md = (unsigned char)paddr;
1183 if (ATARIHW_PRESENT( EXTD_DMA ))
1184 st_dma_ext_dmahi = (unsigned short)paddr;
1186 dma_wd.dma_hi = (unsigned char)paddr;
1188 local_irq_restore(flags);
1190 /* Clear FIFO and switch DMA to correct mode */
1191 dma_wd.dma_mode_status = 0x190;
1193 dma_wd.dma_mode_status = 0x90;
1195 dma_wd.dma_mode_status = 0x190;
1198 /* How many sectors for DMA */
1199 dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1202 /* Start operation */
1203 dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1205 SET_IRQ_HANDLER( fd_writetrack_done );
1206 dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1210 /* wait for interrupt */
1214 static void fd_writetrack_done( int status )
1216 DPRINT(("fd_writetrack_done()\n"));
1220 if (status & FDCSTAT_WPROT) {
1221 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1224 if (status & FDCSTAT_LOST) {
1225 printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1226 SelectedDrive, ReqSide, ReqTrack );
1230 wake_up( &format_wait );
1237 static void fd_times_out( unsigned long dummy )
1239 atari_disable_irq( IRQ_MFP_FDC );
1240 if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1241 * before we came here... */
1243 SET_IRQ_HANDLER( NULL );
1244 /* If the timeout occurred while the readtrack_check timer was
1245 * active, we need to cancel it, else bad things will happen */
1247 del_timer( &readtrack_timer );
1248 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1251 printk(KERN_ERR "floppy timeout\n" );
1254 atari_enable_irq( IRQ_MFP_FDC );
1258 /* The (noop) seek operation here is needed to make the WP bit in the
1259 * FDC status register accessible for check_change. If the last disk
1260 * operation would have been a RDSEC, this bit would always read as 0
1261 * no matter what :-( To save time, the seek goes to the track we're
1265 static void finish_fdc( void )
1268 finish_fdc_done( 0 );
1271 DPRINT(("finish_fdc: dummy seek started\n"));
1272 FDC_WRITE (FDCREG_DATA, SUD.track);
1273 SET_IRQ_HANDLER( finish_fdc_done );
1274 FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1277 /* we must wait for the IRQ here, because the ST-DMA
1278 is released immediately afterwards and the interrupt
1279 may be delivered to the wrong driver. */
1284 static void finish_fdc_done( int dummy )
1286 unsigned long flags;
1288 DPRINT(("finish_fdc_done entered\n"));
1292 if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1293 /* If the check for a disk change is done too early after this
1294 * last seek command, the WP bit still reads wrong :-((
1296 mod_timer(&fd_timer, jiffies + 5);
1298 start_check_change_timer();
1299 start_motor_off_timer();
1301 local_irq_save(flags);
1304 wake_up( &fdc_wait );
1305 local_irq_restore(flags);
1307 DPRINT(("finish_fdc() finished\n"));
1310 /* The detection of disk changes is a dark chapter in Atari history :-(
1311 * Because the "Drive ready" signal isn't present in the Atari
1312 * hardware, one has to rely on the "Write Protect". This works fine,
1313 * as long as no write protected disks are used. TOS solves this
1314 * problem by introducing tri-state logic ("maybe changed") and
1315 * looking at the serial number in block 0. This isn't possible for
1316 * Linux, since the floppy driver can't make assumptions about the
1317 * filesystem used on the disk and thus the contents of block 0. I've
1318 * chosen the method to always say "The disk was changed" if it is
1319 * unsure whether it was. This implies that every open or mount
1320 * invalidates the disk buffers if you work with write protected
1321 * disks. But at least this is better than working with incorrect data
1322 * due to unrecognised disk changes.
1325 static int check_floppy_change(struct gendisk *disk)
1327 struct atari_floppy_struct *p = disk->private_data;
1328 unsigned int drive = p - unit;
1329 if (test_bit (drive, &fake_change)) {
1330 /* simulated change (e.g. after formatting) */
1333 if (test_bit (drive, &changed_floppies)) {
1334 /* surely changed (the WP signal changed at least once) */
1338 /* WP is on -> could be changed: to be sure, buffers should be
1347 static int floppy_revalidate(struct gendisk *disk)
1349 struct atari_floppy_struct *p = disk->private_data;
1350 unsigned int drive = p - unit;
1352 if (test_bit(drive, &changed_floppies) ||
1353 test_bit(drive, &fake_change) ||
1355 if (UD.flags & FTD_MSG)
1356 printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1358 clear_bit(drive, &fake_change);
1359 clear_bit(drive, &changed_floppies);
1360 /* MSch: clearing geometry makes sense only for autoprobe
1361 formats, for 'permanent user-defined' parameter:
1362 restore default_params[] here if flagged valid! */
1363 if (default_params[drive].blocks == 0)
1366 UDT = &default_params[drive];
1372 /* This sets up the global variables describing the current request. */
1374 static void setup_req_params( int drive )
1376 int block = ReqBlock + ReqCnt;
1378 ReqTrack = block / UDT->spt;
1379 ReqSector = block - ReqTrack * UDT->spt + 1;
1380 ReqSide = ReqTrack & 1;
1382 ReqData = ReqBuffer + 512 * ReqCnt;
1385 read_track = (ReqCmd == READ && CURRENT->errors == 0);
1389 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1390 ReqTrack, ReqSector, (unsigned long)ReqData ));
1394 static void redo_fd_request(void)
1397 struct atari_floppy_struct *floppy;
1399 DPRINT(("redo_fd_request: CURRENT=%p dev=%s CURRENT->sector=%ld\n",
1400 CURRENT, CURRENT ? CURRENT->rq_disk->disk_name : "",
1401 CURRENT ? CURRENT->sector : 0 ));
1410 floppy = CURRENT->rq_disk->private_data;
1411 drive = floppy - unit;
1412 type = floppy->type;
1414 if (!UD.connected) {
1415 /* drive not connected */
1416 printk(KERN_ERR "Unknown Device: fd%d\n", drive );
1417 end_request(CURRENT, 0);
1424 UDT = disk_type + StartDiskType[DriveType];
1425 set_capacity(floppy->disk, UDT->blocks);
1430 /* user supplied disk type */
1431 if (--type >= NUM_DISK_MINORS) {
1432 printk(KERN_WARNING "fd%d: invalid disk format", drive );
1433 end_request(CURRENT, 0);
1436 if (minor2disktype[type].drive_types > DriveType) {
1437 printk(KERN_WARNING "fd%d: unsupported disk format", drive );
1438 end_request(CURRENT, 0);
1441 type = minor2disktype[type].index;
1442 UDT = &disk_type[type];
1443 set_capacity(floppy->disk, UDT->blocks);
1447 if (CURRENT->sector + 1 > UDT->blocks) {
1448 end_request(CURRENT, 0);
1452 /* stop deselect timer */
1453 del_timer( &motor_off_timer );
1456 ReqCmd = rq_data_dir(CURRENT);
1457 ReqBlock = CURRENT->sector;
1458 ReqBuffer = CURRENT->buffer;
1459 setup_req_params( drive );
1460 do_fd_action( drive );
1469 void do_fd_request(request_queue_t * q)
1471 unsigned long flags;
1473 DPRINT(("do_fd_request for pid %d\n",current->pid));
1474 while( fdc_busy ) sleep_on( &fdc_wait );
1476 stdma_lock(floppy_irq, NULL);
1478 atari_disable_irq( IRQ_MFP_FDC );
1479 local_save_flags(flags); /* The request function is called with ints
1480 local_irq_disable(); * disabled... so must save the IPL for later */
1482 local_irq_restore(flags);
1483 atari_enable_irq( IRQ_MFP_FDC );
1486 static int fd_ioctl(struct inode *inode, struct file *filp,
1487 unsigned int cmd, unsigned long param)
1489 struct gendisk *disk = inode->i_bdev->bd_disk;
1490 struct atari_floppy_struct *floppy = disk->private_data;
1491 int drive = floppy - unit;
1492 int type = floppy->type;
1493 struct atari_format_descr fmt_desc;
1494 struct atari_disk_type *dtp;
1495 struct floppy_struct getprm;
1497 struct floppy_struct setprm;
1498 void __user *argp = (void __user *)param;
1503 if (--type >= NUM_DISK_MINORS)
1505 if (minor2disktype[type].drive_types > DriveType)
1507 type = minor2disktype[type].index;
1508 dtp = &disk_type[type];
1509 if (UD.flags & FTD_MSG)
1510 printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1511 drive, dtp, dtp->name);
1519 memset((void *)&getprm, 0, sizeof(getprm));
1520 getprm.size = dtp->blocks;
1521 getprm.sect = dtp->spt;
1523 getprm.track = dtp->blocks/dtp->spt/2;
1524 getprm.stretch = dtp->stretch;
1525 if (copy_to_user(argp, &getprm, sizeof(getprm)))
1533 * MSch 7/96: simple 'set geometry' case: just set the
1534 * 'default' device params (minor == 0).
1535 * Currently, the drive geometry is cleared after each
1536 * disk change and subsequent revalidate()! simple
1537 * implementation of FDDEFPRM: save geometry from a
1538 * FDDEFPRM call and restore it in floppy_revalidate() !
1541 /* get the parameters from user space */
1542 if (floppy->ref != 1 && floppy->ref != -1)
1544 if (copy_from_user(&setprm, argp, sizeof(setprm)))
1547 * first of all: check for floppy change and revalidate,
1548 * or the next access will revalidate - and clear UDT :-(
1551 if (check_floppy_change(disk))
1552 floppy_revalidate(disk);
1554 if (UD.flags & FTD_MSG)
1555 printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1556 drive, setprm.size, setprm.sect, setprm.stretch);
1558 /* what if type > 0 here? Overwrite specified entry ? */
1560 /* refuse to re-set a predefined type for now */
1566 * type == 0: first look for a matching entry in the type list,
1567 * and set the UD.disktype field to use the perdefined entry.
1568 * TODO: add user-defined format to head of autoprobe list ?
1569 * Useful to include the user-type for future autodetection!
1572 for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1574 if (minor2disktype[settype].drive_types > DriveType) {
1575 /* skip this one, invalid for drive ... */
1578 setidx = minor2disktype[settype].index;
1579 dtp = &disk_type[setidx];
1581 /* found matching entry ?? */
1582 if ( dtp->blocks == setprm.size
1583 && dtp->spt == setprm.sect
1584 && dtp->stretch == setprm.stretch ) {
1585 if (UD.flags & FTD_MSG)
1586 printk (KERN_INFO "floppy%d: setting %s %p!\n",
1587 drive, dtp->name, dtp);
1589 set_capacity(floppy->disk, UDT->blocks);
1591 if (cmd == FDDEFPRM) {
1592 /* save settings as permanent default type */
1593 default_params[drive].name = dtp->name;
1594 default_params[drive].spt = dtp->spt;
1595 default_params[drive].blocks = dtp->blocks;
1596 default_params[drive].fdc_speed = dtp->fdc_speed;
1597 default_params[drive].stretch = dtp->stretch;
1605 /* no matching disk type found above - setting user_params */
1607 if (cmd == FDDEFPRM) {
1608 /* set permanent type */
1609 dtp = &default_params[drive];
1611 /* set user type (reset by disk change!) */
1612 dtp = &user_params[drive];
1614 dtp->name = "user format";
1615 dtp->blocks = setprm.size;
1616 dtp->spt = setprm.sect;
1617 if (setprm.sect > 14)
1621 dtp->stretch = setprm.stretch;
1623 if (UD.flags & FTD_MSG)
1624 printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1625 drive, dtp->blocks, dtp->spt, dtp->stretch);
1628 if (!dtp || setprm.track != dtp->blocks/dtp->spt/2 ||
1635 set_capacity(floppy->disk, UDT->blocks);
1639 UD.flags |= FTD_MSG;
1642 UD.flags &= ~FTD_MSG;
1644 case FDSETEMSGTRESH:
1649 if (floppy->ref != 1 && floppy->ref != -1)
1651 if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
1653 return do_format(drive, type, &fmt_desc);
1656 /* MSch: invalidate default_params */
1657 default_params[drive].blocks = 0;
1658 set_capacity(floppy->disk, MAX_DISK_SIZE * 2);
1661 /* invalidate the buffer track to force a reread */
1663 set_bit(drive, &fake_change);
1664 check_disk_change(inode->i_bdev);
1672 /* Initialize the 'unit' variable for drive 'drive' */
1674 static void __init fd_probe( int drive )
1679 if (!fd_test_drive_present( drive ))
1684 switch( UserSteprate[drive] ) {
1686 UD.steprate = FDCSTEP_2;
1689 UD.steprate = FDCSTEP_3;
1692 UD.steprate = FDCSTEP_6;
1695 UD.steprate = FDCSTEP_12;
1697 default: /* should be -1 for "not set by user" */
1698 if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1699 UD.steprate = FDCSTEP_3;
1701 UD.steprate = FDCSTEP_6;
1704 MotorOn = 1; /* from probe restore operation! */
1708 /* This function tests the physical presence of a floppy drive (not
1709 * whether a disk is inserted). This is done by issuing a restore
1710 * command, waiting max. 2 seconds (that should be enough to move the
1711 * head across the whole disk) and looking at the state of the "TR00"
1712 * signal. This should now be raised if there is a drive connected
1713 * (and there is no hardware failure :-) Otherwise, the drive is
1717 static int __init fd_test_drive_present( int drive )
1719 unsigned long timeout;
1720 unsigned char status;
1723 if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1724 fd_select_drive( drive );
1726 /* disable interrupt temporarily */
1727 atari_turnoff_irq( IRQ_MFP_FDC );
1728 FDC_WRITE (FDCREG_TRACK, 0xff00);
1729 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1731 timeout = jiffies + 2*HZ+HZ/2;
1732 while (time_before(jiffies, timeout))
1733 if (!(mfp.par_dt_reg & 0x20))
1736 status = FDC_READ( FDCREG_STATUS );
1737 ok = (status & FDCSTAT_TR00) != 0;
1739 /* force interrupt to abort restore operation (FDC would try
1740 * about 50 seconds!) */
1741 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1743 status = FDC_READ( FDCREG_STATUS );
1747 /* dummy seek command to make WP bit accessible */
1748 FDC_WRITE( FDCREG_DATA, 0 );
1749 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
1750 while( mfp.par_dt_reg & 0x20 )
1752 status = FDC_READ( FDCREG_STATUS );
1755 atari_turnon_irq( IRQ_MFP_FDC );
1760 /* Look how many and which kind of drives are connected. If there are
1761 * floppies, additionally start the disk-change and motor-off timers.
1764 static void __init config_types( void )
1768 /* for probing drives, set the FDC speed to 8 MHz */
1769 if (ATARIHW_PRESENT(FDCSPEED))
1770 dma_wd.fdc_speed = 0;
1772 printk(KERN_INFO "Probing floppy drive(s):\n");
1773 for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1776 printk(KERN_INFO "fd%d\n", drive);
1781 if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1782 /* If FDC is still busy from probing, give it another FORCI
1783 * command to abort the operation. If this isn't done, the FDC
1784 * will interrupt later and its IRQ line stays low, because
1785 * the status register isn't read. And this will block any
1786 * interrupts on this IRQ line :-(
1788 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1790 FDC_READ( FDCREG_STATUS );
1795 start_motor_off_timer();
1796 if (cnt == 1) fd_select_drive( 0 );
1797 start_check_change_timer();
1802 * floppy_open check for aliasing (/dev/fd0 can be the same as
1803 * /dev/PS0 etc), and disallows simultaneous access to the same
1804 * drive with different device numbers.
1807 static int floppy_open( struct inode *inode, struct file *filp )
1809 struct atari_floppy_struct *p = inode->i_bdev->bd_disk->private_data;
1810 int type = iminor(inode) >> 2;
1812 DPRINT(("fd_open: type=%d\n",type));
1813 if (p->ref && p->type != type)
1816 if (p->ref == -1 || (p->ref && filp->f_flags & O_EXCL))
1819 if (filp->f_flags & O_EXCL)
1826 if (filp->f_flags & O_NDELAY)
1829 if (filp->f_mode & 3) {
1830 check_disk_change(inode->i_bdev);
1831 if (filp->f_mode & 2) {
1837 floppy_release(inode, filp);
1846 static int floppy_release( struct inode * inode, struct file * filp )
1848 struct atari_floppy_struct *p = inode->i_bdev->bd_disk->private_data;
1851 else if (!p->ref--) {
1852 printk(KERN_ERR "floppy_release with fd_ref == 0");
1858 static struct block_device_operations floppy_fops = {
1859 .owner = THIS_MODULE,
1860 .open = floppy_open,
1861 .release = floppy_release,
1863 .media_changed = check_floppy_change,
1864 .revalidate_disk= floppy_revalidate,
1867 static struct kobject *floppy_find(dev_t dev, int *part, void *data)
1869 int drive = *part & 3;
1870 int type = *part >> 2;
1871 if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1874 return get_disk(unit[drive].disk);
1877 static int __init atari_floppy_init (void)
1882 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
1886 /* Hades doesn't have Atari-compatible floppy */
1889 if (register_blkdev(FLOPPY_MAJOR,"fd"))
1892 for (i = 0; i < FD_MAX_UNITS; i++) {
1893 unit[i].disk = alloc_disk(1);
1898 if (UseTrackbuffer < 0)
1899 /* not set by user -> use default: for now, we turn
1900 track buffering off for all Medusas, though it
1901 could be used with ones that have a counter
1902 card. But the test is too hard :-( */
1903 UseTrackbuffer = !MACH_IS_MEDUSA;
1905 /* initialize variables */
1909 DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
1911 printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
1914 TrackBuffer = DMABuffer + 512;
1915 PhysDMABuffer = virt_to_phys(DMABuffer);
1916 PhysTrackBuffer = virt_to_phys(TrackBuffer);
1917 BufferDrive = BufferSide = BufferTrack = -1;
1919 floppy_queue = blk_init_queue(do_fd_request, &ataflop_lock);
1923 for (i = 0; i < FD_MAX_UNITS; i++) {
1926 unit[i].disk->major = FLOPPY_MAJOR;
1927 unit[i].disk->first_minor = i;
1928 sprintf(unit[i].disk->disk_name, "fd%d", i);
1929 unit[i].disk->fops = &floppy_fops;
1930 unit[i].disk->private_data = &unit[i];
1931 unit[i].disk->queue = floppy_queue;
1932 set_capacity(unit[i].disk, MAX_DISK_SIZE * 2);
1933 add_disk(unit[i].disk);
1936 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
1937 floppy_find, NULL, NULL);
1939 printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
1940 DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
1941 UseTrackbuffer ? "" : "no ");
1947 put_disk(unit[i].disk);
1949 blk_cleanup_queue(floppy_queue);
1950 unregister_blkdev(FLOPPY_MAJOR, "fd");
1955 static int __init atari_floppy_setup(char *str)
1957 int ints[3 + FD_MAX_UNITS];
1963 str = get_options(str, 3 + FD_MAX_UNITS, ints);
1966 printk(KERN_ERR "ataflop_setup: no arguments!\n" );
1969 else if (ints[0] > 2+FD_MAX_UNITS) {
1970 printk(KERN_ERR "ataflop_setup: too many arguments\n" );
1973 if (ints[1] < 0 || ints[1] > 2)
1974 printk(KERN_ERR "ataflop_setup: bad drive type\n" );
1976 DriveType = ints[1];
1979 UseTrackbuffer = (ints[2] > 0);
1981 for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
1982 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
1983 printk(KERN_ERR "ataflop_setup: bad steprate\n" );
1985 UserSteprate[i-3] = ints[i];
1990 __setup("floppy=", atari_floppy_setup);
1993 static void __exit atari_floppy_exit(void)
1996 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
1997 for (i = 0; i < FD_MAX_UNITS; i++) {
1998 del_gendisk(unit[i].disk);
1999 put_disk(unit[i].disk);
2001 unregister_blkdev(FLOPPY_MAJOR, "fd");
2003 blk_cleanup_queue(floppy_queue);
2004 del_timer_sync(&fd_timer);
2005 atari_stram_free( DMABuffer );
2008 module_init(atari_floppy_init)
2009 module_exit(atari_floppy_exit)
2011 MODULE_LICENSE("GPL");