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