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