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