Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / vax / vsa / hdc9224.c
blobbd4dce16c0f58d02538f9a286097a76f18cf791d
1 /* $NetBSD: hdc9224.c,v 1.49 2009/10/26 19:16:58 cegger Exp $ */
2 /*
3 * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
4 * All rights reserved.
6 * This code is derived from software contributed to Ludd by Bertram Barth.
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
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.
36 * with much help from (in alphabetical order):
37 * Jeremy
38 * Roger Ivie
39 * Rick Macklem
40 * Mike Young
42 * Rewritten by Ragge 25 Jun 2000. New features:
43 * - Uses interrupts instead of polling to signal ready.
44 * - Can cooperate with the SCSI routines WRT. the DMA area.
46 * TODO:
47 * - Floppy support missing.
48 * - Bad block forwarding missing.
49 * - Statistics collection.
51 #undef RDDEBUG
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: hdc9224.c,v 1.49 2009/10/26 19:16:58 cegger Exp $");
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/conf.h>
60 #include <sys/file.h>
61 #include <sys/stat.h>
62 #include <sys/ioctl.h>
63 #include <sys/buf.h>
64 #include <sys/bufq.h>
65 #include <sys/proc.h>
66 #include <sys/device.h>
67 #include <sys/disklabel.h>
68 #include <sys/disk.h>
69 #include <sys/syslog.h>
70 #include <sys/reboot.h>
72 #include <uvm/uvm_extern.h>
74 #include <ufs/ufs/dinode.h> /* For BBSIZE */
75 #include <ufs/ffs/fs.h>
77 #include <machine/pte.h>
78 #include <machine/sid.h>
79 #include <machine/cpu.h>
80 #include <machine/uvax.h>
81 #include <machine/ka410.h>
82 #include <machine/vsbus.h>
83 #include <machine/rpb.h>
84 #include <machine/scb.h>
86 #include <dev/mscp/mscp.h> /* For DEC disk encoding */
88 #include <vax/vsa/hdc9224.h>
90 #include "ioconf.h"
91 #include "locators.h"
94 /*
95 * on-disk geometry block
97 #define _aP __attribute__ ((packed)) /* force byte-alignment */
98 struct rdgeom {
99 char mbz[10]; /* 10 bytes of zero */
100 long xbn_count _aP; /* number of XBNs */
101 long dbn_count _aP; /* number of DBNs */
102 long lbn_count _aP; /* number of LBNs (Logical-Block-Numbers) */
103 long rbn_count _aP; /* number of RBNs (Replacement-Block-Numbers) */
104 short nspt; /* number of sectors per track */
105 short ntracks; /* number of tracks */
106 short ncylinders; /* number of cylinders */
107 short precomp; /* first cylinder for write precompensation */
108 short reduced; /* first cylinder for reduced write current */
109 short seek_rate; /* seek rate or zero for buffered seeks */
110 short crc_eec; /* 0 if CRC, 1 if ECC is being used */
111 short rct; /* "replacement control table" (RCT) */
112 short rct_ncopies; /* number of copies of the RCT */
113 long media_id _aP; /* media identifier */
114 short interleave; /* sector-to-sector interleave */
115 short headskew; /* head-to-head skew */
116 short cylskew; /* cylinder-to-cylinder skew */
117 short gap0_size; /* size of GAP 0 in the MFM format */
118 short gap1_size; /* size of GAP 1 in the MFM format */
119 short gap2_size; /* size of GAP 2 in the MFM format */
120 short gap3_size; /* size of GAP 3 in the MFM format */
121 short sync_value; /* sync value used when formatting */
122 char reserved[32]; /* reserved for use by the RQDX formatter */
123 short serial_number; /* serial number */
124 #if 0 /* we don't need these 412 useless bytes ... */
125 char fill[412-2]; /* Filler bytes to the end of the block */
126 short checksum; /* checksum over the XBN */
127 #endif
131 * Software status
133 struct rdsoftc {
134 device_t sc_dev; /* must be here! (pseudo-OOP:) */
135 struct hdcsoftc *sc_hdc;
136 struct disk sc_disk; /* disklabel etc. */
137 struct rdgeom sc_xbn; /* on-disk geometry information */
138 int sc_drive; /* physical unit number */
141 struct hdcsoftc {
142 device_t sc_dev; /* must be here (pseudo-OOP:) */
143 struct evcnt sc_intrcnt;
144 struct vsbus_dma sc_vd;
145 vaddr_t sc_regs; /* register addresses */
146 struct bufq_state *sc_q;
147 struct buf *sc_active;
148 struct hdc9224_UDCreg sc_creg; /* (command) registers to be written */
149 struct hdc9224_UDCreg sc_sreg; /* (status) registers being read */
150 void * sc_dmabase; /* */
151 int sc_dmasize;
152 void *sc_bufaddr; /* Current in-core address */
153 int sc_diskblk; /* Current block on disk */
154 int sc_bytecnt; /* How much left to transfer */
155 int sc_xfer; /* Current transfer size */
156 int sc_retries;
157 volatile u_char sc_status; /* last status from interrupt */
158 char sc_intbit;
161 struct hdc_attach_args {
162 int ha_drive;
166 * prototypes for (almost) all the internal routines
168 static int hdcmatch(device_t, cfdata_t, void *);
169 static void hdcattach(device_t, device_t, void *);
170 static int hdcprint(void *, const char *);
171 static int rdmatch(device_t, cfdata_t, void *);
172 static void rdattach(device_t, device_t, void *);
173 static void hdcintr(void *);
174 static int hdc_command(struct hdcsoftc *, int);
175 static void rd_readgeom(struct hdcsoftc *, struct rdsoftc *);
176 #ifdef RDDEBUG
177 static void hdc_printgeom( struct rdgeom *);
178 #endif
179 static void hdc_writeregs(struct hdcsoftc *);
180 static void hdcstart(struct hdcsoftc *, struct buf *);
181 static int hdc_rdselect(struct hdcsoftc *, int);
182 static void rdmakelabel(struct disklabel *, struct rdgeom *);
183 static void hdc_writeregs(struct hdcsoftc *);
184 static void hdc_readregs(struct hdcsoftc *);
185 static void hdc_qstart(void *);
187 CFATTACH_DECL_NEW(hdc, sizeof(struct hdcsoftc),
188 hdcmatch, hdcattach, NULL, NULL);
190 CFATTACH_DECL_NEW(rd, sizeof(struct rdsoftc),
191 rdmatch, rdattach, NULL, NULL);
193 static dev_type_open(rdopen);
194 static dev_type_close(rdclose);
195 static dev_type_read(rdread);
196 static dev_type_write(rdwrite);
197 static dev_type_ioctl(rdioctl);
198 static dev_type_strategy(rdstrategy);
199 static dev_type_size(rdpsize);
201 const struct bdevsw rd_bdevsw = {
202 .d_open = rdopen,
203 .d_close = rdclose,
204 .d_strategy = rdstrategy,
205 .d_ioctl = rdioctl,
206 .d_dump = nulldump,
207 .d_psize = rdpsize,
208 .d_flag = D_DISK
211 const struct cdevsw rd_cdevsw = {
212 .d_open = rdopen,
213 .d_close = rdclose,
214 .d_read = rdread,
215 .d_write = rdwrite,
216 .d_ioctl = rdioctl,
217 .d_stop = nostop,
218 .d_tty = notty,
219 .d_poll = nopoll,
220 .d_mmap = nommap,
221 .d_kqfilter = nokqfilter,
222 .d_flag = D_DISK
225 /* At least 0.7 uS between register accesses */
226 static int rd_dmasize, inq = 0;
227 static volatile int u;
228 #define WAIT __asm("movl %0,%0;movl %0,%0;movl %0,%0; movl %0,%0" :: "m"(u))
230 #define HDC_WREG(x) *(volatile char *)(sc->sc_regs) = (x)
231 #define HDC_RREG *(volatile char *)(sc->sc_regs)
232 #define HDC_WCMD(x) *(volatile char *)(sc->sc_regs + 4) = (x)
233 #define HDC_RSTAT *(volatile char *)(sc->sc_regs + 4)
236 * new-config's hdcmatch() is similiar to old-config's hdcprobe(),
237 * thus we probe for the existence of the controller and reset it.
238 * NB: we can't initialize the controller yet, since space for hdcsoftc
239 * is not yet allocated. Thus we do this in hdcattach()...
242 hdcmatch(device_t parent, cfdata_t cf, void *aux)
244 struct vsbus_attach_args * const va = aux;
245 volatile char * const hdc_csr = (volatile char *)va->va_addr;
246 int i;
248 u = 8; /* !!! - GCC */
250 if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_46
251 || vax_boardtype == VAX_BTYP_48 || vax_boardtype == VAX_BTYP_53)
252 return 0;
254 hdc_csr[4] = DKC_CMD_RESET; /* reset chip */
255 for (i = 0; i < 1000; i++) {
256 DELAY(1000);
257 if (hdc_csr[4] & DKC_ST_DONE)
258 break;
260 if (i == 100)
261 return 0; /* No response to reset */
263 hdc_csr[4] = DKC_CMD_SETREGPTR|UDC_TERM;
264 WAIT;
265 hdc_csr[0] = UDC_TC_CRCPRE|UDC_TC_INTDONE;
266 WAIT;
267 hdc_csr[4] = DKC_CMD_DRDESELECT; /* Should be harmless */
268 DELAY(1000);
269 return (1);
273 hdcprint(void *aux, const char *name)
275 struct hdc_attach_args * const ha = aux;
277 if (name)
278 aprint_normal ("RD?? at %s drive %d", name, ha->ha_drive);
279 return UNCONF;
283 * hdc_attach() probes for all possible devices
285 void
286 hdcattach(device_t parent, device_t self, void *aux)
288 struct vsbus_attach_args * const va = aux;
289 struct hdcsoftc * const sc = device_private(self);
290 struct hdc_attach_args ha;
291 int status, i;
293 aprint_normal("\n");
295 sc->sc_dev = self;
298 * Get interrupt vector, enable instrumentation.
300 scb_vecalloc(va->va_cvec, hdcintr, sc, SCB_ISTACK, &sc->sc_intrcnt);
301 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
302 device_xname(self), "intr");
304 sc->sc_regs = vax_map_physmem(va->va_paddr, 1);
305 sc->sc_dmabase = (void *)va->va_dmaaddr;
306 sc->sc_dmasize = va->va_dmasize;
307 sc->sc_intbit = va->va_maskno;
308 rd_dmasize = min(MAXPHYS, sc->sc_dmasize); /* Used in rd_minphys */
310 sc->sc_vd.vd_go = hdc_qstart;
311 sc->sc_vd.vd_arg = sc;
313 * Reset controller.
315 HDC_WCMD(DKC_CMD_RESET);
316 DELAY(1000);
317 status = HDC_RSTAT;
318 if (status != (DKC_ST_DONE|DKC_TC_SUCCESS)) {
319 aprint_error_dev(self, "RESET failed, status 0x%x\n", status);
320 return;
322 bufq_alloc(&sc->sc_q, "disksort", BUFQ_SORT_CYLINDER);
325 * now probe for all possible hard drives
327 for (i = 0; i < 4; i++) {
328 if (i == 2) /* Floppy, needs special handling */
329 continue;
330 HDC_WCMD(DKC_CMD_DRSELECT | i);
331 DELAY(1000);
332 status = HDC_RSTAT;
333 ha.ha_drive = i;
334 if ((status & DKC_ST_TERMCOD) == DKC_TC_SUCCESS)
335 config_found(self, (void *)&ha, hdcprint);
340 * rdmatch() probes for the existence of a RD-type disk/floppy
343 rdmatch(device_t parent, cfdata_t cf, void *aux)
345 struct hdc_attach_args * const ha = aux;
347 if (cf->cf_loc[HDCCF_DRIVE] != HDCCF_DRIVE_DEFAULT &&
348 cf->cf_loc[HDCCF_DRIVE] != ha->ha_drive)
349 return 0;
351 if (ha->ha_drive == 2) /* Always floppy, not supported */
352 return 0;
354 return 1;
357 void
358 rdattach(device_t parent, device_t self, void *aux)
360 struct hdcsoftc * const sc = device_private(parent);
361 struct rdsoftc * const rd = device_private(self);
362 struct hdc_attach_args * const ha = aux;
363 struct disklabel *dl;
364 const char *msg;
366 rd->sc_dev = self;
367 rd->sc_drive = ha->ha_drive;
368 rd->sc_hdc = sc;
370 * Initialize and attach the disk structure.
372 disk_init(&rd->sc_disk, device_xname(rd->sc_dev), NULL);
373 disk_attach(&rd->sc_disk);
376 * if it's not a floppy then evaluate the on-disk geometry.
377 * if necessary correct the label...
379 rd_readgeom(sc, rd);
380 disk_printtype(rd->sc_drive, rd->sc_xbn.media_id);
381 dl = rd->sc_disk.dk_label;
382 rdmakelabel(dl, &rd->sc_xbn);
383 msg = readdisklabel(MAKEDISKDEV(cdevsw_lookup_major(&rd_cdevsw),
384 device_unit(rd->sc_dev), RAW_PART),
385 rdstrategy, dl, NULL);
386 if (msg)
387 aprint_normal_dev(self, "%s: size %u sectors",
388 msg, dl->d_secperunit);
389 else
390 aprint_normal_dev(self, "size %u sectors\n", dl->d_secperunit);
391 #ifdef RDDEBUG
392 hdc_printgeom(&rd->sc_xbn);
393 #endif
396 void
397 hdcintr(void *arg)
399 struct hdcsoftc * const sc = arg;
400 struct buf *bp;
402 sc->sc_status = HDC_RSTAT;
403 if (sc->sc_active == 0)
404 return; /* Complain? */
406 if ((sc->sc_status & (DKC_ST_INTPEND|DKC_ST_DONE)) !=
407 (DKC_ST_INTPEND|DKC_ST_DONE))
408 return; /* Why spurious ints sometimes??? */
410 bp = sc->sc_active;
411 sc->sc_active = 0;
412 if ((sc->sc_status & DKC_ST_TERMCOD) != DKC_TC_SUCCESS) {
413 int i;
414 u_char *g = (u_char *)&sc->sc_sreg;
416 if (sc->sc_retries++ < 3) { /* Allow 3 retries */
417 hdcstart(sc, bp);
418 return;
420 aprint_error_dev(sc->sc_dev, "failed, status 0x%x\n",
421 sc->sc_status);
422 hdc_readregs(sc);
423 for (i = 0; i < 10; i++)
424 aprint_error("%i: %x\n", i, g[i]);
425 bp->b_error = ENXIO;
426 bp->b_resid = bp->b_bcount;
427 biodone(bp);
428 vsbus_dma_intr();
429 return;
432 if (bp->b_flags & B_READ) {
433 vsbus_copytoproc(bp->b_proc, sc->sc_dmabase, sc->sc_bufaddr,
434 sc->sc_xfer);
436 sc->sc_diskblk += (sc->sc_xfer/DEV_BSIZE);
437 sc->sc_bytecnt -= sc->sc_xfer;
438 sc->sc_bufaddr = (char *)sc->sc_bufaddr + sc->sc_xfer;
440 if (sc->sc_bytecnt == 0) { /* Finished transfer */
441 biodone(bp);
442 vsbus_dma_intr();
443 } else
444 hdcstart(sc, bp);
450 void
451 rdstrategy(struct buf *bp)
453 struct rdsoftc *rd;
454 struct hdcsoftc *sc;
455 struct disklabel *lp;
456 int s;
458 if ((rd = device_lookup_private(&rd_cd, DISKUNIT(bp->b_dev))) == NULL) {
459 bp->b_error = ENXIO;
460 goto done;
462 sc = rd->sc_hdc;
464 lp = rd->sc_disk.dk_label;
465 if ((bounds_check_with_label(&rd->sc_disk, bp, 1)) <= 0)
466 goto done;
468 if (bp->b_bcount == 0)
469 goto done;
471 bp->b_rawblkno =
472 bp->b_blkno + lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
473 bp->b_cylinder = bp->b_rawblkno / lp->d_secpercyl;
475 s = splbio();
476 bufq_put(sc->sc_q, bp);
477 if (inq == 0) {
478 inq = 1;
479 vsbus_dma_start(&sc->sc_vd);
481 splx(s);
482 return;
484 done: biodone(bp);
487 void
488 hdc_qstart(void *arg)
490 struct hdcsoftc * const sc = arg;
492 inq = 0;
494 hdcstart(sc, 0);
495 if (bufq_peek(sc->sc_q)) {
496 vsbus_dma_start(&sc->sc_vd); /* More to go */
497 inq = 1;
501 void
502 hdcstart(struct hdcsoftc *sc, struct buf *ob)
504 struct hdc9224_UDCreg * const p = &sc->sc_creg;
505 struct disklabel *lp;
506 struct rdsoftc *rd;
507 struct buf *bp;
508 int cn, sn, tn, bn, blks;
509 volatile char ch;
511 if (sc->sc_active)
512 return; /* Already doing something */
514 if (ob == 0) {
515 bp = bufq_get(sc->sc_q);
516 if (bp == NULL)
517 return; /* Nothing to do */
518 sc->sc_bufaddr = bp->b_data;
519 sc->sc_diskblk = bp->b_rawblkno;
520 sc->sc_bytecnt = bp->b_bcount;
521 sc->sc_retries = 0;
522 bp->b_resid = 0;
523 } else
524 bp = ob;
526 rd = device_lookup_private(&rd_cd, DISKUNIT(bp->b_dev));
527 hdc_rdselect(sc, rd->sc_drive);
528 sc->sc_active = bp;
530 bn = sc->sc_diskblk;
531 lp = rd->sc_disk.dk_label;
532 if (bn) {
533 cn = bn / lp->d_secpercyl;
534 sn = bn % lp->d_secpercyl;
535 tn = sn / lp->d_nsectors;
536 sn = sn % lp->d_nsectors;
537 } else
538 cn = sn = tn = 0;
540 cn++; /* first cylinder is reserved */
542 memset(p, 0, sizeof(struct hdc9224_UDCreg));
545 * Tricky thing: the controller do itself only increase the sector
546 * number, not the track or cylinder number. Therefore the driver
547 * is not allowed to have transfers that crosses track boundaries.
549 blks = sc->sc_bytecnt/DEV_BSIZE;
550 if ((sn + blks) > lp->d_nsectors)
551 blks = lp->d_nsectors - sn;
553 p->udc_dsect = sn;
554 p->udc_dcyl = cn & 0xff;
555 p->udc_dhead = ((cn >> 4) & 0x70) | tn;
556 p->udc_scnt = blks;
558 p->udc_rtcnt = UDC_RC_RTRYCNT;
559 p->udc_mode = UDC_MD_HDD;
560 p->udc_term = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWRFLT;
561 hdc_writeregs(sc);
563 /* Count up vars */
564 sc->sc_xfer = blks * DEV_BSIZE;
566 ch = HDC_RSTAT; /* Avoid pending interrupts */
567 WAIT;
568 vsbus_clrintr(sc->sc_intbit); /* Clear pending int's */
570 if (bp->b_flags & B_READ) {
571 HDC_WCMD(DKC_CMD_READ_HDD);
572 } else {
573 vsbus_copyfromproc(bp->b_proc, sc->sc_bufaddr, sc->sc_dmabase,
574 sc->sc_xfer);
575 HDC_WCMD(DKC_CMD_WRITE_HDD);
579 void
580 rd_readgeom(struct hdcsoftc *sc, struct rdsoftc *rd)
582 struct hdc9224_UDCreg * const p = &sc->sc_creg;
584 hdc_rdselect(sc, rd->sc_drive); /* select drive right now */
586 memset(p, 0, sizeof(*p));
588 p->udc_scnt = 1;
589 p->udc_rtcnt = UDC_RC_RTRYCNT;
590 p->udc_mode = UDC_MD_HDD;
591 p->udc_term = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWPROT;
592 hdc_writeregs(sc);
593 sc->sc_status = 0;
594 HDC_WCMD(DKC_CMD_READ_HDD|2);
595 while ((sc->sc_status & DKC_ST_INTPEND) == 0)
597 memcpy(&rd->sc_xbn, sc->sc_dmabase, sizeof(struct rdgeom));
600 #ifdef RDDEBUG
602 * display the contents of the on-disk geometry structure
604 void
605 hdc_printgeom(struct rdgeom *p)
607 printf ("**DiskData** XBNs: %ld, DBNs: %ld, LBNs: %ld, RBNs: %ld\n",
608 p->xbn_count, p->dbn_count, p->lbn_count, p->rbn_count);
609 printf ("sec/track: %d, tracks: %d, cyl: %d, precomp/reduced: %d/%d\n",
610 p->nspt, p->ntracks, p->ncylinders, p->precomp, p->reduced);
611 printf ("seek-rate: %d, crc/eec: %s, RCT: %d, RCT-copies: %d\n",
612 p->seek_rate, p->crc_eec?"EEC":"CRC", p->rct, p->rct_ncopies);
613 printf ("media-ID: %lx, interleave: %d, headskew: %d, cylskew: %d\n",
614 p->media_id, p->interleave, p->headskew, p->cylskew);
615 printf ("gap0: %d, gap1: %d, gap2: %d, gap3: %d, sync-value: %d\n",
616 p->gap0_size, p->gap1_size, p->gap2_size, p->gap3_size,
617 p->sync_value);
619 #endif
622 * Return the size of a partition, if known, or -1 if not.
625 rdpsize(dev_t dev)
627 struct rdsoftc * const rd = device_lookup_private(&rd_cd, DISKUNIT(dev));
628 const int part = DISKPART(dev);
630 if (rd == NULL || part >= rd->sc_disk.dk_label->d_npartitions)
631 return -1;
633 return rd->sc_disk.dk_label->d_partitions[part].p_size *
634 (rd->sc_disk.dk_label->d_secsize / DEV_BSIZE);
641 rdopen(dev_t dev, int flag, int fmt, struct lwp *l)
643 struct rdsoftc * const rd = device_lookup_private(&rd_cd, DISKUNIT(dev));
644 const int part = DISKPART(dev);
646 if (rd == NULL || part >= rd->sc_disk.dk_label->d_npartitions)
647 return ENXIO;
649 switch (fmt) {
650 case S_IFCHR:
651 rd->sc_disk.dk_copenmask |= (1 << part);
652 break;
653 case S_IFBLK:
654 rd->sc_disk.dk_bopenmask |= (1 << part);
655 break;
657 rd->sc_disk.dk_openmask =
658 rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
660 return 0;
667 rdclose(dev_t dev, int flag, int fmt, struct lwp *l)
669 struct rdsoftc * const rd = device_lookup_private(&rd_cd, DISKUNIT(dev));
670 const int part = DISKPART(dev);
672 switch (fmt) {
673 case S_IFCHR:
674 rd->sc_disk.dk_copenmask &= ~(1 << part);
675 break;
676 case S_IFBLK:
677 rd->sc_disk.dk_bopenmask &= ~(1 << part);
678 break;
680 rd->sc_disk.dk_openmask =
681 rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
683 return (0);
690 rdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
692 struct rdsoftc * const rd = device_lookup_private(&rd_cd, DISKUNIT(dev));
693 struct disklabel * const lp = rd->sc_disk.dk_label;
694 int error = 0;
696 switch (cmd) {
697 case DIOCGDINFO:
698 *(struct disklabel *)addr = *lp;
699 break;
701 case DIOCGPART:
702 ((struct partinfo *)addr)->disklab = lp;
703 ((struct partinfo *)addr)->part =
704 &lp->d_partitions[DISKPART(dev)];
705 break;
707 case DIOCWDINFO:
708 case DIOCSDINFO:
709 if ((flag & FWRITE) == 0)
710 return EBADF;
711 error = (cmd == DIOCSDINFO ?
712 setdisklabel(lp, (struct disklabel *)addr, 0, 0) :
713 writedisklabel(dev, rdstrategy, lp, 0));
714 break;
716 case DIOCGDEFLABEL:
717 memset(lp, 0, sizeof(*lp));
718 rdmakelabel(lp, &rd->sc_xbn);
719 break;
721 case DIOCWLABEL:
722 if ((flag & FWRITE) == 0)
723 error = EBADF;
724 break;
726 default:
727 error = ENOTTY;
729 return error;
736 rdread(dev_t dev, struct uio *uio, int flag)
738 return (physio (rdstrategy, NULL, dev, B_READ, minphys, uio));
745 rdwrite(dev_t dev, struct uio *uio, int flag)
747 return (physio (rdstrategy, NULL, dev, B_WRITE, minphys, uio));
751 * we have to wait 0.7 usec between two accesses to any of the
752 * dkc-registers, on a VS2000 with 1 MIPS, this is roughly one
753 * instruction. Thus the loop-overhead will be enough...
755 static void
756 hdc_readregs(struct hdcsoftc *sc)
758 int i;
759 char *p;
761 HDC_WCMD(DKC_CMD_SETREGPTR);
762 WAIT;
763 p = (void*)&sc->sc_sreg;
764 for (i=0; i<10; i++) {
765 *p++ = HDC_RREG; /* dkc_reg auto-increments */
766 WAIT;
770 static void
771 hdc_writeregs(struct hdcsoftc *sc)
773 int i;
774 char *p;
776 HDC_WCMD(DKC_CMD_SETREGPTR);
777 p = (void*)&sc->sc_creg;
778 for (i=0; i<10; i++) {
779 HDC_WREG(*p++); /* dkc_reg auto-increments */
780 WAIT;
785 * hdc_command() issues a command and polls the intreq-register
786 * to find when command has completed
789 hdc_command(struct hdcsoftc *sc, int cmd)
791 hdc_writeregs(sc); /* write the prepared registers */
792 HDC_WCMD(cmd);
793 WAIT;
794 return (0);
798 hdc_rdselect(struct hdcsoftc *sc, int unit)
800 struct hdc9224_UDCreg * const p = &sc->sc_creg;
801 int error;
804 * bring "creg" in some known-to-work state and
805 * select the drive with the DRIVE SELECT command.
807 memset(p, 0, sizeof(*p));
809 p->udc_rtcnt = UDC_RC_HDD_READ;
810 p->udc_mode = UDC_MD_HDD;
811 p->udc_term = UDC_TC_HDD;
813 error = hdc_command(sc, DKC_CMD_DRSEL_HDD | unit);
815 return error;
818 void
819 rdmakelabel(struct disklabel *dl, struct rdgeom *g)
821 int n, p = 0;
823 dl->d_bbsize = BBSIZE;
824 dl->d_sbsize = SBLOCKSIZE;
825 dl->d_typename[p++] = MSCP_MID_CHAR(2, g->media_id);
826 dl->d_typename[p++] = MSCP_MID_CHAR(1, g->media_id);
827 if (MSCP_MID_ECH(0, g->media_id))
828 dl->d_typename[p++] = MSCP_MID_CHAR(0, g->media_id);
829 n = MSCP_MID_NUM(g->media_id);
830 if (n > 99) {
831 dl->d_typename[p++] = '1';
832 n -= 100;
834 if (n > 9) {
835 dl->d_typename[p++] = (n / 10) + '0';
836 n %= 10;
838 dl->d_typename[p++] = n + '0';
839 dl->d_typename[p] = 0;
840 dl->d_type = DTYPE_MSCP; /* XXX - what to use here??? */
841 dl->d_rpm = 3600;
842 dl->d_secsize = DEV_BSIZE;
844 dl->d_secperunit = g->lbn_count;
845 dl->d_nsectors = g->nspt;
846 dl->d_ntracks = g->ntracks;
847 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
848 dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl;
850 dl->d_npartitions = MAXPARTITIONS;
851 dl->d_partitions[0].p_size = dl->d_partitions[2].p_size =
852 dl->d_secperunit;
853 dl->d_partitions[0].p_offset = dl->d_partitions[2].p_offset = 0;
854 dl->d_interleave = dl->d_headswitch = 1;
855 dl->d_magic = dl->d_magic2 = DISKMAGIC;
856 dl->d_checksum = dkcksum(dl);