staging: ST_BT depends on RFKILL
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / block / ataflop.c
blobaceb964765246f9448c7ec65a8369a0d6106b89f
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/fd.h>
67 #include <linux/delay.h>
68 #include <linux/init.h>
69 #include <linux/blkdev.h>
70 #include <linux/smp_lock.h>
72 #include <asm/atafd.h>
73 #include <asm/atafdreg.h>
74 #include <asm/atariints.h>
75 #include <asm/atari_stdma.h>
76 #include <asm/atari_stram.h>
78 #define FD_MAX_UNITS 2
80 #undef DEBUG
82 static struct request_queue *floppy_queue;
83 static struct request *fd_request;
85 /* Disk types: DD, HD, ED */
86 static struct atari_disk_type {
87 const char *name;
88 unsigned spt; /* sectors per track */
89 unsigned blocks; /* total number of blocks */
90 unsigned fdc_speed; /* fdc_speed setting */
91 unsigned stretch; /* track doubling ? */
92 } atari_disk_type[] = {
93 { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
94 { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
95 { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
96 { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
97 /* formats above are probed for type DD */
98 #define MAX_TYPE_DD 3
99 { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
100 { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
101 { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
102 /* formats above are probed for types DD and HD */
103 #define MAX_TYPE_HD 6
104 { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
105 { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
106 /* formats above are probed for types DD, HD and ED */
107 #define MAX_TYPE_ED 8
108 /* types below are never autoprobed */
109 { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
110 { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
111 { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
112 { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
113 { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
114 { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
115 { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
116 { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
117 { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
118 { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
119 { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
120 { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
121 { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
122 { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
123 { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
124 { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
125 { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
126 { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
127 { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
130 static int StartDiskType[] = {
131 MAX_TYPE_DD,
132 MAX_TYPE_HD,
133 MAX_TYPE_ED
136 #define TYPE_DD 0
137 #define TYPE_HD 1
138 #define TYPE_ED 2
140 static int DriveType = TYPE_HD;
142 static DEFINE_SPINLOCK(ataflop_lock);
144 /* Array for translating minors into disk formats */
145 static struct {
146 int index;
147 unsigned drive_types;
148 } minor2disktype[] = {
149 { 0, TYPE_DD }, /* 1: d360 */
150 { 4, TYPE_HD }, /* 2: h1200 */
151 { 1, TYPE_DD }, /* 3: D360 */
152 { 2, TYPE_DD }, /* 4: D720 */
153 { 1, TYPE_DD }, /* 5: h360 = D360 */
154 { 2, TYPE_DD }, /* 6: h720 = D720 */
155 { 5, TYPE_HD }, /* 7: H1440 */
156 { 7, TYPE_ED }, /* 8: E2880 */
157 /* some PC formats :-) */
158 { 8, TYPE_ED }, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
159 { 5, TYPE_HD }, /* 10: h1440 = H1440 */
160 { 9, TYPE_HD }, /* 11: H1680 */
161 { 10, TYPE_DD }, /* 12: h410 */
162 { 3, TYPE_DD }, /* 13: H820 <- == D820, 82x10 */
163 { 11, TYPE_HD }, /* 14: h1476 */
164 { 12, TYPE_HD }, /* 15: H1722 */
165 { 13, TYPE_DD }, /* 16: h420 */
166 { 14, TYPE_DD }, /* 17: H830 */
167 { 15, TYPE_HD }, /* 18: h1494 */
168 { 16, TYPE_HD }, /* 19: H1743 */
169 { 17, TYPE_DD }, /* 20: h880 */
170 { 18, TYPE_DD }, /* 21: D1040 */
171 { 19, TYPE_DD }, /* 22: D1120 */
172 { 20, TYPE_HD }, /* 23: h1600 */
173 { 21, TYPE_HD }, /* 24: H1760 */
174 { 22, TYPE_HD }, /* 25: H1920 */
175 { 23, TYPE_ED }, /* 26: E3200 */
176 { 24, TYPE_ED }, /* 27: E3520 */
177 { 25, TYPE_ED }, /* 28: E3840 */
178 { 26, TYPE_HD }, /* 29: H1840 */
179 { 27, TYPE_DD }, /* 30: D800 */
180 { 6, TYPE_HD }, /* 31: H1640 <- was H1600 == h1600 for PC */
183 #define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
186 * Maximum disk size (in kilobytes). This default is used whenever the
187 * current disk size is unknown.
189 #define MAX_DISK_SIZE 3280
192 * MSch: User-provided type information. 'drive' points to
193 * the respective entry of this array. Set by FDSETPRM ioctls.
195 static struct atari_disk_type user_params[FD_MAX_UNITS];
198 * User-provided permanent type information. 'drive' points to
199 * the respective entry of this array. Set by FDDEFPRM ioctls,
200 * restored upon disk change by floppy_revalidate() if valid (as seen by
201 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
203 static struct atari_disk_type default_params[FD_MAX_UNITS];
205 /* current info on each unit */
206 static struct atari_floppy_struct {
207 int connected; /* !=0 : drive is connected */
208 int autoprobe; /* !=0 : do autoprobe */
210 struct atari_disk_type *disktype; /* current type of disk */
212 int track; /* current head position or -1 if
213 unknown */
214 unsigned int steprate; /* steprate setting */
215 unsigned int wpstat; /* current state of WP signal (for
216 disk change detection) */
217 int flags; /* flags */
218 struct gendisk *disk;
219 int ref;
220 int type;
221 } unit[FD_MAX_UNITS];
223 #define UD unit[drive]
224 #define UDT unit[drive].disktype
225 #define SUD unit[SelectedDrive]
226 #define SUDT unit[SelectedDrive].disktype
229 #define FDC_READ(reg) ({ \
230 /* unsigned long __flags; */ \
231 unsigned short __val; \
232 /* local_irq_save(__flags); */ \
233 dma_wd.dma_mode_status = 0x80 | (reg); \
234 udelay(25); \
235 __val = dma_wd.fdc_acces_seccount; \
236 MFPDELAY(); \
237 /* local_irq_restore(__flags); */ \
238 __val & 0xff; \
241 #define FDC_WRITE(reg,val) \
242 do { \
243 /* unsigned long __flags; */ \
244 /* local_irq_save(__flags); */ \
245 dma_wd.dma_mode_status = 0x80 | (reg); \
246 udelay(25); \
247 dma_wd.fdc_acces_seccount = (val); \
248 MFPDELAY(); \
249 /* local_irq_restore(__flags); */ \
250 } while(0)
253 /* Buffering variables:
254 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
255 * operations. Second, a track buffer is used to cache a whole track
256 * of the disk to save read operations. These are two separate buffers
257 * because that allows write operations without clearing the track buffer.
260 static int MaxSectors[] = {
261 11, 22, 44
263 static int BufferSize[] = {
264 15*512, 30*512, 60*512
267 #define BUFFER_SIZE (BufferSize[DriveType])
269 unsigned char *DMABuffer; /* buffer for writes */
270 static unsigned long PhysDMABuffer; /* physical address */
272 static int UseTrackbuffer = -1; /* Do track buffering? */
273 module_param(UseTrackbuffer, int, 0);
275 unsigned char *TrackBuffer; /* buffer for reads */
276 static unsigned long PhysTrackBuffer; /* physical address */
277 static int BufferDrive, BufferSide, BufferTrack;
278 static int read_track; /* non-zero if we are reading whole tracks */
280 #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
281 #define IS_BUFFERED(drive,side,track) \
282 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
285 * These are global variables, as that's the easiest way to give
286 * information to interrupts. They are the data used for the current
287 * request.
289 static int SelectedDrive = 0;
290 static int ReqCmd, ReqBlock;
291 static int ReqSide, ReqTrack, ReqSector, ReqCnt;
292 static int HeadSettleFlag = 0;
293 static unsigned char *ReqData, *ReqBuffer;
294 static int MotorOn = 0, MotorOffTrys;
295 static int IsFormatting = 0, FormatError;
297 static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
298 module_param_array(UserSteprate, int, NULL, 0);
300 /* Synchronization of FDC access. */
301 static volatile int fdc_busy = 0;
302 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
303 static DECLARE_WAIT_QUEUE_HEAD(format_wait);
305 static unsigned long changed_floppies = 0xff, fake_change = 0;
306 #define CHECK_CHANGE_DELAY HZ/2
308 #define FD_MOTOR_OFF_DELAY (3*HZ)
309 #define FD_MOTOR_OFF_MAXTRY (10*20)
311 #define FLOPPY_TIMEOUT (6*HZ)
312 #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
313 * will be recalibrated. */
314 #define MAX_ERRORS 8 /* After this many errors the driver
315 * will give up. */
319 * The driver is trying to determine the correct media format
320 * while Probing is set. fd_rwsec_done() clears it after a
321 * successful access.
323 static int Probing = 0;
325 /* This flag is set when a dummy seek is necessary to make the WP
326 * status bit accessible.
328 static int NeedSeek = 0;
331 #ifdef DEBUG
332 #define DPRINT(a) printk a
333 #else
334 #define DPRINT(a)
335 #endif
337 /***************************** Prototypes *****************************/
339 static void fd_select_side( int side );
340 static void fd_select_drive( int drive );
341 static void fd_deselect( void );
342 static void fd_motor_off_timer( unsigned long dummy );
343 static void check_change( unsigned long dummy );
344 static irqreturn_t floppy_irq (int irq, void *dummy);
345 static void fd_error( void );
346 static int do_format(int drive, int type, struct atari_format_descr *desc);
347 static void do_fd_action( int drive );
348 static void fd_calibrate( void );
349 static void fd_calibrate_done( int status );
350 static void fd_seek( void );
351 static void fd_seek_done( int status );
352 static void fd_rwsec( void );
353 static void fd_readtrack_check( unsigned long dummy );
354 static void fd_rwsec_done( int status );
355 static void fd_rwsec_done1(int status);
356 static void fd_writetrack( void );
357 static void fd_writetrack_done( int status );
358 static void fd_times_out( unsigned long dummy );
359 static void finish_fdc( void );
360 static void finish_fdc_done( int dummy );
361 static void setup_req_params( int drive );
362 static void redo_fd_request( void);
363 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
364 cmd, unsigned long param);
365 static void fd_probe( int drive );
366 static int fd_test_drive_present( int drive );
367 static void config_types( void );
368 static int floppy_open(struct block_device *bdev, fmode_t mode);
369 static int floppy_release(struct gendisk *disk, fmode_t mode);
371 /************************* End of Prototypes **************************/
373 static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer, 0, 0);
374 static DEFINE_TIMER(readtrack_timer, fd_readtrack_check, 0, 0);
375 static DEFINE_TIMER(timeout_timer, fd_times_out, 0, 0);
376 static DEFINE_TIMER(fd_timer, check_change, 0, 0);
378 static void fd_end_request_cur(int err)
380 if (!__blk_end_request_cur(fd_request, err))
381 fd_request = NULL;
384 static inline void start_motor_off_timer(void)
386 mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
387 MotorOffTrys = 0;
390 static inline void start_check_change_timer( void )
392 mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
395 static inline void start_timeout(void)
397 mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
400 static inline void stop_timeout(void)
402 del_timer(&timeout_timer);
405 /* Select the side to use. */
407 static void fd_select_side( int side )
409 unsigned long flags;
411 /* protect against various other ints mucking around with the PSG */
412 local_irq_save(flags);
414 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
415 sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
416 sound_ym.rd_data_reg_sel & 0xfe;
418 local_irq_restore(flags);
422 /* Select a drive, update the FDC's track register and set the correct
423 * clock speed for this disk's type.
426 static void fd_select_drive( int drive )
428 unsigned long flags;
429 unsigned char tmp;
431 if (drive == SelectedDrive)
432 return;
434 /* protect against various other ints mucking around with the PSG */
435 local_irq_save(flags);
436 sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
437 tmp = sound_ym.rd_data_reg_sel;
438 sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
439 atari_dont_touch_floppy_select = 1;
440 local_irq_restore(flags);
442 /* restore track register to saved value */
443 FDC_WRITE( FDCREG_TRACK, UD.track );
444 udelay(25);
446 /* select 8/16 MHz */
447 if (UDT)
448 if (ATARIHW_PRESENT(FDCSPEED))
449 dma_wd.fdc_speed = UDT->fdc_speed;
451 SelectedDrive = drive;
455 /* Deselect both drives. */
457 static void fd_deselect( void )
459 unsigned long flags;
461 /* protect against various other ints mucking around with the PSG */
462 local_irq_save(flags);
463 atari_dont_touch_floppy_select = 0;
464 sound_ym.rd_data_reg_sel=14; /* Select PSG Port A */
465 sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
466 (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
467 /* On Falcon, the drive B select line is used on the printer port, so
468 * leave it alone... */
469 SelectedDrive = -1;
470 local_irq_restore(flags);
474 /* This timer function deselects the drives when the FDC switched the
475 * motor off. The deselection cannot happen earlier because the FDC
476 * counts the index signals, which arrive only if one drive is selected.
479 static void fd_motor_off_timer( unsigned long dummy )
481 unsigned char status;
483 if (SelectedDrive < 0)
484 /* no drive selected, needn't deselect anyone */
485 return;
487 if (stdma_islocked())
488 goto retry;
490 status = FDC_READ( FDCREG_STATUS );
492 if (!(status & 0x80)) {
493 /* motor already turned off by FDC -> deselect drives */
494 MotorOn = 0;
495 fd_deselect();
496 return;
498 /* not yet off, try again */
500 retry:
501 /* Test again later; if tested too often, it seems there is no disk
502 * in the drive and the FDC will leave the motor on forever (or,
503 * at least until a disk is inserted). So we'll test only twice
504 * per second from then on...
506 mod_timer(&motor_off_timer,
507 jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
511 /* This function is repeatedly called to detect disk changes (as good
512 * as possible) and keep track of the current state of the write protection.
515 static void check_change( unsigned long dummy )
517 static int drive = 0;
519 unsigned long flags;
520 unsigned char old_porta;
521 int stat;
523 if (++drive > 1 || !UD.connected)
524 drive = 0;
526 /* protect against various other ints mucking around with the PSG */
527 local_irq_save(flags);
529 if (!stdma_islocked()) {
530 sound_ym.rd_data_reg_sel = 14;
531 old_porta = sound_ym.rd_data_reg_sel;
532 sound_ym.wd_data = (old_porta | DSKDRVNONE) &
533 ~(drive == 0 ? DSKDRV0 : DSKDRV1);
534 stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
535 sound_ym.wd_data = old_porta;
537 if (stat != UD.wpstat) {
538 DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
539 UD.wpstat = stat;
540 set_bit (drive, &changed_floppies);
543 local_irq_restore(flags);
545 start_check_change_timer();
549 /* Handling of the Head Settling Flag: This flag should be set after each
550 * seek operation, because we don't use seeks with verify.
553 static inline void set_head_settle_flag(void)
555 HeadSettleFlag = FDCCMDADD_E;
558 static inline int get_head_settle_flag(void)
560 int tmp = HeadSettleFlag;
561 HeadSettleFlag = 0;
562 return( tmp );
565 static inline void copy_buffer(void *from, void *to)
567 ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
568 int cnt;
570 for (cnt = 512/4; cnt; cnt--)
571 *p2++ = *p1++;
577 /* General Interrupt Handling */
579 static void (*FloppyIRQHandler)( int status ) = NULL;
581 static irqreturn_t floppy_irq (int irq, void *dummy)
583 unsigned char status;
584 void (*handler)( int );
586 handler = xchg(&FloppyIRQHandler, NULL);
588 if (handler) {
589 nop();
590 status = FDC_READ( FDCREG_STATUS );
591 DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
592 handler( status );
594 else {
595 DPRINT(("FDC irq, no handler\n"));
597 return IRQ_HANDLED;
601 /* Error handling: If some error happened, retry some times, then
602 * recalibrate, then try again, and fail after MAX_ERRORS.
605 static void fd_error( void )
607 if (IsFormatting) {
608 IsFormatting = 0;
609 FormatError = 1;
610 wake_up( &format_wait );
611 return;
614 if (!fd_request)
615 return;
617 fd_request->errors++;
618 if (fd_request->errors >= MAX_ERRORS) {
619 printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
620 fd_end_request_cur(-EIO);
622 else if (fd_request->errors == RECALIBRATE_ERRORS) {
623 printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
624 if (SelectedDrive != -1)
625 SUD.track = -1;
627 redo_fd_request();
632 #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
635 /* ---------- Formatting ---------- */
637 #define FILL(n,val) \
638 do { \
639 memset( p, val, n ); \
640 p += n; \
641 } while(0)
643 static int do_format(int drive, int type, struct atari_format_descr *desc)
645 unsigned char *p;
646 int sect, nsect;
647 unsigned long flags;
649 DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
650 drive, desc->track, desc->head, desc->sect_offset ));
652 local_irq_save(flags);
653 while( fdc_busy ) sleep_on( &fdc_wait );
654 fdc_busy = 1;
655 stdma_lock(floppy_irq, NULL);
656 atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
657 local_irq_restore(flags);
659 if (type) {
660 if (--type >= NUM_DISK_MINORS ||
661 minor2disktype[type].drive_types > DriveType) {
662 redo_fd_request();
663 return -EINVAL;
665 type = minor2disktype[type].index;
666 UDT = &atari_disk_type[type];
669 if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
670 redo_fd_request();
671 return -EINVAL;
674 nsect = UDT->spt;
675 p = TrackBuffer;
676 /* The track buffer is used for the raw track data, so its
677 contents become invalid! */
678 BufferDrive = -1;
679 /* stop deselect timer */
680 del_timer( &motor_off_timer );
682 FILL( 60 * (nsect / 9), 0x4e );
683 for( sect = 0; sect < nsect; ++sect ) {
684 FILL( 12, 0 );
685 FILL( 3, 0xf5 );
686 *p++ = 0xfe;
687 *p++ = desc->track;
688 *p++ = desc->head;
689 *p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
690 *p++ = 2;
691 *p++ = 0xf7;
692 FILL( 22, 0x4e );
693 FILL( 12, 0 );
694 FILL( 3, 0xf5 );
695 *p++ = 0xfb;
696 FILL( 512, 0xe5 );
697 *p++ = 0xf7;
698 FILL( 40, 0x4e );
700 FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
702 IsFormatting = 1;
703 FormatError = 0;
704 ReqTrack = desc->track;
705 ReqSide = desc->head;
706 do_fd_action( drive );
708 sleep_on( &format_wait );
710 redo_fd_request();
711 return( FormatError ? -EIO : 0 );
715 /* do_fd_action() is the general procedure for a fd request: All
716 * required parameter settings (drive select, side select, track
717 * position) are checked and set if needed. For each of these
718 * parameters and the actual reading or writing exist two functions:
719 * one that starts the setting (or skips it if possible) and one
720 * callback for the "done" interrupt. Each done func calls the next
721 * set function to propagate the request down to fd_rwsec_done().
724 static void do_fd_action( int drive )
726 DPRINT(("do_fd_action\n"));
728 if (UseTrackbuffer && !IsFormatting) {
729 repeat:
730 if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
731 if (ReqCmd == READ) {
732 copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
733 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
734 /* read next sector */
735 setup_req_params( drive );
736 goto repeat;
738 else {
739 /* all sectors finished */
740 fd_end_request_cur(0);
741 redo_fd_request();
742 return;
745 else {
746 /* cmd == WRITE, pay attention to track buffer
747 * consistency! */
748 copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
753 if (SelectedDrive != drive)
754 fd_select_drive( drive );
756 if (UD.track == -1)
757 fd_calibrate();
758 else if (UD.track != ReqTrack << UDT->stretch)
759 fd_seek();
760 else if (IsFormatting)
761 fd_writetrack();
762 else
763 fd_rwsec();
767 /* Seek to track 0 if the current track is unknown */
769 static void fd_calibrate( void )
771 if (SUD.track >= 0) {
772 fd_calibrate_done( 0 );
773 return;
776 if (ATARIHW_PRESENT(FDCSPEED))
777 dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
778 DPRINT(("fd_calibrate\n"));
779 SET_IRQ_HANDLER( fd_calibrate_done );
780 /* we can't verify, since the speed may be incorrect */
781 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
783 NeedSeek = 1;
784 MotorOn = 1;
785 start_timeout();
786 /* wait for IRQ */
790 static void fd_calibrate_done( int status )
792 DPRINT(("fd_calibrate_done()\n"));
793 stop_timeout();
795 /* set the correct speed now */
796 if (ATARIHW_PRESENT(FDCSPEED))
797 dma_wd.fdc_speed = SUDT->fdc_speed;
798 if (status & FDCSTAT_RECNF) {
799 printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
800 fd_error();
802 else {
803 SUD.track = 0;
804 fd_seek();
809 /* Seek the drive to the requested track. The drive must have been
810 * calibrated at some point before this.
813 static void fd_seek( void )
815 if (SUD.track == ReqTrack << SUDT->stretch) {
816 fd_seek_done( 0 );
817 return;
820 if (ATARIHW_PRESENT(FDCSPEED)) {
821 dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
822 MFPDELAY();
825 DPRINT(("fd_seek() to track %d\n",ReqTrack));
826 FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
827 udelay(25);
828 SET_IRQ_HANDLER( fd_seek_done );
829 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
831 MotorOn = 1;
832 set_head_settle_flag();
833 start_timeout();
834 /* wait for IRQ */
838 static void fd_seek_done( int status )
840 DPRINT(("fd_seek_done()\n"));
841 stop_timeout();
843 /* set the correct speed */
844 if (ATARIHW_PRESENT(FDCSPEED))
845 dma_wd.fdc_speed = SUDT->fdc_speed;
846 if (status & FDCSTAT_RECNF) {
847 printk(KERN_ERR "fd%d: seek error (to track %d)\n",
848 SelectedDrive, ReqTrack );
849 /* we don't know exactly which track we are on now! */
850 SUD.track = -1;
851 fd_error();
853 else {
854 SUD.track = ReqTrack << SUDT->stretch;
855 NeedSeek = 0;
856 if (IsFormatting)
857 fd_writetrack();
858 else
859 fd_rwsec();
864 /* This does the actual reading/writing after positioning the head
865 * over the correct track.
868 static int MultReadInProgress = 0;
871 static void fd_rwsec( void )
873 unsigned long paddr, flags;
874 unsigned int rwflag, old_motoron;
875 unsigned int track;
877 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
878 if (ReqCmd == WRITE) {
879 if (ATARIHW_PRESENT(EXTD_DMA)) {
880 paddr = virt_to_phys(ReqData);
882 else {
883 copy_buffer( ReqData, DMABuffer );
884 paddr = PhysDMABuffer;
886 dma_cache_maintenance( paddr, 512, 1 );
887 rwflag = 0x100;
889 else {
890 if (read_track)
891 paddr = PhysTrackBuffer;
892 else
893 paddr = ATARIHW_PRESENT(EXTD_DMA) ?
894 virt_to_phys(ReqData) : PhysDMABuffer;
895 rwflag = 0;
898 fd_select_side( ReqSide );
900 /* Start sector of this operation */
901 FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
902 MFPDELAY();
903 /* Cheat for track if stretch != 0 */
904 if (SUDT->stretch) {
905 track = FDC_READ( FDCREG_TRACK);
906 MFPDELAY();
907 FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
909 udelay(25);
911 /* Setup DMA */
912 local_irq_save(flags);
913 dma_wd.dma_lo = (unsigned char)paddr;
914 MFPDELAY();
915 paddr >>= 8;
916 dma_wd.dma_md = (unsigned char)paddr;
917 MFPDELAY();
918 paddr >>= 8;
919 if (ATARIHW_PRESENT(EXTD_DMA))
920 st_dma_ext_dmahi = (unsigned short)paddr;
921 else
922 dma_wd.dma_hi = (unsigned char)paddr;
923 MFPDELAY();
924 local_irq_restore(flags);
926 /* Clear FIFO and switch DMA to correct mode */
927 dma_wd.dma_mode_status = 0x90 | rwflag;
928 MFPDELAY();
929 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
930 MFPDELAY();
931 dma_wd.dma_mode_status = 0x90 | rwflag;
932 MFPDELAY();
934 /* How many sectors for DMA */
935 dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
937 udelay(25);
939 /* Start operation */
940 dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
941 udelay(25);
942 SET_IRQ_HANDLER( fd_rwsec_done );
943 dma_wd.fdc_acces_seccount =
944 (get_head_settle_flag() |
945 (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
947 old_motoron = MotorOn;
948 MotorOn = 1;
949 NeedSeek = 1;
950 /* wait for interrupt */
952 if (read_track) {
953 /* If reading a whole track, wait about one disk rotation and
954 * then check if all sectors are read. The FDC will even
955 * search for the first non-existent sector and need 1 sec to
956 * recognise that it isn't present :-(
958 MultReadInProgress = 1;
959 mod_timer(&readtrack_timer,
960 /* 1 rot. + 5 rot.s if motor was off */
961 jiffies + HZ/5 + (old_motoron ? 0 : HZ));
963 start_timeout();
967 static void fd_readtrack_check( unsigned long dummy )
969 unsigned long flags, addr, addr2;
971 local_irq_save(flags);
973 if (!MultReadInProgress) {
974 /* This prevents a race condition that could arise if the
975 * interrupt is triggered while the calling of this timer
976 * callback function takes place. The IRQ function then has
977 * already cleared 'MultReadInProgress' when flow of control
978 * gets here.
980 local_irq_restore(flags);
981 return;
984 /* get the current DMA address */
985 /* ++ f.a. read twice to avoid being fooled by switcher */
986 addr = 0;
987 do {
988 addr2 = addr;
989 addr = dma_wd.dma_lo & 0xff;
990 MFPDELAY();
991 addr |= (dma_wd.dma_md & 0xff) << 8;
992 MFPDELAY();
993 if (ATARIHW_PRESENT( EXTD_DMA ))
994 addr |= (st_dma_ext_dmahi & 0xffff) << 16;
995 else
996 addr |= (dma_wd.dma_hi & 0xff) << 16;
997 MFPDELAY();
998 } while(addr != addr2);
1000 if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1001 /* already read enough data, force an FDC interrupt to stop
1002 * the read operation
1004 SET_IRQ_HANDLER( NULL );
1005 MultReadInProgress = 0;
1006 local_irq_restore(flags);
1007 DPRINT(("fd_readtrack_check(): done\n"));
1008 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1009 udelay(25);
1011 /* No error until now -- the FDC would have interrupted
1012 * otherwise!
1014 fd_rwsec_done1(0);
1016 else {
1017 /* not yet finished, wait another tenth rotation */
1018 local_irq_restore(flags);
1019 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1020 mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1025 static void fd_rwsec_done( int status )
1027 DPRINT(("fd_rwsec_done()\n"));
1029 if (read_track) {
1030 del_timer(&readtrack_timer);
1031 if (!MultReadInProgress)
1032 return;
1033 MultReadInProgress = 0;
1035 fd_rwsec_done1(status);
1038 static void fd_rwsec_done1(int status)
1040 unsigned int track;
1042 stop_timeout();
1044 /* Correct the track if stretch != 0 */
1045 if (SUDT->stretch) {
1046 track = FDC_READ( FDCREG_TRACK);
1047 MFPDELAY();
1048 FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1051 if (!UseTrackbuffer) {
1052 dma_wd.dma_mode_status = 0x90;
1053 MFPDELAY();
1054 if (!(dma_wd.dma_mode_status & 0x01)) {
1055 printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1056 goto err_end;
1059 MFPDELAY();
1061 if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1062 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1063 goto err_end;
1065 if ((status & FDCSTAT_RECNF) &&
1066 /* RECNF is no error after a multiple read when the FDC
1067 searched for a non-existent sector! */
1068 !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1069 if (Probing) {
1070 if (SUDT > atari_disk_type) {
1071 if (SUDT[-1].blocks > ReqBlock) {
1072 /* try another disk type */
1073 SUDT--;
1074 set_capacity(unit[SelectedDrive].disk,
1075 SUDT->blocks);
1076 } else
1077 Probing = 0;
1079 else {
1080 if (SUD.flags & FTD_MSG)
1081 printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1082 SelectedDrive, SUDT->name );
1083 Probing=0;
1085 } else {
1086 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1087 if (SUD.autoprobe) {
1088 SUDT = atari_disk_type + StartDiskType[DriveType];
1089 set_capacity(unit[SelectedDrive].disk,
1090 SUDT->blocks);
1091 Probing = 1;
1094 if (Probing) {
1095 if (ATARIHW_PRESENT(FDCSPEED)) {
1096 dma_wd.fdc_speed = SUDT->fdc_speed;
1097 MFPDELAY();
1099 setup_req_params( SelectedDrive );
1100 BufferDrive = -1;
1101 do_fd_action( SelectedDrive );
1102 return;
1105 printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1106 SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1107 goto err_end;
1109 if (status & FDCSTAT_CRC) {
1110 printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1111 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1112 goto err_end;
1114 if (status & FDCSTAT_LOST) {
1115 printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1116 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1117 goto err_end;
1120 Probing = 0;
1122 if (ReqCmd == READ) {
1123 if (!read_track) {
1124 void *addr;
1125 addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1126 dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1127 if (!ATARIHW_PRESENT( EXTD_DMA ))
1128 copy_buffer (addr, ReqData);
1129 } else {
1130 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1131 BufferDrive = SelectedDrive;
1132 BufferSide = ReqSide;
1133 BufferTrack = ReqTrack;
1134 copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1138 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
1139 /* read next sector */
1140 setup_req_params( SelectedDrive );
1141 do_fd_action( SelectedDrive );
1143 else {
1144 /* all sectors finished */
1145 fd_end_request_cur(0);
1146 redo_fd_request();
1148 return;
1150 err_end:
1151 BufferDrive = -1;
1152 fd_error();
1156 static void fd_writetrack( void )
1158 unsigned long paddr, flags;
1159 unsigned int track;
1161 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1163 paddr = PhysTrackBuffer;
1164 dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1166 fd_select_side( ReqSide );
1168 /* Cheat for track if stretch != 0 */
1169 if (SUDT->stretch) {
1170 track = FDC_READ( FDCREG_TRACK);
1171 MFPDELAY();
1172 FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1174 udelay(40);
1176 /* Setup DMA */
1177 local_irq_save(flags);
1178 dma_wd.dma_lo = (unsigned char)paddr;
1179 MFPDELAY();
1180 paddr >>= 8;
1181 dma_wd.dma_md = (unsigned char)paddr;
1182 MFPDELAY();
1183 paddr >>= 8;
1184 if (ATARIHW_PRESENT( EXTD_DMA ))
1185 st_dma_ext_dmahi = (unsigned short)paddr;
1186 else
1187 dma_wd.dma_hi = (unsigned char)paddr;
1188 MFPDELAY();
1189 local_irq_restore(flags);
1191 /* Clear FIFO and switch DMA to correct mode */
1192 dma_wd.dma_mode_status = 0x190;
1193 MFPDELAY();
1194 dma_wd.dma_mode_status = 0x90;
1195 MFPDELAY();
1196 dma_wd.dma_mode_status = 0x190;
1197 MFPDELAY();
1199 /* How many sectors for DMA */
1200 dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1201 udelay(40);
1203 /* Start operation */
1204 dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1205 udelay(40);
1206 SET_IRQ_HANDLER( fd_writetrack_done );
1207 dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1209 MotorOn = 1;
1210 start_timeout();
1211 /* wait for interrupt */
1215 static void fd_writetrack_done( int status )
1217 DPRINT(("fd_writetrack_done()\n"));
1219 stop_timeout();
1221 if (status & FDCSTAT_WPROT) {
1222 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1223 goto err_end;
1225 if (status & FDCSTAT_LOST) {
1226 printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1227 SelectedDrive, ReqSide, ReqTrack );
1228 goto err_end;
1231 wake_up( &format_wait );
1232 return;
1234 err_end:
1235 fd_error();
1238 static void fd_times_out( unsigned long dummy )
1240 atari_disable_irq( IRQ_MFP_FDC );
1241 if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1242 * before we came here... */
1244 SET_IRQ_HANDLER( NULL );
1245 /* If the timeout occurred while the readtrack_check timer was
1246 * active, we need to cancel it, else bad things will happen */
1247 if (UseTrackbuffer)
1248 del_timer( &readtrack_timer );
1249 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1250 udelay( 25 );
1252 printk(KERN_ERR "floppy timeout\n" );
1253 fd_error();
1254 end:
1255 atari_enable_irq( IRQ_MFP_FDC );
1259 /* The (noop) seek operation here is needed to make the WP bit in the
1260 * FDC status register accessible for check_change. If the last disk
1261 * operation would have been a RDSEC, this bit would always read as 0
1262 * no matter what :-( To save time, the seek goes to the track we're
1263 * already on.
1266 static void finish_fdc( void )
1268 if (!NeedSeek) {
1269 finish_fdc_done( 0 );
1271 else {
1272 DPRINT(("finish_fdc: dummy seek started\n"));
1273 FDC_WRITE (FDCREG_DATA, SUD.track);
1274 SET_IRQ_HANDLER( finish_fdc_done );
1275 FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1276 MotorOn = 1;
1277 start_timeout();
1278 /* we must wait for the IRQ here, because the ST-DMA
1279 is released immediately afterwards and the interrupt
1280 may be delivered to the wrong driver. */
1285 static void finish_fdc_done( int dummy )
1287 unsigned long flags;
1289 DPRINT(("finish_fdc_done entered\n"));
1290 stop_timeout();
1291 NeedSeek = 0;
1293 if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1294 /* If the check for a disk change is done too early after this
1295 * last seek command, the WP bit still reads wrong :-((
1297 mod_timer(&fd_timer, jiffies + 5);
1298 else
1299 start_check_change_timer();
1300 start_motor_off_timer();
1302 local_irq_save(flags);
1303 stdma_release();
1304 fdc_busy = 0;
1305 wake_up( &fdc_wait );
1306 local_irq_restore(flags);
1308 DPRINT(("finish_fdc() finished\n"));
1311 /* The detection of disk changes is a dark chapter in Atari history :-(
1312 * Because the "Drive ready" signal isn't present in the Atari
1313 * hardware, one has to rely on the "Write Protect". This works fine,
1314 * as long as no write protected disks are used. TOS solves this
1315 * problem by introducing tri-state logic ("maybe changed") and
1316 * looking at the serial number in block 0. This isn't possible for
1317 * Linux, since the floppy driver can't make assumptions about the
1318 * filesystem used on the disk and thus the contents of block 0. I've
1319 * chosen the method to always say "The disk was changed" if it is
1320 * unsure whether it was. This implies that every open or mount
1321 * invalidates the disk buffers if you work with write protected
1322 * disks. But at least this is better than working with incorrect data
1323 * due to unrecognised disk changes.
1326 static int check_floppy_change(struct gendisk *disk)
1328 struct atari_floppy_struct *p = disk->private_data;
1329 unsigned int drive = p - unit;
1330 if (test_bit (drive, &fake_change)) {
1331 /* simulated change (e.g. after formatting) */
1332 return 1;
1334 if (test_bit (drive, &changed_floppies)) {
1335 /* surely changed (the WP signal changed at least once) */
1336 return 1;
1338 if (UD.wpstat) {
1339 /* WP is on -> could be changed: to be sure, buffers should be
1340 * invalidated...
1342 return 1;
1345 return 0;
1348 static int floppy_revalidate(struct gendisk *disk)
1350 struct atari_floppy_struct *p = disk->private_data;
1351 unsigned int drive = p - unit;
1353 if (test_bit(drive, &changed_floppies) ||
1354 test_bit(drive, &fake_change) ||
1355 p->disktype == 0) {
1356 if (UD.flags & FTD_MSG)
1357 printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1358 BufferDrive = -1;
1359 clear_bit(drive, &fake_change);
1360 clear_bit(drive, &changed_floppies);
1361 /* MSch: clearing geometry makes sense only for autoprobe
1362 formats, for 'permanent user-defined' parameter:
1363 restore default_params[] here if flagged valid! */
1364 if (default_params[drive].blocks == 0)
1365 UDT = NULL;
1366 else
1367 UDT = &default_params[drive];
1369 return 0;
1373 /* This sets up the global variables describing the current request. */
1375 static void setup_req_params( int drive )
1377 int block = ReqBlock + ReqCnt;
1379 ReqTrack = block / UDT->spt;
1380 ReqSector = block - ReqTrack * UDT->spt + 1;
1381 ReqSide = ReqTrack & 1;
1382 ReqTrack >>= 1;
1383 ReqData = ReqBuffer + 512 * ReqCnt;
1385 if (UseTrackbuffer)
1386 read_track = (ReqCmd == READ && fd_request->errors == 0);
1387 else
1388 read_track = 0;
1390 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1391 ReqTrack, ReqSector, (unsigned long)ReqData ));
1395 static void redo_fd_request(void)
1397 int drive, type;
1398 struct atari_floppy_struct *floppy;
1400 DPRINT(("redo_fd_request: fd_request=%p dev=%s fd_request->sector=%ld\n",
1401 fd_request, fd_request ? fd_request->rq_disk->disk_name : "",
1402 fd_request ? blk_rq_pos(fd_request) : 0 ));
1404 IsFormatting = 0;
1406 repeat:
1407 if (!fd_request) {
1408 fd_request = blk_fetch_request(floppy_queue);
1409 if (!fd_request)
1410 goto the_end;
1413 floppy = fd_request->rq_disk->private_data;
1414 drive = floppy - unit;
1415 type = floppy->type;
1417 if (!UD.connected) {
1418 /* drive not connected */
1419 printk(KERN_ERR "Unknown Device: fd%d\n", drive );
1420 fd_end_request_cur(-EIO);
1421 goto repeat;
1424 if (type == 0) {
1425 if (!UDT) {
1426 Probing = 1;
1427 UDT = atari_disk_type + StartDiskType[DriveType];
1428 set_capacity(floppy->disk, UDT->blocks);
1429 UD.autoprobe = 1;
1432 else {
1433 /* user supplied disk type */
1434 if (--type >= NUM_DISK_MINORS) {
1435 printk(KERN_WARNING "fd%d: invalid disk format", drive );
1436 fd_end_request_cur(-EIO);
1437 goto repeat;
1439 if (minor2disktype[type].drive_types > DriveType) {
1440 printk(KERN_WARNING "fd%d: unsupported disk format", drive );
1441 fd_end_request_cur(-EIO);
1442 goto repeat;
1444 type = minor2disktype[type].index;
1445 UDT = &atari_disk_type[type];
1446 set_capacity(floppy->disk, UDT->blocks);
1447 UD.autoprobe = 0;
1450 if (blk_rq_pos(fd_request) + 1 > UDT->blocks) {
1451 fd_end_request_cur(-EIO);
1452 goto repeat;
1455 /* stop deselect timer */
1456 del_timer( &motor_off_timer );
1458 ReqCnt = 0;
1459 ReqCmd = rq_data_dir(fd_request);
1460 ReqBlock = blk_rq_pos(fd_request);
1461 ReqBuffer = fd_request->buffer;
1462 setup_req_params( drive );
1463 do_fd_action( drive );
1465 return;
1467 the_end:
1468 finish_fdc();
1472 void do_fd_request(struct request_queue * q)
1474 DPRINT(("do_fd_request for pid %d\n",current->pid));
1475 while( fdc_busy ) sleep_on( &fdc_wait );
1476 fdc_busy = 1;
1477 stdma_lock(floppy_irq, NULL);
1479 atari_disable_irq( IRQ_MFP_FDC );
1480 redo_fd_request();
1481 atari_enable_irq( IRQ_MFP_FDC );
1484 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1485 unsigned int cmd, unsigned long param)
1487 struct gendisk *disk = bdev->bd_disk;
1488 struct atari_floppy_struct *floppy = disk->private_data;
1489 int drive = floppy - unit;
1490 int type = floppy->type;
1491 struct atari_format_descr fmt_desc;
1492 struct atari_disk_type *dtp;
1493 struct floppy_struct getprm;
1494 int settype;
1495 struct floppy_struct setprm;
1496 void __user *argp = (void __user *)param;
1498 switch (cmd) {
1499 case FDGETPRM:
1500 if (type) {
1501 if (--type >= NUM_DISK_MINORS)
1502 return -ENODEV;
1503 if (minor2disktype[type].drive_types > DriveType)
1504 return -ENODEV;
1505 type = minor2disktype[type].index;
1506 dtp = &atari_disk_type[type];
1507 if (UD.flags & FTD_MSG)
1508 printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1509 drive, dtp, dtp->name);
1511 else {
1512 if (!UDT)
1513 return -ENXIO;
1514 else
1515 dtp = UDT;
1517 memset((void *)&getprm, 0, sizeof(getprm));
1518 getprm.size = dtp->blocks;
1519 getprm.sect = dtp->spt;
1520 getprm.head = 2;
1521 getprm.track = dtp->blocks/dtp->spt/2;
1522 getprm.stretch = dtp->stretch;
1523 if (copy_to_user(argp, &getprm, sizeof(getprm)))
1524 return -EFAULT;
1525 return 0;
1527 switch (cmd) {
1528 case FDSETPRM:
1529 case FDDEFPRM:
1531 * MSch 7/96: simple 'set geometry' case: just set the
1532 * 'default' device params (minor == 0).
1533 * Currently, the drive geometry is cleared after each
1534 * disk change and subsequent revalidate()! simple
1535 * implementation of FDDEFPRM: save geometry from a
1536 * FDDEFPRM call and restore it in floppy_revalidate() !
1539 /* get the parameters from user space */
1540 if (floppy->ref != 1 && floppy->ref != -1)
1541 return -EBUSY;
1542 if (copy_from_user(&setprm, argp, sizeof(setprm)))
1543 return -EFAULT;
1545 * first of all: check for floppy change and revalidate,
1546 * or the next access will revalidate - and clear UDT :-(
1549 if (check_floppy_change(disk))
1550 floppy_revalidate(disk);
1552 if (UD.flags & FTD_MSG)
1553 printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1554 drive, setprm.size, setprm.sect, setprm.stretch);
1556 /* what if type > 0 here? Overwrite specified entry ? */
1557 if (type) {
1558 /* refuse to re-set a predefined type for now */
1559 redo_fd_request();
1560 return -EINVAL;
1564 * type == 0: first look for a matching entry in the type list,
1565 * and set the UD.disktype field to use the perdefined entry.
1566 * TODO: add user-defined format to head of autoprobe list ?
1567 * Useful to include the user-type for future autodetection!
1570 for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1571 int setidx = 0;
1572 if (minor2disktype[settype].drive_types > DriveType) {
1573 /* skip this one, invalid for drive ... */
1574 continue;
1576 setidx = minor2disktype[settype].index;
1577 dtp = &atari_disk_type[setidx];
1579 /* found matching entry ?? */
1580 if ( dtp->blocks == setprm.size
1581 && dtp->spt == setprm.sect
1582 && dtp->stretch == setprm.stretch ) {
1583 if (UD.flags & FTD_MSG)
1584 printk (KERN_INFO "floppy%d: setting %s %p!\n",
1585 drive, dtp->name, dtp);
1586 UDT = dtp;
1587 set_capacity(floppy->disk, UDT->blocks);
1589 if (cmd == FDDEFPRM) {
1590 /* save settings as permanent default type */
1591 default_params[drive].name = dtp->name;
1592 default_params[drive].spt = dtp->spt;
1593 default_params[drive].blocks = dtp->blocks;
1594 default_params[drive].fdc_speed = dtp->fdc_speed;
1595 default_params[drive].stretch = dtp->stretch;
1598 return 0;
1603 /* no matching disk type found above - setting user_params */
1605 if (cmd == FDDEFPRM) {
1606 /* set permanent type */
1607 dtp = &default_params[drive];
1608 } else
1609 /* set user type (reset by disk change!) */
1610 dtp = &user_params[drive];
1612 dtp->name = "user format";
1613 dtp->blocks = setprm.size;
1614 dtp->spt = setprm.sect;
1615 if (setprm.sect > 14)
1616 dtp->fdc_speed = 3;
1617 else
1618 dtp->fdc_speed = 0;
1619 dtp->stretch = setprm.stretch;
1621 if (UD.flags & FTD_MSG)
1622 printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1623 drive, dtp->blocks, dtp->spt, dtp->stretch);
1625 /* sanity check */
1626 if (setprm.track != dtp->blocks/dtp->spt/2 ||
1627 setprm.head != 2) {
1628 redo_fd_request();
1629 return -EINVAL;
1632 UDT = dtp;
1633 set_capacity(floppy->disk, UDT->blocks);
1635 return 0;
1636 case FDMSGON:
1637 UD.flags |= FTD_MSG;
1638 return 0;
1639 case FDMSGOFF:
1640 UD.flags &= ~FTD_MSG;
1641 return 0;
1642 case FDSETEMSGTRESH:
1643 return -EINVAL;
1644 case FDFMTBEG:
1645 return 0;
1646 case FDFMTTRK:
1647 if (floppy->ref != 1 && floppy->ref != -1)
1648 return -EBUSY;
1649 if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
1650 return -EFAULT;
1651 return do_format(drive, type, &fmt_desc);
1652 case FDCLRPRM:
1653 UDT = NULL;
1654 /* MSch: invalidate default_params */
1655 default_params[drive].blocks = 0;
1656 set_capacity(floppy->disk, MAX_DISK_SIZE * 2);
1657 case FDFMTEND:
1658 case FDFLUSH:
1659 /* invalidate the buffer track to force a reread */
1660 BufferDrive = -1;
1661 set_bit(drive, &fake_change);
1662 check_disk_change(bdev);
1663 return 0;
1664 default:
1665 return -EINVAL;
1669 static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1670 unsigned int cmd, unsigned long arg)
1672 int ret;
1674 lock_kernel();
1675 ret = fd_locked_ioctl(bdev, mode, cmd, arg);
1676 unlock_kernel();
1678 return ret;
1681 /* Initialize the 'unit' variable for drive 'drive' */
1683 static void __init fd_probe( int drive )
1685 UD.connected = 0;
1686 UDT = NULL;
1688 if (!fd_test_drive_present( drive ))
1689 return;
1691 UD.connected = 1;
1692 UD.track = 0;
1693 switch( UserSteprate[drive] ) {
1694 case 2:
1695 UD.steprate = FDCSTEP_2;
1696 break;
1697 case 3:
1698 UD.steprate = FDCSTEP_3;
1699 break;
1700 case 6:
1701 UD.steprate = FDCSTEP_6;
1702 break;
1703 case 12:
1704 UD.steprate = FDCSTEP_12;
1705 break;
1706 default: /* should be -1 for "not set by user" */
1707 if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1708 UD.steprate = FDCSTEP_3;
1709 else
1710 UD.steprate = FDCSTEP_6;
1711 break;
1713 MotorOn = 1; /* from probe restore operation! */
1717 /* This function tests the physical presence of a floppy drive (not
1718 * whether a disk is inserted). This is done by issuing a restore
1719 * command, waiting max. 2 seconds (that should be enough to move the
1720 * head across the whole disk) and looking at the state of the "TR00"
1721 * signal. This should now be raised if there is a drive connected
1722 * (and there is no hardware failure :-) Otherwise, the drive is
1723 * declared absent.
1726 static int __init fd_test_drive_present( int drive )
1728 unsigned long timeout;
1729 unsigned char status;
1730 int ok;
1732 if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1733 fd_select_drive( drive );
1735 /* disable interrupt temporarily */
1736 atari_turnoff_irq( IRQ_MFP_FDC );
1737 FDC_WRITE (FDCREG_TRACK, 0xff00);
1738 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1740 timeout = jiffies + 2*HZ+HZ/2;
1741 while (time_before(jiffies, timeout))
1742 if (!(st_mfp.par_dt_reg & 0x20))
1743 break;
1745 status = FDC_READ( FDCREG_STATUS );
1746 ok = (status & FDCSTAT_TR00) != 0;
1748 /* force interrupt to abort restore operation (FDC would try
1749 * about 50 seconds!) */
1750 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1751 udelay(500);
1752 status = FDC_READ( FDCREG_STATUS );
1753 udelay(20);
1755 if (ok) {
1756 /* dummy seek command to make WP bit accessible */
1757 FDC_WRITE( FDCREG_DATA, 0 );
1758 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
1759 while( st_mfp.par_dt_reg & 0x20 )
1761 status = FDC_READ( FDCREG_STATUS );
1764 atari_turnon_irq( IRQ_MFP_FDC );
1765 return( ok );
1769 /* Look how many and which kind of drives are connected. If there are
1770 * floppies, additionally start the disk-change and motor-off timers.
1773 static void __init config_types( void )
1775 int drive, cnt = 0;
1777 /* for probing drives, set the FDC speed to 8 MHz */
1778 if (ATARIHW_PRESENT(FDCSPEED))
1779 dma_wd.fdc_speed = 0;
1781 printk(KERN_INFO "Probing floppy drive(s):\n");
1782 for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1783 fd_probe( drive );
1784 if (UD.connected) {
1785 printk(KERN_INFO "fd%d\n", drive);
1786 ++cnt;
1790 if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1791 /* If FDC is still busy from probing, give it another FORCI
1792 * command to abort the operation. If this isn't done, the FDC
1793 * will interrupt later and its IRQ line stays low, because
1794 * the status register isn't read. And this will block any
1795 * interrupts on this IRQ line :-(
1797 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1798 udelay(500);
1799 FDC_READ( FDCREG_STATUS );
1800 udelay(20);
1803 if (cnt > 0) {
1804 start_motor_off_timer();
1805 if (cnt == 1) fd_select_drive( 0 );
1806 start_check_change_timer();
1811 * floppy_open check for aliasing (/dev/fd0 can be the same as
1812 * /dev/PS0 etc), and disallows simultaneous access to the same
1813 * drive with different device numbers.
1816 static int floppy_open(struct block_device *bdev, fmode_t mode)
1818 struct atari_floppy_struct *p = bdev->bd_disk->private_data;
1819 int type = MINOR(bdev->bd_dev) >> 2;
1821 DPRINT(("fd_open: type=%d\n",type));
1822 if (p->ref && p->type != type)
1823 return -EBUSY;
1825 if (p->ref == -1 || (p->ref && mode & FMODE_EXCL))
1826 return -EBUSY;
1828 if (mode & FMODE_EXCL)
1829 p->ref = -1;
1830 else
1831 p->ref++;
1833 p->type = type;
1835 if (mode & FMODE_NDELAY)
1836 return 0;
1838 if (mode & (FMODE_READ|FMODE_WRITE)) {
1839 check_disk_change(bdev);
1840 if (mode & FMODE_WRITE) {
1841 if (p->wpstat) {
1842 if (p->ref < 0)
1843 p->ref = 0;
1844 else
1845 p->ref--;
1846 return -EROFS;
1850 return 0;
1853 static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
1855 int ret;
1857 lock_kernel();
1858 ret = floppy_open(bdev, mode);
1859 unlock_kernel();
1861 return ret;
1864 static int floppy_release(struct gendisk *disk, fmode_t mode)
1866 struct atari_floppy_struct *p = disk->private_data;
1867 lock_kernel();
1868 if (p->ref < 0)
1869 p->ref = 0;
1870 else if (!p->ref--) {
1871 printk(KERN_ERR "floppy_release with fd_ref == 0");
1872 p->ref = 0;
1874 unlock_kernel();
1875 return 0;
1878 static const struct block_device_operations floppy_fops = {
1879 .owner = THIS_MODULE,
1880 .open = floppy_unlocked_open,
1881 .release = floppy_release,
1882 .ioctl = fd_ioctl,
1883 .media_changed = check_floppy_change,
1884 .revalidate_disk= floppy_revalidate,
1887 static struct kobject *floppy_find(dev_t dev, int *part, void *data)
1889 int drive = *part & 3;
1890 int type = *part >> 2;
1891 if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1892 return NULL;
1893 *part = 0;
1894 return get_disk(unit[drive].disk);
1897 static int __init atari_floppy_init (void)
1899 int i;
1901 if (!MACH_IS_ATARI)
1902 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
1903 return -ENODEV;
1905 if (register_blkdev(FLOPPY_MAJOR,"fd"))
1906 return -EBUSY;
1908 for (i = 0; i < FD_MAX_UNITS; i++) {
1909 unit[i].disk = alloc_disk(1);
1910 if (!unit[i].disk)
1911 goto Enomem;
1914 if (UseTrackbuffer < 0)
1915 /* not set by user -> use default: for now, we turn
1916 track buffering off for all Medusas, though it
1917 could be used with ones that have a counter
1918 card. But the test is too hard :-( */
1919 UseTrackbuffer = !MACH_IS_MEDUSA;
1921 /* initialize variables */
1922 SelectedDrive = -1;
1923 BufferDrive = -1;
1925 DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
1926 if (!DMABuffer) {
1927 printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
1928 goto Enomem;
1930 TrackBuffer = DMABuffer + 512;
1931 PhysDMABuffer = virt_to_phys(DMABuffer);
1932 PhysTrackBuffer = virt_to_phys(TrackBuffer);
1933 BufferDrive = BufferSide = BufferTrack = -1;
1935 floppy_queue = blk_init_queue(do_fd_request, &ataflop_lock);
1936 if (!floppy_queue)
1937 goto Enomem;
1939 for (i = 0; i < FD_MAX_UNITS; i++) {
1940 unit[i].track = -1;
1941 unit[i].flags = 0;
1942 unit[i].disk->major = FLOPPY_MAJOR;
1943 unit[i].disk->first_minor = i;
1944 sprintf(unit[i].disk->disk_name, "fd%d", i);
1945 unit[i].disk->fops = &floppy_fops;
1946 unit[i].disk->private_data = &unit[i];
1947 unit[i].disk->queue = floppy_queue;
1948 set_capacity(unit[i].disk, MAX_DISK_SIZE * 2);
1949 add_disk(unit[i].disk);
1952 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
1953 floppy_find, NULL, NULL);
1955 printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
1956 DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
1957 UseTrackbuffer ? "" : "no ");
1958 config_types();
1960 return 0;
1961 Enomem:
1962 while (i--)
1963 put_disk(unit[i].disk);
1964 if (floppy_queue)
1965 blk_cleanup_queue(floppy_queue);
1966 unregister_blkdev(FLOPPY_MAJOR, "fd");
1967 return -ENOMEM;
1970 #ifndef MODULE
1971 static int __init atari_floppy_setup(char *str)
1973 int ints[3 + FD_MAX_UNITS];
1974 int i;
1976 if (!MACH_IS_ATARI)
1977 return 0;
1979 str = get_options(str, 3 + FD_MAX_UNITS, ints);
1981 if (ints[0] < 1) {
1982 printk(KERN_ERR "ataflop_setup: no arguments!\n" );
1983 return 0;
1985 else if (ints[0] > 2+FD_MAX_UNITS) {
1986 printk(KERN_ERR "ataflop_setup: too many arguments\n" );
1989 if (ints[1] < 0 || ints[1] > 2)
1990 printk(KERN_ERR "ataflop_setup: bad drive type\n" );
1991 else
1992 DriveType = ints[1];
1994 if (ints[0] >= 2)
1995 UseTrackbuffer = (ints[2] > 0);
1997 for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
1998 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
1999 printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2000 else
2001 UserSteprate[i-3] = ints[i];
2003 return 1;
2006 __setup("floppy=", atari_floppy_setup);
2007 #endif
2009 static void __exit atari_floppy_exit(void)
2011 int i;
2012 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
2013 for (i = 0; i < FD_MAX_UNITS; i++) {
2014 del_gendisk(unit[i].disk);
2015 put_disk(unit[i].disk);
2017 unregister_blkdev(FLOPPY_MAJOR, "fd");
2019 blk_cleanup_queue(floppy_queue);
2020 del_timer_sync(&fd_timer);
2021 atari_stram_free( DMABuffer );
2024 module_init(atari_floppy_init)
2025 module_exit(atari_floppy_exit)
2027 MODULE_LICENSE("GPL");