treewide: Add SPDX license identifier for more missed files
[linux-block.git] / drivers / block / ataflop.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
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>
6ec3938c 70#include <linux/blk-mq.h>
2a48fc0a 71#include <linux/mutex.h>
7b8a3d22
AB
72#include <linux/completion.h>
73#include <linux/wait.h>
1da177e4 74
1da177e4
LT
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
2a48fc0a 83static DEFINE_MUTEX(ataflop_mutex);
a336ca6f 84static struct request *fd_request;
1da177e4 85
3e6b8c3c
OS
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
1da177e4
LT
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 ? */
5ceadd2a 174} atari_disk_type[] = {
1da177e4
LT
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
945f390f 265#define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
1da177e4
LT
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;
301 int ref;
302 int type;
6ec3938c 303 struct blk_mq_tag_set tag_set;
1da177e4
LT
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? */
8d3b33f6 356module_param(UseTrackbuffer, int, 0);
1da177e4
LT
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 };
8d3b33f6 381module_param_array(UserSteprate, int, NULL, 0);
1da177e4 382
7b8a3d22 383static DECLARE_COMPLETION(format_wait);
1da177e4
LT
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 );
24ed960a
KC
422static void fd_motor_off_timer(struct timer_list *unused);
423static void check_change(struct timer_list *unused);
7d12e780 424static irqreturn_t floppy_irq (int irq, void *dummy);
1da177e4
LT
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 );
24ed960a 433static void fd_readtrack_check(struct timer_list *unused);
1da177e4
LT
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 );
24ed960a 438static void fd_times_out(struct timer_list *unused);
1da177e4
LT
439static void finish_fdc( void );
440static void finish_fdc_done( int dummy );
441static void setup_req_params( int drive );
8a6cfeb6 442static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
1da177e4
LT
443 cmd, unsigned long param);
444static void fd_probe( int drive );
445static int fd_test_drive_present( int drive );
446static void config_types( void );
60ad2340 447static int floppy_open(struct block_device *bdev, fmode_t mode);
db2a144b 448static void floppy_release(struct gendisk *disk, fmode_t mode);
1da177e4
LT
449
450/************************* End of Prototypes **************************/
451
1d27e3e2
KC
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);
1da177e4 456
2a842aca 457static void fd_end_request_cur(blk_status_t err)
a336ca6f 458{
6ec3938c
OS
459 if (!blk_update_request(fd_request, err,
460 blk_rq_cur_bytes(fd_request))) {
461 __blk_mq_end_request(fd_request, err);
a336ca6f 462 fd_request = NULL;
6ec3938c 463 }
a336ca6f
TH
464}
465
1da177e4
LT
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
24ed960a 561static void fd_motor_off_timer(struct timer_list *unused)
1da177e4
LT
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
24ed960a 597static void check_change(struct timer_list *unused)
1da177e4
LT
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
7d12e780 663static irqreturn_t floppy_irq (int irq, void *dummy)
1da177e4
LT
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;
7b8a3d22 692 complete(&format_wait);
1da177e4
LT
693 return;
694 }
695
a336ca6f 696 if (!fd_request)
1da177e4
LT
697 return;
698
c8e90782
CH
699 fd_request->error_count++;
700 if (fd_request->error_count >= MAX_ERRORS) {
1da177e4 701 printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
2a842aca 702 fd_end_request_cur(BLK_STS_IOERR);
1da177e4 703 }
c8e90782 704 else if (fd_request->error_count == RECALIBRATE_ERRORS) {
1da177e4
LT
705 printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
706 if (SelectedDrive != -1)
707 SUD.track = -1;
708 }
1da177e4
LT
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{
6ec3938c 726 struct request_queue *q = unit[drive].disk->queue;
1da177e4
LT
727 unsigned char *p;
728 int sect, nsect;
729 unsigned long flags;
6ec3938c 730 int ret;
1da177e4 731
6ec3938c
OS
732 blk_mq_freeze_queue(q);
733 blk_mq_quiesce_queue(q);
1da177e4
LT
734
735 local_irq_save(flags);
1da177e4
LT
736 stdma_lock(floppy_irq, NULL);
737 atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
738 local_irq_restore(flags);
739
740 if (type) {
741 if (--type >= NUM_DISK_MINORS ||
742 minor2disktype[type].drive_types > DriveType) {
6ec3938c
OS
743 ret = -EINVAL;
744 goto out;
1da177e4
LT
745 }
746 type = minor2disktype[type].index;
5ceadd2a 747 UDT = &atari_disk_type[type];
1da177e4
LT
748 }
749
750 if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
6ec3938c
OS
751 ret = -EINVAL;
752 goto out;
1da177e4
LT
753 }
754
755 nsect = UDT->spt;
756 p = TrackBuffer;
757 /* The track buffer is used for the raw track data, so its
758 contents become invalid! */
759 BufferDrive = -1;
760 /* stop deselect timer */
761 del_timer( &motor_off_timer );
762
763 FILL( 60 * (nsect / 9), 0x4e );
764 for( sect = 0; sect < nsect; ++sect ) {
765 FILL( 12, 0 );
766 FILL( 3, 0xf5 );
767 *p++ = 0xfe;
768 *p++ = desc->track;
769 *p++ = desc->head;
770 *p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
771 *p++ = 2;
772 *p++ = 0xf7;
773 FILL( 22, 0x4e );
774 FILL( 12, 0 );
775 FILL( 3, 0xf5 );
776 *p++ = 0xfb;
777 FILL( 512, 0xe5 );
778 *p++ = 0xf7;
779 FILL( 40, 0x4e );
780 }
781 FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
782
783 IsFormatting = 1;
784 FormatError = 0;
785 ReqTrack = desc->track;
786 ReqSide = desc->head;
787 do_fd_action( drive );
788
7b8a3d22 789 wait_for_completion(&format_wait);
1da177e4 790
6ec3938c
OS
791 ret = FormatError ? -EIO : 0;
792out:
793 blk_mq_unquiesce_queue(q);
794 blk_mq_unfreeze_queue(q);
795 return ret;
1da177e4
LT
796}
797
798
799/* do_fd_action() is the general procedure for a fd request: All
800 * required parameter settings (drive select, side select, track
801 * position) are checked and set if needed. For each of these
802 * parameters and the actual reading or writing exist two functions:
803 * one that starts the setting (or skips it if possible) and one
804 * callback for the "done" interrupt. Each done func calls the next
805 * set function to propagate the request down to fd_rwsec_done().
806 */
807
808static void do_fd_action( int drive )
809{
810 DPRINT(("do_fd_action\n"));
811
812 if (UseTrackbuffer && !IsFormatting) {
813 repeat:
814 if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
815 if (ReqCmd == READ) {
816 copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
a336ca6f 817 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
1da177e4
LT
818 /* read next sector */
819 setup_req_params( drive );
820 goto repeat;
821 }
822 else {
823 /* all sectors finished */
2a842aca 824 fd_end_request_cur(BLK_STS_OK);
1da177e4
LT
825 return;
826 }
827 }
828 else {
829 /* cmd == WRITE, pay attention to track buffer
830 * consistency! */
831 copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
832 }
833 }
834 }
835
836 if (SelectedDrive != drive)
837 fd_select_drive( drive );
838
839 if (UD.track == -1)
840 fd_calibrate();
841 else if (UD.track != ReqTrack << UDT->stretch)
842 fd_seek();
843 else if (IsFormatting)
844 fd_writetrack();
845 else
846 fd_rwsec();
847}
848
849
850/* Seek to track 0 if the current track is unknown */
851
852static void fd_calibrate( void )
853{
854 if (SUD.track >= 0) {
855 fd_calibrate_done( 0 );
856 return;
857 }
858
859 if (ATARIHW_PRESENT(FDCSPEED))
860 dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
861 DPRINT(("fd_calibrate\n"));
862 SET_IRQ_HANDLER( fd_calibrate_done );
863 /* we can't verify, since the speed may be incorrect */
864 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
865
866 NeedSeek = 1;
867 MotorOn = 1;
868 start_timeout();
869 /* wait for IRQ */
870}
871
872
873static void fd_calibrate_done( int status )
874{
875 DPRINT(("fd_calibrate_done()\n"));
876 stop_timeout();
877
878 /* set the correct speed now */
879 if (ATARIHW_PRESENT(FDCSPEED))
880 dma_wd.fdc_speed = SUDT->fdc_speed;
881 if (status & FDCSTAT_RECNF) {
882 printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
883 fd_error();
884 }
885 else {
886 SUD.track = 0;
887 fd_seek();
888 }
889}
890
891
892/* Seek the drive to the requested track. The drive must have been
893 * calibrated at some point before this.
894 */
895
896static void fd_seek( void )
897{
898 if (SUD.track == ReqTrack << SUDT->stretch) {
899 fd_seek_done( 0 );
900 return;
901 }
902
903 if (ATARIHW_PRESENT(FDCSPEED)) {
904 dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
905 MFPDELAY();
906 }
907
908 DPRINT(("fd_seek() to track %d\n",ReqTrack));
909 FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
910 udelay(25);
911 SET_IRQ_HANDLER( fd_seek_done );
912 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
913
914 MotorOn = 1;
915 set_head_settle_flag();
916 start_timeout();
917 /* wait for IRQ */
918}
919
920
921static void fd_seek_done( int status )
922{
923 DPRINT(("fd_seek_done()\n"));
924 stop_timeout();
925
926 /* set the correct speed */
927 if (ATARIHW_PRESENT(FDCSPEED))
928 dma_wd.fdc_speed = SUDT->fdc_speed;
929 if (status & FDCSTAT_RECNF) {
930 printk(KERN_ERR "fd%d: seek error (to track %d)\n",
931 SelectedDrive, ReqTrack );
932 /* we don't know exactly which track we are on now! */
933 SUD.track = -1;
934 fd_error();
935 }
936 else {
937 SUD.track = ReqTrack << SUDT->stretch;
938 NeedSeek = 0;
939 if (IsFormatting)
940 fd_writetrack();
941 else
942 fd_rwsec();
943 }
944}
945
946
947/* This does the actual reading/writing after positioning the head
948 * over the correct track.
949 */
950
951static int MultReadInProgress = 0;
952
953
954static void fd_rwsec( void )
955{
956 unsigned long paddr, flags;
957 unsigned int rwflag, old_motoron;
958 unsigned int track;
959
960 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
961 if (ReqCmd == WRITE) {
962 if (ATARIHW_PRESENT(EXTD_DMA)) {
963 paddr = virt_to_phys(ReqData);
964 }
965 else {
966 copy_buffer( ReqData, DMABuffer );
967 paddr = PhysDMABuffer;
968 }
969 dma_cache_maintenance( paddr, 512, 1 );
970 rwflag = 0x100;
971 }
972 else {
973 if (read_track)
974 paddr = PhysTrackBuffer;
975 else
976 paddr = ATARIHW_PRESENT(EXTD_DMA) ?
977 virt_to_phys(ReqData) : PhysDMABuffer;
978 rwflag = 0;
979 }
980
981 fd_select_side( ReqSide );
982
983 /* Start sector of this operation */
984 FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
985 MFPDELAY();
986 /* Cheat for track if stretch != 0 */
987 if (SUDT->stretch) {
988 track = FDC_READ( FDCREG_TRACK);
989 MFPDELAY();
990 FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
991 }
992 udelay(25);
993
994 /* Setup DMA */
995 local_irq_save(flags);
996 dma_wd.dma_lo = (unsigned char)paddr;
997 MFPDELAY();
998 paddr >>= 8;
999 dma_wd.dma_md = (unsigned char)paddr;
1000 MFPDELAY();
1001 paddr >>= 8;
1002 if (ATARIHW_PRESENT(EXTD_DMA))
1003 st_dma_ext_dmahi = (unsigned short)paddr;
1004 else
1005 dma_wd.dma_hi = (unsigned char)paddr;
1006 MFPDELAY();
1007 local_irq_restore(flags);
1008
1009 /* Clear FIFO and switch DMA to correct mode */
1010 dma_wd.dma_mode_status = 0x90 | rwflag;
1011 MFPDELAY();
1012 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
1013 MFPDELAY();
1014 dma_wd.dma_mode_status = 0x90 | rwflag;
1015 MFPDELAY();
1016
1017 /* How many sectors for DMA */
1018 dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
1019
1020 udelay(25);
1021
1022 /* Start operation */
1023 dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
1024 udelay(25);
1025 SET_IRQ_HANDLER( fd_rwsec_done );
1026 dma_wd.fdc_acces_seccount =
1027 (get_head_settle_flag() |
1028 (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
1029
1030 old_motoron = MotorOn;
1031 MotorOn = 1;
1032 NeedSeek = 1;
1033 /* wait for interrupt */
1034
1035 if (read_track) {
1036 /* If reading a whole track, wait about one disk rotation and
1037 * then check if all sectors are read. The FDC will even
1038 * search for the first non-existent sector and need 1 sec to
1039 * recognise that it isn't present :-(
1040 */
1041 MultReadInProgress = 1;
1042 mod_timer(&readtrack_timer,
1043 /* 1 rot. + 5 rot.s if motor was off */
1044 jiffies + HZ/5 + (old_motoron ? 0 : HZ));
1045 }
1046 start_timeout();
1047}
1048
1049
24ed960a 1050static void fd_readtrack_check(struct timer_list *unused)
1da177e4
LT
1051{
1052 unsigned long flags, addr, addr2;
1053
1054 local_irq_save(flags);
1055
1056 if (!MultReadInProgress) {
1057 /* This prevents a race condition that could arise if the
1058 * interrupt is triggered while the calling of this timer
1059 * callback function takes place. The IRQ function then has
1060 * already cleared 'MultReadInProgress' when flow of control
1061 * gets here.
1062 */
1063 local_irq_restore(flags);
1064 return;
1065 }
1066
1067 /* get the current DMA address */
1068 /* ++ f.a. read twice to avoid being fooled by switcher */
1069 addr = 0;
1070 do {
1071 addr2 = addr;
1072 addr = dma_wd.dma_lo & 0xff;
1073 MFPDELAY();
1074 addr |= (dma_wd.dma_md & 0xff) << 8;
1075 MFPDELAY();
1076 if (ATARIHW_PRESENT( EXTD_DMA ))
1077 addr |= (st_dma_ext_dmahi & 0xffff) << 16;
1078 else
1079 addr |= (dma_wd.dma_hi & 0xff) << 16;
1080 MFPDELAY();
1081 } while(addr != addr2);
1082
1083 if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1084 /* already read enough data, force an FDC interrupt to stop
1085 * the read operation
1086 */
1087 SET_IRQ_HANDLER( NULL );
1088 MultReadInProgress = 0;
1089 local_irq_restore(flags);
1090 DPRINT(("fd_readtrack_check(): done\n"));
1091 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1092 udelay(25);
1093
1094 /* No error until now -- the FDC would have interrupted
1095 * otherwise!
1096 */
1097 fd_rwsec_done1(0);
1098 }
1099 else {
1100 /* not yet finished, wait another tenth rotation */
1101 local_irq_restore(flags);
1102 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1103 mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1104 }
1105}
1106
1107
1108static void fd_rwsec_done( int status )
1109{
1110 DPRINT(("fd_rwsec_done()\n"));
1111
1112 if (read_track) {
1113 del_timer(&readtrack_timer);
1114 if (!MultReadInProgress)
1115 return;
1116 MultReadInProgress = 0;
1117 }
1118 fd_rwsec_done1(status);
1119}
1120
1121static void fd_rwsec_done1(int status)
1122{
1123 unsigned int track;
1124
1125 stop_timeout();
1126
1127 /* Correct the track if stretch != 0 */
1128 if (SUDT->stretch) {
1129 track = FDC_READ( FDCREG_TRACK);
1130 MFPDELAY();
1131 FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1132 }
1133
1134 if (!UseTrackbuffer) {
1135 dma_wd.dma_mode_status = 0x90;
1136 MFPDELAY();
1137 if (!(dma_wd.dma_mode_status & 0x01)) {
1138 printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1139 goto err_end;
1140 }
1141 }
1142 MFPDELAY();
1143
1144 if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1145 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1146 goto err_end;
1147 }
1148 if ((status & FDCSTAT_RECNF) &&
1149 /* RECNF is no error after a multiple read when the FDC
1150 searched for a non-existent sector! */
1151 !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1152 if (Probing) {
5ceadd2a 1153 if (SUDT > atari_disk_type) {
1da177e4
LT
1154 if (SUDT[-1].blocks > ReqBlock) {
1155 /* try another disk type */
1156 SUDT--;
1157 set_capacity(unit[SelectedDrive].disk,
1158 SUDT->blocks);
1159 } else
1160 Probing = 0;
1161 }
1162 else {
1163 if (SUD.flags & FTD_MSG)
1164 printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1165 SelectedDrive, SUDT->name );
1166 Probing=0;
1167 }
1168 } else {
1169/* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1170 if (SUD.autoprobe) {
5ceadd2a 1171 SUDT = atari_disk_type + StartDiskType[DriveType];
1da177e4
LT
1172 set_capacity(unit[SelectedDrive].disk,
1173 SUDT->blocks);
1174 Probing = 1;
1175 }
1176 }
1177 if (Probing) {
1178 if (ATARIHW_PRESENT(FDCSPEED)) {
1179 dma_wd.fdc_speed = SUDT->fdc_speed;
1180 MFPDELAY();
1181 }
1182 setup_req_params( SelectedDrive );
1183 BufferDrive = -1;
1184 do_fd_action( SelectedDrive );
1185 return;
1186 }
1187
1188 printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1189 SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1190 goto err_end;
1191 }
1192 if (status & FDCSTAT_CRC) {
1193 printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1194 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1195 goto err_end;
1196 }
1197 if (status & FDCSTAT_LOST) {
1198 printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1199 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1200 goto err_end;
1201 }
1202
1203 Probing = 0;
1204
1205 if (ReqCmd == READ) {
1206 if (!read_track) {
1207 void *addr;
1208 addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1209 dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1210 if (!ATARIHW_PRESENT( EXTD_DMA ))
1211 copy_buffer (addr, ReqData);
1212 } else {
1213 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1214 BufferDrive = SelectedDrive;
1215 BufferSide = ReqSide;
1216 BufferTrack = ReqTrack;
1217 copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1218 }
1219 }
1220
a336ca6f 1221 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
1da177e4
LT
1222 /* read next sector */
1223 setup_req_params( SelectedDrive );
1224 do_fd_action( SelectedDrive );
1225 }
1226 else {
1227 /* all sectors finished */
2a842aca 1228 fd_end_request_cur(BLK_STS_OK);
1da177e4
LT
1229 }
1230 return;
1231
1232 err_end:
1233 BufferDrive = -1;
1234 fd_error();
1235}
1236
1237
1238static void fd_writetrack( void )
1239{
1240 unsigned long paddr, flags;
1241 unsigned int track;
1242
1243 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1244
1245 paddr = PhysTrackBuffer;
1246 dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1247
1248 fd_select_side( ReqSide );
1249
1250 /* Cheat for track if stretch != 0 */
1251 if (SUDT->stretch) {
1252 track = FDC_READ( FDCREG_TRACK);
1253 MFPDELAY();
1254 FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1255 }
1256 udelay(40);
1257
1258 /* Setup DMA */
1259 local_irq_save(flags);
1260 dma_wd.dma_lo = (unsigned char)paddr;
1261 MFPDELAY();
1262 paddr >>= 8;
1263 dma_wd.dma_md = (unsigned char)paddr;
1264 MFPDELAY();
1265 paddr >>= 8;
1266 if (ATARIHW_PRESENT( EXTD_DMA ))
1267 st_dma_ext_dmahi = (unsigned short)paddr;
1268 else
1269 dma_wd.dma_hi = (unsigned char)paddr;
1270 MFPDELAY();
1271 local_irq_restore(flags);
1272
1273 /* Clear FIFO and switch DMA to correct mode */
1274 dma_wd.dma_mode_status = 0x190;
1275 MFPDELAY();
1276 dma_wd.dma_mode_status = 0x90;
1277 MFPDELAY();
1278 dma_wd.dma_mode_status = 0x190;
1279 MFPDELAY();
1280
1281 /* How many sectors for DMA */
1282 dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1283 udelay(40);
1284
1285 /* Start operation */
1286 dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1287 udelay(40);
1288 SET_IRQ_HANDLER( fd_writetrack_done );
1289 dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1290
1291 MotorOn = 1;
1292 start_timeout();
1293 /* wait for interrupt */
1294}
1295
1296
1297static void fd_writetrack_done( int status )
1298{
1299 DPRINT(("fd_writetrack_done()\n"));
1300
1301 stop_timeout();
1302
1303 if (status & FDCSTAT_WPROT) {
1304 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1305 goto err_end;
1306 }
1307 if (status & FDCSTAT_LOST) {
1308 printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1309 SelectedDrive, ReqSide, ReqTrack );
1310 goto err_end;
1311 }
1312
7b8a3d22 1313 complete(&format_wait);
1da177e4
LT
1314 return;
1315
1316 err_end:
1317 fd_error();
1318}
1319
24ed960a 1320static void fd_times_out(struct timer_list *unused)
1da177e4
LT
1321{
1322 atari_disable_irq( IRQ_MFP_FDC );
1323 if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1324 * before we came here... */
1325
1326 SET_IRQ_HANDLER( NULL );
1327 /* If the timeout occurred while the readtrack_check timer was
1328 * active, we need to cancel it, else bad things will happen */
1329 if (UseTrackbuffer)
1330 del_timer( &readtrack_timer );
1331 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1332 udelay( 25 );
1333
1334 printk(KERN_ERR "floppy timeout\n" );
1335 fd_error();
1336 end:
1337 atari_enable_irq( IRQ_MFP_FDC );
1338}
1339
1340
1341/* The (noop) seek operation here is needed to make the WP bit in the
1342 * FDC status register accessible for check_change. If the last disk
1343 * operation would have been a RDSEC, this bit would always read as 0
1344 * no matter what :-( To save time, the seek goes to the track we're
1345 * already on.
1346 */
1347
1348static void finish_fdc( void )
1349{
1350 if (!NeedSeek) {
1351 finish_fdc_done( 0 );
1352 }
1353 else {
1354 DPRINT(("finish_fdc: dummy seek started\n"));
1355 FDC_WRITE (FDCREG_DATA, SUD.track);
1356 SET_IRQ_HANDLER( finish_fdc_done );
1357 FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1358 MotorOn = 1;
1359 start_timeout();
1360 /* we must wait for the IRQ here, because the ST-DMA
1361 is released immediately afterwards and the interrupt
1362 may be delivered to the wrong driver. */
1363 }
1364}
1365
1366
1367static void finish_fdc_done( int dummy )
1368{
1369 unsigned long flags;
1370
1371 DPRINT(("finish_fdc_done entered\n"));
1372 stop_timeout();
1373 NeedSeek = 0;
1374
1375 if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1376 /* If the check for a disk change is done too early after this
1377 * last seek command, the WP bit still reads wrong :-((
1378 */
1379 mod_timer(&fd_timer, jiffies + 5);
1380 else
1381 start_check_change_timer();
1382 start_motor_off_timer();
1383
1384 local_irq_save(flags);
1385 stdma_release();
1da177e4
LT
1386 local_irq_restore(flags);
1387
1388 DPRINT(("finish_fdc() finished\n"));
1389}
1390
1391/* The detection of disk changes is a dark chapter in Atari history :-(
1392 * Because the "Drive ready" signal isn't present in the Atari
1393 * hardware, one has to rely on the "Write Protect". This works fine,
1394 * as long as no write protected disks are used. TOS solves this
1395 * problem by introducing tri-state logic ("maybe changed") and
1396 * looking at the serial number in block 0. This isn't possible for
1397 * Linux, since the floppy driver can't make assumptions about the
1398 * filesystem used on the disk and thus the contents of block 0. I've
1399 * chosen the method to always say "The disk was changed" if it is
1400 * unsure whether it was. This implies that every open or mount
1401 * invalidates the disk buffers if you work with write protected
1402 * disks. But at least this is better than working with incorrect data
1403 * due to unrecognised disk changes.
1404 */
1405
1a8a74f0
TH
1406static unsigned int floppy_check_events(struct gendisk *disk,
1407 unsigned int clearing)
1da177e4
LT
1408{
1409 struct atari_floppy_struct *p = disk->private_data;
1410 unsigned int drive = p - unit;
1411 if (test_bit (drive, &fake_change)) {
1412 /* simulated change (e.g. after formatting) */
1a8a74f0 1413 return DISK_EVENT_MEDIA_CHANGE;
1da177e4
LT
1414 }
1415 if (test_bit (drive, &changed_floppies)) {
1416 /* surely changed (the WP signal changed at least once) */
1a8a74f0 1417 return DISK_EVENT_MEDIA_CHANGE;
1da177e4
LT
1418 }
1419 if (UD.wpstat) {
1420 /* WP is on -> could be changed: to be sure, buffers should be
1421 * invalidated...
1422 */
1a8a74f0 1423 return DISK_EVENT_MEDIA_CHANGE;
1da177e4
LT
1424 }
1425
1426 return 0;
1427}
1428
1429static int floppy_revalidate(struct gendisk *disk)
1430{
1431 struct atari_floppy_struct *p = disk->private_data;
1432 unsigned int drive = p - unit;
1433
1434 if (test_bit(drive, &changed_floppies) ||
1435 test_bit(drive, &fake_change) ||
1436 p->disktype == 0) {
1437 if (UD.flags & FTD_MSG)
1438 printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1439 BufferDrive = -1;
1440 clear_bit(drive, &fake_change);
1441 clear_bit(drive, &changed_floppies);
1442 /* MSch: clearing geometry makes sense only for autoprobe
1443 formats, for 'permanent user-defined' parameter:
1444 restore default_params[] here if flagged valid! */
1445 if (default_params[drive].blocks == 0)
2c7af511 1446 UDT = NULL;
1da177e4
LT
1447 else
1448 UDT = &default_params[drive];
1449 }
1450 return 0;
1451}
1452
1453
1454/* This sets up the global variables describing the current request. */
1455
1456static void setup_req_params( int drive )
1457{
1458 int block = ReqBlock + ReqCnt;
1459
1460 ReqTrack = block / UDT->spt;
1461 ReqSector = block - ReqTrack * UDT->spt + 1;
1462 ReqSide = ReqTrack & 1;
1463 ReqTrack >>= 1;
1464 ReqData = ReqBuffer + 512 * ReqCnt;
1465
1466 if (UseTrackbuffer)
c8e90782 1467 read_track = (ReqCmd == READ && fd_request->error_count == 0);
1da177e4
LT
1468 else
1469 read_track = 0;
1470
1471 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1472 ReqTrack, ReqSector, (unsigned long)ReqData ));
1473}
1474
80ff2040
JA
1475static void ataflop_commit_rqs(struct blk_mq_hw_ctx *hctx)
1476{
1477 spin_lock_irq(&ataflop_lock);
1478 atari_disable_irq(IRQ_MFP_FDC);
1479 finish_fdc();
1480 atari_enable_irq(IRQ_MFP_FDC);
1481 spin_unlock_irq(&ataflop_lock);
1482}
1483
6ec3938c
OS
1484static blk_status_t ataflop_queue_rq(struct blk_mq_hw_ctx *hctx,
1485 const struct blk_mq_queue_data *bd)
639e2f2a 1486{
6ec3938c
OS
1487 struct atari_floppy_struct *floppy = bd->rq->rq_disk->private_data;
1488 int drive = floppy - unit;
1489 int type = floppy->type;
1da177e4 1490
6ec3938c
OS
1491 spin_lock_irq(&ataflop_lock);
1492 if (fd_request) {
1493 spin_unlock_irq(&ataflop_lock);
1494 return BLK_STS_DEV_RESOURCE;
1495 }
1496 if (!stdma_try_lock(floppy_irq, NULL)) {
1497 spin_unlock_irq(&ataflop_lock);
1498 return BLK_STS_RESOURCE;
1499 }
1500 fd_request = bd->rq;
1501 blk_mq_start_request(fd_request);
1da177e4 1502
6ec3938c 1503 atari_disable_irq( IRQ_MFP_FDC );
1da177e4
LT
1504
1505 IsFormatting = 0;
1506
1da177e4
LT
1507 if (!UD.connected) {
1508 /* drive not connected */
1509 printk(KERN_ERR "Unknown Device: fd%d\n", drive );
2a842aca 1510 fd_end_request_cur(BLK_STS_IOERR);
6ec3938c 1511 goto out;
1da177e4
LT
1512 }
1513
1514 if (type == 0) {
1515 if (!UDT) {
1516 Probing = 1;
5ceadd2a 1517 UDT = atari_disk_type + StartDiskType[DriveType];
1da177e4
LT
1518 set_capacity(floppy->disk, UDT->blocks);
1519 UD.autoprobe = 1;
1520 }
1521 }
1522 else {
1523 /* user supplied disk type */
1524 if (--type >= NUM_DISK_MINORS) {
1525 printk(KERN_WARNING "fd%d: invalid disk format", drive );
2a842aca 1526 fd_end_request_cur(BLK_STS_IOERR);
6ec3938c 1527 goto out;
1da177e4
LT
1528 }
1529 if (minor2disktype[type].drive_types > DriveType) {
1530 printk(KERN_WARNING "fd%d: unsupported disk format", drive );
2a842aca 1531 fd_end_request_cur(BLK_STS_IOERR);
6ec3938c 1532 goto out;
1da177e4
LT
1533 }
1534 type = minor2disktype[type].index;
5ceadd2a 1535 UDT = &atari_disk_type[type];
1da177e4
LT
1536 set_capacity(floppy->disk, UDT->blocks);
1537 UD.autoprobe = 0;
1538 }
1da177e4
LT
1539
1540 /* stop deselect timer */
1541 del_timer( &motor_off_timer );
1542
1543 ReqCnt = 0;
a336ca6f
TH
1544 ReqCmd = rq_data_dir(fd_request);
1545 ReqBlock = blk_rq_pos(fd_request);
b4f42e28 1546 ReqBuffer = bio_data(fd_request->bio);
1da177e4
LT
1547 setup_req_params( drive );
1548 do_fd_action( drive );
1549
6ec3938c
OS
1550 if (bd->last)
1551 finish_fdc();
1da177e4 1552 atari_enable_irq( IRQ_MFP_FDC );
6ec3938c
OS
1553
1554out:
1555 spin_unlock_irq(&ataflop_lock);
1556 return BLK_STS_OK;
1da177e4
LT
1557}
1558
8a6cfeb6 1559static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1da177e4
LT
1560 unsigned int cmd, unsigned long param)
1561{
60ad2340 1562 struct gendisk *disk = bdev->bd_disk;
1da177e4
LT
1563 struct atari_floppy_struct *floppy = disk->private_data;
1564 int drive = floppy - unit;
1565 int type = floppy->type;
1566 struct atari_format_descr fmt_desc;
1567 struct atari_disk_type *dtp;
1568 struct floppy_struct getprm;
1569 int settype;
1570 struct floppy_struct setprm;
2c7af511 1571 void __user *argp = (void __user *)param;
1da177e4
LT
1572
1573 switch (cmd) {
1574 case FDGETPRM:
1575 if (type) {
1576 if (--type >= NUM_DISK_MINORS)
1577 return -ENODEV;
1578 if (minor2disktype[type].drive_types > DriveType)
1579 return -ENODEV;
1580 type = minor2disktype[type].index;
5ceadd2a 1581 dtp = &atari_disk_type[type];
1da177e4
LT
1582 if (UD.flags & FTD_MSG)
1583 printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1584 drive, dtp, dtp->name);
1585 }
1586 else {
1587 if (!UDT)
1588 return -ENXIO;
1589 else
1590 dtp = UDT;
1591 }
1592 memset((void *)&getprm, 0, sizeof(getprm));
1593 getprm.size = dtp->blocks;
1594 getprm.sect = dtp->spt;
1595 getprm.head = 2;
1596 getprm.track = dtp->blocks/dtp->spt/2;
1597 getprm.stretch = dtp->stretch;
2c7af511 1598 if (copy_to_user(argp, &getprm, sizeof(getprm)))
1da177e4
LT
1599 return -EFAULT;
1600 return 0;
1601 }
1602 switch (cmd) {
1603 case FDSETPRM:
1604 case FDDEFPRM:
1605 /*
1606 * MSch 7/96: simple 'set geometry' case: just set the
1607 * 'default' device params (minor == 0).
1608 * Currently, the drive geometry is cleared after each
1609 * disk change and subsequent revalidate()! simple
1610 * implementation of FDDEFPRM: save geometry from a
1611 * FDDEFPRM call and restore it in floppy_revalidate() !
1612 */
1613
1614 /* get the parameters from user space */
1615 if (floppy->ref != 1 && floppy->ref != -1)
1616 return -EBUSY;
2c7af511 1617 if (copy_from_user(&setprm, argp, sizeof(setprm)))
1da177e4
LT
1618 return -EFAULT;
1619 /*
1620 * first of all: check for floppy change and revalidate,
1621 * or the next access will revalidate - and clear UDT :-(
1622 */
1623
1a8a74f0 1624 if (floppy_check_events(disk, 0))
1da177e4
LT
1625 floppy_revalidate(disk);
1626
1627 if (UD.flags & FTD_MSG)
1628 printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1629 drive, setprm.size, setprm.sect, setprm.stretch);
1630
1631 /* what if type > 0 here? Overwrite specified entry ? */
1632 if (type) {
1633 /* refuse to re-set a predefined type for now */
1da177e4
LT
1634 return -EINVAL;
1635 }
1636
1637 /*
1638 * type == 0: first look for a matching entry in the type list,
1639 * and set the UD.disktype field to use the perdefined entry.
1640 * TODO: add user-defined format to head of autoprobe list ?
1641 * Useful to include the user-type for future autodetection!
1642 */
1643
1644 for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1645 int setidx = 0;
1646 if (minor2disktype[settype].drive_types > DriveType) {
1647 /* skip this one, invalid for drive ... */
1648 continue;
1649 }
1650 setidx = minor2disktype[settype].index;
5ceadd2a 1651 dtp = &atari_disk_type[setidx];
1da177e4
LT
1652
1653 /* found matching entry ?? */
1654 if ( dtp->blocks == setprm.size
1655 && dtp->spt == setprm.sect
1656 && dtp->stretch == setprm.stretch ) {
1657 if (UD.flags & FTD_MSG)
1658 printk (KERN_INFO "floppy%d: setting %s %p!\n",
1659 drive, dtp->name, dtp);
1660 UDT = dtp;
1661 set_capacity(floppy->disk, UDT->blocks);
1662
1663 if (cmd == FDDEFPRM) {
1664 /* save settings as permanent default type */
1665 default_params[drive].name = dtp->name;
1666 default_params[drive].spt = dtp->spt;
1667 default_params[drive].blocks = dtp->blocks;
1668 default_params[drive].fdc_speed = dtp->fdc_speed;
1669 default_params[drive].stretch = dtp->stretch;
1670 }
1671
1672 return 0;
1673 }
1674
1675 }
1676
1677 /* no matching disk type found above - setting user_params */
1678
1679 if (cmd == FDDEFPRM) {
1680 /* set permanent type */
1681 dtp = &default_params[drive];
1682 } else
1683 /* set user type (reset by disk change!) */
1684 dtp = &user_params[drive];
1685
1686 dtp->name = "user format";
1687 dtp->blocks = setprm.size;
1688 dtp->spt = setprm.sect;
1689 if (setprm.sect > 14)
1690 dtp->fdc_speed = 3;
1691 else
1692 dtp->fdc_speed = 0;
1693 dtp->stretch = setprm.stretch;
1694
1695 if (UD.flags & FTD_MSG)
1696 printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1697 drive, dtp->blocks, dtp->spt, dtp->stretch);
1698
1699 /* sanity check */
8f474287 1700 if (setprm.track != dtp->blocks/dtp->spt/2 ||
6ec3938c 1701 setprm.head != 2)
1da177e4 1702 return -EINVAL;
1da177e4
LT
1703
1704 UDT = dtp;
1705 set_capacity(floppy->disk, UDT->blocks);
1706
1707 return 0;
1708 case FDMSGON:
1709 UD.flags |= FTD_MSG;
1710 return 0;
1711 case FDMSGOFF:
1712 UD.flags &= ~FTD_MSG;
1713 return 0;
1714 case FDSETEMSGTRESH:
1715 return -EINVAL;
1716 case FDFMTBEG:
1717 return 0;
1718 case FDFMTTRK:
1719 if (floppy->ref != 1 && floppy->ref != -1)
1720 return -EBUSY;
2c7af511 1721 if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
1da177e4
LT
1722 return -EFAULT;
1723 return do_format(drive, type, &fmt_desc);
1724 case FDCLRPRM:
1725 UDT = NULL;
1726 /* MSch: invalidate default_params */
1727 default_params[drive].blocks = 0;
1728 set_capacity(floppy->disk, MAX_DISK_SIZE * 2);
1729 case FDFMTEND:
1730 case FDFLUSH:
1731 /* invalidate the buffer track to force a reread */
1732 BufferDrive = -1;
1733 set_bit(drive, &fake_change);
60ad2340 1734 check_disk_change(bdev);
1da177e4
LT
1735 return 0;
1736 default:
1737 return -EINVAL;
1738 }
1739}
1740
8a6cfeb6
AB
1741static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1742 unsigned int cmd, unsigned long arg)
1743{
1744 int ret;
1745
2a48fc0a 1746 mutex_lock(&ataflop_mutex);
8a6cfeb6 1747 ret = fd_locked_ioctl(bdev, mode, cmd, arg);
2a48fc0a 1748 mutex_unlock(&ataflop_mutex);
8a6cfeb6
AB
1749
1750 return ret;
1751}
1da177e4
LT
1752
1753/* Initialize the 'unit' variable for drive 'drive' */
1754
1755static void __init fd_probe( int drive )
1756{
1757 UD.connected = 0;
1758 UDT = NULL;
1759
1760 if (!fd_test_drive_present( drive ))
1761 return;
1762
1763 UD.connected = 1;
1764 UD.track = 0;
1765 switch( UserSteprate[drive] ) {
1766 case 2:
1767 UD.steprate = FDCSTEP_2;
1768 break;
1769 case 3:
1770 UD.steprate = FDCSTEP_3;
1771 break;
1772 case 6:
1773 UD.steprate = FDCSTEP_6;
1774 break;
1775 case 12:
1776 UD.steprate = FDCSTEP_12;
1777 break;
1778 default: /* should be -1 for "not set by user" */
1779 if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1780 UD.steprate = FDCSTEP_3;
1781 else
1782 UD.steprate = FDCSTEP_6;
1783 break;
1784 }
1785 MotorOn = 1; /* from probe restore operation! */
1786}
1787
1788
1789/* This function tests the physical presence of a floppy drive (not
1790 * whether a disk is inserted). This is done by issuing a restore
1791 * command, waiting max. 2 seconds (that should be enough to move the
1792 * head across the whole disk) and looking at the state of the "TR00"
1793 * signal. This should now be raised if there is a drive connected
1794 * (and there is no hardware failure :-) Otherwise, the drive is
1795 * declared absent.
1796 */
1797
1798static int __init fd_test_drive_present( int drive )
1799{
1800 unsigned long timeout;
1801 unsigned char status;
1802 int ok;
1803
1804 if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1805 fd_select_drive( drive );
1806
1807 /* disable interrupt temporarily */
1808 atari_turnoff_irq( IRQ_MFP_FDC );
1809 FDC_WRITE (FDCREG_TRACK, 0xff00);
1810 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1811
1812 timeout = jiffies + 2*HZ+HZ/2;
1813 while (time_before(jiffies, timeout))
3d92e8f3 1814 if (!(st_mfp.par_dt_reg & 0x20))
1da177e4
LT
1815 break;
1816
1817 status = FDC_READ( FDCREG_STATUS );
1818 ok = (status & FDCSTAT_TR00) != 0;
1819
1820 /* force interrupt to abort restore operation (FDC would try
1821 * about 50 seconds!) */
1822 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1823 udelay(500);
1824 status = FDC_READ( FDCREG_STATUS );
1825 udelay(20);
1826
1827 if (ok) {
1828 /* dummy seek command to make WP bit accessible */
1829 FDC_WRITE( FDCREG_DATA, 0 );
1830 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
3d92e8f3 1831 while( st_mfp.par_dt_reg & 0x20 )
1da177e4
LT
1832 ;
1833 status = FDC_READ( FDCREG_STATUS );
1834 }
1835
1836 atari_turnon_irq( IRQ_MFP_FDC );
1837 return( ok );
1838}
1839
1840
1841/* Look how many and which kind of drives are connected. If there are
1842 * floppies, additionally start the disk-change and motor-off timers.
1843 */
1844
1845static void __init config_types( void )
1846{
1847 int drive, cnt = 0;
1848
1849 /* for probing drives, set the FDC speed to 8 MHz */
1850 if (ATARIHW_PRESENT(FDCSPEED))
1851 dma_wd.fdc_speed = 0;
1852
1853 printk(KERN_INFO "Probing floppy drive(s):\n");
1854 for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1855 fd_probe( drive );
1856 if (UD.connected) {
1857 printk(KERN_INFO "fd%d\n", drive);
1858 ++cnt;
1859 }
1860 }
1861
1862 if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1863 /* If FDC is still busy from probing, give it another FORCI
1864 * command to abort the operation. If this isn't done, the FDC
1865 * will interrupt later and its IRQ line stays low, because
1866 * the status register isn't read. And this will block any
1867 * interrupts on this IRQ line :-(
1868 */
1869 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1870 udelay(500);
1871 FDC_READ( FDCREG_STATUS );
1872 udelay(20);
1873 }
1874
1875 if (cnt > 0) {
1876 start_motor_off_timer();
1877 if (cnt == 1) fd_select_drive( 0 );
1878 start_check_change_timer();
1879 }
1880}
1881
1882/*
1883 * floppy_open check for aliasing (/dev/fd0 can be the same as
1884 * /dev/PS0 etc), and disallows simultaneous access to the same
1885 * drive with different device numbers.
1886 */
1887
60ad2340 1888static int floppy_open(struct block_device *bdev, fmode_t mode)
1da177e4 1889{
60ad2340
AV
1890 struct atari_floppy_struct *p = bdev->bd_disk->private_data;
1891 int type = MINOR(bdev->bd_dev) >> 2;
1da177e4
LT
1892
1893 DPRINT(("fd_open: type=%d\n",type));
1894 if (p->ref && p->type != type)
1895 return -EBUSY;
1896
60ad2340 1897 if (p->ref == -1 || (p->ref && mode & FMODE_EXCL))
1da177e4
LT
1898 return -EBUSY;
1899
60ad2340 1900 if (mode & FMODE_EXCL)
1da177e4
LT
1901 p->ref = -1;
1902 else
1903 p->ref++;
1904
1905 p->type = type;
1906
60ad2340 1907 if (mode & FMODE_NDELAY)
1da177e4
LT
1908 return 0;
1909
60ad2340
AV
1910 if (mode & (FMODE_READ|FMODE_WRITE)) {
1911 check_disk_change(bdev);
1912 if (mode & FMODE_WRITE) {
1da177e4
LT
1913 if (p->wpstat) {
1914 if (p->ref < 0)
1915 p->ref = 0;
1916 else
1917 p->ref--;
1da177e4
LT
1918 return -EROFS;
1919 }
1920 }
1921 }
1922 return 0;
1923}
1924
6e9624b8
AB
1925static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
1926{
1927 int ret;
1928
2a48fc0a 1929 mutex_lock(&ataflop_mutex);
6e9624b8 1930 ret = floppy_open(bdev, mode);
2a48fc0a 1931 mutex_unlock(&ataflop_mutex);
6e9624b8
AB
1932
1933 return ret;
1934}
1da177e4 1935
db2a144b 1936static void floppy_release(struct gendisk *disk, fmode_t mode)
1da177e4 1937{
60ad2340 1938 struct atari_floppy_struct *p = disk->private_data;
2a48fc0a 1939 mutex_lock(&ataflop_mutex);
1da177e4
LT
1940 if (p->ref < 0)
1941 p->ref = 0;
1942 else if (!p->ref--) {
1943 printk(KERN_ERR "floppy_release with fd_ref == 0");
1944 p->ref = 0;
1945 }
2a48fc0a 1946 mutex_unlock(&ataflop_mutex);
1da177e4
LT
1947}
1948
83d5cde4 1949static const struct block_device_operations floppy_fops = {
1da177e4 1950 .owner = THIS_MODULE,
6e9624b8 1951 .open = floppy_unlocked_open,
60ad2340 1952 .release = floppy_release,
8a6cfeb6 1953 .ioctl = fd_ioctl,
1a8a74f0 1954 .check_events = floppy_check_events,
1da177e4
LT
1955 .revalidate_disk= floppy_revalidate,
1956};
1957
6ec3938c
OS
1958static const struct blk_mq_ops ataflop_mq_ops = {
1959 .queue_rq = ataflop_queue_rq,
80ff2040 1960 .commit_rqs = ataflop_commit_rqs,
6ec3938c
OS
1961};
1962
1da177e4
LT
1963static struct kobject *floppy_find(dev_t dev, int *part, void *data)
1964{
1965 int drive = *part & 3;
1966 int type = *part >> 2;
1967 if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1968 return NULL;
1969 *part = 0;
3079c22e 1970 return get_disk_and_module(unit[drive].disk);
1da177e4
LT
1971}
1972
1973static int __init atari_floppy_init (void)
1974{
1975 int i;
6ec3938c 1976 int ret;
1da177e4
LT
1977
1978 if (!MACH_IS_ATARI)
1979 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
e945b568 1980 return -ENODEV;
1da177e4 1981
1da177e4
LT
1982 if (register_blkdev(FLOPPY_MAJOR,"fd"))
1983 return -EBUSY;
1984
1985 for (i = 0; i < FD_MAX_UNITS; i++) {
1986 unit[i].disk = alloc_disk(1);
6ec3938c
OS
1987 if (!unit[i].disk) {
1988 ret = -ENOMEM;
1989 goto err;
1990 }
71327f54 1991
6ec3938c
OS
1992 unit[i].disk->queue = blk_mq_init_sq_queue(&unit[i].tag_set,
1993 &ataflop_mq_ops, 2,
1994 BLK_MQ_F_SHOULD_MERGE);
1995 if (IS_ERR(unit[i].disk->queue)) {
49379e6d 1996 put_disk(unit[i].disk);
6ec3938c
OS
1997 ret = PTR_ERR(unit[i].disk->queue);
1998 unit[i].disk->queue = NULL;
1999 goto err;
2000 }
1da177e4
LT
2001 }
2002
2003 if (UseTrackbuffer < 0)
2004 /* not set by user -> use default: for now, we turn
2005 track buffering off for all Medusas, though it
2006 could be used with ones that have a counter
2007 card. But the test is too hard :-( */
2008 UseTrackbuffer = !MACH_IS_MEDUSA;
2009
2010 /* initialize variables */
2011 SelectedDrive = -1;
2012 BufferDrive = -1;
2013
2014 DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
2015 if (!DMABuffer) {
2016 printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
6ec3938c
OS
2017 ret = -ENOMEM;
2018 goto err;
1da177e4
LT
2019 }
2020 TrackBuffer = DMABuffer + 512;
a4de73fb 2021 PhysDMABuffer = atari_stram_to_phys(DMABuffer);
1da177e4
LT
2022 PhysTrackBuffer = virt_to_phys(TrackBuffer);
2023 BufferDrive = BufferSide = BufferTrack = -1;
2024
1da177e4
LT
2025 for (i = 0; i < FD_MAX_UNITS; i++) {
2026 unit[i].track = -1;
2027 unit[i].flags = 0;
2028 unit[i].disk->major = FLOPPY_MAJOR;
2029 unit[i].disk->first_minor = i;
2030 sprintf(unit[i].disk->disk_name, "fd%d", i);
2031 unit[i].disk->fops = &floppy_fops;
773008f6 2032 unit[i].disk->events = DISK_EVENT_MEDIA_CHANGE;
1da177e4 2033 unit[i].disk->private_data = &unit[i];
1da177e4
LT
2034 set_capacity(unit[i].disk, MAX_DISK_SIZE * 2);
2035 add_disk(unit[i].disk);
2036 }
2037
2038 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
2039 floppy_find, NULL, NULL);
2040
2041 printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
2042 DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
2043 UseTrackbuffer ? "" : "no ");
2044 config_types();
2045
2046 return 0;
6ec3938c
OS
2047
2048err:
49379e6d 2049 while (--i >= 0) {
71327f54 2050 struct gendisk *disk = unit[i].disk;
639e2f2a 2051
49379e6d
DC
2052 blk_cleanup_queue(disk->queue);
2053 blk_mq_free_tag_set(&unit[i].tag_set);
2054 put_disk(unit[i].disk);
2055 }
639e2f2a 2056
1da177e4 2057 unregister_blkdev(FLOPPY_MAJOR, "fd");
6ec3938c 2058 return ret;
1da177e4
LT
2059}
2060
63907435
AV
2061#ifndef MODULE
2062static int __init atari_floppy_setup(char *str)
1da177e4 2063{
63907435 2064 int ints[3 + FD_MAX_UNITS];
1da177e4 2065 int i;
63907435
AV
2066
2067 if (!MACH_IS_ATARI)
2068 return 0;
2069
2070 str = get_options(str, 3 + FD_MAX_UNITS, ints);
1da177e4
LT
2071
2072 if (ints[0] < 1) {
2073 printk(KERN_ERR "ataflop_setup: no arguments!\n" );
63907435 2074 return 0;
1da177e4
LT
2075 }
2076 else if (ints[0] > 2+FD_MAX_UNITS) {
2077 printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2078 }
2079
2080 if (ints[1] < 0 || ints[1] > 2)
2081 printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2082 else
2083 DriveType = ints[1];
2084
2085 if (ints[0] >= 2)
2086 UseTrackbuffer = (ints[2] > 0);
2087
2088 for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2089 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2090 printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2091 else
2092 UserSteprate[i-3] = ints[i];
2093 }
63907435 2094 return 1;
1da177e4
LT
2095}
2096
63907435
AV
2097__setup("floppy=", atari_floppy_setup);
2098#endif
2099
2100static void __exit atari_floppy_exit(void)
1da177e4
LT
2101{
2102 int i;
2103 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
2104 for (i = 0; i < FD_MAX_UNITS; i++) {
2105 del_gendisk(unit[i].disk);
6ec3938c
OS
2106 blk_cleanup_queue(unit[i].disk->queue);
2107 blk_mq_free_tag_set(&unit[i].tag_set);
1da177e4
LT
2108 put_disk(unit[i].disk);
2109 }
2110 unregister_blkdev(FLOPPY_MAJOR, "fd");
2111
1da177e4
LT
2112 del_timer_sync(&fd_timer);
2113 atari_stram_free( DMABuffer );
2114}
2115
2116module_init(atari_floppy_init)
2117module_exit(atari_floppy_exit)
2118
2119MODULE_LICENSE("GPL");