forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amiflop.c
1955 lines (1680 loc) · 49.9 KB
/
amiflop.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
/*
* linux/amiga/amiflop.c
*
* Copyright (C) 1993 Greg Harp
* Portions of this driver are based on code contributed by Brad Pepers
*
* revised 28.5.95 by Joerg Dorchain
* - now no bugs(?) any more for both HD & DD
* - added support for 40 Track 5.25" drives, 80-track hopefully behaves
* like 3.5" dd (no way to test - are there any 5.25" drives out there
* that work on an A4000?)
* - wrote formatting routine (maybe dirty, but works)
*
* june/july 1995 added ms-dos support by Joerg Dorchain
* (portions based on messydos.device and various contributors)
* - currently only 9 and 18 sector disks
*
* - fixed a bug with the internal trackbuffer when using multiple
* disks the same time
* - made formatting a bit safer
* - added command line and machine based default for "silent" df0
*
* december 1995 adapted for 1.2.13pl4 by Joerg Dorchain
* - works but I think it's inefficient. (look in redo_fd_request)
* But the changes were very efficient. (only three and a half lines)
*
* january 1996 added special ioctl for tracking down read/write problems
* - usage ioctl(d, RAW_TRACK, ptr); the raw track buffer (MFM-encoded data
* is copied to area. (area should be large enough since no checking is
* done - 30K is currently sufficient). return the actual size of the
* trackbuffer
* - replaced udelays() by a timer (CIAA timer B) for the waits
* needed for the disk mechanic.
*
* february 1996 fixed error recovery and multiple disk access
* - both got broken the first time I tampered with the driver :-(
* - still not safe, but better than before
*
* revised Marts 3rd, 1996 by Jes Sorensen for use in the 1.3.28 kernel.
* - Minor changes to accept the kdev_t.
* - Replaced some more udelays with ms_delays. Udelay is just a loop,
* and so the delay will be different depending on the given
* processor :-(
* - The driver could use a major cleanup because of the new
* major/minor handling that came with kdev_t. It seems to work for
* the time being, but I can't guarantee that it will stay like
* that when we start using 16 (24?) bit minors.
*
* restructured jan 1997 by Joerg Dorchain
* - Fixed Bug accessing multiple disks
* - some code cleanup
* - added trackbuffer for each drive to speed things up
* - fixed some race conditions (who finds the next may send it to me ;-)
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/fd.h>
#include <linux/hdreg.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <linux/blk-mq.h>
#include <linux/elevator.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <asm/setup.h>
#include <linux/uaccess.h>
#include <asm/amigahw.h>
#include <asm/amigaints.h>
#include <asm/irq.h>
#undef DEBUG /* print _LOTS_ of infos */
#define RAW_IOCTL
#ifdef RAW_IOCTL
#define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */
#endif
/*
* Defines
*/
/*
* CIAAPRA bits (read only)
*/
#define DSKRDY (0x1<<5) /* disk ready when low */
#define DSKTRACK0 (0x1<<4) /* head at track zero when low */
#define DSKPROT (0x1<<3) /* disk protected when low */
#define DSKCHANGE (0x1<<2) /* low when disk removed */
/*
* CIAAPRB bits (read/write)
*/
#define DSKMOTOR (0x1<<7) /* motor on when low */
#define DSKSEL3 (0x1<<6) /* select drive 3 when low */
#define DSKSEL2 (0x1<<5) /* select drive 2 when low */
#define DSKSEL1 (0x1<<4) /* select drive 1 when low */
#define DSKSEL0 (0x1<<3) /* select drive 0 when low */
#define DSKSIDE (0x1<<2) /* side selection: 0 = upper, 1 = lower */
#define DSKDIREC (0x1<<1) /* step direction: 0=in, 1=out (to trk 0) */
#define DSKSTEP (0x1) /* pulse low to step head 1 track */
/*
* DSKBYTR bits (read only)
*/
#define DSKBYT (1<<15) /* register contains valid byte when set */
#define DMAON (1<<14) /* disk DMA enabled */
#define DISKWRITE (1<<13) /* disk write bit in DSKLEN enabled */
#define WORDEQUAL (1<<12) /* DSKSYNC register match when true */
/* bits 7-0 are data */
/*
* ADKCON/ADKCONR bits
*/
#ifndef SETCLR
#define ADK_SETCLR (1<<15) /* control bit */
#endif
#define ADK_PRECOMP1 (1<<14) /* precompensation selection */
#define ADK_PRECOMP0 (1<<13) /* 00=none, 01=140ns, 10=280ns, 11=500ns */
#define ADK_MFMPREC (1<<12) /* 0=GCR precomp., 1=MFM precomp. */
#define ADK_WORDSYNC (1<<10) /* enable DSKSYNC auto DMA */
#define ADK_MSBSYNC (1<<9) /* when 1, enable sync on MSbit (for GCR) */
#define ADK_FAST (1<<8) /* bit cell: 0=2us (GCR), 1=1us (MFM) */
/*
* DSKLEN bits
*/
#define DSKLEN_DMAEN (1<<15)
#define DSKLEN_WRITE (1<<14)
/*
* INTENA/INTREQ bits
*/
#define DSKINDEX (0x1<<4) /* DSKINDEX bit */
/*
* Misc
*/
#define MFM_SYNC 0x4489 /* standard MFM sync value */
/* Values for FD_COMMAND */
#define FD_RECALIBRATE 0x07 /* move to track 0 */
#define FD_SEEK 0x0F /* seek track */
#define FD_READ 0xE6 /* read with MT, MFM, SKip deleted */
#define FD_WRITE 0xC5 /* write with MT, MFM */
#define FD_SENSEI 0x08 /* Sense Interrupt Status */
#define FD_SPECIFY 0x03 /* specify HUT etc */
#define FD_FORMAT 0x4D /* format one track */
#define FD_VERSION 0x10 /* get version code */
#define FD_CONFIGURE 0x13 /* configure FIFO operation */
#define FD_PERPENDICULAR 0x12 /* perpendicular r/w mode */
#define FD_MAX_UNITS 4 /* Max. Number of drives */
#define FLOPPY_MAX_SECTORS 22 /* Max. Number of sectors per track */
struct fd_data_type {
char *name; /* description of data type */
int sects; /* sectors per track */
int (*read_fkt)(int); /* read whole track */
void (*write_fkt)(int); /* write whole track */
};
struct fd_drive_type {
unsigned long code; /* code returned from drive */
char *name; /* description of drive */
unsigned int tracks; /* number of tracks */
unsigned int heads; /* number of heads */
unsigned int read_size; /* raw read size for one track */
unsigned int write_size; /* raw write size for one track */
unsigned int sect_mult; /* sectors and gap multiplier (HD = 2) */
unsigned int precomp1; /* start track for precomp 1 */
unsigned int precomp2; /* start track for precomp 2 */
unsigned int step_delay; /* time (in ms) for delay after step */
unsigned int settle_time; /* time to settle after dir change */
unsigned int side_time; /* time needed to change sides */
};
struct amiga_floppy_struct {
struct fd_drive_type *type; /* type of floppy for this unit */
struct fd_data_type *dtype; /* type of floppy for this unit */
int track; /* current track (-1 == unknown) */
unsigned char *trackbuf; /* current track (kmaloc()'d */
int blocks; /* total # blocks on disk */
int changed; /* true when not known */
int disk; /* disk in drive (-1 == unknown) */
int motor; /* true when motor is at speed */
int busy; /* true when drive is active */
int dirty; /* true when trackbuf is not on disk */
int status; /* current error code for unit */
struct gendisk *gendisk;
struct blk_mq_tag_set tag_set;
};
/*
* Error codes
*/
#define FD_OK 0 /* operation succeeded */
#define FD_ERROR -1 /* general error (seek, read, write, etc) */
#define FD_NOUNIT 1 /* unit does not exist */
#define FD_UNITBUSY 2 /* unit already active */
#define FD_NOTACTIVE 3 /* unit is not active */
#define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */
#define MFM_NOSYNC 1
#define MFM_HEADER 2
#define MFM_DATA 3
#define MFM_TRACK 4
/*
* Floppy ID values
*/
#define FD_NODRIVE 0x00000000 /* response when no unit is present */
#define FD_DD_3 0xffffffff /* double-density 3.5" (880K) drive */
#define FD_HD_3 0x55555555 /* high-density 3.5" (1760K) drive */
#define FD_DD_5 0xaaaaaaaa /* double-density 5.25" (440K) drive */
static DEFINE_MUTEX(amiflop_mutex);
static unsigned long int fd_def_df0 = FD_DD_3; /* default for df0 if it doesn't identify */
module_param(fd_def_df0, ulong, 0);
MODULE_LICENSE("GPL");
/*
* Macros
*/
#define MOTOR_ON (ciab.prb &= ~DSKMOTOR)
#define MOTOR_OFF (ciab.prb |= DSKMOTOR)
#define SELECT(mask) (ciab.prb &= ~mask)
#define DESELECT(mask) (ciab.prb |= mask)
#define SELMASK(drive) (1 << (3 + (drive & 3)))
static struct fd_drive_type drive_types[] = {
/* code name tr he rdsz wrsz sm pc1 pc2 sd st st*/
/* warning: times are now in milliseconds (ms) */
{ FD_DD_3, "DD 3.5", 80, 2, 14716, 13630, 1, 80,161, 3, 18, 1},
{ FD_HD_3, "HD 3.5", 80, 2, 28344, 27258, 2, 80,161, 3, 18, 1},
{ FD_DD_5, "DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2},
{ FD_NODRIVE, "No Drive", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
static int num_dr_types = ARRAY_SIZE(drive_types);
static int amiga_read(int), dos_read(int);
static void amiga_write(int), dos_write(int);
static struct fd_data_type data_types[] = {
{ "Amiga", 11 , amiga_read, amiga_write},
{ "MS-Dos", 9, dos_read, dos_write}
};
/* current info on each unit */
static struct amiga_floppy_struct unit[FD_MAX_UNITS];
static struct timer_list flush_track_timer[FD_MAX_UNITS];
static struct timer_list post_write_timer;
static unsigned long post_write_timer_drive;
static struct timer_list motor_on_timer;
static struct timer_list motor_off_timer[FD_MAX_UNITS];
static int on_attempts;
/* Synchronization of FDC access */
/* request loop (trackbuffer) */
static volatile int fdc_busy = -1;
static volatile int fdc_nested;
static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
static DECLARE_COMPLETION(motor_on_completion);
static volatile int selected = -1; /* currently selected drive */
static int writepending;
static int writefromint;
static char *raw_buf;
static DEFINE_SPINLOCK(amiflop_lock);
#define RAW_BUF_SIZE 30000 /* size of raw disk data */
/*
* 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 volatile char block_flag;
static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block);
/* MS-Dos MFM Coding tables (should go quick and easy) */
static unsigned char mfmencode[16]={
0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
};
static unsigned char mfmdecode[128];
/* floppy internal millisecond timer stuff */
static DECLARE_COMPLETION(ms_wait_completion);
#define MS_TICKS ((amiga_eclock+50)/1000)
/*
* Note that MAX_ERRORS=X doesn't imply that we retry every bad read
* max X times - some types of errors increase the errorcount by 2 or
* even 3, so we might actually retry only X/2 times before giving up.
*/
#define MAX_ERRORS 12
#define custom amiga_custom
/* Prevent "aliased" accesses. */
static int fd_ref[4] = { 0,0,0,0 };
static int fd_device[4] = { 0, 0, 0, 0 };
/*
* Here come the actual hardware access and helper functions.
* They are not reentrant and single threaded because all drives
* share the same hardware and the same trackbuffer.
*/
/* Milliseconds timer */
static irqreturn_t ms_isr(int irq, void *dummy)
{
complete(&ms_wait_completion);
return IRQ_HANDLED;
}
/* all waits are queued up
A more generic routine would do a schedule a la timer.device */
static void ms_delay(int ms)
{
int ticks;
static DEFINE_MUTEX(mutex);
if (ms > 0) {
mutex_lock(&mutex);
ticks = MS_TICKS*ms-1;
ciaa.tblo=ticks%256;
ciaa.tbhi=ticks/256;
ciaa.crb=0x19; /*count eclock, force load, one-shoot, start */
wait_for_completion(&ms_wait_completion);
mutex_unlock(&mutex);
}
}
/* Hardware semaphore */
/* returns true when we would get the semaphore */
static inline int try_fdc(int drive)
{
drive &= 3;
return ((fdc_busy < 0) || (fdc_busy == drive));
}
static void get_fdc(int drive)
{
unsigned long flags;
drive &= 3;
#ifdef DEBUG
printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive,fdc_busy,fdc_nested);
#endif
local_irq_save(flags);
wait_event(fdc_wait, try_fdc(drive));
fdc_busy = drive;
fdc_nested++;
local_irq_restore(flags);
}
static inline void rel_fdc(void)
{
#ifdef DEBUG
if (fdc_nested == 0)
printk("fd: unmatched rel_fdc\n");
printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy,fdc_nested);
#endif
fdc_nested--;
if (fdc_nested == 0) {
fdc_busy = -1;
wake_up(&fdc_wait);
}
}
static void fd_select (int drive)
{
unsigned char prb = ~0;
drive&=3;
#ifdef DEBUG
printk("selecting %d\n",drive);
#endif
if (drive == selected)
return;
get_fdc(drive);
selected = drive;
if (unit[drive].track % 2 != 0)
prb &= ~DSKSIDE;
if (unit[drive].motor == 1)
prb &= ~DSKMOTOR;
ciab.prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
ciab.prb = prb;
prb &= ~SELMASK(drive);
ciab.prb = prb;
rel_fdc();
}
static void fd_deselect (int drive)
{
unsigned char prb;
unsigned long flags;
drive&=3;
#ifdef DEBUG
printk("deselecting %d\n",drive);
#endif
if (drive != selected) {
printk(KERN_WARNING "Deselecting drive %d while %d was selected!\n",drive,selected);
return;
}
get_fdc(drive);
local_irq_save(flags);
selected = -1;
prb = ciab.prb;
prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
ciab.prb = prb;
local_irq_restore (flags);
rel_fdc();
}
static void motor_on_callback(struct timer_list *unused)
{
if (!(ciaa.pra & DSKRDY) || --on_attempts == 0) {
complete_all(&motor_on_completion);
} else {
motor_on_timer.expires = jiffies + HZ/10;
add_timer(&motor_on_timer);
}
}
static int fd_motor_on(int nr)
{
nr &= 3;
del_timer(motor_off_timer + nr);
if (!unit[nr].motor) {
unit[nr].motor = 1;
fd_select(nr);
reinit_completion(&motor_on_completion);
mod_timer(&motor_on_timer, jiffies + HZ/2);
on_attempts = 10;
wait_for_completion(&motor_on_completion);
fd_deselect(nr);
}
if (on_attempts == 0) {
on_attempts = -1;
#if 0
printk (KERN_ERR "motor_on failed, turning motor off\n");
fd_motor_off (motor_off_timer + nr);
return 0;
#else
printk (KERN_WARNING "DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n");
#endif
}
return 1;
}
static void fd_motor_off(struct timer_list *timer)
{
unsigned long drive = ((unsigned long)timer -
(unsigned long)&motor_off_timer[0]) /
sizeof(motor_off_timer[0]);
drive&=3;
if (!try_fdc(drive)) {
/* We would be blocked in an interrupt, so try again later */
timer->expires = jiffies + 1;
add_timer(timer);
return;
}
unit[drive].motor = 0;
fd_select(drive);
udelay (1);
fd_deselect(drive);
}
static void floppy_off (unsigned int nr)
{
int drive;
drive = nr & 3;
mod_timer(motor_off_timer + drive, jiffies + 3*HZ);
}
static int fd_calibrate(int drive)
{
unsigned char prb;
int n;
drive &= 3;
get_fdc(drive);
if (!fd_motor_on (drive))
return 0;
fd_select (drive);
prb = ciab.prb;
prb |= DSKSIDE;
prb &= ~DSKDIREC;
ciab.prb = prb;
for (n = unit[drive].type->tracks/2; n != 0; --n) {
if (ciaa.pra & DSKTRACK0)
break;
prb &= ~DSKSTEP;
ciab.prb = prb;
prb |= DSKSTEP;
udelay (2);
ciab.prb = prb;
ms_delay(unit[drive].type->step_delay);
}
ms_delay (unit[drive].type->settle_time);
prb |= DSKDIREC;
n = unit[drive].type->tracks + 20;
for (;;) {
prb &= ~DSKSTEP;
ciab.prb = prb;
prb |= DSKSTEP;
udelay (2);
ciab.prb = prb;
ms_delay(unit[drive].type->step_delay + 1);
if ((ciaa.pra & DSKTRACK0) == 0)
break;
if (--n == 0) {
printk (KERN_ERR "fd%d: calibrate failed, turning motor off\n", drive);
fd_motor_off (motor_off_timer + drive);
unit[drive].track = -1;
rel_fdc();
return 0;
}
}
unit[drive].track = 0;
ms_delay(unit[drive].type->settle_time);
rel_fdc();
fd_deselect(drive);
return 1;
}
static int fd_seek(int drive, int track)
{
unsigned char prb;
int cnt;
#ifdef DEBUG
printk("seeking drive %d to track %d\n",drive,track);
#endif
drive &= 3;
get_fdc(drive);
if (unit[drive].track == track) {
rel_fdc();
return 1;
}
if (!fd_motor_on(drive)) {
rel_fdc();
return 0;
}
if (unit[drive].track < 0 && !fd_calibrate(drive)) {
rel_fdc();
return 0;
}
fd_select (drive);
cnt = unit[drive].track/2 - track/2;
prb = ciab.prb;
prb |= DSKSIDE | DSKDIREC;
if (track % 2 != 0)
prb &= ~DSKSIDE;
if (cnt < 0) {
cnt = - cnt;
prb &= ~DSKDIREC;
}
ciab.prb = prb;
if (track % 2 != unit[drive].track % 2)
ms_delay (unit[drive].type->side_time);
unit[drive].track = track;
if (cnt == 0) {
rel_fdc();
fd_deselect(drive);
return 1;
}
do {
prb &= ~DSKSTEP;
ciab.prb = prb;
prb |= DSKSTEP;
udelay (1);
ciab.prb = prb;
ms_delay (unit[drive].type->step_delay);
} while (--cnt != 0);
ms_delay (unit[drive].type->settle_time);
rel_fdc();
fd_deselect(drive);
return 1;
}
static unsigned long fd_get_drive_id(int drive)
{
int i;
ulong id = 0;
drive&=3;
get_fdc(drive);
/* set up for ID */
MOTOR_ON;
udelay(2);
SELECT(SELMASK(drive));
udelay(2);
DESELECT(SELMASK(drive));
udelay(2);
MOTOR_OFF;
udelay(2);
SELECT(SELMASK(drive));
udelay(2);
DESELECT(SELMASK(drive));
udelay(2);
/* loop and read disk ID */
for (i=0; i<32; i++) {
SELECT(SELMASK(drive));
udelay(2);
/* read and store value of DSKRDY */
id <<= 1;
id |= (ciaa.pra & DSKRDY) ? 0 : 1; /* cia regs are low-active! */
DESELECT(SELMASK(drive));
}
rel_fdc();
/*
* RB: At least A500/A2000's df0: don't identify themselves.
* As every (real) Amiga has at least a 3.5" DD drive as df0:
* we default to that if df0: doesn't identify as a certain
* type.
*/
if(drive == 0 && id == FD_NODRIVE)
{
id = fd_def_df0;
printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx\n", (ulong)fd_def_df0);
}
/* return the ID value */
return (id);
}
static irqreturn_t fd_block_done(int irq, void *dummy)
{
if (block_flag)
custom.dsklen = 0x4000;
if (block_flag == 2) { /* writing */
writepending = 2;
post_write_timer.expires = jiffies + 1; /* at least 2 ms */
post_write_timer_drive = selected;
add_timer(&post_write_timer);
}
else { /* reading */
block_flag = 0;
wake_up (&wait_fd_block);
}
return IRQ_HANDLED;
}
static void raw_read(int drive)
{
drive&=3;
get_fdc(drive);
wait_event(wait_fd_block, !block_flag);
fd_select(drive);
/* setup adkcon bits correctly */
custom.adkcon = ADK_MSBSYNC;
custom.adkcon = ADK_SETCLR|ADK_WORDSYNC|ADK_FAST;
custom.dsksync = MFM_SYNC;
custom.dsklen = 0;
custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
block_flag = 1;
wait_event(wait_fd_block, !block_flag);
custom.dsklen = 0;
fd_deselect(drive);
rel_fdc();
}
static int raw_write(int drive)
{
ushort adk;
drive&=3;
get_fdc(drive); /* corresponds to rel_fdc() in post_write() */
if ((ciaa.pra & DSKPROT) == 0) {
rel_fdc();
return 0;
}
wait_event(wait_fd_block, !block_flag);
fd_select(drive);
/* clear adkcon bits */
custom.adkcon = ADK_PRECOMP1|ADK_PRECOMP0|ADK_WORDSYNC|ADK_MSBSYNC;
/* set appropriate adkcon bits */
adk = ADK_SETCLR|ADK_FAST;
if ((ulong)unit[drive].track >= unit[drive].type->precomp2)
adk |= ADK_PRECOMP1;
else if ((ulong)unit[drive].track >= unit[drive].type->precomp1)
adk |= ADK_PRECOMP0;
custom.adkcon = adk;
custom.dsklen = DSKLEN_WRITE;
custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
block_flag = 2;
return 1;
}
/*
* to be called at least 2ms after the write has finished but before any
* other access to the hardware.
*/
static void post_write (unsigned long drive)
{
#ifdef DEBUG
printk("post_write for drive %ld\n",drive);
#endif
drive &= 3;
custom.dsklen = 0;
block_flag = 0;
writepending = 0;
writefromint = 0;
unit[drive].dirty = 0;
wake_up(&wait_fd_block);
fd_deselect(drive);
rel_fdc(); /* corresponds to get_fdc() in raw_write */
}
static void post_write_callback(struct timer_list *timer)
{
post_write(post_write_timer_drive);
}
/*
* The following functions are to convert the block contents into raw data
* written to disk and vice versa.
* (Add other formats here ;-))
*/
static unsigned long scan_sync(unsigned long raw, unsigned long end)
{
ushort *ptr = (ushort *)raw, *endp = (ushort *)end;
while (ptr < endp && *ptr++ != 0x4489)
;
if (ptr < endp) {
while (*ptr == 0x4489 && ptr < endp)
ptr++;
return (ulong)ptr;
}
return 0;
}
static inline unsigned long checksum(unsigned long *addr, int len)
{
unsigned long csum = 0;
len /= sizeof(*addr);
while (len-- > 0)
csum ^= *addr++;
csum = ((csum>>1) & 0x55555555) ^ (csum & 0x55555555);
return csum;
}
static unsigned long decode (unsigned long *data, unsigned long *raw,
int len)
{
ulong *odd, *even;
/* convert length from bytes to longwords */
len >>= 2;
odd = raw;
even = odd + len;
/* prepare return pointer */
raw += len * 2;
do {
*data++ = ((*odd++ & 0x55555555) << 1) | (*even++ & 0x55555555);
} while (--len != 0);
return (ulong)raw;
}
struct header {
unsigned char magic;
unsigned char track;
unsigned char sect;
unsigned char ord;
unsigned char labels[16];
unsigned long hdrchk;
unsigned long datachk;
};
static int amiga_read(int drive)
{
unsigned long raw;
unsigned long end;
int scnt;
unsigned long csum;
struct header hdr;
drive&=3;
raw = (long) raw_buf;
end = raw + unit[drive].type->read_size;
for (scnt = 0;scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
if (!(raw = scan_sync(raw, end))) {
printk (KERN_INFO "can't find sync for sector %d\n", scnt);
return MFM_NOSYNC;
}
raw = decode ((ulong *)&hdr.magic, (ulong *)raw, 4);
raw = decode ((ulong *)&hdr.labels, (ulong *)raw, 16);
raw = decode ((ulong *)&hdr.hdrchk, (ulong *)raw, 4);
raw = decode ((ulong *)&hdr.datachk, (ulong *)raw, 4);
csum = checksum((ulong *)&hdr,
(char *)&hdr.hdrchk-(char *)&hdr);
#ifdef DEBUG
printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx\n",
hdr.magic, hdr.track, hdr.sect, hdr.ord,
*(ulong *)&hdr.labels[0], *(ulong *)&hdr.labels[4],
*(ulong *)&hdr.labels[8], *(ulong *)&hdr.labels[12],
hdr.hdrchk, hdr.datachk);
#endif
if (hdr.hdrchk != csum) {
printk(KERN_INFO "MFM_HEADER: %08lx,%08lx\n", hdr.hdrchk, csum);
return MFM_HEADER;
}
/* verify track */
if (hdr.track != unit[drive].track) {
printk(KERN_INFO "MFM_TRACK: %d, %d\n", hdr.track, unit[drive].track);
return MFM_TRACK;
}
raw = decode ((ulong *)(unit[drive].trackbuf + hdr.sect*512),
(ulong *)raw, 512);
csum = checksum((ulong *)(unit[drive].trackbuf + hdr.sect*512), 512);
if (hdr.datachk != csum) {
printk(KERN_INFO "MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx\n",
hdr.magic, hdr.track, hdr.sect, hdr.ord, scnt,
hdr.datachk, csum);
printk (KERN_INFO "data=(%lx,%lx,%lx,%lx)\n",
((ulong *)(unit[drive].trackbuf+hdr.sect*512))[0],
((ulong *)(unit[drive].trackbuf+hdr.sect*512))[1],
((ulong *)(unit[drive].trackbuf+hdr.sect*512))[2],
((ulong *)(unit[drive].trackbuf+hdr.sect*512))[3]);
return MFM_DATA;
}
}
return 0;
}
static void encode(unsigned long data, unsigned long *dest)
{
unsigned long data2;
data &= 0x55555555;
data2 = data ^ 0x55555555;
data |= ((data2 >> 1) | 0x80000000) & (data2 << 1);
if (*(dest - 1) & 0x00000001)
data &= 0x7FFFFFFF;
*dest = data;
}
static void encode_block(unsigned long *dest, unsigned long *src, int len)
{
int cnt, to_cnt = 0;
unsigned long data;
/* odd bits */
for (cnt = 0; cnt < len / 4; cnt++) {
data = src[cnt] >> 1;
encode(data, dest + to_cnt++);
}
/* even bits */
for (cnt = 0; cnt < len / 4; cnt++) {
data = src[cnt];
encode(data, dest + to_cnt++);
}
}
static unsigned long *putsec(int disk, unsigned long *raw, int cnt)
{
struct header hdr;
int i;
disk&=3;
*raw = (raw[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
raw++;
*raw++ = 0x44894489;
hdr.magic = 0xFF;
hdr.track = unit[disk].track;
hdr.sect = cnt;
hdr.ord = unit[disk].dtype->sects * unit[disk].type->sect_mult - cnt;
for (i = 0; i < 16; i++)
hdr.labels[i] = 0;
hdr.hdrchk = checksum((ulong *)&hdr,
(char *)&hdr.hdrchk-(char *)&hdr);
hdr.datachk = checksum((ulong *)(unit[disk].trackbuf+cnt*512), 512);
encode_block(raw, (ulong *)&hdr.magic, 4);
raw += 2;
encode_block(raw, (ulong *)&hdr.labels, 16);
raw += 8;
encode_block(raw, (ulong *)&hdr.hdrchk, 4);
raw += 2;
encode_block(raw, (ulong *)&hdr.datachk, 4);
raw += 2;
encode_block(raw, (ulong *)(unit[disk].trackbuf+cnt*512), 512);
raw += 256;
return raw;
}
static void amiga_write(int disk)
{
unsigned int cnt;
unsigned long *ptr = (unsigned long *)raw_buf;
disk&=3;
/* gap space */
for (cnt = 0; cnt < 415 * unit[disk].type->sect_mult; cnt++)
*ptr++ = 0xaaaaaaaa;
/* sectors */
for (cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
ptr = putsec (disk, ptr, cnt);
*(ushort *)ptr = (ptr[-1]&1) ? 0x2AA8 : 0xAAA8;
}
struct dos_header {
unsigned char track, /* 0-80 */
side, /* 0-1 */
sec, /* 0-...*/
len_desc;/* 2 */
unsigned short crc; /* on 68000 we got an alignment problem,
but this compiler solves it by adding silently
adding a pad byte so data won't fit
and this took about 3h to discover.... */
unsigned char gap1[22]; /* for longword-alignedness (0x4e) */
};
/* crc routines are borrowed from the messydos-handler */
/* excerpt from the messydos-device
; The CRC is computed not only over the actual data, but including
; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb).
; As we don't read or encode these fields into our buffers, we have to
; preload the registers containing the CRC with the values they would have
; after stepping over these fields.