Import 2.3.9pre5
[davej-history.git] / drivers / block / ataflop.c
blobf105efd6873f854cfc97e82451b0f93f065a55b4
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] = {
219 { NULL, 0, 0, 0, 0}, };
221 static int floppy_sizes[256];
222 static int floppy_blocksizes[256] = { 0, };
224 /* current info on each unit */
225 static struct atari_floppy_struct {
226 int connected; /* !=0 : drive is connected */
227 int autoprobe; /* !=0 : do autoprobe */
229 struct atari_disk_type *disktype; /* current type of disk */
231 int track; /* current head position or -1 if
232 unknown */
233 unsigned int steprate; /* steprate setting */
234 unsigned int wpstat; /* current state of WP signal (for
235 disk change detection) */
236 int flags; /* flags */
237 } unit[FD_MAX_UNITS];
239 #define UD unit[drive]
240 #define UDT unit[drive].disktype
241 #define SUD unit[SelectedDrive]
242 #define SUDT unit[SelectedDrive].disktype
245 #define FDC_READ(reg) ({ \
246 /* unsigned long __flags; */ \
247 unsigned short __val; \
248 /* save_flags(__flags); cli(); */ \
249 dma_wd.dma_mode_status = 0x80 | (reg); \
250 udelay(25); \
251 __val = dma_wd.fdc_acces_seccount; \
252 MFPDELAY(); \
253 /* restore_flags(__flags); */ \
254 __val & 0xff; \
257 #define FDC_WRITE(reg,val) \
258 do { \
259 /* unsigned long __flags; */ \
260 /* save_flags(__flags); cli(); */ \
261 dma_wd.dma_mode_status = 0x80 | (reg); \
262 udelay(25); \
263 dma_wd.fdc_acces_seccount = (val); \
264 MFPDELAY(); \
265 /* restore_flags(__flags); */ \
266 } while(0)
269 /* Buffering variables:
270 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
271 * operations. Second, a track buffer is used to cache a whole track
272 * of the disk to save read operations. These are two separate buffers
273 * because that allows write operations without clearing the track buffer.
276 static int MaxSectors[] = {
277 11, 22, 44
279 static int BufferSize[] = {
280 15*512, 30*512, 60*512
283 #define BUFFER_SIZE (BufferSize[DriveType])
285 unsigned char *DMABuffer; /* buffer for writes */
286 static unsigned long PhysDMABuffer; /* physical address */
288 static int UseTrackbuffer = -1; /* Do track buffering? */
289 MODULE_PARM(UseTrackbuffer, "i");
291 unsigned char *TrackBuffer; /* buffer for reads */
292 static unsigned long PhysTrackBuffer; /* physical address */
293 static int BufferDrive, BufferSide, BufferTrack;
294 static int read_track; /* non-zero if we are reading whole tracks */
296 #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
297 #define IS_BUFFERED(drive,side,track) \
298 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
301 * These are global variables, as that's the easiest way to give
302 * information to interrupts. They are the data used for the current
303 * request.
305 static int SelectedDrive = 0;
306 static int ReqCmd, ReqBlock;
307 static int ReqSide, ReqTrack, ReqSector, ReqCnt;
308 static int HeadSettleFlag = 0;
309 static unsigned char *ReqData, *ReqBuffer;
310 static int MotorOn = 0, MotorOffTrys;
311 static int IsFormatting = 0, FormatError;
313 static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
314 MODULE_PARM(UserSteprate, "1-" __MODULE_STRING(FD_MAX_UNITS) "i");
316 /* Synchronization of FDC access. */
317 static volatile int fdc_busy = 0;
318 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
319 static DECLARE_WAIT_QUEUE_HEAD(format_wait);
321 static unsigned int changed_floppies = 0xff, fake_change = 0;
322 #define CHECK_CHANGE_DELAY HZ/2
324 #define FD_MOTOR_OFF_DELAY (3*HZ)
325 #define FD_MOTOR_OFF_MAXTRY (10*20)
327 #define FLOPPY_TIMEOUT (6*HZ)
328 #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
329 * will be recalibrated. */
330 #define MAX_ERRORS 8 /* After this many errors the driver
331 * will give up. */
334 #define START_MOTOR_OFF_TIMER(delay) \
335 do { \
336 motor_off_timer.expires = jiffies + (delay); \
337 add_timer( &motor_off_timer ); \
338 MotorOffTrys = 0; \
339 } while(0)
341 #define START_CHECK_CHANGE_TIMER(delay) \
342 do { \
343 timer_table[FLOPPY_TIMER].expires = jiffies + (delay); \
344 timer_active |= (1 << FLOPPY_TIMER); \
345 } while(0)
347 #define START_TIMEOUT() \
348 mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT)
350 #define STOP_TIMEOUT() \
351 del_timer(&timeout_timer)
355 * The driver is trying to determine the correct media format
356 * while Probing is set. fd_rwsec_done() clears it after a
357 * successful access.
359 static int Probing = 0;
361 /* This flag is set when a dummy seek is necessary to make the WP
362 * status bit accessible.
364 static int NeedSeek = 0;
367 #ifdef DEBUG
368 #define DPRINT(a) printk a
369 #else
370 #define DPRINT(a)
371 #endif
373 /***************************** Prototypes *****************************/
375 static void fd_select_side( int side );
376 static void fd_select_drive( int drive );
377 static void fd_deselect( void );
378 static void fd_motor_off_timer( unsigned long dummy );
379 static void check_change( void );
380 static __inline__ void set_head_settle_flag( void );
381 static __inline__ int get_head_settle_flag( void );
382 static void floppy_irq (int irq, void *dummy, struct pt_regs *fp);
383 static void fd_error( void );
384 static int do_format(kdev_t drive, struct atari_format_descr *desc);
385 static void do_fd_action( int drive );
386 static void fd_calibrate( void );
387 static void fd_calibrate_done( int status );
388 static void fd_seek( void );
389 static void fd_seek_done( int status );
390 static void fd_rwsec( void );
391 static void fd_readtrack_check( unsigned long dummy );
392 static void fd_rwsec_done( int status );
393 static void fd_rwsec_done1(int status);
394 static void fd_writetrack( void );
395 static void fd_writetrack_done( int status );
396 static void fd_times_out( unsigned long dummy );
397 static void finish_fdc( void );
398 static void finish_fdc_done( int dummy );
399 static void floppy_off( unsigned int nr);
400 static __inline__ void copy_buffer( void *from, void *to);
401 static void setup_req_params( int drive );
402 static void redo_fd_request( void);
403 static int invalidate_drive(kdev_t rdev);
404 static int fd_ioctl( struct inode *inode, struct file *filp, unsigned int
405 cmd, unsigned long param);
406 static void fd_probe( int drive );
407 static int fd_test_drive_present( int drive );
408 static void config_types( void );
409 static int floppy_open( struct inode *inode, struct file *filp );
410 static int floppy_release( struct inode * inode, struct file * filp );
412 /************************* End of Prototypes **************************/
414 static struct timer_list motor_off_timer =
415 { NULL, NULL, 0, 0, fd_motor_off_timer };
416 static struct timer_list readtrack_timer =
417 { NULL, NULL, 0, 0, fd_readtrack_check };
419 static struct timer_list timeout_timer =
420 { NULL, NULL, 0, 0, fd_times_out };
424 /* Select the side to use. */
426 static void fd_select_side( int side )
428 unsigned long flags;
430 save_flags(flags);
431 cli(); /* protect against various other ints mucking around with the PSG */
433 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
434 sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
435 sound_ym.rd_data_reg_sel & 0xfe;
437 restore_flags(flags);
441 /* Select a drive, update the FDC's track register and set the correct
442 * clock speed for this disk's type.
445 static void fd_select_drive( int drive )
447 unsigned long flags;
448 unsigned char tmp;
450 if (drive == SelectedDrive)
451 return;
453 save_flags(flags);
454 cli(); /* protect against various other ints mucking around with the PSG */
455 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
456 tmp = sound_ym.rd_data_reg_sel;
457 sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
458 atari_dont_touch_floppy_select = 1;
459 restore_flags(flags);
461 /* restore track register to saved value */
462 FDC_WRITE( FDCREG_TRACK, UD.track );
463 udelay(25);
465 /* select 8/16 MHz */
466 if (UDT)
467 if (ATARIHW_PRESENT(FDCSPEED))
468 dma_wd.fdc_speed = UDT->fdc_speed;
470 SelectedDrive = drive;
474 /* Deselect both drives. */
476 static void fd_deselect( void )
478 unsigned long flags;
480 save_flags(flags);
481 cli(); /* protect against various other ints mucking around with the PSG */
482 atari_dont_touch_floppy_select = 0;
483 sound_ym.rd_data_reg_sel=14; /* Select PSG Port A */
484 sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
485 (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
486 /* On Falcon, the drive B select line is used on the printer port, so
487 * leave it alone... */
488 SelectedDrive = -1;
489 restore_flags(flags);
493 /* This timer function deselects the drives when the FDC switched the
494 * motor off. The deselection cannot happen earlier because the FDC
495 * counts the index signals, which arrive only if one drive is selected.
498 static void fd_motor_off_timer( unsigned long dummy )
500 /* unsigned long flags; */
501 unsigned char status;
502 int delay;
504 del_timer( &motor_off_timer );
506 if (SelectedDrive < 0)
507 /* no drive selected, needn't deselect anyone */
508 return;
510 /* save_flags(flags);
511 cli(); */
513 if (stdma_islocked())
514 goto retry;
516 status = FDC_READ( FDCREG_STATUS );
518 if (!(status & 0x80)) {
519 /* motor already turned off by FDC -> deselect drives */
520 MotorOn = 0;
521 fd_deselect();
522 /* restore_flags(flags); */
523 return;
525 /* not yet off, try again */
527 retry:
528 /* restore_flags(flags); */
529 /* Test again later; if tested too often, it seems there is no disk
530 * in the drive and the FDC will leave the motor on forever (or,
531 * at least until a disk is inserted). So we'll test only twice
532 * per second from then on...
534 delay = (MotorOffTrys < FD_MOTOR_OFF_MAXTRY) ?
535 (++MotorOffTrys, HZ/20) : HZ/2;
536 START_MOTOR_OFF_TIMER( delay );
540 /* This function is repeatedly called to detect disk changes (as good
541 * as possible) and keep track of the current state of the write protection.
544 static void check_change( void )
546 static int drive = 0;
548 unsigned long flags;
549 unsigned char old_porta;
550 int stat;
552 if (++drive > 1 || !UD.connected)
553 drive = 0;
555 save_flags(flags);
556 cli(); /* protect against various other ints mucking around with the PSG */
558 if (!stdma_islocked()) {
559 sound_ym.rd_data_reg_sel = 14;
560 old_porta = sound_ym.rd_data_reg_sel;
561 sound_ym.wd_data = (old_porta | DSKDRVNONE) &
562 ~(drive == 0 ? DSKDRV0 : DSKDRV1);
563 stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
564 sound_ym.wd_data = old_porta;
566 if (stat != UD.wpstat) {
567 DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
568 UD.wpstat = stat;
569 set_bit (drive, &changed_floppies);
572 restore_flags(flags);
574 START_CHECK_CHANGE_TIMER( CHECK_CHANGE_DELAY );
578 /* Handling of the Head Settling Flag: This flag should be set after each
579 * seek operation, because we don't use seeks with verify.
582 static __inline__ void set_head_settle_flag( void )
584 HeadSettleFlag = FDCCMDADD_E;
587 static __inline__ int get_head_settle_flag( void )
589 int tmp = HeadSettleFlag;
590 HeadSettleFlag = 0;
591 return( tmp );
597 /* General Interrupt Handling */
599 static void (*FloppyIRQHandler)( int status ) = NULL;
601 static void floppy_irq (int irq, void *dummy, struct pt_regs *fp)
603 unsigned char status;
604 void (*handler)( int );
606 handler = FloppyIRQHandler;
607 FloppyIRQHandler = NULL;
609 if (handler) {
610 nop();
611 status = FDC_READ( FDCREG_STATUS );
612 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
613 handler( status );
615 else {
616 DPRINT(("FDC irq, no handler\n"));
621 /* Error handling: If some error happened, retry some times, then
622 * recalibrate, then try again, and fail after MAX_ERRORS.
625 static void fd_error( void )
627 if (IsFormatting) {
628 IsFormatting = 0;
629 FormatError = 1;
630 wake_up( &format_wait );
631 return;
634 if (!CURRENT) return;
635 CURRENT->errors++;
636 if (CURRENT->errors >= MAX_ERRORS) {
637 printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
638 end_request( 0 );
640 else if (CURRENT->errors == RECALIBRATE_ERRORS) {
641 printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
642 if (SelectedDrive != -1)
643 SUD.track = -1;
645 redo_fd_request();
650 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
653 /* ---------- Formatting ---------- */
655 #define FILL(n,val) \
656 do { \
657 memset( p, val, n ); \
658 p += n; \
659 } while(0)
661 static int do_format(kdev_t device, struct atari_format_descr *desc)
663 unsigned char *p;
664 int sect, nsect;
665 unsigned long flags;
666 int type, drive = MINOR(device) & 3;
668 DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
669 drive, desc->track, desc->head, desc->sect_offset ));
671 save_flags(flags);
672 cli();
673 while( fdc_busy ) sleep_on( &fdc_wait );
674 fdc_busy = 1;
675 stdma_lock(floppy_irq, NULL);
676 atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
677 restore_flags(flags);
679 type = MINOR(device) >> 2;
680 if (type) {
681 if (--type >= NUM_DISK_MINORS ||
682 minor2disktype[type].drive_types > DriveType) {
683 redo_fd_request();
684 return -EINVAL;
686 type = minor2disktype[type].index;
687 UDT = &disk_type[type];
690 if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
691 redo_fd_request();
692 return -EINVAL;
695 nsect = UDT->spt;
696 p = TrackBuffer;
697 /* The track buffer is used for the raw track data, so its
698 contents become invalid! */
699 BufferDrive = -1;
700 /* stop deselect timer */
701 del_timer( &motor_off_timer );
703 FILL( 60 * (nsect / 9), 0x4e );
704 for( sect = 0; sect < nsect; ++sect ) {
705 FILL( 12, 0 );
706 FILL( 3, 0xf5 );
707 *p++ = 0xfe;
708 *p++ = desc->track;
709 *p++ = desc->head;
710 *p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
711 *p++ = 2;
712 *p++ = 0xf7;
713 FILL( 22, 0x4e );
714 FILL( 12, 0 );
715 FILL( 3, 0xf5 );
716 *p++ = 0xfb;
717 FILL( 512, 0xe5 );
718 *p++ = 0xf7;
719 FILL( 40, 0x4e );
721 FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
723 IsFormatting = 1;
724 FormatError = 0;
725 ReqTrack = desc->track;
726 ReqSide = desc->head;
727 do_fd_action( drive );
729 sleep_on( &format_wait );
731 redo_fd_request();
732 return( FormatError ? -EIO : 0 );
736 /* do_fd_action() is the general procedure for a fd request: All
737 * required parameter settings (drive select, side select, track
738 * position) are checked and set if needed. For each of these
739 * parameters and the actual reading or writing exist two functions:
740 * one that starts the setting (or skips it if possible) and one
741 * callback for the "done" interrupt. Each done func calls the next
742 * set function to propagate the request down to fd_rwsec_done().
745 static void do_fd_action( int drive )
747 DPRINT(("do_fd_action\n"));
749 if (UseTrackbuffer && !IsFormatting) {
750 repeat:
751 if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
752 if (ReqCmd == READ) {
753 copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
754 if (++ReqCnt < CURRENT->current_nr_sectors) {
755 /* read next sector */
756 setup_req_params( drive );
757 goto repeat;
759 else {
760 /* all sectors finished */
761 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
762 CURRENT->sector += CURRENT->current_nr_sectors;
763 end_request( 1 );
764 redo_fd_request();
765 return;
768 else {
769 /* cmd == WRITE, pay attention to track buffer
770 * consistency! */
771 copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
776 if (SelectedDrive != drive)
777 fd_select_drive( drive );
779 if (UD.track == -1)
780 fd_calibrate();
781 else if (UD.track != ReqTrack << UDT->stretch)
782 fd_seek();
783 else if (IsFormatting)
784 fd_writetrack();
785 else
786 fd_rwsec();
790 /* Seek to track 0 if the current track is unknown */
792 static void fd_calibrate( void )
794 if (SUD.track >= 0) {
795 fd_calibrate_done( 0 );
796 return;
799 if (ATARIHW_PRESENT(FDCSPEED))
800 dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
801 DPRINT(("fd_calibrate\n"));
802 SET_IRQ_HANDLER( fd_calibrate_done );
803 /* we can't verify, since the speed may be incorrect */
804 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
806 NeedSeek = 1;
807 MotorOn = 1;
808 START_TIMEOUT();
809 /* wait for IRQ */
813 static void fd_calibrate_done( int status )
815 DPRINT(("fd_calibrate_done()\n"));
816 STOP_TIMEOUT();
818 /* set the correct speed now */
819 if (ATARIHW_PRESENT(FDCSPEED))
820 dma_wd.fdc_speed = SUDT->fdc_speed;
821 if (status & FDCSTAT_RECNF) {
822 printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
823 fd_error();
825 else {
826 SUD.track = 0;
827 fd_seek();
832 /* Seek the drive to the requested track. The drive must have been
833 * calibrated at some point before this.
836 static void fd_seek( void )
838 if (SUD.track == ReqTrack << SUDT->stretch) {
839 fd_seek_done( 0 );
840 return;
843 if (ATARIHW_PRESENT(FDCSPEED)) {
844 dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
845 MFPDELAY();
848 DPRINT(("fd_seek() to track %d\n",ReqTrack));
849 FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
850 udelay(25);
851 SET_IRQ_HANDLER( fd_seek_done );
852 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
854 MotorOn = 1;
855 set_head_settle_flag();
856 START_TIMEOUT();
857 /* wait for IRQ */
861 static void fd_seek_done( int status )
863 DPRINT(("fd_seek_done()\n"));
864 STOP_TIMEOUT();
866 /* set the correct speed */
867 if (ATARIHW_PRESENT(FDCSPEED))
868 dma_wd.fdc_speed = SUDT->fdc_speed;
869 if (status & FDCSTAT_RECNF) {
870 printk(KERN_ERR "fd%d: seek error (to track %d)\n",
871 SelectedDrive, ReqTrack );
872 /* we don't know exactly which track we are on now! */
873 SUD.track = -1;
874 fd_error();
876 else {
877 SUD.track = ReqTrack << SUDT->stretch;
878 NeedSeek = 0;
879 if (IsFormatting)
880 fd_writetrack();
881 else
882 fd_rwsec();
887 /* This does the actual reading/writing after positioning the head
888 * over the correct track.
891 static int MultReadInProgress = 0;
894 static void fd_rwsec( void )
896 unsigned long paddr, flags;
897 unsigned int rwflag, old_motoron;
898 unsigned int track;
900 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
901 if (ReqCmd == WRITE) {
902 if (ATARIHW_PRESENT(EXTD_DMA)) {
903 paddr = (unsigned long)VTOP(ReqData);
905 else {
906 copy_buffer( ReqData, DMABuffer );
907 paddr = PhysDMABuffer;
909 dma_cache_maintenance( paddr, 512, 1 );
910 rwflag = 0x100;
912 else {
913 if (read_track)
914 paddr = PhysTrackBuffer;
915 else
916 paddr = ATARIHW_PRESENT(EXTD_DMA) ? VTOP(ReqData) : PhysDMABuffer;
917 rwflag = 0;
920 fd_select_side( ReqSide );
922 /* Start sector of this operation */
923 FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
924 MFPDELAY();
925 /* Cheat for track if stretch != 0 */
926 if (SUDT->stretch) {
927 track = FDC_READ( FDCREG_TRACK);
928 MFPDELAY();
929 FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
931 udelay(25);
933 /* Setup DMA */
934 save_flags(flags);
935 cli();
936 dma_wd.dma_lo = (unsigned char)paddr;
937 MFPDELAY();
938 paddr >>= 8;
939 dma_wd.dma_md = (unsigned char)paddr;
940 MFPDELAY();
941 paddr >>= 8;
942 if (ATARIHW_PRESENT(EXTD_DMA))
943 st_dma_ext_dmahi = (unsigned short)paddr;
944 else
945 dma_wd.dma_hi = (unsigned char)paddr;
946 MFPDELAY();
947 restore_flags(flags);
949 /* Clear FIFO and switch DMA to correct mode */
950 dma_wd.dma_mode_status = 0x90 | rwflag;
951 MFPDELAY();
952 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
953 MFPDELAY();
954 dma_wd.dma_mode_status = 0x90 | rwflag;
955 MFPDELAY();
957 /* How many sectors for DMA */
958 dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
960 udelay(25);
962 /* Start operation */
963 dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
964 udelay(25);
965 SET_IRQ_HANDLER( fd_rwsec_done );
966 dma_wd.fdc_acces_seccount =
967 (get_head_settle_flag() |
968 (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
970 old_motoron = MotorOn;
971 MotorOn = 1;
972 NeedSeek = 1;
973 /* wait for interrupt */
975 if (read_track) {
976 /* If reading a whole track, wait about one disk rotation and
977 * then check if all sectors are read. The FDC will even
978 * search for the first non-existent sector and need 1 sec to
979 * recognise that it isn't present :-(
981 del_timer (&readtrack_timer);
982 readtrack_timer.expires =
983 jiffies + HZ/5 + (old_motoron ? 0 : HZ);
984 /* 1 rot. + 5 rot.s if motor was off */
985 MultReadInProgress = 1;
986 add_timer( &readtrack_timer );
988 START_TIMEOUT();
992 static void fd_readtrack_check( unsigned long dummy )
994 unsigned long flags, addr, addr2;
996 save_flags(flags);
997 cli();
999 del_timer( &readtrack_timer );
1001 if (!MultReadInProgress) {
1002 /* This prevents a race condition that could arise if the
1003 * interrupt is triggered while the calling of this timer
1004 * callback function takes place. The IRQ function then has
1005 * already cleared 'MultReadInProgress' when flow of control
1006 * gets here.
1008 restore_flags(flags);
1009 return;
1012 /* get the current DMA address */
1013 /* ++ f.a. read twice to avoid being fooled by switcher */
1014 addr = 0;
1015 do {
1016 addr2 = addr;
1017 addr = dma_wd.dma_lo & 0xff;
1018 MFPDELAY();
1019 addr |= (dma_wd.dma_md & 0xff) << 8;
1020 MFPDELAY();
1021 if (ATARIHW_PRESENT( EXTD_DMA ))
1022 addr |= (st_dma_ext_dmahi & 0xffff) << 16;
1023 else
1024 addr |= (dma_wd.dma_hi & 0xff) << 16;
1025 MFPDELAY();
1026 } while(addr != addr2);
1028 if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1029 /* already read enough data, force an FDC interrupt to stop
1030 * the read operation
1032 SET_IRQ_HANDLER( NULL );
1033 MultReadInProgress = 0;
1034 restore_flags(flags);
1035 DPRINT(("fd_readtrack_check(): done\n"));
1036 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1037 udelay(25);
1039 /* No error until now -- the FDC would have interrupted
1040 * otherwise!
1042 fd_rwsec_done1(0);
1044 else {
1045 /* not yet finished, wait another tenth rotation */
1046 restore_flags(flags);
1047 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1048 readtrack_timer.expires = jiffies + HZ/5/10;
1049 add_timer( &readtrack_timer );
1054 static void fd_rwsec_done( int status )
1056 DPRINT(("fd_rwsec_done()\n"));
1058 if (read_track) {
1059 del_timer(&readtrack_timer);
1060 if (!MultReadInProgress)
1061 return;
1062 MultReadInProgress = 0;
1064 fd_rwsec_done1(status);
1067 static void fd_rwsec_done1(int status)
1069 unsigned int track;
1071 STOP_TIMEOUT();
1073 /* Correct the track if stretch != 0 */
1074 if (SUDT->stretch) {
1075 track = FDC_READ( FDCREG_TRACK);
1076 MFPDELAY();
1077 FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1080 if (!UseTrackbuffer) {
1081 dma_wd.dma_mode_status = 0x90;
1082 MFPDELAY();
1083 if (!(dma_wd.dma_mode_status & 0x01)) {
1084 printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1085 goto err_end;
1088 MFPDELAY();
1090 if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1091 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1092 goto err_end;
1094 if ((status & FDCSTAT_RECNF) &&
1095 /* RECNF is no error after a multiple read when the FDC
1096 searched for a non-existent sector! */
1097 !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1098 if (Probing) {
1099 if (SUDT > disk_type) {
1100 if (SUDT[-1].blocks > ReqBlock) {
1101 /* try another disk type */
1102 SUDT--;
1103 floppy_sizes[SelectedDrive] = SUDT->blocks >> 1;
1104 } else
1105 Probing = 0;
1107 else {
1108 if (SUD.flags & FTD_MSG)
1109 printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1110 SelectedDrive, SUDT->name );
1111 Probing=0;
1113 } else {
1114 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1115 if (SUD.autoprobe) {
1116 SUDT = disk_type + StartDiskType[DriveType];
1117 floppy_sizes[SelectedDrive] = SUDT->blocks >> 1;
1118 Probing = 1;
1121 if (Probing) {
1122 if (ATARIHW_PRESENT(FDCSPEED)) {
1123 dma_wd.fdc_speed = SUDT->fdc_speed;
1124 MFPDELAY();
1126 setup_req_params( SelectedDrive );
1127 BufferDrive = -1;
1128 do_fd_action( SelectedDrive );
1129 return;
1132 printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1133 SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1134 goto err_end;
1136 if (status & FDCSTAT_CRC) {
1137 printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1138 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1139 goto err_end;
1141 if (status & FDCSTAT_LOST) {
1142 printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1143 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1144 goto err_end;
1147 Probing = 0;
1149 if (ReqCmd == READ) {
1150 if (!read_track) {
1151 void *addr;
1152 addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1153 dma_cache_maintenance( VTOP(addr), 512, 0 );
1154 if (!ATARIHW_PRESENT( EXTD_DMA ))
1155 copy_buffer (addr, ReqData);
1156 } else {
1157 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1158 BufferDrive = SelectedDrive;
1159 BufferSide = ReqSide;
1160 BufferTrack = ReqTrack;
1161 copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1165 if (++ReqCnt < CURRENT->current_nr_sectors) {
1166 /* read next sector */
1167 setup_req_params( SelectedDrive );
1168 do_fd_action( SelectedDrive );
1170 else {
1171 /* all sectors finished */
1172 CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
1173 CURRENT->sector += CURRENT->current_nr_sectors;
1174 end_request( 1 );
1175 redo_fd_request();
1177 return;
1179 err_end:
1180 BufferDrive = -1;
1181 fd_error();
1185 static void fd_writetrack( void )
1187 unsigned long paddr, flags;
1188 unsigned int track;
1190 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1192 paddr = PhysTrackBuffer;
1193 dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1195 fd_select_side( ReqSide );
1197 /* Cheat for track if stretch != 0 */
1198 if (SUDT->stretch) {
1199 track = FDC_READ( FDCREG_TRACK);
1200 MFPDELAY();
1201 FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1203 udelay(40);
1205 /* Setup DMA */
1206 save_flags(flags);
1207 cli();
1208 dma_wd.dma_lo = (unsigned char)paddr;
1209 MFPDELAY();
1210 paddr >>= 8;
1211 dma_wd.dma_md = (unsigned char)paddr;
1212 MFPDELAY();
1213 paddr >>= 8;
1214 if (ATARIHW_PRESENT( EXTD_DMA ))
1215 st_dma_ext_dmahi = (unsigned short)paddr;
1216 else
1217 dma_wd.dma_hi = (unsigned char)paddr;
1218 MFPDELAY();
1219 restore_flags(flags);
1221 /* Clear FIFO and switch DMA to correct mode */
1222 dma_wd.dma_mode_status = 0x190;
1223 MFPDELAY();
1224 dma_wd.dma_mode_status = 0x90;
1225 MFPDELAY();
1226 dma_wd.dma_mode_status = 0x190;
1227 MFPDELAY();
1229 /* How many sectors for DMA */
1230 dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1231 udelay(40);
1233 /* Start operation */
1234 dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1235 udelay(40);
1236 SET_IRQ_HANDLER( fd_writetrack_done );
1237 dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1239 MotorOn = 1;
1240 START_TIMEOUT();
1241 /* wait for interrupt */
1245 static void fd_writetrack_done( int status )
1247 DPRINT(("fd_writetrack_done()\n"));
1249 STOP_TIMEOUT();
1251 if (status & FDCSTAT_WPROT) {
1252 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1253 goto err_end;
1255 if (status & FDCSTAT_LOST) {
1256 printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1257 SelectedDrive, ReqSide, ReqTrack );
1258 goto err_end;
1261 wake_up( &format_wait );
1262 return;
1264 err_end:
1265 fd_error();
1268 static void fd_times_out( unsigned long dummy )
1270 atari_disable_irq( IRQ_MFP_FDC );
1271 if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1272 * before we came here... */
1274 SET_IRQ_HANDLER( NULL );
1275 /* If the timeout occurred while the readtrack_check timer was
1276 * active, we need to cancel it, else bad things will happen */
1277 if (UseTrackbuffer)
1278 del_timer( &readtrack_timer );
1279 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1280 udelay( 25 );
1282 printk(KERN_ERR "floppy timeout\n" );
1283 fd_error();
1284 end:
1285 atari_enable_irq( IRQ_MFP_FDC );
1289 /* The (noop) seek operation here is needed to make the WP bit in the
1290 * FDC status register accessible for check_change. If the last disk
1291 * operation would have been a RDSEC, this bit would always read as 0
1292 * no matter what :-( To save time, the seek goes to the track we're
1293 * already on.
1296 static void finish_fdc( void )
1298 if (!NeedSeek) {
1299 finish_fdc_done( 0 );
1301 else {
1302 DPRINT(("finish_fdc: dummy seek started\n"));
1303 FDC_WRITE (FDCREG_DATA, SUD.track);
1304 SET_IRQ_HANDLER( finish_fdc_done );
1305 FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1306 MotorOn = 1;
1307 START_TIMEOUT();
1308 /* we must wait for the IRQ here, because the ST-DMA
1309 is released immediately afterwards and the interrupt
1310 may be delivered to the wrong driver. */
1315 static void finish_fdc_done( int dummy )
1317 unsigned long flags;
1319 DPRINT(("finish_fdc_done entered\n"));
1320 STOP_TIMEOUT();
1321 NeedSeek = 0;
1323 if ((timer_active & (1 << FLOPPY_TIMER)) &&
1324 timer_table[FLOPPY_TIMER].expires < jiffies + 5)
1325 /* If the check for a disk change is done too early after this
1326 * last seek command, the WP bit still reads wrong :-((
1328 timer_table[FLOPPY_TIMER].expires = jiffies + 5;
1329 else
1330 START_CHECK_CHANGE_TIMER( CHECK_CHANGE_DELAY );
1331 del_timer( &motor_off_timer );
1332 START_MOTOR_OFF_TIMER( FD_MOTOR_OFF_DELAY );
1334 save_flags(flags);
1335 cli();
1336 stdma_release();
1337 fdc_busy = 0;
1338 wake_up( &fdc_wait );
1339 restore_flags(flags);
1341 DPRINT(("finish_fdc() finished\n"));
1345 /* Prevent "aliased" accesses. */
1346 static int fd_ref[4] = { 0,0,0,0 };
1347 static int fd_device[4] = { 0,0,0,0 };
1350 * Current device number. Taken either from the block header or from the
1351 * format request descriptor.
1353 #define CURRENT_DEVICE (CURRENT->rq_dev)
1355 /* Current error count. */
1356 #define CURRENT_ERRORS (CURRENT->errors)
1359 /* dummy for blk.h */
1360 static void floppy_off( unsigned int nr) {}
1363 /* The detection of disk changes is a dark chapter in Atari history :-(
1364 * Because the "Drive ready" signal isn't present in the Atari
1365 * hardware, one has to rely on the "Write Protect". This works fine,
1366 * as long as no write protected disks are used. TOS solves this
1367 * problem by introducing tri-state logic ("maybe changed") and
1368 * looking at the serial number in block 0. This isn't possible for
1369 * Linux, since the floppy driver can't make assumptions about the
1370 * filesystem used on the disk and thus the contents of block 0. I've
1371 * chosen the method to always say "The disk was changed" if it is
1372 * unsure whether it was. This implies that every open or mount
1373 * invalidates the disk buffers if you work with write protected
1374 * disks. But at least this is better than working with incorrect data
1375 * due to unrecognised disk changes.
1378 static int check_floppy_change (kdev_t dev)
1380 unsigned int drive = MINOR(dev) & 0x03;
1382 if (MAJOR(dev) != MAJOR_NR) {
1383 printk(KERN_ERR "floppy_changed: not a floppy\n");
1384 return 0;
1387 if (test_bit (drive, &fake_change)) {
1388 /* simulated change (e.g. after formatting) */
1389 return 1;
1391 if (test_bit (drive, &changed_floppies)) {
1392 /* surely changed (the WP signal changed at least once) */
1393 return 1;
1395 if (UD.wpstat) {
1396 /* WP is on -> could be changed: to be sure, buffers should be
1397 * invalidated...
1399 return 1;
1402 return 0;
1405 static int floppy_revalidate (kdev_t dev)
1407 int drive = MINOR(dev) & 3;
1409 if (test_bit(drive, &changed_floppies) ||
1410 test_bit(drive, &fake_change) ||
1411 unit[drive].disktype == 0) {
1412 if (UD.flags & FTD_MSG)
1413 printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1414 BufferDrive = -1;
1415 clear_bit(drive, &fake_change);
1416 clear_bit(drive, &changed_floppies);
1417 /* MSch: clearing geometry makes sense only for autoprobe
1418 formats, for 'permanent user-defined' parameter:
1419 restore default_params[] here if flagged valid! */
1420 if (default_params[drive].blocks == 0)
1421 UDT = 0;
1422 else
1423 UDT = &default_params[drive];
1425 return 0;
1428 static __inline__ void copy_buffer(void *from, void *to)
1430 ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
1431 int cnt;
1433 for( cnt = 512/4; cnt; cnt-- )
1434 *p2++ = *p1++;
1438 /* This sets up the global variables describing the current request. */
1440 static void setup_req_params( int drive )
1442 int block = ReqBlock + ReqCnt;
1444 ReqTrack = block / UDT->spt;
1445 ReqSector = block - ReqTrack * UDT->spt + 1;
1446 ReqSide = ReqTrack & 1;
1447 ReqTrack >>= 1;
1448 ReqData = ReqBuffer + 512 * ReqCnt;
1450 if (UseTrackbuffer)
1451 read_track = (ReqCmd == READ && CURRENT_ERRORS == 0);
1452 else
1453 read_track = 0;
1455 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1456 ReqTrack, ReqSector, (unsigned long)ReqData ));
1460 static void redo_fd_request(void)
1462 int device, drive, type;
1464 DPRINT(("redo_fd_request: CURRENT=%08lx CURRENT->dev=%04x CURRENT->sector=%ld\n",
1465 (unsigned long)CURRENT, CURRENT ? CURRENT->rq_dev : 0,
1466 CURRENT ? CURRENT->sector : 0 ));
1468 IsFormatting = 0;
1470 if (CURRENT && CURRENT->rq_status == RQ_INACTIVE){
1471 return;
1474 repeat:
1476 if (!CURRENT)
1477 goto the_end;
1479 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
1480 panic(DEVICE_NAME ": request list destroyed");
1482 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
1483 panic(DEVICE_NAME ": block not locked");
1485 device = MINOR(CURRENT_DEVICE);
1486 drive = device & 3;
1487 type = device >> 2;
1489 if (!UD.connected) {
1490 /* drive not connected */
1491 printk(KERN_ERR "Unknown Device: fd%d\n", drive );
1492 end_request(0);
1493 goto repeat;
1496 if (type == 0) {
1497 if (!UDT) {
1498 Probing = 1;
1499 UDT = disk_type + StartDiskType[DriveType];
1500 floppy_sizes[drive] = UDT->blocks >> 1;
1501 UD.autoprobe = 1;
1504 else {
1505 /* user supplied disk type */
1506 if (--type >= NUM_DISK_MINORS) {
1507 printk(KERN_WARNING "fd%d: invalid disk format", drive );
1508 end_request( 0 );
1509 goto repeat;
1511 if (minor2disktype[type].drive_types > DriveType) {
1512 printk(KERN_WARNING "fd%d: unsupported disk format", drive );
1513 end_request( 0 );
1514 goto repeat;
1516 type = minor2disktype[type].index;
1517 UDT = &disk_type[type];
1518 floppy_sizes[drive] = UDT->blocks >> 1;
1519 UD.autoprobe = 0;
1522 if (CURRENT->sector + 1 > UDT->blocks) {
1523 end_request(0);
1524 goto repeat;
1527 /* stop deselect timer */
1528 del_timer( &motor_off_timer );
1530 ReqCnt = 0;
1531 ReqCmd = CURRENT->cmd;
1532 ReqBlock = CURRENT->sector;
1533 ReqBuffer = CURRENT->buffer;
1534 setup_req_params( drive );
1535 do_fd_action( drive );
1537 return;
1539 the_end:
1540 finish_fdc();
1544 void do_fd_request(void)
1546 unsigned long flags;
1548 DPRINT(("do_fd_request for pid %d\n",current->pid));
1549 while( fdc_busy ) sleep_on( &fdc_wait );
1550 fdc_busy = 1;
1551 stdma_lock(floppy_irq, NULL);
1553 atari_disable_irq( IRQ_MFP_FDC );
1554 save_flags(flags); /* The request function is called with ints
1555 sti(); * disabled... so must save the IPL for later */
1556 redo_fd_request();
1557 restore_flags(flags);
1558 atari_enable_irq( IRQ_MFP_FDC );
1562 static int invalidate_drive(kdev_t rdev)
1564 /* invalidate the buffer track to force a reread */
1565 BufferDrive = -1;
1566 set_bit(MINOR(rdev) & 3, &fake_change);
1567 check_disk_change(rdev);
1568 return 0;
1571 static int fd_ioctl(struct inode *inode, struct file *filp,
1572 unsigned int cmd, unsigned long param)
1574 #define IOCTL_MODE_BIT 8
1575 #define OPEN_WRITE_BIT 16
1576 #define IOCTL_ALLOWED (filp && (filp->f_mode & IOCTL_MODE_BIT))
1578 int drive, type;
1579 kdev_t device;
1580 struct atari_format_descr fmt_desc;
1581 struct atari_disk_type *dtp;
1582 struct floppy_struct getprm;
1583 int settype;
1584 struct floppy_struct setprm;
1586 device = inode->i_rdev;
1587 switch (cmd) {
1588 case BLKROSET:
1589 case BLKROGET:
1590 case BLKRASET:
1591 case BLKRAGET:
1592 case BLKFLSBUF:
1593 return blk_ioctl(device, cmd, param);
1595 drive = MINOR (device);
1596 type = drive >> 2;
1597 drive &= 3;
1598 switch (cmd) {
1599 case FDGETPRM:
1600 case BLKGETSIZE:
1601 if (type) {
1602 if (--type >= NUM_DISK_MINORS)
1603 return -ENODEV;
1604 if (minor2disktype[type].drive_types > DriveType)
1605 return -ENODEV;
1606 type = minor2disktype[type].index;
1607 dtp = &disk_type[type];
1608 if (UD.flags & FTD_MSG)
1609 printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1610 drive, dtp, dtp->name);
1612 else {
1613 if (!UDT)
1614 return -ENXIO;
1615 else
1616 dtp = UDT;
1618 if (cmd == BLKGETSIZE)
1619 return put_user(dtp->blocks, (long *)param);
1621 memset((void *)&getprm, 0, sizeof(getprm));
1622 getprm.size = dtp->blocks;
1623 getprm.sect = dtp->spt;
1624 getprm.head = 2;
1625 getprm.track = dtp->blocks/dtp->spt/2;
1626 getprm.stretch = dtp->stretch;
1627 if (copy_to_user((void *)param, &getprm, sizeof(getprm)))
1628 return -EFAULT;
1629 return 0;
1631 if (!IOCTL_ALLOWED)
1632 return -EPERM;
1633 switch (cmd) {
1634 case FDSETPRM:
1635 case FDDEFPRM:
1637 * MSch 7/96: simple 'set geometry' case: just set the
1638 * 'default' device params (minor == 0).
1639 * Currently, the drive geometry is cleared after each
1640 * disk change and subsequent revalidate()! simple
1641 * implementation of FDDEFPRM: save geometry from a
1642 * FDDEFPRM call and restore it in floppy_revalidate() !
1645 /* get the parameters from user space */
1646 if (fd_ref[drive] != 1 && fd_ref[drive] != -1)
1647 return -EBUSY;
1648 if (copy_from_user(&setprm, (void *) param, sizeof(setprm)))
1649 return -EFAULT;
1651 * first of all: check for floppy change and revalidate,
1652 * or the next access will revalidate - and clear UDT :-(
1655 if (check_floppy_change(device))
1656 floppy_revalidate(device);
1658 if (UD.flags & FTD_MSG)
1659 printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1660 drive, setprm.size, setprm.sect, setprm.stretch);
1662 /* what if type > 0 here? Overwrite specified entry ? */
1663 if (type) {
1664 /* refuse to re-set a predefined type for now */
1665 redo_fd_request();
1666 return -EINVAL;
1670 * type == 0: first look for a matching entry in the type list,
1671 * and set the UD.disktype field to use the perdefined entry.
1672 * TODO: add user-defined format to head of autoprobe list ?
1673 * Useful to include the user-type for future autodetection!
1676 for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1677 int setidx = 0;
1678 if (minor2disktype[settype].drive_types > DriveType) {
1679 /* skip this one, invalid for drive ... */
1680 continue;
1682 setidx = minor2disktype[settype].index;
1683 dtp = &disk_type[setidx];
1685 /* found matching entry ?? */
1686 if ( dtp->blocks == setprm.size
1687 && dtp->spt == setprm.sect
1688 && dtp->stretch == setprm.stretch ) {
1689 if (UD.flags & FTD_MSG)
1690 printk (KERN_INFO "floppy%d: setting %s %p!\n",
1691 drive, dtp->name, dtp);
1692 UDT = dtp;
1693 floppy_sizes[drive] = UDT->blocks >> 1;
1695 if (cmd == FDDEFPRM) {
1696 /* save settings as permanent default type */
1697 default_params[drive].name = dtp->name;
1698 default_params[drive].spt = dtp->spt;
1699 default_params[drive].blocks = dtp->blocks;
1700 default_params[drive].fdc_speed = dtp->fdc_speed;
1701 default_params[drive].stretch = dtp->stretch;
1704 return 0;
1709 /* no matching disk type found above - setting user_params */
1711 if (cmd == FDDEFPRM) {
1712 /* set permanent type */
1713 dtp = &default_params[drive];
1714 } else
1715 /* set user type (reset by disk change!) */
1716 dtp = &user_params[drive];
1718 dtp->name = "user format";
1719 dtp->blocks = setprm.size;
1720 dtp->spt = setprm.sect;
1721 if (setprm.sect > 14)
1722 dtp->fdc_speed = 3;
1723 else
1724 dtp->fdc_speed = 0;
1725 dtp->stretch = setprm.stretch;
1727 if (UD.flags & FTD_MSG)
1728 printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1729 drive, dtp->blocks, dtp->spt, dtp->stretch);
1731 /* sanity check */
1732 if (!dtp || setprm.track != dtp->blocks/dtp->spt/2 ||
1733 setprm.head != 2) {
1734 redo_fd_request();
1735 return -EINVAL;
1738 UDT = dtp;
1739 floppy_sizes[drive] = UDT->blocks >> 1;
1741 return 0;
1742 case FDMSGON:
1743 UD.flags |= FTD_MSG;
1744 return 0;
1745 case FDMSGOFF:
1746 UD.flags &= ~FTD_MSG;
1747 return 0;
1748 case FDSETEMSGTRESH:
1749 return -EINVAL;
1750 case FDFMTBEG:
1751 return 0;
1752 case FDFMTTRK:
1753 if (fd_ref[drive] != 1 && fd_ref[drive] != -1)
1754 return -EBUSY;
1755 if (copy_from_user(&fmt_desc, (void *) param, sizeof(fmt_desc)))
1756 return -EFAULT;
1757 return do_format(device, &fmt_desc);
1758 case FDCLRPRM:
1759 UDT = NULL;
1760 /* MSch: invalidate default_params */
1761 default_params[drive].blocks = 0;
1762 floppy_sizes[drive] = MAX_DISK_SIZE;
1763 return invalidate_drive (device);
1764 case FDFMTEND:
1765 case FDFLUSH:
1766 return invalidate_drive(device);
1768 return -EINVAL;
1772 /* Initialize the 'unit' variable for drive 'drive' */
1774 __initfunc(static void fd_probe( int drive ))
1776 UD.connected = 0;
1777 UDT = NULL;
1779 if (!fd_test_drive_present( drive ))
1780 return;
1782 UD.connected = 1;
1783 UD.track = 0;
1784 switch( UserSteprate[drive] ) {
1785 case 2:
1786 UD.steprate = FDCSTEP_2;
1787 break;
1788 case 3:
1789 UD.steprate = FDCSTEP_3;
1790 break;
1791 case 6:
1792 UD.steprate = FDCSTEP_6;
1793 break;
1794 case 12:
1795 UD.steprate = FDCSTEP_12;
1796 break;
1797 default: /* should be -1 for "not set by user" */
1798 if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1799 UD.steprate = FDCSTEP_3;
1800 else
1801 UD.steprate = FDCSTEP_6;
1802 break;
1804 MotorOn = 1; /* from probe restore operation! */
1808 /* This function tests the physical presence of a floppy drive (not
1809 * whether a disk is inserted). This is done by issuing a restore
1810 * command, waiting max. 2 seconds (that should be enough to move the
1811 * head across the whole disk) and looking at the state of the "TR00"
1812 * signal. This should now be raised if there is a drive connected
1813 * (and there is no hardware failure :-) Otherwise, the drive is
1814 * declared absent.
1817 __initfunc(static int fd_test_drive_present( int drive ))
1819 unsigned long timeout;
1820 unsigned char status;
1821 int ok;
1823 if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1824 fd_select_drive( drive );
1826 /* disable interrupt temporarily */
1827 atari_turnoff_irq( IRQ_MFP_FDC );
1828 FDC_WRITE (FDCREG_TRACK, 0xff00);
1829 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1831 for( ok = 0, timeout = jiffies + 2*HZ+HZ/2; time_before(jiffies, timeout); ) {
1832 if (!(mfp.par_dt_reg & 0x20))
1833 break;
1836 status = FDC_READ( FDCREG_STATUS );
1837 ok = (status & FDCSTAT_TR00) != 0;
1839 /* force interrupt to abort restore operation (FDC would try
1840 * about 50 seconds!) */
1841 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1842 udelay(500);
1843 status = FDC_READ( FDCREG_STATUS );
1844 udelay(20);
1846 if (ok) {
1847 /* dummy seek command to make WP bit accessible */
1848 FDC_WRITE( FDCREG_DATA, 0 );
1849 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
1850 while( mfp.par_dt_reg & 0x20 )
1852 status = FDC_READ( FDCREG_STATUS );
1855 atari_turnon_irq( IRQ_MFP_FDC );
1856 return( ok );
1860 /* Look how many and which kind of drives are connected. If there are
1861 * floppies, additionally start the disk-change and motor-off timers.
1864 __initfunc(static void config_types( void ))
1866 int drive, cnt = 0;
1868 /* for probing drives, set the FDC speed to 8 MHz */
1869 if (ATARIHW_PRESENT(FDCSPEED))
1870 dma_wd.fdc_speed = 0;
1872 printk(KERN_INFO "Probing floppy drive(s):\n");
1873 for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1874 fd_probe( drive );
1875 if (UD.connected) {
1876 printk(KERN_INFO "fd%d\n", drive);
1877 ++cnt;
1881 if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1882 /* If FDC is still busy from probing, give it another FORCI
1883 * command to abort the operation. If this isn't done, the FDC
1884 * will interrupt later and its IRQ line stays low, because
1885 * the status register isn't read. And this will block any
1886 * interrupts on this IRQ line :-(
1888 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1889 udelay(500);
1890 FDC_READ( FDCREG_STATUS );
1891 udelay(20);
1894 if (cnt > 0) {
1895 START_MOTOR_OFF_TIMER( FD_MOTOR_OFF_DELAY );
1896 if (cnt == 1) fd_select_drive( 0 );
1897 START_CHECK_CHANGE_TIMER( CHECK_CHANGE_DELAY );
1902 * floppy_open check for aliasing (/dev/fd0 can be the same as
1903 * /dev/PS0 etc), and disallows simultaneous access to the same
1904 * drive with different device numbers.
1907 static int floppy_open( struct inode *inode, struct file *filp )
1909 int drive, type;
1910 int old_dev;
1912 if (!filp) {
1913 DPRINT (("Weird, open called with filp=0\n"));
1914 return -EIO;
1917 drive = MINOR(inode->i_rdev) & 3;
1918 type = MINOR(inode->i_rdev) >> 2;
1919 DPRINT(("fd_open: type=%d\n",type));
1920 if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1921 return -ENXIO;
1923 old_dev = fd_device[drive];
1925 if (fd_ref[drive] && old_dev != MINOR(inode->i_rdev))
1926 return -EBUSY;
1928 if (fd_ref[drive] == -1 || (fd_ref[drive] && filp->f_flags & O_EXCL))
1929 return -EBUSY;
1931 MOD_INC_USE_COUNT;
1933 if (filp->f_flags & O_EXCL)
1934 fd_ref[drive] = -1;
1935 else
1936 fd_ref[drive]++;
1938 fd_device[drive] = MINOR(inode->i_rdev);
1940 if (old_dev && old_dev != MINOR(inode->i_rdev))
1941 invalidate_buffers(MKDEV(FLOPPY_MAJOR, old_dev));
1943 /* Allow ioctls if we have write-permissions even if read-only open */
1944 if (filp->f_mode & 2 || permission (inode, 2) == 0)
1945 filp->f_mode |= IOCTL_MODE_BIT;
1946 if (filp->f_mode & 2)
1947 filp->f_mode |= OPEN_WRITE_BIT;
1949 if (filp->f_flags & O_NDELAY)
1950 return 0;
1952 if (filp->f_mode & 3) {
1953 check_disk_change(inode->i_rdev);
1954 if (filp->f_mode & 2) {
1955 if (UD.wpstat) {
1956 floppy_release(inode, filp);
1957 return -EROFS;
1962 return 0;
1966 static int floppy_release( struct inode * inode, struct file * filp )
1968 int drive;
1970 drive = MINOR(inode->i_rdev) & 3;
1973 * If filp is NULL, we're being called from blkdev_release
1974 * or after a failed mount attempt. In the former case the
1975 * device has already been sync'ed, and in the latter no
1976 * sync is required. Otherwise, sync if filp is writable.
1978 if (filp && (filp->f_mode & (2 | OPEN_WRITE_BIT)))
1979 block_fsync (filp, filp->f_dentry);
1981 if (fd_ref[drive] < 0)
1982 fd_ref[drive] = 0;
1983 else if (!fd_ref[drive]--) {
1984 printk(KERN_ERR "floppy_release with fd_ref == 0");
1985 fd_ref[drive] = 0;
1988 MOD_DEC_USE_COUNT;
1989 return 0;
1992 static struct file_operations floppy_fops = {
1993 NULL, /* lseek - default */
1994 block_read, /* read - general block-dev read */
1995 block_write, /* write - general block-dev write */
1996 NULL, /* readdir - bad */
1997 NULL, /* select */
1998 fd_ioctl, /* ioctl */
1999 NULL, /* mmap */
2000 floppy_open, /* open */
2001 NULL, /* flush */
2002 floppy_release, /* release */
2003 block_fsync, /* fsync */
2004 NULL, /* fasync */
2005 check_floppy_change, /* media_change */
2006 floppy_revalidate, /* revalidate */
2009 __initfunc(int atari_floppy_init (void))
2011 int i;
2013 if (!MACH_IS_ATARI)
2014 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
2015 return -ENXIO;
2017 if (MACH_IS_HADES)
2018 /* Hades doesn't have Atari-compatible floppy */
2019 return -ENXIO;
2021 if (register_blkdev(MAJOR_NR,"fd",&floppy_fops)) {
2022 printk(KERN_ERR "Unable to get major %d for floppy\n",MAJOR_NR);
2023 return -EBUSY;
2026 if (UseTrackbuffer < 0)
2027 /* not set by user -> use default: for now, we turn
2028 track buffering off for all Medusas, though it
2029 could be used with ones that have a counter
2030 card. But the test is too hard :-( */
2031 UseTrackbuffer = !MACH_IS_MEDUSA;
2033 /* initialize variables */
2034 SelectedDrive = -1;
2035 BufferDrive = -1;
2037 /* initialize check_change timer */
2038 timer_table[FLOPPY_TIMER].fn = check_change;
2039 timer_active &= ~(1 << FLOPPY_TIMER);
2041 DMABuffer = atari_stram_alloc( BUFFER_SIZE+512, NULL, "ataflop" );
2042 if (!DMABuffer) {
2043 printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
2044 unregister_blkdev(MAJOR_NR, "fd");
2045 return -ENOMEM;
2047 TrackBuffer = DMABuffer + 512;
2048 PhysDMABuffer = (unsigned long) VTOP(DMABuffer);
2049 PhysTrackBuffer = (unsigned long) VTOP(TrackBuffer);
2050 BufferDrive = BufferSide = BufferTrack = -1;
2052 for (i = 0; i < FD_MAX_UNITS; i++) {
2053 unit[i].track = -1;
2054 unit[i].flags = 0;
2057 for (i = 0; i < 256; i++)
2058 if ((i >> 2) > 0 && (i >> 2) <= NUM_DISK_MINORS) {
2059 int type = minor2disktype[(i >> 2) - 1].index;
2060 floppy_sizes[i] = disk_type[type].blocks >> 1;
2061 } else
2062 floppy_sizes[i] = MAX_DISK_SIZE;
2064 blk_size[MAJOR_NR] = floppy_sizes;
2065 blksize_size[MAJOR_NR] = floppy_blocksizes;
2066 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
2068 printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
2069 DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
2070 UseTrackbuffer ? "" : "no ");
2071 config_types();
2073 (void)do_floppy; /* avoid warning about unused variable */
2074 return 0;
2078 __initfunc(void atari_floppy_setup( char *str, int *ints ))
2080 int i;
2082 if (ints[0] < 1) {
2083 printk(KERN_ERR "ataflop_setup: no arguments!\n" );
2084 return;
2086 else if (ints[0] > 2+FD_MAX_UNITS) {
2087 printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2090 if (ints[1] < 0 || ints[1] > 2)
2091 printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2092 else
2093 DriveType = ints[1];
2095 if (ints[0] >= 2)
2096 UseTrackbuffer = (ints[2] > 0);
2098 for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2099 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2100 printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2101 else
2102 UserSteprate[i-3] = ints[i];
2106 #ifdef MODULE
2107 int init_module (void)
2109 if (!MACH_IS_ATARI)
2110 return -ENXIO;
2111 return atari_floppy_init ();
2114 void cleanup_module (void)
2116 unregister_blkdev(MAJOR_NR, "fd");
2118 blk_dev[MAJOR_NR].request_fn = 0;
2119 timer_active &= ~(1 << FLOPPY_TIMER);
2120 timer_table[FLOPPY_TIMER].fn = 0;
2121 atari_stram_free( DMABuffer );
2123 #endif