swim.c 20 KB

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