blob: 2e46815876dfa087bd6f61fef9eee12cdf766512 [file] [log] [blame]
Laurent Vivier8852ecd2008-11-15 16:10:10 +01001/*
2 * Driver for SWIM (Sander Woz Integrated Machine) floppy controller
3 *
4 * Copyright (C) 2004,2008 Laurent Vivier <Laurent@lvivier.info>
5 *
6 * based on Alastair Bridgewater SWIM analysis, 2001
7 * based on SWIM3 driver (c) Paul Mackerras, 1996
8 * based on netBSD IWM driver (c) 1997, 1998 Hauke Fath.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * 2004-08-21 (lv) - Initial implementation
16 * 2008-10-30 (lv) - Port to 2.6
17 */
18
19#include <linux/module.h>
20#include <linux/fd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Laurent Vivier8852ecd2008-11-15 16:10:10 +010022#include <linux/blkdev.h>
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +020023#include <linux/smp_lock.h>
Laurent Vivier8852ecd2008-11-15 16:10:10 +010024#include <linux/hdreg.h>
25#include <linux/kernel.h>
26#include <linux/delay.h>
27#include <linux/platform_device.h>
28
29#include <asm/macintosh.h>
30#include <asm/mac_via.h>
31
32#define CARDNAME "swim"
33
34struct sector_header {
35 unsigned char side;
36 unsigned char track;
37 unsigned char sector;
38 unsigned char size;
39 unsigned char crc0;
40 unsigned char crc1;
41} __attribute__((packed));
42
43#define DRIVER_VERSION "Version 0.2 (2008-10-30)"
44
45#define REG(x) unsigned char x, x ## _pad[0x200 - 1];
46
47struct swim {
48 REG(write_data)
49 REG(write_mark)
50 REG(write_CRC)
51 REG(write_parameter)
52 REG(write_phase)
53 REG(write_setup)
54 REG(write_mode0)
55 REG(write_mode1)
56
57 REG(read_data)
58 REG(read_mark)
59 REG(read_error)
60 REG(read_parameter)
61 REG(read_phase)
62 REG(read_setup)
63 REG(read_status)
64 REG(read_handshake)
65} __attribute__((packed));
66
67#define swim_write(base, reg, v) out_8(&(base)->write_##reg, (v))
68#define swim_read(base, reg) in_8(&(base)->read_##reg)
69
70/* IWM registers */
71
72struct iwm {
73 REG(ph0L)
74 REG(ph0H)
75 REG(ph1L)
76 REG(ph1H)
77 REG(ph2L)
78 REG(ph2H)
79 REG(ph3L)
80 REG(ph3H)
81 REG(mtrOff)
82 REG(mtrOn)
83 REG(intDrive)
84 REG(extDrive)
85 REG(q6L)
86 REG(q6H)
87 REG(q7L)
88 REG(q7H)
89} __attribute__((packed));
90
91#define iwm_write(base, reg, v) out_8(&(base)->reg, (v))
92#define iwm_read(base, reg) in_8(&(base)->reg)
93
94/* bits in phase register */
95
96#define SEEK_POSITIVE 0x070
97#define SEEK_NEGATIVE 0x074
98#define STEP 0x071
99#define MOTOR_ON 0x072
100#define MOTOR_OFF 0x076
101#define INDEX 0x073
102#define EJECT 0x077
103#define SETMFM 0x171
104#define SETGCR 0x175
105
106#define RELAX 0x033
107#define LSTRB 0x008
108
109#define CA_MASK 0x077
110
111/* Select values for swim_select and swim_readbit */
112
113#define READ_DATA_0 0x074
114#define TWOMEG_DRIVE 0x075
115#define SINGLE_SIDED 0x076
116#define DRIVE_PRESENT 0x077
117#define DISK_IN 0x170
118#define WRITE_PROT 0x171
119#define TRACK_ZERO 0x172
120#define TACHO 0x173
121#define READ_DATA_1 0x174
122#define MFM_MODE 0x175
123#define SEEK_COMPLETE 0x176
124#define ONEMEG_MEDIA 0x177
125
126/* Bits in handshake register */
127
128#define MARK_BYTE 0x01
129#define CRC_ZERO 0x02
130#define RDDATA 0x04
131#define SENSE 0x08
132#define MOTEN 0x10
133#define ERROR 0x20
134#define DAT2BYTE 0x40
135#define DAT1BYTE 0x80
136
137/* bits in setup register */
138
139#define S_INV_WDATA 0x01
140#define S_3_5_SELECT 0x02
141#define S_GCR 0x04
142#define S_FCLK_DIV2 0x08
143#define S_ERROR_CORR 0x10
144#define S_IBM_DRIVE 0x20
145#define S_GCR_WRITE 0x40
146#define S_TIMEOUT 0x80
147
148/* bits in mode register */
149
150#define CLFIFO 0x01
151#define ENBL1 0x02
152#define ENBL2 0x04
153#define ACTION 0x08
154#define WRITE_MODE 0x10
155#define HEDSEL 0x20
156#define MOTON 0x80
157
158/*----------------------------------------------------------------------------*/
159
160enum drive_location {
161 INTERNAL_DRIVE = 0x02,
162 EXTERNAL_DRIVE = 0x04,
163};
164
165enum media_type {
166 DD_MEDIA,
167 HD_MEDIA,
168};
169
170struct floppy_state {
171
172 /* physical properties */
173
174 enum drive_location location; /* internal or external drive */
175 int head_number; /* single- or double-sided drive */
176
177 /* media */
178
179 int disk_in;
180 int ejected;
181 enum media_type type;
182 int write_protected;
183
184 int total_secs;
185 int secpercyl;
186 int secpertrack;
187
188 /* in-use information */
189
190 int track;
191 int ref_count;
192
193 struct gendisk *disk;
194
195 /* parent controller */
196
197 struct swim_priv *swd;
198};
199
200enum motor_action {
201 OFF,
202 ON,
203};
204
205enum head {
206 LOWER_HEAD = 0,
207 UPPER_HEAD = 1,
208};
209
210#define FD_MAX_UNIT 2
211
212struct swim_priv {
213 struct swim __iomem *base;
214 spinlock_t lock;
215 struct request_queue *queue;
216 int floppy_count;
217 struct floppy_state unit[FD_MAX_UNIT];
218};
219
220extern int swim_read_sector_header(struct swim __iomem *base,
221 struct sector_header *header);
222extern int swim_read_sector_data(struct swim __iomem *base,
223 unsigned char *data);
224
225static inline void set_swim_mode(struct swim __iomem *base, int enable)
226{
227 struct iwm __iomem *iwm_base;
228 unsigned long flags;
229
230 if (!enable) {
231 swim_write(base, mode0, 0xf8);
232 return;
233 }
234
235 iwm_base = (struct iwm __iomem *)base;
236 local_irq_save(flags);
237
238 iwm_read(iwm_base, q7L);
239 iwm_read(iwm_base, mtrOff);
240 iwm_read(iwm_base, q6H);
241
242 iwm_write(iwm_base, q7H, 0x57);
243 iwm_write(iwm_base, q7H, 0x17);
244 iwm_write(iwm_base, q7H, 0x57);
245 iwm_write(iwm_base, q7H, 0x57);
246
247 local_irq_restore(flags);
248}
249
250static inline int get_swim_mode(struct swim __iomem *base)
251{
252 unsigned long flags;
253
254 local_irq_save(flags);
255
256 swim_write(base, phase, 0xf5);
257 if (swim_read(base, phase) != 0xf5)
258 goto is_iwm;
259 swim_write(base, phase, 0xf6);
260 if (swim_read(base, phase) != 0xf6)
261 goto is_iwm;
262 swim_write(base, phase, 0xf7);
263 if (swim_read(base, phase) != 0xf7)
264 goto is_iwm;
265 local_irq_restore(flags);
266 return 1;
267is_iwm:
268 local_irq_restore(flags);
269 return 0;
270}
271
272static inline void swim_select(struct swim __iomem *base, int sel)
273{
274 swim_write(base, phase, RELAX);
275
276 via1_set_head(sel & 0x100);
277
278 swim_write(base, phase, sel & CA_MASK);
279}
280
281static inline void swim_action(struct swim __iomem *base, int action)
282{
283 unsigned long flags;
284
285 local_irq_save(flags);
286
287 swim_select(base, action);
288 udelay(1);
289 swim_write(base, phase, (LSTRB<<4) | LSTRB);
290 udelay(1);
291 swim_write(base, phase, (LSTRB<<4) | ((~LSTRB) & 0x0F));
292 udelay(1);
293
294 local_irq_restore(flags);
295}
296
297static inline int swim_readbit(struct swim __iomem *base, int bit)
298{
299 int stat;
300
301 swim_select(base, bit);
302
303 udelay(10);
304
305 stat = swim_read(base, handshake);
306
307 return (stat & SENSE) == 0;
308}
309
310static inline void swim_drive(struct swim __iomem *base,
311 enum drive_location location)
312{
313 if (location == INTERNAL_DRIVE) {
314 swim_write(base, mode0, EXTERNAL_DRIVE); /* clear drive 1 bit */
315 swim_write(base, mode1, INTERNAL_DRIVE); /* set drive 0 bit */
316 } else if (location == EXTERNAL_DRIVE) {
317 swim_write(base, mode0, INTERNAL_DRIVE); /* clear drive 0 bit */
318 swim_write(base, mode1, EXTERNAL_DRIVE); /* set drive 1 bit */
319 }
320}
321
322static inline void swim_motor(struct swim __iomem *base,
323 enum motor_action action)
324{
325 if (action == ON) {
326 int i;
327
328 swim_action(base, MOTOR_ON);
329
330 for (i = 0; i < 2*HZ; i++) {
331 swim_select(base, RELAX);
332 if (swim_readbit(base, MOTOR_ON))
333 break;
334 current->state = TASK_INTERRUPTIBLE;
335 schedule_timeout(1);
336 }
337 } else if (action == OFF) {
338 swim_action(base, MOTOR_OFF);
339 swim_select(base, RELAX);
340 }
341}
342
343static inline void swim_eject(struct swim __iomem *base)
344{
345 int i;
346
347 swim_action(base, EJECT);
348
349 for (i = 0; i < 2*HZ; i++) {
350 swim_select(base, RELAX);
351 if (!swim_readbit(base, DISK_IN))
352 break;
353 current->state = TASK_INTERRUPTIBLE;
354 schedule_timeout(1);
355 }
356 swim_select(base, RELAX);
357}
358
359static inline void swim_head(struct swim __iomem *base, enum head head)
360{
361 /* wait drive is ready */
362
363 if (head == UPPER_HEAD)
364 swim_select(base, READ_DATA_1);
365 else if (head == LOWER_HEAD)
366 swim_select(base, READ_DATA_0);
367}
368
369static inline int swim_step(struct swim __iomem *base)
370{
371 int wait;
372
373 swim_action(base, STEP);
374
375 for (wait = 0; wait < HZ; wait++) {
376
377 current->state = TASK_INTERRUPTIBLE;
378 schedule_timeout(1);
379
380 swim_select(base, RELAX);
381 if (!swim_readbit(base, STEP))
382 return 0;
383 }
384 return -1;
385}
386
387static inline int swim_track00(struct swim __iomem *base)
388{
389 int try;
390
391 swim_action(base, SEEK_NEGATIVE);
392
393 for (try = 0; try < 100; try++) {
394
395 swim_select(base, RELAX);
396 if (swim_readbit(base, TRACK_ZERO))
397 break;
398
399 if (swim_step(base))
400 return -1;
401 }
402
403 if (swim_readbit(base, TRACK_ZERO))
404 return 0;
405
406 return -1;
407}
408
409static inline int swim_seek(struct swim __iomem *base, int step)
410{
411 if (step == 0)
412 return 0;
413
414 if (step < 0) {
415 swim_action(base, SEEK_NEGATIVE);
416 step = -step;
417 } else
418 swim_action(base, SEEK_POSITIVE);
419
420 for ( ; step > 0; step--) {
421 if (swim_step(base))
422 return -1;
423 }
424
425 return 0;
426}
427
428static inline int swim_track(struct floppy_state *fs, int track)
429{
430 struct swim __iomem *base = fs->swd->base;
431 int ret;
432
433 ret = swim_seek(base, track - fs->track);
434
435 if (ret == 0)
436 fs->track = track;
437 else {
438 swim_track00(base);
439 fs->track = 0;
440 }
441
442 return ret;
443}
444
445static int floppy_eject(struct floppy_state *fs)
446{
447 struct swim __iomem *base = fs->swd->base;
448
449 swim_drive(base, fs->location);
450 swim_motor(base, OFF);
451 swim_eject(base);
452
453 fs->disk_in = 0;
454 fs->ejected = 1;
455
456 return 0;
457}
458
459static inline int swim_read_sector(struct floppy_state *fs,
460 int side, int track,
461 int sector, unsigned char *buffer)
462{
463 struct swim __iomem *base = fs->swd->base;
464 unsigned long flags;
465 struct sector_header header;
466 int ret = -1;
467 short i;
468
469 swim_track(fs, track);
470
471 swim_write(base, mode1, MOTON);
472 swim_head(base, side);
473 swim_write(base, mode0, side);
474
475 local_irq_save(flags);
476 for (i = 0; i < 36; i++) {
477 ret = swim_read_sector_header(base, &header);
478 if (!ret && (header.sector == sector)) {
479 /* found */
480
481 ret = swim_read_sector_data(base, buffer);
482 break;
483 }
484 }
485 local_irq_restore(flags);
486
487 swim_write(base, mode0, MOTON);
488
489 if ((header.side != side) || (header.track != track) ||
490 (header.sector != sector))
491 return 0;
492
493 return ret;
494}
495
496static int floppy_read_sectors(struct floppy_state *fs,
497 int req_sector, int sectors_nb,
498 unsigned char *buffer)
499{
500 struct swim __iomem *base = fs->swd->base;
501 int ret;
502 int side, track, sector;
503 int i, try;
504
505
506 swim_drive(base, fs->location);
507 for (i = req_sector; i < req_sector + sectors_nb; i++) {
508 int x;
509 track = i / fs->secpercyl;
510 x = i % fs->secpercyl;
511 side = x / fs->secpertrack;
512 sector = x % fs->secpertrack + 1;
513
514 try = 5;
515 do {
516 ret = swim_read_sector(fs, side, track, sector,
517 buffer);
518 if (try-- == 0)
Tejun Heo06b06082009-05-08 11:54:09 +0900519 return -EIO;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100520 } while (ret != 512);
521
522 buffer += ret;
523 }
524
525 return 0;
526}
527
528static void redo_fd_request(struct request_queue *q)
529{
530 struct request *req;
531 struct floppy_state *fs;
532
Tejun Heo9934c8c2009-05-08 11:54:16 +0900533 req = blk_fetch_request(q);
Tejun Heo06b06082009-05-08 11:54:09 +0900534 while (req) {
535 int err = -EIO;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100536
537 fs = req->rq_disk->private_data;
Tejun Heo06b06082009-05-08 11:54:09 +0900538 if (blk_rq_pos(req) >= fs->total_secs)
539 goto done;
540 if (!fs->disk_in)
541 goto done;
542 if (rq_data_dir(req) == WRITE && fs->write_protected)
543 goto done;
544
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100545 switch (rq_data_dir(req)) {
546 case WRITE:
547 /* NOT IMPLEMENTED */
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100548 break;
549 case READ:
Tejun Heo06b06082009-05-08 11:54:09 +0900550 err = floppy_read_sectors(fs, blk_rq_pos(req),
551 blk_rq_cur_sectors(req),
552 req->buffer);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100553 break;
554 }
Tejun Heo06b06082009-05-08 11:54:09 +0900555 done:
Tejun Heo9934c8c2009-05-08 11:54:16 +0900556 if (!__blk_end_request_cur(req, err))
557 req = blk_fetch_request(q);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100558 }
559}
560
561static void do_fd_request(struct request_queue *q)
562{
563 redo_fd_request(q);
564}
565
566static struct floppy_struct floppy_type[4] = {
567 { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL }, /* no testing */
568 { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 360KB SS 3.5"*/
569 { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 720KB 3.5" */
570 { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL }, /* 1.44MB 3.5" */
571};
572
573static int get_floppy_geometry(struct floppy_state *fs, int type,
574 struct floppy_struct **g)
575{
576 if (type >= ARRAY_SIZE(floppy_type))
577 return -EINVAL;
578
579 if (type)
580 *g = &floppy_type[type];
581 else if (fs->type == HD_MEDIA) /* High-Density media */
582 *g = &floppy_type[3];
583 else if (fs->head_number == 2) /* double-sided */
584 *g = &floppy_type[2];
585 else
586 *g = &floppy_type[1];
587
588 return 0;
589}
590
591static void setup_medium(struct floppy_state *fs)
592{
593 struct swim __iomem *base = fs->swd->base;
594
595 if (swim_readbit(base, DISK_IN)) {
596 struct floppy_struct *g;
597 fs->disk_in = 1;
598 fs->write_protected = swim_readbit(base, WRITE_PROT);
599 fs->type = swim_readbit(base, ONEMEG_MEDIA);
600
601 if (swim_track00(base))
602 printk(KERN_ERR
603 "SWIM: cannot move floppy head to track 0\n");
604
605 swim_track00(base);
606
607 get_floppy_geometry(fs, 0, &g);
608 fs->total_secs = g->size;
609 fs->secpercyl = g->head * g->sect;
610 fs->secpertrack = g->sect;
611 fs->track = 0;
612 } else {
613 fs->disk_in = 0;
614 }
615}
616
617static int floppy_open(struct block_device *bdev, fmode_t mode)
618{
619 struct floppy_state *fs = bdev->bd_disk->private_data;
620 struct swim __iomem *base = fs->swd->base;
621 int err;
622
623 if (fs->ref_count == -1 || (fs->ref_count && mode & FMODE_EXCL))
624 return -EBUSY;
625
626 if (mode & FMODE_EXCL)
627 fs->ref_count = -1;
628 else
629 fs->ref_count++;
630
631 swim_write(base, setup, S_IBM_DRIVE | S_FCLK_DIV2);
632 udelay(10);
633 swim_drive(base, INTERNAL_DRIVE);
634 swim_motor(base, ON);
635 swim_action(base, SETMFM);
636 if (fs->ejected)
637 setup_medium(fs);
638 if (!fs->disk_in) {
639 err = -ENXIO;
640 goto out;
641 }
642
643 if (mode & FMODE_NDELAY)
644 return 0;
645
646 if (mode & (FMODE_READ|FMODE_WRITE)) {
647 check_disk_change(bdev);
648 if ((mode & FMODE_WRITE) && fs->write_protected) {
649 err = -EROFS;
650 goto out;
651 }
652 }
653 return 0;
654out:
655 if (fs->ref_count < 0)
656 fs->ref_count = 0;
657 else if (fs->ref_count > 0)
658 --fs->ref_count;
659
660 if (fs->ref_count == 0)
661 swim_motor(base, OFF);
662 return err;
663}
664
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200665static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
666{
667 int ret;
668
669 lock_kernel();
670 ret = floppy_open(bdev, mode);
671 unlock_kernel();
672
673 return ret;
674}
675
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100676static int floppy_release(struct gendisk *disk, fmode_t mode)
677{
678 struct floppy_state *fs = disk->private_data;
679 struct swim __iomem *base = fs->swd->base;
680
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200681 lock_kernel();
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100682 if (fs->ref_count < 0)
683 fs->ref_count = 0;
684 else if (fs->ref_count > 0)
685 --fs->ref_count;
686
687 if (fs->ref_count == 0)
688 swim_motor(base, OFF);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200689 unlock_kernel();
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100690
691 return 0;
692}
693
694static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
695 unsigned int cmd, unsigned long param)
696{
697 struct floppy_state *fs = bdev->bd_disk->private_data;
698 int err;
699
700 if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
701 return -EPERM;
702
703 switch (cmd) {
704 case FDEJECT:
705 if (fs->ref_count != 1)
706 return -EBUSY;
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200707 lock_kernel();
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100708 err = floppy_eject(fs);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200709 unlock_kernel();
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100710 return err;
711
712 case FDGETPRM:
713 if (copy_to_user((void __user *) param, (void *) &floppy_type,
714 sizeof(struct floppy_struct)))
715 return -EFAULT;
716 break;
717
718 default:
719 printk(KERN_DEBUG "SWIM floppy_ioctl: unknown cmd %d\n",
720 cmd);
721 return -ENOSYS;
722 }
723 return 0;
724}
725
726static int floppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
727{
728 struct floppy_state *fs = bdev->bd_disk->private_data;
729 struct floppy_struct *g;
730 int ret;
731
732 ret = get_floppy_geometry(fs, 0, &g);
733 if (ret)
734 return ret;
735
736 geo->heads = g->head;
737 geo->sectors = g->sect;
738 geo->cylinders = g->track;
739
740 return 0;
741}
742
743static int floppy_check_change(struct gendisk *disk)
744{
745 struct floppy_state *fs = disk->private_data;
746
747 return fs->ejected;
748}
749
750static int floppy_revalidate(struct gendisk *disk)
751{
752 struct floppy_state *fs = disk->private_data;
753 struct swim __iomem *base = fs->swd->base;
754
755 swim_drive(base, fs->location);
756
757 if (fs->ejected)
758 setup_medium(fs);
759
760 if (!fs->disk_in)
761 swim_motor(base, OFF);
762 else
763 fs->ejected = 0;
764
765 return !fs->disk_in;
766}
767
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700768static const struct block_device_operations floppy_fops = {
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100769 .owner = THIS_MODULE,
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200770 .open = floppy_unlocked_open,
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100771 .release = floppy_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200772 .ioctl = floppy_ioctl,
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100773 .getgeo = floppy_getgeo,
774 .media_changed = floppy_check_change,
775 .revalidate_disk = floppy_revalidate,
776};
777
778static struct kobject *floppy_find(dev_t dev, int *part, void *data)
779{
780 struct swim_priv *swd = data;
781 int drive = (*part & 3);
782
783 if (drive > swd->floppy_count)
784 return NULL;
785
786 *part = 0;
787 return get_disk(swd->unit[drive].disk);
788}
789
790static int __devinit swim_add_floppy(struct swim_priv *swd,
791 enum drive_location location)
792{
793 struct floppy_state *fs = &swd->unit[swd->floppy_count];
794 struct swim __iomem *base = swd->base;
795
796 fs->location = location;
797
798 swim_drive(base, location);
799
800 swim_motor(base, OFF);
801
802 if (swim_readbit(base, SINGLE_SIDED))
803 fs->head_number = 1;
804 else
805 fs->head_number = 2;
806 fs->ref_count = 0;
807 fs->ejected = 1;
808
809 swd->floppy_count++;
810
811 return 0;
812}
813
814static int __devinit swim_floppy_init(struct swim_priv *swd)
815{
816 int err;
817 int drive;
818 struct swim __iomem *base = swd->base;
819
820 /* scan floppy drives */
821
822 swim_drive(base, INTERNAL_DRIVE);
823 if (swim_readbit(base, DRIVE_PRESENT))
824 swim_add_floppy(swd, INTERNAL_DRIVE);
825 swim_drive(base, EXTERNAL_DRIVE);
826 if (swim_readbit(base, DRIVE_PRESENT))
827 swim_add_floppy(swd, EXTERNAL_DRIVE);
828
829 /* register floppy drives */
830
831 err = register_blkdev(FLOPPY_MAJOR, "fd");
832 if (err) {
833 printk(KERN_ERR "Unable to get major %d for SWIM floppy\n",
834 FLOPPY_MAJOR);
835 return -EBUSY;
836 }
837
838 for (drive = 0; drive < swd->floppy_count; drive++) {
839 swd->unit[drive].disk = alloc_disk(1);
840 if (swd->unit[drive].disk == NULL) {
841 err = -ENOMEM;
842 goto exit_put_disks;
843 }
844 swd->unit[drive].swd = swd;
845 }
846
847 swd->queue = blk_init_queue(do_fd_request, &swd->lock);
848 if (!swd->queue) {
849 err = -ENOMEM;
850 goto exit_put_disks;
851 }
852
853 for (drive = 0; drive < swd->floppy_count; drive++) {
854 swd->unit[drive].disk->flags = GENHD_FL_REMOVABLE;
855 swd->unit[drive].disk->major = FLOPPY_MAJOR;
856 swd->unit[drive].disk->first_minor = drive;
857 sprintf(swd->unit[drive].disk->disk_name, "fd%d", drive);
858 swd->unit[drive].disk->fops = &floppy_fops;
859 swd->unit[drive].disk->private_data = &swd->unit[drive];
860 swd->unit[drive].disk->queue = swd->queue;
861 set_capacity(swd->unit[drive].disk, 2880);
862 add_disk(swd->unit[drive].disk);
863 }
864
865 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
866 floppy_find, NULL, swd);
867
868 return 0;
869
870exit_put_disks:
871 unregister_blkdev(FLOPPY_MAJOR, "fd");
872 while (drive--)
873 put_disk(swd->unit[drive].disk);
874 return err;
875}
876
877static int __devinit swim_probe(struct platform_device *dev)
878{
879 struct resource *res;
880 struct swim __iomem *swim_base;
881 struct swim_priv *swd;
882 int ret;
883
Finn Thain2724daf2009-11-04 00:39:56 +1100884 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100885 if (!res) {
886 ret = -ENODEV;
887 goto out;
888 }
889
890 if (!request_mem_region(res->start, resource_size(res), CARDNAME)) {
891 ret = -EBUSY;
892 goto out;
893 }
894
895 swim_base = ioremap(res->start, resource_size(res));
896 if (!swim_base) {
897 return -ENOMEM;
898 goto out_release_io;
899 }
900
901 /* probe device */
902
903 set_swim_mode(swim_base, 1);
904 if (!get_swim_mode(swim_base)) {
905 printk(KERN_INFO "SWIM device not found !\n");
906 ret = -ENODEV;
907 goto out_iounmap;
908 }
909
910 /* set platform driver data */
911
912 swd = kzalloc(sizeof(struct swim_priv), GFP_KERNEL);
913 if (!swd) {
914 ret = -ENOMEM;
915 goto out_iounmap;
916 }
917 platform_set_drvdata(dev, swd);
918
919 swd->base = swim_base;
920
921 ret = swim_floppy_init(swd);
922 if (ret)
923 goto out_kfree;
924
925 return 0;
926
927out_kfree:
928 platform_set_drvdata(dev, NULL);
929 kfree(swd);
930out_iounmap:
931 iounmap(swim_base);
932out_release_io:
933 release_mem_region(res->start, resource_size(res));
934out:
935 return ret;
936}
937
938static int __devexit swim_remove(struct platform_device *dev)
939{
940 struct swim_priv *swd = platform_get_drvdata(dev);
941 int drive;
942 struct resource *res;
943
944 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
945
946 for (drive = 0; drive < swd->floppy_count; drive++) {
947 del_gendisk(swd->unit[drive].disk);
948 put_disk(swd->unit[drive].disk);
949 }
950
951 unregister_blkdev(FLOPPY_MAJOR, "fd");
952
953 blk_cleanup_queue(swd->queue);
954
955 /* eject floppies */
956
957 for (drive = 0; drive < swd->floppy_count; drive++)
958 floppy_eject(&swd->unit[drive]);
959
960 iounmap(swd->base);
961
Finn Thain2724daf2009-11-04 00:39:56 +1100962 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100963 if (res)
964 release_mem_region(res->start, resource_size(res));
965
966 platform_set_drvdata(dev, NULL);
967 kfree(swd);
968
969 return 0;
970}
971
972static struct platform_driver swim_driver = {
973 .probe = swim_probe,
974 .remove = __devexit_p(swim_remove),
975 .driver = {
976 .name = CARDNAME,
977 .owner = THIS_MODULE,
978 },
979};
980
981static int __init swim_init(void)
982{
983 printk(KERN_INFO "SWIM floppy driver %s\n", DRIVER_VERSION);
984
985 return platform_driver_register(&swim_driver);
986}
987module_init(swim_init);
988
989static void __exit swim_exit(void)
990{
991 platform_driver_unregister(&swim_driver);
992}
993module_exit(swim_exit);
994
995MODULE_DESCRIPTION("Driver for SWIM floppy controller");
996MODULE_LICENSE("GPL");
997MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
998MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);