swim.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  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. #include <linux/module.h>
  19. #include <linux/fd.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/hdreg.h>
  22. #include <linux/kernel.h>
  23. #include <linux/delay.h>
  24. #include <linux/platform_device.h>
  25. #include <asm/macintosh.h>
  26. #include <asm/mac_via.h>
  27. #define CARDNAME "swim"
  28. struct sector_header {
  29. unsigned char side;
  30. unsigned char track;
  31. unsigned char sector;
  32. unsigned char size;
  33. unsigned char crc0;
  34. unsigned char crc1;
  35. } __attribute__((packed));
  36. #define DRIVER_VERSION "Version 0.2 (2008-10-30)"
  37. #define REG(x) unsigned char x, x ## _pad[0x200 - 1];
  38. struct swim {
  39. REG(write_data)
  40. REG(write_mark)
  41. REG(write_CRC)
  42. REG(write_parameter)
  43. REG(write_phase)
  44. REG(write_setup)
  45. REG(write_mode0)
  46. REG(write_mode1)
  47. REG(read_data)
  48. REG(read_mark)
  49. REG(read_error)
  50. REG(read_parameter)
  51. REG(read_phase)
  52. REG(read_setup)
  53. REG(read_status)
  54. REG(read_handshake)
  55. } __attribute__((packed));
  56. #define swim_write(base, reg, v) out_8(&(base)->write_##reg, (v))
  57. #define swim_read(base, reg) in_8(&(base)->read_##reg)
  58. /* IWM registers */
  59. struct iwm {
  60. REG(ph0L)
  61. REG(ph0H)
  62. REG(ph1L)
  63. REG(ph1H)
  64. REG(ph2L)
  65. REG(ph2H)
  66. REG(ph3L)
  67. REG(ph3H)
  68. REG(mtrOff)
  69. REG(mtrOn)
  70. REG(intDrive)
  71. REG(extDrive)
  72. REG(q6L)
  73. REG(q6H)
  74. REG(q7L)
  75. REG(q7H)
  76. } __attribute__((packed));
  77. #define iwm_write(base, reg, v) out_8(&(base)->reg, (v))
  78. #define iwm_read(base, reg) in_8(&(base)->reg)
  79. /* bits in phase register */
  80. #define SEEK_POSITIVE 0x070
  81. #define SEEK_NEGATIVE 0x074
  82. #define STEP 0x071
  83. #define MOTOR_ON 0x072
  84. #define MOTOR_OFF 0x076
  85. #define INDEX 0x073
  86. #define EJECT 0x077
  87. #define SETMFM 0x171
  88. #define SETGCR 0x175
  89. #define RELAX 0x033
  90. #define LSTRB 0x008
  91. #define CA_MASK 0x077
  92. /* Select values for swim_select and swim_readbit */
  93. #define READ_DATA_0 0x074
  94. #define TWOMEG_DRIVE 0x075
  95. #define SINGLE_SIDED 0x076
  96. #define DRIVE_PRESENT 0x077
  97. #define DISK_IN 0x170
  98. #define WRITE_PROT 0x171
  99. #define TRACK_ZERO 0x172
  100. #define TACHO 0x173
  101. #define READ_DATA_1 0x174
  102. #define MFM_MODE 0x175
  103. #define SEEK_COMPLETE 0x176
  104. #define ONEMEG_MEDIA 0x177
  105. /* Bits in handshake register */
  106. #define MARK_BYTE 0x01
  107. #define CRC_ZERO 0x02
  108. #define RDDATA 0x04
  109. #define SENSE 0x08
  110. #define MOTEN 0x10
  111. #define ERROR 0x20
  112. #define DAT2BYTE 0x40
  113. #define DAT1BYTE 0x80
  114. /* bits in setup register */
  115. #define S_INV_WDATA 0x01
  116. #define S_3_5_SELECT 0x02
  117. #define S_GCR 0x04
  118. #define S_FCLK_DIV2 0x08
  119. #define S_ERROR_CORR 0x10
  120. #define S_IBM_DRIVE 0x20
  121. #define S_GCR_WRITE 0x40
  122. #define S_TIMEOUT 0x80
  123. /* bits in mode register */
  124. #define CLFIFO 0x01
  125. #define ENBL1 0x02
  126. #define ENBL2 0x04
  127. #define ACTION 0x08
  128. #define WRITE_MODE 0x10
  129. #define HEDSEL 0x20
  130. #define MOTON 0x80
  131. /*----------------------------------------------------------------------------*/
  132. enum drive_location {
  133. INTERNAL_DRIVE = 0x02,
  134. EXTERNAL_DRIVE = 0x04,
  135. };
  136. enum media_type {
  137. DD_MEDIA,
  138. HD_MEDIA,
  139. };
  140. struct floppy_state {
  141. /* physical properties */
  142. enum drive_location location; /* internal or external drive */
  143. int head_number; /* single- or double-sided drive */
  144. /* media */
  145. int disk_in;
  146. int ejected;
  147. enum media_type type;
  148. int write_protected;
  149. int total_secs;
  150. int secpercyl;
  151. int secpertrack;
  152. /* in-use information */
  153. int track;
  154. int ref_count;
  155. struct gendisk *disk;
  156. /* parent controller */
  157. struct swim_priv *swd;
  158. };
  159. enum motor_action {
  160. OFF,
  161. ON,
  162. };
  163. enum head {
  164. LOWER_HEAD = 0,
  165. UPPER_HEAD = 1,
  166. };
  167. #define FD_MAX_UNIT 2
  168. struct swim_priv {
  169. struct swim __iomem *base;
  170. spinlock_t lock;
  171. struct request_queue *queue;
  172. int floppy_count;
  173. struct floppy_state unit[FD_MAX_UNIT];
  174. };
  175. extern int swim_read_sector_header(struct swim __iomem *base,
  176. struct sector_header *header);
  177. extern int swim_read_sector_data(struct swim __iomem *base,
  178. unsigned char *data);
  179. static inline void set_swim_mode(struct swim __iomem *base, int enable)
  180. {
  181. struct iwm __iomem *iwm_base;
  182. unsigned long flags;
  183. if (!enable) {
  184. swim_write(base, mode0, 0xf8);
  185. return;
  186. }
  187. iwm_base = (struct iwm __iomem *)base;
  188. local_irq_save(flags);
  189. iwm_read(iwm_base, q7L);
  190. iwm_read(iwm_base, mtrOff);
  191. iwm_read(iwm_base, q6H);
  192. iwm_write(iwm_base, q7H, 0x57);
  193. iwm_write(iwm_base, q7H, 0x17);
  194. iwm_write(iwm_base, q7H, 0x57);
  195. iwm_write(iwm_base, q7H, 0x57);
  196. local_irq_restore(flags);
  197. }
  198. static inline int get_swim_mode(struct swim __iomem *base)
  199. {
  200. unsigned long flags;
  201. local_irq_save(flags);
  202. swim_write(base, phase, 0xf5);
  203. if (swim_read(base, phase) != 0xf5)
  204. goto is_iwm;
  205. swim_write(base, phase, 0xf6);
  206. if (swim_read(base, phase) != 0xf6)
  207. goto is_iwm;
  208. swim_write(base, phase, 0xf7);
  209. if (swim_read(base, phase) != 0xf7)
  210. goto is_iwm;
  211. local_irq_restore(flags);
  212. return 1;
  213. is_iwm:
  214. local_irq_restore(flags);
  215. return 0;
  216. }
  217. static inline void swim_select(struct swim __iomem *base, int sel)
  218. {
  219. swim_write(base, phase, RELAX);
  220. via1_set_head(sel & 0x100);
  221. swim_write(base, phase, sel & CA_MASK);
  222. }
  223. static inline void swim_action(struct swim __iomem *base, int action)
  224. {
  225. unsigned long flags;
  226. local_irq_save(flags);
  227. swim_select(base, action);
  228. udelay(1);
  229. swim_write(base, phase, (LSTRB<<4) | LSTRB);
  230. udelay(1);
  231. swim_write(base, phase, (LSTRB<<4) | ((~LSTRB) & 0x0F));
  232. udelay(1);
  233. local_irq_restore(flags);
  234. }
  235. static inline int swim_readbit(struct swim __iomem *base, int bit)
  236. {
  237. int stat;
  238. swim_select(base, bit);
  239. udelay(10);
  240. stat = swim_read(base, handshake);
  241. return (stat & SENSE) == 0;
  242. }
  243. static inline void swim_drive(struct swim __iomem *base,
  244. enum drive_location location)
  245. {
  246. if (location == INTERNAL_DRIVE) {
  247. swim_write(base, mode0, EXTERNAL_DRIVE); /* clear drive 1 bit */
  248. swim_write(base, mode1, INTERNAL_DRIVE); /* set drive 0 bit */
  249. } else if (location == EXTERNAL_DRIVE) {
  250. swim_write(base, mode0, INTERNAL_DRIVE); /* clear drive 0 bit */
  251. swim_write(base, mode1, EXTERNAL_DRIVE); /* set drive 1 bit */
  252. }
  253. }
  254. static inline void swim_motor(struct swim __iomem *base,
  255. enum motor_action action)
  256. {
  257. if (action == ON) {
  258. int i;
  259. swim_action(base, MOTOR_ON);
  260. for (i = 0; i < 2*HZ; i++) {
  261. swim_select(base, RELAX);
  262. if (swim_readbit(base, MOTOR_ON))
  263. break;
  264. current->state = TASK_INTERRUPTIBLE;
  265. schedule_timeout(1);
  266. }
  267. } else if (action == OFF) {
  268. swim_action(base, MOTOR_OFF);
  269. swim_select(base, RELAX);
  270. }
  271. }
  272. static inline void swim_eject(struct swim __iomem *base)
  273. {
  274. int i;
  275. swim_action(base, EJECT);
  276. for (i = 0; i < 2*HZ; i++) {
  277. swim_select(base, RELAX);
  278. if (!swim_readbit(base, DISK_IN))
  279. break;
  280. current->state = TASK_INTERRUPTIBLE;
  281. schedule_timeout(1);
  282. }
  283. swim_select(base, RELAX);
  284. }
  285. static inline void swim_head(struct swim __iomem *base, enum head head)
  286. {
  287. /* wait drive is ready */
  288. if (head == UPPER_HEAD)
  289. swim_select(base, READ_DATA_1);
  290. else if (head == LOWER_HEAD)
  291. swim_select(base, READ_DATA_0);
  292. }
  293. static inline int swim_step(struct swim __iomem *base)
  294. {
  295. int wait;
  296. swim_action(base, STEP);
  297. for (wait = 0; wait < HZ; wait++) {
  298. current->state = TASK_INTERRUPTIBLE;
  299. schedule_timeout(1);
  300. swim_select(base, RELAX);
  301. if (!swim_readbit(base, STEP))
  302. return 0;
  303. }
  304. return -1;
  305. }
  306. static inline int swim_track00(struct swim __iomem *base)
  307. {
  308. int try;
  309. swim_action(base, SEEK_NEGATIVE);
  310. for (try = 0; try < 100; try++) {
  311. swim_select(base, RELAX);
  312. if (swim_readbit(base, TRACK_ZERO))
  313. break;
  314. if (swim_step(base))
  315. return -1;
  316. }
  317. if (swim_readbit(base, TRACK_ZERO))
  318. return 0;
  319. return -1;
  320. }
  321. static inline int swim_seek(struct swim __iomem *base, int step)
  322. {
  323. if (step == 0)
  324. return 0;
  325. if (step < 0) {
  326. swim_action(base, SEEK_NEGATIVE);
  327. step = -step;
  328. } else
  329. swim_action(base, SEEK_POSITIVE);
  330. for ( ; step > 0; step--) {
  331. if (swim_step(base))
  332. return -1;
  333. }
  334. return 0;
  335. }
  336. static inline int swim_track(struct floppy_state *fs, int track)
  337. {
  338. struct swim __iomem *base = fs->swd->base;
  339. int ret;
  340. ret = swim_seek(base, track - fs->track);
  341. if (ret == 0)
  342. fs->track = track;
  343. else {
  344. swim_track00(base);
  345. fs->track = 0;
  346. }
  347. return ret;
  348. }
  349. static int floppy_eject(struct floppy_state *fs)
  350. {
  351. struct swim __iomem *base = fs->swd->base;
  352. swim_drive(base, fs->location);
  353. swim_motor(base, OFF);
  354. swim_eject(base);
  355. fs->disk_in = 0;
  356. fs->ejected = 1;
  357. return 0;
  358. }
  359. static inline int swim_read_sector(struct floppy_state *fs,
  360. int side, int track,
  361. int sector, unsigned char *buffer)
  362. {
  363. struct swim __iomem *base = fs->swd->base;
  364. unsigned long flags;
  365. struct sector_header header;
  366. int ret = -1;
  367. short i;
  368. swim_track(fs, track);
  369. swim_write(base, mode1, MOTON);
  370. swim_head(base, side);
  371. swim_write(base, mode0, side);
  372. local_irq_save(flags);
  373. for (i = 0; i < 36; i++) {
  374. ret = swim_read_sector_header(base, &header);
  375. if (!ret && (header.sector == sector)) {
  376. /* found */
  377. ret = swim_read_sector_data(base, buffer);
  378. break;
  379. }
  380. }
  381. local_irq_restore(flags);
  382. swim_write(base, mode0, MOTON);
  383. if ((header.side != side) || (header.track != track) ||
  384. (header.sector != sector))
  385. return 0;
  386. return ret;
  387. }
  388. static int floppy_read_sectors(struct floppy_state *fs,
  389. int req_sector, int sectors_nb,
  390. unsigned char *buffer)
  391. {
  392. struct swim __iomem *base = fs->swd->base;
  393. int ret;
  394. int side, track, sector;
  395. int i, try;
  396. swim_drive(base, fs->location);
  397. for (i = req_sector; i < req_sector + sectors_nb; i++) {
  398. int x;
  399. track = i / fs->secpercyl;
  400. x = i % fs->secpercyl;
  401. side = x / fs->secpertrack;
  402. sector = x % fs->secpertrack + 1;
  403. try = 5;
  404. do {
  405. ret = swim_read_sector(fs, side, track, sector,
  406. buffer);
  407. if (try-- == 0)
  408. return -EIO;
  409. } while (ret != 512);
  410. buffer += ret;
  411. }
  412. return 0;
  413. }
  414. static void redo_fd_request(struct request_queue *q)
  415. {
  416. struct request *req;
  417. struct floppy_state *fs;
  418. req = blk_fetch_request(q);
  419. while (req) {
  420. int err = -EIO;
  421. fs = req->rq_disk->private_data;
  422. if (blk_rq_pos(req) >= fs->total_secs)
  423. goto done;
  424. if (!fs->disk_in)
  425. goto done;
  426. if (rq_data_dir(req) == WRITE && fs->write_protected)
  427. goto done;
  428. switch (rq_data_dir(req)) {
  429. case WRITE:
  430. /* NOT IMPLEMENTED */
  431. break;
  432. case READ:
  433. err = floppy_read_sectors(fs, blk_rq_pos(req),
  434. blk_rq_cur_sectors(req),
  435. req->buffer);
  436. break;
  437. }
  438. done:
  439. if (!__blk_end_request_cur(req, err))
  440. req = blk_fetch_request(q);
  441. }
  442. }
  443. static void do_fd_request(struct request_queue *q)
  444. {
  445. redo_fd_request(q);
  446. }
  447. static struct floppy_struct floppy_type[4] = {
  448. { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL }, /* no testing */
  449. { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 360KB SS 3.5"*/
  450. { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 720KB 3.5" */
  451. { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL }, /* 1.44MB 3.5" */
  452. };
  453. static int get_floppy_geometry(struct floppy_state *fs, int type,
  454. struct floppy_struct **g)
  455. {
  456. if (type >= ARRAY_SIZE(floppy_type))
  457. return -EINVAL;
  458. if (type)
  459. *g = &floppy_type[type];
  460. else if (fs->type == HD_MEDIA) /* High-Density media */
  461. *g = &floppy_type[3];
  462. else if (fs->head_number == 2) /* double-sided */
  463. *g = &floppy_type[2];
  464. else
  465. *g = &floppy_type[1];
  466. return 0;
  467. }
  468. static void setup_medium(struct floppy_state *fs)
  469. {
  470. struct swim __iomem *base = fs->swd->base;
  471. if (swim_readbit(base, DISK_IN)) {
  472. struct floppy_struct *g;
  473. fs->disk_in = 1;
  474. fs->write_protected = swim_readbit(base, WRITE_PROT);
  475. fs->type = swim_readbit(base, ONEMEG_MEDIA);
  476. if (swim_track00(base))
  477. printk(KERN_ERR
  478. "SWIM: cannot move floppy head to track 0\n");
  479. swim_track00(base);
  480. get_floppy_geometry(fs, 0, &g);
  481. fs->total_secs = g->size;
  482. fs->secpercyl = g->head * g->sect;
  483. fs->secpertrack = g->sect;
  484. fs->track = 0;
  485. } else {
  486. fs->disk_in = 0;
  487. }
  488. }
  489. static int floppy_open(struct block_device *bdev, fmode_t mode)
  490. {
  491. struct floppy_state *fs = bdev->bd_disk->private_data;
  492. struct swim __iomem *base = fs->swd->base;
  493. int err;
  494. if (fs->ref_count == -1 || (fs->ref_count && mode & FMODE_EXCL))
  495. return -EBUSY;
  496. if (mode & FMODE_EXCL)
  497. fs->ref_count = -1;
  498. else
  499. fs->ref_count++;
  500. swim_write(base, setup, S_IBM_DRIVE | S_FCLK_DIV2);
  501. udelay(10);
  502. swim_drive(base, INTERNAL_DRIVE);
  503. swim_motor(base, ON);
  504. swim_action(base, SETMFM);
  505. if (fs->ejected)
  506. setup_medium(fs);
  507. if (!fs->disk_in) {
  508. err = -ENXIO;
  509. goto out;
  510. }
  511. if (mode & FMODE_NDELAY)
  512. return 0;
  513. if (mode & (FMODE_READ|FMODE_WRITE)) {
  514. check_disk_change(bdev);
  515. if ((mode & FMODE_WRITE) && fs->write_protected) {
  516. err = -EROFS;
  517. goto out;
  518. }
  519. }
  520. return 0;
  521. out:
  522. if (fs->ref_count < 0)
  523. fs->ref_count = 0;
  524. else if (fs->ref_count > 0)
  525. --fs->ref_count;
  526. if (fs->ref_count == 0)
  527. swim_motor(base, OFF);
  528. return err;
  529. }
  530. static int floppy_release(struct gendisk *disk, fmode_t mode)
  531. {
  532. struct floppy_state *fs = disk->private_data;
  533. struct swim __iomem *base = fs->swd->base;
  534. if (fs->ref_count < 0)
  535. fs->ref_count = 0;
  536. else if (fs->ref_count > 0)
  537. --fs->ref_count;
  538. if (fs->ref_count == 0)
  539. swim_motor(base, OFF);
  540. return 0;
  541. }
  542. static int floppy_ioctl(struct block_device *bdev, fmode_t mode,
  543. unsigned int cmd, unsigned long param)
  544. {
  545. struct floppy_state *fs = bdev->bd_disk->private_data;
  546. int err;
  547. if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
  548. return -EPERM;
  549. switch (cmd) {
  550. case FDEJECT:
  551. if (fs->ref_count != 1)
  552. return -EBUSY;
  553. err = floppy_eject(fs);
  554. return err;
  555. case FDGETPRM:
  556. if (copy_to_user((void __user *) param, (void *) &floppy_type,
  557. sizeof(struct floppy_struct)))
  558. return -EFAULT;
  559. break;
  560. default:
  561. printk(KERN_DEBUG "SWIM floppy_ioctl: unknown cmd %d\n",
  562. cmd);
  563. return -ENOSYS;
  564. }
  565. return 0;
  566. }
  567. static int floppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  568. {
  569. struct floppy_state *fs = bdev->bd_disk->private_data;
  570. struct floppy_struct *g;
  571. int ret;
  572. ret = get_floppy_geometry(fs, 0, &g);
  573. if (ret)
  574. return ret;
  575. geo->heads = g->head;
  576. geo->sectors = g->sect;
  577. geo->cylinders = g->track;
  578. return 0;
  579. }
  580. static int floppy_check_change(struct gendisk *disk)
  581. {
  582. struct floppy_state *fs = disk->private_data;
  583. return fs->ejected;
  584. }
  585. static int floppy_revalidate(struct gendisk *disk)
  586. {
  587. struct floppy_state *fs = disk->private_data;
  588. struct swim __iomem *base = fs->swd->base;
  589. swim_drive(base, fs->location);
  590. if (fs->ejected)
  591. setup_medium(fs);
  592. if (!fs->disk_in)
  593. swim_motor(base, OFF);
  594. else
  595. fs->ejected = 0;
  596. return !fs->disk_in;
  597. }
  598. static const struct block_device_operations floppy_fops = {
  599. .owner = THIS_MODULE,
  600. .open = floppy_open,
  601. .release = floppy_release,
  602. .locked_ioctl = floppy_ioctl,
  603. .getgeo = floppy_getgeo,
  604. .media_changed = floppy_check_change,
  605. .revalidate_disk = floppy_revalidate,
  606. };
  607. static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  608. {
  609. struct swim_priv *swd = data;
  610. int drive = (*part & 3);
  611. if (drive > swd->floppy_count)
  612. return NULL;
  613. *part = 0;
  614. return get_disk(swd->unit[drive].disk);
  615. }
  616. static int __devinit swim_add_floppy(struct swim_priv *swd,
  617. enum drive_location location)
  618. {
  619. struct floppy_state *fs = &swd->unit[swd->floppy_count];
  620. struct swim __iomem *base = swd->base;
  621. fs->location = location;
  622. swim_drive(base, location);
  623. swim_motor(base, OFF);
  624. if (swim_readbit(base, SINGLE_SIDED))
  625. fs->head_number = 1;
  626. else
  627. fs->head_number = 2;
  628. fs->ref_count = 0;
  629. fs->ejected = 1;
  630. swd->floppy_count++;
  631. return 0;
  632. }
  633. static int __devinit swim_floppy_init(struct swim_priv *swd)
  634. {
  635. int err;
  636. int drive;
  637. struct swim __iomem *base = swd->base;
  638. /* scan floppy drives */
  639. swim_drive(base, INTERNAL_DRIVE);
  640. if (swim_readbit(base, DRIVE_PRESENT))
  641. swim_add_floppy(swd, INTERNAL_DRIVE);
  642. swim_drive(base, EXTERNAL_DRIVE);
  643. if (swim_readbit(base, DRIVE_PRESENT))
  644. swim_add_floppy(swd, EXTERNAL_DRIVE);
  645. /* register floppy drives */
  646. err = register_blkdev(FLOPPY_MAJOR, "fd");
  647. if (err) {
  648. printk(KERN_ERR "Unable to get major %d for SWIM floppy\n",
  649. FLOPPY_MAJOR);
  650. return -EBUSY;
  651. }
  652. for (drive = 0; drive < swd->floppy_count; drive++) {
  653. swd->unit[drive].disk = alloc_disk(1);
  654. if (swd->unit[drive].disk == NULL) {
  655. err = -ENOMEM;
  656. goto exit_put_disks;
  657. }
  658. swd->unit[drive].swd = swd;
  659. }
  660. swd->queue = blk_init_queue(do_fd_request, &swd->lock);
  661. if (!swd->queue) {
  662. err = -ENOMEM;
  663. goto exit_put_disks;
  664. }
  665. for (drive = 0; drive < swd->floppy_count; drive++) {
  666. swd->unit[drive].disk->flags = GENHD_FL_REMOVABLE;
  667. swd->unit[drive].disk->major = FLOPPY_MAJOR;
  668. swd->unit[drive].disk->first_minor = drive;
  669. sprintf(swd->unit[drive].disk->disk_name, "fd%d", drive);
  670. swd->unit[drive].disk->fops = &floppy_fops;
  671. swd->unit[drive].disk->private_data = &swd->unit[drive];
  672. swd->unit[drive].disk->queue = swd->queue;
  673. set_capacity(swd->unit[drive].disk, 2880);
  674. add_disk(swd->unit[drive].disk);
  675. }
  676. blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  677. floppy_find, NULL, swd);
  678. return 0;
  679. exit_put_disks:
  680. unregister_blkdev(FLOPPY_MAJOR, "fd");
  681. while (drive--)
  682. put_disk(swd->unit[drive].disk);
  683. return err;
  684. }
  685. static int __devinit swim_probe(struct platform_device *dev)
  686. {
  687. struct resource *res;
  688. struct swim __iomem *swim_base;
  689. struct swim_priv *swd;
  690. int ret;
  691. res = platform_get_resource_byname(dev, IORESOURCE_MEM, "swim-regs");
  692. if (!res) {
  693. ret = -ENODEV;
  694. goto out;
  695. }
  696. if (!request_mem_region(res->start, resource_size(res), CARDNAME)) {
  697. ret = -EBUSY;
  698. goto out;
  699. }
  700. swim_base = ioremap(res->start, resource_size(res));
  701. if (!swim_base) {
  702. return -ENOMEM;
  703. goto out_release_io;
  704. }
  705. /* probe device */
  706. set_swim_mode(swim_base, 1);
  707. if (!get_swim_mode(swim_base)) {
  708. printk(KERN_INFO "SWIM device not found !\n");
  709. ret = -ENODEV;
  710. goto out_iounmap;
  711. }
  712. /* set platform driver data */
  713. swd = kzalloc(sizeof(struct swim_priv), GFP_KERNEL);
  714. if (!swd) {
  715. ret = -ENOMEM;
  716. goto out_iounmap;
  717. }
  718. platform_set_drvdata(dev, swd);
  719. swd->base = swim_base;
  720. ret = swim_floppy_init(swd);
  721. if (ret)
  722. goto out_kfree;
  723. return 0;
  724. out_kfree:
  725. platform_set_drvdata(dev, NULL);
  726. kfree(swd);
  727. out_iounmap:
  728. iounmap(swim_base);
  729. out_release_io:
  730. release_mem_region(res->start, resource_size(res));
  731. out:
  732. return ret;
  733. }
  734. static int __devexit swim_remove(struct platform_device *dev)
  735. {
  736. struct swim_priv *swd = platform_get_drvdata(dev);
  737. int drive;
  738. struct resource *res;
  739. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  740. for (drive = 0; drive < swd->floppy_count; drive++) {
  741. del_gendisk(swd->unit[drive].disk);
  742. put_disk(swd->unit[drive].disk);
  743. }
  744. unregister_blkdev(FLOPPY_MAJOR, "fd");
  745. blk_cleanup_queue(swd->queue);
  746. /* eject floppies */
  747. for (drive = 0; drive < swd->floppy_count; drive++)
  748. floppy_eject(&swd->unit[drive]);
  749. iounmap(swd->base);
  750. res = platform_get_resource_byname(dev, IORESOURCE_MEM, "swim-regs");
  751. if (res)
  752. release_mem_region(res->start, resource_size(res));
  753. platform_set_drvdata(dev, NULL);
  754. kfree(swd);
  755. return 0;
  756. }
  757. static struct platform_driver swim_driver = {
  758. .probe = swim_probe,
  759. .remove = __devexit_p(swim_remove),
  760. .driver = {
  761. .name = CARDNAME,
  762. .owner = THIS_MODULE,
  763. },
  764. };
  765. static int __init swim_init(void)
  766. {
  767. printk(KERN_INFO "SWIM floppy driver %s\n", DRIVER_VERSION);
  768. return platform_driver_register(&swim_driver);
  769. }
  770. module_init(swim_init);
  771. static void __exit swim_exit(void)
  772. {
  773. platform_driver_unregister(&swim_driver);
  774. }
  775. module_exit(swim_exit);
  776. MODULE_DESCRIPTION("Driver for SWIM floppy controller");
  777. MODULE_LICENSE("GPL");
  778. MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
  779. MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);