|
|
1.1 root 1: #include "config.h"
2: #include "libopenbios/bindings.h"
3: #include "kernel/kernel.h"
4: #include "libc/byteorder.h"
5: #include "libc/vsprintf.h"
6:
7: #include "drivers/drivers.h"
8:
9: #include "timer.h"
10:
11: /* DECLARE data structures for the nodes. */
12: DECLARE_UNNAMED_NODE( ob_floppy, INSTALL_OPEN, 2*sizeof(int) );
13:
14: #ifdef CONFIG_DEBUG_FLOPPY
15: #define printk_info printk
16: #define printk_debug printk
17: #else
18: #define printk_info(x ...)
19: #define printk_debug(x ...)
20: #endif
21: #define printk_err printk
22:
23: #define FD_DRIVE 0
24:
25:
26: #define FD_STATUS_A (0) /* Status register A */
27: #define FD_STATUS_B (1) /* Status register B */
28: #define FD_DOR (2) /* Digital Output Register */
29: #define FD_TDR (3) /* Tape Drive Register */
30: #define FD_STATUS (4) /* Main Status Register */
31: #define FD_DSR (4) /* Data Rate Select Register (old) */
32: #define FD_DATA (5) /* Data Transfer (FIFO) register */
33: #define FD_DIR (7) /* Digital Input Register (read) */
34: #define FD_DCR (7) /* Diskette Control Register (write)*/
35:
36: /* Bit of FD_STATUS_A */
37: #define STA_INT_PENDING 0x80 /* Interrupt Pending */
38:
39: /* DOR */
40: #define DOR_DRIVE0 0x00
41: #define DOR_DRIVE1 0x01
42: #define DOR_DRIVE2 0x02
43: #define DOR_DRIVE3 0x03
44: #define DOR_DRIVE_MASK 0x03
45: #define DOR_NO_RESET 0x04
46: #define DOR_DMA_EN 0x08
47: #define DOR_MOT_EN0 0x10
48: #define DOR_MOT_EN1 0x20
49: #define DOR_MOT_EN2 0x40
50: #define DOR_MOT_EN3 0x80
51:
52: /* Bits of main status register */
53: #define STATUS_BUSYMASK 0x0F /* drive busy mask */
54: #define STATUS_BUSY 0x10 /* FDC busy */
55: #define STATUS_NON_DMA 0x20 /* 0- DMA mode */
56: #define STATUS_DIR 0x40 /* 0- cpu->fdc */
57: #define STATUS_READY 0x80 /* Data reg ready */
58:
59: /* Bits of FD_ST0 */
60: #define ST0_DS 0x03 /* drive select mask */
61: #define ST0_HA 0x04 /* Head (Address) */
62: #define ST0_NR 0x08 /* Not Ready */
63: #define ST0_ECE 0x10 /* Equipment check error */
64: #define ST0_SE 0x20 /* Seek end */
65: #define ST0_INTR 0xC0 /* Interrupt code mask */
66: #define ST0_INTR_OK (0 << 6)
67: #define ST0_INTR_ERROR (1 << 6)
68: #define ST0_INTR_INVALID (2 << 6)
69: #define ST0_INTR_POLL_ERROR (3 << 6)
70:
71: /* Bits of FD_ST1 */
72: #define ST1_MAM 0x01 /* Missing Address Mark */
73: #define ST1_WP 0x02 /* Write Protect */
74: #define ST1_ND 0x04 /* No Data - unreadable */
75: #define ST1_OR 0x10 /* OverRun */
76: #define ST1_CRC 0x20 /* CRC error in data or addr */
77: #define ST1_EOC 0x80 /* End Of Cylinder */
78:
79: /* Bits of FD_ST2 */
80: #define ST2_MAM 0x01 /* Missing Address Mark (again) */
81: #define ST2_BC 0x02 /* Bad Cylinder */
82: #define ST2_SNS 0x04 /* Scan Not Satisfied */
83: #define ST2_SEH 0x08 /* Scan Equal Hit */
84: #define ST2_WC 0x10 /* Wrong Cylinder */
85: #define ST2_CRC 0x20 /* CRC error in data field */
86: #define ST2_CM 0x40 /* Control Mark = deleted */
87:
88: /* Bits of FD_ST3 */
89: #define ST3_HA 0x04 /* Head (Address) */
90: #define ST3_DS 0x08 /* drive is double-sided */
91: #define ST3_TZ 0x10 /* Track Zero signal (1=track 0) */
92: #define ST3_RY 0x20 /* drive is ready */
93: #define ST3_WP 0x40 /* Write Protect */
94: #define ST3_FT 0x80 /* Drive Fault */
95:
96: /* Values for FD_COMMAND */
97: #define FD_RECALIBRATE 0x07 /* move to track 0 */
98: #define FD_SEEK 0x0F /* seek track */
99: #define FD_READ 0xA6 /* read with MT, SKip deleted */
100: #define FD_WRITE 0xC5 /* write with MT, MFM */
101: #define FD_SENSEI 0x08 /* Sense Interrupt Status */
102: #define FD_SPECIFY 0x03 /* specify HUT etc */
103: #define FD_FORMAT 0x4D /* format one track */
104: #define FD_VERSION 0x10 /* get version code */
105: #define FD_CONFIGURE 0x13 /* configure FIFO operation */
106: #define FD_PERPENDICULAR 0x12 /* perpendicular r/w mode */
107: #define FD_GETSTATUS 0x04 /* read ST3 */
108: #define FD_DUMPREGS 0x0E /* dump the contents of the fdc regs */
109: #define FD_READID 0xEA /* prints the header of a sector */
110: #define FD_UNLOCK 0x14 /* Fifo config unlock */
111: #define FD_LOCK 0x94 /* Fifo config lock */
112: #define FD_RSEEK_OUT 0x8f /* seek out (i.e. to lower tracks) */
113: #define FD_RSEEK_IN 0xcf /* seek in (i.e. to higher tracks) */
114:
115:
116: /* the following commands are new in the 82078. They are not used in the
117: * floppy driver, except the first three. These commands may be useful for apps
118: * which use the FDRAWCMD interface. For doc, get the 82078 spec sheets at
119: * http://www-techdoc.intel.com/docs/periph/fd_contr/datasheets/ */
120:
121: #define FD_PARTID 0x18 /* part id ("extended" version cmd) */
122: #define FD_SAVE 0x2e /* save fdc regs for later restore */
123: #define FD_DRIVESPEC 0x8e /* drive specification: Access to the
124: * 2 Mbps data transfer rate for tape
125: * drives */
126:
127: #define FD_RESTORE 0x4e /* later restore */
128: #define FD_POWERDOWN 0x27 /* configure FDC's powersave features */
129: #define FD_FORMAT_N_WRITE 0xef /* format and write in one go. */
130: #define FD_OPTION 0x33 /* ISO format (which is a clean way to
131: * pack more sectors on a track) */
132:
133: /* FDC version return types */
134: #define FDC_NONE 0x00
135: #define FDC_UNKNOWN 0x10 /* DO NOT USE THIS TYPE EXCEPT IF IDENTIFICATION
136: FAILS EARLY */
137: #define FDC_8272A 0x20 /* Intel 8272a, NEC 765 */
138: #define FDC_765ED 0x30 /* Non-Intel 1MB-compatible FDC, can't detect */
139: #define FDC_82072 0x40 /* Intel 82072; 8272a + FIFO + DUMPREGS */
140: #define FDC_82072A 0x45 /* 82072A (on Sparcs) */
141: #define FDC_82077_ORIG 0x51 /* Original version of 82077AA, sans LOCK */
142: #define FDC_82077 0x52 /* 82077AA-1 */
143: #define FDC_82078_UNKN 0x5f /* Unknown 82078 variant */
144: #define FDC_82078 0x60 /* 44pin 82078 or 64pin 82078SL */
145: #define FDC_82078_1 0x61 /* 82078-1 (2Mbps fdc) */
146: #define FDC_S82078B 0x62 /* S82078B (first seen on Adaptec AVA-2825 VLB
147: * SCSI/EIDE/Floppy controller) */
148: #define FDC_87306 0x63 /* National Semiconductor PC 87306 */
149:
150: /*
151: * Beware: the fdc type list is roughly sorted by increasing features.
152: * Presence of features is tested by comparing the FDC version id with the
153: * "oldest" version that has the needed feature.
154: * If during FDC detection, an obscure test fails late in the sequence, don't
155: * assign FDC_UNKNOWN. Else the FDC will be treated as a dumb 8272a, or worse.
156: * This is especially true if the tests are unneeded.
157: */
158:
159: /* Parameters for a 1.44 3.5" disk */
160: #define DISK_H1440_SIZE 2880
161: #define DISK_H1440_SECT 18
162: #define DISK_H1440_HEAD 2
163: #define DISK_H1440_TRACK 80
164: #define DISK_H1440_STRETCH 0
165: #define DISK_H1440_GAP 0x1B
166: #define DISK_H1440_RATE 0x00
167: #define DISK_H1440_SPEC1 0xCF
168: #define DISK_H1440_FMT_GAP 0x6C
169:
170: /* Parameters for a 1.44 3.5" drive */
171: #define DRIVE_H1440_MAX_DTR 500
172: #define DRIVE_H1440_HLT 16 /* ms */
173: #define DRIVE_H1440_HUT 16 /* ms */
174: #define DRIVE_H1440_SRT 4000 /* us */
175: #define DRIVE_H1440_SPINUP 400 /* ms */
176: #define DRIVE_H1440_SPINDOWN 3000 /* ms */
177: #define DRIVE_H1440_SPINDOWN_OFFSET 10
178: #define DRIVE_H1440_SELECT_DELAY 20 /* ms */
179: #define DRIVE_H1440_RPS 5
180: #define DRIVE_H1440_TRACKS 83
181: #define DRIVE_H1440_TIMEOUT 3000 /* ms */
182: #define DRIVE_H1440_INTERLEAVE_SECT 20
183:
184: /* Floppy drive configuration */
185: #define FIFO_DEPTH 10
186: #define USE_IMPLIED_SEEK 0
187: #define USE_FIFO 1
188: #define FIFO_THRESHOLD 10
189: #define TRACK_PRECOMPENSATION 0
190:
191: #define SLOW_FLOPPY 0
192:
193: #define FD_RESET_DELAY 20 /* microseconds */
194:
195: /*
196: * FDC state
197: */
198: static struct drive_state {
199: unsigned track;
200: } drive_state[1];
201:
202: static struct floppy_fdc_state {
203: int in_sync;
204: int spec1; /* spec1 value last used */
205: int spec2; /* spec2 value last used */
206: int dtr;
207: unsigned char dor;
208: unsigned char version; /* FDC version code */
209: void (*fdc_outb)(unsigned char data, unsigned long port);
210: unsigned char (*fdc_inb)(unsigned long port);
211: unsigned long io_base;
212: unsigned long mmio_base;
213: } fdc_state;
214:
215: /* Synchronization of FDC access. */
216: #define FD_COMMAND_NONE -1
217: #define FD_COMMAND_ERROR 2
218: #define FD_COMMAND_OKAY 3
219:
220: /*
221: * globals used by 'result()'
222: */
223: #define MAX_REPLIES 16
224:
225: static void show_floppy(void);
226: static void floppy_reset(void);
227:
228: /*
229: * IO port operations
230: */
231: static unsigned char
232: ob_fdc_inb(unsigned long port)
233: {
234: return inb(fdc_state.io_base + port);
235: }
236:
237: static void
238: ob_fdc_outb(unsigned char data, unsigned long port)
239: {
240: outb(data, fdc_state.io_base + port);
241: }
242:
243: /*
244: * MMIO operations
245: */
246: static unsigned char
247: ob_fdc_mmio_readb(unsigned long port)
248: {
249: return *(unsigned char *)(fdc_state.mmio_base + port);
250: }
251:
252: static void
253: ob_fdc_mmio_writeb(unsigned char data, unsigned long port)
254: {
255: *(unsigned char *)(fdc_state.mmio_base + port) = data;
256: }
257:
258: static int set_dor(char mask, char data)
259: {
260: unsigned char newdor,olddor;
261:
262: olddor = fdc_state.dor;
263: newdor = (olddor & mask) | data;
264: if (newdor != olddor){
265: fdc_state.dor = newdor;
266: fdc_state.fdc_outb(newdor, FD_DOR);
267: }
268: return olddor;
269: }
270:
271: /* waits until the fdc becomes ready */
272: static int wait_til_ready(void)
273: {
274: int counter, status;
275: for (counter = 0; counter < 10000; counter++) {
276: status = fdc_state.fdc_inb(FD_STATUS);
277: if (status & STATUS_READY) {
278: return status;
279: }
280: }
281: printk_debug("Getstatus times out (%x)\n", status);
282: show_floppy();
283: return -3;
284: }
285:
286:
287: /* sends a command byte to the fdc */
288: static int output_byte(unsigned char byte)
289: {
290: int status;
291:
292: if ((status = wait_til_ready()) < 0)
293: return status;
294: if ((status & (STATUS_READY|STATUS_DIR|STATUS_NON_DMA)) == STATUS_READY){
295: fdc_state.fdc_outb(byte,FD_DATA);
296: return 0;
297: }
298: printk_debug("Unable to send byte %x to FDC_STATE. Status=%x\n",
299: byte, status);
300:
301: show_floppy();
302: return -2;
303: }
304:
305: /* gets the response from the fdc */
306: static int result(unsigned char *reply_buffer, int max_replies)
307: {
308: int i, status=0;
309:
310: for(i=0; i < max_replies; i++) {
311: if ((status = wait_til_ready()) < 0)
312: break;
313: status &= STATUS_DIR|STATUS_READY|STATUS_BUSY|STATUS_NON_DMA;
314: if ((status & ~STATUS_BUSY) == STATUS_READY){
315: return i;
316: }
317: if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
318: reply_buffer[i] = fdc_state.fdc_inb(FD_DATA);
319: else
320: break;
321: }
322: if (i == max_replies)
323: return i;
324: printk_debug("get result error. Last status=%x Read bytes=%d\n",
325: status, i);
326: show_floppy();
327: return -1;
328: }
329: #define MORE_OUTPUT -2
330: /* does the fdc need more output? */
331: static int need_more_output(void)
332: {
333: unsigned char reply_buffer[MAX_REPLIES];
334: int status;
335: if ((status = wait_til_ready()) < 0)
336: return -1;
337: if ((status & (STATUS_READY|STATUS_DIR|STATUS_NON_DMA)) == STATUS_READY)
338: return MORE_OUTPUT;
339: return result(reply_buffer, MAX_REPLIES);
340: }
341:
342: static int output_command(unsigned char *cmd, int count)
343: {
344: int i, status;
345: for(i = 0; i < count; i++) {
346: if ((status = output_byte(cmd[i])) < 0) {
347: printk_err("full command not acceppted, status =%x\n",
348: status);
349: return -1;
350: }
351: }
352: return 0;
353: }
354:
355: static int output_new_command(unsigned char *cmd, int count)
356: {
357: int i, status;
358: if ((status = output_byte(cmd[0])) < 0)
359: return -1;
360: if (need_more_output() != MORE_OUTPUT)
361: return -1;
362: for(i = 1; i < count; i++) {
363: if ((status = output_byte(cmd[i])) < 0) {
364: printk_err("full new command not acceppted, status =%d\n",
365: status);
366: return -1;
367: }
368: }
369: return 0;
370: }
371:
372:
373: /* Collect pending interrupt status */
374: static unsigned char collect_interrupt(void)
375: {
376: unsigned char pcn = 0xff;
377: unsigned char reply_buffer[MAX_REPLIES];
378: int nr;
379: #ifdef CONFIG_DEBUG_FLOPPY
380: int i, status;
381: #endif
382: nr = result(reply_buffer, MAX_REPLIES);
383: if (nr != 0) {
384: printk_debug("SENSEI\n");
385: }
386: else {
387: int max_sensei = 4;
388: do {
389: if (output_byte(FD_SENSEI) < 0)
390: break;
391: nr = result(reply_buffer, MAX_REPLIES);
392: if (nr == 2) {
393: pcn = reply_buffer[1];
394: printk_debug("SENSEI %02x %02x\n",
395: reply_buffer[0], reply_buffer[1]);
396: }
397: max_sensei--;
398: }while(((reply_buffer[0] & 0x83) != FD_DRIVE) && (nr == 2) && max_sensei);
399: #ifdef CONFIG_DEBUG_FLOPPY
400: status = fdc_state.fdc_inb(FD_STATUS);
401: printk_debug("status = %x, reply_buffer=", status);
402: for(i = 0; i < nr; i++) {
403: printk_debug(" %x",
404: reply_buffer[i]);
405: }
406: printk_debug("\n");
407: #else
408: fdc_state.fdc_inb(FD_STATUS);
409: #endif
410: }
411:
412: return pcn;
413: }
414:
415:
416: /* selects the fdc and drive, and enables the fdc's input/dma, and it's motor. */
417: static void set_drive(int drive)
418: {
419: int fdc = (drive >> 2) & 1;
420: int status;
421: unsigned new_dor;
422: if (drive > 3) {
423: printk_err("bad drive value\n");
424: return;
425: }
426: if (fdc != 0) {
427: printk_err("bad fdc value\n");
428: return;
429: }
430: drive &= 3;
431: #if 0
432: new_dor = 8; /* Enable the controller */
433: #else
434: new_dor = 0; /* Don't enable DMA on the controller */
435: #endif
436: new_dor |= (1 << (drive + 4)); /* Spinup the selected drive */
437: new_dor |= drive; /* Select the drive for commands as well */
438: set_dor(0xc, new_dor);
439:
440: mdelay(DRIVE_H1440_SPINUP);
441:
442: status = fdc_state.fdc_inb(FD_STATUS);
443: printk_debug("set_drive status = %02x, new_dor = %02x\n",
444: status, new_dor);
445: if (status != STATUS_READY) {
446: printk_err("set_drive bad status\n");
447: }
448: }
449:
450:
451: /* Disable the motor for a given floppy drive */
452: static void floppy_motor_off(int drive)
453: {
454: unsigned mask;
455: printk_debug("floppy_motor_off\n");
456: /* fix the number of drives */
457: drive &= 3;
458: /* Clear the bit for the drive we care about */
459: mask = 0xff;
460: mask &= ~(1 << (drive +4));
461: /* Now clear the bit in the Digital Output Register */
462: set_dor(mask, 0);
463: }
464:
465: /* Set the FDC's data transfer rate on behalf of the specified drive.
466: * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
467: * of the specify command (i.e. using the fdc_specify function).
468: */
469: static void fdc_dtr(unsigned rate)
470: {
471: rate &= 3;
472: /* If data rate not already set to desired value, set it. */
473: if (fdc_state.in_sync && (rate == fdc_state.dtr))
474: return;
475:
476: /* Set dtr */
477: fdc_state.fdc_outb(rate, FD_DCR);
478:
479: /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
480: * need a stabilization period of several milliseconds to be
481: * enforced after data rate changes before R/W operations.
482: * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
483: */
484: fdc_state.dtr = rate & 3;
485: mdelay(5);
486: } /* fdc_dtr */
487:
488: static int fdc_configure(int use_implied_seek, int use_fifo,
489: unsigned fifo_threshold, unsigned precompensation)
490: {
491: unsigned config_bits;
492: unsigned char cmd[4];
493: /* 0 EIS EFIFO POLL FIFOOTHR[4] */
494:
495: /* santize parameters */
496: config_bits = fifo_threshold & 0xf;
497: config_bits |= (1 << 4); /* Always disable background floppy poll */
498: config_bits |= (!use_fifo) << 5;
499: config_bits |= (!!use_implied_seek) << 6;
500:
501: precompensation &= 0xff; /* pre-compensation from track 0 upwards */
502:
503: cmd[0] = FD_CONFIGURE;
504: cmd[1] = 0;
505: cmd[2] = config_bits;
506: cmd[3] = precompensation;
507:
508: /* Turn on FIFO */
509: if (output_new_command(cmd, 4) < 0)
510: return 0;
511: return 1;
512: }
513:
514: #define NOMINAL_DTR 500
515: /* Issue a "SPECIFY" command to set the step rate time, head unload time,
516: * head load time, and DMA disable flag to values needed by floppy.
517: *
518: * The value "dtr" is the data transfer rate in Kbps. It is needed
519: * to account for the data rate-based scaling done by the 82072 and 82077
520: * FDC types. This parameter is ignored for other types of FDCs (i.e.
521: * 8272a).
522: *
523: * Note that changing the data transfer rate has a (probably deleterious)
524: * effect on the parameters subject to scaling for 82072/82077 FDCs, so
525: * fdc_specify is called again after each data transfer rate
526: * change.
527: *
528: * srt: 1000 to 16000 in microseconds
529: * hut: 16 to 240 milliseconds
530: * hlt: 2 to 254 milliseconds
531: *
532: * These values are rounded up to the next highest available delay time.
533: */
534: static void fdc_specify(
535: unsigned head_load_time, unsigned head_unload_time, unsigned step_rate)
536: {
537: unsigned char cmd[3];
538: unsigned long srt, hlt, hut;
539: unsigned long dtr = NOMINAL_DTR;
540: unsigned long scale_dtr = NOMINAL_DTR;
541: int hlt_max_code = 0x7f;
542: int hut_max_code = 0xf;
543:
544: printk_debug("fdc_specify\n");
545:
546: switch (DISK_H1440_RATE & 0x03) {
547: case 3:
548: dtr = 1000;
549: break;
550: case 1:
551: dtr = 300;
552: if (fdc_state.version >= FDC_82078) {
553: /* chose the default rate table, not the one
554: * where 1 = 2 Mbps */
555: cmd[0] = FD_DRIVESPEC;
556: cmd[1] = FD_DRIVE & 3;
557: cmd[2] = 0xc0;
558: output_new_command(cmd,3);
559: /* FIXME how do I handle errors here? */
560: }
561: break;
562: case 2:
563: dtr = 250;
564: break;
565: }
566:
567:
568: if (fdc_state.version >= FDC_82072) {
569: scale_dtr = dtr;
570: hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
571: hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
572: }
573:
574: /* Convert step rate from microseconds to milliseconds and 4 bits */
575: srt = 16 - (step_rate*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
576: if (SLOW_FLOPPY) {
577: srt = srt / 4;
578: }
579: if (srt > 0xf) {
580: srt = 0xf;
581: }
582:
583: hlt = (head_load_time*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
584: if (hlt < 0x01)
585: hlt = 0x01;
586: else if (hlt > 0x7f)
587: hlt = hlt_max_code;
588:
589: hut = (head_unload_time*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
590: if (hut < 0x1)
591: hut = 0x1;
592: else if (hut > 0xf)
593: hut = hut_max_code;
594:
595: cmd[0] = FD_SPECIFY;
596: cmd[1] = (srt << 4) | hut;
597: cmd[2] = (hlt << 1) | 1; /* Always disable DMA */
598:
599: /* If these parameters did not change, just return with success */
600: if (!fdc_state.in_sync || fdc_state.spec1 != cmd[1] || fdc_state.spec2 != cmd[2]) {
601: /* Go ahead and set spec1 and spec2 */
602: output_command(cmd, 3);
603: /* FIXME how do I handle errors here... */
604: printk_info("FD_SPECIFY(%02x, %02x)\n", cmd[1], cmd[2]);
605: }
606: } /* fdc_specify */
607:
608:
609: /*
610: * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
611: * or by setting the self clearing bit 7 of STATUS (newer FDCs)
612: */
613: static void reset_fdc(void)
614: {
615: unsigned char reply[MAX_REPLIES];
616:
617: fdc_state.in_sync = 0;
618:
619: /* Pseudo-DMA may intercept 'reset finished' interrupt. */
620: /* Irrelevant for systems with true DMA (i386). */
621:
622: if (fdc_state.version >= FDC_82072A)
623: fdc_state.fdc_outb(0x80 | (fdc_state.dtr &3), FD_DSR);
624: else {
625: fdc_state.fdc_outb(fdc_state.dor & ~DOR_NO_RESET, FD_DOR);
626: udelay(FD_RESET_DELAY);
627: fdc_state.fdc_outb(fdc_state.dor, FD_DOR);
628: }
629: result(reply, MAX_REPLIES);
630: }
631:
632:
633:
634: static void show_floppy(void)
635: {
636:
637: printk_debug("\n");
638: printk_debug("floppy driver state\n");
639: printk_debug("-------------------\n");
640:
641: printk_debug("fdc_bytes: %02x %02x xx %02x %02x %02x xx %02x\n",
642: fdc_state.fdc_inb(FD_STATUS_A),
643: fdc_state.fdc_inb(FD_STATUS_B),
644: fdc_state.fdc_inb(FD_TDR),
645: fdc_state.fdc_inb(FD_STATUS),
646: fdc_state.fdc_inb(FD_DATA),
647: fdc_state.fdc_inb(FD_DIR));
648:
649: printk_debug("status=%x\n", fdc_state.fdc_inb(FD_STATUS));
650: printk_debug("\n");
651: }
652:
653: static void floppy_recalibrate(void)
654: {
655: unsigned char cmd[2];
656: unsigned char reply[MAX_REPLIES];
657: int nr, success;
658: success = 0;
659: do {
660: printk_debug("floppy_recalibrate\n");
661: /* Send the recalibrate command to the controller.
662: * We don't have interrupts or anything we can poll
663: * so we have to guess when it is done.
664: */
665: cmd[0] = FD_RECALIBRATE;
666: cmd[1] = 0;
667: if (output_command(cmd, 2) < 0)
668: continue;
669:
670: /* Sleep for the maximum time the recalibrate command
671: * can run.
672: */
673: mdelay(80*DRIVE_H1440_SRT/1000);
674:
675: /* Now call FD_SENSEI to end the command
676: * and collect up the reply.
677: */
678: if (output_byte(FD_SENSEI) < 0)
679: continue;
680: nr = result(reply, MAX_REPLIES);
681:
682: /* Now see if we have succeeded in our seek */
683: success =
684: /* We have the right size result */
685: (nr == 2) &&
686: /* The command didn't terminate in error */
687: ((reply[0] & ST0_INTR) == ST0_INTR_OK) &&
688: /* We finished a seek */
689: (reply[0] & ST0_SE) &&
690: /* We are at cylinder 0 */
691: (reply[1] == 0);
692: } while(!success);
693: /* Remember we are at track 0 */
694: drive_state[FD_DRIVE].track = 0;
695: }
696:
697:
698: static int floppy_seek(unsigned track)
699: {
700: unsigned char cmd[3];
701: unsigned char reply[MAX_REPLIES];
702: int nr, success;
703: unsigned distance, old_track;
704:
705: /* Look up the old track and see if we need to
706: * do anything.
707: */
708: old_track = drive_state[FD_DRIVE].track;
709: if (old_track == track) {
710: return 1;
711: }
712:
713: /* Compute the distance we are about to move,
714: * We need to know this so we know how long to sleep...
715: */
716: distance = (old_track > track)?(old_track - track):(track - old_track);
717: distance += 1;
718:
719:
720: /* Send the seek command to the controller.
721: * We don't have interrupts or anything we can poll
722: * so we have to guess when it is done.
723: */
724: cmd[0] = FD_SEEK;
725: cmd[1] = FD_DRIVE;
726: cmd[2] = track;
727: if (output_command(cmd, 3) < 0)
728: return 0;
729:
730: /* Sleep for the time it takes to step throuhg distance tracks.
731: */
732: mdelay(distance*DRIVE_H1440_SRT/1000);
733:
734: /* Now call FD_SENSEI to end the command
735: * and collect up the reply.
736: */
737: cmd[0] = FD_SENSEI;
738: if (output_command(cmd, 1) < 0)
739: return 0;
740: nr = result(reply, MAX_REPLIES);
741:
742: /* Now see if we have succeeded in our seek */
743: success =
744: /* We have the right size result */
745: (nr == 2) &&
746: /* The command didn't terminate in error */
747: ((reply[0] & ST0_INTR) == ST0_INTR_OK) &&
748: /* We finished a seek */
749: (reply[0] & ST0_SE) &&
750: /* We are at cylinder 0 */
751: (reply[1] == track);
752: if (success)
753: drive_state[FD_DRIVE].track = track;
754: else {
755: printk_debug("seek failed\n");
756: printk_debug("nr = %d\n", nr);
757: printk_debug("ST0 = %02x\n", reply[0]);
758: printk_debug("PCN = %02x\n", reply[1]);
759: printk_debug("status = %d\n", fdc_state.fdc_inb(FD_STATUS));
760: }
761: return success;
762: }
763:
764: static int read_ok(unsigned head)
765: {
766: unsigned char results[7];
767: int result_ok;
768: int nr;
769:
770: /* read back the read results */
771: nr = result(results, 7);
772:
773: /* Now see if they say we are o.k. */
774: result_ok = 0;
775: /* Are my result bytes o.k.? */
776: if (nr == 7) {
777: /* Are we o.k. */
778: if ((results[0] & ST0_INTR) == ST0_INTR_OK) {
779: result_ok = 1;
780: }
781: /* Or did we get just an overflow error */
782: else if (((results[0] & ST0_INTR) == ST0_INTR_ERROR) &&
783: (results[1]== ST1_OR) &&
784: (results[2] == 0)) {
785: result_ok = 1;
786: }
787: /* Verify the reply had the correct head */
788: if (((results[0] & ST0_HA) >> 2) != head) {
789: result_ok = 0;
790: }
791: /* Verify the reply had the correct drive */
792: if (((results[0] & ST0_DS) != FD_DRIVE)) {
793: result_ok = 0;
794: }
795: }
796: if (!result_ok) {
797: printk_debug("result_bytes = %d\n", nr);
798: printk_debug("ST0 = %02x\n", results[0]);
799: printk_debug("ST1 = %02x\n", results[1]);
800: printk_debug("ST2 = %02x\n", results[2]);
801: printk_debug(" C = %02x\n", results[3]);
802: printk_debug(" H = %02x\n", results[4]);
803: printk_debug(" R = %02x\n", results[5]);
804: printk_debug(" N = %02x\n", results[6]);
805: }
806: return result_ok;
807: }
808:
809: static int floppy_read_sectors(
810: char *dest, unsigned byte_offset, unsigned length,
811: unsigned sector, unsigned head, unsigned track)
812: {
813: /* MT == Multitrack */
814: /* MFM == MFM or FM Mode */
815: /* SK == Skip deleted data addres Mark */
816: /* HDS == Head number select */
817: /* DS0 == Disk Drive Select 0 */
818: /* DS1 == Disk Drive Select 1 */
819: /* C == Cylinder number 0 - 255 */
820: /* H == Head number */
821: /* R == Record */
822: /* N == The number of data bytes written in a sector */
823: /* EOT == End of Track */
824: /* GPL == Gap Length */
825: /* DTL == Data Length */
826: /* MT MFM SK 0 1 1 0 0 */
827: /* 0 0 0 0 0 HDS DS1 DS0 */
828: /* C, H, R, N, EOT, GPL, DTL */
829:
830: int i, status, result_ok;
831: int max_bytes, bytes_read;
832: int ret;
833: unsigned char cmd[9];
834: unsigned end_offset;
835:
836: end_offset = byte_offset + length;
837: max_bytes = 512*(DISK_H1440_SECT - sector + 1);
838:
839: if (byte_offset >= max_bytes) {
840: return 0;
841: }
842: cmd[0] = FD_READ | (((DISK_H1440_HEAD ==2)?1:0) << 6);
843: cmd[1] = (head << 2) | FD_DRIVE;
844: cmd[2] = track;
845: cmd[3] = head;
846: cmd[4] = sector;
847: cmd[5] = 2; /* 2^N *128 == Sector size. Hard coded to 512 bytes */
848: cmd[6] = DISK_H1440_SECT;
849: cmd[7] = DISK_H1440_GAP;
850: cmd[8] = 0xff;
851:
852: /* Output the command bytes */
853: if (output_command(cmd, 9) < 0)
854: return -1;
855:
856: /* The execution stage begins when STATUS_READY&STATUS_NON_DMA is set */
857: do {
858: status = fdc_state.fdc_inb(FD_STATUS);
859: status &= STATUS_READY | STATUS_NON_DMA;
860: } while(status != (STATUS_READY|STATUS_NON_DMA));
861:
862: for(i = 0; i < max_bytes; i++) {
863: unsigned char byte;
864: if ((status = wait_til_ready()) < 0) {
865: break;
866: }
867: status &= STATUS_READY|STATUS_DIR|STATUS_NON_DMA;
868: if (status != (STATUS_READY|STATUS_DIR|STATUS_NON_DMA)) {
869: break;
870: }
871: byte = fdc_state.fdc_inb(FD_DATA);
872: if ((i >= byte_offset) && (i < end_offset)) {
873: dest[i - byte_offset] = byte;
874: }
875: }
876: bytes_read = i;
877:
878: /* The result stage begins when STATUS_NON_DMA is cleared */
879: while((status = fdc_state.fdc_inb(FD_STATUS)) & STATUS_NON_DMA) {
880: /* We get extra bytes in the fifo past
881: * the end of the sector and drop them on the floor.
882: * Otherwise the fifo is polluted.
883: */
884: fdc_state.fdc_inb(FD_DATA);
885: }
886: /* Did I get an error? */
887: result_ok = read_ok(head);
888: /* Did I read enough bytes? */
889: ret = -1;
890: if (result_ok && (bytes_read == max_bytes)) {
891: ret = bytes_read - byte_offset;
892: if (ret > length) {
893: ret = length;
894: }
895: }
896:
897: if (ret < 0) {
898: printk_debug("ret = %d\n", ret);
899: printk_debug("bytes_read = %d\n", bytes_read);
900: printk_debug("status = %x\n", status);
901: }
902: return ret;
903: }
904:
905:
906: static int __floppy_read(char *dest, unsigned long offset, unsigned long length)
907: {
908: unsigned head, track, sector, byte_offset, sector_offset;
909: int ret;
910:
911: /* break the offset up into sectors and bytes */
912: byte_offset = offset % 512;
913: sector_offset = offset / 512;
914:
915: /* Find the disk block we are starting with... */
916: sector = (sector_offset % DISK_H1440_SECT) + 1;
917: head = (sector_offset / DISK_H1440_SECT) % DISK_H1440_HEAD;
918: track = (sector_offset / (DISK_H1440_SECT *DISK_H1440_HEAD))% DISK_H1440_TRACK;
919:
920: /* First seek to our start track */
921: if (!floppy_seek(track)) {
922: return -1;
923: }
924: /* Then read the data */
925: ret = floppy_read_sectors(dest, byte_offset, length, sector, head, track);
926: if (ret >= 0) {
927: return ret;
928: }
929: /* If we failed reset the fdc... */
930: floppy_reset();
931: return -1;
932: }
933:
934: static int floppy_read(char *dest, unsigned long offset, unsigned long length)
935: {
936: int fr_result, bytes_read;;
937:
938: printk_debug("floppy_read\n");
939: bytes_read = 0;
940: do {
941: int max_errors = 3;
942: do {
943: fr_result = __floppy_read(dest + bytes_read, offset,
944: length - bytes_read);
945: if (max_errors-- == 0) {
946: return (bytes_read)?bytes_read: -1;
947: }
948: } while (fr_result <= 0);
949: offset += fr_result;
950: bytes_read += fr_result;
951: } while(bytes_read < length);
952: return bytes_read;
953: }
954:
955: /* Determine the floppy disk controller type */
956: /* This routine was written by David C. Niemi */
957: static char get_fdc_version(void)
958: {
959: int bytes, ret;
960: unsigned char reply_buffer[MAX_REPLIES];
961:
962: ret = output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
963: if (ret < 0)
964: return FDC_NONE;
965: if ((bytes = result(reply_buffer, MAX_REPLIES)) <= 0x00)
966: return FDC_NONE; /* No FDC present ??? */
967: if ((bytes==1) && (reply_buffer[0] == 0x80)){
968: printk_info("FDC is an 8272A\n");
969: return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
970: }
971: if (bytes != 10) {
972: printk_debug("init: DUMPREGS: unexpected return of %d bytes.\n",
973: bytes);
974: return FDC_UNKNOWN;
975: }
976: if (!fdc_configure(USE_IMPLIED_SEEK, USE_FIFO, FIFO_THRESHOLD,
977: TRACK_PRECOMPENSATION)) {
978: printk_info("FDC is an 82072\n");
979: return FDC_82072; /* 82072 doesn't know CONFIGURE */
980: }
981:
982: output_byte(FD_PERPENDICULAR);
983: if (need_more_output() == MORE_OUTPUT) {
984: output_byte(0);
985: } else {
986: printk_info("FDC is an 82072A\n");
987: return FDC_82072A; /* 82072A as found on Sparcs. */
988: }
989:
990: output_byte(FD_UNLOCK);
991: bytes = result(reply_buffer, MAX_REPLIES);
992: if ((bytes == 1) && (reply_buffer[0] == 0x80)){
993: printk_info("FDC is a pre-1991 82077\n");
994: return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
995: * LOCK/UNLOCK */
996: }
997: if ((bytes != 1) || (reply_buffer[0] != 0x00)) {
998: printk_debug("FDC init: UNLOCK: unexpected return of %d bytes.\n",
999: bytes);
1000: return FDC_UNKNOWN;
1001: }
1002: output_byte(FD_PARTID);
1003: bytes = result(reply_buffer, MAX_REPLIES);
1004: if (bytes != 1) {
1005: printk_debug("FDC init: PARTID: unexpected return of %d bytes.\n",
1006: bytes);
1007: return FDC_UNKNOWN;
1008: }
1009: if (reply_buffer[0] == 0x80) {
1010: printk_info("FDC is a post-1991 82077\n");
1011: return FDC_82077; /* Revised 82077AA passes all the tests */
1012: }
1013: switch (reply_buffer[0] >> 5) {
1014: case 0x0:
1015: /* Either a 82078-1 or a 82078SL running at 5Volt */
1016: printk_info("FDC is an 82078.\n");
1017: return FDC_82078;
1018: case 0x1:
1019: printk_info("FDC is a 44pin 82078\n");
1020: return FDC_82078;
1021: case 0x2:
1022: printk_info("FDC is a S82078B\n");
1023: return FDC_S82078B;
1024: case 0x3:
1025: printk_info("FDC is a National Semiconductor PC87306\n");
1026: return FDC_87306;
1027: default:
1028: printk_info("FDC init: 82078 variant with unknown PARTID=%d.\n",
1029: reply_buffer[0] >> 5);
1030: return FDC_82078_UNKN;
1031: }
1032: } /* get_fdc_version */
1033:
1034:
1035: static int floppy_init(unsigned long io_base, unsigned long mmio_base)
1036: {
1037: printk_debug("floppy_init\n");
1038: fdc_state.in_sync = 0;
1039: fdc_state.spec1 = -1;
1040: fdc_state.spec2 = -1;
1041: fdc_state.dtr = -1;
1042: fdc_state.dor = DOR_NO_RESET;
1043: fdc_state.version = FDC_UNKNOWN;
1044: if (mmio_base) {
1045: fdc_state.fdc_inb = ob_fdc_mmio_readb;
1046: fdc_state.fdc_outb = ob_fdc_mmio_writeb;
1047: } else {
1048: fdc_state.fdc_inb = ob_fdc_inb;
1049: fdc_state.fdc_outb = ob_fdc_outb;
1050: }
1051: fdc_state.io_base = io_base;
1052: fdc_state.mmio_base = mmio_base;
1053: reset_fdc();
1054: /* Try to determine the floppy controller type */
1055: fdc_state.version = get_fdc_version();
1056: if (fdc_state.version == FDC_NONE) {
1057: return -1;
1058: }
1059: floppy_reset();
1060: printk_info("fdc_state.version = %04x\n", fdc_state.version);
1061: return 0;
1062: }
1063:
1064: static void floppy_reset(void)
1065: {
1066: printk_debug("floppy_reset\n");
1067: floppy_motor_off(FD_DRIVE);
1068: reset_fdc();
1069: fdc_dtr(DISK_H1440_RATE);
1070: /* program data rate via ccr */
1071: collect_interrupt();
1072: fdc_configure(USE_IMPLIED_SEEK, USE_FIFO, FIFO_THRESHOLD,
1073: TRACK_PRECOMPENSATION);
1074: fdc_specify(DRIVE_H1440_HLT, DRIVE_H1440_HUT, DRIVE_H1440_SRT);
1075: set_drive(FD_DRIVE);
1076: floppy_recalibrate();
1077: fdc_state.in_sync = 1;
1078: }
1079:
1080: static void
1081: ob_floppy_initialize(const char *path)
1082: {
1083: int props[3];
1084: phandle_t ph = find_dev(path);
1085:
1086: set_property(ph, "device_type", "block", sizeof("block"));
1087:
1088: // Set dummy reg properties
1089: props[0] = __cpu_to_be32(0); props[1] = __cpu_to_be32(0); props[2] = __cpu_to_be32(0);
1090: set_property(ph, "reg", (char *)&props, 3*sizeof(int));
1091:
1092: fword("is-deblocker");
1093: }
1094:
1095:
1096: static void
1097: ob_floppy_open(int *idx)
1098: {
1099: int ret = 1;
1100: phandle_t ph;
1101:
1102: fword("my-unit");
1103: idx[0]=POP();
1104:
1105: fword("my-parent");
1106: fword("ihandle>phandle");
1107: ph=(phandle_t)POP();
1108:
1109: selfword("open-deblocker");
1110:
1111: /* interpose disk-label */
1112: ph = find_dev("/packages/disk-label");
1113: fword("my-args");
1114: PUSH_ph( ph );
1115: fword("interpose");
1116:
1117: RET ( -ret );
1118: }
1119:
1120: static void
1121: ob_floppy_close(int *idx)
1122: {
1123: selfword("close-deblocker");
1124: }
1125:
1126: static void
1127: ob_floppy_read_blocks(int *idx)
1128: {
1129: cell cnt = POP();
1130: ucell blk = POP();
1131: char *dest = (char*)POP();
1132: floppy_read(dest, blk*512, cnt*512);
1133: PUSH(cnt);
1134: }
1135:
1136:
1137: static void
1138: ob_floppy_block_size(int *idx)
1139: {
1140: PUSH(512);
1141: }
1142:
1143: static void
1144: ob_floppy_max_transfer(int *idx)
1145: {
1146: // Fixme
1147: PUSH(18 * 512);
1148: }
1149:
1150: NODE_METHODS(ob_floppy) = {
1151: { "open", ob_floppy_open },
1152: { "close", ob_floppy_close },
1153: { "read-blocks", ob_floppy_read_blocks },
1154: { "block-size", ob_floppy_block_size },
1155: { "max-transfer", ob_floppy_max_transfer },
1156: };
1157:
1158:
1159: int ob_floppy_init(const char *path, const char *dev_name,
1160: unsigned long io_base, unsigned long mmio_base)
1161: {
1162: char nodebuff[128];
1163: phandle_t aliases;
1164:
1165: snprintf(nodebuff, sizeof(nodebuff), "%s/%s", path, dev_name);
1166: if (!mmio_base) {
1167: REGISTER_NAMED_NODE(ob_floppy, nodebuff);
1168: ob_floppy_initialize(nodebuff);
1169: } else {
1170: // Already in tree and mapped
1171: REGISTER_NODE_METHODS(ob_floppy, nodebuff);
1172: }
1173: floppy_init(io_base, mmio_base);
1174:
1175: aliases = find_dev("/aliases");
1176: set_property(aliases, "floppy", nodebuff, strlen(nodebuff) + 1);
1177:
1178: return 0;
1179: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.