Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
/*	$NetBSD: fd.c,v 1.96 2023/06/24 05:31:04 msaitoh Exp $	*/

/*
 * Copyright (c) 1995 Leo Weppelman.
 * All rights reserved.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
 */

/*
 * This file contains a driver for the Floppy Disk Controller (FDC)
 * on the Atari TT. It uses the WD 1772 chip, modified for steprates.
 *
 * The ST floppy disk controller shares the access to the DMA circuitry
 * with other devices. For this reason the floppy disk controller makes
 * use of some special DMA accessing code.
 *
 * Interrupts from the FDC are in fact DMA interrupts which get their
 * first level handling in 'dma.c' . If the floppy driver is currently
 * using DMA the interrupt is signalled to 'fdcint'.
 *
 * TODO:
 *   - Test it with 2 drives (I don't have them)
 *   - Test it with an HD-drive (Don't have that either)
 *   - Finish ioctl's
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.96 2023/06/24 05:31:04 msaitoh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/conf.h>
#include <sys/disklabel.h>
#include <sys/disk.h>
#include <sys/dkbad.h>
#include <atari/atari/device.h>
#include <atari/atari/stalloc.h>
#include <machine/disklabel.h>
#include <machine/iomap.h>
#include <machine/mfp.h>
#include <machine/dma.h>
#include <machine/video.h>
#include <machine/cpu.h>
#include <atari/dev/ym2149reg.h>
#include <atari/dev/fdreg.h>

#include "ioconf.h"

/*
 * Be verbose for debugging
 */
/*#define FLP_DEBUG	1 */

#define	FDC_MAX_DMA_AD	0x1000000	/* No DMA possible beyond	*/

/* Parameters for the disk drive. */
#define SECTOR_SIZE	512	/* physical sector size in bytes	*/
#define NR_DRIVES	2	/* maximum number of drives		*/
#define NR_TYPES	3	/* number of diskette/drive combinations*/
#define MAX_ERRORS	10	/* how often to try rd/wt before quitting*/
#define STEP_DELAY	6000	/* 6ms (6000us) delay after stepping	*/


#define	INV_TRK		32000	/* Should fit in unsigned short		*/
#define	INV_PART	NR_TYPES

/*
 * Driver states
 */
#define	FLP_IDLE	0x00	/* floppy is idle			*/
#define	FLP_MON		0x01	/* idle with motor on			*/
#define	FLP_STAT	0x02	/* determine floppy status		*/
#define	FLP_XFER	0x04	/* read/write data from floppy		*/

/*
 * Timer delay's
 */
#define	FLP_MONDELAY	(3 * hz)	/* motor-on delay		*/
#define	FLP_XFERDELAY	(2 * hz)	/* timeout on transfer		*/

/*
 * The density codes
 */
#define	FLP_DD		0		/* Double density		*/
#define	FLP_HD		1		/* High density			*/


#define	b_block		b_resid		/* FIXME: this is not the place	*/

/*
 * Global data for all physical floppy devices
 */
static short	selected = 0;		/* drive/head currently selected*/
static short	motoron  = 0;		/* motor is spinning		*/
static short	nopens   = 0;		/* Number of opens executed	*/

static short	fd_state = FLP_IDLE;	/* Current driver state		*/
static int	lock_stat = 0;		/* DMA locking status		*/
static short	fd_cmd   = 0;		/* command being executed	*/
static const char *fd_error = NULL;	/* error from fd_xfer_ok()	*/

/*
 * Private per device data
 */
struct fd_softc {
	device_t	sc_dev;		/* generic device info		*/
	struct disk	dkdev;		/* generic disk info		*/
	struct bufq_state *bufq;	/* queue of buf's		*/
	struct callout	sc_motor_ch;
	int		unit;		/* unit for atari controlling hw*/
	int		nheads;		/* number of heads in use	*/
	int		nsectors;	/* number of sectors/track	*/
	int		density;	/* density code			*/
	int		nblocks;	/* number of blocks on disk	*/
	int		curtrk;		/* track head positioned on	*/
	short		flags;		/* misc flags			*/
	short		part;		/* Current open partition	*/
	int		sector;		/* logical sector for I/O	*/
	uint8_t		*io_data;	/* KVA for data transfer	*/
	int		io_bytes;	/* bytes left for I/O		*/
	int		io_dir;		/* B_READ/B_WRITE		*/
	int		errcnt;		/* current error count		*/
	uint8_t		*bounceb;	/* Bounce buffer		*/

};

/*
 * Flags in fd_softc:
 */
#define FLPF_NOTRESP	0x001		/* Unit not responding		*/
#define FLPF_ISOPEN	0x002		/* Unit is open			*/
#define FLPF_SPARE	0x004		/* Not used			*/
#define FLPF_HAVELAB	0x008		/* We have a valid label	*/
#define FLPF_BOUNCE	0x010		/* Now using the bounce buffer	*/
#define FLPF_WRTPROT	0x020		/* Unit is write-protected	*/
#define FLPF_EMPTY	0x040		/* Unit is empty		*/
#define FLPF_INOPEN	0x080		/* Currently being opened	*/
#define FLPF_GETSTAT	0x100		/* Getting unit status		*/

struct fd_types {
	int		nheads;		/* Heads in use			*/
	int		nsectors;	/* sectors per track		*/
	int		nblocks;	/* number of blocks		*/
	int		density;	/* density code			*/
	const char	*descr;		/* type description		*/
} fdtypes[NR_TYPES] = {
		{ 1,  9,  720 , FLP_DD , "360KB" },	/* 360  Kb	*/
		{ 2,  9, 1440 , FLP_DD , "720KB" },	/* 720  Kb	*/
		{ 2, 18, 2880 , FLP_HD , "1.44MB" },	/* 1.44 Mb	*/
};

#define	FLP_TYPE_360	0		/* XXX: Please keep these in	*/
#define	FLP_TYPE_720	1		/* sync with the numbering in	*/
#define	FLP_TYPE_144	2		/* 'fdtypes' right above!	*/

/*
 * This is set only once at attach time. The value is determined by reading
 * the configuration switches and is one of the FLP_TYPE_*'s.
 * This is similar to the way Atari handles the _FLP cookie.
 */
static short	def_type = 0;		/* Reflects config-switches	*/

#define	FLP_DEFTYPE	1		/* 720Kb, reasonable default	*/
#define	FLP_TYPE(dev)	( DISKPART(dev) == 0 ? def_type : DISKPART(dev) - 1 )

typedef void	(*FPV)(void *);

static dev_type_open(fdopen);
static dev_type_close(fdclose);
static dev_type_read(fdread);
static dev_type_write(fdwrite);
static dev_type_ioctl(fdioctl);
static dev_type_strategy(fdstrategy);

/*
 * Private drive functions....
 */
static void	fdstart(struct fd_softc *);
static void	fddone(struct fd_softc *);
static void	fdstatus(struct fd_softc *);
static void	fd_xfer(struct fd_softc *);
static void	fdcint(struct fd_softc *);
static int	fd_xfer_ok(struct fd_softc *);
static void	fdmotoroff(struct fd_softc *);
static void	fdminphys(struct buf *);
static void	fdtestdrv(struct fd_softc *);
static void	fdgetdefaultlabel(struct fd_softc *, struct disklabel *,
		    int);
static int	fdgetdisklabel(struct fd_softc *, dev_t);
static int	fdselect(int, int, int);
static void	fddeselect(void);
static void	fdmoff(struct fd_softc *);

static u_short rd_cfg_switch(void);

static inline uint8_t	read_fdreg(u_short);
static inline void	write_fdreg(u_short, u_short);
static inline uint8_t	read_dmastat(void);

static inline
uint8_t read_fdreg(u_short regno)
{

	DMA->dma_mode = regno;
	return DMA->dma_data;
}

static inline
void write_fdreg(u_short regno, u_short val)
{

	DMA->dma_mode = regno;
	DMA->dma_data = val;
}

static inline
uint8_t read_dmastat(void)
{

	DMA->dma_mode = FDC_CS | DMA_SCREG;
	return DMA->dma_stat;
}

/*
 * Config switch stuff. Used only for the floppy type for now. That's
 * why it's here...
 * XXX: If needed in more places, it should be moved to its own include file.
 * Note: This location _must_ be read as an u_short. Failure to do so
 *       will return garbage!
 */
static u_short
rd_cfg_switch(void)
{

	return *(volatile u_short *)AD_CFG_SWITCH;
}

/*
 * Switch definitions.
 * Note: ON reads as a zero bit!
 */
#define	CFG_SWITCH_NOHD	0x4000

/*
 * Autoconfig stuff....
 */
static int	fdcmatch(device_t, cfdata_t, void *);
static int	fdcprint(void *, const char *);
static void	fdcattach(device_t, device_t, void *);

CFATTACH_DECL_NEW(fdc, 0,
    fdcmatch, fdcattach, NULL, NULL);

const struct bdevsw fd_bdevsw = {
	.d_open = fdopen,
	.d_close = fdclose,
	.d_strategy = fdstrategy,
	.d_ioctl = fdioctl,
	.d_dump = nodump,
	.d_psize = nosize,
	.d_discard = nodiscard,
	.d_flag = D_DISK
};

const struct cdevsw fd_cdevsw = {
	.d_open = fdopen,
	.d_close = fdclose,
	.d_read = fdread,
	.d_write = fdwrite,
	.d_ioctl = fdioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = nopoll,
	.d_mmap = nommap,
	.d_kqfilter = nokqfilter,
	.d_discard = nodiscard,
	.d_flag = D_DISK
};

static int
fdcmatch(device_t parent, cfdata_t match, void *aux)
{
	static int fdc_matched = 0;

	/* Match only once */
	if (strcmp("fdc", aux) || fdc_matched)
		return 0;
	fdc_matched = 1;
	return 1;
}

static void
fdcattach(device_t parent, device_t self, void *aux)
{
	struct fd_softc	fdsoftc;
	int i, nfound, first_found;

	nfound = first_found = 0;
	aprint_normal("\n");
	fddeselect();
	for (i = 0; i < NR_DRIVES; i++) {

		/*
		 * Test if unit is present
		 */
		fdsoftc.unit  = i;
		fdsoftc.flags = 0;
		st_dmagrab((dma_farg)fdcint, (dma_farg)fdtestdrv, &fdsoftc,
		    &lock_stat, 0, NULL);
		st_dmafree(&fdsoftc, &lock_stat);

		if ((fdsoftc.flags & FLPF_NOTRESP) == 0) {
			if (nfound == 0)
				first_found = i;
			nfound++;
			config_found(self, (void *)i, fdcprint, CFARGS_NONE);
		}
	}

	if (nfound != 0) {
		struct fd_softc *fdsc =
		    device_lookup_private(&fd_cd, first_found);

		/*
		 * Make sure motor will be turned of when a floppy is
		 * inserted in the first selected drive.
		 */
		fdselect(first_found, 0, FLP_DD);
		fd_state = FLP_MON;
		callout_reset(&fdsc->sc_motor_ch, 0, (FPV)fdmotoroff, fdsc);

		/*
		 * enable disk related interrupts
		 */
		MFP->mf_ierb |= IB_DINT;
		MFP->mf_iprb  = (uint8_t)~IB_DINT;
		MFP->mf_imrb |= IB_DINT;
	}
}

static int
fdcprint(void *aux, const char *pnp)
{

	if (pnp != NULL)
		aprint_normal("fd%d at %s:", (int)aux, pnp);

	return UNCONF;
}

static int	fdmatch(device_t, cfdata_t, void *);
static void	fdattach(device_t, device_t, void *);

struct dkdriver fddkdriver = {
	.d_strategy = fdstrategy
};

CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc),
    fdmatch, fdattach, NULL, NULL);

static int
fdmatch(device_t parent, cfdata_t match, void *aux)
{

	return 1;
}

static void
fdattach(device_t parent, device_t self, void *aux)
{
	struct fd_softc	*sc;
	struct fd_types *type;
	u_short		swtch;

	sc = device_private(self);
	sc->sc_dev = self;

	callout_init(&sc->sc_motor_ch, 0);

	/*
	 * Find out if an Ajax chip might be installed. Set the default
	 * floppy type accordingly.
	 */
	swtch    = rd_cfg_switch();
	def_type = (swtch & CFG_SWITCH_NOHD) ? FLP_TYPE_720 : FLP_TYPE_144;
	type     = &fdtypes[def_type];

	aprint_normal(": %s %d cyl, %d head, %d sec\n", type->descr,
	    type->nblocks / (type->nsectors * type->nheads), type->nheads,
	    type->nsectors);

	/*
	 * Initialize and attach the disk structure.
	 */
	disk_init(&sc->dkdev, device_xname(sc->sc_dev), &fddkdriver);
	disk_attach(&sc->dkdev);
}

static int
fdioctl(dev_t dev, u_long cmd, void * addr, int flag, struct lwp *l)
{
	struct fd_softc *sc;
	int error;

	sc = device_lookup_private(&fd_cd, DISKUNIT(dev));

	if ((sc->flags & FLPF_HAVELAB) == 0)
		return EBADF;

	error = disk_ioctl(&sc->dkdev, RAW_PART, cmd, addr, flag, l);
	if (error != EPASSTHROUGH)
		return error;

	switch (cmd) {
	case DIOCSBAD:
		return EINVAL;
#ifdef notyet /* XXX LWP */
	case DIOCSRETRIES:
	case DIOCSSTEP:
	case DIOCSDINFO:
	case DIOCWDINFO:
	case DIOCWLABEL:
		break;
#endif /* notyet */
	case DIOCGDEFLABEL:
		fdgetdefaultlabel(sc, (struct disklabel *)addr, RAW_PART);
		return 0;
	}
	return ENOTTY;
}

/*
 * Open the device. If this is the first open on both the floppy devices,
 * initialize the controller.
 * Note that partition info on the floppy device is used to distinguise
 * between 780Kb and 360Kb floppy's.
 *	partition 0: 360Kb
 *	partition 1: 780Kb
 */
static int
fdopen(dev_t dev, int flags, int devtype, struct lwp *l)
{
	struct fd_softc	*sc;
	int s;

#ifdef FLP_DEBUG
	printf("fdopen dev=0x%x\n", dev);
#endif

	if (FLP_TYPE(dev) >= NR_TYPES)
		return ENXIO;

	if ((sc = device_lookup_private(&fd_cd, DISKUNIT(dev))) == NULL)
		return ENXIO;

	/*
	 * If no floppy currently open, reset the controller and select
	 * floppy type.
	 */
	if (nopens == 0) {

#ifdef FLP_DEBUG
		printf("fdopen device not yet open\n");
#endif
		nopens++;
		write_fdreg(FDC_CS, IRUPT);
		delay(40);
	}

	/*
	 * Sleep while other process is opening the device
	 */
	s = splbio();
	while (sc->flags & FLPF_INOPEN)
		tsleep((void *)sc, PRIBIO, "fdopen", 0);
	splx(s);

	if ((sc->flags & FLPF_ISOPEN) == 0) {
		/*
		 * Initialise some driver values.
		 */
		int type;
		void *addr;

		type = FLP_TYPE(dev);

		bufq_alloc(&sc->bufq, "disksort", BUFQ_SORT_RAWBLOCK);
		sc->unit        = DISKUNIT(dev);
		sc->part        = RAW_PART;
		sc->nheads	= fdtypes[type].nheads;
		sc->nsectors	= fdtypes[type].nsectors;
		sc->nblocks     = fdtypes[type].nblocks;
		sc->density	= fdtypes[type].density;
		sc->curtrk	= INV_TRK;
		sc->sector	= 0;
		sc->errcnt	= 0;
		sc->bounceb	= alloc_stmem(SECTOR_SIZE, &addr);
		if (sc->bounceb == NULL)
			return ENOMEM; /* XXX */

		/*
		 * Go get write protect + loaded status
		 */
		sc->flags |= FLPF_INOPEN|FLPF_GETSTAT;
		s = splbio();
		st_dmagrab((dma_farg)fdcint, (dma_farg)fdstatus, sc,
		    &lock_stat, 0, NULL);
		while ((sc->flags & FLPF_GETSTAT) != 0)
			tsleep((void *)sc, PRIBIO, "fdopen", 0);
		splx(s);
		wakeup((void *)sc);

		if ((sc->flags & FLPF_WRTPROT) != 0 &&
		    (flags & FWRITE) != 0) {
			sc->flags = 0;
			return EPERM;
		}
		if ((sc->flags & FLPF_EMPTY) != 0) {
			sc->flags = 0;
			return ENXIO;
		}
		sc->flags &= ~(FLPF_INOPEN|FLPF_GETSTAT);
		sc->flags |= FLPF_ISOPEN;
	} else {
		/*
		 * Multiply opens are granted when accessing the same type of
		 * floppy (eq. the same partition).
		 */
		if (sc->density != fdtypes[DISKPART(dev)].density)
			return ENXIO;	/* XXX temporarily out of business */
	}
	fdgetdisklabel(sc, dev);
#ifdef FLP_DEBUG
	printf("fdopen open succeeded on type %d\n", sc->part);
#endif
	return 0;
}

static int
fdclose(dev_t dev, int flags, int devtype, struct lwp *l)
{
	struct fd_softc	*sc;

	sc = device_lookup_private(&fd_cd, DISKUNIT(dev));
	free_stmem(sc->bounceb);
	sc->flags = 0;
	nopens--;

#ifdef FLP_DEBUG
	printf("Closed floppy device -- nopens: %d\n", nopens);
#endif
	return 0;
}

static void
fdstrategy(struct buf *bp)
{
	struct fd_softc *sc;
	struct disklabel *lp;
	int s, sz;

	sc = device_lookup_private(&fd_cd, DISKUNIT(bp->b_dev));

#ifdef FLP_DEBUG
	printf("fdstrategy: %p, b_bcount: %ld\n", bp, bp->b_bcount);
#endif

	/*
	 * check for valid partition and bounds
	 */
	lp = sc->dkdev.dk_label;
	if ((sc->flags & FLPF_HAVELAB) == 0) {
		bp->b_error = EIO;
		goto done;
	}
	if (bp->b_blkno < 0 || (bp->b_bcount % SECTOR_SIZE) != 0) {
		bp->b_error = EINVAL;
		goto done;
	}
	if (bp->b_bcount == 0)
		goto done;

	sz = howmany(bp->b_bcount, SECTOR_SIZE);

	if (bp->b_blkno + sz > sc->nblocks) {
		sz = sc->nblocks - bp->b_blkno;
		if (sz == 0) /* Exactly at EndOfDisk */
			goto done;
		if (sz < 0) { /* Past EndOfDisk */
			bp->b_error = EINVAL;
			goto done;
		}
		/* Truncate it */
		if (bp->b_flags & B_RAW)
			bp->b_bcount = sz << DEV_BSHIFT;
		else
			bp->b_bcount = sz * lp->d_secsize;
	}

	/* No partition translation. */
	bp->b_rawblkno = bp->b_blkno;

	/*
	 * queue the buf and kick the low level code
	 */
	s = splbio();
	bufq_put(sc->bufq, bp);	/* XXX disksort_cylinder */
	if (!lock_stat) {
		if (fd_state & FLP_MON)
			callout_stop(&sc->sc_motor_ch);
		fd_state = FLP_IDLE;
		st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc,
		    &lock_stat, 0, NULL);
	}
	splx(s);

	return;
done:
	bp->b_resid = bp->b_bcount;
	biodone(bp);
}

static int
fdread(dev_t dev, struct uio *uio, int flags)
{

	return physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio);
}

static int
fdwrite(dev_t dev, struct uio *uio, int flags)
{

	return physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio);
}

/*
 * Called through DMA-dispatcher, get status.
 */
static void
fdstatus(struct fd_softc *sc)
{

#ifdef FLP_DEBUG
	printf("fdstatus\n");
#endif
	sc->errcnt = 0;
	fd_state   = FLP_STAT;
	fd_xfer(sc);
}

/*
 * Called through the DMA-dispatcher. So we know we are the only ones
 * messing with the floppy-controller.
 * Initialize some fields in the fdsoftc for the state-machine and get
 * it going.
 */
static void
fdstart(struct fd_softc *sc)
{
	struct buf *bp;

	bp	     = bufq_peek(sc->bufq);
	sc->sector   = bp->b_blkno;	/* Start sector for I/O		*/
	sc->io_data  = bp->b_data;	/* KVA base for I/O		*/
	sc->io_bytes = bp->b_bcount;	/* Transfer size in bytes	*/
	sc->io_dir   = bp->b_flags & B_READ;/* Direction of transfer	*/
	sc->errcnt   = 0;		/* No errors yet		*/
	fd_state     = FLP_XFER;	/* Yes, we're going to transfer	*/

	/* Instrumentation. */
	disk_busy(&sc->dkdev);

	fd_xfer(sc);
}

/*
 * The current transaction is finished (for good or bad). Let go of
 * the DMA-resources. Call biodone() to finish the transaction.
 * Find a new transaction to work on.
 */
static void
fddone(register struct fd_softc *sc)
{
	struct buf *bp;
	struct fd_softc	*sc1;
	int i, s;

	/*
	 * Give others a chance to use the DMA.
	 */
	st_dmafree(sc, &lock_stat);


	if (fd_state != FLP_STAT) {
		/*
		 * Finish current transaction.
		 */
		s = splbio();
		bp = bufq_get(sc->bufq);
		if (bp == NULL)
			panic("fddone");
		splx(s);

#ifdef FLP_DEBUG
		printf("fddone: unit: %d, buf: %p, resid: %d\n",sc->unit, bp,
		    sc->io_bytes);
#endif
		bp->b_resid = sc->io_bytes;

		disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid),
		    (bp->b_flags & B_READ));

		biodone(bp);
	}
	fd_state = FLP_MON;

	if (lock_stat)
		return;		/* XXX Is this possible?	*/

	/*
	 * Find a new transaction on round-robin basis.
	 */
	for (i = sc->unit + 1;; i++) {
		if (i >= fd_cd.cd_ndevs)
			i = 0;
		if ((sc1 = device_lookup_private(&fd_cd, i)) == NULL)
			continue;
		if (bufq_peek(sc1->bufq) != NULL)
			break;
		if (i == sc->unit) {
			callout_reset(&sc->sc_motor_ch, FLP_MONDELAY,
			    (FPV)fdmotoroff, sc);
#ifdef FLP_DEBUG
			printf("fddone: Nothing to do\n");
#endif
			return;	/* No work */
		}
	}
	fd_state = FLP_IDLE;
#ifdef FLP_DEBUG
	printf("fddone: Staring job on unit %d\n", sc1->unit);
#endif
	st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc1, &lock_stat, 0,
	    NULL);
}

static int
fdselect(int drive, int head, int dense)
{
	int i, spinning;

#ifdef FLP_DEBUG
	printf("fdselect: drive=%d, head=%d, dense=%d\n", drive, head, dense);
#endif
	i = ((drive == 1) ? PA_FLOP1 : PA_FLOP0) | head;
	spinning = motoron;
	motoron  = 1;

	switch (dense) {
	case FLP_DD:
		DMA->dma_drvmode = 0;
		break;
	case FLP_HD:
		DMA->dma_drvmode = (FDC_HDSET|FDC_HDSIG);
		break;
	default:
		panic("fdselect: unknown density code");
	}
	if (i != selected) {
		selected = i;
		ym2149_fd_select((i ^ PA_FDSEL));
	}
	return spinning;
}

static void
fddeselect(void)
{

	ym2149_fd_select(PA_FDSEL);
	motoron = selected = 0;
	DMA->dma_drvmode   = 0;
}

/****************************************************************************
 * The following functions assume to be running as a result of a            *
 * disk-interrupt (e.q. spl = splbio).				            *
 * They form the finit-state machine, the actual driver.                    *
 *                                                                          *
 *	fdstart()/ --> fd_xfer() -> activate hardware                       *
 *  fdopen()          ^                                                     *
 *                    |                                                     *
 *                    +-- not ready -<------------+                         *
 *                                                |                         *
 *  fdmotoroff()/ --> fdcint() -> fd_xfer_ok() ---+                         *
 *  h/w interrupt                 |                                         *
 *                               \|/                                        *
 *                            finished ---> fdone()                         *
 *                                                                          *
 ****************************************************************************/
static void
fd_xfer(struct fd_softc *sc)
{
	int head;
	int track, sector, hbit;
	paddr_t phys_addr;

	head = track = 0;
	switch (fd_state) {
	case FLP_XFER:
		/*
		 * Calculate head/track values
		 */
		track  = sc->sector / sc->nsectors;
		head   = track % sc->nheads;
		track  = track / sc->nheads;
#ifdef FLP_DEBUG
		printf("fd_xfer: sector:%d,head:%d,track:%d\n",
		    sc->sector, head, track);
#endif
		break;

	case FLP_STAT:
		/*
		 * FLP_STAT only wants to recalibrate
		 */
		sc->curtrk = INV_TRK;
		break;
	default:
		panic("fd_xfer: wrong state (0x%x)", fd_state);
	}

	/*
	 * Select the drive.
	 */
	hbit = fdselect(sc->unit, head, sc->density) ? HBIT : 0;

	if (sc->curtrk == INV_TRK) {
		/*
		 * Recalibrate, since we lost track of head positioning.
		 * The floppy disk controller has no way of determining its
		 * absolute arm position (track).  Instead, it steps the
		 * arm a track at a time and keeps track of where it
		 * thinks it is (in software).  However, after a SEEK, the
		 * hardware reads information from the diskette telling
		 * where the arm actually is.  If the arm is in the wrong place,
		 * a recalibration is done, which forces the arm to track 0.
		 * This way the controller can get back into sync with reality.
		 */
		fd_cmd = RESTORE;
		write_fdreg(FDC_CS, RESTORE|VBIT|hbit);
		callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
		    (FPV)fdmotoroff, sc);

#ifdef FLP_DEBUG
		printf("fd_xfer:Recalibrating drive %d\n", sc->unit);
#endif
		return;
	}

	write_fdreg(FDC_TR, sc->curtrk);

	/*
	 * Issue a SEEK command on the indicated drive unless the arm is
	 * already positioned on the correct track.
	 */
	if (track != sc->curtrk) {
		sc->curtrk = track;	/* be optimistic */
		write_fdreg(FDC_DR, track);
		write_fdreg(FDC_CS, SEEK|RATE6|VBIT|hbit);
		callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
		    (FPV)fdmotoroff, sc);
		fd_cmd = SEEK;
#ifdef FLP_DEBUG
		printf("fd_xfer:Seek to track %d on drive %d\n",
		    track, sc->unit);
#endif
		return;
	}

	/*
	 * The drive is now on the proper track. Read or write 1 block.
	 */
	sector = sc->sector % sc->nsectors;
	sector++;	/* start numbering at 1 */

	write_fdreg(FDC_SR, sector);

	phys_addr = (paddr_t)kvtop(sc->io_data);
	if (phys_addr >= FDC_MAX_DMA_AD) {
		/*
		 * We _must_ bounce this address
		 */
		phys_addr = (paddr_t)kvtop(sc->bounceb);
		if (sc->io_dir == B_WRITE)
			memcpy(sc->bounceb, sc->io_data, SECTOR_SIZE);
		sc->flags |= FLPF_BOUNCE;
	}
	st_dmaaddr_set((void *)phys_addr);	/* DMA address setup */

#ifdef FLP_DEBUG
	printf("fd_xfer:Start io (io_addr:%lx)\n", (u_long)kvtop(sc->io_data));
#endif

	if (sc->io_dir == B_READ) {
		/* Issue the command */
		st_dmacomm(DMA_FDC | DMA_SCREG, 1);
		write_fdreg(FDC_CS, F_READ|hbit);
		fd_cmd = F_READ;
	} else {
		/* Issue the command */
		st_dmacomm(DMA_WRBIT | DMA_FDC | DMA_SCREG, 1);
		write_fdreg(DMA_WRBIT | FDC_CS, F_WRITE|hbit|EBIT|PBIT);
		fd_cmd = F_WRITE;
	}
	callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY, (FPV)fdmotoroff, sc);
}

/* return values of fd_xfer_ok(): */
#define X_OK			0
#define X_AGAIN			1
#define X_ERROR			2
#define X_FAIL			3

/*
 * Hardware interrupt function.
 */
static void
fdcint(struct fd_softc *sc)
{
	struct buf *bp;

#ifdef FLP_DEBUG
	printf("fdcint: unit = %d\n", sc->unit);
#endif

	/*
	 * Cancel timeout (we made it, didn't we)
	 */
	callout_stop(&sc->sc_motor_ch);

	switch (fd_xfer_ok(sc)) {
	case X_ERROR:
		if (++sc->errcnt < MAX_ERRORS) {
			/*
			 * Command failed but still retries left.
			 */
			break;
		}
		/* FALL THROUGH */
	case X_FAIL:
		/*
		 * Non recoverable error. Fall back to motor-on
		 * idle-state.
		 */
		if (fd_error != NULL) {
			printf("Floppy error: %s\n", fd_error);
			fd_error = NULL;
		}

		if (fd_state == FLP_STAT) {
			sc->flags |= FLPF_EMPTY;
			sc->flags &= ~FLPF_GETSTAT;
			wakeup((void *)sc);
			fddone(sc);
			return;
		}

		bp = bufq_peek(sc->bufq);

		bp->b_error  = EIO;
		fd_state     = FLP_MON;

		break;
	case X_AGAIN:
		/*
		 * Start next part of state machine.
		 */
		break;
	case X_OK:
		/*
		 * Command ok and finished. Reset error-counter.
		 * If there are no more bytes to transfer fall back
		 * to motor-on idle state.
		 */
		sc->errcnt = 0;

		if (fd_state == FLP_STAT) {
			sc->flags &= ~FLPF_GETSTAT;
			wakeup((void *)sc);
			fddone(sc);
			return;
		}

		if ((sc->flags & FLPF_BOUNCE) != 0 &&
		    sc->io_dir == B_READ)
			memcpy(sc->io_data, sc->bounceb, SECTOR_SIZE);
		sc->flags &= ~FLPF_BOUNCE;

		sc->sector++;
		sc->io_data  += SECTOR_SIZE;
		sc->io_bytes -= SECTOR_SIZE;
		if (sc->io_bytes <= 0)
			fd_state = FLP_MON;
	}
	if (fd_state == FLP_MON)
		fddone(sc);
	else
		fd_xfer(sc);
}

/*
 * Determine status of last command. Should only be called through
 * 'fdcint()'.
 * Returns:
 *	X_ERROR : Error on command; might succeed next time.
 *	X_FAIL  : Error on command; will never succeed.
 *	X_AGAIN : Part of a command succeeded, call 'fd_xfer()' to complete.
 *	X_OK	: Command succeeded and is complete.
 *
 * This function only affects sc->curtrk.
 */
static int
fd_xfer_ok(register struct fd_softc *sc)
{
	int status;

#ifdef FLP_DEBUG
	printf("fd_xfer_ok: cmd: 0x%x, state: 0x%x\n", fd_cmd, fd_state);
#endif
	switch (fd_cmd) {
	case IRUPT:
		/*
		 * Timeout. Force a recalibrate before we try again.
		 */
		status = read_fdreg(FDC_CS);

		fd_error = "Timeout";
		sc->curtrk = INV_TRK;
		return X_ERROR;
	case F_READ:
		/*
		 * Test for DMA error
		 */
		status = read_dmastat();
		if ((status & DMAOK) == 0) {
			fd_error = "DMA error";
			return X_ERROR;
		}
		/*
		 * Get controller status and check for errors.
		 */
		status = read_fdreg(FDC_CS);
		if ((status & (RNF | CRCERR | LD_T00)) != 0) {
			fd_error = "Read error";
			if ((status & RNF) != 0)
				sc->curtrk = INV_TRK;
			return X_ERROR;
		}
		break;
	case F_WRITE:
		/*
		 * Test for DMA error
		 */
		status = read_dmastat();
		if ((status & DMAOK) == 0) {
			fd_error = "DMA error";
			return X_ERROR;
		}
		/*
		 * Get controller status and check for errors.
		 */
		status = read_fdreg(FDC_CS);
		if ((status & WRI_PRO) != 0) {
			fd_error = "Write protected";
			return X_FAIL;
		}
		if ((status & (RNF | CRCERR | LD_T00)) != 0) {
			fd_error = "Write error";
			sc->curtrk = INV_TRK;
			return X_ERROR;
		}
		break;
	case SEEK:
		status = read_fdreg(FDC_CS);
		if ((status & (RNF | CRCERR)) != 0) {
			fd_error = "Seek error";
			sc->curtrk = INV_TRK;
			return X_ERROR;
		}
		return X_AGAIN;
	case RESTORE:
		/*
		 * Determine if the recalibration succeeded.
		 */
		status = read_fdreg(FDC_CS);
		if ((status & RNF) != 0) {
			fd_error = "Recalibrate error";
			/* reset controller */
			write_fdreg(FDC_CS, IRUPT);
			sc->curtrk = INV_TRK;
			return X_ERROR;
		}
		sc->curtrk = 0;
		if (fd_state == FLP_STAT) {
			if ((status & WRI_PRO) != 0)
				sc->flags |= FLPF_WRTPROT;
			break;
		}
		return X_AGAIN;
	default:
		fd_error = "Driver error: fd_xfer_ok : Unknown state";
		return X_FAIL;
	}
	return X_OK;
}

/*
 * All timeouts will call this function.
 */
static void
fdmotoroff(struct fd_softc *sc)
{
	int s;

	/*
	 * Get at harware interrupt level
	 */
	s = splbio();

#if FLP_DEBUG
	printf("fdmotoroff, state = 0x%x\n", fd_state);
#endif

	switch (fd_state) {
	case FLP_STAT:
	case FLP_XFER:
		/*
		 * Timeout during a transfer; cancel transaction
		 * set command to 'IRUPT'.
		 * A drive-interrupt is simulated to trigger the state
		 * machine.
		 */
		/*
		 * Cancel current transaction
		 */
		fd_cmd = IRUPT;
		write_fdreg(FDC_CS, IRUPT);
		delay(20);
		(void)read_fdreg(FDC_CS);
		write_fdreg(FDC_CS, RESTORE);
		break;

	case FLP_MON:
		/*
		 * Turn motor off.
		 */
		if (selected) {
			int tmp;

			st_dmagrab((dma_farg)fdcint, (dma_farg)fdmoff, sc,
			    &tmp, 0, NULL);
		} else
			fd_state = FLP_IDLE;
		break;
	}
	splx(s);
}

/*
 * min byte count to whats left of the track in question
 */
static void
fdminphys(struct buf *bp)
{
	struct fd_softc	*sc;
	int sec, toff, tsz;

	if ((sc = device_lookup_private(&fd_cd, DISKUNIT(bp->b_dev))) == NULL)
		panic("fdminphys: couldn't get softc");

	sec  = bp->b_blkno % (sc->nsectors * sc->nheads);
	toff = sec * SECTOR_SIZE;
	tsz  = sc->nsectors * sc->nheads * SECTOR_SIZE;

#ifdef FLP_DEBUG
	printf("fdminphys: before %ld", bp->b_bcount);
#endif

	bp->b_bcount = uimin(bp->b_bcount, tsz - toff);

#ifdef FLP_DEBUG
	printf(" after %ld\n", bp->b_bcount);
#endif

	minphys(bp);
}

/*
 * Called from fdmotoroff to turn the motor actually off....
 * This can't be done in fdmotoroff itself, because exclusive access to the
 * DMA controller is needed to read the FDC-status register. The function
 * 'fdmoff()' always runs as the result of a 'dmagrab()'.
 * We need to test the status-register because we want to be sure that the
 * drive motor is really off before deselecting the drive. The FDC only
 * turns off the drive motor after having seen 10 index-pulses. You only
 * get index-pulses when a drive is selected....This means that if the
 * drive is deselected when the motor is still spinning, it will continue
 * to spin _even_ when you insert a floppy later on...
 */
static void
fdmoff(struct fd_softc *fdsoftc)
{
	int tmp;

	if ((fd_state == FLP_MON) && selected) {
		tmp = read_fdreg(FDC_CS);
		if ((tmp & MOTORON) == 0) {
			fddeselect();
			fd_state = FLP_IDLE;
		} else
			callout_reset(&fdsoftc->sc_motor_ch, 10 * FLP_MONDELAY,
			    (FPV)fdmotoroff, fdsoftc);
	}
	st_dmafree(fdsoftc, &tmp);
}

/*
 * Used to find out which drives are actually connected. We do this by issuing
 * is 'RESTORE' command and check if the 'track-0' bit is set. This also works
 * if the drive is present but no floppy is inserted.
 */
static void
fdtestdrv(struct fd_softc *fdsoftc)
{
	int status;

	/*
	 * Select the right unit and head.
	 */
	fdselect(fdsoftc->unit, 0, FLP_DD);

	write_fdreg(FDC_CS, RESTORE|HBIT);

	/*
	 * Wait for about 2 seconds.
	 */
	delay(2000000);

	status = read_fdreg(FDC_CS);
	if ((status & (RNF|BUSY)) != 0) {
		write_fdreg(FDC_CS, IRUPT);	/* reset controller */
		delay(40);
	}

	if ((status & LD_T00) == 0)
		fdsoftc->flags |= FLPF_NOTRESP;

	fddeselect();
}

static void
fdgetdefaultlabel(struct fd_softc *sc, struct disklabel *lp, int part)
{

	memset(lp, 0, sizeof(struct disklabel));

	lp->d_secsize     = SECTOR_SIZE;
	lp->d_ntracks     = sc->nheads;
	lp->d_nsectors    = sc->nsectors;
	lp->d_secpercyl   = lp->d_ntracks * lp->d_nsectors;
	lp->d_ncylinders  = sc->nblocks / lp->d_secpercyl;
	lp->d_secperunit  = sc->nblocks;

	lp->d_type        = DKTYPE_FLOPPY;
	lp->d_rpm         = 300;	/* good guess I suppose.	*/
	lp->d_interleave  = 1;		/* FIXME: is this OK?		*/
	lp->d_bbsize      = 0;
	lp->d_sbsize      = 0;
	lp->d_npartitions = part + 1;
	lp->d_trkseek     = STEP_DELAY;
	lp->d_magic       = DISKMAGIC;
	lp->d_magic2      = DISKMAGIC;
	lp->d_checksum    = dkcksum(lp);
	lp->d_partitions[part].p_size   = lp->d_secperunit;
	lp->d_partitions[part].p_fstype = FS_UNUSED;
	lp->d_partitions[part].p_fsize  = 1024;
	lp->d_partitions[part].p_frag   = 8;
}

/*
 * Build disk label. For now we only create a label from what we know
 * from 'sc'.
 */
static int
fdgetdisklabel(struct fd_softc *sc, dev_t dev)
{
	struct disklabel *lp;
	int part;

	/*
	 * If we already got one, get out.
	 */
	if ((sc->flags & FLPF_HAVELAB) != 0)
		return 0;

#ifdef FLP_DEBUG
	printf("fdgetdisklabel()\n");
#endif

	part = RAW_PART;
	lp   = sc->dkdev.dk_label;
	fdgetdefaultlabel(sc, lp, part);
	sc->flags |= FLPF_HAVELAB;

	return 0;
}