block: convert to pos and nr_sectors accessors
[linux-2.6.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/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(struct request_queue * 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);
242 /*static void fd_dma_interrupt(int irq, void *dev_id);*/
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 block_device *bdev, fmode_t mode,
248                         unsigned int cmd, unsigned long param);
249 static int floppy_open(struct block_device *bdev, fmode_t mode);
250 static int floppy_release(struct gendisk *disk, fmode_t mode);
251 static int floppy_check_change(struct gendisk *disk);
252 static int floppy_revalidate(struct gendisk *disk);
253
254 static void swim3_select(struct floppy_state *fs, int sel)
255 {
256         struct swim3 __iomem *sw = fs->swim3;
257
258         out_8(&sw->select, RELAX);
259         if (sel & 8)
260                 out_8(&sw->control_bis, SELECT);
261         else
262                 out_8(&sw->control_bic, SELECT);
263         out_8(&sw->select, sel & CA_MASK);
264 }
265
266 static void swim3_action(struct floppy_state *fs, int action)
267 {
268         struct swim3 __iomem *sw = fs->swim3;
269
270         swim3_select(fs, action);
271         udelay(1);
272         out_8(&sw->select, sw->select | LSTRB);
273         udelay(2);
274         out_8(&sw->select, sw->select & ~LSTRB);
275         udelay(1);
276 }
277
278 static int swim3_readbit(struct floppy_state *fs, int bit)
279 {
280         struct swim3 __iomem *sw = fs->swim3;
281         int stat;
282
283         swim3_select(fs, bit);
284         udelay(1);
285         stat = in_8(&sw->status);
286         return (stat & DATA) == 0;
287 }
288
289 static void do_fd_request(struct request_queue * q)
290 {
291         int i;
292         for(i=0;i<floppy_count;i++)
293         {
294 #ifdef CONFIG_PMAC_MEDIABAY
295                 if (floppy_states[i].media_bay &&
296                         check_media_bay(floppy_states[i].media_bay, MB_FD))
297                         continue;
298 #endif /* CONFIG_PMAC_MEDIABAY */
299                 start_request(&floppy_states[i]);
300         }
301 }
302
303 static void start_request(struct floppy_state *fs)
304 {
305         struct request *req;
306         unsigned long x;
307
308         if (fs->state == idle && fs->wanted) {
309                 fs->state = available;
310                 wake_up(&fs->wait);
311                 return;
312         }
313         while (fs->state == idle && (req = elv_next_request(swim3_queue))) {
314 #if 0
315                 printk("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%u buf=%p\n",
316                        req->rq_disk->disk_name, req->cmd,
317                        (long)blk_rq_pos(req), blk_rq_sectors(req), req->buffer);
318                 printk("           errors=%d current_nr_sectors=%u\n",
319                        req->errors, blk_rq_cur_sectors(req));
320 #endif
321
322                 if (blk_rq_pos(req) >= fs->total_secs) {
323                         __blk_end_request_cur(req, -EIO);
324                         continue;
325                 }
326                 if (fs->ejected) {
327                         __blk_end_request_cur(req, -EIO);
328                         continue;
329                 }
330
331                 if (rq_data_dir(req) == WRITE) {
332                         if (fs->write_prot < 0)
333                                 fs->write_prot = swim3_readbit(fs, WRITE_PROT);
334                         if (fs->write_prot) {
335                                 __blk_end_request_cur(req, -EIO);
336                                 continue;
337                         }
338                 }
339
340                 /* Do not remove the cast. blk_rq_pos(req) is now a
341                  * sector_t and can be 64 bits, but it will never go
342                  * past 32 bits for this driver anyway, so we can
343                  * safely cast it down and not have to do a 64/32
344                  * division
345                  */
346                 fs->req_cyl = ((long)blk_rq_pos(req)) / fs->secpercyl;
347                 x = ((long)blk_rq_pos(req)) % fs->secpercyl;
348                 fs->head = x / fs->secpertrack;
349                 fs->req_sector = x % fs->secpertrack + 1;
350                 fd_req = req;
351                 fs->state = do_transfer;
352                 fs->retries = 0;
353
354                 act(fs);
355         }
356 }
357
358 static void set_timeout(struct floppy_state *fs, int nticks,
359                         void (*proc)(unsigned long))
360 {
361         unsigned long flags;
362
363         spin_lock_irqsave(&fs->lock, flags);
364         if (fs->timeout_pending)
365                 del_timer(&fs->timeout);
366         fs->timeout.expires = jiffies + nticks;
367         fs->timeout.function = proc;
368         fs->timeout.data = (unsigned long) fs;
369         add_timer(&fs->timeout);
370         fs->timeout_pending = 1;
371         spin_unlock_irqrestore(&fs->lock, flags);
372 }
373
374 static inline void scan_track(struct floppy_state *fs)
375 {
376         struct swim3 __iomem *sw = fs->swim3;
377
378         swim3_select(fs, READ_DATA_0);
379         in_8(&sw->intr);                /* clear SEEN_SECTOR bit */
380         in_8(&sw->error);
381         out_8(&sw->intr_enable, SEEN_SECTOR);
382         out_8(&sw->control_bis, DO_ACTION);
383         /* enable intr when track found */
384         set_timeout(fs, HZ, scan_timeout);      /* enable timeout */
385 }
386
387 static inline void seek_track(struct floppy_state *fs, int n)
388 {
389         struct swim3 __iomem *sw = fs->swim3;
390
391         if (n >= 0) {
392                 swim3_action(fs, SEEK_POSITIVE);
393                 sw->nseek = n;
394         } else {
395                 swim3_action(fs, SEEK_NEGATIVE);
396                 sw->nseek = -n;
397         }
398         fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1;
399         swim3_select(fs, STEP);
400         in_8(&sw->error);
401         /* enable intr when seek finished */
402         out_8(&sw->intr_enable, SEEK_DONE);
403         out_8(&sw->control_bis, DO_SEEK);
404         set_timeout(fs, 3*HZ, seek_timeout);    /* enable timeout */
405         fs->settle_time = 0;
406 }
407
408 static inline void init_dma(struct dbdma_cmd *cp, int cmd,
409                             void *buf, int count)
410 {
411         st_le16(&cp->req_count, count);
412         st_le16(&cp->command, cmd);
413         st_le32(&cp->phy_addr, virt_to_bus(buf));
414         cp->xfer_status = 0;
415 }
416
417 static inline void setup_transfer(struct floppy_state *fs)
418 {
419         int n;
420         struct swim3 __iomem *sw = fs->swim3;
421         struct dbdma_cmd *cp = fs->dma_cmd;
422         struct dbdma_regs __iomem *dr = fs->dma;
423
424         if (blk_rq_cur_sectors(fd_req) <= 0) {
425                 printk(KERN_ERR "swim3: transfer 0 sectors?\n");
426                 return;
427         }
428         if (rq_data_dir(fd_req) == WRITE)
429                 n = 1;
430         else {
431                 n = fs->secpertrack - fs->req_sector + 1;
432                 if (n > blk_rq_cur_sectors(fd_req))
433                         n = blk_rq_cur_sectors(fd_req);
434         }
435         fs->scount = n;
436         swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
437         out_8(&sw->sector, fs->req_sector);
438         out_8(&sw->nsect, n);
439         out_8(&sw->gap3, 0);
440         out_le32(&dr->cmdptr, virt_to_bus(cp));
441         if (rq_data_dir(fd_req) == WRITE) {
442                 /* Set up 3 dma commands: write preamble, data, postamble */
443                 init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));
444                 ++cp;
445                 init_dma(cp, OUTPUT_MORE, fd_req->buffer, 512);
446                 ++cp;
447                 init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble));
448         } else {
449                 init_dma(cp, INPUT_LAST, fd_req->buffer, n * 512);
450         }
451         ++cp;
452         out_le16(&cp->command, DBDMA_STOP);
453         out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
454         in_8(&sw->error);
455         out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
456         if (rq_data_dir(fd_req) == WRITE)
457                 out_8(&sw->control_bis, WRITE_SECTORS);
458         in_8(&sw->intr);
459         out_le32(&dr->control, (RUN << 16) | RUN);
460         /* enable intr when transfer complete */
461         out_8(&sw->intr_enable, TRANSFER_DONE);
462         out_8(&sw->control_bis, DO_ACTION);
463         set_timeout(fs, 2*HZ, xfer_timeout);    /* enable timeout */
464 }
465
466 static void act(struct floppy_state *fs)
467 {
468         for (;;) {
469                 switch (fs->state) {
470                 case idle:
471                         return;         /* XXX shouldn't get here */
472
473                 case locating:
474                         if (swim3_readbit(fs, TRACK_ZERO)) {
475                                 fs->cur_cyl = 0;
476                                 if (fs->req_cyl == 0)
477                                         fs->state = do_transfer;
478                                 else
479                                         fs->state = seeking;
480                                 break;
481                         }
482                         scan_track(fs);
483                         return;
484
485                 case seeking:
486                         if (fs->cur_cyl < 0) {
487                                 fs->expect_cyl = -1;
488                                 fs->state = locating;
489                                 break;
490                         }
491                         if (fs->req_cyl == fs->cur_cyl) {
492                                 printk("whoops, seeking 0\n");
493                                 fs->state = do_transfer;
494                                 break;
495                         }
496                         seek_track(fs, fs->req_cyl - fs->cur_cyl);
497                         return;
498
499                 case settling:
500                         /* check for SEEK_COMPLETE after 30ms */
501                         fs->settle_time = (HZ + 32) / 33;
502                         set_timeout(fs, fs->settle_time, settle_timeout);
503                         return;
504
505                 case do_transfer:
506                         if (fs->cur_cyl != fs->req_cyl) {
507                                 if (fs->retries > 5) {
508                                         __blk_end_request_cur(fd_req, -EIO);
509                                         fs->state = idle;
510                                         return;
511                                 }
512                                 fs->state = seeking;
513                                 break;
514                         }
515                         setup_transfer(fs);
516                         return;
517
518                 case jogging:
519                         seek_track(fs, -5);
520                         return;
521
522                 default:
523                         printk(KERN_ERR"swim3: unknown state %d\n", fs->state);
524                         return;
525                 }
526         }
527 }
528
529 static void scan_timeout(unsigned long data)
530 {
531         struct floppy_state *fs = (struct floppy_state *) data;
532         struct swim3 __iomem *sw = fs->swim3;
533
534         fs->timeout_pending = 0;
535         out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
536         out_8(&sw->select, RELAX);
537         out_8(&sw->intr_enable, 0);
538         fs->cur_cyl = -1;
539         if (fs->retries > 5) {
540                 __blk_end_request_cur(fd_req, -EIO);
541                 fs->state = idle;
542                 start_request(fs);
543         } else {
544                 fs->state = jogging;
545                 act(fs);
546         }
547 }
548
549 static void seek_timeout(unsigned long data)
550 {
551         struct floppy_state *fs = (struct floppy_state *) data;
552         struct swim3 __iomem *sw = fs->swim3;
553
554         fs->timeout_pending = 0;
555         out_8(&sw->control_bic, DO_SEEK);
556         out_8(&sw->select, RELAX);
557         out_8(&sw->intr_enable, 0);
558         printk(KERN_ERR "swim3: seek timeout\n");
559         __blk_end_request_cur(fd_req, -EIO);
560         fs->state = idle;
561         start_request(fs);
562 }
563
564 static void settle_timeout(unsigned long data)
565 {
566         struct floppy_state *fs = (struct floppy_state *) data;
567         struct swim3 __iomem *sw = fs->swim3;
568
569         fs->timeout_pending = 0;
570         if (swim3_readbit(fs, SEEK_COMPLETE)) {
571                 out_8(&sw->select, RELAX);
572                 fs->state = locating;
573                 act(fs);
574                 return;
575         }
576         out_8(&sw->select, RELAX);
577         if (fs->settle_time < 2*HZ) {
578                 ++fs->settle_time;
579                 set_timeout(fs, 1, settle_timeout);
580                 return;
581         }
582         printk(KERN_ERR "swim3: seek settle timeout\n");
583         __blk_end_request_cur(fd_req, -EIO);
584         fs->state = idle;
585         start_request(fs);
586 }
587
588 static void xfer_timeout(unsigned long data)
589 {
590         struct floppy_state *fs = (struct floppy_state *) data;
591         struct swim3 __iomem *sw = fs->swim3;
592         struct dbdma_regs __iomem *dr = fs->dma;
593         int n;
594
595         fs->timeout_pending = 0;
596         out_le32(&dr->control, RUN << 16);
597         /* We must wait a bit for dbdma to stop */
598         for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++)
599                 udelay(1);
600         out_8(&sw->intr_enable, 0);
601         out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
602         out_8(&sw->select, RELAX);
603         printk(KERN_ERR "swim3: timeout %sing sector %ld\n",
604                (rq_data_dir(fd_req)==WRITE? "writ": "read"),
605                (long)blk_rq_pos(fd_req));
606         __blk_end_request_cur(fd_req, -EIO);
607         fs->state = idle;
608         start_request(fs);
609 }
610
611 static irqreturn_t swim3_interrupt(int irq, void *dev_id)
612 {
613         struct floppy_state *fs = (struct floppy_state *) dev_id;
614         struct swim3 __iomem *sw = fs->swim3;
615         int intr, err, n;
616         int stat, resid;
617         struct dbdma_regs __iomem *dr;
618         struct dbdma_cmd *cp;
619
620         intr = in_8(&sw->intr);
621         err = (intr & ERROR_INTR)? in_8(&sw->error): 0;
622         if ((intr & ERROR_INTR) && fs->state != do_transfer)
623                 printk(KERN_ERR "swim3_interrupt, state=%d, dir=%x, intr=%x, err=%x\n",
624                        fs->state, rq_data_dir(fd_req), intr, err);
625         switch (fs->state) {
626         case locating:
627                 if (intr & SEEN_SECTOR) {
628                         out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
629                         out_8(&sw->select, RELAX);
630                         out_8(&sw->intr_enable, 0);
631                         del_timer(&fs->timeout);
632                         fs->timeout_pending = 0;
633                         if (sw->ctrack == 0xff) {
634                                 printk(KERN_ERR "swim3: seen sector but cyl=ff?\n");
635                                 fs->cur_cyl = -1;
636                                 if (fs->retries > 5) {
637                                         __blk_end_request_cur(fd_req, -EIO);
638                                         fs->state = idle;
639                                         start_request(fs);
640                                 } else {
641                                         fs->state = jogging;
642                                         act(fs);
643                                 }
644                                 break;
645                         }
646                         fs->cur_cyl = sw->ctrack;
647                         fs->cur_sector = sw->csect;
648                         if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
649                                 printk(KERN_ERR "swim3: expected cyl %d, got %d\n",
650                                        fs->expect_cyl, fs->cur_cyl);
651                         fs->state = do_transfer;
652                         act(fs);
653                 }
654                 break;
655         case seeking:
656         case jogging:
657                 if (sw->nseek == 0) {
658                         out_8(&sw->control_bic, DO_SEEK);
659                         out_8(&sw->select, RELAX);
660                         out_8(&sw->intr_enable, 0);
661                         del_timer(&fs->timeout);
662                         fs->timeout_pending = 0;
663                         if (fs->state == seeking)
664                                 ++fs->retries;
665                         fs->state = settling;
666                         act(fs);
667                 }
668                 break;
669         case settling:
670                 out_8(&sw->intr_enable, 0);
671                 del_timer(&fs->timeout);
672                 fs->timeout_pending = 0;
673                 act(fs);
674                 break;
675         case do_transfer:
676                 if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
677                         break;
678                 out_8(&sw->intr_enable, 0);
679                 out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
680                 out_8(&sw->select, RELAX);
681                 del_timer(&fs->timeout);
682                 fs->timeout_pending = 0;
683                 dr = fs->dma;
684                 cp = fs->dma_cmd;
685                 if (rq_data_dir(fd_req) == WRITE)
686                         ++cp;
687                 /*
688                  * Check that the main data transfer has finished.
689                  * On writing, the swim3 sometimes doesn't use
690                  * up all the bytes of the postamble, so we can still
691                  * see DMA active here.  That doesn't matter as long
692                  * as all the sector data has been transferred.
693                  */
694                 if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) {
695                         /* wait a little while for DMA to complete */
696                         for (n = 0; n < 100; ++n) {
697                                 if (cp->xfer_status != 0)
698                                         break;
699                                 udelay(1);
700                                 barrier();
701                         }
702                 }
703                 /* turn off DMA */
704                 out_le32(&dr->control, (RUN | PAUSE) << 16);
705                 stat = ld_le16(&cp->xfer_status);
706                 resid = ld_le16(&cp->res_count);
707                 if (intr & ERROR_INTR) {
708                         n = fs->scount - 1 - resid / 512;
709                         if (n > 0) {
710                                 blk_update_request(fd_req, 0, n << 9);
711                                 fs->req_sector += n;
712                         }
713                         if (fs->retries < 5) {
714                                 ++fs->retries;
715                                 act(fs);
716                         } else {
717                                 printk("swim3: error %sing block %ld (err=%x)\n",
718                                        rq_data_dir(fd_req) == WRITE? "writ": "read",
719                                        (long)blk_rq_pos(fd_req), err);
720                                 __blk_end_request_cur(fd_req, -EIO);
721                                 fs->state = idle;
722                         }
723                 } else {
724                         if ((stat & ACTIVE) == 0 || resid != 0) {
725                                 /* musta been an error */
726                                 printk(KERN_ERR "swim3: fd dma: stat=%x resid=%d\n", stat, resid);
727                                 printk(KERN_ERR "  state=%d, dir=%x, intr=%x, err=%x\n",
728                                        fs->state, rq_data_dir(fd_req), intr, err);
729                                 __blk_end_request_cur(fd_req, -EIO);
730                                 fs->state = idle;
731                                 start_request(fs);
732                                 break;
733                         }
734                         if (__blk_end_request(fd_req, 0, fs->scount << 9)) {
735                                 fs->req_sector += fs->scount;
736                                 if (fs->req_sector > fs->secpertrack) {
737                                         fs->req_sector -= fs->secpertrack;
738                                         if (++fs->head > 1) {
739                                                 fs->head = 0;
740                                                 ++fs->req_cyl;
741                                         }
742                                 }
743                                 act(fs);
744                         } else
745                                 fs->state = idle;
746                 }
747                 if (fs->state == idle)
748                         start_request(fs);
749                 break;
750         default:
751                 printk(KERN_ERR "swim3: don't know what to do in state %d\n", fs->state);
752         }
753         return IRQ_HANDLED;
754 }
755
756 /*
757 static void fd_dma_interrupt(int irq, void *dev_id)
758 {
759 }
760 */
761
762 static int grab_drive(struct floppy_state *fs, enum swim_state state,
763                       int interruptible)
764 {
765         unsigned long flags;
766
767         spin_lock_irqsave(&fs->lock, flags);
768         if (fs->state != idle) {
769                 ++fs->wanted;
770                 while (fs->state != available) {
771                         if (interruptible && signal_pending(current)) {
772                                 --fs->wanted;
773                                 spin_unlock_irqrestore(&fs->lock, flags);
774                                 return -EINTR;
775                         }
776                         interruptible_sleep_on(&fs->wait);
777                 }
778                 --fs->wanted;
779         }
780         fs->state = state;
781         spin_unlock_irqrestore(&fs->lock, flags);
782         return 0;
783 }
784
785 static void release_drive(struct floppy_state *fs)
786 {
787         unsigned long flags;
788
789         spin_lock_irqsave(&fs->lock, flags);
790         fs->state = idle;
791         start_request(fs);
792         spin_unlock_irqrestore(&fs->lock, flags);
793 }
794
795 static int fd_eject(struct floppy_state *fs)
796 {
797         int err, n;
798
799         err = grab_drive(fs, ejecting, 1);
800         if (err)
801                 return err;
802         swim3_action(fs, EJECT);
803         for (n = 20; n > 0; --n) {
804                 if (signal_pending(current)) {
805                         err = -EINTR;
806                         break;
807                 }
808                 swim3_select(fs, RELAX);
809                 schedule_timeout_interruptible(1);
810                 if (swim3_readbit(fs, DISK_IN) == 0)
811                         break;
812         }
813         swim3_select(fs, RELAX);
814         udelay(150);
815         fs->ejected = 1;
816         release_drive(fs);
817         return err;
818 }
819
820 static struct floppy_struct floppy_type =
821         { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL };    /*  7 1.44MB 3.5"   */
822
823 static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
824                         unsigned int cmd, unsigned long param)
825 {
826         struct floppy_state *fs = bdev->bd_disk->private_data;
827         int err;
828                 
829         if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
830                 return -EPERM;
831
832 #ifdef CONFIG_PMAC_MEDIABAY
833         if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
834                 return -ENXIO;
835 #endif
836
837         switch (cmd) {
838         case FDEJECT:
839                 if (fs->ref_count != 1)
840                         return -EBUSY;
841                 err = fd_eject(fs);
842                 return err;
843         case FDGETPRM:
844                 if (copy_to_user((void __user *) param, &floppy_type,
845                                  sizeof(struct floppy_struct)))
846                         return -EFAULT;
847                 return 0;
848         }
849         return -ENOTTY;
850 }
851
852 static int floppy_open(struct block_device *bdev, fmode_t mode)
853 {
854         struct floppy_state *fs = bdev->bd_disk->private_data;
855         struct swim3 __iomem *sw = fs->swim3;
856         int n, err = 0;
857
858         if (fs->ref_count == 0) {
859 #ifdef CONFIG_PMAC_MEDIABAY
860                 if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
861                         return -ENXIO;
862 #endif
863                 out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
864                 out_8(&sw->control_bic, 0xff);
865                 out_8(&sw->mode, 0x95);
866                 udelay(10);
867                 out_8(&sw->intr_enable, 0);
868                 out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
869                 swim3_action(fs, MOTOR_ON);
870                 fs->write_prot = -1;
871                 fs->cur_cyl = -1;
872                 for (n = 0; n < 2 * HZ; ++n) {
873                         if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE))
874                                 break;
875                         if (signal_pending(current)) {
876                                 err = -EINTR;
877                                 break;
878                         }
879                         swim3_select(fs, RELAX);
880                         schedule_timeout_interruptible(1);
881                 }
882                 if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
883                                  || swim3_readbit(fs, DISK_IN) == 0))
884                         err = -ENXIO;
885                 swim3_action(fs, SETMFM);
886                 swim3_select(fs, RELAX);
887
888         } else if (fs->ref_count == -1 || mode & FMODE_EXCL)
889                 return -EBUSY;
890
891         if (err == 0 && (mode & FMODE_NDELAY) == 0
892             && (mode & (FMODE_READ|FMODE_WRITE))) {
893                 check_disk_change(bdev);
894                 if (fs->ejected)
895                         err = -ENXIO;
896         }
897
898         if (err == 0 && (mode & FMODE_WRITE)) {
899                 if (fs->write_prot < 0)
900                         fs->write_prot = swim3_readbit(fs, WRITE_PROT);
901                 if (fs->write_prot)
902                         err = -EROFS;
903         }
904
905         if (err) {
906                 if (fs->ref_count == 0) {
907                         swim3_action(fs, MOTOR_OFF);
908                         out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
909                         swim3_select(fs, RELAX);
910                 }
911                 return err;
912         }
913
914         if (mode & FMODE_EXCL)
915                 fs->ref_count = -1;
916         else
917                 ++fs->ref_count;
918
919         return 0;
920 }
921
922 static int floppy_release(struct gendisk *disk, fmode_t mode)
923 {
924         struct floppy_state *fs = disk->private_data;
925         struct swim3 __iomem *sw = fs->swim3;
926         if (fs->ref_count > 0 && --fs->ref_count == 0) {
927                 swim3_action(fs, MOTOR_OFF);
928                 out_8(&sw->control_bic, 0xff);
929                 swim3_select(fs, RELAX);
930         }
931         return 0;
932 }
933
934 static int floppy_check_change(struct gendisk *disk)
935 {
936         struct floppy_state *fs = disk->private_data;
937         return fs->ejected;
938 }
939
940 static int floppy_revalidate(struct gendisk *disk)
941 {
942         struct floppy_state *fs = disk->private_data;
943         struct swim3 __iomem *sw;
944         int ret, n;
945
946 #ifdef CONFIG_PMAC_MEDIABAY
947         if (fs->media_bay && check_media_bay(fs->media_bay, MB_FD))
948                 return -ENXIO;
949 #endif
950
951         sw = fs->swim3;
952         grab_drive(fs, revalidating, 0);
953         out_8(&sw->intr_enable, 0);
954         out_8(&sw->control_bis, DRIVE_ENABLE);
955         swim3_action(fs, MOTOR_ON);     /* necessary? */
956         fs->write_prot = -1;
957         fs->cur_cyl = -1;
958         mdelay(1);
959         for (n = HZ; n > 0; --n) {
960                 if (swim3_readbit(fs, SEEK_COMPLETE))
961                         break;
962                 if (signal_pending(current))
963                         break;
964                 swim3_select(fs, RELAX);
965                 schedule_timeout_interruptible(1);
966         }
967         ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
968                 || swim3_readbit(fs, DISK_IN) == 0;
969         if (ret)
970                 swim3_action(fs, MOTOR_OFF);
971         else {
972                 fs->ejected = 0;
973                 swim3_action(fs, SETMFM);
974         }
975         swim3_select(fs, RELAX);
976
977         release_drive(fs);
978         return ret;
979 }
980
981 static struct block_device_operations floppy_fops = {
982         .open           = floppy_open,
983         .release        = floppy_release,
984         .locked_ioctl   = floppy_ioctl,
985         .media_changed  = floppy_check_change,
986         .revalidate_disk= floppy_revalidate,
987 };
988
989 static int swim3_add_device(struct macio_dev *mdev, int index)
990 {
991         struct device_node *swim = mdev->ofdev.node;
992         struct device_node *mediabay;
993         struct floppy_state *fs = &floppy_states[index];
994         int rc = -EBUSY;
995
996         /* Check & Request resources */
997         if (macio_resource_count(mdev) < 2) {
998                 printk(KERN_WARNING "ifd%d: no address for %s\n",
999                        index, swim->full_name);
1000                 return -ENXIO;
1001         }
1002         if (macio_irq_count(mdev) < 2) {
1003                 printk(KERN_WARNING "fd%d: no intrs for device %s\n",
1004                         index, swim->full_name);
1005         }
1006         if (macio_request_resource(mdev, 0, "swim3 (mmio)")) {
1007                 printk(KERN_ERR "fd%d: can't request mmio resource for %s\n",
1008                        index, swim->full_name);
1009                 return -EBUSY;
1010         }
1011         if (macio_request_resource(mdev, 1, "swim3 (dma)")) {
1012                 printk(KERN_ERR "fd%d: can't request dma resource for %s\n",
1013                        index, swim->full_name);
1014                 macio_release_resource(mdev, 0);
1015                 return -EBUSY;
1016         }
1017         dev_set_drvdata(&mdev->ofdev.dev, fs);
1018
1019         mediabay = (strcasecmp(swim->parent->type, "media-bay") == 0) ?
1020                 swim->parent : NULL;
1021         if (mediabay == NULL)
1022                 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
1023         
1024         memset(fs, 0, sizeof(*fs));
1025         spin_lock_init(&fs->lock);
1026         fs->state = idle;
1027         fs->swim3 = (struct swim3 __iomem *)
1028                 ioremap(macio_resource_start(mdev, 0), 0x200);
1029         if (fs->swim3 == NULL) {
1030                 printk("fd%d: couldn't map registers for %s\n",
1031                        index, swim->full_name);
1032                 rc = -ENOMEM;
1033                 goto out_release;
1034         }
1035         fs->dma = (struct dbdma_regs __iomem *)
1036                 ioremap(macio_resource_start(mdev, 1), 0x200);
1037         if (fs->dma == NULL) {
1038                 printk("fd%d: couldn't map DMA for %s\n",
1039                        index, swim->full_name);
1040                 iounmap(fs->swim3);
1041                 rc = -ENOMEM;
1042                 goto out_release;
1043         }
1044         fs->swim3_intr = macio_irq(mdev, 0);
1045         fs->dma_intr = macio_irq(mdev, 1);;
1046         fs->cur_cyl = -1;
1047         fs->cur_sector = -1;
1048         fs->secpercyl = 36;
1049         fs->secpertrack = 18;
1050         fs->total_secs = 2880;
1051         fs->media_bay = mediabay;
1052         init_waitqueue_head(&fs->wait);
1053
1054         fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
1055         memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
1056         st_le16(&fs->dma_cmd[1].command, DBDMA_STOP);
1057
1058         if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
1059                 printk(KERN_ERR "fd%d: couldn't request irq %d for %s\n",
1060                        index, fs->swim3_intr, swim->full_name);
1061                 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
1062                 goto out_unmap;
1063                 return -EBUSY;
1064         }
1065 /*
1066         if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) {
1067                 printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA",
1068                        fs->dma_intr);
1069                 return -EBUSY;
1070         }
1071 */
1072
1073         init_timer(&fs->timeout);
1074
1075         printk(KERN_INFO "fd%d: SWIM3 floppy controller %s\n", floppy_count,
1076                 mediabay ? "in media bay" : "");
1077
1078         return 0;
1079
1080  out_unmap:
1081         iounmap(fs->dma);
1082         iounmap(fs->swim3);
1083
1084  out_release:
1085         macio_release_resource(mdev, 0);
1086         macio_release_resource(mdev, 1);
1087
1088         return rc;
1089 }
1090
1091 static int __devinit swim3_attach(struct macio_dev *mdev, const struct of_device_id *match)
1092 {
1093         int i, rc;
1094         struct gendisk *disk;
1095
1096         /* Add the drive */
1097         rc = swim3_add_device(mdev, floppy_count);
1098         if (rc)
1099                 return rc;
1100
1101         /* Now create the queue if not there yet */
1102         if (swim3_queue == NULL) {
1103                 /* If we failed, there isn't much we can do as the driver is still
1104                  * too dumb to remove the device, just bail out
1105                  */
1106                 if (register_blkdev(FLOPPY_MAJOR, "fd"))
1107                         return 0;
1108                 swim3_queue = blk_init_queue(do_fd_request, &swim3_lock);
1109                 if (swim3_queue == NULL) {
1110                         unregister_blkdev(FLOPPY_MAJOR, "fd");
1111                         return 0;
1112                 }
1113         }
1114
1115         /* Now register that disk. Same comment about failure handling */
1116         i = floppy_count++;
1117         disk = disks[i] = alloc_disk(1);
1118         if (disk == NULL)
1119                 return 0;
1120
1121         disk->major = FLOPPY_MAJOR;
1122         disk->first_minor = i;
1123         disk->fops = &floppy_fops;
1124         disk->private_data = &floppy_states[i];
1125         disk->queue = swim3_queue;
1126         disk->flags |= GENHD_FL_REMOVABLE;
1127         sprintf(disk->disk_name, "fd%d", i);
1128         set_capacity(disk, 2880);
1129         add_disk(disk);
1130
1131         return 0;
1132 }
1133
1134 static struct of_device_id swim3_match[] =
1135 {
1136         {
1137         .name           = "swim3",
1138         },
1139         {
1140         .compatible     = "ohare-swim3"
1141         },
1142         {
1143         .compatible     = "swim3"
1144         },
1145 };
1146
1147 static struct macio_driver swim3_driver =
1148 {
1149         .name           = "swim3",
1150         .match_table    = swim3_match,
1151         .probe          = swim3_attach,
1152 #if 0
1153         .suspend        = swim3_suspend,
1154         .resume         = swim3_resume,
1155 #endif
1156 };
1157
1158
1159 int swim3_init(void)
1160 {
1161         macio_register_driver(&swim3_driver);
1162         return 0;
1163 }
1164
1165 module_init(swim3_init)
1166
1167 MODULE_LICENSE("GPL");
1168 MODULE_AUTHOR("Paul Mackerras");
1169 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);