-
Notifications
You must be signed in to change notification settings - Fork 0
/
ataflop.c
2011 lines (1697 loc) · 51.2 KB
/
ataflop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* drivers/block/ataflop.c
*
* Copyright (C) 1993 Greg Harp
* Atari Support by Bjoern Brauel, Roman Hodek
*
* Big cleanup Sep 11..14 1994 Roman Hodek:
* - Driver now works interrupt driven
* - Support for two drives; should work, but I cannot test that :-(
* - Reading is done in whole tracks and buffered to speed up things
* - Disk change detection and drive deselecting after motor-off
* similar to TOS
* - Autodetection of disk format (DD/HD); untested yet, because I
* don't have an HD drive :-(
*
* Fixes Nov 13 1994 Martin Schaller:
* - Autodetection works now
* - Support for 5 1/4'' disks
* - Removed drive type (unknown on atari)
* - Do seeks with 8 Mhz
*
* Changes by Andreas Schwab:
* - After errors in multiple read mode try again reading single sectors
* (Feb 1995):
* - Clean up error handling
* - Set blk_size for proper size checking
* - Initialize track register when testing presence of floppy
* - Implement some ioctl's
*
* Changes by Torsten Lang:
* - When probing the floppies we should add the FDCCMDADD_H flag since
* the FDC will otherwise wait forever when no disk is inserted...
*
* ++ Freddi Aschwanden (fa) 20.9.95 fixes for medusa:
* - MFPDELAY() after each FDC access -> atari
* - more/other disk formats
* - DMA to the block buffer directly if we have a 32bit DMA
* - for medusa, the step rate is always 3ms
* - on medusa, use only cache_push()
* Roman:
* - Make disk format numbering independent from minors
* - Let user set max. supported drive type (speeds up format
* detection, saves buffer space)
*
* Roman 10/15/95:
* - implement some more ioctls
* - disk formatting
*
* Andreas 95/12/12:
* - increase gap size at start of track for HD/ED disks
*
* Michael (MSch) 11/07/96:
* - implemented FDSETPRM and FDDEFPRM ioctl
*
* Andreas (97/03/19):
* - implemented missing BLK* ioctls
*
* Things left to do:
* - Formatting
* - Maybe a better strategy for disk change detection (does anyone
* know one?)
*/
#include <linux/module.h>
#include <linux/fd.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <asm/atafd.h>
#include <asm/atafdreg.h>
#include <asm/atariints.h>
#include <asm/atari_stdma.h>
#include <asm/atari_stram.h>
#define FD_MAX_UNITS 2
#undef DEBUG
static struct request_queue *floppy_queue;
#define QUEUE (floppy_queue)
#define CURRENT elv_next_request(floppy_queue)
/* Disk types: DD, HD, ED */
static struct atari_disk_type {
const char *name;
unsigned spt; /* sectors per track */
unsigned blocks; /* total number of blocks */
unsigned fdc_speed; /* fdc_speed setting */
unsigned stretch; /* track doubling ? */
} disk_type[] = {
{ "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
{ "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
{ "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
{ "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
/* formats above are probed for type DD */
#define MAX_TYPE_DD 3
{ "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
{ "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
{ "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
/* formats above are probed for types DD and HD */
#define MAX_TYPE_HD 6
{ "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
{ "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
/* formats above are probed for types DD, HD and ED */
#define MAX_TYPE_ED 8
/* types below are never autoprobed */
{ "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
{ "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
{ "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
{ "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
{ "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
{ "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
{ "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
{ "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
{ "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
{ "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
{ "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
{ "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
{ "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
{ "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
{ "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
{ "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
{ "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
{ "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
{ "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
};
static int StartDiskType[] = {
MAX_TYPE_DD,
MAX_TYPE_HD,
MAX_TYPE_ED
};
#define TYPE_DD 0
#define TYPE_HD 1
#define TYPE_ED 2
static int DriveType = TYPE_HD;
static DEFINE_SPINLOCK(ataflop_lock);
/* Array for translating minors into disk formats */
static struct {
int index;
unsigned drive_types;
} minor2disktype[] = {
{ 0, TYPE_DD }, /* 1: d360 */
{ 4, TYPE_HD }, /* 2: h1200 */
{ 1, TYPE_DD }, /* 3: D360 */
{ 2, TYPE_DD }, /* 4: D720 */
{ 1, TYPE_DD }, /* 5: h360 = D360 */
{ 2, TYPE_DD }, /* 6: h720 = D720 */
{ 5, TYPE_HD }, /* 7: H1440 */
{ 7, TYPE_ED }, /* 8: E2880 */
/* some PC formats :-) */
{ 8, TYPE_ED }, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
{ 5, TYPE_HD }, /* 10: h1440 = H1440 */
{ 9, TYPE_HD }, /* 11: H1680 */
{ 10, TYPE_DD }, /* 12: h410 */
{ 3, TYPE_DD }, /* 13: H820 <- == D820, 82x10 */
{ 11, TYPE_HD }, /* 14: h1476 */
{ 12, TYPE_HD }, /* 15: H1722 */
{ 13, TYPE_DD }, /* 16: h420 */
{ 14, TYPE_DD }, /* 17: H830 */
{ 15, TYPE_HD }, /* 18: h1494 */
{ 16, TYPE_HD }, /* 19: H1743 */
{ 17, TYPE_DD }, /* 20: h880 */
{ 18, TYPE_DD }, /* 21: D1040 */
{ 19, TYPE_DD }, /* 22: D1120 */
{ 20, TYPE_HD }, /* 23: h1600 */
{ 21, TYPE_HD }, /* 24: H1760 */
{ 22, TYPE_HD }, /* 25: H1920 */
{ 23, TYPE_ED }, /* 26: E3200 */
{ 24, TYPE_ED }, /* 27: E3520 */
{ 25, TYPE_ED }, /* 28: E3840 */
{ 26, TYPE_HD }, /* 29: H1840 */
{ 27, TYPE_DD }, /* 30: D800 */
{ 6, TYPE_HD }, /* 31: H1640 <- was H1600 == h1600 for PC */
};
#define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
/*
* Maximum disk size (in kilobytes). This default is used whenever the
* current disk size is unknown.
*/
#define MAX_DISK_SIZE 3280
/*
* MSch: User-provided type information. 'drive' points to
* the respective entry of this array. Set by FDSETPRM ioctls.
*/
static struct atari_disk_type user_params[FD_MAX_UNITS];
/*
* User-provided permanent type information. 'drive' points to
* the respective entry of this array. Set by FDDEFPRM ioctls,
* restored upon disk change by floppy_revalidate() if valid (as seen by
* default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
*/
static struct atari_disk_type default_params[FD_MAX_UNITS];
/* current info on each unit */
static struct atari_floppy_struct {
int connected; /* !=0 : drive is connected */
int autoprobe; /* !=0 : do autoprobe */
struct atari_disk_type *disktype; /* current type of disk */
int track; /* current head position or -1 if
unknown */
unsigned int steprate; /* steprate setting */
unsigned int wpstat; /* current state of WP signal (for
disk change detection) */
int flags; /* flags */
struct gendisk *disk;
int ref;
int type;
} unit[FD_MAX_UNITS];
#define UD unit[drive]
#define UDT unit[drive].disktype
#define SUD unit[SelectedDrive]
#define SUDT unit[SelectedDrive].disktype
#define FDC_READ(reg) ({ \
/* unsigned long __flags; */ \
unsigned short __val; \
/* local_irq_save(__flags); */ \
dma_wd.dma_mode_status = 0x80 | (reg); \
udelay(25); \
__val = dma_wd.fdc_acces_seccount; \
MFPDELAY(); \
/* local_irq_restore(__flags); */ \
__val & 0xff; \
})
#define FDC_WRITE(reg,val) \
do { \
/* unsigned long __flags; */ \
/* local_irq_save(__flags); */ \
dma_wd.dma_mode_status = 0x80 | (reg); \
udelay(25); \
dma_wd.fdc_acces_seccount = (val); \
MFPDELAY(); \
/* local_irq_restore(__flags); */ \
} while(0)
/* Buffering variables:
* First, there is a DMA buffer in ST-RAM that is used for floppy DMA
* operations. Second, a track buffer is used to cache a whole track
* of the disk to save read operations. These are two separate buffers
* because that allows write operations without clearing the track buffer.
*/
static int MaxSectors[] = {
11, 22, 44
};
static int BufferSize[] = {
15*512, 30*512, 60*512
};
#define BUFFER_SIZE (BufferSize[DriveType])
unsigned char *DMABuffer; /* buffer for writes */
static unsigned long PhysDMABuffer; /* physical address */
static int UseTrackbuffer = -1; /* Do track buffering? */
module_param(UseTrackbuffer, int, 0);
unsigned char *TrackBuffer; /* buffer for reads */
static unsigned long PhysTrackBuffer; /* physical address */
static int BufferDrive, BufferSide, BufferTrack;
static int read_track; /* non-zero if we are reading whole tracks */
#define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
#define IS_BUFFERED(drive,side,track) \
(BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
/*
* These are global variables, as that's the easiest way to give
* information to interrupts. They are the data used for the current
* request.
*/
static int SelectedDrive = 0;
static int ReqCmd, ReqBlock;
static int ReqSide, ReqTrack, ReqSector, ReqCnt;
static int HeadSettleFlag = 0;
static unsigned char *ReqData, *ReqBuffer;
static int MotorOn = 0, MotorOffTrys;
static int IsFormatting = 0, FormatError;
static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
module_param_array(UserSteprate, int, NULL, 0);
/* Synchronization of FDC access. */
static volatile int fdc_busy = 0;
static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
static DECLARE_WAIT_QUEUE_HEAD(format_wait);
static unsigned long changed_floppies = 0xff, fake_change = 0;
#define CHECK_CHANGE_DELAY HZ/2
#define FD_MOTOR_OFF_DELAY (3*HZ)
#define FD_MOTOR_OFF_MAXTRY (10*20)
#define FLOPPY_TIMEOUT (6*HZ)
#define RECALIBRATE_ERRORS 4 /* After this many errors the drive
* will be recalibrated. */
#define MAX_ERRORS 8 /* After this many errors the driver
* will give up. */
/*
* The driver is trying to determine the correct media format
* while Probing is set. fd_rwsec_done() clears it after a
* successful access.
*/
static int Probing = 0;
/* This flag is set when a dummy seek is necessary to make the WP
* status bit accessible.
*/
static int NeedSeek = 0;
#ifdef DEBUG
#define DPRINT(a) printk a
#else
#define DPRINT(a)
#endif
/***************************** Prototypes *****************************/
static void fd_select_side( int side );
static void fd_select_drive( int drive );
static void fd_deselect( void );
static void fd_motor_off_timer( unsigned long dummy );
static void check_change( unsigned long dummy );
static irqreturn_t floppy_irq (int irq, void *dummy, struct pt_regs *fp);
static void fd_error( void );
static int do_format(int drive, int type, struct atari_format_descr *desc);
static void do_fd_action( int drive );
static void fd_calibrate( void );
static void fd_calibrate_done( int status );
static void fd_seek( void );
static void fd_seek_done( int status );
static void fd_rwsec( void );
static void fd_readtrack_check( unsigned long dummy );
static void fd_rwsec_done( int status );
static void fd_rwsec_done1(int status);
static void fd_writetrack( void );
static void fd_writetrack_done( int status );
static void fd_times_out( unsigned long dummy );
static void finish_fdc( void );
static void finish_fdc_done( int dummy );
static void setup_req_params( int drive );
static void redo_fd_request( void);
static int fd_ioctl( struct inode *inode, struct file *filp, unsigned int
cmd, unsigned long param);
static void fd_probe( int drive );
static int fd_test_drive_present( int drive );
static void config_types( void );
static int floppy_open( struct inode *inode, struct file *filp );
static int floppy_release( struct inode * inode, struct file * filp );
/************************* End of Prototypes **************************/
static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer, 0, 0);
static DEFINE_TIMER(readtrack_timer, fd_readtrack_check, 0, 0);
static DEFINE_TIMER(timeout_timer, fd_times_out, 0, 0);
static DEFINE_TIMER(fd_timer, check_change, 0, 0);
static inline void start_motor_off_timer(void)
{
mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
MotorOffTrys = 0;
}
static inline void start_check_change_timer( void )
{
mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
}
static inline void start_timeout(void)
{
mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
}
static inline void stop_timeout(void)
{
del_timer(&timeout_timer);
}
/* Select the side to use. */
static void fd_select_side( int side )
{
unsigned long flags;
/* protect against various other ints mucking around with the PSG */
local_irq_save(flags);
sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
sound_ym.rd_data_reg_sel & 0xfe;
local_irq_restore(flags);
}
/* Select a drive, update the FDC's track register and set the correct
* clock speed for this disk's type.
*/
static void fd_select_drive( int drive )
{
unsigned long flags;
unsigned char tmp;
if (drive == SelectedDrive)
return;
/* protect against various other ints mucking around with the PSG */
local_irq_save(flags);
sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
tmp = sound_ym.rd_data_reg_sel;
sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
atari_dont_touch_floppy_select = 1;
local_irq_restore(flags);
/* restore track register to saved value */
FDC_WRITE( FDCREG_TRACK, UD.track );
udelay(25);
/* select 8/16 MHz */
if (UDT)
if (ATARIHW_PRESENT(FDCSPEED))
dma_wd.fdc_speed = UDT->fdc_speed;
SelectedDrive = drive;
}
/* Deselect both drives. */
static void fd_deselect( void )
{
unsigned long flags;
/* protect against various other ints mucking around with the PSG */
local_irq_save(flags);
atari_dont_touch_floppy_select = 0;
sound_ym.rd_data_reg_sel=14; /* Select PSG Port A */
sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
(MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
/* On Falcon, the drive B select line is used on the printer port, so
* leave it alone... */
SelectedDrive = -1;
local_irq_restore(flags);
}
/* This timer function deselects the drives when the FDC switched the
* motor off. The deselection cannot happen earlier because the FDC
* counts the index signals, which arrive only if one drive is selected.
*/
static void fd_motor_off_timer( unsigned long dummy )
{
unsigned char status;
if (SelectedDrive < 0)
/* no drive selected, needn't deselect anyone */
return;
if (stdma_islocked())
goto retry;
status = FDC_READ( FDCREG_STATUS );
if (!(status & 0x80)) {
/* motor already turned off by FDC -> deselect drives */
MotorOn = 0;
fd_deselect();
return;
}
/* not yet off, try again */
retry:
/* Test again later; if tested too often, it seems there is no disk
* in the drive and the FDC will leave the motor on forever (or,
* at least until a disk is inserted). So we'll test only twice
* per second from then on...
*/
mod_timer(&motor_off_timer,
jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
}
/* This function is repeatedly called to detect disk changes (as good
* as possible) and keep track of the current state of the write protection.
*/
static void check_change( unsigned long dummy )
{
static int drive = 0;
unsigned long flags;
unsigned char old_porta;
int stat;
if (++drive > 1 || !UD.connected)
drive = 0;
/* protect against various other ints mucking around with the PSG */
local_irq_save(flags);
if (!stdma_islocked()) {
sound_ym.rd_data_reg_sel = 14;
old_porta = sound_ym.rd_data_reg_sel;
sound_ym.wd_data = (old_porta | DSKDRVNONE) &
~(drive == 0 ? DSKDRV0 : DSKDRV1);
stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
sound_ym.wd_data = old_porta;
if (stat != UD.wpstat) {
DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
UD.wpstat = stat;
set_bit (drive, &changed_floppies);
}
}
local_irq_restore(flags);
start_check_change_timer();
}
/* Handling of the Head Settling Flag: This flag should be set after each
* seek operation, because we don't use seeks with verify.
*/
static inline void set_head_settle_flag(void)
{
HeadSettleFlag = FDCCMDADD_E;
}
static inline int get_head_settle_flag(void)
{
int tmp = HeadSettleFlag;
HeadSettleFlag = 0;
return( tmp );
}
static inline void copy_buffer(void *from, void *to)
{
ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
int cnt;
for (cnt = 512/4; cnt; cnt--)
*p2++ = *p1++;
}
/* General Interrupt Handling */
static void (*FloppyIRQHandler)( int status ) = NULL;
static irqreturn_t floppy_irq (int irq, void *dummy, struct pt_regs *fp)
{
unsigned char status;
void (*handler)( int );
handler = xchg(&FloppyIRQHandler, NULL);
if (handler) {
nop();
status = FDC_READ( FDCREG_STATUS );
DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
handler( status );
}
else {
DPRINT(("FDC irq, no handler\n"));
}
return IRQ_HANDLED;
}
/* Error handling: If some error happened, retry some times, then
* recalibrate, then try again, and fail after MAX_ERRORS.
*/
static void fd_error( void )
{
if (IsFormatting) {
IsFormatting = 0;
FormatError = 1;
wake_up( &format_wait );
return;
}
if (!CURRENT)
return;
CURRENT->errors++;
if (CURRENT->errors >= MAX_ERRORS) {
printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
end_request(CURRENT, 0);
}
else if (CURRENT->errors == RECALIBRATE_ERRORS) {
printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
if (SelectedDrive != -1)
SUD.track = -1;
}
redo_fd_request();
}
#define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
/* ---------- Formatting ---------- */
#define FILL(n,val) \
do { \
memset( p, val, n ); \
p += n; \
} while(0)
static int do_format(int drive, int type, struct atari_format_descr *desc)
{
unsigned char *p;
int sect, nsect;
unsigned long flags;
DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
drive, desc->track, desc->head, desc->sect_offset ));
local_irq_save(flags);
while( fdc_busy ) sleep_on( &fdc_wait );
fdc_busy = 1;
stdma_lock(floppy_irq, NULL);
atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
local_irq_restore(flags);
if (type) {
if (--type >= NUM_DISK_MINORS ||
minor2disktype[type].drive_types > DriveType) {
redo_fd_request();
return -EINVAL;
}
type = minor2disktype[type].index;
UDT = &disk_type[type];
}
if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
redo_fd_request();
return -EINVAL;
}
nsect = UDT->spt;
p = TrackBuffer;
/* The track buffer is used for the raw track data, so its
contents become invalid! */
BufferDrive = -1;
/* stop deselect timer */
del_timer( &motor_off_timer );
FILL( 60 * (nsect / 9), 0x4e );
for( sect = 0; sect < nsect; ++sect ) {
FILL( 12, 0 );
FILL( 3, 0xf5 );
*p++ = 0xfe;
*p++ = desc->track;
*p++ = desc->head;
*p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
*p++ = 2;
*p++ = 0xf7;
FILL( 22, 0x4e );
FILL( 12, 0 );
FILL( 3, 0xf5 );
*p++ = 0xfb;
FILL( 512, 0xe5 );
*p++ = 0xf7;
FILL( 40, 0x4e );
}
FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
IsFormatting = 1;
FormatError = 0;
ReqTrack = desc->track;
ReqSide = desc->head;
do_fd_action( drive );
sleep_on( &format_wait );
redo_fd_request();
return( FormatError ? -EIO : 0 );
}
/* do_fd_action() is the general procedure for a fd request: All
* required parameter settings (drive select, side select, track
* position) are checked and set if needed. For each of these
* parameters and the actual reading or writing exist two functions:
* one that starts the setting (or skips it if possible) and one
* callback for the "done" interrupt. Each done func calls the next
* set function to propagate the request down to fd_rwsec_done().
*/
static void do_fd_action( int drive )
{
DPRINT(("do_fd_action\n"));
if (UseTrackbuffer && !IsFormatting) {
repeat:
if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
if (ReqCmd == READ) {
copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
if (++ReqCnt < CURRENT->current_nr_sectors) {
/* read next sector */
setup_req_params( drive );
goto repeat;
}
else {
/* all sectors finished */
CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
CURRENT->sector += CURRENT->current_nr_sectors;
end_request(CURRENT, 1);
redo_fd_request();
return;
}
}
else {
/* cmd == WRITE, pay attention to track buffer
* consistency! */
copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
}
}
}
if (SelectedDrive != drive)
fd_select_drive( drive );
if (UD.track == -1)
fd_calibrate();
else if (UD.track != ReqTrack << UDT->stretch)
fd_seek();
else if (IsFormatting)
fd_writetrack();
else
fd_rwsec();
}
/* Seek to track 0 if the current track is unknown */
static void fd_calibrate( void )
{
if (SUD.track >= 0) {
fd_calibrate_done( 0 );
return;
}
if (ATARIHW_PRESENT(FDCSPEED))
dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
DPRINT(("fd_calibrate\n"));
SET_IRQ_HANDLER( fd_calibrate_done );
/* we can't verify, since the speed may be incorrect */
FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
NeedSeek = 1;
MotorOn = 1;
start_timeout();
/* wait for IRQ */
}
static void fd_calibrate_done( int status )
{
DPRINT(("fd_calibrate_done()\n"));
stop_timeout();
/* set the correct speed now */
if (ATARIHW_PRESENT(FDCSPEED))
dma_wd.fdc_speed = SUDT->fdc_speed;
if (status & FDCSTAT_RECNF) {
printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
fd_error();
}
else {
SUD.track = 0;
fd_seek();
}
}
/* Seek the drive to the requested track. The drive must have been
* calibrated at some point before this.
*/
static void fd_seek( void )
{
if (SUD.track == ReqTrack << SUDT->stretch) {
fd_seek_done( 0 );
return;
}
if (ATARIHW_PRESENT(FDCSPEED)) {
dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
MFPDELAY();
}
DPRINT(("fd_seek() to track %d\n",ReqTrack));
FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
udelay(25);
SET_IRQ_HANDLER( fd_seek_done );
FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
MotorOn = 1;
set_head_settle_flag();
start_timeout();
/* wait for IRQ */
}
static void fd_seek_done( int status )
{
DPRINT(("fd_seek_done()\n"));
stop_timeout();
/* set the correct speed */
if (ATARIHW_PRESENT(FDCSPEED))
dma_wd.fdc_speed = SUDT->fdc_speed;
if (status & FDCSTAT_RECNF) {
printk(KERN_ERR "fd%d: seek error (to track %d)\n",
SelectedDrive, ReqTrack );
/* we don't know exactly which track we are on now! */
SUD.track = -1;
fd_error();
}
else {
SUD.track = ReqTrack << SUDT->stretch;
NeedSeek = 0;
if (IsFormatting)
fd_writetrack();
else
fd_rwsec();
}
}
/* This does the actual reading/writing after positioning the head
* over the correct track.
*/
static int MultReadInProgress = 0;
static void fd_rwsec( void )
{
unsigned long paddr, flags;
unsigned int rwflag, old_motoron;
unsigned int track;
DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
if (ReqCmd == WRITE) {
if (ATARIHW_PRESENT(EXTD_DMA)) {
paddr = virt_to_phys(ReqData);
}
else {
copy_buffer( ReqData, DMABuffer );
paddr = PhysDMABuffer;
}
dma_cache_maintenance( paddr, 512, 1 );
rwflag = 0x100;
}
else {
if (read_track)
paddr = PhysTrackBuffer;
else
paddr = ATARIHW_PRESENT(EXTD_DMA) ?
virt_to_phys(ReqData) : PhysDMABuffer;
rwflag = 0;
}
fd_select_side( ReqSide );
/* Start sector of this operation */
FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
MFPDELAY();
/* Cheat for track if stretch != 0 */
if (SUDT->stretch) {
track = FDC_READ( FDCREG_TRACK);
MFPDELAY();
FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
}
udelay(25);
/* Setup DMA */
local_irq_save(flags);
dma_wd.dma_lo = (unsigned char)paddr;
MFPDELAY();
paddr >>= 8;
dma_wd.dma_md = (unsigned char)paddr;
MFPDELAY();
paddr >>= 8;
if (ATARIHW_PRESENT(EXTD_DMA))
st_dma_ext_dmahi = (unsigned short)paddr;
else
dma_wd.dma_hi = (unsigned char)paddr;
MFPDELAY();
local_irq_restore(flags);
/* Clear FIFO and switch DMA to correct mode */
dma_wd.dma_mode_status = 0x90 | rwflag;
MFPDELAY();
dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
MFPDELAY();
dma_wd.dma_mode_status = 0x90 | rwflag;
MFPDELAY();
/* How many sectors for DMA */
dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
udelay(25);
/* Start operation */
dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
udelay(25);
SET_IRQ_HANDLER( fd_rwsec_done );
dma_wd.fdc_acces_seccount =
(get_head_settle_flag() |
(rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
old_motoron = MotorOn;
MotorOn = 1;
NeedSeek = 1;
/* wait for interrupt */
if (read_track) {
/* If reading a whole track, wait about one disk rotation and
* then check if all sectors are read. The FDC will even
* search for the first non-existent sector and need 1 sec to
* recognise that it isn't present :-(
*/
MultReadInProgress = 1;
mod_timer(&readtrack_timer,
/* 1 rot. + 5 rot.s if motor was off */
jiffies + HZ/5 + (old_motoron ? 0 : HZ));
}
start_timeout();
}
static void fd_readtrack_check( unsigned long dummy )
{
unsigned long flags, addr, addr2;
local_irq_save(flags);
if (!MultReadInProgress) {
/* This prevents a race condition that could arise if the
* interrupt is triggered while the calling of this timer
* callback function takes place. The IRQ function then has
* already cleared 'MultReadInProgress' when flow of control
* gets here.
*/
local_irq_restore(flags);
return;
}
/* get the current DMA address */
/* ++ f.a. read twice to avoid being fooled by switcher */
addr = 0;
do {
addr2 = addr;
addr = dma_wd.dma_lo & 0xff;
MFPDELAY();
addr |= (dma_wd.dma_md & 0xff) << 8;
MFPDELAY();
if (ATARIHW_PRESENT( EXTD_DMA ))
addr |= (st_dma_ext_dmahi & 0xffff) << 16;
else
addr |= (dma_wd.dma_hi & 0xff) << 16;
MFPDELAY();
} while(addr != addr2);
if (addr >= PhysTrackBuffer + SUDT->spt*512) {
/* already read enough data, force an FDC interrupt to stop
* the read operation
*/