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/blkdev.h>
22 #include <linux/hdreg.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/platform_device.h>
27 #include <asm/macintosh.h>
28 #include <asm/mac_via.h>
30 #define CARDNAME "swim"
32 struct sector_header {
39 } __attribute__((packed));
41 #define DRIVER_VERSION "Version 0.2 (2008-10-30)"
43 #define REG(x) unsigned char x, x ## _pad[0x200 - 1];
63 } __attribute__((packed));
65 #define swim_write(base, reg, v) out_8(&(base)->write_##reg, (v))
66 #define swim_read(base, reg) in_8(&(base)->read_##reg)
87 } __attribute__((packed));
89 #define iwm_write(base, reg, v) out_8(&(base)->reg, (v))
90 #define iwm_read(base, reg) in_8(&(base)->reg)
92 /* bits in phase register */
94 #define SEEK_POSITIVE 0x070
95 #define SEEK_NEGATIVE 0x074
97 #define MOTOR_ON 0x072
98 #define MOTOR_OFF 0x076
107 #define CA_MASK 0x077
109 /* Select values for swim_select and swim_readbit */
111 #define READ_DATA_0 0x074
112 #define TWOMEG_DRIVE 0x075
113 #define SINGLE_SIDED 0x076
114 #define DRIVE_PRESENT 0x077
115 #define DISK_IN 0x170
116 #define WRITE_PROT 0x171
117 #define TRACK_ZERO 0x172
119 #define READ_DATA_1 0x174
120 #define MFM_MODE 0x175
121 #define SEEK_COMPLETE 0x176
122 #define ONEMEG_MEDIA 0x177
124 /* Bits in handshake register */
126 #define MARK_BYTE 0x01
127 #define CRC_ZERO 0x02
132 #define DAT2BYTE 0x40
133 #define DAT1BYTE 0x80
135 /* bits in setup register */
137 #define S_INV_WDATA 0x01
138 #define S_3_5_SELECT 0x02
140 #define S_FCLK_DIV2 0x08
141 #define S_ERROR_CORR 0x10
142 #define S_IBM_DRIVE 0x20
143 #define S_GCR_WRITE 0x40
144 #define S_TIMEOUT 0x80
146 /* bits in mode register */
152 #define WRITE_MODE 0x10
156 /*----------------------------------------------------------------------------*/
158 enum drive_location {
159 INTERNAL_DRIVE = 0x02,
160 EXTERNAL_DRIVE = 0x04,
168 struct floppy_state {
170 /* physical properties */
172 enum drive_location location; /* internal or external drive */
173 int head_number; /* single- or double-sided drive */
179 enum media_type type;
186 /* in-use information */
191 struct gendisk *disk;
193 /* parent controller */
195 struct swim_priv *swd;
208 #define FD_MAX_UNIT 2
211 struct swim __iomem *base;
213 struct request_queue *queue;
215 struct floppy_state unit[FD_MAX_UNIT];
218 extern int swim_read_sector_header(struct swim __iomem *base,
219 struct sector_header *header);
220 extern int swim_read_sector_data(struct swim __iomem *base,
221 unsigned char *data);
223 static inline void set_swim_mode(struct swim __iomem *base, int enable)
225 struct iwm __iomem *iwm_base;
229 swim_write(base, mode0, 0xf8);
233 iwm_base = (struct iwm __iomem *)base;
234 local_irq_save(flags);
236 iwm_read(iwm_base, q7L);
237 iwm_read(iwm_base, mtrOff);
238 iwm_read(iwm_base, q6H);
240 iwm_write(iwm_base, q7H, 0x57);
241 iwm_write(iwm_base, q7H, 0x17);
242 iwm_write(iwm_base, q7H, 0x57);
243 iwm_write(iwm_base, q7H, 0x57);
245 local_irq_restore(flags);
248 static inline int get_swim_mode(struct swim __iomem *base)
252 local_irq_save(flags);
254 swim_write(base, phase, 0xf5);
255 if (swim_read(base, phase) != 0xf5)
257 swim_write(base, phase, 0xf6);
258 if (swim_read(base, phase) != 0xf6)
260 swim_write(base, phase, 0xf7);
261 if (swim_read(base, phase) != 0xf7)
263 local_irq_restore(flags);
266 local_irq_restore(flags);
270 static inline void swim_select(struct swim __iomem *base, int sel)
272 swim_write(base, phase, RELAX);
274 via1_set_head(sel & 0x100);
276 swim_write(base, phase, sel & CA_MASK);
279 static inline void swim_action(struct swim __iomem *base, int action)
283 local_irq_save(flags);
285 swim_select(base, action);
287 swim_write(base, phase, (LSTRB<<4) | LSTRB);
289 swim_write(base, phase, (LSTRB<<4) | ((~LSTRB) & 0x0F));
292 local_irq_restore(flags);
295 static inline int swim_readbit(struct swim __iomem *base, int bit)
299 swim_select(base, bit);
303 stat = swim_read(base, handshake);
305 return (stat & SENSE) == 0;
308 static inline void swim_drive(struct swim __iomem *base,
309 enum drive_location location)
311 if (location == INTERNAL_DRIVE) {
312 swim_write(base, mode0, EXTERNAL_DRIVE); /* clear drive 1 bit */
313 swim_write(base, mode1, INTERNAL_DRIVE); /* set drive 0 bit */
314 } else if (location == EXTERNAL_DRIVE) {
315 swim_write(base, mode0, INTERNAL_DRIVE); /* clear drive 0 bit */
316 swim_write(base, mode1, EXTERNAL_DRIVE); /* set drive 1 bit */
320 static inline void swim_motor(struct swim __iomem *base,
321 enum motor_action action)
326 swim_action(base, MOTOR_ON);
328 for (i = 0; i < 2*HZ; i++) {
329 swim_select(base, RELAX);
330 if (swim_readbit(base, MOTOR_ON))
332 current->state = TASK_INTERRUPTIBLE;
335 } else if (action == OFF) {
336 swim_action(base, MOTOR_OFF);
337 swim_select(base, RELAX);
341 static inline void swim_eject(struct swim __iomem *base)
345 swim_action(base, EJECT);
347 for (i = 0; i < 2*HZ; i++) {
348 swim_select(base, RELAX);
349 if (!swim_readbit(base, DISK_IN))
351 current->state = TASK_INTERRUPTIBLE;
354 swim_select(base, RELAX);
357 static inline void swim_head(struct swim __iomem *base, enum head head)
359 /* wait drive is ready */
361 if (head == UPPER_HEAD)
362 swim_select(base, READ_DATA_1);
363 else if (head == LOWER_HEAD)
364 swim_select(base, READ_DATA_0);
367 static inline int swim_step(struct swim __iomem *base)
371 swim_action(base, STEP);
373 for (wait = 0; wait < HZ; wait++) {
375 current->state = TASK_INTERRUPTIBLE;
378 swim_select(base, RELAX);
379 if (!swim_readbit(base, STEP))
385 static inline int swim_track00(struct swim __iomem *base)
389 swim_action(base, SEEK_NEGATIVE);
391 for (try = 0; try < 100; try++) {
393 swim_select(base, RELAX);
394 if (swim_readbit(base, TRACK_ZERO))
401 if (swim_readbit(base, TRACK_ZERO))
407 static inline int swim_seek(struct swim __iomem *base, int step)
413 swim_action(base, SEEK_NEGATIVE);
416 swim_action(base, SEEK_POSITIVE);
418 for ( ; step > 0; step--) {
426 static inline int swim_track(struct floppy_state *fs, int track)
428 struct swim __iomem *base = fs->swd->base;
431 ret = swim_seek(base, track - fs->track);
443 static int floppy_eject(struct floppy_state *fs)
445 struct swim __iomem *base = fs->swd->base;
447 swim_drive(base, fs->location);
448 swim_motor(base, OFF);
457 static inline int swim_read_sector(struct floppy_state *fs,
459 int sector, unsigned char *buffer)
461 struct swim __iomem *base = fs->swd->base;
463 struct sector_header header;
467 swim_track(fs, track);
469 swim_write(base, mode1, MOTON);
470 swim_head(base, side);
471 swim_write(base, mode0, side);
473 local_irq_save(flags);
474 for (i = 0; i < 36; i++) {
475 ret = swim_read_sector_header(base, &header);
476 if (!ret && (header.sector == sector)) {
479 ret = swim_read_sector_data(base, buffer);
483 local_irq_restore(flags);
485 swim_write(base, mode0, MOTON);
487 if ((header.side != side) || (header.track != track) ||
488 (header.sector != sector))
494 static int floppy_read_sectors(struct floppy_state *fs,
495 int req_sector, int sectors_nb,
496 unsigned char *buffer)
498 struct swim __iomem *base = fs->swd->base;
500 int side, track, sector;
504 swim_drive(base, fs->location);
505 for (i = req_sector; i < req_sector + sectors_nb; i++) {
507 track = i / fs->secpercyl;
508 x = i % fs->secpercyl;
509 side = x / fs->secpertrack;
510 sector = x % fs->secpertrack + 1;
514 ret = swim_read_sector(fs, side, track, sector,
518 } while (ret != 512);
526 static void redo_fd_request(struct request_queue *q)
529 struct floppy_state *fs;
531 while ((req = elv_next_request(q))) {
533 fs = req->rq_disk->private_data;
534 if (req->sector < 0 || req->sector >= fs->total_secs) {
538 if (req->current_nr_sectors == 0) {
546 if (rq_data_dir(req) == WRITE) {
547 if (fs->write_protected) {
552 switch (rq_data_dir(req)) {
554 /* NOT IMPLEMENTED */
558 if (floppy_read_sectors(fs, req->sector,
559 req->current_nr_sectors,
564 req->nr_sectors -= req->current_nr_sectors;
565 req->sector += req->current_nr_sectors;
566 req->buffer += req->current_nr_sectors * 512;
573 static void do_fd_request(struct request_queue *q)
578 static struct floppy_struct floppy_type[4] = {
579 { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL }, /* no testing */
580 { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 360KB SS 3.5"*/
581 { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 720KB 3.5" */
582 { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL }, /* 1.44MB 3.5" */
585 static int get_floppy_geometry(struct floppy_state *fs, int type,
586 struct floppy_struct **g)
588 if (type >= ARRAY_SIZE(floppy_type))
592 *g = &floppy_type[type];
593 else if (fs->type == HD_MEDIA) /* High-Density media */
594 *g = &floppy_type[3];
595 else if (fs->head_number == 2) /* double-sided */
596 *g = &floppy_type[2];
598 *g = &floppy_type[1];
603 static void setup_medium(struct floppy_state *fs)
605 struct swim __iomem *base = fs->swd->base;
607 if (swim_readbit(base, DISK_IN)) {
608 struct floppy_struct *g;
610 fs->write_protected = swim_readbit(base, WRITE_PROT);
611 fs->type = swim_readbit(base, ONEMEG_MEDIA);
613 if (swim_track00(base))
615 "SWIM: cannot move floppy head to track 0\n");
619 get_floppy_geometry(fs, 0, &g);
620 fs->total_secs = g->size;
621 fs->secpercyl = g->head * g->sect;
622 fs->secpertrack = g->sect;
629 static int floppy_open(struct block_device *bdev, fmode_t mode)
631 struct floppy_state *fs = bdev->bd_disk->private_data;
632 struct swim __iomem *base = fs->swd->base;
635 if (fs->ref_count == -1 || (fs->ref_count && mode & FMODE_EXCL))
638 if (mode & FMODE_EXCL)
643 swim_write(base, setup, S_IBM_DRIVE | S_FCLK_DIV2);
645 swim_drive(base, INTERNAL_DRIVE);
646 swim_motor(base, ON);
647 swim_action(base, SETMFM);
655 if (mode & FMODE_NDELAY)
658 if (mode & (FMODE_READ|FMODE_WRITE)) {
659 check_disk_change(bdev);
660 if ((mode & FMODE_WRITE) && fs->write_protected) {
667 if (fs->ref_count < 0)
669 else if (fs->ref_count > 0)
672 if (fs->ref_count == 0)
673 swim_motor(base, OFF);
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 if (fs->ref_count < 0)
684 else if (fs->ref_count > 0)
687 if (fs->ref_count == 0)
688 swim_motor(base, OFF);
693 static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
694 unsigned int cmd, unsigned long param)
696 struct floppy_state *fs = bdev->bd_disk->private_data;
699 if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
704 if (fs->ref_count != 1)
706 err = floppy_eject(fs);
710 if (copy_to_user((void __user *) param, (void *) &floppy_type,
711 sizeof(struct floppy_struct)))
716 printk(KERN_DEBUG "SWIM floppy_ioctl: unknown cmd %d\n",
723 static int floppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
725 struct floppy_state *fs = bdev->bd_disk->private_data;
726 struct floppy_struct *g;
729 ret = get_floppy_geometry(fs, 0, &g);
733 geo->heads = g->head;
734 geo->sectors = g->sect;
735 geo->cylinders = g->track;
740 static int floppy_check_change(struct gendisk *disk)
742 struct floppy_state *fs = disk->private_data;
747 static int floppy_revalidate(struct gendisk *disk)
749 struct floppy_state *fs = disk->private_data;
750 struct swim __iomem *base = fs->swd->base;
752 swim_drive(base, fs->location);
758 swim_motor(base, OFF);
765 static struct block_device_operations floppy_fops = {
766 .owner = THIS_MODULE,
768 .release = floppy_release,
769 .locked_ioctl = floppy_ioctl,
770 .getgeo = floppy_getgeo,
771 .media_changed = floppy_check_change,
772 .revalidate_disk = floppy_revalidate,
775 static struct kobject *floppy_find(dev_t dev, int *part, void *data)
777 struct swim_priv *swd = data;
778 int drive = (*part & 3);
780 if (drive > swd->floppy_count)
784 return get_disk(swd->unit[drive].disk);
787 static int __devinit swim_add_floppy(struct swim_priv *swd,
788 enum drive_location location)
790 struct floppy_state *fs = &swd->unit[swd->floppy_count];
791 struct swim __iomem *base = swd->base;
793 fs->location = location;
795 swim_drive(base, location);
797 swim_motor(base, OFF);
799 if (swim_readbit(base, SINGLE_SIDED))
811 static int __devinit swim_floppy_init(struct swim_priv *swd)
815 struct swim __iomem *base = swd->base;
817 /* scan floppy drives */
819 swim_drive(base, INTERNAL_DRIVE);
820 if (swim_readbit(base, DRIVE_PRESENT))
821 swim_add_floppy(swd, INTERNAL_DRIVE);
822 swim_drive(base, EXTERNAL_DRIVE);
823 if (swim_readbit(base, DRIVE_PRESENT))
824 swim_add_floppy(swd, EXTERNAL_DRIVE);
826 /* register floppy drives */
828 err = register_blkdev(FLOPPY_MAJOR, "fd");
830 printk(KERN_ERR "Unable to get major %d for SWIM floppy\n",
835 for (drive = 0; drive < swd->floppy_count; drive++) {
836 swd->unit[drive].disk = alloc_disk(1);
837 if (swd->unit[drive].disk == NULL) {
841 swd->unit[drive].swd = swd;
844 swd->queue = blk_init_queue(do_fd_request, &swd->lock);
850 for (drive = 0; drive < swd->floppy_count; drive++) {
851 swd->unit[drive].disk->flags = GENHD_FL_REMOVABLE;
852 swd->unit[drive].disk->major = FLOPPY_MAJOR;
853 swd->unit[drive].disk->first_minor = drive;
854 sprintf(swd->unit[drive].disk->disk_name, "fd%d", drive);
855 swd->unit[drive].disk->fops = &floppy_fops;
856 swd->unit[drive].disk->private_data = &swd->unit[drive];
857 swd->unit[drive].disk->queue = swd->queue;
858 set_capacity(swd->unit[drive].disk, 2880);
859 add_disk(swd->unit[drive].disk);
862 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
863 floppy_find, NULL, swd);
868 unregister_blkdev(FLOPPY_MAJOR, "fd");
870 put_disk(swd->unit[drive].disk);
874 static int __devinit swim_probe(struct platform_device *dev)
876 struct resource *res;
877 struct swim __iomem *swim_base;
878 struct swim_priv *swd;
881 res = platform_get_resource_byname(dev, IORESOURCE_MEM, "swim-regs");
887 if (!request_mem_region(res->start, resource_size(res), CARDNAME)) {
892 swim_base = ioremap(res->start, resource_size(res));
900 set_swim_mode(swim_base, 1);
901 if (!get_swim_mode(swim_base)) {
902 printk(KERN_INFO "SWIM device not found !\n");
907 /* set platform driver data */
909 swd = kzalloc(sizeof(struct swim_priv), GFP_KERNEL);
914 platform_set_drvdata(dev, swd);
916 swd->base = swim_base;
918 ret = swim_floppy_init(swd);
925 platform_set_drvdata(dev, NULL);
930 release_mem_region(res->start, resource_size(res));
935 static int __devexit swim_remove(struct platform_device *dev)
937 struct swim_priv *swd = platform_get_drvdata(dev);
939 struct resource *res;
941 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
943 for (drive = 0; drive < swd->floppy_count; drive++) {
944 del_gendisk(swd->unit[drive].disk);
945 put_disk(swd->unit[drive].disk);
948 unregister_blkdev(FLOPPY_MAJOR, "fd");
950 blk_cleanup_queue(swd->queue);
954 for (drive = 0; drive < swd->floppy_count; drive++)
955 floppy_eject(&swd->unit[drive]);
959 res = platform_get_resource_byname(dev, IORESOURCE_MEM, "swim-regs");
961 release_mem_region(res->start, resource_size(res));
963 platform_set_drvdata(dev, NULL);
969 static struct platform_driver swim_driver = {
971 .remove = __devexit_p(swim_remove),
974 .owner = THIS_MODULE,
978 static int __init swim_init(void)
980 printk(KERN_INFO "SWIM floppy driver %s\n", DRIVER_VERSION);
982 return platform_driver_register(&swim_driver);
984 module_init(swim_init);
986 static void __exit swim_exit(void)
988 platform_driver_unregister(&swim_driver);
990 module_exit(swim_exit);
992 MODULE_DESCRIPTION("Driver for SWIM floppy controller");
993 MODULE_LICENSE("GPL");
994 MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
995 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);