swim3.c 28 KB

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