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