|
|
1.1 root 1: /*-
2: * Copyright (c) 1990 The Regents of the University of California.
3: * All rights reserved.
4: *
5: * This code is derived from software contributed to Berkeley by
6: * Don Ahn.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: *
36: * @(#)fd.c 7.3 (Berkeley) 5/25/91
37: */
38:
39: /****************************************************************************/
40: /* standalone fd driver */
41: /****************************************************************************/
42: #include "param.h"
1.1.1.2 ! root 43: #include "disklabel.h"
1.1 root 44: #include "i386/isa/fdreg.h"
45: #include "i386/isa/isa.h"
46: #include "saio.h"
47:
48: #define NUMRETRY 10
49: /*#define FDDEBUG*/
50:
51: #define NFD 2
52: #define FDBLK 512
53: #define NUMTYPES 4
54:
1.1.1.2 ! root 55: extern struct disklabel disklabel;
! 56:
1.1 root 57: struct fd_type {
58: int sectrac; /* sectors per track */
59: int secsize; /* size code for sectors */
60: int datalen; /* data len when secsize = 0 */
61: int gap; /* gap len between sectors */
62: int tracks; /* total num of tracks */
63: int size; /* size of disk in sectors */
64: int steptrac; /* steps per cylinder */
65: int trans; /* transfer speed code */
66: };
67:
68: struct fd_type fd_types[NUMTYPES] = {
69: { 18,2,0xFF,0x1B,80,2880,1,0 }, /* 1.44 meg HD 3.5in floppy */
70: { 15,2,0xFF,0x1B,80,2400,1,0 }, /* 1.2 meg HD floppy */
71: { 9,2,0xFF,0x23,40,720,2,1 }, /* 360k floppy in 1.2meg drive */
72: { 9,2,0xFF,0x2A,40,720,1,1 }, /* 360k floppy in DD drive */
73: };
74:
75:
76: /* state needed for current transfer */
77: static int fd_type;
78: static int fd_motor;
79: static int fd_retry;
80: static int fd_drive;
81: static int fd_status[7];
82:
83: static int fdc = IO_FD1; /* floppy disk base */
84:
85: /* Make sure DMA buffer doesn't cross 64k boundary */
86: char bounce[FDBLK];
87:
88:
89: /****************************************************************************/
90: /* fdstrategy */
91: /****************************************************************************/
92: int
93: fdstrategy(io,func)
94: register struct iob *io;
95: int func;
96: {
97: char *address;
98: long nblocks,blknum;
99: int unit, iosize;
100:
101: #ifdef FDDEBUG
102: printf("fdstrat ");
103: #endif
104: unit = io->i_unit;
105: fd_type = io->i_part;
106:
107: /*
108: * Set up block calculations.
109: */
110: iosize = io->i_cc / FDBLK;
111: blknum = (unsigned long) io->i_bn * DEV_BSIZE / FDBLK;
1.1.1.2 ! root 112: nblocks = disklabel.d_secperunit;
1.1 root 113: if ((blknum + iosize > nblocks) || blknum < 0) {
1.1.1.2 ! root 114: #ifdef SMALL
1.1 root 115: printf("bn = %d; sectors = %d; type = %d; fssize = %d ",
116: blknum, iosize, fd_type, nblocks);
117: printf("fdstrategy - I/O out of filesystem boundaries\n");
118: #endif
119: return(-1);
120: }
121:
122: address = io->i_ma;
123: while (iosize > 0) {
1.1.1.2 ! root 124: /*printf("iosize %d ", iosize);*/
1.1 root 125: if (fdio(func, unit, blknum, address))
126: return(-1);
127: iosize--;
128: blknum++;
129: address += FDBLK;
130: }
131: return(io->i_cc);
132: }
133:
134: int
135: fdio(func, unit, blknum, address)
136: int func,unit,blknum;
137: char *address;
138: {
139: int i,j,cyl,sectrac,sec,head,numretry;
140: struct fd_type *ft;
141:
1.1.1.2 ! root 142: /*printf("fdio ");*/
1.1 root 143: ft = &fd_types[fd_type];
144:
1.1.1.2 ! root 145: sectrac = disklabel.d_nsectors;
! 146: cyl = blknum / disklabel.d_secpercyl;
1.1 root 147: numretry = NUMRETRY;
148:
149: if (func == F_WRITE) bcopy(address,bounce,FDBLK);
150:
151: retry:
152: out_fdc(15); /* Seek function */
153: out_fdc(unit); /* Drive number */
154: out_fdc(cyl);
155:
156: waitio();
157:
158: out_fdc(0x8);
159: i = in_fdc(); j = in_fdc();
160: if (!(i&0x20) || (cyl != j)) {
161: numretry--;
162: if (numretry) goto retry;
1.1.1.2 ! root 163: #ifdef SMALL
1.1 root 164: printf("Seek error %d, req = %d, at = %d\n",i,cyl,j);
165: printf("unit %d, type %d, sectrac %d, blknum %d\n",
166: unit,fd_type,sectrac,blknum);
167: #endif
168: return -1;
169: }
170:
171: /* set up transfer */
172: fd_dma(func == F_READ, bounce, FDBLK);
1.1.1.2 ! root 173: sec = blknum % disklabel.d_secpercyl;
1.1 root 174: head = sec / sectrac;
175: sec = sec % sectrac + 1;
176: #ifdef FDDEBUG
177: printf("sec %d hd %d cyl %d ", sec, head, cyl);
178: #endif
179:
180: if (func == F_READ) out_fdc(0xE6);/* READ */
181: else out_fdc(0xC5); /* WRITE */
182: out_fdc(head << 2 | fd_drive); /* head & unit */
183: out_fdc(cyl); /* track */
184: out_fdc(head);
185: out_fdc(sec); /* sector XXX +1? */
186: out_fdc(ft->secsize); /* sector size */
187: out_fdc(sectrac); /* sectors/track */
188: out_fdc(ft->gap); /* gap size */
189: out_fdc(ft->datalen); /* data length */
190:
191: waitio();
192:
193: for(i=0;i<7;i++) {
194: fd_status[i] = in_fdc();
195: }
196: if (fd_status[0]&0xF8) {
197: numretry--;
1.1.1.2 ! root 198: #ifdef SMALL
1.1 root 199: printf("FD err %lx %lx %lx %lx %lx %lx %lx\n",
200: fd_status[0], fd_status[1], fd_status[2], fd_status[3],
201: fd_status[4], fd_status[5], fd_status[6] );
202: #endif
1.1.1.2 ! root 203: if (numretry) goto retry;
1.1 root 204: return -1;
205: }
206: if (func == F_READ) bcopy(bounce,address,FDBLK);
207: return 0;
208: }
209:
210: /****************************************************************************/
211: /* fdc in/out */
212: /****************************************************************************/
213: int
214: in_fdc()
215: {
216: int i;
217: while ((i = inb(fdc+fdsts) & 192) != 192) if (i == 128) return -1;
218: return inb(0x3f5);
219: }
220:
221: dump_stat()
222: {
223: int i;
224: for(i=0;i<7;i++) {
225: fd_status[i] = in_fdc();
226: if (fd_status[i] < 0) break;
227: }
228: #ifdef FDDEBUGx
229: printf("FD bad status :%lx %lx %lx %lx %lx %lx %lx\n",
230: fd_status[0], fd_status[1], fd_status[2], fd_status[3],
231: fd_status[4], fd_status[5], fd_status[6] );
232: #endif
233: }
234:
235: set_intr()
236: {
237: /* initialize 8259's */
238: outb(0x20,0x11);
239: outb(0x21,32);
240: outb(0x21,4);
241: outb(0x21,1);
242: outb(0x21,0x0f); /* turn on int 6 */
243:
244: /*
245: outb(0xa0,0x11);
246: outb(0xa1,40);
247: outb(0xa1,2);
248: outb(0xa1,1);
249: outb(0xa1,0xff); */
250:
251: }
252:
253:
254:
255: waitio()
256: {
257: char c;
258: int n;
259:
260: do
261: outb(0x20,0xc); /* read polled interrupt */
262: while ((c=inb(0x20))&0x7f != 6); /* wait for int */
263: outb(0x20,0x20);
264: }
265:
266: out_fdc(x)
267: int x;
268: {
269: int r;
270: do {
271: r = (inb(fdc+fdsts) & 192);
272: if (r==128) break;
273: if (r==192) {
274: dump_stat(); /* error: direction. eat up output */
275: }
276: } while (1);
277: outb(0x3f5,x&0xFF);
278: }
279:
280:
281: /****************************************************************************/
282: /* fdopen/fdclose */
283: /****************************************************************************/
284: fdopen(io)
285: register struct iob *io;
286: {
287: int unit, type, i;
288: struct fd_type *ft;
289:
290: unit = io->i_unit;
291: type = io->i_part;
292: io->i_boff = 0; /* no disklabels -- tar/dump wont work */
293: #ifdef FDDEBUG
294: printf("fdopen %d %d ", unit, type);
295: #endif
296: ft = &fd_types[type];
297: fd_drive = unit;
298:
299: set_intr(); /* init intr cont */
300:
301: /* Try a reset, keep motor on */
302: outb(0x3f2,0);
303: for(i=0; i < 100000; i++);
304: outb(0x3f2,unit | (unit ? 32 : 16) );
305: for(i=0; i < 100000; i++);
306: outb(0x3f2,unit | 0xC | (unit ? 32 : 16) );
307: outb(0x3f7,ft->trans);
308: fd_motor = 1;
309:
310: waitio();
311:
312: out_fdc(3); /* specify command */
313: out_fdc(0xDF);
314: out_fdc(2);
315:
316: out_fdc(7); /* Recalibrate Function */
317: out_fdc(unit);
318:
319: waitio();
320: return(0);
321: }
322:
323:
324: /****************************************************************************/
325: /* fd_dma */
326: /* set up DMA read/write operation and virtual address addr for nbytes */
327: /****************************************************************************/
328: fd_dma(read,addr,nbytes)
329: int read;
330: unsigned long addr;
331: int nbytes;
332: {
333: /* Set read/write bytes */
334: if (read) {
335: outb(0xC,0x46); outb(0xB,0x46);
336: } else {
337: outb(0xC,0x4A); outb(0xB,0x4A);
338: }
339: /* Send start address */
340: outb(0x4,addr & 0xFF);
341: outb(0x4,(addr>>8) & 0xFF);
342: outb(0x81,(addr>>16) & 0xFF);
343: /* Send count */
344: nbytes--;
345: outb(0x5,nbytes & 0xFF);
346: outb(0x5,(nbytes>>8) & 0xFF);
347: /* set channel 2 */
348: outb(0x0A,2);
349: }
350:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.