summaryrefslogtreecommitdiffstats
path: root/sys/dev/fdc/fdc.c
blob: 518aa38b11e757388ff47557a85207dcc2938189 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
/*#define DEBUG 1*/
/*-
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Don Ahn.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
 *	$Id: fd.c,v 1.26 1994/05/25 08:58:39 rgrimes Exp $
 *
 */

#include "ft.h"
#if NFT < 1
#undef NFDC
#endif
#include "fd.h"

#if NFDC > 0

#include <sys/param.h>
#include <sys/dkbad.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <machine/ioctl_fd.h>
#include <sys/disklabel.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/syslog.h>
#include <i386/isa/isa.h>
#include <i386/isa/isa_device.h>
#include <i386/isa/fdreg.h>
#include <i386/isa/fdc.h>
#include <i386/isa/icu.h>
#include <i386/isa/rtc.h>

#if NFT > 0
extern int ftopen(), ftintr(), ftattach(), ftclose(), ftioctl();
#endif

#define b_cylin b_resid
#define FDBLK 512

/* misuse a flag to identify format operation */
#define B_FORMAT B_XXX

#define NUMTYPES 14
#define NUMDENS  (NUMTYPES - 6)

/* This defines (-1) must match index for fd_types */
#define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
#define NO_TYPE		0	/* must match NO_TYPE in ft.c */
#define FD_1720         1
#define FD_1480         2
#define FD_1440         3
#define FD_1200         4
#define FD_820          5
#define FD_800          6
#define FD_720          7
#define FD_360          8

#define FD_1480in5_25   9
#define FD_1440in5_25   10
#define FD_820in5_25    11
#define FD_800in5_25    12
#define FD_720in5_25    13
#define FD_360in5_25    14


struct fd_type fd_types[NUMTYPES] =
{
{ 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
{ 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
{ 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
{ 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
{ 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
{ 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
{  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
{  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */

{ 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
{ 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
{ 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
{ 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
{  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
{  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
};

#define DRVS_PER_CTLR 2		/* 2 floppies */
/***********************************************************************\
* Per controller structure.						*
\***********************************************************************/
struct fdc_data fdc_data[NFDC];

/***********************************************************************\
* Per drive structure.							*
* N per controller  (DRVS_PER_CTLR)					*
\***********************************************************************/
struct fd_data {
	struct	fdc_data *fdc;	/* pointer to controller structure */
	int	fdsu;		/* this units number on this controller */
	int	type;		/* Drive type (HD, DD     */
	struct	fd_type *ft;	/* pointer to the type descriptor */
	int	flags;
#define	FD_OPEN		0x01	/* it's open		*/
#define	FD_ACTIVE	0x02	/* it's active		*/
#define	FD_MOTOR	0x04	/* motor should be on	*/
#define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
	int skip;
	int hddrv;
	int track;		/* where we think the head is */
} fd_data[NFD];

/***********************************************************************\
* Throughout this file the following conventions will be used:		*
* fd is a pointer to the fd_data struct for the drive in question	*
* fdc is a pointer to the fdc_data struct for the controller		*
* fdu is the floppy drive unit number					*
* fdcu is the floppy controller unit number				*
* fdsu is the floppy drive unit number on that controller. (sub-unit)	*
\***********************************************************************/

#define	id_physid id_scsiid	/* this biotab field doubles as a field */
				/* for the physical unit number on the controller */

static int retrier(fdcu_t);

#define DEVIDLE		0
#define FINDWORK	1
#define	DOSEEK		2
#define SEEKCOMPLETE 	3
#define	IOCOMPLETE	4
#define RECALCOMPLETE	5
#define	STARTRECAL	6
#define	RESETCTLR	7
#define	SEEKWAIT	8
#define	RECALWAIT	9
#define	MOTORWAIT	10
#define	IOTIMEDOUT	11

#ifdef	DEBUG
char *fdstates[] =
{
"DEVIDLE",
"FINDWORK",
"DOSEEK",
"SEEKCOMPLETE",
"IOCOMPLETE",
"RECALCOMPLETE",
"STARTRECAL",
"RESETCTLR",
"SEEKWAIT",
"RECALWAIT",
"MOTORWAIT",
"IOTIMEDOUT"
};


int	fd_debug = 1;
#define TRACE0(arg) if(fd_debug) printf(arg)
#define TRACE1(arg1,arg2) if(fd_debug) printf(arg1,arg2)
#else /* DEBUG */
#define TRACE0(arg)
#define TRACE1(arg1,arg2)
#endif /* DEBUG */

static void fdstart(fdcu_t);
void fdintr(fdcu_t);
static void fd_turnoff(caddr_t);

/****************************************************************************/
/*                      autoconfiguration stuff                             */
/****************************************************************************/
static int fdprobe(struct isa_device *);
static int fdattach(struct isa_device *);

struct	isa_driver fdcdriver = {
	fdprobe, fdattach, "fdc",
};

/*
 * probe for existance of controller
 */
int
fdprobe(dev)
	struct isa_device *dev;
{
	fdcu_t	fdcu = dev->id_unit;
	if(fdc_data[fdcu].flags & FDC_ATTACHED)
	{
		printf("fdc: same unit (%d) used multiple times\n",fdcu);
		return 0;
	}

	fdc_data[fdcu].baseport = dev->id_iobase;

	/* First - lets reset the floppy controller */

	outb(dev->id_iobase+fdout,0);
	DELAY(100);
	outb(dev->id_iobase+fdout,FDO_FRST);

	/* see if it can handle a command */
	if (out_fdc(fdcu,NE7CMD_SPECIFY) < 0)
	{
		return(0);
	}
	out_fdc(fdcu,0xDF);
	out_fdc(fdcu,2);
	return (IO_FDCSIZE);
}

/*
 * wire controller into system, look for floppy units
 */
int
fdattach(dev)
	struct isa_device *dev;
{
	unsigned fdt,st0, cyl;
	int	hdr;
	fdu_t	fdu;
	fdcu_t	fdcu = dev->id_unit;
	fdc_p	fdc = fdc_data + fdcu;
	fd_p	fd;
	int	fdsu;
	struct isa_device *fdup;

	fdc->fdcu = fdcu;
	fdc->flags |= FDC_ATTACHED;
	fdc->dmachan = dev->id_drq;
	fdc->state = DEVIDLE;
	hdr = 0;
	printf("fdc%d:", fdcu);

	/* check for each floppy drive */
	for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) {
		if (fdup->id_iobase != dev->id_iobase)
			continue;
		fdu = fdup->id_unit;
		fd = &fd_data[fdu];
		if (fdu >= (NFD+NFT))
			continue;
		fdsu = fdup->id_physid;
				/* look up what bios thinks we have */
		switch (fdu) {
			case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
				break;
			case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
				break;
			default: fdt = RTCFDT_NONE;
				break;
		}
		/* is there a unit? */
		if ((fdt == RTCFDT_NONE)
#if NFT > 0
		|| (fdsu >= DRVS_PER_CTLR)) {
#else
		) {
			fd->type = NO_TYPE;
#endif
#if NFT > 0
				/* If BIOS says no floppy, or > 2nd device */
				/* Probe for and attach a floppy tape.     */
			if (ftattach(dev, fdup))
				continue;
			if (fdsu < DRVS_PER_CTLR) 
				fd->type = NO_TYPE;
#endif
			continue;
		}

#ifdef notyet
		/* select it */
		fd_turnon1(fdu);
		spinwait(1000);	/* 1 sec */
		out_fdc(fdcu,NE7CMD_RECAL);	/* Recalibrate Function */
		out_fdc(fdcu,fdsu);
		spinwait(1000);	/* 1 sec */

		/* anything responding */
		out_fdc(fdcu,NE7CMD_SENSEI);
		st0 = in_fdc(fdcu);
		cyl = in_fdc(fdcu);
		if (st0 & 0xd0)
			continue;

#endif
		fd->track = -2;
		fd->fdc = fdc;
		fd->fdsu = fdsu;
		printf(" [%d: fd%d: ", fdsu, fdu);
		
		switch (fdt) {
		case RTCFDT_12M:
			printf("1.2MB 5.25in]");
			fd->type = FD_1200;
			break;
		case RTCFDT_144M:
			printf("1.44MB 3.5in]");
			fd->type = FD_1440;
			break;
		case RTCFDT_360K:
			printf("360KB 5.25in]");
			fd->type = FD_360;
			break;
		case RTCFDT_720K:
			printf("720KB 3.5in]");
			fd->type = FD_720;
			break;
		default:
			printf("unknown]");
			fd->type = NO_TYPE;
			break;
		}

		fd_turnoff((caddr_t)fdu);
		hdr = 1;
	}
	printf("\n");

	/* Set transfer to 500kbps */
	outb(fdc->baseport+fdctl,0); /*XXX*/
	return 1;
}

int
fdsize(dev)
	dev_t	dev;
{
	return(0);
}

/****************************************************************************/
/*                               fdstrategy                                 */
/****************************************************************************/
void fdstrategy(struct buf *bp)
{
	register struct buf *dp,*dp0,*dp1;
	long nblocks,blknum;
 	int	s;
 	fdcu_t	fdcu;
 	fdu_t	fdu;
 	fdc_p	fdc;
 	fd_p	fd;

 	fdu = FDUNIT(minor(bp->b_dev));
	fd = &fd_data[fdu];
	fdc = fd->fdc;
	fdcu = fdc->fdcu;

#if NFT > 0
	/* check for controller already busy with tape */
	if (fdc->flags & FDC_TAPE_BUSY) {
		bp->b_error = EBUSY;
		bp->b_flags |= B_ERROR;
		return; 
		}
#endif
	if ((fdu >= NFD) || (bp->b_blkno < 0)) {
		printf("fdstrat: fdu = %d, blkno = %d, bcount = %d\n",
			fdu, bp->b_blkno, bp->b_bcount);
		pg("fd:error in fdstrategy");
		bp->b_error = EINVAL;
		bp->b_flags |= B_ERROR;
		goto bad;
	}
	/*
	 * Set up block calculations.
	 */
	blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/FDBLK;
 	nblocks = fd->ft->size;
	if (blknum + (bp->b_bcount / FDBLK) > nblocks) {
		if (blknum == nblocks) {
			bp->b_resid = bp->b_bcount;
		} else {
			bp->b_error = ENOSPC;
			bp->b_flags |= B_ERROR;
		}
		goto bad;
	}
 	bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads);
	bp->b_pblkno = bp->b_blkno;
	dp = &(fdc->head);
	s = splbio();
	disksort(dp, bp);
	untimeout((timeout_func_t)fd_turnoff, (caddr_t)fdu); /* a good idea */
	fdstart(fdcu);
	splx(s);
	return;

bad:
	biodone(bp);
	return;
}

/****************************************************************************/
/*                            motor control stuff                           */
/*		remember to not deselect the drive we're working on         */
/****************************************************************************/
void
set_motor(fdcu, fdu, reset)
	fdcu_t fdcu;
	fdu_t fdu;
	int reset;
{
	int m0,m1;
	int selunit;
	fd_p fd;
	if(fd = fdc_data[fdcu].fd)/* yes an assign! */
	{
		selunit =  fd->fdsu;
	}
	else
	{
		selunit = 0;
	}
	m0 = fd_data[fdcu * DRVS_PER_CTLR + 0].flags & FD_MOTOR;
	m1 = fd_data[fdcu * DRVS_PER_CTLR + 1].flags & FD_MOTOR;
	outb(fdc_data[fdcu].baseport+fdout,
		selunit
		| (reset ? 0 : (FDO_FRST|FDO_FDMAEN))
		| (m0 ? FDO_MOEN0 : 0)
		| (m1 ? FDO_MOEN1 : 0));
	TRACE1("[0x%x->fdout]",(
		selunit
		| (reset ? 0 : (FDO_FRST|FDO_FDMAEN))
		| (m0 ? FDO_MOEN0 : 0)
		| (m1 ? FDO_MOEN1 : 0)));
}

static void
fd_turnoff(caddr_t arg1)
{
	fdu_t fdu = (fdu_t)arg1;
	int	s;

	fd_p fd = fd_data + fdu;
	s = splbio();
	fd->flags &= ~FD_MOTOR;
	set_motor(fd->fdc->fdcu,fd->fdsu,0);
	splx(s);
}

void
fd_motor_on(caddr_t arg1)
{
	fdu_t fdu = (fdu_t)arg1;
	int	s;

	fd_p fd = fd_data + fdu;
	s = splbio();
	fd->flags &= ~FD_MOTOR_WAIT;
	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
	{
		fdintr(fd->fdc->fdcu);
	}
	splx(s);
}

static void fd_turnon1(fdu_t);

void
fd_turnon(fdu) 
	fdu_t fdu;
{
	fd_p fd = fd_data + fdu;
	if(!(fd->flags & FD_MOTOR))
	{
		fd_turnon1(fdu);
		fd->flags |= FD_MOTOR_WAIT;
		timeout((timeout_func_t)fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */
	}
}

static void
fd_turnon1(fdu_t fdu) 
{
	fd_p fd = fd_data + fdu;
	fd->flags |= FD_MOTOR;
	set_motor(fd->fdc->fdcu,fd->fdsu,0);
}

/****************************************************************************/
/*                             fdc in/out                                   */
/****************************************************************************/
int
in_fdc(fdcu)
	fdcu_t fdcu;
{
	int baseport = fdc_data[fdcu].baseport;
	int i, j = 100000;
	while ((i = inb(baseport+fdsts) & (NE7_DIO|NE7_RQM))
		!= (NE7_DIO|NE7_RQM) && j-- > 0)
		if (i == NE7_RQM) return -1;
	if (j <= 0)
		return(-1);
#ifdef	DEBUG
	i = inb(baseport+fddata);
	TRACE1("[fddata->0x%x]",(unsigned char)i);
	return(i);
#else
	return inb(baseport+fddata);
#endif
}

int
out_fdc(fdcu, x)
	fdcu_t fdcu;
	int x;
{
	int baseport = fdc_data[fdcu].baseport;
	int i;

	/* Check that the direction bit is set */
	i = 100000;
	while ((inb(baseport+fdsts) & NE7_DIO) && i-- > 0);
	if (i <= 0) return (-1);	/* Floppy timed out */

	/* Check that the floppy controller is ready for a command */
	i = 100000;
	while ((inb(baseport+fdsts) & NE7_RQM) == 0 && i-- > 0);
	if (i <= 0) return (-1);	/* Floppy timed out */

	/* Send the command and return */
	outb(baseport+fddata,x);
	TRACE1("[0x%x->fddata]",x);
	return (0);
}

/****************************************************************************/
/*                           fdopen/fdclose                                 */
/****************************************************************************/
int
Fdopen(dev, flags)
	dev_t	dev;
	int	flags;
{
 	fdu_t fdu = FDUNIT(minor(dev));
	int type = FDTYPE(minor(dev));
	fdc_p	fdc;

#if NFT > 0
	/* check for a tape open */
	if (type & F_TAPE_TYPE)
		return(ftopen(dev, flags));
#endif
	/* check bounds */
	if (fdu >= NFD) 
		return(ENXIO);
	fdc = fd_data[fdu].fdc;
	if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE))
		return(ENXIO);
	if (type > NUMDENS)
		return(ENXIO);
	if (type == 0)
		type = fd_data[fdu].type;
	else {
		if (type != fd_data[fdu].type) {
			switch (fd_data[fdu].type) {
			case FD_360:
				return(ENXIO);
			case FD_720:
				if (   type != FD_820
				    && type != FD_800
				   )
					return(ENXIO);
				break;
			case FD_1200:
				switch (type) {
				case FD_1480:
					type = FD_1480in5_25;
					break;
				case FD_1440:
					type = FD_1440in5_25;
					break;
				case FD_820:
					type = FD_820in5_25;
					break;
				case FD_800:
					type = FD_800in5_25;
					break;
				case FD_720:
					type = FD_720in5_25;
					break;
				case FD_360:
					type = FD_360in5_25;
					break;
				default:
					return(ENXIO);
				}
				break;
			case FD_1440:
				if (   type != FD_1720
				    && type != FD_1480
				    && type != FD_1200
				    && type != FD_820
				    && type != FD_800
				    && type != FD_720
				    )
					return(ENXIO);
				break;
			}
		}
	}
	fd_data[fdu].ft = fd_types + type - 1;
	fd_data[fdu].flags |= FD_OPEN;

	return 0;
}

int
fdclose(dev, flags)
	dev_t dev;
	int flags;
{
 	fdu_t fdu = FDUNIT(minor(dev));
	int type = FDTYPE(minor(dev));

#if NFT > 0
	if (type & F_TAPE_TYPE)
		return ftclose(0);
#endif
	fd_data[fdu].flags &= ~FD_OPEN;
	return(0);
}


/***************************************************************\
*				fdstart				*
* We have just queued something.. if the controller is not busy	*
* then simulate the case where it has just finished a command	*
* So that it (the interrupt routine) looks on the queue for more*
* work to do and picks up what we just added.			*
* If the controller is already busy, we need do nothing, as it	*
* will pick up our work when the present work completes		*
\***************************************************************/
static void
fdstart(fdcu)
	fdcu_t fdcu;
{
	register struct buf *dp,*bp;
	int s;
 	fdu_t fdu;

	s = splbio();
	if(fdc_data[fdcu].state == DEVIDLE)
	{
		fdintr(fdcu);
	}
	splx(s);
}

static void
fd_timeout(caddr_t arg1)
{
	fdcu_t fdcu = (fdcu_t)arg1;
	fdu_t fdu = fdc_data[fdcu].fdu;
	int st0, st3, cyl;
	struct buf *dp,*bp;
	int	s;

	dp = &fdc_data[fdcu].head;
	s = splbio();
	bp = dp->b_actf;

	out_fdc(fdcu,NE7CMD_SENSED);
	out_fdc(fdcu,fd_data[fdu].hddrv);
	st3 = in_fdc(fdcu);

	out_fdc(fdcu,NE7CMD_SENSEI);
	st0 = in_fdc(fdcu);
	cyl = in_fdc(fdcu);
	printf("fd%d: Operation timeout ST0 %b cyl %d ST3 %b\n",
			fdu,
			st0,
			NE7_ST0BITS,
			cyl,
			st3,
			NE7_ST3BITS);

	if (bp)
	{
		retrier(fdcu);
		fdc_data[fdcu].status[0] = 0xc0;
		fdc_data[fdcu].state = IOTIMEDOUT;
		if( fdc_data[fdcu].retry < 6)
			fdc_data[fdcu].retry = 6;
	}
	else
	{
		fdc_data[fdcu].fd = (fd_p) 0;
		fdc_data[fdcu].fdu = -1;
		fdc_data[fdcu].state = DEVIDLE;
	}
	fdintr(fdcu);
	splx(s);
}

/* just ensure it has the right spl */
static void
fd_pseudointr(caddr_t arg1, int arg2)
{
	fdcu_t fdcu = (fdcu_t)arg1;
	int	s;
	s = splbio();
	fdintr(fdcu);
	splx(s);
}

/***********************************************************************\
*                                 fdintr				*
* keep calling the state machine until it returns a 0			*
* ALWAYS called at SPLBIO 						*
\***********************************************************************/
void
fdintr(fdcu_t fdcu)
{
	fdc_p fdc = fdc_data + fdcu;
#if NFT > 0
	fdu_t fdu = fdc->fdu;

	if (fdc->flags & FDC_TAPE_BUSY)
		(ftintr(fdu));
	else
#endif
	while(fdstate(fdcu, fdc))
	  ;
}

/***********************************************************************\
* The controller state machine.						*
* if it returns a non zero value, it should be called again immediatly	*
\***********************************************************************/
int
fdstate(fdcu, fdc)
	fdcu_t fdcu;
	fdc_p fdc;
{
	int read, format, head, trac, sec = 0, i = 0, s, sectrac, cyl, st0;
	unsigned long blknum;
	fdu_t fdu = fdc->fdu;
	fd_p fd;
	register struct buf *dp,*bp;
	struct fd_formb *finfo = NULL;

	dp = &(fdc->head);
	bp = dp->b_actf;
	if(!bp) 
	{
		/***********************************************\
		* nothing left for this controller to do	*
		* Force into the IDLE state,			*
		\***********************************************/
		fdc->state = DEVIDLE;
		if(fdc->fd)
		{
			printf("unexpected valid fd pointer (fdu = %d)\n"
						,fdc->fdu);
			fdc->fd = (fd_p) 0;
			fdc->fdu = -1;
		}
		TRACE1("[fdc%d IDLE]",fdcu);
 		return(0);
	}
	fdu = FDUNIT(minor(bp->b_dev));
	fd = fd_data + fdu;
	if (fdc->fd && (fd != fdc->fd))
	{
		printf("confused fd pointers\n");
	}
	read = bp->b_flags & B_READ;
	format = bp->b_flags & B_FORMAT;
	if(format)
		finfo = (struct fd_formb *)bp->b_un.b_addr;
	TRACE1("fd%d",fdu);
	TRACE1("[%s]",fdstates[fdc->state]);
	TRACE1("(0x%x)",fd->flags);
	untimeout((timeout_func_t)fd_turnoff, (caddr_t)fdu);
	timeout((timeout_func_t)fd_turnoff, (caddr_t)fdu, 4 * hz);
	switch (fdc->state)
	{
	case DEVIDLE:
	case FINDWORK:	/* we have found new work */
		fdc->retry = 0;
		fd->skip = 0;
		fdc->fd = fd;
		fdc->fdu = fdu;
		outb(fdc->baseport+fdctl, fd->ft->trans);
		/*******************************************************\
		* If the next drive has a motor startup pending, then	*
		* it will start up in it's own good time		*
		\*******************************************************/
		if(fd->flags & FD_MOTOR_WAIT)
		{
			fdc->state = MOTORWAIT;
			return(0); /* come back later */
		}
		/*******************************************************\
		* Maybe if it's not starting, it SHOULD be starting	*
		\*******************************************************/
		if (!(fd->flags & FD_MOTOR))
		{
			fdc->state = MOTORWAIT;
			fd_turnon(fdu);
			return(0);
		}
		else	/* at least make sure we are selected */
		{
			set_motor(fdcu,fd->fdsu,0);
		}
		fdc->state = DOSEEK;
		break;
	case DOSEEK:
		if (bp->b_cylin == fd->track)
		{
			fdc->state = SEEKCOMPLETE;
			break;
		}
		out_fdc(fdcu,NE7CMD_SEEK);	/* Seek function */
		out_fdc(fdcu,fd->fdsu);		/* Drive number */
		out_fdc(fdcu,bp->b_cylin * fd->ft->steptrac);
		fd->track = -2;
		fdc->state = SEEKWAIT;
		timeout((timeout_func_t)fd_timeout, (caddr_t)fdcu, 2 * hz);
		return(0);	/* will return later */
	case SEEKWAIT:
		untimeout((timeout_func_t)fd_timeout, (caddr_t)fdcu);
		/* allow heads to settle */
		timeout((timeout_func_t)fd_pseudointr, (caddr_t)fdcu, hz / 50);
		fdc->state = SEEKCOMPLETE;
		return(0);	/* will return later */
		break;
		
	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
		/* Make sure seek really happened*/
		if(fd->track == -2)
		{
			int descyl = bp->b_cylin * fd->ft->steptrac;
			out_fdc(fdcu,NE7CMD_SENSEI);
			i = in_fdc(fdcu);
			cyl = in_fdc(fdcu);
			if (cyl != descyl)
			{
				printf("fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
				fdu, descyl, cyl, i, NE7_ST0BITS);
				return(retrier(fdcu));
			}
		}

		fd->track = bp->b_cylin;
		if(format)
			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
				- (char *)finfo;
		isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip,
			format ? bp->b_bcount : FDBLK, fdc->dmachan);
		blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/FDBLK
			+ fd->skip/FDBLK;
		sectrac = fd->ft->sectrac;
		sec = blknum %  (sectrac * fd->ft->heads);
		head = sec / sectrac;
		sec = sec % sectrac + 1;
/*XXX*/		fd->hddrv = ((head&1)<<2)+fdu;

		if(format)
		{
			/* formatting */
			out_fdc(fdcu,/* NE7CMD_FORMAT */ 0x4d);
			out_fdc(fdcu,head << 2 | fdu);
			out_fdc(fdcu,finfo->fd_formb_secshift);
			out_fdc(fdcu,finfo->fd_formb_nsecs);
			out_fdc(fdcu,finfo->fd_formb_gaplen);
			out_fdc(fdcu,finfo->fd_formb_fillbyte);
		}			
		else
		{
			if (read)
			{
				out_fdc(fdcu,NE7CMD_READ);      /* READ */
			}
			else
			{
				out_fdc(fdcu,NE7CMD_WRITE);     /* WRITE */
			}
			out_fdc(fdcu,head << 2 | fdu);  /* head & unit */
			out_fdc(fdcu,fd->track);        /* track */
			out_fdc(fdcu,head);
			out_fdc(fdcu,sec);              /* sector XXX +1? */
			out_fdc(fdcu,fd->ft->secsize);  /* sector size */
			out_fdc(fdcu,sectrac);          /* sectors/track */
			out_fdc(fdcu,fd->ft->gap);      /* gap size */
			out_fdc(fdcu,fd->ft->datalen);  /* data length */
		}
		fdc->state = IOCOMPLETE;
		timeout((timeout_func_t)fd_timeout, (caddr_t)fdcu, 2 * hz);
		return(0);	/* will return later */
	case IOCOMPLETE: /* IO DONE, post-analyze */
		untimeout((timeout_func_t)fd_timeout, (caddr_t)fdcu);
		for(i=0;i<7;i++)
		{
			fdc->status[i] = in_fdc(fdcu);
		}
	case IOTIMEDOUT: /*XXX*/
		isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip,
			format ? bp->b_bcount : FDBLK, fdc->dmachan);
		if (fdc->status[0]&0xF8)
		{
                        if (fdc->status[1] & 0x10) {
                                /*
				 * Operation not completed in reasonable time.
				 * Just restart it, don't increment retry count.
				 * (vak)
                                 */
                                fdc->state = SEEKCOMPLETE;
                                return (1);
                        }
			return(retrier(fdcu));
		}
		/* All OK */
		fd->skip += FDBLK;
		if (!format && fd->skip < bp->b_bcount)
		{
			/* set up next transfer */
			blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/FDBLK
				+ fd->skip/FDBLK;
			bp->b_cylin = (blknum / (fd->ft->sectrac * fd->ft->heads));
			fdc->state = DOSEEK;
		}
		else
		{
			/* ALL DONE */
			fd->skip = 0;
			bp->b_resid = 0;
			dp->b_actf = bp->b_actf;
			biodone(bp);
			fdc->fd = (fd_p) 0;
			fdc->fdu = -1;
			fdc->state = FINDWORK;
		}
		return(1);
	case RESETCTLR:
		/* Try a reset, keep motor on */
		set_motor(fdcu,fd->fdsu,1);
		DELAY(100);
		set_motor(fdcu,fd->fdsu,0);
		outb(fdc->baseport+fdctl,fd->ft->trans);
		TRACE1("[0x%x->fdctl]",fd->ft->trans);
		fdc->retry++;
		fdc->state = STARTRECAL;
		break;
	case STARTRECAL:
		out_fdc(fdcu,NE7CMD_SPECIFY); /* specify command */
		out_fdc(fdcu,0xDF);
		out_fdc(fdcu,2);
		out_fdc(fdcu,NE7CMD_RECAL);	/* Recalibrate Function */
		out_fdc(fdcu,fdu);
		fdc->state = RECALWAIT;
		return(0);	/* will return later */
	case RECALWAIT:
		/* allow heads to settle */
		timeout((timeout_func_t)fd_pseudointr, (caddr_t)fdcu, hz / 30);
		fdc->state = RECALCOMPLETE;
		return(0);	/* will return later */
	case RECALCOMPLETE:
		out_fdc(fdcu,NE7CMD_SENSEI);
		st0 = in_fdc(fdcu);
		cyl = in_fdc(fdcu);
		if (cyl != 0)
		{
			printf("fd%d: recal failed ST0 %b cyl %d\n", fdu,
				st0, NE7_ST0BITS, cyl);
			return(retrier(fdcu));
		}
		fd->track = 0;
		/* Seek (probably) necessary */
		fdc->state = DOSEEK;
		return(1);	/* will return immediatly */
	case	MOTORWAIT:
		if(fd->flags & FD_MOTOR_WAIT)
		{
			return(0); /* time's not up yet */
		}
		fdc->state = DOSEEK;
		return(1);	/* will return immediatly */
	default:
		printf("Unexpected FD int->");
		out_fdc(fdcu,NE7CMD_SENSEI);
		st0 = in_fdc(fdcu);
		cyl = in_fdc(fdcu);
		printf("ST0 = %lx, PCN = %lx\n",i,sec);
		out_fdc(fdcu,0x4A); 
		out_fdc(fdcu,fd->fdsu);
		for(i=0;i<7;i++) {
			fdc->status[i] = in_fdc(fdcu);
		}
	printf("intr status :%lx %lx %lx %lx %lx %lx %lx ",
		fdc->status[0],
		fdc->status[1],
		fdc->status[2],
		fdc->status[3],
		fdc->status[4],
		fdc->status[5],
		fdc->status[6] );
		return(0);
	}
	return(1); /* Come back immediatly to new state */
}

static int
retrier(fdcu)
	fdcu_t fdcu;
{
	fdc_p fdc = fdc_data + fdcu;
	register struct buf *dp,*bp;

	dp = &(fdc->head);
	bp = dp->b_actf;

	switch(fdc->retry)
	{
	case 0: case 1: case 2:
		fdc->state = SEEKCOMPLETE;
		break;
	case 3: case 4: case 5:
		fdc->state = STARTRECAL;
		break;
	case 6:
		fdc->state = RESETCTLR;
		break;
	case 7:
		break;
	default:
		{
			dev_t sav_b_dev = bp->b_dev;
			/* Trick diskerr */
			bp->b_dev = makedev(major(bp->b_dev), (FDUNIT(minor(bp->b_dev))<<3)|3);
			diskerr(bp, "fd", "hard error", LOG_PRINTF,
				fdc->fd->skip, (struct disklabel *)NULL);
			bp->b_dev = sav_b_dev;
			printf(" (ST0 %b ", fdc->status[0], NE7_ST0BITS);
			printf(" ST1 %b ", fdc->status[1], NE7_ST1BITS);
			printf(" ST2 %b ", fdc->status[2], NE7_ST2BITS);
			printf("cyl %d hd %d sec %d)\n",
			       fdc->status[3], fdc->status[4], fdc->status[5]);
		}
		bp->b_flags |= B_ERROR;
		bp->b_error = EIO;
		bp->b_resid = bp->b_bcount - fdc->fd->skip;
		dp->b_actf = bp->b_actf;
		fdc->fd->skip = 0;
		biodone(bp);
		fdc->state = FINDWORK;
		fdc->fd = (fd_p) 0;
		fdc->fdu = -1;
		/* XXX abort current command, if any.  */
		return(1);
	}
	fdc->retry++;
	return(1);
}

static int
fdformat(dev, finfo, p)
	dev_t dev;
	struct fd_formb *finfo;
	struct proc *p;
{
 	fdu_t	fdu;
 	fd_p	fd;

	struct buf *bp;
	int rv = 0, s;
	
 	fdu = FDUNIT(minor(dev));
	fd = &fd_data[fdu];

	/* set up a buffer header for fdstrategy() */
	bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
	if(bp == 0)
		return ENOBUFS;
	bzero((void *)bp, sizeof(struct buf));
	bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
	bp->b_proc = p;
	bp->b_dev = dev;

	/*
	 * calculate a fake blkno, so fdstrategy() would initiate a
	 * seek to the requested cylinder
	 */
	bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
		+ finfo->head * fd->ft->sectrac) * FDBLK / DEV_BSIZE;

	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
	bp->b_un.b_addr = (caddr_t)finfo;
	
	/* now do the format */
	fdstrategy(bp);

	/* ...and wait for it to complete */
	s = splbio();
	while(!(bp->b_flags & B_DONE))
	{
		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
		if(rv == EWOULDBLOCK)
			break;
	}
	splx(s);
	
	if(rv == EWOULDBLOCK)
	{
		/* timed out */
		biodone(bp);
		rv = EIO;
	}
	free(bp, M_TEMP);
	return rv;
}

/*
 * fdioctl() from jc@irbs.UUCP (John Capo)
 * i386/i386/conf.c needs to have fdioctl() declared and remove the line that
 * defines fdioctl to be enxio.
 *
 * TODO: Reformat.
 *       Think about allocating buffer off stack.
 *       Don't pass uncast 0's and NULL's to read/write/setdisklabel().
 *       Watch out for NetBSD's different *disklabel() interface.
 *
 * Added functionality for floppy formatting
 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
 */

int
fdioctl (dev, cmd, addr, flag, p)
	dev_t dev;
	int cmd;
	caddr_t addr;
	int flag;
	struct proc *p;
{
	struct fd_type *fdt;
	struct disklabel *dl;
	char buffer[DEV_BSIZE];
	int error;

#if NFT > 0
	int type = FDTYPE(minor(dev));

	/* check for a tape ioctl */
	if (type & F_TAPE_TYPE)
		return ftioctl(dev, cmd, addr, flag, p);
#endif

	error = 0;

	switch (cmd)
	{
	case DIOCGDINFO:
		bzero(buffer, sizeof (buffer));
		dl = (struct disklabel *)buffer;
		dl->d_secsize = FDBLK;
		fdt = fd_data[FDUNIT(minor(dev))].ft;
		dl->d_secpercyl = fdt->size / fdt->tracks;
		dl->d_type = DTYPE_FLOPPY;

		if (readdisklabel(dev, fdstrategy, dl, NULL, 0, 0) == NULL)
			error = 0;
		else
			error = EINVAL;

		*(struct disklabel *)addr = *dl;
		break;

	case DIOCSDINFO:
		if ((flag & FWRITE) == 0)
			error = EBADF;
		break;

	case DIOCWLABEL:
		if ((flag & FWRITE) == 0)
			error = EBADF;
		break;

	case DIOCWDINFO:
		if ((flag & FWRITE) == 0)
		{
			error = EBADF;
			break;
		}

		dl = (struct disklabel *)addr;

		if (error = setdisklabel ((struct disklabel *)buffer,
		    dl, 0, NULL))
			break;

		error = writedisklabel(dev, fdstrategy,
			(struct disklabel *)buffer, NULL);
		break;

	case FD_FORM:
		if((flag & FWRITE) == 0)
			error = EBADF;	/* must be opened for writing */
		else if(((struct fd_formb *)addr)->format_version !=
			FD_FORMAT_VERSION)
			error = EINVAL;	/* wrong version of formatting prog */
		else
			error = fdformat(dev, (struct fd_formb *)addr, p);
		break;

	case FD_GTYPE:                  /* get drive type */
		*(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft;
		break;

	default:
		error = EINVAL;
		break;
	}
	return (error);
}

#endif
OpenPOWER on IntegriCloud