blob: d1ee4670666a6026fd3da4c4a8bf54d0c04d31f2 [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 Bergmann2a48fc02010-06-02 14:28:52 +020023#include <linux/mutex.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
Laurent Vivier8852ecd2008-11-15 16:10:10 +010029#include <asm/mac_via.h>
30
31#define CARDNAME "swim"
32
33struct sector_header {
34 unsigned char side;
35 unsigned char track;
36 unsigned char sector;
37 unsigned char size;
38 unsigned char crc0;
39 unsigned char crc1;
40} __attribute__((packed));
41
42#define DRIVER_VERSION "Version 0.2 (2008-10-30)"
43
44#define REG(x) unsigned char x, x ## _pad[0x200 - 1];
45
46struct swim {
47 REG(write_data)
48 REG(write_mark)
49 REG(write_CRC)
50 REG(write_parameter)
51 REG(write_phase)
52 REG(write_setup)
53 REG(write_mode0)
54 REG(write_mode1)
55
56 REG(read_data)
57 REG(read_mark)
58 REG(read_error)
59 REG(read_parameter)
60 REG(read_phase)
61 REG(read_setup)
62 REG(read_status)
63 REG(read_handshake)
64} __attribute__((packed));
65
66#define swim_write(base, reg, v) out_8(&(base)->write_##reg, (v))
67#define swim_read(base, reg) in_8(&(base)->read_##reg)
68
69/* IWM registers */
70
71struct iwm {
72 REG(ph0L)
73 REG(ph0H)
74 REG(ph1L)
75 REG(ph1H)
76 REG(ph2L)
77 REG(ph2H)
78 REG(ph3L)
79 REG(ph3H)
80 REG(mtrOff)
81 REG(mtrOn)
82 REG(intDrive)
83 REG(extDrive)
84 REG(q6L)
85 REG(q6H)
86 REG(q7L)
87 REG(q7H)
88} __attribute__((packed));
89
90#define iwm_write(base, reg, v) out_8(&(base)->reg, (v))
91#define iwm_read(base, reg) in_8(&(base)->reg)
92
93/* bits in phase register */
94
95#define SEEK_POSITIVE 0x070
96#define SEEK_NEGATIVE 0x074
97#define STEP 0x071
98#define MOTOR_ON 0x072
99#define MOTOR_OFF 0x076
100#define INDEX 0x073
101#define EJECT 0x077
102#define SETMFM 0x171
103#define SETGCR 0x175
104
105#define RELAX 0x033
106#define LSTRB 0x008
107
108#define CA_MASK 0x077
109
110/* Select values for swim_select and swim_readbit */
111
112#define READ_DATA_0 0x074
Finn Thain56a1c5e2018-04-11 20:50:14 -0400113#define ONEMEG_DRIVE 0x075
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100114#define SINGLE_SIDED 0x076
115#define DRIVE_PRESENT 0x077
116#define DISK_IN 0x170
117#define WRITE_PROT 0x171
118#define TRACK_ZERO 0x172
119#define TACHO 0x173
120#define READ_DATA_1 0x174
Finn Thain56a1c5e2018-04-11 20:50:14 -0400121#define GCR_MODE 0x175
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100122#define SEEK_COMPLETE 0x176
Finn Thain56a1c5e2018-04-11 20:50:14 -0400123#define TWOMEG_MEDIA 0x177
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100124
125/* Bits in handshake register */
126
127#define MARK_BYTE 0x01
128#define CRC_ZERO 0x02
129#define RDDATA 0x04
130#define SENSE 0x08
131#define MOTEN 0x10
132#define ERROR 0x20
133#define DAT2BYTE 0x40
134#define DAT1BYTE 0x80
135
136/* bits in setup register */
137
138#define S_INV_WDATA 0x01
139#define S_3_5_SELECT 0x02
140#define S_GCR 0x04
141#define S_FCLK_DIV2 0x08
142#define S_ERROR_CORR 0x10
143#define S_IBM_DRIVE 0x20
144#define S_GCR_WRITE 0x40
145#define S_TIMEOUT 0x80
146
147/* bits in mode register */
148
149#define CLFIFO 0x01
150#define ENBL1 0x02
151#define ENBL2 0x04
152#define ACTION 0x08
153#define WRITE_MODE 0x10
154#define HEDSEL 0x20
155#define MOTON 0x80
156
157/*----------------------------------------------------------------------------*/
158
159enum drive_location {
160 INTERNAL_DRIVE = 0x02,
161 EXTERNAL_DRIVE = 0x04,
162};
163
164enum media_type {
165 DD_MEDIA,
166 HD_MEDIA,
167};
168
169struct floppy_state {
170
171 /* physical properties */
172
173 enum drive_location location; /* internal or external drive */
174 int head_number; /* single- or double-sided drive */
175
176 /* media */
177
178 int disk_in;
179 int ejected;
180 enum media_type type;
181 int write_protected;
182
183 int total_secs;
184 int secpercyl;
185 int secpertrack;
186
187 /* in-use information */
188
189 int track;
190 int ref_count;
191
192 struct gendisk *disk;
193
194 /* parent controller */
195
196 struct swim_priv *swd;
197};
198
199enum motor_action {
200 OFF,
201 ON,
202};
203
204enum head {
205 LOWER_HEAD = 0,
206 UPPER_HEAD = 1,
207};
208
209#define FD_MAX_UNIT 2
210
211struct swim_priv {
212 struct swim __iomem *base;
213 spinlock_t lock;
Omar Sandoval103db8b2017-03-27 23:28:46 -0700214 int fdc_queue;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100215 int floppy_count;
216 struct floppy_state unit[FD_MAX_UNIT];
217};
218
219extern int swim_read_sector_header(struct swim __iomem *base,
220 struct sector_header *header);
221extern int swim_read_sector_data(struct swim __iomem *base,
222 unsigned char *data);
223
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200224static DEFINE_MUTEX(swim_mutex);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100225static 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
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200496static blk_status_t floppy_read_sectors(struct floppy_state *fs,
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100497 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)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200519 return BLK_STS_IOERR;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100520 } while (ret != 512);
521
522 buffer += ret;
523 }
524
525 return 0;
526}
527
Omar Sandoval103db8b2017-03-27 23:28:46 -0700528static struct request *swim_next_request(struct swim_priv *swd)
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100529{
Omar Sandoval103db8b2017-03-27 23:28:46 -0700530 struct request_queue *q;
531 struct request *rq;
532 int old_pos = swd->fdc_queue;
533
534 do {
535 q = swd->unit[swd->fdc_queue].disk->queue;
536 if (++swd->fdc_queue == swd->floppy_count)
537 swd->fdc_queue = 0;
538 if (q) {
539 rq = blk_fetch_request(q);
540 if (rq)
541 return rq;
542 }
543 } while (swd->fdc_queue != old_pos);
544
545 return NULL;
546}
547
548static void do_fd_request(struct request_queue *q)
549{
550 struct swim_priv *swd = q->queuedata;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100551 struct request *req;
552 struct floppy_state *fs;
553
Omar Sandoval103db8b2017-03-27 23:28:46 -0700554 req = swim_next_request(swd);
Tejun Heo06b06082009-05-08 11:54:09 +0900555 while (req) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200556 blk_status_t err = BLK_STS_IOERR;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100557
558 fs = req->rq_disk->private_data;
Tejun Heo06b06082009-05-08 11:54:09 +0900559 if (blk_rq_pos(req) >= fs->total_secs)
560 goto done;
561 if (!fs->disk_in)
562 goto done;
563 if (rq_data_dir(req) == WRITE && fs->write_protected)
564 goto done;
565
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100566 switch (rq_data_dir(req)) {
567 case WRITE:
568 /* NOT IMPLEMENTED */
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100569 break;
570 case READ:
Tejun Heo06b06082009-05-08 11:54:09 +0900571 err = floppy_read_sectors(fs, blk_rq_pos(req),
572 blk_rq_cur_sectors(req),
Jens Axboeb4f42e22014-04-10 09:46:28 -0600573 bio_data(req->bio));
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100574 break;
575 }
Tejun Heo06b06082009-05-08 11:54:09 +0900576 done:
Tejun Heo9934c8c2009-05-08 11:54:16 +0900577 if (!__blk_end_request_cur(req, err))
Omar Sandoval103db8b2017-03-27 23:28:46 -0700578 req = swim_next_request(swd);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100579 }
580}
581
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100582static struct floppy_struct floppy_type[4] = {
583 { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL }, /* no testing */
584 { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 360KB SS 3.5"*/
585 { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 720KB 3.5" */
586 { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL }, /* 1.44MB 3.5" */
587};
588
589static int get_floppy_geometry(struct floppy_state *fs, int type,
590 struct floppy_struct **g)
591{
592 if (type >= ARRAY_SIZE(floppy_type))
593 return -EINVAL;
594
595 if (type)
596 *g = &floppy_type[type];
597 else if (fs->type == HD_MEDIA) /* High-Density media */
598 *g = &floppy_type[3];
599 else if (fs->head_number == 2) /* double-sided */
600 *g = &floppy_type[2];
601 else
602 *g = &floppy_type[1];
603
604 return 0;
605}
606
607static void setup_medium(struct floppy_state *fs)
608{
609 struct swim __iomem *base = fs->swd->base;
610
611 if (swim_readbit(base, DISK_IN)) {
612 struct floppy_struct *g;
613 fs->disk_in = 1;
614 fs->write_protected = swim_readbit(base, WRITE_PROT);
Finn Thain56a1c5e2018-04-11 20:50:14 -0400615 fs->type = swim_readbit(base, TWOMEG_MEDIA);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100616
617 if (swim_track00(base))
618 printk(KERN_ERR
619 "SWIM: cannot move floppy head to track 0\n");
620
621 swim_track00(base);
622
623 get_floppy_geometry(fs, 0, &g);
624 fs->total_secs = g->size;
625 fs->secpercyl = g->head * g->sect;
626 fs->secpertrack = g->sect;
627 fs->track = 0;
628 } else {
629 fs->disk_in = 0;
630 }
631}
632
633static int floppy_open(struct block_device *bdev, fmode_t mode)
634{
635 struct floppy_state *fs = bdev->bd_disk->private_data;
636 struct swim __iomem *base = fs->swd->base;
637 int err;
638
639 if (fs->ref_count == -1 || (fs->ref_count && mode & FMODE_EXCL))
640 return -EBUSY;
641
642 if (mode & FMODE_EXCL)
643 fs->ref_count = -1;
644 else
645 fs->ref_count++;
646
647 swim_write(base, setup, S_IBM_DRIVE | S_FCLK_DIV2);
648 udelay(10);
649 swim_drive(base, INTERNAL_DRIVE);
650 swim_motor(base, ON);
651 swim_action(base, SETMFM);
652 if (fs->ejected)
653 setup_medium(fs);
654 if (!fs->disk_in) {
655 err = -ENXIO;
656 goto out;
657 }
658
659 if (mode & FMODE_NDELAY)
660 return 0;
661
662 if (mode & (FMODE_READ|FMODE_WRITE)) {
663 check_disk_change(bdev);
664 if ((mode & FMODE_WRITE) && fs->write_protected) {
665 err = -EROFS;
666 goto out;
667 }
668 }
669 return 0;
670out:
671 if (fs->ref_count < 0)
672 fs->ref_count = 0;
673 else if (fs->ref_count > 0)
674 --fs->ref_count;
675
676 if (fs->ref_count == 0)
677 swim_motor(base, OFF);
678 return err;
679}
680
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200681static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
682{
683 int ret;
684
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200685 mutex_lock(&swim_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200686 ret = floppy_open(bdev, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200687 mutex_unlock(&swim_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200688
689 return ret;
690}
691
Al Virodb2a1442013-05-05 21:52:57 -0400692static void floppy_release(struct gendisk *disk, fmode_t mode)
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100693{
694 struct floppy_state *fs = disk->private_data;
695 struct swim __iomem *base = fs->swd->base;
696
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200697 mutex_lock(&swim_mutex);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100698 if (fs->ref_count < 0)
699 fs->ref_count = 0;
700 else if (fs->ref_count > 0)
701 --fs->ref_count;
702
703 if (fs->ref_count == 0)
704 swim_motor(base, OFF);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200705 mutex_unlock(&swim_mutex);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100706}
707
708static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
709 unsigned int cmd, unsigned long param)
710{
711 struct floppy_state *fs = bdev->bd_disk->private_data;
712 int err;
713
714 if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
715 return -EPERM;
716
717 switch (cmd) {
718 case FDEJECT:
719 if (fs->ref_count != 1)
720 return -EBUSY;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200721 mutex_lock(&swim_mutex);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100722 err = floppy_eject(fs);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200723 mutex_unlock(&swim_mutex);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100724 return err;
725
726 case FDGETPRM:
727 if (copy_to_user((void __user *) param, (void *) &floppy_type,
728 sizeof(struct floppy_struct)))
729 return -EFAULT;
Finn Thain8e2ab5a2018-04-11 20:50:14 -0400730 return 0;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100731 }
Finn Thain8e2ab5a2018-04-11 20:50:14 -0400732 return -ENOTTY;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100733}
734
735static int floppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
736{
737 struct floppy_state *fs = bdev->bd_disk->private_data;
738 struct floppy_struct *g;
739 int ret;
740
741 ret = get_floppy_geometry(fs, 0, &g);
742 if (ret)
743 return ret;
744
745 geo->heads = g->head;
746 geo->sectors = g->sect;
747 geo->cylinders = g->track;
748
749 return 0;
750}
751
Tejun Heo4bbde772011-03-09 19:54:28 +0100752static unsigned int floppy_check_events(struct gendisk *disk,
753 unsigned int clearing)
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100754{
755 struct floppy_state *fs = disk->private_data;
756
Tejun Heo4bbde772011-03-09 19:54:28 +0100757 return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100758}
759
760static int floppy_revalidate(struct gendisk *disk)
761{
762 struct floppy_state *fs = disk->private_data;
763 struct swim __iomem *base = fs->swd->base;
764
765 swim_drive(base, fs->location);
766
767 if (fs->ejected)
768 setup_medium(fs);
769
770 if (!fs->disk_in)
771 swim_motor(base, OFF);
772 else
773 fs->ejected = 0;
774
775 return !fs->disk_in;
776}
777
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700778static const struct block_device_operations floppy_fops = {
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100779 .owner = THIS_MODULE,
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200780 .open = floppy_unlocked_open,
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100781 .release = floppy_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200782 .ioctl = floppy_ioctl,
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100783 .getgeo = floppy_getgeo,
Tejun Heo4bbde772011-03-09 19:54:28 +0100784 .check_events = floppy_check_events,
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100785 .revalidate_disk = floppy_revalidate,
786};
787
788static struct kobject *floppy_find(dev_t dev, int *part, void *data)
789{
790 struct swim_priv *swd = data;
791 int drive = (*part & 3);
792
Finn Thain7ae6a2b2018-04-11 20:50:14 -0400793 if (drive >= swd->floppy_count)
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100794 return NULL;
795
796 *part = 0;
Jan Kara3079c222018-02-26 13:01:38 +0100797 return get_disk_and_module(swd->unit[drive].disk);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100798}
799
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800800static int swim_add_floppy(struct swim_priv *swd, enum drive_location location)
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100801{
802 struct floppy_state *fs = &swd->unit[swd->floppy_count];
803 struct swim __iomem *base = swd->base;
804
805 fs->location = location;
806
807 swim_drive(base, location);
808
809 swim_motor(base, OFF);
810
811 if (swim_readbit(base, SINGLE_SIDED))
812 fs->head_number = 1;
813 else
814 fs->head_number = 2;
815 fs->ref_count = 0;
816 fs->ejected = 1;
817
818 swd->floppy_count++;
819
820 return 0;
821}
822
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800823static int swim_floppy_init(struct swim_priv *swd)
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100824{
825 int err;
826 int drive;
827 struct swim __iomem *base = swd->base;
828
829 /* scan floppy drives */
830
831 swim_drive(base, INTERNAL_DRIVE);
832 if (swim_readbit(base, DRIVE_PRESENT))
833 swim_add_floppy(swd, INTERNAL_DRIVE);
834 swim_drive(base, EXTERNAL_DRIVE);
835 if (swim_readbit(base, DRIVE_PRESENT))
836 swim_add_floppy(swd, EXTERNAL_DRIVE);
837
838 /* register floppy drives */
839
840 err = register_blkdev(FLOPPY_MAJOR, "fd");
841 if (err) {
842 printk(KERN_ERR "Unable to get major %d for SWIM floppy\n",
843 FLOPPY_MAJOR);
844 return -EBUSY;
845 }
846
Omar Sandoval103db8b2017-03-27 23:28:46 -0700847 spin_lock_init(&swd->lock);
848
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100849 for (drive = 0; drive < swd->floppy_count; drive++) {
850 swd->unit[drive].disk = alloc_disk(1);
851 if (swd->unit[drive].disk == NULL) {
852 err = -ENOMEM;
853 goto exit_put_disks;
854 }
Omar Sandoval103db8b2017-03-27 23:28:46 -0700855 swd->unit[drive].disk->queue = blk_init_queue(do_fd_request,
856 &swd->lock);
857 if (!swd->unit[drive].disk->queue) {
858 err = -ENOMEM;
Omar Sandoval103db8b2017-03-27 23:28:46 -0700859 goto exit_put_disks;
860 }
Christoph Hellwig8fc45042017-06-19 09:26:26 +0200861 blk_queue_bounce_limit(swd->unit[drive].disk->queue,
862 BLK_BOUNCE_HIGH);
Omar Sandoval103db8b2017-03-27 23:28:46 -0700863 swd->unit[drive].disk->queue->queuedata = swd;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100864 swd->unit[drive].swd = swd;
865 }
866
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100867 for (drive = 0; drive < swd->floppy_count; drive++) {
868 swd->unit[drive].disk->flags = GENHD_FL_REMOVABLE;
869 swd->unit[drive].disk->major = FLOPPY_MAJOR;
870 swd->unit[drive].disk->first_minor = drive;
871 sprintf(swd->unit[drive].disk->disk_name, "fd%d", drive);
872 swd->unit[drive].disk->fops = &floppy_fops;
873 swd->unit[drive].disk->private_data = &swd->unit[drive];
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100874 set_capacity(swd->unit[drive].disk, 2880);
875 add_disk(swd->unit[drive].disk);
876 }
877
878 blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
879 floppy_find, NULL, swd);
880
881 return 0;
882
883exit_put_disks:
884 unregister_blkdev(FLOPPY_MAJOR, "fd");
885 while (drive--)
886 put_disk(swd->unit[drive].disk);
887 return err;
888}
889
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800890static int swim_probe(struct platform_device *dev)
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100891{
892 struct resource *res;
893 struct swim __iomem *swim_base;
894 struct swim_priv *swd;
895 int ret;
896
Finn Thain2724daf2009-11-04 00:39:56 +1100897 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100898 if (!res) {
899 ret = -ENODEV;
900 goto out;
901 }
902
903 if (!request_mem_region(res->start, resource_size(res), CARDNAME)) {
904 ret = -EBUSY;
905 goto out;
906 }
907
Finn Thainb64576c2018-04-11 20:50:14 -0400908 swim_base = (struct swim __iomem *)res->start;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100909 if (!swim_base) {
Joe Perches957d6bf2013-06-04 11:34:35 -0700910 ret = -ENOMEM;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100911 goto out_release_io;
912 }
913
914 /* probe device */
915
916 set_swim_mode(swim_base, 1);
917 if (!get_swim_mode(swim_base)) {
918 printk(KERN_INFO "SWIM device not found !\n");
919 ret = -ENODEV;
Finn Thainb64576c2018-04-11 20:50:14 -0400920 goto out_release_io;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100921 }
922
923 /* set platform driver data */
924
925 swd = kzalloc(sizeof(struct swim_priv), GFP_KERNEL);
926 if (!swd) {
927 ret = -ENOMEM;
Finn Thainb64576c2018-04-11 20:50:14 -0400928 goto out_release_io;
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100929 }
930 platform_set_drvdata(dev, swd);
931
932 swd->base = swim_base;
933
934 ret = swim_floppy_init(swd);
935 if (ret)
936 goto out_kfree;
937
938 return 0;
939
940out_kfree:
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100941 kfree(swd);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100942out_release_io:
943 release_mem_region(res->start, resource_size(res));
944out:
945 return ret;
946}
947
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800948static int swim_remove(struct platform_device *dev)
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100949{
950 struct swim_priv *swd = platform_get_drvdata(dev);
951 int drive;
952 struct resource *res;
953
954 blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
955
956 for (drive = 0; drive < swd->floppy_count; drive++) {
957 del_gendisk(swd->unit[drive].disk);
Omar Sandoval103db8b2017-03-27 23:28:46 -0700958 blk_cleanup_queue(swd->unit[drive].disk->queue);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100959 put_disk(swd->unit[drive].disk);
960 }
961
962 unregister_blkdev(FLOPPY_MAJOR, "fd");
963
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100964 /* eject floppies */
965
966 for (drive = 0; drive < swd->floppy_count; drive++)
967 floppy_eject(&swd->unit[drive]);
968
Finn Thain2724daf2009-11-04 00:39:56 +1100969 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100970 if (res)
971 release_mem_region(res->start, resource_size(res));
972
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100973 kfree(swd);
974
975 return 0;
976}
977
978static struct platform_driver swim_driver = {
979 .probe = swim_probe,
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800980 .remove = swim_remove,
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100981 .driver = {
982 .name = CARDNAME,
Laurent Vivier8852ecd2008-11-15 16:10:10 +0100983 },
984};
985
986static int __init swim_init(void)
987{
988 printk(KERN_INFO "SWIM floppy driver %s\n", DRIVER_VERSION);
989
990 return platform_driver_register(&swim_driver);
991}
992module_init(swim_init);
993
994static void __exit swim_exit(void)
995{
996 platform_driver_unregister(&swim_driver);
997}
998module_exit(swim_exit);
999
1000MODULE_DESCRIPTION("Driver for SWIM floppy controller");
1001MODULE_LICENSE("GPL");
1002MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
1003MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);