2 * Driver for SWIM (Sander Woz Integrated Machine) floppy controller
4 * Copyright (C) 2004,2008 Laurent Vivier <Laurent@lvivier.info>
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.
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.
15 * 2004-08-21 (lv) - Initial implementation
16 * 2008-10-30 (lv) - Port to 2.6
19 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/blkdev.h>
23 #include <linux/mutex.h>
24 #include <linux/hdreg.h>
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/platform_device.h>
29 #include <asm/macintosh.h>
30 #include <asm/mac_via.h>
32 #define CARDNAME "swim"
34 struct sector_header
{
41 } __attribute__((packed
));
43 #define DRIVER_VERSION "Version 0.2 (2008-10-30)"
45 #define REG(x) unsigned char x, x ## _pad[0x200 - 1];
65 } __attribute__((packed
));
67 #define swim_write(base, reg, v) out_8(&(base)->write_##reg, (v))
68 #define swim_read(base, reg) in_8(&(base)->read_##reg)
89 } __attribute__((packed
));
91 #define iwm_write(base, reg, v) out_8(&(base)->reg, (v))
92 #define iwm_read(base, reg) in_8(&(base)->reg)
94 /* bits in phase register */
96 #define SEEK_POSITIVE 0x070
97 #define SEEK_NEGATIVE 0x074
99 #define MOTOR_ON 0x072
100 #define MOTOR_OFF 0x076
109 #define CA_MASK 0x077
111 /* Select values for swim_select and swim_readbit */
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
121 #define READ_DATA_1 0x174
122 #define MFM_MODE 0x175
123 #define SEEK_COMPLETE 0x176
124 #define ONEMEG_MEDIA 0x177
126 /* Bits in handshake register */
128 #define MARK_BYTE 0x01
129 #define CRC_ZERO 0x02
134 #define DAT2BYTE 0x40
135 #define DAT1BYTE 0x80
137 /* bits in setup register */
139 #define S_INV_WDATA 0x01
140 #define S_3_5_SELECT 0x02
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
148 /* bits in mode register */
154 #define WRITE_MODE 0x10
158 /*----------------------------------------------------------------------------*/
160 enum drive_location
{
161 INTERNAL_DRIVE
= 0x02,
162 EXTERNAL_DRIVE
= 0x04,
170 struct floppy_state
{
172 /* physical properties */
174 enum drive_location location
; /* internal or external drive */
175 int head_number
; /* single- or double-sided drive */
181 enum media_type type
;
188 /* in-use information */
193 struct gendisk
*disk
;
195 /* parent controller */
197 struct swim_priv
*swd
;
210 #define FD_MAX_UNIT 2
213 struct swim __iomem
*base
;
215 struct request_queue
*queue
;
217 struct floppy_state unit
[FD_MAX_UNIT
];
220 extern int swim_read_sector_header(struct swim __iomem
*base
,
221 struct sector_header
*header
);
222 extern int swim_read_sector_data(struct swim __iomem
*base
,
223 unsigned char *data
);
225 static DEFINE_MUTEX(swim_mutex
);
226 static inline void set_swim_mode(struct swim __iomem
*base
, int enable
)
228 struct iwm __iomem
*iwm_base
;
232 swim_write(base
, mode0
, 0xf8);
236 iwm_base
= (struct iwm __iomem
*)base
;
237 local_irq_save(flags
);
239 iwm_read(iwm_base
, q7L
);
240 iwm_read(iwm_base
, mtrOff
);
241 iwm_read(iwm_base
, q6H
);
243 iwm_write(iwm_base
, q7H
, 0x57);
244 iwm_write(iwm_base
, q7H
, 0x17);
245 iwm_write(iwm_base
, q7H
, 0x57);
246 iwm_write(iwm_base
, q7H
, 0x57);
248 local_irq_restore(flags
);
251 static inline int get_swim_mode(struct swim __iomem
*base
)
255 local_irq_save(flags
);
257 swim_write(base
, phase
, 0xf5);
258 if (swim_read(base
, phase
) != 0xf5)
260 swim_write(base
, phase
, 0xf6);
261 if (swim_read(base
, phase
) != 0xf6)
263 swim_write(base
, phase
, 0xf7);
264 if (swim_read(base
, phase
) != 0xf7)
266 local_irq_restore(flags
);
269 local_irq_restore(flags
);
273 static inline void swim_select(struct swim __iomem
*base
, int sel
)
275 swim_write(base
, phase
, RELAX
);
277 via1_set_head(sel
& 0x100);
279 swim_write(base
, phase
, sel
& CA_MASK
);
282 static inline void swim_action(struct swim __iomem
*base
, int action
)
286 local_irq_save(flags
);
288 swim_select(base
, action
);
290 swim_write(base
, phase
, (LSTRB
<<4) | LSTRB
);
292 swim_write(base
, phase
, (LSTRB
<<4) | ((~LSTRB
) & 0x0F));
295 local_irq_restore(flags
);
298 static inline int swim_readbit(struct swim __iomem
*base
, int bit
)
302 swim_select(base
, bit
);
306 stat
= swim_read(base
, handshake
);
308 return (stat
& SENSE
) == 0;
311 static inline void swim_drive(struct swim __iomem
*base
,
312 enum drive_location location
)
314 if (location
== INTERNAL_DRIVE
) {
315 swim_write(base
, mode0
, EXTERNAL_DRIVE
); /* clear drive 1 bit */
316 swim_write(base
, mode1
, INTERNAL_DRIVE
); /* set drive 0 bit */
317 } else if (location
== EXTERNAL_DRIVE
) {
318 swim_write(base
, mode0
, INTERNAL_DRIVE
); /* clear drive 0 bit */
319 swim_write(base
, mode1
, EXTERNAL_DRIVE
); /* set drive 1 bit */
323 static inline void swim_motor(struct swim __iomem
*base
,
324 enum motor_action action
)
329 swim_action(base
, MOTOR_ON
);
331 for (i
= 0; i
< 2*HZ
; i
++) {
332 swim_select(base
, RELAX
);
333 if (swim_readbit(base
, MOTOR_ON
))
335 current
->state
= TASK_INTERRUPTIBLE
;
338 } else if (action
== OFF
) {
339 swim_action(base
, MOTOR_OFF
);
340 swim_select(base
, RELAX
);
344 static inline void swim_eject(struct swim __iomem
*base
)
348 swim_action(base
, EJECT
);
350 for (i
= 0; i
< 2*HZ
; i
++) {
351 swim_select(base
, RELAX
);
352 if (!swim_readbit(base
, DISK_IN
))
354 current
->state
= TASK_INTERRUPTIBLE
;
357 swim_select(base
, RELAX
);
360 static inline void swim_head(struct swim __iomem
*base
, enum head head
)
362 /* wait drive is ready */
364 if (head
== UPPER_HEAD
)
365 swim_select(base
, READ_DATA_1
);
366 else if (head
== LOWER_HEAD
)
367 swim_select(base
, READ_DATA_0
);
370 static inline int swim_step(struct swim __iomem
*base
)
374 swim_action(base
, STEP
);
376 for (wait
= 0; wait
< HZ
; wait
++) {
378 current
->state
= TASK_INTERRUPTIBLE
;
381 swim_select(base
, RELAX
);
382 if (!swim_readbit(base
, STEP
))
388 static inline int swim_track00(struct swim __iomem
*base
)
392 swim_action(base
, SEEK_NEGATIVE
);
394 for (try = 0; try < 100; try++) {
396 swim_select(base
, RELAX
);
397 if (swim_readbit(base
, TRACK_ZERO
))
404 if (swim_readbit(base
, TRACK_ZERO
))
410 static inline int swim_seek(struct swim __iomem
*base
, int step
)
416 swim_action(base
, SEEK_NEGATIVE
);
419 swim_action(base
, SEEK_POSITIVE
);
421 for ( ; step
> 0; step
--) {
429 static inline int swim_track(struct floppy_state
*fs
, int track
)
431 struct swim __iomem
*base
= fs
->swd
->base
;
434 ret
= swim_seek(base
, track
- fs
->track
);
446 static int floppy_eject(struct floppy_state
*fs
)
448 struct swim __iomem
*base
= fs
->swd
->base
;
450 swim_drive(base
, fs
->location
);
451 swim_motor(base
, OFF
);
460 static inline int swim_read_sector(struct floppy_state
*fs
,
462 int sector
, unsigned char *buffer
)
464 struct swim __iomem
*base
= fs
->swd
->base
;
466 struct sector_header header
;
470 swim_track(fs
, track
);
472 swim_write(base
, mode1
, MOTON
);
473 swim_head(base
, side
);
474 swim_write(base
, mode0
, side
);
476 local_irq_save(flags
);
477 for (i
= 0; i
< 36; i
++) {
478 ret
= swim_read_sector_header(base
, &header
);
479 if (!ret
&& (header
.sector
== sector
)) {
482 ret
= swim_read_sector_data(base
, buffer
);
486 local_irq_restore(flags
);
488 swim_write(base
, mode0
, MOTON
);
490 if ((header
.side
!= side
) || (header
.track
!= track
) ||
491 (header
.sector
!= sector
))
497 static int floppy_read_sectors(struct floppy_state
*fs
,
498 int req_sector
, int sectors_nb
,
499 unsigned char *buffer
)
501 struct swim __iomem
*base
= fs
->swd
->base
;
503 int side
, track
, sector
;
507 swim_drive(base
, fs
->location
);
508 for (i
= req_sector
; i
< req_sector
+ sectors_nb
; i
++) {
510 track
= i
/ fs
->secpercyl
;
511 x
= i
% fs
->secpercyl
;
512 side
= x
/ fs
->secpertrack
;
513 sector
= x
% fs
->secpertrack
+ 1;
517 ret
= swim_read_sector(fs
, side
, track
, sector
,
521 } while (ret
!= 512);
529 static void redo_fd_request(struct request_queue
*q
)
532 struct floppy_state
*fs
;
534 req
= blk_fetch_request(q
);
538 fs
= req
->rq_disk
->private_data
;
539 if (blk_rq_pos(req
) >= fs
->total_secs
)
543 if (rq_data_dir(req
) == WRITE
&& fs
->write_protected
)
546 switch (rq_data_dir(req
)) {
548 /* NOT IMPLEMENTED */
551 err
= floppy_read_sectors(fs
, blk_rq_pos(req
),
552 blk_rq_cur_sectors(req
),
557 if (!__blk_end_request_cur(req
, err
))
558 req
= blk_fetch_request(q
);
562 static void do_fd_request(struct request_queue
*q
)
567 static struct floppy_struct floppy_type
[4] = {
568 { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL
}, /* no testing */
569 { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL
}, /* 360KB SS 3.5"*/
570 { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL
}, /* 720KB 3.5" */
571 { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL
}, /* 1.44MB 3.5" */
574 static int get_floppy_geometry(struct floppy_state
*fs
, int type
,
575 struct floppy_struct
**g
)
577 if (type
>= ARRAY_SIZE(floppy_type
))
581 *g
= &floppy_type
[type
];
582 else if (fs
->type
== HD_MEDIA
) /* High-Density media */
583 *g
= &floppy_type
[3];
584 else if (fs
->head_number
== 2) /* double-sided */
585 *g
= &floppy_type
[2];
587 *g
= &floppy_type
[1];
592 static void setup_medium(struct floppy_state
*fs
)
594 struct swim __iomem
*base
= fs
->swd
->base
;
596 if (swim_readbit(base
, DISK_IN
)) {
597 struct floppy_struct
*g
;
599 fs
->write_protected
= swim_readbit(base
, WRITE_PROT
);
600 fs
->type
= swim_readbit(base
, ONEMEG_MEDIA
);
602 if (swim_track00(base
))
604 "SWIM: cannot move floppy head to track 0\n");
608 get_floppy_geometry(fs
, 0, &g
);
609 fs
->total_secs
= g
->size
;
610 fs
->secpercyl
= g
->head
* g
->sect
;
611 fs
->secpertrack
= g
->sect
;
618 static int floppy_open(struct block_device
*bdev
, fmode_t mode
)
620 struct floppy_state
*fs
= bdev
->bd_disk
->private_data
;
621 struct swim __iomem
*base
= fs
->swd
->base
;
624 if (fs
->ref_count
== -1 || (fs
->ref_count
&& mode
& FMODE_EXCL
))
627 if (mode
& FMODE_EXCL
)
632 swim_write(base
, setup
, S_IBM_DRIVE
| S_FCLK_DIV2
);
634 swim_drive(base
, INTERNAL_DRIVE
);
635 swim_motor(base
, ON
);
636 swim_action(base
, SETMFM
);
644 if (mode
& FMODE_NDELAY
)
647 if (mode
& (FMODE_READ
|FMODE_WRITE
)) {
648 check_disk_change(bdev
);
649 if ((mode
& FMODE_WRITE
) && fs
->write_protected
) {
656 if (fs
->ref_count
< 0)
658 else if (fs
->ref_count
> 0)
661 if (fs
->ref_count
== 0)
662 swim_motor(base
, OFF
);
666 static int floppy_unlocked_open(struct block_device
*bdev
, fmode_t mode
)
670 mutex_lock(&swim_mutex
);
671 ret
= floppy_open(bdev
, mode
);
672 mutex_unlock(&swim_mutex
);
677 static int floppy_release(struct gendisk
*disk
, fmode_t mode
)
679 struct floppy_state
*fs
= disk
->private_data
;
680 struct swim __iomem
*base
= fs
->swd
->base
;
682 mutex_lock(&swim_mutex
);
683 if (fs
->ref_count
< 0)
685 else if (fs
->ref_count
> 0)
688 if (fs
->ref_count
== 0)
689 swim_motor(base
, OFF
);
690 mutex_unlock(&swim_mutex
);
695 static int floppy_ioctl(struct block_device
*bdev
, fmode_t mode
,
696 unsigned int cmd
, unsigned long param
)
698 struct floppy_state
*fs
= bdev
->bd_disk
->private_data
;
701 if ((cmd
& 0x80) && !capable(CAP_SYS_ADMIN
))
706 if (fs
->ref_count
!= 1)
708 mutex_lock(&swim_mutex
);
709 err
= floppy_eject(fs
);
710 mutex_unlock(&swim_mutex
);
714 if (copy_to_user((void __user
*) param
, (void *) &floppy_type
,
715 sizeof(struct floppy_struct
)))
720 printk(KERN_DEBUG
"SWIM floppy_ioctl: unknown cmd %d\n",
727 static int floppy_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
729 struct floppy_state
*fs
= bdev
->bd_disk
->private_data
;
730 struct floppy_struct
*g
;
733 ret
= get_floppy_geometry(fs
, 0, &g
);
737 geo
->heads
= g
->head
;
738 geo
->sectors
= g
->sect
;
739 geo
->cylinders
= g
->track
;
744 static unsigned int floppy_check_events(struct gendisk
*disk
,
745 unsigned int clearing
)
747 struct floppy_state
*fs
= disk
->private_data
;
749 return fs
->ejected
? DISK_EVENT_MEDIA_CHANGE
: 0;
752 static int floppy_revalidate(struct gendisk
*disk
)
754 struct floppy_state
*fs
= disk
->private_data
;
755 struct swim __iomem
*base
= fs
->swd
->base
;
757 swim_drive(base
, fs
->location
);
763 swim_motor(base
, OFF
);
770 static const struct block_device_operations floppy_fops
= {
771 .owner
= THIS_MODULE
,
772 .open
= floppy_unlocked_open
,
773 .release
= floppy_release
,
774 .ioctl
= floppy_ioctl
,
775 .getgeo
= floppy_getgeo
,
776 .check_events
= floppy_check_events
,
777 .revalidate_disk
= floppy_revalidate
,
780 static struct kobject
*floppy_find(dev_t dev
, int *part
, void *data
)
782 struct swim_priv
*swd
= data
;
783 int drive
= (*part
& 3);
785 if (drive
> swd
->floppy_count
)
789 return get_disk(swd
->unit
[drive
].disk
);
792 static int __devinit
swim_add_floppy(struct swim_priv
*swd
,
793 enum drive_location location
)
795 struct floppy_state
*fs
= &swd
->unit
[swd
->floppy_count
];
796 struct swim __iomem
*base
= swd
->base
;
798 fs
->location
= location
;
800 swim_drive(base
, location
);
802 swim_motor(base
, OFF
);
804 if (swim_readbit(base
, SINGLE_SIDED
))
816 static int __devinit
swim_floppy_init(struct swim_priv
*swd
)
820 struct swim __iomem
*base
= swd
->base
;
822 /* scan floppy drives */
824 swim_drive(base
, INTERNAL_DRIVE
);
825 if (swim_readbit(base
, DRIVE_PRESENT
))
826 swim_add_floppy(swd
, INTERNAL_DRIVE
);
827 swim_drive(base
, EXTERNAL_DRIVE
);
828 if (swim_readbit(base
, DRIVE_PRESENT
))
829 swim_add_floppy(swd
, EXTERNAL_DRIVE
);
831 /* register floppy drives */
833 err
= register_blkdev(FLOPPY_MAJOR
, "fd");
835 printk(KERN_ERR
"Unable to get major %d for SWIM floppy\n",
840 for (drive
= 0; drive
< swd
->floppy_count
; drive
++) {
841 swd
->unit
[drive
].disk
= alloc_disk(1);
842 if (swd
->unit
[drive
].disk
== NULL
) {
846 swd
->unit
[drive
].swd
= swd
;
849 swd
->queue
= blk_init_queue(do_fd_request
, &swd
->lock
);
855 for (drive
= 0; drive
< swd
->floppy_count
; drive
++) {
856 swd
->unit
[drive
].disk
->flags
= GENHD_FL_REMOVABLE
;
857 swd
->unit
[drive
].disk
->major
= FLOPPY_MAJOR
;
858 swd
->unit
[drive
].disk
->first_minor
= drive
;
859 sprintf(swd
->unit
[drive
].disk
->disk_name
, "fd%d", drive
);
860 swd
->unit
[drive
].disk
->fops
= &floppy_fops
;
861 swd
->unit
[drive
].disk
->private_data
= &swd
->unit
[drive
];
862 swd
->unit
[drive
].disk
->queue
= swd
->queue
;
863 set_capacity(swd
->unit
[drive
].disk
, 2880);
864 add_disk(swd
->unit
[drive
].disk
);
867 blk_register_region(MKDEV(FLOPPY_MAJOR
, 0), 256, THIS_MODULE
,
868 floppy_find
, NULL
, swd
);
873 unregister_blkdev(FLOPPY_MAJOR
, "fd");
875 put_disk(swd
->unit
[drive
].disk
);
879 static int __devinit
swim_probe(struct platform_device
*dev
)
881 struct resource
*res
;
882 struct swim __iomem
*swim_base
;
883 struct swim_priv
*swd
;
886 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
892 if (!request_mem_region(res
->start
, resource_size(res
), CARDNAME
)) {
897 swim_base
= ioremap(res
->start
, resource_size(res
));
905 set_swim_mode(swim_base
, 1);
906 if (!get_swim_mode(swim_base
)) {
907 printk(KERN_INFO
"SWIM device not found !\n");
912 /* set platform driver data */
914 swd
= kzalloc(sizeof(struct swim_priv
), GFP_KERNEL
);
919 platform_set_drvdata(dev
, swd
);
921 swd
->base
= swim_base
;
923 ret
= swim_floppy_init(swd
);
930 platform_set_drvdata(dev
, NULL
);
935 release_mem_region(res
->start
, resource_size(res
));
940 static int __devexit
swim_remove(struct platform_device
*dev
)
942 struct swim_priv
*swd
= platform_get_drvdata(dev
);
944 struct resource
*res
;
946 blk_unregister_region(MKDEV(FLOPPY_MAJOR
, 0), 256);
948 for (drive
= 0; drive
< swd
->floppy_count
; drive
++) {
949 del_gendisk(swd
->unit
[drive
].disk
);
950 put_disk(swd
->unit
[drive
].disk
);
953 unregister_blkdev(FLOPPY_MAJOR
, "fd");
955 blk_cleanup_queue(swd
->queue
);
959 for (drive
= 0; drive
< swd
->floppy_count
; drive
++)
960 floppy_eject(&swd
->unit
[drive
]);
964 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
966 release_mem_region(res
->start
, resource_size(res
));
968 platform_set_drvdata(dev
, NULL
);
974 static struct platform_driver swim_driver
= {
976 .remove
= __devexit_p(swim_remove
),
979 .owner
= THIS_MODULE
,
983 static int __init
swim_init(void)
985 printk(KERN_INFO
"SWIM floppy driver %s\n", DRIVER_VERSION
);
987 return platform_driver_register(&swim_driver
);
989 module_init(swim_init
);
991 static void __exit
swim_exit(void)
993 platform_driver_unregister(&swim_driver
);
995 module_exit(swim_exit
);
997 MODULE_DESCRIPTION("Driver for SWIM floppy controller");
998 MODULE_LICENSE("GPL");
999 MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
1000 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR
);