// SPDX-License-Identifier: GPL-2.0-only /* * linux/amiga/amiflop.c * * Copyright (C) 1993 Greg Harp * Portions of this driver are based on code contributed by Brad Pepers * * revised 28.5.95 by Joerg Dorchain * - now no bugs(?) any more for both HD & DD * - added support for 40 Track 5.25" drives, 80-track hopefully behaves * like 3.5" dd (no way to test - are there any 5.25" drives out there * that work on an A4000?) * - wrote formatting routine (maybe dirty, but works) * * june/july 1995 added ms-dos support by Joerg Dorchain * (portions based on messydos.device and various contributors) * - currently only 9 and 18 sector disks * * - fixed a bug with the internal trackbuffer when using multiple * disks the same time * - made formatting a bit safer * - added command line and machine based default for "silent" df0 * * december 1995 adapted for 1.2.13pl4 by Joerg Dorchain * - works but I think it's inefficient. (look in redo_fd_request) * But the changes were very efficient. (only three and a half lines) * * january 1996 added special ioctl for tracking down read/write problems * - usage ioctl(d, RAW_TRACK, ptr); the raw track buffer (MFM-encoded data * is copied to area. (area should be large enough since no checking is * done - 30K is currently sufficient). return the actual size of the * trackbuffer * - replaced udelays() by a timer (CIAA timer B) for the waits * needed for the disk mechanic. * * february 1996 fixed error recovery and multiple disk access * - both got broken the first time I tampered with the driver :-( * - still not safe, but better than before * * revised Marts 3rd, 1996 by Jes Sorensen for use in the 1.3.28 kernel. * - Minor changes to accept the kdev_t. * - Replaced some more udelays with ms_delays. Udelay is just a loop, * and so the delay will be different depending on the given * processor :-( * - The driver could use a major cleanup because of the new * major/minor handling that came with kdev_t. It seems to work for * the time being, but I can't guarantee that it will stay like * that when we start using 16 (24?) bit minors. * * restructured jan 1997 by Joerg Dorchain * - Fixed Bug accessing multiple disks * - some code cleanup * - added trackbuffer for each drive to speed things up * - fixed some race conditions (who finds the next may send it to me ;-)
*/
#define DSKRDY (0x1<<5) /* disk ready when low */ #define DSKTRACK0 (0x1<<4) /* head at track zero when low */ #define DSKPROT (0x1<<3) /* disk protected when low */ #define DSKCHANGE (0x1<<2) /* low when disk removed */
/* * CIAAPRB bits (read/write)
*/
#define DSKMOTOR (0x1<<7) /* motor on when low */ #define DSKSEL3 (0x1<<6) /* select drive 3 when low */ #define DSKSEL2 (0x1<<5) /* select drive 2 when low */ #define DSKSEL1 (0x1<<4) /* select drive 1 when low */ #define DSKSEL0 (0x1<<3) /* select drive 0 when low */ #define DSKSIDE (0x1<<2) /* side selection: 0 = upper, 1 = lower */ #define DSKDIREC (0x1<<1) /* step direction: 0=in, 1=out (to trk 0) */ #define DSKSTEP (0x1) /* pulse low to step head 1 track */
/* * DSKBYTR bits (read only)
*/
#define DSKBYT (1<<15) /* register contains valid byte when set */ #define DMAON (1<<14) /* disk DMA enabled */ #define DISKWRITE (1<<13) /* disk write bit in DSKLEN enabled */ #define WORDEQUAL (1<<12) /* DSKSYNC register match when true */ /* bits 7-0 are data */
#define MFM_SYNC 0x4489 /* standard MFM sync value */
/* Values for FD_COMMAND */ #define FD_RECALIBRATE 0x07 /* move to track 0 */ #define FD_SEEK 0x0F /* seek track */ #define FD_READ 0xE6 /* read with MT, MFM, SKip deleted */ #define FD_WRITE 0xC5 /* write with MT, MFM */ #define FD_SENSEI 0x08 /* Sense Interrupt Status */ #define FD_SPECIFY 0x03 /* specify HUT etc */ #define FD_FORMAT 0x4D /* format one track */ #define FD_VERSION 0x10 /* get version code */ #define FD_CONFIGURE 0x13 /* configure FIFO operation */ #define FD_PERPENDICULAR 0x12 /* perpendicular r/w mode */
#define FD_MAX_UNITS 4 /* Max. Number of drives */ #define FLOPPY_MAX_SECTORS 22 /* Max. Number of sectors per track */
struct fd_data_type { char *name; /* description of data type */ int sects; /* sectors per track */ int (*read_fkt)(int); /* read whole track */ void (*write_fkt)(int); /* write whole track */
};
struct fd_drive_type { unsignedlong code; /* code returned from drive */ char *name; /* description of drive */ unsignedint tracks; /* number of tracks */ unsignedint heads; /* number of heads */ unsignedint read_size; /* raw read size for one track */ unsignedint write_size; /* raw write size for one track */ unsignedint sect_mult; /* sectors and gap multiplier (HD = 2) */ unsignedint precomp1; /* start track for precomp 1 */ unsignedint precomp2; /* start track for precomp 2 */ unsignedint step_delay; /* time (in ms) for delay after step */ unsignedint settle_time; /* time to settle after dir change */ unsignedint side_time; /* time needed to change sides */
};
struct amiga_floppy_struct { struct fd_drive_type *type; /* type of floppy for this unit */ struct fd_data_type *dtype; /* type of floppy for this unit */ int track; /* current track (-1 == unknown) */ unsignedchar *trackbuf; /* current track (kmaloc()'d */
int blocks; /* total # blocks on disk */
int changed; /* true when not known */ int disk; /* disk in drive (-1 == unknown) */ int motor; /* true when motor is at speed */ int busy; /* true when drive is active */ int dirty; /* true when trackbuf is not on disk */ int status; /* current error code for unit */ struct gendisk *gendisk[2]; struct blk_mq_tag_set tag_set;
};
/* * Error codes
*/ #define FD_OK 0 /* operation succeeded */ #define FD_ERROR -1 /* general error (seek, read, write, etc) */ #define FD_NOUNIT 1 /* unit does not exist */ #define FD_UNITBUSY 2 /* unit already active */ #define FD_NOTACTIVE 3 /* unit is not active */ #define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */
#define RAW_BUF_SIZE 30000 /* size of raw disk data */
/* * These are global variables, as that's the easiest way to give * information to interrupts. They are the data used for the current * request.
*/ staticvolatilechar block_flag; static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block);
/* * Note that MAX_ERRORS=X doesn't imply that we retry every bad read * max X times - some types of errors increase the errorcount by 2 or * even 3, so we might actually retry only X/2 times before giving up.
*/ #define MAX_ERRORS 12
/* * Here come the actual hardware access and helper functions. * They are not reentrant and single threaded because all drives * share the same hardware and the same trackbuffer.
*/
/* all waits are queued up
A more generic routine would do a schedule a la timer.device */ staticvoid ms_delay(int ms)
{ int ticks; static DEFINE_MUTEX(mutex);
drive&=3; if (!try_fdc(drive)) { /* We would be blocked in an interrupt, so try again later */
timer->expires = jiffies + 1;
add_timer(timer); return;
}
unit[drive].motor = 0;
fd_select(drive);
udelay (1);
fd_deselect(drive);
}
staticvoid floppy_off (unsignedint nr)
{ int drive;
staticunsignedlong fd_get_drive_id(int drive)
{ int i;
ulong id = 0;
drive&=3;
get_fdc(drive); /* set up for ID */
MOTOR_ON;
udelay(2);
SELECT(SELMASK(drive));
udelay(2);
DESELECT(SELMASK(drive));
udelay(2);
MOTOR_OFF;
udelay(2);
SELECT(SELMASK(drive));
udelay(2);
DESELECT(SELMASK(drive));
udelay(2);
/* loop and read disk ID */ for (i=0; i<32; i++) {
SELECT(SELMASK(drive));
udelay(2);
/* read and store value of DSKRDY */
id <<= 1;
id |= (ciaa.pra & DSKRDY) ? 0 : 1; /* cia regs are low-active! */
DESELECT(SELMASK(drive));
}
rel_fdc();
/* * RB: At least A500/A2000's df0: don't identify themselves. * As every (real) Amiga has at least a 3.5" DD drive as df0: * we default to that if df0: doesn't identify as a certain * type.
*/ if(drive == 0 && id == FD_NODRIVE)
{
id = fd_def_df0;
printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx\n", (ulong)fd_def_df0);
} /* return the ID value */ return (id);
}
/* * to be called at least 2ms after the write has finished but before any * other access to the hardware.
*/ staticvoid post_write (unsignedlong drive)
{ #ifdef DEBUG
printk("post_write for drive %ld\n",drive); #endif
drive &= 3;
custom.dsklen = 0;
block_flag = 0;
writepending = 0;
writefromint = 0;
unit[drive].dirty = 0;
wake_up(&wait_fd_block);
fd_deselect(drive);
rel_fdc(); /* corresponds to get_fdc() in raw_write */
}
struct dos_header { unsignedchar track, /* 0-80 */
side, /* 0-1 */
sec, /* 0-...*/
len_desc;/* 2 */ unsignedshort crc; /* on 68000 we got an alignment problem, but this compiler solves it by adding silently adding a pad byte so data won't fit
and this took about 3h to discover.... */ unsignedchar gap1[22]; /* for longword-alignedness (0x4e) */
};
/* crc routines are borrowed from the messydos-handler */
/* excerpt from the messydos-device ; The CRC is computed not only over the actual data, but including ; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb). ; As we don't read or encode these fields into our buffers, we have to ; preload the registers containing the CRC with the values they would have ; after stepping over these fields. ; ; How CRCs "really" work: ; ; First, you should regard a bitstring as a series of coefficients of ; polynomials. We calculate with these polynomials in modulo-2 ; arithmetic, in which both add and subtract are done the same as ; exclusive-or. Now, we modify our data (a very long polynomial) in ; such a way that it becomes divisible by the CCITT-standard 16-bit ; 16 12 5 ; polynomial: x + x + x + 1, represented by $11021. The easiest ; way to do this would be to multiply (using proper arithmetic) our ; datablock with $11021. So we have: ; data * $11021 = ; data * ($10000 + $1021) = ; data * $10000 + data * $1021 ; The left part of this is simple: Just add two 0 bytes. But then ; the right part (data $1021) remains difficult and even could have ; a carry into the left part. The solution is to use a modified ; multiplication, which has a result that is not correct, but with ; a difference of any multiple of $11021. We then only need to keep ; the 16 least significant bits of the result. ; ; The following algorithm does this for us: ; ; unsigned char *data, c, crclo, crchi; ; while (not done) { ; c = *data++ + crchi; ; crchi = (@ c) >> 8 + crclo; ; crclo = @ c; ; } ; ; Remember, + is done with EOR, the @ operator is in two tables (high ; and low byte separately), which is calculated as ; ; $1021 * (c & $F0) ; xor $1021 * (c & $0F) ; xor $1021 * (c >> 4) (* is regular multiplication) ; ; ; Anyway, the end result is the same as the remainder of the division of ; the data by $11021. I am afraid I need to study theory a bit more...
/* look at the asm-code - what looks in C a bit strange is almost as good as handmade */ registerint i; registerunsignedchar *CRCT1, *CRCT2, *data, c, crch, crcl;
drive&=3;
raw = (long) raw_buf;
end = raw + unit[drive].type->read_size;
for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) { do { /* search for the right sync of each sec-hdr */ if (!(raw = scan_sync (raw, end))) {
printk(KERN_INFO "dos_read: no hdr sync on " "track %d, unit %d for sector %d\n",
unit[drive].track,drive,scnt); return MFM_NOSYNC;
} #ifdef DEBUG
dbg(raw); #endif
} while (*((ushort *)raw)!=0x5554); /* loop usually only once done */
raw+=2; /* skip over headermark */
raw = dos_decode((unsignedchar *)&hdr,(ushort *) raw,8);
crc = dos_hdr_crc(&hdr);
if (hdr.len_desc != 2) {
printk(KERN_INFO "dos_read: unknown sector len " "descriptor %d\n", hdr.len_desc); return MFM_DATA;
} #ifdef DEBUG
printk("hdr accepted\n"); #endif if (!(raw = scan_sync (raw, end))) {
printk(KERN_INFO "dos_read: no data sync on track " "%d, unit %d for sector%d, disk sector %d\n",
unit[drive].track, drive, scnt, hdr.sec); return MFM_NOSYNC;
} #ifdef DEBUG
dbg(raw); #endif
if (*((ushort *)raw)!=0x5545) {
printk(KERN_INFO "dos_read: no data mark after " "sync (%d,%d,%d,%d) sc=%d\n",
hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt); return MFM_NOSYNC;
}
raw+=2; /* skip data mark (included in checksum) */
raw = dos_decode((unsignedchar *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512);
raw = dos_decode((unsignedchar *)data_crc,(ushort *) raw,4);
crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512);
staticvoid dos_encode_block(ushort *dest, unsignedchar *src, int len)
{ int i;
for (i = 0; i < len; i++) {
*dest=dos_encode_byte(*src++);
*dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000;
dest++;
}
}
staticunsignedlong *ms_putsec(int drive, unsignedlong *raw, int cnt)
{ staticstruct dos_header hdr={0,0,0,2,0,
{78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}}; int i; static ushort crc[2]={0,0x4e4e};
drive&=3; /* id gap 1 */ /* the MFM word before is always 9254 */ for(i=0;i<6;i++)
*raw++=0xaaaaaaaa; /* 3 sync + 1 headermark */
*raw++=0x44894489;
*raw++=0x44895554;
/* fill in the variable parts of the header */
hdr.track=unit[drive].track/unit[drive].type->heads;
hdr.side=unit[drive].track%unit[drive].type->heads;
hdr.sec=cnt+1;
hdr.crc=dos_hdr_crc(&hdr);
/* header (without "magic") and id gap 2*/
dos_encode_block((ushort *)raw,(unsignedchar *) &hdr.track,28);
raw+=14;
/*id gap 3 */ for(i=0;i<6;i++)
*raw++=0xaaaaaaaa;
/* 3 syncs and 1 datamark */
*raw++=0x44894489;
*raw++=0x44895545;
/* data */
dos_encode_block((ushort *)raw,
(unsignedchar *)unit[drive].trackbuf+cnt*512,512);
raw+=256;
/*data crc + jd's special gap (long words :-/) */
crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512);
dos_encode_block((ushort *) raw,(unsignedchar *)crc,4);
raw+=2;
/* data gap */ for(i=0;i<38;i++)
*raw++=0x92549254;
return raw; /* wrote 652 MFM words */
}
staticvoid dos_write(int disk)
{ int cnt; unsignedlong raw = (unsignedlong) raw_buf; unsignedlong *ptr=(unsignedlong *)raw;
disk&=3; /* really gap4 + indexgap , but we write it first and round it up */ for (cnt=0;cnt<425;cnt++)
*ptr++=0x92549254;
/* the following is just guessed */ if (unit[disk].type->sect_mult==2) /* check for HD-Disks */ for(cnt=0;cnt<473;cnt++)
*ptr++=0x92549254;
/* now the index marks...*/ for (cnt=0;cnt<20;cnt++)
*ptr++=0x92549254; for (cnt=0;cnt<6;cnt++)
*ptr++=0xaaaaaaaa;
*ptr++=0x52245224;
*ptr++=0x52245552; for (cnt=0;cnt<20;cnt++)
*ptr++=0x92549254;
*(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */
}
/* * Here comes the high level stuff (i.e. the filesystem interface) * and helper functions. * Normally this should be the only part that has to be adapted to * different kernel versions.
*/
/* FIXME: this assumes the drive is still spinning - * which is only true if we complete writing a track within three seconds
*/ staticvoid flush_track_callback(struct timer_list *timer)
{ unsignedlong nr = ((unsignedlong)timer -
(unsignedlong)&flush_track_timer[0]) / sizeof(flush_track_timer[0]);
nr&=3;
writefromint = 1; if (!try_fdc(nr)) { /* we might block in an interrupt, so try again later */
flush_track_timer[nr].expires = jiffies + 1;
add_timer(flush_track_timer + nr); return;
}
get_fdc(nr);
(*unit[nr].dtype->write_fkt)(nr); if (!raw_write(nr)) {
printk (KERN_NOTICE "floppy disk write protected\n");
writefromint = 0;
writepending = 0;
}
rel_fdc();
}
/* keep the drive spinning while writes are scheduled */ if (!fd_motor_on(drive)) return BLK_STS_IOERR; /* * setup a callback to write the track buffer * after a short (1 tick) delay.
*/
floppy->dirty = 1; /* reset the timer */
mod_timer (flush_track_timer + drive, jiffies + 1);
}
}
/* * floppy_open check for aliasing (/dev/fd0 can be the same as * /dev/PS0 etc), and disallows simultaneous access to the same * drive with different device numbers.
*/ staticint floppy_open(struct gendisk *disk, blk_mode_t mode)
{ int drive = disk->first_minor & 3; int system = (disk->first_minor & 4) >> 2; int old_dev; unsignedlong flags;
if (!fd_ref[drive]--) {
printk(KERN_CRIT "floppy_release with fd_ref == 0");
fd_ref[drive] = 0;
} #ifdef MODULE
floppy_off (drive); #endif
mutex_unlock(&amiflop_mutex);
}
/* * check_events is never called from an interrupt, so we can relax a bit * here, sleep etc. Note that floppy-on tries to set current_DOR to point * to the desired drive, but it will probably not survive the sleep if * several floppies are used at the same time: thus the loop.
*/ staticunsigned amiga_check_events(struct gendisk *disk, unsignedint clearing)
{ struct amiga_floppy_struct *p = disk->private_data; int drive = p - unit; int changed; staticint first_time = 1;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.