swim3.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. /*
  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. * TODO:
  14. * handle 2 drives
  15. * handle GCR disks
  16. */
  17. #undef DEBUG
  18. #include <linux/stddef.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/timer.h>
  22. #include <linux/delay.h>
  23. #include <linux/fd.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/mutex.h>
  28. #include <linux/module.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/wait.h>
  31. #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. #define MAX_FLOPPIES 2
  39. static DEFINE_MUTEX(swim3_mutex);
  40. static struct gendisk *disks[MAX_FLOPPIES];
  41. enum swim_state {
  42. idle,
  43. locating,
  44. seeking,
  45. settling,
  46. do_transfer,
  47. jogging,
  48. available,
  49. revalidating,
  50. ejecting
  51. };
  52. #define REG(x) unsigned char x; char x ## _pad[15];
  53. /*
  54. * The names for these registers mostly represent speculation on my part.
  55. * It will be interesting to see how close they are to the names Apple uses.
  56. */
  57. struct swim3 {
  58. REG(data);
  59. REG(timer); /* counts down at 1MHz */
  60. REG(error);
  61. REG(mode);
  62. REG(select); /* controls CA0, CA1, CA2 and LSTRB signals */
  63. REG(setup);
  64. REG(control); /* writing bits clears them */
  65. REG(status); /* writing bits sets them in control */
  66. REG(intr);
  67. REG(nseek); /* # tracks to seek */
  68. REG(ctrack); /* current track number */
  69. REG(csect); /* current sector number */
  70. REG(gap3); /* size of gap 3 in track format */
  71. REG(sector); /* sector # to read or write */
  72. REG(nsect); /* # sectors to read or write */
  73. REG(intr_enable);
  74. };
  75. #define control_bic control
  76. #define control_bis status
  77. /* Bits in select register */
  78. #define CA_MASK 7
  79. #define LSTRB 8
  80. /* Bits in control register */
  81. #define DO_SEEK 0x80
  82. #define FORMAT 0x40
  83. #define SELECT 0x20
  84. #define WRITE_SECTORS 0x10
  85. #define DO_ACTION 0x08
  86. #define DRIVE2_ENABLE 0x04
  87. #define DRIVE_ENABLE 0x02
  88. #define INTR_ENABLE 0x01
  89. /* Bits in status register */
  90. #define FIFO_1BYTE 0x80
  91. #define FIFO_2BYTE 0x40
  92. #define ERROR 0x20
  93. #define DATA 0x08
  94. #define RDDATA 0x04
  95. #define INTR_PENDING 0x02
  96. #define MARK_BYTE 0x01
  97. /* Bits in intr and intr_enable registers */
  98. #define ERROR_INTR 0x20
  99. #define DATA_CHANGED 0x10
  100. #define TRANSFER_DONE 0x08
  101. #define SEEN_SECTOR 0x04
  102. #define SEEK_DONE 0x02
  103. #define TIMER_DONE 0x01
  104. /* Bits in error register */
  105. #define ERR_DATA_CRC 0x80
  106. #define ERR_ADDR_CRC 0x40
  107. #define ERR_OVERRUN 0x04
  108. #define ERR_UNDERRUN 0x01
  109. /* Bits in setup register */
  110. #define S_SW_RESET 0x80
  111. #define S_GCR_WRITE 0x40
  112. #define S_IBM_DRIVE 0x20
  113. #define S_TEST_MODE 0x10
  114. #define S_FCLK_DIV2 0x08
  115. #define S_GCR 0x04
  116. #define S_COPY_PROT 0x02
  117. #define S_INV_WDATA 0x01
  118. /* Select values for swim3_action */
  119. #define SEEK_POSITIVE 0
  120. #define SEEK_NEGATIVE 4
  121. #define STEP 1
  122. #define MOTOR_ON 2
  123. #define MOTOR_OFF 6
  124. #define INDEX 3
  125. #define EJECT 7
  126. #define SETMFM 9
  127. #define SETGCR 13
  128. /* Select values for swim3_select and swim3_readbit */
  129. #define STEP_DIR 0
  130. #define STEPPING 1
  131. #define MOTOR_ON 2
  132. #define RELAX 3 /* also eject in progress */
  133. #define READ_DATA_0 4
  134. #define TWOMEG_DRIVE 5
  135. #define SINGLE_SIDED 6 /* drive or diskette is 4MB type? */
  136. #define DRIVE_PRESENT 7
  137. #define DISK_IN 8
  138. #define WRITE_PROT 9
  139. #define TRACK_ZERO 10
  140. #define TACHO 11
  141. #define READ_DATA_1 12
  142. #define MFM_MODE 13
  143. #define SEEK_COMPLETE 14
  144. #define ONEMEG_MEDIA 15
  145. /* Definitions of values used in writing and formatting */
  146. #define DATA_ESCAPE 0x99
  147. #define GCR_SYNC_EXC 0x3f
  148. #define GCR_SYNC_CONV 0x80
  149. #define GCR_FIRST_MARK 0xd5
  150. #define GCR_SECOND_MARK 0xaa
  151. #define GCR_ADDR_MARK "\xd5\xaa\x00"
  152. #define GCR_DATA_MARK "\xd5\xaa\x0b"
  153. #define GCR_SLIP_BYTE "\x27\xaa"
  154. #define GCR_SELF_SYNC "\x3f\xbf\x1e\x34\x3c\x3f"
  155. #define DATA_99 "\x99\x99"
  156. #define MFM_ADDR_MARK "\x99\xa1\x99\xa1\x99\xa1\x99\xfe"
  157. #define MFM_INDEX_MARK "\x99\xc2\x99\xc2\x99\xc2\x99\xfc"
  158. #define MFM_GAP_LEN 12
  159. struct floppy_state {
  160. enum swim_state state;
  161. struct swim3 __iomem *swim3; /* hardware registers */
  162. struct dbdma_regs __iomem *dma; /* DMA controller registers */
  163. int swim3_intr; /* interrupt number for SWIM3 */
  164. int dma_intr; /* interrupt number for DMA channel */
  165. int cur_cyl; /* cylinder head is on, or -1 */
  166. int cur_sector; /* last sector we saw go past */
  167. int req_cyl; /* the cylinder for the current r/w request */
  168. int head; /* head number ditto */
  169. int req_sector; /* sector number ditto */
  170. int scount; /* # sectors we're transferring at present */
  171. int retries;
  172. int settle_time;
  173. int secpercyl; /* disk geometry information */
  174. int secpertrack;
  175. int total_secs;
  176. int write_prot; /* 1 if write-protected, 0 if not, -1 dunno */
  177. struct dbdma_cmd *dma_cmd;
  178. int ref_count;
  179. int expect_cyl;
  180. struct timer_list timeout;
  181. int timeout_pending;
  182. int ejected;
  183. wait_queue_head_t wait;
  184. int wanted;
  185. struct macio_dev *mdev;
  186. char dbdma_cmd_space[5 * sizeof(struct dbdma_cmd)];
  187. int index;
  188. struct request *cur_req;
  189. };
  190. #define swim3_err(fmt, arg...) dev_err(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
  191. #define swim3_warn(fmt, arg...) dev_warn(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
  192. #define swim3_info(fmt, arg...) dev_info(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
  193. #ifdef DEBUG
  194. #define swim3_dbg(fmt, arg...) dev_dbg(&fs->mdev->ofdev.dev, "[fd%d] " fmt, fs->index, arg)
  195. #else
  196. #define swim3_dbg(fmt, arg...) do { } while(0)
  197. #endif
  198. static struct floppy_state floppy_states[MAX_FLOPPIES];
  199. static int floppy_count = 0;
  200. static DEFINE_SPINLOCK(swim3_lock);
  201. static unsigned short write_preamble[] = {
  202. 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, 0x4e4e, /* gap field */
  203. 0, 0, 0, 0, 0, 0, /* sync field */
  204. 0x99a1, 0x99a1, 0x99a1, 0x99fb, /* data address mark */
  205. 0x990f /* no escape for 512 bytes */
  206. };
  207. static unsigned short write_postamble[] = {
  208. 0x9904, /* insert CRC */
  209. 0x4e4e, 0x4e4e,
  210. 0x9908, /* stop writing */
  211. 0, 0, 0, 0, 0, 0
  212. };
  213. static void seek_track(struct floppy_state *fs, int n);
  214. static void init_dma(struct dbdma_cmd *cp, int cmd, void *buf, int count);
  215. static void act(struct floppy_state *fs);
  216. static void scan_timeout(unsigned long data);
  217. static void seek_timeout(unsigned long data);
  218. static void settle_timeout(unsigned long data);
  219. static void xfer_timeout(unsigned long data);
  220. static irqreturn_t swim3_interrupt(int irq, void *dev_id);
  221. /*static void fd_dma_interrupt(int irq, void *dev_id);*/
  222. static int grab_drive(struct floppy_state *fs, enum swim_state state,
  223. int interruptible);
  224. static void release_drive(struct floppy_state *fs);
  225. static int fd_eject(struct floppy_state *fs);
  226. static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
  227. unsigned int cmd, unsigned long param);
  228. static int floppy_open(struct block_device *bdev, fmode_t mode);
  229. static void floppy_release(struct gendisk *disk, fmode_t mode);
  230. static unsigned int floppy_check_events(struct gendisk *disk,
  231. unsigned int clearing);
  232. static int floppy_revalidate(struct gendisk *disk);
  233. static bool swim3_end_request(struct floppy_state *fs, int err, unsigned int nr_bytes)
  234. {
  235. struct request *req = fs->cur_req;
  236. int rc;
  237. swim3_dbg(" end request, err=%d nr_bytes=%d, cur_req=%p\n",
  238. err, nr_bytes, req);
  239. if (err)
  240. nr_bytes = blk_rq_cur_bytes(req);
  241. rc = __blk_end_request(req, err, nr_bytes);
  242. if (rc)
  243. return true;
  244. fs->cur_req = NULL;
  245. return false;
  246. }
  247. static void swim3_select(struct floppy_state *fs, int sel)
  248. {
  249. struct swim3 __iomem *sw = fs->swim3;
  250. out_8(&sw->select, RELAX);
  251. if (sel & 8)
  252. out_8(&sw->control_bis, SELECT);
  253. else
  254. out_8(&sw->control_bic, SELECT);
  255. out_8(&sw->select, sel & CA_MASK);
  256. }
  257. static void swim3_action(struct floppy_state *fs, int action)
  258. {
  259. struct swim3 __iomem *sw = fs->swim3;
  260. swim3_select(fs, action);
  261. udelay(1);
  262. out_8(&sw->select, sw->select | LSTRB);
  263. udelay(2);
  264. out_8(&sw->select, sw->select & ~LSTRB);
  265. udelay(1);
  266. }
  267. static int swim3_readbit(struct floppy_state *fs, int bit)
  268. {
  269. struct swim3 __iomem *sw = fs->swim3;
  270. int stat;
  271. swim3_select(fs, bit);
  272. udelay(1);
  273. stat = in_8(&sw->status);
  274. return (stat & DATA) == 0;
  275. }
  276. static void start_request(struct floppy_state *fs)
  277. {
  278. struct request *req;
  279. unsigned long x;
  280. swim3_dbg("start request, initial state=%d\n", fs->state);
  281. if (fs->state == idle && fs->wanted) {
  282. fs->state = available;
  283. wake_up(&fs->wait);
  284. return;
  285. }
  286. while (fs->state == idle) {
  287. swim3_dbg("start request, idle loop, cur_req=%p\n", fs->cur_req);
  288. if (!fs->cur_req) {
  289. fs->cur_req = blk_fetch_request(disks[fs->index]->queue);
  290. swim3_dbg(" fetched request %p\n", fs->cur_req);
  291. if (!fs->cur_req)
  292. break;
  293. }
  294. req = fs->cur_req;
  295. if (fs->mdev->media_bay &&
  296. check_media_bay(fs->mdev->media_bay) != MB_FD) {
  297. swim3_dbg("%s", " media bay absent, dropping req\n");
  298. swim3_end_request(fs, -ENODEV, 0);
  299. continue;
  300. }
  301. #if 0 /* This is really too verbose */
  302. swim3_dbg("do_fd_req: dev=%s cmd=%d sec=%ld nr_sec=%u buf=%p\n",
  303. req->rq_disk->disk_name, req->cmd,
  304. (long)blk_rq_pos(req), blk_rq_sectors(req),
  305. bio_data(req->bio));
  306. swim3_dbg(" errors=%d current_nr_sectors=%u\n",
  307. req->errors, blk_rq_cur_sectors(req));
  308. #endif
  309. if (blk_rq_pos(req) >= fs->total_secs) {
  310. swim3_dbg(" pos out of bounds (%ld, max is %ld)\n",
  311. (long)blk_rq_pos(req), (long)fs->total_secs);
  312. swim3_end_request(fs, -EIO, 0);
  313. continue;
  314. }
  315. if (fs->ejected) {
  316. swim3_dbg("%s", " disk ejected\n");
  317. swim3_end_request(fs, -EIO, 0);
  318. continue;
  319. }
  320. if (rq_data_dir(req) == WRITE) {
  321. if (fs->write_prot < 0)
  322. fs->write_prot = swim3_readbit(fs, WRITE_PROT);
  323. if (fs->write_prot) {
  324. swim3_dbg("%s", " try to write, disk write protected\n");
  325. swim3_end_request(fs, -EIO, 0);
  326. continue;
  327. }
  328. }
  329. /* Do not remove the cast. blk_rq_pos(req) is now a
  330. * sector_t and can be 64 bits, but it will never go
  331. * past 32 bits for this driver anyway, so we can
  332. * safely cast it down and not have to do a 64/32
  333. * division
  334. */
  335. fs->req_cyl = ((long)blk_rq_pos(req)) / fs->secpercyl;
  336. x = ((long)blk_rq_pos(req)) % fs->secpercyl;
  337. fs->head = x / fs->secpertrack;
  338. fs->req_sector = x % fs->secpertrack + 1;
  339. fs->state = do_transfer;
  340. fs->retries = 0;
  341. act(fs);
  342. }
  343. }
  344. static void do_fd_request(struct request_queue * q)
  345. {
  346. start_request(q->queuedata);
  347. }
  348. static void set_timeout(struct floppy_state *fs, int nticks,
  349. void (*proc)(unsigned long))
  350. {
  351. if (fs->timeout_pending)
  352. del_timer(&fs->timeout);
  353. fs->timeout.expires = jiffies + nticks;
  354. fs->timeout.function = proc;
  355. fs->timeout.data = (unsigned long) fs;
  356. add_timer(&fs->timeout);
  357. fs->timeout_pending = 1;
  358. }
  359. static inline void scan_track(struct floppy_state *fs)
  360. {
  361. struct swim3 __iomem *sw = fs->swim3;
  362. swim3_select(fs, READ_DATA_0);
  363. in_8(&sw->intr); /* clear SEEN_SECTOR bit */
  364. in_8(&sw->error);
  365. out_8(&sw->intr_enable, SEEN_SECTOR);
  366. out_8(&sw->control_bis, DO_ACTION);
  367. /* enable intr when track found */
  368. set_timeout(fs, HZ, scan_timeout); /* enable timeout */
  369. }
  370. static inline void seek_track(struct floppy_state *fs, int n)
  371. {
  372. struct swim3 __iomem *sw = fs->swim3;
  373. if (n >= 0) {
  374. swim3_action(fs, SEEK_POSITIVE);
  375. sw->nseek = n;
  376. } else {
  377. swim3_action(fs, SEEK_NEGATIVE);
  378. sw->nseek = -n;
  379. }
  380. fs->expect_cyl = (fs->cur_cyl >= 0)? fs->cur_cyl + n: -1;
  381. swim3_select(fs, STEP);
  382. in_8(&sw->error);
  383. /* enable intr when seek finished */
  384. out_8(&sw->intr_enable, SEEK_DONE);
  385. out_8(&sw->control_bis, DO_SEEK);
  386. set_timeout(fs, 3*HZ, seek_timeout); /* enable timeout */
  387. fs->settle_time = 0;
  388. }
  389. static inline void init_dma(struct dbdma_cmd *cp, int cmd,
  390. void *buf, int count)
  391. {
  392. cp->req_count = cpu_to_le16(count);
  393. cp->command = cpu_to_le16(cmd);
  394. cp->phy_addr = cpu_to_le32(virt_to_bus(buf));
  395. cp->xfer_status = 0;
  396. }
  397. static inline void setup_transfer(struct floppy_state *fs)
  398. {
  399. int n;
  400. struct swim3 __iomem *sw = fs->swim3;
  401. struct dbdma_cmd *cp = fs->dma_cmd;
  402. struct dbdma_regs __iomem *dr = fs->dma;
  403. struct request *req = fs->cur_req;
  404. if (blk_rq_cur_sectors(req) <= 0) {
  405. swim3_warn("%s", "Transfer 0 sectors ?\n");
  406. return;
  407. }
  408. if (rq_data_dir(req) == WRITE)
  409. n = 1;
  410. else {
  411. n = fs->secpertrack - fs->req_sector + 1;
  412. if (n > blk_rq_cur_sectors(req))
  413. n = blk_rq_cur_sectors(req);
  414. }
  415. swim3_dbg(" setup xfer at sect %d (of %d) head %d for %d\n",
  416. fs->req_sector, fs->secpertrack, fs->head, n);
  417. fs->scount = n;
  418. swim3_select(fs, fs->head? READ_DATA_1: READ_DATA_0);
  419. out_8(&sw->sector, fs->req_sector);
  420. out_8(&sw->nsect, n);
  421. out_8(&sw->gap3, 0);
  422. out_le32(&dr->cmdptr, virt_to_bus(cp));
  423. if (rq_data_dir(req) == WRITE) {
  424. /* Set up 3 dma commands: write preamble, data, postamble */
  425. init_dma(cp, OUTPUT_MORE, write_preamble, sizeof(write_preamble));
  426. ++cp;
  427. init_dma(cp, OUTPUT_MORE, bio_data(req->bio), 512);
  428. ++cp;
  429. init_dma(cp, OUTPUT_LAST, write_postamble, sizeof(write_postamble));
  430. } else {
  431. init_dma(cp, INPUT_LAST, bio_data(req->bio), n * 512);
  432. }
  433. ++cp;
  434. out_le16(&cp->command, DBDMA_STOP);
  435. out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
  436. in_8(&sw->error);
  437. out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
  438. if (rq_data_dir(req) == WRITE)
  439. out_8(&sw->control_bis, WRITE_SECTORS);
  440. in_8(&sw->intr);
  441. out_le32(&dr->control, (RUN << 16) | RUN);
  442. /* enable intr when transfer complete */
  443. out_8(&sw->intr_enable, TRANSFER_DONE);
  444. out_8(&sw->control_bis, DO_ACTION);
  445. set_timeout(fs, 2*HZ, xfer_timeout); /* enable timeout */
  446. }
  447. static void act(struct floppy_state *fs)
  448. {
  449. for (;;) {
  450. swim3_dbg(" act loop, state=%d, req_cyl=%d, cur_cyl=%d\n",
  451. fs->state, fs->req_cyl, fs->cur_cyl);
  452. switch (fs->state) {
  453. case idle:
  454. return; /* XXX shouldn't get here */
  455. case locating:
  456. if (swim3_readbit(fs, TRACK_ZERO)) {
  457. swim3_dbg("%s", " locate track 0\n");
  458. fs->cur_cyl = 0;
  459. if (fs->req_cyl == 0)
  460. fs->state = do_transfer;
  461. else
  462. fs->state = seeking;
  463. break;
  464. }
  465. scan_track(fs);
  466. return;
  467. case seeking:
  468. if (fs->cur_cyl < 0) {
  469. fs->expect_cyl = -1;
  470. fs->state = locating;
  471. break;
  472. }
  473. if (fs->req_cyl == fs->cur_cyl) {
  474. swim3_warn("%s", "Whoops, seeking 0\n");
  475. fs->state = do_transfer;
  476. break;
  477. }
  478. seek_track(fs, fs->req_cyl - fs->cur_cyl);
  479. return;
  480. case settling:
  481. /* check for SEEK_COMPLETE after 30ms */
  482. fs->settle_time = (HZ + 32) / 33;
  483. set_timeout(fs, fs->settle_time, settle_timeout);
  484. return;
  485. case do_transfer:
  486. if (fs->cur_cyl != fs->req_cyl) {
  487. if (fs->retries > 5) {
  488. swim3_err("Wrong cylinder in transfer, want: %d got %d\n",
  489. fs->req_cyl, fs->cur_cyl);
  490. swim3_end_request(fs, -EIO, 0);
  491. fs->state = idle;
  492. return;
  493. }
  494. fs->state = seeking;
  495. break;
  496. }
  497. setup_transfer(fs);
  498. return;
  499. case jogging:
  500. seek_track(fs, -5);
  501. return;
  502. default:
  503. swim3_err("Unknown state %d\n", fs->state);
  504. return;
  505. }
  506. }
  507. }
  508. static void scan_timeout(unsigned long data)
  509. {
  510. struct floppy_state *fs = (struct floppy_state *) data;
  511. struct swim3 __iomem *sw = fs->swim3;
  512. unsigned long flags;
  513. swim3_dbg("* scan timeout, state=%d\n", fs->state);
  514. spin_lock_irqsave(&swim3_lock, flags);
  515. fs->timeout_pending = 0;
  516. out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
  517. out_8(&sw->select, RELAX);
  518. out_8(&sw->intr_enable, 0);
  519. fs->cur_cyl = -1;
  520. if (fs->retries > 5) {
  521. swim3_end_request(fs, -EIO, 0);
  522. fs->state = idle;
  523. start_request(fs);
  524. } else {
  525. fs->state = jogging;
  526. act(fs);
  527. }
  528. spin_unlock_irqrestore(&swim3_lock, flags);
  529. }
  530. static void seek_timeout(unsigned long data)
  531. {
  532. struct floppy_state *fs = (struct floppy_state *) data;
  533. struct swim3 __iomem *sw = fs->swim3;
  534. unsigned long flags;
  535. swim3_dbg("* seek timeout, state=%d\n", fs->state);
  536. spin_lock_irqsave(&swim3_lock, flags);
  537. fs->timeout_pending = 0;
  538. out_8(&sw->control_bic, DO_SEEK);
  539. out_8(&sw->select, RELAX);
  540. out_8(&sw->intr_enable, 0);
  541. swim3_err("%s", "Seek timeout\n");
  542. swim3_end_request(fs, -EIO, 0);
  543. fs->state = idle;
  544. start_request(fs);
  545. spin_unlock_irqrestore(&swim3_lock, flags);
  546. }
  547. static void settle_timeout(unsigned long data)
  548. {
  549. struct floppy_state *fs = (struct floppy_state *) data;
  550. struct swim3 __iomem *sw = fs->swim3;
  551. unsigned long flags;
  552. swim3_dbg("* settle timeout, state=%d\n", fs->state);
  553. spin_lock_irqsave(&swim3_lock, flags);
  554. fs->timeout_pending = 0;
  555. if (swim3_readbit(fs, SEEK_COMPLETE)) {
  556. out_8(&sw->select, RELAX);
  557. fs->state = locating;
  558. act(fs);
  559. goto unlock;
  560. }
  561. out_8(&sw->select, RELAX);
  562. if (fs->settle_time < 2*HZ) {
  563. ++fs->settle_time;
  564. set_timeout(fs, 1, settle_timeout);
  565. goto unlock;
  566. }
  567. swim3_err("%s", "Seek settle timeout\n");
  568. swim3_end_request(fs, -EIO, 0);
  569. fs->state = idle;
  570. start_request(fs);
  571. unlock:
  572. spin_unlock_irqrestore(&swim3_lock, flags);
  573. }
  574. static void xfer_timeout(unsigned long data)
  575. {
  576. struct floppy_state *fs = (struct floppy_state *) data;
  577. struct swim3 __iomem *sw = fs->swim3;
  578. struct dbdma_regs __iomem *dr = fs->dma;
  579. unsigned long flags;
  580. int n;
  581. swim3_dbg("* xfer timeout, state=%d\n", fs->state);
  582. spin_lock_irqsave(&swim3_lock, flags);
  583. fs->timeout_pending = 0;
  584. out_le32(&dr->control, RUN << 16);
  585. /* We must wait a bit for dbdma to stop */
  586. for (n = 0; (in_le32(&dr->status) & ACTIVE) && n < 1000; n++)
  587. udelay(1);
  588. out_8(&sw->intr_enable, 0);
  589. out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
  590. out_8(&sw->select, RELAX);
  591. swim3_err("Timeout %sing sector %ld\n",
  592. (rq_data_dir(fs->cur_req)==WRITE? "writ": "read"),
  593. (long)blk_rq_pos(fs->cur_req));
  594. swim3_end_request(fs, -EIO, 0);
  595. fs->state = idle;
  596. start_request(fs);
  597. spin_unlock_irqrestore(&swim3_lock, flags);
  598. }
  599. static irqreturn_t swim3_interrupt(int irq, void *dev_id)
  600. {
  601. struct floppy_state *fs = (struct floppy_state *) dev_id;
  602. struct swim3 __iomem *sw = fs->swim3;
  603. int intr, err, n;
  604. int stat, resid;
  605. struct dbdma_regs __iomem *dr;
  606. struct dbdma_cmd *cp;
  607. unsigned long flags;
  608. struct request *req = fs->cur_req;
  609. swim3_dbg("* interrupt, state=%d\n", fs->state);
  610. spin_lock_irqsave(&swim3_lock, flags);
  611. intr = in_8(&sw->intr);
  612. err = (intr & ERROR_INTR)? in_8(&sw->error): 0;
  613. if ((intr & ERROR_INTR) && fs->state != do_transfer)
  614. swim3_err("Non-transfer error interrupt: state=%d, dir=%x, intr=%x, err=%x\n",
  615. fs->state, rq_data_dir(req), intr, err);
  616. switch (fs->state) {
  617. case locating:
  618. if (intr & SEEN_SECTOR) {
  619. out_8(&sw->control_bic, DO_ACTION | WRITE_SECTORS);
  620. out_8(&sw->select, RELAX);
  621. out_8(&sw->intr_enable, 0);
  622. del_timer(&fs->timeout);
  623. fs->timeout_pending = 0;
  624. if (sw->ctrack == 0xff) {
  625. swim3_err("%s", "Seen sector but cyl=ff?\n");
  626. fs->cur_cyl = -1;
  627. if (fs->retries > 5) {
  628. swim3_end_request(fs, -EIO, 0);
  629. fs->state = idle;
  630. start_request(fs);
  631. } else {
  632. fs->state = jogging;
  633. act(fs);
  634. }
  635. break;
  636. }
  637. fs->cur_cyl = sw->ctrack;
  638. fs->cur_sector = sw->csect;
  639. if (fs->expect_cyl != -1 && fs->expect_cyl != fs->cur_cyl)
  640. swim3_err("Expected cyl %d, got %d\n",
  641. fs->expect_cyl, fs->cur_cyl);
  642. fs->state = do_transfer;
  643. act(fs);
  644. }
  645. break;
  646. case seeking:
  647. case jogging:
  648. if (sw->nseek == 0) {
  649. out_8(&sw->control_bic, DO_SEEK);
  650. out_8(&sw->select, RELAX);
  651. out_8(&sw->intr_enable, 0);
  652. del_timer(&fs->timeout);
  653. fs->timeout_pending = 0;
  654. if (fs->state == seeking)
  655. ++fs->retries;
  656. fs->state = settling;
  657. act(fs);
  658. }
  659. break;
  660. case settling:
  661. out_8(&sw->intr_enable, 0);
  662. del_timer(&fs->timeout);
  663. fs->timeout_pending = 0;
  664. act(fs);
  665. break;
  666. case do_transfer:
  667. if ((intr & (ERROR_INTR | TRANSFER_DONE)) == 0)
  668. break;
  669. out_8(&sw->intr_enable, 0);
  670. out_8(&sw->control_bic, WRITE_SECTORS | DO_ACTION);
  671. out_8(&sw->select, RELAX);
  672. del_timer(&fs->timeout);
  673. fs->timeout_pending = 0;
  674. dr = fs->dma;
  675. cp = fs->dma_cmd;
  676. if (rq_data_dir(req) == WRITE)
  677. ++cp;
  678. /*
  679. * Check that the main data transfer has finished.
  680. * On writing, the swim3 sometimes doesn't use
  681. * up all the bytes of the postamble, so we can still
  682. * see DMA active here. That doesn't matter as long
  683. * as all the sector data has been transferred.
  684. */
  685. if ((intr & ERROR_INTR) == 0 && cp->xfer_status == 0) {
  686. /* wait a little while for DMA to complete */
  687. for (n = 0; n < 100; ++n) {
  688. if (cp->xfer_status != 0)
  689. break;
  690. udelay(1);
  691. barrier();
  692. }
  693. }
  694. /* turn off DMA */
  695. out_le32(&dr->control, (RUN | PAUSE) << 16);
  696. stat = le16_to_cpu(cp->xfer_status);
  697. resid = le16_to_cpu(cp->res_count);
  698. if (intr & ERROR_INTR) {
  699. n = fs->scount - 1 - resid / 512;
  700. if (n > 0) {
  701. blk_update_request(req, 0, n << 9);
  702. fs->req_sector += n;
  703. }
  704. if (fs->retries < 5) {
  705. ++fs->retries;
  706. act(fs);
  707. } else {
  708. swim3_err("Error %sing block %ld (err=%x)\n",
  709. rq_data_dir(req) == WRITE? "writ": "read",
  710. (long)blk_rq_pos(req), err);
  711. swim3_end_request(fs, -EIO, 0);
  712. fs->state = idle;
  713. }
  714. } else {
  715. if ((stat & ACTIVE) == 0 || resid != 0) {
  716. /* musta been an error */
  717. swim3_err("fd dma error: stat=%x resid=%d\n", stat, resid);
  718. swim3_err(" state=%d, dir=%x, intr=%x, err=%x\n",
  719. fs->state, rq_data_dir(req), intr, err);
  720. swim3_end_request(fs, -EIO, 0);
  721. fs->state = idle;
  722. start_request(fs);
  723. break;
  724. }
  725. fs->retries = 0;
  726. if (swim3_end_request(fs, 0, fs->scount << 9)) {
  727. fs->req_sector += fs->scount;
  728. if (fs->req_sector > fs->secpertrack) {
  729. fs->req_sector -= fs->secpertrack;
  730. if (++fs->head > 1) {
  731. fs->head = 0;
  732. ++fs->req_cyl;
  733. }
  734. }
  735. act(fs);
  736. } else
  737. fs->state = idle;
  738. }
  739. if (fs->state == idle)
  740. start_request(fs);
  741. break;
  742. default:
  743. swim3_err("Don't know what to do in state %d\n", fs->state);
  744. }
  745. spin_unlock_irqrestore(&swim3_lock, flags);
  746. return IRQ_HANDLED;
  747. }
  748. /*
  749. static void fd_dma_interrupt(int irq, void *dev_id)
  750. {
  751. }
  752. */
  753. /* Called under the mutex to grab exclusive access to a drive */
  754. static int grab_drive(struct floppy_state *fs, enum swim_state state,
  755. int interruptible)
  756. {
  757. unsigned long flags;
  758. swim3_dbg("%s", "-> grab drive\n");
  759. spin_lock_irqsave(&swim3_lock, flags);
  760. if (fs->state != idle && fs->state != available) {
  761. ++fs->wanted;
  762. /* this will enable irqs in order to sleep */
  763. if (!interruptible)
  764. wait_event_lock_irq(fs->wait,
  765. fs->state == available,
  766. swim3_lock);
  767. else if (wait_event_interruptible_lock_irq(fs->wait,
  768. fs->state == available,
  769. swim3_lock)) {
  770. --fs->wanted;
  771. spin_unlock_irqrestore(&swim3_lock, flags);
  772. return -EINTR;
  773. }
  774. --fs->wanted;
  775. }
  776. fs->state = state;
  777. spin_unlock_irqrestore(&swim3_lock, flags);
  778. return 0;
  779. }
  780. static void release_drive(struct floppy_state *fs)
  781. {
  782. unsigned long flags;
  783. swim3_dbg("%s", "-> release drive\n");
  784. spin_lock_irqsave(&swim3_lock, flags);
  785. fs->state = idle;
  786. start_request(fs);
  787. spin_unlock_irqrestore(&swim3_lock, flags);
  788. }
  789. static int fd_eject(struct floppy_state *fs)
  790. {
  791. int err, n;
  792. err = grab_drive(fs, ejecting, 1);
  793. if (err)
  794. return err;
  795. swim3_action(fs, EJECT);
  796. for (n = 20; n > 0; --n) {
  797. if (signal_pending(current)) {
  798. err = -EINTR;
  799. break;
  800. }
  801. swim3_select(fs, RELAX);
  802. schedule_timeout_interruptible(1);
  803. if (swim3_readbit(fs, DISK_IN) == 0)
  804. break;
  805. }
  806. swim3_select(fs, RELAX);
  807. udelay(150);
  808. fs->ejected = 1;
  809. release_drive(fs);
  810. return err;
  811. }
  812. static struct floppy_struct floppy_type =
  813. { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,NULL }; /* 7 1.44MB 3.5" */
  814. static int floppy_locked_ioctl(struct block_device *bdev, fmode_t mode,
  815. unsigned int cmd, unsigned long param)
  816. {
  817. struct floppy_state *fs = bdev->bd_disk->private_data;
  818. int err;
  819. if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
  820. return -EPERM;
  821. if (fs->mdev->media_bay &&
  822. check_media_bay(fs->mdev->media_bay) != MB_FD)
  823. return -ENXIO;
  824. switch (cmd) {
  825. case FDEJECT:
  826. if (fs->ref_count != 1)
  827. return -EBUSY;
  828. err = fd_eject(fs);
  829. return err;
  830. case FDGETPRM:
  831. if (copy_to_user((void __user *) param, &floppy_type,
  832. sizeof(struct floppy_struct)))
  833. return -EFAULT;
  834. return 0;
  835. }
  836. return -ENOTTY;
  837. }
  838. static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
  839. unsigned int cmd, unsigned long param)
  840. {
  841. int ret;
  842. mutex_lock(&swim3_mutex);
  843. ret = floppy_locked_ioctl(bdev, mode, cmd, param);
  844. mutex_unlock(&swim3_mutex);
  845. return ret;
  846. }
  847. static int floppy_open(struct block_device *bdev, fmode_t mode)
  848. {
  849. struct floppy_state *fs = bdev->bd_disk->private_data;
  850. struct swim3 __iomem *sw = fs->swim3;
  851. int n, err = 0;
  852. if (fs->ref_count == 0) {
  853. if (fs->mdev->media_bay &&
  854. check_media_bay(fs->mdev->media_bay) != MB_FD)
  855. return -ENXIO;
  856. out_8(&sw->setup, S_IBM_DRIVE | S_FCLK_DIV2);
  857. out_8(&sw->control_bic, 0xff);
  858. out_8(&sw->mode, 0x95);
  859. udelay(10);
  860. out_8(&sw->intr_enable, 0);
  861. out_8(&sw->control_bis, DRIVE_ENABLE | INTR_ENABLE);
  862. swim3_action(fs, MOTOR_ON);
  863. fs->write_prot = -1;
  864. fs->cur_cyl = -1;
  865. for (n = 0; n < 2 * HZ; ++n) {
  866. if (n >= HZ/30 && swim3_readbit(fs, SEEK_COMPLETE))
  867. break;
  868. if (signal_pending(current)) {
  869. err = -EINTR;
  870. break;
  871. }
  872. swim3_select(fs, RELAX);
  873. schedule_timeout_interruptible(1);
  874. }
  875. if (err == 0 && (swim3_readbit(fs, SEEK_COMPLETE) == 0
  876. || swim3_readbit(fs, DISK_IN) == 0))
  877. err = -ENXIO;
  878. swim3_action(fs, SETMFM);
  879. swim3_select(fs, RELAX);
  880. } else if (fs->ref_count == -1 || mode & FMODE_EXCL)
  881. return -EBUSY;
  882. if (err == 0 && (mode & FMODE_NDELAY) == 0
  883. && (mode & (FMODE_READ|FMODE_WRITE))) {
  884. check_disk_change(bdev);
  885. if (fs->ejected)
  886. err = -ENXIO;
  887. }
  888. if (err == 0 && (mode & FMODE_WRITE)) {
  889. if (fs->write_prot < 0)
  890. fs->write_prot = swim3_readbit(fs, WRITE_PROT);
  891. if (fs->write_prot)
  892. err = -EROFS;
  893. }
  894. if (err) {
  895. if (fs->ref_count == 0) {
  896. swim3_action(fs, MOTOR_OFF);
  897. out_8(&sw->control_bic, DRIVE_ENABLE | INTR_ENABLE);
  898. swim3_select(fs, RELAX);
  899. }
  900. return err;
  901. }
  902. if (mode & FMODE_EXCL)
  903. fs->ref_count = -1;
  904. else
  905. ++fs->ref_count;
  906. return 0;
  907. }
  908. static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
  909. {
  910. int ret;
  911. mutex_lock(&swim3_mutex);
  912. ret = floppy_open(bdev, mode);
  913. mutex_unlock(&swim3_mutex);
  914. return ret;
  915. }
  916. static void floppy_release(struct gendisk *disk, fmode_t mode)
  917. {
  918. struct floppy_state *fs = disk->private_data;
  919. struct swim3 __iomem *sw = fs->swim3;
  920. mutex_lock(&swim3_mutex);
  921. if (fs->ref_count > 0 && --fs->ref_count == 0) {
  922. swim3_action(fs, MOTOR_OFF);
  923. out_8(&sw->control_bic, 0xff);
  924. swim3_select(fs, RELAX);
  925. }
  926. mutex_unlock(&swim3_mutex);
  927. }
  928. static unsigned int floppy_check_events(struct gendisk *disk,
  929. unsigned int clearing)
  930. {
  931. struct floppy_state *fs = disk->private_data;
  932. return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
  933. }
  934. static int floppy_revalidate(struct gendisk *disk)
  935. {
  936. struct floppy_state *fs = disk->private_data;
  937. struct swim3 __iomem *sw;
  938. int ret, n;
  939. if (fs->mdev->media_bay &&
  940. check_media_bay(fs->mdev->media_bay) != MB_FD)
  941. return -ENXIO;
  942. sw = fs->swim3;
  943. grab_drive(fs, revalidating, 0);
  944. out_8(&sw->intr_enable, 0);
  945. out_8(&sw->control_bis, DRIVE_ENABLE);
  946. swim3_action(fs, MOTOR_ON); /* necessary? */
  947. fs->write_prot = -1;
  948. fs->cur_cyl = -1;
  949. mdelay(1);
  950. for (n = HZ; n > 0; --n) {
  951. if (swim3_readbit(fs, SEEK_COMPLETE))
  952. break;
  953. if (signal_pending(current))
  954. break;
  955. swim3_select(fs, RELAX);
  956. schedule_timeout_interruptible(1);
  957. }
  958. ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
  959. || swim3_readbit(fs, DISK_IN) == 0;
  960. if (ret)
  961. swim3_action(fs, MOTOR_OFF);
  962. else {
  963. fs->ejected = 0;
  964. swim3_action(fs, SETMFM);
  965. }
  966. swim3_select(fs, RELAX);
  967. release_drive(fs);
  968. return ret;
  969. }
  970. static const struct block_device_operations floppy_fops = {
  971. .open = floppy_unlocked_open,
  972. .release = floppy_release,
  973. .ioctl = floppy_ioctl,
  974. .check_events = floppy_check_events,
  975. .revalidate_disk= floppy_revalidate,
  976. };
  977. static void swim3_mb_event(struct macio_dev* mdev, int mb_state)
  978. {
  979. struct floppy_state *fs = macio_get_drvdata(mdev);
  980. struct swim3 __iomem *sw;
  981. if (!fs)
  982. return;
  983. sw = fs->swim3;
  984. if (mb_state != MB_FD)
  985. return;
  986. /* Clear state */
  987. out_8(&sw->intr_enable, 0);
  988. in_8(&sw->intr);
  989. in_8(&sw->error);
  990. }
  991. static int swim3_add_device(struct macio_dev *mdev, int index)
  992. {
  993. struct device_node *swim = mdev->ofdev.dev.of_node;
  994. struct floppy_state *fs = &floppy_states[index];
  995. int rc = -EBUSY;
  996. /* Do this first for message macros */
  997. memset(fs, 0, sizeof(*fs));
  998. fs->mdev = mdev;
  999. fs->index = index;
  1000. /* Check & Request resources */
  1001. if (macio_resource_count(mdev) < 2) {
  1002. swim3_err("%s", "No address in device-tree\n");
  1003. return -ENXIO;
  1004. }
  1005. if (macio_irq_count(mdev) < 1) {
  1006. swim3_err("%s", "No interrupt in device-tree\n");
  1007. return -ENXIO;
  1008. }
  1009. if (macio_request_resource(mdev, 0, "swim3 (mmio)")) {
  1010. swim3_err("%s", "Can't request mmio resource\n");
  1011. return -EBUSY;
  1012. }
  1013. if (macio_request_resource(mdev, 1, "swim3 (dma)")) {
  1014. swim3_err("%s", "Can't request dma resource\n");
  1015. macio_release_resource(mdev, 0);
  1016. return -EBUSY;
  1017. }
  1018. dev_set_drvdata(&mdev->ofdev.dev, fs);
  1019. if (mdev->media_bay == NULL)
  1020. pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
  1021. fs->state = idle;
  1022. fs->swim3 = (struct swim3 __iomem *)
  1023. ioremap(macio_resource_start(mdev, 0), 0x200);
  1024. if (fs->swim3 == NULL) {
  1025. swim3_err("%s", "Couldn't map mmio registers\n");
  1026. rc = -ENOMEM;
  1027. goto out_release;
  1028. }
  1029. fs->dma = (struct dbdma_regs __iomem *)
  1030. ioremap(macio_resource_start(mdev, 1), 0x200);
  1031. if (fs->dma == NULL) {
  1032. swim3_err("%s", "Couldn't map dma registers\n");
  1033. iounmap(fs->swim3);
  1034. rc = -ENOMEM;
  1035. goto out_release;
  1036. }
  1037. fs->swim3_intr = macio_irq(mdev, 0);
  1038. fs->dma_intr = macio_irq(mdev, 1);
  1039. fs->cur_cyl = -1;
  1040. fs->cur_sector = -1;
  1041. fs->secpercyl = 36;
  1042. fs->secpertrack = 18;
  1043. fs->total_secs = 2880;
  1044. init_waitqueue_head(&fs->wait);
  1045. fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
  1046. memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
  1047. fs->dma_cmd[1].command = cpu_to_le16(DBDMA_STOP);
  1048. if (mdev->media_bay == NULL || check_media_bay(mdev->media_bay) == MB_FD)
  1049. swim3_mb_event(mdev, MB_FD);
  1050. if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
  1051. swim3_err("%s", "Couldn't request interrupt\n");
  1052. pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
  1053. goto out_unmap;
  1054. return -EBUSY;
  1055. }
  1056. init_timer(&fs->timeout);
  1057. swim3_info("SWIM3 floppy controller %s\n",
  1058. mdev->media_bay ? "in media bay" : "");
  1059. return 0;
  1060. out_unmap:
  1061. iounmap(fs->dma);
  1062. iounmap(fs->swim3);
  1063. out_release:
  1064. macio_release_resource(mdev, 0);
  1065. macio_release_resource(mdev, 1);
  1066. return rc;
  1067. }
  1068. static int swim3_attach(struct macio_dev *mdev,
  1069. const struct of_device_id *match)
  1070. {
  1071. struct gendisk *disk;
  1072. int index, rc;
  1073. index = floppy_count++;
  1074. if (index >= MAX_FLOPPIES)
  1075. return -ENXIO;
  1076. /* Add the drive */
  1077. rc = swim3_add_device(mdev, index);
  1078. if (rc)
  1079. return rc;
  1080. /* Now register that disk. Same comment about failure handling */
  1081. disk = disks[index] = alloc_disk(1);
  1082. if (disk == NULL)
  1083. return -ENOMEM;
  1084. disk->queue = blk_init_queue(do_fd_request, &swim3_lock);
  1085. if (disk->queue == NULL) {
  1086. put_disk(disk);
  1087. return -ENOMEM;
  1088. }
  1089. disk->queue->queuedata = &floppy_states[index];
  1090. if (index == 0) {
  1091. /* If we failed, there isn't much we can do as the driver is still
  1092. * too dumb to remove the device, just bail out
  1093. */
  1094. if (register_blkdev(FLOPPY_MAJOR, "fd"))
  1095. return 0;
  1096. }
  1097. disk->major = FLOPPY_MAJOR;
  1098. disk->first_minor = index;
  1099. disk->fops = &floppy_fops;
  1100. disk->private_data = &floppy_states[index];
  1101. disk->flags |= GENHD_FL_REMOVABLE;
  1102. sprintf(disk->disk_name, "fd%d", index);
  1103. set_capacity(disk, 2880);
  1104. add_disk(disk);
  1105. return 0;
  1106. }
  1107. static struct of_device_id swim3_match[] =
  1108. {
  1109. {
  1110. .name = "swim3",
  1111. },
  1112. {
  1113. .compatible = "ohare-swim3"
  1114. },
  1115. {
  1116. .compatible = "swim3"
  1117. },
  1118. { /* end of list */ }
  1119. };
  1120. static struct macio_driver swim3_driver =
  1121. {
  1122. .driver = {
  1123. .name = "swim3",
  1124. .of_match_table = swim3_match,
  1125. },
  1126. .probe = swim3_attach,
  1127. #ifdef CONFIG_PMAC_MEDIABAY
  1128. .mediabay_event = swim3_mb_event,
  1129. #endif
  1130. #if 0
  1131. .suspend = swim3_suspend,
  1132. .resume = swim3_resume,
  1133. #endif
  1134. };
  1135. int swim3_init(void)
  1136. {
  1137. macio_register_driver(&swim3_driver);
  1138. return 0;
  1139. }
  1140. module_init(swim3_init)
  1141. MODULE_LICENSE("GPL");
  1142. MODULE_AUTHOR("Paul Mackerras");
  1143. MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);