Commit better SPARC32 fix for obp_devclose().
[openbios.git] / drivers / floppy.c
bloba59e9f5a80e3801730ab0d06ad1aa5caee623da0
1 #include "config.h"
2 #include "libopenbios/bindings.h"
3 #include "kernel/kernel.h"
4 #include "libc/byteorder.h"
5 #include "libc/vsprintf.h"
7 #include "drivers/drivers.h"
9 #include "timer.h"
11 /* DECLARE data structures for the nodes. */
12 DECLARE_UNNAMED_NODE( ob_floppy, INSTALL_OPEN, 2*sizeof(int) );
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
23 #define FD_DRIVE 0
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)*/
36 /* Bit of FD_STATUS_A */
37 #define STA_INT_PENDING 0x80 /* Interrupt Pending */
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
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 */
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)
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 */
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 */
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 */
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) */
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/ */
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 */
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) */
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 */
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.
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
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
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
191 #define SLOW_FLOPPY 0
193 #define FD_RESET_DELAY 20 /* microseconds */
196 * FDC state
198 static struct drive_state {
199 unsigned track;
200 } drive_state[1];
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;
215 /* Synchronization of FDC access. */
216 #define FD_COMMAND_NONE -1
217 #define FD_COMMAND_ERROR 2
218 #define FD_COMMAND_OKAY 3
221 * globals used by 'result()'
223 #define MAX_REPLIES 16
225 static void show_floppy(void);
226 static void floppy_reset(void);
229 * IO port operations
231 static unsigned char
232 ob_fdc_inb(unsigned long port)
234 return inb(fdc_state.io_base + port);
237 static void
238 ob_fdc_outb(unsigned char data, unsigned long port)
240 outb(data, fdc_state.io_base + port);
244 * MMIO operations
246 static unsigned char
247 ob_fdc_mmio_readb(unsigned long port)
249 return *(unsigned char *)(fdc_state.mmio_base + port);
252 static void
253 ob_fdc_mmio_writeb(unsigned char data, unsigned long port)
255 *(unsigned char *)(fdc_state.mmio_base + port) = data;
258 static int set_dor(char mask, char data)
260 unsigned char newdor,olddor;
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);
268 return olddor;
271 /* waits until the fdc becomes ready */
272 static int wait_til_ready(void)
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;
281 printk_debug("Getstatus times out (%x)\n", status);
282 show_floppy();
283 return -3;
287 /* sends a command byte to the fdc */
288 static int output_byte(unsigned char byte)
290 int status;
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;
298 printk_debug("Unable to send byte %x to FDC_STATE. Status=%x\n",
299 byte, status);
301 show_floppy();
302 return -2;
305 /* gets the response from the fdc */
306 static int result(unsigned char *reply_buffer, int max_replies)
308 int i, status=0;
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;
317 if (status == (STATUS_DIR|STATUS_READY|STATUS_BUSY))
318 reply_buffer[i] = fdc_state.fdc_inb(FD_DATA);
319 else
320 break;
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;
329 #define MORE_OUTPUT -2
330 /* does the fdc need more output? */
331 static int need_more_output(void)
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);
342 static int output_command(unsigned char *cmd, int count)
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;
352 return 0;
355 static int output_new_command(unsigned char *cmd, int count)
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;
369 return 0;
373 /* Collect pending interrupt status */
374 static unsigned char collect_interrupt(void)
376 unsigned char pcn = 0xff;
377 unsigned char reply_buffer[MAX_REPLIES];
378 int nr, i, status;
379 nr = result(reply_buffer, MAX_REPLIES);
380 if (nr != 0) {
381 printk_debug("SENSEI\n");
383 else {
384 int max_sensei = 4;
385 do {
386 if (output_byte(FD_SENSEI) < 0)
387 break;
388 nr = result(reply_buffer, MAX_REPLIES);
389 if (nr == 2) {
390 pcn = reply_buffer[1];
391 printk_debug("SENSEI %02x %02x\n",
392 reply_buffer[0], reply_buffer[1]);
394 max_sensei--;
395 }while(((reply_buffer[0] & 0x83) != FD_DRIVE) && (nr == 2) && max_sensei);
396 status = fdc_state.fdc_inb(FD_STATUS);
397 printk_debug("status = %x, reply_buffer=", status);
398 for(i = 0; i < nr; i++) {
399 printk_debug(" %x",
400 reply_buffer[i]);
402 printk_debug("\n");
405 return pcn;
409 /* selects the fdc and drive, and enables the fdc's input/dma, and it's motor. */
410 static void set_drive(int drive)
412 int fdc = (drive >> 2) & 1;
413 int status;
414 unsigned new_dor;
415 if (drive > 3) {
416 printk_err("bad drive value\n");
417 return;
419 if (fdc != 0) {
420 printk_err("bad fdc value\n");
421 return;
423 drive &= 3;
424 #if 0
425 new_dor = 8; /* Enable the controller */
426 #else
427 new_dor = 0; /* Don't enable DMA on the controller */
428 #endif
429 new_dor |= (1 << (drive + 4)); /* Spinup the selected drive */
430 new_dor |= drive; /* Select the drive for commands as well */
431 set_dor(0xc, new_dor);
433 mdelay(DRIVE_H1440_SPINUP);
435 status = fdc_state.fdc_inb(FD_STATUS);
436 printk_debug("set_drive status = %02x, new_dor = %02x\n",
437 status, new_dor);
438 if (status != STATUS_READY) {
439 printk_err("set_drive bad status\n");
444 /* Disable the motor for a given floppy drive */
445 static void floppy_motor_off(int drive)
447 unsigned mask;
448 printk_debug("floppy_motor_off\n");
449 /* fix the number of drives */
450 drive &= 3;
451 /* Clear the bit for the drive we care about */
452 mask = 0xff;
453 mask &= ~(1 << (drive +4));
454 /* Now clear the bit in the Digital Output Register */
455 set_dor(mask, 0);
458 /* Set the FDC's data transfer rate on behalf of the specified drive.
459 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
460 * of the specify command (i.e. using the fdc_specify function).
462 static void fdc_dtr(unsigned rate)
464 rate &= 3;
465 /* If data rate not already set to desired value, set it. */
466 if (fdc_state.in_sync && (rate == fdc_state.dtr))
467 return;
469 /* Set dtr */
470 fdc_state.fdc_outb(rate, FD_DCR);
472 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
473 * need a stabilization period of several milliseconds to be
474 * enforced after data rate changes before R/W operations.
475 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
477 fdc_state.dtr = rate & 3;
478 mdelay(5);
479 } /* fdc_dtr */
481 static int fdc_configure(int use_implied_seek, int use_fifo,
482 unsigned fifo_threshold, unsigned precompensation)
484 unsigned config_bits;
485 unsigned char cmd[4];
486 /* 0 EIS EFIFO POLL FIFOOTHR[4] */
488 /* santize parameters */
489 config_bits = fifo_threshold & 0xf;
490 config_bits |= (1 << 4); /* Always disable background floppy poll */
491 config_bits |= (!use_fifo) << 5;
492 config_bits |= (!!use_implied_seek) << 6;
494 precompensation &= 0xff; /* pre-compensation from track 0 upwards */
496 cmd[0] = FD_CONFIGURE;
497 cmd[1] = 0;
498 cmd[2] = config_bits;
499 cmd[3] = precompensation;
501 /* Turn on FIFO */
502 if (output_new_command(cmd, 4) < 0)
503 return 0;
504 return 1;
507 #define NOMINAL_DTR 500
508 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
509 * head load time, and DMA disable flag to values needed by floppy.
511 * The value "dtr" is the data transfer rate in Kbps. It is needed
512 * to account for the data rate-based scaling done by the 82072 and 82077
513 * FDC types. This parameter is ignored for other types of FDCs (i.e.
514 * 8272a).
516 * Note that changing the data transfer rate has a (probably deleterious)
517 * effect on the parameters subject to scaling for 82072/82077 FDCs, so
518 * fdc_specify is called again after each data transfer rate
519 * change.
521 * srt: 1000 to 16000 in microseconds
522 * hut: 16 to 240 milliseconds
523 * hlt: 2 to 254 milliseconds
525 * These values are rounded up to the next highest available delay time.
527 static void fdc_specify(
528 unsigned head_load_time, unsigned head_unload_time, unsigned step_rate)
530 unsigned char cmd[3];
531 unsigned long srt, hlt, hut;
532 unsigned long dtr = NOMINAL_DTR;
533 unsigned long scale_dtr = NOMINAL_DTR;
534 int hlt_max_code = 0x7f;
535 int hut_max_code = 0xf;
537 printk_debug("fdc_specify\n");
539 switch (DISK_H1440_RATE & 0x03) {
540 case 3:
541 dtr = 1000;
542 break;
543 case 1:
544 dtr = 300;
545 if (fdc_state.version >= FDC_82078) {
546 /* chose the default rate table, not the one
547 * where 1 = 2 Mbps */
548 cmd[0] = FD_DRIVESPEC;
549 cmd[1] = FD_DRIVE & 3;
550 cmd[2] = 0xc0;
551 output_new_command(cmd,3);
552 /* FIXME how do I handle errors here? */
554 break;
555 case 2:
556 dtr = 250;
557 break;
561 if (fdc_state.version >= FDC_82072) {
562 scale_dtr = dtr;
563 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */
564 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */
567 /* Convert step rate from microseconds to milliseconds and 4 bits */
568 srt = 16 - (step_rate*scale_dtr/1000 + NOMINAL_DTR - 1)/NOMINAL_DTR;
569 if (SLOW_FLOPPY) {
570 srt = srt / 4;
572 if (srt > 0xf) {
573 srt = 0xf;
576 hlt = (head_load_time*scale_dtr/2 + NOMINAL_DTR - 1)/NOMINAL_DTR;
577 if (hlt < 0x01)
578 hlt = 0x01;
579 else if (hlt > 0x7f)
580 hlt = hlt_max_code;
582 hut = (head_unload_time*scale_dtr/16 + NOMINAL_DTR - 1)/NOMINAL_DTR;
583 if (hut < 0x1)
584 hut = 0x1;
585 else if (hut > 0xf)
586 hut = hut_max_code;
588 cmd[0] = FD_SPECIFY;
589 cmd[1] = (srt << 4) | hut;
590 cmd[2] = (hlt << 1) | 1; /* Always disable DMA */
592 /* If these parameters did not change, just return with success */
593 if (!fdc_state.in_sync || fdc_state.spec1 != cmd[1] || fdc_state.spec2 != cmd[2]) {
594 /* Go ahead and set spec1 and spec2 */
595 output_command(cmd, 3);
596 /* FIXME how do I handle errors here... */
597 printk_info("FD_SPECIFY(%02x, %02x)\n", cmd[1], cmd[2]);
599 } /* fdc_specify */
603 * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
604 * or by setting the self clearing bit 7 of STATUS (newer FDCs)
606 static void reset_fdc(void)
608 unsigned char reply[MAX_REPLIES];
610 fdc_state.in_sync = 0;
612 /* Pseudo-DMA may intercept 'reset finished' interrupt. */
613 /* Irrelevant for systems with true DMA (i386). */
615 if (fdc_state.version >= FDC_82072A)
616 fdc_state.fdc_outb(0x80 | (fdc_state.dtr &3), FD_DSR);
617 else {
618 fdc_state.fdc_outb(fdc_state.dor & ~DOR_NO_RESET, FD_DOR);
619 udelay(FD_RESET_DELAY);
620 fdc_state.fdc_outb(fdc_state.dor, FD_DOR);
622 result(reply, MAX_REPLIES);
627 static void show_floppy(void)
630 printk_debug("\n");
631 printk_debug("floppy driver state\n");
632 printk_debug("-------------------\n");
634 printk_debug("fdc_bytes: %02x %02x xx %02x %02x %02x xx %02x\n",
635 fdc_state.fdc_inb(FD_STATUS_A),
636 fdc_state.fdc_inb(FD_STATUS_B),
637 fdc_state.fdc_inb(FD_TDR),
638 fdc_state.fdc_inb(FD_STATUS),
639 fdc_state.fdc_inb(FD_DATA),
640 fdc_state.fdc_inb(FD_DIR));
642 printk_debug("status=%x\n", fdc_state.fdc_inb(FD_STATUS));
643 printk_debug("\n");
646 static void floppy_recalibrate(void)
648 unsigned char cmd[2];
649 unsigned char reply[MAX_REPLIES];
650 int nr, success;
651 success = 0;
652 do {
653 printk_debug("floppy_recalibrate\n");
654 /* Send the recalibrate command to the controller.
655 * We don't have interrupts or anything we can poll
656 * so we have to guess when it is done.
658 cmd[0] = FD_RECALIBRATE;
659 cmd[1] = 0;
660 if (output_command(cmd, 2) < 0)
661 continue;
663 /* Sleep for the maximum time the recalibrate command
664 * can run.
666 mdelay(80*DRIVE_H1440_SRT/1000);
668 /* Now call FD_SENSEI to end the command
669 * and collect up the reply.
671 if (output_byte(FD_SENSEI) < 0)
672 continue;
673 nr = result(reply, MAX_REPLIES);
675 /* Now see if we have succeeded in our seek */
676 success =
677 /* We have the right size result */
678 (nr == 2) &&
679 /* The command didn't terminate in error */
680 ((reply[0] & ST0_INTR) == ST0_INTR_OK) &&
681 /* We finished a seek */
682 (reply[0] & ST0_SE) &&
683 /* We are at cylinder 0 */
684 (reply[1] == 0);
685 } while(!success);
686 /* Remember we are at track 0 */
687 drive_state[FD_DRIVE].track = 0;
691 static int floppy_seek(unsigned track)
693 unsigned char cmd[3];
694 unsigned char reply[MAX_REPLIES];
695 int nr, success;
696 unsigned distance, old_track;
698 /* Look up the old track and see if we need to
699 * do anything.
701 old_track = drive_state[FD_DRIVE].track;
702 if (old_track == track) {
703 return 1;
706 /* Compute the distance we are about to move,
707 * We need to know this so we know how long to sleep...
709 distance = (old_track > track)?(old_track - track):(track - old_track);
710 distance += 1;
713 /* Send the seek command to the controller.
714 * We don't have interrupts or anything we can poll
715 * so we have to guess when it is done.
717 cmd[0] = FD_SEEK;
718 cmd[1] = FD_DRIVE;
719 cmd[2] = track;
720 if (output_command(cmd, 3) < 0)
721 return 0;
723 /* Sleep for the time it takes to step throuhg distance tracks.
725 mdelay(distance*DRIVE_H1440_SRT/1000);
727 /* Now call FD_SENSEI to end the command
728 * and collect up the reply.
730 cmd[0] = FD_SENSEI;
731 if (output_command(cmd, 1) < 0)
732 return 0;
733 nr = result(reply, MAX_REPLIES);
735 /* Now see if we have succeeded in our seek */
736 success =
737 /* We have the right size result */
738 (nr == 2) &&
739 /* The command didn't terminate in error */
740 ((reply[0] & ST0_INTR) == ST0_INTR_OK) &&
741 /* We finished a seek */
742 (reply[0] & ST0_SE) &&
743 /* We are at cylinder 0 */
744 (reply[1] == track);
745 if (success)
746 drive_state[FD_DRIVE].track = track;
747 else {
748 printk_debug("seek failed\n");
749 printk_debug("nr = %d\n", nr);
750 printk_debug("ST0 = %02x\n", reply[0]);
751 printk_debug("PCN = %02x\n", reply[1]);
752 printk_debug("status = %d\n", fdc_state.fdc_inb(FD_STATUS));
754 return success;
757 static int read_ok(unsigned head)
759 unsigned char results[7];
760 int result_ok;
761 int nr;
763 /* read back the read results */
764 nr = result(results, 7);
766 /* Now see if they say we are o.k. */
767 result_ok = 0;
768 /* Are my result bytes o.k.? */
769 if (nr == 7) {
770 /* Are we o.k. */
771 if ((results[0] & ST0_INTR) == ST0_INTR_OK) {
772 result_ok = 1;
774 /* Or did we get just an overflow error */
775 else if (((results[0] & ST0_INTR) == ST0_INTR_ERROR) &&
776 (results[1]== ST1_OR) &&
777 (results[2] == 0)) {
778 result_ok = 1;
780 /* Verify the reply had the correct head */
781 if (((results[0] & ST0_HA) >> 2) != head) {
782 result_ok = 0;
784 /* Verify the reply had the correct drive */
785 if (((results[0] & ST0_DS) != FD_DRIVE)) {
786 result_ok = 0;
789 if (!result_ok) {
790 printk_debug("result_bytes = %d\n", nr);
791 printk_debug("ST0 = %02x\n", results[0]);
792 printk_debug("ST1 = %02x\n", results[1]);
793 printk_debug("ST2 = %02x\n", results[2]);
794 printk_debug(" C = %02x\n", results[3]);
795 printk_debug(" H = %02x\n", results[4]);
796 printk_debug(" R = %02x\n", results[5]);
797 printk_debug(" N = %02x\n", results[6]);
799 return result_ok;
802 static int floppy_read_sectors(
803 char *dest, unsigned byte_offset, unsigned length,
804 unsigned sector, unsigned head, unsigned track)
806 /* MT == Multitrack */
807 /* MFM == MFM or FM Mode */
808 /* SK == Skip deleted data addres Mark */
809 /* HDS == Head number select */
810 /* DS0 == Disk Drive Select 0 */
811 /* DS1 == Disk Drive Select 1 */
812 /* C == Cylinder number 0 - 255 */
813 /* H == Head number */
814 /* R == Record */
815 /* N == The number of data bytes written in a sector */
816 /* EOT == End of Track */
817 /* GPL == Gap Length */
818 /* DTL == Data Length */
819 /* MT MFM SK 0 1 1 0 0 */
820 /* 0 0 0 0 0 HDS DS1 DS0 */
821 /* C, H, R, N, EOT, GPL, DTL */
823 int i, status, result_ok;
824 int max_bytes, bytes_read;
825 int ret;
826 unsigned char cmd[9];
827 unsigned end_offset;
829 end_offset = byte_offset + length;
830 max_bytes = 512*(DISK_H1440_SECT - sector + 1);
832 if (byte_offset >= max_bytes) {
833 return 0;
835 cmd[0] = FD_READ | (((DISK_H1440_HEAD ==2)?1:0) << 6);
836 cmd[1] = (head << 2) | FD_DRIVE;
837 cmd[2] = track;
838 cmd[3] = head;
839 cmd[4] = sector;
840 cmd[5] = 2; /* 2^N *128 == Sector size. Hard coded to 512 bytes */
841 cmd[6] = DISK_H1440_SECT;
842 cmd[7] = DISK_H1440_GAP;
843 cmd[8] = 0xff;
845 /* Output the command bytes */
846 if (output_command(cmd, 9) < 0)
847 return -1;
849 /* The execution stage begins when STATUS_READY&STATUS_NON_DMA is set */
850 do {
851 status = fdc_state.fdc_inb(FD_STATUS);
852 status &= STATUS_READY | STATUS_NON_DMA;
853 } while(status != (STATUS_READY|STATUS_NON_DMA));
855 for(i = 0; i < max_bytes; i++) {
856 unsigned char byte;
857 if ((status = wait_til_ready()) < 0) {
858 break;
860 status &= STATUS_READY|STATUS_DIR|STATUS_NON_DMA;
861 if (status != (STATUS_READY|STATUS_DIR|STATUS_NON_DMA)) {
862 break;
864 byte = fdc_state.fdc_inb(FD_DATA);
865 if ((i >= byte_offset) && (i < end_offset)) {
866 dest[i - byte_offset] = byte;
869 bytes_read = i;
871 /* The result stage begins when STATUS_NON_DMA is cleared */
872 while((status = fdc_state.fdc_inb(FD_STATUS)) & STATUS_NON_DMA) {
873 /* We get extra bytes in the fifo past
874 * the end of the sector and drop them on the floor.
875 * Otherwise the fifo is polluted.
877 fdc_state.fdc_inb(FD_DATA);
879 /* Did I get an error? */
880 result_ok = read_ok(head);
881 /* Did I read enough bytes? */
882 ret = -1;
883 if (result_ok && (bytes_read == max_bytes)) {
884 ret = bytes_read - byte_offset;
885 if (ret > length) {
886 ret = length;
890 if (ret < 0) {
891 printk_debug("ret = %d\n", ret);
892 printk_debug("bytes_read = %d\n", bytes_read);
893 printk_debug("status = %x\n", status);
895 return ret;
899 static int __floppy_read(char *dest, unsigned long offset, unsigned long length)
901 unsigned head, track, sector, byte_offset, sector_offset;
902 int ret;
904 /* break the offset up into sectors and bytes */
905 byte_offset = offset % 512;
906 sector_offset = offset / 512;
908 /* Find the disk block we are starting with... */
909 sector = (sector_offset % DISK_H1440_SECT) + 1;
910 head = (sector_offset / DISK_H1440_SECT) % DISK_H1440_HEAD;
911 track = (sector_offset / (DISK_H1440_SECT *DISK_H1440_HEAD))% DISK_H1440_TRACK;
913 /* First seek to our start track */
914 if (!floppy_seek(track)) {
915 return -1;
917 /* Then read the data */
918 ret = floppy_read_sectors(dest, byte_offset, length, sector, head, track);
919 if (ret >= 0) {
920 return ret;
922 /* If we failed reset the fdc... */
923 floppy_reset();
924 return -1;
927 static int floppy_read(char *dest, unsigned long offset, unsigned long length)
929 int fr_result, bytes_read;;
931 printk_debug("floppy_read\n");
932 bytes_read = 0;
933 do {
934 int max_errors = 3;
935 do {
936 fr_result = __floppy_read(dest + bytes_read, offset,
937 length - bytes_read);
938 if (max_errors-- == 0) {
939 return (bytes_read)?bytes_read: -1;
941 } while (fr_result <= 0);
942 offset += fr_result;
943 bytes_read += fr_result;
944 } while(bytes_read < length);
945 return bytes_read;
948 /* Determine the floppy disk controller type */
949 /* This routine was written by David C. Niemi */
950 static char get_fdc_version(void)
952 int bytes, ret;
953 unsigned char reply_buffer[MAX_REPLIES];
955 ret = output_byte(FD_DUMPREGS); /* 82072 and better know DUMPREGS */
956 if (ret < 0)
957 return FDC_NONE;
958 if ((bytes = result(reply_buffer, MAX_REPLIES)) <= 0x00)
959 return FDC_NONE; /* No FDC present ??? */
960 if ((bytes==1) && (reply_buffer[0] == 0x80)){
961 printk_info("FDC is an 8272A\n");
962 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */
964 if (bytes != 10) {
965 printk_debug("init: DUMPREGS: unexpected return of %d bytes.\n",
966 bytes);
967 return FDC_UNKNOWN;
969 if (!fdc_configure(USE_IMPLIED_SEEK, USE_FIFO, FIFO_THRESHOLD,
970 TRACK_PRECOMPENSATION)) {
971 printk_info("FDC is an 82072\n");
972 return FDC_82072; /* 82072 doesn't know CONFIGURE */
975 output_byte(FD_PERPENDICULAR);
976 if (need_more_output() == MORE_OUTPUT) {
977 output_byte(0);
978 } else {
979 printk_info("FDC is an 82072A\n");
980 return FDC_82072A; /* 82072A as found on Sparcs. */
983 output_byte(FD_UNLOCK);
984 bytes = result(reply_buffer, MAX_REPLIES);
985 if ((bytes == 1) && (reply_buffer[0] == 0x80)){
986 printk_info("FDC is a pre-1991 82077\n");
987 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know
988 * LOCK/UNLOCK */
990 if ((bytes != 1) || (reply_buffer[0] != 0x00)) {
991 printk_debug("FDC init: UNLOCK: unexpected return of %d bytes.\n",
992 bytes);
993 return FDC_UNKNOWN;
995 output_byte(FD_PARTID);
996 bytes = result(reply_buffer, MAX_REPLIES);
997 if (bytes != 1) {
998 printk_debug("FDC init: PARTID: unexpected return of %d bytes.\n",
999 bytes);
1000 return FDC_UNKNOWN;
1002 if (reply_buffer[0] == 0x80) {
1003 printk_info("FDC is a post-1991 82077\n");
1004 return FDC_82077; /* Revised 82077AA passes all the tests */
1006 switch (reply_buffer[0] >> 5) {
1007 case 0x0:
1008 /* Either a 82078-1 or a 82078SL running at 5Volt */
1009 printk_info("FDC is an 82078.\n");
1010 return FDC_82078;
1011 case 0x1:
1012 printk_info("FDC is a 44pin 82078\n");
1013 return FDC_82078;
1014 case 0x2:
1015 printk_info("FDC is a S82078B\n");
1016 return FDC_S82078B;
1017 case 0x3:
1018 printk_info("FDC is a National Semiconductor PC87306\n");
1019 return FDC_87306;
1020 default:
1021 printk_info("FDC init: 82078 variant with unknown PARTID=%d.\n",
1022 reply_buffer[0] >> 5);
1023 return FDC_82078_UNKN;
1025 } /* get_fdc_version */
1028 static int floppy_init(unsigned long io_base, unsigned long mmio_base)
1030 printk_debug("floppy_init\n");
1031 fdc_state.in_sync = 0;
1032 fdc_state.spec1 = -1;
1033 fdc_state.spec2 = -1;
1034 fdc_state.dtr = -1;
1035 fdc_state.dor = DOR_NO_RESET;
1036 fdc_state.version = FDC_UNKNOWN;
1037 if (mmio_base) {
1038 fdc_state.fdc_inb = ob_fdc_mmio_readb;
1039 fdc_state.fdc_outb = ob_fdc_mmio_writeb;
1040 } else {
1041 fdc_state.fdc_inb = ob_fdc_inb;
1042 fdc_state.fdc_outb = ob_fdc_outb;
1044 fdc_state.io_base = io_base;
1045 fdc_state.mmio_base = mmio_base;
1046 reset_fdc();
1047 /* Try to determine the floppy controller type */
1048 fdc_state.version = get_fdc_version();
1049 if (fdc_state.version == FDC_NONE) {
1050 return -1;
1052 floppy_reset();
1053 printk_info("fdc_state.version = %04x\n", fdc_state.version);
1054 return 0;
1057 static void floppy_reset(void)
1059 printk_debug("floppy_reset\n");
1060 floppy_motor_off(FD_DRIVE);
1061 reset_fdc();
1062 fdc_dtr(DISK_H1440_RATE);
1063 /* program data rate via ccr */
1064 collect_interrupt();
1065 fdc_configure(USE_IMPLIED_SEEK, USE_FIFO, FIFO_THRESHOLD,
1066 TRACK_PRECOMPENSATION);
1067 fdc_specify(DRIVE_H1440_HLT, DRIVE_H1440_HUT, DRIVE_H1440_SRT);
1068 set_drive(FD_DRIVE);
1069 floppy_recalibrate();
1070 fdc_state.in_sync = 1;
1073 static void
1074 ob_floppy_initialize(const char *path)
1076 int props[3];
1077 phandle_t ph = find_dev(path);
1079 set_property(ph, "device_type", "block", sizeof("block"));
1081 // Set dummy reg properties
1082 props[0] = __cpu_to_be32(0); props[1] = __cpu_to_be32(0); props[2] = __cpu_to_be32(0);
1083 set_property(ph, "reg", (char *)&props, 3*sizeof(int));
1085 fword("is-deblocker");
1089 static void
1090 ob_floppy_open(int *idx)
1092 int ret = 1;
1093 phandle_t ph;
1095 fword("my-unit");
1096 idx[0]=POP();
1098 fword("my-parent");
1099 fword("ihandle>phandle");
1100 ph=(phandle_t)POP();
1102 selfword("open-deblocker");
1104 /* interpose disk-label */
1105 ph = find_dev("/packages/disk-label");
1106 fword("my-args");
1107 PUSH_ph( ph );
1108 fword("interpose");
1110 RET ( -ret );
1113 static void
1114 ob_floppy_close(int *idx)
1116 selfword("close-deblocker");
1119 static void
1120 ob_floppy_read_blocks(int *idx)
1122 cell cnt = POP();
1123 ucell blk = POP();
1124 char *dest = (char*)POP();
1125 floppy_read(dest, blk*512, cnt*512);
1126 PUSH(cnt);
1130 static void
1131 ob_floppy_block_size(int *idx)
1133 PUSH(512);
1136 static void
1137 ob_floppy_max_transfer(int *idx)
1139 // Fixme
1140 PUSH(18 * 512);
1143 NODE_METHODS(ob_floppy) = {
1144 { "open", ob_floppy_open },
1145 { "close", ob_floppy_close },
1146 { "read-blocks", ob_floppy_read_blocks },
1147 { "block-size", ob_floppy_block_size },
1148 { "max-transfer", ob_floppy_max_transfer },
1152 int ob_floppy_init(const char *path, const char *dev_name,
1153 unsigned long io_base, unsigned long mmio_base)
1155 char nodebuff[128];
1156 phandle_t aliases;
1158 snprintf(nodebuff, sizeof(nodebuff), "%s/%s", path, dev_name);
1159 if (!mmio_base) {
1160 REGISTER_NAMED_NODE(ob_floppy, nodebuff);
1161 ob_floppy_initialize(nodebuff);
1162 } else {
1163 // Already in tree and mapped
1164 REGISTER_NODE_METHODS(ob_floppy, nodebuff);
1166 floppy_init(io_base, mmio_base);
1168 aliases = find_dev("/aliases");
1169 set_property(aliases, "floppy", nodebuff, strlen(nodebuff) + 1);
1171 return 0;