Linux Audio

Check our new training course

Loading...
v3.1
 
   1/*
   2 *  drivers/block/ataflop.c
   3 *
   4 *  Copyright (C) 1993  Greg Harp
   5 *  Atari Support by Bjoern Brauel, Roman Hodek
   6 *
   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 :-(
  15 *
  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
  21 *
  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
  29 *
  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...
  33 *
  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)
  44 *
  45 * Roman 10/15/95:
  46 *  - implement some more ioctls
  47 *  - disk formatting
  48 *  
  49 * Andreas 95/12/12:
  50 *  - increase gap size at start of track for HD/ED disks
  51 *
  52 * Michael (MSch) 11/07/96:
  53 *  - implemented FDSETPRM and FDDEFPRM ioctl
  54 *
  55 * Andreas (97/03/19):
  56 *  - implemented missing BLK* ioctls
  57 *
  58 *  Things left to do:
  59 *   - Formatting
  60 *   - Maybe a better strategy for disk change detection (does anyone
  61 *     know one?)
  62 */
  63
  64#include <linux/module.h>
  65
  66#include <linux/fd.h>
  67#include <linux/delay.h>
  68#include <linux/init.h>
  69#include <linux/blkdev.h>
  70#include <linux/mutex.h>
 
 
  71
  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>
  77
  78#define	FD_MAX_UNITS 2
  79
  80#undef DEBUG
  81
  82static DEFINE_MUTEX(ataflop_mutex);
  83static struct request *fd_request;
  84static int fdc_queue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  85
  86/* Disk types: DD, HD, ED */
  87static struct atari_disk_type {
  88	const char	*name;
  89	unsigned	spt;		/* sectors per track */
  90	unsigned	blocks;		/* total number of blocks */
  91	unsigned	fdc_speed;	/* fdc_speed setting */
  92	unsigned 	stretch;	/* track doubling ? */
  93} atari_disk_type[] = {
  94	{ "d360",  9, 720, 0, 0},	/*  0: 360kB diskette */
  95	{ "D360",  9, 720, 0, 1},	/*  1: 360kb in 720k or 1.2MB drive */
  96	{ "D720",  9,1440, 0, 0},	/*  2: 720kb in 720k or 1.2MB drive */
  97	{ "D820", 10,1640, 0, 0},	/*  3: DD disk with 82 tracks/10 sectors */
  98/* formats above are probed for type DD */
  99#define	MAX_TYPE_DD 3
 100	{ "h1200",15,2400, 3, 0},	/*  4: 1.2MB diskette */
 101	{ "H1440",18,2880, 3, 0},	/*  5: 1.4 MB diskette (HD) */
 102	{ "H1640",20,3280, 3, 0},	/*  6: 1.64MB diskette (fat HD) 82 tr 20 sec */
 103/* formats above are probed for types DD and HD */
 104#define	MAX_TYPE_HD 6
 105	{ "E2880",36,5760, 3, 0},	/*  7: 2.8 MB diskette (ED) */
 106	{ "E3280",40,6560, 3, 0},	/*  8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
 107/* formats above are probed for types DD, HD and ED */
 108#define	MAX_TYPE_ED 8
 109/* types below are never autoprobed */
 110	{ "H1680",21,3360, 3, 0},	/*  9: 1.68MB diskette (fat HD) 80 tr 21 sec */
 111	{ "h410",10,820, 0, 1},		/* 10: 410k diskette 41 tr 10 sec, stretch */
 112	{ "h1476",18,2952, 3, 0},	/* 11: 1.48MB diskette 82 tr 18 sec */
 113	{ "H1722",21,3444, 3, 0},	/* 12: 1.72MB diskette 82 tr 21 sec */
 114	{ "h420",10,840, 0, 1},		/* 13: 420k diskette 42 tr 10 sec, stretch */
 115	{ "H830",10,1660, 0, 0},	/* 14: 820k diskette 83 tr 10 sec */
 116	{ "h1494",18,2952, 3, 0},	/* 15: 1.49MB diskette 83 tr 18 sec */
 117	{ "H1743",21,3486, 3, 0},	/* 16: 1.74MB diskette 83 tr 21 sec */
 118	{ "h880",11,1760, 0, 0},	/* 17: 880k diskette 80 tr 11 sec */
 119	{ "D1040",13,2080, 0, 0},	/* 18: 1.04MB diskette 80 tr 13 sec */
 120	{ "D1120",14,2240, 0, 0},	/* 19: 1.12MB diskette 80 tr 14 sec */
 121	{ "h1600",20,3200, 3, 0},	/* 20: 1.60MB diskette 80 tr 20 sec */
 122	{ "H1760",22,3520, 3, 0},	/* 21: 1.76MB diskette 80 tr 22 sec */
 123	{ "H1920",24,3840, 3, 0},	/* 22: 1.92MB diskette 80 tr 24 sec */
 124	{ "E3200",40,6400, 3, 0},	/* 23: 3.2MB diskette 80 tr 40 sec */
 125	{ "E3520",44,7040, 3, 0},	/* 24: 3.52MB diskette 80 tr 44 sec */
 126	{ "E3840",48,7680, 3, 0},	/* 25: 3.84MB diskette 80 tr 48 sec */
 127	{ "H1840",23,3680, 3, 0},	/* 26: 1.84MB diskette 80 tr 23 sec */
 128	{ "D800",10,1600, 0, 0},	/* 27: 800k diskette 80 tr 10 sec */
 129};
 130
 131static int StartDiskType[] = {
 132	MAX_TYPE_DD,
 133	MAX_TYPE_HD,
 134	MAX_TYPE_ED
 135};
 136
 137#define	TYPE_DD		0
 138#define	TYPE_HD		1
 139#define	TYPE_ED		2
 140
 141static int DriveType = TYPE_HD;
 142
 143static DEFINE_SPINLOCK(ataflop_lock);
 144
 145/* Array for translating minors into disk formats */
 146static struct {
 147	int 	 index;
 148	unsigned drive_types;
 149} minor2disktype[] = {
 150	{  0, TYPE_DD },	/*  1: d360 */
 151	{  4, TYPE_HD },	/*  2: h1200 */
 152	{  1, TYPE_DD },	/*  3: D360 */
 153	{  2, TYPE_DD },	/*  4: D720 */
 154	{  1, TYPE_DD },	/*  5: h360 = D360 */
 155	{  2, TYPE_DD },	/*  6: h720 = D720 */
 156	{  5, TYPE_HD },	/*  7: H1440 */
 157	{  7, TYPE_ED },	/*  8: E2880 */
 158/* some PC formats :-) */
 159	{  8, TYPE_ED },	/*  9: E3280    <- was "CompaQ" == E2880 for PC */
 160	{  5, TYPE_HD },	/* 10: h1440 = H1440 */
 161	{  9, TYPE_HD },	/* 11: H1680 */
 162	{ 10, TYPE_DD },	/* 12: h410  */
 163	{  3, TYPE_DD },	/* 13: H820     <- == D820, 82x10 */
 164	{ 11, TYPE_HD },	/* 14: h1476 */
 165	{ 12, TYPE_HD },	/* 15: H1722 */
 166	{ 13, TYPE_DD },	/* 16: h420  */
 167	{ 14, TYPE_DD },	/* 17: H830  */
 168	{ 15, TYPE_HD },	/* 18: h1494 */
 169	{ 16, TYPE_HD },	/* 19: H1743 */
 170	{ 17, TYPE_DD },	/* 20: h880  */
 171	{ 18, TYPE_DD },	/* 21: D1040 */
 172	{ 19, TYPE_DD },	/* 22: D1120 */
 173	{ 20, TYPE_HD },	/* 23: h1600 */
 174	{ 21, TYPE_HD },	/* 24: H1760 */
 175	{ 22, TYPE_HD },	/* 25: H1920 */
 176	{ 23, TYPE_ED },	/* 26: E3200 */
 177	{ 24, TYPE_ED },	/* 27: E3520 */
 178	{ 25, TYPE_ED },	/* 28: E3840 */
 179	{ 26, TYPE_HD },	/* 29: H1840 */
 180	{ 27, TYPE_DD },	/* 30: D800  */
 181	{  6, TYPE_HD },	/* 31: H1640    <- was H1600 == h1600 for PC */
 182};
 183
 184#define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
 185
 186/*
 187 * Maximum disk size (in kilobytes). This default is used whenever the
 188 * current disk size is unknown.
 189 */
 190#define MAX_DISK_SIZE 3280
 191
 192/*
 193 * MSch: User-provided type information. 'drive' points to
 194 * the respective entry of this array. Set by FDSETPRM ioctls.
 195 */
 196static struct atari_disk_type user_params[FD_MAX_UNITS];
 197
 198/*
 199 * User-provided permanent type information. 'drive' points to
 200 * the respective entry of this array.  Set by FDDEFPRM ioctls, 
 201 * restored upon disk change by floppy_revalidate() if valid (as seen by
 202 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
 203 */
 204static struct atari_disk_type default_params[FD_MAX_UNITS];
 205
 206/* current info on each unit */
 207static struct atari_floppy_struct {
 208	int connected;				/* !=0 : drive is connected */
 209	int autoprobe;				/* !=0 : do autoprobe	    */
 210
 211	struct atari_disk_type	*disktype;	/* current type of disk */
 212
 213	int track;		/* current head position or -1 if
 214				   unknown */
 215	unsigned int steprate;	/* steprate setting */
 216	unsigned int wpstat;	/* current state of WP signal (for
 217				   disk change detection) */
 218	int flags;		/* flags */
 219	struct gendisk *disk;
 220	int ref;
 221	int type;
 
 222} unit[FD_MAX_UNITS];
 223
 224#define	UD	unit[drive]
 225#define	UDT	unit[drive].disktype
 226#define	SUD	unit[SelectedDrive]
 227#define	SUDT	unit[SelectedDrive].disktype
 228
 229
 230#define FDC_READ(reg) ({			\
 231    /* unsigned long __flags; */		\
 232    unsigned short __val;			\
 233    /* local_irq_save(__flags); */		\
 234    dma_wd.dma_mode_status = 0x80 | (reg);	\
 235    udelay(25);					\
 236    __val = dma_wd.fdc_acces_seccount;		\
 237    MFPDELAY();					\
 238    /* local_irq_restore(__flags); */		\
 239    __val & 0xff;				\
 240})
 241
 242#define FDC_WRITE(reg,val)			\
 243    do {					\
 244	/* unsigned long __flags; */		\
 245	/* local_irq_save(__flags); */		\
 246	dma_wd.dma_mode_status = 0x80 | (reg);	\
 247	udelay(25);				\
 248	dma_wd.fdc_acces_seccount = (val);	\
 249	MFPDELAY();				\
 250        /* local_irq_restore(__flags); */	\
 251    } while(0)
 252
 253
 254/* Buffering variables:
 255 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
 256 * operations. Second, a track buffer is used to cache a whole track
 257 * of the disk to save read operations. These are two separate buffers
 258 * because that allows write operations without clearing the track buffer.
 259 */
 260
 261static int MaxSectors[] = {
 262	11, 22, 44
 263};
 264static int BufferSize[] = {
 265	15*512, 30*512, 60*512
 266};
 267
 268#define	BUFFER_SIZE	(BufferSize[DriveType])
 269
 270unsigned char *DMABuffer;			  /* buffer for writes */
 271static unsigned long PhysDMABuffer;   /* physical address */
 272
 273static int UseTrackbuffer = -1;		  /* Do track buffering? */
 274module_param(UseTrackbuffer, int, 0);
 275
 276unsigned char *TrackBuffer;			  /* buffer for reads */
 277static unsigned long PhysTrackBuffer; /* physical address */
 278static int BufferDrive, BufferSide, BufferTrack;
 279static int read_track;		/* non-zero if we are reading whole tracks */
 280
 281#define	SECTOR_BUFFER(sec)	(TrackBuffer + ((sec)-1)*512)
 282#define	IS_BUFFERED(drive,side,track) \
 283    (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
 284
 285/*
 286 * These are global variables, as that's the easiest way to give
 287 * information to interrupts. They are the data used for the current
 288 * request.
 289 */
 290static int SelectedDrive = 0;
 291static int ReqCmd, ReqBlock;
 292static int ReqSide, ReqTrack, ReqSector, ReqCnt;
 293static int HeadSettleFlag = 0;
 294static unsigned char *ReqData, *ReqBuffer;
 295static int MotorOn = 0, MotorOffTrys;
 296static int IsFormatting = 0, FormatError;
 297
 298static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
 299module_param_array(UserSteprate, int, NULL, 0);
 300
 301/* Synchronization of FDC access. */
 302static volatile int fdc_busy = 0;
 303static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
 304static DECLARE_WAIT_QUEUE_HEAD(format_wait);
 305
 306static unsigned long changed_floppies = 0xff, fake_change = 0;
 307#define	CHECK_CHANGE_DELAY	HZ/2
 308
 309#define	FD_MOTOR_OFF_DELAY	(3*HZ)
 310#define	FD_MOTOR_OFF_MAXTRY	(10*20)
 311
 312#define FLOPPY_TIMEOUT		(6*HZ)
 313#define RECALIBRATE_ERRORS	4	/* After this many errors the drive
 314					 * will be recalibrated. */
 315#define MAX_ERRORS		8	/* After this many errors the driver
 316					 * will give up. */
 317
 318
 319/*
 320 * The driver is trying to determine the correct media format
 321 * while Probing is set. fd_rwsec_done() clears it after a
 322 * successful access.
 323 */
 324static int Probing = 0;
 325
 326/* This flag is set when a dummy seek is necessary to make the WP
 327 * status bit accessible.
 328 */
 329static int NeedSeek = 0;
 330
 331
 332#ifdef DEBUG
 333#define DPRINT(a)	printk a
 334#else
 335#define DPRINT(a)
 336#endif
 337
 338/***************************** Prototypes *****************************/
 339
 340static void fd_select_side( int side );
 341static void fd_select_drive( int drive );
 342static void fd_deselect( void );
 343static void fd_motor_off_timer( unsigned long dummy );
 344static void check_change( unsigned long dummy );
 345static irqreturn_t floppy_irq (int irq, void *dummy);
 346static void fd_error( void );
 347static int do_format(int drive, int type, struct atari_format_descr *desc);
 348static void do_fd_action( int drive );
 349static void fd_calibrate( void );
 350static void fd_calibrate_done( int status );
 351static void fd_seek( void );
 352static void fd_seek_done( int status );
 353static void fd_rwsec( void );
 354static void fd_readtrack_check( unsigned long dummy );
 355static void fd_rwsec_done( int status );
 356static void fd_rwsec_done1(int status);
 357static void fd_writetrack( void );
 358static void fd_writetrack_done( int status );
 359static void fd_times_out( unsigned long dummy );
 360static void finish_fdc( void );
 361static void finish_fdc_done( int dummy );
 362static void setup_req_params( int drive );
 363static void redo_fd_request( void);
 364static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
 365                     cmd, unsigned long param);
 366static void fd_probe( int drive );
 367static int fd_test_drive_present( int drive );
 368static void config_types( void );
 369static int floppy_open(struct block_device *bdev, fmode_t mode);
 370static int floppy_release(struct gendisk *disk, fmode_t mode);
 371
 372/************************* End of Prototypes **************************/
 373
 374static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer, 0, 0);
 375static DEFINE_TIMER(readtrack_timer, fd_readtrack_check, 0, 0);
 376static DEFINE_TIMER(timeout_timer, fd_times_out, 0, 0);
 377static DEFINE_TIMER(fd_timer, check_change, 0, 0);
 378	
 379static void fd_end_request_cur(int err)
 380{
 381	if (!__blk_end_request_cur(fd_request, err))
 
 
 382		fd_request = NULL;
 
 383}
 384
 385static inline void start_motor_off_timer(void)
 386{
 387	mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
 388	MotorOffTrys = 0;
 389}
 390
 391static inline void start_check_change_timer( void )
 392{
 393	mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
 394}
 395
 396static inline void start_timeout(void)
 397{
 398	mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
 399}
 400
 401static inline void stop_timeout(void)
 402{
 403	del_timer(&timeout_timer);
 404}
 405
 406/* Select the side to use. */
 407
 408static void fd_select_side( int side )
 409{
 410	unsigned long flags;
 411
 412	/* protect against various other ints mucking around with the PSG */
 413	local_irq_save(flags);
 414  
 415	sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
 416	sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
 417	                                 sound_ym.rd_data_reg_sel & 0xfe;
 418
 419	local_irq_restore(flags);
 420}
 421
 422
 423/* Select a drive, update the FDC's track register and set the correct
 424 * clock speed for this disk's type.
 425 */
 426
 427static void fd_select_drive( int drive )
 428{
 429	unsigned long flags;
 430	unsigned char tmp;
 431  
 432	if (drive == SelectedDrive)
 433	  return;
 434
 435	/* protect against various other ints mucking around with the PSG */
 436	local_irq_save(flags);
 437	sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
 438	tmp = sound_ym.rd_data_reg_sel;
 439	sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
 440	atari_dont_touch_floppy_select = 1;
 441	local_irq_restore(flags);
 442
 443	/* restore track register to saved value */
 444	FDC_WRITE( FDCREG_TRACK, UD.track );
 445	udelay(25);
 446
 447	/* select 8/16 MHz */
 448	if (UDT)
 449		if (ATARIHW_PRESENT(FDCSPEED))
 450			dma_wd.fdc_speed = UDT->fdc_speed;
 451	
 452	SelectedDrive = drive;
 453}
 454
 455
 456/* Deselect both drives. */
 457
 458static void fd_deselect( void )
 459{
 460	unsigned long flags;
 461
 462	/* protect against various other ints mucking around with the PSG */
 463	local_irq_save(flags);
 464	atari_dont_touch_floppy_select = 0;
 465	sound_ym.rd_data_reg_sel=14;	/* Select PSG Port A */
 466	sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
 467			    (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
 468	/* On Falcon, the drive B select line is used on the printer port, so
 469	 * leave it alone... */
 470	SelectedDrive = -1;
 471	local_irq_restore(flags);
 472}
 473
 474
 475/* This timer function deselects the drives when the FDC switched the
 476 * motor off. The deselection cannot happen earlier because the FDC
 477 * counts the index signals, which arrive only if one drive is selected.
 478 */
 479
 480static void fd_motor_off_timer( unsigned long dummy )
 481{
 482	unsigned char status;
 483
 484	if (SelectedDrive < 0)
 485		/* no drive selected, needn't deselect anyone */
 486		return;
 487
 488	if (stdma_islocked())
 489		goto retry;
 490
 491	status = FDC_READ( FDCREG_STATUS );
 492
 493	if (!(status & 0x80)) {
 494		/* motor already turned off by FDC -> deselect drives */
 495		MotorOn = 0;
 496		fd_deselect();
 497		return;
 498	}
 499	/* not yet off, try again */
 500
 501  retry:
 502	/* Test again later; if tested too often, it seems there is no disk
 503	 * in the drive and the FDC will leave the motor on forever (or,
 504	 * at least until a disk is inserted). So we'll test only twice
 505	 * per second from then on...
 506	 */
 507	mod_timer(&motor_off_timer,
 508		  jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
 509}
 510
 511
 512/* This function is repeatedly called to detect disk changes (as good
 513 * as possible) and keep track of the current state of the write protection.
 514 */
 515
 516static void check_change( unsigned long dummy )
 517{
 518	static int    drive = 0;
 519
 520	unsigned long flags;
 521	unsigned char old_porta;
 522	int			  stat;
 523
 524	if (++drive > 1 || !UD.connected)
 525		drive = 0;
 526
 527	/* protect against various other ints mucking around with the PSG */
 528	local_irq_save(flags);
 529
 530	if (!stdma_islocked()) {
 531		sound_ym.rd_data_reg_sel = 14;
 532		old_porta = sound_ym.rd_data_reg_sel;
 533		sound_ym.wd_data = (old_porta | DSKDRVNONE) &
 534			               ~(drive == 0 ? DSKDRV0 : DSKDRV1);
 535		stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
 536		sound_ym.wd_data = old_porta;
 537
 538		if (stat != UD.wpstat) {
 539			DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
 540			UD.wpstat = stat;
 541			set_bit (drive, &changed_floppies);
 542		}
 543	}
 544	local_irq_restore(flags);
 545
 546	start_check_change_timer();
 547}
 548
 549 
 550/* Handling of the Head Settling Flag: This flag should be set after each
 551 * seek operation, because we don't use seeks with verify.
 552 */
 553
 554static inline void set_head_settle_flag(void)
 555{
 556	HeadSettleFlag = FDCCMDADD_E;
 557}
 558
 559static inline int get_head_settle_flag(void)
 560{
 561	int	tmp = HeadSettleFlag;
 562	HeadSettleFlag = 0;
 563	return( tmp );
 564}
 565
 566static inline void copy_buffer(void *from, void *to)
 567{
 568	ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
 569	int cnt;
 570
 571	for (cnt = 512/4; cnt; cnt--)
 572		*p2++ = *p1++;
 573}
 574
 575  
 576  
 577
 578/* General Interrupt Handling */
 579
 580static void (*FloppyIRQHandler)( int status ) = NULL;
 581
 582static irqreturn_t floppy_irq (int irq, void *dummy)
 583{
 584	unsigned char status;
 585	void (*handler)( int );
 586
 587	handler = xchg(&FloppyIRQHandler, NULL);
 588
 589	if (handler) {
 590		nop();
 591		status = FDC_READ( FDCREG_STATUS );
 592		DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
 593		handler( status );
 594	}
 595	else {
 596		DPRINT(("FDC irq, no handler\n"));
 597	}
 598	return IRQ_HANDLED;
 599}
 600
 601
 602/* Error handling: If some error happened, retry some times, then
 603 * recalibrate, then try again, and fail after MAX_ERRORS.
 604 */
 605
 606static void fd_error( void )
 607{
 608	if (IsFormatting) {
 609		IsFormatting = 0;
 610		FormatError = 1;
 611		wake_up( &format_wait );
 612		return;
 613	}
 614
 615	if (!fd_request)
 616		return;
 617
 618	fd_request->errors++;
 619	if (fd_request->errors >= MAX_ERRORS) {
 620		printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
 621		fd_end_request_cur(-EIO);
 622	}
 623	else if (fd_request->errors == RECALIBRATE_ERRORS) {
 624		printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
 625		if (SelectedDrive != -1)
 626			SUD.track = -1;
 627	}
 628	redo_fd_request();
 629}
 630
 631
 632
 633#define	SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
 634
 635
 636/* ---------- Formatting ---------- */
 637
 638#define FILL(n,val)		\
 639    do {			\
 640	memset( p, val, n );	\
 641	p += n;			\
 642    } while(0)
 643
 644static int do_format(int drive, int type, struct atari_format_descr *desc)
 645{
 
 646	unsigned char	*p;
 647	int sect, nsect;
 648	unsigned long	flags;
 
 649
 650	DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
 651		drive, desc->track, desc->head, desc->sect_offset ));
 
 
 
 
 
 
 
 
 652
 653	local_irq_save(flags);
 654	while( fdc_busy ) sleep_on( &fdc_wait );
 655	fdc_busy = 1;
 656	stdma_lock(floppy_irq, NULL);
 657	atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
 658	local_irq_restore(flags);
 659
 660	if (type) {
 661		if (--type >= NUM_DISK_MINORS ||
 662		    minor2disktype[type].drive_types > DriveType) {
 663			redo_fd_request();
 664			return -EINVAL;
 665		}
 666		type = minor2disktype[type].index;
 667		UDT = &atari_disk_type[type];
 668	}
 669
 670	if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
 671		redo_fd_request();
 672		return -EINVAL;
 673	}
 674
 675	nsect = UDT->spt;
 676	p = TrackBuffer;
 677	/* The track buffer is used for the raw track data, so its
 678	   contents become invalid! */
 679	BufferDrive = -1;
 680	/* stop deselect timer */
 681	del_timer( &motor_off_timer );
 682
 683	FILL( 60 * (nsect / 9), 0x4e );
 684	for( sect = 0; sect < nsect; ++sect ) {
 685		FILL( 12, 0 );
 686		FILL( 3, 0xf5 );
 687		*p++ = 0xfe;
 688		*p++ = desc->track;
 689		*p++ = desc->head;
 690		*p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
 691		*p++ = 2;
 692		*p++ = 0xf7;
 693		FILL( 22, 0x4e );
 694		FILL( 12, 0 );
 695		FILL( 3, 0xf5 );
 696		*p++ = 0xfb;
 697		FILL( 512, 0xe5 );
 698		*p++ = 0xf7;
 699		FILL( 40, 0x4e );
 700	}
 701	FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
 702
 703	IsFormatting = 1;
 704	FormatError = 0;
 705	ReqTrack = desc->track;
 706	ReqSide  = desc->head;
 707	do_fd_action( drive );
 708
 709	sleep_on( &format_wait );
 710
 711	redo_fd_request();
 712	return( FormatError ? -EIO : 0 );	
 
 
 
 713}
 714
 715
 716/* do_fd_action() is the general procedure for a fd request: All
 717 * required parameter settings (drive select, side select, track
 718 * position) are checked and set if needed. For each of these
 719 * parameters and the actual reading or writing exist two functions:
 720 * one that starts the setting (or skips it if possible) and one
 721 * callback for the "done" interrupt. Each done func calls the next
 722 * set function to propagate the request down to fd_rwsec_done().
 723 */
 724
 725static void do_fd_action( int drive )
 726{
 727	DPRINT(("do_fd_action\n"));
 728	
 729	if (UseTrackbuffer && !IsFormatting) {
 730	repeat:
 731	    if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
 732		if (ReqCmd == READ) {
 733		    copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
 734		    if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
 735			/* read next sector */
 736			setup_req_params( drive );
 737			goto repeat;
 738		    }
 739		    else {
 740			/* all sectors finished */
 741			fd_end_request_cur(0);
 742			redo_fd_request();
 743			return;
 744		    }
 745		}
 746		else {
 747		    /* cmd == WRITE, pay attention to track buffer
 748		     * consistency! */
 749		    copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
 750		}
 751	    }
 752	}
 753
 754	if (SelectedDrive != drive)
 755		fd_select_drive( drive );
 756    
 757	if (UD.track == -1)
 758		fd_calibrate();
 759	else if (UD.track != ReqTrack << UDT->stretch)
 760		fd_seek();
 761	else if (IsFormatting)
 762		fd_writetrack();
 763	else
 764		fd_rwsec();
 765}
 766
 767
 768/* Seek to track 0 if the current track is unknown */
 769
 770static void fd_calibrate( void )
 771{
 772	if (SUD.track >= 0) {
 773		fd_calibrate_done( 0 );
 774		return;
 775	}
 776
 777	if (ATARIHW_PRESENT(FDCSPEED))
 778		dma_wd.fdc_speed = 0; 	/* always seek with 8 Mhz */;
 779	DPRINT(("fd_calibrate\n"));
 780	SET_IRQ_HANDLER( fd_calibrate_done );
 781	/* we can't verify, since the speed may be incorrect */
 782	FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
 783
 784	NeedSeek = 1;
 785	MotorOn = 1;
 786	start_timeout();
 787	/* wait for IRQ */
 788}
 789
 790
 791static void fd_calibrate_done( int status )
 792{
 793	DPRINT(("fd_calibrate_done()\n"));
 794	stop_timeout();
 795    
 796	/* set the correct speed now */
 797	if (ATARIHW_PRESENT(FDCSPEED))
 798		dma_wd.fdc_speed = SUDT->fdc_speed;
 799	if (status & FDCSTAT_RECNF) {
 800		printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
 801		fd_error();
 802	}
 803	else {
 804		SUD.track = 0;
 805		fd_seek();
 806	}
 807}
 808  
 809  
 810/* Seek the drive to the requested track. The drive must have been
 811 * calibrated at some point before this.
 812 */
 813  
 814static void fd_seek( void )
 815{
 816	if (SUD.track == ReqTrack << SUDT->stretch) {
 817		fd_seek_done( 0 );
 818		return;
 819	}
 820
 821	if (ATARIHW_PRESENT(FDCSPEED)) {
 822		dma_wd.fdc_speed = 0;	/* always seek witch 8 Mhz */
 823		MFPDELAY();
 824	}
 825
 826	DPRINT(("fd_seek() to track %d\n",ReqTrack));
 827	FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
 828	udelay(25);
 829	SET_IRQ_HANDLER( fd_seek_done );
 830	FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
 831
 832	MotorOn = 1;
 833	set_head_settle_flag();
 834	start_timeout();
 835	/* wait for IRQ */
 836}
 837
 838
 839static void fd_seek_done( int status )
 840{
 841	DPRINT(("fd_seek_done()\n"));
 842	stop_timeout();
 843	
 844	/* set the correct speed */
 845	if (ATARIHW_PRESENT(FDCSPEED))
 846		dma_wd.fdc_speed = SUDT->fdc_speed;
 847	if (status & FDCSTAT_RECNF) {
 848		printk(KERN_ERR "fd%d: seek error (to track %d)\n",
 849				SelectedDrive, ReqTrack );
 850		/* we don't know exactly which track we are on now! */
 851		SUD.track = -1;
 852		fd_error();
 853	}
 854	else {
 855		SUD.track = ReqTrack << SUDT->stretch;
 856		NeedSeek = 0;
 857		if (IsFormatting)
 858			fd_writetrack();
 859		else
 860			fd_rwsec();
 861	}
 862}
 863
 864
 865/* This does the actual reading/writing after positioning the head
 866 * over the correct track.
 867 */
 868
 869static int MultReadInProgress = 0;
 870
 871
 872static void fd_rwsec( void )
 873{
 874	unsigned long paddr, flags;
 875	unsigned int  rwflag, old_motoron;
 876	unsigned int track;
 877	
 878	DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
 879	if (ReqCmd == WRITE) {
 880		if (ATARIHW_PRESENT(EXTD_DMA)) {
 881			paddr = virt_to_phys(ReqData);
 882		}
 883		else {
 884			copy_buffer( ReqData, DMABuffer );
 885			paddr = PhysDMABuffer;
 886		}
 887		dma_cache_maintenance( paddr, 512, 1 );
 888		rwflag = 0x100;
 889	}
 890	else {
 891		if (read_track)
 892			paddr = PhysTrackBuffer;
 893		else
 894			paddr = ATARIHW_PRESENT(EXTD_DMA) ? 
 895				virt_to_phys(ReqData) : PhysDMABuffer;
 896		rwflag = 0;
 897	}
 898
 899	fd_select_side( ReqSide );
 900  
 901	/* Start sector of this operation */
 902	FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
 903	MFPDELAY();
 904	/* Cheat for track if stretch != 0 */
 905	if (SUDT->stretch) {
 906		track = FDC_READ( FDCREG_TRACK);
 907		MFPDELAY();
 908		FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
 909	}
 910	udelay(25);
 911  
 912	/* Setup DMA */
 913	local_irq_save(flags);
 914	dma_wd.dma_lo = (unsigned char)paddr;
 915	MFPDELAY();
 916	paddr >>= 8;
 917	dma_wd.dma_md = (unsigned char)paddr;
 918	MFPDELAY();
 919	paddr >>= 8;
 920	if (ATARIHW_PRESENT(EXTD_DMA))
 921		st_dma_ext_dmahi = (unsigned short)paddr;
 922	else
 923		dma_wd.dma_hi = (unsigned char)paddr;
 924	MFPDELAY();
 925	local_irq_restore(flags);
 926  
 927	/* Clear FIFO and switch DMA to correct mode */  
 928	dma_wd.dma_mode_status = 0x90 | rwflag;  
 929	MFPDELAY();
 930	dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);  
 931	MFPDELAY();
 932	dma_wd.dma_mode_status = 0x90 | rwflag;
 933	MFPDELAY();
 934  
 935	/* How many sectors for DMA */
 936	dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
 937  
 938	udelay(25);  
 939  
 940	/* Start operation */
 941	dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
 942	udelay(25);
 943	SET_IRQ_HANDLER( fd_rwsec_done );
 944	dma_wd.fdc_acces_seccount =
 945	  (get_head_settle_flag() |
 946	   (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
 947
 948	old_motoron = MotorOn;
 949	MotorOn = 1;
 950	NeedSeek = 1;
 951	/* wait for interrupt */
 952
 953	if (read_track) {
 954		/* If reading a whole track, wait about one disk rotation and
 955		 * then check if all sectors are read. The FDC will even
 956		 * search for the first non-existent sector and need 1 sec to
 957		 * recognise that it isn't present :-(
 958		 */
 959		MultReadInProgress = 1;
 960		mod_timer(&readtrack_timer,
 961			  /* 1 rot. + 5 rot.s if motor was off  */
 962			  jiffies + HZ/5 + (old_motoron ? 0 : HZ));
 963	}
 964	start_timeout();
 965}
 966
 967    
 968static void fd_readtrack_check( unsigned long dummy )
 969{
 970	unsigned long flags, addr, addr2;
 971
 972	local_irq_save(flags);
 973
 974	if (!MultReadInProgress) {
 975		/* This prevents a race condition that could arise if the
 976		 * interrupt is triggered while the calling of this timer
 977		 * callback function takes place. The IRQ function then has
 978		 * already cleared 'MultReadInProgress'  when flow of control
 979		 * gets here.
 980		 */
 981		local_irq_restore(flags);
 982		return;
 983	}
 984
 985	/* get the current DMA address */
 986	/* ++ f.a. read twice to avoid being fooled by switcher */
 987	addr = 0;
 988	do {
 989		addr2 = addr;
 990		addr = dma_wd.dma_lo & 0xff;
 991		MFPDELAY();
 992		addr |= (dma_wd.dma_md & 0xff) << 8;
 993		MFPDELAY();
 994		if (ATARIHW_PRESENT( EXTD_DMA ))
 995			addr |= (st_dma_ext_dmahi & 0xffff) << 16;
 996		else
 997			addr |= (dma_wd.dma_hi & 0xff) << 16;
 998		MFPDELAY();
 999	} while(addr != addr2);
1000  
1001	if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1002		/* already read enough data, force an FDC interrupt to stop
1003		 * the read operation
1004		 */
1005		SET_IRQ_HANDLER( NULL );
1006		MultReadInProgress = 0;
1007		local_irq_restore(flags);
1008		DPRINT(("fd_readtrack_check(): done\n"));
1009		FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1010		udelay(25);
1011
1012		/* No error until now -- the FDC would have interrupted
1013		 * otherwise!
1014		 */
1015		fd_rwsec_done1(0);
1016	}
1017	else {
1018		/* not yet finished, wait another tenth rotation */
1019		local_irq_restore(flags);
1020		DPRINT(("fd_readtrack_check(): not yet finished\n"));
1021		mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1022	}
1023}
1024
1025
1026static void fd_rwsec_done( int status )
1027{
1028	DPRINT(("fd_rwsec_done()\n"));
1029
1030	if (read_track) {
1031		del_timer(&readtrack_timer);
1032		if (!MultReadInProgress)
1033			return;
1034		MultReadInProgress = 0;
1035	}
1036	fd_rwsec_done1(status);
1037}
1038
1039static void fd_rwsec_done1(int status)
1040{
1041	unsigned int track;
1042
1043	stop_timeout();
1044	
1045	/* Correct the track if stretch != 0 */
1046	if (SUDT->stretch) {
1047		track = FDC_READ( FDCREG_TRACK);
1048		MFPDELAY();
1049		FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1050	}
1051
1052	if (!UseTrackbuffer) {
1053		dma_wd.dma_mode_status = 0x90;
1054		MFPDELAY();
1055		if (!(dma_wd.dma_mode_status & 0x01)) {
1056			printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1057			goto err_end;
1058		}
1059	}
1060	MFPDELAY();
1061
1062	if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1063		printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1064		goto err_end;
1065	}	
1066	if ((status & FDCSTAT_RECNF) &&
1067	    /* RECNF is no error after a multiple read when the FDC
1068	       searched for a non-existent sector! */
1069	    !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1070		if (Probing) {
1071			if (SUDT > atari_disk_type) {
1072			    if (SUDT[-1].blocks > ReqBlock) {
1073				/* try another disk type */
1074				SUDT--;
1075				set_capacity(unit[SelectedDrive].disk,
1076							SUDT->blocks);
1077			    } else
1078				Probing = 0;
1079			}
1080			else {
1081				if (SUD.flags & FTD_MSG)
1082					printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1083					       SelectedDrive, SUDT->name );
1084				Probing=0;
1085			}
1086		} else {	
1087/* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1088			if (SUD.autoprobe) {
1089				SUDT = atari_disk_type + StartDiskType[DriveType];
1090				set_capacity(unit[SelectedDrive].disk,
1091							SUDT->blocks);
1092				Probing = 1;
1093			}
1094		}
1095		if (Probing) {
1096			if (ATARIHW_PRESENT(FDCSPEED)) {
1097				dma_wd.fdc_speed = SUDT->fdc_speed;
1098				MFPDELAY();
1099			}
1100			setup_req_params( SelectedDrive );
1101			BufferDrive = -1;
1102			do_fd_action( SelectedDrive );
1103			return;
1104		}
1105
1106		printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1107		       SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1108		goto err_end;
1109	}
1110	if (status & FDCSTAT_CRC) {
1111		printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1112		       SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1113		goto err_end;
1114	}
1115	if (status & FDCSTAT_LOST) {
1116		printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1117		       SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1118		goto err_end;
1119	}
1120
1121	Probing = 0;
1122	
1123	if (ReqCmd == READ) {
1124		if (!read_track) {
1125			void *addr;
1126			addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1127			dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1128			if (!ATARIHW_PRESENT( EXTD_DMA ))
1129				copy_buffer (addr, ReqData);
1130		} else {
1131			dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1132			BufferDrive = SelectedDrive;
1133			BufferSide  = ReqSide;
1134			BufferTrack = ReqTrack;
1135			copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1136		}
1137	}
1138  
1139	if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
1140		/* read next sector */
1141		setup_req_params( SelectedDrive );
1142		do_fd_action( SelectedDrive );
1143	}
1144	else {
1145		/* all sectors finished */
1146		fd_end_request_cur(0);
1147		redo_fd_request();
1148	}
1149	return;
1150  
1151  err_end:
1152	BufferDrive = -1;
1153	fd_error();
1154}
1155
1156
1157static void fd_writetrack( void )
1158{
1159	unsigned long paddr, flags;
1160	unsigned int track;
1161	
1162	DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1163
1164	paddr = PhysTrackBuffer;
1165	dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1166
1167	fd_select_side( ReqSide );
1168  
1169	/* Cheat for track if stretch != 0 */
1170	if (SUDT->stretch) {
1171		track = FDC_READ( FDCREG_TRACK);
1172		MFPDELAY();
1173		FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1174	}
1175	udelay(40);
1176  
1177	/* Setup DMA */
1178	local_irq_save(flags);
1179	dma_wd.dma_lo = (unsigned char)paddr;
1180	MFPDELAY();
1181	paddr >>= 8;
1182	dma_wd.dma_md = (unsigned char)paddr;
1183	MFPDELAY();
1184	paddr >>= 8;
1185	if (ATARIHW_PRESENT( EXTD_DMA ))
1186		st_dma_ext_dmahi = (unsigned short)paddr;
1187	else
1188		dma_wd.dma_hi = (unsigned char)paddr;
1189	MFPDELAY();
1190	local_irq_restore(flags);
1191  
1192	/* Clear FIFO and switch DMA to correct mode */  
1193	dma_wd.dma_mode_status = 0x190;  
1194	MFPDELAY();
1195	dma_wd.dma_mode_status = 0x90;  
1196	MFPDELAY();
1197	dma_wd.dma_mode_status = 0x190;
1198	MFPDELAY();
1199  
1200	/* How many sectors for DMA */
1201	dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1202	udelay(40);  
1203  
1204	/* Start operation */
1205	dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1206	udelay(40);
1207	SET_IRQ_HANDLER( fd_writetrack_done );
1208	dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag(); 
1209
1210	MotorOn = 1;
1211	start_timeout();
1212	/* wait for interrupt */
1213}
1214
1215
1216static void fd_writetrack_done( int status )
1217{
1218	DPRINT(("fd_writetrack_done()\n"));
1219
1220	stop_timeout();
1221
1222	if (status & FDCSTAT_WPROT) {
1223		printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1224		goto err_end;
1225	}	
1226	if (status & FDCSTAT_LOST) {
1227		printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1228				SelectedDrive, ReqSide, ReqTrack );
1229		goto err_end;
1230	}
1231
1232	wake_up( &format_wait );
1233	return;
1234
1235  err_end:
1236	fd_error();
1237}
1238
1239static void fd_times_out( unsigned long dummy )
1240{
1241	atari_disable_irq( IRQ_MFP_FDC );
1242	if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1243					  * before we came here... */
1244
1245	SET_IRQ_HANDLER( NULL );
1246	/* If the timeout occurred while the readtrack_check timer was
1247	 * active, we need to cancel it, else bad things will happen */
1248	if (UseTrackbuffer)
1249		del_timer( &readtrack_timer );
1250	FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1251	udelay( 25 );
1252	
1253	printk(KERN_ERR "floppy timeout\n" );
1254	fd_error();
1255  end:
1256	atari_enable_irq( IRQ_MFP_FDC );
1257}
1258
1259
1260/* The (noop) seek operation here is needed to make the WP bit in the
1261 * FDC status register accessible for check_change. If the last disk
1262 * operation would have been a RDSEC, this bit would always read as 0
1263 * no matter what :-( To save time, the seek goes to the track we're
1264 * already on.
1265 */
1266
1267static void finish_fdc( void )
1268{
1269	if (!NeedSeek) {
1270		finish_fdc_done( 0 );
1271	}
1272	else {
1273		DPRINT(("finish_fdc: dummy seek started\n"));
1274		FDC_WRITE (FDCREG_DATA, SUD.track);
1275		SET_IRQ_HANDLER( finish_fdc_done );
1276		FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1277		MotorOn = 1;
1278		start_timeout();
1279		/* we must wait for the IRQ here, because the ST-DMA
1280		   is released immediately afterwards and the interrupt
1281		   may be delivered to the wrong driver. */
1282	  }
1283}
1284
1285
1286static void finish_fdc_done( int dummy )
1287{
1288	unsigned long flags;
1289
1290	DPRINT(("finish_fdc_done entered\n"));
1291	stop_timeout();
1292	NeedSeek = 0;
1293
1294	if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1295		/* If the check for a disk change is done too early after this
1296		 * last seek command, the WP bit still reads wrong :-((
1297		 */
1298		mod_timer(&fd_timer, jiffies + 5);
1299	else
1300		start_check_change_timer();
1301	start_motor_off_timer();
1302
1303	local_irq_save(flags);
1304	stdma_release();
1305	fdc_busy = 0;
1306	wake_up( &fdc_wait );
1307	local_irq_restore(flags);
1308
1309	DPRINT(("finish_fdc() finished\n"));
1310}
1311
1312/* The detection of disk changes is a dark chapter in Atari history :-(
1313 * Because the "Drive ready" signal isn't present in the Atari
1314 * hardware, one has to rely on the "Write Protect". This works fine,
1315 * as long as no write protected disks are used. TOS solves this
1316 * problem by introducing tri-state logic ("maybe changed") and
1317 * looking at the serial number in block 0. This isn't possible for
1318 * Linux, since the floppy driver can't make assumptions about the
1319 * filesystem used on the disk and thus the contents of block 0. I've
1320 * chosen the method to always say "The disk was changed" if it is
1321 * unsure whether it was. This implies that every open or mount
1322 * invalidates the disk buffers if you work with write protected
1323 * disks. But at least this is better than working with incorrect data
1324 * due to unrecognised disk changes.
1325 */
1326
1327static unsigned int floppy_check_events(struct gendisk *disk,
1328					unsigned int clearing)
1329{
1330	struct atari_floppy_struct *p = disk->private_data;
1331	unsigned int drive = p - unit;
1332	if (test_bit (drive, &fake_change)) {
1333		/* simulated change (e.g. after formatting) */
1334		return DISK_EVENT_MEDIA_CHANGE;
1335	}
1336	if (test_bit (drive, &changed_floppies)) {
1337		/* surely changed (the WP signal changed at least once) */
1338		return DISK_EVENT_MEDIA_CHANGE;
1339	}
1340	if (UD.wpstat) {
1341		/* WP is on -> could be changed: to be sure, buffers should be
1342		 * invalidated...
1343		 */
1344		return DISK_EVENT_MEDIA_CHANGE;
1345	}
1346
1347	return 0;
1348}
1349
1350static int floppy_revalidate(struct gendisk *disk)
1351{
1352	struct atari_floppy_struct *p = disk->private_data;
1353	unsigned int drive = p - unit;
1354
1355	if (test_bit(drive, &changed_floppies) ||
1356	    test_bit(drive, &fake_change) ||
1357	    p->disktype == 0) {
1358		if (UD.flags & FTD_MSG)
1359			printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1360		BufferDrive = -1;
1361		clear_bit(drive, &fake_change);
1362		clear_bit(drive, &changed_floppies);
1363		/* MSch: clearing geometry makes sense only for autoprobe
1364		   formats, for 'permanent user-defined' parameter:
1365		   restore default_params[] here if flagged valid! */
1366		if (default_params[drive].blocks == 0)
1367			UDT = NULL;
1368		else
1369			UDT = &default_params[drive];
1370	}
1371	return 0;
1372}
1373
1374
1375/* This sets up the global variables describing the current request. */
1376
1377static void setup_req_params( int drive )
1378{
1379	int block = ReqBlock + ReqCnt;
1380
1381	ReqTrack = block / UDT->spt;
1382	ReqSector = block - ReqTrack * UDT->spt + 1;
1383	ReqSide = ReqTrack & 1;
1384	ReqTrack >>= 1;
1385	ReqData = ReqBuffer + 512 * ReqCnt;
1386
1387	if (UseTrackbuffer)
1388		read_track = (ReqCmd == READ && fd_request->errors == 0);
1389	else
1390		read_track = 0;
1391
1392	DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1393			ReqTrack, ReqSector, (unsigned long)ReqData ));
1394}
1395
1396/*
1397 * Round-robin between our available drives, doing one request from each
1398 */
1399static struct request *set_next_request(void)
1400{
1401	struct request_queue *q;
1402	int old_pos = fdc_queue;
1403	struct request *rq = NULL;
1404
1405	do {
1406		q = unit[fdc_queue].disk->queue;
1407		if (++fdc_queue == FD_MAX_UNITS)
1408			fdc_queue = 0;
1409		if (q) {
1410			rq = blk_fetch_request(q);
1411			if (rq)
1412				break;
1413		}
1414	} while (fdc_queue != old_pos);
1415
1416	return rq;
1417}
1418
1419
1420static void redo_fd_request(void)
1421{
1422	int drive, type;
1423	struct atari_floppy_struct *floppy;
 
1424
1425	DPRINT(("redo_fd_request: fd_request=%p dev=%s fd_request->sector=%ld\n",
1426		fd_request, fd_request ? fd_request->rq_disk->disk_name : "",
1427		fd_request ? blk_rq_pos(fd_request) : 0 ));
 
 
 
 
 
 
 
 
1428
1429	IsFormatting = 0;
1430
1431repeat:
1432	if (!fd_request) {
1433		fd_request = set_next_request();
1434		if (!fd_request)
1435			goto the_end;
1436	}
1437
1438	floppy = fd_request->rq_disk->private_data;
1439	drive = floppy - unit;
1440	type = floppy->type;
1441	
1442	if (!UD.connected) {
1443		/* drive not connected */
1444		printk(KERN_ERR "Unknown Device: fd%d\n", drive );
1445		fd_end_request_cur(-EIO);
1446		goto repeat;
1447	}
1448		
1449	if (type == 0) {
1450		if (!UDT) {
1451			Probing = 1;
1452			UDT = atari_disk_type + StartDiskType[DriveType];
1453			set_capacity(floppy->disk, UDT->blocks);
1454			UD.autoprobe = 1;
1455		}
1456	} 
1457	else {
1458		/* user supplied disk type */
1459		if (--type >= NUM_DISK_MINORS) {
1460			printk(KERN_WARNING "fd%d: invalid disk format", drive );
1461			fd_end_request_cur(-EIO);
1462			goto repeat;
1463		}
1464		if (minor2disktype[type].drive_types > DriveType)  {
1465			printk(KERN_WARNING "fd%d: unsupported disk format", drive );
1466			fd_end_request_cur(-EIO);
1467			goto repeat;
1468		}
1469		type = minor2disktype[type].index;
1470		UDT = &atari_disk_type[type];
1471		set_capacity(floppy->disk, UDT->blocks);
1472		UD.autoprobe = 0;
1473	}
1474	
1475	if (blk_rq_pos(fd_request) + 1 > UDT->blocks) {
1476		fd_end_request_cur(-EIO);
1477		goto repeat;
1478	}
1479
1480	/* stop deselect timer */
1481	del_timer( &motor_off_timer );
1482		
1483	ReqCnt = 0;
1484	ReqCmd = rq_data_dir(fd_request);
1485	ReqBlock = blk_rq_pos(fd_request);
1486	ReqBuffer = fd_request->buffer;
1487	setup_req_params( drive );
1488	do_fd_action( drive );
1489
1490	return;
1491
1492  the_end:
1493	finish_fdc();
1494}
1495
1496
1497void do_fd_request(struct request_queue * q)
1498{
1499	DPRINT(("do_fd_request for pid %d\n",current->pid));
1500	while( fdc_busy ) sleep_on( &fdc_wait );
1501	fdc_busy = 1;
1502	stdma_lock(floppy_irq, NULL);
1503
1504	atari_disable_irq( IRQ_MFP_FDC );
1505	redo_fd_request();
1506	atari_enable_irq( IRQ_MFP_FDC );
 
 
 
 
1507}
1508
1509static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1510		    unsigned int cmd, unsigned long param)
1511{
1512	struct gendisk *disk = bdev->bd_disk;
1513	struct atari_floppy_struct *floppy = disk->private_data;
1514	int drive = floppy - unit;
1515	int type = floppy->type;
1516	struct atari_format_descr fmt_desc;
1517	struct atari_disk_type *dtp;
1518	struct floppy_struct getprm;
1519	int settype;
1520	struct floppy_struct setprm;
1521	void __user *argp = (void __user *)param;
1522
1523	switch (cmd) {
1524	case FDGETPRM:
1525		if (type) {
1526			if (--type >= NUM_DISK_MINORS)
1527				return -ENODEV;
1528			if (minor2disktype[type].drive_types > DriveType)
1529				return -ENODEV;
1530			type = minor2disktype[type].index;
1531			dtp = &atari_disk_type[type];
1532			if (UD.flags & FTD_MSG)
1533			    printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1534			        drive, dtp, dtp->name);
1535		}
1536		else {
1537			if (!UDT)
1538				return -ENXIO;
1539			else
1540				dtp = UDT;
1541		}
1542		memset((void *)&getprm, 0, sizeof(getprm));
1543		getprm.size = dtp->blocks;
1544		getprm.sect = dtp->spt;
1545		getprm.head = 2;
1546		getprm.track = dtp->blocks/dtp->spt/2;
1547		getprm.stretch = dtp->stretch;
1548		if (copy_to_user(argp, &getprm, sizeof(getprm)))
1549			return -EFAULT;
1550		return 0;
1551	}
1552	switch (cmd) {
1553	case FDSETPRM:
1554	case FDDEFPRM:
1555	        /* 
1556		 * MSch 7/96: simple 'set geometry' case: just set the
1557		 * 'default' device params (minor == 0).
1558		 * Currently, the drive geometry is cleared after each
1559		 * disk change and subsequent revalidate()! simple
1560		 * implementation of FDDEFPRM: save geometry from a
1561		 * FDDEFPRM call and restore it in floppy_revalidate() !
1562		 */
1563
1564		/* get the parameters from user space */
1565		if (floppy->ref != 1 && floppy->ref != -1)
1566			return -EBUSY;
1567		if (copy_from_user(&setprm, argp, sizeof(setprm)))
1568			return -EFAULT;
1569		/* 
1570		 * first of all: check for floppy change and revalidate, 
1571		 * or the next access will revalidate - and clear UDT :-(
1572		 */
1573
1574		if (floppy_check_events(disk, 0))
1575		        floppy_revalidate(disk);
1576
1577		if (UD.flags & FTD_MSG)
1578		    printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1579			drive, setprm.size, setprm.sect, setprm.stretch);
1580
1581		/* what if type > 0 here? Overwrite specified entry ? */
1582		if (type) {
1583		        /* refuse to re-set a predefined type for now */
1584			redo_fd_request();
1585			return -EINVAL;
1586		}
1587
1588		/* 
1589		 * type == 0: first look for a matching entry in the type list,
1590		 * and set the UD.disktype field to use the perdefined entry.
1591		 * TODO: add user-defined format to head of autoprobe list ? 
1592		 * Useful to include the user-type for future autodetection!
1593		 */
1594
1595		for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1596			int setidx = 0;
1597			if (minor2disktype[settype].drive_types > DriveType) {
1598				/* skip this one, invalid for drive ... */
1599				continue;
1600			}
1601			setidx = minor2disktype[settype].index;
1602			dtp = &atari_disk_type[setidx];
1603
1604			/* found matching entry ?? */
1605			if (   dtp->blocks  == setprm.size 
1606			    && dtp->spt     == setprm.sect
1607			    && dtp->stretch == setprm.stretch ) {
1608				if (UD.flags & FTD_MSG)
1609				    printk (KERN_INFO "floppy%d: setting %s %p!\n",
1610				        drive, dtp->name, dtp);
1611				UDT = dtp;
1612				set_capacity(floppy->disk, UDT->blocks);
1613
1614				if (cmd == FDDEFPRM) {
1615				  /* save settings as permanent default type */
1616				  default_params[drive].name    = dtp->name;
1617				  default_params[drive].spt     = dtp->spt;
1618				  default_params[drive].blocks  = dtp->blocks;
1619				  default_params[drive].fdc_speed = dtp->fdc_speed;
1620				  default_params[drive].stretch = dtp->stretch;
1621				}
1622				
1623				return 0;
1624			}
1625
1626		}
1627
1628		/* no matching disk type found above - setting user_params */
1629
1630	       	if (cmd == FDDEFPRM) {
1631			/* set permanent type */
1632			dtp = &default_params[drive];
1633		} else
1634			/* set user type (reset by disk change!) */
1635			dtp = &user_params[drive];
1636
1637		dtp->name   = "user format";
1638		dtp->blocks = setprm.size;
1639		dtp->spt    = setprm.sect;
1640		if (setprm.sect > 14) 
1641			dtp->fdc_speed = 3;
1642		else
1643			dtp->fdc_speed = 0;
1644		dtp->stretch = setprm.stretch;
1645
1646		if (UD.flags & FTD_MSG)
1647			printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1648				drive, dtp->blocks, dtp->spt, dtp->stretch);
1649
1650		/* sanity check */
1651		if (setprm.track != dtp->blocks/dtp->spt/2 ||
1652		    setprm.head != 2) {
1653			redo_fd_request();
1654			return -EINVAL;
1655		}
1656
1657		UDT = dtp;
1658		set_capacity(floppy->disk, UDT->blocks);
1659
1660		return 0;
1661	case FDMSGON:
1662		UD.flags |= FTD_MSG;
1663		return 0;
1664	case FDMSGOFF:
1665		UD.flags &= ~FTD_MSG;
1666		return 0;
1667	case FDSETEMSGTRESH:
1668		return -EINVAL;
1669	case FDFMTBEG:
1670		return 0;
1671	case FDFMTTRK:
1672		if (floppy->ref != 1 && floppy->ref != -1)
1673			return -EBUSY;
1674		if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
1675			return -EFAULT;
1676		return do_format(drive, type, &fmt_desc);
1677	case FDCLRPRM:
1678		UDT = NULL;
1679		/* MSch: invalidate default_params */
1680		default_params[drive].blocks  = 0;
1681		set_capacity(floppy->disk, MAX_DISK_SIZE * 2);
 
1682	case FDFMTEND:
1683	case FDFLUSH:
1684		/* invalidate the buffer track to force a reread */
1685		BufferDrive = -1;
1686		set_bit(drive, &fake_change);
1687		check_disk_change(bdev);
 
1688		return 0;
1689	default:
1690		return -EINVAL;
1691	}
1692}
1693
1694static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1695			     unsigned int cmd, unsigned long arg)
1696{
1697	int ret;
1698
1699	mutex_lock(&ataflop_mutex);
1700	ret = fd_locked_ioctl(bdev, mode, cmd, arg);
1701	mutex_unlock(&ataflop_mutex);
1702
1703	return ret;
1704}
1705
1706/* Initialize the 'unit' variable for drive 'drive' */
1707
1708static void __init fd_probe( int drive )
1709{
1710	UD.connected = 0;
1711	UDT  = NULL;
1712
1713	if (!fd_test_drive_present( drive ))
1714		return;
1715
1716	UD.connected = 1;
1717	UD.track     = 0;
1718	switch( UserSteprate[drive] ) {
1719	case 2:
1720		UD.steprate = FDCSTEP_2;
1721		break;
1722	case 3:
1723		UD.steprate = FDCSTEP_3;
1724		break;
1725	case 6:
1726		UD.steprate = FDCSTEP_6;
1727		break;
1728	case 12:
1729		UD.steprate = FDCSTEP_12;
1730		break;
1731	default: /* should be -1 for "not set by user" */
1732		if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1733			UD.steprate = FDCSTEP_3;
1734		else
1735			UD.steprate = FDCSTEP_6;
1736		break;
1737	}
1738	MotorOn = 1;	/* from probe restore operation! */
1739}
1740
1741
1742/* This function tests the physical presence of a floppy drive (not
1743 * whether a disk is inserted). This is done by issuing a restore
1744 * command, waiting max. 2 seconds (that should be enough to move the
1745 * head across the whole disk) and looking at the state of the "TR00"
1746 * signal. This should now be raised if there is a drive connected
1747 * (and there is no hardware failure :-) Otherwise, the drive is
1748 * declared absent.
1749 */
1750
1751static int __init fd_test_drive_present( int drive )
1752{
1753	unsigned long timeout;
1754	unsigned char status;
1755	int ok;
1756	
1757	if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1758	fd_select_drive( drive );
1759
1760	/* disable interrupt temporarily */
1761	atari_turnoff_irq( IRQ_MFP_FDC );
1762	FDC_WRITE (FDCREG_TRACK, 0xff00);
1763	FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1764
1765	timeout = jiffies + 2*HZ+HZ/2;
1766	while (time_before(jiffies, timeout))
1767		if (!(st_mfp.par_dt_reg & 0x20))
1768			break;
1769
1770	status = FDC_READ( FDCREG_STATUS );
1771	ok = (status & FDCSTAT_TR00) != 0;
1772
1773	/* force interrupt to abort restore operation (FDC would try
1774	 * about 50 seconds!) */
1775	FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1776	udelay(500);
1777	status = FDC_READ( FDCREG_STATUS );
1778	udelay(20);
1779
1780	if (ok) {
1781		/* dummy seek command to make WP bit accessible */
1782		FDC_WRITE( FDCREG_DATA, 0 );
1783		FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
1784		while( st_mfp.par_dt_reg & 0x20 )
1785			;
1786		status = FDC_READ( FDCREG_STATUS );
1787	}
1788
1789	atari_turnon_irq( IRQ_MFP_FDC );
1790	return( ok );
1791}
1792
1793
1794/* Look how many and which kind of drives are connected. If there are
1795 * floppies, additionally start the disk-change and motor-off timers.
1796 */
1797
1798static void __init config_types( void )
1799{
1800	int drive, cnt = 0;
1801
1802	/* for probing drives, set the FDC speed to 8 MHz */
1803	if (ATARIHW_PRESENT(FDCSPEED))
1804		dma_wd.fdc_speed = 0;
1805
1806	printk(KERN_INFO "Probing floppy drive(s):\n");
1807	for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1808		fd_probe( drive );
1809		if (UD.connected) {
1810			printk(KERN_INFO "fd%d\n", drive);
1811			++cnt;
1812		}
1813	}
1814
1815	if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1816		/* If FDC is still busy from probing, give it another FORCI
1817		 * command to abort the operation. If this isn't done, the FDC
1818		 * will interrupt later and its IRQ line stays low, because
1819		 * the status register isn't read. And this will block any
1820		 * interrupts on this IRQ line :-(
1821		 */
1822		FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1823		udelay(500);
1824		FDC_READ( FDCREG_STATUS );
1825		udelay(20);
1826	}
1827	
1828	if (cnt > 0) {
1829		start_motor_off_timer();
1830		if (cnt == 1) fd_select_drive( 0 );
1831		start_check_change_timer();
1832	}
1833}
1834
1835/*
1836 * floppy_open check for aliasing (/dev/fd0 can be the same as
1837 * /dev/PS0 etc), and disallows simultaneous access to the same
1838 * drive with different device numbers.
1839 */
1840
1841static int floppy_open(struct block_device *bdev, fmode_t mode)
1842{
1843	struct atari_floppy_struct *p = bdev->bd_disk->private_data;
1844	int type  = MINOR(bdev->bd_dev) >> 2;
1845
1846	DPRINT(("fd_open: type=%d\n",type));
1847	if (p->ref && p->type != type)
1848		return -EBUSY;
1849
1850	if (p->ref == -1 || (p->ref && mode & FMODE_EXCL))
1851		return -EBUSY;
1852
1853	if (mode & FMODE_EXCL)
1854		p->ref = -1;
1855	else
1856		p->ref++;
1857
1858	p->type = type;
1859
1860	if (mode & FMODE_NDELAY)
1861		return 0;
1862
1863	if (mode & (FMODE_READ|FMODE_WRITE)) {
1864		check_disk_change(bdev);
 
1865		if (mode & FMODE_WRITE) {
1866			if (p->wpstat) {
1867				if (p->ref < 0)
1868					p->ref = 0;
1869				else
1870					p->ref--;
1871				return -EROFS;
1872			}
1873		}
1874	}
1875	return 0;
1876}
1877
1878static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
1879{
1880	int ret;
1881
1882	mutex_lock(&ataflop_mutex);
1883	ret = floppy_open(bdev, mode);
1884	mutex_unlock(&ataflop_mutex);
1885
1886	return ret;
1887}
1888
1889static int floppy_release(struct gendisk *disk, fmode_t mode)
1890{
1891	struct atari_floppy_struct *p = disk->private_data;
1892	mutex_lock(&ataflop_mutex);
1893	if (p->ref < 0)
1894		p->ref = 0;
1895	else if (!p->ref--) {
1896		printk(KERN_ERR "floppy_release with fd_ref == 0");
1897		p->ref = 0;
1898	}
1899	mutex_unlock(&ataflop_mutex);
1900	return 0;
1901}
1902
1903static const struct block_device_operations floppy_fops = {
1904	.owner		= THIS_MODULE,
1905	.open		= floppy_unlocked_open,
1906	.release	= floppy_release,
1907	.ioctl		= fd_ioctl,
1908	.check_events	= floppy_check_events,
1909	.revalidate_disk= floppy_revalidate,
1910};
1911
1912static struct kobject *floppy_find(dev_t dev, int *part, void *data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1913{
1914	int drive = *part & 3;
1915	int type  = *part >> 2;
1916	if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1917		return NULL;
1918	*part = 0;
1919	return get_disk(unit[drive].disk);
 
 
 
 
 
 
 
 
1920}
1921
1922static int __init atari_floppy_init (void)
1923{
1924	int i;
 
1925
1926	if (!MACH_IS_ATARI)
1927		/* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
1928		return -ENODEV;
1929
1930	if (register_blkdev(FLOPPY_MAJOR,"fd"))
1931		return -EBUSY;
 
 
1932
1933	for (i = 0; i < FD_MAX_UNITS; i++) {
1934		unit[i].disk = alloc_disk(1);
1935		if (!unit[i].disk)
1936			goto Enomem;
 
 
 
 
 
 
 
 
 
 
 
 
 
1937	}
1938
1939	if (UseTrackbuffer < 0)
1940		/* not set by user -> use default: for now, we turn
1941		   track buffering off for all Medusas, though it
1942		   could be used with ones that have a counter
1943		   card. But the test is too hard :-( */
1944		UseTrackbuffer = !MACH_IS_MEDUSA;
1945
1946	/* initialize variables */
1947	SelectedDrive = -1;
1948	BufferDrive = -1;
1949
1950	DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
1951	if (!DMABuffer) {
1952		printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
1953		goto Enomem;
 
1954	}
1955	TrackBuffer = DMABuffer + 512;
1956	PhysDMABuffer = virt_to_phys(DMABuffer);
1957	PhysTrackBuffer = virt_to_phys(TrackBuffer);
1958	BufferDrive = BufferSide = BufferTrack = -1;
1959
1960	for (i = 0; i < FD_MAX_UNITS; i++) {
1961		unit[i].track = -1;
1962		unit[i].flags = 0;
1963		unit[i].disk->major = FLOPPY_MAJOR;
1964		unit[i].disk->first_minor = i;
1965		sprintf(unit[i].disk->disk_name, "fd%d", i);
1966		unit[i].disk->fops = &floppy_fops;
1967		unit[i].disk->private_data = &unit[i];
1968		unit[i].disk->queue = blk_init_queue(do_fd_request,
1969					&ataflop_lock);
1970		if (!unit[i].disk->queue)
1971			goto Enomem;
1972		set_capacity(unit[i].disk, MAX_DISK_SIZE * 2);
1973		add_disk(unit[i].disk);
1974	}
1975
1976	blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
1977				floppy_find, NULL, NULL);
1978
1979	printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
1980	       DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
1981	       UseTrackbuffer ? "" : "no ");
1982	config_types();
1983
1984	return 0;
1985Enomem:
1986	while (i--) {
1987		struct request_queue *q = unit[i].disk->queue;
1988
1989		put_disk(unit[i].disk);
1990		if (q)
1991			blk_cleanup_queue(q);
1992	}
1993
1994	unregister_blkdev(FLOPPY_MAJOR, "fd");
1995	return -ENOMEM;
 
 
1996}
1997
1998#ifndef MODULE
1999static int __init atari_floppy_setup(char *str)
2000{
2001	int ints[3 + FD_MAX_UNITS];
2002	int i;
2003
2004	if (!MACH_IS_ATARI)
2005		return 0;
2006
2007	str = get_options(str, 3 + FD_MAX_UNITS, ints);
2008	
2009	if (ints[0] < 1) {
2010		printk(KERN_ERR "ataflop_setup: no arguments!\n" );
2011		return 0;
2012	}
2013	else if (ints[0] > 2+FD_MAX_UNITS) {
2014		printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2015	}
2016
2017	if (ints[1] < 0 || ints[1] > 2)
2018		printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2019	else
2020		DriveType = ints[1];
2021
2022	if (ints[0] >= 2)
2023		UseTrackbuffer = (ints[2] > 0);
2024
2025	for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2026		if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2027			printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2028		else
2029			UserSteprate[i-3] = ints[i];
2030	}
2031	return 1;
2032}
2033
2034__setup("floppy=", atari_floppy_setup);
2035#endif
2036
2037static void __exit atari_floppy_exit(void)
2038{
2039	int i;
2040	blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
2041	for (i = 0; i < FD_MAX_UNITS; i++) {
2042		struct request_queue *q = unit[i].disk->queue;
2043
2044		del_gendisk(unit[i].disk);
2045		put_disk(unit[i].disk);
2046		blk_cleanup_queue(q);
 
 
 
 
 
 
2047	}
2048	unregister_blkdev(FLOPPY_MAJOR, "fd");
2049
2050	del_timer_sync(&fd_timer);
2051	atari_stram_free( DMABuffer );
2052}
2053
2054module_init(atari_floppy_init)
2055module_exit(atari_floppy_exit)
2056
2057MODULE_LICENSE("GPL");
v5.14.15
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  drivers/block/ataflop.c
   4 *
   5 *  Copyright (C) 1993  Greg Harp
   6 *  Atari Support by Bjoern Brauel, Roman Hodek
   7 *
   8 *  Big cleanup Sep 11..14 1994 Roman Hodek:
   9 *   - Driver now works interrupt driven
  10 *   - Support for two drives; should work, but I cannot test that :-(
  11 *   - Reading is done in whole tracks and buffered to speed up things
  12 *   - Disk change detection and drive deselecting after motor-off
  13 *     similar to TOS
  14 *   - Autodetection of disk format (DD/HD); untested yet, because I
  15 *     don't have an HD drive :-(
  16 *
  17 *  Fixes Nov 13 1994 Martin Schaller:
  18 *   - Autodetection works now
  19 *   - Support for 5 1/4'' disks
  20 *   - Removed drive type (unknown on atari)
  21 *   - Do seeks with 8 Mhz
  22 *
  23 *  Changes by Andreas Schwab:
  24 *   - After errors in multiple read mode try again reading single sectors
  25 *  (Feb 1995):
  26 *   - Clean up error handling
  27 *   - Set blk_size for proper size checking
  28 *   - Initialize track register when testing presence of floppy
  29 *   - Implement some ioctl's
  30 *
  31 *  Changes by Torsten Lang:
  32 *   - When probing the floppies we should add the FDCCMDADD_H flag since
  33 *     the FDC will otherwise wait forever when no disk is inserted...
  34 *
  35 * ++ Freddi Aschwanden (fa) 20.9.95 fixes for medusa:
  36 *  - MFPDELAY() after each FDC access -> atari 
  37 *  - more/other disk formats
  38 *  - DMA to the block buffer directly if we have a 32bit DMA
  39 *  - for medusa, the step rate is always 3ms
  40 *  - on medusa, use only cache_push()
  41 * Roman:
  42 *  - Make disk format numbering independent from minors
  43 *  - Let user set max. supported drive type (speeds up format
  44 *    detection, saves buffer space)
  45 *
  46 * Roman 10/15/95:
  47 *  - implement some more ioctls
  48 *  - disk formatting
  49 *  
  50 * Andreas 95/12/12:
  51 *  - increase gap size at start of track for HD/ED disks
  52 *
  53 * Michael (MSch) 11/07/96:
  54 *  - implemented FDSETPRM and FDDEFPRM ioctl
  55 *
  56 * Andreas (97/03/19):
  57 *  - implemented missing BLK* ioctls
  58 *
  59 *  Things left to do:
  60 *   - Formatting
  61 *   - Maybe a better strategy for disk change detection (does anyone
  62 *     know one?)
  63 */
  64
  65#include <linux/module.h>
  66
  67#include <linux/fd.h>
  68#include <linux/delay.h>
  69#include <linux/init.h>
  70#include <linux/blk-mq.h>
  71#include <linux/mutex.h>
  72#include <linux/completion.h>
  73#include <linux/wait.h>
  74
 
 
  75#include <asm/atariints.h>
  76#include <asm/atari_stdma.h>
  77#include <asm/atari_stram.h>
  78
  79#define	FD_MAX_UNITS 2
  80
  81#undef DEBUG
  82
  83static DEFINE_MUTEX(ataflop_mutex);
  84static struct request *fd_request;
  85
  86/*
  87 * WD1772 stuff
  88 */
  89
  90/* register codes */
  91
  92#define FDCSELREG_STP   (0x80)   /* command/status register */
  93#define FDCSELREG_TRA   (0x82)   /* track register */
  94#define FDCSELREG_SEC   (0x84)   /* sector register */
  95#define FDCSELREG_DTA   (0x86)   /* data register */
  96
  97/* register names for FDC_READ/WRITE macros */
  98
  99#define FDCREG_CMD		0
 100#define FDCREG_STATUS	0
 101#define FDCREG_TRACK	2
 102#define FDCREG_SECTOR	4
 103#define FDCREG_DATA		6
 104
 105/* command opcodes */
 106
 107#define FDCCMD_RESTORE  (0x00)   /*  -                   */
 108#define FDCCMD_SEEK     (0x10)   /*   |                  */
 109#define FDCCMD_STEP     (0x20)   /*   |  TYP 1 Commands  */
 110#define FDCCMD_STIN     (0x40)   /*   |                  */
 111#define FDCCMD_STOT     (0x60)   /*  -                   */
 112#define FDCCMD_RDSEC    (0x80)   /*  -   TYP 2 Commands  */
 113#define FDCCMD_WRSEC    (0xa0)   /*  -          "        */
 114#define FDCCMD_RDADR    (0xc0)   /*  -                   */
 115#define FDCCMD_RDTRA    (0xe0)   /*   |  TYP 3 Commands  */
 116#define FDCCMD_WRTRA    (0xf0)   /*  -                   */
 117#define FDCCMD_FORCI    (0xd0)   /*  -   TYP 4 Command   */
 118
 119/* command modifier bits */
 120
 121#define FDCCMDADD_SR6   (0x00)   /* step rate settings */
 122#define FDCCMDADD_SR12  (0x01)
 123#define FDCCMDADD_SR2   (0x02)
 124#define FDCCMDADD_SR3   (0x03)
 125#define FDCCMDADD_V     (0x04)   /* verify */
 126#define FDCCMDADD_H     (0x08)   /* wait for spin-up */
 127#define FDCCMDADD_U     (0x10)   /* update track register */
 128#define FDCCMDADD_M     (0x10)   /* multiple sector access */
 129#define FDCCMDADD_E     (0x04)   /* head settling flag */
 130#define FDCCMDADD_P     (0x02)   /* precompensation off */
 131#define FDCCMDADD_A0    (0x01)   /* DAM flag */
 132
 133/* status register bits */
 134
 135#define	FDCSTAT_MOTORON	(0x80)   /* motor on */
 136#define	FDCSTAT_WPROT	(0x40)   /* write protected (FDCCMD_WR*) */
 137#define	FDCSTAT_SPINUP	(0x20)   /* motor speed stable (Type I) */
 138#define	FDCSTAT_DELDAM	(0x20)   /* sector has deleted DAM (Type II+III) */
 139#define	FDCSTAT_RECNF	(0x10)   /* record not found */
 140#define	FDCSTAT_CRC		(0x08)   /* CRC error */
 141#define	FDCSTAT_TR00	(0x04)   /* Track 00 flag (Type I) */
 142#define	FDCSTAT_LOST	(0x04)   /* Lost Data (Type II+III) */
 143#define	FDCSTAT_IDX		(0x02)   /* Index status (Type I) */
 144#define	FDCSTAT_DRQ		(0x02)   /* DRQ status (Type II+III) */
 145#define	FDCSTAT_BUSY	(0x01)   /* FDC is busy */
 146
 147
 148/* PSG Port A Bit Nr 0 .. Side Sel .. 0 -> Side 1  1 -> Side 2 */
 149#define DSKSIDE     (0x01)
 150
 151#define DSKDRVNONE  (0x06)
 152#define DSKDRV0     (0x02)
 153#define DSKDRV1     (0x04)
 154
 155/* step rates */
 156#define	FDCSTEP_6	0x00
 157#define	FDCSTEP_12	0x01
 158#define	FDCSTEP_2	0x02
 159#define	FDCSTEP_3	0x03
 160
 161struct atari_format_descr {
 162	int track;		/* to be formatted */
 163	int head;		/*   ""     ""     */
 164	int sect_offset;	/* offset of first sector */
 165};
 166
 167/* Disk types: DD, HD, ED */
 168static struct atari_disk_type {
 169	const char	*name;
 170	unsigned	spt;		/* sectors per track */
 171	unsigned	blocks;		/* total number of blocks */
 172	unsigned	fdc_speed;	/* fdc_speed setting */
 173	unsigned 	stretch;	/* track doubling ? */
 174} atari_disk_type[] = {
 175	{ "d360",  9, 720, 0, 0},	/*  0: 360kB diskette */
 176	{ "D360",  9, 720, 0, 1},	/*  1: 360kb in 720k or 1.2MB drive */
 177	{ "D720",  9,1440, 0, 0},	/*  2: 720kb in 720k or 1.2MB drive */
 178	{ "D820", 10,1640, 0, 0},	/*  3: DD disk with 82 tracks/10 sectors */
 179/* formats above are probed for type DD */
 180#define	MAX_TYPE_DD 3
 181	{ "h1200",15,2400, 3, 0},	/*  4: 1.2MB diskette */
 182	{ "H1440",18,2880, 3, 0},	/*  5: 1.4 MB diskette (HD) */
 183	{ "H1640",20,3280, 3, 0},	/*  6: 1.64MB diskette (fat HD) 82 tr 20 sec */
 184/* formats above are probed for types DD and HD */
 185#define	MAX_TYPE_HD 6
 186	{ "E2880",36,5760, 3, 0},	/*  7: 2.8 MB diskette (ED) */
 187	{ "E3280",40,6560, 3, 0},	/*  8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
 188/* formats above are probed for types DD, HD and ED */
 189#define	MAX_TYPE_ED 8
 190/* types below are never autoprobed */
 191	{ "H1680",21,3360, 3, 0},	/*  9: 1.68MB diskette (fat HD) 80 tr 21 sec */
 192	{ "h410",10,820, 0, 1},		/* 10: 410k diskette 41 tr 10 sec, stretch */
 193	{ "h1476",18,2952, 3, 0},	/* 11: 1.48MB diskette 82 tr 18 sec */
 194	{ "H1722",21,3444, 3, 0},	/* 12: 1.72MB diskette 82 tr 21 sec */
 195	{ "h420",10,840, 0, 1},		/* 13: 420k diskette 42 tr 10 sec, stretch */
 196	{ "H830",10,1660, 0, 0},	/* 14: 820k diskette 83 tr 10 sec */
 197	{ "h1494",18,2952, 3, 0},	/* 15: 1.49MB diskette 83 tr 18 sec */
 198	{ "H1743",21,3486, 3, 0},	/* 16: 1.74MB diskette 83 tr 21 sec */
 199	{ "h880",11,1760, 0, 0},	/* 17: 880k diskette 80 tr 11 sec */
 200	{ "D1040",13,2080, 0, 0},	/* 18: 1.04MB diskette 80 tr 13 sec */
 201	{ "D1120",14,2240, 0, 0},	/* 19: 1.12MB diskette 80 tr 14 sec */
 202	{ "h1600",20,3200, 3, 0},	/* 20: 1.60MB diskette 80 tr 20 sec */
 203	{ "H1760",22,3520, 3, 0},	/* 21: 1.76MB diskette 80 tr 22 sec */
 204	{ "H1920",24,3840, 3, 0},	/* 22: 1.92MB diskette 80 tr 24 sec */
 205	{ "E3200",40,6400, 3, 0},	/* 23: 3.2MB diskette 80 tr 40 sec */
 206	{ "E3520",44,7040, 3, 0},	/* 24: 3.52MB diskette 80 tr 44 sec */
 207	{ "E3840",48,7680, 3, 0},	/* 25: 3.84MB diskette 80 tr 48 sec */
 208	{ "H1840",23,3680, 3, 0},	/* 26: 1.84MB diskette 80 tr 23 sec */
 209	{ "D800",10,1600, 0, 0},	/* 27: 800k diskette 80 tr 10 sec */
 210};
 211
 212static int StartDiskType[] = {
 213	MAX_TYPE_DD,
 214	MAX_TYPE_HD,
 215	MAX_TYPE_ED
 216};
 217
 218#define	TYPE_DD		0
 219#define	TYPE_HD		1
 220#define	TYPE_ED		2
 221
 222static int DriveType = TYPE_HD;
 223
 224static DEFINE_SPINLOCK(ataflop_lock);
 225
 226/* Array for translating minors into disk formats */
 227static struct {
 228	int 	 index;
 229	unsigned drive_types;
 230} minor2disktype[] = {
 231	{  0, TYPE_DD },	/*  1: d360 */
 232	{  4, TYPE_HD },	/*  2: h1200 */
 233	{  1, TYPE_DD },	/*  3: D360 */
 234	{  2, TYPE_DD },	/*  4: D720 */
 235	{  1, TYPE_DD },	/*  5: h360 = D360 */
 236	{  2, TYPE_DD },	/*  6: h720 = D720 */
 237	{  5, TYPE_HD },	/*  7: H1440 */
 238	{  7, TYPE_ED },	/*  8: E2880 */
 239/* some PC formats :-) */
 240	{  8, TYPE_ED },	/*  9: E3280    <- was "CompaQ" == E2880 for PC */
 241	{  5, TYPE_HD },	/* 10: h1440 = H1440 */
 242	{  9, TYPE_HD },	/* 11: H1680 */
 243	{ 10, TYPE_DD },	/* 12: h410  */
 244	{  3, TYPE_DD },	/* 13: H820     <- == D820, 82x10 */
 245	{ 11, TYPE_HD },	/* 14: h1476 */
 246	{ 12, TYPE_HD },	/* 15: H1722 */
 247	{ 13, TYPE_DD },	/* 16: h420  */
 248	{ 14, TYPE_DD },	/* 17: H830  */
 249	{ 15, TYPE_HD },	/* 18: h1494 */
 250	{ 16, TYPE_HD },	/* 19: H1743 */
 251	{ 17, TYPE_DD },	/* 20: h880  */
 252	{ 18, TYPE_DD },	/* 21: D1040 */
 253	{ 19, TYPE_DD },	/* 22: D1120 */
 254	{ 20, TYPE_HD },	/* 23: h1600 */
 255	{ 21, TYPE_HD },	/* 24: H1760 */
 256	{ 22, TYPE_HD },	/* 25: H1920 */
 257	{ 23, TYPE_ED },	/* 26: E3200 */
 258	{ 24, TYPE_ED },	/* 27: E3520 */
 259	{ 25, TYPE_ED },	/* 28: E3840 */
 260	{ 26, TYPE_HD },	/* 29: H1840 */
 261	{ 27, TYPE_DD },	/* 30: D800  */
 262	{  6, TYPE_HD },	/* 31: H1640    <- was H1600 == h1600 for PC */
 263};
 264
 265#define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
 266
 267/*
 268 * Maximum disk size (in kilobytes). This default is used whenever the
 269 * current disk size is unknown.
 270 */
 271#define MAX_DISK_SIZE 3280
 272
 273/*
 274 * MSch: User-provided type information. 'drive' points to
 275 * the respective entry of this array. Set by FDSETPRM ioctls.
 276 */
 277static struct atari_disk_type user_params[FD_MAX_UNITS];
 278
 279/*
 280 * User-provided permanent type information. 'drive' points to
 281 * the respective entry of this array.  Set by FDDEFPRM ioctls, 
 282 * restored upon disk change by floppy_revalidate() if valid (as seen by
 283 * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
 284 */
 285static struct atari_disk_type default_params[FD_MAX_UNITS];
 286
 287/* current info on each unit */
 288static struct atari_floppy_struct {
 289	int connected;				/* !=0 : drive is connected */
 290	int autoprobe;				/* !=0 : do autoprobe	    */
 291
 292	struct atari_disk_type	*disktype;	/* current type of disk */
 293
 294	int track;		/* current head position or -1 if
 295				   unknown */
 296	unsigned int steprate;	/* steprate setting */
 297	unsigned int wpstat;	/* current state of WP signal (for
 298				   disk change detection) */
 299	int flags;		/* flags */
 300	struct gendisk *disk[NUM_DISK_MINORS];
 301	int ref;
 302	int type;
 303	struct blk_mq_tag_set tag_set;
 304} unit[FD_MAX_UNITS];
 305
 306#define	UD	unit[drive]
 307#define	UDT	unit[drive].disktype
 308#define	SUD	unit[SelectedDrive]
 309#define	SUDT	unit[SelectedDrive].disktype
 310
 311
 312#define FDC_READ(reg) ({			\
 313    /* unsigned long __flags; */		\
 314    unsigned short __val;			\
 315    /* local_irq_save(__flags); */		\
 316    dma_wd.dma_mode_status = 0x80 | (reg);	\
 317    udelay(25);					\
 318    __val = dma_wd.fdc_acces_seccount;		\
 319    MFPDELAY();					\
 320    /* local_irq_restore(__flags); */		\
 321    __val & 0xff;				\
 322})
 323
 324#define FDC_WRITE(reg,val)			\
 325    do {					\
 326	/* unsigned long __flags; */		\
 327	/* local_irq_save(__flags); */		\
 328	dma_wd.dma_mode_status = 0x80 | (reg);	\
 329	udelay(25);				\
 330	dma_wd.fdc_acces_seccount = (val);	\
 331	MFPDELAY();				\
 332        /* local_irq_restore(__flags); */	\
 333    } while(0)
 334
 335
 336/* Buffering variables:
 337 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
 338 * operations. Second, a track buffer is used to cache a whole track
 339 * of the disk to save read operations. These are two separate buffers
 340 * because that allows write operations without clearing the track buffer.
 341 */
 342
 343static int MaxSectors[] = {
 344	11, 22, 44
 345};
 346static int BufferSize[] = {
 347	15*512, 30*512, 60*512
 348};
 349
 350#define	BUFFER_SIZE	(BufferSize[DriveType])
 351
 352unsigned char *DMABuffer;			  /* buffer for writes */
 353static unsigned long PhysDMABuffer;   /* physical address */
 354
 355static int UseTrackbuffer = -1;		  /* Do track buffering? */
 356module_param(UseTrackbuffer, int, 0);
 357
 358unsigned char *TrackBuffer;			  /* buffer for reads */
 359static unsigned long PhysTrackBuffer; /* physical address */
 360static int BufferDrive, BufferSide, BufferTrack;
 361static int read_track;		/* non-zero if we are reading whole tracks */
 362
 363#define	SECTOR_BUFFER(sec)	(TrackBuffer + ((sec)-1)*512)
 364#define	IS_BUFFERED(drive,side,track) \
 365    (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
 366
 367/*
 368 * These are global variables, as that's the easiest way to give
 369 * information to interrupts. They are the data used for the current
 370 * request.
 371 */
 372static int SelectedDrive = 0;
 373static int ReqCmd, ReqBlock;
 374static int ReqSide, ReqTrack, ReqSector, ReqCnt;
 375static int HeadSettleFlag = 0;
 376static unsigned char *ReqData, *ReqBuffer;
 377static int MotorOn = 0, MotorOffTrys;
 378static int IsFormatting = 0, FormatError;
 379
 380static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
 381module_param_array(UserSteprate, int, NULL, 0);
 382
 383static DECLARE_COMPLETION(format_wait);
 
 
 
 384
 385static unsigned long changed_floppies = 0xff, fake_change = 0;
 386#define	CHECK_CHANGE_DELAY	HZ/2
 387
 388#define	FD_MOTOR_OFF_DELAY	(3*HZ)
 389#define	FD_MOTOR_OFF_MAXTRY	(10*20)
 390
 391#define FLOPPY_TIMEOUT		(6*HZ)
 392#define RECALIBRATE_ERRORS	4	/* After this many errors the drive
 393					 * will be recalibrated. */
 394#define MAX_ERRORS		8	/* After this many errors the driver
 395					 * will give up. */
 396
 397
 398/*
 399 * The driver is trying to determine the correct media format
 400 * while Probing is set. fd_rwsec_done() clears it after a
 401 * successful access.
 402 */
 403static int Probing = 0;
 404
 405/* This flag is set when a dummy seek is necessary to make the WP
 406 * status bit accessible.
 407 */
 408static int NeedSeek = 0;
 409
 410
 411#ifdef DEBUG
 412#define DPRINT(a)	printk a
 413#else
 414#define DPRINT(a)
 415#endif
 416
 417/***************************** Prototypes *****************************/
 418
 419static void fd_select_side( int side );
 420static void fd_select_drive( int drive );
 421static void fd_deselect( void );
 422static void fd_motor_off_timer(struct timer_list *unused);
 423static void check_change(struct timer_list *unused);
 424static irqreturn_t floppy_irq (int irq, void *dummy);
 425static void fd_error( void );
 426static int do_format(int drive, int type, struct atari_format_descr *desc);
 427static void do_fd_action( int drive );
 428static void fd_calibrate( void );
 429static void fd_calibrate_done( int status );
 430static void fd_seek( void );
 431static void fd_seek_done( int status );
 432static void fd_rwsec( void );
 433static void fd_readtrack_check(struct timer_list *unused);
 434static void fd_rwsec_done( int status );
 435static void fd_rwsec_done1(int status);
 436static void fd_writetrack( void );
 437static void fd_writetrack_done( int status );
 438static void fd_times_out(struct timer_list *unused);
 439static void finish_fdc( void );
 440static void finish_fdc_done( int dummy );
 441static void setup_req_params( int drive );
 
 442static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
 443                     cmd, unsigned long param);
 444static void fd_probe( int drive );
 445static int fd_test_drive_present( int drive );
 446static void config_types( void );
 447static int floppy_open(struct block_device *bdev, fmode_t mode);
 448static void floppy_release(struct gendisk *disk, fmode_t mode);
 449
 450/************************* End of Prototypes **************************/
 451
 452static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer);
 453static DEFINE_TIMER(readtrack_timer, fd_readtrack_check);
 454static DEFINE_TIMER(timeout_timer, fd_times_out);
 455static DEFINE_TIMER(fd_timer, check_change);
 456	
 457static void fd_end_request_cur(blk_status_t err)
 458{
 459	if (!blk_update_request(fd_request, err,
 460				blk_rq_cur_bytes(fd_request))) {
 461		__blk_mq_end_request(fd_request, err);
 462		fd_request = NULL;
 463	}
 464}
 465
 466static inline void start_motor_off_timer(void)
 467{
 468	mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
 469	MotorOffTrys = 0;
 470}
 471
 472static inline void start_check_change_timer( void )
 473{
 474	mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
 475}
 476
 477static inline void start_timeout(void)
 478{
 479	mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
 480}
 481
 482static inline void stop_timeout(void)
 483{
 484	del_timer(&timeout_timer);
 485}
 486
 487/* Select the side to use. */
 488
 489static void fd_select_side( int side )
 490{
 491	unsigned long flags;
 492
 493	/* protect against various other ints mucking around with the PSG */
 494	local_irq_save(flags);
 495  
 496	sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
 497	sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
 498	                                 sound_ym.rd_data_reg_sel & 0xfe;
 499
 500	local_irq_restore(flags);
 501}
 502
 503
 504/* Select a drive, update the FDC's track register and set the correct
 505 * clock speed for this disk's type.
 506 */
 507
 508static void fd_select_drive( int drive )
 509{
 510	unsigned long flags;
 511	unsigned char tmp;
 512  
 513	if (drive == SelectedDrive)
 514	  return;
 515
 516	/* protect against various other ints mucking around with the PSG */
 517	local_irq_save(flags);
 518	sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
 519	tmp = sound_ym.rd_data_reg_sel;
 520	sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
 521	atari_dont_touch_floppy_select = 1;
 522	local_irq_restore(flags);
 523
 524	/* restore track register to saved value */
 525	FDC_WRITE( FDCREG_TRACK, UD.track );
 526	udelay(25);
 527
 528	/* select 8/16 MHz */
 529	if (UDT)
 530		if (ATARIHW_PRESENT(FDCSPEED))
 531			dma_wd.fdc_speed = UDT->fdc_speed;
 532	
 533	SelectedDrive = drive;
 534}
 535
 536
 537/* Deselect both drives. */
 538
 539static void fd_deselect( void )
 540{
 541	unsigned long flags;
 542
 543	/* protect against various other ints mucking around with the PSG */
 544	local_irq_save(flags);
 545	atari_dont_touch_floppy_select = 0;
 546	sound_ym.rd_data_reg_sel=14;	/* Select PSG Port A */
 547	sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
 548			    (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
 549	/* On Falcon, the drive B select line is used on the printer port, so
 550	 * leave it alone... */
 551	SelectedDrive = -1;
 552	local_irq_restore(flags);
 553}
 554
 555
 556/* This timer function deselects the drives when the FDC switched the
 557 * motor off. The deselection cannot happen earlier because the FDC
 558 * counts the index signals, which arrive only if one drive is selected.
 559 */
 560
 561static void fd_motor_off_timer(struct timer_list *unused)
 562{
 563	unsigned char status;
 564
 565	if (SelectedDrive < 0)
 566		/* no drive selected, needn't deselect anyone */
 567		return;
 568
 569	if (stdma_islocked())
 570		goto retry;
 571
 572	status = FDC_READ( FDCREG_STATUS );
 573
 574	if (!(status & 0x80)) {
 575		/* motor already turned off by FDC -> deselect drives */
 576		MotorOn = 0;
 577		fd_deselect();
 578		return;
 579	}
 580	/* not yet off, try again */
 581
 582  retry:
 583	/* Test again later; if tested too often, it seems there is no disk
 584	 * in the drive and the FDC will leave the motor on forever (or,
 585	 * at least until a disk is inserted). So we'll test only twice
 586	 * per second from then on...
 587	 */
 588	mod_timer(&motor_off_timer,
 589		  jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
 590}
 591
 592
 593/* This function is repeatedly called to detect disk changes (as good
 594 * as possible) and keep track of the current state of the write protection.
 595 */
 596
 597static void check_change(struct timer_list *unused)
 598{
 599	static int    drive = 0;
 600
 601	unsigned long flags;
 602	unsigned char old_porta;
 603	int			  stat;
 604
 605	if (++drive > 1 || !UD.connected)
 606		drive = 0;
 607
 608	/* protect against various other ints mucking around with the PSG */
 609	local_irq_save(flags);
 610
 611	if (!stdma_islocked()) {
 612		sound_ym.rd_data_reg_sel = 14;
 613		old_porta = sound_ym.rd_data_reg_sel;
 614		sound_ym.wd_data = (old_porta | DSKDRVNONE) &
 615			               ~(drive == 0 ? DSKDRV0 : DSKDRV1);
 616		stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
 617		sound_ym.wd_data = old_porta;
 618
 619		if (stat != UD.wpstat) {
 620			DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
 621			UD.wpstat = stat;
 622			set_bit (drive, &changed_floppies);
 623		}
 624	}
 625	local_irq_restore(flags);
 626
 627	start_check_change_timer();
 628}
 629
 630 
 631/* Handling of the Head Settling Flag: This flag should be set after each
 632 * seek operation, because we don't use seeks with verify.
 633 */
 634
 635static inline void set_head_settle_flag(void)
 636{
 637	HeadSettleFlag = FDCCMDADD_E;
 638}
 639
 640static inline int get_head_settle_flag(void)
 641{
 642	int	tmp = HeadSettleFlag;
 643	HeadSettleFlag = 0;
 644	return( tmp );
 645}
 646
 647static inline void copy_buffer(void *from, void *to)
 648{
 649	ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
 650	int cnt;
 651
 652	for (cnt = 512/4; cnt; cnt--)
 653		*p2++ = *p1++;
 654}
 655
 656  
 657  
 658
 659/* General Interrupt Handling */
 660
 661static void (*FloppyIRQHandler)( int status ) = NULL;
 662
 663static irqreturn_t floppy_irq (int irq, void *dummy)
 664{
 665	unsigned char status;
 666	void (*handler)( int );
 667
 668	handler = xchg(&FloppyIRQHandler, NULL);
 669
 670	if (handler) {
 671		nop();
 672		status = FDC_READ( FDCREG_STATUS );
 673		DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
 674		handler( status );
 675	}
 676	else {
 677		DPRINT(("FDC irq, no handler\n"));
 678	}
 679	return IRQ_HANDLED;
 680}
 681
 682
 683/* Error handling: If some error happened, retry some times, then
 684 * recalibrate, then try again, and fail after MAX_ERRORS.
 685 */
 686
 687static void fd_error( void )
 688{
 689	if (IsFormatting) {
 690		IsFormatting = 0;
 691		FormatError = 1;
 692		complete(&format_wait);
 693		return;
 694	}
 695
 696	if (!fd_request)
 697		return;
 698
 699	fd_request->error_count++;
 700	if (fd_request->error_count >= MAX_ERRORS) {
 701		printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
 702		fd_end_request_cur(BLK_STS_IOERR);
 703	}
 704	else if (fd_request->error_count == RECALIBRATE_ERRORS) {
 705		printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
 706		if (SelectedDrive != -1)
 707			SUD.track = -1;
 708	}
 
 709}
 710
 711
 712
 713#define	SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
 714
 715
 716/* ---------- Formatting ---------- */
 717
 718#define FILL(n,val)		\
 719    do {			\
 720	memset( p, val, n );	\
 721	p += n;			\
 722    } while(0)
 723
 724static int do_format(int drive, int type, struct atari_format_descr *desc)
 725{
 726	struct request_queue *q;
 727	unsigned char	*p;
 728	int sect, nsect;
 729	unsigned long	flags;
 730	int ret;
 731
 732	if (type) {
 733		type--;
 734		if (type >= NUM_DISK_MINORS ||
 735		    minor2disktype[type].drive_types > DriveType)
 736			return -EINVAL;
 737	}
 738
 739	q = unit[drive].disk[type]->queue;
 740	blk_mq_freeze_queue(q);
 741	blk_mq_quiesce_queue(q);
 742
 743	local_irq_save(flags);
 
 
 744	stdma_lock(floppy_irq, NULL);
 745	atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
 746	local_irq_restore(flags);
 747
 748	if (type) {
 
 
 
 
 
 749		type = minor2disktype[type].index;
 750		UDT = &atari_disk_type[type];
 751	}
 752
 753	if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
 754		ret = -EINVAL;
 755		goto out;
 756	}
 757
 758	nsect = UDT->spt;
 759	p = TrackBuffer;
 760	/* The track buffer is used for the raw track data, so its
 761	   contents become invalid! */
 762	BufferDrive = -1;
 763	/* stop deselect timer */
 764	del_timer( &motor_off_timer );
 765
 766	FILL( 60 * (nsect / 9), 0x4e );
 767	for( sect = 0; sect < nsect; ++sect ) {
 768		FILL( 12, 0 );
 769		FILL( 3, 0xf5 );
 770		*p++ = 0xfe;
 771		*p++ = desc->track;
 772		*p++ = desc->head;
 773		*p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
 774		*p++ = 2;
 775		*p++ = 0xf7;
 776		FILL( 22, 0x4e );
 777		FILL( 12, 0 );
 778		FILL( 3, 0xf5 );
 779		*p++ = 0xfb;
 780		FILL( 512, 0xe5 );
 781		*p++ = 0xf7;
 782		FILL( 40, 0x4e );
 783	}
 784	FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
 785
 786	IsFormatting = 1;
 787	FormatError = 0;
 788	ReqTrack = desc->track;
 789	ReqSide  = desc->head;
 790	do_fd_action( drive );
 791
 792	wait_for_completion(&format_wait);
 793
 794	ret = FormatError ? -EIO : 0;
 795out:
 796	blk_mq_unquiesce_queue(q);
 797	blk_mq_unfreeze_queue(q);
 798	return ret;
 799}
 800
 801
 802/* do_fd_action() is the general procedure for a fd request: All
 803 * required parameter settings (drive select, side select, track
 804 * position) are checked and set if needed. For each of these
 805 * parameters and the actual reading or writing exist two functions:
 806 * one that starts the setting (or skips it if possible) and one
 807 * callback for the "done" interrupt. Each done func calls the next
 808 * set function to propagate the request down to fd_rwsec_done().
 809 */
 810
 811static void do_fd_action( int drive )
 812{
 813	DPRINT(("do_fd_action\n"));
 814	
 815	if (UseTrackbuffer && !IsFormatting) {
 816	repeat:
 817	    if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
 818		if (ReqCmd == READ) {
 819		    copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
 820		    if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
 821			/* read next sector */
 822			setup_req_params( drive );
 823			goto repeat;
 824		    }
 825		    else {
 826			/* all sectors finished */
 827			fd_end_request_cur(BLK_STS_OK);
 
 828			return;
 829		    }
 830		}
 831		else {
 832		    /* cmd == WRITE, pay attention to track buffer
 833		     * consistency! */
 834		    copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
 835		}
 836	    }
 837	}
 838
 839	if (SelectedDrive != drive)
 840		fd_select_drive( drive );
 841    
 842	if (UD.track == -1)
 843		fd_calibrate();
 844	else if (UD.track != ReqTrack << UDT->stretch)
 845		fd_seek();
 846	else if (IsFormatting)
 847		fd_writetrack();
 848	else
 849		fd_rwsec();
 850}
 851
 852
 853/* Seek to track 0 if the current track is unknown */
 854
 855static void fd_calibrate( void )
 856{
 857	if (SUD.track >= 0) {
 858		fd_calibrate_done( 0 );
 859		return;
 860	}
 861
 862	if (ATARIHW_PRESENT(FDCSPEED))
 863		dma_wd.fdc_speed = 0;   /* always seek with 8 Mhz */
 864	DPRINT(("fd_calibrate\n"));
 865	SET_IRQ_HANDLER( fd_calibrate_done );
 866	/* we can't verify, since the speed may be incorrect */
 867	FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
 868
 869	NeedSeek = 1;
 870	MotorOn = 1;
 871	start_timeout();
 872	/* wait for IRQ */
 873}
 874
 875
 876static void fd_calibrate_done( int status )
 877{
 878	DPRINT(("fd_calibrate_done()\n"));
 879	stop_timeout();
 880    
 881	/* set the correct speed now */
 882	if (ATARIHW_PRESENT(FDCSPEED))
 883		dma_wd.fdc_speed = SUDT->fdc_speed;
 884	if (status & FDCSTAT_RECNF) {
 885		printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
 886		fd_error();
 887	}
 888	else {
 889		SUD.track = 0;
 890		fd_seek();
 891	}
 892}
 893  
 894  
 895/* Seek the drive to the requested track. The drive must have been
 896 * calibrated at some point before this.
 897 */
 898  
 899static void fd_seek( void )
 900{
 901	if (SUD.track == ReqTrack << SUDT->stretch) {
 902		fd_seek_done( 0 );
 903		return;
 904	}
 905
 906	if (ATARIHW_PRESENT(FDCSPEED)) {
 907		dma_wd.fdc_speed = 0;	/* always seek witch 8 Mhz */
 908		MFPDELAY();
 909	}
 910
 911	DPRINT(("fd_seek() to track %d\n",ReqTrack));
 912	FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
 913	udelay(25);
 914	SET_IRQ_HANDLER( fd_seek_done );
 915	FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
 916
 917	MotorOn = 1;
 918	set_head_settle_flag();
 919	start_timeout();
 920	/* wait for IRQ */
 921}
 922
 923
 924static void fd_seek_done( int status )
 925{
 926	DPRINT(("fd_seek_done()\n"));
 927	stop_timeout();
 928	
 929	/* set the correct speed */
 930	if (ATARIHW_PRESENT(FDCSPEED))
 931		dma_wd.fdc_speed = SUDT->fdc_speed;
 932	if (status & FDCSTAT_RECNF) {
 933		printk(KERN_ERR "fd%d: seek error (to track %d)\n",
 934				SelectedDrive, ReqTrack );
 935		/* we don't know exactly which track we are on now! */
 936		SUD.track = -1;
 937		fd_error();
 938	}
 939	else {
 940		SUD.track = ReqTrack << SUDT->stretch;
 941		NeedSeek = 0;
 942		if (IsFormatting)
 943			fd_writetrack();
 944		else
 945			fd_rwsec();
 946	}
 947}
 948
 949
 950/* This does the actual reading/writing after positioning the head
 951 * over the correct track.
 952 */
 953
 954static int MultReadInProgress = 0;
 955
 956
 957static void fd_rwsec( void )
 958{
 959	unsigned long paddr, flags;
 960	unsigned int  rwflag, old_motoron;
 961	unsigned int track;
 962	
 963	DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
 964	if (ReqCmd == WRITE) {
 965		if (ATARIHW_PRESENT(EXTD_DMA)) {
 966			paddr = virt_to_phys(ReqData);
 967		}
 968		else {
 969			copy_buffer( ReqData, DMABuffer );
 970			paddr = PhysDMABuffer;
 971		}
 972		dma_cache_maintenance( paddr, 512, 1 );
 973		rwflag = 0x100;
 974	}
 975	else {
 976		if (read_track)
 977			paddr = PhysTrackBuffer;
 978		else
 979			paddr = ATARIHW_PRESENT(EXTD_DMA) ? 
 980				virt_to_phys(ReqData) : PhysDMABuffer;
 981		rwflag = 0;
 982	}
 983
 984	fd_select_side( ReqSide );
 985  
 986	/* Start sector of this operation */
 987	FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
 988	MFPDELAY();
 989	/* Cheat for track if stretch != 0 */
 990	if (SUDT->stretch) {
 991		track = FDC_READ( FDCREG_TRACK);
 992		MFPDELAY();
 993		FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
 994	}
 995	udelay(25);
 996  
 997	/* Setup DMA */
 998	local_irq_save(flags);
 999	dma_wd.dma_lo = (unsigned char)paddr;
1000	MFPDELAY();
1001	paddr >>= 8;
1002	dma_wd.dma_md = (unsigned char)paddr;
1003	MFPDELAY();
1004	paddr >>= 8;
1005	if (ATARIHW_PRESENT(EXTD_DMA))
1006		st_dma_ext_dmahi = (unsigned short)paddr;
1007	else
1008		dma_wd.dma_hi = (unsigned char)paddr;
1009	MFPDELAY();
1010	local_irq_restore(flags);
1011  
1012	/* Clear FIFO and switch DMA to correct mode */  
1013	dma_wd.dma_mode_status = 0x90 | rwflag;  
1014	MFPDELAY();
1015	dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);  
1016	MFPDELAY();
1017	dma_wd.dma_mode_status = 0x90 | rwflag;
1018	MFPDELAY();
1019  
1020	/* How many sectors for DMA */
1021	dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
1022  
1023	udelay(25);  
1024  
1025	/* Start operation */
1026	dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
1027	udelay(25);
1028	SET_IRQ_HANDLER( fd_rwsec_done );
1029	dma_wd.fdc_acces_seccount =
1030	  (get_head_settle_flag() |
1031	   (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
1032
1033	old_motoron = MotorOn;
1034	MotorOn = 1;
1035	NeedSeek = 1;
1036	/* wait for interrupt */
1037
1038	if (read_track) {
1039		/* If reading a whole track, wait about one disk rotation and
1040		 * then check if all sectors are read. The FDC will even
1041		 * search for the first non-existent sector and need 1 sec to
1042		 * recognise that it isn't present :-(
1043		 */
1044		MultReadInProgress = 1;
1045		mod_timer(&readtrack_timer,
1046			  /* 1 rot. + 5 rot.s if motor was off  */
1047			  jiffies + HZ/5 + (old_motoron ? 0 : HZ));
1048	}
1049	start_timeout();
1050}
1051
1052    
1053static void fd_readtrack_check(struct timer_list *unused)
1054{
1055	unsigned long flags, addr, addr2;
1056
1057	local_irq_save(flags);
1058
1059	if (!MultReadInProgress) {
1060		/* This prevents a race condition that could arise if the
1061		 * interrupt is triggered while the calling of this timer
1062		 * callback function takes place. The IRQ function then has
1063		 * already cleared 'MultReadInProgress'  when flow of control
1064		 * gets here.
1065		 */
1066		local_irq_restore(flags);
1067		return;
1068	}
1069
1070	/* get the current DMA address */
1071	/* ++ f.a. read twice to avoid being fooled by switcher */
1072	addr = 0;
1073	do {
1074		addr2 = addr;
1075		addr = dma_wd.dma_lo & 0xff;
1076		MFPDELAY();
1077		addr |= (dma_wd.dma_md & 0xff) << 8;
1078		MFPDELAY();
1079		if (ATARIHW_PRESENT( EXTD_DMA ))
1080			addr |= (st_dma_ext_dmahi & 0xffff) << 16;
1081		else
1082			addr |= (dma_wd.dma_hi & 0xff) << 16;
1083		MFPDELAY();
1084	} while(addr != addr2);
1085  
1086	if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1087		/* already read enough data, force an FDC interrupt to stop
1088		 * the read operation
1089		 */
1090		SET_IRQ_HANDLER( NULL );
1091		MultReadInProgress = 0;
1092		local_irq_restore(flags);
1093		DPRINT(("fd_readtrack_check(): done\n"));
1094		FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1095		udelay(25);
1096
1097		/* No error until now -- the FDC would have interrupted
1098		 * otherwise!
1099		 */
1100		fd_rwsec_done1(0);
1101	}
1102	else {
1103		/* not yet finished, wait another tenth rotation */
1104		local_irq_restore(flags);
1105		DPRINT(("fd_readtrack_check(): not yet finished\n"));
1106		mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1107	}
1108}
1109
1110
1111static void fd_rwsec_done( int status )
1112{
1113	DPRINT(("fd_rwsec_done()\n"));
1114
1115	if (read_track) {
1116		del_timer(&readtrack_timer);
1117		if (!MultReadInProgress)
1118			return;
1119		MultReadInProgress = 0;
1120	}
1121	fd_rwsec_done1(status);
1122}
1123
1124static void fd_rwsec_done1(int status)
1125{
1126	unsigned int track;
1127
1128	stop_timeout();
1129	
1130	/* Correct the track if stretch != 0 */
1131	if (SUDT->stretch) {
1132		track = FDC_READ( FDCREG_TRACK);
1133		MFPDELAY();
1134		FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1135	}
1136
1137	if (!UseTrackbuffer) {
1138		dma_wd.dma_mode_status = 0x90;
1139		MFPDELAY();
1140		if (!(dma_wd.dma_mode_status & 0x01)) {
1141			printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1142			goto err_end;
1143		}
1144	}
1145	MFPDELAY();
1146
1147	if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1148		printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1149		goto err_end;
1150	}	
1151	if ((status & FDCSTAT_RECNF) &&
1152	    /* RECNF is no error after a multiple read when the FDC
1153	       searched for a non-existent sector! */
1154	    !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1155		if (Probing) {
1156			if (SUDT > atari_disk_type) {
1157			    if (SUDT[-1].blocks > ReqBlock) {
1158				/* try another disk type */
1159				SUDT--;
1160				set_capacity(unit[SelectedDrive].disk[0],
1161							SUDT->blocks);
1162			    } else
1163				Probing = 0;
1164			}
1165			else {
1166				if (SUD.flags & FTD_MSG)
1167					printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1168					       SelectedDrive, SUDT->name );
1169				Probing=0;
1170			}
1171		} else {	
1172/* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1173			if (SUD.autoprobe) {
1174				SUDT = atari_disk_type + StartDiskType[DriveType];
1175				set_capacity(unit[SelectedDrive].disk[0],
1176							SUDT->blocks);
1177				Probing = 1;
1178			}
1179		}
1180		if (Probing) {
1181			if (ATARIHW_PRESENT(FDCSPEED)) {
1182				dma_wd.fdc_speed = SUDT->fdc_speed;
1183				MFPDELAY();
1184			}
1185			setup_req_params( SelectedDrive );
1186			BufferDrive = -1;
1187			do_fd_action( SelectedDrive );
1188			return;
1189		}
1190
1191		printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1192		       SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1193		goto err_end;
1194	}
1195	if (status & FDCSTAT_CRC) {
1196		printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1197		       SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1198		goto err_end;
1199	}
1200	if (status & FDCSTAT_LOST) {
1201		printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1202		       SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1203		goto err_end;
1204	}
1205
1206	Probing = 0;
1207	
1208	if (ReqCmd == READ) {
1209		if (!read_track) {
1210			void *addr;
1211			addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1212			dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1213			if (!ATARIHW_PRESENT( EXTD_DMA ))
1214				copy_buffer (addr, ReqData);
1215		} else {
1216			dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1217			BufferDrive = SelectedDrive;
1218			BufferSide  = ReqSide;
1219			BufferTrack = ReqTrack;
1220			copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1221		}
1222	}
1223  
1224	if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
1225		/* read next sector */
1226		setup_req_params( SelectedDrive );
1227		do_fd_action( SelectedDrive );
1228	}
1229	else {
1230		/* all sectors finished */
1231		fd_end_request_cur(BLK_STS_OK);
 
1232	}
1233	return;
1234  
1235  err_end:
1236	BufferDrive = -1;
1237	fd_error();
1238}
1239
1240
1241static void fd_writetrack( void )
1242{
1243	unsigned long paddr, flags;
1244	unsigned int track;
1245	
1246	DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1247
1248	paddr = PhysTrackBuffer;
1249	dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1250
1251	fd_select_side( ReqSide );
1252  
1253	/* Cheat for track if stretch != 0 */
1254	if (SUDT->stretch) {
1255		track = FDC_READ( FDCREG_TRACK);
1256		MFPDELAY();
1257		FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1258	}
1259	udelay(40);
1260  
1261	/* Setup DMA */
1262	local_irq_save(flags);
1263	dma_wd.dma_lo = (unsigned char)paddr;
1264	MFPDELAY();
1265	paddr >>= 8;
1266	dma_wd.dma_md = (unsigned char)paddr;
1267	MFPDELAY();
1268	paddr >>= 8;
1269	if (ATARIHW_PRESENT( EXTD_DMA ))
1270		st_dma_ext_dmahi = (unsigned short)paddr;
1271	else
1272		dma_wd.dma_hi = (unsigned char)paddr;
1273	MFPDELAY();
1274	local_irq_restore(flags);
1275  
1276	/* Clear FIFO and switch DMA to correct mode */  
1277	dma_wd.dma_mode_status = 0x190;  
1278	MFPDELAY();
1279	dma_wd.dma_mode_status = 0x90;  
1280	MFPDELAY();
1281	dma_wd.dma_mode_status = 0x190;
1282	MFPDELAY();
1283  
1284	/* How many sectors for DMA */
1285	dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1286	udelay(40);  
1287  
1288	/* Start operation */
1289	dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1290	udelay(40);
1291	SET_IRQ_HANDLER( fd_writetrack_done );
1292	dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag(); 
1293
1294	MotorOn = 1;
1295	start_timeout();
1296	/* wait for interrupt */
1297}
1298
1299
1300static void fd_writetrack_done( int status )
1301{
1302	DPRINT(("fd_writetrack_done()\n"));
1303
1304	stop_timeout();
1305
1306	if (status & FDCSTAT_WPROT) {
1307		printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1308		goto err_end;
1309	}	
1310	if (status & FDCSTAT_LOST) {
1311		printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1312				SelectedDrive, ReqSide, ReqTrack );
1313		goto err_end;
1314	}
1315
1316	complete(&format_wait);
1317	return;
1318
1319  err_end:
1320	fd_error();
1321}
1322
1323static void fd_times_out(struct timer_list *unused)
1324{
1325	atari_disable_irq( IRQ_MFP_FDC );
1326	if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1327					  * before we came here... */
1328
1329	SET_IRQ_HANDLER( NULL );
1330	/* If the timeout occurred while the readtrack_check timer was
1331	 * active, we need to cancel it, else bad things will happen */
1332	if (UseTrackbuffer)
1333		del_timer( &readtrack_timer );
1334	FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1335	udelay( 25 );
1336	
1337	printk(KERN_ERR "floppy timeout\n" );
1338	fd_error();
1339  end:
1340	atari_enable_irq( IRQ_MFP_FDC );
1341}
1342
1343
1344/* The (noop) seek operation here is needed to make the WP bit in the
1345 * FDC status register accessible for check_change. If the last disk
1346 * operation would have been a RDSEC, this bit would always read as 0
1347 * no matter what :-( To save time, the seek goes to the track we're
1348 * already on.
1349 */
1350
1351static void finish_fdc( void )
1352{
1353	if (!NeedSeek) {
1354		finish_fdc_done( 0 );
1355	}
1356	else {
1357		DPRINT(("finish_fdc: dummy seek started\n"));
1358		FDC_WRITE (FDCREG_DATA, SUD.track);
1359		SET_IRQ_HANDLER( finish_fdc_done );
1360		FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1361		MotorOn = 1;
1362		start_timeout();
1363		/* we must wait for the IRQ here, because the ST-DMA
1364		   is released immediately afterwards and the interrupt
1365		   may be delivered to the wrong driver. */
1366	  }
1367}
1368
1369
1370static void finish_fdc_done( int dummy )
1371{
1372	unsigned long flags;
1373
1374	DPRINT(("finish_fdc_done entered\n"));
1375	stop_timeout();
1376	NeedSeek = 0;
1377
1378	if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1379		/* If the check for a disk change is done too early after this
1380		 * last seek command, the WP bit still reads wrong :-((
1381		 */
1382		mod_timer(&fd_timer, jiffies + 5);
1383	else
1384		start_check_change_timer();
1385	start_motor_off_timer();
1386
1387	local_irq_save(flags);
1388	stdma_release();
 
 
1389	local_irq_restore(flags);
1390
1391	DPRINT(("finish_fdc() finished\n"));
1392}
1393
1394/* The detection of disk changes is a dark chapter in Atari history :-(
1395 * Because the "Drive ready" signal isn't present in the Atari
1396 * hardware, one has to rely on the "Write Protect". This works fine,
1397 * as long as no write protected disks are used. TOS solves this
1398 * problem by introducing tri-state logic ("maybe changed") and
1399 * looking at the serial number in block 0. This isn't possible for
1400 * Linux, since the floppy driver can't make assumptions about the
1401 * filesystem used on the disk and thus the contents of block 0. I've
1402 * chosen the method to always say "The disk was changed" if it is
1403 * unsure whether it was. This implies that every open or mount
1404 * invalidates the disk buffers if you work with write protected
1405 * disks. But at least this is better than working with incorrect data
1406 * due to unrecognised disk changes.
1407 */
1408
1409static unsigned int floppy_check_events(struct gendisk *disk,
1410					unsigned int clearing)
1411{
1412	struct atari_floppy_struct *p = disk->private_data;
1413	unsigned int drive = p - unit;
1414	if (test_bit (drive, &fake_change)) {
1415		/* simulated change (e.g. after formatting) */
1416		return DISK_EVENT_MEDIA_CHANGE;
1417	}
1418	if (test_bit (drive, &changed_floppies)) {
1419		/* surely changed (the WP signal changed at least once) */
1420		return DISK_EVENT_MEDIA_CHANGE;
1421	}
1422	if (UD.wpstat) {
1423		/* WP is on -> could be changed: to be sure, buffers should be
1424		 * invalidated...
1425		 */
1426		return DISK_EVENT_MEDIA_CHANGE;
1427	}
1428
1429	return 0;
1430}
1431
1432static int floppy_revalidate(struct gendisk *disk)
1433{
1434	struct atari_floppy_struct *p = disk->private_data;
1435	unsigned int drive = p - unit;
1436
1437	if (test_bit(drive, &changed_floppies) ||
1438	    test_bit(drive, &fake_change) ||
1439	    p->disktype == 0) {
1440		if (UD.flags & FTD_MSG)
1441			printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1442		BufferDrive = -1;
1443		clear_bit(drive, &fake_change);
1444		clear_bit(drive, &changed_floppies);
1445		/* MSch: clearing geometry makes sense only for autoprobe
1446		   formats, for 'permanent user-defined' parameter:
1447		   restore default_params[] here if flagged valid! */
1448		if (default_params[drive].blocks == 0)
1449			UDT = NULL;
1450		else
1451			UDT = &default_params[drive];
1452	}
1453	return 0;
1454}
1455
1456
1457/* This sets up the global variables describing the current request. */
1458
1459static void setup_req_params( int drive )
1460{
1461	int block = ReqBlock + ReqCnt;
1462
1463	ReqTrack = block / UDT->spt;
1464	ReqSector = block - ReqTrack * UDT->spt + 1;
1465	ReqSide = ReqTrack & 1;
1466	ReqTrack >>= 1;
1467	ReqData = ReqBuffer + 512 * ReqCnt;
1468
1469	if (UseTrackbuffer)
1470		read_track = (ReqCmd == READ && fd_request->error_count == 0);
1471	else
1472		read_track = 0;
1473
1474	DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1475			ReqTrack, ReqSector, (unsigned long)ReqData ));
1476}
1477
1478static void ataflop_commit_rqs(struct blk_mq_hw_ctx *hctx)
 
 
 
1479{
1480	spin_lock_irq(&ataflop_lock);
1481	atari_disable_irq(IRQ_MFP_FDC);
1482	finish_fdc();
1483	atari_enable_irq(IRQ_MFP_FDC);
1484	spin_unlock_irq(&ataflop_lock);
 
 
 
 
 
 
 
 
 
 
 
1485}
1486
1487static blk_status_t ataflop_queue_rq(struct blk_mq_hw_ctx *hctx,
1488				     const struct blk_mq_queue_data *bd)
1489{
1490	struct atari_floppy_struct *floppy = bd->rq->rq_disk->private_data;
1491	int drive = floppy - unit;
1492	int type = floppy->type;
1493
1494	spin_lock_irq(&ataflop_lock);
1495	if (fd_request) {
1496		spin_unlock_irq(&ataflop_lock);
1497		return BLK_STS_DEV_RESOURCE;
1498	}
1499	if (!stdma_try_lock(floppy_irq, NULL))  {
1500		spin_unlock_irq(&ataflop_lock);
1501		return BLK_STS_RESOURCE;
1502	}
1503	fd_request = bd->rq;
1504	blk_mq_start_request(fd_request);
1505
1506	atari_disable_irq( IRQ_MFP_FDC );
1507
1508	IsFormatting = 0;
 
 
 
 
 
1509
 
 
 
 
1510	if (!UD.connected) {
1511		/* drive not connected */
1512		printk(KERN_ERR "Unknown Device: fd%d\n", drive );
1513		fd_end_request_cur(BLK_STS_IOERR);
1514		goto out;
1515	}
1516		
1517	if (type == 0) {
1518		if (!UDT) {
1519			Probing = 1;
1520			UDT = atari_disk_type + StartDiskType[DriveType];
1521			set_capacity(bd->rq->rq_disk, UDT->blocks);
1522			UD.autoprobe = 1;
1523		}
1524	} 
1525	else {
1526		/* user supplied disk type */
1527		if (--type >= NUM_DISK_MINORS) {
1528			printk(KERN_WARNING "fd%d: invalid disk format", drive );
1529			fd_end_request_cur(BLK_STS_IOERR);
1530			goto out;
1531		}
1532		if (minor2disktype[type].drive_types > DriveType)  {
1533			printk(KERN_WARNING "fd%d: unsupported disk format", drive );
1534			fd_end_request_cur(BLK_STS_IOERR);
1535			goto out;
1536		}
1537		type = minor2disktype[type].index;
1538		UDT = &atari_disk_type[type];
1539		set_capacity(bd->rq->rq_disk, UDT->blocks);
1540		UD.autoprobe = 0;
1541	}
 
 
 
 
 
1542
1543	/* stop deselect timer */
1544	del_timer( &motor_off_timer );
1545		
1546	ReqCnt = 0;
1547	ReqCmd = rq_data_dir(fd_request);
1548	ReqBlock = blk_rq_pos(fd_request);
1549	ReqBuffer = bio_data(fd_request->bio);
1550	setup_req_params( drive );
1551	do_fd_action( drive );
1552
1553	if (bd->last)
1554		finish_fdc();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1555	atari_enable_irq( IRQ_MFP_FDC );
1556
1557out:
1558	spin_unlock_irq(&ataflop_lock);
1559	return BLK_STS_OK;
1560}
1561
1562static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1563		    unsigned int cmd, unsigned long param)
1564{
1565	struct gendisk *disk = bdev->bd_disk;
1566	struct atari_floppy_struct *floppy = disk->private_data;
1567	int drive = floppy - unit;
1568	int type = floppy->type;
1569	struct atari_format_descr fmt_desc;
1570	struct atari_disk_type *dtp;
1571	struct floppy_struct getprm;
1572	int settype;
1573	struct floppy_struct setprm;
1574	void __user *argp = (void __user *)param;
1575
1576	switch (cmd) {
1577	case FDGETPRM:
1578		if (type) {
1579			if (--type >= NUM_DISK_MINORS)
1580				return -ENODEV;
1581			if (minor2disktype[type].drive_types > DriveType)
1582				return -ENODEV;
1583			type = minor2disktype[type].index;
1584			dtp = &atari_disk_type[type];
1585			if (UD.flags & FTD_MSG)
1586			    printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1587			        drive, dtp, dtp->name);
1588		}
1589		else {
1590			if (!UDT)
1591				return -ENXIO;
1592			else
1593				dtp = UDT;
1594		}
1595		memset((void *)&getprm, 0, sizeof(getprm));
1596		getprm.size = dtp->blocks;
1597		getprm.sect = dtp->spt;
1598		getprm.head = 2;
1599		getprm.track = dtp->blocks/dtp->spt/2;
1600		getprm.stretch = dtp->stretch;
1601		if (copy_to_user(argp, &getprm, sizeof(getprm)))
1602			return -EFAULT;
1603		return 0;
1604	}
1605	switch (cmd) {
1606	case FDSETPRM:
1607	case FDDEFPRM:
1608	        /* 
1609		 * MSch 7/96: simple 'set geometry' case: just set the
1610		 * 'default' device params (minor == 0).
1611		 * Currently, the drive geometry is cleared after each
1612		 * disk change and subsequent revalidate()! simple
1613		 * implementation of FDDEFPRM: save geometry from a
1614		 * FDDEFPRM call and restore it in floppy_revalidate() !
1615		 */
1616
1617		/* get the parameters from user space */
1618		if (floppy->ref != 1 && floppy->ref != -1)
1619			return -EBUSY;
1620		if (copy_from_user(&setprm, argp, sizeof(setprm)))
1621			return -EFAULT;
1622		/* 
1623		 * first of all: check for floppy change and revalidate, 
1624		 * or the next access will revalidate - and clear UDT :-(
1625		 */
1626
1627		if (floppy_check_events(disk, 0))
1628		        floppy_revalidate(disk);
1629
1630		if (UD.flags & FTD_MSG)
1631		    printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1632			drive, setprm.size, setprm.sect, setprm.stretch);
1633
1634		/* what if type > 0 here? Overwrite specified entry ? */
1635		if (type) {
1636		        /* refuse to re-set a predefined type for now */
 
1637			return -EINVAL;
1638		}
1639
1640		/* 
1641		 * type == 0: first look for a matching entry in the type list,
1642		 * and set the UD.disktype field to use the perdefined entry.
1643		 * TODO: add user-defined format to head of autoprobe list ? 
1644		 * Useful to include the user-type for future autodetection!
1645		 */
1646
1647		for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1648			int setidx = 0;
1649			if (minor2disktype[settype].drive_types > DriveType) {
1650				/* skip this one, invalid for drive ... */
1651				continue;
1652			}
1653			setidx = minor2disktype[settype].index;
1654			dtp = &atari_disk_type[setidx];
1655
1656			/* found matching entry ?? */
1657			if (   dtp->blocks  == setprm.size 
1658			    && dtp->spt     == setprm.sect
1659			    && dtp->stretch == setprm.stretch ) {
1660				if (UD.flags & FTD_MSG)
1661				    printk (KERN_INFO "floppy%d: setting %s %p!\n",
1662				        drive, dtp->name, dtp);
1663				UDT = dtp;
1664				set_capacity(disk, UDT->blocks);
1665
1666				if (cmd == FDDEFPRM) {
1667				  /* save settings as permanent default type */
1668				  default_params[drive].name    = dtp->name;
1669				  default_params[drive].spt     = dtp->spt;
1670				  default_params[drive].blocks  = dtp->blocks;
1671				  default_params[drive].fdc_speed = dtp->fdc_speed;
1672				  default_params[drive].stretch = dtp->stretch;
1673				}
1674				
1675				return 0;
1676			}
1677
1678		}
1679
1680		/* no matching disk type found above - setting user_params */
1681
1682	       	if (cmd == FDDEFPRM) {
1683			/* set permanent type */
1684			dtp = &default_params[drive];
1685		} else
1686			/* set user type (reset by disk change!) */
1687			dtp = &user_params[drive];
1688
1689		dtp->name   = "user format";
1690		dtp->blocks = setprm.size;
1691		dtp->spt    = setprm.sect;
1692		if (setprm.sect > 14) 
1693			dtp->fdc_speed = 3;
1694		else
1695			dtp->fdc_speed = 0;
1696		dtp->stretch = setprm.stretch;
1697
1698		if (UD.flags & FTD_MSG)
1699			printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1700				drive, dtp->blocks, dtp->spt, dtp->stretch);
1701
1702		/* sanity check */
1703		if (setprm.track != dtp->blocks/dtp->spt/2 ||
1704		    setprm.head != 2)
 
1705			return -EINVAL;
 
1706
1707		UDT = dtp;
1708		set_capacity(disk, UDT->blocks);
1709
1710		return 0;
1711	case FDMSGON:
1712		UD.flags |= FTD_MSG;
1713		return 0;
1714	case FDMSGOFF:
1715		UD.flags &= ~FTD_MSG;
1716		return 0;
1717	case FDSETEMSGTRESH:
1718		return -EINVAL;
1719	case FDFMTBEG:
1720		return 0;
1721	case FDFMTTRK:
1722		if (floppy->ref != 1 && floppy->ref != -1)
1723			return -EBUSY;
1724		if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
1725			return -EFAULT;
1726		return do_format(drive, type, &fmt_desc);
1727	case FDCLRPRM:
1728		UDT = NULL;
1729		/* MSch: invalidate default_params */
1730		default_params[drive].blocks  = 0;
1731		set_capacity(disk, MAX_DISK_SIZE * 2);
1732		fallthrough;
1733	case FDFMTEND:
1734	case FDFLUSH:
1735		/* invalidate the buffer track to force a reread */
1736		BufferDrive = -1;
1737		set_bit(drive, &fake_change);
1738		if (bdev_check_media_change(bdev))
1739			floppy_revalidate(bdev->bd_disk);
1740		return 0;
1741	default:
1742		return -EINVAL;
1743	}
1744}
1745
1746static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1747			     unsigned int cmd, unsigned long arg)
1748{
1749	int ret;
1750
1751	mutex_lock(&ataflop_mutex);
1752	ret = fd_locked_ioctl(bdev, mode, cmd, arg);
1753	mutex_unlock(&ataflop_mutex);
1754
1755	return ret;
1756}
1757
1758/* Initialize the 'unit' variable for drive 'drive' */
1759
1760static void __init fd_probe( int drive )
1761{
1762	UD.connected = 0;
1763	UDT  = NULL;
1764
1765	if (!fd_test_drive_present( drive ))
1766		return;
1767
1768	UD.connected = 1;
1769	UD.track     = 0;
1770	switch( UserSteprate[drive] ) {
1771	case 2:
1772		UD.steprate = FDCSTEP_2;
1773		break;
1774	case 3:
1775		UD.steprate = FDCSTEP_3;
1776		break;
1777	case 6:
1778		UD.steprate = FDCSTEP_6;
1779		break;
1780	case 12:
1781		UD.steprate = FDCSTEP_12;
1782		break;
1783	default: /* should be -1 for "not set by user" */
1784		if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1785			UD.steprate = FDCSTEP_3;
1786		else
1787			UD.steprate = FDCSTEP_6;
1788		break;
1789	}
1790	MotorOn = 1;	/* from probe restore operation! */
1791}
1792
1793
1794/* This function tests the physical presence of a floppy drive (not
1795 * whether a disk is inserted). This is done by issuing a restore
1796 * command, waiting max. 2 seconds (that should be enough to move the
1797 * head across the whole disk) and looking at the state of the "TR00"
1798 * signal. This should now be raised if there is a drive connected
1799 * (and there is no hardware failure :-) Otherwise, the drive is
1800 * declared absent.
1801 */
1802
1803static int __init fd_test_drive_present( int drive )
1804{
1805	unsigned long timeout;
1806	unsigned char status;
1807	int ok;
1808	
1809	if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1810	fd_select_drive( drive );
1811
1812	/* disable interrupt temporarily */
1813	atari_turnoff_irq( IRQ_MFP_FDC );
1814	FDC_WRITE (FDCREG_TRACK, 0xff00);
1815	FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1816
1817	timeout = jiffies + 2*HZ+HZ/2;
1818	while (time_before(jiffies, timeout))
1819		if (!(st_mfp.par_dt_reg & 0x20))
1820			break;
1821
1822	status = FDC_READ( FDCREG_STATUS );
1823	ok = (status & FDCSTAT_TR00) != 0;
1824
1825	/* force interrupt to abort restore operation (FDC would try
1826	 * about 50 seconds!) */
1827	FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1828	udelay(500);
1829	status = FDC_READ( FDCREG_STATUS );
1830	udelay(20);
1831
1832	if (ok) {
1833		/* dummy seek command to make WP bit accessible */
1834		FDC_WRITE( FDCREG_DATA, 0 );
1835		FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
1836		while( st_mfp.par_dt_reg & 0x20 )
1837			;
1838		status = FDC_READ( FDCREG_STATUS );
1839	}
1840
1841	atari_turnon_irq( IRQ_MFP_FDC );
1842	return( ok );
1843}
1844
1845
1846/* Look how many and which kind of drives are connected. If there are
1847 * floppies, additionally start the disk-change and motor-off timers.
1848 */
1849
1850static void __init config_types( void )
1851{
1852	int drive, cnt = 0;
1853
1854	/* for probing drives, set the FDC speed to 8 MHz */
1855	if (ATARIHW_PRESENT(FDCSPEED))
1856		dma_wd.fdc_speed = 0;
1857
1858	printk(KERN_INFO "Probing floppy drive(s):\n");
1859	for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1860		fd_probe( drive );
1861		if (UD.connected) {
1862			printk(KERN_INFO "fd%d\n", drive);
1863			++cnt;
1864		}
1865	}
1866
1867	if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1868		/* If FDC is still busy from probing, give it another FORCI
1869		 * command to abort the operation. If this isn't done, the FDC
1870		 * will interrupt later and its IRQ line stays low, because
1871		 * the status register isn't read. And this will block any
1872		 * interrupts on this IRQ line :-(
1873		 */
1874		FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1875		udelay(500);
1876		FDC_READ( FDCREG_STATUS );
1877		udelay(20);
1878	}
1879	
1880	if (cnt > 0) {
1881		start_motor_off_timer();
1882		if (cnt == 1) fd_select_drive( 0 );
1883		start_check_change_timer();
1884	}
1885}
1886
1887/*
1888 * floppy_open check for aliasing (/dev/fd0 can be the same as
1889 * /dev/PS0 etc), and disallows simultaneous access to the same
1890 * drive with different device numbers.
1891 */
1892
1893static int floppy_open(struct block_device *bdev, fmode_t mode)
1894{
1895	struct atari_floppy_struct *p = bdev->bd_disk->private_data;
1896	int type  = MINOR(bdev->bd_dev) >> 2;
1897
1898	DPRINT(("fd_open: type=%d\n",type));
1899	if (p->ref && p->type != type)
1900		return -EBUSY;
1901
1902	if (p->ref == -1 || (p->ref && mode & FMODE_EXCL))
1903		return -EBUSY;
1904
1905	if (mode & FMODE_EXCL)
1906		p->ref = -1;
1907	else
1908		p->ref++;
1909
1910	p->type = type;
1911
1912	if (mode & FMODE_NDELAY)
1913		return 0;
1914
1915	if (mode & (FMODE_READ|FMODE_WRITE)) {
1916		if (bdev_check_media_change(bdev))
1917			floppy_revalidate(bdev->bd_disk);
1918		if (mode & FMODE_WRITE) {
1919			if (p->wpstat) {
1920				if (p->ref < 0)
1921					p->ref = 0;
1922				else
1923					p->ref--;
1924				return -EROFS;
1925			}
1926		}
1927	}
1928	return 0;
1929}
1930
1931static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
1932{
1933	int ret;
1934
1935	mutex_lock(&ataflop_mutex);
1936	ret = floppy_open(bdev, mode);
1937	mutex_unlock(&ataflop_mutex);
1938
1939	return ret;
1940}
1941
1942static void floppy_release(struct gendisk *disk, fmode_t mode)
1943{
1944	struct atari_floppy_struct *p = disk->private_data;
1945	mutex_lock(&ataflop_mutex);
1946	if (p->ref < 0)
1947		p->ref = 0;
1948	else if (!p->ref--) {
1949		printk(KERN_ERR "floppy_release with fd_ref == 0");
1950		p->ref = 0;
1951	}
1952	mutex_unlock(&ataflop_mutex);
 
1953}
1954
1955static const struct block_device_operations floppy_fops = {
1956	.owner		= THIS_MODULE,
1957	.open		= floppy_unlocked_open,
1958	.release	= floppy_release,
1959	.ioctl		= fd_ioctl,
1960	.check_events	= floppy_check_events,
 
1961};
1962
1963static const struct blk_mq_ops ataflop_mq_ops = {
1964	.queue_rq = ataflop_queue_rq,
1965	.commit_rqs = ataflop_commit_rqs,
1966};
1967
1968static int ataflop_alloc_disk(unsigned int drive, unsigned int type)
1969{
1970	struct gendisk *disk;
1971
1972	disk = blk_mq_alloc_disk(&unit[drive].tag_set, NULL);
1973	if (IS_ERR(disk))
1974		return PTR_ERR(disk);
1975
1976	disk->major = FLOPPY_MAJOR;
1977	disk->first_minor = drive + (type << 2);
1978	disk->minors = 1;
1979	sprintf(disk->disk_name, "fd%d", drive);
1980	disk->fops = &floppy_fops;
1981	disk->events = DISK_EVENT_MEDIA_CHANGE;
1982	disk->private_data = &unit[drive];
1983	set_capacity(disk, MAX_DISK_SIZE * 2);
1984
1985	unit[drive].disk[type] = disk;
1986	return 0;
1987}
1988
1989static DEFINE_MUTEX(ataflop_probe_lock);
1990
1991static void ataflop_probe(dev_t dev)
1992{
1993	int drive = MINOR(dev) & 3;
1994	int type  = MINOR(dev) >> 2;
1995
1996	if (type)
1997		type--;
1998
1999	if (drive >= FD_MAX_UNITS || type >= NUM_DISK_MINORS)
2000		return;
2001	mutex_lock(&ataflop_probe_lock);
2002	if (!unit[drive].disk[type]) {
2003		if (ataflop_alloc_disk(drive, type) == 0)
2004			add_disk(unit[drive].disk[type]);
2005	}
2006	mutex_unlock(&ataflop_probe_lock);
2007}
2008
2009static int __init atari_floppy_init (void)
2010{
2011	int i;
2012	int ret;
2013
2014	if (!MACH_IS_ATARI)
2015		/* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
2016		return -ENODEV;
2017
2018	mutex_lock(&ataflop_probe_lock);
2019	ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe);
2020	if (ret)
2021		goto out_unlock;
2022
2023	for (i = 0; i < FD_MAX_UNITS; i++) {
2024		memset(&unit[i].tag_set, 0, sizeof(unit[i].tag_set));
2025		unit[i].tag_set.ops = &ataflop_mq_ops;
2026		unit[i].tag_set.nr_hw_queues = 1;
2027		unit[i].tag_set.nr_maps = 1;
2028		unit[i].tag_set.queue_depth = 2;
2029		unit[i].tag_set.numa_node = NUMA_NO_NODE;
2030		unit[i].tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
2031		ret = blk_mq_alloc_tag_set(&unit[i].tag_set);
2032		if (ret)
2033			goto err;
2034
2035		ret = ataflop_alloc_disk(i, 0);
2036		if (ret) {
2037			blk_mq_free_tag_set(&unit[i].tag_set);
2038			goto err;
2039		}
2040	}
2041
2042	if (UseTrackbuffer < 0)
2043		/* not set by user -> use default: for now, we turn
2044		   track buffering off for all Medusas, though it
2045		   could be used with ones that have a counter
2046		   card. But the test is too hard :-( */
2047		UseTrackbuffer = !MACH_IS_MEDUSA;
2048
2049	/* initialize variables */
2050	SelectedDrive = -1;
2051	BufferDrive = -1;
2052
2053	DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
2054	if (!DMABuffer) {
2055		printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
2056		ret = -ENOMEM;
2057		goto err;
2058	}
2059	TrackBuffer = DMABuffer + 512;
2060	PhysDMABuffer = atari_stram_to_phys(DMABuffer);
2061	PhysTrackBuffer = virt_to_phys(TrackBuffer);
2062	BufferDrive = BufferSide = BufferTrack = -1;
2063
2064	for (i = 0; i < FD_MAX_UNITS; i++) {
2065		unit[i].track = -1;
2066		unit[i].flags = 0;
2067		add_disk(unit[i].disk[0]);
 
 
 
 
 
 
 
 
 
 
2068	}
2069
 
 
 
2070	printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
2071	       DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
2072	       UseTrackbuffer ? "" : "no ");
2073	config_types();
2074
2075	return 0;
2076
2077err:
2078	while (--i >= 0) {
2079		blk_cleanup_queue(unit[i].disk[0]->queue);
2080		put_disk(unit[i].disk[0]);
2081		blk_mq_free_tag_set(&unit[i].tag_set);
 
2082	}
2083
2084	unregister_blkdev(FLOPPY_MAJOR, "fd");
2085out_unlock:
2086	mutex_unlock(&ataflop_probe_lock);
2087	return ret;
2088}
2089
2090#ifndef MODULE
2091static int __init atari_floppy_setup(char *str)
2092{
2093	int ints[3 + FD_MAX_UNITS];
2094	int i;
2095
2096	if (!MACH_IS_ATARI)
2097		return 0;
2098
2099	str = get_options(str, 3 + FD_MAX_UNITS, ints);
2100	
2101	if (ints[0] < 1) {
2102		printk(KERN_ERR "ataflop_setup: no arguments!\n" );
2103		return 0;
2104	}
2105	else if (ints[0] > 2+FD_MAX_UNITS) {
2106		printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2107	}
2108
2109	if (ints[1] < 0 || ints[1] > 2)
2110		printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2111	else
2112		DriveType = ints[1];
2113
2114	if (ints[0] >= 2)
2115		UseTrackbuffer = (ints[2] > 0);
2116
2117	for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2118		if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2119			printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2120		else
2121			UserSteprate[i-3] = ints[i];
2122	}
2123	return 1;
2124}
2125
2126__setup("floppy=", atari_floppy_setup);
2127#endif
2128
2129static void __exit atari_floppy_exit(void)
2130{
2131	int i, type;
 
 
 
2132
2133	for (i = 0; i < FD_MAX_UNITS; i++) {
2134		for (type = 0; type < NUM_DISK_MINORS; type++) {
2135			if (!unit[i].disk[type])
2136				continue;
2137			del_gendisk(unit[i].disk[type]);
2138			blk_cleanup_queue(unit[i].disk[type]->queue);
2139			put_disk(unit[i].disk[type]);
2140		}
2141		blk_mq_free_tag_set(&unit[i].tag_set);
2142	}
2143	unregister_blkdev(FLOPPY_MAJOR, "fd");
2144
2145	del_timer_sync(&fd_timer);
2146	atari_stram_free( DMABuffer );
2147}
2148
2149module_init(atari_floppy_init)
2150module_exit(atari_floppy_exit)
2151
2152MODULE_LICENSE("GPL");