blob: 17df631c5d8591e26528f2f309bef15e6d55a845 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020070#include <linux/mutex.h>
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +010071#include <linux/completion.h>
72#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#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
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020082static DEFINE_MUTEX(ataflop_mutex);
Tejun Heoa336ca62009-05-08 11:54:03 +090083static struct request *fd_request;
Vivek Goyal639e2f22010-09-24 20:35:45 +020084static int fdc_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Omar Sandoval3e6b8c3c2018-10-11 12:20:48 -070086/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* 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 ? */
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -0800174} atari_disk_type[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 { "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
Tobias Klauser945f3902006-01-08 01:05:11 -0800265#define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
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;
303} unit[FD_MAX_UNITS];
304
305#define UD unit[drive]
306#define UDT unit[drive].disktype
307#define SUD unit[SelectedDrive]
308#define SUDT unit[SelectedDrive].disktype
309
310
311#define FDC_READ(reg) ({ \
312 /* unsigned long __flags; */ \
313 unsigned short __val; \
314 /* local_irq_save(__flags); */ \
315 dma_wd.dma_mode_status = 0x80 | (reg); \
316 udelay(25); \
317 __val = dma_wd.fdc_acces_seccount; \
318 MFPDELAY(); \
319 /* local_irq_restore(__flags); */ \
320 __val & 0xff; \
321})
322
323#define FDC_WRITE(reg,val) \
324 do { \
325 /* unsigned long __flags; */ \
326 /* local_irq_save(__flags); */ \
327 dma_wd.dma_mode_status = 0x80 | (reg); \
328 udelay(25); \
329 dma_wd.fdc_acces_seccount = (val); \
330 MFPDELAY(); \
331 /* local_irq_restore(__flags); */ \
332 } while(0)
333
334
335/* Buffering variables:
336 * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
337 * operations. Second, a track buffer is used to cache a whole track
338 * of the disk to save read operations. These are two separate buffers
339 * because that allows write operations without clearing the track buffer.
340 */
341
342static int MaxSectors[] = {
343 11, 22, 44
344};
345static int BufferSize[] = {
346 15*512, 30*512, 60*512
347};
348
349#define BUFFER_SIZE (BufferSize[DriveType])
350
351unsigned char *DMABuffer; /* buffer for writes */
352static unsigned long PhysDMABuffer; /* physical address */
353
354static int UseTrackbuffer = -1; /* Do track buffering? */
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800355module_param(UseTrackbuffer, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357unsigned char *TrackBuffer; /* buffer for reads */
358static unsigned long PhysTrackBuffer; /* physical address */
359static int BufferDrive, BufferSide, BufferTrack;
360static int read_track; /* non-zero if we are reading whole tracks */
361
362#define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
363#define IS_BUFFERED(drive,side,track) \
364 (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
365
366/*
367 * These are global variables, as that's the easiest way to give
368 * information to interrupts. They are the data used for the current
369 * request.
370 */
371static int SelectedDrive = 0;
372static int ReqCmd, ReqBlock;
373static int ReqSide, ReqTrack, ReqSector, ReqCnt;
374static int HeadSettleFlag = 0;
375static unsigned char *ReqData, *ReqBuffer;
376static int MotorOn = 0, MotorOffTrys;
377static int IsFormatting = 0, FormatError;
378
379static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800380module_param_array(UserSteprate, int, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382/* Synchronization of FDC access. */
383static volatile int fdc_busy = 0;
384static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +0100385static DECLARE_COMPLETION(format_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387static unsigned long changed_floppies = 0xff, fake_change = 0;
388#define CHECK_CHANGE_DELAY HZ/2
389
390#define FD_MOTOR_OFF_DELAY (3*HZ)
391#define FD_MOTOR_OFF_MAXTRY (10*20)
392
393#define FLOPPY_TIMEOUT (6*HZ)
394#define RECALIBRATE_ERRORS 4 /* After this many errors the drive
395 * will be recalibrated. */
396#define MAX_ERRORS 8 /* After this many errors the driver
397 * will give up. */
398
399
400/*
401 * The driver is trying to determine the correct media format
402 * while Probing is set. fd_rwsec_done() clears it after a
403 * successful access.
404 */
405static int Probing = 0;
406
407/* This flag is set when a dummy seek is necessary to make the WP
408 * status bit accessible.
409 */
410static int NeedSeek = 0;
411
412
413#ifdef DEBUG
414#define DPRINT(a) printk a
415#else
416#define DPRINT(a)
417#endif
418
419/***************************** Prototypes *****************************/
420
421static void fd_select_side( int side );
422static void fd_select_drive( int drive );
423static void fd_deselect( void );
Kees Cook24ed9602017-08-28 11:28:21 -0700424static void fd_motor_off_timer(struct timer_list *unused);
425static void check_change(struct timer_list *unused);
David Howells7d12e782006-10-05 14:55:46 +0100426static irqreturn_t floppy_irq (int irq, void *dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427static void fd_error( void );
428static int do_format(int drive, int type, struct atari_format_descr *desc);
429static void do_fd_action( int drive );
430static void fd_calibrate( void );
431static void fd_calibrate_done( int status );
432static void fd_seek( void );
433static void fd_seek_done( int status );
434static void fd_rwsec( void );
Kees Cook24ed9602017-08-28 11:28:21 -0700435static void fd_readtrack_check(struct timer_list *unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void fd_rwsec_done( int status );
437static void fd_rwsec_done1(int status);
438static void fd_writetrack( void );
439static void fd_writetrack_done( int status );
Kees Cook24ed9602017-08-28 11:28:21 -0700440static void fd_times_out(struct timer_list *unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static void finish_fdc( void );
442static void finish_fdc_done( int dummy );
443static void setup_req_params( int drive );
444static void redo_fd_request( void);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200445static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 cmd, unsigned long param);
447static void fd_probe( int drive );
448static int fd_test_drive_present( int drive );
449static void config_types( void );
Al Viro60ad2342008-03-02 09:23:54 -0500450static int floppy_open(struct block_device *bdev, fmode_t mode);
Al Virodb2a1442013-05-05 21:52:57 -0400451static void floppy_release(struct gendisk *disk, fmode_t mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453/************************* End of Prototypes **************************/
454
Kees Cook1d27e3e2017-10-04 16:27:04 -0700455static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer);
456static DEFINE_TIMER(readtrack_timer, fd_readtrack_check);
457static DEFINE_TIMER(timeout_timer, fd_times_out);
458static DEFINE_TIMER(fd_timer, check_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200460static void fd_end_request_cur(blk_status_t err)
Tejun Heoa336ca62009-05-08 11:54:03 +0900461{
462 if (!__blk_end_request_cur(fd_request, err))
463 fd_request = NULL;
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466static 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
Kees Cook24ed9602017-08-28 11:28:21 -0700561static void fd_motor_off_timer(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
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
Kees Cook24ed9602017-08-28 11:28:21 -0700597static void check_change(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
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
David Howells7d12e782006-10-05 14:55:46 +0100663static irqreturn_t floppy_irq (int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
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;
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +0100692 complete(&format_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return;
694 }
695
Tejun Heoa336ca62009-05-08 11:54:03 +0900696 if (!fd_request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return;
698
Christoph Hellwigc8e90782017-04-20 16:03:13 +0200699 fd_request->error_count++;
700 if (fd_request->error_count >= MAX_ERRORS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200702 fd_end_request_cur(BLK_STS_IOERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Christoph Hellwigc8e90782017-04-20 16:03:13 +0200704 else if (fd_request->error_count == RECALIBRATE_ERRORS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
706 if (SelectedDrive != -1)
707 SUD.track = -1;
708 }
709 redo_fd_request();
710}
711
712
713
714#define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
715
716
717/* ---------- Formatting ---------- */
718
719#define FILL(n,val) \
720 do { \
721 memset( p, val, n ); \
722 p += n; \
723 } while(0)
724
725static int do_format(int drive, int type, struct atari_format_descr *desc)
726{
727 unsigned char *p;
728 int sect, nsect;
729 unsigned long flags;
730
731 DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
732 drive, desc->track, desc->head, desc->sect_offset ));
733
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +0100734 wait_event(fdc_wait, cmpxchg(&fdc_busy, 0, 1) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 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) {
743 redo_fd_request();
744 return -EINVAL;
745 }
746 type = minor2disktype[type].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -0800747 UDT = &atari_disk_type[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
750 if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
751 redo_fd_request();
752 return -EINVAL;
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
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +0100789 wait_for_completion(&format_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 redo_fd_request();
792 return( FormatError ? -EIO : 0 );
793}
794
795
796/* do_fd_action() is the general procedure for a fd request: All
797 * required parameter settings (drive select, side select, track
798 * position) are checked and set if needed. For each of these
799 * parameters and the actual reading or writing exist two functions:
800 * one that starts the setting (or skips it if possible) and one
801 * callback for the "done" interrupt. Each done func calls the next
802 * set function to propagate the request down to fd_rwsec_done().
803 */
804
805static void do_fd_action( int drive )
806{
807 DPRINT(("do_fd_action\n"));
808
809 if (UseTrackbuffer && !IsFormatting) {
810 repeat:
811 if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
812 if (ReqCmd == READ) {
813 copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
Tejun Heoa336ca62009-05-08 11:54:03 +0900814 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* read next sector */
816 setup_req_params( drive );
817 goto repeat;
818 }
819 else {
820 /* all sectors finished */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200821 fd_end_request_cur(BLK_STS_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 redo_fd_request();
823 return;
824 }
825 }
826 else {
827 /* cmd == WRITE, pay attention to track buffer
828 * consistency! */
829 copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
830 }
831 }
832 }
833
834 if (SelectedDrive != drive)
835 fd_select_drive( drive );
836
837 if (UD.track == -1)
838 fd_calibrate();
839 else if (UD.track != ReqTrack << UDT->stretch)
840 fd_seek();
841 else if (IsFormatting)
842 fd_writetrack();
843 else
844 fd_rwsec();
845}
846
847
848/* Seek to track 0 if the current track is unknown */
849
850static void fd_calibrate( void )
851{
852 if (SUD.track >= 0) {
853 fd_calibrate_done( 0 );
854 return;
855 }
856
857 if (ATARIHW_PRESENT(FDCSPEED))
858 dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
859 DPRINT(("fd_calibrate\n"));
860 SET_IRQ_HANDLER( fd_calibrate_done );
861 /* we can't verify, since the speed may be incorrect */
862 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
863
864 NeedSeek = 1;
865 MotorOn = 1;
866 start_timeout();
867 /* wait for IRQ */
868}
869
870
871static void fd_calibrate_done( int status )
872{
873 DPRINT(("fd_calibrate_done()\n"));
874 stop_timeout();
875
876 /* set the correct speed now */
877 if (ATARIHW_PRESENT(FDCSPEED))
878 dma_wd.fdc_speed = SUDT->fdc_speed;
879 if (status & FDCSTAT_RECNF) {
880 printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
881 fd_error();
882 }
883 else {
884 SUD.track = 0;
885 fd_seek();
886 }
887}
888
889
890/* Seek the drive to the requested track. The drive must have been
891 * calibrated at some point before this.
892 */
893
894static void fd_seek( void )
895{
896 if (SUD.track == ReqTrack << SUDT->stretch) {
897 fd_seek_done( 0 );
898 return;
899 }
900
901 if (ATARIHW_PRESENT(FDCSPEED)) {
902 dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
903 MFPDELAY();
904 }
905
906 DPRINT(("fd_seek() to track %d\n",ReqTrack));
907 FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
908 udelay(25);
909 SET_IRQ_HANDLER( fd_seek_done );
910 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
911
912 MotorOn = 1;
913 set_head_settle_flag();
914 start_timeout();
915 /* wait for IRQ */
916}
917
918
919static void fd_seek_done( int status )
920{
921 DPRINT(("fd_seek_done()\n"));
922 stop_timeout();
923
924 /* set the correct speed */
925 if (ATARIHW_PRESENT(FDCSPEED))
926 dma_wd.fdc_speed = SUDT->fdc_speed;
927 if (status & FDCSTAT_RECNF) {
928 printk(KERN_ERR "fd%d: seek error (to track %d)\n",
929 SelectedDrive, ReqTrack );
930 /* we don't know exactly which track we are on now! */
931 SUD.track = -1;
932 fd_error();
933 }
934 else {
935 SUD.track = ReqTrack << SUDT->stretch;
936 NeedSeek = 0;
937 if (IsFormatting)
938 fd_writetrack();
939 else
940 fd_rwsec();
941 }
942}
943
944
945/* This does the actual reading/writing after positioning the head
946 * over the correct track.
947 */
948
949static int MultReadInProgress = 0;
950
951
952static void fd_rwsec( void )
953{
954 unsigned long paddr, flags;
955 unsigned int rwflag, old_motoron;
956 unsigned int track;
957
958 DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
959 if (ReqCmd == WRITE) {
960 if (ATARIHW_PRESENT(EXTD_DMA)) {
961 paddr = virt_to_phys(ReqData);
962 }
963 else {
964 copy_buffer( ReqData, DMABuffer );
965 paddr = PhysDMABuffer;
966 }
967 dma_cache_maintenance( paddr, 512, 1 );
968 rwflag = 0x100;
969 }
970 else {
971 if (read_track)
972 paddr = PhysTrackBuffer;
973 else
974 paddr = ATARIHW_PRESENT(EXTD_DMA) ?
975 virt_to_phys(ReqData) : PhysDMABuffer;
976 rwflag = 0;
977 }
978
979 fd_select_side( ReqSide );
980
981 /* Start sector of this operation */
982 FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
983 MFPDELAY();
984 /* Cheat for track if stretch != 0 */
985 if (SUDT->stretch) {
986 track = FDC_READ( FDCREG_TRACK);
987 MFPDELAY();
988 FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
989 }
990 udelay(25);
991
992 /* Setup DMA */
993 local_irq_save(flags);
994 dma_wd.dma_lo = (unsigned char)paddr;
995 MFPDELAY();
996 paddr >>= 8;
997 dma_wd.dma_md = (unsigned char)paddr;
998 MFPDELAY();
999 paddr >>= 8;
1000 if (ATARIHW_PRESENT(EXTD_DMA))
1001 st_dma_ext_dmahi = (unsigned short)paddr;
1002 else
1003 dma_wd.dma_hi = (unsigned char)paddr;
1004 MFPDELAY();
1005 local_irq_restore(flags);
1006
1007 /* Clear FIFO and switch DMA to correct mode */
1008 dma_wd.dma_mode_status = 0x90 | rwflag;
1009 MFPDELAY();
1010 dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
1011 MFPDELAY();
1012 dma_wd.dma_mode_status = 0x90 | rwflag;
1013 MFPDELAY();
1014
1015 /* How many sectors for DMA */
1016 dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
1017
1018 udelay(25);
1019
1020 /* Start operation */
1021 dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
1022 udelay(25);
1023 SET_IRQ_HANDLER( fd_rwsec_done );
1024 dma_wd.fdc_acces_seccount =
1025 (get_head_settle_flag() |
1026 (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
1027
1028 old_motoron = MotorOn;
1029 MotorOn = 1;
1030 NeedSeek = 1;
1031 /* wait for interrupt */
1032
1033 if (read_track) {
1034 /* If reading a whole track, wait about one disk rotation and
1035 * then check if all sectors are read. The FDC will even
1036 * search for the first non-existent sector and need 1 sec to
1037 * recognise that it isn't present :-(
1038 */
1039 MultReadInProgress = 1;
1040 mod_timer(&readtrack_timer,
1041 /* 1 rot. + 5 rot.s if motor was off */
1042 jiffies + HZ/5 + (old_motoron ? 0 : HZ));
1043 }
1044 start_timeout();
1045}
1046
1047
Kees Cook24ed9602017-08-28 11:28:21 -07001048static void fd_readtrack_check(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 unsigned long flags, addr, addr2;
1051
1052 local_irq_save(flags);
1053
1054 if (!MultReadInProgress) {
1055 /* This prevents a race condition that could arise if the
1056 * interrupt is triggered while the calling of this timer
1057 * callback function takes place. The IRQ function then has
1058 * already cleared 'MultReadInProgress' when flow of control
1059 * gets here.
1060 */
1061 local_irq_restore(flags);
1062 return;
1063 }
1064
1065 /* get the current DMA address */
1066 /* ++ f.a. read twice to avoid being fooled by switcher */
1067 addr = 0;
1068 do {
1069 addr2 = addr;
1070 addr = dma_wd.dma_lo & 0xff;
1071 MFPDELAY();
1072 addr |= (dma_wd.dma_md & 0xff) << 8;
1073 MFPDELAY();
1074 if (ATARIHW_PRESENT( EXTD_DMA ))
1075 addr |= (st_dma_ext_dmahi & 0xffff) << 16;
1076 else
1077 addr |= (dma_wd.dma_hi & 0xff) << 16;
1078 MFPDELAY();
1079 } while(addr != addr2);
1080
1081 if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1082 /* already read enough data, force an FDC interrupt to stop
1083 * the read operation
1084 */
1085 SET_IRQ_HANDLER( NULL );
1086 MultReadInProgress = 0;
1087 local_irq_restore(flags);
1088 DPRINT(("fd_readtrack_check(): done\n"));
1089 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1090 udelay(25);
1091
1092 /* No error until now -- the FDC would have interrupted
1093 * otherwise!
1094 */
1095 fd_rwsec_done1(0);
1096 }
1097 else {
1098 /* not yet finished, wait another tenth rotation */
1099 local_irq_restore(flags);
1100 DPRINT(("fd_readtrack_check(): not yet finished\n"));
1101 mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1102 }
1103}
1104
1105
1106static void fd_rwsec_done( int status )
1107{
1108 DPRINT(("fd_rwsec_done()\n"));
1109
1110 if (read_track) {
1111 del_timer(&readtrack_timer);
1112 if (!MultReadInProgress)
1113 return;
1114 MultReadInProgress = 0;
1115 }
1116 fd_rwsec_done1(status);
1117}
1118
1119static void fd_rwsec_done1(int status)
1120{
1121 unsigned int track;
1122
1123 stop_timeout();
1124
1125 /* Correct the track if stretch != 0 */
1126 if (SUDT->stretch) {
1127 track = FDC_READ( FDCREG_TRACK);
1128 MFPDELAY();
1129 FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1130 }
1131
1132 if (!UseTrackbuffer) {
1133 dma_wd.dma_mode_status = 0x90;
1134 MFPDELAY();
1135 if (!(dma_wd.dma_mode_status & 0x01)) {
1136 printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1137 goto err_end;
1138 }
1139 }
1140 MFPDELAY();
1141
1142 if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1143 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1144 goto err_end;
1145 }
1146 if ((status & FDCSTAT_RECNF) &&
1147 /* RECNF is no error after a multiple read when the FDC
1148 searched for a non-existent sector! */
1149 !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1150 if (Probing) {
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001151 if (SUDT > atari_disk_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (SUDT[-1].blocks > ReqBlock) {
1153 /* try another disk type */
1154 SUDT--;
1155 set_capacity(unit[SelectedDrive].disk,
1156 SUDT->blocks);
1157 } else
1158 Probing = 0;
1159 }
1160 else {
1161 if (SUD.flags & FTD_MSG)
1162 printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1163 SelectedDrive, SUDT->name );
1164 Probing=0;
1165 }
1166 } else {
1167/* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1168 if (SUD.autoprobe) {
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001169 SUDT = atari_disk_type + StartDiskType[DriveType];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 set_capacity(unit[SelectedDrive].disk,
1171 SUDT->blocks);
1172 Probing = 1;
1173 }
1174 }
1175 if (Probing) {
1176 if (ATARIHW_PRESENT(FDCSPEED)) {
1177 dma_wd.fdc_speed = SUDT->fdc_speed;
1178 MFPDELAY();
1179 }
1180 setup_req_params( SelectedDrive );
1181 BufferDrive = -1;
1182 do_fd_action( SelectedDrive );
1183 return;
1184 }
1185
1186 printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1187 SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1188 goto err_end;
1189 }
1190 if (status & FDCSTAT_CRC) {
1191 printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1192 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1193 goto err_end;
1194 }
1195 if (status & FDCSTAT_LOST) {
1196 printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1197 SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1198 goto err_end;
1199 }
1200
1201 Probing = 0;
1202
1203 if (ReqCmd == READ) {
1204 if (!read_track) {
1205 void *addr;
1206 addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1207 dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1208 if (!ATARIHW_PRESENT( EXTD_DMA ))
1209 copy_buffer (addr, ReqData);
1210 } else {
1211 dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1212 BufferDrive = SelectedDrive;
1213 BufferSide = ReqSide;
1214 BufferTrack = ReqTrack;
1215 copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1216 }
1217 }
1218
Tejun Heoa336ca62009-05-08 11:54:03 +09001219 if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /* read next sector */
1221 setup_req_params( SelectedDrive );
1222 do_fd_action( SelectedDrive );
1223 }
1224 else {
1225 /* all sectors finished */
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001226 fd_end_request_cur(BLK_STS_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 redo_fd_request();
1228 }
1229 return;
1230
1231 err_end:
1232 BufferDrive = -1;
1233 fd_error();
1234}
1235
1236
1237static void fd_writetrack( void )
1238{
1239 unsigned long paddr, flags;
1240 unsigned int track;
1241
1242 DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1243
1244 paddr = PhysTrackBuffer;
1245 dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1246
1247 fd_select_side( ReqSide );
1248
1249 /* Cheat for track if stretch != 0 */
1250 if (SUDT->stretch) {
1251 track = FDC_READ( FDCREG_TRACK);
1252 MFPDELAY();
1253 FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1254 }
1255 udelay(40);
1256
1257 /* Setup DMA */
1258 local_irq_save(flags);
1259 dma_wd.dma_lo = (unsigned char)paddr;
1260 MFPDELAY();
1261 paddr >>= 8;
1262 dma_wd.dma_md = (unsigned char)paddr;
1263 MFPDELAY();
1264 paddr >>= 8;
1265 if (ATARIHW_PRESENT( EXTD_DMA ))
1266 st_dma_ext_dmahi = (unsigned short)paddr;
1267 else
1268 dma_wd.dma_hi = (unsigned char)paddr;
1269 MFPDELAY();
1270 local_irq_restore(flags);
1271
1272 /* Clear FIFO and switch DMA to correct mode */
1273 dma_wd.dma_mode_status = 0x190;
1274 MFPDELAY();
1275 dma_wd.dma_mode_status = 0x90;
1276 MFPDELAY();
1277 dma_wd.dma_mode_status = 0x190;
1278 MFPDELAY();
1279
1280 /* How many sectors for DMA */
1281 dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1282 udelay(40);
1283
1284 /* Start operation */
1285 dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1286 udelay(40);
1287 SET_IRQ_HANDLER( fd_writetrack_done );
1288 dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1289
1290 MotorOn = 1;
1291 start_timeout();
1292 /* wait for interrupt */
1293}
1294
1295
1296static void fd_writetrack_done( int status )
1297{
1298 DPRINT(("fd_writetrack_done()\n"));
1299
1300 stop_timeout();
1301
1302 if (status & FDCSTAT_WPROT) {
1303 printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1304 goto err_end;
1305 }
1306 if (status & FDCSTAT_LOST) {
1307 printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1308 SelectedDrive, ReqSide, ReqTrack );
1309 goto err_end;
1310 }
1311
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +01001312 complete(&format_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return;
1314
1315 err_end:
1316 fd_error();
1317}
1318
Kees Cook24ed9602017-08-28 11:28:21 -07001319static void fd_times_out(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 atari_disable_irq( IRQ_MFP_FDC );
1322 if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1323 * before we came here... */
1324
1325 SET_IRQ_HANDLER( NULL );
1326 /* If the timeout occurred while the readtrack_check timer was
1327 * active, we need to cancel it, else bad things will happen */
1328 if (UseTrackbuffer)
1329 del_timer( &readtrack_timer );
1330 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1331 udelay( 25 );
1332
1333 printk(KERN_ERR "floppy timeout\n" );
1334 fd_error();
1335 end:
1336 atari_enable_irq( IRQ_MFP_FDC );
1337}
1338
1339
1340/* The (noop) seek operation here is needed to make the WP bit in the
1341 * FDC status register accessible for check_change. If the last disk
1342 * operation would have been a RDSEC, this bit would always read as 0
1343 * no matter what :-( To save time, the seek goes to the track we're
1344 * already on.
1345 */
1346
1347static void finish_fdc( void )
1348{
1349 if (!NeedSeek) {
1350 finish_fdc_done( 0 );
1351 }
1352 else {
1353 DPRINT(("finish_fdc: dummy seek started\n"));
1354 FDC_WRITE (FDCREG_DATA, SUD.track);
1355 SET_IRQ_HANDLER( finish_fdc_done );
1356 FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1357 MotorOn = 1;
1358 start_timeout();
1359 /* we must wait for the IRQ here, because the ST-DMA
1360 is released immediately afterwards and the interrupt
1361 may be delivered to the wrong driver. */
1362 }
1363}
1364
1365
1366static void finish_fdc_done( int dummy )
1367{
1368 unsigned long flags;
1369
1370 DPRINT(("finish_fdc_done entered\n"));
1371 stop_timeout();
1372 NeedSeek = 0;
1373
1374 if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1375 /* If the check for a disk change is done too early after this
1376 * last seek command, the WP bit still reads wrong :-((
1377 */
1378 mod_timer(&fd_timer, jiffies + 5);
1379 else
1380 start_check_change_timer();
1381 start_motor_off_timer();
1382
1383 local_irq_save(flags);
1384 stdma_release();
1385 fdc_busy = 0;
1386 wake_up( &fdc_wait );
1387 local_irq_restore(flags);
1388
1389 DPRINT(("finish_fdc() finished\n"));
1390}
1391
1392/* The detection of disk changes is a dark chapter in Atari history :-(
1393 * Because the "Drive ready" signal isn't present in the Atari
1394 * hardware, one has to rely on the "Write Protect". This works fine,
1395 * as long as no write protected disks are used. TOS solves this
1396 * problem by introducing tri-state logic ("maybe changed") and
1397 * looking at the serial number in block 0. This isn't possible for
1398 * Linux, since the floppy driver can't make assumptions about the
1399 * filesystem used on the disk and thus the contents of block 0. I've
1400 * chosen the method to always say "The disk was changed" if it is
1401 * unsure whether it was. This implies that every open or mount
1402 * invalidates the disk buffers if you work with write protected
1403 * disks. But at least this is better than working with incorrect data
1404 * due to unrecognised disk changes.
1405 */
1406
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001407static unsigned int floppy_check_events(struct gendisk *disk,
1408 unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 struct atari_floppy_struct *p = disk->private_data;
1411 unsigned int drive = p - unit;
1412 if (test_bit (drive, &fake_change)) {
1413 /* simulated change (e.g. after formatting) */
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001414 return DISK_EVENT_MEDIA_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416 if (test_bit (drive, &changed_floppies)) {
1417 /* surely changed (the WP signal changed at least once) */
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001418 return DISK_EVENT_MEDIA_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420 if (UD.wpstat) {
1421 /* WP is on -> could be changed: to be sure, buffers should be
1422 * invalidated...
1423 */
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001424 return DISK_EVENT_MEDIA_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426
1427 return 0;
1428}
1429
1430static int floppy_revalidate(struct gendisk *disk)
1431{
1432 struct atari_floppy_struct *p = disk->private_data;
1433 unsigned int drive = p - unit;
1434
1435 if (test_bit(drive, &changed_floppies) ||
1436 test_bit(drive, &fake_change) ||
1437 p->disktype == 0) {
1438 if (UD.flags & FTD_MSG)
1439 printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1440 BufferDrive = -1;
1441 clear_bit(drive, &fake_change);
1442 clear_bit(drive, &changed_floppies);
1443 /* MSch: clearing geometry makes sense only for autoprobe
1444 formats, for 'permanent user-defined' parameter:
1445 restore default_params[] here if flagged valid! */
1446 if (default_params[drive].blocks == 0)
Al Viro2c7af512006-01-12 01:06:29 -08001447 UDT = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 else
1449 UDT = &default_params[drive];
1450 }
1451 return 0;
1452}
1453
1454
1455/* This sets up the global variables describing the current request. */
1456
1457static void setup_req_params( int drive )
1458{
1459 int block = ReqBlock + ReqCnt;
1460
1461 ReqTrack = block / UDT->spt;
1462 ReqSector = block - ReqTrack * UDT->spt + 1;
1463 ReqSide = ReqTrack & 1;
1464 ReqTrack >>= 1;
1465 ReqData = ReqBuffer + 512 * ReqCnt;
1466
1467 if (UseTrackbuffer)
Christoph Hellwigc8e90782017-04-20 16:03:13 +02001468 read_track = (ReqCmd == READ && fd_request->error_count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 else
1470 read_track = 0;
1471
1472 DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1473 ReqTrack, ReqSector, (unsigned long)ReqData ));
1474}
1475
Vivek Goyal639e2f22010-09-24 20:35:45 +02001476/*
1477 * Round-robin between our available drives, doing one request from each
1478 */
1479static struct request *set_next_request(void)
1480{
1481 struct request_queue *q;
1482 int old_pos = fdc_queue;
Vivek Goyal3e9bb2a2010-11-15 19:32:43 +01001483 struct request *rq = NULL;
Vivek Goyal639e2f22010-09-24 20:35:45 +02001484
1485 do {
1486 q = unit[fdc_queue].disk->queue;
1487 if (++fdc_queue == FD_MAX_UNITS)
1488 fdc_queue = 0;
1489 if (q) {
1490 rq = blk_fetch_request(q);
Christoph Hellwigc8e90782017-04-20 16:03:13 +02001491 if (rq) {
1492 rq->error_count = 0;
Vivek Goyal639e2f22010-09-24 20:35:45 +02001493 break;
Christoph Hellwigc8e90782017-04-20 16:03:13 +02001494 }
Vivek Goyal639e2f22010-09-24 20:35:45 +02001495 }
1496 } while (fdc_queue != old_pos);
1497
1498 return rq;
1499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502static void redo_fd_request(void)
1503{
1504 int drive, type;
1505 struct atari_floppy_struct *floppy;
1506
Tejun Heoa336ca62009-05-08 11:54:03 +09001507 DPRINT(("redo_fd_request: fd_request=%p dev=%s fd_request->sector=%ld\n",
1508 fd_request, fd_request ? fd_request->rq_disk->disk_name : "",
1509 fd_request ? blk_rq_pos(fd_request) : 0 ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 IsFormatting = 0;
1512
1513repeat:
Tejun Heoa336ca62009-05-08 11:54:03 +09001514 if (!fd_request) {
Vivek Goyal639e2f22010-09-24 20:35:45 +02001515 fd_request = set_next_request();
Tejun Heoa336ca62009-05-08 11:54:03 +09001516 if (!fd_request)
1517 goto the_end;
Tejun Heoa336ca62009-05-08 11:54:03 +09001518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Tejun Heoa336ca62009-05-08 11:54:03 +09001520 floppy = fd_request->rq_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 drive = floppy - unit;
1522 type = floppy->type;
1523
1524 if (!UD.connected) {
1525 /* drive not connected */
1526 printk(KERN_ERR "Unknown Device: fd%d\n", drive );
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001527 fd_end_request_cur(BLK_STS_IOERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 goto repeat;
1529 }
1530
1531 if (type == 0) {
1532 if (!UDT) {
1533 Probing = 1;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001534 UDT = atari_disk_type + StartDiskType[DriveType];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 set_capacity(floppy->disk, UDT->blocks);
1536 UD.autoprobe = 1;
1537 }
1538 }
1539 else {
1540 /* user supplied disk type */
1541 if (--type >= NUM_DISK_MINORS) {
1542 printk(KERN_WARNING "fd%d: invalid disk format", drive );
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001543 fd_end_request_cur(BLK_STS_IOERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 goto repeat;
1545 }
1546 if (minor2disktype[type].drive_types > DriveType) {
1547 printk(KERN_WARNING "fd%d: unsupported disk format", drive );
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001548 fd_end_request_cur(BLK_STS_IOERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 goto repeat;
1550 }
1551 type = minor2disktype[type].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001552 UDT = &atari_disk_type[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 set_capacity(floppy->disk, UDT->blocks);
1554 UD.autoprobe = 0;
1555 }
1556
Tejun Heoa336ca62009-05-08 11:54:03 +09001557 if (blk_rq_pos(fd_request) + 1 > UDT->blocks) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001558 fd_end_request_cur(BLK_STS_IOERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 goto repeat;
1560 }
1561
1562 /* stop deselect timer */
1563 del_timer( &motor_off_timer );
1564
1565 ReqCnt = 0;
Tejun Heoa336ca62009-05-08 11:54:03 +09001566 ReqCmd = rq_data_dir(fd_request);
1567 ReqBlock = blk_rq_pos(fd_request);
Jens Axboeb4f42e22014-04-10 09:46:28 -06001568 ReqBuffer = bio_data(fd_request->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 setup_req_params( drive );
1570 do_fd_action( drive );
1571
1572 return;
1573
1574 the_end:
1575 finish_fdc();
1576}
1577
1578
Jens Axboe165125e2007-07-24 09:28:11 +02001579void do_fd_request(struct request_queue * q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 DPRINT(("do_fd_request for pid %d\n",current->pid));
Arnd Bergmann7b8a3d22014-02-26 12:01:41 +01001582 wait_event(fdc_wait, cmpxchg(&fdc_busy, 0, 1) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 stdma_lock(floppy_irq, NULL);
1584
1585 atari_disable_irq( IRQ_MFP_FDC );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 redo_fd_request();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 atari_enable_irq( IRQ_MFP_FDC );
1588}
1589
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001590static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 unsigned int cmd, unsigned long param)
1592{
Al Viro60ad2342008-03-02 09:23:54 -05001593 struct gendisk *disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 struct atari_floppy_struct *floppy = disk->private_data;
1595 int drive = floppy - unit;
1596 int type = floppy->type;
1597 struct atari_format_descr fmt_desc;
1598 struct atari_disk_type *dtp;
1599 struct floppy_struct getprm;
1600 int settype;
1601 struct floppy_struct setprm;
Al Viro2c7af512006-01-12 01:06:29 -08001602 void __user *argp = (void __user *)param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 switch (cmd) {
1605 case FDGETPRM:
1606 if (type) {
1607 if (--type >= NUM_DISK_MINORS)
1608 return -ENODEV;
1609 if (minor2disktype[type].drive_types > DriveType)
1610 return -ENODEV;
1611 type = minor2disktype[type].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001612 dtp = &atari_disk_type[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (UD.flags & FTD_MSG)
1614 printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1615 drive, dtp, dtp->name);
1616 }
1617 else {
1618 if (!UDT)
1619 return -ENXIO;
1620 else
1621 dtp = UDT;
1622 }
1623 memset((void *)&getprm, 0, sizeof(getprm));
1624 getprm.size = dtp->blocks;
1625 getprm.sect = dtp->spt;
1626 getprm.head = 2;
1627 getprm.track = dtp->blocks/dtp->spt/2;
1628 getprm.stretch = dtp->stretch;
Al Viro2c7af512006-01-12 01:06:29 -08001629 if (copy_to_user(argp, &getprm, sizeof(getprm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return -EFAULT;
1631 return 0;
1632 }
1633 switch (cmd) {
1634 case FDSETPRM:
1635 case FDDEFPRM:
1636 /*
1637 * MSch 7/96: simple 'set geometry' case: just set the
1638 * 'default' device params (minor == 0).
1639 * Currently, the drive geometry is cleared after each
1640 * disk change and subsequent revalidate()! simple
1641 * implementation of FDDEFPRM: save geometry from a
1642 * FDDEFPRM call and restore it in floppy_revalidate() !
1643 */
1644
1645 /* get the parameters from user space */
1646 if (floppy->ref != 1 && floppy->ref != -1)
1647 return -EBUSY;
Al Viro2c7af512006-01-12 01:06:29 -08001648 if (copy_from_user(&setprm, argp, sizeof(setprm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return -EFAULT;
1650 /*
1651 * first of all: check for floppy change and revalidate,
1652 * or the next access will revalidate - and clear UDT :-(
1653 */
1654
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001655 if (floppy_check_events(disk, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 floppy_revalidate(disk);
1657
1658 if (UD.flags & FTD_MSG)
1659 printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1660 drive, setprm.size, setprm.sect, setprm.stretch);
1661
1662 /* what if type > 0 here? Overwrite specified entry ? */
1663 if (type) {
1664 /* refuse to re-set a predefined type for now */
1665 redo_fd_request();
1666 return -EINVAL;
1667 }
1668
1669 /*
1670 * type == 0: first look for a matching entry in the type list,
1671 * and set the UD.disktype field to use the perdefined entry.
1672 * TODO: add user-defined format to head of autoprobe list ?
1673 * Useful to include the user-type for future autodetection!
1674 */
1675
1676 for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1677 int setidx = 0;
1678 if (minor2disktype[settype].drive_types > DriveType) {
1679 /* skip this one, invalid for drive ... */
1680 continue;
1681 }
1682 setidx = minor2disktype[settype].index;
Geert Uytterhoeven5ceadd22008-02-06 01:38:09 -08001683 dtp = &atari_disk_type[setidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 /* found matching entry ?? */
1686 if ( dtp->blocks == setprm.size
1687 && dtp->spt == setprm.sect
1688 && dtp->stretch == setprm.stretch ) {
1689 if (UD.flags & FTD_MSG)
1690 printk (KERN_INFO "floppy%d: setting %s %p!\n",
1691 drive, dtp->name, dtp);
1692 UDT = dtp;
1693 set_capacity(floppy->disk, UDT->blocks);
1694
1695 if (cmd == FDDEFPRM) {
1696 /* save settings as permanent default type */
1697 default_params[drive].name = dtp->name;
1698 default_params[drive].spt = dtp->spt;
1699 default_params[drive].blocks = dtp->blocks;
1700 default_params[drive].fdc_speed = dtp->fdc_speed;
1701 default_params[drive].stretch = dtp->stretch;
1702 }
1703
1704 return 0;
1705 }
1706
1707 }
1708
1709 /* no matching disk type found above - setting user_params */
1710
1711 if (cmd == FDDEFPRM) {
1712 /* set permanent type */
1713 dtp = &default_params[drive];
1714 } else
1715 /* set user type (reset by disk change!) */
1716 dtp = &user_params[drive];
1717
1718 dtp->name = "user format";
1719 dtp->blocks = setprm.size;
1720 dtp->spt = setprm.sect;
1721 if (setprm.sect > 14)
1722 dtp->fdc_speed = 3;
1723 else
1724 dtp->fdc_speed = 0;
1725 dtp->stretch = setprm.stretch;
1726
1727 if (UD.flags & FTD_MSG)
1728 printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1729 drive, dtp->blocks, dtp->spt, dtp->stretch);
1730
1731 /* sanity check */
Julia Lawall8f474282009-07-13 22:43:41 +02001732 if (setprm.track != dtp->blocks/dtp->spt/2 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 setprm.head != 2) {
1734 redo_fd_request();
1735 return -EINVAL;
1736 }
1737
1738 UDT = dtp;
1739 set_capacity(floppy->disk, UDT->blocks);
1740
1741 return 0;
1742 case FDMSGON:
1743 UD.flags |= FTD_MSG;
1744 return 0;
1745 case FDMSGOFF:
1746 UD.flags &= ~FTD_MSG;
1747 return 0;
1748 case FDSETEMSGTRESH:
1749 return -EINVAL;
1750 case FDFMTBEG:
1751 return 0;
1752 case FDFMTTRK:
1753 if (floppy->ref != 1 && floppy->ref != -1)
1754 return -EBUSY;
Al Viro2c7af512006-01-12 01:06:29 -08001755 if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return -EFAULT;
1757 return do_format(drive, type, &fmt_desc);
1758 case FDCLRPRM:
1759 UDT = NULL;
1760 /* MSch: invalidate default_params */
1761 default_params[drive].blocks = 0;
1762 set_capacity(floppy->disk, MAX_DISK_SIZE * 2);
1763 case FDFMTEND:
1764 case FDFLUSH:
1765 /* invalidate the buffer track to force a reread */
1766 BufferDrive = -1;
1767 set_bit(drive, &fake_change);
Al Viro60ad2342008-03-02 09:23:54 -05001768 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return 0;
1770 default:
1771 return -EINVAL;
1772 }
1773}
1774
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001775static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1776 unsigned int cmd, unsigned long arg)
1777{
1778 int ret;
1779
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001780 mutex_lock(&ataflop_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001781 ret = fd_locked_ioctl(bdev, mode, cmd, arg);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001782 mutex_unlock(&ataflop_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001783
1784 return ret;
1785}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787/* Initialize the 'unit' variable for drive 'drive' */
1788
1789static void __init fd_probe( int drive )
1790{
1791 UD.connected = 0;
1792 UDT = NULL;
1793
1794 if (!fd_test_drive_present( drive ))
1795 return;
1796
1797 UD.connected = 1;
1798 UD.track = 0;
1799 switch( UserSteprate[drive] ) {
1800 case 2:
1801 UD.steprate = FDCSTEP_2;
1802 break;
1803 case 3:
1804 UD.steprate = FDCSTEP_3;
1805 break;
1806 case 6:
1807 UD.steprate = FDCSTEP_6;
1808 break;
1809 case 12:
1810 UD.steprate = FDCSTEP_12;
1811 break;
1812 default: /* should be -1 for "not set by user" */
1813 if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1814 UD.steprate = FDCSTEP_3;
1815 else
1816 UD.steprate = FDCSTEP_6;
1817 break;
1818 }
1819 MotorOn = 1; /* from probe restore operation! */
1820}
1821
1822
1823/* This function tests the physical presence of a floppy drive (not
1824 * whether a disk is inserted). This is done by issuing a restore
1825 * command, waiting max. 2 seconds (that should be enough to move the
1826 * head across the whole disk) and looking at the state of the "TR00"
1827 * signal. This should now be raised if there is a drive connected
1828 * (and there is no hardware failure :-) Otherwise, the drive is
1829 * declared absent.
1830 */
1831
1832static int __init fd_test_drive_present( int drive )
1833{
1834 unsigned long timeout;
1835 unsigned char status;
1836 int ok;
1837
1838 if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1839 fd_select_drive( drive );
1840
1841 /* disable interrupt temporarily */
1842 atari_turnoff_irq( IRQ_MFP_FDC );
1843 FDC_WRITE (FDCREG_TRACK, 0xff00);
1844 FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1845
1846 timeout = jiffies + 2*HZ+HZ/2;
1847 while (time_before(jiffies, timeout))
Geert Uytterhoeven3d92e8f2009-02-22 09:38:47 +01001848 if (!(st_mfp.par_dt_reg & 0x20))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 break;
1850
1851 status = FDC_READ( FDCREG_STATUS );
1852 ok = (status & FDCSTAT_TR00) != 0;
1853
1854 /* force interrupt to abort restore operation (FDC would try
1855 * about 50 seconds!) */
1856 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1857 udelay(500);
1858 status = FDC_READ( FDCREG_STATUS );
1859 udelay(20);
1860
1861 if (ok) {
1862 /* dummy seek command to make WP bit accessible */
1863 FDC_WRITE( FDCREG_DATA, 0 );
1864 FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
Geert Uytterhoeven3d92e8f2009-02-22 09:38:47 +01001865 while( st_mfp.par_dt_reg & 0x20 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 ;
1867 status = FDC_READ( FDCREG_STATUS );
1868 }
1869
1870 atari_turnon_irq( IRQ_MFP_FDC );
1871 return( ok );
1872}
1873
1874
1875/* Look how many and which kind of drives are connected. If there are
1876 * floppies, additionally start the disk-change and motor-off timers.
1877 */
1878
1879static void __init config_types( void )
1880{
1881 int drive, cnt = 0;
1882
1883 /* for probing drives, set the FDC speed to 8 MHz */
1884 if (ATARIHW_PRESENT(FDCSPEED))
1885 dma_wd.fdc_speed = 0;
1886
1887 printk(KERN_INFO "Probing floppy drive(s):\n");
1888 for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1889 fd_probe( drive );
1890 if (UD.connected) {
1891 printk(KERN_INFO "fd%d\n", drive);
1892 ++cnt;
1893 }
1894 }
1895
1896 if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1897 /* If FDC is still busy from probing, give it another FORCI
1898 * command to abort the operation. If this isn't done, the FDC
1899 * will interrupt later and its IRQ line stays low, because
1900 * the status register isn't read. And this will block any
1901 * interrupts on this IRQ line :-(
1902 */
1903 FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1904 udelay(500);
1905 FDC_READ( FDCREG_STATUS );
1906 udelay(20);
1907 }
1908
1909 if (cnt > 0) {
1910 start_motor_off_timer();
1911 if (cnt == 1) fd_select_drive( 0 );
1912 start_check_change_timer();
1913 }
1914}
1915
1916/*
1917 * floppy_open check for aliasing (/dev/fd0 can be the same as
1918 * /dev/PS0 etc), and disallows simultaneous access to the same
1919 * drive with different device numbers.
1920 */
1921
Al Viro60ad2342008-03-02 09:23:54 -05001922static int floppy_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Al Viro60ad2342008-03-02 09:23:54 -05001924 struct atari_floppy_struct *p = bdev->bd_disk->private_data;
1925 int type = MINOR(bdev->bd_dev) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 DPRINT(("fd_open: type=%d\n",type));
1928 if (p->ref && p->type != type)
1929 return -EBUSY;
1930
Al Viro60ad2342008-03-02 09:23:54 -05001931 if (p->ref == -1 || (p->ref && mode & FMODE_EXCL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return -EBUSY;
1933
Al Viro60ad2342008-03-02 09:23:54 -05001934 if (mode & FMODE_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 p->ref = -1;
1936 else
1937 p->ref++;
1938
1939 p->type = type;
1940
Al Viro60ad2342008-03-02 09:23:54 -05001941 if (mode & FMODE_NDELAY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return 0;
1943
Al Viro60ad2342008-03-02 09:23:54 -05001944 if (mode & (FMODE_READ|FMODE_WRITE)) {
1945 check_disk_change(bdev);
1946 if (mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (p->wpstat) {
1948 if (p->ref < 0)
1949 p->ref = 0;
1950 else
1951 p->ref--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return -EROFS;
1953 }
1954 }
1955 }
1956 return 0;
1957}
1958
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001959static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
1960{
1961 int ret;
1962
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001963 mutex_lock(&ataflop_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001964 ret = floppy_open(bdev, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001965 mutex_unlock(&ataflop_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001966
1967 return ret;
1968}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Al Virodb2a1442013-05-05 21:52:57 -04001970static void floppy_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Al Viro60ad2342008-03-02 09:23:54 -05001972 struct atari_floppy_struct *p = disk->private_data;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001973 mutex_lock(&ataflop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (p->ref < 0)
1975 p->ref = 0;
1976 else if (!p->ref--) {
1977 printk(KERN_ERR "floppy_release with fd_ref == 0");
1978 p->ref = 0;
1979 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001980 mutex_unlock(&ataflop_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001983static const struct block_device_operations floppy_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 .owner = THIS_MODULE,
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001985 .open = floppy_unlocked_open,
Al Viro60ad2342008-03-02 09:23:54 -05001986 .release = floppy_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001987 .ioctl = fd_ioctl,
Tejun Heo1a8a74f2011-03-09 19:54:27 +01001988 .check_events = floppy_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 .revalidate_disk= floppy_revalidate,
1990};
1991
1992static struct kobject *floppy_find(dev_t dev, int *part, void *data)
1993{
1994 int drive = *part & 3;
1995 int type = *part >> 2;
1996 if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1997 return NULL;
1998 *part = 0;
Jan Kara3079c222018-02-26 13:01:38 +01001999 return get_disk_and_module(unit[drive].disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
2002static int __init atari_floppy_init (void)
2003{
2004 int i;
2005
2006 if (!MACH_IS_ATARI)
2007 /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
Geert Uytterhoevene945b562008-07-17 21:16:10 +02002008 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (register_blkdev(FLOPPY_MAJOR,"fd"))
2011 return -EBUSY;
2012
2013 for (i = 0; i < FD_MAX_UNITS; i++) {
2014 unit[i].disk = alloc_disk(1);
2015 if (!unit[i].disk)
2016 goto Enomem;
2017 }
2018
2019 if (UseTrackbuffer < 0)
2020 /* not set by user -> use default: for now, we turn
2021 track buffering off for all Medusas, though it
2022 could be used with ones that have a counter
2023 card. But the test is too hard :-( */
2024 UseTrackbuffer = !MACH_IS_MEDUSA;
2025
2026 /* initialize variables */
2027 SelectedDrive = -1;
2028 BufferDrive = -1;
2029
2030 DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
2031 if (!DMABuffer) {
2032 printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
2033 goto Enomem;
2034 }
2035 TrackBuffer = DMABuffer + 512;
Michael Schmitza4de73f2014-03-31 21:06:07 +13002036 PhysDMABuffer = atari_stram_to_phys(DMABuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 PhysTrackBuffer = virt_to_phys(TrackBuffer);
2038 BufferDrive = BufferSide = BufferTrack = -1;
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 for (i = 0; i < FD_MAX_UNITS; i++) {
2041 unit[i].track = -1;
2042 unit[i].flags = 0;
2043 unit[i].disk->major = FLOPPY_MAJOR;
2044 unit[i].disk->first_minor = i;
2045 sprintf(unit[i].disk->disk_name, "fd%d", i);
2046 unit[i].disk->fops = &floppy_fops;
2047 unit[i].disk->private_data = &unit[i];
Vivek Goyal639e2f22010-09-24 20:35:45 +02002048 unit[i].disk->queue = blk_init_queue(do_fd_request,
2049 &ataflop_lock);
2050 if (!unit[i].disk->queue)
2051 goto Enomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 set_capacity(unit[i].disk, MAX_DISK_SIZE * 2);
2053 add_disk(unit[i].disk);
2054 }
2055
2056 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
2057 floppy_find, NULL, NULL);
2058
2059 printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
2060 DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
2061 UseTrackbuffer ? "" : "no ");
2062 config_types();
2063
2064 return 0;
2065Enomem:
Vivek Goyal639e2f22010-09-24 20:35:45 +02002066 while (i--) {
2067 struct request_queue *q = unit[i].disk->queue;
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 put_disk(unit[i].disk);
Vivek Goyal639e2f22010-09-24 20:35:45 +02002070 if (q)
2071 blk_cleanup_queue(q);
2072 }
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 unregister_blkdev(FLOPPY_MAJOR, "fd");
2075 return -ENOMEM;
2076}
2077
Al Viro63907432006-01-12 01:06:36 -08002078#ifndef MODULE
2079static int __init atari_floppy_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Al Viro63907432006-01-12 01:06:36 -08002081 int ints[3 + FD_MAX_UNITS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 int i;
Al Viro63907432006-01-12 01:06:36 -08002083
2084 if (!MACH_IS_ATARI)
2085 return 0;
2086
2087 str = get_options(str, 3 + FD_MAX_UNITS, ints);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 if (ints[0] < 1) {
2090 printk(KERN_ERR "ataflop_setup: no arguments!\n" );
Al Viro63907432006-01-12 01:06:36 -08002091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093 else if (ints[0] > 2+FD_MAX_UNITS) {
2094 printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2095 }
2096
2097 if (ints[1] < 0 || ints[1] > 2)
2098 printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2099 else
2100 DriveType = ints[1];
2101
2102 if (ints[0] >= 2)
2103 UseTrackbuffer = (ints[2] > 0);
2104
2105 for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2106 if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2107 printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2108 else
2109 UserSteprate[i-3] = ints[i];
2110 }
Al Viro63907432006-01-12 01:06:36 -08002111 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Al Viro63907432006-01-12 01:06:36 -08002114__setup("floppy=", atari_floppy_setup);
2115#endif
2116
2117static void __exit atari_floppy_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
2119 int i;
2120 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
2121 for (i = 0; i < FD_MAX_UNITS; i++) {
Vivek Goyal639e2f22010-09-24 20:35:45 +02002122 struct request_queue *q = unit[i].disk->queue;
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 del_gendisk(unit[i].disk);
2125 put_disk(unit[i].disk);
Vivek Goyal639e2f22010-09-24 20:35:45 +02002126 blk_cleanup_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
2128 unregister_blkdev(FLOPPY_MAJOR, "fd");
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 del_timer_sync(&fd_timer);
2131 atari_stram_free( DMABuffer );
2132}
2133
2134module_init(atari_floppy_init)
2135module_exit(atari_floppy_exit)
2136
2137MODULE_LICENSE("GPL");