Import 2.3.1pre2
[davej-history.git] / drivers / block / ataflop.c
blobf06aaccc2e2b413bf43979e83b158f97a540b35a
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>
97 #define FD_MAX_UNITS 2
99 #undef DEBUG
101 /* Disk types: DD, HD, ED */
102 static struct atari_disk_type {
103 const char *name;
104 unsigned spt; /* sectors per track */
105 unsigned blocks; /* total number of blocks */
106 unsigned fdc_speed; /* fdc_speed setting */
107 unsigned stretch; /* track doubling ? */
108 } disk_type[] = {
109 { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
110 { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
111 { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
112 { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
113 /* formats above are probed for type DD */
114 #define MAX_TYPE_DD 3
115 { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
116 { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
117 { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
118 /* formats above are probed for types DD and HD */
119 #define MAX_TYPE_HD 6
120 { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
121 { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
122 /* formats above are probed for types DD, HD and ED */
123 #define MAX_TYPE_ED 8
124 /* types below are never autoprobed */
125 { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
126 { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
127 { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
128 { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
129 { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
130 { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
131 { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
132 { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
133 { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
134 { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
135 { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
136 { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
137 { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
138 { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
139 { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
140 { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
141 { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
142 { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
143 { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
146 static int StartDiskType[] = {
147 MAX_TYPE_DD,
148 MAX_TYPE_HD,
149 MAX_TYPE_ED
152 #define TYPE_DD 0
153 #define TYPE_HD 1
154 #define TYPE_ED 2
156 static int DriveType = TYPE_HD;
158 /* Array for translating minors into disk formats */
159 static struct {
160 int index;
161 unsigned drive_types;
162 } minor2disktype[] = {
163 { 0, TYPE_DD }, /* 1: d360 */
164 { 4, TYPE_HD }, /* 2: h1200 */
165 { 1, TYPE_DD }, /* 3: D360 */
166 { 2, TYPE_DD }, /* 4: D720 */
167 { 1, TYPE_DD }, /* 5: h360 = D360 */
168 { 2, TYPE_DD }, /* 6: h720 = D720 */
169 { 5, TYPE_HD }, /* 7: H1440 */
170 { 7, TYPE_ED }, /* 8: E2880 */
171 /* some PC formats :-) */
172 { 8, TYPE_ED }, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
173 { 5, TYPE_HD }, /* 10: h1440 = H1440 */
174 { 9, TYPE_HD }, /* 11: H1680 */
175 { 10, TYPE_DD }, /* 12: h410 */
176 { 3, TYPE_DD }, /* 13: H820 <- == D820, 82x10 */
177 { 11, TYPE_HD }, /* 14: h1476 */
178 { 12, TYPE_HD }, /* 15: H1722 */
179 { 13, TYPE_DD }, /* 16: h420 */
180 { 14, TYPE_DD }, /* 17: H830 */
181 { 15, TYPE_HD }, /* 18: h1494 */
182 { 16, TYPE_HD }, /* 19: H1743 */
183 { 17, TYPE_DD }, /* 20: h880 */
184 { 18, TYPE_DD }, /* 21: D1040 */
185 { 19, TYPE_DD }, /* 22: D1120 */
186 { 20, TYPE_HD }, /* 23: h1600 */
187 { 21, TYPE_HD }, /* 24: H1760 */
188 { 22, TYPE_HD }, /* 25: H1920 */
189 { 23, TYPE_ED }, /* 26: E3200 */
190 { 24, TYPE_ED }, /* 27: E3520 */
191 { 25, TYPE_ED }, /* 28: E3840 */
192 { 26, TYPE_HD }, /* 29: H1840 */
193 { 27, TYPE_DD }, /* 30: D800 */
194 { 6, TYPE_HD }, /* 31: H1640 <- was H1600 == h1600 for PC */
197 #define NUM_DISK_MINORS (sizeof(minor2disktype)/sizeof(*minor2disktype))
200 * Maximum disk size (in kilobytes). This default is used whenever the
201 * current disk size is unknown.
203 #define MAX_DISK_SIZE 3280
206 * MSch: User-provided type information. 'drive' points to
207 * the respective entry of this array. Set by FDSETPRM ioctls.
209 static struct atari_disk_type user_params[FD_MAX_UNITS];
212 * User-provided permanent type information. 'drive' points to
213 * the respective entry of this array. Set by FDDEFPRM ioctls,
214 * restored upon disk change by floppy_revalidate() if valid (as seen by
215 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
217 static struct atari_disk_type default_params[FD_MAX_UNITS] = {
218 { NULL, 0, 0, 0, 0}, };
220 static int floppy_sizes[256];
221 static int floppy_blocksizes[256] = { 0, };
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. */
333 #define START_MOTOR_OFF_TIMER(delay) \
334 do { \
335 motor_off_timer.expires = jiffies + (delay); \
336 add_timer( &motor_off_timer ); \
337 MotorOffTrys = 0; \
338 } while(0)
340 #define START_CHECK_CHANGE_TIMER(delay) \
341 do { \
342 timer_table[FLOPPY_TIMER].expires = jiffies + (delay); \
343 timer_active |= (1 << FLOPPY_TIMER); \
344 } while(0)
346 #define START_TIMEOUT() \
347 mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT)
349 #define STOP_TIMEOUT() \
350 del_timer(&timeout_timer)
354 * The driver is trying to determine the correct media format
355 * while Probing is set. fd_rwsec_done() clears it after a
356 * successful access.
358 static int Probing = 0;
360 /* This flag is set when a dummy seek is necessary to make the WP
361 * status bit accessible.
363 static int NeedSeek = 0;
366 #ifdef DEBUG
367 #define DPRINT(a) printk a
368 #else
369 #define DPRINT(a)
370 #endif
372 /***************************** Prototypes *****************************/
374 static void fd_select_side( int side );
375 static void fd_select_drive( int drive );
376 static void fd_deselect( void );
377 static void fd_motor_off_timer( unsigned long dummy );
378 static void check_change( void );
379 static __inline__ void set_head_settle_flag( void );
380 static __inline__ int get_head_settle_flag( void );
381 static void floppy_irq (int irq, void *dummy, struct pt_regs *fp);
382 static void fd_error( void );
383 static int do_format(kdev_t drive, struct atari_format_descr *desc);
384 static void do_fd_action( int drive );
385 static void fd_calibrate( void );
386 static void fd_calibrate_done( int status );
387 static void fd_seek( void );
388 static void fd_seek_done( int status );
389 static void fd_rwsec( void );
390 static void fd_readtrack_check( unsigned long dummy );
391 static void fd_rwsec_done( int status );
392 static void fd_rwsec_done1(int status);
393 static void fd_writetrack( void );
394 static void fd_writetrack_done( int status );
395 static void fd_times_out( unsigned long dummy );
396 static void finish_fdc( void );
397 static void finish_fdc_done( int dummy );
398 static void floppy_off( unsigned int nr);
399 static __inline__ void copy_buffer( void *from, void *to);
400 static void setup_req_params( int drive );
401 static void redo_fd_request( void);
402 static int invalidate_drive(kdev_t rdev);
403 static int fd_ioctl( struct inode *inode, struct file *filp, unsigned int
404 cmd, unsigned long param);
405 static void fd_probe( int drive );
406 static int fd_test_drive_present( int drive );
407 static void config_types( void );
408 static int floppy_open( struct inode *inode, struct file *filp );
409 static int floppy_release( struct inode * inode, struct file * filp );
411 /************************* End of Prototypes **************************/
413 static struct timer_list motor_off_timer =
414 { NULL, NULL, 0, 0, fd_motor_off_timer };
415 static struct timer_list readtrack_timer =
416 { NULL, NULL, 0, 0, fd_readtrack_check };
418 static struct timer_list timeout_timer =
419 { NULL, NULL, 0, 0, fd_times_out };
423 /* Select the side to use. */
425 static void fd_select_side( int side )
427 unsigned long flags;
429 save_flags(flags);
430 cli(); /* protect against various other ints mucking around with the PSG */
432 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
433 sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
434 sound_ym.rd_data_reg_sel & 0xfe;
436 restore_flags(flags);
440 /* Select a drive, update the FDC's track register and set the correct
441 * clock speed for this disk's type.
444 static void fd_select_drive( int drive )
446 unsigned long flags;
447 unsigned char tmp;
449 if (drive == SelectedDrive)
450 return;
452 save_flags(flags);
453 cli(); /* protect against various other ints mucking around with the PSG */
454 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
455 tmp = sound_ym.rd_data_reg_sel;
456 sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
457 atari_dont_touch_floppy_select = 1;
458 restore_flags(flags);
460 /* restore track register to saved value */
461 FDC_WRITE( FDCREG_TRACK, UD.track );
462 udelay(25);
464 /* select 8/16 MHz */
465 if (UDT)
466 if (ATARIHW_PRESENT(FDCSPEED))
467 dma_wd.fdc_speed = UDT->fdc_speed;
469 SelectedDrive = drive;
473 /* Deselect both drives. */
475 static void fd_deselect( void )
477 unsigned long flags;
479 save_flags(flags);
480 cli(); /* protect against various other ints mucking around with the PSG */
481 atari_dont_touch_floppy_select = 0;
482 sound_ym.rd_data_reg_sel=14; /* Select PSG Port A */
483 sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
484 (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
485 /* On Falcon, the drive B select line is used on the printer port, so
486 * leave it alone... */
487 SelectedDrive = -1;
488 restore_flags(flags);
492 /* This timer function deselects the drives when the FDC switched the
493 * motor off. The deselection cannot happen earlier because the FDC
494 * counts the index signals, which arrive only if one drive is selected.
497 static void fd_motor_off_timer( unsigned long dummy )
499 /* unsigned long flags; */
500 unsigned char status;
501 int delay;
503 del_timer( &motor_off_timer );
505 if (SelectedDrive < 0)
506 /* no drive selected, needn't deselect anyone */
507 return;
509 /* save_flags(flags);
510 cli(); */
512 if (stdma_islocked())
513 goto retry;
515 status = FDC_READ( FDCREG_STATUS );
517 if (!(status & 0x80)) {
518 /* motor already turned off by FDC -> deselect drives */
519 MotorOn = 0;
520 fd_deselect();
521 /* restore_flags(flags); */
522 return;
524 /* not yet off, try again */
526 retry:
527 /* restore_flags(flags); */
528 /* Test again later; if tested too often, it seems there is no disk
529 * in the drive and the FDC will leave the motor on forever (or,
530 * at least until a disk is inserted). So we'll test only twice
531 * per second from then on...
533 delay = (MotorOffTrys < FD_MOTOR_OFF_MAXTRY) ?
534 (++MotorOffTrys, HZ/20) : HZ/2;
535 START_MOTOR_OFF_TIMER( delay );
539 /* This function is repeatedly called to detect disk changes (as good
540 * as possible) and keep track of the current state of the write protection.
543 static void check_change( void )
545 static int drive = 0;
547 unsigned long flags;
548 unsigned char old_porta;
549 int stat;
551 if (++drive > 1 || !UD.connected)
552 drive = 0;
554 save_flags(flags);
555 cli(); /* protect against various other ints mucking around with the PSG */
557 if (!stdma_islocked()) {
558 sound_ym.rd_data_reg_sel = 14;
559 old_porta = sound_ym.rd_data_reg_sel;
560 sound_ym.wd_data = (old_porta | DSKDRVNONE) &
561 ~(drive == 0 ? DSKDRV0 : DSKDRV1);
562 stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
563 sound_ym.wd_data = old_porta;
565 if (stat != UD.wpstat) {
566 DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
567 UD.wpstat = stat;
568 set_bit (drive, &changed_floppies);
571 restore_flags(flags);
573 START_CHECK_CHANGE_TIMER( CHECK_CHANGE_DELAY );
577 /* Handling of the Head Settling Flag: This flag should be set after each
578 * seek operation, because we don't use seeks with verify.
581 static __inline__ void set_head_settle_flag( void )
583 HeadSettleFlag = FDCCMDADD_E;
586 static __inline__ int get_head_settle_flag( void )
588 int tmp = HeadSettleFlag;
589 HeadSettleFlag = 0;
590 return( tmp );
596 /* General Interrupt Handling */
598 static void (*FloppyIRQHandler)( int status ) = NULL;
600 static void floppy_irq (int irq, void *dummy, struct pt_regs *fp)
602 unsigned char status;
603 void (*handler)( int );
605 handler = FloppyIRQHandler;
606 FloppyIRQHandler = NULL;
608 if (handler) {
609 nop();
610 status = FDC_READ( FDCREG_STATUS );
611 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
612 handler( status );
614 else {
615 DPRINT(("FDC irq, no handler\n"));
620 /* Error handling: If some error happened, retry some times, then
621 * recalibrate, then try again, and fail after MAX_ERRORS.
624 static void fd_error( void )
626 if (IsFormatting) {
627 IsFormatting = 0;
628 FormatError = 1;
629 wake_up( &format_wait );
630 return;
633 if (!CURRENT) return;
634 CURRENT->errors++;
635 if (CURRENT->errors >= MAX_ERRORS) {
636 printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
637 end_request( 0 );
639 else if (CURRENT->errors == RECALIBRATE_ERRORS) {
640 printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
641 if (SelectedDrive != -1)
642 SUD.track = -1;
644 redo_fd_request();
649 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
652 /* ---------- Formatting ---------- */
654 #define FILL(n,val) \
655 do { \
656 memset( p, val, n ); \
657 p += n; \
658 } while(0)
660 static int do_format(kdev_t device, struct atari_format_descr *desc)
662 unsigned char *p;
663 int sect, nsect;
664 unsigned long flags;
665 int type, drive = MINOR(device) & 3;
667 DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
668 drive, desc->track, desc->head, desc->sect_offset ));
670 save_flags(flags);
671 cli();
672 while( fdc_busy ) sleep_on( &fdc_wait );
673 fdc_busy = 1;
674 stdma_lock(floppy_irq, NULL);
675 atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
676 restore_flags(flags);
678 type = MINOR(device) >> 2;
679 if (type) {
680 if (--type >= NUM_DISK_MINORS ||
681 minor2disktype[type].drive_types > DriveType) {
682 redo_fd_request();
683 return -EINVAL;
685 type = minor2disktype[type].index;
686 UDT = &disk_type[type];
689 if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
690 redo_fd_request();
691 return -EINVAL;
694 nsect = UDT->spt;
695 p = TrackBuffer;
696 /* The track buffer is used for the raw track data, so its
697 contents become invalid! */
698 BufferDrive = -1;
699 /* stop deselect timer */
700 del_timer( &motor_off_timer );
702 FILL( 60 * (nsect / 9), 0x4e );
703 for( sect = 0; sect < nsect; ++sect ) {
704 FILL( 12, 0 );
705 FILL( 3, 0xf5 );
706 *p++ = 0xfe;
707 *p++ = desc->track;
708 *p++ = desc->head;
709 *p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
710 *p++ = 2;
711 *p++ = 0xf7;
712 FILL( 22, 0x4e );
713 FILL( 12, 0 );
714 FILL( 3, 0xf5 );
715 *p++ = 0xfb;
716 FILL( 512, 0xe5 );
717 *p++ = 0xf7;
718 FILL( 40, 0x4e );
720 FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
722 IsFormatting = 1;
723 FormatError = 0;
724 ReqTrack = desc->track;
725 ReqSide = desc->head;
726 do_fd_action( drive );
728 sleep_on( &format_wait );
730 redo_fd_request();
731 return( FormatError ? -EIO : 0 );
735 /* do_fd_action() is the general procedure for a fd request: All
736 * required parameter settings (drive select, side select, track
737 * position) are checked and set if needed. For each of these
738 * parameters and the actual reading or writing exist two functions:
739 * one that starts the setting (or skips it if possible) and one
740 * callback for the "done" interrupt. Each done func calls the next
741 * set function to propagate the request down to fd_rwsec_done().
744 static void do_fd_action( int drive )
746 DPRINT(("do_fd_action\n"));
748 if (UseTrackbuffer && !IsFormatting) {
749 repeat:
750 if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
751 if (ReqCmd == READ) {
752 copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
753 if (++ReqCnt < CURRENT->current_nr_sectors) {
754 /* read next sector */
755 setup_req_params( drive );
756 goto repeat;
758 else {
759 /* all sectors finished */
760 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
761 CURRENT->sector += CURRENT->current_nr_sectors;
762 end_request( 1 );
763 redo_fd_request();
764 return;
767 else {
768 /* cmd == WRITE, pay attention to track buffer
769 * consistency! */
770 copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
775 if (SelectedDrive != drive)
776 fd_select_drive( drive );
778 if (UD.track == -1)
779 fd_calibrate();
780 else if (UD.track != ReqTrack << UDT->stretch)
781 fd_seek();
782 else if (IsFormatting)
783 fd_writetrack();
784 else
785 fd_rwsec();
789 /* Seek to track 0 if the current track is unknown */
791 static void fd_calibrate( void )
793 if (SUD.track >= 0) {
794 fd_calibrate_done( 0 );
795 return;
798 if (ATARIHW_PRESENT(FDCSPEED))
799 dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
800 DPRINT(("fd_calibrate\n"));
801 SET_IRQ_HANDLER( fd_calibrate_done );
802 /* we can't verify, since the speed may be incorrect */
803 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
805 NeedSeek = 1;
806 MotorOn = 1;
807 START_TIMEOUT();
808 /* wait for IRQ */
812 static void fd_calibrate_done( int status )
814 DPRINT(("fd_calibrate_done()\n"));
815 STOP_TIMEOUT();
817 /* set the correct speed now */
818 if (ATARIHW_PRESENT(FDCSPEED))
819 dma_wd.fdc_speed = SUDT->fdc_speed;
820 if (status & FDCSTAT_RECNF) {
821 printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
822 fd_error();
824 else {
825 SUD.track = 0;
826 fd_seek();
831 /* Seek the drive to the requested track. The drive must have been
832 * calibrated at some point before this.
835 static void fd_seek( void )
837 if (SUD.track == ReqTrack << SUDT->stretch) {
838 fd_seek_done( 0 );
839 return;
842 if (ATARIHW_PRESENT(FDCSPEED)) {
843 dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
844 MFPDELAY();
847 DPRINT(("fd_seek() to track %d\n",ReqTrack));
848 FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
849 udelay(25);
850 SET_IRQ_HANDLER( fd_seek_done );
851 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
853 MotorOn = 1;
854 set_head_settle_flag();
855 START_TIMEOUT();
856 /* wait for IRQ */
860 static void fd_seek_done( int status )
862 DPRINT(("fd_seek_done()\n"));
863 STOP_TIMEOUT();
865 /* set the correct speed */
866 if (ATARIHW_PRESENT(FDCSPEED))
867 dma_wd.fdc_speed = SUDT->fdc_speed;
868 if (status & FDCSTAT_RECNF) {
869 printk(KERN_ERR "fd%d: seek error (to track %d)\n",
870 SelectedDrive, ReqTrack );
871 /* we don't know exactly which track we are on now! */
872 SUD.track = -1;
873 fd_error();
875 else {
876 SUD.track = ReqTrack << SUDT->stretch;
877 NeedSeek = 0;
878 if (IsFormatting)
879 fd_writetrack();
880 else
881 fd_rwsec();
886 /* This does the actual reading/writing after positioning the head
887 * over the correct track.
890 static int MultReadInProgress = 0;
893 static void fd_rwsec( void )
895 unsigned long paddr, flags;
896 unsigned int rwflag, old_motoron;
897 unsigned int track;
899 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
900 if (ReqCmd == WRITE) {
901 if (ATARIHW_PRESENT(EXTD_DMA)) {
902 paddr = (unsigned long)VTOP(ReqData);
904 else {
905 copy_buffer( ReqData, DMABuffer );
906 paddr = PhysDMABuffer;
908 dma_cache_maintenance( paddr, 512, 1 );
909 rwflag = 0x100;
911 else {
912 if (read_track)
913 paddr = PhysTrackBuffer;
914 else
915 paddr = ATARIHW_PRESENT(EXTD_DMA) ? VTOP(ReqData) : PhysDMABuffer;
916 rwflag = 0;
919 fd_select_side( ReqSide );
921 /* Start sector of this operation */
922 FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
923 MFPDELAY();
924 /* Cheat for track if stretch != 0 */
925 if (SUDT->stretch) {
926 track = FDC_READ( FDCREG_TRACK);
927 MFPDELAY();
928 FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
930 udelay(25);
932 /* Setup DMA */
933 save_flags(flags);
934 cli();
935 dma_wd.dma_lo = (unsigned char)paddr;
936 MFPDELAY();
937 paddr >>= 8;
938 dma_wd.dma_md = (unsigned char)paddr;
939 MFPDELAY();
940 paddr >>= 8;
941 if (ATARIHW_PRESENT(EXTD_DMA))
942 st_dma_ext_dmahi = (unsigned short)paddr;
943 else
944 dma_wd.dma_hi = (unsigned char)paddr;
945 MFPDELAY();
946 restore_flags(flags);
948 /* Clear FIFO and switch DMA to correct mode */
949 dma_wd.dma_mode_status = 0x90 | rwflag;
950 MFPDELAY();
951 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
952 MFPDELAY();
953 dma_wd.dma_mode_status = 0x90 | rwflag;
954 MFPDELAY();
956 /* How many sectors for DMA */
957 dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
959 udelay(25);
961 /* Start operation */
962 dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
963 udelay(25);
964 SET_IRQ_HANDLER( fd_rwsec_done );
965 dma_wd.fdc_acces_seccount =
966 (get_head_settle_flag() |
967 (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
969 old_motoron = MotorOn;
970 MotorOn = 1;
971 NeedSeek = 1;
972 /* wait for interrupt */
974 if (read_track) {
975 /* If reading a whole track, wait about one disk rotation and
976 * then check if all sectors are read. The FDC will even
977 * search for the first non-existent sector and need 1 sec to
978 * recognise that it isn't present :-(
980 del_timer (&readtrack_timer);
981 readtrack_timer.expires =
982 jiffies + HZ/5 + (old_motoron ? 0 : HZ);
983 /* 1 rot. + 5 rot.s if motor was off */
984 MultReadInProgress = 1;
985 add_timer( &readtrack_timer );
987 START_TIMEOUT();
991 static void fd_readtrack_check( unsigned long dummy )
993 unsigned long flags, addr, addr2;
995 save_flags(flags);
996 cli();
998 del_timer( &readtrack_timer );
1000 if (!MultReadInProgress) {
1001 /* This prevents a race condition that could arise if the
1002 * interrupt is triggered while the calling of this timer
1003 * callback function takes place. The IRQ function then has
1004 * already cleared 'MultReadInProgress' when flow of control
1005 * gets here.
1007 restore_flags(flags);
1008 return;
1011 /* get the current DMA address */
1012 /* ++ f.a. read twice to avoid being fooled by switcher */
1013 addr = 0;
1014 do {
1015 addr2 = addr;
1016 addr = dma_wd.dma_lo & 0xff;
1017 MFPDELAY();
1018 addr |= (dma_wd.dma_md & 0xff) << 8;
1019 MFPDELAY();
1020 if (ATARIHW_PRESENT( EXTD_DMA ))
1021 addr |= (st_dma_ext_dmahi & 0xffff) << 16;
1022 else
1023 addr |= (dma_wd.dma_hi & 0xff) << 16;
1024 MFPDELAY();
1025 } while(addr != addr2);
1027 if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1028 /* already read enough data, force an FDC interrupt to stop
1029 * the read operation
1031 SET_IRQ_HANDLER( NULL );
1032 MultReadInProgress = 0;
1033 restore_flags(flags);
1034 DPRINT(("fd_readtrack_check(): done\n"));
1035 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1036 udelay(25);
1038 /* No error until now -- the FDC would have interrupted
1039 * otherwise!
1041 fd_rwsec_done1(0);
1043 else {
1044 /* not yet finished, wait another tenth rotation */
1045 restore_flags(flags);
1046 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1047 readtrack_timer.expires = jiffies + HZ/5/10;
1048 add_timer( &readtrack_timer );
1053 static void fd_rwsec_done( int status )
1055 DPRINT(("fd_rwsec_done()\n"));
1057 if (read_track) {
1058 del_timer(&readtrack_timer);
1059 if (!MultReadInProgress)
1060 return;
1061 MultReadInProgress = 0;
1063 fd_rwsec_done1(status);
1066 static void fd_rwsec_done1(int status)
1068 unsigned int track;
1070 STOP_TIMEOUT();
1072 /* Correct the track if stretch != 0 */
1073 if (SUDT->stretch) {
1074 track = FDC_READ( FDCREG_TRACK);
1075 MFPDELAY();
1076 FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1079 if (!UseTrackbuffer) {
1080 dma_wd.dma_mode_status = 0x90;
1081 MFPDELAY();
1082 if (!(dma_wd.dma_mode_status & 0x01)) {
1083 printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1084 goto err_end;
1087 MFPDELAY();
1089 if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1090 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1091 goto err_end;
1093 if ((status & FDCSTAT_RECNF) &&
1094 /* RECNF is no error after a multiple read when the FDC
1095 searched for a non-existent sector! */
1096 !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1097 if (Probing) {
1098 if (SUDT > disk_type) {
1099 if (SUDT[-1].blocks > ReqBlock) {
1100 /* try another disk type */
1101 SUDT--;
1102 floppy_sizes[SelectedDrive] = SUDT->blocks >> 1;
1103 } else
1104 Probing = 0;
1106 else {
1107 if (SUD.flags & FTD_MSG)
1108 printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1109 SelectedDrive, SUDT->name );
1110 Probing=0;
1112 } else {
1113 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1114 if (SUD.autoprobe) {
1115 SUDT = disk_type + StartDiskType[DriveType];
1116 floppy_sizes[SelectedDrive] = SUDT->blocks >> 1;
1117 Probing = 1;
1120 if (Probing) {
1121 if (ATARIHW_PRESENT(FDCSPEED)) {
1122 dma_wd.fdc_speed = SUDT->fdc_speed;
1123 MFPDELAY();
1125 setup_req_params( SelectedDrive );
1126 BufferDrive = -1;
1127 do_fd_action( SelectedDrive );
1128 return;
1131 printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1132 SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1133 goto err_end;
1135 if (status & FDCSTAT_CRC) {
1136 printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1137 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1138 goto err_end;
1140 if (status & FDCSTAT_LOST) {
1141 printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1142 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1143 goto err_end;
1146 Probing = 0;
1148 if (ReqCmd == READ) {
1149 if (!read_track) {
1150 void *addr;
1151 addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1152 dma_cache_maintenance( VTOP(addr), 512, 0 );
1153 if (!ATARIHW_PRESENT( EXTD_DMA ))
1154 copy_buffer (addr, ReqData);
1155 } else {
1156 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1157 BufferDrive = SelectedDrive;
1158 BufferSide = ReqSide;
1159 BufferTrack = ReqTrack;
1160 copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1164 if (++ReqCnt < CURRENT->current_nr_sectors) {
1165 /* read next sector */
1166 setup_req_params( SelectedDrive );
1167 do_fd_action( SelectedDrive );
1169 else {
1170 /* all sectors finished */
1171 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
1172 CURRENT->sector += CURRENT->current_nr_sectors;
1173 end_request( 1 );
1174 redo_fd_request();
1176 return;
1178 err_end:
1179 BufferDrive = -1;
1180 fd_error();
1184 static void fd_writetrack( void )
1186 unsigned long paddr, flags;
1187 unsigned int track;
1189 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1191 paddr = PhysTrackBuffer;
1192 dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1194 fd_select_side( ReqSide );
1196 /* Cheat for track if stretch != 0 */
1197 if (SUDT->stretch) {
1198 track = FDC_READ( FDCREG_TRACK);
1199 MFPDELAY();
1200 FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1202 udelay(40);
1204 /* Setup DMA */
1205 save_flags(flags);
1206 cli();
1207 dma_wd.dma_lo = (unsigned char)paddr;
1208 MFPDELAY();
1209 paddr >>= 8;
1210 dma_wd.dma_md = (unsigned char)paddr;
1211 MFPDELAY();
1212 paddr >>= 8;
1213 if (ATARIHW_PRESENT( EXTD_DMA ))
1214 st_dma_ext_dmahi = (unsigned short)paddr;
1215 else
1216 dma_wd.dma_hi = (unsigned char)paddr;
1217 MFPDELAY();
1218 restore_flags(flags);
1220 /* Clear FIFO and switch DMA to correct mode */
1221 dma_wd.dma_mode_status = 0x190;
1222 MFPDELAY();
1223 dma_wd.dma_mode_status = 0x90;
1224 MFPDELAY();
1225 dma_wd.dma_mode_status = 0x190;
1226 MFPDELAY();
1228 /* How many sectors for DMA */
1229 dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1230 udelay(40);
1232 /* Start operation */
1233 dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1234 udelay(40);
1235 SET_IRQ_HANDLER( fd_writetrack_done );
1236 dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1238 MotorOn = 1;
1239 START_TIMEOUT();
1240 /* wait for interrupt */
1244 static void fd_writetrack_done( int status )
1246 DPRINT(("fd_writetrack_done()\n"));
1248 STOP_TIMEOUT();
1250 if (status & FDCSTAT_WPROT) {
1251 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1252 goto err_end;
1254 if (status & FDCSTAT_LOST) {
1255 printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1256 SelectedDrive, ReqSide, ReqTrack );
1257 goto err_end;
1260 wake_up( &format_wait );
1261 return;
1263 err_end:
1264 fd_error();
1267 static void fd_times_out( unsigned long dummy )
1269 atari_disable_irq( IRQ_MFP_FDC );
1270 if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1271 * before we came here... */
1273 SET_IRQ_HANDLER( NULL );
1274 /* If the timeout occurred while the readtrack_check timer was
1275 * active, we need to cancel it, else bad things will happen */
1276 if (UseTrackbuffer)
1277 del_timer( &readtrack_timer );
1278 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1279 udelay( 25 );
1281 printk(KERN_ERR "floppy timeout\n" );
1282 fd_error();
1283 end:
1284 atari_enable_irq( IRQ_MFP_FDC );
1288 /* The (noop) seek operation here is needed to make the WP bit in the
1289 * FDC status register accessible for check_change. If the last disk
1290 * operation would have been a RDSEC, this bit would always read as 0
1291 * no matter what :-( To save time, the seek goes to the track we're
1292 * already on.
1295 static void finish_fdc( void )
1297 if (!NeedSeek) {
1298 finish_fdc_done( 0 );
1300 else {
1301 DPRINT(("finish_fdc: dummy seek started\n"));
1302 FDC_WRITE (FDCREG_DATA, SUD.track);
1303 SET_IRQ_HANDLER( finish_fdc_done );
1304 FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1305 MotorOn = 1;
1306 START_TIMEOUT();
1307 /* we must wait for the IRQ here, because the ST-DMA
1308 is released immediately afterwards and the interrupt
1309 may be delivered to the wrong driver. */
1314 static void finish_fdc_done( int dummy )
1316 unsigned long flags;
1318 DPRINT(("finish_fdc_done entered\n"));
1319 STOP_TIMEOUT();
1320 NeedSeek = 0;
1322 if ((timer_active & (1 << FLOPPY_TIMER)) &&
1323 timer_table[FLOPPY_TIMER].expires < jiffies + 5)
1324 /* If the check for a disk change is done too early after this
1325 * last seek command, the WP bit still reads wrong :-((
1327 timer_table[FLOPPY_TIMER].expires = jiffies + 5;
1328 else
1329 START_CHECK_CHANGE_TIMER( CHECK_CHANGE_DELAY );
1330 del_timer( &motor_off_timer );
1331 START_MOTOR_OFF_TIMER( FD_MOTOR_OFF_DELAY );
1333 save_flags(flags);
1334 cli();
1335 stdma_release();
1336 fdc_busy = 0;
1337 wake_up( &fdc_wait );
1338 restore_flags(flags);
1340 DPRINT(("finish_fdc() finished\n"));
1344 /* Prevent "aliased" accesses. */
1345 static int fd_ref[4] = { 0,0,0,0 };
1346 static int fd_device[4] = { 0,0,0,0 };
1349 * Current device number. Taken either from the block header or from the
1350 * format request descriptor.
1352 #define CURRENT_DEVICE (CURRENT->rq_dev)
1354 /* Current error count. */
1355 #define CURRENT_ERRORS (CURRENT->errors)
1358 /* dummy for blk.h */
1359 static void floppy_off( unsigned int nr) {}
1362 /* The detection of disk changes is a dark chapter in Atari history :-(
1363 * Because the "Drive ready" signal isn't present in the Atari
1364 * hardware, one has to rely on the "Write Protect". This works fine,
1365 * as long as no write protected disks are used. TOS solves this
1366 * problem by introducing tri-state logic ("maybe changed") and
1367 * looking at the serial number in block 0. This isn't possible for
1368 * Linux, since the floppy driver can't make assumptions about the
1369 * filesystem used on the disk and thus the contents of block 0. I've
1370 * chosen the method to always say "The disk was changed" if it is
1371 * unsure whether it was. This implies that every open or mount
1372 * invalidates the disk buffers if you work with write protected
1373 * disks. But at least this is better than working with incorrect data
1374 * due to unrecognised disk changes.
1377 static int check_floppy_change (kdev_t dev)
1379 unsigned int drive = MINOR(dev) & 0x03;
1381 if (MAJOR(dev) != MAJOR_NR) {
1382 printk(KERN_ERR "floppy_changed: not a floppy\n");
1383 return 0;
1386 if (test_bit (drive, &fake_change)) {
1387 /* simulated change (e.g. after formatting) */
1388 return 1;
1390 if (test_bit (drive, &changed_floppies)) {
1391 /* surely changed (the WP signal changed at least once) */
1392 return 1;
1394 if (UD.wpstat) {
1395 /* WP is on -> could be changed: to be sure, buffers should be
1396 * invalidated...
1398 return 1;
1401 return 0;
1404 static int floppy_revalidate (kdev_t dev)
1406 int drive = MINOR(dev) & 3;
1408 if (test_bit(drive, &changed_floppies) ||
1409 test_bit(drive, &fake_change) ||
1410 unit[drive].disktype == 0) {
1411 if (UD.flags & FTD_MSG)
1412 printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1413 BufferDrive = -1;
1414 clear_bit(drive, &fake_change);
1415 clear_bit(drive, &changed_floppies);
1416 /* MSch: clearing geometry makes sense only for autoprobe
1417 formats, for 'permanent user-defined' parameter:
1418 restore default_params[] here if flagged valid! */
1419 if (default_params[drive].blocks == 0)
1420 UDT = 0;
1421 else
1422 UDT = &default_params[drive];
1424 return 0;
1427 static __inline__ void copy_buffer(void *from, void *to)
1429 ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
1430 int cnt;
1432 for( cnt = 512/4; cnt; cnt-- )
1433 *p2++ = *p1++;
1437 /* This sets up the global variables describing the current request. */
1439 static void setup_req_params( int drive )
1441 int block = ReqBlock + ReqCnt;
1443 ReqTrack = block / UDT->spt;
1444 ReqSector = block - ReqTrack * UDT->spt + 1;
1445 ReqSide = ReqTrack & 1;
1446 ReqTrack >>= 1;
1447 ReqData = ReqBuffer + 512 * ReqCnt;
1449 if (UseTrackbuffer)
1450 read_track = (ReqCmd == READ && CURRENT_ERRORS == 0);
1451 else
1452 read_track = 0;
1454 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1455 ReqTrack, ReqSector, (unsigned long)ReqData ));
1459 static void redo_fd_request(void)
1461 int device, drive, type;
1463 DPRINT(("redo_fd_request: CURRENT=%08lx CURRENT->dev=%04x CURRENT->sector=%ld\n",
1464 (unsigned long)CURRENT, CURRENT ? CURRENT->rq_dev : 0,
1465 CURRENT ? CURRENT->sector : 0 ));
1467 IsFormatting = 0;
1469 if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
1470 return;
1473 repeat:
1475 if (!CURRENT)
1476 goto the_end;
1478 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1479 panic(DEVICE_NAME ": request list destroyed");
1481 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
1482 panic(DEVICE_NAME ": block not locked");
1484 device = MINOR(CURRENT_DEVICE);
1485 drive = device & 3;
1486 type = device >> 2;
1488 if (!UD.connected) {
1489 /* drive not connected */
1490 printk(KERN_ERR "Unknown Device: fd%d\n", drive );
1491 end_request(0);
1492 goto repeat;
1495 if (type == 0) {
1496 if (!UDT) {
1497 Probing = 1;
1498 UDT = disk_type + StartDiskType[DriveType];
1499 floppy_sizes[drive] = UDT->blocks >> 1;
1500 UD.autoprobe = 1;
1503 else {
1504 /* user supplied disk type */
1505 if (--type >= NUM_DISK_MINORS) {
1506 printk(KERN_WARNING "fd%d: invalid disk format", drive );
1507 end_request( 0 );
1508 goto repeat;
1510 if (minor2disktype[type].drive_types > DriveType) {
1511 printk(KERN_WARNING "fd%d: unsupported disk format", drive );
1512 end_request( 0 );
1513 goto repeat;
1515 type = minor2disktype[type].index;
1516 UDT = &disk_type[type];
1517 floppy_sizes[drive] = UDT->blocks >> 1;
1518 UD.autoprobe = 0;
1521 if (CURRENT->sector + 1 > UDT->blocks) {
1522 end_request(0);
1523 goto repeat;
1526 /* stop deselect timer */
1527 del_timer( &motor_off_timer );
1529 ReqCnt = 0;
1530 ReqCmd = CURRENT->cmd;
1531 ReqBlock = CURRENT->sector;
1532 ReqBuffer = CURRENT->buffer;
1533 setup_req_params( drive );
1534 do_fd_action( drive );
1536 return;
1538 the_end:
1539 finish_fdc();
1543 void do_fd_request(void)
1545 unsigned long flags;
1547 DPRINT(("do_fd_request for pid %d\n",current->pid));
1548 while( fdc_busy ) sleep_on( &fdc_wait );
1549 fdc_busy = 1;
1550 stdma_lock(floppy_irq, NULL);
1552 atari_disable_irq( IRQ_MFP_FDC );
1553 save_flags(flags); /* The request function is called with ints
1554 sti(); * disabled... so must save the IPL for later */
1555 redo_fd_request();
1556 restore_flags(flags);
1557 atari_enable_irq( IRQ_MFP_FDC );
1561 static int invalidate_drive(kdev_t rdev)
1563 /* invalidate the buffer track to force a reread */
1564 BufferDrive = -1;
1565 set_bit(MINOR(rdev) & 3, &fake_change);
1566 check_disk_change(rdev);
1567 return 0;
1570 static int fd_ioctl(struct inode *inode, struct file *filp,
1571 unsigned int cmd, unsigned long param)
1573 #define IOCTL_MODE_BIT 8
1574 #define OPEN_WRITE_BIT 16
1575 #define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
1577 int drive, type;
1578 kdev_t device;
1579 struct atari_format_descr fmt_desc;
1580 struct atari_disk_type *dtp;
1581 struct floppy_struct getprm;
1582 int settype;
1583 struct floppy_struct setprm;
1585 device = inode->i_rdev;
1586 switch (cmd) {
1587 RO_IOCTLS (device, param);
1589 drive = MINOR (device);
1590 type = drive >> 2;
1591 drive &= 3;
1592 switch (cmd) {
1593 case FDGETPRM:
1594 case BLKGETSIZE:
1595 if (type) {
1596 if (--type >= NUM_DISK_MINORS)
1597 return -ENODEV;
1598 if (minor2disktype[type].drive_types > DriveType)
1599 return -ENODEV;
1600 type = minor2disktype[type].index;
1601 dtp = &disk_type[type];
1602 if (UD.flags & FTD_MSG)
1603 printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1604 drive, dtp, dtp->name);
1606 else {
1607 if (!UDT)
1608 return -ENXIO;
1609 else
1610 dtp = UDT;
1612 if (cmd == BLKGETSIZE)
1613 return put_user(dtp->blocks, (long *)param);
1615 memset((void *)&getprm, 0, sizeof(getprm));
1616 getprm.size = dtp->blocks;
1617 getprm.sect = dtp->spt;
1618 getprm.head = 2;
1619 getprm.track = dtp->blocks/dtp->spt/2;
1620 getprm.stretch = dtp->stretch;
1621 if (copy_to_user((void *)param, &getprm, sizeof(getprm)))
1622 return -EFAULT;
1623 return 0;
1624 case BLKRASET:
1625 if (!capable(CAP_SYS_ADMIN))
1626 return -EACCES;
1627 if (param > 0xff)
1628 return -EINVAL;
1629 read_ahead[MAJOR(inode->i_rdev)] = param;
1630 return 0;
1631 case BLKRAGET:
1632 return put_user(read_ahead[MAJOR(inode->i_rdev)],
1633 (int *) param);
1634 case BLKFLSBUF:
1635 if (!capable(CAP_SYS_ADMIN))
1636 return -EACCES;
1637 fsync_dev(inode->i_rdev);
1638 invalidate_buffers(inode->i_rdev);
1639 return 0;
1641 if (!IOCTL_ALLOWED)
1642 return -EPERM;
1643 switch (cmd) {
1644 case FDSETPRM:
1645 case FDDEFPRM:
1647 * MSch 7/96: simple 'set geometry' case: just set the
1648 * 'default' device params (minor == 0).
1649 * Currently, the drive geometry is cleared after each
1650 * disk change and subsequent revalidate()! simple
1651 * implementation of FDDEFPRM: save geometry from a
1652 * FDDEFPRM call and restore it in floppy_revalidate() !
1655 /* get the parameters from user space */
1656 if (fd_ref[drive] != 1 && fd_ref[drive] != -1)
1657 return -EBUSY;
1658 if (copy_from_user(&setprm, (void *) param, sizeof(setprm)))
1659 return -EFAULT;
1661 * first of all: check for floppy change and revalidate,
1662 * or the next access will revalidate - and clear UDT :-(
1665 if (check_floppy_change(device))
1666 floppy_revalidate(device);
1668 if (UD.flags & FTD_MSG)
1669 printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1670 drive, setprm.size, setprm.sect, setprm.stretch);
1672 /* what if type > 0 here? Overwrite specified entry ? */
1673 if (type) {
1674 /* refuse to re-set a predefined type for now */
1675 redo_fd_request();
1676 return -EINVAL;
1680 * type == 0: first look for a matching entry in the type list,
1681 * and set the UD.disktype field to use the perdefined entry.
1682 * TODO: add user-defined format to head of autoprobe list ?
1683 * Useful to include the user-type for future autodetection!
1686 for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1687 int setidx = 0;
1688 if (minor2disktype[settype].drive_types > DriveType) {
1689 /* skip this one, invalid for drive ... */
1690 continue;
1692 setidx = minor2disktype[settype].index;
1693 dtp = &disk_type[setidx];
1695 /* found matching entry ?? */
1696 if ( dtp->blocks == setprm.size
1697 && dtp->spt == setprm.sect
1698 && dtp->stretch == setprm.stretch ) {
1699 if (UD.flags & FTD_MSG)
1700 printk (KERN_INFO "floppy%d: setting %s %p!\n",
1701 drive, dtp->name, dtp);
1702 UDT = dtp;
1703 floppy_sizes[drive] = UDT->blocks >> 1;
1705 if (cmd == FDDEFPRM) {
1706 /* save settings as permanent default type */
1707 default_params[drive].name = dtp->name;
1708 default_params[drive].spt = dtp->spt;
1709 default_params[drive].blocks = dtp->blocks;
1710 default_params[drive].fdc_speed = dtp->fdc_speed;
1711 default_params[drive].stretch = dtp->stretch;
1714 return 0;
1719 /* no matching disk type found above - setting user_params */
1721 if (cmd == FDDEFPRM) {
1722 /* set permanent type */
1723 dtp = &default_params[drive];
1724 } else
1725 /* set user type (reset by disk change!) */
1726 dtp = &user_params[drive];
1728 dtp->name = "user format";
1729 dtp->blocks = setprm.size;
1730 dtp->spt = setprm.sect;
1731 if (setprm.sect > 14)
1732 dtp->fdc_speed = 3;
1733 else
1734 dtp->fdc_speed = 0;
1735 dtp->stretch = setprm.stretch;
1737 if (UD.flags & FTD_MSG)
1738 printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1739 drive, dtp->blocks, dtp->spt, dtp->stretch);
1741 /* sanity check */
1742 if (!dtp || setprm.track != dtp->blocks/dtp->spt/2 ||
1743 setprm.head != 2) {
1744 redo_fd_request();
1745 return -EINVAL;
1748 UDT = dtp;
1749 floppy_sizes[drive] = UDT->blocks >> 1;
1751 return 0;
1752 case FDMSGON:
1753 UD.flags |= FTD_MSG;
1754 return 0;
1755 case FDMSGOFF:
1756 UD.flags &= ~FTD_MSG;
1757 return 0;
1758 case FDSETEMSGTRESH:
1759 return -EINVAL;
1760 case FDFMTBEG:
1761 return 0;
1762 case FDFMTTRK:
1763 if (fd_ref[drive] != 1 && fd_ref[drive] != -1)
1764 return -EBUSY;
1765 if (copy_from_user(&fmt_desc, (void *) param, sizeof(fmt_desc)))
1766 return -EFAULT;
1767 return do_format(device, &fmt_desc);
1768 case FDCLRPRM:
1769 UDT = NULL;
1770 /* MSch: invalidate default_params */
1771 default_params[drive].blocks = 0;
1772 floppy_sizes[drive] = MAX_DISK_SIZE;
1773 return invalidate_drive (device);
1774 case FDFMTEND:
1775 case FDFLUSH:
1776 return invalidate_drive(device);
1778 return -EINVAL;
1782 /* Initialize the 'unit' variable for drive 'drive' */
1784 __initfunc(static void fd_probe( int drive ))
1786 UD.connected = 0;
1787 UDT = NULL;
1789 if (!fd_test_drive_present( drive ))
1790 return;
1792 UD.connected = 1;
1793 UD.track = 0;
1794 switch( UserSteprate[drive] ) {
1795 case 2:
1796 UD.steprate = FDCSTEP_2;
1797 break;
1798 case 3:
1799 UD.steprate = FDCSTEP_3;
1800 break;
1801 case 6:
1802 UD.steprate = FDCSTEP_6;
1803 break;
1804 case 12:
1805 UD.steprate = FDCSTEP_12;
1806 break;
1807 default: /* should be -1 for "not set by user" */
1808 if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1809 UD.steprate = FDCSTEP_3;
1810 else
1811 UD.steprate = FDCSTEP_6;
1812 break;
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
1824 * declared absent.
1827 __initfunc(static int fd_test_drive_present( int drive ))
1829 unsigned long timeout;
1830 unsigned char status;
1831 int ok;
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 for( ok = 0, timeout = jiffies + 2*HZ+HZ/2; time_before(jiffies, timeout); ) {
1842 if (!(mfp.par_dt_reg & 0x20))
1843 break;
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 );
1852 udelay(500);
1853 status = FDC_READ( FDCREG_STATUS );
1854 udelay(20);
1856 if (ok) {
1857 /* dummy seek command to make WP bit accessible */
1858 FDC_WRITE( FDCREG_DATA, 0 );
1859 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
1860 while( mfp.par_dt_reg & 0x20 )
1862 status = FDC_READ( FDCREG_STATUS );
1865 atari_turnon_irq( IRQ_MFP_FDC );
1866 return( ok );
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 __initfunc(static void config_types( void ))
1876 int drive, cnt = 0;
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++ ) {
1884 fd_probe( drive );
1885 if (UD.connected) {
1886 printk(KERN_INFO "fd%d\n", drive);
1887 ++cnt;
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 );
1899 udelay(500);
1900 FDC_READ( FDCREG_STATUS );
1901 udelay(20);
1904 if (cnt > 0) {
1905 START_MOTOR_OFF_TIMER( FD_MOTOR_OFF_DELAY );
1906 if (cnt == 1) fd_select_drive( 0 );
1907 START_CHECK_CHANGE_TIMER( CHECK_CHANGE_DELAY );
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 inode *inode, struct file *filp )
1919 int drive, type;
1920 int old_dev;
1922 if (!filp) {
1923 DPRINT (("Weird, open called with filp=0\n"));
1924 return -EIO;
1927 drive = MINOR(inode->i_rdev) & 3;
1928 type = MINOR(inode->i_rdev) >> 2;
1929 DPRINT(("fd_open: type=%d\n",type));
1930 if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1931 return -ENXIO;
1933 old_dev = fd_device[drive];
1935 if (fd_ref[drive] && old_dev != MINOR(inode->i_rdev))
1936 return -EBUSY;
1938 if (fd_ref[drive] == -1 || (fd_ref[drive] && filp->f_flags & O_EXCL))
1939 return -EBUSY;
1941 MOD_INC_USE_COUNT;
1943 if (filp->f_flags & O_EXCL)
1944 fd_ref[drive] = -1;
1945 else
1946 fd_ref[drive]++;
1948 fd_device[drive] = MINOR(inode->i_rdev);
1950 if (old_dev && old_dev != MINOR(inode->i_rdev))
1951 invalidate_buffers(MKDEV(FLOPPY_MAJOR, old_dev));
1953 /* Allow ioctls if we have write-permissions even if read-only open */
1954 if (filp->f_mode & 2 || permission (inode, 2) == 0)
1955 filp->f_mode |= IOCTL_MODE_BIT;
1956 if (filp->f_mode & 2)
1957 filp->f_mode |= OPEN_WRITE_BIT;
1959 if (filp->f_flags & O_NDELAY)
1960 return 0;
1962 if (filp->f_mode & 3) {
1963 check_disk_change(inode->i_rdev);
1964 if (filp->f_mode & 2) {
1965 if (UD.wpstat) {
1966 floppy_release(inode, filp);
1967 return -EROFS;
1972 return 0;
1976 static int floppy_release( struct inode * inode, struct file * filp )
1978 int drive;
1980 drive = MINOR(inode->i_rdev) & 3;
1983 * If filp is NULL, we're being called from blkdev_release
1984 * or after a failed mount attempt. In the former case the
1985 * device has already been sync'ed, and in the latter no
1986 * sync is required. Otherwise, sync if filp is writable.
1988 if (filp && (filp->f_mode & (2 | OPEN_WRITE_BIT)))
1989 block_fsync (filp, filp->f_dentry);
1991 if (fd_ref[drive] < 0)
1992 fd_ref[drive] = 0;
1993 else if (!fd_ref[drive]--) {
1994 printk(KERN_ERR "floppy_release with fd_ref == 0");
1995 fd_ref[drive] = 0;
1998 MOD_DEC_USE_COUNT;
1999 return 0;
2002 static struct file_operations floppy_fops = {
2003 NULL, /* lseek - default */
2004 block_read, /* read - general block-dev read */
2005 block_write, /* write - general block-dev write */
2006 NULL, /* readdir - bad */
2007 NULL, /* select */
2008 fd_ioctl, /* ioctl */
2009 NULL, /* mmap */
2010 floppy_open, /* open */
2011 NULL, /* flush */
2012 floppy_release, /* release */
2013 block_fsync, /* fsync */
2014 NULL, /* fasync */
2015 check_floppy_change, /* media_change */
2016 floppy_revalidate, /* revalidate */
2019 __initfunc(int atari_floppy_init (void))
2021 int i;
2023 if (!MACH_IS_ATARI)
2024 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
2025 return -ENXIO;
2027 if (MACH_IS_HADES)
2028 /* Hades doesn't have Atari-compatible floppy */
2029 return -ENXIO;
2031 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
2032 printk(KERN_ERR "Unable to get major %d for floppy\n",MAJOR_NR);
2033 return -EBUSY;
2036 if (UseTrackbuffer < 0)
2037 /* not set by user -> use default: for now, we turn
2038 track buffering off for all Medusas, though it
2039 could be used with ones that have a counter
2040 card. But the test is too hard :-( */
2041 UseTrackbuffer = !MACH_IS_MEDUSA;
2043 /* initialize variables */
2044 SelectedDrive = -1;
2045 BufferDrive = -1;
2047 /* initialize check_change timer */
2048 timer_table[FLOPPY_TIMER].fn = check_change;
2049 timer_active &= ~(1 << FLOPPY_TIMER);
2051 DMABuffer = atari_stram_alloc( BUFFER_SIZE+512, NULL, "ataflop" );
2052 if (!DMABuffer) {
2053 printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
2054 unregister_blkdev(MAJOR_NR, "fd");
2055 return -ENOMEM;
2057 TrackBuffer = DMABuffer + 512;
2058 PhysDMABuffer = (unsigned long) VTOP(DMABuffer);
2059 PhysTrackBuffer = (unsigned long) VTOP(TrackBuffer);
2060 BufferDrive = BufferSide = BufferTrack = -1;
2062 for (i = 0; i < FD_MAX_UNITS; i++) {
2063 unit[i].track = -1;
2064 unit[i].flags = 0;
2067 for (i = 0; i < 256; i++)
2068 if ((i >> 2) > 0 && (i >> 2) <= NUM_DISK_MINORS) {
2069 int type = minor2disktype[(i >> 2) - 1].index;
2070 floppy_sizes[i] = disk_type[type].blocks >> 1;
2071 } else
2072 floppy_sizes[i] = MAX_DISK_SIZE;
2074 blk_size[MAJOR_NR] = floppy_sizes;
2075 blksize_size[MAJOR_NR] = floppy_blocksizes;
2076 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
2078 printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
2079 DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
2080 UseTrackbuffer ? "" : "no ");
2081 config_types();
2083 (void)do_floppy; /* avoid warning about unused variable */
2084 return 0;
2088 __initfunc(void atari_floppy_setup( char *str, int *ints ))
2090 int i;
2092 if (ints[0] < 1) {
2093 printk(KERN_ERR "ataflop_setup: no arguments!\n" );
2094 return;
2096 else if (ints[0] > 2+FD_MAX_UNITS) {
2097 printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2100 if (ints[1] < 0 || ints[1] > 2)
2101 printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2102 else
2103 DriveType = ints[1];
2105 if (ints[0] >= 2)
2106 UseTrackbuffer = (ints[2] > 0);
2108 for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2109 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2110 printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2111 else
2112 UserSteprate[i-3] = ints[i];
2116 #ifdef MODULE
2117 int init_module (void)
2119 if (!MACH_IS_ATARI)
2120 return -ENXIO;
2121 return atari_floppy_init ();
2124 void cleanup_module (void)
2126 unregister_blkdev(MAJOR_NR, "fd");
2128 blk_dev[MAJOR_NR].request_fn = 0;
2129 timer_active &= ~(1 << FLOPPY_TIMER);
2130 timer_table[FLOPPY_TIMER].fn = 0;
2131 atari_stram_free( DMABuffer );
2133 #endif