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
;
82 static struct request
*fd_request
;
84 /* Disk types: DD, HD, ED */
85 static struct atari_disk_type
{
87 unsigned spt
; /* sectors per track */
88 unsigned blocks
; /* total number of blocks */
89 unsigned fdc_speed
; /* fdc_speed setting */
90 unsigned stretch
; /* track doubling ? */
91 } atari_disk_type
[] = {
92 { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
93 { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
94 { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
95 { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
96 /* formats above are probed for type DD */
98 { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
99 { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
100 { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
101 /* formats above are probed for types DD and HD */
102 #define MAX_TYPE_HD 6
103 { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
104 { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
105 /* formats above are probed for types DD, HD and ED */
106 #define MAX_TYPE_ED 8
107 /* types below are never autoprobed */
108 { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
109 { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
110 { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
111 { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
112 { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
113 { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
114 { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
115 { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
116 { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
117 { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
118 { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
119 { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
120 { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
121 { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
122 { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
123 { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
124 { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
125 { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
126 { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
129 static int StartDiskType
[] = {
139 static int DriveType
= TYPE_HD
;
141 static DEFINE_SPINLOCK(ataflop_lock
);
143 /* Array for translating minors into disk formats */
146 unsigned drive_types
;
147 } minor2disktype
[] = {
148 { 0, TYPE_DD
}, /* 1: d360 */
149 { 4, TYPE_HD
}, /* 2: h1200 */
150 { 1, TYPE_DD
}, /* 3: D360 */
151 { 2, TYPE_DD
}, /* 4: D720 */
152 { 1, TYPE_DD
}, /* 5: h360 = D360 */
153 { 2, TYPE_DD
}, /* 6: h720 = D720 */
154 { 5, TYPE_HD
}, /* 7: H1440 */
155 { 7, TYPE_ED
}, /* 8: E2880 */
156 /* some PC formats :-) */
157 { 8, TYPE_ED
}, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
158 { 5, TYPE_HD
}, /* 10: h1440 = H1440 */
159 { 9, TYPE_HD
}, /* 11: H1680 */
160 { 10, TYPE_DD
}, /* 12: h410 */
161 { 3, TYPE_DD
}, /* 13: H820 <- == D820, 82x10 */
162 { 11, TYPE_HD
}, /* 14: h1476 */
163 { 12, TYPE_HD
}, /* 15: H1722 */
164 { 13, TYPE_DD
}, /* 16: h420 */
165 { 14, TYPE_DD
}, /* 17: H830 */
166 { 15, TYPE_HD
}, /* 18: h1494 */
167 { 16, TYPE_HD
}, /* 19: H1743 */
168 { 17, TYPE_DD
}, /* 20: h880 */
169 { 18, TYPE_DD
}, /* 21: D1040 */
170 { 19, TYPE_DD
}, /* 22: D1120 */
171 { 20, TYPE_HD
}, /* 23: h1600 */
172 { 21, TYPE_HD
}, /* 24: H1760 */
173 { 22, TYPE_HD
}, /* 25: H1920 */
174 { 23, TYPE_ED
}, /* 26: E3200 */
175 { 24, TYPE_ED
}, /* 27: E3520 */
176 { 25, TYPE_ED
}, /* 28: E3840 */
177 { 26, TYPE_HD
}, /* 29: H1840 */
178 { 27, TYPE_DD
}, /* 30: D800 */
179 { 6, TYPE_HD
}, /* 31: H1640 <- was H1600 == h1600 for PC */
182 #define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
185 * Maximum disk size (in kilobytes). This default is used whenever the
186 * current disk size is unknown.
188 #define MAX_DISK_SIZE 3280
191 * MSch: User-provided type information. 'drive' points to
192 * the respective entry of this array. Set by FDSETPRM ioctls.
194 static struct atari_disk_type user_params
[FD_MAX_UNITS
];
197 * User-provided permanent type information. 'drive' points to
198 * the respective entry of this array. Set by FDDEFPRM ioctls,
199 * restored upon disk change by floppy_revalidate() if valid (as seen by
200 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
202 static struct atari_disk_type default_params
[FD_MAX_UNITS
];
204 /* current info on each unit */
205 static struct atari_floppy_struct
{
206 int connected
; /* !=0 : drive is connected */
207 int autoprobe
; /* !=0 : do autoprobe */
209 struct atari_disk_type
*disktype
; /* current type of disk */
211 int track
; /* current head position or -1 if
213 unsigned int steprate
; /* steprate setting */
214 unsigned int wpstat
; /* current state of WP signal (for
215 disk change detection) */
216 int flags
; /* flags */
217 struct gendisk
*disk
;
220 } unit
[FD_MAX_UNITS
];
222 #define UD unit[drive]
223 #define UDT unit[drive].disktype
224 #define SUD unit[SelectedDrive]
225 #define SUDT unit[SelectedDrive].disktype
228 #define FDC_READ(reg) ({ \
229 /* unsigned long __flags; */ \
230 unsigned short __val; \
231 /* local_irq_save(__flags); */ \
232 dma_wd.dma_mode_status = 0x80 | (reg); \
234 __val = dma_wd.fdc_acces_seccount; \
236 /* local_irq_restore(__flags); */ \
240 #define FDC_WRITE(reg,val) \
242 /* unsigned long __flags; */ \
243 /* local_irq_save(__flags); */ \
244 dma_wd.dma_mode_status = 0x80 | (reg); \
246 dma_wd.fdc_acces_seccount = (val); \
248 /* local_irq_restore(__flags); */ \
252 /* Buffering variables:
253 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
254 * operations. Second, a track buffer is used to cache a whole track
255 * of the disk to save read operations. These are two separate buffers
256 * because that allows write operations without clearing the track buffer.
259 static int MaxSectors
[] = {
262 static int BufferSize
[] = {
263 15*512, 30*512, 60*512
266 #define BUFFER_SIZE (BufferSize[DriveType])
268 unsigned char *DMABuffer
; /* buffer for writes */
269 static unsigned long PhysDMABuffer
; /* physical address */
271 static int UseTrackbuffer
= -1; /* Do track buffering? */
272 module_param(UseTrackbuffer
, int, 0);
274 unsigned char *TrackBuffer
; /* buffer for reads */
275 static unsigned long PhysTrackBuffer
; /* physical address */
276 static int BufferDrive
, BufferSide
, BufferTrack
;
277 static int read_track
; /* non-zero if we are reading whole tracks */
279 #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
280 #define IS_BUFFERED(drive,side,track) \
281 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
284 * These are global variables, as that's the easiest way to give
285 * information to interrupts. They are the data used for the current
288 static int SelectedDrive
= 0;
289 static int ReqCmd
, ReqBlock
;
290 static int ReqSide
, ReqTrack
, ReqSector
, ReqCnt
;
291 static int HeadSettleFlag
= 0;
292 static unsigned char *ReqData
, *ReqBuffer
;
293 static int MotorOn
= 0, MotorOffTrys
;
294 static int IsFormatting
= 0, FormatError
;
296 static int UserSteprate
[FD_MAX_UNITS
] = { -1, -1 };
297 module_param_array(UserSteprate
, int, NULL
, 0);
299 /* Synchronization of FDC access. */
300 static volatile int fdc_busy
= 0;
301 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait
);
302 static DECLARE_WAIT_QUEUE_HEAD(format_wait
);
304 static unsigned long changed_floppies
= 0xff, fake_change
= 0;
305 #define CHECK_CHANGE_DELAY HZ/2
307 #define FD_MOTOR_OFF_DELAY (3*HZ)
308 #define FD_MOTOR_OFF_MAXTRY (10*20)
310 #define FLOPPY_TIMEOUT (6*HZ)
311 #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
312 * will be recalibrated. */
313 #define MAX_ERRORS 8 /* After this many errors the driver
318 * The driver is trying to determine the correct media format
319 * while Probing is set. fd_rwsec_done() clears it after a
322 static int Probing
= 0;
324 /* This flag is set when a dummy seek is necessary to make the WP
325 * status bit accessible.
327 static int NeedSeek
= 0;
331 #define DPRINT(a) printk a
336 /***************************** Prototypes *****************************/
338 static void fd_select_side( int side
);
339 static void fd_select_drive( int drive
);
340 static void fd_deselect( void );
341 static void fd_motor_off_timer( unsigned long dummy
);
342 static void check_change( unsigned long dummy
);
343 static irqreturn_t
floppy_irq (int irq
, void *dummy
);
344 static void fd_error( void );
345 static int do_format(int drive
, int type
, struct atari_format_descr
*desc
);
346 static void do_fd_action( int drive
);
347 static void fd_calibrate( void );
348 static void fd_calibrate_done( int status
);
349 static void fd_seek( void );
350 static void fd_seek_done( int status
);
351 static void fd_rwsec( void );
352 static void fd_readtrack_check( unsigned long dummy
);
353 static void fd_rwsec_done( int status
);
354 static void fd_rwsec_done1(int status
);
355 static void fd_writetrack( void );
356 static void fd_writetrack_done( int status
);
357 static void fd_times_out( unsigned long dummy
);
358 static void finish_fdc( void );
359 static void finish_fdc_done( int dummy
);
360 static void setup_req_params( int drive
);
361 static void redo_fd_request( void);
362 static int fd_ioctl(struct block_device
*bdev
, fmode_t mode
, unsigned int
363 cmd
, unsigned long param
);
364 static void fd_probe( int drive
);
365 static int fd_test_drive_present( int drive
);
366 static void config_types( void );
367 static int floppy_open(struct block_device
*bdev
, fmode_t mode
);
368 static int floppy_release(struct gendisk
*disk
, fmode_t mode
);
370 /************************* End of Prototypes **************************/
372 static DEFINE_TIMER(motor_off_timer
, fd_motor_off_timer
, 0, 0);
373 static DEFINE_TIMER(readtrack_timer
, fd_readtrack_check
, 0, 0);
374 static DEFINE_TIMER(timeout_timer
, fd_times_out
, 0, 0);
375 static DEFINE_TIMER(fd_timer
, check_change
, 0, 0);
377 static void fd_end_request_cur(int err
)
379 if (!__blk_end_request_cur(fd_request
, err
))
383 static inline void start_motor_off_timer(void)
385 mod_timer(&motor_off_timer
, jiffies
+ FD_MOTOR_OFF_DELAY
);
389 static inline void start_check_change_timer( void )
391 mod_timer(&fd_timer
, jiffies
+ CHECK_CHANGE_DELAY
);
394 static inline void start_timeout(void)
396 mod_timer(&timeout_timer
, jiffies
+ FLOPPY_TIMEOUT
);
399 static inline void stop_timeout(void)
401 del_timer(&timeout_timer
);
404 /* Select the side to use. */
406 static void fd_select_side( int side
)
410 /* protect against various other ints mucking around with the PSG */
411 local_irq_save(flags
);
413 sound_ym
.rd_data_reg_sel
= 14; /* Select PSG Port A */
414 sound_ym
.wd_data
= (side
== 0) ? sound_ym
.rd_data_reg_sel
| 0x01 :
415 sound_ym
.rd_data_reg_sel
& 0xfe;
417 local_irq_restore(flags
);
421 /* Select a drive, update the FDC's track register and set the correct
422 * clock speed for this disk's type.
425 static void fd_select_drive( int drive
)
430 if (drive
== SelectedDrive
)
433 /* protect against various other ints mucking around with the PSG */
434 local_irq_save(flags
);
435 sound_ym
.rd_data_reg_sel
= 14; /* Select PSG Port A */
436 tmp
= sound_ym
.rd_data_reg_sel
;
437 sound_ym
.wd_data
= (tmp
| DSKDRVNONE
) & ~(drive
== 0 ? DSKDRV0
: DSKDRV1
);
438 atari_dont_touch_floppy_select
= 1;
439 local_irq_restore(flags
);
441 /* restore track register to saved value */
442 FDC_WRITE( FDCREG_TRACK
, UD
.track
);
445 /* select 8/16 MHz */
447 if (ATARIHW_PRESENT(FDCSPEED
))
448 dma_wd
.fdc_speed
= UDT
->fdc_speed
;
450 SelectedDrive
= drive
;
454 /* Deselect both drives. */
456 static void fd_deselect( void )
460 /* protect against various other ints mucking around with the PSG */
461 local_irq_save(flags
);
462 atari_dont_touch_floppy_select
= 0;
463 sound_ym
.rd_data_reg_sel
=14; /* Select PSG Port A */
464 sound_ym
.wd_data
= (sound_ym
.rd_data_reg_sel
|
465 (MACH_IS_FALCON
? 3 : 7)); /* no drives selected */
466 /* On Falcon, the drive B select line is used on the printer port, so
467 * leave it alone... */
469 local_irq_restore(flags
);
473 /* This timer function deselects the drives when the FDC switched the
474 * motor off. The deselection cannot happen earlier because the FDC
475 * counts the index signals, which arrive only if one drive is selected.
478 static void fd_motor_off_timer( unsigned long dummy
)
480 unsigned char status
;
482 if (SelectedDrive
< 0)
483 /* no drive selected, needn't deselect anyone */
486 if (stdma_islocked())
489 status
= FDC_READ( FDCREG_STATUS
);
491 if (!(status
& 0x80)) {
492 /* motor already turned off by FDC -> deselect drives */
497 /* not yet off, try again */
500 /* Test again later; if tested too often, it seems there is no disk
501 * in the drive and the FDC will leave the motor on forever (or,
502 * at least until a disk is inserted). So we'll test only twice
503 * per second from then on...
505 mod_timer(&motor_off_timer
,
506 jiffies
+ (MotorOffTrys
++ < FD_MOTOR_OFF_MAXTRY
? HZ
/20 : HZ
/2));
510 /* This function is repeatedly called to detect disk changes (as good
511 * as possible) and keep track of the current state of the write protection.
514 static void check_change( unsigned long dummy
)
516 static int drive
= 0;
519 unsigned char old_porta
;
522 if (++drive
> 1 || !UD
.connected
)
525 /* protect against various other ints mucking around with the PSG */
526 local_irq_save(flags
);
528 if (!stdma_islocked()) {
529 sound_ym
.rd_data_reg_sel
= 14;
530 old_porta
= sound_ym
.rd_data_reg_sel
;
531 sound_ym
.wd_data
= (old_porta
| DSKDRVNONE
) &
532 ~(drive
== 0 ? DSKDRV0
: DSKDRV1
);
533 stat
= !!(FDC_READ( FDCREG_STATUS
) & FDCSTAT_WPROT
);
534 sound_ym
.wd_data
= old_porta
;
536 if (stat
!= UD
.wpstat
) {
537 DPRINT(( "wpstat[%d] = %d\n", drive
, stat
));
539 set_bit (drive
, &changed_floppies
);
542 local_irq_restore(flags
);
544 start_check_change_timer();
548 /* Handling of the Head Settling Flag: This flag should be set after each
549 * seek operation, because we don't use seeks with verify.
552 static inline void set_head_settle_flag(void)
554 HeadSettleFlag
= FDCCMDADD_E
;
557 static inline int get_head_settle_flag(void)
559 int tmp
= HeadSettleFlag
;
564 static inline void copy_buffer(void *from
, void *to
)
566 ulong
*p1
= (ulong
*)from
, *p2
= (ulong
*)to
;
569 for (cnt
= 512/4; cnt
; cnt
--)
576 /* General Interrupt Handling */
578 static void (*FloppyIRQHandler
)( int status
) = NULL
;
580 static irqreturn_t
floppy_irq (int irq
, void *dummy
)
582 unsigned char status
;
583 void (*handler
)( int );
585 handler
= xchg(&FloppyIRQHandler
, NULL
);
589 status
= FDC_READ( FDCREG_STATUS
);
590 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status
,(unsigned long)handler
));
594 DPRINT(("FDC irq, no handler\n"));
600 /* Error handling: If some error happened, retry some times, then
601 * recalibrate, then try again, and fail after MAX_ERRORS.
604 static void fd_error( void )
609 wake_up( &format_wait
);
616 fd_request
->errors
++;
617 if (fd_request
->errors
>= MAX_ERRORS
) {
618 printk(KERN_ERR
"fd%d: too many errors.\n", SelectedDrive
);
619 fd_end_request_cur(-EIO
);
621 else if (fd_request
->errors
== RECALIBRATE_ERRORS
) {
622 printk(KERN_WARNING
"fd%d: recalibrating\n", SelectedDrive
);
623 if (SelectedDrive
!= -1)
631 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
634 /* ---------- Formatting ---------- */
636 #define FILL(n,val) \
638 memset( p, val, n ); \
642 static int do_format(int drive
, int type
, struct atari_format_descr
*desc
)
648 DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
649 drive
, desc
->track
, desc
->head
, desc
->sect_offset
));
651 local_irq_save(flags
);
652 while( fdc_busy
) sleep_on( &fdc_wait
);
654 stdma_lock(floppy_irq
, NULL
);
655 atari_turnon_irq( IRQ_MFP_FDC
); /* should be already, just to be sure */
656 local_irq_restore(flags
);
659 if (--type
>= NUM_DISK_MINORS
||
660 minor2disktype
[type
].drive_types
> DriveType
) {
664 type
= minor2disktype
[type
].index
;
665 UDT
= &atari_disk_type
[type
];
668 if (!UDT
|| desc
->track
>= UDT
->blocks
/UDT
->spt
/2 || desc
->head
>= 2) {
675 /* The track buffer is used for the raw track data, so its
676 contents become invalid! */
678 /* stop deselect timer */
679 del_timer( &motor_off_timer
);
681 FILL( 60 * (nsect
/ 9), 0x4e );
682 for( sect
= 0; sect
< nsect
; ++sect
) {
688 *p
++ = (nsect
+ sect
- desc
->sect_offset
) % nsect
+ 1;
699 FILL( TrackBuffer
+BUFFER_SIZE
-p
, 0x4e );
703 ReqTrack
= desc
->track
;
704 ReqSide
= desc
->head
;
705 do_fd_action( drive
);
707 sleep_on( &format_wait
);
710 return( FormatError
? -EIO
: 0 );
714 /* do_fd_action() is the general procedure for a fd request: All
715 * required parameter settings (drive select, side select, track
716 * position) are checked and set if needed. For each of these
717 * parameters and the actual reading or writing exist two functions:
718 * one that starts the setting (or skips it if possible) and one
719 * callback for the "done" interrupt. Each done func calls the next
720 * set function to propagate the request down to fd_rwsec_done().
723 static void do_fd_action( int drive
)
725 DPRINT(("do_fd_action\n"));
727 if (UseTrackbuffer
&& !IsFormatting
) {
729 if (IS_BUFFERED( drive
, ReqSide
, ReqTrack
)) {
730 if (ReqCmd
== READ
) {
731 copy_buffer( SECTOR_BUFFER(ReqSector
), ReqData
);
732 if (++ReqCnt
< blk_rq_cur_sectors(fd_request
)) {
733 /* read next sector */
734 setup_req_params( drive
);
738 /* all sectors finished */
739 fd_end_request_cur(0);
745 /* cmd == WRITE, pay attention to track buffer
747 copy_buffer( ReqData
, SECTOR_BUFFER(ReqSector
) );
752 if (SelectedDrive
!= drive
)
753 fd_select_drive( drive
);
757 else if (UD
.track
!= ReqTrack
<< UDT
->stretch
)
759 else if (IsFormatting
)
766 /* Seek to track 0 if the current track is unknown */
768 static void fd_calibrate( void )
770 if (SUD
.track
>= 0) {
771 fd_calibrate_done( 0 );
775 if (ATARIHW_PRESENT(FDCSPEED
))
776 dma_wd
.fdc_speed
= 0; /* always seek with 8 Mhz */;
777 DPRINT(("fd_calibrate\n"));
778 SET_IRQ_HANDLER( fd_calibrate_done
);
779 /* we can't verify, since the speed may be incorrect */
780 FDC_WRITE( FDCREG_CMD
, FDCCMD_RESTORE
| SUD
.steprate
);
789 static void fd_calibrate_done( int status
)
791 DPRINT(("fd_calibrate_done()\n"));
794 /* set the correct speed now */
795 if (ATARIHW_PRESENT(FDCSPEED
))
796 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
797 if (status
& FDCSTAT_RECNF
) {
798 printk(KERN_ERR
"fd%d: restore failed\n", SelectedDrive
);
808 /* Seek the drive to the requested track. The drive must have been
809 * calibrated at some point before this.
812 static void fd_seek( void )
814 if (SUD
.track
== ReqTrack
<< SUDT
->stretch
) {
819 if (ATARIHW_PRESENT(FDCSPEED
)) {
820 dma_wd
.fdc_speed
= 0; /* always seek witch 8 Mhz */
824 DPRINT(("fd_seek() to track %d\n",ReqTrack
));
825 FDC_WRITE( FDCREG_DATA
, ReqTrack
<< SUDT
->stretch
);
827 SET_IRQ_HANDLER( fd_seek_done
);
828 FDC_WRITE( FDCREG_CMD
, FDCCMD_SEEK
| SUD
.steprate
);
831 set_head_settle_flag();
837 static void fd_seek_done( int status
)
839 DPRINT(("fd_seek_done()\n"));
842 /* set the correct speed */
843 if (ATARIHW_PRESENT(FDCSPEED
))
844 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
845 if (status
& FDCSTAT_RECNF
) {
846 printk(KERN_ERR
"fd%d: seek error (to track %d)\n",
847 SelectedDrive
, ReqTrack
);
848 /* we don't know exactly which track we are on now! */
853 SUD
.track
= ReqTrack
<< SUDT
->stretch
;
863 /* This does the actual reading/writing after positioning the head
864 * over the correct track.
867 static int MultReadInProgress
= 0;
870 static void fd_rwsec( void )
872 unsigned long paddr
, flags
;
873 unsigned int rwflag
, old_motoron
;
876 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector
, ReqCmd
== WRITE
? 'w' : 'r' ));
877 if (ReqCmd
== WRITE
) {
878 if (ATARIHW_PRESENT(EXTD_DMA
)) {
879 paddr
= virt_to_phys(ReqData
);
882 copy_buffer( ReqData
, DMABuffer
);
883 paddr
= PhysDMABuffer
;
885 dma_cache_maintenance( paddr
, 512, 1 );
890 paddr
= PhysTrackBuffer
;
892 paddr
= ATARIHW_PRESENT(EXTD_DMA
) ?
893 virt_to_phys(ReqData
) : PhysDMABuffer
;
897 fd_select_side( ReqSide
);
899 /* Start sector of this operation */
900 FDC_WRITE( FDCREG_SECTOR
, read_track
? 1 : ReqSector
);
902 /* Cheat for track if stretch != 0 */
904 track
= FDC_READ( FDCREG_TRACK
);
906 FDC_WRITE( FDCREG_TRACK
, track
>> SUDT
->stretch
);
911 local_irq_save(flags
);
912 dma_wd
.dma_lo
= (unsigned char)paddr
;
915 dma_wd
.dma_md
= (unsigned char)paddr
;
918 if (ATARIHW_PRESENT(EXTD_DMA
))
919 st_dma_ext_dmahi
= (unsigned short)paddr
;
921 dma_wd
.dma_hi
= (unsigned char)paddr
;
923 local_irq_restore(flags
);
925 /* Clear FIFO and switch DMA to correct mode */
926 dma_wd
.dma_mode_status
= 0x90 | rwflag
;
928 dma_wd
.dma_mode_status
= 0x90 | (rwflag
^ 0x100);
930 dma_wd
.dma_mode_status
= 0x90 | rwflag
;
933 /* How many sectors for DMA */
934 dma_wd
.fdc_acces_seccount
= read_track
? SUDT
->spt
: 1;
938 /* Start operation */
939 dma_wd
.dma_mode_status
= FDCSELREG_STP
| rwflag
;
941 SET_IRQ_HANDLER( fd_rwsec_done
);
942 dma_wd
.fdc_acces_seccount
=
943 (get_head_settle_flag() |
944 (rwflag
? FDCCMD_WRSEC
: (FDCCMD_RDSEC
| (read_track
? FDCCMDADD_M
: 0))));
946 old_motoron
= MotorOn
;
949 /* wait for interrupt */
952 /* If reading a whole track, wait about one disk rotation and
953 * then check if all sectors are read. The FDC will even
954 * search for the first non-existent sector and need 1 sec to
955 * recognise that it isn't present :-(
957 MultReadInProgress
= 1;
958 mod_timer(&readtrack_timer
,
959 /* 1 rot. + 5 rot.s if motor was off */
960 jiffies
+ HZ
/5 + (old_motoron
? 0 : HZ
));
966 static void fd_readtrack_check( unsigned long dummy
)
968 unsigned long flags
, addr
, addr2
;
970 local_irq_save(flags
);
972 if (!MultReadInProgress
) {
973 /* This prevents a race condition that could arise if the
974 * interrupt is triggered while the calling of this timer
975 * callback function takes place. The IRQ function then has
976 * already cleared 'MultReadInProgress' when flow of control
979 local_irq_restore(flags
);
983 /* get the current DMA address */
984 /* ++ f.a. read twice to avoid being fooled by switcher */
988 addr
= dma_wd
.dma_lo
& 0xff;
990 addr
|= (dma_wd
.dma_md
& 0xff) << 8;
992 if (ATARIHW_PRESENT( EXTD_DMA
))
993 addr
|= (st_dma_ext_dmahi
& 0xffff) << 16;
995 addr
|= (dma_wd
.dma_hi
& 0xff) << 16;
997 } while(addr
!= addr2
);
999 if (addr
>= PhysTrackBuffer
+ SUDT
->spt
*512) {
1000 /* already read enough data, force an FDC interrupt to stop
1001 * the read operation
1003 SET_IRQ_HANDLER( NULL
);
1004 MultReadInProgress
= 0;
1005 local_irq_restore(flags
);
1006 DPRINT(("fd_readtrack_check(): done\n"));
1007 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1010 /* No error until now -- the FDC would have interrupted
1016 /* not yet finished, wait another tenth rotation */
1017 local_irq_restore(flags
);
1018 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1019 mod_timer(&readtrack_timer
, jiffies
+ HZ
/5/10);
1024 static void fd_rwsec_done( int status
)
1026 DPRINT(("fd_rwsec_done()\n"));
1029 del_timer(&readtrack_timer
);
1030 if (!MultReadInProgress
)
1032 MultReadInProgress
= 0;
1034 fd_rwsec_done1(status
);
1037 static void fd_rwsec_done1(int status
)
1043 /* Correct the track if stretch != 0 */
1044 if (SUDT
->stretch
) {
1045 track
= FDC_READ( FDCREG_TRACK
);
1047 FDC_WRITE( FDCREG_TRACK
, track
<< SUDT
->stretch
);
1050 if (!UseTrackbuffer
) {
1051 dma_wd
.dma_mode_status
= 0x90;
1053 if (!(dma_wd
.dma_mode_status
& 0x01)) {
1054 printk(KERN_ERR
"fd%d: DMA error\n", SelectedDrive
);
1060 if (ReqCmd
== WRITE
&& (status
& FDCSTAT_WPROT
)) {
1061 printk(KERN_NOTICE
"fd%d: is write protected\n", SelectedDrive
);
1064 if ((status
& FDCSTAT_RECNF
) &&
1065 /* RECNF is no error after a multiple read when the FDC
1066 searched for a non-existent sector! */
1067 !(read_track
&& FDC_READ(FDCREG_SECTOR
) > SUDT
->spt
)) {
1069 if (SUDT
> atari_disk_type
) {
1070 if (SUDT
[-1].blocks
> ReqBlock
) {
1071 /* try another disk type */
1073 set_capacity(unit
[SelectedDrive
].disk
,
1079 if (SUD
.flags
& FTD_MSG
)
1080 printk(KERN_INFO
"fd%d: Auto-detected floppy type %s\n",
1081 SelectedDrive
, SUDT
->name
);
1085 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1086 if (SUD
.autoprobe
) {
1087 SUDT
= atari_disk_type
+ StartDiskType
[DriveType
];
1088 set_capacity(unit
[SelectedDrive
].disk
,
1094 if (ATARIHW_PRESENT(FDCSPEED
)) {
1095 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
1098 setup_req_params( SelectedDrive
);
1100 do_fd_action( SelectedDrive
);
1104 printk(KERN_ERR
"fd%d: sector %d not found (side %d, track %d)\n",
1105 SelectedDrive
, FDC_READ (FDCREG_SECTOR
), ReqSide
, ReqTrack
);
1108 if (status
& FDCSTAT_CRC
) {
1109 printk(KERN_ERR
"fd%d: CRC error (side %d, track %d, sector %d)\n",
1110 SelectedDrive
, ReqSide
, ReqTrack
, FDC_READ (FDCREG_SECTOR
) );
1113 if (status
& FDCSTAT_LOST
) {
1114 printk(KERN_ERR
"fd%d: lost data (side %d, track %d, sector %d)\n",
1115 SelectedDrive
, ReqSide
, ReqTrack
, FDC_READ (FDCREG_SECTOR
) );
1121 if (ReqCmd
== READ
) {
1124 addr
= ATARIHW_PRESENT( EXTD_DMA
) ? ReqData
: DMABuffer
;
1125 dma_cache_maintenance( virt_to_phys(addr
), 512, 0 );
1126 if (!ATARIHW_PRESENT( EXTD_DMA
))
1127 copy_buffer (addr
, ReqData
);
1129 dma_cache_maintenance( PhysTrackBuffer
, MaxSectors
[DriveType
] * 512, 0 );
1130 BufferDrive
= SelectedDrive
;
1131 BufferSide
= ReqSide
;
1132 BufferTrack
= ReqTrack
;
1133 copy_buffer (SECTOR_BUFFER (ReqSector
), ReqData
);
1137 if (++ReqCnt
< blk_rq_cur_sectors(fd_request
)) {
1138 /* read next sector */
1139 setup_req_params( SelectedDrive
);
1140 do_fd_action( SelectedDrive
);
1143 /* all sectors finished */
1144 fd_end_request_cur(0);
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
&& fd_request
->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: fd_request=%p dev=%s fd_request->sector=%ld\n",
1400 fd_request
, fd_request
? fd_request
->rq_disk
->disk_name
: "",
1401 fd_request
? blk_rq_pos(fd_request
) : 0 ));
1407 fd_request
= blk_fetch_request(floppy_queue
);
1412 floppy
= fd_request
->rq_disk
->private_data
;
1413 drive
= floppy
- unit
;
1414 type
= floppy
->type
;
1416 if (!UD
.connected
) {
1417 /* drive not connected */
1418 printk(KERN_ERR
"Unknown Device: fd%d\n", drive
);
1419 fd_end_request_cur(-EIO
);
1426 UDT
= atari_disk_type
+ StartDiskType
[DriveType
];
1427 set_capacity(floppy
->disk
, UDT
->blocks
);
1432 /* user supplied disk type */
1433 if (--type
>= NUM_DISK_MINORS
) {
1434 printk(KERN_WARNING
"fd%d: invalid disk format", drive
);
1435 fd_end_request_cur(-EIO
);
1438 if (minor2disktype
[type
].drive_types
> DriveType
) {
1439 printk(KERN_WARNING
"fd%d: unsupported disk format", drive
);
1440 fd_end_request_cur(-EIO
);
1443 type
= minor2disktype
[type
].index
;
1444 UDT
= &atari_disk_type
[type
];
1445 set_capacity(floppy
->disk
, UDT
->blocks
);
1449 if (blk_rq_pos(fd_request
) + 1 > UDT
->blocks
) {
1450 fd_end_request_cur(-EIO
);
1454 /* stop deselect timer */
1455 del_timer( &motor_off_timer
);
1458 ReqCmd
= rq_data_dir(fd_request
);
1459 ReqBlock
= blk_rq_pos(fd_request
);
1460 ReqBuffer
= fd_request
->buffer
;
1461 setup_req_params( drive
);
1462 do_fd_action( drive
);
1471 void do_fd_request(struct request_queue
* q
)
1473 unsigned long flags
;
1475 DPRINT(("do_fd_request for pid %d\n",current
->pid
));
1476 while( fdc_busy
) sleep_on( &fdc_wait
);
1478 stdma_lock(floppy_irq
, NULL
);
1480 atari_disable_irq( IRQ_MFP_FDC
);
1482 atari_enable_irq( IRQ_MFP_FDC
);
1485 static int fd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1486 unsigned int cmd
, unsigned long param
)
1488 struct gendisk
*disk
= bdev
->bd_disk
;
1489 struct atari_floppy_struct
*floppy
= disk
->private_data
;
1490 int drive
= floppy
- unit
;
1491 int type
= floppy
->type
;
1492 struct atari_format_descr fmt_desc
;
1493 struct atari_disk_type
*dtp
;
1494 struct floppy_struct getprm
;
1496 struct floppy_struct setprm
;
1497 void __user
*argp
= (void __user
*)param
;
1502 if (--type
>= NUM_DISK_MINORS
)
1504 if (minor2disktype
[type
].drive_types
> DriveType
)
1506 type
= minor2disktype
[type
].index
;
1507 dtp
= &atari_disk_type
[type
];
1508 if (UD
.flags
& FTD_MSG
)
1509 printk (KERN_ERR
"floppy%d: found dtp %p name %s!\n",
1510 drive
, dtp
, dtp
->name
);
1518 memset((void *)&getprm
, 0, sizeof(getprm
));
1519 getprm
.size
= dtp
->blocks
;
1520 getprm
.sect
= dtp
->spt
;
1522 getprm
.track
= dtp
->blocks
/dtp
->spt
/2;
1523 getprm
.stretch
= dtp
->stretch
;
1524 if (copy_to_user(argp
, &getprm
, sizeof(getprm
)))
1532 * MSch 7/96: simple 'set geometry' case: just set the
1533 * 'default' device params (minor == 0).
1534 * Currently, the drive geometry is cleared after each
1535 * disk change and subsequent revalidate()! simple
1536 * implementation of FDDEFPRM: save geometry from a
1537 * FDDEFPRM call and restore it in floppy_revalidate() !
1540 /* get the parameters from user space */
1541 if (floppy
->ref
!= 1 && floppy
->ref
!= -1)
1543 if (copy_from_user(&setprm
, argp
, sizeof(setprm
)))
1546 * first of all: check for floppy change and revalidate,
1547 * or the next access will revalidate - and clear UDT :-(
1550 if (check_floppy_change(disk
))
1551 floppy_revalidate(disk
);
1553 if (UD
.flags
& FTD_MSG
)
1554 printk (KERN_INFO
"floppy%d: setting size %d spt %d str %d!\n",
1555 drive
, setprm
.size
, setprm
.sect
, setprm
.stretch
);
1557 /* what if type > 0 here? Overwrite specified entry ? */
1559 /* refuse to re-set a predefined type for now */
1565 * type == 0: first look for a matching entry in the type list,
1566 * and set the UD.disktype field to use the perdefined entry.
1567 * TODO: add user-defined format to head of autoprobe list ?
1568 * Useful to include the user-type for future autodetection!
1571 for (settype
= 0; settype
< NUM_DISK_MINORS
; settype
++) {
1573 if (minor2disktype
[settype
].drive_types
> DriveType
) {
1574 /* skip this one, invalid for drive ... */
1577 setidx
= minor2disktype
[settype
].index
;
1578 dtp
= &atari_disk_type
[setidx
];
1580 /* found matching entry ?? */
1581 if ( dtp
->blocks
== setprm
.size
1582 && dtp
->spt
== setprm
.sect
1583 && dtp
->stretch
== setprm
.stretch
) {
1584 if (UD
.flags
& FTD_MSG
)
1585 printk (KERN_INFO
"floppy%d: setting %s %p!\n",
1586 drive
, dtp
->name
, dtp
);
1588 set_capacity(floppy
->disk
, UDT
->blocks
);
1590 if (cmd
== FDDEFPRM
) {
1591 /* save settings as permanent default type */
1592 default_params
[drive
].name
= dtp
->name
;
1593 default_params
[drive
].spt
= dtp
->spt
;
1594 default_params
[drive
].blocks
= dtp
->blocks
;
1595 default_params
[drive
].fdc_speed
= dtp
->fdc_speed
;
1596 default_params
[drive
].stretch
= dtp
->stretch
;
1604 /* no matching disk type found above - setting user_params */
1606 if (cmd
== FDDEFPRM
) {
1607 /* set permanent type */
1608 dtp
= &default_params
[drive
];
1610 /* set user type (reset by disk change!) */
1611 dtp
= &user_params
[drive
];
1613 dtp
->name
= "user format";
1614 dtp
->blocks
= setprm
.size
;
1615 dtp
->spt
= setprm
.sect
;
1616 if (setprm
.sect
> 14)
1620 dtp
->stretch
= setprm
.stretch
;
1622 if (UD
.flags
& FTD_MSG
)
1623 printk (KERN_INFO
"floppy%d: blk %d spt %d str %d!\n",
1624 drive
, dtp
->blocks
, dtp
->spt
, dtp
->stretch
);
1627 if (setprm
.track
!= dtp
->blocks
/dtp
->spt
/2 ||
1634 set_capacity(floppy
->disk
, UDT
->blocks
);
1638 UD
.flags
|= FTD_MSG
;
1641 UD
.flags
&= ~FTD_MSG
;
1643 case FDSETEMSGTRESH
:
1648 if (floppy
->ref
!= 1 && floppy
->ref
!= -1)
1650 if (copy_from_user(&fmt_desc
, argp
, sizeof(fmt_desc
)))
1652 return do_format(drive
, type
, &fmt_desc
);
1655 /* MSch: invalidate default_params */
1656 default_params
[drive
].blocks
= 0;
1657 set_capacity(floppy
->disk
, MAX_DISK_SIZE
* 2);
1660 /* invalidate the buffer track to force a reread */
1662 set_bit(drive
, &fake_change
);
1663 check_disk_change(bdev
);
1671 /* Initialize the 'unit' variable for drive 'drive' */
1673 static void __init
fd_probe( int drive
)
1678 if (!fd_test_drive_present( drive
))
1683 switch( UserSteprate
[drive
] ) {
1685 UD
.steprate
= FDCSTEP_2
;
1688 UD
.steprate
= FDCSTEP_3
;
1691 UD
.steprate
= FDCSTEP_6
;
1694 UD
.steprate
= FDCSTEP_12
;
1696 default: /* should be -1 for "not set by user" */
1697 if (ATARIHW_PRESENT( FDCSPEED
) || MACH_IS_MEDUSA
)
1698 UD
.steprate
= FDCSTEP_3
;
1700 UD
.steprate
= FDCSTEP_6
;
1703 MotorOn
= 1; /* from probe restore operation! */
1707 /* This function tests the physical presence of a floppy drive (not
1708 * whether a disk is inserted). This is done by issuing a restore
1709 * command, waiting max. 2 seconds (that should be enough to move the
1710 * head across the whole disk) and looking at the state of the "TR00"
1711 * signal. This should now be raised if there is a drive connected
1712 * (and there is no hardware failure :-) Otherwise, the drive is
1716 static int __init
fd_test_drive_present( int drive
)
1718 unsigned long timeout
;
1719 unsigned char status
;
1722 if (drive
>= (MACH_IS_FALCON
? 1 : 2)) return( 0 );
1723 fd_select_drive( drive
);
1725 /* disable interrupt temporarily */
1726 atari_turnoff_irq( IRQ_MFP_FDC
);
1727 FDC_WRITE (FDCREG_TRACK
, 0xff00);
1728 FDC_WRITE( FDCREG_CMD
, FDCCMD_RESTORE
| FDCCMDADD_H
| FDCSTEP_6
);
1730 timeout
= jiffies
+ 2*HZ
+HZ
/2;
1731 while (time_before(jiffies
, timeout
))
1732 if (!(st_mfp
.par_dt_reg
& 0x20))
1735 status
= FDC_READ( FDCREG_STATUS
);
1736 ok
= (status
& FDCSTAT_TR00
) != 0;
1738 /* force interrupt to abort restore operation (FDC would try
1739 * about 50 seconds!) */
1740 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1742 status
= FDC_READ( FDCREG_STATUS
);
1746 /* dummy seek command to make WP bit accessible */
1747 FDC_WRITE( FDCREG_DATA
, 0 );
1748 FDC_WRITE( FDCREG_CMD
, FDCCMD_SEEK
);
1749 while( st_mfp
.par_dt_reg
& 0x20 )
1751 status
= FDC_READ( FDCREG_STATUS
);
1754 atari_turnon_irq( IRQ_MFP_FDC
);
1759 /* Look how many and which kind of drives are connected. If there are
1760 * floppies, additionally start the disk-change and motor-off timers.
1763 static void __init
config_types( void )
1767 /* for probing drives, set the FDC speed to 8 MHz */
1768 if (ATARIHW_PRESENT(FDCSPEED
))
1769 dma_wd
.fdc_speed
= 0;
1771 printk(KERN_INFO
"Probing floppy drive(s):\n");
1772 for( drive
= 0; drive
< FD_MAX_UNITS
; drive
++ ) {
1775 printk(KERN_INFO
"fd%d\n", drive
);
1780 if (FDC_READ( FDCREG_STATUS
) & FDCSTAT_BUSY
) {
1781 /* If FDC is still busy from probing, give it another FORCI
1782 * command to abort the operation. If this isn't done, the FDC
1783 * will interrupt later and its IRQ line stays low, because
1784 * the status register isn't read. And this will block any
1785 * interrupts on this IRQ line :-(
1787 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1789 FDC_READ( FDCREG_STATUS
);
1794 start_motor_off_timer();
1795 if (cnt
== 1) fd_select_drive( 0 );
1796 start_check_change_timer();
1801 * floppy_open check for aliasing (/dev/fd0 can be the same as
1802 * /dev/PS0 etc), and disallows simultaneous access to the same
1803 * drive with different device numbers.
1806 static int floppy_open(struct block_device
*bdev
, fmode_t mode
)
1808 struct atari_floppy_struct
*p
= bdev
->bd_disk
->private_data
;
1809 int type
= MINOR(bdev
->bd_dev
) >> 2;
1811 DPRINT(("fd_open: type=%d\n",type
));
1812 if (p
->ref
&& p
->type
!= type
)
1815 if (p
->ref
== -1 || (p
->ref
&& mode
& FMODE_EXCL
))
1818 if (mode
& FMODE_EXCL
)
1825 if (mode
& FMODE_NDELAY
)
1828 if (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1829 check_disk_change(bdev
);
1830 if (mode
& FMODE_WRITE
) {
1844 static int floppy_release(struct gendisk
*disk
, fmode_t mode
)
1846 struct atari_floppy_struct
*p
= disk
->private_data
;
1849 else if (!p
->ref
--) {
1850 printk(KERN_ERR
"floppy_release with fd_ref == 0");
1856 static const struct block_device_operations floppy_fops
= {
1857 .owner
= THIS_MODULE
,
1858 .open
= floppy_open
,
1859 .release
= floppy_release
,
1860 .locked_ioctl
= fd_ioctl
,
1861 .media_changed
= check_floppy_change
,
1862 .revalidate_disk
= floppy_revalidate
,
1865 static struct kobject
*floppy_find(dev_t dev
, int *part
, void *data
)
1867 int drive
= *part
& 3;
1868 int type
= *part
>> 2;
1869 if (drive
>= FD_MAX_UNITS
|| type
> NUM_DISK_MINORS
)
1872 return get_disk(unit
[drive
].disk
);
1875 static int __init
atari_floppy_init (void)
1880 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
1883 if (register_blkdev(FLOPPY_MAJOR
,"fd"))
1886 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
1887 unit
[i
].disk
= alloc_disk(1);
1892 if (UseTrackbuffer
< 0)
1893 /* not set by user -> use default: for now, we turn
1894 track buffering off for all Medusas, though it
1895 could be used with ones that have a counter
1896 card. But the test is too hard :-( */
1897 UseTrackbuffer
= !MACH_IS_MEDUSA
;
1899 /* initialize variables */
1903 DMABuffer
= atari_stram_alloc(BUFFER_SIZE
+512, "ataflop");
1905 printk(KERN_ERR
"atari_floppy_init: cannot get dma buffer\n");
1908 TrackBuffer
= DMABuffer
+ 512;
1909 PhysDMABuffer
= virt_to_phys(DMABuffer
);
1910 PhysTrackBuffer
= virt_to_phys(TrackBuffer
);
1911 BufferDrive
= BufferSide
= BufferTrack
= -1;
1913 floppy_queue
= blk_init_queue(do_fd_request
, &ataflop_lock
);
1917 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
1920 unit
[i
].disk
->major
= FLOPPY_MAJOR
;
1921 unit
[i
].disk
->first_minor
= i
;
1922 sprintf(unit
[i
].disk
->disk_name
, "fd%d", i
);
1923 unit
[i
].disk
->fops
= &floppy_fops
;
1924 unit
[i
].disk
->private_data
= &unit
[i
];
1925 unit
[i
].disk
->queue
= floppy_queue
;
1926 set_capacity(unit
[i
].disk
, MAX_DISK_SIZE
* 2);
1927 add_disk(unit
[i
].disk
);
1930 blk_register_region(MKDEV(FLOPPY_MAJOR
, 0), 256, THIS_MODULE
,
1931 floppy_find
, NULL
, NULL
);
1933 printk(KERN_INFO
"Atari floppy driver: max. %cD, %strack buffering\n",
1934 DriveType
== 0 ? 'D' : DriveType
== 1 ? 'H' : 'E',
1935 UseTrackbuffer
? "" : "no ");
1941 put_disk(unit
[i
].disk
);
1943 blk_cleanup_queue(floppy_queue
);
1944 unregister_blkdev(FLOPPY_MAJOR
, "fd");
1949 static int __init
atari_floppy_setup(char *str
)
1951 int ints
[3 + FD_MAX_UNITS
];
1957 str
= get_options(str
, 3 + FD_MAX_UNITS
, ints
);
1960 printk(KERN_ERR
"ataflop_setup: no arguments!\n" );
1963 else if (ints
[0] > 2+FD_MAX_UNITS
) {
1964 printk(KERN_ERR
"ataflop_setup: too many arguments\n" );
1967 if (ints
[1] < 0 || ints
[1] > 2)
1968 printk(KERN_ERR
"ataflop_setup: bad drive type\n" );
1970 DriveType
= ints
[1];
1973 UseTrackbuffer
= (ints
[2] > 0);
1975 for( i
= 3; i
<= ints
[0] && i
-3 < FD_MAX_UNITS
; ++i
) {
1976 if (ints
[i
] != 2 && ints
[i
] != 3 && ints
[i
] != 6 && ints
[i
] != 12)
1977 printk(KERN_ERR
"ataflop_setup: bad steprate\n" );
1979 UserSteprate
[i
-3] = ints
[i
];
1984 __setup("floppy=", atari_floppy_setup
);
1987 static void __exit
atari_floppy_exit(void)
1990 blk_unregister_region(MKDEV(FLOPPY_MAJOR
, 0), 256);
1991 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
1992 del_gendisk(unit
[i
].disk
);
1993 put_disk(unit
[i
].disk
);
1995 unregister_blkdev(FLOPPY_MAJOR
, "fd");
1997 blk_cleanup_queue(floppy_queue
);
1998 del_timer_sync(&fd_timer
);
1999 atari_stram_free( DMABuffer
);
2002 module_init(atari_floppy_init
)
2003 module_exit(atari_floppy_exit
)
2005 MODULE_LICENSE("GPL");