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