1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/amiga/amiflop.c
5 * Copyright (C) 1993 Greg Harp
6 * Portions of this driver are based on code contributed by Brad Pepers
8 * revised 28.5.95 by Joerg Dorchain
9 * - now no bugs(?) any more for both HD & DD
10 * - added support for 40 Track 5.25" drives, 80-track hopefully behaves
11 * like 3.5" dd (no way to test - are there any 5.25" drives out there
12 * that work on an A4000?)
13 * - wrote formatting routine (maybe dirty, but works)
15 * june/july 1995 added ms-dos support by Joerg Dorchain
16 * (portions based on messydos.device and various contributors)
17 * - currently only 9 and 18 sector disks
19 * - fixed a bug with the internal trackbuffer when using multiple
21 * - made formatting a bit safer
22 * - added command line and machine based default for "silent" df0
24 * december 1995 adapted for 1.2.13pl4 by Joerg Dorchain
25 * - works but I think it's inefficient. (look in redo_fd_request)
26 * But the changes were very efficient. (only three and a half lines)
28 * january 1996 added special ioctl for tracking down read/write problems
29 * - usage ioctl(d, RAW_TRACK, ptr); the raw track buffer (MFM-encoded data
30 * is copied to area. (area should be large enough since no checking is
31 * done - 30K is currently sufficient). return the actual size of the
33 * - replaced udelays() by a timer (CIAA timer B) for the waits
34 * needed for the disk mechanic.
36 * february 1996 fixed error recovery and multiple disk access
37 * - both got broken the first time I tampered with the driver :-(
38 * - still not safe, but better than before
40 * revised Marts 3rd, 1996 by Jes Sorensen for use in the 1.3.28 kernel.
41 * - Minor changes to accept the kdev_t.
42 * - Replaced some more udelays with ms_delays. Udelay is just a loop,
43 * and so the delay will be different depending on the given
45 * - The driver could use a major cleanup because of the new
46 * major/minor handling that came with kdev_t. It seems to work for
47 * the time being, but I can't guarantee that it will stay like
48 * that when we start using 16 (24?) bit minors.
50 * restructured jan 1997 by Joerg Dorchain
51 * - Fixed Bug accessing multiple disks
53 * - added trackbuffer for each drive to speed things up
54 * - fixed some race conditions (who finds the next may send it to me ;-)
57 #include <linux/module.h>
58 #include <linux/slab.h>
61 #include <linux/hdreg.h>
62 #include <linux/delay.h>
63 #include <linux/init.h>
64 #include <linux/major.h>
65 #include <linux/mutex.h>
67 #include <linux/blk-mq.h>
68 #include <linux/elevator.h>
69 #include <linux/interrupt.h>
70 #include <linux/platform_device.h>
72 #include <asm/setup.h>
73 #include <linux/uaccess.h>
74 #include <asm/amigahw.h>
75 #include <asm/amigaints.h>
78 #undef DEBUG /* print _LOTS_ of infos */
82 #define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */
90 * CIAAPRA bits (read only)
93 #define DSKRDY (0x1<<5) /* disk ready when low */
94 #define DSKTRACK0 (0x1<<4) /* head at track zero when low */
95 #define DSKPROT (0x1<<3) /* disk protected when low */
96 #define DSKCHANGE (0x1<<2) /* low when disk removed */
99 * CIAAPRB bits (read/write)
102 #define DSKMOTOR (0x1<<7) /* motor on when low */
103 #define DSKSEL3 (0x1<<6) /* select drive 3 when low */
104 #define DSKSEL2 (0x1<<5) /* select drive 2 when low */
105 #define DSKSEL1 (0x1<<4) /* select drive 1 when low */
106 #define DSKSEL0 (0x1<<3) /* select drive 0 when low */
107 #define DSKSIDE (0x1<<2) /* side selection: 0 = upper, 1 = lower */
108 #define DSKDIREC (0x1<<1) /* step direction: 0=in, 1=out (to trk 0) */
109 #define DSKSTEP (0x1) /* pulse low to step head 1 track */
112 * DSKBYTR bits (read only)
115 #define DSKBYT (1<<15) /* register contains valid byte when set */
116 #define DMAON (1<<14) /* disk DMA enabled */
117 #define DISKWRITE (1<<13) /* disk write bit in DSKLEN enabled */
118 #define WORDEQUAL (1<<12) /* DSKSYNC register match when true */
119 /* bits 7-0 are data */
122 * ADKCON/ADKCONR bits
126 #define ADK_SETCLR (1<<15) /* control bit */
128 #define ADK_PRECOMP1 (1<<14) /* precompensation selection */
129 #define ADK_PRECOMP0 (1<<13) /* 00=none, 01=140ns, 10=280ns, 11=500ns */
130 #define ADK_MFMPREC (1<<12) /* 0=GCR precomp., 1=MFM precomp. */
131 #define ADK_WORDSYNC (1<<10) /* enable DSKSYNC auto DMA */
132 #define ADK_MSBSYNC (1<<9) /* when 1, enable sync on MSbit (for GCR) */
133 #define ADK_FAST (1<<8) /* bit cell: 0=2us (GCR), 1=1us (MFM) */
139 #define DSKLEN_DMAEN (1<<15)
140 #define DSKLEN_WRITE (1<<14)
146 #define DSKINDEX (0x1<<4) /* DSKINDEX bit */
152 #define MFM_SYNC 0x4489 /* standard MFM sync value */
154 /* Values for FD_COMMAND */
155 #define FD_RECALIBRATE 0x07 /* move to track 0 */
156 #define FD_SEEK 0x0F /* seek track */
157 #define FD_READ 0xE6 /* read with MT, MFM, SKip deleted */
158 #define FD_WRITE 0xC5 /* write with MT, MFM */
159 #define FD_SENSEI 0x08 /* Sense Interrupt Status */
160 #define FD_SPECIFY 0x03 /* specify HUT etc */
161 #define FD_FORMAT 0x4D /* format one track */
162 #define FD_VERSION 0x10 /* get version code */
163 #define FD_CONFIGURE 0x13 /* configure FIFO operation */
164 #define FD_PERPENDICULAR 0x12 /* perpendicular r/w mode */
166 #define FD_MAX_UNITS 4 /* Max. Number of drives */
167 #define FLOPPY_MAX_SECTORS 22 /* Max. Number of sectors per track */
169 struct fd_data_type {
170 char *name; /* description of data type */
171 int sects; /* sectors per track */
172 int (*read_fkt)(int); /* read whole track */
173 void (*write_fkt)(int); /* write whole track */
176 struct fd_drive_type {
177 unsigned long code; /* code returned from drive */
178 char *name; /* description of drive */
179 unsigned int tracks; /* number of tracks */
180 unsigned int heads; /* number of heads */
181 unsigned int read_size; /* raw read size for one track */
182 unsigned int write_size; /* raw write size for one track */
183 unsigned int sect_mult; /* sectors and gap multiplier (HD = 2) */
184 unsigned int precomp1; /* start track for precomp 1 */
185 unsigned int precomp2; /* start track for precomp 2 */
186 unsigned int step_delay; /* time (in ms) for delay after step */
187 unsigned int settle_time; /* time to settle after dir change */
188 unsigned int side_time; /* time needed to change sides */
191 struct amiga_floppy_struct {
192 struct fd_drive_type *type; /* type of floppy for this unit */
193 struct fd_data_type *dtype; /* type of floppy for this unit */
194 int track; /* current track (-1 == unknown) */
195 unsigned char *trackbuf; /* current track (kmaloc()'d */
197 int blocks; /* total # blocks on disk */
199 int changed; /* true when not known */
200 int disk; /* disk in drive (-1 == unknown) */
201 int motor; /* true when motor is at speed */
202 int busy; /* true when drive is active */
203 int dirty; /* true when trackbuf is not on disk */
204 int status; /* current error code for unit */
205 struct gendisk *gendisk[2];
206 struct blk_mq_tag_set tag_set;
212 #define FD_OK 0 /* operation succeeded */
213 #define FD_ERROR -1 /* general error (seek, read, write, etc) */
214 #define FD_NOUNIT 1 /* unit does not exist */
215 #define FD_UNITBUSY 2 /* unit already active */
216 #define FD_NOTACTIVE 3 /* unit is not active */
217 #define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */
227 #define FD_NODRIVE 0x00000000 /* response when no unit is present */
228 #define FD_DD_3 0xffffffff /* double-density 3.5" (880K) drive */
229 #define FD_HD_3 0x55555555 /* high-density 3.5" (1760K) drive */
230 #define FD_DD_5 0xaaaaaaaa /* double-density 5.25" (440K) drive */
232 static DEFINE_MUTEX(amiflop_mutex);
233 static unsigned long int fd_def_df0 = FD_DD_3; /* default for df0 if it doesn't identify */
235 module_param(fd_def_df0, ulong, 0);
236 MODULE_LICENSE("GPL");
241 #define MOTOR_ON (ciab.prb &= ~DSKMOTOR)
242 #define MOTOR_OFF (ciab.prb |= DSKMOTOR)
243 #define SELECT(mask) (ciab.prb &= ~mask)
244 #define DESELECT(mask) (ciab.prb |= mask)
245 #define SELMASK(drive) (1 << (3 + (drive & 3)))
247 static struct fd_drive_type drive_types[] = {
248 /* code name tr he rdsz wrsz sm pc1 pc2 sd st st*/
249 /* warning: times are now in milliseconds (ms) */
250 { FD_DD_3, "DD 3.5", 80, 2, 14716, 13630, 1, 80,161, 3, 18, 1},
251 { FD_HD_3, "HD 3.5", 80, 2, 28344, 27258, 2, 80,161, 3, 18, 1},
252 { FD_DD_5, "DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2},
253 { FD_NODRIVE, "No Drive", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
255 static int num_dr_types = ARRAY_SIZE(drive_types);
257 static int amiga_read(int), dos_read(int);
258 static void amiga_write(int), dos_write(int);
259 static struct fd_data_type data_types[] = {
260 { "Amiga", 11 , amiga_read, amiga_write},
261 { "MS-Dos", 9, dos_read, dos_write}
264 /* current info on each unit */
265 static struct amiga_floppy_struct unit[FD_MAX_UNITS];
267 static struct timer_list flush_track_timer[FD_MAX_UNITS];
268 static struct timer_list post_write_timer;
269 static unsigned long post_write_timer_drive;
270 static struct timer_list motor_on_timer;
271 static struct timer_list motor_off_timer[FD_MAX_UNITS];
272 static int on_attempts;
274 /* Synchronization of FDC access */
275 /* request loop (trackbuffer) */
276 static volatile int fdc_busy = -1;
277 static volatile int fdc_nested;
278 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
280 static DECLARE_COMPLETION(motor_on_completion);
282 static volatile int selected = -1; /* currently selected drive */
284 static int writepending;
285 static int writefromint;
286 static char *raw_buf;
288 static DEFINE_SPINLOCK(amiflop_lock);
290 #define RAW_BUF_SIZE 30000 /* size of raw disk data */
293 * These are global variables, as that's the easiest way to give
294 * information to interrupts. They are the data used for the current
297 static volatile char block_flag;
298 static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block);
300 /* MS-Dos MFM Coding tables (should go quick and easy) */
301 static unsigned char mfmencode[16]={
302 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
303 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
305 static unsigned char mfmdecode[128];
307 /* floppy internal millisecond timer stuff */
308 static DECLARE_COMPLETION(ms_wait_completion);
309 #define MS_TICKS ((amiga_eclock+50)/1000)
312 * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
313 * max X times - some types of errors increase the errorcount by 2 or
314 * even 3, so we might actually retry only X/2 times before giving up.
316 #define MAX_ERRORS 12
318 #define custom amiga_custom
320 /* Prevent "aliased" accesses. */
321 static int fd_ref[4] = { 0,0,0,0 };
322 static int fd_device[4] = { 0, 0, 0, 0 };
325 * Here come the actual hardware access and helper functions.
326 * They are not reentrant and single threaded because all drives
327 * share the same hardware and the same trackbuffer.
330 /* Milliseconds timer */
332 static irqreturn_t ms_isr(int irq, void *dummy)
334 complete(&ms_wait_completion);
338 /* all waits are queued up
339 A more generic routine would do a schedule a la timer.device */
340 static void ms_delay(int ms)
343 static DEFINE_MUTEX(mutex);
347 ticks = MS_TICKS*ms-1;
350 ciaa.crb=0x19; /*count eclock, force load, one-shoot, start */
351 wait_for_completion(&ms_wait_completion);
352 mutex_unlock(&mutex);
356 /* Hardware semaphore */
358 /* returns true when we would get the semaphore */
359 static inline int try_fdc(int drive)
362 return ((fdc_busy < 0) || (fdc_busy == drive));
365 static void get_fdc(int drive)
371 printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive,fdc_busy,fdc_nested);
373 local_irq_save(flags);
374 wait_event(fdc_wait, try_fdc(drive));
377 local_irq_restore(flags);
380 static inline void rel_fdc(void)
384 printk("fd: unmatched rel_fdc\n");
385 printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy,fdc_nested);
388 if (fdc_nested == 0) {
394 static void fd_select (int drive)
396 unsigned char prb = ~0;
400 printk("selecting %d\n",drive);
402 if (drive == selected)
407 if (unit[drive].track % 2 != 0)
409 if (unit[drive].motor == 1)
411 ciab.prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
413 prb &= ~SELMASK(drive);
418 static void fd_deselect (int drive)
425 printk("deselecting %d\n",drive);
427 if (drive != selected) {
428 printk(KERN_WARNING "Deselecting drive %d while %d was selected!\n",drive,selected);
433 local_irq_save(flags);
438 prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
441 local_irq_restore (flags);
446 static void motor_on_callback(struct timer_list *unused)
448 if (!(ciaa.pra & DSKRDY) || --on_attempts == 0) {
449 complete_all(&motor_on_completion);
451 motor_on_timer.expires = jiffies + HZ/10;
452 add_timer(&motor_on_timer);
456 static int fd_motor_on(int nr)
460 del_timer(motor_off_timer + nr);
462 if (!unit[nr].motor) {
466 reinit_completion(&motor_on_completion);
467 mod_timer(&motor_on_timer, jiffies + HZ/2);
470 wait_for_completion(&motor_on_completion);
474 if (on_attempts == 0) {
477 printk (KERN_ERR "motor_on failed, turning motor off\n");
478 fd_motor_off (motor_off_timer + nr);
481 printk (KERN_WARNING "DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n");
488 static void fd_motor_off(struct timer_list *timer)
490 unsigned long drive = ((unsigned long)timer -
491 (unsigned long)&motor_off_timer[0]) /
492 sizeof(motor_off_timer[0]);
495 if (!try_fdc(drive)) {
496 /* We would be blocked in an interrupt, so try again later */
497 timer->expires = jiffies + 1;
501 unit[drive].motor = 0;
507 static void floppy_off (unsigned int nr)
512 mod_timer(motor_off_timer + drive, jiffies + 3*HZ);
515 static int fd_calibrate(int drive)
522 if (!fd_motor_on (drive))
529 for (n = unit[drive].type->tracks/2; n != 0; --n) {
530 if (ciaa.pra & DSKTRACK0)
537 ms_delay(unit[drive].type->step_delay);
539 ms_delay (unit[drive].type->settle_time);
541 n = unit[drive].type->tracks + 20;
548 ms_delay(unit[drive].type->step_delay + 1);
549 if ((ciaa.pra & DSKTRACK0) == 0)
552 printk (KERN_ERR "fd%d: calibrate failed, turning motor off\n", drive);
553 fd_motor_off (motor_off_timer + drive);
554 unit[drive].track = -1;
559 unit[drive].track = 0;
560 ms_delay(unit[drive].type->settle_time);
567 static int fd_seek(int drive, int track)
573 printk("seeking drive %d to track %d\n",drive,track);
577 if (unit[drive].track == track) {
581 if (!fd_motor_on(drive)) {
585 if (unit[drive].track < 0 && !fd_calibrate(drive)) {
591 cnt = unit[drive].track/2 - track/2;
593 prb |= DSKSIDE | DSKDIREC;
601 if (track % 2 != unit[drive].track % 2)
602 ms_delay (unit[drive].type->side_time);
603 unit[drive].track = track;
615 ms_delay (unit[drive].type->step_delay);
616 } while (--cnt != 0);
617 ms_delay (unit[drive].type->settle_time);
624 static unsigned long fd_get_drive_id(int drive)
634 SELECT(SELMASK(drive));
636 DESELECT(SELMASK(drive));
640 SELECT(SELMASK(drive));
642 DESELECT(SELMASK(drive));
645 /* loop and read disk ID */
646 for (i=0; i<32; i++) {
647 SELECT(SELMASK(drive));
650 /* read and store value of DSKRDY */
652 id |= (ciaa.pra & DSKRDY) ? 0 : 1; /* cia regs are low-active! */
654 DESELECT(SELMASK(drive));
660 * RB: At least A500/A2000's df0: don't identify themselves.
661 * As every (real) Amiga has at least a 3.5" DD drive as df0:
662 * we default to that if df0: doesn't identify as a certain
665 if(drive == 0 && id == FD_NODRIVE)
668 printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx\n", (ulong)fd_def_df0);
670 /* return the ID value */
674 static irqreturn_t fd_block_done(int irq, void *dummy)
677 custom.dsklen = 0x4000;
679 if (block_flag == 2) { /* writing */
681 post_write_timer.expires = jiffies + 1; /* at least 2 ms */
682 post_write_timer_drive = selected;
683 add_timer(&post_write_timer);
687 wake_up (&wait_fd_block);
692 static void raw_read(int drive)
696 wait_event(wait_fd_block, !block_flag);
698 /* setup adkcon bits correctly */
699 custom.adkcon = ADK_MSBSYNC;
700 custom.adkcon = ADK_SETCLR|ADK_WORDSYNC|ADK_FAST;
702 custom.dsksync = MFM_SYNC;
705 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
706 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
707 custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
711 wait_event(wait_fd_block, !block_flag);
718 static int raw_write(int drive)
723 get_fdc(drive); /* corresponds to rel_fdc() in post_write() */
724 if ((ciaa.pra & DSKPROT) == 0) {
728 wait_event(wait_fd_block, !block_flag);
730 /* clear adkcon bits */
731 custom.adkcon = ADK_PRECOMP1|ADK_PRECOMP0|ADK_WORDSYNC|ADK_MSBSYNC;
732 /* set appropriate adkcon bits */
733 adk = ADK_SETCLR|ADK_FAST;
734 if ((ulong)unit[drive].track >= unit[drive].type->precomp2)
736 else if ((ulong)unit[drive].track >= unit[drive].type->precomp1)
740 custom.dsklen = DSKLEN_WRITE;
741 custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
742 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
743 custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
750 * to be called at least 2ms after the write has finished but before any
751 * other access to the hardware.
753 static void post_write (unsigned long drive)
756 printk("post_write for drive %ld\n",drive);
763 unit[drive].dirty = 0;
764 wake_up(&wait_fd_block);
766 rel_fdc(); /* corresponds to get_fdc() in raw_write */
769 static void post_write_callback(struct timer_list *timer)
771 post_write(post_write_timer_drive);
775 * The following functions are to convert the block contents into raw data
776 * written to disk and vice versa.
777 * (Add other formats here ;-))
780 static unsigned long scan_sync(unsigned long raw, unsigned long end)
782 ushort *ptr = (ushort *)raw, *endp = (ushort *)end;
784 while (ptr < endp && *ptr++ != 0x4489)
787 while (*ptr == 0x4489 && ptr < endp)
794 static inline unsigned long checksum(unsigned long *addr, int len)
796 unsigned long csum = 0;
798 len /= sizeof(*addr);
801 csum = ((csum>>1) & 0x55555555) ^ (csum & 0x55555555);
806 static unsigned long decode (unsigned long *data, unsigned long *raw,
811 /* convert length from bytes to longwords */
816 /* prepare return pointer */
820 *data++ = ((*odd++ & 0x55555555) << 1) | (*even++ & 0x55555555);
821 } while (--len != 0);
831 unsigned char labels[16];
832 unsigned long hdrchk;
833 unsigned long datachk;
836 static int amiga_read(int drive)
845 raw = (long) raw_buf;
846 end = raw + unit[drive].type->read_size;
848 for (scnt = 0;scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
849 if (!(raw = scan_sync(raw, end))) {
850 printk (KERN_INFO "can't find sync for sector %d\n", scnt);
854 raw = decode ((ulong *)&hdr.magic, (ulong *)raw, 4);
855 raw = decode ((ulong *)&hdr.labels, (ulong *)raw, 16);
856 raw = decode ((ulong *)&hdr.hdrchk, (ulong *)raw, 4);
857 raw = decode ((ulong *)&hdr.datachk, (ulong *)raw, 4);
858 csum = checksum((ulong *)&hdr,
859 (char *)&hdr.hdrchk-(char *)&hdr);
862 printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx\n",
863 hdr.magic, hdr.track, hdr.sect, hdr.ord,
864 *(ulong *)&hdr.labels[0], *(ulong *)&hdr.labels[4],
865 *(ulong *)&hdr.labels[8], *(ulong *)&hdr.labels[12],
866 hdr.hdrchk, hdr.datachk);
869 if (hdr.hdrchk != csum) {
870 printk(KERN_INFO "MFM_HEADER: %08lx,%08lx\n", hdr.hdrchk, csum);
875 if (hdr.track != unit[drive].track) {
876 printk(KERN_INFO "MFM_TRACK: %d, %d\n", hdr.track, unit[drive].track);
880 raw = decode ((ulong *)(unit[drive].trackbuf + hdr.sect*512),
882 csum = checksum((ulong *)(unit[drive].trackbuf + hdr.sect*512), 512);
884 if (hdr.datachk != csum) {
885 printk(KERN_INFO "MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx\n",
886 hdr.magic, hdr.track, hdr.sect, hdr.ord, scnt,
888 printk (KERN_INFO "data=(%lx,%lx,%lx,%lx)\n",
889 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[0],
890 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[1],
891 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[2],
892 ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[3]);
900 static void encode(unsigned long data, unsigned long *dest)
905 data2 = data ^ 0x55555555;
906 data |= ((data2 >> 1) | 0x80000000) & (data2 << 1);
908 if (*(dest - 1) & 0x00000001)
914 static void encode_block(unsigned long *dest, unsigned long *src, int len)
920 for (cnt = 0; cnt < len / 4; cnt++) {
921 data = src[cnt] >> 1;
922 encode(data, dest + to_cnt++);
926 for (cnt = 0; cnt < len / 4; cnt++) {
928 encode(data, dest + to_cnt++);
932 static unsigned long *putsec(int disk, unsigned long *raw, int cnt)
938 *raw = (raw[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
943 hdr.track = unit[disk].track;
945 hdr.ord = unit[disk].dtype->sects * unit[disk].type->sect_mult - cnt;
946 for (i = 0; i < 16; i++)
948 hdr.hdrchk = checksum((ulong *)&hdr,
949 (char *)&hdr.hdrchk-(char *)&hdr);
950 hdr.datachk = checksum((ulong *)(unit[disk].trackbuf+cnt*512), 512);
952 encode_block(raw, (ulong *)&hdr.magic, 4);
954 encode_block(raw, (ulong *)&hdr.labels, 16);
956 encode_block(raw, (ulong *)&hdr.hdrchk, 4);
958 encode_block(raw, (ulong *)&hdr.datachk, 4);
960 encode_block(raw, (ulong *)(unit[disk].trackbuf+cnt*512), 512);
966 static void amiga_write(int disk)
969 unsigned long *ptr = (unsigned long *)raw_buf;
973 for (cnt = 0; cnt < 415 * unit[disk].type->sect_mult; cnt++)
977 for (cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
978 ptr = putsec (disk, ptr, cnt);
979 *(ushort *)ptr = (ptr[-1]&1) ? 0x2AA8 : 0xAAA8;
984 unsigned char track, /* 0-80 */
988 unsigned short crc; /* on 68000 we got an alignment problem,
989 but this compiler solves it by adding silently
990 adding a pad byte so data won't fit
991 and this took about 3h to discover.... */
992 unsigned char gap1[22]; /* for longword-alignedness (0x4e) */
995 /* crc routines are borrowed from the messydos-handler */
997 /* excerpt from the messydos-device
998 ; The CRC is computed not only over the actual data, but including
999 ; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb).
1000 ; As we don't read or encode these fields into our buffers, we have to
1001 ; preload the registers containing the CRC with the values they would have
1002 ; after stepping over these fields.
1004 ; How CRCs "really" work:
1006 ; First, you should regard a bitstring as a series of coefficients of
1007 ; polynomials. We calculate with these polynomials in modulo-2
1008 ; arithmetic, in which both add and subtract are done the same as
1009 ; exclusive-or. Now, we modify our data (a very long polynomial) in
1010 ; such a way that it becomes divisible by the CCITT-standard 16-bit
1012 ; polynomial: x + x + x + 1, represented by $11021. The easiest
1013 ; way to do this would be to multiply (using proper arithmetic) our
1014 ; datablock with $11021. So we have:
1016 ; data * ($10000 + $1021) =
1017 ; data * $10000 + data * $1021
1018 ; The left part of this is simple: Just add two 0 bytes. But then
1019 ; the right part (data $1021) remains difficult and even could have
1020 ; a carry into the left part. The solution is to use a modified
1021 ; multiplication, which has a result that is not correct, but with
1022 ; a difference of any multiple of $11021. We then only need to keep
1023 ; the 16 least significant bits of the result.
1025 ; The following algorithm does this for us:
1027 ; unsigned char *data, c, crclo, crchi;
1028 ; while (not done) {
1029 ; c = *data++ + crchi;
1030 ; crchi = (@ c) >> 8 + crclo;
1034 ; Remember, + is done with EOR, the @ operator is in two tables (high
1035 ; and low byte separately), which is calculated as
1038 ; xor $1021 * (c & $0F)
1039 ; xor $1021 * (c >> 4) (* is regular multiplication)
1042 ; Anyway, the end result is the same as the remainder of the division of
1043 ; the data by $11021. I am afraid I need to study theory a bit more...
1046 my only works was to code this from manx to C....
1050 static ushort dos_crc(void * data_a3, int data_d0, int data_d1, int data_d3)
1052 static unsigned char CRCTable1[] = {
1053 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1,
1054 0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62,0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3,
1055 0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54,0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5,
1056 0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46,0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7,
1057 0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38,0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9,
1058 0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a,0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab,
1059 0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c,0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d,
1060 0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e,0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,
1061 0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1,0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60,
1062 0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,
1063 0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5,0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44,
1064 0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7,0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56,
1065 0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9,0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28,
1066 0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb,0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a,
1067 0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d,0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c,
1068 0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f,0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e
1071 static unsigned char CRCTable2[] = {
1072 0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7,0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef,
1073 0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6,0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde,
1074 0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85,0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d,
1075 0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4,0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc,
1076 0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23,0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b,
1077 0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12,0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a,
1078 0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41,0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49,
1079 0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70,0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78,
1080 0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f,0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67,
1081 0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e,0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56,
1082 0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d,0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05,
1083 0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c,0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34,
1084 0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab,0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3,
1085 0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a,0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92,
1086 0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9,0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1,
1087 0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8,0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0
1090 /* look at the asm-code - what looks in C a bit strange is almost as good as handmade */
1092 register unsigned char *CRCT1, *CRCT2, *data, c, crch, crcl;
1099 for (i=data_d3; i>=0; i--) {
1100 c = (*data++) ^ crch;
1101 crch = CRCT1[c] ^ crcl;
1104 return (crch<<8)|crcl;
1107 static inline ushort dos_hdr_crc (struct dos_header *hdr)
1109 return dos_crc(&(hdr->track), 0xb2, 0x30, 3); /* precomputed magic */
1112 static inline ushort dos_data_crc(unsigned char *data)
1114 return dos_crc(data, 0xe2, 0x95 ,511); /* precomputed magic */
1117 static inline unsigned char dos_decode_byte(ushort word)
1120 register unsigned char byte;
1121 register unsigned char *dec = mfmdecode;
1133 static unsigned long dos_decode(unsigned char *data, unsigned short *raw, int len)
1137 for (i = 0; i < len; i++)
1138 *data++=dos_decode_byte(*raw++);
1139 return ((ulong)raw);
1143 static void dbg(unsigned long ptr)
1145 printk("raw data @%08lx: %08lx, %08lx ,%08lx, %08lx\n", ptr,
1146 ((ulong *)ptr)[0], ((ulong *)ptr)[1],
1147 ((ulong *)ptr)[2], ((ulong *)ptr)[3]);
1151 static int dos_read(int drive)
1156 unsigned short crc,data_crc[2];
1157 struct dos_header hdr;
1160 raw = (long) raw_buf;
1161 end = raw + unit[drive].type->read_size;
1163 for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
1164 do { /* search for the right sync of each sec-hdr */
1165 if (!(raw = scan_sync (raw, end))) {
1166 printk(KERN_INFO "dos_read: no hdr sync on "
1167 "track %d, unit %d for sector %d\n",
1168 unit[drive].track,drive,scnt);
1174 } while (*((ushort *)raw)!=0x5554); /* loop usually only once done */
1175 raw+=2; /* skip over headermark */
1176 raw = dos_decode((unsigned char *)&hdr,(ushort *) raw,8);
1177 crc = dos_hdr_crc(&hdr);
1180 printk("(%3d,%d,%2d,%d) %x\n", hdr.track, hdr.side,
1181 hdr.sec, hdr.len_desc, hdr.crc);
1184 if (crc != hdr.crc) {
1185 printk(KERN_INFO "dos_read: MFM_HEADER %04x,%04x\n",
1189 if (hdr.track != unit[drive].track/unit[drive].type->heads) {
1190 printk(KERN_INFO "dos_read: MFM_TRACK %d, %d\n",
1192 unit[drive].track/unit[drive].type->heads);
1196 if (hdr.side != unit[drive].track%unit[drive].type->heads) {
1197 printk(KERN_INFO "dos_read: MFM_SIDE %d, %d\n",
1199 unit[drive].track%unit[drive].type->heads);
1203 if (hdr.len_desc != 2) {
1204 printk(KERN_INFO "dos_read: unknown sector len "
1205 "descriptor %d\n", hdr.len_desc);
1209 printk("hdr accepted\n");
1211 if (!(raw = scan_sync (raw, end))) {
1212 printk(KERN_INFO "dos_read: no data sync on track "
1213 "%d, unit %d for sector%d, disk sector %d\n",
1214 unit[drive].track, drive, scnt, hdr.sec);
1221 if (*((ushort *)raw)!=0x5545) {
1222 printk(KERN_INFO "dos_read: no data mark after "
1223 "sync (%d,%d,%d,%d) sc=%d\n",
1224 hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt);
1228 raw+=2; /* skip data mark (included in checksum) */
1229 raw = dos_decode((unsigned char *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512);
1230 raw = dos_decode((unsigned char *)data_crc,(ushort *) raw,4);
1231 crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512);
1233 if (crc != data_crc[0]) {
1234 printk(KERN_INFO "dos_read: MFM_DATA (%d,%d,%d,%d) "
1235 "sc=%d, %x %x\n", hdr.track, hdr.side,
1236 hdr.sec, hdr.len_desc, scnt,data_crc[0], crc);
1237 printk(KERN_INFO "data=(%lx,%lx,%lx,%lx,...)\n",
1238 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[0],
1239 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[1],
1240 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[2],
1241 ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[3]);
1248 static inline ushort dos_encode_byte(unsigned char byte)
1250 register unsigned char *enc, b2, b1;
1251 register ushort word;
1257 word=enc[b2] <<8 | enc [b1];
1258 return (word|((word&(256|64)) ? 0: 128));
1261 static void dos_encode_block(ushort *dest, unsigned char *src, int len)
1265 for (i = 0; i < len; i++) {
1266 *dest=dos_encode_byte(*src++);
1267 *dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000;
1272 static unsigned long *ms_putsec(int drive, unsigned long *raw, int cnt)
1274 static struct dos_header hdr={0,0,0,2,0,
1275 {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
1277 static ushort crc[2]={0,0x4e4e};
1281 /* the MFM word before is always 9254 */
1284 /* 3 sync + 1 headermark */
1288 /* fill in the variable parts of the header */
1289 hdr.track=unit[drive].track/unit[drive].type->heads;
1290 hdr.side=unit[drive].track%unit[drive].type->heads;
1292 hdr.crc=dos_hdr_crc(&hdr);
1294 /* header (without "magic") and id gap 2*/
1295 dos_encode_block((ushort *)raw,(unsigned char *) &hdr.track,28);
1302 /* 3 syncs and 1 datamark */
1307 dos_encode_block((ushort *)raw,
1308 (unsigned char *)unit[drive].trackbuf+cnt*512,512);
1311 /*data crc + jd's special gap (long words :-/) */
1312 crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512);
1313 dos_encode_block((ushort *) raw,(unsigned char *)crc,4);
1320 return raw; /* wrote 652 MFM words */
1323 static void dos_write(int disk)
1326 unsigned long raw = (unsigned long) raw_buf;
1327 unsigned long *ptr=(unsigned long *)raw;
1330 /* really gap4 + indexgap , but we write it first and round it up */
1331 for (cnt=0;cnt<425;cnt++)
1334 /* the following is just guessed */
1335 if (unit[disk].type->sect_mult==2) /* check for HD-Disks */
1336 for(cnt=0;cnt<473;cnt++)
1339 /* now the index marks...*/
1340 for (cnt=0;cnt<20;cnt++)
1342 for (cnt=0;cnt<6;cnt++)
1346 for (cnt=0;cnt<20;cnt++)
1350 for(cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
1351 ptr=ms_putsec(disk,ptr,cnt);
1353 *(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */
1357 * Here comes the high level stuff (i.e. the filesystem interface)
1358 * and helper functions.
1359 * Normally this should be the only part that has to be adapted to
1360 * different kernel versions.
1363 /* FIXME: this assumes the drive is still spinning -
1364 * which is only true if we complete writing a track within three seconds
1366 static void flush_track_callback(struct timer_list *timer)
1368 unsigned long nr = ((unsigned long)timer -
1369 (unsigned long)&flush_track_timer[0]) /
1370 sizeof(flush_track_timer[0]);
1375 /* we might block in an interrupt, so try again later */
1376 flush_track_timer[nr].expires = jiffies + 1;
1377 add_timer(flush_track_timer + nr);
1381 (*unit[nr].dtype->write_fkt)(nr);
1382 if (!raw_write(nr)) {
1383 printk (KERN_NOTICE "floppy disk write protected\n");
1390 static int non_int_flush_track (unsigned long nr)
1392 unsigned long flags;
1396 del_timer(&post_write_timer);
1398 if (!fd_motor_on(nr)) {
1403 local_irq_save(flags);
1404 if (writepending != 2) {
1405 local_irq_restore(flags);
1406 (*unit[nr].dtype->write_fkt)(nr);
1407 if (!raw_write(nr)) {
1408 printk (KERN_NOTICE "floppy disk write protected "
1413 wait_event(wait_fd_block, block_flag != 2);
1416 local_irq_restore(flags);
1417 ms_delay(2); /* 2 ms post_write delay */
1424 static int get_track(int drive, int track)
1429 if (unit[drive].track == track)
1432 if (!fd_motor_on(drive)) {
1437 if (unit[drive].dirty == 1) {
1438 del_timer (flush_track_timer + drive);
1439 non_int_flush_track (drive);
1442 while (errcnt < MAX_ERRORS) {
1443 if (!fd_seek(drive, track))
1446 error = (*unit[drive].dtype->read_fkt)(drive);
1451 /* Read Error Handling: recalibrate and try again */
1452 unit[drive].track = -1;
1459 static blk_status_t amiflop_rw_cur_segment(struct amiga_floppy_struct *floppy,
1462 int drive = floppy - unit;
1463 unsigned int cnt, block, track, sector;
1466 for (cnt = 0; cnt < blk_rq_cur_sectors(rq); cnt++) {
1468 printk("fd: sector %ld + %d requested for %s\n",
1469 blk_rq_pos(rq), cnt,
1470 (rq_data_dir(rq) == READ) ? "read" : "write");
1472 block = blk_rq_pos(rq) + cnt;
1473 track = block / (floppy->dtype->sects * floppy->type->sect_mult);
1474 sector = block % (floppy->dtype->sects * floppy->type->sect_mult);
1475 data = bio_data(rq->bio) + 512 * cnt;
1477 printk("access to track %d, sector %d, with buffer at "
1478 "0x%08lx\n", track, sector, data);
1481 if (get_track(drive, track) == -1)
1482 return BLK_STS_IOERR;
1484 if (rq_data_dir(rq) == READ) {
1485 memcpy(data, floppy->trackbuf + sector * 512, 512);
1487 memcpy(floppy->trackbuf + sector * 512, data, 512);
1489 /* keep the drive spinning while writes are scheduled */
1490 if (!fd_motor_on(drive))
1491 return BLK_STS_IOERR;
1493 * setup a callback to write the track buffer
1494 * after a short (1 tick) delay.
1497 /* reset the timer */
1498 mod_timer (flush_track_timer + drive, jiffies + 1);
1505 static blk_status_t amiflop_queue_rq(struct blk_mq_hw_ctx *hctx,
1506 const struct blk_mq_queue_data *bd)
1508 struct request *rq = bd->rq;
1509 struct amiga_floppy_struct *floppy = rq->rq_disk->private_data;
1512 if (!spin_trylock_irq(&amiflop_lock))
1513 return BLK_STS_DEV_RESOURCE;
1515 blk_mq_start_request(rq);
1518 err = amiflop_rw_cur_segment(floppy, rq);
1519 } while (blk_update_request(rq, err, blk_rq_cur_bytes(rq)));
1520 blk_mq_end_request(rq, err);
1522 spin_unlock_irq(&amiflop_lock);
1526 static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1528 int drive = MINOR(bdev->bd_dev) & 3;
1530 geo->heads = unit[drive].type->heads;
1531 geo->sectors = unit[drive].dtype->sects * unit[drive].type->sect_mult;
1532 geo->cylinders = unit[drive].type->tracks;
1536 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1537 unsigned int cmd, unsigned long param)
1539 struct amiga_floppy_struct *p = bdev->bd_disk->private_data;
1540 int drive = p - unit;
1541 static struct floppy_struct getprm;
1542 void __user *argp = (void __user *)param;
1547 if (fd_ref[drive] > 1) {
1552 if (fd_motor_on(drive) == 0) {
1556 if (fd_calibrate(drive) == 0) {
1564 if (param < p->type->tracks * p->type->heads)
1567 if (fd_seek(drive,param) != 0){
1568 memset(p->trackbuf, FD_FILL_BYTE,
1569 p->dtype->sects * p->type->sect_mult * 512);
1570 non_int_flush_track(drive);
1580 invalidate_bdev(bdev);
1583 memset((void *)&getprm, 0, sizeof (getprm));
1584 getprm.track=p->type->tracks;
1585 getprm.head=p->type->heads;
1586 getprm.sect=p->dtype->sects * p->type->sect_mult;
1587 getprm.size=p->blocks;
1588 if (copy_to_user(argp, &getprm, sizeof(struct floppy_struct)))
1594 case FDFLUSH: /* unconditionally, even if not needed */
1595 del_timer (flush_track_timer + drive);
1596 non_int_flush_track(drive);
1599 case IOCTL_RAW_TRACK:
1600 if (copy_to_user(argp, raw_buf, p->type->read_size))
1603 return p->type->read_size;
1611 static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1612 unsigned int cmd, unsigned long param)
1616 mutex_lock(&amiflop_mutex);
1617 ret = fd_locked_ioctl(bdev, mode, cmd, param);
1618 mutex_unlock(&amiflop_mutex);
1623 static void fd_probe(int dev)
1630 code = fd_get_drive_id(drive);
1632 /* get drive type */
1633 for (type = 0; type < num_dr_types; type++)
1634 if (drive_types[type].code == code)
1637 if (type >= num_dr_types) {
1638 printk(KERN_WARNING "fd_probe: unsupported drive type "
1639 "%08lx found\n", code);
1640 unit[drive].type = &drive_types[num_dr_types-1]; /* FD_NODRIVE */
1644 unit[drive].type = drive_types + type;
1645 unit[drive].track = -1;
1647 unit[drive].disk = -1;
1648 unit[drive].motor = 0;
1649 unit[drive].busy = 0;
1650 unit[drive].status = -1;
1654 * floppy_open check for aliasing (/dev/fd0 can be the same as
1655 * /dev/PS0 etc), and disallows simultaneous access to the same
1656 * drive with different device numbers.
1658 static int floppy_open(struct block_device *bdev, fmode_t mode)
1660 int drive = MINOR(bdev->bd_dev) & 3;
1661 int system = (MINOR(bdev->bd_dev) & 4) >> 2;
1663 unsigned long flags;
1665 mutex_lock(&amiflop_mutex);
1666 old_dev = fd_device[drive];
1668 if (fd_ref[drive] && old_dev != system) {
1669 mutex_unlock(&amiflop_mutex);
1673 if (unit[drive].type->code == FD_NODRIVE) {
1674 mutex_unlock(&amiflop_mutex);
1678 if (mode & (FMODE_READ|FMODE_WRITE)) {
1679 bdev_check_media_change(bdev);
1680 if (mode & FMODE_WRITE) {
1685 wrprot = !(ciaa.pra & DSKPROT);
1686 fd_deselect (drive);
1690 mutex_unlock(&amiflop_mutex);
1696 local_irq_save(flags);
1698 fd_device[drive] = system;
1699 local_irq_restore(flags);
1701 unit[drive].dtype=&data_types[system];
1702 unit[drive].blocks=unit[drive].type->heads*unit[drive].type->tracks*
1703 data_types[system].sects*unit[drive].type->sect_mult;
1704 set_capacity(unit[drive].gendisk[system], unit[drive].blocks);
1706 printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive,
1707 unit[drive].type->name, data_types[system].name);
1709 mutex_unlock(&amiflop_mutex);
1713 static void floppy_release(struct gendisk *disk, fmode_t mode)
1715 struct amiga_floppy_struct *p = disk->private_data;
1716 int drive = p - unit;
1718 mutex_lock(&amiflop_mutex);
1719 if (unit[drive].dirty == 1) {
1720 del_timer (flush_track_timer + drive);
1721 non_int_flush_track (drive);
1724 if (!fd_ref[drive]--) {
1725 printk(KERN_CRIT "floppy_release with fd_ref == 0");
1731 mutex_unlock(&amiflop_mutex);
1735 * check_events is never called from an interrupt, so we can relax a bit
1736 * here, sleep etc. Note that floppy-on tries to set current_DOR to point
1737 * to the desired drive, but it will probably not survive the sleep if
1738 * several floppies are used at the same time: thus the loop.
1740 static unsigned amiga_check_events(struct gendisk *disk, unsigned int clearing)
1742 struct amiga_floppy_struct *p = disk->private_data;
1743 int drive = p - unit;
1745 static int first_time = 1;
1748 changed = first_time--;
1752 changed = !(ciaa.pra & DSKCHANGE);
1753 fd_deselect (drive);
1761 writepending = 0; /* if this was true before, too bad! */
1763 return DISK_EVENT_MEDIA_CHANGE;
1768 static const struct block_device_operations floppy_fops = {
1769 .owner = THIS_MODULE,
1770 .open = floppy_open,
1771 .release = floppy_release,
1773 .getgeo = fd_getgeo,
1774 .check_events = amiga_check_events,
1777 static const struct blk_mq_ops amiflop_mq_ops = {
1778 .queue_rq = amiflop_queue_rq,
1781 static int fd_alloc_disk(int drive, int system)
1783 struct gendisk *disk;
1785 disk = blk_mq_alloc_disk(&unit[drive].tag_set, NULL);
1787 return PTR_ERR(disk);
1789 disk->major = FLOPPY_MAJOR;
1790 disk->first_minor = drive + system;
1792 disk->fops = &floppy_fops;
1793 disk->events = DISK_EVENT_MEDIA_CHANGE;
1795 sprintf(disk->disk_name, "fd%d_msdos", drive);
1797 sprintf(disk->disk_name, "fd%d", drive);
1798 disk->private_data = &unit[drive];
1799 set_capacity(disk, 880 * 2);
1801 unit[drive].gendisk[system] = disk;
1806 static int fd_alloc_drive(int drive)
1808 unit[drive].trackbuf = kmalloc(FLOPPY_MAX_SECTORS * 512, GFP_KERNEL);
1809 if (!unit[drive].trackbuf)
1812 memset(&unit[drive].tag_set, 0, sizeof(unit[drive].tag_set));
1813 unit[drive].tag_set.ops = &amiflop_mq_ops;
1814 unit[drive].tag_set.nr_hw_queues = 1;
1815 unit[drive].tag_set.nr_maps = 1;
1816 unit[drive].tag_set.queue_depth = 2;
1817 unit[drive].tag_set.numa_node = NUMA_NO_NODE;
1818 unit[drive].tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1819 if (blk_mq_alloc_tag_set(&unit[drive].tag_set))
1820 goto out_cleanup_trackbuf;
1822 pr_cont(" fd%d", drive);
1824 if (fd_alloc_disk(drive, 0) || fd_alloc_disk(drive, 1))
1825 goto out_cleanup_tagset;
1829 blk_mq_free_tag_set(&unit[drive].tag_set);
1830 out_cleanup_trackbuf:
1831 kfree(unit[drive].trackbuf);
1833 unit[drive].type->code = FD_NODRIVE;
1837 static int __init fd_probe_drives(void)
1839 int drive,drives,nomem;
1841 pr_info("FD: probing units\nfound");
1844 for(drive=0;drive<FD_MAX_UNITS;drive++) {
1846 if (unit[drive].type->code == FD_NODRIVE)
1849 if (fd_alloc_drive(drive) < 0) {
1850 pr_cont(" no mem for fd%d", drive);
1856 if ((drives > 0) || (nomem == 0)) {
1858 pr_cont(" no drives");
1866 static int __init amiga_floppy_probe(struct platform_device *pdev)
1870 if (register_blkdev(FLOPPY_MAJOR,"fd"))
1874 raw_buf = amiga_chip_alloc(RAW_BUF_SIZE, "Floppy");
1876 printk("fd: cannot get chip mem buffer\n");
1881 if (request_irq(IRQ_AMIGA_DSKBLK, fd_block_done, 0, "floppy_dma", NULL)) {
1882 printk("fd: cannot get irq for dma\n");
1886 if (request_irq(IRQ_AMIGA_CIAA_TB, ms_isr, 0, "floppy_timer", NULL)) {
1887 printk("fd: cannot get irq for timer\n");
1892 if (fd_probe_drives() < 1) /* No usable drives */
1895 /* initialize variables */
1896 timer_setup(&motor_on_timer, motor_on_callback, 0);
1897 motor_on_timer.expires = 0;
1898 for (i = 0; i < FD_MAX_UNITS; i++) {
1899 timer_setup(&motor_off_timer[i], fd_motor_off, 0);
1900 motor_off_timer[i].expires = 0;
1901 timer_setup(&flush_track_timer[i], flush_track_callback, 0);
1902 flush_track_timer[i].expires = 0;
1907 timer_setup(&post_write_timer, post_write_callback, 0);
1908 post_write_timer.expires = 0;
1910 for (i = 0; i < 128; i++)
1912 for (i = 0; i < 16; i++)
1913 mfmdecode[mfmencode[i]]=i;
1915 /* make sure that disk DMA is enabled */
1916 custom.dmacon = DMAF_SETCLR | DMAF_DISK;
1919 ciaa.crb = 8; /* one-shot, stop */
1923 free_irq(IRQ_AMIGA_CIAA_TB, NULL);
1925 free_irq(IRQ_AMIGA_DSKBLK, NULL);
1927 amiga_chip_free(raw_buf);
1929 unregister_blkdev(FLOPPY_MAJOR,"fd");
1933 static struct platform_driver amiga_floppy_driver = {
1935 .name = "amiga-floppy",
1939 static int __init amiga_floppy_init(void)
1941 return platform_driver_probe(&amiga_floppy_driver, amiga_floppy_probe);
1944 module_init(amiga_floppy_init);
1947 static int __init amiga_floppy_setup (char *str)
1952 if (!get_option(&str, &n))
1954 printk (KERN_INFO "amiflop: Setting default df0 to %x\n", n);
1959 __setup("floppy=", amiga_floppy_setup);
1962 MODULE_ALIAS("platform:amiga-floppy");