hdc9224.c revision 1.9 1 1.9 ragge /* $NetBSD: hdc9224.c,v 1.9 1998/04/13 12:17:31 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.9 ragge /* #define DEBUG */
44 1.9 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.8 ragge int hdcmatch __P((struct device *parent, struct 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 cfattach hdc_ca = {
186 1.1 ragge sizeof(struct hdcsoftc), hdcmatch, hdcattach
187 1.1 ragge };
188 1.1 ragge
189 1.8 ragge int rdmatch __P((struct device *parent, struct cfdata *cfdata, void *aux));
190 1.1 ragge void rdattach __P((struct device *parent, struct device *self, void *aux));
191 1.2 cgd int rdprint __P((void *aux, const char *name));
192 1.1 ragge void rdstrategy __P((struct buf *bp));
193 1.1 ragge
194 1.1 ragge struct cfattach rd_ca = {
195 1.1 ragge sizeof(struct rdsoftc), rdmatch, rdattach
196 1.1 ragge };
197 1.7 thorpej
198 1.7 thorpej extern struct cfdriver rd_cd;
199 1.1 ragge
200 1.1 ragge struct dkdriver rddkdriver = { rdstrategy };
201 1.1 ragge
202 1.1 ragge /*
203 1.1 ragge * prototypes for (almost) all the internal routines
204 1.1 ragge */
205 1.1 ragge int hdc_reset __P((struct hdcsoftc *sc));
206 1.1 ragge int hdc_select __P((struct hdcsoftc *sc, int drive));
207 1.1 ragge int hdc_command __P((struct hdcsoftc *sc, int cmd));
208 1.1 ragge
209 1.1 ragge int hdc_getdata __P((struct hdcsoftc *hdc, struct rdsoftc *rd, int drive));
210 1.1 ragge int hdc_getlabel __P((struct hdcsoftc *hdc, struct rdsoftc *rd, int drive));
211 1.1 ragge
212 1.1 ragge void rdgetlabel __P((struct rdsoftc *sc));
213 1.1 ragge
214 1.1 ragge /*
215 1.1 ragge * new-config's hdcmatch() is similiar to old-config's hdcprobe(),
216 1.1 ragge * thus we probe for the existence of the controller and reset it.
217 1.1 ragge * NB: we can't initialize the controller yet, since space for hdcsoftc
218 1.1 ragge * is not yet allocated. Thus we do this in hdcattach()...
219 1.1 ragge */
220 1.1 ragge int
221 1.8 ragge hdcmatch(parent, cf, aux)
222 1.1 ragge struct device *parent;
223 1.8 ragge struct cfdata *cf;
224 1.8 ragge void *aux;
225 1.1 ragge {
226 1.1 ragge struct confargs *ca = aux;
227 1.1 ragge
228 1.1 ragge trace(("hdcmatch(0x%x, %d, %s)\n", parent, cf->cf_unit, ca->ca_name));
229 1.1 ragge
230 1.1 ragge if (strcmp(ca->ca_name, "hdc") &&
231 1.1 ragge strcmp(ca->ca_name, "hdc9224") &&
232 1.1 ragge strcmp(ca->ca_name, "HDC9224"))
233 1.1 ragge return (0);
234 1.1 ragge
235 1.1 ragge /*
236 1.1 ragge * only(?) VS2000/KA410 has exactly one HDC9224 controller
237 1.1 ragge */
238 1.1 ragge if (vax_boardtype != VAX_BTYP_410) {
239 1.4 christos printf ("unexpected boardtype 0x%x in hdcmatch()\n",
240 1.1 ragge vax_boardtype);
241 1.1 ragge return (0);
242 1.1 ragge }
243 1.1 ragge if (cf->cf_unit != 0)
244 1.1 ragge return (0);
245 1.1 ragge
246 1.1 ragge return (1);
247 1.1 ragge }
248 1.1 ragge
249 1.1 ragge struct hdc_attach_args {
250 1.1 ragge int ha_drive;
251 1.1 ragge };
252 1.1 ragge
253 1.1 ragge int
254 1.1 ragge rdprint(aux, name)
255 1.1 ragge void *aux;
256 1.2 cgd const char *name;
257 1.1 ragge {
258 1.1 ragge struct hdc_attach_args *ha = aux;
259 1.1 ragge
260 1.1 ragge trace(("rdprint(%d, %s)\n", ha->ha_drive, name));
261 1.1 ragge
262 1.1 ragge if (!name)
263 1.4 christos printf (" drive %d", ha->ha_drive);
264 1.1 ragge return (QUIET);
265 1.1 ragge }
266 1.1 ragge
267 1.1 ragge /*
268 1.1 ragge * hdc_attach() probes for all possible devices
269 1.1 ragge */
270 1.1 ragge void
271 1.1 ragge hdcattach(parent, self, aux)
272 1.1 ragge struct device *parent, *self;
273 1.1 ragge void *aux;
274 1.1 ragge {
275 1.1 ragge struct hdcsoftc *sc = (void*)self;
276 1.1 ragge struct confargs *ca = aux;
277 1.1 ragge struct hdc_attach_args ha;
278 1.1 ragge
279 1.1 ragge trace(("hdcattach(0x%x, 0x%x, %s)\n", parent, self, ca->ca_name));
280 1.1 ragge
281 1.4 christos printf ("\n");
282 1.1 ragge /*
283 1.1 ragge * first reset/initialize the controller
284 1.1 ragge */
285 1.1 ragge sc->sc_cfargs = ca;
286 1.1 ragge
287 1.1 ragge sc->sc_ioaddr = ca->ca_ioaddr;
288 1.1 ragge sc->sc_dkc = (void*)uvax_phys2virt(sc->sc_ioaddr);
289 1.1 ragge sc->sc_ibit = ca->ca_intbit;
290 1.1 ragge sc->sc_ivec = ca->ca_intvec;
291 1.1 ragge sc->sc_status = 0;
292 1.1 ragge sc->sc_state = 0;
293 1.1 ragge sc->sc_flags = 0;
294 1.1 ragge sc->sc_errors = 0;
295 1.1 ragge
296 1.1 ragge sc->sc_dkc = (void*)uvax_phys2virt(KA410_DKC_BASE);
297 1.1 ragge sc->sc_dmabase = (void*)uvax_phys2virt(KA410_DMA_BASE);
298 1.1 ragge sc->sc_dmasize = KA410_DMA_SIZE;
299 1.1 ragge
300 1.1 ragge if (hdc_reset(sc) != 0) {
301 1.1 ragge delay(500*1000); /* wait .5 seconds */
302 1.1 ragge if (hdc_reset(sc) != 0)
303 1.4 christos printf ("problems with hdc_reset()...\n");
304 1.1 ragge }
305 1.1 ragge
306 1.1 ragge /*
307 1.1 ragge * now probe for all possible disks
308 1.1 ragge */
309 1.1 ragge for (ha.ha_drive=0; ha.ha_drive<3; ha.ha_drive++)
310 1.1 ragge (void)config_found(self, (void*)&ha, rdprint);
311 1.1 ragge
312 1.1 ragge #ifdef notyet
313 1.1 ragge /*
314 1.1 ragge * now that probing is done, we can register and enable interrupts
315 1.1 ragge */
316 1.1 ragge vsbus_intr_register(XXX);
317 1.1 ragge vsbus_intr_enable(XXX);
318 1.1 ragge #endif
319 1.1 ragge }
320 1.1 ragge
321 1.1 ragge /*
322 1.1 ragge * rdmatch() probes for the existence of a RD-type disk/floppy
323 1.1 ragge */
324 1.1 ragge int
325 1.8 ragge rdmatch(parent, cf, aux)
326 1.1 ragge struct device *parent;
327 1.8 ragge struct cfdata *cf;
328 1.8 ragge void *aux;
329 1.1 ragge {
330 1.1 ragge struct hdcsoftc *hdc = (void*)parent;
331 1.1 ragge struct hdc_attach_args *ha = aux;
332 1.1 ragge int drive = ha->ha_drive;
333 1.1 ragge int res;
334 1.1 ragge
335 1.1 ragge trace(("rdmatch(%d, %d)\n", cf->cf_unit, drive));
336 1.1 ragge
337 1.1 ragge if (cf->cf_unit != ha->ha_drive)
338 1.1 ragge return (0);
339 1.1 ragge
340 1.1 ragge switch (drive) {
341 1.1 ragge case 0:
342 1.1 ragge case 1:
343 1.1 ragge case 2:
344 1.1 ragge res = hdc_select(hdc, drive);
345 1.1 ragge break;
346 1.1 ragge default:
347 1.4 christos printf ("rdmatch: invalid unit-number %d\n", drive);
348 1.1 ragge return (0);
349 1.1 ragge }
350 1.1 ragge
351 1.1 ragge debug (("cstat: %x dstat: %x\n", hdc->sc_sreg.udc_cstat,
352 1.1 ragge hdc->sc_sreg.udc_dstat));
353 1.1 ragge if (drive == 1)
354 1.1 ragge return (0); /* XXX */
355 1.1 ragge
356 1.1 ragge return (1);
357 1.1 ragge }
358 1.1 ragge
359 1.1 ragge void
360 1.1 ragge rdattach(parent, self, aux)
361 1.1 ragge struct device *parent, *self;
362 1.1 ragge void *aux;
363 1.1 ragge {
364 1.1 ragge struct hdcsoftc *hdc = (void*)parent;
365 1.1 ragge struct rdsoftc *rd = (void*)self;
366 1.1 ragge struct hdc_attach_args *ha = aux;
367 1.1 ragge struct rdparams *rp = &rd->sc_param;
368 1.1 ragge
369 1.1 ragge trace(("rdattach(%d)\n", ha->ha_drive));
370 1.1 ragge
371 1.1 ragge rd->sc_drive = ha->ha_drive;
372 1.1 ragge /*
373 1.1 ragge * Initialize and attach the disk structure.
374 1.1 ragge */
375 1.1 ragge rd->sc_dk.dk_driver = &rddkdriver;
376 1.1 ragge rd->sc_dk.dk_name = rd->sc_dev.dv_xname;
377 1.1 ragge disk_attach(&rd->sc_dk);
378 1.1 ragge /*
379 1.1 ragge * if it's not a floppy then evaluate the on-disk geometry.
380 1.1 ragge * if neccessary correct the label...
381 1.1 ragge */
382 1.4 christos printf("\n%s: ", rd->sc_dev.dv_xname);
383 1.1 ragge if (rd->sc_drive == 2) {
384 1.4 christos printf("floppy (RX33)\n");
385 1.1 ragge }
386 1.1 ragge else {
387 1.1 ragge hdc_getdata(hdc, rd, rd->sc_drive);
388 1.9 ragge printf("%s, %ld MB, %ld LBN, %d cyl, %d head, %d sect/track\n",
389 1.1 ragge rp->diskname, rp->diskblks/2048, rp->disklbns,
390 1.1 ragge rp->cylinders, rp->heads, rp->sectors);
391 1.1 ragge }
392 1.6 ragge /*
393 1.6 ragge * Know where we booted from.
394 1.6 ragge */
395 1.6 ragge if ((B_TYPE(bootdev) == BDEV_RD) && (rd->sc_drive == B_UNIT(bootdev)))
396 1.6 ragge booted_from = self;
397 1.1 ragge }
398 1.1 ragge
399 1.9 ragge int hdc_strategy(struct hdcsoftc *, struct rdsoftc *, int, int, int, int, char *);
400 1.1 ragge /*
401 1.1 ragge * Read/write routine for a buffer. For now we poll the controller,
402 1.1 ragge * thus this routine waits for the transfer to complete.
403 1.1 ragge */
404 1.1 ragge void
405 1.1 ragge rdstrategy(bp)
406 1.1 ragge struct buf *bp;
407 1.1 ragge {
408 1.1 ragge struct rdsoftc *rd = rd_cd.cd_devs[HDCUNIT(bp->b_dev)];
409 1.1 ragge struct hdcsoftc *hdc = (void *)rd->sc_dev.dv_parent;
410 1.1 ragge struct partition *p;
411 1.9 ragge int blkno;
412 1.1 ragge
413 1.1 ragge trace (("rdstrategy(#%d/%d)\n", bp->b_blkno, bp->b_bcount));
414 1.1 ragge
415 1.1 ragge /* XXX should make some checks... */
416 1.1 ragge
417 1.1 ragge /*
418 1.1 ragge * If it's a null transfer, return immediatly
419 1.1 ragge */
420 1.1 ragge if (bp->b_bcount == 0)
421 1.1 ragge goto done;
422 1.1 ragge
423 1.1 ragge /*
424 1.1 ragge * what follows now should not be here but in rdstart...
425 1.1 ragge */
426 1.1 ragge /*------------------------------*/
427 1.1 ragge blkno = bp->b_blkno / (rd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
428 1.5 ragge p = &rd->sc_dk.dk_label->d_partitions[HDCPART(bp->b_dev)];
429 1.5 ragge blkno += p->p_offset;
430 1.5 ragge
431 1.1 ragge /* nblks = howmany(bp->b_bcount, sd->sc_dk.dk_label->d_secsize); */
432 1.1 ragge
433 1.1 ragge if (hdc_strategy(hdc, rd, HDCUNIT(bp->b_dev),
434 1.1 ragge ((bp->b_flags & B_READ) ? F_READ : F_WRITE),
435 1.1 ragge blkno, bp->b_bcount, bp->b_data) == 0)
436 1.1 ragge goto done;
437 1.1 ragge /*------------------------------*/
438 1.1 ragge bp->b_flags |= B_ERROR;
439 1.1 ragge done:
440 1.1 ragge /*
441 1.1 ragge * Correctly set the buf to indicate a completed xfer
442 1.1 ragge */
443 1.1 ragge bp->b_resid = 0; /* ??? bertram */
444 1.1 ragge biodone(bp);
445 1.1 ragge }
446 1.1 ragge
447 1.1 ragge int
448 1.1 ragge hdc_strategy(hdc, rd, unit, func, dblk, size, buf)
449 1.1 ragge struct hdcsoftc *hdc;
450 1.1 ragge struct rdsoftc *rd;
451 1.1 ragge int unit;
452 1.1 ragge int func;
453 1.1 ragge int dblk;
454 1.1 ragge int size;
455 1.1 ragge char *buf;
456 1.1 ragge {
457 1.1 ragge struct hdc9224_UDCreg *p = &hdc->sc_creg;
458 1.1 ragge struct disklabel *lp = rd->sc_dk.dk_label;
459 1.1 ragge int sect, head, cyl;
460 1.1 ragge int scount;
461 1.1 ragge int cmd, res = 0;
462 1.1 ragge
463 1.1 ragge trace (("hdc_strategy(%d, %d, %d, %d, 0x%x)\n",
464 1.1 ragge unit, func, dblk, size, buf));
465 1.1 ragge
466 1.1 ragge hdc_select(hdc, unit); /* select drive right now */
467 1.1 ragge
468 1.1 ragge if (unit != 2 && dblk == -1) { /* read the on-disk geometry */
469 1.1 ragge
470 1.1 ragge p->udc_dma7 = 0;
471 1.1 ragge p->udc_dma15 = 0;
472 1.1 ragge p->udc_dma23 = 0;
473 1.1 ragge
474 1.1 ragge p->udc_dsect = 0;
475 1.1 ragge p->udc_dhead = 0;
476 1.1 ragge p->udc_dcyl = 0;
477 1.1 ragge
478 1.1 ragge p->udc_scnt = size/512;
479 1.1 ragge p->udc_rtcnt = 0xF0;
480 1.1 ragge p->udc_mode = 0xC0;
481 1.1 ragge p->udc_term = 0xB4;
482 1.1 ragge
483 1.1 ragge vsbus_lockDMA(hdc->sc_cfargs); /* bertram XXX */
484 1.1 ragge haveLock = 1;
485 1.1 ragge keepLock = 1;
486 1.1 ragge
487 1.1 ragge #ifdef PARANOID
488 1.1 ragge bzero (hdc->sc_dmabase, size); /* clear disk buffer */
489 1.1 ragge #endif
490 1.1 ragge cmd = 0x5C | 0x03; /* bypass bad sectors */
491 1.1 ragge cmd = 0x5C | 0x01; /* terminate if bad sector */
492 1.1 ragge
493 1.1 ragge res = hdc_command (hdc, cmd);
494 1.1 ragge /* hold the locking ! */
495 1.1 ragge bcopy (hdc->sc_dmabase, buf, size); /* copy to buf */
496 1.1 ragge /* now release the locking */
497 1.1 ragge
498 1.1 ragge vsbus_unlockDMA(hdc->sc_cfargs);
499 1.1 ragge haveLock = 0;
500 1.1 ragge keepLock = 0;
501 1.1 ragge
502 1.1 ragge return (res);
503 1.1 ragge }
504 1.1 ragge
505 1.1 ragge scount = size / 512;
506 1.1 ragge while (scount) {
507 1.1 ragge /*
508 1.1 ragge * prepare drive/operation parameter
509 1.1 ragge */
510 1.1 ragge cyl = dblk / lp->d_secpercyl;
511 1.1 ragge sect = dblk % lp->d_secpercyl;
512 1.1 ragge head = sect / lp->d_nsectors;
513 1.1 ragge sect = sect % lp->d_nsectors;
514 1.1 ragge if (unit == 2)
515 1.1 ragge sect++;
516 1.1 ragge else
517 1.1 ragge cyl++; /* first cylinder is reserved */
518 1.1 ragge
519 1.1 ragge size = 512 * min(scount, lp->d_nsectors - sect);
520 1.1 ragge
521 1.1 ragge debug (("hdc_strategy: block #%d ==> s/t/c=%d/%d/%d (%d/%d)\n",
522 1.1 ragge dblk, sect, head, cyl, scount, size));
523 1.1 ragge
524 1.1 ragge /*
525 1.1 ragge * now initialize the register values ...
526 1.1 ragge */
527 1.1 ragge p->udc_dma7 = 0;
528 1.1 ragge p->udc_dma15 = 0;
529 1.1 ragge p->udc_dma23 = 0;
530 1.1 ragge
531 1.1 ragge p->udc_dsect = sect;
532 1.1 ragge head |= (cyl >> 4) & 0x70;
533 1.1 ragge p->udc_dhead = head;
534 1.1 ragge p->udc_dcyl = cyl;
535 1.1 ragge
536 1.1 ragge p->udc_scnt = size/512;
537 1.1 ragge
538 1.1 ragge if (unit == 2) { /* floppy */
539 1.1 ragge p->udc_rtcnt = 0xF2;
540 1.1 ragge p->udc_mode = 0x81; /* RX33 with RX50 media */
541 1.1 ragge p->udc_mode = 0x82; /* RX33 with RX33 media */
542 1.1 ragge p->udc_term = 0xB4;
543 1.1 ragge } else { /* disk */
544 1.1 ragge p->udc_rtcnt = 0xF0;
545 1.1 ragge p->udc_mode = 0xC0;
546 1.1 ragge p->udc_term = 0xB4;
547 1.1 ragge }
548 1.1 ragge
549 1.1 ragge vsbus_lockDMA(hdc->sc_cfargs);
550 1.1 ragge haveLock = 1;
551 1.1 ragge keepLock = 1;
552 1.1 ragge
553 1.1 ragge if (func == F_WRITE) {
554 1.1 ragge bcopy (buf, hdc->sc_dmabase, size); /* copy from buf */
555 1.1 ragge cmd = 0xA0 | (unit==2 ? 1 : 0);
556 1.1 ragge res = hdc_command (hdc, cmd);
557 1.1 ragge }
558 1.1 ragge else {
559 1.1 ragge #ifdef PARANOID
560 1.1 ragge bzero (hdc->sc_dmabase, size); /* clear disk buffer */
561 1.1 ragge #endif
562 1.1 ragge cmd = 0x5C | 0x03; /* bypass bad sectors */
563 1.1 ragge cmd = 0x5C | 0x01; /* terminate if bad sector */
564 1.1 ragge res = hdc_command (hdc, cmd);
565 1.1 ragge bcopy (hdc->sc_dmabase, buf, size); /* copy to buf */
566 1.1 ragge }
567 1.1 ragge
568 1.1 ragge vsbus_unlockDMA(hdc->sc_cfargs);
569 1.1 ragge haveLock = 0;
570 1.1 ragge keepLock = 0;
571 1.1 ragge
572 1.1 ragge scount -= size/512;
573 1.1 ragge dblk += size/512;
574 1.1 ragge buf += size;
575 1.1 ragge }
576 1.1 ragge
577 1.1 ragge if (unit != 2) /* deselect drive, if not floppy */
578 1.1 ragge hdc_command (hdc, DKC_CMD_DRDESELECT);
579 1.1 ragge
580 1.1 ragge return 0;
581 1.1 ragge }
582 1.1 ragge
583 1.1 ragge char hdc_iobuf[17*512]; /* we won't need more */
584 1.1 ragge
585 1.9 ragge void hdc_mid2str(long, char *);
586 1.1 ragge #ifdef DEBUG
587 1.9 ragge void hdc_printgeom( struct rdgeom *);
588 1.1 ragge /*
589 1.1 ragge * display the contents of the on-disk geometry structure
590 1.1 ragge */
591 1.9 ragge void
592 1.1 ragge hdc_printgeom(p)
593 1.1 ragge struct rdgeom *p;
594 1.1 ragge {
595 1.1 ragge char dname[8];
596 1.1 ragge hdc_mid2str(p->media_id, dname);
597 1.1 ragge
598 1.9 ragge printf ("**DiskData** XBNs: %ld, DBNs: %ld, LBNs: %ld, RBNs: %ld\n",
599 1.1 ragge p->xbn_count, p->dbn_count, p->lbn_count, p->rbn_count);
600 1.4 christos printf ("sec/track: %d, tracks: %d, cyl: %d, precomp/reduced: %d/%d\n",
601 1.1 ragge p->nspt, p->ntracks, p->ncylinders, p->precomp, p->reduced);
602 1.4 christos printf ("seek-rate: %d, crc/eec: %s, RCT: %d, RCT-copies: %d\n",
603 1.1 ragge p->seek_rate, p->crc_eec?"EEC":"CRC", p->rct, p->rct_ncopies);
604 1.4 christos printf ("media-ID: %s, interleave: %d, headskew: %d, cylskew: %d\n",
605 1.1 ragge dname, p->interleave, p->headskew, p->cylskew);
606 1.4 christos printf ("gap0: %d, gap1: %d, gap2: %d, gap3: %d, sync-value: %d\n",
607 1.1 ragge p->gap0_size, p->gap1_size, p->gap2_size, p->gap3_size,
608 1.1 ragge p->sync_value);
609 1.1 ragge }
610 1.1 ragge #endif
611 1.1 ragge
612 1.1 ragge /*
613 1.1 ragge * Convert media_id to string/name (encoding is documented in mscp.h)
614 1.1 ragge */
615 1.9 ragge void
616 1.1 ragge hdc_mid2str(media_id, name)
617 1.1 ragge long media_id;
618 1.1 ragge char *name;
619 1.1 ragge {
620 1.1 ragge struct { /* For RD32 this struct holds: */
621 1.1 ragge u_long mt:7; /* number in name: 0x20 == 32 */
622 1.1 ragge u_long a2:5; /* ' ' encoded as 0x0 */
623 1.1 ragge u_long a1:5; /* 'D' encoded with base '@' */
624 1.1 ragge u_long a0:5; /* 'R' encoded with base '@' */
625 1.1 ragge u_long d1:5; /* 'U' encoded with base '@' */
626 1.1 ragge u_long d0:5; /* 'D' encoded with base '@' */
627 1.1 ragge } *p = (void*)&media_id;
628 1.1 ragge
629 1.1 ragge #define MIDCHR(x) (x ? x + '@' : ' ')
630 1.1 ragge
631 1.4 christos sprintf (name, "%c%c%d", MIDCHR(p->a0), MIDCHR(p->a1), p->mt);
632 1.1 ragge }
633 1.1 ragge
634 1.1 ragge int
635 1.1 ragge hdc_getdata(hdc, rd, unit)
636 1.1 ragge struct hdcsoftc *hdc;
637 1.1 ragge struct rdsoftc *rd;
638 1.1 ragge int unit;
639 1.1 ragge {
640 1.1 ragge struct disklabel *lp = rd->sc_dk.dk_label;
641 1.1 ragge struct rdparams *rp = &rd->sc_param;
642 1.1 ragge int res;
643 1.1 ragge
644 1.1 ragge trace (("hdc_getdata(%d)\n", unit));
645 1.1 ragge
646 1.1 ragge bzero(rd->sc_dk.dk_label, sizeof(struct disklabel));
647 1.1 ragge bzero(rd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
648 1.1 ragge
649 1.1 ragge if (unit == 2) {
650 1.1 ragge lp->d_secsize = DEV_BSIZE;
651 1.1 ragge lp->d_ntracks = 2;
652 1.1 ragge lp->d_nsectors = 15;
653 1.1 ragge lp->d_ncylinders = 80;
654 1.1 ragge lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
655 1.1 ragge
656 1.1 ragge return (0);
657 1.1 ragge }
658 1.1 ragge
659 1.1 ragge res = hdc_strategy(hdc, rd, unit, F_READ, -1, 4096, hdc_iobuf);
660 1.1 ragge bcopy (hdc_iobuf, &rd->sc_xbn, sizeof(struct rdgeom));
661 1.1 ragge #ifdef DEBUG
662 1.1 ragge hdc_printgeom(&rd->sc_xbn);
663 1.1 ragge #endif
664 1.1 ragge lp->d_secsize = DEV_BSIZE;
665 1.1 ragge lp->d_ntracks = rd->sc_xbn.ntracks;
666 1.1 ragge lp->d_nsectors = rd->sc_xbn.nspt;
667 1.1 ragge lp->d_ncylinders = rd->sc_xbn.ncylinders;
668 1.1 ragge lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
669 1.1 ragge
670 1.1 ragge rp->cylinders = rd->sc_xbn.ncylinders;
671 1.1 ragge rp->heads = rd->sc_xbn.ntracks;
672 1.1 ragge rp->sectors = rd->sc_xbn.nspt;
673 1.1 ragge rp->diskblks = rp->cylinders * rp->heads * rp->sectors;
674 1.1 ragge rp->disklbns = rd->sc_xbn.lbn_count;
675 1.1 ragge rp->blksize = DEV_BSIZE;
676 1.1 ragge rp->diskbytes = rp->disklbns * rp->blksize;
677 1.1 ragge hdc_mid2str(rd->sc_xbn.media_id, rp->diskname);
678 1.1 ragge
679 1.1 ragge return (0);
680 1.1 ragge }
681 1.1 ragge
682 1.1 ragge int
683 1.1 ragge hdc_getlabel(hdc, rd, unit)
684 1.1 ragge struct hdcsoftc *hdc;
685 1.1 ragge struct rdsoftc *rd;
686 1.1 ragge int unit;
687 1.1 ragge {
688 1.1 ragge struct disklabel *lp = rd->sc_dk.dk_label;
689 1.1 ragge struct disklabel *xp = (void*)(hdc_iobuf + 64);
690 1.1 ragge int res;
691 1.1 ragge
692 1.1 ragge trace (("hdc_getlabel(%d)\n", unit));
693 1.1 ragge
694 1.1 ragge #define LBL_CHECK(x) if (xp->x != lp->x) { \
695 1.4 christos printf ("%d-->%d\n", xp->x, lp->x); \
696 1.1 ragge xp->x = lp->x; \
697 1.1 ragge }
698 1.1 ragge res = hdc_strategy(hdc, rd, unit, F_READ, 0, DEV_BSIZE, hdc_iobuf);
699 1.1 ragge LBL_CHECK(d_secsize);
700 1.1 ragge LBL_CHECK(d_ntracks);
701 1.1 ragge LBL_CHECK(d_nsectors);
702 1.1 ragge LBL_CHECK(d_ncylinders);
703 1.1 ragge LBL_CHECK(d_secpercyl);
704 1.1 ragge bcopy(xp, lp, sizeof(struct disklabel));
705 1.1 ragge
706 1.1 ragge return (0);
707 1.1 ragge }
708 1.1 ragge
709 1.9 ragge bdev_decl(hdc);
710 1.9 ragge int hdcsize(dev_t);
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.9 ragge int
715 1.1 ragge hdcsize(dev)
716 1.1 ragge dev_t dev;
717 1.1 ragge {
718 1.1 ragge int unit = HDCUNIT(dev);
719 1.1 ragge int part = HDCPART(dev);
720 1.1 ragge struct rdsoftc *rd = rd_cd.cd_devs[unit];
721 1.1 ragge int size;
722 1.1 ragge
723 1.1 ragge trace (("hdcsize(%x == %d/%d)\n", dev, unit, part));
724 1.1 ragge
725 1.9 ragge if (hdcopen(dev, 0, S_IFBLK, 0) != 0)
726 1.1 ragge return (-1);
727 1.1 ragge #if 0
728 1.1 ragge if (rd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
729 1.1 ragge size = -1;
730 1.1 ragge else
731 1.1 ragge #endif
732 1.1 ragge size = rd->sc_dk.dk_label->d_partitions[part].p_size;
733 1.9 ragge if (hdcclose(dev, 0, S_IFBLK, 0) != 0)
734 1.1 ragge return (-1);
735 1.1 ragge debug (("hdcsize: size=%d\n", size));
736 1.1 ragge return (size);
737 1.1 ragge }
738 1.1 ragge
739 1.1 ragge /*
740 1.1 ragge *
741 1.1 ragge */
742 1.1 ragge int
743 1.9 ragge hdcopen (dev, flag, fmt, p)
744 1.1 ragge dev_t dev;
745 1.1 ragge int flag;
746 1.1 ragge int fmt;
747 1.9 ragge struct proc *p;
748 1.1 ragge {
749 1.1 ragge int unit = HDCUNIT(dev);
750 1.1 ragge struct hdcsoftc *hdc;
751 1.1 ragge struct rdsoftc *rd;
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.9 ragge hdcclose (dev, flag, type, p)
779 1.1 ragge dev_t dev;
780 1.9 ragge int flag, type;
781 1.9 ragge struct proc *p;
782 1.1 ragge {
783 1.1 ragge trace (("hdcclose()\n"));
784 1.1 ragge return (0);
785 1.1 ragge }
786 1.1 ragge
787 1.1 ragge /*
788 1.1 ragge *
789 1.1 ragge */
790 1.1 ragge void
791 1.1 ragge hdcstrategy(bp)
792 1.1 ragge register struct buf *bp;
793 1.1 ragge {
794 1.1 ragge trace (("hdcstrategy()\n"));
795 1.1 ragge rdstrategy(bp);
796 1.1 ragge debug (("hdcstrategy done.\n"));
797 1.1 ragge }
798 1.1 ragge
799 1.1 ragge /*
800 1.1 ragge *
801 1.1 ragge */
802 1.1 ragge int
803 1.1 ragge hdcioctl(dev, cmd, data, flag, p)
804 1.1 ragge dev_t dev;
805 1.9 ragge u_long cmd;
806 1.1 ragge caddr_t data; /* aka: addr */
807 1.1 ragge int flag;
808 1.1 ragge struct proc *p;
809 1.1 ragge {
810 1.1 ragge struct rdsoftc *rd = rd_cd.cd_devs[HDCUNIT(dev)];
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.9 ragge printf ("IOCTL %lx not implemented.\n", cmd);
870 1.1 ragge return (-1);
871 1.1 ragge }
872 1.1 ragge }
873 1.1 ragge
874 1.9 ragge cdev_decl(hdc);
875 1.1 ragge
876 1.1 ragge /*
877 1.1 ragge *
878 1.1 ragge */
879 1.1 ragge int
880 1.9 ragge hdcread (dev, uio, flag)
881 1.1 ragge dev_t dev;
882 1.1 ragge struct uio *uio;
883 1.9 ragge int flag;
884 1.1 ragge {
885 1.1 ragge trace (("hdcread()\n"));
886 1.1 ragge return (physio (hdcstrategy, NULL, dev, B_READ, minphys, uio));
887 1.1 ragge }
888 1.1 ragge
889 1.1 ragge /*
890 1.1 ragge *
891 1.1 ragge */
892 1.1 ragge int
893 1.9 ragge hdcwrite (dev, uio, flag)
894 1.1 ragge dev_t dev;
895 1.1 ragge struct uio *uio;
896 1.9 ragge int flag;
897 1.1 ragge {
898 1.1 ragge trace (("hdcwrite()\n"));
899 1.1 ragge return (physio (hdcstrategy, NULL, dev, B_WRITE, minphys, uio));
900 1.1 ragge }
901 1.1 ragge
902 1.1 ragge /*
903 1.1 ragge *
904 1.1 ragge */
905 1.1 ragge int
906 1.9 ragge hdcdump(dev, daddr, addr, size)
907 1.1 ragge dev_t dev;
908 1.9 ragge daddr_t daddr;
909 1.9 ragge caddr_t addr;
910 1.9 ragge size_t size;
911 1.1 ragge {
912 1.1 ragge trace (("hdcdump (%x)\n", dev));
913 1.9 ragge return 0;
914 1.1 ragge }
915 1.1 ragge
916 1.9 ragge void hdc_readregs (struct hdcsoftc *);
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.9 ragge void hdc_writeregs( struct hdcsoftc *);
938 1.9 ragge
939 1.1 ragge void
940 1.1 ragge hdc_writeregs(sc)
941 1.1 ragge struct hdcsoftc *sc;
942 1.1 ragge {
943 1.1 ragge int i;
944 1.1 ragge char *p;
945 1.1 ragge
946 1.1 ragge trace(("hdc_writeregs()\n"));
947 1.1 ragge
948 1.1 ragge sc->sc_dkc->dkc_cmd = 0x40; /* set internal counter to zero */
949 1.1 ragge p = (void*)&sc->sc_creg;
950 1.1 ragge for (i=0; i<10; i++)
951 1.1 ragge sc->sc_dkc->dkc_reg = *p++; /* dkc_reg auto-increments */
952 1.1 ragge }
953 1.1 ragge
954 1.1 ragge /*
955 1.1 ragge * hdc_command() issues a command and polls the intreq-register
956 1.1 ragge * to find when command has completed
957 1.1 ragge */
958 1.1 ragge int
959 1.1 ragge hdc_command(sc, cmd)
960 1.1 ragge struct hdcsoftc *sc;
961 1.1 ragge int cmd;
962 1.1 ragge {
963 1.1 ragge volatile u_char *intreq = (void*)uvax_phys2virt(KA410_INTREQ);
964 1.1 ragge volatile u_char *intclr = (void*)uvax_phys2virt(KA410_INTCLR);
965 1.1 ragge int i, c;
966 1.1 ragge
967 1.1 ragge trace (("hdc_command(%x)\n", cmd));
968 1.1 ragge debug (("intr-state: %x %x %x\n", *intreq, *intclr, *intmsk));
969 1.1 ragge
970 1.1 ragge if (!haveLock) {
971 1.1 ragge vsbus_lockDMA(sc->sc_cfargs);
972 1.1 ragge haveLock = 1;
973 1.1 ragge }
974 1.1 ragge
975 1.1 ragge hdc_writeregs(sc); /* write the prepared registers */
976 1.1 ragge *intclr = INTR_DC; /* clear any old interrupt */
977 1.1 ragge sc->sc_dkc->dkc_cmd = cmd; /* issue the command */
978 1.1 ragge for (i=0; i<MAX_WAIT; i++) {
979 1.1 ragge if ((c = *intreq) & INTR_DC)
980 1.1 ragge break;
981 1.1 ragge }
982 1.1 ragge if ((c & INTR_DC) == 0) {
983 1.4 christos printf ("hdc_command: timeout in command 0x%x\n", cmd);
984 1.1 ragge }
985 1.1 ragge hdc_readregs(sc); /* read the status registers */
986 1.1 ragge sc->sc_status = sc->sc_dkc->dkc_stat;
987 1.1 ragge
988 1.1 ragge if (!keepLock) {
989 1.1 ragge vsbus_unlockDMA(sc->sc_cfargs);
990 1.1 ragge haveLock = 0;
991 1.1 ragge }
992 1.1 ragge
993 1.9 ragge if (sc->sc_status != (DKC_ST_DONE|DKC_TC_SUCCESS)) {
994 1.4 christos printf ("command 0x%x completed with status 0x%x\n",
995 1.1 ragge cmd, sc->sc_status);
996 1.1 ragge return (-1);
997 1.1 ragge }
998 1.1 ragge return (0);
999 1.1 ragge }
1000 1.1 ragge
1001 1.1 ragge /*
1002 1.1 ragge * writing zero into the command-register will reset the controller.
1003 1.1 ragge * This will not interrupt data-transfer commands!
1004 1.1 ragge * Also no interrupt is generated, thus we don't use hdc_command()
1005 1.1 ragge */
1006 1.1 ragge int
1007 1.1 ragge hdc_reset(sc)
1008 1.1 ragge struct hdcsoftc *sc;
1009 1.1 ragge {
1010 1.1 ragge trace (("hdc_reset()\n"));
1011 1.1 ragge
1012 1.1 ragge sc->sc_dkc->dkc_cmd = DKC_CMD_RESET; /* issue RESET command */
1013 1.1 ragge hdc_readregs(sc); /* read the status registers */
1014 1.1 ragge sc->sc_status = sc->sc_dkc->dkc_stat;
1015 1.9 ragge if (sc->sc_status != (DKC_ST_DONE|DKC_TC_SUCCESS)) {
1016 1.4 christos printf ("RESET command completed with status 0x%x\n",
1017 1.1 ragge sc->sc_status);
1018 1.1 ragge return (-1);
1019 1.1 ragge }
1020 1.1 ragge return (0);
1021 1.1 ragge }
1022 1.1 ragge
1023 1.9 ragge int hdc_rxselect(struct hdcsoftc *, int);
1024 1.9 ragge
1025 1.1 ragge int
1026 1.1 ragge hdc_rxselect(sc, unit)
1027 1.1 ragge struct hdcsoftc *sc;
1028 1.1 ragge int unit;
1029 1.1 ragge {
1030 1.1 ragge register struct hdc9224_UDCreg *p = &sc->sc_creg;
1031 1.1 ragge register struct hdc9224_UDCreg *q = &sc->sc_sreg;
1032 1.1 ragge int error;
1033 1.1 ragge
1034 1.1 ragge /*
1035 1.1 ragge * bring command-regs in some known-to-work state and
1036 1.1 ragge * select the drive with the DRIVE SELECT command.
1037 1.1 ragge */
1038 1.1 ragge p->udc_dma7 = 0;
1039 1.1 ragge p->udc_dma15 = 0;
1040 1.1 ragge p->udc_dma23 = 0;
1041 1.1 ragge p->udc_dsect = 1; /* sectors are numbered 1..15 !!! */
1042 1.1 ragge p->udc_dhead = 0;
1043 1.1 ragge p->udc_dcyl = 0;
1044 1.1 ragge p->udc_scnt = 0;
1045 1.1 ragge
1046 1.1 ragge p->udc_rtcnt = UDC_RC_RX33READ;
1047 1.1 ragge p->udc_mode = UDC_MD_RX33;
1048 1.1 ragge p->udc_term = UDC_TC_FDD;
1049 1.1 ragge
1050 1.1 ragge /*
1051 1.1 ragge * this is ...
1052 1.1 ragge */
1053 1.1 ragge error = hdc_command (sc, DKC_CMD_DRSEL_RX33 | unit);
1054 1.1 ragge
1055 1.9 ragge if ((error != 0) || ((q->udc_dstat & UDC_DS_READY) == 0)) {
1056 1.4 christos printf("\nfloppy-drive not ready (new floppy inserted?)\n\n");
1057 1.1 ragge p->udc_rtcnt &= ~UDC_RC_INVRDY; /* clear INVRDY-flag */
1058 1.1 ragge error = hdc_command(sc, DKC_CMD_DRSEL_RX33 | unit);
1059 1.9 ragge if ((error != 0) || ((q->udc_dstat & UDC_DS_READY) == 0)) {
1060 1.4 christos printf("diskette not ready(1): %x/%x\n", error, q->udc_dstat);
1061 1.4 christos printf("floppy-drive offline?\n");
1062 1.1 ragge return (-1);
1063 1.1 ragge }
1064 1.1 ragge
1065 1.1 ragge if (q->udc_dstat & UDC_DS_TRK00) /* if track-0 */
1066 1.1 ragge error = hdc_command(sc, DKC_CMD_STEPIN_FDD); /* step inwards */
1067 1.1 ragge else /* else */
1068 1.1 ragge error = hdc_command(sc, DKC_CMD_STEPOUT_FDD); /* step outwards */
1069 1.1 ragge
1070 1.9 ragge if ((error != 0) || ((q->udc_dstat & UDC_DS_READY) == UDC_DS_READY)) {
1071 1.4 christos printf("diskette not ready(2): %x/%x\n", error, q->udc_dstat);
1072 1.4 christos printf("No floppy inserted or drive offline\n");
1073 1.1 ragge /* return (-1); */
1074 1.1 ragge }
1075 1.1 ragge
1076 1.1 ragge p->udc_rtcnt |= UDC_RC_INVRDY;
1077 1.1 ragge error = hdc_command(sc, DKC_CMD_DRSEL_RX33 | unit);
1078 1.9 ragge if ((error != 0) || ((q->udc_dstat & UDC_DS_READY) == 0)) {
1079 1.4 christos printf("diskette not ready(3): %x/%x\n", error, q->udc_dstat);
1080 1.4 christos printf("no floppy inserted or floppy-door open\n");
1081 1.1 ragge return(-1);
1082 1.1 ragge }
1083 1.4 christos printf("floppy-drive reselected.\n");
1084 1.1 ragge }
1085 1.1 ragge if (error)
1086 1.1 ragge error = hdc_command (sc, DKC_CMD_DRSEL_RX33 | unit);
1087 1.1 ragge
1088 1.1 ragge return (error);
1089 1.1 ragge }
1090 1.1 ragge
1091 1.9 ragge int hdc_rdselect (struct hdcsoftc *, int);
1092 1.9 ragge
1093 1.1 ragge int
1094 1.1 ragge hdc_rdselect(sc, unit)
1095 1.1 ragge struct hdcsoftc *sc;
1096 1.1 ragge int unit;
1097 1.1 ragge {
1098 1.1 ragge register struct hdc9224_UDCreg *p = &sc->sc_creg;
1099 1.1 ragge int error;
1100 1.1 ragge
1101 1.1 ragge /*
1102 1.1 ragge * bring "creg" in some known-to-work state and
1103 1.1 ragge * select the drive with the DRIVE SELECT command.
1104 1.1 ragge */
1105 1.1 ragge p->udc_dma7 = 0;
1106 1.1 ragge p->udc_dma15 = 0;
1107 1.1 ragge p->udc_dma23 = 0;
1108 1.1 ragge p->udc_dsect = 0; /* sectors are numbered 0..16 */
1109 1.1 ragge p->udc_dhead = 0;
1110 1.1 ragge p->udc_dcyl = 0;
1111 1.1 ragge p->udc_scnt = 0;
1112 1.1 ragge
1113 1.1 ragge p->udc_rtcnt = UDC_RC_HDD_READ;
1114 1.1 ragge p->udc_mode = UDC_MD_HDD;
1115 1.1 ragge p->udc_term = UDC_TC_HDD;
1116 1.1 ragge
1117 1.1 ragge error = hdc_command (sc, DKC_CMD_DRSEL_HDD | unit);
1118 1.1 ragge if (error)
1119 1.1 ragge error = hdc_command (sc, DKC_CMD_DRSEL_HDD | unit);
1120 1.1 ragge
1121 1.1 ragge return (error);
1122 1.1 ragge }
1123 1.1 ragge
1124 1.1 ragge /*
1125 1.1 ragge * bring command-regs into some known-to-work state and select
1126 1.1 ragge * the drive with the DRIVE SELECT command.
1127 1.1 ragge */
1128 1.1 ragge int
1129 1.1 ragge hdc_select(sc, unit)
1130 1.1 ragge struct hdcsoftc *sc;
1131 1.1 ragge int unit;
1132 1.1 ragge {
1133 1.1 ragge int error;
1134 1.1 ragge
1135 1.1 ragge trace (("hdc_select(%x,%d)\n", sc, unit));
1136 1.1 ragge
1137 1.1 ragge switch (unit) {
1138 1.1 ragge case 0:
1139 1.1 ragge case 1:
1140 1.1 ragge error = hdc_rdselect(sc, unit);
1141 1.1 ragge break;
1142 1.1 ragge case 2:
1143 1.1 ragge error = hdc_rxselect(sc, unit);
1144 1.1 ragge /* bertram: delay ??? XXX */
1145 1.1 ragge break;
1146 1.1 ragge default:
1147 1.4 christos printf("invalid unit %d in hdc_select()\n", unit);
1148 1.1 ragge error = -1;
1149 1.1 ragge }
1150 1.1 ragge
1151 1.1 ragge return (error);
1152 1.1 ragge }
1153 1.1 ragge #endif /* NHDC > 0 */
1154