* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / block / ataflop.c
blobb8df872053afa41e494191cd5d0267c293fb4d92
1 /*
2 * drivers/block/ataflop.c
4 * Copyright (C) 1993 Greg Harp
5 * Atari Support by Bjoern Brauel, Roman Hodek
7 * Big cleanup Sep 11..14 1994 Roman Hodek:
8 * - Driver now works interrupt driven
9 * - Support for two drives; should work, but I cannot test that :-(
10 * - Reading is done in whole tracks and buffered to speed up things
11 * - Disk change detection and drive deselecting after motor-off
12 * similar to TOS
13 * - Autodetection of disk format (DD/HD); untested yet, because I
14 * don't have an HD drive :-(
16 * Fixes Nov 13 1994 Martin Schaller:
17 * - Autodetection works now
18 * - Support for 5 1/4'' disks
19 * - Removed drive type (unknown on atari)
20 * - Do seeks with 8 Mhz
22 * Changes by Andreas Schwab:
23 * - After errors in multiple read mode try again reading single sectors
24 * (Feb 1995):
25 * - Clean up error handling
26 * - Set blk_size for proper size checking
27 * - Initialize track register when testing presence of floppy
28 * - Implement some ioctl's
30 * Changes by Torsten Lang:
31 * - When probing the floppies we should add the FDCCMDADD_H flag since
32 * the FDC will otherwise wait forever when no disk is inserted...
34 * ++ Freddi Aschwanden (fa) 20.9.95 fixes for medusa:
35 * - MFPDELAY() after each FDC access -> atari
36 * - more/other disk formats
37 * - DMA to the block buffer directly if we have a 32bit DMA
38 * - for medusa, the step rate is always 3ms
39 * - on medusa, use only cache_push()
40 * Roman:
41 * - Make disk format numbering independent from minors
42 * - Let user set max. supported drive type (speeds up format
43 * detection, saves buffer space)
45 * Roman 10/15/95:
46 * - implement some more ioctls
47 * - disk formatting
49 * Andreas 95/12/12:
50 * - increase gap size at start of track for HD/ED disks
52 * Michael (MSch) 11/07/96:
53 * - implemented FDSETPRM and FDDEFPRM ioctl
55 * Andreas (97/03/19):
56 * - implemented missing BLK* ioctls
58 * Things left to do:
59 * - Formatting
60 * - Maybe a better strategy for disk change detection (does anyone
61 * know one?)
64 #include <linux/module.h>
66 #include <linux/sched.h>
67 #include <linux/string.h>
68 #include <linux/fs.h>
69 #include <linux/fcntl.h>
70 #include <linux/kernel.h>
71 #include <linux/timer.h>
72 #include <linux/fd.h>
73 #include <linux/errno.h>
74 #include <linux/types.h>
75 #include <linux/delay.h>
76 #include <linux/mm.h>
77 #include <linux/malloc.h>
78 #include <linux/init.h>
80 #include <asm/setup.h>
81 #include <asm/system.h>
82 #include <asm/bitops.h>
83 #include <asm/irq.h>
84 #include <asm/pgtable.h>
85 #include <asm/uaccess.h>
87 #include <asm/atafd.h>
88 #include <asm/atafdreg.h>
89 #include <asm/atarihw.h>
90 #include <asm/atariints.h>
91 #include <asm/atari_stdma.h>
92 #include <asm/atari_stram.h>
94 #define MAJOR_NR FLOPPY_MAJOR
95 #include <linux/blk.h>
96 #include <linux/blkpg.h>
98 #define FD_MAX_UNITS 2
100 #undef DEBUG
102 /* Disk types: DD, HD, ED */
103 static struct atari_disk_type {
104 const char *name;
105 unsigned spt; /* sectors per track */
106 unsigned blocks; /* total number of blocks */
107 unsigned fdc_speed; /* fdc_speed setting */
108 unsigned stretch; /* track doubling ? */
109 } disk_type[] = {
110 { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
111 { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
112 { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
113 { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
114 /* formats above are probed for type DD */
115 #define MAX_TYPE_DD 3
116 { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
117 { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
118 { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
119 /* formats above are probed for types DD and HD */
120 #define MAX_TYPE_HD 6
121 { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
122 { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
123 /* formats above are probed for types DD, HD and ED */
124 #define MAX_TYPE_ED 8
125 /* types below are never autoprobed */
126 { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
127 { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
128 { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
129 { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
130 { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
131 { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
132 { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
133 { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
134 { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
135 { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
136 { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
137 { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
138 { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
139 { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
140 { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
141 { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
142 { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
143 { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
144 { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
147 static int StartDiskType[] = {
148 MAX_TYPE_DD,
149 MAX_TYPE_HD,
150 MAX_TYPE_ED
153 #define TYPE_DD 0
154 #define TYPE_HD 1
155 #define TYPE_ED 2
157 static int DriveType = TYPE_HD;
159 /* Array for translating minors into disk formats */
160 static struct {
161 int index;
162 unsigned drive_types;
163 } minor2disktype[] = {
164 { 0, TYPE_DD }, /* 1: d360 */
165 { 4, TYPE_HD }, /* 2: h1200 */
166 { 1, TYPE_DD }, /* 3: D360 */
167 { 2, TYPE_DD }, /* 4: D720 */
168 { 1, TYPE_DD }, /* 5: h360 = D360 */
169 { 2, TYPE_DD }, /* 6: h720 = D720 */
170 { 5, TYPE_HD }, /* 7: H1440 */
171 { 7, TYPE_ED }, /* 8: E2880 */
172 /* some PC formats :-) */
173 { 8, TYPE_ED }, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
174 { 5, TYPE_HD }, /* 10: h1440 = H1440 */
175 { 9, TYPE_HD }, /* 11: H1680 */
176 { 10, TYPE_DD }, /* 12: h410 */
177 { 3, TYPE_DD }, /* 13: H820 <- == D820, 82x10 */
178 { 11, TYPE_HD }, /* 14: h1476 */
179 { 12, TYPE_HD }, /* 15: H1722 */
180 { 13, TYPE_DD }, /* 16: h420 */
181 { 14, TYPE_DD }, /* 17: H830 */
182 { 15, TYPE_HD }, /* 18: h1494 */
183 { 16, TYPE_HD }, /* 19: H1743 */
184 { 17, TYPE_DD }, /* 20: h880 */
185 { 18, TYPE_DD }, /* 21: D1040 */
186 { 19, TYPE_DD }, /* 22: D1120 */
187 { 20, TYPE_HD }, /* 23: h1600 */
188 { 21, TYPE_HD }, /* 24: H1760 */
189 { 22, TYPE_HD }, /* 25: H1920 */
190 { 23, TYPE_ED }, /* 26: E3200 */
191 { 24, TYPE_ED }, /* 27: E3520 */
192 { 25, TYPE_ED }, /* 28: E3840 */
193 { 26, TYPE_HD }, /* 29: H1840 */
194 { 27, TYPE_DD }, /* 30: D800 */
195 { 6, TYPE_HD }, /* 31: H1640 <- was H1600 == h1600 for PC */
198 #define NUM_DISK_MINORS (sizeof(minor2disktype)/sizeof(*minor2disktype))
201 * Maximum disk size (in kilobytes). This default is used whenever the
202 * current disk size is unknown.
204 #define MAX_DISK_SIZE 3280
207 * MSch: User-provided type information. 'drive' points to
208 * the respective entry of this array. Set by FDSETPRM ioctls.
210 static struct atari_disk_type user_params[FD_MAX_UNITS];
213 * User-provided permanent type information. 'drive' points to
214 * the respective entry of this array. Set by FDDEFPRM ioctls,
215 * restored upon disk change by floppy_revalidate() if valid (as seen by
216 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
218 static struct atari_disk_type default_params[FD_MAX_UNITS];
220 static int floppy_sizes[256];
221 static int floppy_blocksizes[256];
223 /* current info on each unit */
224 static struct atari_floppy_struct {
225 int connected; /* !=0 : drive is connected */
226 int autoprobe; /* !=0 : do autoprobe */
228 struct atari_disk_type *disktype; /* current type of disk */
230 int track; /* current head position or -1 if
231 unknown */
232 unsigned int steprate; /* steprate setting */
233 unsigned int wpstat; /* current state of WP signal (for
234 disk change detection) */
235 int flags; /* flags */
236 } unit[FD_MAX_UNITS];
238 #define UD unit[drive]
239 #define UDT unit[drive].disktype
240 #define SUD unit[SelectedDrive]
241 #define SUDT unit[SelectedDrive].disktype
244 #define FDC_READ(reg) ({ \
245 /* unsigned long __flags; */ \
246 unsigned short __val; \
247 /* save_flags(__flags); cli(); */ \
248 dma_wd.dma_mode_status = 0x80 | (reg); \
249 udelay(25); \
250 __val = dma_wd.fdc_acces_seccount; \
251 MFPDELAY(); \
252 /* restore_flags(__flags); */ \
253 __val & 0xff; \
256 #define FDC_WRITE(reg,val) \
257 do { \
258 /* unsigned long __flags; */ \
259 /* save_flags(__flags); cli(); */ \
260 dma_wd.dma_mode_status = 0x80 | (reg); \
261 udelay(25); \
262 dma_wd.fdc_acces_seccount = (val); \
263 MFPDELAY(); \
264 /* restore_flags(__flags); */ \
265 } while(0)
268 /* Buffering variables:
269 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
270 * operations. Second, a track buffer is used to cache a whole track
271 * of the disk to save read operations. These are two separate buffers
272 * because that allows write operations without clearing the track buffer.
275 static int MaxSectors[] = {
276 11, 22, 44
278 static int BufferSize[] = {
279 15*512, 30*512, 60*512
282 #define BUFFER_SIZE (BufferSize[DriveType])
284 unsigned char *DMABuffer; /* buffer for writes */
285 static unsigned long PhysDMABuffer; /* physical address */
287 static int UseTrackbuffer = -1; /* Do track buffering? */
288 MODULE_PARM(UseTrackbuffer, "i");
290 unsigned char *TrackBuffer; /* buffer for reads */
291 static unsigned long PhysTrackBuffer; /* physical address */
292 static int BufferDrive, BufferSide, BufferTrack;
293 static int read_track; /* non-zero if we are reading whole tracks */
295 #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
296 #define IS_BUFFERED(drive,side,track) \
297 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
300 * These are global variables, as that's the easiest way to give
301 * information to interrupts. They are the data used for the current
302 * request.
304 static int SelectedDrive = 0;
305 static int ReqCmd, ReqBlock;
306 static int ReqSide, ReqTrack, ReqSector, ReqCnt;
307 static int HeadSettleFlag = 0;
308 static unsigned char *ReqData, *ReqBuffer;
309 static int MotorOn = 0, MotorOffTrys;
310 static int IsFormatting = 0, FormatError;
312 static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
313 MODULE_PARM(UserSteprate, "1-" __MODULE_STRING(FD_MAX_UNITS) "i");
315 /* Synchronization of FDC access. */
316 static volatile int fdc_busy = 0;
317 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
318 static DECLARE_WAIT_QUEUE_HEAD(format_wait);
320 static unsigned int changed_floppies = 0xff, fake_change = 0;
321 #define CHECK_CHANGE_DELAY HZ/2
323 #define FD_MOTOR_OFF_DELAY (3*HZ)
324 #define FD_MOTOR_OFF_MAXTRY (10*20)
326 #define FLOPPY_TIMEOUT (6*HZ)
327 #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
328 * will be recalibrated. */
329 #define MAX_ERRORS 8 /* After this many errors the driver
330 * will give up. */
334 * The driver is trying to determine the correct media format
335 * while Probing is set. fd_rwsec_done() clears it after a
336 * successful access.
338 static int Probing = 0;
340 /* This flag is set when a dummy seek is necessary to make the WP
341 * status bit accessible.
343 static int NeedSeek = 0;
346 #ifdef DEBUG
347 #define DPRINT(a) printk a
348 #else
349 #define DPRINT(a)
350 #endif
352 /***************************** Prototypes *****************************/
354 static void fd_select_side( int side );
355 static void fd_select_drive( int drive );
356 static void fd_deselect( void );
357 static void fd_motor_off_timer( unsigned long dummy );
358 static void check_change( void );
359 static __inline__ void set_head_settle_flag( void );
360 static __inline__ int get_head_settle_flag( void );
361 static void floppy_irq (int irq, void *dummy, struct pt_regs *fp);
362 static void fd_error( void );
363 static int do_format(kdev_t drive, struct atari_format_descr *desc);
364 static void do_fd_action( int drive );
365 static void fd_calibrate( void );
366 static void fd_calibrate_done( int status );
367 static void fd_seek( void );
368 static void fd_seek_done( int status );
369 static void fd_rwsec( void );
370 static void fd_readtrack_check( unsigned long dummy );
371 static void fd_rwsec_done( int status );
372 static void fd_rwsec_done1(int status);
373 static void fd_writetrack( void );
374 static void fd_writetrack_done( int status );
375 static void fd_times_out( unsigned long dummy );
376 static void finish_fdc( void );
377 static void finish_fdc_done( int dummy );
378 static void floppy_off( unsigned int nr);
379 static __inline__ void copy_buffer( void *from, void *to);
380 static void setup_req_params( int drive );
381 static void redo_fd_request( void);
382 static int invalidate_drive(kdev_t rdev);
383 static int fd_ioctl( struct inode *inode, struct file *filp, unsigned int
384 cmd, unsigned long param);
385 static void fd_probe( int drive );
386 static int fd_test_drive_present( int drive );
387 static void config_types( void );
388 static int floppy_open( struct inode *inode, struct file *filp );
389 static int floppy_release( struct inode * inode, struct file * filp );
391 /************************* End of Prototypes **************************/
393 static struct timer_list motor_off_timer =
394 { NULL, NULL, 0, 0, fd_motor_off_timer };
395 static struct timer_list readtrack_timer =
396 { NULL, NULL, 0, 0, fd_readtrack_check };
398 static struct timer_list timeout_timer =
399 { NULL, NULL, 0, 0, fd_times_out };
402 static inline void
403 start_motor_off_timer(void)
405 mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
406 MotorOffTrys = 0;
409 static inline void
410 start_check_change_timer(void)
412 timer_table[FLOPPY_TIMER].expires = jiffies + CHECK_CHANGE_DELAY;
413 timer_active |= (1 << FLOPPY_TIMER);
416 static inline void
417 start_timeout(void)
419 mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
422 static inline void
423 stop_timeout(void)
425 del_timer(&timeout_timer);
428 /* Select the side to use. */
430 static void fd_select_side( int side )
432 unsigned long flags;
434 save_flags(flags);
435 cli(); /* protect against various other ints mucking around with the PSG */
437 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
438 sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
439 sound_ym.rd_data_reg_sel & 0xfe;
441 restore_flags(flags);
445 /* Select a drive, update the FDC's track register and set the correct
446 * clock speed for this disk's type.
449 static void fd_select_drive( int drive )
451 unsigned long flags;
452 unsigned char tmp;
454 if (drive == SelectedDrive)
455 return;
457 save_flags(flags);
458 cli(); /* protect against various other ints mucking around with the PSG */
459 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
460 tmp = sound_ym.rd_data_reg_sel;
461 sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
462 atari_dont_touch_floppy_select = 1;
463 restore_flags(flags);
465 /* restore track register to saved value */
466 FDC_WRITE( FDCREG_TRACK, UD.track );
467 udelay(25);
469 /* select 8/16 MHz */
470 if (UDT)
471 if (ATARIHW_PRESENT(FDCSPEED))
472 dma_wd.fdc_speed = UDT->fdc_speed;
474 SelectedDrive = drive;
478 /* Deselect both drives. */
480 static void fd_deselect( void )
482 unsigned long flags;
484 save_flags(flags);
485 cli(); /* protect against various other ints mucking around with the PSG */
486 atari_dont_touch_floppy_select = 0;
487 sound_ym.rd_data_reg_sel=14; /* Select PSG Port A */
488 sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
489 (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
490 /* On Falcon, the drive B select line is used on the printer port, so
491 * leave it alone... */
492 SelectedDrive = -1;
493 restore_flags(flags);
497 /* This timer function deselects the drives when the FDC switched the
498 * motor off. The deselection cannot happen earlier because the FDC
499 * counts the index signals, which arrive only if one drive is selected.
502 static void fd_motor_off_timer( unsigned long dummy )
504 unsigned char status;
506 if (SelectedDrive < 0)
507 /* no drive selected, needn't deselect anyone */
508 return;
510 if (stdma_islocked())
511 goto retry;
513 status = FDC_READ( FDCREG_STATUS );
515 if (!(status & 0x80)) {
516 /* motor already turned off by FDC -> deselect drives */
517 MotorOn = 0;
518 fd_deselect();
519 return;
521 /* not yet off, try again */
523 retry:
524 /* Test again later; if tested too often, it seems there is no disk
525 * in the drive and the FDC will leave the motor on forever (or,
526 * at least until a disk is inserted). So we'll test only twice
527 * per second from then on...
529 mod_timer(&motor_off_timer,
530 jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
534 /* This function is repeatedly called to detect disk changes (as good
535 * as possible) and keep track of the current state of the write protection.
538 static void check_change( void )
540 static int drive = 0;
542 unsigned long flags;
543 unsigned char old_porta;
544 int stat;
546 if (++drive > 1 || !UD.connected)
547 drive = 0;
549 save_flags(flags);
550 cli(); /* protect against various other ints mucking around with the PSG */
552 if (!stdma_islocked()) {
553 sound_ym.rd_data_reg_sel = 14;
554 old_porta = sound_ym.rd_data_reg_sel;
555 sound_ym.wd_data = (old_porta | DSKDRVNONE) &
556 ~(drive == 0 ? DSKDRV0 : DSKDRV1);
557 stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
558 sound_ym.wd_data = old_porta;
560 if (stat != UD.wpstat) {
561 DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
562 UD.wpstat = stat;
563 set_bit (drive, &changed_floppies);
566 restore_flags(flags);
568 start_check_change_timer();
572 /* Handling of the Head Settling Flag: This flag should be set after each
573 * seek operation, because we don't use seeks with verify.
576 static __inline__ void set_head_settle_flag( void )
578 HeadSettleFlag = FDCCMDADD_E;
581 static __inline__ int get_head_settle_flag( void )
583 int tmp = HeadSettleFlag;
584 HeadSettleFlag = 0;
585 return( tmp );
591 /* General Interrupt Handling */
593 static void (*FloppyIRQHandler)( int status ) = NULL;
595 static void floppy_irq (int irq, void *dummy, struct pt_regs *fp)
597 unsigned char status;
598 void (*handler)( int );
600 handler = xchg(&FloppyIRQHandler, NULL);
602 if (handler) {
603 nop();
604 status = FDC_READ( FDCREG_STATUS );
605 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
606 handler( status );
608 else {
609 DPRINT(("FDC irq, no handler\n"));
614 /* Error handling: If some error happened, retry some times, then
615 * recalibrate, then try again, and fail after MAX_ERRORS.
618 static void fd_error( void )
620 if (IsFormatting) {
621 IsFormatting = 0;
622 FormatError = 1;
623 wake_up( &format_wait );
624 return;
627 if (!CURRENT) return;
628 CURRENT->errors++;
629 if (CURRENT->errors >= MAX_ERRORS) {
630 printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
631 end_request( 0 );
633 else if (CURRENT->errors == RECALIBRATE_ERRORS) {
634 printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
635 if (SelectedDrive != -1)
636 SUD.track = -1;
638 redo_fd_request();
643 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
646 /* ---------- Formatting ---------- */
648 #define FILL(n,val) \
649 do { \
650 memset( p, val, n ); \
651 p += n; \
652 } while(0)
654 static int do_format(kdev_t device, struct atari_format_descr *desc)
656 unsigned char *p;
657 int sect, nsect;
658 unsigned long flags;
659 int type, drive = MINOR(device) & 3;
661 DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
662 drive, desc->track, desc->head, desc->sect_offset ));
664 save_flags(flags);
665 cli();
666 while( fdc_busy ) sleep_on( &fdc_wait );
667 fdc_busy = 1;
668 stdma_lock(floppy_irq, NULL);
669 atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
670 restore_flags(flags);
672 type = MINOR(device) >> 2;
673 if (type) {
674 if (--type >= NUM_DISK_MINORS ||
675 minor2disktype[type].drive_types > DriveType) {
676 redo_fd_request();
677 return -EINVAL;
679 type = minor2disktype[type].index;
680 UDT = &disk_type[type];
683 if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
684 redo_fd_request();
685 return -EINVAL;
688 nsect = UDT->spt;
689 p = TrackBuffer;
690 /* The track buffer is used for the raw track data, so its
691 contents become invalid! */
692 BufferDrive = -1;
693 /* stop deselect timer */
694 del_timer( &motor_off_timer );
696 FILL( 60 * (nsect / 9), 0x4e );
697 for( sect = 0; sect < nsect; ++sect ) {
698 FILL( 12, 0 );
699 FILL( 3, 0xf5 );
700 *p++ = 0xfe;
701 *p++ = desc->track;
702 *p++ = desc->head;
703 *p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
704 *p++ = 2;
705 *p++ = 0xf7;
706 FILL( 22, 0x4e );
707 FILL( 12, 0 );
708 FILL( 3, 0xf5 );
709 *p++ = 0xfb;
710 FILL( 512, 0xe5 );
711 *p++ = 0xf7;
712 FILL( 40, 0x4e );
714 FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
716 IsFormatting = 1;
717 FormatError = 0;
718 ReqTrack = desc->track;
719 ReqSide = desc->head;
720 do_fd_action( drive );
722 sleep_on( &format_wait );
724 redo_fd_request();
725 return( FormatError ? -EIO : 0 );
729 /* do_fd_action() is the general procedure for a fd request: All
730 * required parameter settings (drive select, side select, track
731 * position) are checked and set if needed. For each of these
732 * parameters and the actual reading or writing exist two functions:
733 * one that starts the setting (or skips it if possible) and one
734 * callback for the "done" interrupt. Each done func calls the next
735 * set function to propagate the request down to fd_rwsec_done().
738 static void do_fd_action( int drive )
740 DPRINT(("do_fd_action\n"));
742 if (UseTrackbuffer && !IsFormatting) {
743 repeat:
744 if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
745 if (ReqCmd == READ) {
746 copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
747 if (++ReqCnt < CURRENT->current_nr_sectors) {
748 /* read next sector */
749 setup_req_params( drive );
750 goto repeat;
752 else {
753 /* all sectors finished */
754 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
755 CURRENT->sector += CURRENT->current_nr_sectors;
756 end_request( 1 );
757 redo_fd_request();
758 return;
761 else {
762 /* cmd == WRITE, pay attention to track buffer
763 * consistency! */
764 copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
769 if (SelectedDrive != drive)
770 fd_select_drive( drive );
772 if (UD.track == -1)
773 fd_calibrate();
774 else if (UD.track != ReqTrack << UDT->stretch)
775 fd_seek();
776 else if (IsFormatting)
777 fd_writetrack();
778 else
779 fd_rwsec();
783 /* Seek to track 0 if the current track is unknown */
785 static void fd_calibrate( void )
787 if (SUD.track >= 0) {
788 fd_calibrate_done( 0 );
789 return;
792 if (ATARIHW_PRESENT(FDCSPEED))
793 dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
794 DPRINT(("fd_calibrate\n"));
795 SET_IRQ_HANDLER( fd_calibrate_done );
796 /* we can't verify, since the speed may be incorrect */
797 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
799 NeedSeek = 1;
800 MotorOn = 1;
801 start_timeout();
802 /* wait for IRQ */
806 static void fd_calibrate_done( int status )
808 DPRINT(("fd_calibrate_done()\n"));
809 stop_timeout();
811 /* set the correct speed now */
812 if (ATARIHW_PRESENT(FDCSPEED))
813 dma_wd.fdc_speed = SUDT->fdc_speed;
814 if (status & FDCSTAT_RECNF) {
815 printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
816 fd_error();
818 else {
819 SUD.track = 0;
820 fd_seek();
825 /* Seek the drive to the requested track. The drive must have been
826 * calibrated at some point before this.
829 static void fd_seek( void )
831 if (SUD.track == ReqTrack << SUDT->stretch) {
832 fd_seek_done( 0 );
833 return;
836 if (ATARIHW_PRESENT(FDCSPEED)) {
837 dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
838 MFPDELAY();
841 DPRINT(("fd_seek() to track %d\n",ReqTrack));
842 FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
843 udelay(25);
844 SET_IRQ_HANDLER( fd_seek_done );
845 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
847 MotorOn = 1;
848 set_head_settle_flag();
849 start_timeout();
850 /* wait for IRQ */
854 static void fd_seek_done( int status )
856 DPRINT(("fd_seek_done()\n"));
857 stop_timeout();
859 /* set the correct speed */
860 if (ATARIHW_PRESENT(FDCSPEED))
861 dma_wd.fdc_speed = SUDT->fdc_speed;
862 if (status & FDCSTAT_RECNF) {
863 printk(KERN_ERR "fd%d: seek error (to track %d)\n",
864 SelectedDrive, ReqTrack );
865 /* we don't know exactly which track we are on now! */
866 SUD.track = -1;
867 fd_error();
869 else {
870 SUD.track = ReqTrack << SUDT->stretch;
871 NeedSeek = 0;
872 if (IsFormatting)
873 fd_writetrack();
874 else
875 fd_rwsec();
880 /* This does the actual reading/writing after positioning the head
881 * over the correct track.
884 static int MultReadInProgress = 0;
887 static void fd_rwsec( void )
889 unsigned long paddr, flags;
890 unsigned int rwflag, old_motoron;
891 unsigned int track;
893 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
894 if (ReqCmd == WRITE) {
895 if (ATARIHW_PRESENT(EXTD_DMA)) {
896 paddr = virt_to_phys(ReqData);
898 else {
899 copy_buffer( ReqData, DMABuffer );
900 paddr = PhysDMABuffer;
902 dma_cache_maintenance( paddr, 512, 1 );
903 rwflag = 0x100;
905 else {
906 if (read_track)
907 paddr = PhysTrackBuffer;
908 else
909 paddr = ATARIHW_PRESENT(EXTD_DMA) ?
910 virt_to_phys(ReqData) : PhysDMABuffer;
911 rwflag = 0;
914 fd_select_side( ReqSide );
916 /* Start sector of this operation */
917 FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
918 MFPDELAY();
919 /* Cheat for track if stretch != 0 */
920 if (SUDT->stretch) {
921 track = FDC_READ( FDCREG_TRACK);
922 MFPDELAY();
923 FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
925 udelay(25);
927 /* Setup DMA */
928 save_flags(flags);
929 cli();
930 dma_wd.dma_lo = (unsigned char)paddr;
931 MFPDELAY();
932 paddr >>= 8;
933 dma_wd.dma_md = (unsigned char)paddr;
934 MFPDELAY();
935 paddr >>= 8;
936 if (ATARIHW_PRESENT(EXTD_DMA))
937 st_dma_ext_dmahi = (unsigned short)paddr;
938 else
939 dma_wd.dma_hi = (unsigned char)paddr;
940 MFPDELAY();
941 restore_flags(flags);
943 /* Clear FIFO and switch DMA to correct mode */
944 dma_wd.dma_mode_status = 0x90 | rwflag;
945 MFPDELAY();
946 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
947 MFPDELAY();
948 dma_wd.dma_mode_status = 0x90 | rwflag;
949 MFPDELAY();
951 /* How many sectors for DMA */
952 dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
954 udelay(25);
956 /* Start operation */
957 dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
958 udelay(25);
959 SET_IRQ_HANDLER( fd_rwsec_done );
960 dma_wd.fdc_acces_seccount =
961 (get_head_settle_flag() |
962 (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
964 old_motoron = MotorOn;
965 MotorOn = 1;
966 NeedSeek = 1;
967 /* wait for interrupt */
969 if (read_track) {
970 /* If reading a whole track, wait about one disk rotation and
971 * then check if all sectors are read. The FDC will even
972 * search for the first non-existent sector and need 1 sec to
973 * recognise that it isn't present :-(
975 MultReadInProgress = 1;
976 mod_timer(&readtrack_timer,
977 /* 1 rot. + 5 rot.s if motor was off */
978 jiffies + HZ/5 + (old_motoron ? 0 : HZ));
980 start_timeout();
984 static void fd_readtrack_check( unsigned long dummy )
986 unsigned long flags, addr, addr2;
988 save_flags(flags);
989 cli();
991 if (!MultReadInProgress) {
992 /* This prevents a race condition that could arise if the
993 * interrupt is triggered while the calling of this timer
994 * callback function takes place. The IRQ function then has
995 * already cleared 'MultReadInProgress' when flow of control
996 * gets here.
998 restore_flags(flags);
999 return;
1002 /* get the current DMA address */
1003 /* ++ f.a. read twice to avoid being fooled by switcher */
1004 addr = 0;
1005 do {
1006 addr2 = addr;
1007 addr = dma_wd.dma_lo & 0xff;
1008 MFPDELAY();
1009 addr |= (dma_wd.dma_md & 0xff) << 8;
1010 MFPDELAY();
1011 if (ATARIHW_PRESENT( EXTD_DMA ))
1012 addr |= (st_dma_ext_dmahi & 0xffff) << 16;
1013 else
1014 addr |= (dma_wd.dma_hi & 0xff) << 16;
1015 MFPDELAY();
1016 } while(addr != addr2);
1018 if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1019 /* already read enough data, force an FDC interrupt to stop
1020 * the read operation
1022 SET_IRQ_HANDLER( NULL );
1023 MultReadInProgress = 0;
1024 restore_flags(flags);
1025 DPRINT(("fd_readtrack_check(): done\n"));
1026 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1027 udelay(25);
1029 /* No error until now -- the FDC would have interrupted
1030 * otherwise!
1032 fd_rwsec_done1(0);
1034 else {
1035 /* not yet finished, wait another tenth rotation */
1036 restore_flags(flags);
1037 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1038 mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1043 static void fd_rwsec_done( int status )
1045 DPRINT(("fd_rwsec_done()\n"));
1047 if (read_track) {
1048 del_timer(&readtrack_timer);
1049 if (!MultReadInProgress)
1050 return;
1051 MultReadInProgress = 0;
1053 fd_rwsec_done1(status);
1056 static void fd_rwsec_done1(int status)
1058 unsigned int track;
1060 stop_timeout();
1062 /* Correct the track if stretch != 0 */
1063 if (SUDT->stretch) {
1064 track = FDC_READ( FDCREG_TRACK);
1065 MFPDELAY();
1066 FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1069 if (!UseTrackbuffer) {
1070 dma_wd.dma_mode_status = 0x90;
1071 MFPDELAY();
1072 if (!(dma_wd.dma_mode_status & 0x01)) {
1073 printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1074 goto err_end;
1077 MFPDELAY();
1079 if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1080 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1081 goto err_end;
1083 if ((status & FDCSTAT_RECNF) &&
1084 /* RECNF is no error after a multiple read when the FDC
1085 searched for a non-existent sector! */
1086 !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1087 if (Probing) {
1088 if (SUDT > disk_type) {
1089 if (SUDT[-1].blocks > ReqBlock) {
1090 /* try another disk type */
1091 SUDT--;
1092 floppy_sizes[SelectedDrive] = SUDT->blocks >> 1;
1093 } else
1094 Probing = 0;
1096 else {
1097 if (SUD.flags & FTD_MSG)
1098 printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1099 SelectedDrive, SUDT->name );
1100 Probing=0;
1102 } else {
1103 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1104 if (SUD.autoprobe) {
1105 SUDT = disk_type + StartDiskType[DriveType];
1106 floppy_sizes[SelectedDrive] = SUDT->blocks >> 1;
1107 Probing = 1;
1110 if (Probing) {
1111 if (ATARIHW_PRESENT(FDCSPEED)) {
1112 dma_wd.fdc_speed = SUDT->fdc_speed;
1113 MFPDELAY();
1115 setup_req_params( SelectedDrive );
1116 BufferDrive = -1;
1117 do_fd_action( SelectedDrive );
1118 return;
1121 printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1122 SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1123 goto err_end;
1125 if (status & FDCSTAT_CRC) {
1126 printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1127 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1128 goto err_end;
1130 if (status & FDCSTAT_LOST) {
1131 printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1132 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1133 goto err_end;
1136 Probing = 0;
1138 if (ReqCmd == READ) {
1139 if (!read_track) {
1140 void *addr;
1141 addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1142 dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1143 if (!ATARIHW_PRESENT( EXTD_DMA ))
1144 copy_buffer (addr, ReqData);
1145 } else {
1146 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1147 BufferDrive = SelectedDrive;
1148 BufferSide = ReqSide;
1149 BufferTrack = ReqTrack;
1150 copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1154 if (++ReqCnt < CURRENT->current_nr_sectors) {
1155 /* read next sector */
1156 setup_req_params( SelectedDrive );
1157 do_fd_action( SelectedDrive );
1159 else {
1160 /* all sectors finished */
1161 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
1162 CURRENT->sector += CURRENT->current_nr_sectors;
1163 end_request( 1 );
1164 redo_fd_request();
1166 return;
1168 err_end:
1169 BufferDrive = -1;
1170 fd_error();
1174 static void fd_writetrack( void )
1176 unsigned long paddr, flags;
1177 unsigned int track;
1179 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1181 paddr = PhysTrackBuffer;
1182 dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1184 fd_select_side( ReqSide );
1186 /* Cheat for track if stretch != 0 */
1187 if (SUDT->stretch) {
1188 track = FDC_READ( FDCREG_TRACK);
1189 MFPDELAY();
1190 FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1192 udelay(40);
1194 /* Setup DMA */
1195 save_flags(flags);
1196 cli();
1197 dma_wd.dma_lo = (unsigned char)paddr;
1198 MFPDELAY();
1199 paddr >>= 8;
1200 dma_wd.dma_md = (unsigned char)paddr;
1201 MFPDELAY();
1202 paddr >>= 8;
1203 if (ATARIHW_PRESENT( EXTD_DMA ))
1204 st_dma_ext_dmahi = (unsigned short)paddr;
1205 else
1206 dma_wd.dma_hi = (unsigned char)paddr;
1207 MFPDELAY();
1208 restore_flags(flags);
1210 /* Clear FIFO and switch DMA to correct mode */
1211 dma_wd.dma_mode_status = 0x190;
1212 MFPDELAY();
1213 dma_wd.dma_mode_status = 0x90;
1214 MFPDELAY();
1215 dma_wd.dma_mode_status = 0x190;
1216 MFPDELAY();
1218 /* How many sectors for DMA */
1219 dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1220 udelay(40);
1222 /* Start operation */
1223 dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1224 udelay(40);
1225 SET_IRQ_HANDLER( fd_writetrack_done );
1226 dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1228 MotorOn = 1;
1229 start_timeout();
1230 /* wait for interrupt */
1234 static void fd_writetrack_done( int status )
1236 DPRINT(("fd_writetrack_done()\n"));
1238 stop_timeout();
1240 if (status & FDCSTAT_WPROT) {
1241 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1242 goto err_end;
1244 if (status & FDCSTAT_LOST) {
1245 printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1246 SelectedDrive, ReqSide, ReqTrack );
1247 goto err_end;
1250 wake_up( &format_wait );
1251 return;
1253 err_end:
1254 fd_error();
1257 static void fd_times_out( unsigned long dummy )
1259 atari_disable_irq( IRQ_MFP_FDC );
1260 if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1261 * before we came here... */
1263 SET_IRQ_HANDLER( NULL );
1264 /* If the timeout occurred while the readtrack_check timer was
1265 * active, we need to cancel it, else bad things will happen */
1266 if (UseTrackbuffer)
1267 del_timer( &readtrack_timer );
1268 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1269 udelay( 25 );
1271 printk(KERN_ERR "floppy timeout\n" );
1272 fd_error();
1273 end:
1274 atari_enable_irq( IRQ_MFP_FDC );
1278 /* The (noop) seek operation here is needed to make the WP bit in the
1279 * FDC status register accessible for check_change. If the last disk
1280 * operation would have been a RDSEC, this bit would always read as 0
1281 * no matter what :-( To save time, the seek goes to the track we're
1282 * already on.
1285 static void finish_fdc( void )
1287 if (!NeedSeek) {
1288 finish_fdc_done( 0 );
1290 else {
1291 DPRINT(("finish_fdc: dummy seek started\n"));
1292 FDC_WRITE (FDCREG_DATA, SUD.track);
1293 SET_IRQ_HANDLER( finish_fdc_done );
1294 FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1295 MotorOn = 1;
1296 start_timeout();
1297 /* we must wait for the IRQ here, because the ST-DMA
1298 is released immediately afterwards and the interrupt
1299 may be delivered to the wrong driver. */
1304 static void finish_fdc_done( int dummy )
1306 unsigned long flags;
1308 DPRINT(("finish_fdc_done entered\n"));
1309 stop_timeout();
1310 NeedSeek = 0;
1312 if ((timer_active & (1 << FLOPPY_TIMER)) &&
1313 time_before(timer_table[FLOPPY_TIMER].expires, jiffies + 5))
1314 /* If the check for a disk change is done too early after this
1315 * last seek command, the WP bit still reads wrong :-((
1317 timer_table[FLOPPY_TIMER].expires = jiffies + 5;
1318 else
1319 start_check_change_timer();
1320 start_motor_off_timer();
1322 save_flags(flags);
1323 cli();
1324 stdma_release();
1325 fdc_busy = 0;
1326 wake_up( &fdc_wait );
1327 restore_flags(flags);
1329 DPRINT(("finish_fdc() finished\n"));
1333 /* Prevent "aliased" accesses. */
1334 static int fd_ref[4] = { 0,0,0,0 };
1335 static int fd_device[4] = { 0,0,0,0 };
1338 * Current device number. Taken either from the block header or from the
1339 * format request descriptor.
1341 #define CURRENT_DEVICE (CURRENT->rq_dev)
1343 /* Current error count. */
1344 #define CURRENT_ERRORS (CURRENT->errors)
1347 /* dummy for blk.h */
1348 static void floppy_off( unsigned int nr) {}
1351 /* The detection of disk changes is a dark chapter in Atari history :-(
1352 * Because the "Drive ready" signal isn't present in the Atari
1353 * hardware, one has to rely on the "Write Protect". This works fine,
1354 * as long as no write protected disks are used. TOS solves this
1355 * problem by introducing tri-state logic ("maybe changed") and
1356 * looking at the serial number in block 0. This isn't possible for
1357 * Linux, since the floppy driver can't make assumptions about the
1358 * filesystem used on the disk and thus the contents of block 0. I've
1359 * chosen the method to always say "The disk was changed" if it is
1360 * unsure whether it was. This implies that every open or mount
1361 * invalidates the disk buffers if you work with write protected
1362 * disks. But at least this is better than working with incorrect data
1363 * due to unrecognised disk changes.
1366 static int check_floppy_change (kdev_t dev)
1368 unsigned int drive = MINOR(dev) & 0x03;
1370 if (MAJOR(dev) != MAJOR_NR) {
1371 printk(KERN_ERR "floppy_changed: not a floppy\n");
1372 return 0;
1375 if (test_bit (drive, &fake_change)) {
1376 /* simulated change (e.g. after formatting) */
1377 return 1;
1379 if (test_bit (drive, &changed_floppies)) {
1380 /* surely changed (the WP signal changed at least once) */
1381 return 1;
1383 if (UD.wpstat) {
1384 /* WP is on -> could be changed: to be sure, buffers should be
1385 * invalidated...
1387 return 1;
1390 return 0;
1393 static int floppy_revalidate (kdev_t dev)
1395 int drive = MINOR(dev) & 3;
1397 if (test_bit(drive, &changed_floppies) ||
1398 test_bit(drive, &fake_change) ||
1399 unit[drive].disktype == 0) {
1400 if (UD.flags & FTD_MSG)
1401 printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1402 BufferDrive = -1;
1403 clear_bit(drive, &fake_change);
1404 clear_bit(drive, &changed_floppies);
1405 /* MSch: clearing geometry makes sense only for autoprobe
1406 formats, for 'permanent user-defined' parameter:
1407 restore default_params[] here if flagged valid! */
1408 if (default_params[drive].blocks == 0)
1409 UDT = 0;
1410 else
1411 UDT = &default_params[drive];
1413 return 0;
1416 static __inline__ void copy_buffer(void *from, void *to)
1418 ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
1419 int cnt;
1421 for( cnt = 512/4; cnt; cnt-- )
1422 *p2++ = *p1++;
1426 /* This sets up the global variables describing the current request. */
1428 static void setup_req_params( int drive )
1430 int block = ReqBlock + ReqCnt;
1432 ReqTrack = block / UDT->spt;
1433 ReqSector = block - ReqTrack * UDT->spt + 1;
1434 ReqSide = ReqTrack & 1;
1435 ReqTrack >>= 1;
1436 ReqData = ReqBuffer + 512 * ReqCnt;
1438 if (UseTrackbuffer)
1439 read_track = (ReqCmd == READ && CURRENT_ERRORS == 0);
1440 else
1441 read_track = 0;
1443 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1444 ReqTrack, ReqSector, (unsigned long)ReqData ));
1448 static void redo_fd_request(void)
1450 int device, drive, type;
1452 DPRINT(("redo_fd_request: CURRENT=%08lx CURRENT->dev=%04x CURRENT->sector=%ld\n",
1453 (unsigned long)CURRENT, CURRENT ? CURRENT->rq_dev : 0,
1454 CURRENT ? CURRENT->sector : 0 ));
1456 IsFormatting = 0;
1458 if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
1459 return;
1462 repeat:
1464 if (!CURRENT)
1465 goto the_end;
1467 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1468 panic(DEVICE_NAME ": request list destroyed");
1470 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
1471 panic(DEVICE_NAME ": block not locked");
1473 device = MINOR(CURRENT_DEVICE);
1474 drive = device & 3;
1475 type = device >> 2;
1477 if (!UD.connected) {
1478 /* drive not connected */
1479 printk(KERN_ERR "Unknown Device: fd%d\n", drive );
1480 end_request(0);
1481 goto repeat;
1484 if (type == 0) {
1485 if (!UDT) {
1486 Probing = 1;
1487 UDT = disk_type + StartDiskType[DriveType];
1488 floppy_sizes[drive] = UDT->blocks >> 1;
1489 UD.autoprobe = 1;
1492 else {
1493 /* user supplied disk type */
1494 if (--type >= NUM_DISK_MINORS) {
1495 printk(KERN_WARNING "fd%d: invalid disk format", drive );
1496 end_request( 0 );
1497 goto repeat;
1499 if (minor2disktype[type].drive_types > DriveType) {
1500 printk(KERN_WARNING "fd%d: unsupported disk format", drive );
1501 end_request( 0 );
1502 goto repeat;
1504 type = minor2disktype[type].index;
1505 UDT = &disk_type[type];
1506 floppy_sizes[drive] = UDT->blocks >> 1;
1507 UD.autoprobe = 0;
1510 if (CURRENT->sector + 1 > UDT->blocks) {
1511 end_request(0);
1512 goto repeat;
1515 /* stop deselect timer */
1516 del_timer( &motor_off_timer );
1518 ReqCnt = 0;
1519 ReqCmd = CURRENT->cmd;
1520 ReqBlock = CURRENT->sector;
1521 ReqBuffer = CURRENT->buffer;
1522 setup_req_params( drive );
1523 do_fd_action( drive );
1525 return;
1527 the_end:
1528 finish_fdc();
1532 void do_fd_request(void)
1534 unsigned long flags;
1536 DPRINT(("do_fd_request for pid %d\n",current->pid));
1537 while( fdc_busy ) sleep_on( &fdc_wait );
1538 fdc_busy = 1;
1539 stdma_lock(floppy_irq, NULL);
1541 atari_disable_irq( IRQ_MFP_FDC );
1542 save_flags(flags); /* The request function is called with ints
1543 sti(); * disabled... so must save the IPL for later */
1544 redo_fd_request();
1545 restore_flags(flags);
1546 atari_enable_irq( IRQ_MFP_FDC );
1550 static int invalidate_drive(kdev_t rdev)
1552 /* invalidate the buffer track to force a reread */
1553 BufferDrive = -1;
1554 set_bit(MINOR(rdev) & 3, &fake_change);
1555 check_disk_change(rdev);
1556 return 0;
1559 static int fd_ioctl(struct inode *inode, struct file *filp,
1560 unsigned int cmd, unsigned long param)
1562 #define IOCTL_MODE_BIT 8
1563 #define OPEN_WRITE_BIT 16
1564 #define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
1566 int drive, type;
1567 kdev_t device;
1568 struct atari_format_descr fmt_desc;
1569 struct atari_disk_type *dtp;
1570 struct floppy_struct getprm;
1571 int settype;
1572 struct floppy_struct setprm;
1574 device = inode->i_rdev;
1575 switch (cmd) {
1576 case BLKROSET:
1577 case BLKROGET:
1578 case BLKRASET:
1579 case BLKRAGET:
1580 case BLKFLSBUF:
1581 return blk_ioctl(device, cmd, param);
1583 drive = MINOR (device);
1584 type = drive >> 2;
1585 drive &= 3;
1586 switch (cmd) {
1587 case FDGETPRM:
1588 case BLKGETSIZE:
1589 if (type) {
1590 if (--type >= NUM_DISK_MINORS)
1591 return -ENODEV;
1592 if (minor2disktype[type].drive_types > DriveType)
1593 return -ENODEV;
1594 type = minor2disktype[type].index;
1595 dtp = &disk_type[type];
1596 if (UD.flags & FTD_MSG)
1597 printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1598 drive, dtp, dtp->name);
1600 else {
1601 if (!UDT)
1602 return -ENXIO;
1603 else
1604 dtp = UDT;
1606 if (cmd == BLKGETSIZE)
1607 return put_user(dtp->blocks, (long *)param);
1609 memset((void *)&getprm, 0, sizeof(getprm));
1610 getprm.size = dtp->blocks;
1611 getprm.sect = dtp->spt;
1612 getprm.head = 2;
1613 getprm.track = dtp->blocks/dtp->spt/2;
1614 getprm.stretch = dtp->stretch;
1615 if (copy_to_user((void *)param, &getprm, sizeof(getprm)))
1616 return -EFAULT;
1617 return 0;
1619 if (!IOCTL_ALLOWED)
1620 return -EPERM;
1621 switch (cmd) {
1622 case FDSETPRM:
1623 case FDDEFPRM:
1625 * MSch 7/96: simple 'set geometry' case: just set the
1626 * 'default' device params (minor == 0).
1627 * Currently, the drive geometry is cleared after each
1628 * disk change and subsequent revalidate()! simple
1629 * implementation of FDDEFPRM: save geometry from a
1630 * FDDEFPRM call and restore it in floppy_revalidate() !
1633 /* get the parameters from user space */
1634 if (fd_ref[drive] != 1 && fd_ref[drive] != -1)
1635 return -EBUSY;
1636 if (copy_from_user(&setprm, (void *) param, sizeof(setprm)))
1637 return -EFAULT;
1639 * first of all: check for floppy change and revalidate,
1640 * or the next access will revalidate - and clear UDT :-(
1643 if (check_floppy_change(device))
1644 floppy_revalidate(device);
1646 if (UD.flags & FTD_MSG)
1647 printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1648 drive, setprm.size, setprm.sect, setprm.stretch);
1650 /* what if type > 0 here? Overwrite specified entry ? */
1651 if (type) {
1652 /* refuse to re-set a predefined type for now */
1653 redo_fd_request();
1654 return -EINVAL;
1658 * type == 0: first look for a matching entry in the type list,
1659 * and set the UD.disktype field to use the perdefined entry.
1660 * TODO: add user-defined format to head of autoprobe list ?
1661 * Useful to include the user-type for future autodetection!
1664 for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1665 int setidx = 0;
1666 if (minor2disktype[settype].drive_types > DriveType) {
1667 /* skip this one, invalid for drive ... */
1668 continue;
1670 setidx = minor2disktype[settype].index;
1671 dtp = &disk_type[setidx];
1673 /* found matching entry ?? */
1674 if ( dtp->blocks == setprm.size
1675 && dtp->spt == setprm.sect
1676 && dtp->stretch == setprm.stretch ) {
1677 if (UD.flags & FTD_MSG)
1678 printk (KERN_INFO "floppy%d: setting %s %p!\n",
1679 drive, dtp->name, dtp);
1680 UDT = dtp;
1681 floppy_sizes[drive] = UDT->blocks >> 1;
1683 if (cmd == FDDEFPRM) {
1684 /* save settings as permanent default type */
1685 default_params[drive].name = dtp->name;
1686 default_params[drive].spt = dtp->spt;
1687 default_params[drive].blocks = dtp->blocks;
1688 default_params[drive].fdc_speed = dtp->fdc_speed;
1689 default_params[drive].stretch = dtp->stretch;
1692 return 0;
1697 /* no matching disk type found above - setting user_params */
1699 if (cmd == FDDEFPRM) {
1700 /* set permanent type */
1701 dtp = &default_params[drive];
1702 } else
1703 /* set user type (reset by disk change!) */
1704 dtp = &user_params[drive];
1706 dtp->name = "user format";
1707 dtp->blocks = setprm.size;
1708 dtp->spt = setprm.sect;
1709 if (setprm.sect > 14)
1710 dtp->fdc_speed = 3;
1711 else
1712 dtp->fdc_speed = 0;
1713 dtp->stretch = setprm.stretch;
1715 if (UD.flags & FTD_MSG)
1716 printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1717 drive, dtp->blocks, dtp->spt, dtp->stretch);
1719 /* sanity check */
1720 if (!dtp || setprm.track != dtp->blocks/dtp->spt/2 ||
1721 setprm.head != 2) {
1722 redo_fd_request();
1723 return -EINVAL;
1726 UDT = dtp;
1727 floppy_sizes[drive] = UDT->blocks >> 1;
1729 return 0;
1730 case FDMSGON:
1731 UD.flags |= FTD_MSG;
1732 return 0;
1733 case FDMSGOFF:
1734 UD.flags &= ~FTD_MSG;
1735 return 0;
1736 case FDSETEMSGTRESH:
1737 return -EINVAL;
1738 case FDFMTBEG:
1739 return 0;
1740 case FDFMTTRK:
1741 if (fd_ref[drive] != 1 && fd_ref[drive] != -1)
1742 return -EBUSY;
1743 if (copy_from_user(&fmt_desc, (void *) param, sizeof(fmt_desc)))
1744 return -EFAULT;
1745 return do_format(device, &fmt_desc);
1746 case FDCLRPRM:
1747 UDT = NULL;
1748 /* MSch: invalidate default_params */
1749 default_params[drive].blocks = 0;
1750 floppy_sizes[drive] = MAX_DISK_SIZE;
1751 return invalidate_drive (device);
1752 case FDFMTEND:
1753 case FDFLUSH:
1754 return invalidate_drive(device);
1756 return -EINVAL;
1760 /* Initialize the 'unit' variable for drive 'drive' */
1762 static void __init fd_probe( int drive )
1764 UD.connected = 0;
1765 UDT = NULL;
1767 if (!fd_test_drive_present( drive ))
1768 return;
1770 UD.connected = 1;
1771 UD.track = 0;
1772 switch( UserSteprate[drive] ) {
1773 case 2:
1774 UD.steprate = FDCSTEP_2;
1775 break;
1776 case 3:
1777 UD.steprate = FDCSTEP_3;
1778 break;
1779 case 6:
1780 UD.steprate = FDCSTEP_6;
1781 break;
1782 case 12:
1783 UD.steprate = FDCSTEP_12;
1784 break;
1785 default: /* should be -1 for "not set by user" */
1786 if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1787 UD.steprate = FDCSTEP_3;
1788 else
1789 UD.steprate = FDCSTEP_6;
1790 break;
1792 MotorOn = 1; /* from probe restore operation! */
1796 /* This function tests the physical presence of a floppy drive (not
1797 * whether a disk is inserted). This is done by issuing a restore
1798 * command, waiting max. 2 seconds (that should be enough to move the
1799 * head across the whole disk) and looking at the state of the "TR00"
1800 * signal. This should now be raised if there is a drive connected
1801 * (and there is no hardware failure :-) Otherwise, the drive is
1802 * declared absent.
1805 static int __init fd_test_drive_present( int drive )
1807 unsigned long timeout;
1808 unsigned char status;
1809 int ok;
1811 if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1812 fd_select_drive( drive );
1814 /* disable interrupt temporarily */
1815 atari_turnoff_irq( IRQ_MFP_FDC );
1816 FDC_WRITE (FDCREG_TRACK, 0xff00);
1817 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1819 timeout = jiffies + 2*HZ+HZ/2;
1820 while (time_before(jiffies, timeout))
1821 if (!(mfp.par_dt_reg & 0x20))
1822 break;
1824 status = FDC_READ( FDCREG_STATUS );
1825 ok = (status & FDCSTAT_TR00) != 0;
1827 /* force interrupt to abort restore operation (FDC would try
1828 * about 50 seconds!) */
1829 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1830 udelay(500);
1831 status = FDC_READ( FDCREG_STATUS );
1832 udelay(20);
1834 if (ok) {
1835 /* dummy seek command to make WP bit accessible */
1836 FDC_WRITE( FDCREG_DATA, 0 );
1837 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
1838 while( mfp.par_dt_reg & 0x20 )
1840 status = FDC_READ( FDCREG_STATUS );
1843 atari_turnon_irq( IRQ_MFP_FDC );
1844 return( ok );
1848 /* Look how many and which kind of drives are connected. If there are
1849 * floppies, additionally start the disk-change and motor-off timers.
1852 static void __init config_types( void )
1854 int drive, cnt = 0;
1856 /* for probing drives, set the FDC speed to 8 MHz */
1857 if (ATARIHW_PRESENT(FDCSPEED))
1858 dma_wd.fdc_speed = 0;
1860 printk(KERN_INFO "Probing floppy drive(s):\n");
1861 for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1862 fd_probe( drive );
1863 if (UD.connected) {
1864 printk(KERN_INFO "fd%d\n", drive);
1865 ++cnt;
1869 if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1870 /* If FDC is still busy from probing, give it another FORCI
1871 * command to abort the operation. If this isn't done, the FDC
1872 * will interrupt later and its IRQ line stays low, because
1873 * the status register isn't read. And this will block any
1874 * interrupts on this IRQ line :-(
1876 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1877 udelay(500);
1878 FDC_READ( FDCREG_STATUS );
1879 udelay(20);
1882 if (cnt > 0) {
1883 start_motor_off_timer();
1884 if (cnt == 1) fd_select_drive( 0 );
1885 start_check_change_timer();
1890 * floppy_open check for aliasing (/dev/fd0 can be the same as
1891 * /dev/PS0 etc), and disallows simultaneous access to the same
1892 * drive with different device numbers.
1895 static int floppy_open( struct inode *inode, struct file *filp )
1897 int drive, type;
1898 int old_dev;
1900 if (!filp) {
1901 DPRINT (("Weird, open called with filp=0\n"));
1902 return -EIO;
1905 drive = MINOR(inode->i_rdev) & 3;
1906 type = MINOR(inode->i_rdev) >> 2;
1907 DPRINT(("fd_open: type=%d\n",type));
1908 if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1909 return -ENXIO;
1911 old_dev = fd_device[drive];
1913 if (fd_ref[drive] && old_dev != MINOR(inode->i_rdev))
1914 return -EBUSY;
1916 if (fd_ref[drive] == -1 || (fd_ref[drive] && filp->f_flags & O_EXCL))
1917 return -EBUSY;
1919 MOD_INC_USE_COUNT;
1921 if (filp->f_flags & O_EXCL)
1922 fd_ref[drive] = -1;
1923 else
1924 fd_ref[drive]++;
1926 fd_device[drive] = MINOR(inode->i_rdev);
1928 if (old_dev && old_dev != MINOR(inode->i_rdev))
1929 invalidate_buffers(MKDEV(FLOPPY_MAJOR, old_dev));
1931 /* Allow ioctls if we have write-permissions even if read-only open */
1932 if (filp->f_mode & 2 || permission (inode, 2) == 0)
1933 filp->f_mode |= IOCTL_MODE_BIT;
1934 if (filp->f_mode & 2)
1935 filp->f_mode |= OPEN_WRITE_BIT;
1937 if (filp->f_flags & O_NDELAY)
1938 return 0;
1940 if (filp->f_mode & 3) {
1941 check_disk_change(inode->i_rdev);
1942 if (filp->f_mode & 2) {
1943 if (UD.wpstat) {
1944 floppy_release(inode, filp);
1945 return -EROFS;
1950 return 0;
1954 static int floppy_release( struct inode * inode, struct file * filp )
1956 int drive;
1958 drive = MINOR(inode->i_rdev) & 3;
1961 * If filp is NULL, we're being called from blkdev_release
1962 * or after a failed mount attempt. In the former case the
1963 * device has already been sync'ed, and in the latter no
1964 * sync is required. Otherwise, sync if filp is writable.
1966 if (filp && (filp->f_mode & (2 | OPEN_WRITE_BIT)))
1967 block_fsync (filp, filp->f_dentry);
1969 if (fd_ref[drive] < 0)
1970 fd_ref[drive] = 0;
1971 else if (!fd_ref[drive]--) {
1972 printk(KERN_ERR "floppy_release with fd_ref == 0");
1973 fd_ref[drive] = 0;
1976 MOD_DEC_USE_COUNT;
1977 return 0;
1980 static struct file_operations floppy_fops = {
1981 NULL, /* lseek - default */
1982 block_read, /* read - general block-dev read */
1983 block_write, /* write - general block-dev write */
1984 NULL, /* readdir - bad */
1985 NULL, /* select */
1986 fd_ioctl, /* ioctl */
1987 NULL, /* mmap */
1988 floppy_open, /* open */
1989 NULL, /* flush */
1990 floppy_release, /* release */
1991 block_fsync, /* fsync */
1992 NULL, /* fasync */
1993 check_floppy_change, /* media_change */
1994 floppy_revalidate, /* revalidate */
1997 int __init atari_floppy_init (void)
1999 int i;
2001 if (!MACH_IS_ATARI)
2002 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
2003 return -ENXIO;
2005 if (MACH_IS_HADES)
2006 /* Hades doesn't have Atari-compatible floppy */
2007 return -ENXIO;
2009 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
2010 printk(KERN_ERR "Unable to get major %d for floppy\n",MAJOR_NR);
2011 return -EBUSY;
2014 if (UseTrackbuffer < 0)
2015 /* not set by user -> use default: for now, we turn
2016 track buffering off for all Medusas, though it
2017 could be used with ones that have a counter
2018 card. But the test is too hard :-( */
2019 UseTrackbuffer = !MACH_IS_MEDUSA;
2021 /* initialize variables */
2022 SelectedDrive = -1;
2023 BufferDrive = -1;
2025 /* initialize check_change timer */
2026 timer_table[FLOPPY_TIMER].fn = check_change;
2027 timer_active &= ~(1 << FLOPPY_TIMER);
2029 DMABuffer = atari_stram_alloc( BUFFER_SIZE+512, NULL, "ataflop" );
2030 if (!DMABuffer) {
2031 printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
2032 unregister_blkdev(MAJOR_NR, "fd");
2033 return -ENOMEM;
2035 TrackBuffer = DMABuffer + 512;
2036 PhysDMABuffer = virt_to_phys(DMABuffer);
2037 PhysTrackBuffer = virt_to_phys(TrackBuffer);
2038 BufferDrive = BufferSide = BufferTrack = -1;
2040 for (i = 0; i < FD_MAX_UNITS; i++) {
2041 unit[i].track = -1;
2042 unit[i].flags = 0;
2045 for (i = 0; i < 256; i++)
2046 if ((i >> 2) > 0 && (i >> 2) <= NUM_DISK_MINORS) {
2047 int type = minor2disktype[(i >> 2) - 1].index;
2048 floppy_sizes[i] = disk_type[type].blocks >> 1;
2049 } else
2050 floppy_sizes[i] = MAX_DISK_SIZE;
2052 blk_size[MAJOR_NR] = floppy_sizes;
2053 blksize_size[MAJOR_NR] = floppy_blocksizes;
2054 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
2056 printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
2057 DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
2058 UseTrackbuffer ? "" : "no ");
2059 config_types();
2061 (void)do_floppy; /* avoid warning about unused variable */
2062 return 0;
2066 void __init atari_floppy_setup( char *str, int *ints )
2068 int i;
2070 if (ints[0] < 1) {
2071 printk(KERN_ERR "ataflop_setup: no arguments!\n" );
2072 return;
2074 else if (ints[0] > 2+FD_MAX_UNITS) {
2075 printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2078 if (ints[1] < 0 || ints[1] > 2)
2079 printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2080 else
2081 DriveType = ints[1];
2083 if (ints[0] >= 2)
2084 UseTrackbuffer = (ints[2] > 0);
2086 for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2087 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2088 printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2089 else
2090 UserSteprate[i-3] = ints[i];
2094 #ifdef MODULE
2095 int init_module (void)
2097 if (!MACH_IS_ATARI)
2098 return -ENXIO;
2099 return atari_floppy_init ();
2102 void cleanup_module (void)
2104 unregister_blkdev(MAJOR_NR, "fd");
2106 blk_dev[MAJOR_NR].request_fn = 0;
2107 timer_active &= ~(1 << FLOPPY_TIMER);
2108 timer_table[FLOPPY_TIMER].fn = 0;
2109 atari_stram_free( DMABuffer );
2111 #endif