blob: cc6a3864822cf72a7412fff054b07e520335e056 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for the SWIM3 (Super Woz Integrated Machine 3)
3 * floppy controller found on Power Macintoshes.
4 *
5 * Copyright (C) 1996 Paul Mackerras.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13/*
14 * TODO:
15 * handle 2 drives
16 * handle GCR disks
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/stddef.h>
20#include <linux/kernel.h>
21#include <linux/sched.h>
22#include <linux/timer.h>
23#include <linux/delay.h>
24#include <linux/fd.h>
25#include <linux/ioctl.h>
26#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/interrupt.h>
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +020028#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
Paul Mackerras515729e2005-11-08 12:15:36 +110030#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/io.h>
32#include <asm/dbdma.h>
33#include <asm/prom.h>
34#include <asm/uaccess.h>
35#include <asm/mediabay.h>
36#include <asm/machdep.h>
37#include <asm/pmac_feature.h>
38
39static struct request_queue *swim3_queue;
40static struct gendisk *disks[2];
41static struct request *fd_req;
42
43#define MAX_FLOPPIES 2
44
45enum swim_state {
46 idle,
47 locating,
48 seeking,
49 settling,
50 do_transfer,
51 jogging,
52 available,
53 revalidating,
54 ejecting
55};
56
57#define REG(x) unsigned char x; char x ## _pad[15];
58
59/*
60 * The names for these registers mostly represent speculation on my part.
61 * It will be interesting to see how close they are to the names Apple uses.
62 */
63struct swim3 {
64 REG(data);
65 REG(timer); /* counts down at 1MHz */
66 REG(error);
67 REG(mode);
68 REG(select); /* controls CA0, CA1, CA2 and LSTRB signals */
69 REG(setup);
70 REG(control); /* writing bits clears them */
71 REG(status); /* writing bits sets them in control */
72 REG(intr);
73 REG(nseek); /* # tracks to seek */
74 REG(ctrack); /* current track number */
75 REG(csect); /* current sector number */
76 REG(gap3); /* size of gap 3 in track format */
77 REG(sector); /* sector # to read or write */
78 REG(nsect); /* # sectors to read or write */
79 REG(intr_enable);
80};
81
82#define control_bic control
83#define control_bis status
84
85/* Bits in select register */
86#define CA_MASK 7
87#define LSTRB 8
88
89/* Bits in control register */
90#define DO_SEEK 0x80
91#define FORMAT 0x40
92#define SELECT 0x20
93#define WRITE_SECTORS 0x10
94#define DO_ACTION 0x08
95#define DRIVE2_ENABLE 0x04
96#define DRIVE_ENABLE 0x02
97#define INTR_ENABLE 0x01
98
99/* Bits in status register */
100#define FIFO_1BYTE 0x80
101#define FIFO_2BYTE 0x40
102#define ERROR 0x20
103#define DATA 0x08
104#define RDDATA 0x04
105#define INTR_PENDING 0x02
106#define MARK_BYTE 0x01
107
108/* Bits in intr and intr_enable registers */
109#define ERROR_INTR 0x20
110#define DATA_CHANGED 0x10
111#define TRANSFER_DONE 0x08
112#define SEEN_SECTOR 0x04
113#define SEEK_DONE 0x02
114#define TIMER_DONE 0x01
115
116/* Bits in error register */
117#define ERR_DATA_CRC 0x80
118#define ERR_ADDR_CRC 0x40
119#define ERR_OVERRUN 0x04
120#define ERR_UNDERRUN 0x01
121
122/* Bits in setup register */
123#define S_SW_RESET 0x80
124#define S_GCR_WRITE 0x40
125#define S_IBM_DRIVE 0x20
126#define S_TEST_MODE 0x10
127#define S_FCLK_DIV2 0x08
128#define S_GCR 0x04
129#define S_COPY_PROT 0x02
130#define S_INV_WDATA 0x01
131
132/* Select values for swim3_action */
133#define SEEK_POSITIVE 0
134#define SEEK_NEGATIVE 4
135#define STEP 1
136#define MOTOR_ON 2
137#define MOTOR_OFF 6
138#define INDEX 3
139#define EJECT 7
140#define SETMFM 9
141#define SETGCR 13
142
143/* Select values for swim3_select and swim3_readbit */
144#define STEP_DIR 0
145#define STEPPING 1
146#define MOTOR_ON 2
147#define RELAX 3 /* also eject in progress */
148#define READ_DATA_0 4
149#define TWOMEG_DRIVE 5
150#define SINGLE_SIDED 6 /* drive or diskette is 4MB type? */
151#define DRIVE_PRESENT 7
152#define DISK_IN 8
153#define WRITE_PROT 9
154#define TRACK_ZERO 10
155#define TACHO 11
156#define READ_DATA_1 12
157#define MFM_MODE 13
158#define SEEK_COMPLETE 14
159#define ONEMEG_MEDIA 15
160
161/* Definitions of values used in writing and formatting */
162#define DATA_ESCAPE 0x99
163#define GCR_SYNC_EXC 0x3f
164#define GCR_SYNC_CONV 0x80
165#define GCR_FIRST_MARK 0xd5
166#define GCR_SECOND_MARK 0xaa
167#define GCR_ADDR_MARK "\xd5\xaa\x00"
168#define GCR_DATA_MARK "\xd5\xaa\x0b"
169#define GCR_SLIP_BYTE "\x27\xaa"
170#define GCR_SELF_SYNC "\x3f\xbf\x1e\x34\x3c\x3f"
171
172#define DATA_99 "\x99\x99"
173#define MFM_ADDR_MARK "\x99\xa1\x99\xa1\x99\xa1\x99\xfe"
174#define MFM_INDEX_MARK "\x99\xc2\x99\xc2\x99\xc2\x99\xfc"
175#define MFM_GAP_LEN 12
176
177struct floppy_state {
178 enum swim_state state;
Paul Mackerras515729e2005-11-08 12:15:36 +1100179 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct swim3 __iomem *swim3; /* hardware registers */
181 struct dbdma_regs __iomem *dma; /* DMA controller registers */
182 int swim3_intr; /* interrupt number for SWIM3 */
183 int dma_intr; /* interrupt number for DMA channel */
184 int cur_cyl; /* cylinder head is on, or -1 */
185 int cur_sector; /* last sector we saw go past */
186 int req_cyl; /* the cylinder for the current r/w request */
187 int head; /* head number ditto */
188 int req_sector; /* sector number ditto */
189 int scount; /* # sectors we're transferring at present */
190 int retries;
191 int settle_time;
192 int secpercyl; /* disk geometry information */
193 int secpertrack;
194 int total_secs;
195 int write_prot; /* 1 if write-protected, 0 if not, -1 dunno */
196 struct dbdma_cmd *dma_cmd;
197 int ref_count;
198 int expect_cyl;
199 struct timer_list timeout;
200 int timeout_pending;
201 int ejected;
202 wait_queue_head_t wait;
203 int wanted;
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +0000204 struct macio_dev *mdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 char dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)];
206};
207
208static struct floppy_state floppy_states[MAX_FLOPPIES];
209static int floppy_count = 0;
210static DEFINE_SPINLOCK(swim3_lock);
211
212static unsigned short write_preamble[] = {
213 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */
214 0, 0, 0, 0, 0, 0, /* sync field */
215 0x99a1, 0x99a1, 0x99a1, 0x99fb, /* data address mark */
216 0x990f /* no escape for 512 bytes */
217};
218
219static unsigned short write_postamble[] = {
220 0x9904, /* insert CRC */
221 0x4e4e, 0x4e4e,
222 0x9908, /* stop writing */
223 0, 0, 0, 0, 0, 0
224};
225
226static void swim3_select(struct floppy_state *fs, int sel);
227static void swim3_action(struct floppy_state *fs, int action);
228static int swim3_readbit(struct floppy_state *fs, int bit);
Jens Axboe165125e2007-07-24 09:28:11 +0200229static void do_fd_request(struct request_queue * q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static void start_request(struct floppy_state *fs);
231static void set_timeout(struct floppy_state *fs, int nticks,
232 void (*proc)(unsigned long));
233static void scan_track(struct floppy_state *fs);
234static void seek_track(struct floppy_state *fs, int n);
235static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count);
236static void setup_transfer(struct floppy_state *fs);
237static void act(struct floppy_state *fs);
238static void scan_timeout(unsigned long data);
239static void seek_timeout(unsigned long data);
240static void settle_timeout(unsigned long data);
241static void xfer_timeout(unsigned long data);
David Howells7d12e782006-10-05 14:55:46 +0100242static irqreturn_t swim3_interrupt(int irq, void *dev_id);
243/*static void fd_dma_interrupt(int irq, void *dev_id);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static int grab_drive(struct floppy_state *fs, enum swim_state state,
245 int interruptible);
246static void release_drive(struct floppy_state *fs);
247static int fd_eject(struct floppy_state *fs);
Al Virob4d9a442008-03-02 10:20:44 -0500248static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 unsigned int cmd, unsigned long param);
Al Virob4d9a442008-03-02 10:20:44 -0500250static int floppy_open(struct block_device *bdev, fmode_t mode);
251static int floppy_release(struct gendisk *disk, fmode_t mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static int floppy_check_change(struct gendisk *disk);
253static int floppy_revalidate(struct gendisk *disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Tejun Heof4bd4b92009-05-08 11:54:04 +0900255static bool swim3_end_request(int err, unsigned int nr_bytes)
256{
257 if (__blk_end_request(fd_req, err, nr_bytes))
258 return true;
259
260 fd_req = NULL;
261 return false;
262}
263
264static bool swim3_end_request_cur(int err)
265{
266 return swim3_end_request(err, blk_rq_cur_bytes(fd_req));
267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static void swim3_select(struct floppy_state *fs, int sel)
270{
271 struct swim3 __iomem *sw = fs->swim3;
272
273 out_8(&sw->select, RELAX);
274 if (sel & 8)
275 out_8(&sw->control_bis, SELECT);
276 else
277 out_8(&sw->control_bic, SELECT);
278 out_8(&sw->select, sel & CA_MASK);
279}
280
281static void swim3_action(struct floppy_state *fs, int action)
282{
283 struct swim3 __iomem *sw = fs->swim3;
284
285 swim3_select(fs, action);
286 udelay(1);
287 out_8(&sw->select, sw->select | LSTRB);
288 udelay(2);
289 out_8(&sw->select, sw->select & ~LSTRB);
290 udelay(1);
291}
292
293static int swim3_readbit(struct floppy_state *fs, int bit)
294{
295 struct swim3 __iomem *sw = fs->swim3;
296 int stat;
297
298 swim3_select(fs, bit);
299 udelay(1);
300 stat = in_8(&sw->status);
301 return (stat & DATA) == 0;
302}
303
Jens Axboe165125e2007-07-24 09:28:11 +0200304static void do_fd_request(struct request_queue * q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 int i;
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +0000307
308 for(i=0; i<floppy_count; i++) {
309 struct floppy_state *fs = &floppy_states[i];
310 if (fs->mdev->media_bay &&
311 check_media_bay(fs->mdev->media_bay) != MB_FD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 continue;
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +0000313 start_request(fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317static void start_request(struct floppy_state *fs)
318{
319 struct request *req;
320 unsigned long x;
321
322 if (fs->state == idle && fs->wanted) {
323 fs->state = available;
324 wake_up(&fs->wait);
325 return;
326 }
Tejun Heof4bd4b92009-05-08 11:54:04 +0900327 while (fs->state == idle) {
328 if (!fd_req) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900329 fd_req = blk_fetch_request(swim3_queue);
Tejun Heof4bd4b92009-05-08 11:54:04 +0900330 if (!fd_req)
331 break;
Tejun Heof4bd4b92009-05-08 11:54:04 +0900332 }
333 req = fd_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#if 0
Tejun Heo83096eb2009-05-07 22:24:39 +0900335 printk("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%u buf=%p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 req->rq_disk->disk_name, req->cmd,
Tejun Heo83096eb2009-05-07 22:24:39 +0900337 (long)blk_rq_pos(req), blk_rq_sectors(req), req->buffer);
338 printk(" errors=%d current_nr_sectors=%u\n",
339 req->errors, blk_rq_cur_sectors(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#endif
341
Tejun Heo83096eb2009-05-07 22:24:39 +0900342 if (blk_rq_pos(req) >= fs->total_secs) {
Tejun Heof4bd4b92009-05-08 11:54:04 +0900343 swim3_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 continue;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (fs->ejected) {
Tejun Heof4bd4b92009-05-08 11:54:04 +0900347 swim3_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 continue;
349 }
350
351 if (rq_data_dir(req) == WRITE) {
352 if (fs->write_prot < 0)
353 fs->write_prot = swim3_readbit(fs, WRITE_PROT);
354 if (fs->write_prot) {
Tejun Heof4bd4b92009-05-08 11:54:04 +0900355 swim3_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 continue;
357 }
358 }
359
Tejun Heo83096eb2009-05-07 22:24:39 +0900360 /* Do not remove the cast. blk_rq_pos(req) is now a
361 * sector_t and can be 64 bits, but it will never go
362 * past 32 bits for this driver anyway, so we can
363 * safely cast it down and not have to do a 64/32
364 * division
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900366 fs->req_cyl = ((long)blk_rq_pos(req)) / fs->secpercyl;
367 x = ((long)blk_rq_pos(req)) % fs->secpercyl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 fs->head = x / fs->secpertrack;
369 fs->req_sector = x % fs->secpertrack + 1;
370 fd_req = req;
371 fs->state = do_transfer;
372 fs->retries = 0;
373
374 act(fs);
375 }
376}
377
378static void set_timeout(struct floppy_state *fs, int nticks,
379 void (*proc)(unsigned long))
380{
381 unsigned long flags;
382
Paul Mackerras515729e2005-11-08 12:15:36 +1100383 spin_lock_irqsave(&fs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (fs->timeout_pending)
385 del_timer(&fs->timeout);
386 fs->timeout.expires = jiffies + nticks;
387 fs->timeout.function = proc;
388 fs->timeout.data = (unsigned long) fs;
389 add_timer(&fs->timeout);
390 fs->timeout_pending = 1;
Paul Mackerras515729e2005-11-08 12:15:36 +1100391 spin_unlock_irqrestore(&fs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394static inline void scan_track(struct floppy_state *fs)
395{
396 struct swim3 __iomem *sw = fs->swim3;
397
398 swim3_select(fs, READ_DATA_0);
399 in_8(&sw->intr); /* clear SEEN_SECTOR bit */
400 in_8(&sw->error);
401 out_8(&sw->intr_enable, SEEN_SECTOR);
402 out_8(&sw->control_bis, DO_ACTION);
403 /* enable intr when track found */
404 set_timeout(fs, HZ, scan_timeout); /* enable timeout */
405}
406
407static inline void seek_track(struct floppy_state *fs, int n)
408{
409 struct swim3 __iomem *sw = fs->swim3;
410
411 if (n >= 0) {
412 swim3_action(fs, SEEK_POSITIVE);
413 sw->nseek = n;
414 } else {
415 swim3_action(fs, SEEK_NEGATIVE);
416 sw->nseek = -n;
417 }
418 fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1;
419 swim3_select(fs, STEP);
420 in_8(&sw->error);
421 /* enable intr when seek finished */
422 out_8(&sw->intr_enable, SEEK_DONE);
423 out_8(&sw->control_bis, DO_SEEK);
424 set_timeout(fs, 3*HZ, seek_timeout); /* enable timeout */
425 fs->settle_time = 0;
426}
427
428static inline void init_dma(struct dbdma_cmd *cp, int cmd,
429 void *buf, int count)
430{
431 st_le16(&cp->req_count, count);
432 st_le16(&cp->command, cmd);
433 st_le32(&cp->phy_addr, virt_to_bus(buf));
434 cp->xfer_status = 0;
435}
436
437static inline void setup_transfer(struct floppy_state *fs)
438{
439 int n;
440 struct swim3 __iomem *sw = fs->swim3;
441 struct dbdma_cmd *cp = fs->dma_cmd;
442 struct dbdma_regs __iomem *dr = fs->dma;
443
Tejun Heo83096eb2009-05-07 22:24:39 +0900444 if (blk_rq_cur_sectors(fd_req) <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 printk(KERN_ERR "swim3: transfer 0 sectors?\n");
446 return;
447 }
448 if (rq_data_dir(fd_req) == WRITE)
449 n = 1;
450 else {
451 n = fs->secpertrack - fs->req_sector + 1;
Tejun Heo83096eb2009-05-07 22:24:39 +0900452 if (n > blk_rq_cur_sectors(fd_req))
453 n = blk_rq_cur_sectors(fd_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455 fs->scount = n;
456 swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
457 out_8(&sw->sector, fs->req_sector);
458 out_8(&sw->nsect, n);
459 out_8(&sw->gap3, 0);
460 out_le32(&dr->cmdptr, virt_to_bus(cp));
461 if (rq_data_dir(fd_req) == WRITE) {
462 /* Set up 3 dma commands: write preamble, data, postamble */
463 init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));
464 ++cp;
465 init_dma(cp, OUTPUT_MORE, fd_req->buffer, 512);
466 ++cp;
467 init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble));
468 } else {
469 init_dma(cp, INPUT_LAST, fd_req->buffer, n * 512);
470 }
471 ++cp;
472 out_le16(&cp->command, DBDMA_STOP);
473 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
474 in_8(&sw->error);
475 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
476 if (rq_data_dir(fd_req) == WRITE)
477 out_8(&sw->control_bis, WRITE_SECTORS);
478 in_8(&sw->intr);
479 out_le32(&dr->control, (RUN << 16) | RUN);
480 /* enable intr when transfer complete */
481 out_8(&sw->intr_enable, TRANSFER_DONE);
482 out_8(&sw->control_bis, DO_ACTION);
483 set_timeout(fs, 2*HZ, xfer_timeout); /* enable timeout */
484}
485
486static void act(struct floppy_state *fs)
487{
488 for (;;) {
489 switch (fs->state) {
490 case idle:
491 return; /* XXX shouldn't get here */
492
493 case locating:
494 if (swim3_readbit(fs, TRACK_ZERO)) {
495 fs->cur_cyl = 0;
496 if (fs->req_cyl == 0)
497 fs->state = do_transfer;
498 else
499 fs->state = seeking;
500 break;
501 }
502 scan_track(fs);
503 return;
504
505 case seeking:
506 if (fs->cur_cyl < 0) {
507 fs->expect_cyl = -1;
508 fs->state = locating;
509 break;
510 }
511 if (fs->req_cyl == fs->cur_cyl) {
512 printk("whoops, seeking 0\n");
513 fs->state = do_transfer;
514 break;
515 }
516 seek_track(fs, fs->req_cyl - fs->cur_cyl);
517 return;
518
519 case settling:
520 /* check for SEEK_COMPLETE after 30ms */
521 fs->settle_time = (HZ + 32) / 33;
522 set_timeout(fs, fs->settle_time, settle_timeout);
523 return;
524
525 case do_transfer:
526 if (fs->cur_cyl != fs->req_cyl) {
527 if (fs->retries > 5) {
Tejun Heof4bd4b92009-05-08 11:54:04 +0900528 swim3_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 fs->state = idle;
530 return;
531 }
532 fs->state = seeking;
533 break;
534 }
535 setup_transfer(fs);
536 return;
537
538 case jogging:
539 seek_track(fs, -5);
540 return;
541
542 default:
543 printk(KERN_ERR"swim3: unknown state %d\n", fs->state);
544 return;
545 }
546 }
547}
548
549static void scan_timeout(unsigned long data)
550{
551 struct floppy_state *fs = (struct floppy_state *) data;
552 struct swim3 __iomem *sw = fs->swim3;
553
554 fs->timeout_pending = 0;
555 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
556 out_8(&sw->select, RELAX);
557 out_8(&sw->intr_enable, 0);
558 fs->cur_cyl = -1;
559 if (fs->retries > 5) {
Tejun Heof4bd4b92009-05-08 11:54:04 +0900560 swim3_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 fs->state = idle;
562 start_request(fs);
563 } else {
564 fs->state = jogging;
565 act(fs);
566 }
567}
568
569static void seek_timeout(unsigned long data)
570{
571 struct floppy_state *fs = (struct floppy_state *) data;
572 struct swim3 __iomem *sw = fs->swim3;
573
574 fs->timeout_pending = 0;
575 out_8(&sw->control_bic, DO_SEEK);
576 out_8(&sw->select, RELAX);
577 out_8(&sw->intr_enable, 0);
578 printk(KERN_ERR "swim3: seek timeout\n");
Tejun Heof4bd4b92009-05-08 11:54:04 +0900579 swim3_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 fs->state = idle;
581 start_request(fs);
582}
583
584static void settle_timeout(unsigned long data)
585{
586 struct floppy_state *fs = (struct floppy_state *) data;
587 struct swim3 __iomem *sw = fs->swim3;
588
589 fs->timeout_pending = 0;
590 if (swim3_readbit(fs, SEEK_COMPLETE)) {
591 out_8(&sw->select, RELAX);
592 fs->state = locating;
593 act(fs);
594 return;
595 }
596 out_8(&sw->select, RELAX);
597 if (fs->settle_time < 2*HZ) {
598 ++fs->settle_time;
599 set_timeout(fs, 1, settle_timeout);
600 return;
601 }
602 printk(KERN_ERR "swim3: seek settle timeout\n");
Tejun Heof4bd4b92009-05-08 11:54:04 +0900603 swim3_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 fs->state = idle;
605 start_request(fs);
606}
607
608static void xfer_timeout(unsigned long data)
609{
610 struct floppy_state *fs = (struct floppy_state *) data;
611 struct swim3 __iomem *sw = fs->swim3;
612 struct dbdma_regs __iomem *dr = fs->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int n;
614
615 fs->timeout_pending = 0;
616 out_le32(&dr->control, RUN << 16);
617 /* We must wait a bit for dbdma to stop */
618 for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++)
619 udelay(1);
620 out_8(&sw->intr_enable, 0);
621 out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
622 out_8(&sw->select, RELAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 printk(KERN_ERR "swim3: timeout %sing sector %ld\n",
Tejun Heo83096eb2009-05-07 22:24:39 +0900624 (rq_data_dir(fd_req)==WRITE? "writ": "read"),
625 (long)blk_rq_pos(fd_req));
Tejun Heof4bd4b92009-05-08 11:54:04 +0900626 swim3_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 fs->state = idle;
628 start_request(fs);
629}
630
David Howells7d12e782006-10-05 14:55:46 +0100631static irqreturn_t swim3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 struct floppy_state *fs = (struct floppy_state *) dev_id;
634 struct swim3 __iomem *sw = fs->swim3;
635 int intr, err, n;
636 int stat, resid;
637 struct dbdma_regs __iomem *dr;
638 struct dbdma_cmd *cp;
639
640 intr = in_8(&sw->intr);
641 err = (intr & ERROR_INTR)? in_8(&sw->error): 0;
642 if ((intr & ERROR_INTR) && fs->state != do_transfer)
Olaf Hering14b1ffb2006-10-02 22:57:23 +0200643 printk(KERN_ERR "swim3_interrupt, state=%d, dir=%x, intr=%x, err=%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 fs->state, rq_data_dir(fd_req), intr, err);
645 switch (fs->state) {
646 case locating:
647 if (intr & SEEN_SECTOR) {
648 out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
649 out_8(&sw->select, RELAX);
650 out_8(&sw->intr_enable, 0);
651 del_timer(&fs->timeout);
652 fs->timeout_pending = 0;
653 if (sw->ctrack == 0xff) {
654 printk(KERN_ERR "swim3: seen sector but cyl=ff?\n");
655 fs->cur_cyl = -1;
656 if (fs->retries > 5) {
Tejun Heof4bd4b92009-05-08 11:54:04 +0900657 swim3_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 fs->state = idle;
659 start_request(fs);
660 } else {
661 fs->state = jogging;
662 act(fs);
663 }
664 break;
665 }
666 fs->cur_cyl = sw->ctrack;
667 fs->cur_sector = sw->csect;
668 if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
669 printk(KERN_ERR "swim3: expected cyl %d, got %d\n",
670 fs->expect_cyl, fs->cur_cyl);
671 fs->state = do_transfer;
672 act(fs);
673 }
674 break;
675 case seeking:
676 case jogging:
677 if (sw->nseek == 0) {
678 out_8(&sw->control_bic, DO_SEEK);
679 out_8(&sw->select, RELAX);
680 out_8(&sw->intr_enable, 0);
681 del_timer(&fs->timeout);
682 fs->timeout_pending = 0;
683 if (fs->state == seeking)
684 ++fs->retries;
685 fs->state = settling;
686 act(fs);
687 }
688 break;
689 case settling:
690 out_8(&sw->intr_enable, 0);
691 del_timer(&fs->timeout);
692 fs->timeout_pending = 0;
693 act(fs);
694 break;
695 case do_transfer:
696 if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
697 break;
698 out_8(&sw->intr_enable, 0);
699 out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
700 out_8(&sw->select, RELAX);
701 del_timer(&fs->timeout);
702 fs->timeout_pending = 0;
703 dr = fs->dma;
704 cp = fs->dma_cmd;
705 if (rq_data_dir(fd_req) == WRITE)
706 ++cp;
707 /*
708 * Check that the main data transfer has finished.
709 * On writing, the swim3 sometimes doesn't use
710 * up all the bytes of the postamble, so we can still
711 * see DMA active here. That doesn't matter as long
712 * as all the sector data has been transferred.
713 */
714 if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) {
715 /* wait a little while for DMA to complete */
716 for (n = 0; n < 100; ++n) {
717 if (cp->xfer_status != 0)
718 break;
719 udelay(1);
720 barrier();
721 }
722 }
723 /* turn off DMA */
724 out_le32(&dr->control, (RUN | PAUSE) << 16);
725 stat = ld_le16(&cp->xfer_status);
726 resid = ld_le16(&cp->res_count);
727 if (intr & ERROR_INTR) {
728 n = fs->scount - 1 - resid / 512;
729 if (n > 0) {
Tejun Heo467ca752009-04-28 13:06:12 +0900730 blk_update_request(fd_req, 0, n << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 fs->req_sector += n;
732 }
733 if (fs->retries < 5) {
734 ++fs->retries;
735 act(fs);
736 } else {
737 printk("swim3: error %sing block %ld (err=%x)\n",
738 rq_data_dir(fd_req) == WRITE? "writ": "read",
Tejun Heo83096eb2009-05-07 22:24:39 +0900739 (long)blk_rq_pos(fd_req), err);
Tejun Heof4bd4b92009-05-08 11:54:04 +0900740 swim3_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 fs->state = idle;
742 }
743 } else {
744 if ((stat & ACTIVE) == 0 || resid != 0) {
745 /* musta been an error */
746 printk(KERN_ERR "swim3: fd dma: stat=%x resid=%d\n", stat, resid);
Olaf Hering14b1ffb2006-10-02 22:57:23 +0200747 printk(KERN_ERR " state=%d, dir=%x, intr=%x, err=%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 fs->state, rq_data_dir(fd_req), intr, err);
Tejun Heof4bd4b92009-05-08 11:54:04 +0900749 swim3_end_request_cur(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 fs->state = idle;
751 start_request(fs);
752 break;
753 }
Tejun Heof4bd4b92009-05-08 11:54:04 +0900754 if (swim3_end_request(0, fs->scount << 9)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 fs->req_sector += fs->scount;
756 if (fs->req_sector > fs->secpertrack) {
757 fs->req_sector -= fs->secpertrack;
758 if (++fs->head > 1) {
759 fs->head = 0;
760 ++fs->req_cyl;
761 }
762 }
763 act(fs);
Tejun Heo467ca752009-04-28 13:06:12 +0900764 } else
765 fs->state = idle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 if (fs->state == idle)
768 start_request(fs);
769 break;
770 default:
771 printk(KERN_ERR "swim3: don't know what to do in state %d\n", fs->state);
772 }
773 return IRQ_HANDLED;
774}
775
776/*
David Howells7d12e782006-10-05 14:55:46 +0100777static void fd_dma_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779}
780*/
781
782static int grab_drive(struct floppy_state *fs, enum swim_state state,
783 int interruptible)
784{
785 unsigned long flags;
786
Paul Mackerras515729e2005-11-08 12:15:36 +1100787 spin_lock_irqsave(&fs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (fs->state != idle) {
789 ++fs->wanted;
790 while (fs->state != available) {
791 if (interruptible && signal_pending(current)) {
792 --fs->wanted;
Paul Mackerras515729e2005-11-08 12:15:36 +1100793 spin_unlock_irqrestore(&fs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return -EINTR;
795 }
796 interruptible_sleep_on(&fs->wait);
797 }
798 --fs->wanted;
799 }
800 fs->state = state;
Paul Mackerras515729e2005-11-08 12:15:36 +1100801 spin_unlock_irqrestore(&fs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
803}
804
805static void release_drive(struct floppy_state *fs)
806{
807 unsigned long flags;
808
Paul Mackerras515729e2005-11-08 12:15:36 +1100809 spin_lock_irqsave(&fs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 fs->state = idle;
811 start_request(fs);
Paul Mackerras515729e2005-11-08 12:15:36 +1100812 spin_unlock_irqrestore(&fs->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815static int fd_eject(struct floppy_state *fs)
816{
817 int err, n;
818
819 err = grab_drive(fs, ejecting, 1);
820 if (err)
821 return err;
822 swim3_action(fs, EJECT);
823 for (n = 20; n > 0; --n) {
824 if (signal_pending(current)) {
825 err = -EINTR;
826 break;
827 }
828 swim3_select(fs, RELAX);
Nishanth Aravamudan86e84862005-09-10 00:27:28 -0700829 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (swim3_readbit(fs, DISK_IN) == 0)
831 break;
832 }
833 swim3_select(fs, RELAX);
834 udelay(150);
835 fs->ejected = 1;
836 release_drive(fs);
837 return err;
838}
839
840static struct floppy_struct floppy_type =
841 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }; /* 7 1.44MB 3.5" */
842
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200843static int floppy_locked_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 unsigned int cmd, unsigned long param)
845{
Al Virob4d9a442008-03-02 10:20:44 -0500846 struct floppy_state *fs = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int err;
848
849 if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
850 return -EPERM;
851
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +0000852 if (fs->mdev->media_bay &&
853 check_media_bay(fs->mdev->media_bay) != MB_FD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return -ENXIO;
855
856 switch (cmd) {
857 case FDEJECT:
858 if (fs->ref_count != 1)
859 return -EBUSY;
860 err = fd_eject(fs);
861 return err;
862 case FDGETPRM:
863 if (copy_to_user((void __user *) param, &floppy_type,
864 sizeof(struct floppy_struct)))
865 return -EFAULT;
866 return 0;
867 }
868 return -ENOTTY;
869}
870
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200871static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
872 unsigned int cmd, unsigned long param)
873{
874 int ret;
875
876 lock_kernel();
877 ret = floppy_locked_ioctl(bdev, mode, cmd, param);
878 unlock_kernel();
879
880 return ret;
881}
882
Al Virob4d9a442008-03-02 10:20:44 -0500883static int floppy_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Al Virob4d9a442008-03-02 10:20:44 -0500885 struct floppy_state *fs = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 struct swim3 __iomem *sw = fs->swim3;
887 int n, err = 0;
888
889 if (fs->ref_count == 0) {
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +0000890 if (fs->mdev->media_bay &&
891 check_media_bay(fs->mdev->media_bay) != MB_FD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -ENXIO;
893 out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
894 out_8(&sw->control_bic, 0xff);
895 out_8(&sw->mode, 0x95);
896 udelay(10);
897 out_8(&sw->intr_enable, 0);
898 out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
899 swim3_action(fs, MOTOR_ON);
900 fs->write_prot = -1;
901 fs->cur_cyl = -1;
902 for (n = 0; n < 2 * HZ; ++n) {
903 if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE))
904 break;
905 if (signal_pending(current)) {
906 err = -EINTR;
907 break;
908 }
909 swim3_select(fs, RELAX);
Nishanth Aravamudan86e84862005-09-10 00:27:28 -0700910 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
913 || swim3_readbit(fs, DISK_IN) == 0))
914 err = -ENXIO;
915 swim3_action(fs, SETMFM);
916 swim3_select(fs, RELAX);
917
Al Virob4d9a442008-03-02 10:20:44 -0500918 } else if (fs->ref_count == -1 || mode & FMODE_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return -EBUSY;
920
Al Virob4d9a442008-03-02 10:20:44 -0500921 if (err == 0 && (mode & FMODE_NDELAY) == 0
922 && (mode & (FMODE_READ|FMODE_WRITE))) {
923 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (fs->ejected)
925 err = -ENXIO;
926 }
927
Al Virob4d9a442008-03-02 10:20:44 -0500928 if (err == 0 && (mode & FMODE_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (fs->write_prot < 0)
930 fs->write_prot = swim3_readbit(fs, WRITE_PROT);
931 if (fs->write_prot)
932 err = -EROFS;
933 }
934
935 if (err) {
936 if (fs->ref_count == 0) {
937 swim3_action(fs, MOTOR_OFF);
938 out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
939 swim3_select(fs, RELAX);
940 }
941 return err;
942 }
943
Al Virob4d9a442008-03-02 10:20:44 -0500944 if (mode & FMODE_EXCL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 fs->ref_count = -1;
946 else
947 ++fs->ref_count;
948
949 return 0;
950}
951
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200952static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
953{
954 int ret;
955
956 lock_kernel();
957 ret = floppy_open(bdev, mode);
958 unlock_kernel();
959
960 return ret;
961}
962
Al Virob4d9a442008-03-02 10:20:44 -0500963static int floppy_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Al Virob4d9a442008-03-02 10:20:44 -0500965 struct floppy_state *fs = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 struct swim3 __iomem *sw = fs->swim3;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200967 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (fs->ref_count > 0 && --fs->ref_count == 0) {
969 swim3_action(fs, MOTOR_OFF);
970 out_8(&sw->control_bic, 0xff);
971 swim3_select(fs, RELAX);
972 }
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200973 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return 0;
975}
976
977static int floppy_check_change(struct gendisk *disk)
978{
979 struct floppy_state *fs = disk->private_data;
980 return fs->ejected;
981}
982
983static int floppy_revalidate(struct gendisk *disk)
984{
985 struct floppy_state *fs = disk->private_data;
986 struct swim3 __iomem *sw;
987 int ret, n;
988
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +0000989 if (fs->mdev->media_bay &&
990 check_media_bay(fs->mdev->media_bay) != MB_FD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 sw = fs->swim3;
994 grab_drive(fs, revalidating, 0);
995 out_8(&sw->intr_enable, 0);
996 out_8(&sw->control_bis, DRIVE_ENABLE);
997 swim3_action(fs, MOTOR_ON); /* necessary? */
998 fs->write_prot = -1;
999 fs->cur_cyl = -1;
1000 mdelay(1);
1001 for (n = HZ; n > 0; --n) {
1002 if (swim3_readbit(fs, SEEK_COMPLETE))
1003 break;
1004 if (signal_pending(current))
1005 break;
1006 swim3_select(fs, RELAX);
Nishanth Aravamudan86e84862005-09-10 00:27:28 -07001007 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009 ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
1010 || swim3_readbit(fs, DISK_IN) == 0;
1011 if (ret)
1012 swim3_action(fs, MOTOR_OFF);
1013 else {
1014 fs->ejected = 0;
1015 swim3_action(fs, SETMFM);
1016 }
1017 swim3_select(fs, RELAX);
1018
1019 release_drive(fs);
1020 return ret;
1021}
1022
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001023static const struct block_device_operations floppy_fops = {
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001024 .open = floppy_unlocked_open,
Al Virob4d9a442008-03-02 10:20:44 -05001025 .release = floppy_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001026 .ioctl = floppy_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 .media_changed = floppy_check_change,
1028 .revalidate_disk= floppy_revalidate,
1029};
1030
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001031static int swim3_add_device(struct macio_dev *mdev, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Grant Likely61c7a082010-04-13 16:12:29 -07001033 struct device_node *swim = mdev->ofdev.dev.of_node;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001034 struct floppy_state *fs = &floppy_states[index];
1035 int rc = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001037 /* Check & Request resources */
1038 if (macio_resource_count(mdev) < 2) {
1039 printk(KERN_WARNING "ifd%d: no address for %s\n",
1040 index, swim->full_name);
1041 return -ENXIO;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001042 }
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001043 if (macio_irq_count(mdev) < 2) {
1044 printk(KERN_WARNING "fd%d: no intrs for device %s\n",
1045 index, swim->full_name);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11001046 }
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001047 if (macio_request_resource(mdev, 0, "swim3 (mmio)")) {
1048 printk(KERN_ERR "fd%d: can't request mmio resource for %s\n",
1049 index, swim->full_name);
1050 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001052 if (macio_request_resource(mdev, 1, "swim3 (dma)")) {
1053 printk(KERN_ERR "fd%d: can't request dma resource for %s\n",
1054 index, swim->full_name);
1055 macio_release_resource(mdev, 0);
1056 return -EBUSY;
1057 }
1058 dev_set_drvdata(&mdev->ofdev.dev, fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +00001060 if (mdev->media_bay == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
1062
1063 memset(fs, 0, sizeof(*fs));
Paul Mackerras515729e2005-11-08 12:15:36 +11001064 spin_lock_init(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 fs->state = idle;
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001066 fs->swim3 = (struct swim3 __iomem *)
1067 ioremap(macio_resource_start(mdev, 0), 0x200);
1068 if (fs->swim3 == NULL) {
1069 printk("fd%d: couldn't map registers for %s\n",
1070 index, swim->full_name);
1071 rc = -ENOMEM;
1072 goto out_release;
1073 }
1074 fs->dma = (struct dbdma_regs __iomem *)
1075 ioremap(macio_resource_start(mdev, 1), 0x200);
1076 if (fs->dma == NULL) {
1077 printk("fd%d: couldn't map DMA for %s\n",
1078 index, swim->full_name);
1079 iounmap(fs->swim3);
1080 rc = -ENOMEM;
1081 goto out_release;
1082 }
1083 fs->swim3_intr = macio_irq(mdev, 0);
Joe Perchesa419aef2009-08-18 11:18:35 -07001084 fs->dma_intr = macio_irq(mdev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 fs->cur_cyl = -1;
1086 fs->cur_sector = -1;
1087 fs->secpercyl = 36;
1088 fs->secpertrack = 18;
1089 fs->total_secs = 2880;
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +00001090 fs->mdev = mdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 init_waitqueue_head(&fs->wait);
1092
1093 fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
1094 memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
1095 st_le16(&fs->dma_cmd[1].command, DBDMA_STOP);
1096
1097 if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001098 printk(KERN_ERR "fd%d: couldn't request irq %d for %s\n",
1099 index, fs->swim3_intr, swim->full_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001101 goto out_unmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return -EBUSY;
1103 }
1104/*
1105 if (request_irq(fs->dma_intr, fd_dma_interrupt, 0, "SWIM3-dma", fs)) {
1106 printk(KERN_ERR "Couldn't get irq %d for SWIM3 DMA",
1107 fs->dma_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return -EBUSY;
1109 }
1110*/
1111
1112 init_timer(&fs->timeout);
1113
1114 printk(KERN_INFO "fd%d: SWIM3 floppy controller %s\n", floppy_count,
Benjamin Herrenschmidtd58b0c32009-12-01 14:36:28 +00001115 mdev->media_bay ? "in media bay" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001117 return 0;
1118
1119 out_unmap:
1120 iounmap(fs->dma);
1121 iounmap(fs->swim3);
1122
1123 out_release:
1124 macio_release_resource(mdev, 0);
1125 macio_release_resource(mdev, 1);
1126
1127 return rc;
1128}
1129
1130static int __devinit swim3_attach(struct macio_dev *mdev, const struct of_device_id *match)
1131{
1132 int i, rc;
1133 struct gendisk *disk;
1134
1135 /* Add the drive */
1136 rc = swim3_add_device(mdev, floppy_count);
1137 if (rc)
1138 return rc;
1139
1140 /* Now create the queue if not there yet */
1141 if (swim3_queue == NULL) {
1142 /* If we failed, there isn't much we can do as the driver is still
1143 * too dumb to remove the device, just bail out
1144 */
1145 if (register_blkdev(FLOPPY_MAJOR, "fd"))
1146 return 0;
1147 swim3_queue = blk_init_queue(do_fd_request, &swim3_lock);
1148 if (swim3_queue == NULL) {
1149 unregister_blkdev(FLOPPY_MAJOR, "fd");
1150 return 0;
1151 }
1152 }
1153
1154 /* Now register that disk. Same comment about failure handling */
1155 i = floppy_count++;
1156 disk = disks[i] = alloc_disk(1);
1157 if (disk == NULL)
1158 return 0;
1159
1160 disk->major = FLOPPY_MAJOR;
1161 disk->first_minor = i;
1162 disk->fops = &floppy_fops;
1163 disk->private_data = &floppy_states[i];
1164 disk->queue = swim3_queue;
1165 disk->flags |= GENHD_FL_REMOVABLE;
1166 sprintf(disk->disk_name, "fd%d", i);
1167 set_capacity(disk, 2880);
1168 add_disk(disk);
1169
1170 return 0;
1171}
1172
1173static struct of_device_id swim3_match[] =
1174{
1175 {
1176 .name = "swim3",
1177 },
1178 {
1179 .compatible = "ohare-swim3"
1180 },
1181 {
1182 .compatible = "swim3"
1183 },
1184};
1185
1186static struct macio_driver swim3_driver =
1187{
Benjamin Herrenschmidtc2cdf6a2010-06-02 17:09:18 +10001188 .driver = {
1189 .name = "swim3",
1190 .of_match_table = swim3_match,
1191 },
Benjamin Herrenschmidt3e9a6922006-07-03 17:25:26 +10001192 .probe = swim3_attach,
1193#if 0
1194 .suspend = swim3_suspend,
1195 .resume = swim3_resume,
1196#endif
1197};
1198
1199
1200int swim3_init(void)
1201{
1202 macio_register_driver(&swim3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return 0;
1204}
1205
1206module_init(swim3_init)
1207
1208MODULE_LICENSE("GPL");
1209MODULE_AUTHOR("Paul Mackerras");
1210MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);