File: /usr/src/linux/drivers/block/swim3.c

1     /*
2      * Driver for the SWIM3 (Super Woz Integrated Machine 3)
3      * floppy controller found on Power Macintoshes.
4      *
5      * Copyright (C) 1996 Paul Mackerras.
6      *
7      * This program is free software; you can redistribute it and/or
8      * modify it under the terms of the GNU General Public License
9      * as published by the Free Software Foundation; either version
10      * 2 of the License, or (at your option) any later version.
11      */
12     
13     /*
14      * TODO:
15      * handle 2 drives
16      * handle GCR disks
17      */
18     
19     #include <linux/config.h>
20     #include <linux/stddef.h>
21     #include <linux/kernel.h>
22     #include <linux/sched.h>
23     #include <linux/timer.h>
24     #include <linux/delay.h>
25     #include <linux/fd.h>
26     #include <linux/ioctl.h>
27     #include <asm/io.h>
28     #include <asm/dbdma.h>
29     #include <asm/prom.h>
30     #include <asm/uaccess.h>
31     #include <asm/mediabay.h>
32     #include <asm/feature.h>
33     
34     #define MAJOR_NR	FLOPPY_MAJOR
35     #include <linux/blk.h>
36     #include <linux/devfs_fs_kernel.h>
37     
38     static int floppy_blocksizes[2] = {512,512};
39     static int floppy_sizes[2] = {2880,2880};
40     
41     #define MAX_FLOPPIES	2
42     
43     enum swim_state {
44     	idle,
45     	locating,
46     	seeking,
47     	settling,
48     	do_transfer,
49     	jogging,
50     	available,
51     	revalidating,
52     	ejecting
53     };
54     
55     #define REG(x)	unsigned char x; char x ## _pad[15];
56     
57     /*
58      * The names for these registers mostly represent speculation on my part.
59      * It will be interesting to see how close they are to the names Apple uses.
60      */
61     struct swim3 {
62     	REG(data);
63     	REG(timer);		/* counts down at 1MHz */
64     	REG(error);
65     	REG(mode);
66     	REG(select);		/* controls CA0, CA1, CA2 and LSTRB signals */
67     	REG(setup);
68     	REG(control);		/* writing bits clears them */
69     	REG(status);		/* writing bits sets them in control */
70     	REG(intr);
71     	REG(nseek);		/* # tracks to seek */
72     	REG(ctrack);		/* current track number */
73     	REG(csect);		/* current sector number */
74     	REG(gap3);		/* size of gap 3 in track format */
75     	REG(sector);		/* sector # to read or write */
76     	REG(nsect);		/* # sectors to read or write */
77     	REG(intr_enable);
78     };
79     
80     #define control_bic	control
81     #define control_bis	status
82     
83     /* Bits in select register */
84     #define CA_MASK		7
85     #define LSTRB		8
86     
87     /* Bits in control register */
88     #define DO_SEEK		0x80
89     #define FORMAT		0x40
90     #define SELECT		0x20
91     #define WRITE_SECTORS	0x10
92     #define DO_ACTION	0x08
93     #define DRIVE2_ENABLE	0x04
94     #define DRIVE_ENABLE	0x02
95     #define INTR_ENABLE	0x01
96     
97     /* Bits in status register */
98     #define FIFO_1BYTE	0x80
99     #define FIFO_2BYTE	0x40
100     #define ERROR		0x20
101     #define DATA		0x08
102     #define RDDATA		0x04
103     #define INTR_PENDING	0x02
104     #define MARK_BYTE	0x01
105     
106     /* Bits in intr and intr_enable registers */
107     #define ERROR_INTR	0x20
108     #define DATA_CHANGED	0x10
109     #define TRANSFER_DONE	0x08
110     #define SEEN_SECTOR	0x04
111     #define SEEK_DONE	0x02
112     #define TIMER_DONE	0x01
113     
114     /* Bits in error register */
115     #define ERR_DATA_CRC	0x80
116     #define ERR_ADDR_CRC	0x40
117     #define ERR_OVERRUN	0x04
118     #define ERR_UNDERRUN	0x01
119     
120     /* Bits in setup register */
121     #define S_SW_RESET	0x80
122     #define S_GCR_WRITE	0x40
123     #define S_IBM_DRIVE	0x20
124     #define S_TEST_MODE	0x10
125     #define S_FCLK_DIV2	0x08
126     #define S_GCR		0x04
127     #define S_COPY_PROT	0x02
128     #define S_INV_WDATA	0x01
129     
130     /* Select values for swim3_action */
131     #define SEEK_POSITIVE	0
132     #define SEEK_NEGATIVE	4
133     #define STEP		1
134     #define MOTOR_ON	2
135     #define MOTOR_OFF	6
136     #define INDEX		3
137     #define EJECT		7
138     #define SETMFM		9
139     #define SETGCR		13
140     
141     /* Select values for swim3_select and swim3_readbit */
142     #define STEP_DIR	0
143     #define STEPPING	1
144     #define MOTOR_ON	2
145     #define RELAX		3	/* also eject in progress */
146     #define READ_DATA_0	4
147     #define TWOMEG_DRIVE	5
148     #define SINGLE_SIDED	6
149     #define DRIVE_PRESENT	7
150     #define DISK_IN		8
151     #define WRITE_PROT	9
152     #define TRACK_ZERO	10
153     #define TACHO		11
154     #define READ_DATA_1	12
155     #define MFM_MODE	13
156     #define SEEK_COMPLETE	14
157     #define ONEMEG_MEDIA	15
158     
159     /* Definitions of values used in writing and formatting */
160     #define DATA_ESCAPE	0x99
161     #define GCR_SYNC_EXC	0x3f
162     #define GCR_SYNC_CONV	0x80
163     #define GCR_FIRST_MARK	0xd5
164     #define GCR_SECOND_MARK	0xaa
165     #define GCR_ADDR_MARK	"\xd5\xaa\x00"
166     #define GCR_DATA_MARK	"\xd5\xaa\x0b"
167     #define GCR_SLIP_BYTE	"\x27\xaa"
168     #define GCR_SELF_SYNC	"\x3f\xbf\x1e\x34\x3c\x3f"
169     
170     #define DATA_99		"\x99\x99"
171     #define MFM_ADDR_MARK	"\x99\xa1\x99\xa1\x99\xa1\x99\xfe"
172     #define MFM_INDEX_MARK	"\x99\xc2\x99\xc2\x99\xc2\x99\xfc"
173     #define MFM_GAP_LEN	12
174     
175     struct floppy_state {
176     	enum swim_state	state;
177     	volatile struct swim3 *swim3;	/* hardware registers */
178     	struct dbdma_regs *dma;	/* DMA controller registers */
179     	int	swim3_intr;	/* interrupt number for SWIM3 */
180     	int	dma_intr;	/* interrupt number for DMA channel */
181     	int	cur_cyl;	/* cylinder head is on, or -1 */
182     	int	cur_sector;	/* last sector we saw go past */
183     	int	req_cyl;	/* the cylinder for the current r/w request */
184     	int	head;		/* head number ditto */
185     	int	req_sector;	/* sector number ditto */
186     	int	scount;		/* # sectors we're transferring at present */
187     	int	retries;
188     	int	secpercyl;	/* disk geometry information */
189     	int	secpertrack;
190     	int	total_secs;
191     	int	write_prot;	/* 1 if write-protected, 0 if not, -1 dunno */
192     	struct dbdma_cmd *dma_cmd;
193     	int	ref_count;
194     	int	expect_cyl;
195     	struct timer_list timeout;
196     	int	timeout_pending;
197     	int	ejected;
198     	wait_queue_head_t wait;
199     	int	wanted;
200     	struct device_node*	media_bay; /* NULL when not in bay */
201     	char	dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)];
202     };
203     
204     static struct floppy_state floppy_states[MAX_FLOPPIES];
205     static int floppy_count = 0;
206     
207     static unsigned short write_preamble[] = {
208     	0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e,	/* gap field */
209     	0, 0, 0, 0, 0, 0,			/* sync field */
210     	0x99a1, 0x99a1, 0x99a1, 0x99fb,		/* data address mark */
211     	0x990f					/* no escape for 512 bytes */
212     };
213     
214     static unsigned short write_postamble[] = {
215     	0x9904,					/* insert CRC */
216     	0x4e4e, 0x4e4e,
217     	0x9908,					/* stop writing */
218     	0, 0, 0, 0, 0, 0
219     };
220     
221     static void swim3_select(struct floppy_state *fs, int sel);
222     static void swim3_action(struct floppy_state *fs, int action);
223     static int swim3_readbit(struct floppy_state *fs, int bit);
224     static void do_fd_request(request_queue_t * q);
225     static void start_request(struct floppy_state *fs);
226     static void set_timeout(struct floppy_state *fs, int nticks,
227     			void (*proc)(unsigned long));
228     static void scan_track(struct floppy_state *fs);
229     static void seek_track(struct floppy_state *fs, int n);
230     static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count);
231     static void setup_transfer(struct floppy_state *fs);
232     static void act(struct floppy_state *fs);
233     static void scan_timeout(unsigned long data);
234     static void seek_timeout(unsigned long data);
235     static void xfer_timeout(unsigned long data);
236     static void swim3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
237     /*static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs);*/
238     static int grab_drive(struct floppy_state *fs, enum swim_state state,
239     		      int interruptible);
240     static void release_drive(struct floppy_state *fs);
241     static int fd_eject(struct floppy_state *fs);
242     static int floppy_ioctl(struct inode *inode, struct file *filp,
243     			unsigned int cmd, unsigned long param);
244     static int floppy_open(struct inode *inode, struct file *filp);
245     static int floppy_release(struct inode *inode, struct file *filp);
246     static int floppy_check_change(kdev_t dev);
247     static int floppy_revalidate(kdev_t dev);
248     static int swim3_add_device(struct device_node *swims);
249     int swim3_init(void);
250     
251     #ifndef CONFIG_PMAC_PBOOK
252     #define check_media_bay(which, what)	1
253     #endif
254     
255     static void swim3_select(struct floppy_state *fs, int sel)
256     {
257     	volatile struct swim3 *sw = fs->swim3;
258     
259     	out_8(&sw->select, RELAX);
260     	if (sel & 8)
261     		out_8(&sw->control_bis, SELECT);
262     	else
263     		out_8(&sw->control_bic, SELECT);
264     	out_8(&sw->select, sel & CA_MASK);
265     }
266     
267     static void swim3_action(struct floppy_state *fs, int action)
268     {
269     	volatile struct swim3 *sw = fs->swim3;
270     
271     	swim3_select(fs, action);
272     	udelay(1);
273     	out_8(&sw->select, sw->select | LSTRB);
274     	udelay(2);
275     	out_8(&sw->select, sw->select & ~LSTRB);
276     	udelay(1);
277     	out_8(&sw->select, RELAX);
278     }
279     
280     static int swim3_readbit(struct floppy_state *fs, int bit)
281     {
282     	volatile struct swim3 *sw = fs->swim3;
283     	int stat;
284     
285     	swim3_select(fs, bit);
286     	udelay(10);
287     	stat = in_8(&sw->status);
288     	out_8(&sw->select, RELAX);
289     	return (stat & DATA) == 0;
290     }
291     
292     static void do_fd_request(request_queue_t * q)
293     {
294     	int i;
295     	for(i=0;i<floppy_count;i++)
296     	{
297     		if (floppy_states[i].media_bay &&
298     			check_media_bay(floppy_states[i].media_bay, MB_FD))
299     			continue;
300     		start_request(&floppy_states[i]);
301     	}
302     	sti();
303     }
304     
305     static void start_request(struct floppy_state *fs)
306     {
307     	unsigned long x;
308     
309     	if (fs->state == idle && fs->wanted) {
310     		fs->state = available;
311     		wake_up(&fs->wait);
312     		return;
313     	}
314     	while (!QUEUE_EMPTY && fs->state == idle) {
315     		if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
316     			panic(DEVICE_NAME ": request list destroyed");
317     		if (CURRENT->bh && !buffer_locked(CURRENT->bh))
318     			panic(DEVICE_NAME ": block not locked");
319     #if 0
320     		printk("do_fd_req: dev=%x cmd=%d sec=%ld nr_sec=%ld buf=%p\n",
321     		       kdev_t_to_nr(CURRENT->rq_dev), CURRENT->cmd,
322     		       CURRENT->sector, CURRENT->nr_sectors, CURRENT->buffer);
323     		printk("           rq_status=%d errors=%d current_nr_sectors=%ld\n",
324     		       CURRENT->rq_status, CURRENT->errors, CURRENT->current_nr_sectors);
325     #endif
326     
327     		if (CURRENT->sector < 0 || CURRENT->sector >= fs->total_secs) {
328     			end_request(0);
329     			continue;
330     		}
331     		if (CURRENT->current_nr_sectors == 0) {
332     			end_request(1);
333     			continue;
334     		}
335     		if (fs->ejected) {
336     			end_request(0);
337     			continue;
338     		}
339     
340     		if (CURRENT->cmd == WRITE) {
341     			if (fs->write_prot < 0)
342     				fs->write_prot = swim3_readbit(fs, WRITE_PROT);
343     			if (fs->write_prot) {
344     				end_request(0);
345     				continue;
346     			}
347     		}
348     
349     		fs->req_cyl = CURRENT->sector / fs->secpercyl;
350     		x = CURRENT->sector % fs->secpercyl;
351     		fs->head = x / fs->secpertrack;
352     		fs->req_sector = x % fs->secpertrack + 1;
353     		fs->state = do_transfer;
354     		fs->retries = 0;
355     
356     		act(fs);
357     	}
358     }
359     
360     static void set_timeout(struct floppy_state *fs, int nticks,
361     			void (*proc)(unsigned long))
362     {
363     	unsigned long flags;
364     
365     	save_flags(flags); cli();
366     	if (fs->timeout_pending)
367     		del_timer(&fs->timeout);
368     	fs->timeout.expires = jiffies + nticks;
369     	fs->timeout.function = proc;
370     	fs->timeout.data = (unsigned long) fs;
371     	add_timer(&fs->timeout);
372     	fs->timeout_pending = 1;
373     	restore_flags(flags);
374     }
375     
376     static inline void scan_track(struct floppy_state *fs)
377     {
378     	volatile struct swim3 *sw = fs->swim3;
379     	int xx;
380     
381     	swim3_select(fs, READ_DATA_0);
382     	xx = sw->intr;		/* clear SEEN_SECTOR bit */
383     	out_8(&sw->control_bis, DO_ACTION);
384     	/* enable intr when track found */
385     	out_8(&sw->intr_enable, ERROR_INTR | SEEN_SECTOR);
386     	set_timeout(fs, HZ, scan_timeout);	/* enable timeout */
387     }
388     
389     static inline void seek_track(struct floppy_state *fs, int n)
390     {
391     	volatile struct swim3 *sw = fs->swim3;
392     
393     	if (n >= 0) {
394     		swim3_action(fs, SEEK_POSITIVE);
395     		sw->nseek = n;
396     	} else {
397     		swim3_action(fs, SEEK_NEGATIVE);
398     		sw->nseek = -n;
399     	}
400     	fs->expect_cyl = (fs->cur_cyl > 0)? fs->cur_cyl + n: -1;
401     	swim3_select(fs, STEP);
402     	out_8(&sw->control_bis, DO_SEEK);
403     	/* enable intr when seek finished */
404     	out_8(&sw->intr_enable, ERROR_INTR | SEEK_DONE);
405     	set_timeout(fs, HZ/2, seek_timeout);	/* enable timeout */
406     }
407     
408     static inline void init_dma(struct dbdma_cmd *cp, int cmd,
409     			    void *buf, int count)
410     {
411     	st_le16(&cp->req_count, count);
412     	st_le16(&cp->command, cmd);
413     	st_le32(&cp->phy_addr, virt_to_bus(buf));
414     	cp->xfer_status = 0;
415     }
416     
417     static inline void setup_transfer(struct floppy_state *fs)
418     {
419     	int n;
420     	volatile struct swim3 *sw = fs->swim3;
421     	struct dbdma_cmd *cp = fs->dma_cmd;
422     	struct dbdma_regs *dr = fs->dma;
423     
424     	if (CURRENT->current_nr_sectors <= 0) {
425     		printk(KERN_ERR "swim3: transfer 0 sectors?\n");
426     		return;
427     	}
428     	if (CURRENT->cmd == WRITE)
429     		n = 1;
430     	else {
431     		n = fs->secpertrack - fs->req_sector + 1;
432     		if (n > CURRENT->current_nr_sectors)
433     			n = CURRENT->current_nr_sectors;
434     	}
435     	fs->scount = n;
436     	swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
437     	out_8(&sw->sector, fs->req_sector);
438     	out_8(&sw->nsect, n);
439     	out_8(&sw->gap3, 0);
440     	st_le32(&dr->cmdptr, virt_to_bus(cp));
441     	if (CURRENT->cmd == WRITE) {
442     		/* Set up 3 dma commands: write preamble, data, postamble */
443     		init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));
444     		++cp;
445     		init_dma(cp, OUTPUT_MORE, CURRENT->buffer, 512);
446     		++cp;
447     		init_dma(cp, OUTPUT_MORE, write_postamble, sizeof(write_postamble));
448     	} else {
449     		init_dma(cp, INPUT_MORE, CURRENT->buffer, n * 512);
450     	}
451     	++cp;
452     	out_le16(&cp->command, DBDMA_STOP);
453     	out_le32(&dr->control, (RUN << 16) | RUN);
454     	out_8(&sw->control_bis,
455     	      (CURRENT->cmd == WRITE? WRITE_SECTORS: 0) | DO_ACTION);
456     	/* enable intr when transfer complete */
457     	out_8(&sw->intr_enable, ERROR_INTR | TRANSFER_DONE);
458     	set_timeout(fs, 2*HZ, xfer_timeout);	/* enable timeout */
459     }
460     
461     static void act(struct floppy_state *fs)
462     {
463     	volatile struct swim3 *sw = fs->swim3;
464     
465     	for (;;) {
466     		switch (fs->state) {
467     		case idle:
468     			return;		/* XXX shouldn't get here */
469     
470     		case locating:
471     			if (swim3_readbit(fs, TRACK_ZERO)) {
472     				fs->cur_cyl = 0;
473     				if (fs->req_cyl == 0)
474     					fs->state = do_transfer;
475     				else
476     					fs->state = seeking;
477     				break;
478     			}
479     			scan_track(fs);
480     			return;
481     
482     		case seeking:
483     			if (fs->cur_cyl < 0) {
484     				fs->expect_cyl = -1;
485     				fs->state = locating;
486     				break;
487     			}
488     			if (fs->req_cyl == fs->cur_cyl) {
489     				printk("whoops, seeking 0\n");
490     				fs->state = do_transfer;
491     				break;
492     			}
493     			seek_track(fs, fs->req_cyl - fs->cur_cyl);
494     			return;
495     
496     		case settling:
497     			/* wait for SEEK_COMPLETE to become true */
498     			swim3_select(fs, SEEK_COMPLETE);
499     			udelay(10);
500     			out_8(&sw->intr_enable, ERROR_INTR | DATA_CHANGED);
501     			in_8(&sw->intr);	/* clear DATA_CHANGED */
502     			if (in_8(&sw->status) & DATA) {
503     				/* seek_complete is not yet true */
504     				set_timeout(fs, HZ/2, seek_timeout);
505     				return;
506     			}
507     			out_8(&sw->intr_enable, 0);
508     			in_8(&sw->intr);
509     			fs->state = locating;
510     			break;
511     
512     		case do_transfer:
513     			if (fs->cur_cyl != fs->req_cyl) {
514     				if (fs->retries > 5) {
515     					end_request(0);
516     					fs->state = idle;
517     					return;
518     				}
519     				fs->state = seeking;
520     				break;
521     			}
522     			setup_transfer(fs);
523     			return;
524     
525     		case jogging:
526     			seek_track(fs, -5);
527     			return;
528     
529     		default:
530     			printk(KERN_ERR"swim3: unknown state %d\n", fs->state);
531     			return;
532     		}
533     	}
534     }
535     
536     static void scan_timeout(unsigned long data)
537     {
538     	struct floppy_state *fs = (struct floppy_state *) data;
539     	volatile struct swim3 *sw = fs->swim3;
540     
541     	fs->timeout_pending = 0;
542     	out_8(&sw->control_bic, DO_ACTION);
543     	out_8(&sw->select, RELAX);
544     	out_8(&sw->intr_enable, 0);
545     	fs->cur_cyl = -1;
546     	if (fs->retries > 5) {
547     		end_request(0);
548     		fs->state = idle;
549     		start_request(fs);
550     	} else {
551     		fs->state = jogging;
552     		act(fs);
553     	}
554     }
555     
556     static void seek_timeout(unsigned long data)
557     {
558     	struct floppy_state *fs = (struct floppy_state *) data;
559     	volatile struct swim3 *sw = fs->swim3;
560     
561     	fs->timeout_pending = 0;
562     	if (fs->state == settling) {
563     		printk(KERN_ERR "swim3: MSI sel=%x ctrl=%x stat=%x intr=%x ie=%x\n",
564     		       sw->select, sw->control, sw->status, sw->intr, sw->intr_enable);
565     	}
566     	out_8(&sw->control_bic, DO_SEEK);
567     	out_8(&sw->select, RELAX);
568     	out_8(&sw->intr_enable, 0);
569     	if (fs->state == settling && swim3_readbit(fs, SEEK_COMPLETE)) {
570     		/* printk(KERN_DEBUG "swim3: missed settling interrupt\n"); */
571     		fs->state = locating;
572     		act(fs);
573     		return;
574     	}
575     	printk(KERN_ERR "swim3: seek timeout\n");
576     	end_request(0);
577     	fs->state = idle;
578     	start_request(fs);
579     }
580     
581     static void xfer_timeout(unsigned long data)
582     {
583     	struct floppy_state *fs = (struct floppy_state *) data;
584     	volatile struct swim3 *sw = fs->swim3;
585     	struct dbdma_regs *dr = fs->dma;
586     	struct dbdma_cmd *cp = fs->dma_cmd;
587     	unsigned long s;
588     
589     	fs->timeout_pending = 0;
590     	st_le32(&dr->control, RUN << 16);
591     	out_8(&sw->intr_enable, 0);
592     	out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
593     	out_8(&sw->select, RELAX);
594     	if (CURRENT->cmd == WRITE)
595     		++cp;
596     	if (ld_le16(&cp->xfer_status) != 0)
597     		s = fs->scount - ((ld_le16(&cp->res_count) + 511) >> 9);
598     	else
599     		s = 0;
600     	CURRENT->sector += s;
601     	CURRENT->current_nr_sectors -= s;
602     	printk(KERN_ERR "swim3: timeout %sing sector %ld\n",
603     	       (CURRENT->cmd==WRITE? "writ": "read"), CURRENT->sector);
604     	end_request(0);
605     	fs->state = idle;
606     	start_request(fs);
607     }
608     
609     static void swim3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
610     {
611     	struct floppy_state *fs = (struct floppy_state *) dev_id;
612     	volatile struct swim3 *sw = fs->swim3;
613     	int intr, err, n;
614     	int stat, resid;
615     	struct dbdma_regs *dr;
616     	struct dbdma_cmd *cp;
617     
618     	err = in_8(&sw->error);
619     	intr = in_8(&sw->intr);
620     #if 0
621     	printk("swim3 intr state=%d intr=%x err=%x\n", fs->state, intr, err);
622     #endif
623     	if ((intr & ERROR_INTR) && fs->state != do_transfer)
624     		printk(KERN_ERR "swim3_interrupt, state=%d, cmd=%x, intr=%x, err=%x\n",
625     		       fs->state, CURRENT->cmd, intr, err);
626     	switch (fs->state) {
627     	case locating:
628     		if (intr & SEEN_SECTOR) {
629     			out_8(&sw->control_bic, DO_ACTION);
630     			out_8(&sw->select, RELAX);
631     			out_8(&sw->intr_enable, 0);
632     			del_timer(&fs->timeout);
633     			fs->timeout_pending = 0;
634     			if (sw->ctrack == 0xff) {
635     				printk(KERN_ERR "swim3: seen sector but cyl=ff?\n");
636     				fs->cur_cyl = -1;
637     				if (fs->retries > 5) {
638     					end_request(0);
639     					fs->state = idle;
640     					start_request(fs);
641     				} else {
642     					fs->state = jogging;
643     					act(fs);
644     				}
645     				break;
646     			}
647     			fs->cur_cyl = sw->ctrack;
648     			fs->cur_sector = sw->csect;
649     			if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
650     				printk(KERN_ERR "swim3: expected cyl %d, got %d\n",
651     				       fs->expect_cyl, fs->cur_cyl);
652     			fs->state = do_transfer;
653     			act(fs);
654     		}
655     		break;
656     	case seeking:
657     	case jogging:
658     		if (sw->nseek == 0) {
659     			out_8(&sw->control_bic, DO_SEEK);
660     			out_8(&sw->select, RELAX);
661     			out_8(&sw->intr_enable, 0);
662     			del_timer(&fs->timeout);
663     			fs->timeout_pending = 0;
664     			if (fs->state == seeking)
665     				++fs->retries;
666     			fs->state = settling;
667     			act(fs);
668     		}
669     		break;
670     	case settling:
671     		out_8(&sw->intr_enable, 0);
672     		del_timer(&fs->timeout);
673     		fs->timeout_pending = 0;
674     		act(fs);
675     		break;
676     	case do_transfer:
677     		if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
678     			break;
679     		dr = fs->dma;
680     		cp = fs->dma_cmd;
681     		st_le32(&dr->control, RUN << 16);
682     		out_8(&sw->intr_enable, 0);
683     		out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
684     		out_8(&sw->select, RELAX);
685     		del_timer(&fs->timeout);
686     		fs->timeout_pending = 0;
687     		if (CURRENT->cmd == WRITE)
688     			++cp;
689     		stat = ld_le16(&cp->xfer_status);
690     		resid = ld_le16(&cp->res_count);
691     		if (intr & ERROR_INTR) {
692     			n = fs->scount - 1 - resid / 512;
693     			if (n > 0) {
694     				CURRENT->sector += n;
695     				CURRENT->current_nr_sectors -= n;
696     				CURRENT->buffer += n * 512;
697     				fs->req_sector += n;
698     			}
699     			if (fs->retries < 5) {
700     				++fs->retries;
701     				act(fs);
702     			} else {
703     				printk("swim3: error %sing block %ld (err=%x)\n",
704     				       CURRENT->cmd == WRITE? "writ": "read",
705     				       CURRENT->sector, err);
706     				end_request(0);
707     				fs->state = idle;
708     			}
709     		} else {
710     			if ((stat & ACTIVE) == 0 || resid != 0) {
711     				/* musta been an error */
712     				printk(KERN_ERR "swim3: fd dma: stat=%x resid=%d\n", stat, resid);
713     				printk(KERN_ERR "  state=%d, cmd=%x, intr=%x, err=%x\n",
714     				       fs->state, CURRENT->cmd, intr, err);
715     				end_request(0);
716     				fs->state = idle;
717     				start_request(fs);
718     				break;
719     			}
720     			CURRENT->sector += fs->scount;
721     			CURRENT->current_nr_sectors -= fs->scount;
722     			CURRENT->buffer += fs->scount * 512;
723     			if (CURRENT->current_nr_sectors <= 0) {
724     				end_request(1);
725     				fs->state = idle;
726     			} else {
727     				fs->req_sector += fs->scount;
728     				if (fs->req_sector > fs->secpertrack) {
729     					fs->req_sector -= fs->secpertrack;
730     					if (++fs->head > 1) {
731     						fs->head = 0;
732     						++fs->req_cyl;
733     					}
734     				}
735     				act(fs);
736     			}
737     		}
738     		if (fs->state == idle)
739     			start_request(fs);
740     		break;
741     	default:
742     		printk(KERN_ERR "swim3: don't know what to do in state %d\n", fs->state);
743     	}
744     }
745     
746     /*
747     static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
748     {
749     }
750     */
751     
752     static int grab_drive(struct floppy_state *fs, enum swim_state state,
753     		      int interruptible)
754     {
755     	unsigned long flags;
756     
757     	save_flags(flags);
758     	cli();
759     	if (fs->state != idle) {
760     		++fs->wanted;
761     		while (fs->state != available) {
762     			if (interruptible && signal_pending(current)) {
763     				--fs->wanted;
764     				restore_flags(flags);
765     				return -EINTR;
766     			}
767     			interruptible_sleep_on(&fs->wait);
768     		}
769     		--fs->wanted;
770     	}
771     	fs->state = state;
772     	restore_flags(flags);
773     	return 0;
774     }
775     
776     static void release_drive(struct floppy_state *fs)
777     {
778     	unsigned long flags;
779     
780     	save_flags(flags);
781     	cli();
782     	fs->state = idle;
783     	start_request(fs);
784     	restore_flags(flags);
785     }
786     
787     static int fd_eject(struct floppy_state *fs)
788     {
789     	int err, n;
790     
791     	err = grab_drive(fs, ejecting, 1);
792     	if (err)
793     		return err;
794     	swim3_action(fs, EJECT);
795     	for (n = 2*HZ; n > 0; --n) {
796     		if (swim3_readbit(fs, RELAX))
797     			break;
798     		if (signal_pending(current)) {
799     			err = -EINTR;
800     			break;
801     		}
802     		current->state = TASK_INTERRUPTIBLE;
803     		schedule_timeout(1);
804     	}
805     	fs->ejected = 1;
806     	release_drive(fs);
807     	return err;
808     }
809     
810     int swim3_fd_eject(int devnum)
811     {
812     	if (devnum >= floppy_count)
813     		return -ENODEV;
814     	/* Do not check this - this function should ONLY be called early
815     	 * in the boot process! */
816     	/* if (floppy_states[devnum].ref_count != 1) return -EBUSY; */
817     	return fd_eject(&floppy_states[devnum]);
818     }
819     
820     static struct floppy_struct floppy_type =
821     	{ 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL };	/*  7 1.44MB 3.5"   */
822     
823     static int floppy_ioctl(struct inode *inode, struct file *filp,
824     			unsigned int cmd, unsigned long param)
825     {
826     	struct floppy_state *fs;
827     	int err;
828     	int devnum = MINOR(inode->i_rdev);
829     
830     	if (devnum >= floppy_count)
831     		return -ENODEV;
832     		
833     	if ((cmd & 0x80) && !suser())
834     		return -EPERM;
835     
836     	fs = &floppy_states[devnum];
837     
838     	if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
839     		return -ENXIO;
840     
841     	switch (cmd) {
842     	case FDEJECT:
843     		if (fs->ref_count != 1)
844     			return -EBUSY;
845     		err = fd_eject(fs);
846     		return err;
847     	case FDGETPRM:
848     	        err = copy_to_user((void *) param, (void *) &floppy_type,
849     				   sizeof(struct floppy_struct));
850     		return err;
851     	}
852     	return -ENOTTY;
853     }
854     
855     static int floppy_open(struct inode *inode, struct file *filp)
856     {
857     	struct floppy_state *fs;
858     	volatile struct swim3 *sw;
859     	int n, err;
860     	int devnum = MINOR(inode->i_rdev);
861     
862     	if (devnum >= floppy_count)
863     		return -ENODEV;
864     	if (filp == 0)
865     		return -EIO;
866     		
867     	fs = &floppy_states[devnum];
868     	sw = fs->swim3;
869     	err = 0;
870     	if (fs->ref_count == 0) {
871     		if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
872     			return -ENXIO;
873     		out_8(&sw->mode, 0x95);
874     		out_8(&sw->control_bic, 0xff);
875     		out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
876     		udelay(10);
877     		out_8(&sw->intr_enable, 0);
878     		out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
879     		swim3_action(fs, MOTOR_ON);
880     		fs->write_prot = -1;
881     		fs->cur_cyl = -1;
882     		for (n = HZ; n > 0; --n) {
883     			if (swim3_readbit(fs, SEEK_COMPLETE))
884     				break;
885     			if (signal_pending(current)) {
886     				err = -EINTR;
887     				break;
888     			}
889     			current->state = TASK_INTERRUPTIBLE;
890     			schedule_timeout(1);
891     		}
892     		if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
893     				 || swim3_readbit(fs, DISK_IN) == 0))
894     			err = -ENXIO;
895     		swim3_action(fs, 9);
896     
897     	} else if (fs->ref_count == -1 || filp->f_flags & O_EXCL)
898     		return -EBUSY;
899     
900     	if (err == 0 && (filp->f_flags & O_NDELAY) == 0
901     	    && (filp->f_mode & 3)) {
902     		check_disk_change(inode->i_rdev);
903     		if (fs->ejected)
904     			err = -ENXIO;
905     	}
906     
907     	if (err == 0 && (filp->f_mode & 2)) {
908     		if (fs->write_prot < 0)
909     			fs->write_prot = swim3_readbit(fs, WRITE_PROT);
910     		if (fs->write_prot)
911     			err = -EROFS;
912     	}
913     
914     	if (err) {
915     		if (fs->ref_count == 0) {
916     			swim3_action(fs, MOTOR_OFF);
917     			out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
918     		}
919     		return err;
920     	}
921     
922     	if (filp->f_flags & O_EXCL)
923     		fs->ref_count = -1;
924     	else
925     		++fs->ref_count;
926     
927     	return 0;
928     }
929     
930     static int floppy_release(struct inode *inode, struct file *filp)
931     {
932     	struct floppy_state *fs;
933     	volatile struct swim3 *sw;
934     	int devnum = MINOR(inode->i_rdev);
935     
936     	if (devnum >= floppy_count)
937     		return -ENODEV;
938     
939     	fs = &floppy_states[devnum];
940     	sw = fs->swim3;
941     	if (fs->ref_count > 0 && --fs->ref_count == 0) {
942     		swim3_action(fs, MOTOR_OFF);
943     		out_8(&sw->control_bic, 0xff);
944     	}
945     	return 0;
946     }
947     
948     static int floppy_check_change(kdev_t dev)
949     {
950     	struct floppy_state *fs;
951     	int devnum = MINOR(dev);
952     
953     	if (MAJOR(dev) != MAJOR_NR || (devnum >= floppy_count))
954     		return 0;
955     		
956     	fs = &floppy_states[devnum];
957     	return fs->ejected;
958     }
959     
960     static int floppy_revalidate(kdev_t dev)
961     {
962     	struct floppy_state *fs;
963     	volatile struct swim3 *sw;
964     	int ret, n;
965     	int devnum = MINOR(dev);
966     
967     	if (MAJOR(dev) != MAJOR_NR || (devnum >= floppy_count))
968     		return 0;
969     
970     	fs = &floppy_states[devnum];
971     
972     	if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
973     		return -ENXIO;
974     
975     	sw = fs->swim3;
976     	grab_drive(fs, revalidating, 0);
977     	out_8(&sw->intr_enable, 0);
978     	out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
979     	swim3_action(fs, MOTOR_ON);
980     	fs->write_prot = -1;
981     	fs->cur_cyl = -1;
982     	for (n = HZ; n > 0; --n) {
983     		if (swim3_readbit(fs, SEEK_COMPLETE))
984     			break;
985     		if (signal_pending(current))
986     			break;
987     		current->state = TASK_INTERRUPTIBLE;
988     		schedule_timeout(1);
989     	}
990     	ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
991     		|| swim3_readbit(fs, DISK_IN) == 0;
992     	if (ret)
993     		swim3_action(fs, MOTOR_OFF);
994     	else {
995     		fs->ejected = 0;
996     		swim3_action(fs, 9);
997     	}
998     
999     	release_drive(fs);
1000     	return ret;
1001     }
1002     
1003     static void floppy_off(unsigned int nr)
1004     {
1005     }
1006     
1007     static struct block_device_operations floppy_fops = {
1008     	open:			floppy_open,
1009     	release:		floppy_release,
1010     	ioctl:			floppy_ioctl,
1011     	check_media_change:	floppy_check_change,
1012     	revalidate:		floppy_revalidate,
1013     };
1014     
1015     static devfs_handle_t floppy_devfs_handle;
1016     
1017     int swim3_init(void)
1018     {
1019     	struct device_node *swim;
1020     
1021     	floppy_devfs_handle = devfs_mk_dir(NULL, "floppy", NULL);
1022     
1023     	swim = find_devices("floppy");
1024     	while (swim && (floppy_count < MAX_FLOPPIES))
1025     	{
1026     		swim3_add_device(swim);
1027     		swim = swim->next;
1028     	}
1029     
1030     	swim = find_devices("swim3");
1031     	while (swim && (floppy_count < MAX_FLOPPIES))
1032     	{
1033     		swim3_add_device(swim);
1034     		swim = swim->next;
1035     	}
1036     
1037     	if (floppy_count > 0)
1038     	{
1039     		if (devfs_register_blkdev(MAJOR_NR, "fd", &floppy_fops)) {
1040     			printk(KERN_ERR "Unable to get major %d for floppy\n",
1041     			       MAJOR_NR);
1042     			return -EBUSY;
1043     		}
1044     		blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
1045     		blksize_size[MAJOR_NR] = floppy_blocksizes;
1046     		blk_size[MAJOR_NR] = floppy_sizes;
1047     	}
1048     
1049     	return 0;
1050     }
1051     
1052     static int swim3_add_device(struct device_node *swim)
1053     {
1054     	struct device_node *mediabay;
1055     	struct floppy_state *fs = &floppy_states[floppy_count];
1056     	char floppy_name[16];
1057     	devfs_handle_t floppy_handle;
1058     
1059     	if (swim->n_addrs < 2)
1060     	{
1061     		printk(KERN_INFO "swim3: expecting 2 addrs (n_addrs:%d, n_intrs:%d)\n",
1062     		       swim->n_addrs, swim->n_intrs);
1063     		return -EINVAL;
1064     	}
1065     
1066     	if (swim->n_intrs < 2)
1067     	{
1068     		printk(KERN_INFO "swim3: expecting 2 intrs (n_addrs:%d, n_intrs:%d)\n",
1069     		       swim->n_addrs, swim->n_intrs);
1070     		return -EINVAL;
1071     	}
1072     
1073     	mediabay = (strcasecmp(swim->parent->type, "media-bay") == 0) ? swim->parent : NULL;
1074     	if (mediabay == NULL)
1075     		feature_set(swim, FEATURE_SWIM3_enable);
1076     	
1077     	memset(fs, 0, sizeof(*fs));
1078     	fs->state = idle;
1079     	fs->swim3 = (volatile struct swim3 *) ioremap(swim->addrs[0].address, 0x200);
1080     	fs->dma = (struct dbdma_regs *) ioremap(swim->addrs[1].address, 0x200);
1081     	fs->swim3_intr = swim->intrs[0].line;
1082     	fs->dma_intr = swim->intrs[1].line;
1083     	fs->cur_cyl = -1;
1084     	fs->cur_sector = -1;
1085     	fs->secpercyl = 36;
1086     	fs->secpertrack = 18;
1087     	fs->total_secs = 2880;
1088     	fs->media_bay = mediabay;
1089     	init_waitqueue_head(&fs->wait);
1090     
1091     	fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
1092     	memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
1093     	st_le16(&fs->dma_cmd[1].command, DBDMA_STOP);
1094     
1095     	if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
1096     		printk(KERN_ERR "Couldn't get irq %d for SWIM3\n", fs->swim3_intr);
1097     		feature_clear(swim, FEATURE_SWIM3_enable);
1098     		return -EBUSY;
1099     	}
1100     /*
1101     	if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) {
1102     		printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA",
1103     		       fs->dma_intr);
1104     		feature_clear(swim, FEATURE_SWIM3_enable);
1105     		return -EBUSY;
1106     	}
1107     */
1108     
1109     	init_timer(&fs->timeout);
1110     
1111     	do_floppy = NULL;
1112     
1113     	printk(KERN_INFO "fd%d: SWIM3 floppy controller %s\n", floppy_count,
1114     		mediabay ? "in media bay" : "");
1115     	sprintf(floppy_name, "%s%d", floppy_devfs_handle ? "" : "floppy",
1116     			floppy_count);
1117     	floppy_handle = devfs_register(floppy_devfs_handle, floppy_name, 
1118     			DEVFS_FL_DEFAULT, MAJOR_NR, floppy_count, 
1119     			S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP |S_IWGRP, 
1120     			&floppy_fops, NULL);
1121     
1122     	floppy_count++;
1123     	
1124     	return 0;
1125     }
1126