xref: /netbsd-src/sys/arch/vax/vsa/hdc9224.c (revision 84d0ab551791493d2630bbef27063a9d514b9108)
1 /*	$NetBSD: hdc9224.c,v 1.6 1997/03/15 16:32:22 ragge Exp $ */
2 /*
3  * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Ludd by Bertram Barth.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed at Ludd, University of
19  *	Lule}, Sweden and its contributors.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * with much help from (in alphabetical order):
37  *	Jeremy
38  *	Roger Ivie
39  *	Rick Macklem
40  *	Mike Young
41  */
42 
43 /* #define DEBUG	/* */
44 /* #define TRACE	/* */
45 static int haveLock = 0;
46 static int keepLock = 0;
47 
48 #define F_READ	11
49 #define F_WRITE 12
50 
51 #define trace(x)
52 #define debug(x)
53 
54 #include "hdc.h"
55 #if NHDC > 0
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/conf.h>
61 #include <sys/file.h>
62 #include <sys/stat.h>
63 #include <sys/ioctl.h>
64 #include <sys/buf.h>
65 #include <sys/proc.h>
66 #include <sys/user.h>
67 #include <sys/map.h>
68 #include <sys/device.h>
69 #include <sys/dkstat.h>
70 #include <sys/disklabel.h>
71 #include <sys/disk.h>
72 #include <sys/syslog.h>
73 #include <sys/reboot.h>
74 
75 #include <machine/pte.h>
76 #include <machine/sid.h>
77 #include <machine/cpu.h>
78 #include <machine/uvax.h>
79 #include <machine/ka410.h>
80 #include <machine/vsbus.h>
81 #include <machine/rpb.h>
82 
83 #include <vax/vsa/hdc9224.h>
84 
85 
86 /*
87  * some definitions
88  */
89 #define CTLRNAME  "hdc"
90 #define UNITNAME  "rd"
91 #define HDC_PRI	  LOG_INFO
92 
93 /* Bits in minor device */
94 #define HDCUNIT(dev)	DISKUNIT(dev)
95 #define HDCPART(dev)	DISKPART(dev)
96 #define HDCCTLR(dev)	0
97 #define HDCLABELDEV(dev)	(MAKEDISKDEV(major(dev),HDCUNIT(dev),RAW_PART))
98 
99 #define MAX_WAIT	(1000*1000)	/* # of loop-instructions in seconds */
100 
101 
102 /*
103  * on-disk geometry block
104  */
105 #define _aP	__attribute__ ((packed))	/* force byte-alignment */
106 struct rdgeom {
107   char mbz[10];		/* 10 bytes of zero */
108   long xbn_count _aP;	/* number of XBNs */
109   long dbn_count _aP;	/* number of DBNs */
110   long lbn_count _aP;	/* number of LBNs (Logical-Block-Numbers) */
111   long rbn_count _aP;	/* number of RBNs (Replacement-Block-Numbers) */
112   short nspt;		/* number of sectors per track */
113   short ntracks;	/* number of tracks */
114   short ncylinders;	/* number of cylinders */
115   short precomp;	/* first cylinder for write precompensation */
116   short reduced;	/* first cylinder for reduced write current */
117   short seek_rate;	/* seek rate or zero for buffered seeks */
118   short crc_eec;	/* 0 if CRC is being used or 1 if ECC is being used */
119   short rct;		/* "replacement control table" (RCT) */
120   short rct_ncopies;	/* number of copies of the RCT */
121   long	media_id _aP;	/* media identifier */
122   short interleave;	/* sector-to-sector interleave */
123   short headskew;	/* head-to-head skew */
124   short cylskew;	/* cylinder-to-cylinder skew */
125   short gap0_size;	/* size of GAP 0 in the MFM format */
126   short gap1_size;	/* size of GAP 1 in the MFM format */
127   short gap2_size;	/* size of GAP 2 in the MFM format */
128   short gap3_size;	/* size of GAP 3 in the MFM format */
129   short sync_value;	/* sync value used to start a track when formatting */
130   char	reserved[32];	/* reserved for use by the RQDX1/2/3 formatter */
131   short serial_number;	/* serial number */
132 #if 0	/* we don't need these 412 useless bytes ... */
133   char	fill[412-2];	/* Filler bytes to the end of the block */
134   short checksum;	/* checksum over the XBN */
135 #endif
136 };
137 
138 /*
139  * Software status
140  */
141 struct	rdsoftc {
142 	struct device	sc_dev;		/* must be here! (pseudo-OOP:) */
143 	struct disk	sc_dk;		/* disklabel etc. */
144 	struct rdgeom	sc_xbn;		/* on-disk geometry information */
145 	struct rdparams {
146 		u_short cylinders;	/* number of cylinders */
147 		u_char	heads;		/* number of heads (tracks) */
148 		u_char	sectors;	/* number of sectors/track */
149 		u_long	diskblks;	/* number of sectors/disk */
150 		u_long	disklbns;	/* number of available sectors */
151 		u_long	blksize;	/* number of bytes/sector */
152 		u_long	diskbytes;	/* number of bytes/disk */
153 		char	diskname[8];
154 	} sc_param;
155 	int	sc_drive;		/* physical unit number */
156 	int	sc_flags;
157 	int	sc_state;
158 	int	sc_mode;
159 };
160 
161 struct	hdcsoftc {
162 	struct device sc_dev;		/* must be here (pseudo-OOP:) */
163 	struct hdc9224_DKCreg *sc_dkc;	/* I/O address of the controller */
164 	struct hdc9224_UDCreg sc_creg;	/* (command) registers to be written */
165 	struct hdc9224_UDCreg sc_sreg;	/* (status) registers being read */
166 	struct confargs *sc_cfargs;	/* remember args being probed with */
167 	char	*sc_dmabase;		/* */
168 	long	sc_dmasize;		/* */
169 	long	sc_ioaddr;		/* unmapped I/O address */
170 	long	sc_ivec;		/* interrupt vector address */
171 	short	sc_ibit;		/* bit-value in interrupt register */
172 	short	sc_status;		/* copy of status register */
173 	short	sc_state;
174 	short	sc_flags;
175 	short	sc_errors;
176 };
177 
178 /*
179  * Device definition for (new) autoconfiguration.
180  */
181 int	hdcmatch  __P((struct device *parent, void *cfdata, void *aux));
182 void	hdcattach __P((struct device *parent, struct device *self, void *aux));
183 int	hdcprint  __P((void *aux, const char *name));
184 
185 struct	cfdriver hdc_cd = {
186 	NULL, "hdc", DV_DULL
187 };
188 struct	cfattach hdc_ca = {
189 	sizeof(struct hdcsoftc), hdcmatch, hdcattach
190 };
191 
192 int	rdmatch __P((struct device *parent, void *cfdata, void *aux));
193 void	rdattach __P((struct device *parent, struct device *self, void *aux));
194 int	rdprint __P((void *aux, const char *name));
195 void	rdstrategy __P((struct buf *bp));
196 
197 struct	cfdriver rd_cd = {
198 	NULL, "rd", DV_DISK
199 };
200 struct	cfattach rd_ca = {
201 	sizeof(struct rdsoftc), rdmatch, rdattach
202 };
203 
204 struct dkdriver rddkdriver = { rdstrategy };
205 
206 /*
207  * prototypes for (almost) all the internal routines
208  */
209 int hdc_reset	__P((struct hdcsoftc *sc));
210 int hdc_select	__P((struct hdcsoftc *sc, int drive));
211 int hdc_command __P((struct hdcsoftc *sc, int cmd));
212 
213 int hdc_getdata	 __P((struct hdcsoftc *hdc, struct rdsoftc *rd, int drive));
214 int hdc_getlabel __P((struct hdcsoftc *hdc, struct rdsoftc *rd, int drive));
215 
216 void rdgetlabel __P((struct rdsoftc *sc));
217 
218 /*
219  * new-config's hdcmatch() is similiar to old-config's hdcprobe(),
220  * thus we probe for the existence of the controller and reset it.
221  * NB: we can't initialize the controller yet, since space for hdcsoftc
222  *     is not yet allocated. Thus we do this in hdcattach()...
223  */
224 int
225 hdcmatch(parent, match, aux)
226 	struct device *parent;
227 	void *match, *aux;
228 {
229 	struct cfdata *cf = match;
230 	struct confargs *ca = aux;
231 
232 	trace(("hdcmatch(0x%x, %d, %s)\n", parent, cf->cf_unit, ca->ca_name));
233 
234 	if (strcmp(ca->ca_name, "hdc") &&
235 	    strcmp(ca->ca_name, "hdc9224") &&
236 	    strcmp(ca->ca_name, "HDC9224"))
237 		return (0);
238 
239 	/*
240 	 * only(?) VS2000/KA410 has exactly one HDC9224 controller
241 	 */
242 	if (vax_boardtype != VAX_BTYP_410) {
243 		printf ("unexpected boardtype 0x%x in hdcmatch()\n",
244 			vax_boardtype);
245 		return (0);
246 	}
247 	if (cf->cf_unit != 0)
248 		return (0);
249 
250 	return (1);
251 }
252 
253 struct hdc_attach_args {
254 	int ha_drive;
255 };
256 
257 int
258 rdprint(aux, name)
259 	void *aux;
260 	const char *name;
261 {
262 	struct hdc_attach_args *ha = aux;
263 
264 	trace(("rdprint(%d, %s)\n", ha->ha_drive, name));
265 
266 	if (!name)
267 		printf (" drive %d", ha->ha_drive);
268 	return (QUIET);
269 }
270 
271 /*
272  * hdc_attach() probes for all possible devices
273  */
274 void
275 hdcattach(parent, self, aux)
276 	struct device *parent, *self;
277 	void *aux;
278 {
279 	struct hdcsoftc *sc = (void*)self;
280 	struct confargs *ca = aux;
281 	struct hdc_attach_args ha;
282 
283 	trace(("hdcattach(0x%x, 0x%x, %s)\n", parent, self, ca->ca_name));
284 
285 	printf ("\n");
286 	/*
287 	 * first reset/initialize the controller
288 	 */
289 	sc->sc_cfargs = ca;
290 
291 	sc->sc_ioaddr = ca->ca_ioaddr;
292 	sc->sc_dkc = (void*)uvax_phys2virt(sc->sc_ioaddr);
293 	sc->sc_ibit = ca->ca_intbit;
294 	sc->sc_ivec = ca->ca_intvec;
295 	sc->sc_status = 0;
296 	sc->sc_state = 0;
297 	sc->sc_flags = 0;
298 	sc->sc_errors = 0;
299 
300 	sc->sc_dkc     = (void*)uvax_phys2virt(KA410_DKC_BASE);
301 	sc->sc_dmabase = (void*)uvax_phys2virt(KA410_DMA_BASE);
302 	sc->sc_dmasize = KA410_DMA_SIZE;
303 
304 	if (hdc_reset(sc) != 0) {
305 		delay(500*1000);	/* wait .5 seconds */
306 		if (hdc_reset(sc) != 0)
307 			printf ("problems with hdc_reset()...\n");
308 	}
309 
310 	/*
311 	 * now probe for all possible disks
312 	 */
313 	for (ha.ha_drive=0; ha.ha_drive<3; ha.ha_drive++)
314 		(void)config_found(self, (void*)&ha, rdprint);
315 
316 #ifdef notyet
317 	/*
318 	 * now that probing is done, we can register and enable interrupts
319 	 */
320 	vsbus_intr_register(XXX);
321 	vsbus_intr_enable(XXX);
322 #endif
323 }
324 
325 /*
326  * rdmatch() probes for the existence of a RD-type disk/floppy
327  */
328 int
329 rdmatch(parent, match, aux)
330 	struct device *parent;
331 	void *match, *aux;
332 {
333 	struct hdcsoftc *hdc = (void*)parent;
334 	struct cfdata *cf = match;
335 	struct hdc_attach_args *ha = aux;
336 	int drive = ha->ha_drive;
337 	int res;
338 
339 	trace(("rdmatch(%d, %d)\n", cf->cf_unit, drive));
340 
341 	if (cf->cf_unit != ha->ha_drive)
342 		return (0);
343 
344 	switch (drive) {
345 	case 0:
346 	case 1:
347 	case 2:
348 		res = hdc_select(hdc, drive);
349 		break;
350 	default:
351 		printf ("rdmatch: invalid unit-number %d\n", drive);
352 		return (0);
353 	}
354 
355 	debug (("cstat: %x dstat: %x\n", hdc->sc_sreg.udc_cstat,
356 		hdc->sc_sreg.udc_dstat));
357 	if (drive == 1)
358 	  return (0);	/* XXX */
359 
360 	return (1);
361 }
362 
363 void
364 rdattach(parent, self, aux)
365 	struct device *parent, *self;
366 	void *aux;
367 {
368 	struct hdcsoftc *hdc = (void*)parent;
369 	struct rdsoftc *rd = (void*)self;
370 	struct hdc_attach_args *ha = aux;
371 	struct rdparams *rp = &rd->sc_param;
372 
373 	trace(("rdattach(%d)\n", ha->ha_drive));
374 
375 	rd->sc_drive = ha->ha_drive;
376 	/*
377 	 * Initialize and attach the disk structure.
378 	 */
379 	rd->sc_dk.dk_driver = &rddkdriver;
380 	rd->sc_dk.dk_name = rd->sc_dev.dv_xname;
381 	disk_attach(&rd->sc_dk);
382 	/*
383 	 * if it's not a floppy then evaluate the on-disk geometry.
384 	 * if neccessary correct the label...
385 	 */
386 	printf("\n%s: ", rd->sc_dev.dv_xname);
387 	if (rd->sc_drive == 2) {
388 		printf("floppy (RX33)\n");
389 	}
390 	else {
391 		hdc_getdata(hdc, rd, rd->sc_drive);
392 		printf("%s, %d MB, %d LBN, %d cyl, %d head, %d sect/track\n",
393 		       rp->diskname, rp->diskblks/2048, rp->disklbns,
394 		       rp->cylinders, rp->heads, rp->sectors);
395 	}
396 	/*
397 	 * Know where we booted from.
398 	 */
399 	if ((B_TYPE(bootdev) == BDEV_RD) && (rd->sc_drive == B_UNIT(bootdev)))
400 		booted_from = self;
401 }
402 
403 /*
404  * Read/write routine for a buffer.  For now we poll the controller,
405  * thus this routine waits for the transfer to complete.
406  */
407 void
408 rdstrategy(bp)
409 	struct buf *bp;
410 {
411 	struct rdsoftc *rd = rd_cd.cd_devs[HDCUNIT(bp->b_dev)];
412 	struct hdcsoftc *hdc = (void *)rd->sc_dev.dv_parent;
413 	struct partition *p;
414 	int blkno, i, s;
415 
416 	trace (("rdstrategy(#%d/%d)\n", bp->b_blkno, bp->b_bcount));
417 
418 	/* XXX		should make some checks... */
419 
420 	/*
421 	 * If it's a null transfer, return immediatly
422 	 */
423 	if (bp->b_bcount == 0)
424 		goto done;
425 
426 	/*
427 	 * what follows now should not be here but in rdstart...
428 	 */
429 	/*------------------------------*/
430 	blkno = bp->b_blkno / (rd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
431 	p = &rd->sc_dk.dk_label->d_partitions[HDCPART(bp->b_dev)];
432 	blkno += p->p_offset;
433 
434 	/* nblks = howmany(bp->b_bcount, sd->sc_dk.dk_label->d_secsize); */
435 
436 	if (hdc_strategy(hdc, rd, HDCUNIT(bp->b_dev),
437 			 ((bp->b_flags & B_READ) ? F_READ : F_WRITE),
438 			 blkno, bp->b_bcount, bp->b_data) == 0)
439 		goto done;
440 	/*------------------------------*/
441 bad:
442 	bp->b_flags |= B_ERROR;
443 done:
444 	/*
445 	 * Correctly set the buf to indicate a completed xfer
446 	 */
447 	bp->b_resid = 0;	/* ??? bertram */
448 	biodone(bp);
449 }
450 
451 int
452 hdc_strategy(hdc, rd, unit, func, dblk, size, buf)
453 	struct hdcsoftc *hdc;
454 	struct rdsoftc *rd;
455 	int unit;
456 	int func;
457 	int dblk;
458 	int size;
459 	char *buf;
460 {
461 	struct hdc9224_UDCreg *p = &hdc->sc_creg;
462 	struct disklabel *lp = rd->sc_dk.dk_label;
463 	int sect, head, cyl;
464 	int scount;
465 	int cmd, res = 0;
466 
467 	trace (("hdc_strategy(%d, %d, %d, %d, 0x%x)\n",
468 		unit, func, dblk, size, buf));
469 
470 	hdc_select(hdc, unit);		/* select drive right now */
471 
472 	if (unit != 2 && dblk == -1) {	/* read the on-disk geometry */
473 
474 	  p->udc_dma7  = 0;
475 	  p->udc_dma15 = 0;
476 	  p->udc_dma23 = 0;
477 
478 	  p->udc_dsect = 0;
479 	  p->udc_dhead = 0;
480 	  p->udc_dcyl  = 0;
481 
482 	  p->udc_scnt  = size/512;
483 	  p->udc_rtcnt = 0xF0;
484 	  p->udc_mode  = 0xC0;
485 	  p->udc_term  = 0xB4;
486 
487 	  vsbus_lockDMA(hdc->sc_cfargs);		/* bertram XXX */
488 	  haveLock = 1;
489 	  keepLock = 1;
490 
491 #ifdef PARANOID
492 	  bzero (hdc->sc_dmabase, size);	/* clear disk buffer */
493 #endif
494 	  cmd = 0x5C | 0x03;			/* bypass bad sectors */
495 	  cmd = 0x5C | 0x01;			/* terminate if bad sector */
496 
497 	  res = hdc_command (hdc, cmd);
498 	  /* hold the locking ! */
499 	  bcopy (hdc->sc_dmabase, buf, size);	/* copy to buf */
500 	  /* now release the locking */
501 
502 	  vsbus_unlockDMA(hdc->sc_cfargs);
503 	  haveLock = 0;
504 	  keepLock = 0;
505 
506 	  return (res);
507 	}
508 
509 	scount = size / 512;
510 	while (scount) {
511 	  /*
512 	   * prepare drive/operation parameter
513 	   */
514 	  cyl  = dblk / lp->d_secpercyl;
515 	  sect = dblk % lp->d_secpercyl;
516 	  head = sect / lp->d_nsectors;
517 	  sect = sect % lp->d_nsectors;
518 	  if (unit == 2)
519 		sect++;
520 	  else
521 		cyl++;		/* first cylinder is reserved */
522 
523 	  size = 512 * min(scount, lp->d_nsectors - sect);
524 
525 	  debug (("hdc_strategy: block #%d ==> s/t/c=%d/%d/%d (%d/%d)\n",
526 		  dblk, sect, head, cyl, scount, size));
527 
528 	  /*
529 	   * now initialize the register values ...
530 	   */
531 	  p->udc_dma7  = 0;
532 	  p->udc_dma15 = 0;
533 	  p->udc_dma23 = 0;
534 
535 	  p->udc_dsect = sect;
536 	  head |= (cyl >> 4) & 0x70;
537 	  p->udc_dhead = head;
538 	  p->udc_dcyl  = cyl;
539 
540 	  p->udc_scnt  = size/512;
541 
542 	  if (unit == 2) {	/* floppy */
543 	    p->udc_rtcnt = 0xF2;
544 	    p->udc_mode	 = 0x81;	/* RX33 with RX50 media */
545 	    p->udc_mode	 = 0x82;	/* RX33 with RX33 media */
546 	    p->udc_term	 = 0xB4;
547 	  } else {		 /* disk */
548 	    p->udc_rtcnt = 0xF0;
549 	    p->udc_mode	 = 0xC0;
550 	    p->udc_term	 = 0xB4;
551 	  }
552 
553 	  vsbus_lockDMA(hdc->sc_cfargs);
554 	  haveLock = 1;
555 	  keepLock = 1;
556 
557 	  if (func == F_WRITE) {
558 	    bcopy (buf, hdc->sc_dmabase, size); /* copy from buf */
559 	    cmd = 0xA0 | (unit==2 ? 1 : 0);
560 	    res = hdc_command (hdc, cmd);
561 	  }
562 	  else {
563 #ifdef PARANOID
564 	    bzero (hdc->sc_dmabase, size);		/* clear disk buffer */
565 #endif
566 	    cmd = 0x5C | 0x03;	/* bypass bad sectors */
567 	    cmd = 0x5C | 0x01;	/* terminate if bad sector */
568 	    res = hdc_command (hdc, cmd);
569 	    bcopy (hdc->sc_dmabase, buf, size); /* copy to buf */
570 	  }
571 
572 	  vsbus_unlockDMA(hdc->sc_cfargs);
573 	  haveLock = 0;
574 	  keepLock = 0;
575 
576 	  scount -= size/512;
577 	  dblk += size/512;
578 	  buf += size;
579 	}
580 
581 	if (unit != 2)		/* deselect drive, if not floppy */
582 	  hdc_command (hdc, DKC_CMD_DRDESELECT);
583 
584 	return 0;
585 }
586 
587 char hdc_iobuf[17*512];		/* we won't need more */
588 
589 #ifdef DEBUG
590 /*
591  * display the contents of the on-disk geometry structure
592  */
593 int
594 hdc_printgeom(p)
595 	struct rdgeom *p;
596 {
597 	char dname[8];
598 	hdc_mid2str(p->media_id, dname);
599 
600 	printf ("**DiskData**	 XBNs: %d, DBNs: %d, LBNs: %d, RBNs: %d\n",
601 		p->xbn_count, p->dbn_count, p->lbn_count, p->rbn_count);
602 	printf ("sec/track: %d, tracks: %d, cyl: %d, precomp/reduced: %d/%d\n",
603 		p->nspt, p->ntracks, p->ncylinders, p->precomp, p->reduced);
604 	printf ("seek-rate: %d, crc/eec: %s, RCT: %d, RCT-copies: %d\n",
605 		p->seek_rate, p->crc_eec?"EEC":"CRC", p->rct, p->rct_ncopies);
606 	printf ("media-ID: %s, interleave: %d, headskew: %d, cylskew: %d\n",
607 		dname, p->interleave, p->headskew, p->cylskew);
608 	printf ("gap0: %d, gap1: %d, gap2: %d, gap3: %d, sync-value: %d\n",
609 		p->gap0_size, p->gap1_size, p->gap2_size, p->gap3_size,
610 		p->sync_value);
611 }
612 #endif
613 
614 /*
615  * Convert media_id to string/name (encoding is documented in mscp.h)
616  */
617 int
618 hdc_mid2str(media_id, name)
619 	long media_id;
620 	char *name;
621 {
622 	struct {			/* For RD32 this struct holds: */
623 		u_long mt:7;		/* number in name: 0x20 == 32 */
624 		u_long a2:5;		/* ' ' encoded as 0x0 */
625 		u_long a1:5;		/* 'D' encoded with base '@' */
626 		u_long a0:5;		/* 'R' encoded with base '@' */
627 		u_long d1:5;		/* 'U' encoded with base '@' */
628 		u_long d0:5;		/* 'D' encoded with base '@' */
629 	} *p = (void*)&media_id;
630 
631 #define MIDCHR(x)	(x ? x + '@' : ' ')
632 
633 	sprintf (name, "%c%c%d", MIDCHR(p->a0), MIDCHR(p->a1), p->mt);
634 }
635 
636 int
637 hdc_getdata(hdc, rd, unit)
638 	struct hdcsoftc *hdc;
639 	struct rdsoftc *rd;
640 	int unit;
641 {
642 	struct disklabel *lp = rd->sc_dk.dk_label;
643 	struct rdparams *rp = &rd->sc_param;
644 	int res;
645 
646 	trace (("hdc_getdata(%d)\n", unit));
647 
648 	bzero(rd->sc_dk.dk_label, sizeof(struct disklabel));
649 	bzero(rd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
650 
651 	if (unit == 2) {
652 		lp->d_secsize = DEV_BSIZE;
653 		lp->d_ntracks = 2;
654 		lp->d_nsectors = 15;
655 		lp->d_ncylinders = 80;
656 		lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
657 
658 		return (0);
659 	}
660 
661 	res = hdc_strategy(hdc, rd, unit, F_READ, -1, 4096, hdc_iobuf);
662 	bcopy (hdc_iobuf, &rd->sc_xbn, sizeof(struct rdgeom));
663 #ifdef DEBUG
664 	hdc_printgeom(&rd->sc_xbn);
665 #endif
666 	lp->d_secsize = DEV_BSIZE;
667 	lp->d_ntracks = rd->sc_xbn.ntracks;
668 	lp->d_nsectors = rd->sc_xbn.nspt;
669 	lp->d_ncylinders = rd->sc_xbn.ncylinders;
670 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
671 
672 	rp->cylinders = rd->sc_xbn.ncylinders;
673 	rp->heads = rd->sc_xbn.ntracks;
674 	rp->sectors = rd->sc_xbn.nspt;
675 	rp->diskblks = rp->cylinders * rp->heads * rp->sectors;
676 	rp->disklbns = rd->sc_xbn.lbn_count;
677 	rp->blksize = DEV_BSIZE;
678 	rp->diskbytes = rp->disklbns * rp->blksize;
679 	hdc_mid2str(rd->sc_xbn.media_id, rp->diskname);
680 
681 	return (0);
682 }
683 
684 int
685 hdc_getlabel(hdc, rd, unit)
686 	struct hdcsoftc *hdc;
687 	struct rdsoftc *rd;
688 	int unit;
689 {
690 	struct disklabel *lp = rd->sc_dk.dk_label;
691 	struct disklabel *xp = (void*)(hdc_iobuf + 64);
692 	int res;
693 
694 	trace (("hdc_getlabel(%d)\n", unit));
695 
696 #define LBL_CHECK(x)	if (xp->x != lp->x) {			\
697 			  printf ("%d-->%d\n", xp->x, lp->x);	\
698 			  xp->x = lp->x;			\
699 			}
700 	res = hdc_strategy(hdc, rd, unit, F_READ, 0, DEV_BSIZE, hdc_iobuf);
701 	LBL_CHECK(d_secsize);
702 	LBL_CHECK(d_ntracks);
703 	LBL_CHECK(d_nsectors);
704 	LBL_CHECK(d_ncylinders);
705 	LBL_CHECK(d_secpercyl);
706 	bcopy(xp, lp, sizeof(struct disklabel));
707 
708 	return (0);
709 }
710 
711 /*
712  * Return the size of a partition, if known, or -1 if not.
713  */
714 hdcsize(dev)
715 	dev_t dev;
716 {
717 	int unit = HDCUNIT(dev);
718 	int part = HDCPART(dev);
719 	struct rdsoftc *rd = rd_cd.cd_devs[unit];
720 	int size;
721 
722 	trace (("hdcsize(%x == %d/%d)\n", dev, unit, part));
723 
724 	if (hdcopen(dev, 0, S_IFBLK) != 0)
725 		return (-1);
726 #if 0
727 	if (rd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
728 		size = -1;
729 	else
730 #endif
731 		size = rd->sc_dk.dk_label->d_partitions[part].p_size;
732 	if (hdcclose(dev, 0, S_IFBLK) != 0)
733 		return (-1);
734 	debug (("hdcsize: size=%d\n", size));
735 	return (size);
736 }
737 
738 /*
739  *
740  */
741 int
742 hdcopen (dev, flag, fmt)
743 	dev_t dev;
744 	int flag;
745 	int fmt;
746 {
747 	int unit = HDCUNIT(dev);
748 	int part = HDCPART(dev);
749 	struct hdcsoftc *hdc;
750 	struct rdsoftc *rd;
751 	int res, error;
752 
753 	trace (("hdcopen(0x%x = %d/%d)\n", dev, unit, part));
754 
755 	if (unit >= rd_cd.cd_ndevs) {
756 		printf ("hdcopen: invalid unit %d\n", unit);
757 		return ENXIO;
758 	}
759 	rd = rd_cd.cd_devs[unit];
760 	if (!rd) {
761 		printf("hdcopen: null-pointer in rdsoftc.\n");
762 		return (ENXIO);
763 	}
764 	hdc = (void *)rd->sc_dev.dv_parent;
765 
766 	/* XXX here's much more to do! XXX */
767 
768 	hdc_getdata (hdc, rd, unit);
769 	hdc_getlabel (hdc, rd, unit);
770 
771 	return (0);
772 }
773 
774 /*
775  *
776  */
777 int
778 hdcclose (dev, flag)
779 	dev_t dev;
780 	int flag;
781 {
782 	trace (("hdcclose()\n"));
783 	return (0);
784 }
785 
786 /*
787  *
788  */
789 void
790 hdcstrategy(bp)
791 	register struct buf *bp;
792 {
793 	trace (("hdcstrategy()\n"));
794 	rdstrategy(bp);
795 	debug (("hdcstrategy done.\n"));
796 }
797 
798 /*
799  *
800  */
801 int
802 hdcioctl(dev, cmd, data, flag, p)
803 	dev_t dev;
804 	int cmd;
805 	caddr_t data;	/* aka: addr */
806 	int flag;
807 	struct proc *p;
808 {
809 	struct rdsoftc *rd = rd_cd.cd_devs[HDCUNIT(dev)];
810 	struct hdcsoftc *hdc = (void *)rd->sc_dev.dv_parent;
811 	int error;
812 
813 	trace (("hdcioctl(%x, %x)\n", dev, cmd));
814 
815 	/*
816 	 * If the device is not valid.. abandon ship
817 	 */
818 	/* XXX */
819 
820 	switch (cmd) {
821 	case DIOCGDINFO:
822 		*(struct disklabel *)data = *(rd->sc_dk.dk_label);
823 		return (0);
824 
825 	case DIOCGPART:
826 		((struct partinfo *)data)->disklab = rd->sc_dk.dk_label;
827 		((struct partinfo *)data)->part =
828 		  &rd->sc_dk.dk_label->d_partitions[HDCPART(dev)];
829 		return (0);
830 
831 	case DIOCWDINFO:
832 	case DIOCSDINFO:
833 /* XXX
834 		if ((flag & FWRITE) == 0)
835 			return EBADF;
836 
837 		if ((error = sdlock(sd)) != 0)
838 			return error;
839 		sd->flags |= SDF_LABELLING;
840 */
841 		error = setdisklabel(rd->sc_dk.dk_label,
842 		     (struct disklabel *)data, 0, rd->sc_dk.dk_cpulabel);
843 		if (error == 0) {
844 			if (cmd == DIOCWDINFO)
845 				error = writedisklabel(HDCLABELDEV(dev),
846 					rdstrategy, rd->sc_dk.dk_label,
847 					rd->sc_dk.dk_cpulabel);
848 		}
849 /* XXX
850 		sd->flags &= ~SDF_LABELLING;
851 		sdunlock(sd);
852 */
853 		return (error);
854 
855 	case DIOCWLABEL:
856 		if ((flag & FWRITE) == 0)
857 			return (EBADF);
858 /* XXX
859 		if (*(int *)data)
860 			sd->flags |= SDF_WLABEL;
861 		else
862 			sd->flags &= ~SDF_WLABEL;
863 */
864 		return (0);
865 
866 	default:
867 		if (HDCPART(dev) != RAW_PART)
868 			return ENOTTY;
869 		printf ("IOCTL %x not implemented.\n", cmd);
870 		return (-1);
871 	}
872 }
873 
874 /*
875  *
876  */
877 int
878 hdcintr()
879 {
880 	trace (("hdcintr()\n"));
881 }
882 
883 /*
884  *
885  */
886 int
887 hdcread (dev, uio)
888 	dev_t dev;
889 	struct uio *uio;
890 {
891 	trace (("hdcread()\n"));
892 	return (physio (hdcstrategy, NULL, dev, B_READ, minphys, uio));
893 }
894 
895 /*
896  *
897  */
898 int
899 hdcwrite (dev, uio)
900 	dev_t dev;
901 	struct uio *uio;
902 {
903 	trace (("hdcwrite()\n"));
904 	return (physio (hdcstrategy, NULL, dev, B_WRITE, minphys, uio));
905 }
906 
907 /*
908  *
909  */
910 int
911 hdcdump(dev)
912 	dev_t dev;
913 {
914 	trace (("hdcdump (%x)\n", dev));
915 }
916 
917 /*
918  * we have to wait 0.7 usec between two accesses to any of the
919  * dkc-registers, on a VS2000 with 1 MIPS, this is roughly one
920  * instruction. Thus the loop-overhead will be enough...
921  */
922 void
923 hdc_readregs(sc)
924 	struct hdcsoftc *sc;
925 {
926 	int i;
927 	char *p;
928 
929 	trace(("hdc_readregs()\n"));
930 
931 	sc->sc_dkc->dkc_cmd = 0x40;	/* set internal counter to zero */
932 	p = (void*)&sc->sc_sreg;
933 	for (i=0; i<10; i++)
934 		*p++ = sc->sc_dkc->dkc_reg;	/* dkc_reg auto-increments */
935 }
936 
937 void
938 hdc_writeregs(sc)
939 	struct hdcsoftc *sc;
940 {
941 	int i;
942 	char *p;
943 
944 	trace(("hdc_writeregs()\n"));
945 
946 	sc->sc_dkc->dkc_cmd = 0x40;	/* set internal counter to zero */
947 	p = (void*)&sc->sc_creg;
948 	for (i=0; i<10; i++)
949 		sc->sc_dkc->dkc_reg = *p++;	/* dkc_reg auto-increments */
950 }
951 
952 /*
953  * hdc_command() issues a command and polls the intreq-register
954  * to find when command has completed
955  */
956 int
957 hdc_command(sc, cmd)
958 	struct hdcsoftc *sc;
959 	int cmd;
960 {
961 	volatile u_char *intreq = (void*)uvax_phys2virt(KA410_INTREQ);
962 	volatile u_char *intclr = (void*)uvax_phys2virt(KA410_INTCLR);
963 	volatile u_char *intmsk = (void*)uvax_phys2virt(KA410_INTMSK);
964 	int i, c;
965 
966 	trace (("hdc_command(%x)\n", cmd));
967 	debug (("intr-state: %x %x %x\n", *intreq, *intclr, *intmsk));
968 
969 	if (!haveLock) {
970 	  vsbus_lockDMA(sc->sc_cfargs);
971 	  haveLock = 1;
972 	}
973 
974 	hdc_writeregs(sc);		/* write the prepared registers */
975 	*intclr = INTR_DC;		/* clear any old interrupt */
976 	sc->sc_dkc->dkc_cmd = cmd;	/* issue the command */
977 	for (i=0; i<MAX_WAIT; i++) {
978 		if ((c = *intreq) & INTR_DC)
979 			break;
980 	}
981 	if ((c & INTR_DC) == 0) {
982 		printf ("hdc_command: timeout in command 0x%x\n", cmd);
983 	}
984 	hdc_readregs(sc);		/* read the status registers */
985 	sc->sc_status = sc->sc_dkc->dkc_stat;
986 
987 	if (!keepLock) {
988 	  vsbus_unlockDMA(sc->sc_cfargs);
989 	  haveLock = 0;
990 	}
991 
992 	if (sc->sc_status != DKC_ST_DONE|DKC_TC_SUCCESS) {
993 		printf ("command 0x%x completed with status 0x%x\n",
994 			cmd, sc->sc_status);
995 		return (-1);
996 	}
997 	return (0);
998 }
999 
1000 /*
1001  * writing zero into the command-register will reset the controller.
1002  * This will not interrupt data-transfer commands!
1003  * Also no interrupt is generated, thus we don't use hdc_command()
1004  */
1005 int
1006 hdc_reset(sc)
1007 	struct hdcsoftc *sc;
1008 {
1009 	trace (("hdc_reset()\n"));
1010 
1011 	sc->sc_dkc->dkc_cmd = DKC_CMD_RESET;	/* issue RESET command */
1012 	hdc_readregs(sc);			/* read the status registers */
1013 	sc->sc_status = sc->sc_dkc->dkc_stat;
1014 	if (sc->sc_status != DKC_ST_DONE|DKC_TC_SUCCESS) {
1015 		printf ("RESET command completed with status 0x%x\n",
1016 			sc->sc_status);
1017 		return (-1);
1018 	}
1019 	return (0);
1020 }
1021 
1022 int
1023 hdc_rxselect(sc, unit)
1024 	struct hdcsoftc *sc;
1025 	int unit;
1026 {
1027 	register struct hdc9224_UDCreg *p = &sc->sc_creg;
1028 	register struct hdc9224_UDCreg *q = &sc->sc_sreg;
1029 	int error;
1030 
1031 	/*
1032 	 * bring command-regs in some known-to-work state and
1033 	 * select the drive with the DRIVE SELECT command.
1034 	 */
1035 	p->udc_dma7  = 0;
1036 	p->udc_dma15 = 0;
1037 	p->udc_dma23 = 0;
1038 	p->udc_dsect = 1;	/* sectors are numbered 1..15 !!! */
1039 	p->udc_dhead = 0;
1040 	p->udc_dcyl  = 0;
1041 	p->udc_scnt  = 0;
1042 
1043 	p->udc_rtcnt = UDC_RC_RX33READ;
1044 	p->udc_mode  = UDC_MD_RX33;
1045 	p->udc_term  = UDC_TC_FDD;
1046 
1047 	/*
1048 	 * this is ...
1049 	 */
1050 	error = hdc_command (sc, DKC_CMD_DRSEL_RX33 | unit);
1051 
1052 	if ((error != 0) || (q->udc_dstat & UDC_DS_READY == 0)) {
1053 	  printf("\nfloppy-drive not ready (new floppy inserted?)\n\n");
1054 	  p->udc_rtcnt &= ~UDC_RC_INVRDY;	/* clear INVRDY-flag */
1055 	  error = hdc_command(sc, DKC_CMD_DRSEL_RX33 | unit);
1056 	  if ((error != 0) || (q->udc_dstat & UDC_DS_READY == 0)) {
1057 	    printf("diskette not ready(1): %x/%x\n", error, q->udc_dstat);
1058 	    printf("floppy-drive offline?\n");
1059 	    return (-1);
1060 	  }
1061 
1062 	  if (q->udc_dstat & UDC_DS_TRK00)		    /* if track-0 */
1063 	    error = hdc_command(sc, DKC_CMD_STEPIN_FDD);   /* step inwards */
1064 	  else						    /* else */
1065 	    error = hdc_command(sc, DKC_CMD_STEPOUT_FDD);  /* step outwards */
1066 
1067 	  if ((error != 0) || (q->udc_dstat & UDC_DS_READY == 1)) {
1068 	    printf("diskette not ready(2): %x/%x\n", error, q->udc_dstat);
1069 	    printf("No floppy inserted or drive offline\n");
1070 	    /* return (-1); */
1071 	  }
1072 
1073 	  p->udc_rtcnt |= UDC_RC_INVRDY;
1074 	  error = hdc_command(sc, DKC_CMD_DRSEL_RX33 | unit);
1075 	  if ((error != 0) || (q->udc_dstat & UDC_DS_READY == 0)) {
1076 	    printf("diskette not ready(3): %x/%x\n", error, q->udc_dstat);
1077 	    printf("no floppy inserted or floppy-door open\n");
1078 	    return(-1);
1079 	  }
1080 	  printf("floppy-drive reselected.\n");
1081 	}
1082 	if (error)
1083 		error = hdc_command (sc, DKC_CMD_DRSEL_RX33 | unit);
1084 
1085 	return (error);
1086 }
1087 
1088 int
1089 hdc_rdselect(sc, unit)
1090 	struct hdcsoftc *sc;
1091 	int unit;
1092 {
1093 	register struct hdc9224_UDCreg *p = &sc->sc_creg;
1094 	register struct hdc9224_UDCreg *q = &sc->sc_sreg;
1095 	int error;
1096 
1097 	/*
1098 	 * bring "creg" in some known-to-work state and
1099 	 * select the drive with the DRIVE SELECT command.
1100 	 */
1101 	p->udc_dma7  = 0;
1102 	p->udc_dma15 = 0;
1103 	p->udc_dma23 = 0;
1104 	p->udc_dsect = 0;		/* sectors are numbered 0..16 */
1105 	p->udc_dhead = 0;
1106 	p->udc_dcyl  = 0;
1107 	p->udc_scnt  = 0;
1108 
1109 	p->udc_rtcnt = UDC_RC_HDD_READ;
1110 	p->udc_mode  = UDC_MD_HDD;
1111 	p->udc_term  = UDC_TC_HDD;
1112 
1113 	error = hdc_command (sc, DKC_CMD_DRSEL_HDD | unit);
1114 	if (error)
1115 		error = hdc_command (sc, DKC_CMD_DRSEL_HDD | unit);
1116 
1117 	return (error);
1118 }
1119 
1120 /*
1121  * bring command-regs into some known-to-work state and select
1122  * the drive with the DRIVE SELECT command.
1123  */
1124 int
1125 hdc_select(sc, unit)
1126 	struct hdcsoftc *sc;
1127 	int unit;
1128 {
1129 	int error;
1130 
1131 	trace (("hdc_select(%x,%d)\n", sc, unit));
1132 
1133 	switch (unit) {
1134 	case 0:
1135 	case 1:
1136 		error = hdc_rdselect(sc, unit);
1137 		break;
1138 	case 2:
1139 		error = hdc_rxselect(sc, unit);
1140 		/* bertram: delay ??? XXX */
1141 		break;
1142 	default:
1143 		printf("invalid unit %d in hdc_select()\n", unit);
1144 		error = -1;
1145 	}
1146 
1147 	return (error);
1148 }
1149 #endif	/* NHDC > 0 */
1150