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/blk-mq.h>
70 #include <linux/mutex.h>
71 #include <linux/completion.h>
72 #include <linux/wait.h>
74 #include <asm/atariints.h>
75 #include <asm/atari_stdma.h>
76 #include <asm/atari_stram.h>
78 #define FD_MAX_UNITS 2
82 static DEFINE_MUTEX(ataflop_mutex
);
83 static struct request
*fd_request
;
91 #define FDCSELREG_STP (0x80) /* command/status register */
92 #define FDCSELREG_TRA (0x82) /* track register */
93 #define FDCSELREG_SEC (0x84) /* sector register */
94 #define FDCSELREG_DTA (0x86) /* data register */
96 /* register names for FDC_READ/WRITE macros */
99 #define FDCREG_STATUS 0
100 #define FDCREG_TRACK 2
101 #define FDCREG_SECTOR 4
102 #define FDCREG_DATA 6
104 /* command opcodes */
106 #define FDCCMD_RESTORE (0x00) /* - */
107 #define FDCCMD_SEEK (0x10) /* | */
108 #define FDCCMD_STEP (0x20) /* | TYP 1 Commands */
109 #define FDCCMD_STIN (0x40) /* | */
110 #define FDCCMD_STOT (0x60) /* - */
111 #define FDCCMD_RDSEC (0x80) /* - TYP 2 Commands */
112 #define FDCCMD_WRSEC (0xa0) /* - " */
113 #define FDCCMD_RDADR (0xc0) /* - */
114 #define FDCCMD_RDTRA (0xe0) /* | TYP 3 Commands */
115 #define FDCCMD_WRTRA (0xf0) /* - */
116 #define FDCCMD_FORCI (0xd0) /* - TYP 4 Command */
118 /* command modifier bits */
120 #define FDCCMDADD_SR6 (0x00) /* step rate settings */
121 #define FDCCMDADD_SR12 (0x01)
122 #define FDCCMDADD_SR2 (0x02)
123 #define FDCCMDADD_SR3 (0x03)
124 #define FDCCMDADD_V (0x04) /* verify */
125 #define FDCCMDADD_H (0x08) /* wait for spin-up */
126 #define FDCCMDADD_U (0x10) /* update track register */
127 #define FDCCMDADD_M (0x10) /* multiple sector access */
128 #define FDCCMDADD_E (0x04) /* head settling flag */
129 #define FDCCMDADD_P (0x02) /* precompensation off */
130 #define FDCCMDADD_A0 (0x01) /* DAM flag */
132 /* status register bits */
134 #define FDCSTAT_MOTORON (0x80) /* motor on */
135 #define FDCSTAT_WPROT (0x40) /* write protected (FDCCMD_WR*) */
136 #define FDCSTAT_SPINUP (0x20) /* motor speed stable (Type I) */
137 #define FDCSTAT_DELDAM (0x20) /* sector has deleted DAM (Type II+III) */
138 #define FDCSTAT_RECNF (0x10) /* record not found */
139 #define FDCSTAT_CRC (0x08) /* CRC error */
140 #define FDCSTAT_TR00 (0x04) /* Track 00 flag (Type I) */
141 #define FDCSTAT_LOST (0x04) /* Lost Data (Type II+III) */
142 #define FDCSTAT_IDX (0x02) /* Index status (Type I) */
143 #define FDCSTAT_DRQ (0x02) /* DRQ status (Type II+III) */
144 #define FDCSTAT_BUSY (0x01) /* FDC is busy */
147 /* PSG Port A Bit Nr 0 .. Side Sel .. 0 -> Side 1 1 -> Side 2 */
148 #define DSKSIDE (0x01)
150 #define DSKDRVNONE (0x06)
151 #define DSKDRV0 (0x02)
152 #define DSKDRV1 (0x04)
155 #define FDCSTEP_6 0x00
156 #define FDCSTEP_12 0x01
157 #define FDCSTEP_2 0x02
158 #define FDCSTEP_3 0x03
160 struct atari_format_descr
{
161 int track
; /* to be formatted */
162 int head
; /* "" "" */
163 int sect_offset
; /* offset of first sector */
166 /* Disk types: DD, HD, ED */
167 static struct atari_disk_type
{
169 unsigned spt
; /* sectors per track */
170 unsigned blocks
; /* total number of blocks */
171 unsigned fdc_speed
; /* fdc_speed setting */
172 unsigned stretch
; /* track doubling ? */
173 } atari_disk_type
[] = {
174 { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
175 { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
176 { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
177 { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
178 /* formats above are probed for type DD */
179 #define MAX_TYPE_DD 3
180 { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
181 { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
182 { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
183 /* formats above are probed for types DD and HD */
184 #define MAX_TYPE_HD 6
185 { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
186 { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
187 /* formats above are probed for types DD, HD and ED */
188 #define MAX_TYPE_ED 8
189 /* types below are never autoprobed */
190 { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
191 { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
192 { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
193 { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
194 { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
195 { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
196 { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
197 { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
198 { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
199 { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
200 { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
201 { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
202 { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
203 { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
204 { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
205 { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
206 { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
207 { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
208 { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
211 static int StartDiskType
[] = {
221 static int DriveType
= TYPE_HD
;
223 static DEFINE_SPINLOCK(ataflop_lock
);
225 /* Array for translating minors into disk formats */
228 unsigned drive_types
;
229 } minor2disktype
[] = {
230 { 0, TYPE_DD
}, /* 1: d360 */
231 { 4, TYPE_HD
}, /* 2: h1200 */
232 { 1, TYPE_DD
}, /* 3: D360 */
233 { 2, TYPE_DD
}, /* 4: D720 */
234 { 1, TYPE_DD
}, /* 5: h360 = D360 */
235 { 2, TYPE_DD
}, /* 6: h720 = D720 */
236 { 5, TYPE_HD
}, /* 7: H1440 */
237 { 7, TYPE_ED
}, /* 8: E2880 */
238 /* some PC formats :-) */
239 { 8, TYPE_ED
}, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
240 { 5, TYPE_HD
}, /* 10: h1440 = H1440 */
241 { 9, TYPE_HD
}, /* 11: H1680 */
242 { 10, TYPE_DD
}, /* 12: h410 */
243 { 3, TYPE_DD
}, /* 13: H820 <- == D820, 82x10 */
244 { 11, TYPE_HD
}, /* 14: h1476 */
245 { 12, TYPE_HD
}, /* 15: H1722 */
246 { 13, TYPE_DD
}, /* 16: h420 */
247 { 14, TYPE_DD
}, /* 17: H830 */
248 { 15, TYPE_HD
}, /* 18: h1494 */
249 { 16, TYPE_HD
}, /* 19: H1743 */
250 { 17, TYPE_DD
}, /* 20: h880 */
251 { 18, TYPE_DD
}, /* 21: D1040 */
252 { 19, TYPE_DD
}, /* 22: D1120 */
253 { 20, TYPE_HD
}, /* 23: h1600 */
254 { 21, TYPE_HD
}, /* 24: H1760 */
255 { 22, TYPE_HD
}, /* 25: H1920 */
256 { 23, TYPE_ED
}, /* 26: E3200 */
257 { 24, TYPE_ED
}, /* 27: E3520 */
258 { 25, TYPE_ED
}, /* 28: E3840 */
259 { 26, TYPE_HD
}, /* 29: H1840 */
260 { 27, TYPE_DD
}, /* 30: D800 */
261 { 6, TYPE_HD
}, /* 31: H1640 <- was H1600 == h1600 for PC */
264 #define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
267 * Maximum disk size (in kilobytes). This default is used whenever the
268 * current disk size is unknown.
270 #define MAX_DISK_SIZE 3280
273 * MSch: User-provided type information. 'drive' points to
274 * the respective entry of this array. Set by FDSETPRM ioctls.
276 static struct atari_disk_type user_params
[FD_MAX_UNITS
];
279 * User-provided permanent type information. 'drive' points to
280 * the respective entry of this array. Set by FDDEFPRM ioctls,
281 * restored upon disk change by floppy_revalidate() if valid (as seen by
282 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
284 static struct atari_disk_type default_params
[FD_MAX_UNITS
];
286 /* current info on each unit */
287 static struct atari_floppy_struct
{
288 int connected
; /* !=0 : drive is connected */
289 int autoprobe
; /* !=0 : do autoprobe */
291 struct atari_disk_type
*disktype
; /* current type of disk */
293 int track
; /* current head position or -1 if
295 unsigned int steprate
; /* steprate setting */
296 unsigned int wpstat
; /* current state of WP signal (for
297 disk change detection) */
298 int flags
; /* flags */
299 struct gendisk
*disk
;
302 struct blk_mq_tag_set tag_set
;
303 } unit
[FD_MAX_UNITS
];
305 #define UD unit[drive]
306 #define UDT unit[drive].disktype
307 #define SUD unit[SelectedDrive]
308 #define SUDT unit[SelectedDrive].disktype
311 #define FDC_READ(reg) ({ \
312 /* unsigned long __flags; */ \
313 unsigned short __val; \
314 /* local_irq_save(__flags); */ \
315 dma_wd.dma_mode_status = 0x80 | (reg); \
317 __val = dma_wd.fdc_acces_seccount; \
319 /* local_irq_restore(__flags); */ \
323 #define FDC_WRITE(reg,val) \
325 /* unsigned long __flags; */ \
326 /* local_irq_save(__flags); */ \
327 dma_wd.dma_mode_status = 0x80 | (reg); \
329 dma_wd.fdc_acces_seccount = (val); \
331 /* local_irq_restore(__flags); */ \
335 /* Buffering variables:
336 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
337 * operations. Second, a track buffer is used to cache a whole track
338 * of the disk to save read operations. These are two separate buffers
339 * because that allows write operations without clearing the track buffer.
342 static int MaxSectors
[] = {
345 static int BufferSize
[] = {
346 15*512, 30*512, 60*512
349 #define BUFFER_SIZE (BufferSize[DriveType])
351 unsigned char *DMABuffer
; /* buffer for writes */
352 static unsigned long PhysDMABuffer
; /* physical address */
354 static int UseTrackbuffer
= -1; /* Do track buffering? */
355 module_param(UseTrackbuffer
, int, 0);
357 unsigned char *TrackBuffer
; /* buffer for reads */
358 static unsigned long PhysTrackBuffer
; /* physical address */
359 static int BufferDrive
, BufferSide
, BufferTrack
;
360 static int read_track
; /* non-zero if we are reading whole tracks */
362 #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
363 #define IS_BUFFERED(drive,side,track) \
364 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
367 * These are global variables, as that's the easiest way to give
368 * information to interrupts. They are the data used for the current
371 static int SelectedDrive
= 0;
372 static int ReqCmd
, ReqBlock
;
373 static int ReqSide
, ReqTrack
, ReqSector
, ReqCnt
;
374 static int HeadSettleFlag
= 0;
375 static unsigned char *ReqData
, *ReqBuffer
;
376 static int MotorOn
= 0, MotorOffTrys
;
377 static int IsFormatting
= 0, FormatError
;
379 static int UserSteprate
[FD_MAX_UNITS
] = { -1, -1 };
380 module_param_array(UserSteprate
, int, NULL
, 0);
382 static DECLARE_COMPLETION(format_wait
);
384 static unsigned long changed_floppies
= 0xff, fake_change
= 0;
385 #define CHECK_CHANGE_DELAY HZ/2
387 #define FD_MOTOR_OFF_DELAY (3*HZ)
388 #define FD_MOTOR_OFF_MAXTRY (10*20)
390 #define FLOPPY_TIMEOUT (6*HZ)
391 #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
392 * will be recalibrated. */
393 #define MAX_ERRORS 8 /* After this many errors the driver
398 * The driver is trying to determine the correct media format
399 * while Probing is set. fd_rwsec_done() clears it after a
402 static int Probing
= 0;
404 /* This flag is set when a dummy seek is necessary to make the WP
405 * status bit accessible.
407 static int NeedSeek
= 0;
411 #define DPRINT(a) printk a
416 /***************************** Prototypes *****************************/
418 static void fd_select_side( int side
);
419 static void fd_select_drive( int drive
);
420 static void fd_deselect( void );
421 static void fd_motor_off_timer(struct timer_list
*unused
);
422 static void check_change(struct timer_list
*unused
);
423 static irqreturn_t
floppy_irq (int irq
, void *dummy
);
424 static void fd_error( void );
425 static int do_format(int drive
, int type
, struct atari_format_descr
*desc
);
426 static void do_fd_action( int drive
);
427 static void fd_calibrate( void );
428 static void fd_calibrate_done( int status
);
429 static void fd_seek( void );
430 static void fd_seek_done( int status
);
431 static void fd_rwsec( void );
432 static void fd_readtrack_check(struct timer_list
*unused
);
433 static void fd_rwsec_done( int status
);
434 static void fd_rwsec_done1(int status
);
435 static void fd_writetrack( void );
436 static void fd_writetrack_done( int status
);
437 static void fd_times_out(struct timer_list
*unused
);
438 static void finish_fdc( void );
439 static void finish_fdc_done( int dummy
);
440 static void setup_req_params( int drive
);
441 static int fd_locked_ioctl(struct block_device
*bdev
, fmode_t mode
, unsigned int
442 cmd
, unsigned long param
);
443 static void fd_probe( int drive
);
444 static int fd_test_drive_present( int drive
);
445 static void config_types( void );
446 static int floppy_open(struct block_device
*bdev
, fmode_t mode
);
447 static void floppy_release(struct gendisk
*disk
, fmode_t mode
);
449 /************************* End of Prototypes **************************/
451 static DEFINE_TIMER(motor_off_timer
, fd_motor_off_timer
);
452 static DEFINE_TIMER(readtrack_timer
, fd_readtrack_check
);
453 static DEFINE_TIMER(timeout_timer
, fd_times_out
);
454 static DEFINE_TIMER(fd_timer
, check_change
);
456 static void fd_end_request_cur(blk_status_t err
)
458 if (!blk_update_request(fd_request
, err
,
459 blk_rq_cur_bytes(fd_request
))) {
460 __blk_mq_end_request(fd_request
, err
);
465 static inline void start_motor_off_timer(void)
467 mod_timer(&motor_off_timer
, jiffies
+ FD_MOTOR_OFF_DELAY
);
471 static inline void start_check_change_timer( void )
473 mod_timer(&fd_timer
, jiffies
+ CHECK_CHANGE_DELAY
);
476 static inline void start_timeout(void)
478 mod_timer(&timeout_timer
, jiffies
+ FLOPPY_TIMEOUT
);
481 static inline void stop_timeout(void)
483 del_timer(&timeout_timer
);
486 /* Select the side to use. */
488 static void fd_select_side( int side
)
492 /* protect against various other ints mucking around with the PSG */
493 local_irq_save(flags
);
495 sound_ym
.rd_data_reg_sel
= 14; /* Select PSG Port A */
496 sound_ym
.wd_data
= (side
== 0) ? sound_ym
.rd_data_reg_sel
| 0x01 :
497 sound_ym
.rd_data_reg_sel
& 0xfe;
499 local_irq_restore(flags
);
503 /* Select a drive, update the FDC's track register and set the correct
504 * clock speed for this disk's type.
507 static void fd_select_drive( int drive
)
512 if (drive
== SelectedDrive
)
515 /* protect against various other ints mucking around with the PSG */
516 local_irq_save(flags
);
517 sound_ym
.rd_data_reg_sel
= 14; /* Select PSG Port A */
518 tmp
= sound_ym
.rd_data_reg_sel
;
519 sound_ym
.wd_data
= (tmp
| DSKDRVNONE
) & ~(drive
== 0 ? DSKDRV0
: DSKDRV1
);
520 atari_dont_touch_floppy_select
= 1;
521 local_irq_restore(flags
);
523 /* restore track register to saved value */
524 FDC_WRITE( FDCREG_TRACK
, UD
.track
);
527 /* select 8/16 MHz */
529 if (ATARIHW_PRESENT(FDCSPEED
))
530 dma_wd
.fdc_speed
= UDT
->fdc_speed
;
532 SelectedDrive
= drive
;
536 /* Deselect both drives. */
538 static void fd_deselect( void )
542 /* protect against various other ints mucking around with the PSG */
543 local_irq_save(flags
);
544 atari_dont_touch_floppy_select
= 0;
545 sound_ym
.rd_data_reg_sel
=14; /* Select PSG Port A */
546 sound_ym
.wd_data
= (sound_ym
.rd_data_reg_sel
|
547 (MACH_IS_FALCON
? 3 : 7)); /* no drives selected */
548 /* On Falcon, the drive B select line is used on the printer port, so
549 * leave it alone... */
551 local_irq_restore(flags
);
555 /* This timer function deselects the drives when the FDC switched the
556 * motor off. The deselection cannot happen earlier because the FDC
557 * counts the index signals, which arrive only if one drive is selected.
560 static void fd_motor_off_timer(struct timer_list
*unused
)
562 unsigned char status
;
564 if (SelectedDrive
< 0)
565 /* no drive selected, needn't deselect anyone */
568 if (stdma_islocked())
571 status
= FDC_READ( FDCREG_STATUS
);
573 if (!(status
& 0x80)) {
574 /* motor already turned off by FDC -> deselect drives */
579 /* not yet off, try again */
582 /* Test again later; if tested too often, it seems there is no disk
583 * in the drive and the FDC will leave the motor on forever (or,
584 * at least until a disk is inserted). So we'll test only twice
585 * per second from then on...
587 mod_timer(&motor_off_timer
,
588 jiffies
+ (MotorOffTrys
++ < FD_MOTOR_OFF_MAXTRY
? HZ
/20 : HZ
/2));
592 /* This function is repeatedly called to detect disk changes (as good
593 * as possible) and keep track of the current state of the write protection.
596 static void check_change(struct timer_list
*unused
)
598 static int drive
= 0;
601 unsigned char old_porta
;
604 if (++drive
> 1 || !UD
.connected
)
607 /* protect against various other ints mucking around with the PSG */
608 local_irq_save(flags
);
610 if (!stdma_islocked()) {
611 sound_ym
.rd_data_reg_sel
= 14;
612 old_porta
= sound_ym
.rd_data_reg_sel
;
613 sound_ym
.wd_data
= (old_porta
| DSKDRVNONE
) &
614 ~(drive
== 0 ? DSKDRV0
: DSKDRV1
);
615 stat
= !!(FDC_READ( FDCREG_STATUS
) & FDCSTAT_WPROT
);
616 sound_ym
.wd_data
= old_porta
;
618 if (stat
!= UD
.wpstat
) {
619 DPRINT(( "wpstat[%d] = %d\n", drive
, stat
));
621 set_bit (drive
, &changed_floppies
);
624 local_irq_restore(flags
);
626 start_check_change_timer();
630 /* Handling of the Head Settling Flag: This flag should be set after each
631 * seek operation, because we don't use seeks with verify.
634 static inline void set_head_settle_flag(void)
636 HeadSettleFlag
= FDCCMDADD_E
;
639 static inline int get_head_settle_flag(void)
641 int tmp
= HeadSettleFlag
;
646 static inline void copy_buffer(void *from
, void *to
)
648 ulong
*p1
= (ulong
*)from
, *p2
= (ulong
*)to
;
651 for (cnt
= 512/4; cnt
; cnt
--)
658 /* General Interrupt Handling */
660 static void (*FloppyIRQHandler
)( int status
) = NULL
;
662 static irqreturn_t
floppy_irq (int irq
, void *dummy
)
664 unsigned char status
;
665 void (*handler
)( int );
667 handler
= xchg(&FloppyIRQHandler
, NULL
);
671 status
= FDC_READ( FDCREG_STATUS
);
672 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status
,(unsigned long)handler
));
676 DPRINT(("FDC irq, no handler\n"));
682 /* Error handling: If some error happened, retry some times, then
683 * recalibrate, then try again, and fail after MAX_ERRORS.
686 static void fd_error( void )
691 complete(&format_wait
);
698 fd_request
->error_count
++;
699 if (fd_request
->error_count
>= MAX_ERRORS
) {
700 printk(KERN_ERR
"fd%d: too many errors.\n", SelectedDrive
);
701 fd_end_request_cur(BLK_STS_IOERR
);
703 else if (fd_request
->error_count
== RECALIBRATE_ERRORS
) {
704 printk(KERN_WARNING
"fd%d: recalibrating\n", SelectedDrive
);
705 if (SelectedDrive
!= -1)
712 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
715 /* ---------- Formatting ---------- */
717 #define FILL(n,val) \
719 memset( p, val, n ); \
723 static int do_format(int drive
, int type
, struct atari_format_descr
*desc
)
725 struct request_queue
*q
= unit
[drive
].disk
->queue
;
731 blk_mq_freeze_queue(q
);
732 blk_mq_quiesce_queue(q
);
734 local_irq_save(flags
);
735 stdma_lock(floppy_irq
, NULL
);
736 atari_turnon_irq( IRQ_MFP_FDC
); /* should be already, just to be sure */
737 local_irq_restore(flags
);
740 if (--type
>= NUM_DISK_MINORS
||
741 minor2disktype
[type
].drive_types
> DriveType
) {
745 type
= minor2disktype
[type
].index
;
746 UDT
= &atari_disk_type
[type
];
749 if (!UDT
|| desc
->track
>= UDT
->blocks
/UDT
->spt
/2 || desc
->head
>= 2) {
756 /* The track buffer is used for the raw track data, so its
757 contents become invalid! */
759 /* stop deselect timer */
760 del_timer( &motor_off_timer
);
762 FILL( 60 * (nsect
/ 9), 0x4e );
763 for( sect
= 0; sect
< nsect
; ++sect
) {
769 *p
++ = (nsect
+ sect
- desc
->sect_offset
) % nsect
+ 1;
780 FILL( TrackBuffer
+BUFFER_SIZE
-p
, 0x4e );
784 ReqTrack
= desc
->track
;
785 ReqSide
= desc
->head
;
786 do_fd_action( drive
);
788 wait_for_completion(&format_wait
);
790 ret
= FormatError
? -EIO
: 0;
792 blk_mq_unquiesce_queue(q
);
793 blk_mq_unfreeze_queue(q
);
798 /* do_fd_action() is the general procedure for a fd request: All
799 * required parameter settings (drive select, side select, track
800 * position) are checked and set if needed. For each of these
801 * parameters and the actual reading or writing exist two functions:
802 * one that starts the setting (or skips it if possible) and one
803 * callback for the "done" interrupt. Each done func calls the next
804 * set function to propagate the request down to fd_rwsec_done().
807 static void do_fd_action( int drive
)
809 DPRINT(("do_fd_action\n"));
811 if (UseTrackbuffer
&& !IsFormatting
) {
813 if (IS_BUFFERED( drive
, ReqSide
, ReqTrack
)) {
814 if (ReqCmd
== READ
) {
815 copy_buffer( SECTOR_BUFFER(ReqSector
), ReqData
);
816 if (++ReqCnt
< blk_rq_cur_sectors(fd_request
)) {
817 /* read next sector */
818 setup_req_params( drive
);
822 /* all sectors finished */
823 fd_end_request_cur(BLK_STS_OK
);
828 /* cmd == WRITE, pay attention to track buffer
830 copy_buffer( ReqData
, SECTOR_BUFFER(ReqSector
) );
835 if (SelectedDrive
!= drive
)
836 fd_select_drive( drive
);
840 else if (UD
.track
!= ReqTrack
<< UDT
->stretch
)
842 else if (IsFormatting
)
849 /* Seek to track 0 if the current track is unknown */
851 static void fd_calibrate( void )
853 if (SUD
.track
>= 0) {
854 fd_calibrate_done( 0 );
858 if (ATARIHW_PRESENT(FDCSPEED
))
859 dma_wd
.fdc_speed
= 0; /* always seek with 8 Mhz */;
860 DPRINT(("fd_calibrate\n"));
861 SET_IRQ_HANDLER( fd_calibrate_done
);
862 /* we can't verify, since the speed may be incorrect */
863 FDC_WRITE( FDCREG_CMD
, FDCCMD_RESTORE
| SUD
.steprate
);
872 static void fd_calibrate_done( int status
)
874 DPRINT(("fd_calibrate_done()\n"));
877 /* set the correct speed now */
878 if (ATARIHW_PRESENT(FDCSPEED
))
879 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
880 if (status
& FDCSTAT_RECNF
) {
881 printk(KERN_ERR
"fd%d: restore failed\n", SelectedDrive
);
891 /* Seek the drive to the requested track. The drive must have been
892 * calibrated at some point before this.
895 static void fd_seek( void )
897 if (SUD
.track
== ReqTrack
<< SUDT
->stretch
) {
902 if (ATARIHW_PRESENT(FDCSPEED
)) {
903 dma_wd
.fdc_speed
= 0; /* always seek witch 8 Mhz */
907 DPRINT(("fd_seek() to track %d\n",ReqTrack
));
908 FDC_WRITE( FDCREG_DATA
, ReqTrack
<< SUDT
->stretch
);
910 SET_IRQ_HANDLER( fd_seek_done
);
911 FDC_WRITE( FDCREG_CMD
, FDCCMD_SEEK
| SUD
.steprate
);
914 set_head_settle_flag();
920 static void fd_seek_done( int status
)
922 DPRINT(("fd_seek_done()\n"));
925 /* set the correct speed */
926 if (ATARIHW_PRESENT(FDCSPEED
))
927 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
928 if (status
& FDCSTAT_RECNF
) {
929 printk(KERN_ERR
"fd%d: seek error (to track %d)\n",
930 SelectedDrive
, ReqTrack
);
931 /* we don't know exactly which track we are on now! */
936 SUD
.track
= ReqTrack
<< SUDT
->stretch
;
946 /* This does the actual reading/writing after positioning the head
947 * over the correct track.
950 static int MultReadInProgress
= 0;
953 static void fd_rwsec( void )
955 unsigned long paddr
, flags
;
956 unsigned int rwflag
, old_motoron
;
959 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector
, ReqCmd
== WRITE
? 'w' : 'r' ));
960 if (ReqCmd
== WRITE
) {
961 if (ATARIHW_PRESENT(EXTD_DMA
)) {
962 paddr
= virt_to_phys(ReqData
);
965 copy_buffer( ReqData
, DMABuffer
);
966 paddr
= PhysDMABuffer
;
968 dma_cache_maintenance( paddr
, 512, 1 );
973 paddr
= PhysTrackBuffer
;
975 paddr
= ATARIHW_PRESENT(EXTD_DMA
) ?
976 virt_to_phys(ReqData
) : PhysDMABuffer
;
980 fd_select_side( ReqSide
);
982 /* Start sector of this operation */
983 FDC_WRITE( FDCREG_SECTOR
, read_track
? 1 : ReqSector
);
985 /* Cheat for track if stretch != 0 */
987 track
= FDC_READ( FDCREG_TRACK
);
989 FDC_WRITE( FDCREG_TRACK
, track
>> SUDT
->stretch
);
994 local_irq_save(flags
);
995 dma_wd
.dma_lo
= (unsigned char)paddr
;
998 dma_wd
.dma_md
= (unsigned char)paddr
;
1001 if (ATARIHW_PRESENT(EXTD_DMA
))
1002 st_dma_ext_dmahi
= (unsigned short)paddr
;
1004 dma_wd
.dma_hi
= (unsigned char)paddr
;
1006 local_irq_restore(flags
);
1008 /* Clear FIFO and switch DMA to correct mode */
1009 dma_wd
.dma_mode_status
= 0x90 | rwflag
;
1011 dma_wd
.dma_mode_status
= 0x90 | (rwflag
^ 0x100);
1013 dma_wd
.dma_mode_status
= 0x90 | rwflag
;
1016 /* How many sectors for DMA */
1017 dma_wd
.fdc_acces_seccount
= read_track
? SUDT
->spt
: 1;
1021 /* Start operation */
1022 dma_wd
.dma_mode_status
= FDCSELREG_STP
| rwflag
;
1024 SET_IRQ_HANDLER( fd_rwsec_done
);
1025 dma_wd
.fdc_acces_seccount
=
1026 (get_head_settle_flag() |
1027 (rwflag
? FDCCMD_WRSEC
: (FDCCMD_RDSEC
| (read_track
? FDCCMDADD_M
: 0))));
1029 old_motoron
= MotorOn
;
1032 /* wait for interrupt */
1035 /* If reading a whole track, wait about one disk rotation and
1036 * then check if all sectors are read. The FDC will even
1037 * search for the first non-existent sector and need 1 sec to
1038 * recognise that it isn't present :-(
1040 MultReadInProgress
= 1;
1041 mod_timer(&readtrack_timer
,
1042 /* 1 rot. + 5 rot.s if motor was off */
1043 jiffies
+ HZ
/5 + (old_motoron
? 0 : HZ
));
1049 static void fd_readtrack_check(struct timer_list
*unused
)
1051 unsigned long flags
, addr
, addr2
;
1053 local_irq_save(flags
);
1055 if (!MultReadInProgress
) {
1056 /* This prevents a race condition that could arise if the
1057 * interrupt is triggered while the calling of this timer
1058 * callback function takes place. The IRQ function then has
1059 * already cleared 'MultReadInProgress' when flow of control
1062 local_irq_restore(flags
);
1066 /* get the current DMA address */
1067 /* ++ f.a. read twice to avoid being fooled by switcher */
1071 addr
= dma_wd
.dma_lo
& 0xff;
1073 addr
|= (dma_wd
.dma_md
& 0xff) << 8;
1075 if (ATARIHW_PRESENT( EXTD_DMA
))
1076 addr
|= (st_dma_ext_dmahi
& 0xffff) << 16;
1078 addr
|= (dma_wd
.dma_hi
& 0xff) << 16;
1080 } while(addr
!= addr2
);
1082 if (addr
>= PhysTrackBuffer
+ SUDT
->spt
*512) {
1083 /* already read enough data, force an FDC interrupt to stop
1084 * the read operation
1086 SET_IRQ_HANDLER( NULL
);
1087 MultReadInProgress
= 0;
1088 local_irq_restore(flags
);
1089 DPRINT(("fd_readtrack_check(): done\n"));
1090 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1093 /* No error until now -- the FDC would have interrupted
1099 /* not yet finished, wait another tenth rotation */
1100 local_irq_restore(flags
);
1101 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1102 mod_timer(&readtrack_timer
, jiffies
+ HZ
/5/10);
1107 static void fd_rwsec_done( int status
)
1109 DPRINT(("fd_rwsec_done()\n"));
1112 del_timer(&readtrack_timer
);
1113 if (!MultReadInProgress
)
1115 MultReadInProgress
= 0;
1117 fd_rwsec_done1(status
);
1120 static void fd_rwsec_done1(int status
)
1126 /* Correct the track if stretch != 0 */
1127 if (SUDT
->stretch
) {
1128 track
= FDC_READ( FDCREG_TRACK
);
1130 FDC_WRITE( FDCREG_TRACK
, track
<< SUDT
->stretch
);
1133 if (!UseTrackbuffer
) {
1134 dma_wd
.dma_mode_status
= 0x90;
1136 if (!(dma_wd
.dma_mode_status
& 0x01)) {
1137 printk(KERN_ERR
"fd%d: DMA error\n", SelectedDrive
);
1143 if (ReqCmd
== WRITE
&& (status
& FDCSTAT_WPROT
)) {
1144 printk(KERN_NOTICE
"fd%d: is write protected\n", SelectedDrive
);
1147 if ((status
& FDCSTAT_RECNF
) &&
1148 /* RECNF is no error after a multiple read when the FDC
1149 searched for a non-existent sector! */
1150 !(read_track
&& FDC_READ(FDCREG_SECTOR
) > SUDT
->spt
)) {
1152 if (SUDT
> atari_disk_type
) {
1153 if (SUDT
[-1].blocks
> ReqBlock
) {
1154 /* try another disk type */
1156 set_capacity(unit
[SelectedDrive
].disk
,
1162 if (SUD
.flags
& FTD_MSG
)
1163 printk(KERN_INFO
"fd%d: Auto-detected floppy type %s\n",
1164 SelectedDrive
, SUDT
->name
);
1168 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1169 if (SUD
.autoprobe
) {
1170 SUDT
= atari_disk_type
+ StartDiskType
[DriveType
];
1171 set_capacity(unit
[SelectedDrive
].disk
,
1177 if (ATARIHW_PRESENT(FDCSPEED
)) {
1178 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
1181 setup_req_params( SelectedDrive
);
1183 do_fd_action( SelectedDrive
);
1187 printk(KERN_ERR
"fd%d: sector %d not found (side %d, track %d)\n",
1188 SelectedDrive
, FDC_READ (FDCREG_SECTOR
), ReqSide
, ReqTrack
);
1191 if (status
& FDCSTAT_CRC
) {
1192 printk(KERN_ERR
"fd%d: CRC error (side %d, track %d, sector %d)\n",
1193 SelectedDrive
, ReqSide
, ReqTrack
, FDC_READ (FDCREG_SECTOR
) );
1196 if (status
& FDCSTAT_LOST
) {
1197 printk(KERN_ERR
"fd%d: lost data (side %d, track %d, sector %d)\n",
1198 SelectedDrive
, ReqSide
, ReqTrack
, FDC_READ (FDCREG_SECTOR
) );
1204 if (ReqCmd
== READ
) {
1207 addr
= ATARIHW_PRESENT( EXTD_DMA
) ? ReqData
: DMABuffer
;
1208 dma_cache_maintenance( virt_to_phys(addr
), 512, 0 );
1209 if (!ATARIHW_PRESENT( EXTD_DMA
))
1210 copy_buffer (addr
, ReqData
);
1212 dma_cache_maintenance( PhysTrackBuffer
, MaxSectors
[DriveType
] * 512, 0 );
1213 BufferDrive
= SelectedDrive
;
1214 BufferSide
= ReqSide
;
1215 BufferTrack
= ReqTrack
;
1216 copy_buffer (SECTOR_BUFFER (ReqSector
), ReqData
);
1220 if (++ReqCnt
< blk_rq_cur_sectors(fd_request
)) {
1221 /* read next sector */
1222 setup_req_params( SelectedDrive
);
1223 do_fd_action( SelectedDrive
);
1226 /* all sectors finished */
1227 fd_end_request_cur(BLK_STS_OK
);
1237 static void fd_writetrack( void )
1239 unsigned long paddr
, flags
;
1242 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack
, ReqSide
));
1244 paddr
= PhysTrackBuffer
;
1245 dma_cache_maintenance( paddr
, BUFFER_SIZE
, 1 );
1247 fd_select_side( ReqSide
);
1249 /* Cheat for track if stretch != 0 */
1250 if (SUDT
->stretch
) {
1251 track
= FDC_READ( FDCREG_TRACK
);
1253 FDC_WRITE(FDCREG_TRACK
,track
>> SUDT
->stretch
);
1258 local_irq_save(flags
);
1259 dma_wd
.dma_lo
= (unsigned char)paddr
;
1262 dma_wd
.dma_md
= (unsigned char)paddr
;
1265 if (ATARIHW_PRESENT( EXTD_DMA
))
1266 st_dma_ext_dmahi
= (unsigned short)paddr
;
1268 dma_wd
.dma_hi
= (unsigned char)paddr
;
1270 local_irq_restore(flags
);
1272 /* Clear FIFO and switch DMA to correct mode */
1273 dma_wd
.dma_mode_status
= 0x190;
1275 dma_wd
.dma_mode_status
= 0x90;
1277 dma_wd
.dma_mode_status
= 0x190;
1280 /* How many sectors for DMA */
1281 dma_wd
.fdc_acces_seccount
= BUFFER_SIZE
/512;
1284 /* Start operation */
1285 dma_wd
.dma_mode_status
= FDCSELREG_STP
| 0x100;
1287 SET_IRQ_HANDLER( fd_writetrack_done
);
1288 dma_wd
.fdc_acces_seccount
= FDCCMD_WRTRA
| get_head_settle_flag();
1292 /* wait for interrupt */
1296 static void fd_writetrack_done( int status
)
1298 DPRINT(("fd_writetrack_done()\n"));
1302 if (status
& FDCSTAT_WPROT
) {
1303 printk(KERN_NOTICE
"fd%d: is write protected\n", SelectedDrive
);
1306 if (status
& FDCSTAT_LOST
) {
1307 printk(KERN_ERR
"fd%d: lost data (side %d, track %d)\n",
1308 SelectedDrive
, ReqSide
, ReqTrack
);
1312 complete(&format_wait
);
1319 static void fd_times_out(struct timer_list
*unused
)
1321 atari_disable_irq( IRQ_MFP_FDC
);
1322 if (!FloppyIRQHandler
) goto end
; /* int occurred after timer was fired, but
1323 * before we came here... */
1325 SET_IRQ_HANDLER( NULL
);
1326 /* If the timeout occurred while the readtrack_check timer was
1327 * active, we need to cancel it, else bad things will happen */
1329 del_timer( &readtrack_timer
);
1330 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1333 printk(KERN_ERR
"floppy timeout\n" );
1336 atari_enable_irq( IRQ_MFP_FDC
);
1340 /* The (noop) seek operation here is needed to make the WP bit in the
1341 * FDC status register accessible for check_change. If the last disk
1342 * operation would have been a RDSEC, this bit would always read as 0
1343 * no matter what :-( To save time, the seek goes to the track we're
1347 static void finish_fdc( void )
1350 finish_fdc_done( 0 );
1353 DPRINT(("finish_fdc: dummy seek started\n"));
1354 FDC_WRITE (FDCREG_DATA
, SUD
.track
);
1355 SET_IRQ_HANDLER( finish_fdc_done
);
1356 FDC_WRITE (FDCREG_CMD
, FDCCMD_SEEK
);
1359 /* we must wait for the IRQ here, because the ST-DMA
1360 is released immediately afterwards and the interrupt
1361 may be delivered to the wrong driver. */
1366 static void finish_fdc_done( int dummy
)
1368 unsigned long flags
;
1370 DPRINT(("finish_fdc_done entered\n"));
1374 if (timer_pending(&fd_timer
) && time_before(fd_timer
.expires
, jiffies
+ 5))
1375 /* If the check for a disk change is done too early after this
1376 * last seek command, the WP bit still reads wrong :-((
1378 mod_timer(&fd_timer
, jiffies
+ 5);
1380 start_check_change_timer();
1381 start_motor_off_timer();
1383 local_irq_save(flags
);
1385 local_irq_restore(flags
);
1387 DPRINT(("finish_fdc() finished\n"));
1390 /* The detection of disk changes is a dark chapter in Atari history :-(
1391 * Because the "Drive ready" signal isn't present in the Atari
1392 * hardware, one has to rely on the "Write Protect". This works fine,
1393 * as long as no write protected disks are used. TOS solves this
1394 * problem by introducing tri-state logic ("maybe changed") and
1395 * looking at the serial number in block 0. This isn't possible for
1396 * Linux, since the floppy driver can't make assumptions about the
1397 * filesystem used on the disk and thus the contents of block 0. I've
1398 * chosen the method to always say "The disk was changed" if it is
1399 * unsure whether it was. This implies that every open or mount
1400 * invalidates the disk buffers if you work with write protected
1401 * disks. But at least this is better than working with incorrect data
1402 * due to unrecognised disk changes.
1405 static unsigned int floppy_check_events(struct gendisk
*disk
,
1406 unsigned int clearing
)
1408 struct atari_floppy_struct
*p
= disk
->private_data
;
1409 unsigned int drive
= p
- unit
;
1410 if (test_bit (drive
, &fake_change
)) {
1411 /* simulated change (e.g. after formatting) */
1412 return DISK_EVENT_MEDIA_CHANGE
;
1414 if (test_bit (drive
, &changed_floppies
)) {
1415 /* surely changed (the WP signal changed at least once) */
1416 return DISK_EVENT_MEDIA_CHANGE
;
1419 /* WP is on -> could be changed: to be sure, buffers should be
1422 return DISK_EVENT_MEDIA_CHANGE
;
1428 static int floppy_revalidate(struct gendisk
*disk
)
1430 struct atari_floppy_struct
*p
= disk
->private_data
;
1431 unsigned int drive
= p
- unit
;
1433 if (test_bit(drive
, &changed_floppies
) ||
1434 test_bit(drive
, &fake_change
) ||
1436 if (UD
.flags
& FTD_MSG
)
1437 printk(KERN_ERR
"floppy: clear format %p!\n", UDT
);
1439 clear_bit(drive
, &fake_change
);
1440 clear_bit(drive
, &changed_floppies
);
1441 /* MSch: clearing geometry makes sense only for autoprobe
1442 formats, for 'permanent user-defined' parameter:
1443 restore default_params[] here if flagged valid! */
1444 if (default_params
[drive
].blocks
== 0)
1447 UDT
= &default_params
[drive
];
1453 /* This sets up the global variables describing the current request. */
1455 static void setup_req_params( int drive
)
1457 int block
= ReqBlock
+ ReqCnt
;
1459 ReqTrack
= block
/ UDT
->spt
;
1460 ReqSector
= block
- ReqTrack
* UDT
->spt
+ 1;
1461 ReqSide
= ReqTrack
& 1;
1463 ReqData
= ReqBuffer
+ 512 * ReqCnt
;
1466 read_track
= (ReqCmd
== READ
&& fd_request
->error_count
== 0);
1470 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide
,
1471 ReqTrack
, ReqSector
, (unsigned long)ReqData
));
1474 static void ataflop_commit_rqs(struct blk_mq_hw_ctx
*hctx
)
1476 spin_lock_irq(&ataflop_lock
);
1477 atari_disable_irq(IRQ_MFP_FDC
);
1479 atari_enable_irq(IRQ_MFP_FDC
);
1480 spin_unlock_irq(&ataflop_lock
);
1483 static blk_status_t
ataflop_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1484 const struct blk_mq_queue_data
*bd
)
1486 struct atari_floppy_struct
*floppy
= bd
->rq
->rq_disk
->private_data
;
1487 int drive
= floppy
- unit
;
1488 int type
= floppy
->type
;
1490 spin_lock_irq(&ataflop_lock
);
1492 spin_unlock_irq(&ataflop_lock
);
1493 return BLK_STS_DEV_RESOURCE
;
1495 if (!stdma_try_lock(floppy_irq
, NULL
)) {
1496 spin_unlock_irq(&ataflop_lock
);
1497 return BLK_STS_RESOURCE
;
1499 fd_request
= bd
->rq
;
1500 blk_mq_start_request(fd_request
);
1502 atari_disable_irq( IRQ_MFP_FDC
);
1506 if (!UD
.connected
) {
1507 /* drive not connected */
1508 printk(KERN_ERR
"Unknown Device: fd%d\n", drive
);
1509 fd_end_request_cur(BLK_STS_IOERR
);
1516 UDT
= atari_disk_type
+ StartDiskType
[DriveType
];
1517 set_capacity(floppy
->disk
, UDT
->blocks
);
1522 /* user supplied disk type */
1523 if (--type
>= NUM_DISK_MINORS
) {
1524 printk(KERN_WARNING
"fd%d: invalid disk format", drive
);
1525 fd_end_request_cur(BLK_STS_IOERR
);
1528 if (minor2disktype
[type
].drive_types
> DriveType
) {
1529 printk(KERN_WARNING
"fd%d: unsupported disk format", drive
);
1530 fd_end_request_cur(BLK_STS_IOERR
);
1533 type
= minor2disktype
[type
].index
;
1534 UDT
= &atari_disk_type
[type
];
1535 set_capacity(floppy
->disk
, UDT
->blocks
);
1539 /* stop deselect timer */
1540 del_timer( &motor_off_timer
);
1543 ReqCmd
= rq_data_dir(fd_request
);
1544 ReqBlock
= blk_rq_pos(fd_request
);
1545 ReqBuffer
= bio_data(fd_request
->bio
);
1546 setup_req_params( drive
);
1547 do_fd_action( drive
);
1551 atari_enable_irq( IRQ_MFP_FDC
);
1554 spin_unlock_irq(&ataflop_lock
);
1558 static int fd_locked_ioctl(struct block_device
*bdev
, fmode_t mode
,
1559 unsigned int cmd
, unsigned long param
)
1561 struct gendisk
*disk
= bdev
->bd_disk
;
1562 struct atari_floppy_struct
*floppy
= disk
->private_data
;
1563 int drive
= floppy
- unit
;
1564 int type
= floppy
->type
;
1565 struct atari_format_descr fmt_desc
;
1566 struct atari_disk_type
*dtp
;
1567 struct floppy_struct getprm
;
1569 struct floppy_struct setprm
;
1570 void __user
*argp
= (void __user
*)param
;
1575 if (--type
>= NUM_DISK_MINORS
)
1577 if (minor2disktype
[type
].drive_types
> DriveType
)
1579 type
= minor2disktype
[type
].index
;
1580 dtp
= &atari_disk_type
[type
];
1581 if (UD
.flags
& FTD_MSG
)
1582 printk (KERN_ERR
"floppy%d: found dtp %p name %s!\n",
1583 drive
, dtp
, dtp
->name
);
1591 memset((void *)&getprm
, 0, sizeof(getprm
));
1592 getprm
.size
= dtp
->blocks
;
1593 getprm
.sect
= dtp
->spt
;
1595 getprm
.track
= dtp
->blocks
/dtp
->spt
/2;
1596 getprm
.stretch
= dtp
->stretch
;
1597 if (copy_to_user(argp
, &getprm
, sizeof(getprm
)))
1605 * MSch 7/96: simple 'set geometry' case: just set the
1606 * 'default' device params (minor == 0).
1607 * Currently, the drive geometry is cleared after each
1608 * disk change and subsequent revalidate()! simple
1609 * implementation of FDDEFPRM: save geometry from a
1610 * FDDEFPRM call and restore it in floppy_revalidate() !
1613 /* get the parameters from user space */
1614 if (floppy
->ref
!= 1 && floppy
->ref
!= -1)
1616 if (copy_from_user(&setprm
, argp
, sizeof(setprm
)))
1619 * first of all: check for floppy change and revalidate,
1620 * or the next access will revalidate - and clear UDT :-(
1623 if (floppy_check_events(disk
, 0))
1624 floppy_revalidate(disk
);
1626 if (UD
.flags
& FTD_MSG
)
1627 printk (KERN_INFO
"floppy%d: setting size %d spt %d str %d!\n",
1628 drive
, setprm
.size
, setprm
.sect
, setprm
.stretch
);
1630 /* what if type > 0 here? Overwrite specified entry ? */
1632 /* refuse to re-set a predefined type for now */
1637 * type == 0: first look for a matching entry in the type list,
1638 * and set the UD.disktype field to use the perdefined entry.
1639 * TODO: add user-defined format to head of autoprobe list ?
1640 * Useful to include the user-type for future autodetection!
1643 for (settype
= 0; settype
< NUM_DISK_MINORS
; settype
++) {
1645 if (minor2disktype
[settype
].drive_types
> DriveType
) {
1646 /* skip this one, invalid for drive ... */
1649 setidx
= minor2disktype
[settype
].index
;
1650 dtp
= &atari_disk_type
[setidx
];
1652 /* found matching entry ?? */
1653 if ( dtp
->blocks
== setprm
.size
1654 && dtp
->spt
== setprm
.sect
1655 && dtp
->stretch
== setprm
.stretch
) {
1656 if (UD
.flags
& FTD_MSG
)
1657 printk (KERN_INFO
"floppy%d: setting %s %p!\n",
1658 drive
, dtp
->name
, dtp
);
1660 set_capacity(floppy
->disk
, UDT
->blocks
);
1662 if (cmd
== FDDEFPRM
) {
1663 /* save settings as permanent default type */
1664 default_params
[drive
].name
= dtp
->name
;
1665 default_params
[drive
].spt
= dtp
->spt
;
1666 default_params
[drive
].blocks
= dtp
->blocks
;
1667 default_params
[drive
].fdc_speed
= dtp
->fdc_speed
;
1668 default_params
[drive
].stretch
= dtp
->stretch
;
1676 /* no matching disk type found above - setting user_params */
1678 if (cmd
== FDDEFPRM
) {
1679 /* set permanent type */
1680 dtp
= &default_params
[drive
];
1682 /* set user type (reset by disk change!) */
1683 dtp
= &user_params
[drive
];
1685 dtp
->name
= "user format";
1686 dtp
->blocks
= setprm
.size
;
1687 dtp
->spt
= setprm
.sect
;
1688 if (setprm
.sect
> 14)
1692 dtp
->stretch
= setprm
.stretch
;
1694 if (UD
.flags
& FTD_MSG
)
1695 printk (KERN_INFO
"floppy%d: blk %d spt %d str %d!\n",
1696 drive
, dtp
->blocks
, dtp
->spt
, dtp
->stretch
);
1699 if (setprm
.track
!= dtp
->blocks
/dtp
->spt
/2 ||
1704 set_capacity(floppy
->disk
, UDT
->blocks
);
1708 UD
.flags
|= FTD_MSG
;
1711 UD
.flags
&= ~FTD_MSG
;
1713 case FDSETEMSGTRESH
:
1718 if (floppy
->ref
!= 1 && floppy
->ref
!= -1)
1720 if (copy_from_user(&fmt_desc
, argp
, sizeof(fmt_desc
)))
1722 return do_format(drive
, type
, &fmt_desc
);
1725 /* MSch: invalidate default_params */
1726 default_params
[drive
].blocks
= 0;
1727 set_capacity(floppy
->disk
, MAX_DISK_SIZE
* 2);
1730 /* invalidate the buffer track to force a reread */
1732 set_bit(drive
, &fake_change
);
1733 check_disk_change(bdev
);
1740 static int fd_ioctl(struct block_device
*bdev
, fmode_t mode
,
1741 unsigned int cmd
, unsigned long arg
)
1745 mutex_lock(&ataflop_mutex
);
1746 ret
= fd_locked_ioctl(bdev
, mode
, cmd
, arg
);
1747 mutex_unlock(&ataflop_mutex
);
1752 /* Initialize the 'unit' variable for drive 'drive' */
1754 static void __init
fd_probe( int drive
)
1759 if (!fd_test_drive_present( drive
))
1764 switch( UserSteprate
[drive
] ) {
1766 UD
.steprate
= FDCSTEP_2
;
1769 UD
.steprate
= FDCSTEP_3
;
1772 UD
.steprate
= FDCSTEP_6
;
1775 UD
.steprate
= FDCSTEP_12
;
1777 default: /* should be -1 for "not set by user" */
1778 if (ATARIHW_PRESENT( FDCSPEED
) || MACH_IS_MEDUSA
)
1779 UD
.steprate
= FDCSTEP_3
;
1781 UD
.steprate
= FDCSTEP_6
;
1784 MotorOn
= 1; /* from probe restore operation! */
1788 /* This function tests the physical presence of a floppy drive (not
1789 * whether a disk is inserted). This is done by issuing a restore
1790 * command, waiting max. 2 seconds (that should be enough to move the
1791 * head across the whole disk) and looking at the state of the "TR00"
1792 * signal. This should now be raised if there is a drive connected
1793 * (and there is no hardware failure :-) Otherwise, the drive is
1797 static int __init
fd_test_drive_present( int drive
)
1799 unsigned long timeout
;
1800 unsigned char status
;
1803 if (drive
>= (MACH_IS_FALCON
? 1 : 2)) return( 0 );
1804 fd_select_drive( drive
);
1806 /* disable interrupt temporarily */
1807 atari_turnoff_irq( IRQ_MFP_FDC
);
1808 FDC_WRITE (FDCREG_TRACK
, 0xff00);
1809 FDC_WRITE( FDCREG_CMD
, FDCCMD_RESTORE
| FDCCMDADD_H
| FDCSTEP_6
);
1811 timeout
= jiffies
+ 2*HZ
+HZ
/2;
1812 while (time_before(jiffies
, timeout
))
1813 if (!(st_mfp
.par_dt_reg
& 0x20))
1816 status
= FDC_READ( FDCREG_STATUS
);
1817 ok
= (status
& FDCSTAT_TR00
) != 0;
1819 /* force interrupt to abort restore operation (FDC would try
1820 * about 50 seconds!) */
1821 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1823 status
= FDC_READ( FDCREG_STATUS
);
1827 /* dummy seek command to make WP bit accessible */
1828 FDC_WRITE( FDCREG_DATA
, 0 );
1829 FDC_WRITE( FDCREG_CMD
, FDCCMD_SEEK
);
1830 while( st_mfp
.par_dt_reg
& 0x20 )
1832 status
= FDC_READ( FDCREG_STATUS
);
1835 atari_turnon_irq( IRQ_MFP_FDC
);
1840 /* Look how many and which kind of drives are connected. If there are
1841 * floppies, additionally start the disk-change and motor-off timers.
1844 static void __init
config_types( void )
1848 /* for probing drives, set the FDC speed to 8 MHz */
1849 if (ATARIHW_PRESENT(FDCSPEED
))
1850 dma_wd
.fdc_speed
= 0;
1852 printk(KERN_INFO
"Probing floppy drive(s):\n");
1853 for( drive
= 0; drive
< FD_MAX_UNITS
; drive
++ ) {
1856 printk(KERN_INFO
"fd%d\n", drive
);
1861 if (FDC_READ( FDCREG_STATUS
) & FDCSTAT_BUSY
) {
1862 /* If FDC is still busy from probing, give it another FORCI
1863 * command to abort the operation. If this isn't done, the FDC
1864 * will interrupt later and its IRQ line stays low, because
1865 * the status register isn't read. And this will block any
1866 * interrupts on this IRQ line :-(
1868 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1870 FDC_READ( FDCREG_STATUS
);
1875 start_motor_off_timer();
1876 if (cnt
== 1) fd_select_drive( 0 );
1877 start_check_change_timer();
1882 * floppy_open check for aliasing (/dev/fd0 can be the same as
1883 * /dev/PS0 etc), and disallows simultaneous access to the same
1884 * drive with different device numbers.
1887 static int floppy_open(struct block_device
*bdev
, fmode_t mode
)
1889 struct atari_floppy_struct
*p
= bdev
->bd_disk
->private_data
;
1890 int type
= MINOR(bdev
->bd_dev
) >> 2;
1892 DPRINT(("fd_open: type=%d\n",type
));
1893 if (p
->ref
&& p
->type
!= type
)
1896 if (p
->ref
== -1 || (p
->ref
&& mode
& FMODE_EXCL
))
1899 if (mode
& FMODE_EXCL
)
1906 if (mode
& FMODE_NDELAY
)
1909 if (mode
& (FMODE_READ
|FMODE_WRITE
)) {
1910 check_disk_change(bdev
);
1911 if (mode
& FMODE_WRITE
) {
1924 static int floppy_unlocked_open(struct block_device
*bdev
, fmode_t mode
)
1928 mutex_lock(&ataflop_mutex
);
1929 ret
= floppy_open(bdev
, mode
);
1930 mutex_unlock(&ataflop_mutex
);
1935 static void floppy_release(struct gendisk
*disk
, fmode_t mode
)
1937 struct atari_floppy_struct
*p
= disk
->private_data
;
1938 mutex_lock(&ataflop_mutex
);
1941 else if (!p
->ref
--) {
1942 printk(KERN_ERR
"floppy_release with fd_ref == 0");
1945 mutex_unlock(&ataflop_mutex
);
1948 static const struct block_device_operations floppy_fops
= {
1949 .owner
= THIS_MODULE
,
1950 .open
= floppy_unlocked_open
,
1951 .release
= floppy_release
,
1953 .check_events
= floppy_check_events
,
1954 .revalidate_disk
= floppy_revalidate
,
1957 static const struct blk_mq_ops ataflop_mq_ops
= {
1958 .queue_rq
= ataflop_queue_rq
,
1959 .commit_rqs
= ataflop_commit_rqs
,
1962 static struct kobject
*floppy_find(dev_t dev
, int *part
, void *data
)
1964 int drive
= *part
& 3;
1965 int type
= *part
>> 2;
1966 if (drive
>= FD_MAX_UNITS
|| type
> NUM_DISK_MINORS
)
1969 return get_disk_and_module(unit
[drive
].disk
);
1972 static int __init
atari_floppy_init (void)
1978 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
1981 if (register_blkdev(FLOPPY_MAJOR
,"fd"))
1984 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
1985 unit
[i
].disk
= alloc_disk(1);
1986 if (!unit
[i
].disk
) {
1991 unit
[i
].disk
->queue
= blk_mq_init_sq_queue(&unit
[i
].tag_set
,
1993 BLK_MQ_F_SHOULD_MERGE
);
1994 if (IS_ERR(unit
[i
].disk
->queue
)) {
1995 put_disk(unit
[i
].disk
);
1996 ret
= PTR_ERR(unit
[i
].disk
->queue
);
1997 unit
[i
].disk
->queue
= NULL
;
2002 if (UseTrackbuffer
< 0)
2003 /* not set by user -> use default: for now, we turn
2004 track buffering off for all Medusas, though it
2005 could be used with ones that have a counter
2006 card. But the test is too hard :-( */
2007 UseTrackbuffer
= !MACH_IS_MEDUSA
;
2009 /* initialize variables */
2013 DMABuffer
= atari_stram_alloc(BUFFER_SIZE
+512, "ataflop");
2015 printk(KERN_ERR
"atari_floppy_init: cannot get dma buffer\n");
2019 TrackBuffer
= DMABuffer
+ 512;
2020 PhysDMABuffer
= atari_stram_to_phys(DMABuffer
);
2021 PhysTrackBuffer
= virt_to_phys(TrackBuffer
);
2022 BufferDrive
= BufferSide
= BufferTrack
= -1;
2024 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
2027 unit
[i
].disk
->major
= FLOPPY_MAJOR
;
2028 unit
[i
].disk
->first_minor
= i
;
2029 sprintf(unit
[i
].disk
->disk_name
, "fd%d", i
);
2030 unit
[i
].disk
->fops
= &floppy_fops
;
2031 unit
[i
].disk
->private_data
= &unit
[i
];
2032 set_capacity(unit
[i
].disk
, MAX_DISK_SIZE
* 2);
2033 add_disk(unit
[i
].disk
);
2036 blk_register_region(MKDEV(FLOPPY_MAJOR
, 0), 256, THIS_MODULE
,
2037 floppy_find
, NULL
, NULL
);
2039 printk(KERN_INFO
"Atari floppy driver: max. %cD, %strack buffering\n",
2040 DriveType
== 0 ? 'D' : DriveType
== 1 ? 'H' : 'E',
2041 UseTrackbuffer
? "" : "no ");
2048 struct gendisk
*disk
= unit
[i
].disk
;
2050 blk_cleanup_queue(disk
->queue
);
2051 blk_mq_free_tag_set(&unit
[i
].tag_set
);
2052 put_disk(unit
[i
].disk
);
2055 unregister_blkdev(FLOPPY_MAJOR
, "fd");
2060 static int __init
atari_floppy_setup(char *str
)
2062 int ints
[3 + FD_MAX_UNITS
];
2068 str
= get_options(str
, 3 + FD_MAX_UNITS
, ints
);
2071 printk(KERN_ERR
"ataflop_setup: no arguments!\n" );
2074 else if (ints
[0] > 2+FD_MAX_UNITS
) {
2075 printk(KERN_ERR
"ataflop_setup: too many arguments\n" );
2078 if (ints
[1] < 0 || ints
[1] > 2)
2079 printk(KERN_ERR
"ataflop_setup: bad drive type\n" );
2081 DriveType
= ints
[1];
2084 UseTrackbuffer
= (ints
[2] > 0);
2086 for( i
= 3; i
<= ints
[0] && i
-3 < FD_MAX_UNITS
; ++i
) {
2087 if (ints
[i
] != 2 && ints
[i
] != 3 && ints
[i
] != 6 && ints
[i
] != 12)
2088 printk(KERN_ERR
"ataflop_setup: bad steprate\n" );
2090 UserSteprate
[i
-3] = ints
[i
];
2095 __setup("floppy=", atari_floppy_setup
);
2098 static void __exit
atari_floppy_exit(void)
2101 blk_unregister_region(MKDEV(FLOPPY_MAJOR
, 0), 256);
2102 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
2103 del_gendisk(unit
[i
].disk
);
2104 blk_cleanup_queue(unit
[i
].disk
->queue
);
2105 blk_mq_free_tag_set(&unit
[i
].tag_set
);
2106 put_disk(unit
[i
].disk
);
2108 unregister_blkdev(FLOPPY_MAJOR
, "fd");
2110 del_timer_sync(&fd_timer
);
2111 atari_stram_free( DMABuffer
);
2114 module_init(atari_floppy_init
)
2115 module_exit(atari_floppy_exit
)
2117 MODULE_LICENSE("GPL");