1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/block/ataflop.c
5 * Copyright (C) 1993 Greg Harp
6 * Atari Support by Bjoern Brauel, Roman Hodek
8 * Big cleanup Sep 11..14 1994 Roman Hodek:
9 * - Driver now works interrupt driven
10 * - Support for two drives; should work, but I cannot test that :-(
11 * - Reading is done in whole tracks and buffered to speed up things
12 * - Disk change detection and drive deselecting after motor-off
14 * - Autodetection of disk format (DD/HD); untested yet, because I
15 * don't have an HD drive :-(
17 * Fixes Nov 13 1994 Martin Schaller:
18 * - Autodetection works now
19 * - Support for 5 1/4'' disks
20 * - Removed drive type (unknown on atari)
21 * - Do seeks with 8 Mhz
23 * Changes by Andreas Schwab:
24 * - After errors in multiple read mode try again reading single sectors
26 * - Clean up error handling
27 * - Set blk_size for proper size checking
28 * - Initialize track register when testing presence of floppy
29 * - Implement some ioctl's
31 * Changes by Torsten Lang:
32 * - When probing the floppies we should add the FDCCMDADD_H flag since
33 * the FDC will otherwise wait forever when no disk is inserted...
35 * ++ Freddi Aschwanden (fa) 20.9.95 fixes for medusa:
36 * - MFPDELAY() after each FDC access -> atari
37 * - more/other disk formats
38 * - DMA to the block buffer directly if we have a 32bit DMA
39 * - for medusa, the step rate is always 3ms
40 * - on medusa, use only cache_push()
42 * - Make disk format numbering independent from minors
43 * - Let user set max. supported drive type (speeds up format
44 * detection, saves buffer space)
47 * - implement some more ioctls
51 * - increase gap size at start of track for HD/ED disks
53 * Michael (MSch) 11/07/96:
54 * - implemented FDSETPRM and FDDEFPRM ioctl
57 * - implemented missing BLK* ioctls
61 * - Maybe a better strategy for disk change detection (does anyone
65 #include <linux/module.h>
68 #include <linux/delay.h>
69 #include <linux/init.h>
70 #include <linux/blk-mq.h>
71 #include <linux/major.h>
72 #include <linux/mutex.h>
73 #include <linux/completion.h>
74 #include <linux/wait.h>
76 #include <asm/atariints.h>
77 #include <asm/atari_stdma.h>
78 #include <asm/atari_stram.h>
80 #define FD_MAX_UNITS 2
84 static DEFINE_MUTEX(ataflop_mutex
);
85 static struct request
*fd_request
;
93 #define FDCSELREG_STP (0x80) /* command/status register */
94 #define FDCSELREG_TRA (0x82) /* track register */
95 #define FDCSELREG_SEC (0x84) /* sector register */
96 #define FDCSELREG_DTA (0x86) /* data register */
98 /* register names for FDC_READ/WRITE macros */
101 #define FDCREG_STATUS 0
102 #define FDCREG_TRACK 2
103 #define FDCREG_SECTOR 4
104 #define FDCREG_DATA 6
106 /* command opcodes */
108 #define FDCCMD_RESTORE (0x00) /* - */
109 #define FDCCMD_SEEK (0x10) /* | */
110 #define FDCCMD_STEP (0x20) /* | TYP 1 Commands */
111 #define FDCCMD_STIN (0x40) /* | */
112 #define FDCCMD_STOT (0x60) /* - */
113 #define FDCCMD_RDSEC (0x80) /* - TYP 2 Commands */
114 #define FDCCMD_WRSEC (0xa0) /* - " */
115 #define FDCCMD_RDADR (0xc0) /* - */
116 #define FDCCMD_RDTRA (0xe0) /* | TYP 3 Commands */
117 #define FDCCMD_WRTRA (0xf0) /* - */
118 #define FDCCMD_FORCI (0xd0) /* - TYP 4 Command */
120 /* command modifier bits */
122 #define FDCCMDADD_SR6 (0x00) /* step rate settings */
123 #define FDCCMDADD_SR12 (0x01)
124 #define FDCCMDADD_SR2 (0x02)
125 #define FDCCMDADD_SR3 (0x03)
126 #define FDCCMDADD_V (0x04) /* verify */
127 #define FDCCMDADD_H (0x08) /* wait for spin-up */
128 #define FDCCMDADD_U (0x10) /* update track register */
129 #define FDCCMDADD_M (0x10) /* multiple sector access */
130 #define FDCCMDADD_E (0x04) /* head settling flag */
131 #define FDCCMDADD_P (0x02) /* precompensation off */
132 #define FDCCMDADD_A0 (0x01) /* DAM flag */
134 /* status register bits */
136 #define FDCSTAT_MOTORON (0x80) /* motor on */
137 #define FDCSTAT_WPROT (0x40) /* write protected (FDCCMD_WR*) */
138 #define FDCSTAT_SPINUP (0x20) /* motor speed stable (Type I) */
139 #define FDCSTAT_DELDAM (0x20) /* sector has deleted DAM (Type II+III) */
140 #define FDCSTAT_RECNF (0x10) /* record not found */
141 #define FDCSTAT_CRC (0x08) /* CRC error */
142 #define FDCSTAT_TR00 (0x04) /* Track 00 flag (Type I) */
143 #define FDCSTAT_LOST (0x04) /* Lost Data (Type II+III) */
144 #define FDCSTAT_IDX (0x02) /* Index status (Type I) */
145 #define FDCSTAT_DRQ (0x02) /* DRQ status (Type II+III) */
146 #define FDCSTAT_BUSY (0x01) /* FDC is busy */
149 /* PSG Port A Bit Nr 0 .. Side Sel .. 0 -> Side 1 1 -> Side 2 */
150 #define DSKSIDE (0x01)
152 #define DSKDRVNONE (0x06)
153 #define DSKDRV0 (0x02)
154 #define DSKDRV1 (0x04)
157 #define FDCSTEP_6 0x00
158 #define FDCSTEP_12 0x01
159 #define FDCSTEP_2 0x02
160 #define FDCSTEP_3 0x03
162 struct atari_format_descr
{
163 int track
; /* to be formatted */
164 int head
; /* "" "" */
165 int sect_offset
; /* offset of first sector */
168 /* Disk types: DD, HD, ED */
169 static struct atari_disk_type
{
171 unsigned spt
; /* sectors per track */
172 unsigned blocks
; /* total number of blocks */
173 unsigned fdc_speed
; /* fdc_speed setting */
174 unsigned stretch
; /* track doubling ? */
175 } atari_disk_type
[] = {
176 { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
177 { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
178 { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
179 { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
180 /* formats above are probed for type DD */
181 #define MAX_TYPE_DD 3
182 { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
183 { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
184 { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
185 /* formats above are probed for types DD and HD */
186 #define MAX_TYPE_HD 6
187 { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
188 { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
189 /* formats above are probed for types DD, HD and ED */
190 #define MAX_TYPE_ED 8
191 /* types below are never autoprobed */
192 { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
193 { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
194 { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
195 { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
196 { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
197 { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
198 { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
199 { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
200 { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
201 { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
202 { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
203 { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
204 { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
205 { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
206 { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
207 { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
208 { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
209 { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
210 { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
213 static int StartDiskType
[] = {
223 static int DriveType
= TYPE_HD
;
225 static DEFINE_SPINLOCK(ataflop_lock
);
227 /* Array for translating minors into disk formats */
230 unsigned drive_types
;
231 } minor2disktype
[] = {
232 { 0, TYPE_DD
}, /* 1: d360 */
233 { 4, TYPE_HD
}, /* 2: h1200 */
234 { 1, TYPE_DD
}, /* 3: D360 */
235 { 2, TYPE_DD
}, /* 4: D720 */
236 { 1, TYPE_DD
}, /* 5: h360 = D360 */
237 { 2, TYPE_DD
}, /* 6: h720 = D720 */
238 { 5, TYPE_HD
}, /* 7: H1440 */
239 { 7, TYPE_ED
}, /* 8: E2880 */
240 /* some PC formats :-) */
241 { 8, TYPE_ED
}, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
242 { 5, TYPE_HD
}, /* 10: h1440 = H1440 */
243 { 9, TYPE_HD
}, /* 11: H1680 */
244 { 10, TYPE_DD
}, /* 12: h410 */
245 { 3, TYPE_DD
}, /* 13: H820 <- == D820, 82x10 */
246 { 11, TYPE_HD
}, /* 14: h1476 */
247 { 12, TYPE_HD
}, /* 15: H1722 */
248 { 13, TYPE_DD
}, /* 16: h420 */
249 { 14, TYPE_DD
}, /* 17: H830 */
250 { 15, TYPE_HD
}, /* 18: h1494 */
251 { 16, TYPE_HD
}, /* 19: H1743 */
252 { 17, TYPE_DD
}, /* 20: h880 */
253 { 18, TYPE_DD
}, /* 21: D1040 */
254 { 19, TYPE_DD
}, /* 22: D1120 */
255 { 20, TYPE_HD
}, /* 23: h1600 */
256 { 21, TYPE_HD
}, /* 24: H1760 */
257 { 22, TYPE_HD
}, /* 25: H1920 */
258 { 23, TYPE_ED
}, /* 26: E3200 */
259 { 24, TYPE_ED
}, /* 27: E3520 */
260 { 25, TYPE_ED
}, /* 28: E3840 */
261 { 26, TYPE_HD
}, /* 29: H1840 */
262 { 27, TYPE_DD
}, /* 30: D800 */
263 { 6, TYPE_HD
}, /* 31: H1640 <- was H1600 == h1600 for PC */
266 #define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
269 * Maximum disk size (in kilobytes). This default is used whenever the
270 * current disk size is unknown.
272 #define MAX_DISK_SIZE 3280
275 * MSch: User-provided type information. 'drive' points to
276 * the respective entry of this array. Set by FDSETPRM ioctls.
278 static struct atari_disk_type user_params
[FD_MAX_UNITS
];
281 * User-provided permanent type information. 'drive' points to
282 * the respective entry of this array. Set by FDDEFPRM ioctls,
283 * restored upon disk change by floppy_revalidate() if valid (as seen by
284 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
286 static struct atari_disk_type default_params
[FD_MAX_UNITS
];
288 /* current info on each unit */
289 static struct atari_floppy_struct
{
290 int connected
; /* !=0 : drive is connected */
291 int autoprobe
; /* !=0 : do autoprobe */
293 struct atari_disk_type
*disktype
; /* current type of disk */
295 int track
; /* current head position or -1 if
297 unsigned int steprate
; /* steprate setting */
298 unsigned int wpstat
; /* current state of WP signal (for
299 disk change detection) */
300 int flags
; /* flags */
301 struct gendisk
*disk
[NUM_DISK_MINORS
];
302 bool registered
[NUM_DISK_MINORS
];
305 struct blk_mq_tag_set tag_set
;
307 } unit
[FD_MAX_UNITS
];
309 #define UD unit[drive]
310 #define UDT unit[drive].disktype
311 #define SUD unit[SelectedDrive]
312 #define SUDT unit[SelectedDrive].disktype
315 #define FDC_READ(reg) ({ \
316 /* unsigned long __flags; */ \
317 unsigned short __val; \
318 /* local_irq_save(__flags); */ \
319 dma_wd.dma_mode_status = 0x80 | (reg); \
321 __val = dma_wd.fdc_acces_seccount; \
323 /* local_irq_restore(__flags); */ \
327 #define FDC_WRITE(reg,val) \
329 /* unsigned long __flags; */ \
330 /* local_irq_save(__flags); */ \
331 dma_wd.dma_mode_status = 0x80 | (reg); \
333 dma_wd.fdc_acces_seccount = (val); \
335 /* local_irq_restore(__flags); */ \
339 /* Buffering variables:
340 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
341 * operations. Second, a track buffer is used to cache a whole track
342 * of the disk to save read operations. These are two separate buffers
343 * because that allows write operations without clearing the track buffer.
346 static int MaxSectors
[] = {
349 static int BufferSize
[] = {
350 15*512, 30*512, 60*512
353 #define BUFFER_SIZE (BufferSize[DriveType])
355 unsigned char *DMABuffer
; /* buffer for writes */
356 static unsigned long PhysDMABuffer
; /* physical address */
358 static int UseTrackbuffer
= -1; /* Do track buffering? */
359 module_param(UseTrackbuffer
, int, 0);
361 unsigned char *TrackBuffer
; /* buffer for reads */
362 static unsigned long PhysTrackBuffer
; /* physical address */
363 static int BufferDrive
, BufferSide
, BufferTrack
;
364 static int read_track
; /* non-zero if we are reading whole tracks */
366 #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
367 #define IS_BUFFERED(drive,side,track) \
368 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
371 * These are global variables, as that's the easiest way to give
372 * information to interrupts. They are the data used for the current
375 static int SelectedDrive
= 0;
376 static int ReqCmd
, ReqBlock
;
377 static int ReqSide
, ReqTrack
, ReqSector
, ReqCnt
;
378 static int HeadSettleFlag
= 0;
379 static unsigned char *ReqData
, *ReqBuffer
;
380 static int MotorOn
= 0, MotorOffTrys
;
381 static int IsFormatting
= 0, FormatError
;
383 static int UserSteprate
[FD_MAX_UNITS
] = { -1, -1 };
384 module_param_array(UserSteprate
, int, NULL
, 0);
386 static DECLARE_COMPLETION(format_wait
);
388 static unsigned long changed_floppies
= 0xff, fake_change
= 0;
389 #define CHECK_CHANGE_DELAY HZ/2
391 #define FD_MOTOR_OFF_DELAY (3*HZ)
392 #define FD_MOTOR_OFF_MAXTRY (10*20)
394 #define FLOPPY_TIMEOUT (6*HZ)
395 #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
396 * will be recalibrated. */
397 #define MAX_ERRORS 8 /* After this many errors the driver
402 * The driver is trying to determine the correct media format
403 * while Probing is set. fd_rwsec_done() clears it after a
406 static int Probing
= 0;
408 /* This flag is set when a dummy seek is necessary to make the WP
409 * status bit accessible.
411 static int NeedSeek
= 0;
415 #define DPRINT(a) printk a
420 /***************************** Prototypes *****************************/
422 static void fd_select_side( int side
);
423 static void fd_select_drive( int drive
);
424 static void fd_deselect( void );
425 static void fd_motor_off_timer(struct timer_list
*unused
);
426 static void check_change(struct timer_list
*unused
);
427 static irqreturn_t
floppy_irq (int irq
, void *dummy
);
428 static void fd_error( void );
429 static int do_format(int drive
, int type
, struct atari_format_descr
*desc
);
430 static void do_fd_action( int drive
);
431 static void fd_calibrate( void );
432 static void fd_calibrate_done( int status
);
433 static void fd_seek( void );
434 static void fd_seek_done( int status
);
435 static void fd_rwsec( void );
436 static void fd_readtrack_check(struct timer_list
*unused
);
437 static void fd_rwsec_done( int status
);
438 static void fd_rwsec_done1(int status
);
439 static void fd_writetrack( void );
440 static void fd_writetrack_done( int status
);
441 static void fd_times_out(struct timer_list
*unused
);
442 static void finish_fdc( void );
443 static void finish_fdc_done( int dummy
);
444 static void setup_req_params( int drive
);
445 static int fd_locked_ioctl(struct block_device
*bdev
, blk_mode_t mode
,
446 unsigned int cmd
, unsigned long param
);
447 static void fd_probe( int drive
);
448 static int fd_test_drive_present( int drive
);
449 static void config_types( void );
450 static int floppy_open(struct gendisk
*disk
, blk_mode_t mode
);
451 static void floppy_release(struct gendisk
*disk
);
453 /************************* End of Prototypes **************************/
455 static DEFINE_TIMER(motor_off_timer
, fd_motor_off_timer
);
456 static DEFINE_TIMER(readtrack_timer
, fd_readtrack_check
);
457 static DEFINE_TIMER(timeout_timer
, fd_times_out
);
458 static DEFINE_TIMER(fd_timer
, check_change
);
460 static void fd_end_request_cur(blk_status_t err
)
462 DPRINT(("fd_end_request_cur(), bytes %d of %d\n",
463 blk_rq_cur_bytes(fd_request
),
464 blk_rq_bytes(fd_request
)));
466 if (!blk_update_request(fd_request
, err
,
467 blk_rq_cur_bytes(fd_request
))) {
468 DPRINT(("calling __blk_mq_end_request()\n"));
469 __blk_mq_end_request(fd_request
, err
);
472 /* requeue rest of request */
473 DPRINT(("calling blk_mq_requeue_request()\n"));
474 blk_mq_requeue_request(fd_request
, true);
479 static inline void start_motor_off_timer(void)
481 mod_timer(&motor_off_timer
, jiffies
+ FD_MOTOR_OFF_DELAY
);
485 static inline void start_check_change_timer( void )
487 mod_timer(&fd_timer
, jiffies
+ CHECK_CHANGE_DELAY
);
490 static inline void start_timeout(void)
492 mod_timer(&timeout_timer
, jiffies
+ FLOPPY_TIMEOUT
);
495 static inline void stop_timeout(void)
497 del_timer(&timeout_timer
);
500 /* Select the side to use. */
502 static void fd_select_side( int side
)
506 /* protect against various other ints mucking around with the PSG */
507 local_irq_save(flags
);
509 sound_ym
.rd_data_reg_sel
= 14; /* Select PSG Port A */
510 sound_ym
.wd_data
= (side
== 0) ? sound_ym
.rd_data_reg_sel
| 0x01 :
511 sound_ym
.rd_data_reg_sel
& 0xfe;
513 local_irq_restore(flags
);
517 /* Select a drive, update the FDC's track register and set the correct
518 * clock speed for this disk's type.
521 static void fd_select_drive( int drive
)
526 if (drive
== SelectedDrive
)
529 /* protect against various other ints mucking around with the PSG */
530 local_irq_save(flags
);
531 sound_ym
.rd_data_reg_sel
= 14; /* Select PSG Port A */
532 tmp
= sound_ym
.rd_data_reg_sel
;
533 sound_ym
.wd_data
= (tmp
| DSKDRVNONE
) & ~(drive
== 0 ? DSKDRV0
: DSKDRV1
);
534 atari_dont_touch_floppy_select
= 1;
535 local_irq_restore(flags
);
537 /* restore track register to saved value */
538 FDC_WRITE( FDCREG_TRACK
, UD
.track
);
541 /* select 8/16 MHz */
543 if (ATARIHW_PRESENT(FDCSPEED
))
544 dma_wd
.fdc_speed
= UDT
->fdc_speed
;
546 SelectedDrive
= drive
;
550 /* Deselect both drives. */
552 static void fd_deselect( void )
556 /* protect against various other ints mucking around with the PSG */
557 local_irq_save(flags
);
558 atari_dont_touch_floppy_select
= 0;
559 sound_ym
.rd_data_reg_sel
=14; /* Select PSG Port A */
560 sound_ym
.wd_data
= (sound_ym
.rd_data_reg_sel
|
561 (MACH_IS_FALCON
? 3 : 7)); /* no drives selected */
562 /* On Falcon, the drive B select line is used on the printer port, so
563 * leave it alone... */
565 local_irq_restore(flags
);
569 /* This timer function deselects the drives when the FDC switched the
570 * motor off. The deselection cannot happen earlier because the FDC
571 * counts the index signals, which arrive only if one drive is selected.
574 static void fd_motor_off_timer(struct timer_list
*unused
)
576 unsigned char status
;
578 if (SelectedDrive
< 0)
579 /* no drive selected, needn't deselect anyone */
582 if (stdma_islocked())
585 status
= FDC_READ( FDCREG_STATUS
);
587 if (!(status
& 0x80)) {
588 /* motor already turned off by FDC -> deselect drives */
593 /* not yet off, try again */
596 /* Test again later; if tested too often, it seems there is no disk
597 * in the drive and the FDC will leave the motor on forever (or,
598 * at least until a disk is inserted). So we'll test only twice
599 * per second from then on...
601 mod_timer(&motor_off_timer
,
602 jiffies
+ (MotorOffTrys
++ < FD_MOTOR_OFF_MAXTRY
? HZ
/20 : HZ
/2));
606 /* This function is repeatedly called to detect disk changes (as good
607 * as possible) and keep track of the current state of the write protection.
610 static void check_change(struct timer_list
*unused
)
612 static int drive
= 0;
615 unsigned char old_porta
;
618 if (++drive
> 1 || !UD
.connected
)
621 /* protect against various other ints mucking around with the PSG */
622 local_irq_save(flags
);
624 if (!stdma_islocked()) {
625 sound_ym
.rd_data_reg_sel
= 14;
626 old_porta
= sound_ym
.rd_data_reg_sel
;
627 sound_ym
.wd_data
= (old_porta
| DSKDRVNONE
) &
628 ~(drive
== 0 ? DSKDRV0
: DSKDRV1
);
629 stat
= !!(FDC_READ( FDCREG_STATUS
) & FDCSTAT_WPROT
);
630 sound_ym
.wd_data
= old_porta
;
632 if (stat
!= UD
.wpstat
) {
633 DPRINT(( "wpstat[%d] = %d\n", drive
, stat
));
635 set_bit (drive
, &changed_floppies
);
638 local_irq_restore(flags
);
640 start_check_change_timer();
644 /* Handling of the Head Settling Flag: This flag should be set after each
645 * seek operation, because we don't use seeks with verify.
648 static inline void set_head_settle_flag(void)
650 HeadSettleFlag
= FDCCMDADD_E
;
653 static inline int get_head_settle_flag(void)
655 int tmp
= HeadSettleFlag
;
660 static inline void copy_buffer(void *from
, void *to
)
662 ulong
*p1
= (ulong
*)from
, *p2
= (ulong
*)to
;
665 for (cnt
= 512/4; cnt
; cnt
--)
669 /* General Interrupt Handling */
671 static void (*FloppyIRQHandler
)( int status
) = NULL
;
673 static irqreturn_t
floppy_irq (int irq
, void *dummy
)
675 unsigned char status
;
676 void (*handler
)( int );
678 handler
= xchg(&FloppyIRQHandler
, NULL
);
682 status
= FDC_READ( FDCREG_STATUS
);
683 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status
,(unsigned long)handler
));
687 DPRINT(("FDC irq, no handler\n"));
693 /* Error handling: If some error happened, retry some times, then
694 * recalibrate, then try again, and fail after MAX_ERRORS.
697 static void fd_error( void )
702 complete(&format_wait
);
709 unit
[SelectedDrive
].error_count
++;
710 if (unit
[SelectedDrive
].error_count
>= MAX_ERRORS
) {
711 printk(KERN_ERR
"fd%d: too many errors.\n", SelectedDrive
);
712 fd_end_request_cur(BLK_STS_IOERR
);
716 else if (unit
[SelectedDrive
].error_count
== RECALIBRATE_ERRORS
) {
717 printk(KERN_WARNING
"fd%d: recalibrating\n", SelectedDrive
);
718 if (SelectedDrive
!= -1)
721 /* need to re-run request to recalibrate */
722 atari_disable_irq( IRQ_MFP_FDC
);
724 setup_req_params( SelectedDrive
);
725 do_fd_action( SelectedDrive
);
727 atari_enable_irq( IRQ_MFP_FDC
);
732 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
735 /* ---------- Formatting ---------- */
737 #define FILL(n,val) \
739 memset( p, val, n ); \
743 static int do_format(int drive
, int type
, struct atari_format_descr
*desc
)
745 struct request_queue
*q
;
753 if (type
>= NUM_DISK_MINORS
||
754 minor2disktype
[type
].drive_types
> DriveType
) {
760 q
= unit
[drive
].disk
[type
]->queue
;
761 blk_mq_freeze_queue(q
);
762 blk_mq_quiesce_queue(q
);
764 local_irq_save(flags
);
765 stdma_lock(floppy_irq
, NULL
);
766 atari_turnon_irq( IRQ_MFP_FDC
); /* should be already, just to be sure */
767 local_irq_restore(flags
);
770 type
= minor2disktype
[type
].index
;
771 UDT
= &atari_disk_type
[type
];
774 if (!UDT
|| desc
->track
>= UDT
->blocks
/UDT
->spt
/2 || desc
->head
>= 2) {
782 /* The track buffer is used for the raw track data, so its
783 contents become invalid! */
785 /* stop deselect timer */
786 del_timer( &motor_off_timer
);
788 FILL( 60 * (nsect
/ 9), 0x4e );
789 for( sect
= 0; sect
< nsect
; ++sect
) {
795 *p
++ = (nsect
+ sect
- desc
->sect_offset
) % nsect
+ 1;
806 FILL( TrackBuffer
+BUFFER_SIZE
-p
, 0x4e );
810 ReqTrack
= desc
->track
;
811 ReqSide
= desc
->head
;
812 do_fd_action( drive
);
814 wait_for_completion(&format_wait
);
817 ret
= FormatError
? -EIO
: 0;
819 blk_mq_unquiesce_queue(q
);
820 blk_mq_unfreeze_queue(q
);
825 /* do_fd_action() is the general procedure for a fd request: All
826 * required parameter settings (drive select, side select, track
827 * position) are checked and set if needed. For each of these
828 * parameters and the actual reading or writing exist two functions:
829 * one that starts the setting (or skips it if possible) and one
830 * callback for the "done" interrupt. Each done func calls the next
831 * set function to propagate the request down to fd_rwsec_done().
834 static void do_fd_action( int drive
)
836 DPRINT(("do_fd_action\n"));
838 if (UseTrackbuffer
&& !IsFormatting
) {
840 if (IS_BUFFERED( drive
, ReqSide
, ReqTrack
)) {
841 if (ReqCmd
== READ
) {
842 copy_buffer( SECTOR_BUFFER(ReqSector
), ReqData
);
843 if (++ReqCnt
< blk_rq_cur_sectors(fd_request
)) {
844 /* read next sector */
845 setup_req_params( drive
);
849 /* all sectors finished */
850 fd_end_request_cur(BLK_STS_OK
);
856 /* cmd == WRITE, pay attention to track buffer
858 copy_buffer( ReqData
, SECTOR_BUFFER(ReqSector
) );
863 if (SelectedDrive
!= drive
)
864 fd_select_drive( drive
);
868 else if (UD
.track
!= ReqTrack
<< UDT
->stretch
)
870 else if (IsFormatting
)
877 /* Seek to track 0 if the current track is unknown */
879 static void fd_calibrate( void )
881 if (SUD
.track
>= 0) {
882 fd_calibrate_done( 0 );
886 if (ATARIHW_PRESENT(FDCSPEED
))
887 dma_wd
.fdc_speed
= 0; /* always seek with 8 Mhz */
888 DPRINT(("fd_calibrate\n"));
889 SET_IRQ_HANDLER( fd_calibrate_done
);
890 /* we can't verify, since the speed may be incorrect */
891 FDC_WRITE( FDCREG_CMD
, FDCCMD_RESTORE
| SUD
.steprate
);
900 static void fd_calibrate_done( int status
)
902 DPRINT(("fd_calibrate_done()\n"));
905 /* set the correct speed now */
906 if (ATARIHW_PRESENT(FDCSPEED
))
907 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
908 if (status
& FDCSTAT_RECNF
) {
909 printk(KERN_ERR
"fd%d: restore failed\n", SelectedDrive
);
919 /* Seek the drive to the requested track. The drive must have been
920 * calibrated at some point before this.
923 static void fd_seek( void )
925 if (SUD
.track
== ReqTrack
<< SUDT
->stretch
) {
930 if (ATARIHW_PRESENT(FDCSPEED
)) {
931 dma_wd
.fdc_speed
= 0; /* always seek witch 8 Mhz */
935 DPRINT(("fd_seek() to track %d\n",ReqTrack
));
936 FDC_WRITE( FDCREG_DATA
, ReqTrack
<< SUDT
->stretch
);
938 SET_IRQ_HANDLER( fd_seek_done
);
939 FDC_WRITE( FDCREG_CMD
, FDCCMD_SEEK
| SUD
.steprate
);
942 set_head_settle_flag();
948 static void fd_seek_done( int status
)
950 DPRINT(("fd_seek_done()\n"));
953 /* set the correct speed */
954 if (ATARIHW_PRESENT(FDCSPEED
))
955 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
956 if (status
& FDCSTAT_RECNF
) {
957 printk(KERN_ERR
"fd%d: seek error (to track %d)\n",
958 SelectedDrive
, ReqTrack
);
959 /* we don't know exactly which track we are on now! */
964 SUD
.track
= ReqTrack
<< SUDT
->stretch
;
974 /* This does the actual reading/writing after positioning the head
975 * over the correct track.
978 static int MultReadInProgress
= 0;
981 static void fd_rwsec( void )
983 unsigned long paddr
, flags
;
984 unsigned int rwflag
, old_motoron
;
987 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector
, ReqCmd
== WRITE
? 'w' : 'r' ));
988 if (ReqCmd
== WRITE
) {
989 if (ATARIHW_PRESENT(EXTD_DMA
)) {
990 paddr
= virt_to_phys(ReqData
);
993 copy_buffer( ReqData
, DMABuffer
);
994 paddr
= PhysDMABuffer
;
996 dma_cache_maintenance( paddr
, 512, 1 );
1001 paddr
= PhysTrackBuffer
;
1003 paddr
= ATARIHW_PRESENT(EXTD_DMA
) ?
1004 virt_to_phys(ReqData
) : PhysDMABuffer
;
1008 fd_select_side( ReqSide
);
1010 /* Start sector of this operation */
1011 FDC_WRITE( FDCREG_SECTOR
, read_track
? 1 : ReqSector
);
1013 /* Cheat for track if stretch != 0 */
1014 if (SUDT
->stretch
) {
1015 track
= FDC_READ( FDCREG_TRACK
);
1017 FDC_WRITE( FDCREG_TRACK
, track
>> SUDT
->stretch
);
1022 local_irq_save(flags
);
1023 dma_wd
.dma_lo
= (unsigned char)paddr
;
1026 dma_wd
.dma_md
= (unsigned char)paddr
;
1029 if (ATARIHW_PRESENT(EXTD_DMA
))
1030 st_dma_ext_dmahi
= (unsigned short)paddr
;
1032 dma_wd
.dma_hi
= (unsigned char)paddr
;
1034 local_irq_restore(flags
);
1036 /* Clear FIFO and switch DMA to correct mode */
1037 dma_wd
.dma_mode_status
= 0x90 | rwflag
;
1039 dma_wd
.dma_mode_status
= 0x90 | (rwflag
^ 0x100);
1041 dma_wd
.dma_mode_status
= 0x90 | rwflag
;
1044 /* How many sectors for DMA */
1045 dma_wd
.fdc_acces_seccount
= read_track
? SUDT
->spt
: 1;
1049 /* Start operation */
1050 dma_wd
.dma_mode_status
= FDCSELREG_STP
| rwflag
;
1052 SET_IRQ_HANDLER( fd_rwsec_done
);
1053 dma_wd
.fdc_acces_seccount
=
1054 (get_head_settle_flag() |
1055 (rwflag
? FDCCMD_WRSEC
: (FDCCMD_RDSEC
| (read_track
? FDCCMDADD_M
: 0))));
1057 old_motoron
= MotorOn
;
1060 /* wait for interrupt */
1063 /* If reading a whole track, wait about one disk rotation and
1064 * then check if all sectors are read. The FDC will even
1065 * search for the first non-existent sector and need 1 sec to
1066 * recognise that it isn't present :-(
1068 MultReadInProgress
= 1;
1069 mod_timer(&readtrack_timer
,
1070 /* 1 rot. + 5 rot.s if motor was off */
1071 jiffies
+ HZ
/5 + (old_motoron
? 0 : HZ
));
1077 static void fd_readtrack_check(struct timer_list
*unused
)
1079 unsigned long flags
, addr
, addr2
;
1081 local_irq_save(flags
);
1083 if (!MultReadInProgress
) {
1084 /* This prevents a race condition that could arise if the
1085 * interrupt is triggered while the calling of this timer
1086 * callback function takes place. The IRQ function then has
1087 * already cleared 'MultReadInProgress' when flow of control
1090 local_irq_restore(flags
);
1094 /* get the current DMA address */
1095 /* ++ f.a. read twice to avoid being fooled by switcher */
1099 addr
= dma_wd
.dma_lo
& 0xff;
1101 addr
|= (dma_wd
.dma_md
& 0xff) << 8;
1103 if (ATARIHW_PRESENT( EXTD_DMA
))
1104 addr
|= (st_dma_ext_dmahi
& 0xffff) << 16;
1106 addr
|= (dma_wd
.dma_hi
& 0xff) << 16;
1108 } while(addr
!= addr2
);
1110 if (addr
>= PhysTrackBuffer
+ SUDT
->spt
*512) {
1111 /* already read enough data, force an FDC interrupt to stop
1112 * the read operation
1114 SET_IRQ_HANDLER( NULL
);
1115 MultReadInProgress
= 0;
1116 local_irq_restore(flags
);
1117 DPRINT(("fd_readtrack_check(): done\n"));
1118 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1121 /* No error until now -- the FDC would have interrupted
1127 /* not yet finished, wait another tenth rotation */
1128 local_irq_restore(flags
);
1129 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1130 mod_timer(&readtrack_timer
, jiffies
+ HZ
/5/10);
1135 static void fd_rwsec_done( int status
)
1137 DPRINT(("fd_rwsec_done()\n"));
1140 del_timer(&readtrack_timer
);
1141 if (!MultReadInProgress
)
1143 MultReadInProgress
= 0;
1145 fd_rwsec_done1(status
);
1148 static void fd_rwsec_done1(int status
)
1154 /* Correct the track if stretch != 0 */
1155 if (SUDT
->stretch
) {
1156 track
= FDC_READ( FDCREG_TRACK
);
1158 FDC_WRITE( FDCREG_TRACK
, track
<< SUDT
->stretch
);
1161 if (!UseTrackbuffer
) {
1162 dma_wd
.dma_mode_status
= 0x90;
1164 if (!(dma_wd
.dma_mode_status
& 0x01)) {
1165 printk(KERN_ERR
"fd%d: DMA error\n", SelectedDrive
);
1171 if (ReqCmd
== WRITE
&& (status
& FDCSTAT_WPROT
)) {
1172 printk(KERN_NOTICE
"fd%d: is write protected\n", SelectedDrive
);
1175 if ((status
& FDCSTAT_RECNF
) &&
1176 /* RECNF is no error after a multiple read when the FDC
1177 searched for a non-existent sector! */
1178 !(read_track
&& FDC_READ(FDCREG_SECTOR
) > SUDT
->spt
)) {
1180 if (SUDT
> atari_disk_type
) {
1181 if (SUDT
[-1].blocks
> ReqBlock
) {
1182 /* try another disk type */
1184 set_capacity(unit
[SelectedDrive
].disk
[0],
1190 if (SUD
.flags
& FTD_MSG
)
1191 printk(KERN_INFO
"fd%d: Auto-detected floppy type %s\n",
1192 SelectedDrive
, SUDT
->name
);
1196 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1197 if (SUD
.autoprobe
) {
1198 SUDT
= atari_disk_type
+ StartDiskType
[DriveType
];
1199 set_capacity(unit
[SelectedDrive
].disk
[0],
1205 if (ATARIHW_PRESENT(FDCSPEED
)) {
1206 dma_wd
.fdc_speed
= SUDT
->fdc_speed
;
1209 setup_req_params( SelectedDrive
);
1211 do_fd_action( SelectedDrive
);
1215 printk(KERN_ERR
"fd%d: sector %d not found (side %d, track %d)\n",
1216 SelectedDrive
, FDC_READ (FDCREG_SECTOR
), ReqSide
, ReqTrack
);
1219 if (status
& FDCSTAT_CRC
) {
1220 printk(KERN_ERR
"fd%d: CRC error (side %d, track %d, sector %d)\n",
1221 SelectedDrive
, ReqSide
, ReqTrack
, FDC_READ (FDCREG_SECTOR
) );
1224 if (status
& FDCSTAT_LOST
) {
1225 printk(KERN_ERR
"fd%d: lost data (side %d, track %d, sector %d)\n",
1226 SelectedDrive
, ReqSide
, ReqTrack
, FDC_READ (FDCREG_SECTOR
) );
1232 if (ReqCmd
== READ
) {
1235 addr
= ATARIHW_PRESENT( EXTD_DMA
) ? ReqData
: DMABuffer
;
1236 dma_cache_maintenance( virt_to_phys(addr
), 512, 0 );
1237 if (!ATARIHW_PRESENT( EXTD_DMA
))
1238 copy_buffer (addr
, ReqData
);
1240 dma_cache_maintenance( PhysTrackBuffer
, MaxSectors
[DriveType
] * 512, 0 );
1241 BufferDrive
= SelectedDrive
;
1242 BufferSide
= ReqSide
;
1243 BufferTrack
= ReqTrack
;
1244 copy_buffer (SECTOR_BUFFER (ReqSector
), ReqData
);
1248 if (++ReqCnt
< blk_rq_cur_sectors(fd_request
)) {
1249 /* read next sector */
1250 setup_req_params( SelectedDrive
);
1251 do_fd_action( SelectedDrive
);
1254 /* all sectors finished */
1255 fd_end_request_cur(BLK_STS_OK
);
1266 static void fd_writetrack( void )
1268 unsigned long paddr
, flags
;
1271 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack
, ReqSide
));
1273 paddr
= PhysTrackBuffer
;
1274 dma_cache_maintenance( paddr
, BUFFER_SIZE
, 1 );
1276 fd_select_side( ReqSide
);
1278 /* Cheat for track if stretch != 0 */
1279 if (SUDT
->stretch
) {
1280 track
= FDC_READ( FDCREG_TRACK
);
1282 FDC_WRITE(FDCREG_TRACK
,track
>> SUDT
->stretch
);
1287 local_irq_save(flags
);
1288 dma_wd
.dma_lo
= (unsigned char)paddr
;
1291 dma_wd
.dma_md
= (unsigned char)paddr
;
1294 if (ATARIHW_PRESENT( EXTD_DMA
))
1295 st_dma_ext_dmahi
= (unsigned short)paddr
;
1297 dma_wd
.dma_hi
= (unsigned char)paddr
;
1299 local_irq_restore(flags
);
1301 /* Clear FIFO and switch DMA to correct mode */
1302 dma_wd
.dma_mode_status
= 0x190;
1304 dma_wd
.dma_mode_status
= 0x90;
1306 dma_wd
.dma_mode_status
= 0x190;
1309 /* How many sectors for DMA */
1310 dma_wd
.fdc_acces_seccount
= BUFFER_SIZE
/512;
1313 /* Start operation */
1314 dma_wd
.dma_mode_status
= FDCSELREG_STP
| 0x100;
1316 SET_IRQ_HANDLER( fd_writetrack_done
);
1317 dma_wd
.fdc_acces_seccount
= FDCCMD_WRTRA
| get_head_settle_flag();
1321 /* wait for interrupt */
1325 static void fd_writetrack_done( int status
)
1327 DPRINT(("fd_writetrack_done()\n"));
1331 if (status
& FDCSTAT_WPROT
) {
1332 printk(KERN_NOTICE
"fd%d: is write protected\n", SelectedDrive
);
1335 if (status
& FDCSTAT_LOST
) {
1336 printk(KERN_ERR
"fd%d: lost data (side %d, track %d)\n",
1337 SelectedDrive
, ReqSide
, ReqTrack
);
1341 complete(&format_wait
);
1348 static void fd_times_out(struct timer_list
*unused
)
1350 atari_disable_irq( IRQ_MFP_FDC
);
1351 if (!FloppyIRQHandler
) goto end
; /* int occurred after timer was fired, but
1352 * before we came here... */
1354 SET_IRQ_HANDLER( NULL
);
1355 /* If the timeout occurred while the readtrack_check timer was
1356 * active, we need to cancel it, else bad things will happen */
1358 del_timer( &readtrack_timer
);
1359 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1362 printk(KERN_ERR
"floppy timeout\n" );
1365 atari_enable_irq( IRQ_MFP_FDC
);
1369 /* The (noop) seek operation here is needed to make the WP bit in the
1370 * FDC status register accessible for check_change. If the last disk
1371 * operation would have been a RDSEC, this bit would always read as 0
1372 * no matter what :-( To save time, the seek goes to the track we're
1376 static void finish_fdc( void )
1378 if (!NeedSeek
|| !stdma_is_locked_by(floppy_irq
)) {
1379 finish_fdc_done( 0 );
1382 DPRINT(("finish_fdc: dummy seek started\n"));
1383 FDC_WRITE (FDCREG_DATA
, SUD
.track
);
1384 SET_IRQ_HANDLER( finish_fdc_done
);
1385 FDC_WRITE (FDCREG_CMD
, FDCCMD_SEEK
);
1388 /* we must wait for the IRQ here, because the ST-DMA
1389 is released immediately afterwards and the interrupt
1390 may be delivered to the wrong driver. */
1395 static void finish_fdc_done( int dummy
)
1397 unsigned long flags
;
1399 DPRINT(("finish_fdc_done entered\n"));
1403 if (timer_pending(&fd_timer
) && time_before(fd_timer
.expires
, jiffies
+ 5))
1404 /* If the check for a disk change is done too early after this
1405 * last seek command, the WP bit still reads wrong :-((
1407 mod_timer(&fd_timer
, jiffies
+ 5);
1409 start_check_change_timer();
1410 start_motor_off_timer();
1412 local_irq_save(flags
);
1413 if (stdma_is_locked_by(floppy_irq
))
1415 local_irq_restore(flags
);
1417 DPRINT(("finish_fdc() finished\n"));
1420 /* The detection of disk changes is a dark chapter in Atari history :-(
1421 * Because the "Drive ready" signal isn't present in the Atari
1422 * hardware, one has to rely on the "Write Protect". This works fine,
1423 * as long as no write protected disks are used. TOS solves this
1424 * problem by introducing tri-state logic ("maybe changed") and
1425 * looking at the serial number in block 0. This isn't possible for
1426 * Linux, since the floppy driver can't make assumptions about the
1427 * filesystem used on the disk and thus the contents of block 0. I've
1428 * chosen the method to always say "The disk was changed" if it is
1429 * unsure whether it was. This implies that every open or mount
1430 * invalidates the disk buffers if you work with write protected
1431 * disks. But at least this is better than working with incorrect data
1432 * due to unrecognised disk changes.
1435 static unsigned int floppy_check_events(struct gendisk
*disk
,
1436 unsigned int clearing
)
1438 struct atari_floppy_struct
*p
= disk
->private_data
;
1439 unsigned int drive
= p
- unit
;
1440 if (test_bit (drive
, &fake_change
)) {
1441 /* simulated change (e.g. after formatting) */
1442 return DISK_EVENT_MEDIA_CHANGE
;
1444 if (test_bit (drive
, &changed_floppies
)) {
1445 /* surely changed (the WP signal changed at least once) */
1446 return DISK_EVENT_MEDIA_CHANGE
;
1449 /* WP is on -> could be changed: to be sure, buffers should be
1452 return DISK_EVENT_MEDIA_CHANGE
;
1458 static int floppy_revalidate(struct gendisk
*disk
)
1460 struct atari_floppy_struct
*p
= disk
->private_data
;
1461 unsigned int drive
= p
- unit
;
1463 if (test_bit(drive
, &changed_floppies
) ||
1464 test_bit(drive
, &fake_change
) || !p
->disktype
) {
1465 if (UD
.flags
& FTD_MSG
)
1466 printk(KERN_ERR
"floppy: clear format %p!\n", UDT
);
1468 clear_bit(drive
, &fake_change
);
1469 clear_bit(drive
, &changed_floppies
);
1470 /* MSch: clearing geometry makes sense only for autoprobe
1471 formats, for 'permanent user-defined' parameter:
1472 restore default_params[] here if flagged valid! */
1473 if (default_params
[drive
].blocks
== 0)
1476 UDT
= &default_params
[drive
];
1482 /* This sets up the global variables describing the current request. */
1484 static void setup_req_params( int drive
)
1486 int block
= ReqBlock
+ ReqCnt
;
1488 ReqTrack
= block
/ UDT
->spt
;
1489 ReqSector
= block
- ReqTrack
* UDT
->spt
+ 1;
1490 ReqSide
= ReqTrack
& 1;
1492 ReqData
= ReqBuffer
+ 512 * ReqCnt
;
1495 read_track
= (ReqCmd
== READ
&& unit
[drive
].error_count
== 0);
1499 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide
,
1500 ReqTrack
, ReqSector
, (unsigned long)ReqData
));
1503 static blk_status_t
ataflop_queue_rq(struct blk_mq_hw_ctx
*hctx
,
1504 const struct blk_mq_queue_data
*bd
)
1506 struct atari_floppy_struct
*floppy
= bd
->rq
->q
->disk
->private_data
;
1507 int drive
= floppy
- unit
;
1508 int type
= floppy
->type
;
1510 DPRINT(("Queue request: drive %d type %d sectors %d of %d last %d\n",
1511 drive
, type
, blk_rq_cur_sectors(bd
->rq
),
1512 blk_rq_sectors(bd
->rq
), bd
->last
));
1514 spin_lock_irq(&ataflop_lock
);
1516 spin_unlock_irq(&ataflop_lock
);
1517 return BLK_STS_DEV_RESOURCE
;
1519 if (!stdma_try_lock(floppy_irq
, NULL
)) {
1520 spin_unlock_irq(&ataflop_lock
);
1521 return BLK_STS_RESOURCE
;
1523 fd_request
= bd
->rq
;
1524 unit
[drive
].error_count
= 0;
1525 blk_mq_start_request(fd_request
);
1527 atari_disable_irq( IRQ_MFP_FDC
);
1531 if (!UD
.connected
) {
1532 /* drive not connected */
1533 printk(KERN_ERR
"Unknown Device: fd%d\n", drive
);
1534 fd_end_request_cur(BLK_STS_IOERR
);
1542 UDT
= atari_disk_type
+ StartDiskType
[DriveType
];
1543 set_capacity(bd
->rq
->q
->disk
, UDT
->blocks
);
1548 /* user supplied disk type */
1549 if (--type
>= NUM_DISK_MINORS
) {
1550 printk(KERN_WARNING
"fd%d: invalid disk format", drive
);
1551 fd_end_request_cur(BLK_STS_IOERR
);
1555 if (minor2disktype
[type
].drive_types
> DriveType
) {
1556 printk(KERN_WARNING
"fd%d: unsupported disk format", drive
);
1557 fd_end_request_cur(BLK_STS_IOERR
);
1561 type
= minor2disktype
[type
].index
;
1562 UDT
= &atari_disk_type
[type
];
1563 set_capacity(bd
->rq
->q
->disk
, UDT
->blocks
);
1567 /* stop deselect timer */
1568 del_timer( &motor_off_timer
);
1571 ReqCmd
= rq_data_dir(fd_request
);
1572 ReqBlock
= blk_rq_pos(fd_request
);
1573 ReqBuffer
= bio_data(fd_request
->bio
);
1574 setup_req_params( drive
);
1575 do_fd_action( drive
);
1577 atari_enable_irq( IRQ_MFP_FDC
);
1580 spin_unlock_irq(&ataflop_lock
);
1584 static int fd_locked_ioctl(struct block_device
*bdev
, blk_mode_t mode
,
1585 unsigned int cmd
, unsigned long param
)
1587 struct gendisk
*disk
= bdev
->bd_disk
;
1588 struct atari_floppy_struct
*floppy
= disk
->private_data
;
1589 int drive
= floppy
- unit
;
1590 int type
= floppy
->type
;
1591 struct atari_format_descr fmt_desc
;
1592 struct atari_disk_type
*dtp
;
1593 struct floppy_struct getprm
;
1595 struct floppy_struct setprm
;
1596 void __user
*argp
= (void __user
*)param
;
1601 if (--type
>= NUM_DISK_MINORS
)
1603 if (minor2disktype
[type
].drive_types
> DriveType
)
1605 type
= minor2disktype
[type
].index
;
1606 dtp
= &atari_disk_type
[type
];
1607 if (UD
.flags
& FTD_MSG
)
1608 printk (KERN_ERR
"floppy%d: found dtp %p name %s!\n",
1609 drive
, dtp
, dtp
->name
);
1617 memset((void *)&getprm
, 0, sizeof(getprm
));
1618 getprm
.size
= dtp
->blocks
;
1619 getprm
.sect
= dtp
->spt
;
1621 getprm
.track
= dtp
->blocks
/dtp
->spt
/2;
1622 getprm
.stretch
= dtp
->stretch
;
1623 if (copy_to_user(argp
, &getprm
, sizeof(getprm
)))
1631 * MSch 7/96: simple 'set geometry' case: just set the
1632 * 'default' device params (minor == 0).
1633 * Currently, the drive geometry is cleared after each
1634 * disk change and subsequent revalidate()! simple
1635 * implementation of FDDEFPRM: save geometry from a
1636 * FDDEFPRM call and restore it in floppy_revalidate() !
1639 /* get the parameters from user space */
1640 if (floppy
->ref
!= 1 && floppy
->ref
!= -1)
1642 if (copy_from_user(&setprm
, argp
, sizeof(setprm
)))
1645 * first of all: check for floppy change and revalidate,
1646 * or the next access will revalidate - and clear UDT :-(
1649 if (floppy_check_events(disk
, 0))
1650 floppy_revalidate(disk
);
1652 if (UD
.flags
& FTD_MSG
)
1653 printk (KERN_INFO
"floppy%d: setting size %d spt %d str %d!\n",
1654 drive
, setprm
.size
, setprm
.sect
, setprm
.stretch
);
1656 /* what if type > 0 here? Overwrite specified entry ? */
1658 /* refuse to re-set a predefined type for now */
1664 * type == 0: first look for a matching entry in the type list,
1665 * and set the UD.disktype field to use the perdefined entry.
1666 * TODO: add user-defined format to head of autoprobe list ?
1667 * Useful to include the user-type for future autodetection!
1670 for (settype
= 0; settype
< NUM_DISK_MINORS
; settype
++) {
1672 if (minor2disktype
[settype
].drive_types
> DriveType
) {
1673 /* skip this one, invalid for drive ... */
1676 setidx
= minor2disktype
[settype
].index
;
1677 dtp
= &atari_disk_type
[setidx
];
1679 /* found matching entry ?? */
1680 if ( dtp
->blocks
== setprm
.size
1681 && dtp
->spt
== setprm
.sect
1682 && dtp
->stretch
== setprm
.stretch
) {
1683 if (UD
.flags
& FTD_MSG
)
1684 printk (KERN_INFO
"floppy%d: setting %s %p!\n",
1685 drive
, dtp
->name
, dtp
);
1687 set_capacity(disk
, UDT
->blocks
);
1689 if (cmd
== FDDEFPRM
) {
1690 /* save settings as permanent default type */
1691 default_params
[drive
].name
= dtp
->name
;
1692 default_params
[drive
].spt
= dtp
->spt
;
1693 default_params
[drive
].blocks
= dtp
->blocks
;
1694 default_params
[drive
].fdc_speed
= dtp
->fdc_speed
;
1695 default_params
[drive
].stretch
= dtp
->stretch
;
1703 /* no matching disk type found above - setting user_params */
1705 if (cmd
== FDDEFPRM
) {
1706 /* set permanent type */
1707 dtp
= &default_params
[drive
];
1709 /* set user type (reset by disk change!) */
1710 dtp
= &user_params
[drive
];
1712 dtp
->name
= "user format";
1713 dtp
->blocks
= setprm
.size
;
1714 dtp
->spt
= setprm
.sect
;
1715 if (setprm
.sect
> 14)
1719 dtp
->stretch
= setprm
.stretch
;
1721 if (UD
.flags
& FTD_MSG
)
1722 printk (KERN_INFO
"floppy%d: blk %d spt %d str %d!\n",
1723 drive
, dtp
->blocks
, dtp
->spt
, dtp
->stretch
);
1726 if (setprm
.track
!= dtp
->blocks
/dtp
->spt
/2 ||
1733 set_capacity(disk
, UDT
->blocks
);
1737 UD
.flags
|= FTD_MSG
;
1740 UD
.flags
&= ~FTD_MSG
;
1742 case FDSETEMSGTRESH
:
1747 if (floppy
->ref
!= 1 && floppy
->ref
!= -1)
1749 if (copy_from_user(&fmt_desc
, argp
, sizeof(fmt_desc
)))
1751 return do_format(drive
, type
, &fmt_desc
);
1754 /* MSch: invalidate default_params */
1755 default_params
[drive
].blocks
= 0;
1756 set_capacity(disk
, MAX_DISK_SIZE
* 2);
1760 /* invalidate the buffer track to force a reread */
1762 set_bit(drive
, &fake_change
);
1763 if (disk_check_media_change(disk
)) {
1764 bdev_mark_dead(disk
->part0
, true);
1765 floppy_revalidate(disk
);
1773 static int fd_ioctl(struct block_device
*bdev
, blk_mode_t mode
,
1774 unsigned int cmd
, unsigned long arg
)
1778 mutex_lock(&ataflop_mutex
);
1779 ret
= fd_locked_ioctl(bdev
, mode
, cmd
, arg
);
1780 mutex_unlock(&ataflop_mutex
);
1785 /* Initialize the 'unit' variable for drive 'drive' */
1787 static void __init
fd_probe( int drive
)
1792 if (!fd_test_drive_present( drive
))
1797 switch( UserSteprate
[drive
] ) {
1799 UD
.steprate
= FDCSTEP_2
;
1802 UD
.steprate
= FDCSTEP_3
;
1805 UD
.steprate
= FDCSTEP_6
;
1808 UD
.steprate
= FDCSTEP_12
;
1810 default: /* should be -1 for "not set by user" */
1811 if (ATARIHW_PRESENT( FDCSPEED
) || MACH_IS_MEDUSA
)
1812 UD
.steprate
= FDCSTEP_3
;
1814 UD
.steprate
= FDCSTEP_6
;
1817 MotorOn
= 1; /* from probe restore operation! */
1821 /* This function tests the physical presence of a floppy drive (not
1822 * whether a disk is inserted). This is done by issuing a restore
1823 * command, waiting max. 2 seconds (that should be enough to move the
1824 * head across the whole disk) and looking at the state of the "TR00"
1825 * signal. This should now be raised if there is a drive connected
1826 * (and there is no hardware failure :-) Otherwise, the drive is
1830 static int __init
fd_test_drive_present( int drive
)
1832 unsigned long timeout
;
1833 unsigned char status
;
1836 if (drive
>= (MACH_IS_FALCON
? 1 : 2)) return( 0 );
1837 fd_select_drive( drive
);
1839 /* disable interrupt temporarily */
1840 atari_turnoff_irq( IRQ_MFP_FDC
);
1841 FDC_WRITE (FDCREG_TRACK
, 0xff00);
1842 FDC_WRITE( FDCREG_CMD
, FDCCMD_RESTORE
| FDCCMDADD_H
| FDCSTEP_6
);
1844 timeout
= jiffies
+ 2*HZ
+HZ
/2;
1845 while (time_before(jiffies
, timeout
))
1846 if (!(st_mfp
.par_dt_reg
& 0x20))
1849 status
= FDC_READ( FDCREG_STATUS
);
1850 ok
= (status
& FDCSTAT_TR00
) != 0;
1852 /* force interrupt to abort restore operation (FDC would try
1853 * about 50 seconds!) */
1854 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1856 status
= FDC_READ( FDCREG_STATUS
);
1860 /* dummy seek command to make WP bit accessible */
1861 FDC_WRITE( FDCREG_DATA
, 0 );
1862 FDC_WRITE( FDCREG_CMD
, FDCCMD_SEEK
);
1863 while( st_mfp
.par_dt_reg
& 0x20 )
1865 status
= FDC_READ( FDCREG_STATUS
);
1868 atari_turnon_irq( IRQ_MFP_FDC
);
1873 /* Look how many and which kind of drives are connected. If there are
1874 * floppies, additionally start the disk-change and motor-off timers.
1877 static void __init
config_types( void )
1881 /* for probing drives, set the FDC speed to 8 MHz */
1882 if (ATARIHW_PRESENT(FDCSPEED
))
1883 dma_wd
.fdc_speed
= 0;
1885 printk(KERN_INFO
"Probing floppy drive(s):\n");
1886 for( drive
= 0; drive
< FD_MAX_UNITS
; drive
++ ) {
1889 printk(KERN_INFO
"fd%d\n", drive
);
1894 if (FDC_READ( FDCREG_STATUS
) & FDCSTAT_BUSY
) {
1895 /* If FDC is still busy from probing, give it another FORCI
1896 * command to abort the operation. If this isn't done, the FDC
1897 * will interrupt later and its IRQ line stays low, because
1898 * the status register isn't read. And this will block any
1899 * interrupts on this IRQ line :-(
1901 FDC_WRITE( FDCREG_CMD
, FDCCMD_FORCI
);
1903 FDC_READ( FDCREG_STATUS
);
1908 start_motor_off_timer();
1909 if (cnt
== 1) fd_select_drive( 0 );
1910 start_check_change_timer();
1915 * floppy_open check for aliasing (/dev/fd0 can be the same as
1916 * /dev/PS0 etc), and disallows simultaneous access to the same
1917 * drive with different device numbers.
1920 static int floppy_open(struct gendisk
*disk
, blk_mode_t mode
)
1922 struct atari_floppy_struct
*p
= disk
->private_data
;
1923 int type
= disk
->first_minor
>> 2;
1925 DPRINT(("fd_open: type=%d\n",type
));
1926 if (p
->ref
&& p
->type
!= type
)
1929 if (p
->ref
== -1 || (p
->ref
&& mode
& BLK_OPEN_EXCL
))
1931 if (mode
& BLK_OPEN_EXCL
)
1938 if (mode
& BLK_OPEN_NDELAY
)
1941 if (mode
& (BLK_OPEN_READ
| BLK_OPEN_WRITE
)) {
1942 if (disk_check_media_change(disk
))
1943 floppy_revalidate(disk
);
1944 if (mode
& BLK_OPEN_WRITE
) {
1957 static int floppy_unlocked_open(struct gendisk
*disk
, blk_mode_t mode
)
1961 mutex_lock(&ataflop_mutex
);
1962 ret
= floppy_open(disk
, mode
);
1963 mutex_unlock(&ataflop_mutex
);
1968 static void floppy_release(struct gendisk
*disk
)
1970 struct atari_floppy_struct
*p
= disk
->private_data
;
1971 mutex_lock(&ataflop_mutex
);
1974 else if (!p
->ref
--) {
1975 printk(KERN_ERR
"floppy_release with fd_ref == 0");
1978 mutex_unlock(&ataflop_mutex
);
1981 static const struct block_device_operations floppy_fops
= {
1982 .owner
= THIS_MODULE
,
1983 .open
= floppy_unlocked_open
,
1984 .release
= floppy_release
,
1986 .check_events
= floppy_check_events
,
1989 static const struct blk_mq_ops ataflop_mq_ops
= {
1990 .queue_rq
= ataflop_queue_rq
,
1993 static int ataflop_alloc_disk(unsigned int drive
, unsigned int type
)
1995 struct queue_limits lim
= {
1996 .features
= BLK_FEAT_ROTATIONAL
,
1998 struct gendisk
*disk
;
2000 disk
= blk_mq_alloc_disk(&unit
[drive
].tag_set
, &lim
, NULL
);
2002 return PTR_ERR(disk
);
2004 disk
->major
= FLOPPY_MAJOR
;
2005 disk
->first_minor
= drive
+ (type
<< 2);
2007 sprintf(disk
->disk_name
, "fd%d", drive
);
2008 disk
->fops
= &floppy_fops
;
2009 disk
->flags
|= GENHD_FL_NO_PART
;
2010 disk
->events
= DISK_EVENT_MEDIA_CHANGE
;
2011 disk
->private_data
= &unit
[drive
];
2012 set_capacity(disk
, MAX_DISK_SIZE
* 2);
2014 unit
[drive
].disk
[type
] = disk
;
2018 static void ataflop_probe(dev_t dev
)
2020 int drive
= MINOR(dev
) & 3;
2021 int type
= MINOR(dev
) >> 2;
2026 if (drive
>= FD_MAX_UNITS
|| type
>= NUM_DISK_MINORS
)
2028 if (unit
[drive
].disk
[type
])
2030 if (ataflop_alloc_disk(drive
, type
))
2032 if (add_disk(unit
[drive
].disk
[type
]))
2034 unit
[drive
].registered
[type
] = true;
2038 put_disk(unit
[drive
].disk
[type
]);
2039 unit
[drive
].disk
[type
] = NULL
;
2042 static void atari_floppy_cleanup(void)
2047 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
2048 for (type
= 0; type
< NUM_DISK_MINORS
; type
++) {
2049 if (!unit
[i
].disk
[type
])
2051 del_gendisk(unit
[i
].disk
[type
]);
2052 put_disk(unit
[i
].disk
[type
]);
2054 blk_mq_free_tag_set(&unit
[i
].tag_set
);
2057 del_timer_sync(&fd_timer
);
2058 atari_stram_free(DMABuffer
);
2061 static void atari_cleanup_floppy_disk(struct atari_floppy_struct
*fs
)
2065 for (type
= 0; type
< NUM_DISK_MINORS
; type
++) {
2066 if (!fs
->disk
[type
])
2068 if (fs
->registered
[type
])
2069 del_gendisk(fs
->disk
[type
]);
2070 put_disk(fs
->disk
[type
]);
2072 blk_mq_free_tag_set(&fs
->tag_set
);
2075 static int __init
atari_floppy_init (void)
2081 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
2084 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
2085 memset(&unit
[i
].tag_set
, 0, sizeof(unit
[i
].tag_set
));
2086 unit
[i
].tag_set
.ops
= &ataflop_mq_ops
;
2087 unit
[i
].tag_set
.nr_hw_queues
= 1;
2088 unit
[i
].tag_set
.nr_maps
= 1;
2089 unit
[i
].tag_set
.queue_depth
= 2;
2090 unit
[i
].tag_set
.numa_node
= NUMA_NO_NODE
;
2091 unit
[i
].tag_set
.flags
= BLK_MQ_F_SHOULD_MERGE
;
2092 ret
= blk_mq_alloc_tag_set(&unit
[i
].tag_set
);
2096 ret
= ataflop_alloc_disk(i
, 0);
2098 blk_mq_free_tag_set(&unit
[i
].tag_set
);
2103 if (UseTrackbuffer
< 0)
2104 /* not set by user -> use default: for now, we turn
2105 track buffering off for all Medusas, though it
2106 could be used with ones that have a counter
2107 card. But the test is too hard :-( */
2108 UseTrackbuffer
= !MACH_IS_MEDUSA
;
2110 /* initialize variables */
2114 DMABuffer
= atari_stram_alloc(BUFFER_SIZE
+512, "ataflop");
2116 printk(KERN_ERR
"atari_floppy_init: cannot get dma buffer\n");
2120 TrackBuffer
= DMABuffer
+ 512;
2121 PhysDMABuffer
= atari_stram_to_phys(DMABuffer
);
2122 PhysTrackBuffer
= virt_to_phys(TrackBuffer
);
2123 BufferDrive
= BufferSide
= BufferTrack
= -1;
2125 for (i
= 0; i
< FD_MAX_UNITS
; i
++) {
2128 ret
= add_disk(unit
[i
].disk
[0]);
2131 unit
[i
].registered
[0] = true;
2134 printk(KERN_INFO
"Atari floppy driver: max. %cD, %strack buffering\n",
2135 DriveType
== 0 ? 'D' : DriveType
== 1 ? 'H' : 'E',
2136 UseTrackbuffer
? "" : "no ");
2139 ret
= __register_blkdev(FLOPPY_MAJOR
, "fd", ataflop_probe
);
2141 printk(KERN_ERR
"atari_floppy_init: cannot register block device\n");
2142 atari_floppy_cleanup();
2147 atari_stram_free(DMABuffer
);
2150 atari_cleanup_floppy_disk(&unit
[i
]);
2156 static int __init
atari_floppy_setup(char *str
)
2158 int ints
[3 + FD_MAX_UNITS
];
2164 str
= get_options(str
, 3 + FD_MAX_UNITS
, ints
);
2167 printk(KERN_ERR
"ataflop_setup: no arguments!\n" );
2170 else if (ints
[0] > 2+FD_MAX_UNITS
) {
2171 printk(KERN_ERR
"ataflop_setup: too many arguments\n" );
2174 if (ints
[1] < 0 || ints
[1] > 2)
2175 printk(KERN_ERR
"ataflop_setup: bad drive type\n" );
2177 DriveType
= ints
[1];
2180 UseTrackbuffer
= (ints
[2] > 0);
2182 for( i
= 3; i
<= ints
[0] && i
-3 < FD_MAX_UNITS
; ++i
) {
2183 if (ints
[i
] != 2 && ints
[i
] != 3 && ints
[i
] != 6 && ints
[i
] != 12)
2184 printk(KERN_ERR
"ataflop_setup: bad steprate\n" );
2186 UserSteprate
[i
-3] = ints
[i
];
2191 __setup("floppy=", atari_floppy_setup
);
2194 static void __exit
atari_floppy_exit(void)
2196 unregister_blkdev(FLOPPY_MAJOR
, "fd");
2197 atari_floppy_cleanup();
2200 module_init(atari_floppy_init
)
2201 module_exit(atari_floppy_exit
)
2203 MODULE_DESCRIPTION("Atari floppy driver");
2204 MODULE_LICENSE("GPL");