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