swim.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for SWIM (Sander Woz Integrated Machine) floppy controller
  4. *
  5. * Copyright (C) 2004,2008 Laurent Vivier <Laurent@lvivier.info>
  6. *
  7. * based on Alastair Bridgewater SWIM analysis, 2001
  8. * based on SWIM3 driver (c) Paul Mackerras, 1996
  9. * based on netBSD IWM driver (c) 1997, 1998 Hauke Fath.
  10. *
  11. * 2004-08-21 (lv) - Initial implementation
  12. * 2008-10-30 (lv) - Port to 2.6
  13. */
  14. #include <linux/module.h>
  15. #include <linux/fd.h>
  16. #include <linux/slab.h>
  17. #include <linux/blk-mq.h>
  18. #include <linux/major.h>
  19. #include <linux/mutex.h>
  20. #include <linux/hdreg.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <asm/mac_via.h>
  25. #define CARDNAME "swim"
  26. struct sector_header {
  27. unsigned char side;
  28. unsigned char track;
  29. unsigned char sector;
  30. unsigned char size;
  31. unsigned char crc0;
  32. unsigned char crc1;
  33. } __attribute__((packed));
  34. #define DRIVER_VERSION "Version 0.2 (2008-10-30)"
  35. #define REG(x) unsigned char x, x ## _pad[0x200 - 1];
  36. struct swim {
  37. REG(write_data)
  38. REG(write_mark)
  39. REG(write_CRC)
  40. REG(write_parameter)
  41. REG(write_phase)
  42. REG(write_setup)
  43. REG(write_mode0)
  44. REG(write_mode1)
  45. REG(read_data)
  46. REG(read_mark)
  47. REG(read_error)
  48. REG(read_parameter)
  49. REG(read_phase)
  50. REG(read_setup)
  51. REG(read_status)
  52. REG(read_handshake)
  53. } __attribute__((packed));
  54. #define swim_write(base, reg, v) out_8(&(base)->write_##reg, (v))
  55. #define swim_read(base, reg) in_8(&(base)->read_##reg)
  56. /* IWM registers */
  57. struct iwm {
  58. REG(ph0L)
  59. REG(ph0H)
  60. REG(ph1L)
  61. REG(ph1H)
  62. REG(ph2L)
  63. REG(ph2H)
  64. REG(ph3L)
  65. REG(ph3H)
  66. REG(mtrOff)
  67. REG(mtrOn)
  68. REG(intDrive)
  69. REG(extDrive)
  70. REG(q6L)
  71. REG(q6H)
  72. REG(q7L)
  73. REG(q7H)
  74. } __attribute__((packed));
  75. #define iwm_write(base, reg, v) out_8(&(base)->reg, (v))
  76. #define iwm_read(base, reg) in_8(&(base)->reg)
  77. /* bits in phase register */
  78. #define SEEK_POSITIVE 0x070
  79. #define SEEK_NEGATIVE 0x074
  80. #define STEP 0x071
  81. #define MOTOR_ON 0x072
  82. #define MOTOR_OFF 0x076
  83. #define INDEX 0x073
  84. #define EJECT 0x077
  85. #define SETMFM 0x171
  86. #define SETGCR 0x175
  87. #define RELAX 0x033
  88. #define LSTRB 0x008
  89. #define CA_MASK 0x077
  90. /* Select values for swim_select and swim_readbit */
  91. #define READ_DATA_0 0x074
  92. #define ONEMEG_DRIVE 0x075
  93. #define SINGLE_SIDED 0x076
  94. #define DRIVE_PRESENT 0x077
  95. #define DISK_IN 0x170
  96. #define WRITE_PROT 0x171
  97. #define TRACK_ZERO 0x172
  98. #define TACHO 0x173
  99. #define READ_DATA_1 0x174
  100. #define GCR_MODE 0x175
  101. #define SEEK_COMPLETE 0x176
  102. #define TWOMEG_MEDIA 0x177
  103. /* Bits in handshake register */
  104. #define MARK_BYTE 0x01
  105. #define CRC_ZERO 0x02
  106. #define RDDATA 0x04
  107. #define SENSE 0x08
  108. #define MOTEN 0x10
  109. #define ERROR 0x20
  110. #define DAT2BYTE 0x40
  111. #define DAT1BYTE 0x80
  112. /* bits in setup register */
  113. #define S_INV_WDATA 0x01
  114. #define S_3_5_SELECT 0x02
  115. #define S_GCR 0x04
  116. #define S_FCLK_DIV2 0x08
  117. #define S_ERROR_CORR 0x10
  118. #define S_IBM_DRIVE 0x20
  119. #define S_GCR_WRITE 0x40
  120. #define S_TIMEOUT 0x80
  121. /* bits in mode register */
  122. #define CLFIFO 0x01
  123. #define ENBL1 0x02
  124. #define ENBL2 0x04
  125. #define ACTION 0x08
  126. #define WRITE_MODE 0x10
  127. #define HEDSEL 0x20
  128. #define MOTON 0x80
  129. /*----------------------------------------------------------------------------*/
  130. enum drive_location {
  131. INTERNAL_DRIVE = 0x02,
  132. EXTERNAL_DRIVE = 0x04,
  133. };
  134. enum media_type {
  135. DD_MEDIA,
  136. HD_MEDIA,
  137. };
  138. struct floppy_state {
  139. /* physical properties */
  140. enum drive_location location; /* internal or external drive */
  141. int head_number; /* single- or double-sided drive */
  142. /* media */
  143. int disk_in;
  144. int ejected;
  145. enum media_type type;
  146. int write_protected;
  147. int total_secs;
  148. int secpercyl;
  149. int secpertrack;
  150. /* in-use information */
  151. int track;
  152. int ref_count;
  153. bool registered;
  154. struct gendisk *disk;
  155. struct blk_mq_tag_set tag_set;
  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. int floppy_count;
  172. struct floppy_state unit[FD_MAX_UNIT];
  173. };
  174. extern int swim_read_sector_header(struct swim __iomem *base,
  175. struct sector_header *header);
  176. extern int swim_read_sector_data(struct swim __iomem *base,
  177. unsigned char *data);
  178. static DEFINE_MUTEX(swim_mutex);
  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. set_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. set_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. set_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 blk_status_t 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 BLK_STS_IOERR;
  409. } while (ret != 512);
  410. buffer += ret;
  411. }
  412. return 0;
  413. }
  414. static blk_status_t swim_queue_rq(struct blk_mq_hw_ctx *hctx,
  415. const struct blk_mq_queue_data *bd)
  416. {
  417. struct floppy_state *fs = hctx->queue->queuedata;
  418. struct swim_priv *swd = fs->swd;
  419. struct request *req = bd->rq;
  420. blk_status_t err;
  421. if (!spin_trylock_irq(&swd->lock))
  422. return BLK_STS_DEV_RESOURCE;
  423. blk_mq_start_request(req);
  424. if (!fs->disk_in || rq_data_dir(req) == WRITE) {
  425. err = BLK_STS_IOERR;
  426. goto out;
  427. }
  428. do {
  429. err = floppy_read_sectors(fs, blk_rq_pos(req),
  430. blk_rq_cur_sectors(req),
  431. bio_data(req->bio));
  432. } while (blk_update_request(req, err, blk_rq_cur_bytes(req)));
  433. __blk_mq_end_request(req, err);
  434. err = BLK_STS_OK;
  435. out:
  436. spin_unlock_irq(&swd->lock);
  437. return err;
  438. }
  439. static struct floppy_struct floppy_type[4] = {
  440. { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL }, /* no testing */
  441. { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 360KB SS 3.5"*/
  442. { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 720KB 3.5" */
  443. { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL }, /* 1.44MB 3.5" */
  444. };
  445. static int get_floppy_geometry(struct floppy_state *fs, int type,
  446. struct floppy_struct **g)
  447. {
  448. if (type >= ARRAY_SIZE(floppy_type))
  449. return -EINVAL;
  450. if (type)
  451. *g = &floppy_type[type];
  452. else if (fs->type == HD_MEDIA) /* High-Density media */
  453. *g = &floppy_type[3];
  454. else if (fs->head_number == 2) /* double-sided */
  455. *g = &floppy_type[2];
  456. else
  457. *g = &floppy_type[1];
  458. return 0;
  459. }
  460. static void setup_medium(struct floppy_state *fs)
  461. {
  462. struct swim __iomem *base = fs->swd->base;
  463. if (swim_readbit(base, DISK_IN)) {
  464. struct floppy_struct *g;
  465. fs->disk_in = 1;
  466. fs->write_protected = swim_readbit(base, WRITE_PROT);
  467. if (swim_track00(base))
  468. printk(KERN_ERR
  469. "SWIM: cannot move floppy head to track 0\n");
  470. swim_track00(base);
  471. fs->type = swim_readbit(base, TWOMEG_MEDIA) ?
  472. HD_MEDIA : DD_MEDIA;
  473. fs->head_number = swim_readbit(base, SINGLE_SIDED) ? 1 : 2;
  474. get_floppy_geometry(fs, 0, &g);
  475. fs->total_secs = g->size;
  476. fs->secpercyl = g->head * g->sect;
  477. fs->secpertrack = g->sect;
  478. fs->track = 0;
  479. } else {
  480. fs->disk_in = 0;
  481. }
  482. }
  483. static int floppy_open(struct gendisk *disk, blk_mode_t mode)
  484. {
  485. struct floppy_state *fs = disk->private_data;
  486. struct swim __iomem *base = fs->swd->base;
  487. int err;
  488. if (fs->ref_count == -1 || (fs->ref_count && mode & BLK_OPEN_EXCL))
  489. return -EBUSY;
  490. if (mode & BLK_OPEN_EXCL)
  491. fs->ref_count = -1;
  492. else
  493. fs->ref_count++;
  494. swim_write(base, setup, S_IBM_DRIVE | S_FCLK_DIV2);
  495. udelay(10);
  496. swim_drive(base, fs->location);
  497. swim_motor(base, ON);
  498. swim_action(base, SETMFM);
  499. if (fs->ejected)
  500. setup_medium(fs);
  501. if (!fs->disk_in) {
  502. err = -ENXIO;
  503. goto out;
  504. }
  505. set_capacity(fs->disk, fs->total_secs);
  506. if (mode & BLK_OPEN_NDELAY)
  507. return 0;
  508. if (mode & (BLK_OPEN_READ | BLK_OPEN_WRITE)) {
  509. if (disk_check_media_change(disk) && fs->disk_in)
  510. fs->ejected = 0;
  511. if ((mode & BLK_OPEN_WRITE) && fs->write_protected) {
  512. err = -EROFS;
  513. goto out;
  514. }
  515. }
  516. return 0;
  517. out:
  518. if (fs->ref_count < 0)
  519. fs->ref_count = 0;
  520. else if (fs->ref_count > 0)
  521. --fs->ref_count;
  522. if (fs->ref_count == 0)
  523. swim_motor(base, OFF);
  524. return err;
  525. }
  526. static int floppy_unlocked_open(struct gendisk *disk, blk_mode_t mode)
  527. {
  528. int ret;
  529. mutex_lock(&swim_mutex);
  530. ret = floppy_open(disk, mode);
  531. mutex_unlock(&swim_mutex);
  532. return ret;
  533. }
  534. static void floppy_release(struct gendisk *disk)
  535. {
  536. struct floppy_state *fs = disk->private_data;
  537. struct swim __iomem *base = fs->swd->base;
  538. mutex_lock(&swim_mutex);
  539. if (fs->ref_count < 0)
  540. fs->ref_count = 0;
  541. else if (fs->ref_count > 0)
  542. --fs->ref_count;
  543. if (fs->ref_count == 0)
  544. swim_motor(base, OFF);
  545. mutex_unlock(&swim_mutex);
  546. }
  547. static int floppy_ioctl(struct block_device *bdev, blk_mode_t mode,
  548. unsigned int cmd, unsigned long param)
  549. {
  550. struct floppy_state *fs = bdev->bd_disk->private_data;
  551. int err;
  552. if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))
  553. return -EPERM;
  554. switch (cmd) {
  555. case FDEJECT:
  556. if (fs->ref_count != 1)
  557. return -EBUSY;
  558. mutex_lock(&swim_mutex);
  559. err = floppy_eject(fs);
  560. mutex_unlock(&swim_mutex);
  561. return err;
  562. case FDGETPRM:
  563. if (copy_to_user((void __user *) param, (void *) &floppy_type,
  564. sizeof(struct floppy_struct)))
  565. return -EFAULT;
  566. return 0;
  567. }
  568. return -ENOTTY;
  569. }
  570. static int floppy_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  571. {
  572. struct floppy_state *fs = bdev->bd_disk->private_data;
  573. struct floppy_struct *g;
  574. int ret;
  575. ret = get_floppy_geometry(fs, 0, &g);
  576. if (ret)
  577. return ret;
  578. geo->heads = g->head;
  579. geo->sectors = g->sect;
  580. geo->cylinders = g->track;
  581. return 0;
  582. }
  583. static unsigned int floppy_check_events(struct gendisk *disk,
  584. unsigned int clearing)
  585. {
  586. struct floppy_state *fs = disk->private_data;
  587. return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
  588. }
  589. static const struct block_device_operations floppy_fops = {
  590. .owner = THIS_MODULE,
  591. .open = floppy_unlocked_open,
  592. .release = floppy_release,
  593. .ioctl = floppy_ioctl,
  594. .getgeo = floppy_getgeo,
  595. .check_events = floppy_check_events,
  596. };
  597. static int swim_add_floppy(struct swim_priv *swd, enum drive_location location)
  598. {
  599. struct floppy_state *fs = &swd->unit[swd->floppy_count];
  600. struct swim __iomem *base = swd->base;
  601. fs->location = location;
  602. swim_drive(base, location);
  603. swim_motor(base, OFF);
  604. fs->type = HD_MEDIA;
  605. fs->head_number = 2;
  606. fs->ref_count = 0;
  607. fs->ejected = 1;
  608. swd->floppy_count++;
  609. return 0;
  610. }
  611. static const struct blk_mq_ops swim_mq_ops = {
  612. .queue_rq = swim_queue_rq,
  613. };
  614. static void swim_cleanup_floppy_disk(struct floppy_state *fs)
  615. {
  616. struct gendisk *disk = fs->disk;
  617. if (!disk)
  618. return;
  619. if (fs->registered)
  620. del_gendisk(fs->disk);
  621. put_disk(disk);
  622. blk_mq_free_tag_set(&fs->tag_set);
  623. }
  624. static int swim_floppy_init(struct swim_priv *swd)
  625. {
  626. struct queue_limits lim = {
  627. .features = BLK_FEAT_ROTATIONAL,
  628. };
  629. int err;
  630. int drive;
  631. struct swim __iomem *base = swd->base;
  632. /* scan floppy drives */
  633. swim_drive(base, INTERNAL_DRIVE);
  634. if (swim_readbit(base, DRIVE_PRESENT) &&
  635. !swim_readbit(base, ONEMEG_DRIVE))
  636. swim_add_floppy(swd, INTERNAL_DRIVE);
  637. swim_drive(base, EXTERNAL_DRIVE);
  638. if (swim_readbit(base, DRIVE_PRESENT) &&
  639. !swim_readbit(base, ONEMEG_DRIVE))
  640. swim_add_floppy(swd, EXTERNAL_DRIVE);
  641. /* register floppy drives */
  642. err = register_blkdev(FLOPPY_MAJOR, "fd");
  643. if (err) {
  644. printk(KERN_ERR "Unable to get major %d for SWIM floppy\n",
  645. FLOPPY_MAJOR);
  646. return -EBUSY;
  647. }
  648. spin_lock_init(&swd->lock);
  649. for (drive = 0; drive < swd->floppy_count; drive++) {
  650. err = blk_mq_alloc_sq_tag_set(&swd->unit[drive].tag_set,
  651. &swim_mq_ops, 2, BLK_MQ_F_SHOULD_MERGE);
  652. if (err)
  653. goto exit_put_disks;
  654. swd->unit[drive].disk =
  655. blk_mq_alloc_disk(&swd->unit[drive].tag_set, &lim,
  656. &swd->unit[drive]);
  657. if (IS_ERR(swd->unit[drive].disk)) {
  658. blk_mq_free_tag_set(&swd->unit[drive].tag_set);
  659. err = PTR_ERR(swd->unit[drive].disk);
  660. goto exit_put_disks;
  661. }
  662. swd->unit[drive].swd = swd;
  663. }
  664. for (drive = 0; drive < swd->floppy_count; drive++) {
  665. swd->unit[drive].disk->flags = GENHD_FL_REMOVABLE;
  666. swd->unit[drive].disk->major = FLOPPY_MAJOR;
  667. swd->unit[drive].disk->first_minor = drive;
  668. swd->unit[drive].disk->minors = 1;
  669. sprintf(swd->unit[drive].disk->disk_name, "fd%d", drive);
  670. swd->unit[drive].disk->fops = &floppy_fops;
  671. swd->unit[drive].disk->flags |= GENHD_FL_NO_PART;
  672. swd->unit[drive].disk->events = DISK_EVENT_MEDIA_CHANGE;
  673. swd->unit[drive].disk->private_data = &swd->unit[drive];
  674. set_capacity(swd->unit[drive].disk, 2880);
  675. err = add_disk(swd->unit[drive].disk);
  676. if (err)
  677. goto exit_put_disks;
  678. swd->unit[drive].registered = true;
  679. }
  680. return 0;
  681. exit_put_disks:
  682. unregister_blkdev(FLOPPY_MAJOR, "fd");
  683. do {
  684. swim_cleanup_floppy_disk(&swd->unit[drive]);
  685. } while (drive--);
  686. return err;
  687. }
  688. static int swim_probe(struct platform_device *dev)
  689. {
  690. struct resource *res;
  691. struct swim __iomem *swim_base;
  692. struct swim_priv *swd;
  693. int ret;
  694. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  695. if (!res) {
  696. ret = -ENODEV;
  697. goto out;
  698. }
  699. if (!request_mem_region(res->start, resource_size(res), CARDNAME)) {
  700. ret = -EBUSY;
  701. goto out;
  702. }
  703. swim_base = (struct swim __iomem *)res->start;
  704. if (!swim_base) {
  705. ret = -ENOMEM;
  706. goto out_release_io;
  707. }
  708. /* probe device */
  709. set_swim_mode(swim_base, 1);
  710. if (!get_swim_mode(swim_base)) {
  711. printk(KERN_INFO "SWIM device not found !\n");
  712. ret = -ENODEV;
  713. goto out_release_io;
  714. }
  715. /* set platform driver data */
  716. swd = kzalloc(sizeof(struct swim_priv), GFP_KERNEL);
  717. if (!swd) {
  718. ret = -ENOMEM;
  719. goto out_release_io;
  720. }
  721. platform_set_drvdata(dev, swd);
  722. swd->base = swim_base;
  723. ret = swim_floppy_init(swd);
  724. if (ret)
  725. goto out_kfree;
  726. return 0;
  727. out_kfree:
  728. kfree(swd);
  729. out_release_io:
  730. release_mem_region(res->start, resource_size(res));
  731. out:
  732. return ret;
  733. }
  734. static void swim_remove(struct platform_device *dev)
  735. {
  736. struct swim_priv *swd = platform_get_drvdata(dev);
  737. int drive;
  738. struct resource *res;
  739. for (drive = 0; drive < swd->floppy_count; drive++)
  740. swim_cleanup_floppy_disk(&swd->unit[drive]);
  741. unregister_blkdev(FLOPPY_MAJOR, "fd");
  742. /* eject floppies */
  743. for (drive = 0; drive < swd->floppy_count; drive++)
  744. floppy_eject(&swd->unit[drive]);
  745. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  746. if (res)
  747. release_mem_region(res->start, resource_size(res));
  748. kfree(swd);
  749. }
  750. static struct platform_driver swim_driver = {
  751. .probe = swim_probe,
  752. .remove_new = swim_remove,
  753. .driver = {
  754. .name = CARDNAME,
  755. },
  756. };
  757. static int __init swim_init(void)
  758. {
  759. printk(KERN_INFO "SWIM floppy driver %s\n", DRIVER_VERSION);
  760. return platform_driver_register(&swim_driver);
  761. }
  762. module_init(swim_init);
  763. static void __exit swim_exit(void)
  764. {
  765. platform_driver_unregister(&swim_driver);
  766. }
  767. module_exit(swim_exit);
  768. MODULE_DESCRIPTION("Driver for SWIM floppy controller");
  769. MODULE_LICENSE("GPL");
  770. MODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>");
  771. MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);