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