added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.git] / 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/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
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     secpercyl;      /* disk geometry information */
190         int     secpertrack;
191         int     total_secs;
192         int     write_prot;     /* 1 if write-protected, 0 if not, -1 dunno */
193         struct dbdma_cmd *dma_cmd;
194         int     ref_count;
195         int     expect_cyl;
196         struct timer_list timeout;
197         int     timeout_pending;
198         int     ejected;
199         wait_queue_head_t wait;
200         int     wanted;
201         struct device_node*     media_bay; /* NULL when not in bay */
202         char    dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)];
203 };
204
205 static struct floppy_state floppy_states[MAX_FLOPPIES];
206 static int floppy_count = 0;
207
208 static unsigned short write_preamble[] = {
209         0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */
210         0, 0, 0, 0, 0, 0,                       /* sync field */
211         0x99a1, 0x99a1, 0x99a1, 0x99fb,         /* data address mark */
212         0x990f                                  /* no escape for 512 bytes */
213 };
214
215 static unsigned short write_postamble[] = {
216         0x9904,                                 /* insert CRC */
217         0x4e4e, 0x4e4e,
218         0x9908,                                 /* stop writing */
219         0, 0, 0, 0, 0, 0
220 };
221
222 static void swim3_select(struct floppy_state *fs, int sel);
223 static void swim3_action(struct floppy_state *fs, int action);
224 static int swim3_readbit(struct floppy_state *fs, int bit);
225 static void do_fd_request(request_queue_t * q);
226 static void start_request(struct floppy_state *fs);
227 static void set_timeout(struct floppy_state *fs, int nticks,
228                         void (*proc)(unsigned long));
229 static void scan_track(struct floppy_state *fs);
230 static void seek_track(struct floppy_state *fs, int n);
231 static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count);
232 static void setup_transfer(struct floppy_state *fs);
233 static void act(struct floppy_state *fs);
234 static void scan_timeout(unsigned long data);
235 static void seek_timeout(unsigned long data);
236 static void xfer_timeout(unsigned long data);
237 static void swim3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
238 /*static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs);*/
239 static int grab_drive(struct floppy_state *fs, enum swim_state state,
240                       int interruptible);
241 static void release_drive(struct floppy_state *fs);
242 static int fd_eject(struct floppy_state *fs);
243 static int floppy_ioctl(struct inode *inode, struct file *filp,
244                         unsigned int cmd, unsigned long param);
245 static int floppy_open(struct inode *inode, struct file *filp);
246 static int floppy_release(struct inode *inode, struct file *filp);
247 static int floppy_check_change(kdev_t dev);
248 static int floppy_revalidate(kdev_t dev);
249 static int swim3_add_device(struct device_node *swims);
250 int swim3_init(void);
251
252 #ifndef CONFIG_PMAC_PBOOK
253 #define check_media_bay(which, what)    1
254 #endif
255
256 static void swim3_select(struct floppy_state *fs, int sel)
257 {
258         volatile struct swim3 *sw = fs->swim3;
259
260         out_8(&sw->select, RELAX);
261         if (sel & 8)
262                 out_8(&sw->control_bis, SELECT);
263         else
264                 out_8(&sw->control_bic, SELECT);
265         out_8(&sw->select, sel & CA_MASK);
266 }
267
268 static void swim3_action(struct floppy_state *fs, int action)
269 {
270         volatile struct swim3 *sw = fs->swim3;
271
272         swim3_select(fs, action);
273         udelay(1);
274         out_8(&sw->select, sw->select | LSTRB);
275         udelay(2);
276         out_8(&sw->select, sw->select & ~LSTRB);
277         udelay(1);
278         out_8(&sw->select, RELAX);
279 }
280
281 static int swim3_readbit(struct floppy_state *fs, int bit)
282 {
283         volatile struct swim3 *sw = fs->swim3;
284         int stat;
285
286         swim3_select(fs, bit);
287         udelay(10);
288         stat = in_8(&sw->status);
289         out_8(&sw->select, RELAX);
290         return (stat & DATA) == 0;
291 }
292
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
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
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
377 static inline void scan_track(struct floppy_state *fs)
378 {
379         volatile struct swim3 *sw = fs->swim3;
380         int xx;
381
382         swim3_select(fs, READ_DATA_0);
383         xx = sw->intr;          /* clear SEEN_SECTOR bit */
384         out_8(&sw->control_bis, DO_ACTION);
385         /* enable intr when track found */
386         out_8(&sw->intr_enable, ERROR_INTR | SEEN_SECTOR);
387         set_timeout(fs, HZ, scan_timeout);      /* enable timeout */
388 }
389
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         out_8(&sw->control_bis, DO_SEEK);
404         /* enable intr when seek finished */
405         out_8(&sw->intr_enable, ERROR_INTR | SEEK_DONE);
406         set_timeout(fs, HZ/2, seek_timeout);    /* enable timeout */
407 }
408
409 static inline void init_dma(struct dbdma_cmd *cp, int cmd,
410                             void *buf, int count)
411 {
412         st_le16(&cp->req_count, count);
413         st_le16(&cp->command, cmd);
414         st_le32(&cp->phy_addr, virt_to_bus(buf));
415         cp->xfer_status = 0;
416 }
417
418 static inline void setup_transfer(struct floppy_state *fs)
419 {
420         int n;
421         volatile struct swim3 *sw = fs->swim3;
422         struct dbdma_cmd *cp = fs->dma_cmd;
423         struct dbdma_regs *dr = fs->dma;
424
425         if (CURRENT->current_nr_sectors <= 0) {
426                 printk(KERN_ERR "swim3: transfer 0 sectors?\n");
427                 return;
428         }
429         if (CURRENT->cmd == WRITE)
430                 n = 1;
431         else {
432                 n = fs->secpertrack - fs->req_sector + 1;
433                 if (n > CURRENT->current_nr_sectors)
434                         n = CURRENT->current_nr_sectors;
435         }
436         fs->scount = n;
437         swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
438         out_8(&sw->sector, fs->req_sector);
439         out_8(&sw->nsect, n);
440         out_8(&sw->gap3, 0);
441         st_le32(&dr->cmdptr, virt_to_bus(cp));
442         if (CURRENT->cmd == WRITE) {
443                 /* Set up 3 dma commands: write preamble, data, postamble */
444                 init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));
445                 ++cp;
446                 init_dma(cp, OUTPUT_MORE, CURRENT->buffer, 512);
447                 ++cp;
448                 init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble));
449         } else {
450                 init_dma(cp, INPUT_LAST, CURRENT->buffer, n * 512);
451         }
452         ++cp;
453         out_le16(&cp->command, DBDMA_STOP);
454         out_le32(&dr->control, (RUN << 16) | RUN);
455         out_8(&sw->control_bis,
456               (CURRENT->cmd == WRITE? WRITE_SECTORS: 0) | DO_ACTION);
457         /* enable intr when transfer complete */
458         out_8(&sw->intr_enable, ERROR_INTR | TRANSFER_DONE);
459         set_timeout(fs, 2*HZ, xfer_timeout);    /* enable timeout */
460 }
461
462 static void act(struct floppy_state *fs)
463 {
464         volatile struct swim3 *sw = fs->swim3;
465
466         for (;;) {
467                 switch (fs->state) {
468                 case idle:
469                         return;         /* XXX shouldn't get here */
470
471                 case locating:
472                         if (swim3_readbit(fs, TRACK_ZERO)) {
473                                 fs->cur_cyl = 0;
474                                 if (fs->req_cyl == 0)
475                                         fs->state = do_transfer;
476                                 else
477                                         fs->state = seeking;
478                                 break;
479                         }
480                         scan_track(fs);
481                         return;
482
483                 case seeking:
484                         if (fs->cur_cyl < 0) {
485                                 fs->expect_cyl = -1;
486                                 fs->state = locating;
487                                 break;
488                         }
489                         if (fs->req_cyl == fs->cur_cyl) {
490                                 printk("whoops, seeking 0\n");
491                                 fs->state = do_transfer;
492                                 break;
493                         }
494                         seek_track(fs, fs->req_cyl - fs->cur_cyl);
495                         return;
496
497                 case settling:
498                         /* wait for SEEK_COMPLETE to become true */
499                         swim3_select(fs, SEEK_COMPLETE);
500                         udelay(10);
501                         out_8(&sw->intr_enable, ERROR_INTR | DATA_CHANGED);
502                         in_8(&sw->intr);        /* clear DATA_CHANGED */
503                         if (in_8(&sw->status) & DATA) {
504                                 /* seek_complete is not yet true */
505                                 set_timeout(fs, HZ/2, seek_timeout);
506                                 return;
507                         }
508                         out_8(&sw->intr_enable, 0);
509                         in_8(&sw->intr);
510                         fs->state = locating;
511                         break;
512
513                 case do_transfer:
514                         if (fs->cur_cyl != fs->req_cyl) {
515                                 if (fs->retries > 5) {
516                                         end_request(0);
517                                         fs->state = idle;
518                                         return;
519                                 }
520                                 fs->state = seeking;
521                                 break;
522                         }
523                         setup_transfer(fs);
524                         return;
525
526                 case jogging:
527                         seek_track(fs, -5);
528                         return;
529
530                 default:
531                         printk(KERN_ERR"swim3: unknown state %d\n", fs->state);
532                         return;
533                 }
534         }
535 }
536
537 static void scan_timeout(unsigned long data)
538 {
539         struct floppy_state *fs = (struct floppy_state *) data;
540         volatile struct swim3 *sw = fs->swim3;
541
542         fs->timeout_pending = 0;
543         out_8(&sw->control_bic, DO_ACTION);
544         out_8(&sw->select, RELAX);
545         out_8(&sw->intr_enable, 0);
546         fs->cur_cyl = -1;
547         if (fs->retries > 5) {
548                 end_request(0);
549                 fs->state = idle;
550                 start_request(fs);
551         } else {
552                 fs->state = jogging;
553                 act(fs);
554         }
555 }
556
557 static void seek_timeout(unsigned long data)
558 {
559         struct floppy_state *fs = (struct floppy_state *) data;
560         volatile struct swim3 *sw = fs->swim3;
561
562         fs->timeout_pending = 0;
563         if (fs->state == settling) {
564                 printk(KERN_ERR "swim3: MSI sel=%x ctrl=%x stat=%x intr=%x ie=%x\n",
565                        sw->select, sw->control, sw->status, sw->intr, sw->intr_enable);
566         }
567         out_8(&sw->control_bic, DO_SEEK);
568         out_8(&sw->select, RELAX);
569         out_8(&sw->intr_enable, 0);
570         if (fs->state == settling && swim3_readbit(fs, SEEK_COMPLETE)) {
571                 /* printk(KERN_DEBUG "swim3: missed settling interrupt\n"); */
572                 fs->state = locating;
573                 act(fs);
574                 return;
575         }
576         printk(KERN_ERR "swim3: seek timeout\n");
577         end_request(0);
578         fs->state = idle;
579         start_request(fs);
580 }
581
582 static void xfer_timeout(unsigned long data)
583 {
584         struct floppy_state *fs = (struct floppy_state *) data;
585         volatile struct swim3 *sw = fs->swim3;
586         struct dbdma_regs *dr = fs->dma;
587         struct dbdma_cmd *cp = fs->dma_cmd;
588         unsigned long s;
589
590         fs->timeout_pending = 0;
591         st_le32(&dr->control, RUN << 16);
592         out_8(&sw->intr_enable, 0);
593         out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
594         out_8(&sw->select, RELAX);
595         if (CURRENT->cmd == WRITE)
596                 ++cp;
597         if (ld_le16(&cp->xfer_status) != 0)
598                 s = fs->scount - ((ld_le16(&cp->res_count) + 511) >> 9);
599         else
600                 s = 0;
601         CURRENT->sector += s;
602         CURRENT->current_nr_sectors -= s;
603         printk(KERN_ERR "swim3: timeout %sing sector %ld\n",
604                (CURRENT->cmd==WRITE? "writ": "read"), CURRENT->sector);
605         end_request(0);
606         fs->state = idle;
607         start_request(fs);
608 }
609
610 static void swim3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
611 {
612         struct floppy_state *fs = (struct floppy_state *) dev_id;
613         volatile struct swim3 *sw = fs->swim3;
614         int intr, err, n;
615         int stat, resid;
616         struct dbdma_regs *dr;
617         struct dbdma_cmd *cp;
618
619         err = in_8(&sw->error);
620         intr = in_8(&sw->intr);
621 #if 0
622         printk("swim3 intr state=%d intr=%x err=%x\n", fs->state, intr, err);
623 #endif
624         if ((intr & ERROR_INTR) && fs->state != do_transfer)
625                 printk(KERN_ERR "swim3_interrupt, state=%d, cmd=%x, intr=%x, err=%x\n",
626                        fs->state, CURRENT->cmd, intr, err);
627         switch (fs->state) {
628         case locating:
629                 if (intr & SEEN_SECTOR) {
630                         out_8(&sw->control_bic, DO_ACTION);
631                         out_8(&sw->select, RELAX);
632                         out_8(&sw->intr_enable, 0);
633                         del_timer(&fs->timeout);
634                         fs->timeout_pending = 0;
635                         if (sw->ctrack == 0xff) {
636                                 printk(KERN_ERR "swim3: seen sector but cyl=ff?\n");
637                                 fs->cur_cyl = -1;
638                                 if (fs->retries > 5) {
639                                         end_request(0);
640                                         fs->state = idle;
641                                         start_request(fs);
642                                 } else {
643                                         fs->state = jogging;
644                                         act(fs);
645                                 }
646                                 break;
647                         }
648                         fs->cur_cyl = sw->ctrack;
649                         fs->cur_sector = sw->csect;
650                         if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
651                                 printk(KERN_ERR "swim3: expected cyl %d, got %d\n",
652                                        fs->expect_cyl, fs->cur_cyl);
653                         fs->state = do_transfer;
654                         act(fs);
655                 }
656                 break;
657         case seeking:
658         case jogging:
659                 if (sw->nseek == 0) {
660                         out_8(&sw->control_bic, DO_SEEK);
661                         out_8(&sw->select, RELAX);
662                         out_8(&sw->intr_enable, 0);
663                         del_timer(&fs->timeout);
664                         fs->timeout_pending = 0;
665                         if (fs->state == seeking)
666                                 ++fs->retries;
667                         fs->state = settling;
668                         act(fs);
669                 }
670                 break;
671         case settling:
672                 out_8(&sw->intr_enable, 0);
673                 del_timer(&fs->timeout);
674                 fs->timeout_pending = 0;
675                 act(fs);
676                 break;
677         case do_transfer:
678                 if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
679                         break;
680                 dr = fs->dma;
681                 cp = fs->dma_cmd;
682                 /* We must wait a bit for dbdma to complete */
683                 for (n=0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++)
684                         udelay(10);
685                 DBDMA_DO_STOP(dr);
686                 out_8(&sw->intr_enable, 0);
687                 out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
688                 out_8(&sw->select, RELAX);
689                 del_timer(&fs->timeout);
690                 fs->timeout_pending = 0;
691                 if (CURRENT->cmd == WRITE)
692                         ++cp;
693                 stat = ld_le16(&cp->xfer_status);
694                 resid = ld_le16(&cp->res_count);
695                 if (intr & ERROR_INTR) {
696                         n = fs->scount - 1 - resid / 512;
697                         if (n > 0) {
698                                 CURRENT->sector += n;
699                                 CURRENT->current_nr_sectors -= n;
700                                 CURRENT->buffer += n * 512;
701                                 fs->req_sector += n;
702                         }
703                         if (fs->retries < 5) {
704                                 ++fs->retries;
705                                 act(fs);
706                         } else {
707                                 printk("swim3: error %sing block %ld (err=%x)\n",
708                                        CURRENT->cmd == WRITE? "writ": "read",
709                                        CURRENT->sector, err);
710                                 end_request(0);
711                                 fs->state = idle;
712                         }
713                 } else {
714                         if ((stat & ACTIVE) == 0 || resid != 0) {
715                                 /* musta been an error */
716                                 printk(KERN_ERR "swim3: fd dma: stat=%x resid=%d\n", stat, resid);
717                                 printk(KERN_ERR "  state=%d, cmd=%x, intr=%x, err=%x\n",
718                                        fs->state, CURRENT->cmd, intr, err);
719                                 end_request(0);
720                                 fs->state = idle;
721                                 start_request(fs);
722                                 break;
723                         }
724                         CURRENT->sector += fs->scount;
725                         CURRENT->current_nr_sectors -= fs->scount;
726                         CURRENT->buffer += fs->scount * 512;
727                         if (CURRENT->current_nr_sectors <= 0) {
728                                 end_request(1);
729                                 fs->state = idle;
730                         } else {
731                                 fs->req_sector += fs->scount;
732                                 if (fs->req_sector > fs->secpertrack) {
733                                         fs->req_sector -= fs->secpertrack;
734                                         if (++fs->head > 1) {
735                                                 fs->head = 0;
736                                                 ++fs->req_cyl;
737                                         }
738                                 }
739                                 act(fs);
740                         }
741                 }
742                 if (fs->state == idle)
743                         start_request(fs);
744                 break;
745         default:
746                 printk(KERN_ERR "swim3: don't know what to do in state %d\n", fs->state);
747         }
748 }
749
750 /*
751 static void fd_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
752 {
753 }
754 */
755
756 static int grab_drive(struct floppy_state *fs, enum swim_state state,
757                       int interruptible)
758 {
759         unsigned long flags;
760
761         save_flags(flags);
762         cli();
763         if (fs->state != idle) {
764                 ++fs->wanted;
765                 while (fs->state != available) {
766                         if (interruptible && signal_pending(current)) {
767                                 --fs->wanted;
768                                 restore_flags(flags);
769                                 return -EINTR;
770                         }
771                         interruptible_sleep_on(&fs->wait);
772                 }
773                 --fs->wanted;
774         }
775         fs->state = state;
776         restore_flags(flags);
777         return 0;
778 }
779
780 static void release_drive(struct floppy_state *fs)
781 {
782         unsigned long flags;
783
784         save_flags(flags);
785         cli();
786         fs->state = idle;
787         start_request(fs);
788         restore_flags(flags);
789 }
790
791 static int fd_eject(struct floppy_state *fs)
792 {
793         int err, n;
794
795         err = grab_drive(fs, ejecting, 1);
796         if (err)
797                 return err;
798         swim3_action(fs, EJECT);
799         for (n = 2*HZ; n > 0; --n) {
800                 if (swim3_readbit(fs, RELAX))
801                         break;
802                 if (signal_pending(current)) {
803                         err = -EINTR;
804                         break;
805                 }
806                 current->state = TASK_INTERRUPTIBLE;
807                 schedule_timeout(1);
808         }
809         fs->ejected = 1;
810         release_drive(fs);
811         return err;
812 }
813
814 static struct floppy_struct floppy_type =
815         { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL };    /*  7 1.44MB 3.5"   */
816
817 static int floppy_ioctl(struct inode *inode, struct file *filp,
818                         unsigned int cmd, unsigned long param)
819 {
820         struct floppy_state *fs;
821         int err;
822         int devnum = MINOR(inode->i_rdev);
823
824         if (devnum >= floppy_count)
825                 return -ENODEV;
826                 
827         if ((cmd & 0x80) && !suser())
828                 return -EPERM;
829
830         fs = &floppy_states[devnum];
831
832         if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
833                 return -ENXIO;
834
835         switch (cmd) {
836         case FDEJECT:
837                 if (fs->ref_count != 1)
838                         return -EBUSY;
839                 err = fd_eject(fs);
840                 return err;
841         case FDGETPRM:
842                 if (copy_to_user((void *) param, (void *)&floppy_type,
843                                  sizeof(struct floppy_struct)))
844                         return -EFAULT;
845                 return 0;
846         }
847         return -ENOTTY;
848 }
849
850 static int floppy_open(struct inode *inode, struct file *filp)
851 {
852         struct floppy_state *fs;
853         volatile struct swim3 *sw;
854         int n, err;
855         int devnum = MINOR(inode->i_rdev);
856
857         if (devnum >= floppy_count)
858                 return -ENODEV;
859         if (filp == 0)
860                 return -EIO;
861                 
862         fs = &floppy_states[devnum];
863         sw = fs->swim3;
864         err = 0;
865         if (fs->ref_count == 0) {
866                 if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
867                         return -ENXIO;
868                 out_8(&sw->mode, 0x95);
869                 out_8(&sw->control_bic, 0xff);
870                 out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
871                 udelay(10);
872                 out_8(&sw->intr_enable, 0);
873                 out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
874                 swim3_action(fs, MOTOR_ON);
875                 fs->write_prot = -1;
876                 fs->cur_cyl = -1;
877                 for (n = HZ; n > 0; --n) {
878                         if (swim3_readbit(fs, SEEK_COMPLETE))
879                                 break;
880                         if (signal_pending(current)) {
881                                 err = -EINTR;
882                                 break;
883                         }
884                         current->state = TASK_INTERRUPTIBLE;
885                         schedule_timeout(1);
886                 }
887                 if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
888                                  || swim3_readbit(fs, DISK_IN) == 0))
889                         err = -ENXIO;
890                 swim3_action(fs, 9);
891
892         } else if (fs->ref_count == -1 || filp->f_flags & O_EXCL)
893                 return -EBUSY;
894
895         if (err == 0 && (filp->f_flags & O_NDELAY) == 0
896             && (filp->f_mode & 3)) {
897                 check_disk_change(inode->i_rdev);
898                 if (fs->ejected)
899                         err = -ENXIO;
900         }
901
902         if (err == 0 && (filp->f_mode & 2)) {
903                 if (fs->write_prot < 0)
904                         fs->write_prot = swim3_readbit(fs, WRITE_PROT);
905                 if (fs->write_prot)
906                         err = -EROFS;
907         }
908
909         if (err) {
910                 if (fs->ref_count == 0) {
911                         swim3_action(fs, MOTOR_OFF);
912                         out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
913                 }
914                 return err;
915         }
916
917         if (filp->f_flags & O_EXCL)
918                 fs->ref_count = -1;
919         else
920                 ++fs->ref_count;
921
922         return 0;
923 }
924
925 static int floppy_release(struct inode *inode, struct file *filp)
926 {
927         struct floppy_state *fs;
928         volatile struct swim3 *sw;
929         int devnum = MINOR(inode->i_rdev);
930
931         if (devnum >= floppy_count)
932                 return -ENODEV;
933
934         fs = &floppy_states[devnum];
935         sw = fs->swim3;
936         if (fs->ref_count > 0 && --fs->ref_count == 0) {
937                 swim3_action(fs, MOTOR_OFF);
938                 out_8(&sw->control_bic, 0xff);
939         }
940         return 0;
941 }
942
943 static int floppy_check_change(kdev_t dev)
944 {
945         struct floppy_state *fs;
946         int devnum = MINOR(dev);
947
948         if (MAJOR(dev) != MAJOR_NR || (devnum >= floppy_count))
949                 return 0;
950                 
951         fs = &floppy_states[devnum];
952         return fs->ejected;
953 }
954
955 static int floppy_revalidate(kdev_t dev)
956 {
957         struct floppy_state *fs;
958         volatile struct swim3 *sw;
959         int ret, n;
960         int devnum = MINOR(dev);
961
962         if (MAJOR(dev) != MAJOR_NR || (devnum >= floppy_count))
963                 return 0;
964
965         fs = &floppy_states[devnum];
966
967         if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
968                 return -ENXIO;
969
970         sw = fs->swim3;
971         grab_drive(fs, revalidating, 0);
972         out_8(&sw->intr_enable, 0);
973         out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
974         swim3_action(fs, MOTOR_ON);
975         fs->write_prot = -1;
976         fs->cur_cyl = -1;
977         for (n = HZ; n > 0; --n) {
978                 if (swim3_readbit(fs, SEEK_COMPLETE))
979                         break;
980                 if (signal_pending(current))
981                         break;
982                 current->state = TASK_INTERRUPTIBLE;
983                 schedule_timeout(1);
984         }
985         ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
986                 || swim3_readbit(fs, DISK_IN) == 0;
987         if (ret)
988                 swim3_action(fs, MOTOR_OFF);
989         else {
990                 fs->ejected = 0;
991                 swim3_action(fs, 9);
992         }
993
994         release_drive(fs);
995         return ret;
996 }
997
998 static void floppy_off(unsigned int nr)
999 {
1000 }
1001
1002 static struct block_device_operations floppy_fops = {
1003         open:                   floppy_open,
1004         release:                floppy_release,
1005         ioctl:                  floppy_ioctl,
1006         check_media_change:     floppy_check_change,
1007         revalidate:             floppy_revalidate,
1008 };
1009
1010 static devfs_handle_t floppy_devfs_handle;
1011
1012 int swim3_init(void)
1013 {
1014         struct device_node *swim;
1015
1016         floppy_devfs_handle = devfs_mk_dir(NULL, "floppy", NULL);
1017
1018         swim = find_devices("floppy");
1019         while (swim && (floppy_count < MAX_FLOPPIES))
1020         {
1021                 swim3_add_device(swim);
1022                 swim = swim->next;
1023         }
1024
1025         swim = find_devices("swim3");
1026         while (swim && (floppy_count < MAX_FLOPPIES))
1027         {
1028                 swim3_add_device(swim);
1029                 swim = swim->next;
1030         }
1031
1032         if (floppy_count > 0)
1033         {
1034                 if (devfs_register_blkdev(MAJOR_NR, "fd", &floppy_fops)) {
1035                         printk(KERN_ERR "Unable to get major %d for floppy\n",
1036                                MAJOR_NR);
1037                         return -EBUSY;
1038                 }
1039                 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
1040                 blksize_size[MAJOR_NR] = floppy_blocksizes;
1041                 blk_size[MAJOR_NR] = floppy_sizes;
1042         }
1043
1044         return 0;
1045 }
1046
1047 static int swim3_add_device(struct device_node *swim)
1048 {
1049         struct device_node *mediabay;
1050         struct floppy_state *fs = &floppy_states[floppy_count];
1051         char floppy_name[16];
1052         devfs_handle_t floppy_handle;
1053
1054         if (swim->n_addrs < 2)
1055         {
1056                 printk(KERN_INFO "swim3: expecting 2 addrs (n_addrs:%d, n_intrs:%d)\n",
1057                        swim->n_addrs, swim->n_intrs);
1058                 return -EINVAL;
1059         }
1060
1061         if (swim->n_intrs < 2)
1062         {
1063                 printk(KERN_INFO "swim3: expecting 2 intrs (n_addrs:%d, n_intrs:%d)\n",
1064                        swim->n_addrs, swim->n_intrs);
1065                 return -EINVAL;
1066         }
1067
1068         if (!request_OF_resource(swim, 0, NULL)) {
1069                 printk(KERN_INFO "swim3: can't request IO resource !\n");
1070                 return -EINVAL;
1071         }
1072
1073         mediabay = (strcasecmp(swim->parent->type, "media-bay") == 0) ? swim->parent : NULL;
1074         if (mediabay == NULL)
1075                 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
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                 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
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                 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
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 }