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;
306 } unit[FD_MAX_UNITS];
308 #define UD unit[drive]
309 #define UDT unit[drive].disktype
310 #define SUD unit[SelectedDrive]
311 #define SUDT unit[SelectedDrive].disktype
314 #define FDC_READ(reg) ({ \
315 /* unsigned long __flags; */ \
316 unsigned short __val; \
317 /* local_irq_save(__flags); */ \
318 dma_wd.dma_mode_status = 0x80 | (reg); \
320 __val = dma_wd.fdc_acces_seccount; \
322 /* local_irq_restore(__flags); */ \
326 #define FDC_WRITE(reg,val) \
328 /* unsigned long __flags; */ \
329 /* local_irq_save(__flags); */ \
330 dma_wd.dma_mode_status = 0x80 | (reg); \
332 dma_wd.fdc_acces_seccount = (val); \
334 /* local_irq_restore(__flags); */ \
338 /* Buffering variables:
339 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
340 * operations. Second, a track buffer is used to cache a whole track
341 * of the disk to save read operations. These are two separate buffers
342 * because that allows write operations without clearing the track buffer.
345 static int MaxSectors[] = {
348 static int BufferSize[] = {
349 15*512, 30*512, 60*512
352 #define BUFFER_SIZE (BufferSize[DriveType])
354 unsigned char *DMABuffer; /* buffer for writes */
355 static unsigned long PhysDMABuffer; /* physical address */
357 static int UseTrackbuffer = -1; /* Do track buffering? */
358 module_param(UseTrackbuffer, int, 0);
360 unsigned char *TrackBuffer; /* buffer for reads */
361 static unsigned long PhysTrackBuffer; /* physical address */
362 static int BufferDrive, BufferSide, BufferTrack;
363 static int read_track; /* non-zero if we are reading whole tracks */
365 #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
366 #define IS_BUFFERED(drive,side,track) \
367 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
370 * These are global variables, as that's the easiest way to give
371 * information to interrupts. They are the data used for the current
374 static int SelectedDrive = 0;
375 static int ReqCmd, ReqBlock;
376 static int ReqSide, ReqTrack, ReqSector, ReqCnt;
377 static int HeadSettleFlag = 0;
378 static unsigned char *ReqData, *ReqBuffer;
379 static int MotorOn = 0, MotorOffTrys;
380 static int IsFormatting = 0, FormatError;
382 static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
383 module_param_array(UserSteprate, int, NULL, 0);
385 static DECLARE_COMPLETION(format_wait);
387 static unsigned long changed_floppies = 0xff, fake_change = 0;
388 #define CHECK_CHANGE_DELAY HZ/2
390 #define FD_MOTOR_OFF_DELAY (3*HZ)
391 #define FD_MOTOR_OFF_MAXTRY (10*20)
393 #define FLOPPY_TIMEOUT (6*HZ)
394 #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
395 * will be recalibrated. */
396 #define MAX_ERRORS 8 /* After this many errors the driver
401 * The driver is trying to determine the correct media format
402 * while Probing is set. fd_rwsec_done() clears it after a
405 static int Probing = 0;
407 /* This flag is set when a dummy seek is necessary to make the WP
408 * status bit accessible.
410 static int NeedSeek = 0;
414 #define DPRINT(a) printk a
419 /***************************** Prototypes *****************************/
421 static void fd_select_side( int side );
422 static void fd_select_drive( int drive );
423 static void fd_deselect( void );
424 static void fd_motor_off_timer(struct timer_list *unused);
425 static void check_change(struct timer_list *unused);
426 static irqreturn_t floppy_irq (int irq, void *dummy);
427 static void fd_error( void );
428 static int do_format(int drive, int type, struct atari_format_descr *desc);
429 static void do_fd_action( int drive );
430 static void fd_calibrate( void );
431 static void fd_calibrate_done( int status );
432 static void fd_seek( void );
433 static void fd_seek_done( int status );
434 static void fd_rwsec( void );
435 static void fd_readtrack_check(struct timer_list *unused);
436 static void fd_rwsec_done( int status );
437 static void fd_rwsec_done1(int status);
438 static void fd_writetrack( void );
439 static void fd_writetrack_done( int status );
440 static void fd_times_out(struct timer_list *unused);
441 static void finish_fdc( void );
442 static void finish_fdc_done( int dummy );
443 static void setup_req_params( int drive );
444 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
445 cmd, unsigned long param);
446 static void fd_probe( int drive );
447 static int fd_test_drive_present( int drive );
448 static void config_types( void );
449 static int floppy_open(struct block_device *bdev, fmode_t mode);
450 static void floppy_release(struct gendisk *disk, fmode_t mode);
452 /************************* End of Prototypes **************************/
454 static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer);
455 static DEFINE_TIMER(readtrack_timer, fd_readtrack_check);
456 static DEFINE_TIMER(timeout_timer, fd_times_out);
457 static DEFINE_TIMER(fd_timer, check_change);
459 static void fd_end_request_cur(blk_status_t err)
461 DPRINT(("fd_end_request_cur(), bytes %d of %d\n",
462 blk_rq_cur_bytes(fd_request),
463 blk_rq_bytes(fd_request)));
465 if (!blk_update_request(fd_request, err,
466 blk_rq_cur_bytes(fd_request))) {
467 DPRINT(("calling __blk_mq_end_request()\n"));
468 __blk_mq_end_request(fd_request, err);
471 /* requeue rest of request */
472 DPRINT(("calling blk_mq_requeue_request()\n"));
473 blk_mq_requeue_request(fd_request, true);
478 static inline void start_motor_off_timer(void)
480 mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
484 static inline void start_check_change_timer( void )
486 mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
489 static inline void start_timeout(void)
491 mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
494 static inline void stop_timeout(void)
496 del_timer(&timeout_timer);
499 /* Select the side to use. */
501 static void fd_select_side( int side )
505 /* protect against various other ints mucking around with the PSG */
506 local_irq_save(flags);
508 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
509 sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
510 sound_ym.rd_data_reg_sel & 0xfe;
512 local_irq_restore(flags);
516 /* Select a drive, update the FDC's track register and set the correct
517 * clock speed for this disk's type.
520 static void fd_select_drive( int drive )
525 if (drive == SelectedDrive)
528 /* protect against various other ints mucking around with the PSG */
529 local_irq_save(flags);
530 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
531 tmp = sound_ym.rd_data_reg_sel;
532 sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
533 atari_dont_touch_floppy_select = 1;
534 local_irq_restore(flags);
536 /* restore track register to saved value */
537 FDC_WRITE( FDCREG_TRACK, UD.track );
540 /* select 8/16 MHz */
542 if (ATARIHW_PRESENT(FDCSPEED))
543 dma_wd.fdc_speed = UDT->fdc_speed;
545 SelectedDrive = drive;
549 /* Deselect both drives. */
551 static void fd_deselect( void )
555 /* protect against various other ints mucking around with the PSG */
556 local_irq_save(flags);
557 atari_dont_touch_floppy_select = 0;
558 sound_ym.rd_data_reg_sel=14; /* Select PSG Port A */
559 sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
560 (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
561 /* On Falcon, the drive B select line is used on the printer port, so
562 * leave it alone... */
564 local_irq_restore(flags);
568 /* This timer function deselects the drives when the FDC switched the
569 * motor off. The deselection cannot happen earlier because the FDC
570 * counts the index signals, which arrive only if one drive is selected.
573 static void fd_motor_off_timer(struct timer_list *unused)
575 unsigned char status;
577 if (SelectedDrive < 0)
578 /* no drive selected, needn't deselect anyone */
581 if (stdma_islocked())
584 status = FDC_READ( FDCREG_STATUS );
586 if (!(status & 0x80)) {
587 /* motor already turned off by FDC -> deselect drives */
592 /* not yet off, try again */
595 /* Test again later; if tested too often, it seems there is no disk
596 * in the drive and the FDC will leave the motor on forever (or,
597 * at least until a disk is inserted). So we'll test only twice
598 * per second from then on...
600 mod_timer(&motor_off_timer,
601 jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
605 /* This function is repeatedly called to detect disk changes (as good
606 * as possible) and keep track of the current state of the write protection.
609 static void check_change(struct timer_list *unused)
611 static int drive = 0;
614 unsigned char old_porta;
617 if (++drive > 1 || !UD.connected)
620 /* protect against various other ints mucking around with the PSG */
621 local_irq_save(flags);
623 if (!stdma_islocked()) {
624 sound_ym.rd_data_reg_sel = 14;
625 old_porta = sound_ym.rd_data_reg_sel;
626 sound_ym.wd_data = (old_porta | DSKDRVNONE) &
627 ~(drive == 0 ? DSKDRV0 : DSKDRV1);
628 stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
629 sound_ym.wd_data = old_porta;
631 if (stat != UD.wpstat) {
632 DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
634 set_bit (drive, &changed_floppies);
637 local_irq_restore(flags);
639 start_check_change_timer();
643 /* Handling of the Head Settling Flag: This flag should be set after each
644 * seek operation, because we don't use seeks with verify.
647 static inline void set_head_settle_flag(void)
649 HeadSettleFlag = FDCCMDADD_E;
652 static inline int get_head_settle_flag(void)
654 int tmp = HeadSettleFlag;
659 static inline void copy_buffer(void *from, void *to)
661 ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
664 for (cnt = 512/4; cnt; cnt--)
668 /* General Interrupt Handling */
670 static void (*FloppyIRQHandler)( int status ) = NULL;
672 static irqreturn_t floppy_irq (int irq, void *dummy)
674 unsigned char status;
675 void (*handler)( int );
677 handler = xchg(&FloppyIRQHandler, NULL);
681 status = FDC_READ( FDCREG_STATUS );
682 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
686 DPRINT(("FDC irq, no handler\n"));
692 /* Error handling: If some error happened, retry some times, then
693 * recalibrate, then try again, and fail after MAX_ERRORS.
696 static void fd_error( void )
701 complete(&format_wait);
708 fd_request->error_count++;
709 if (fd_request->error_count >= MAX_ERRORS) {
710 printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
711 fd_end_request_cur(BLK_STS_IOERR);
715 else if (fd_request->error_count == RECALIBRATE_ERRORS) {
716 printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
717 if (SelectedDrive != -1)
720 /* need to re-run request to recalibrate */
721 atari_disable_irq( IRQ_MFP_FDC );
723 setup_req_params( SelectedDrive );
724 do_fd_action( SelectedDrive );
726 atari_enable_irq( IRQ_MFP_FDC );
731 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
734 /* ---------- Formatting ---------- */
736 #define FILL(n,val) \
738 memset( p, val, n ); \
742 static int do_format(int drive, int type, struct atari_format_descr *desc)
744 struct request_queue *q;
752 if (type >= NUM_DISK_MINORS ||
753 minor2disktype[type].drive_types > DriveType) {
759 q = unit[drive].disk[type]->queue;
760 blk_mq_freeze_queue(q);
761 blk_mq_quiesce_queue(q);
763 local_irq_save(flags);
764 stdma_lock(floppy_irq, NULL);
765 atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
766 local_irq_restore(flags);
769 type = minor2disktype[type].index;
770 UDT = &atari_disk_type[type];
773 if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
781 /* The track buffer is used for the raw track data, so its
782 contents become invalid! */
784 /* stop deselect timer */
785 del_timer( &motor_off_timer );
787 FILL( 60 * (nsect / 9), 0x4e );
788 for( sect = 0; sect < nsect; ++sect ) {
794 *p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
805 FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
809 ReqTrack = desc->track;
810 ReqSide = desc->head;
811 do_fd_action( drive );
813 wait_for_completion(&format_wait);
816 ret = FormatError ? -EIO : 0;
818 blk_mq_unquiesce_queue(q);
819 blk_mq_unfreeze_queue(q);
824 /* do_fd_action() is the general procedure for a fd request: All
825 * required parameter settings (drive select, side select, track
826 * position) are checked and set if needed. For each of these
827 * parameters and the actual reading or writing exist two functions:
828 * one that starts the setting (or skips it if possible) and one
829 * callback for the "done" interrupt. Each done func calls the next
830 * set function to propagate the request down to fd_rwsec_done().
833 static void do_fd_action( int drive )
835 DPRINT(("do_fd_action\n"));
837 if (UseTrackbuffer && !IsFormatting) {
839 if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
840 if (ReqCmd == READ) {
841 copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
842 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
843 /* read next sector */
844 setup_req_params( drive );
848 /* all sectors finished */
849 fd_end_request_cur(BLK_STS_OK);
855 /* cmd == WRITE, pay attention to track buffer
857 copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
862 if (SelectedDrive != drive)
863 fd_select_drive( drive );
867 else if (UD.track != ReqTrack << UDT->stretch)
869 else if (IsFormatting)
876 /* Seek to track 0 if the current track is unknown */
878 static void fd_calibrate( void )
880 if (SUD.track >= 0) {
881 fd_calibrate_done( 0 );
885 if (ATARIHW_PRESENT(FDCSPEED))
886 dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */
887 DPRINT(("fd_calibrate\n"));
888 SET_IRQ_HANDLER( fd_calibrate_done );
889 /* we can't verify, since the speed may be incorrect */
890 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
899 static void fd_calibrate_done( int status )
901 DPRINT(("fd_calibrate_done()\n"));
904 /* set the correct speed now */
905 if (ATARIHW_PRESENT(FDCSPEED))
906 dma_wd.fdc_speed = SUDT->fdc_speed;
907 if (status & FDCSTAT_RECNF) {
908 printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
918 /* Seek the drive to the requested track. The drive must have been
919 * calibrated at some point before this.
922 static void fd_seek( void )
924 if (SUD.track == ReqTrack << SUDT->stretch) {
929 if (ATARIHW_PRESENT(FDCSPEED)) {
930 dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
934 DPRINT(("fd_seek() to track %d\n",ReqTrack));
935 FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
937 SET_IRQ_HANDLER( fd_seek_done );
938 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
941 set_head_settle_flag();
947 static void fd_seek_done( int status )
949 DPRINT(("fd_seek_done()\n"));
952 /* set the correct speed */
953 if (ATARIHW_PRESENT(FDCSPEED))
954 dma_wd.fdc_speed = SUDT->fdc_speed;
955 if (status & FDCSTAT_RECNF) {
956 printk(KERN_ERR "fd%d: seek error (to track %d)\n",
957 SelectedDrive, ReqTrack );
958 /* we don't know exactly which track we are on now! */
963 SUD.track = ReqTrack << SUDT->stretch;
973 /* This does the actual reading/writing after positioning the head
974 * over the correct track.
977 static int MultReadInProgress = 0;
980 static void fd_rwsec( void )
982 unsigned long paddr, flags;
983 unsigned int rwflag, old_motoron;
986 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
987 if (ReqCmd == WRITE) {
988 if (ATARIHW_PRESENT(EXTD_DMA)) {
989 paddr = virt_to_phys(ReqData);
992 copy_buffer( ReqData, DMABuffer );
993 paddr = PhysDMABuffer;
995 dma_cache_maintenance( paddr, 512, 1 );
1000 paddr = PhysTrackBuffer;
1002 paddr = ATARIHW_PRESENT(EXTD_DMA) ?
1003 virt_to_phys(ReqData) : PhysDMABuffer;
1007 fd_select_side( ReqSide );
1009 /* Start sector of this operation */
1010 FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
1012 /* Cheat for track if stretch != 0 */
1013 if (SUDT->stretch) {
1014 track = FDC_READ( FDCREG_TRACK);
1016 FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
1021 local_irq_save(flags);
1022 dma_wd.dma_lo = (unsigned char)paddr;
1025 dma_wd.dma_md = (unsigned char)paddr;
1028 if (ATARIHW_PRESENT(EXTD_DMA))
1029 st_dma_ext_dmahi = (unsigned short)paddr;
1031 dma_wd.dma_hi = (unsigned char)paddr;
1033 local_irq_restore(flags);
1035 /* Clear FIFO and switch DMA to correct mode */
1036 dma_wd.dma_mode_status = 0x90 | rwflag;
1038 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
1040 dma_wd.dma_mode_status = 0x90 | rwflag;
1043 /* How many sectors for DMA */
1044 dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
1048 /* Start operation */
1049 dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
1051 SET_IRQ_HANDLER( fd_rwsec_done );
1052 dma_wd.fdc_acces_seccount =
1053 (get_head_settle_flag() |
1054 (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
1056 old_motoron = MotorOn;
1059 /* wait for interrupt */
1062 /* If reading a whole track, wait about one disk rotation and
1063 * then check if all sectors are read. The FDC will even
1064 * search for the first non-existent sector and need 1 sec to
1065 * recognise that it isn't present :-(
1067 MultReadInProgress = 1;
1068 mod_timer(&readtrack_timer,
1069 /* 1 rot. + 5 rot.s if motor was off */
1070 jiffies + HZ/5 + (old_motoron ? 0 : HZ));
1076 static void fd_readtrack_check(struct timer_list *unused)
1078 unsigned long flags, addr, addr2;
1080 local_irq_save(flags);
1082 if (!MultReadInProgress) {
1083 /* This prevents a race condition that could arise if the
1084 * interrupt is triggered while the calling of this timer
1085 * callback function takes place. The IRQ function then has
1086 * already cleared 'MultReadInProgress' when flow of control
1089 local_irq_restore(flags);
1093 /* get the current DMA address */
1094 /* ++ f.a. read twice to avoid being fooled by switcher */
1098 addr = dma_wd.dma_lo & 0xff;
1100 addr |= (dma_wd.dma_md & 0xff) << 8;
1102 if (ATARIHW_PRESENT( EXTD_DMA ))
1103 addr |= (st_dma_ext_dmahi & 0xffff) << 16;
1105 addr |= (dma_wd.dma_hi & 0xff) << 16;
1107 } while(addr != addr2);
1109 if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1110 /* already read enough data, force an FDC interrupt to stop
1111 * the read operation
1113 SET_IRQ_HANDLER( NULL );
1114 MultReadInProgress = 0;
1115 local_irq_restore(flags);
1116 DPRINT(("fd_readtrack_check(): done\n"));
1117 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1120 /* No error until now -- the FDC would have interrupted
1126 /* not yet finished, wait another tenth rotation */
1127 local_irq_restore(flags);
1128 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1129 mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1134 static void fd_rwsec_done( int status )
1136 DPRINT(("fd_rwsec_done()\n"));
1139 del_timer(&readtrack_timer);
1140 if (!MultReadInProgress)
1142 MultReadInProgress = 0;
1144 fd_rwsec_done1(status);
1147 static void fd_rwsec_done1(int status)
1153 /* Correct the track if stretch != 0 */
1154 if (SUDT->stretch) {
1155 track = FDC_READ( FDCREG_TRACK);
1157 FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1160 if (!UseTrackbuffer) {
1161 dma_wd.dma_mode_status = 0x90;
1163 if (!(dma_wd.dma_mode_status & 0x01)) {
1164 printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1170 if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1171 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1174 if ((status & FDCSTAT_RECNF) &&
1175 /* RECNF is no error after a multiple read when the FDC
1176 searched for a non-existent sector! */
1177 !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1179 if (SUDT > atari_disk_type) {
1180 if (SUDT[-1].blocks > ReqBlock) {
1181 /* try another disk type */
1183 set_capacity(unit[SelectedDrive].disk[0],
1189 if (SUD.flags & FTD_MSG)
1190 printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1191 SelectedDrive, SUDT->name );
1195 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1196 if (SUD.autoprobe) {
1197 SUDT = atari_disk_type + StartDiskType[DriveType];
1198 set_capacity(unit[SelectedDrive].disk[0],
1204 if (ATARIHW_PRESENT(FDCSPEED)) {
1205 dma_wd.fdc_speed = SUDT->fdc_speed;
1208 setup_req_params( SelectedDrive );
1210 do_fd_action( SelectedDrive );
1214 printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1215 SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1218 if (status & FDCSTAT_CRC) {
1219 printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1220 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1223 if (status & FDCSTAT_LOST) {
1224 printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1225 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1231 if (ReqCmd == READ) {
1234 addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1235 dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1236 if (!ATARIHW_PRESENT( EXTD_DMA ))
1237 copy_buffer (addr, ReqData);
1239 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1240 BufferDrive = SelectedDrive;
1241 BufferSide = ReqSide;
1242 BufferTrack = ReqTrack;
1243 copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1247 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
1248 /* read next sector */
1249 setup_req_params( SelectedDrive );
1250 do_fd_action( SelectedDrive );
1253 /* all sectors finished */
1254 fd_end_request_cur(BLK_STS_OK);
1265 static void fd_writetrack( void )
1267 unsigned long paddr, flags;
1270 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1272 paddr = PhysTrackBuffer;
1273 dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1275 fd_select_side( ReqSide );
1277 /* Cheat for track if stretch != 0 */
1278 if (SUDT->stretch) {
1279 track = FDC_READ( FDCREG_TRACK);
1281 FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1286 local_irq_save(flags);
1287 dma_wd.dma_lo = (unsigned char)paddr;
1290 dma_wd.dma_md = (unsigned char)paddr;
1293 if (ATARIHW_PRESENT( EXTD_DMA ))
1294 st_dma_ext_dmahi = (unsigned short)paddr;
1296 dma_wd.dma_hi = (unsigned char)paddr;
1298 local_irq_restore(flags);
1300 /* Clear FIFO and switch DMA to correct mode */
1301 dma_wd.dma_mode_status = 0x190;
1303 dma_wd.dma_mode_status = 0x90;
1305 dma_wd.dma_mode_status = 0x190;
1308 /* How many sectors for DMA */
1309 dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1312 /* Start operation */
1313 dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1315 SET_IRQ_HANDLER( fd_writetrack_done );
1316 dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1320 /* wait for interrupt */
1324 static void fd_writetrack_done( int status )
1326 DPRINT(("fd_writetrack_done()\n"));
1330 if (status & FDCSTAT_WPROT) {
1331 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1334 if (status & FDCSTAT_LOST) {
1335 printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1336 SelectedDrive, ReqSide, ReqTrack );
1340 complete(&format_wait);
1347 static void fd_times_out(struct timer_list *unused)
1349 atari_disable_irq( IRQ_MFP_FDC );
1350 if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1351 * before we came here... */
1353 SET_IRQ_HANDLER( NULL );
1354 /* If the timeout occurred while the readtrack_check timer was
1355 * active, we need to cancel it, else bad things will happen */
1357 del_timer( &readtrack_timer );
1358 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1361 printk(KERN_ERR "floppy timeout\n" );
1364 atari_enable_irq( IRQ_MFP_FDC );
1368 /* The (noop) seek operation here is needed to make the WP bit in the
1369 * FDC status register accessible for check_change. If the last disk
1370 * operation would have been a RDSEC, this bit would always read as 0
1371 * no matter what :-( To save time, the seek goes to the track we're
1375 static void finish_fdc( void )
1377 if (!NeedSeek || !stdma_is_locked_by(floppy_irq)) {
1378 finish_fdc_done( 0 );
1381 DPRINT(("finish_fdc: dummy seek started\n"));
1382 FDC_WRITE (FDCREG_DATA, SUD.track);
1383 SET_IRQ_HANDLER( finish_fdc_done );
1384 FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1387 /* we must wait for the IRQ here, because the ST-DMA
1388 is released immediately afterwards and the interrupt
1389 may be delivered to the wrong driver. */
1394 static void finish_fdc_done( int dummy )
1396 unsigned long flags;
1398 DPRINT(("finish_fdc_done entered\n"));
1402 if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1403 /* If the check for a disk change is done too early after this
1404 * last seek command, the WP bit still reads wrong :-((
1406 mod_timer(&fd_timer, jiffies + 5);
1408 start_check_change_timer();
1409 start_motor_off_timer();
1411 local_irq_save(flags);
1412 if (stdma_is_locked_by(floppy_irq))
1414 local_irq_restore(flags);
1416 DPRINT(("finish_fdc() finished\n"));
1419 /* The detection of disk changes is a dark chapter in Atari history :-(
1420 * Because the "Drive ready" signal isn't present in the Atari
1421 * hardware, one has to rely on the "Write Protect". This works fine,
1422 * as long as no write protected disks are used. TOS solves this
1423 * problem by introducing tri-state logic ("maybe changed") and
1424 * looking at the serial number in block 0. This isn't possible for
1425 * Linux, since the floppy driver can't make assumptions about the
1426 * filesystem used on the disk and thus the contents of block 0. I've
1427 * chosen the method to always say "The disk was changed" if it is
1428 * unsure whether it was. This implies that every open or mount
1429 * invalidates the disk buffers if you work with write protected
1430 * disks. But at least this is better than working with incorrect data
1431 * due to unrecognised disk changes.
1434 static unsigned int floppy_check_events(struct gendisk *disk,
1435 unsigned int clearing)
1437 struct atari_floppy_struct *p = disk->private_data;
1438 unsigned int drive = p - unit;
1439 if (test_bit (drive, &fake_change)) {
1440 /* simulated change (e.g. after formatting) */
1441 return DISK_EVENT_MEDIA_CHANGE;
1443 if (test_bit (drive, &changed_floppies)) {
1444 /* surely changed (the WP signal changed at least once) */
1445 return DISK_EVENT_MEDIA_CHANGE;
1448 /* WP is on -> could be changed: to be sure, buffers should be
1451 return DISK_EVENT_MEDIA_CHANGE;
1457 static int floppy_revalidate(struct gendisk *disk)
1459 struct atari_floppy_struct *p = disk->private_data;
1460 unsigned int drive = p - unit;
1462 if (test_bit(drive, &changed_floppies) ||
1463 test_bit(drive, &fake_change) ||
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 && fd_request->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->rq_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 blk_mq_start_request(fd_request);
1526 atari_disable_irq( IRQ_MFP_FDC );
1530 if (!UD.connected) {
1531 /* drive not connected */
1532 printk(KERN_ERR "Unknown Device: fd%d\n", drive );
1533 fd_end_request_cur(BLK_STS_IOERR);
1541 UDT = atari_disk_type + StartDiskType[DriveType];
1542 set_capacity(bd->rq->rq_disk, UDT->blocks);
1547 /* user supplied disk type */
1548 if (--type >= NUM_DISK_MINORS) {
1549 printk(KERN_WARNING "fd%d: invalid disk format", drive );
1550 fd_end_request_cur(BLK_STS_IOERR);
1554 if (minor2disktype[type].drive_types > DriveType) {
1555 printk(KERN_WARNING "fd%d: unsupported disk format", drive );
1556 fd_end_request_cur(BLK_STS_IOERR);
1560 type = minor2disktype[type].index;
1561 UDT = &atari_disk_type[type];
1562 set_capacity(bd->rq->rq_disk, UDT->blocks);
1566 /* stop deselect timer */
1567 del_timer( &motor_off_timer );
1570 ReqCmd = rq_data_dir(fd_request);
1571 ReqBlock = blk_rq_pos(fd_request);
1572 ReqBuffer = bio_data(fd_request->bio);
1573 setup_req_params( drive );
1574 do_fd_action( drive );
1576 atari_enable_irq( IRQ_MFP_FDC );
1579 spin_unlock_irq(&ataflop_lock);
1583 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1584 unsigned int cmd, unsigned long param)
1586 struct gendisk *disk = bdev->bd_disk;
1587 struct atari_floppy_struct *floppy = disk->private_data;
1588 int drive = floppy - unit;
1589 int type = floppy->type;
1590 struct atari_format_descr fmt_desc;
1591 struct atari_disk_type *dtp;
1592 struct floppy_struct getprm;
1594 struct floppy_struct setprm;
1595 void __user *argp = (void __user *)param;
1600 if (--type >= NUM_DISK_MINORS)
1602 if (minor2disktype[type].drive_types > DriveType)
1604 type = minor2disktype[type].index;
1605 dtp = &atari_disk_type[type];
1606 if (UD.flags & FTD_MSG)
1607 printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1608 drive, dtp, dtp->name);
1616 memset((void *)&getprm, 0, sizeof(getprm));
1617 getprm.size = dtp->blocks;
1618 getprm.sect = dtp->spt;
1620 getprm.track = dtp->blocks/dtp->spt/2;
1621 getprm.stretch = dtp->stretch;
1622 if (copy_to_user(argp, &getprm, sizeof(getprm)))
1630 * MSch 7/96: simple 'set geometry' case: just set the
1631 * 'default' device params (minor == 0).
1632 * Currently, the drive geometry is cleared after each
1633 * disk change and subsequent revalidate()! simple
1634 * implementation of FDDEFPRM: save geometry from a
1635 * FDDEFPRM call and restore it in floppy_revalidate() !
1638 /* get the parameters from user space */
1639 if (floppy->ref != 1 && floppy->ref != -1)
1641 if (copy_from_user(&setprm, argp, sizeof(setprm)))
1644 * first of all: check for floppy change and revalidate,
1645 * or the next access will revalidate - and clear UDT :-(
1648 if (floppy_check_events(disk, 0))
1649 floppy_revalidate(disk);
1651 if (UD.flags & FTD_MSG)
1652 printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1653 drive, setprm.size, setprm.sect, setprm.stretch);
1655 /* what if type > 0 here? Overwrite specified entry ? */
1657 /* refuse to re-set a predefined type for now */
1663 * type == 0: first look for a matching entry in the type list,
1664 * and set the UD.disktype field to use the perdefined entry.
1665 * TODO: add user-defined format to head of autoprobe list ?
1666 * Useful to include the user-type for future autodetection!
1669 for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1671 if (minor2disktype[settype].drive_types > DriveType) {
1672 /* skip this one, invalid for drive ... */
1675 setidx = minor2disktype[settype].index;
1676 dtp = &atari_disk_type[setidx];
1678 /* found matching entry ?? */
1679 if ( dtp->blocks == setprm.size
1680 && dtp->spt == setprm.sect
1681 && dtp->stretch == setprm.stretch ) {
1682 if (UD.flags & FTD_MSG)
1683 printk (KERN_INFO "floppy%d: setting %s %p!\n",
1684 drive, dtp->name, dtp);
1686 set_capacity(disk, UDT->blocks);
1688 if (cmd == FDDEFPRM) {
1689 /* save settings as permanent default type */
1690 default_params[drive].name = dtp->name;
1691 default_params[drive].spt = dtp->spt;
1692 default_params[drive].blocks = dtp->blocks;
1693 default_params[drive].fdc_speed = dtp->fdc_speed;
1694 default_params[drive].stretch = dtp->stretch;
1702 /* no matching disk type found above - setting user_params */
1704 if (cmd == FDDEFPRM) {
1705 /* set permanent type */
1706 dtp = &default_params[drive];
1708 /* set user type (reset by disk change!) */
1709 dtp = &user_params[drive];
1711 dtp->name = "user format";
1712 dtp->blocks = setprm.size;
1713 dtp->spt = setprm.sect;
1714 if (setprm.sect > 14)
1718 dtp->stretch = setprm.stretch;
1720 if (UD.flags & FTD_MSG)
1721 printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1722 drive, dtp->blocks, dtp->spt, dtp->stretch);
1725 if (setprm.track != dtp->blocks/dtp->spt/2 ||
1732 set_capacity(disk, UDT->blocks);
1736 UD.flags |= FTD_MSG;
1739 UD.flags &= ~FTD_MSG;
1741 case FDSETEMSGTRESH:
1746 if (floppy->ref != 1 && floppy->ref != -1)
1748 if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
1750 return do_format(drive, type, &fmt_desc);
1753 /* MSch: invalidate default_params */
1754 default_params[drive].blocks = 0;
1755 set_capacity(disk, MAX_DISK_SIZE * 2);
1759 /* invalidate the buffer track to force a reread */
1761 set_bit(drive, &fake_change);
1762 if (bdev_check_media_change(bdev))
1763 floppy_revalidate(bdev->bd_disk);
1770 static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1771 unsigned int cmd, unsigned long arg)
1775 mutex_lock(&ataflop_mutex);
1776 ret = fd_locked_ioctl(bdev, mode, cmd, arg);
1777 mutex_unlock(&ataflop_mutex);
1782 /* Initialize the 'unit' variable for drive 'drive' */
1784 static void __init fd_probe( int drive )
1789 if (!fd_test_drive_present( drive ))
1794 switch( UserSteprate[drive] ) {
1796 UD.steprate = FDCSTEP_2;
1799 UD.steprate = FDCSTEP_3;
1802 UD.steprate = FDCSTEP_6;
1805 UD.steprate = FDCSTEP_12;
1807 default: /* should be -1 for "not set by user" */
1808 if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1809 UD.steprate = FDCSTEP_3;
1811 UD.steprate = FDCSTEP_6;
1814 MotorOn = 1; /* from probe restore operation! */
1818 /* This function tests the physical presence of a floppy drive (not
1819 * whether a disk is inserted). This is done by issuing a restore
1820 * command, waiting max. 2 seconds (that should be enough to move the
1821 * head across the whole disk) and looking at the state of the "TR00"
1822 * signal. This should now be raised if there is a drive connected
1823 * (and there is no hardware failure :-) Otherwise, the drive is
1827 static int __init fd_test_drive_present( int drive )
1829 unsigned long timeout;
1830 unsigned char status;
1833 if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1834 fd_select_drive( drive );
1836 /* disable interrupt temporarily */
1837 atari_turnoff_irq( IRQ_MFP_FDC );
1838 FDC_WRITE (FDCREG_TRACK, 0xff00);
1839 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1841 timeout = jiffies + 2*HZ+HZ/2;
1842 while (time_before(jiffies, timeout))
1843 if (!(st_mfp.par_dt_reg & 0x20))
1846 status = FDC_READ( FDCREG_STATUS );
1847 ok = (status & FDCSTAT_TR00) != 0;
1849 /* force interrupt to abort restore operation (FDC would try
1850 * about 50 seconds!) */
1851 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1853 status = FDC_READ( FDCREG_STATUS );
1857 /* dummy seek command to make WP bit accessible */
1858 FDC_WRITE( FDCREG_DATA, 0 );
1859 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
1860 while( st_mfp.par_dt_reg & 0x20 )
1862 status = FDC_READ( FDCREG_STATUS );
1865 atari_turnon_irq( IRQ_MFP_FDC );
1870 /* Look how many and which kind of drives are connected. If there are
1871 * floppies, additionally start the disk-change and motor-off timers.
1874 static void __init config_types( void )
1878 /* for probing drives, set the FDC speed to 8 MHz */
1879 if (ATARIHW_PRESENT(FDCSPEED))
1880 dma_wd.fdc_speed = 0;
1882 printk(KERN_INFO "Probing floppy drive(s):\n");
1883 for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1886 printk(KERN_INFO "fd%d\n", drive);
1891 if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1892 /* If FDC is still busy from probing, give it another FORCI
1893 * command to abort the operation. If this isn't done, the FDC
1894 * will interrupt later and its IRQ line stays low, because
1895 * the status register isn't read. And this will block any
1896 * interrupts on this IRQ line :-(
1898 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1900 FDC_READ( FDCREG_STATUS );
1905 start_motor_off_timer();
1906 if (cnt == 1) fd_select_drive( 0 );
1907 start_check_change_timer();
1912 * floppy_open check for aliasing (/dev/fd0 can be the same as
1913 * /dev/PS0 etc), and disallows simultaneous access to the same
1914 * drive with different device numbers.
1917 static int floppy_open(struct block_device *bdev, fmode_t mode)
1919 struct atari_floppy_struct *p = bdev->bd_disk->private_data;
1920 int type = MINOR(bdev->bd_dev) >> 2;
1922 DPRINT(("fd_open: type=%d\n",type));
1923 if (p->ref && p->type != type)
1926 if (p->ref == -1 || (p->ref && mode & FMODE_EXCL))
1929 if (mode & FMODE_EXCL)
1936 if (mode & FMODE_NDELAY)
1939 if (mode & (FMODE_READ|FMODE_WRITE)) {
1940 if (bdev_check_media_change(bdev))
1941 floppy_revalidate(bdev->bd_disk);
1942 if (mode & FMODE_WRITE) {
1955 static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
1959 mutex_lock(&ataflop_mutex);
1960 ret = floppy_open(bdev, mode);
1961 mutex_unlock(&ataflop_mutex);
1966 static void floppy_release(struct gendisk *disk, fmode_t mode)
1968 struct atari_floppy_struct *p = disk->private_data;
1969 mutex_lock(&ataflop_mutex);
1972 else if (!p->ref--) {
1973 printk(KERN_ERR "floppy_release with fd_ref == 0");
1976 mutex_unlock(&ataflop_mutex);
1979 static const struct block_device_operations floppy_fops = {
1980 .owner = THIS_MODULE,
1981 .open = floppy_unlocked_open,
1982 .release = floppy_release,
1984 .check_events = floppy_check_events,
1987 static const struct blk_mq_ops ataflop_mq_ops = {
1988 .queue_rq = ataflop_queue_rq,
1991 static int ataflop_alloc_disk(unsigned int drive, unsigned int type)
1993 struct gendisk *disk;
1995 disk = blk_mq_alloc_disk(&unit[drive].tag_set, NULL);
1997 return PTR_ERR(disk);
1999 disk->major = FLOPPY_MAJOR;
2000 disk->first_minor = drive + (type << 2);
2002 sprintf(disk->disk_name, "fd%d", drive);
2003 disk->fops = &floppy_fops;
2004 disk->events = DISK_EVENT_MEDIA_CHANGE;
2005 disk->private_data = &unit[drive];
2006 set_capacity(disk, MAX_DISK_SIZE * 2);
2008 unit[drive].disk[type] = disk;
2012 static void ataflop_probe(dev_t dev)
2014 int drive = MINOR(dev) & 3;
2015 int type = MINOR(dev) >> 2;
2020 if (drive >= FD_MAX_UNITS || type >= NUM_DISK_MINORS)
2022 if (!unit[drive].disk[type]) {
2023 if (ataflop_alloc_disk(drive, type) == 0) {
2024 add_disk(unit[drive].disk[type]);
2025 unit[drive].registered[type] = true;
2030 static void atari_floppy_cleanup(void)
2035 for (i = 0; i < FD_MAX_UNITS; i++) {
2036 for (type = 0; type < NUM_DISK_MINORS; type++) {
2037 if (!unit[i].disk[type])
2039 del_gendisk(unit[i].disk[type]);
2040 blk_cleanup_queue(unit[i].disk[type]->queue);
2041 put_disk(unit[i].disk[type]);
2043 blk_mq_free_tag_set(&unit[i].tag_set);
2046 del_timer_sync(&fd_timer);
2047 atari_stram_free(DMABuffer);
2050 static void atari_cleanup_floppy_disk(struct atari_floppy_struct *fs)
2054 for (type = 0; type < NUM_DISK_MINORS; type++) {
2055 if (!fs->disk[type])
2057 if (fs->registered[type])
2058 del_gendisk(fs->disk[type]);
2059 blk_cleanup_disk(fs->disk[type]);
2061 blk_mq_free_tag_set(&fs->tag_set);
2064 static int __init atari_floppy_init (void)
2070 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
2073 for (i = 0; i < FD_MAX_UNITS; i++) {
2074 memset(&unit[i].tag_set, 0, sizeof(unit[i].tag_set));
2075 unit[i].tag_set.ops = &ataflop_mq_ops;
2076 unit[i].tag_set.nr_hw_queues = 1;
2077 unit[i].tag_set.nr_maps = 1;
2078 unit[i].tag_set.queue_depth = 2;
2079 unit[i].tag_set.numa_node = NUMA_NO_NODE;
2080 unit[i].tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2081 ret = blk_mq_alloc_tag_set(&unit[i].tag_set);
2085 ret = ataflop_alloc_disk(i, 0);
2087 blk_mq_free_tag_set(&unit[i].tag_set);
2092 if (UseTrackbuffer < 0)
2093 /* not set by user -> use default: for now, we turn
2094 track buffering off for all Medusas, though it
2095 could be used with ones that have a counter
2096 card. But the test is too hard :-( */
2097 UseTrackbuffer = !MACH_IS_MEDUSA;
2099 /* initialize variables */
2103 DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
2105 printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
2109 TrackBuffer = DMABuffer + 512;
2110 PhysDMABuffer = atari_stram_to_phys(DMABuffer);
2111 PhysTrackBuffer = virt_to_phys(TrackBuffer);
2112 BufferDrive = BufferSide = BufferTrack = -1;
2114 for (i = 0; i < FD_MAX_UNITS; i++) {
2117 add_disk(unit[i].disk[0]);
2118 unit[i].registered[0] = true;
2121 printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
2122 DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
2123 UseTrackbuffer ? "" : "no ");
2126 ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe);
2128 printk(KERN_ERR "atari_floppy_init: cannot register block device\n");
2129 atari_floppy_cleanup();
2135 atari_cleanup_floppy_disk(&unit[i]);
2141 static int __init atari_floppy_setup(char *str)
2143 int ints[3 + FD_MAX_UNITS];
2149 str = get_options(str, 3 + FD_MAX_UNITS, ints);
2152 printk(KERN_ERR "ataflop_setup: no arguments!\n" );
2155 else if (ints[0] > 2+FD_MAX_UNITS) {
2156 printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2159 if (ints[1] < 0 || ints[1] > 2)
2160 printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2162 DriveType = ints[1];
2165 UseTrackbuffer = (ints[2] > 0);
2167 for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2168 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2169 printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2171 UserSteprate[i-3] = ints[i];
2176 __setup("floppy=", atari_floppy_setup);
2179 static void __exit atari_floppy_exit(void)
2181 unregister_blkdev(FLOPPY_MAJOR, "fd");
2182 atari_floppy_cleanup();
2185 module_init(atari_floppy_init)
2186 module_exit(atari_floppy_exit)
2188 MODULE_LICENSE("GPL");