swim3.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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/signal.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 <linux/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 ONEMEG_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 GCR_MODE 13
  143. #define SEEK_COMPLETE 14
  144. #define TWOMEG_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, blk_status_t 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, BLK_STS_IOERR, 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(" current_nr_sectors=%u\n",
  307. 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, BLK_STS_IOERR, 0);
  313. continue;
  314. }
  315. if (fs->ejected) {
  316. swim3_dbg("%s", " disk ejected\n");
  317. swim3_end_request(fs, BLK_STS_IOERR, 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, BLK_STS_IOERR, 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, BLK_STS_IOERR, 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, BLK_STS_IOERR, 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, BLK_STS_IOERR, 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, BLK_STS_IOERR, 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, BLK_STS_IOERR, 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, BLK_STS_IOERR, 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, BLK_STS_IOERR, 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, BLK_STS_IOERR, 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)
  922. --fs->ref_count;
  923. else if (fs->ref_count == -1)
  924. fs->ref_count = 0;
  925. if (fs->ref_count == 0) {
  926. swim3_action(fs, MOTOR_OFF);
  927. out_8(&sw->control_bic, 0xff);
  928. swim3_select(fs, RELAX);
  929. }
  930. mutex_unlock(&swim3_mutex);
  931. }
  932. static unsigned int floppy_check_events(struct gendisk *disk,
  933. unsigned int clearing)
  934. {
  935. struct floppy_state *fs = disk->private_data;
  936. return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0;
  937. }
  938. static int floppy_revalidate(struct gendisk *disk)
  939. {
  940. struct floppy_state *fs = disk->private_data;
  941. struct swim3 __iomem *sw;
  942. int ret, n;
  943. if (fs->mdev->media_bay &&
  944. check_media_bay(fs->mdev->media_bay) != MB_FD)
  945. return -ENXIO;
  946. sw = fs->swim3;
  947. grab_drive(fs, revalidating, 0);
  948. out_8(&sw->intr_enable, 0);
  949. out_8(&sw->control_bis, DRIVE_ENABLE);
  950. swim3_action(fs, MOTOR_ON); /* necessary? */
  951. fs->write_prot = -1;
  952. fs->cur_cyl = -1;
  953. mdelay(1);
  954. for (n = HZ; n > 0; --n) {
  955. if (swim3_readbit(fs, SEEK_COMPLETE))
  956. break;
  957. if (signal_pending(current))
  958. break;
  959. swim3_select(fs, RELAX);
  960. schedule_timeout_interruptible(1);
  961. }
  962. ret = swim3_readbit(fs, SEEK_COMPLETE) == 0
  963. || swim3_readbit(fs, DISK_IN) == 0;
  964. if (ret)
  965. swim3_action(fs, MOTOR_OFF);
  966. else {
  967. fs->ejected = 0;
  968. swim3_action(fs, SETMFM);
  969. }
  970. swim3_select(fs, RELAX);
  971. release_drive(fs);
  972. return ret;
  973. }
  974. static const struct block_device_operations floppy_fops = {
  975. .open = floppy_unlocked_open,
  976. .release = floppy_release,
  977. .ioctl = floppy_ioctl,
  978. .check_events = floppy_check_events,
  979. .revalidate_disk= floppy_revalidate,
  980. };
  981. static void swim3_mb_event(struct macio_dev* mdev, int mb_state)
  982. {
  983. struct floppy_state *fs = macio_get_drvdata(mdev);
  984. struct swim3 __iomem *sw;
  985. if (!fs)
  986. return;
  987. sw = fs->swim3;
  988. if (mb_state != MB_FD)
  989. return;
  990. /* Clear state */
  991. out_8(&sw->intr_enable, 0);
  992. in_8(&sw->intr);
  993. in_8(&sw->error);
  994. }
  995. static int swim3_add_device(struct macio_dev *mdev, int index)
  996. {
  997. struct device_node *swim = mdev->ofdev.dev.of_node;
  998. struct floppy_state *fs = &floppy_states[index];
  999. int rc = -EBUSY;
  1000. /* Do this first for message macros */
  1001. memset(fs, 0, sizeof(*fs));
  1002. fs->mdev = mdev;
  1003. fs->index = index;
  1004. /* Check & Request resources */
  1005. if (macio_resource_count(mdev) < 2) {
  1006. swim3_err("%s", "No address in device-tree\n");
  1007. return -ENXIO;
  1008. }
  1009. if (macio_irq_count(mdev) < 1) {
  1010. swim3_err("%s", "No interrupt in device-tree\n");
  1011. return -ENXIO;
  1012. }
  1013. if (macio_request_resource(mdev, 0, "swim3 (mmio)")) {
  1014. swim3_err("%s", "Can't request mmio resource\n");
  1015. return -EBUSY;
  1016. }
  1017. if (macio_request_resource(mdev, 1, "swim3 (dma)")) {
  1018. swim3_err("%s", "Can't request dma resource\n");
  1019. macio_release_resource(mdev, 0);
  1020. return -EBUSY;
  1021. }
  1022. dev_set_drvdata(&mdev->ofdev.dev, fs);
  1023. if (mdev->media_bay == NULL)
  1024. pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 1);
  1025. fs->state = idle;
  1026. fs->swim3 = (struct swim3 __iomem *)
  1027. ioremap(macio_resource_start(mdev, 0), 0x200);
  1028. if (fs->swim3 == NULL) {
  1029. swim3_err("%s", "Couldn't map mmio registers\n");
  1030. rc = -ENOMEM;
  1031. goto out_release;
  1032. }
  1033. fs->dma = (struct dbdma_regs __iomem *)
  1034. ioremap(macio_resource_start(mdev, 1), 0x200);
  1035. if (fs->dma == NULL) {
  1036. swim3_err("%s", "Couldn't map dma registers\n");
  1037. iounmap(fs->swim3);
  1038. rc = -ENOMEM;
  1039. goto out_release;
  1040. }
  1041. fs->swim3_intr = macio_irq(mdev, 0);
  1042. fs->dma_intr = macio_irq(mdev, 1);
  1043. fs->cur_cyl = -1;
  1044. fs->cur_sector = -1;
  1045. fs->secpercyl = 36;
  1046. fs->secpertrack = 18;
  1047. fs->total_secs = 2880;
  1048. init_waitqueue_head(&fs->wait);
  1049. fs->dma_cmd = (struct dbdma_cmd *) DBDMA_ALIGN(fs->dbdma_cmd_space);
  1050. memset(fs->dma_cmd, 0, 2 * sizeof(struct dbdma_cmd));
  1051. fs->dma_cmd[1].command = cpu_to_le16(DBDMA_STOP);
  1052. if (mdev->media_bay == NULL || check_media_bay(mdev->media_bay) == MB_FD)
  1053. swim3_mb_event(mdev, MB_FD);
  1054. if (request_irq(fs->swim3_intr, swim3_interrupt, 0, "SWIM3", fs)) {
  1055. swim3_err("%s", "Couldn't request interrupt\n");
  1056. pmac_call_feature(PMAC_FTR_SWIM3_ENABLE, swim, 0, 0);
  1057. goto out_unmap;
  1058. return -EBUSY;
  1059. }
  1060. init_timer(&fs->timeout);
  1061. swim3_info("SWIM3 floppy controller %s\n",
  1062. mdev->media_bay ? "in media bay" : "");
  1063. return 0;
  1064. out_unmap:
  1065. iounmap(fs->dma);
  1066. iounmap(fs->swim3);
  1067. out_release:
  1068. macio_release_resource(mdev, 0);
  1069. macio_release_resource(mdev, 1);
  1070. return rc;
  1071. }
  1072. static int swim3_attach(struct macio_dev *mdev,
  1073. const struct of_device_id *match)
  1074. {
  1075. struct gendisk *disk;
  1076. int index, rc;
  1077. index = floppy_count++;
  1078. if (index >= MAX_FLOPPIES)
  1079. return -ENXIO;
  1080. /* Add the drive */
  1081. rc = swim3_add_device(mdev, index);
  1082. if (rc)
  1083. return rc;
  1084. /* Now register that disk. Same comment about failure handling */
  1085. disk = disks[index] = alloc_disk(1);
  1086. if (disk == NULL)
  1087. return -ENOMEM;
  1088. disk->queue = blk_init_queue(do_fd_request, &swim3_lock);
  1089. if (disk->queue == NULL) {
  1090. put_disk(disk);
  1091. return -ENOMEM;
  1092. }
  1093. blk_queue_bounce_limit(disk->queue, BLK_BOUNCE_HIGH);
  1094. disk->queue->queuedata = &floppy_states[index];
  1095. if (index == 0) {
  1096. /* If we failed, there isn't much we can do as the driver is still
  1097. * too dumb to remove the device, just bail out
  1098. */
  1099. if (register_blkdev(FLOPPY_MAJOR, "fd"))
  1100. return 0;
  1101. }
  1102. disk->major = FLOPPY_MAJOR;
  1103. disk->first_minor = index;
  1104. disk->fops = &floppy_fops;
  1105. disk->private_data = &floppy_states[index];
  1106. disk->flags |= GENHD_FL_REMOVABLE;
  1107. sprintf(disk->disk_name, "fd%d", index);
  1108. set_capacity(disk, 2880);
  1109. add_disk(disk);
  1110. return 0;
  1111. }
  1112. static const struct of_device_id swim3_match[] =
  1113. {
  1114. {
  1115. .name = "swim3",
  1116. },
  1117. {
  1118. .compatible = "ohare-swim3"
  1119. },
  1120. {
  1121. .compatible = "swim3"
  1122. },
  1123. { /* end of list */ }
  1124. };
  1125. static struct macio_driver swim3_driver =
  1126. {
  1127. .driver = {
  1128. .name = "swim3",
  1129. .of_match_table = swim3_match,
  1130. },
  1131. .probe = swim3_attach,
  1132. #ifdef CONFIG_PMAC_MEDIABAY
  1133. .mediabay_event = swim3_mb_event,
  1134. #endif
  1135. #if 0
  1136. .suspend = swim3_suspend,
  1137. .resume = swim3_resume,
  1138. #endif
  1139. };
  1140. int swim3_init(void)
  1141. {
  1142. macio_register_driver(&swim3_driver);
  1143. return 0;
  1144. }
  1145. module_init(swim3_init)
  1146. MODULE_LICENSE("GPL");
  1147. MODULE_AUTHOR("Paul Mackerras");
  1148. MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);