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
);
1481 local_save_flags(flags
); /* The request function is called with ints
1482 local_irq_disable(); * disabled... so must save the IPL for later */
1484 local_irq_restore(flags
);
1485 atari_enable_irq( IRQ_MFP_FDC
);
1488 static int fd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1489 unsigned int cmd
, unsigned long param
)
1491 struct gendisk
*disk
= bdev
->bd_disk
;
1492 struct atari_floppy_struct
*floppy
= disk
->private_data
;
1493 int drive
= floppy
- unit
;
1494 int type
= floppy
->type
;
1495 struct atari_format_descr fmt_desc
;
1496 struct atari_disk_type
*dtp
;
1497 struct floppy_struct getprm
;
1499 struct floppy_struct setprm
;
1500 void __user
*argp
= (void __user
*)param
;
1505 if (--type
>= NUM_DISK_MINORS
)
1507 if (minor2disktype
[type
].drive_types
> DriveType
)
1509 type
= minor2disktype
[type
].index
;
1510 dtp
= &atari_disk_type
[type
];
1511 if (UD
.flags
& FTD_MSG
)
1512 printk (KERN_ERR
"floppy%d: found dtp %p name %s!\n",
1513 drive
, dtp
, dtp
->name
);
1521 memset((void *)&getprm
, 0, sizeof(getprm
));
1522 getprm
.size
= dtp
->blocks
;
1523 getprm
.sect
= dtp
->spt
;
1525 getprm
.track
= dtp
->blocks
/dtp
->spt
/2;
1526 getprm
.stretch
= dtp
->stretch
;
1527 if (copy_to_user(argp
, &getprm
, sizeof(getprm
)))
1535 * MSch 7/96: simple 'set geometry' case: just set the
1536 * 'default' device params (minor == 0).
1537 * Currently, the drive geometry is cleared after each
1538 * disk change and subsequent revalidate()! simple
1539 * implementation of FDDEFPRM: save geometry from a
1540 * FDDEFPRM call and restore it in floppy_revalidate() !
1543 /* get the parameters from user space */
1544 if (floppy
->ref
!= 1 && floppy
->ref
!= -1)
1546 if (copy_from_user(&setprm
, argp
, sizeof(setprm
)))
1549 * first of all: check for floppy change and revalidate,
1550 * or the next access will revalidate - and clear UDT :-(
1553 if (check_floppy_change(disk
))
1554 floppy_revalidate(disk
);
1556 if (UD
.flags
& FTD_MSG
)
1557 printk (KERN_INFO
"floppy%d: setting size %d spt %d str %d!\n",
1558 drive
, setprm
.size
, setprm
.sect
, setprm
.stretch
);
1560 /* what if type > 0 here? Overwrite specified entry ? */
1562 /* refuse to re-set a predefined type for now */
1568 * type == 0: first look for a matching entry in the type list,
1569 * and set the UD.disktype field to use the perdefined entry.
1570 * TODO: add user-defined format to head of autoprobe list ?
1571 * Useful to include the user-type for future autodetection!
1574 for (settype
= 0; settype
< NUM_DISK_MINORS
; settype
++) {
1576 if (minor2disktype
[settype
].drive_types
> DriveType
) {
1577 /* skip this one, invalid for drive ... */
1580 setidx
= minor2disktype
[settype
].index
;
1581 dtp
= &atari_disk_type
[setidx
];
1583 /* found matching entry ?? */
1584 if ( dtp
->blocks
== setprm
.size
1585 && dtp
->spt
== setprm
.sect
1586 && dtp
->stretch
== setprm
.stretch
) {
1587 if (UD
.flags
& FTD_MSG
)
1588 printk (KERN_INFO
"floppy%d: setting %s %p!\n",
1589 drive
, dtp
->name
, dtp
);
1591 set_capacity(floppy
->disk
, UDT
->blocks
);
1593 if (cmd
== FDDEFPRM
) {
1594 /* save settings as permanent default type */
1595 default_params
[drive
].name
= dtp
->name
;
1596 default_params
[drive
].spt
= dtp
->spt
;
1597 default_params
[drive
].blocks
= dtp
->blocks
;
1598 default_params
[drive
].fdc_speed
= dtp
->fdc_speed
;
1599 default_params
[drive
].stretch
= dtp
->stretch
;
1607 /* no matching disk type found above - setting user_params */
1609 if (cmd
== FDDEFPRM
) {
1610 /* set permanent type */
1611 dtp
= &default_params
[drive
];
1613 /* set user type (reset by disk change!) */
1614 dtp
= &user_params
[drive
];
1616 dtp
->name
= "user format";
1617 dtp
->blocks
= setprm
.size
;
1618 dtp
->spt
= setprm
.sect
;
1619 if (setprm
.sect
> 14)
1623 dtp
->stretch
= setprm
.stretch
;
1625 if (UD
.flags
& FTD_MSG
)
1626 printk (KERN_INFO
"floppy%d: blk %d spt %d str %d!\n",
1627 drive
, dtp
->blocks
, dtp
->spt
, dtp
->stretch
);
1630 if (setprm
.track
!= dtp
->blocks
/dtp
->spt
/2 ||
1637 set_capacity(floppy
->disk
, UDT
->blocks
);
1641 UD
.flags
|= FTD_MSG
;
1644 UD
.flags
&= ~FTD_MSG
;
1646 case FDSETEMSGTRESH
:
1651 if (floppy
->ref
!= 1 && floppy
->ref
!= -1)
1653 if (copy_from_user(&fmt_desc
, argp
, sizeof(fmt_desc
)))
1655 return do_format(drive
, type
, &fmt_desc
);
1658 /* MSch: invalidate default_params */
1659 default_params
[drive
].blocks
= 0;
1660 set_capacity(floppy
->disk
, MAX_DISK_SIZE
* 2);
1663 /* invalidate the buffer track to force a reread */
1665 set_bit(drive
, &fake_change
);
1666 check_disk_change(bdev
);
1674 /* Initialize the 'unit' variable for drive 'drive' */
1676 static void __init
fd_probe( int drive
)
1681 if (!fd_test_drive_present( drive
))
1686 switch( UserSteprate
[drive
] ) {
1688 UD
.steprate
= FDCSTEP_2
;
1691 UD
.steprate
= FDCSTEP_3
;
1694 UD
.steprate
= FDCSTEP_6
;
1697 UD
.steprate
= FDCSTEP_12
;
1699 default: /* should be -1 for "not set by user" */
1700 if (ATARIHW_PRESENT( FDCSPEED
) || MACH_IS_MEDUSA
)
1701 UD
.steprate
= FDCSTEP_3
;
1703 UD
.steprate
= FDCSTEP_6
;
1706 MotorOn
= 1; /* from probe restore operation! */
1710 /* This function tests the physical presence of a floppy drive (not
1711 * whether a disk is inserted). This is done by issuing a restore
1712 * command, waiting max. 2 seconds (that should be enough to move the
1713 * head across the whole disk) and looking at the state of the "TR00"
1714 * signal. This should now be raised if there is a drive connected
1715 * (and there is no hardware failure :-) Otherwise, the drive is
1719 static int __init
fd_test_drive_present( int drive
)
1721 unsigned long timeout
;
1722 unsigned char status
;
1725 if (drive
>= (MACH_IS_FALCON
? 1 : 2)) return( 0 );
1726 fd_select_drive( drive
);
1728 /* disable interrupt temporarily */
1729 atari_turnoff_irq( IRQ_MFP_FDC
);
1730 FDC_WRITE (FDCREG_TRACK
, 0xff00);
1731 FDC_WRITE( FDCREG_CMD
, FDCCMD_RESTORE
| FDCCMDADD_H
| FDCSTEP_6
);
1733 timeout
= jiffies
+ 2*HZ
+HZ
/2;
1734 while (time_before(jiffies
, timeout
))
1735 if (!(st_mfp
.par_dt_reg
& 0x20))
1738 status
= FDC_READ( FDCREG_STATUS
);
1739 ok
= (status
& FDCSTAT_TR00
) != 0;
1741 /* force interrupt to abort restore operation (FDC would try
1742 * about 50 seconds!) */
1743 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1745 status
= FDC_READ( FDCREG_STATUS
);
1749 /* dummy seek command to make WP bit accessible */
1750 FDC_WRITE( FDCREG_DATA
, 0 );
1751 FDC_WRITE( FDCREG_CMD
, FDCCMD_SEEK
);
1752 while( st_mfp
.par_dt_reg
& 0x20 )
1754 status
= FDC_READ( FDCREG_STATUS
);
1757 atari_turnon_irq( IRQ_MFP_FDC
);
1762 /* Look how many and which kind of drives are connected. If there are
1763 * floppies, additionally start the disk-change and motor-off timers.
1766 static void __init
config_types( void )
1770 /* for probing drives, set the FDC speed to 8 MHz */
1771 if (ATARIHW_PRESENT(FDCSPEED
))
1772 dma_wd
.fdc_speed
= 0;
1774 printk(KERN_INFO
"Probing floppy drive(s):\n");
1775 for( drive
= 0; drive
< FD_MAX_UNITS
; drive
++ ) {
1778 printk(KERN_INFO
"fd%d\n", drive
);
1783 if (FDC_READ( FDCREG_STATUS
) & FDCSTAT_BUSY
) {
1784 /* If FDC is still busy from probing, give it another FORCI
1785 * command to abort the operation. If this isn't done, the FDC
1786 * will interrupt later and its IRQ line stays low, because
1787 * the status register isn't read. And this will block any
1788 * interrupts on this IRQ line :-(
1790 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1792 FDC_READ( FDCREG_STATUS
);
1797 start_motor_off_timer();
1798 if (cnt
== 1) fd_select_drive( 0 );
1799 start_check_change_timer();
1804 * floppy_open check for aliasing (/dev/fd0 can be the same as
1805 * /dev/PS0 etc), and disallows simultaneous access to the same
1806 * drive with different device numbers.
1809 static int floppy_open(struct block_device
*bdev
, fmode_t mode
)
1811 struct atari_floppy_struct
*p
= bdev
->bd_disk
->private_data
;
1812 int type
= MINOR(bdev
->bd_dev
) >> 2;
1814 DPRINT(("fd_open: type=%d\n",type
));
1815 if (p
->ref
&& p
->type
!= type
)
1818 if (p
->ref
== -1 || (p
->ref
&& mode
& FMODE_EXCL
))
1821 if (mode
& FMODE_EXCL
)
1828 if (mode
& FMODE_NDELAY
)
1831 if (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1832 check_disk_change(bdev
);
1833 if (mode
& FMODE_WRITE
) {
1847 static int floppy_release(struct gendisk
*disk
, fmode_t mode
)
1849 struct atari_floppy_struct
*p
= disk
->private_data
;
1852 else if (!p
->ref
--) {
1853 printk(KERN_ERR
"floppy_release with fd_ref == 0");
1859 static const struct block_device_operations floppy_fops
= {
1860 .owner
= THIS_MODULE
,
1861 .open
= floppy_open
,
1862 .release
= floppy_release
,
1863 .locked_ioctl
= fd_ioctl
,
1864 .media_changed
= check_floppy_change
,
1865 .revalidate_disk
= floppy_revalidate
,
1868 static struct kobject
*floppy_find(dev_t dev
, int *part
, void *data
)
1870 int drive
= *part
& 3;
1871 int type
= *part
>> 2;
1872 if (drive
>= FD_MAX_UNITS
|| type
> NUM_DISK_MINORS
)
1875 return get_disk(unit
[drive
].disk
);
1878 static int __init
atari_floppy_init (void)
1883 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
1886 if (register_blkdev(FLOPPY_MAJOR
,"fd"))
1889 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
1890 unit
[i
].disk
= alloc_disk(1);
1895 if (UseTrackbuffer
< 0)
1896 /* not set by user -> use default: for now, we turn
1897 track buffering off for all Medusas, though it
1898 could be used with ones that have a counter
1899 card. But the test is too hard :-( */
1900 UseTrackbuffer
= !MACH_IS_MEDUSA
;
1902 /* initialize variables */
1906 DMABuffer
= atari_stram_alloc(BUFFER_SIZE
+512, "ataflop");
1908 printk(KERN_ERR
"atari_floppy_init: cannot get dma buffer\n");
1911 TrackBuffer
= DMABuffer
+ 512;
1912 PhysDMABuffer
= virt_to_phys(DMABuffer
);
1913 PhysTrackBuffer
= virt_to_phys(TrackBuffer
);
1914 BufferDrive
= BufferSide
= BufferTrack
= -1;
1916 floppy_queue
= blk_init_queue(do_fd_request
, &ataflop_lock
);
1920 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
1923 unit
[i
].disk
->major
= FLOPPY_MAJOR
;
1924 unit
[i
].disk
->first_minor
= i
;
1925 sprintf(unit
[i
].disk
->disk_name
, "fd%d", i
);
1926 unit
[i
].disk
->fops
= &floppy_fops
;
1927 unit
[i
].disk
->private_data
= &unit
[i
];
1928 unit
[i
].disk
->queue
= floppy_queue
;
1929 set_capacity(unit
[i
].disk
, MAX_DISK_SIZE
* 2);
1930 add_disk(unit
[i
].disk
);
1933 blk_register_region(MKDEV(FLOPPY_MAJOR
, 0), 256, THIS_MODULE
,
1934 floppy_find
, NULL
, NULL
);
1936 printk(KERN_INFO
"Atari floppy driver: max. %cD, %strack buffering\n",
1937 DriveType
== 0 ? 'D' : DriveType
== 1 ? 'H' : 'E',
1938 UseTrackbuffer
? "" : "no ");
1944 put_disk(unit
[i
].disk
);
1946 blk_cleanup_queue(floppy_queue
);
1947 unregister_blkdev(FLOPPY_MAJOR
, "fd");
1952 static int __init
atari_floppy_setup(char *str
)
1954 int ints
[3 + FD_MAX_UNITS
];
1960 str
= get_options(str
, 3 + FD_MAX_UNITS
, ints
);
1963 printk(KERN_ERR
"ataflop_setup: no arguments!\n" );
1966 else if (ints
[0] > 2+FD_MAX_UNITS
) {
1967 printk(KERN_ERR
"ataflop_setup: too many arguments\n" );
1970 if (ints
[1] < 0 || ints
[1] > 2)
1971 printk(KERN_ERR
"ataflop_setup: bad drive type\n" );
1973 DriveType
= ints
[1];
1976 UseTrackbuffer
= (ints
[2] > 0);
1978 for( i
= 3; i
<= ints
[0] && i
-3 < FD_MAX_UNITS
; ++i
) {
1979 if (ints
[i
] != 2 && ints
[i
] != 3 && ints
[i
] != 6 && ints
[i
] != 12)
1980 printk(KERN_ERR
"ataflop_setup: bad steprate\n" );
1982 UserSteprate
[i
-3] = ints
[i
];
1987 __setup("floppy=", atari_floppy_setup
);
1990 static void __exit
atari_floppy_exit(void)
1993 blk_unregister_region(MKDEV(FLOPPY_MAJOR
, 0), 256);
1994 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
1995 del_gendisk(unit
[i
].disk
);
1996 put_disk(unit
[i
].disk
);
1998 unregister_blkdev(FLOPPY_MAJOR
, "fd");
2000 blk_cleanup_queue(floppy_queue
);
2001 del_timer_sync(&fd_timer
);
2002 atari_stram_free( DMABuffer
);
2005 module_init(atari_floppy_init
)
2006 module_exit(atari_floppy_exit
)
2008 MODULE_LICENSE("GPL");