|
|
1.1 root 1: /*
2: * linux/kernel/floppy.c
3: *
4: * (C) 1991 Linus Torvalds
5: */
6:
7: /*
1.1.1.2 root 8: * 02.12.91 - Changed to static variables to indicate need for reset
9: * and recalibrate. This makes some things easier (output_byte reset
10: * checking etc), and means less interrupt jumping in case of errors,
11: * so the code is hopefully easier to understand.
12: */
13:
14: /*
15: * This file is certainly a mess. I've tried my best to get it working,
16: * but I don't like programming floppies, and I have only one anyway.
17: * Urgel. I should check for more errors, and do more graceful error
18: * recovery. Seems there are problems with several drives. I've tried to
19: * correct them. No promises.
20: */
21:
22: /*
1.1 root 23: * As with hd.c, all routines within this file can (and will) be called
24: * by interrupts, so extreme caution is needed. A hardware interrupt
25: * handler may not sleep, or a kernel panic will happen. Thus I cannot
26: * call "floppy-on" directly, but have to set a special timer interrupt
27: * etc.
1.1.1.4 ! root 28: */
! 29:
! 30: /*
! 31: * 28.02.92 - made track-buffering routines, based on the routines written
! 32: * by [email protected] (Lawrence Foard). Linus.
1.1 root 33: */
34:
35: #include <linux/sched.h>
36: #include <linux/fs.h>
37: #include <linux/kernel.h>
38: #include <linux/fdreg.h>
39: #include <asm/system.h>
40: #include <asm/io.h>
41: #include <asm/segment.h>
42:
43: #define MAJOR_NR 2
44: #include "blk.h"
45:
1.1.1.2 root 46: static int recalibrate = 0;
47: static int reset = 0;
48: static int seek = 0;
1.1 root 49:
50: extern unsigned char current_DOR;
51:
52: #define immoutb_p(val,port) \
53: __asm__("outb %0,%1\n\tjmp 1f\n1:\tjmp 1f\n1:"::"a" ((char) (val)),"i" (port))
54:
55: #define TYPE(x) ((x)>>2)
56: #define DRIVE(x) ((x)&0x03)
57: /*
1.1.1.4 ! root 58: * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
! 59: * max X times - some types of errors increase the errorcount by 2,
! 60: * so we might actually retry only X/2 times before giving up.
1.1 root 61: */
1.1.1.4 ! root 62: #define MAX_ERRORS 12
1.1 root 63:
64: /*
65: * globals used by 'result()'
66: */
67: #define MAX_REPLIES 7
68: static unsigned char reply_buffer[MAX_REPLIES];
69: #define ST0 (reply_buffer[0])
70: #define ST1 (reply_buffer[1])
71: #define ST2 (reply_buffer[2])
72: #define ST3 (reply_buffer[3])
73:
74: /*
75: * This struct defines the different floppy types. Unlike minix
76: * linux doesn't have a "search for right type"-type, as the code
1.1.1.2 root 77: * for that is convoluted and weird. I've got enough problems with
78: * this driver as it is.
1.1 root 79: *
80: * The 'stretch' tells if the tracks need to be boubled for some
81: * types (ie 360kB diskette in 1.2MB drive etc). Others should
82: * be self-explanatory.
83: */
1.1.1.4 ! root 84: struct floppy_struct {
1.1.1.2 root 85: unsigned int size, sect, head, track, stretch;
1.1 root 86: unsigned char gap,rate,spec1;
1.1.1.4 ! root 87: };
! 88:
! 89: static struct floppy_struct floppy_type[] = {
1.1 root 90: { 0, 0,0, 0,0,0x00,0x00,0x00 }, /* no testing */
91: { 720, 9,2,40,0,0x2A,0x02,0xDF }, /* 360kB PC diskettes */
92: { 2400,15,2,80,0,0x1B,0x00,0xDF }, /* 1.2 MB AT-diskettes */
93: { 720, 9,2,40,1,0x2A,0x02,0xDF }, /* 360kB in 720kB drive */
94: { 1440, 9,2,80,0,0x2A,0x02,0xDF }, /* 3.5" 720kB diskette */
95: { 720, 9,2,40,1,0x23,0x01,0xDF }, /* 360kB in 1.2MB drive */
96: { 1440, 9,2,80,0,0x23,0x01,0xDF }, /* 720kB in 1.2MB drive */
97: { 2880,18,2,80,0,0x1B,0x00,0xCF }, /* 1.44MB diskette */
98: };
1.1.1.3 root 99:
1.1 root 100: /*
101: * Rate is 0 for 500kb/s, 2 for 300kbps, 1 for 250kbps
102: * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
103: * H is head unload time (1=16ms, 2=32ms, etc)
104: *
105: * Spec2 is (HLD<<1 | ND), where HLD is head load time (1=2ms, 2=4 ms etc)
106: * and ND is set means no DMA. Hardcoded to 6 (HLD=6ms, use DMA).
107: */
108:
109: extern void floppy_interrupt(void);
110: extern char tmp_floppy_area[1024];
1.1.1.4 ! root 111: extern char floppy_track_buffer[512*2*18];
1.1 root 112:
113: /*
114: * These are global variables, as that's the easiest way to give
115: * information to interrupts. They are the data used for the current
116: * request.
117: */
1.1.1.4 ! root 118: static int read_track = 0; /* flag to indicate if we want to read all track */
! 119: static int buffer_track = -1;
! 120: static int buffer_drive = -1;
1.1 root 121: static int cur_spec1 = -1;
122: static int cur_rate = -1;
123: static struct floppy_struct * floppy = floppy_type;
124: static unsigned char current_drive = 0;
125: static unsigned char sector = 0;
126: static unsigned char head = 0;
127: static unsigned char track = 0;
128: static unsigned char seek_track = 0;
1.1.1.2 root 129: static unsigned char current_track = 255;
1.1 root 130: static unsigned char command = 0;
1.1.1.2 root 131: unsigned char selected = 0;
132: struct task_struct * wait_on_floppy_select = NULL;
133:
134: void floppy_deselect(unsigned int nr)
135: {
136: if (nr != (current_DOR & 3))
137: printk("floppy_deselect: drive not selected\n\r");
138: selected = 0;
139: wake_up(&wait_on_floppy_select);
140: }
1.1 root 141:
142: /*
143: * floppy-change is never called from an interrupt, so we can relax a bit
1.1.1.2 root 144: * here, sleep etc. Note that floppy-on tries to set current_DOR to point
145: * to the desired drive, but it will probably not survive the sleep if
146: * several floppies are used at the same time: thus the loop.
1.1 root 147: */
148: int floppy_change(unsigned int nr)
149: {
1.1.1.2 root 150: repeat:
1.1 root 151: floppy_on(nr);
1.1.1.2 root 152: while ((current_DOR & 3) != nr && selected)
1.1.1.3 root 153: sleep_on(&wait_on_floppy_select);
1.1.1.2 root 154: if ((current_DOR & 3) != nr)
155: goto repeat;
1.1 root 156: if (inb(FD_DIR) & 0x80) {
157: floppy_off(nr);
1.1.1.4 ! root 158: buffer_track = -1;
1.1 root 159: return 1;
160: }
161: floppy_off(nr);
162: return 0;
163: }
164:
165: #define copy_buffer(from,to) \
166: __asm__("cld ; rep ; movsl" \
167: ::"c" (BLOCK_SIZE/4),"S" ((long)(from)),"D" ((long)(to)) \
168: :"cx","di","si")
169:
170: static void setup_DMA(void)
171: {
1.1.1.4 ! root 172: unsigned long addr = (long) CURRENT->buffer;
! 173: unsigned long count = 1024;
1.1 root 174:
1.1.1.2 root 175: cli();
1.1.1.4 ! root 176: if (read_track) {
! 177: /* mark buffer-track bad, in case all this fails.. */
! 178: buffer_drive = buffer_track = -1;
! 179: count = floppy->sect*2*512;
! 180: addr = (long) floppy_track_buffer;
! 181: } else if (addr >= 0x100000) {
1.1 root 182: addr = (long) tmp_floppy_area;
183: if (command == FD_WRITE)
184: copy_buffer(CURRENT->buffer,tmp_floppy_area);
185: }
186: /* mask DMA 2 */
187: immoutb_p(4|2,10);
188: /* output command byte. I don't know why, but everyone (minix, */
189: /* sanches & canton) output this twice, first to 12 then to 11 */
190: __asm__("outb %%al,$12\n\tjmp 1f\n1:\tjmp 1f\n1:\t"
191: "outb %%al,$11\n\tjmp 1f\n1:\tjmp 1f\n1:"::
192: "a" ((char) ((command == FD_READ)?DMA_READ:DMA_WRITE)));
193: /* 8 low bits of addr */
194: immoutb_p(addr,4);
195: addr >>= 8;
196: /* bits 8-15 of addr */
197: immoutb_p(addr,4);
198: addr >>= 8;
199: /* bits 16-19 of addr */
200: immoutb_p(addr,0x81);
1.1.1.4 ! root 201: /* low 8 bits of count-1 */
! 202: count--;
! 203: immoutb_p(count,5);
! 204: count >>= 8;
1.1 root 205: /* high 8 bits of count-1 */
1.1.1.4 ! root 206: immoutb_p(count,5);
1.1 root 207: /* activate DMA 2 */
208: immoutb_p(0|2,10);
1.1.1.2 root 209: sti();
1.1 root 210: }
211:
212: static void output_byte(char byte)
213: {
214: int counter;
215: unsigned char status;
216:
1.1.1.2 root 217: if (reset)
218: return;
1.1 root 219: for(counter = 0 ; counter < 10000 ; counter++) {
1.1.1.2 root 220: status = inb_p(FD_STATUS) & (STATUS_READY | STATUS_DIR);
1.1 root 221: if (status == STATUS_READY) {
222: outb(byte,FD_DATA);
223: return;
224: }
225: }
1.1.1.2 root 226: reset = 1;
1.1 root 227: printk("Unable to send byte to FDC\n\r");
228: }
229:
230: static int result(void)
231: {
232: int i = 0, counter, status;
233:
1.1.1.2 root 234: if (reset)
235: return -1;
1.1 root 236: for (counter = 0 ; counter < 10000 ; counter++) {
1.1.1.2 root 237: status = inb_p(FD_STATUS)&(STATUS_DIR|STATUS_READY|STATUS_BUSY);
1.1 root 238: if (status == STATUS_READY)
239: return i;
240: if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY)) {
241: if (i >= MAX_REPLIES)
242: break;
1.1.1.2 root 243: reply_buffer[i++] = inb_p(FD_DATA);
1.1 root 244: }
245: }
1.1.1.2 root 246: reset = 1;
1.1 root 247: printk("Getstatus times out\n\r");
248: return -1;
249: }
250:
1.1.1.2 root 251: static void bad_flp_intr(void)
252: {
1.1.1.4 ! root 253: current_track = -1;
1.1.1.2 root 254: CURRENT->errors++;
255: if (CURRENT->errors > MAX_ERRORS) {
256: floppy_deselect(current_drive);
257: end_request(0);
258: }
259: if (CURRENT->errors > MAX_ERRORS/2)
260: reset = 1;
261: }
262:
1.1 root 263: /*
1.1.1.2 root 264: * Ok, this interrupt is called after a DMA read/write has succeeded,
265: * so we check the results, and copy any buffers.
1.1 root 266: */
1.1.1.2 root 267: static void rw_interrupt(void)
1.1 root 268: {
1.1.1.4 ! root 269: char * buffer_area;
! 270:
1.1.1.2 root 271: if (result() != 7 || (ST0 & 0xf8) || (ST1 & 0xbf) || (ST2 & 0x73)) {
272: if (ST1 & 0x02) {
273: printk("Drive %d is write protected\n\r",current_drive);
1.1 root 274: floppy_deselect(current_drive);
275: end_request(0);
1.1.1.2 root 276: } else
277: bad_flp_intr();
278: do_fd_request();
1.1 root 279: return;
280: }
1.1.1.4 ! root 281: if (read_track) {
! 282: buffer_track = seek_track;
! 283: buffer_drive = current_drive;
! 284: buffer_area = floppy_track_buffer +
! 285: ((sector-1 + head*floppy->sect)<<9);
! 286: copy_buffer(buffer_area,CURRENT->buffer);
! 287: } else if (command == FD_READ &&
! 288: (unsigned long)(CURRENT->buffer) >= 0x100000)
1.1.1.2 root 289: copy_buffer(tmp_floppy_area,CURRENT->buffer);
290: floppy_deselect(current_drive);
291: end_request(1);
292: do_fd_request();
293: }
294:
1.1.1.4 ! root 295: /*
! 296: * We try to read tracks, but if we get too many errors, we
! 297: * go back to reading just one sector at a time.
! 298: *
! 299: * This means we should be able to read a sector even if there
! 300: * are other bad sectors on this track.
! 301: */
1.1.1.2 root 302: inline void setup_rw_floppy(void)
303: {
1.1 root 304: setup_DMA();
305: do_floppy = rw_interrupt;
306: output_byte(command);
1.1.1.4 ! root 307: if (read_track) {
! 308: output_byte(current_drive);
! 309: output_byte(track);
! 310: output_byte(0);
! 311: output_byte(1);
! 312: } else {
! 313: output_byte(head<<2 | current_drive);
! 314: output_byte(track);
! 315: output_byte(head);
! 316: output_byte(sector);
! 317: }
1.1 root 318: output_byte(2); /* sector size = 512 */
319: output_byte(floppy->sect);
320: output_byte(floppy->gap);
321: output_byte(0xFF); /* sector size (0xff when n!=0 ?) */
1.1.1.2 root 322: if (reset)
323: do_fd_request();
1.1 root 324: }
325:
326: /*
1.1.1.2 root 327: * This is the routine called after every seek (or recalibrate) interrupt
328: * from the floppy controller. Note that the "unexpected interrupt" routine
329: * also does a recalibrate, but doesn't come here.
1.1 root 330: */
1.1.1.2 root 331: static void seek_interrupt(void)
1.1 root 332: {
1.1.1.2 root 333: /* sense drive status */
334: output_byte(FD_SENSEI);
335: if (result() != 2 || (ST0 & 0xF8) != 0x20 || ST1 != seek_track) {
1.1.1.4 ! root 336: recalibrate = 1;
1.1.1.2 root 337: bad_flp_intr();
338: do_fd_request();
1.1 root 339: return;
340: }
1.1.1.2 root 341: current_track = ST1;
342: setup_rw_floppy();
1.1 root 343: }
344:
345: /*
346: * This routine is called when everything should be correctly set up
347: * for the transfer (ie floppy motor is on and the correct floppy is
348: * selected).
349: */
350: static void transfer(void)
351: {
1.1.1.4 ! root 352: read_track = (command == FD_READ) && (CURRENT->errors < 4);
1.1 root 353: if (cur_spec1 != floppy->spec1) {
354: cur_spec1 = floppy->spec1;
355: output_byte(FD_SPECIFY);
356: output_byte(cur_spec1); /* hut etc */
357: output_byte(6); /* Head load time =6ms, DMA */
358: }
359: if (cur_rate != floppy->rate)
360: outb_p(cur_rate = floppy->rate,FD_DCR);
1.1.1.2 root 361: if (reset) {
362: do_fd_request();
363: return;
364: }
365: if (!seek) {
366: setup_rw_floppy();
367: return;
368: }
1.1 root 369: do_floppy = seek_interrupt;
1.1.1.4 ! root 370: output_byte(FD_SEEK);
! 371: if (read_track)
! 372: output_byte(current_drive);
! 373: else
! 374: output_byte((head<<2) | current_drive);
! 375: output_byte(seek_track);
1.1.1.2 root 376: if (reset)
377: do_fd_request();
1.1 root 378: }
379:
380: /*
381: * Special case - used after a unexpected interrupt (or reset)
382: */
383: static void recal_interrupt(void)
384: {
385: output_byte(FD_SENSEI);
1.1.1.2 root 386: if (result()!=2 || (ST0 & 0xE0) == 0x60)
387: reset = 1;
388: else
389: recalibrate = 0;
1.1 root 390: do_fd_request();
391: }
392:
393: void unexpected_floppy_interrupt(void)
394: {
395: output_byte(FD_SENSEI);
1.1.1.2 root 396: if (result()!=2 || (ST0 & 0xE0) == 0x60)
397: reset = 1;
398: else
399: recalibrate = 1;
400: }
401:
402: static void recalibrate_floppy(void)
403: {
404: recalibrate = 0;
405: current_track = 0;
1.1 root 406: do_floppy = recal_interrupt;
407: output_byte(FD_RECALIBRATE);
408: output_byte(head<<2 | current_drive);
1.1.1.2 root 409: if (reset)
410: do_fd_request();
1.1 root 411: }
412:
413: static void reset_interrupt(void)
414: {
415: output_byte(FD_SENSEI);
416: (void) result();
1.1.1.2 root 417: output_byte(FD_SPECIFY);
418: output_byte(cur_spec1); /* hut etc */
419: output_byte(6); /* Head load time =6ms, DMA */
420: do_fd_request();
1.1 root 421: }
422:
1.1.1.2 root 423: /*
424: * reset is done by pulling bit 2 of DOR low for a while.
425: */
1.1 root 426: static void reset_floppy(void)
427: {
1.1.1.2 root 428: int i;
429:
430: reset = 0;
431: cur_spec1 = -1;
432: cur_rate = -1;
433: recalibrate = 1;
1.1 root 434: printk("Reset-floppy called\n\r");
1.1.1.2 root 435: cli();
1.1 root 436: do_floppy = reset_interrupt;
1.1.1.2 root 437: outb_p(current_DOR & ~0x04,FD_DOR);
438: for (i=0 ; i<100 ; i++)
439: __asm__("nop");
1.1 root 440: outb(current_DOR,FD_DOR);
1.1.1.2 root 441: sti();
1.1 root 442: }
443:
444: static void floppy_on_interrupt(void)
445: {
446: /* We cannot do a floppy-select, as that might sleep. We just force it */
447: selected = 1;
1.1.1.2 root 448: if (current_drive != (current_DOR & 3)) {
449: current_DOR &= 0xFC;
450: current_DOR |= current_drive;
451: outb(current_DOR,FD_DOR);
452: add_timer(2,&transfer);
453: } else
454: transfer();
1.1 root 455: }
456:
457: void do_fd_request(void)
458: {
459: unsigned int block;
1.1.1.4 ! root 460: char * buffer_area;
1.1 root 461:
1.1.1.2 root 462: seek = 0;
463: if (reset) {
464: reset_floppy();
465: return;
466: }
467: if (recalibrate) {
468: recalibrate_floppy();
469: return;
470: }
1.1 root 471: INIT_REQUEST;
472: floppy = (MINOR(CURRENT->dev)>>2) + floppy_type;
1.1.1.2 root 473: if (current_drive != CURRENT_DEV)
474: seek = 1;
1.1 root 475: current_drive = CURRENT_DEV;
476: block = CURRENT->sector;
477: if (block+2 > floppy->size) {
478: end_request(0);
479: goto repeat;
480: }
481: sector = block % floppy->sect;
482: block /= floppy->sect;
483: head = block % floppy->head;
484: track = block / floppy->head;
485: seek_track = track << floppy->stretch;
486: if (CURRENT->cmd == READ)
487: command = FD_READ;
488: else if (CURRENT->cmd == WRITE)
489: command = FD_WRITE;
1.1.1.4 ! root 490: else {
! 491: printk("do_fd_request: unknown command\n");
! 492: end_request(0);
! 493: goto repeat;
! 494: }
! 495: if ((seek_track == buffer_track) &&
! 496: (current_drive == buffer_drive)) {
! 497: buffer_area = floppy_track_buffer +
! 498: ((sector + head*floppy->sect)<<9);
! 499: if (command == FD_READ) {
! 500: copy_buffer(buffer_area,CURRENT->buffer);
! 501: end_request(1);
! 502: goto repeat;
! 503: } else
! 504: copy_buffer(CURRENT->buffer,buffer_area);
! 505: }
! 506: if (seek_track != current_track)
! 507: seek = 1;
! 508: sector++;
1.1 root 509: add_timer(ticks_to_floppy_on(current_drive),&floppy_on_interrupt);
510: }
511:
1.1.1.3 root 512: static int floppy_sizes[] ={
513: 0, 0, 0, 0,
514: 360, 360 ,360, 360,
515: 1200,1200,1200,1200,
516: 360, 360, 360, 360,
517: 720, 720, 720, 720,
518: 360, 360, 360, 360,
519: 720, 720, 720, 720,
520: 1440,1440,1440,1440
521: };
522:
1.1 root 523: void floppy_init(void)
524: {
1.1.1.4 ! root 525: outb(current_DOR,FD_DOR);
1.1.1.3 root 526: blk_size[MAJOR_NR] = floppy_sizes;
1.1 root 527: blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1.1.1.4 ! root 528: set_intr_gate(0x26,&floppy_interrupt);
1.1 root 529: outb(inb_p(0x21)&~0x40,0x21);
530: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.