fd1772.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  1. /*
  2. * linux/kernel/arch/arm/drivers/block/fd1772.c
  3. * Based on ataflop.c in the m68k Linux
  4. * Copyright (C) 1993 Greg Harp
  5. * Atari Support by Bjoern Brauel, Roman Hodek
  6. * Archimedes Support by Dave Gilbert (linux@treblig.org)
  7. *
  8. * Big cleanup Sep 11..14 1994 Roman Hodek:
  9. * - Driver now works interrupt driven
  10. * - Support for two drives; should work, but I cannot test that :-(
  11. * - Reading is done in whole tracks and buffered to speed up things
  12. * - Disk change detection and drive deselecting after motor-off
  13. * similar to TOS
  14. * - Autodetection of disk format (DD/HD); untested yet, because I
  15. * don't have an HD drive :-(
  16. *
  17. * Fixes Nov 13 1994 Martin Schaller:
  18. * - Autodetection works now
  19. * - Support for 5 1/4" disks
  20. * - Removed drive type (unknown on atari)
  21. * - Do seeks with 8 Mhz
  22. *
  23. * Changes by Andreas Schwab:
  24. * - After errors in multiple read mode try again reading single sectors
  25. * (Feb 1995):
  26. * - Clean up error handling
  27. * - Set blk_size for proper size checking
  28. * - Initialize track register when testing presence of floppy
  29. * - Implement some ioctl's
  30. *
  31. * Changes by Torsten Lang:
  32. * - When probing the floppies we should add the FDC1772CMDADD_H flag since
  33. * the FDC1772 will otherwise wait forever when no disk is inserted...
  34. *
  35. * Things left to do:
  36. * - Formatting
  37. * - Maybe a better strategy for disk change detection (does anyone
  38. * know one?)
  39. * - There are some strange problems left: The strangest one is
  40. * that, at least on my TT (4+4MB), the first 2 Bytes of the last
  41. * page of the TT-Ram (!) change their contents (some bits get
  42. * set) while a floppy DMA is going on. But there are no accesses
  43. * to these memory locations from the kernel... (I tested that by
  44. * making the page read-only). I cannot explain what's going on...
  45. * - Sometimes the drive-change-detection stops to work. The
  46. * function is still called, but the WP bit always reads as 0...
  47. * Maybe a problem with the status reg mode or a timing problem.
  48. * Note 10/12/94: The change detection now seems to work reliably.
  49. * There is no proof, but I've seen no hang for a long time...
  50. *
  51. * ARCHIMEDES changes: (gilbertd@cs.man.ac.uk)
  52. * 26/12/95 - Changed all names starting with FDC to FDC1772
  53. * Removed all references to clock speed of FDC - we're stuck with 8MHz
  54. * Modified disk_type structure to remove HD formats
  55. *
  56. * 7/ 1/96 - Wrote FIQ code, removed most remaining atariisms
  57. *
  58. * 13/ 1/96 - Well I think its read a single sector; but there is a problem
  59. * fd_rwsec_done which is called in FIQ mode starts another transfer
  60. * off (in fd_rwsec) while still in FIQ mode. Because its still in
  61. * FIQ mode it can't service the DMA and loses data. So need to
  62. * heavily restructure.
  63. * 14/ 1/96 - Found that the definitions of the register numbers of the
  64. * FDC were multiplied by 2 in the header for the 16bit words
  65. * of the atari so half the writes were going in the wrong place.
  66. * Also realised that the FIQ entry didn't make any attempt to
  67. * preserve registers or return correctly; now in assembler.
  68. *
  69. * 11/ 2/96 - Hmm - doesn't work on real machine. Auto detect doesn't
  70. * and hacking that past seems to wait forever - check motor
  71. * being turned on.
  72. *
  73. * 17/ 2/96 - still having problems - forcing track to -1 when selecting
  74. * new drives seems to allow it to read first few sectors
  75. * but then we get solid hangs at apparently random places
  76. * which change depending what is happening.
  77. *
  78. * 9/ 3/96 - Fiddled a lot of stuff around to move to kernel 1.3.35
  79. * A lot of fiddling in DMA stuff. Having problems with it
  80. * constnatly thinking its timeing out. Ah - its timeout
  81. * was set to (6*HZ) rather than jiffies+(6*HZ). Now giving
  82. * duff data!
  83. *
  84. * 5/ 4/96 - Made it use the new IOC_ macros rather than *ioc
  85. * Hmm - giving unexpected FIQ and then timeouts
  86. * 18/ 8/96 - Ran through indent -kr -i8
  87. * Some changes to disc change detect; don't know how well it
  88. * works.
  89. * 24/ 8/96 - Put all the track buffering code back in from the atari
  90. * code - I wonder if it will still work... No :-)
  91. * Still works if I turn off track buffering.
  92. * 25/ 8/96 - Changed the timer expires that I'd added back to be
  93. * jiffies + ....; and it all sprang to life! Got 2.8K/sec
  94. * off a cp -r of a 679K disc (showed 94% cpu usage!)
  95. * (PC gets 14.3K/sec - 0% CPU!) Hmm - hard drive corrupt!
  96. * Also perhaps that compile was with cache off.
  97. * changed cli in fd_readtrack_check to cliIF
  98. * changed vmallocs to kmalloc (whats the difference!!)
  99. * Removed the busy wait loop in do_fd_request and replaced
  100. * by a routine on tq_immediate; only 11% cpu on a dd off the
  101. * raw disc - but the speed is the same.
  102. * 1/ 9/96 - Idea (failed!) - set the 'disable spin-up sequence'
  103. * when we read the track if we know the motor is on; didn't
  104. * help - perhaps we have to do it in stepping as well.
  105. * Nope. Still doesn't help.
  106. * Hmm - what seems to be happening is that fd_readtrack_check
  107. * is never getting called. Its job is to terminate the read
  108. * just after we think we should have got the data; otherwise
  109. * the fdc takes 1 second to timeout; which is what's happening
  110. * Now I can see 'readtrack_timer' being set (which should do the
  111. * call); but it never seems to be called - hmm!
  112. * OK - I've moved the check to my tq_immediate code -
  113. * and it WORKS! 13.95K/second at 19% CPU.
  114. * I wish I knew why that timer didn't work.....
  115. *
  116. * 16/11/96 - Fiddled and frigged for 2.0.18
  117. *
  118. * DAG 30/01/99 - Started frobbing for 2.2.1
  119. * DAG 20/06/99 - A little more frobbing:
  120. * Included include/asm/uaccess.h for get_user/put_user
  121. *
  122. * DAG 1/09/00 - Dusted off for 2.4.0-test7
  123. * MAX_SECTORS was name clashing so it is now FD1772_...
  124. * Minor parameter, name layouts for 2.4.x differences
  125. */
  126. #include <linux/sched.h>
  127. #include <linux/fs.h>
  128. #include <linux/fcntl.h>
  129. #include <linux/slab.h>
  130. #include <linux/kernel.h>
  131. #include <linux/interrupt.h>
  132. #include <linux/timer.h>
  133. #include <linux/workqueue.h>
  134. #include <linux/fd.h>
  135. #include <linux/fd1772.h>
  136. #include <linux/errno.h>
  137. #include <linux/types.h>
  138. #include <linux/delay.h>
  139. #include <linux/mm.h>
  140. #include <linux/bitops.h>
  141. #include <asm/arch/oldlatches.h>
  142. #include <asm/dma.h>
  143. #include <asm/hardware.h>
  144. #include <asm/hardware/ioc.h>
  145. #include <asm/io.h>
  146. #include <asm/irq.h>
  147. #include <asm/mach-types.h>
  148. #include <asm/pgtable.h>
  149. #include <asm/system.h>
  150. #include <asm/uaccess.h>
  151. /* Note: FD_MAX_UNITS could be redefined to 2 for the Atari (with
  152. * little additional rework in this file). But I'm not yet sure if
  153. * some other code depends on the number of floppies... (It is defined
  154. * in a public header!)
  155. */
  156. #if 0
  157. #undef FD_MAX_UNITS
  158. #define FD_MAX_UNITS 2
  159. #endif
  160. /* Ditto worries for Arc - DAG */
  161. #define FD_MAX_UNITS 4
  162. #define TRACKBUFFER 0
  163. /*#define DEBUG*/
  164. #ifdef DEBUG
  165. #define DPRINT(a) printk a
  166. #else
  167. #define DPRINT(a)
  168. #endif
  169. static struct request_queue *floppy_queue;
  170. #define MAJOR_NR FLOPPY_MAJOR
  171. #define FLOPPY_DMA 0
  172. #define DEVICE_NAME "floppy"
  173. #define QUEUE (floppy_queue)
  174. #define CURRENT elv_next_request(floppy_queue)
  175. /* Disk types: DD */
  176. static struct archy_disk_type {
  177. const char *name;
  178. unsigned spt; /* sectors per track */
  179. unsigned blocks; /* total number of blocks */
  180. unsigned stretch; /* track doubling ? */
  181. } disk_type[] = {
  182. { "d360", 9, 720, 0 }, /* 360kB diskette */
  183. { "D360", 9, 720, 1 }, /* 360kb in 720kb drive */
  184. { "D720", 9, 1440, 0 }, /* 720kb diskette (DD) */
  185. /*{ "D820", 10,1640, 0}, *//* DD disk with 82 tracks/10 sectors
  186. - DAG - can't see how type detect can distinguish this
  187. from 720K until it reads block 4 by which time its too late! */
  188. };
  189. #define NUM_DISK_TYPES (sizeof(disk_type)/sizeof(*disk_type))
  190. /*
  191. * Maximum disk size (in kilobytes). This default is used whenever the
  192. * current disk size is unknown.
  193. */
  194. #define MAX_DISK_SIZE 720
  195. static struct gendisk *disks[FD_MAX_UNIT];
  196. /* current info on each unit */
  197. static struct archy_floppy_struct {
  198. int connected; /* !=0 : drive is connected */
  199. int autoprobe; /* !=0 : do autoprobe */
  200. struct archy_disk_type *disktype; /* current type of disk */
  201. int track; /* current head position or -1
  202. * if unknown */
  203. unsigned int steprate; /* steprate setting */
  204. unsigned int wpstat; /* current state of WP signal
  205. * (for disk change detection) */
  206. } unit[FD_MAX_UNITS];
  207. /* DAG: On Arc we spin on a flag being cleared by fdc1772_comendhandler which
  208. is an assembler routine */
  209. extern void fdc1772_comendhandler(void); /* Actually doens't have these parameters - see fd1772.S */
  210. extern volatile int fdc1772_comendstatus;
  211. extern volatile int fdc1772_fdc_int_done;
  212. #define FDC1772BASE ((0x210000>>2)|0x80000000)
  213. #define FDC1772_READ(reg) inb(FDC1772BASE+(reg/2))
  214. /* DAG: You wouldn't be silly to ask why FDC1772_WRITE is a function rather
  215. than the #def below - well simple - the #def won't compile - and I
  216. don't understand why (__outwc not defined) */
  217. /* NOTE: Reg is 0,2,4,6 as opposed to 0,1,2,3 or 0,4,8,12 to keep compatibility
  218. with the ST version of fd1772.h */
  219. /*#define FDC1772_WRITE(reg,val) outw(val,(reg+FDC1772BASE)); */
  220. void FDC1772_WRITE(int reg, unsigned char val)
  221. {
  222. if (reg == FDC1772REG_CMD) {
  223. DPRINT(("FDC1772_WRITE new command 0x%x @ %d\n", val,jiffies));
  224. if (fdc1772_fdc_int_done) {
  225. DPRINT(("FDC1772_WRITE: Hmm fdc1772_fdc_int_done true - resetting\n"));
  226. fdc1772_fdc_int_done = 0;
  227. };
  228. };
  229. outb(val, (reg / 2) + FDC1772BASE);
  230. };
  231. #define FD1772_MAX_SECTORS 22
  232. unsigned char *DMABuffer; /* buffer for writes */
  233. /*static unsigned long PhysDMABuffer; *//* physical address */
  234. /* DAG: On Arc we just go straight for the DMA buffer */
  235. #define PhysDMABuffer DMABuffer
  236. #ifdef TRACKBUFFER
  237. unsigned char *TrackBuffer; /* buffer for reads */
  238. #define PhysTrackBuffer TrackBuffer /* physical address */
  239. static int BufferDrive, BufferSide, BufferTrack;
  240. static int read_track; /* non-zero if we are reading whole tracks */
  241. #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
  242. #define IS_BUFFERED(drive,side,track) \
  243. (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
  244. #endif
  245. /*
  246. * These are global variables, as that's the easiest way to give
  247. * information to interrupts. They are the data used for the current
  248. * request.
  249. */
  250. static int SelectedDrive = 0;
  251. static int ReqCmd, ReqBlock;
  252. static int ReqSide, ReqTrack, ReqSector, ReqCnt;
  253. static int HeadSettleFlag = 0;
  254. static unsigned char *ReqData, *ReqBuffer;
  255. static int MotorOn = 0, MotorOffTrys;
  256. /* Synchronization of FDC1772 access. */
  257. static volatile int fdc_busy = 0;
  258. static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
  259. /* long req'd for set_bit --RR */
  260. static unsigned long changed_floppies = 0xff, fake_change = 0;
  261. #define CHECK_CHANGE_DELAY HZ/2
  262. /* DAG - increased to 30*HZ - not sure if this is the correct thing to do */
  263. #define FD_MOTOR_OFF_DELAY (10*HZ)
  264. #define FD_MOTOR_OFF_MAXTRY (10*20)
  265. #define FLOPPY_TIMEOUT (6*HZ)
  266. #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
  267. * will be recalibrated. */
  268. #define MAX_ERRORS 8 /* After this many errors the driver
  269. * will give up. */
  270. #define START_MOTOR_OFF_TIMER(delay) \
  271. do { \
  272. motor_off_timer.expires = jiffies + (delay); \
  273. add_timer( &motor_off_timer ); \
  274. MotorOffTrys = 0; \
  275. } while(0)
  276. #define START_CHECK_CHANGE_TIMER(delay) \
  277. do { \
  278. mod_timer(&fd_timer, jiffies + (delay)); \
  279. } while(0)
  280. #define START_TIMEOUT() \
  281. do { \
  282. mod_timer(&timeout_timer, jiffies+FLOPPY_TIMEOUT); \
  283. } while(0)
  284. #define STOP_TIMEOUT() \
  285. do { \
  286. del_timer( &timeout_timer ); \
  287. } while(0)
  288. #define ENABLE_IRQ() enable_irq(FIQ_FD1772+64);
  289. #define DISABLE_IRQ() disable_irq(FIQ_FD1772+64);
  290. static void fd1772_checkint(void);
  291. DECLARE_WORK(fd1772_tq, (void *)fd1772_checkint, NULL);
  292. /*
  293. * The driver is trying to determine the correct media format
  294. * while Probing is set. fd_rwsec_done() clears it after a
  295. * successful access.
  296. */
  297. static int Probing = 0;
  298. /* This flag is set when a dummy seek is necessary to make the WP
  299. * status bit accessible.
  300. */
  301. static int NeedSeek = 0;
  302. /***************************** Prototypes *****************************/
  303. static void fd_select_side(int side);
  304. static void fd_select_drive(int drive);
  305. static void fd_deselect(void);
  306. static void fd_motor_off_timer(unsigned long dummy);
  307. static void check_change(unsigned long dummy);
  308. static void floppy_irqconsequencehandler(void);
  309. static void fd_error(void);
  310. static void do_fd_action(int drive);
  311. static void fd_calibrate(void);
  312. static void fd_calibrate_done(int status);
  313. static void fd_seek(void);
  314. static void fd_seek_done(int status);
  315. static void fd_rwsec(void);
  316. #ifdef TRACKBUFFER
  317. static void fd_readtrack_check( unsigned long dummy );
  318. #endif
  319. static void fd_rwsec_done(int status);
  320. static void fd_times_out(unsigned long dummy);
  321. static void finish_fdc(void);
  322. static void finish_fdc_done(int dummy);
  323. static void floppy_off(unsigned int nr);
  324. static void setup_req_params(int drive);
  325. static void redo_fd_request(void);
  326. static int fd_ioctl(struct inode *inode, struct file *filp, unsigned int
  327. cmd, unsigned long param);
  328. static void fd_probe(int drive);
  329. static int fd_test_drive_present(int drive);
  330. static void config_types(void);
  331. static int floppy_open(struct inode *inode, struct file *filp);
  332. static int floppy_release(struct inode *inode, struct file *filp);
  333. static void do_fd_request(request_queue_t *);
  334. /************************* End of Prototypes **************************/
  335. static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer, 0, 0);
  336. #ifdef TRACKBUFFER
  337. static DEFINE_TIMER(readtrack_timer, fd_readtrack_check, 0, 0);
  338. #endif
  339. static DEFINE_TIMER(timeout_timer, fd_times_out, 0, 0);
  340. static DEFINE_TIMER(fd_timer, check_change, 0, 0);
  341. /* DAG: Haven't got a clue what this is? */
  342. int stdma_islocked(void)
  343. {
  344. return 0;
  345. };
  346. /* Select the side to use. */
  347. static void fd_select_side(int side)
  348. {
  349. oldlatch_aupdate(LATCHA_SIDESEL, side ? 0 : LATCHA_SIDESEL);
  350. }
  351. /* Select a drive, update the FDC1772's track register
  352. */
  353. static void fd_select_drive(int drive)
  354. {
  355. #ifdef DEBUG
  356. printk("fd_select_drive:%d\n", drive);
  357. #endif
  358. /* Hmm - nowhere do we seem to turn the motor on - I'm going to do it here! */
  359. oldlatch_aupdate(LATCHA_MOTOR | LATCHA_INUSE, 0);
  360. if (drive == SelectedDrive)
  361. return;
  362. oldlatch_aupdate(LATCHA_FDSELALL, 0xf - (1 << drive));
  363. /* restore track register to saved value */
  364. FDC1772_WRITE(FDC1772REG_TRACK, unit[drive].track);
  365. udelay(25);
  366. SelectedDrive = drive;
  367. }
  368. /* Deselect both drives. */
  369. static void fd_deselect(void)
  370. {
  371. unsigned long flags;
  372. DPRINT(("fd_deselect\n"));
  373. oldlatch_aupdate(LATCHA_FDSELALL | LATCHA_MOTOR | LATCHA_INUSE, 0xf | LATCHA_MOTOR | LATCHA_INUSE);
  374. SelectedDrive = -1;
  375. }
  376. /* This timer function deselects the drives when the FDC1772 switched the
  377. * motor off. The deselection cannot happen earlier because the FDC1772
  378. * counts the index signals, which arrive only if one drive is selected.
  379. */
  380. static void fd_motor_off_timer(unsigned long dummy)
  381. {
  382. unsigned long flags;
  383. unsigned char status;
  384. int delay;
  385. del_timer(&motor_off_timer);
  386. if (SelectedDrive < 0)
  387. /* no drive selected, needn't deselect anyone */
  388. return;
  389. save_flags(flags);
  390. cli();
  391. if (fdc_busy) /* was stdma_islocked */
  392. goto retry;
  393. status = FDC1772_READ(FDC1772REG_STATUS);
  394. if (!(status & 0x80)) {
  395. /*
  396. * motor already turned off by FDC1772 -> deselect drives
  397. * In actual fact its this deselection which turns the motor
  398. * off on the Arc, since the motor control is actually on
  399. * Latch A
  400. */
  401. DPRINT(("fdc1772: deselecting in fd_motor_off_timer\n"));
  402. fd_deselect();
  403. MotorOn = 0;
  404. restore_flags(flags);
  405. return;
  406. }
  407. /* not yet off, try again */
  408. retry:
  409. restore_flags(flags);
  410. /* Test again later; if tested too often, it seems there is no disk
  411. * in the drive and the FDC1772 will leave the motor on forever (or,
  412. * at least until a disk is inserted). So we'll test only twice
  413. * per second from then on...
  414. */
  415. delay = (MotorOffTrys < FD_MOTOR_OFF_MAXTRY) ?
  416. (++MotorOffTrys, HZ / 20) : HZ / 2;
  417. START_MOTOR_OFF_TIMER(delay);
  418. }
  419. /* This function is repeatedly called to detect disk changes (as good
  420. * as possible) and keep track of the current state of the write protection.
  421. */
  422. static void check_change(unsigned long dummy)
  423. {
  424. static int drive = 0;
  425. unsigned long flags;
  426. int stat;
  427. if (fdc_busy)
  428. return; /* Don't start poking about if the fdc is busy */
  429. return; /* let's just forget it for the mo DAG */
  430. if (++drive > 1 || !unit[drive].connected)
  431. drive = 0;
  432. save_flags(flags);
  433. cli();
  434. if (!stdma_islocked()) {
  435. stat = !!(FDC1772_READ(FDC1772REG_STATUS) & FDC1772STAT_WPROT);
  436. /* The idea here is that if the write protect line has changed then
  437. the disc must have changed */
  438. if (stat != unit[drive].wpstat) {
  439. DPRINT(("wpstat[%d] = %d\n", drive, stat));
  440. unit[drive].wpstat = stat;
  441. set_bit(drive, &changed_floppies);
  442. }
  443. }
  444. restore_flags(flags);
  445. START_CHECK_CHANGE_TIMER(CHECK_CHANGE_DELAY);
  446. }
  447. /* Handling of the Head Settling Flag: This flag should be set after each
  448. * seek operation, because we don't use seeks with verify.
  449. */
  450. static inline void set_head_settle_flag(void)
  451. {
  452. HeadSettleFlag = FDC1772CMDADD_E;
  453. }
  454. static inline int get_head_settle_flag(void)
  455. {
  456. int tmp = HeadSettleFlag;
  457. HeadSettleFlag = 0;
  458. return (tmp);
  459. }
  460. /* General Interrupt Handling */
  461. static inline void copy_buffer(void *from, void *to)
  462. {
  463. ulong *p1 = (ulong *) from, *p2 = (ulong *) to;
  464. int cnt;
  465. for (cnt = 512 / 4; cnt; cnt--)
  466. *p2++ = *p1++;
  467. }
  468. static void (*FloppyIRQHandler) (int status) = NULL;
  469. static void floppy_irqconsequencehandler(void)
  470. {
  471. unsigned char status;
  472. void (*handler) (int);
  473. fdc1772_fdc_int_done = 0;
  474. handler = FloppyIRQHandler;
  475. FloppyIRQHandler = NULL;
  476. if (handler) {
  477. nop();
  478. status = (unsigned char) fdc1772_comendstatus;
  479. DPRINT(("FDC1772 irq, status = %02x handler = %08lx\n", (unsigned int) status, (unsigned long) handler));
  480. handler(status);
  481. } else {
  482. DPRINT(("FDC1772 irq, no handler status=%02x\n", fdc1772_comendstatus));
  483. }
  484. DPRINT(("FDC1772 irq: end of floppy_irq\n"));
  485. }
  486. /* Error handling: If some error happened, retry some times, then
  487. * recalibrate, then try again, and fail after MAX_ERRORS.
  488. */
  489. static void fd_error(void)
  490. {
  491. printk("FDC1772: fd_error\n");
  492. /*panic("fd1772: fd_error"); *//* DAG tmp */
  493. if (!CURRENT)
  494. return;
  495. CURRENT->errors++;
  496. if (CURRENT->errors >= MAX_ERRORS) {
  497. printk("fd%d: too many errors.\n", SelectedDrive);
  498. end_request(CURRENT, 0);
  499. } else if (CURRENT->errors == RECALIBRATE_ERRORS) {
  500. printk("fd%d: recalibrating\n", SelectedDrive);
  501. if (SelectedDrive != -1)
  502. unit[SelectedDrive].track = -1;
  503. }
  504. redo_fd_request();
  505. }
  506. #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
  507. /* do_fd_action() is the general procedure for a fd request: All
  508. * required parameter settings (drive select, side select, track
  509. * position) are checked and set if needed. For each of these
  510. * parameters and the actual reading or writing exist two functions:
  511. * one that starts the setting (or skips it if possible) and one
  512. * callback for the "done" interrupt. Each done func calls the next
  513. * set function to propagate the request down to fd_rwsec_done().
  514. */
  515. static void do_fd_action(int drive)
  516. {
  517. struct request *req;
  518. DPRINT(("do_fd_action unit[drive].track=%d\n", unit[drive].track));
  519. #ifdef TRACKBUFFER
  520. repeat:
  521. if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
  522. req = CURRENT;
  523. if (ReqCmd == READ) {
  524. copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
  525. if (++ReqCnt < req->current_nr_sectors) {
  526. /* read next sector */
  527. setup_req_params( drive );
  528. goto repeat;
  529. } else {
  530. /* all sectors finished */
  531. req->nr_sectors -= req->current_nr_sectors;
  532. req->sector += req->current_nr_sectors;
  533. end_request(req, 1);
  534. redo_fd_request();
  535. return;
  536. }
  537. } else {
  538. /* cmd == WRITE, pay attention to track buffer
  539. * consistency! */
  540. copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
  541. }
  542. }
  543. #endif
  544. if (SelectedDrive != drive) {
  545. /*unit[drive].track = -1; DAG */
  546. fd_select_drive(drive);
  547. };
  548. if (unit[drive].track == -1)
  549. fd_calibrate();
  550. else if (unit[drive].track != ReqTrack << unit[drive].disktype->stretch)
  551. fd_seek();
  552. else
  553. fd_rwsec();
  554. }
  555. /* Seek to track 0 if the current track is unknown */
  556. static void fd_calibrate(void)
  557. {
  558. DPRINT(("fd_calibrate\n"));
  559. if (unit[SelectedDrive].track >= 0) {
  560. fd_calibrate_done(0);
  561. return;
  562. }
  563. DPRINT(("fd_calibrate (after track compare)\n"));
  564. SET_IRQ_HANDLER(fd_calibrate_done);
  565. /* we can't verify, since the speed may be incorrect */
  566. FDC1772_WRITE(FDC1772REG_CMD, FDC1772CMD_RESTORE | unit[SelectedDrive].steprate);
  567. NeedSeek = 1;
  568. MotorOn = 1;
  569. START_TIMEOUT();
  570. /* wait for IRQ */
  571. }
  572. static void fd_calibrate_done(int status)
  573. {
  574. DPRINT(("fd_calibrate_done()\n"));
  575. STOP_TIMEOUT();
  576. /* set the correct speed now */
  577. if (status & FDC1772STAT_RECNF) {
  578. printk("fd%d: restore failed\n", SelectedDrive);
  579. fd_error();
  580. } else {
  581. unit[SelectedDrive].track = 0;
  582. fd_seek();
  583. }
  584. }
  585. /* Seek the drive to the requested track. The drive must have been
  586. * calibrated at some point before this.
  587. */
  588. static void fd_seek(void)
  589. {
  590. unsigned long flags;
  591. DPRINT(("fd_seek() to track %d (unit[SelectedDrive].track=%d)\n", ReqTrack,
  592. unit[SelectedDrive].track));
  593. if (unit[SelectedDrive].track == ReqTrack <<
  594. unit[SelectedDrive].disktype->stretch) {
  595. fd_seek_done(0);
  596. return;
  597. }
  598. FDC1772_WRITE(FDC1772REG_DATA, ReqTrack <<
  599. unit[SelectedDrive].disktype->stretch);
  600. udelay(25);
  601. save_flags(flags);
  602. clf();
  603. SET_IRQ_HANDLER(fd_seek_done);
  604. FDC1772_WRITE(FDC1772REG_CMD, FDC1772CMD_SEEK | unit[SelectedDrive].steprate |
  605. /* DAG */
  606. (MotorOn?FDC1772CMDADD_H:0));
  607. restore_flags(flags);
  608. MotorOn = 1;
  609. set_head_settle_flag();
  610. START_TIMEOUT();
  611. /* wait for IRQ */
  612. }
  613. static void fd_seek_done(int status)
  614. {
  615. DPRINT(("fd_seek_done()\n"));
  616. STOP_TIMEOUT();
  617. /* set the correct speed */
  618. if (status & FDC1772STAT_RECNF) {
  619. printk("fd%d: seek error (to track %d)\n",
  620. SelectedDrive, ReqTrack);
  621. /* we don't know exactly which track we are on now! */
  622. unit[SelectedDrive].track = -1;
  623. fd_error();
  624. } else {
  625. unit[SelectedDrive].track = ReqTrack <<
  626. unit[SelectedDrive].disktype->stretch;
  627. NeedSeek = 0;
  628. fd_rwsec();
  629. }
  630. }
  631. /* This does the actual reading/writing after positioning the head
  632. * over the correct track.
  633. */
  634. #ifdef TRACKBUFFER
  635. static int MultReadInProgress = 0;
  636. #endif
  637. static void fd_rwsec(void)
  638. {
  639. unsigned long paddr, flags;
  640. unsigned int rwflag, old_motoron;
  641. unsigned int track;
  642. DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n", ReqSector, ReqCmd == WRITE ? 'w' : 'r'));
  643. if (ReqCmd == WRITE) {
  644. /*cache_push( (unsigned long)ReqData, 512 ); */
  645. paddr = (unsigned long) ReqData;
  646. rwflag = 0x100;
  647. } else {
  648. paddr = (unsigned long) PhysDMABuffer;
  649. #ifdef TRACKBUFFER
  650. if (read_track)
  651. paddr = (unsigned long)PhysTrackBuffer;
  652. #endif
  653. rwflag = 0;
  654. }
  655. DPRINT(("fd_rwsec() before sidesel rwflag=%d sec=%d trk=%d\n", rwflag,
  656. ReqSector, FDC1772_READ(FDC1772REG_TRACK)));
  657. fd_select_side(ReqSide);
  658. /*DPRINT(("fd_rwsec() before start sector \n")); */
  659. /* Start sector of this operation */
  660. #ifdef TRACKBUFFER
  661. FDC1772_WRITE( FDC1772REG_SECTOR, !read_track ? ReqSector : 1 );
  662. #else
  663. FDC1772_WRITE( FDC1772REG_SECTOR, ReqSector );
  664. #endif
  665. /* Cheat for track if stretch != 0 */
  666. if (unit[SelectedDrive].disktype->stretch) {
  667. track = FDC1772_READ(FDC1772REG_TRACK);
  668. FDC1772_WRITE(FDC1772REG_TRACK, track >>
  669. unit[SelectedDrive].disktype->stretch);
  670. }
  671. udelay(25);
  672. DPRINT(("fd_rwsec() before setup DMA \n"));
  673. /* Setup DMA - Heavily modified by DAG */
  674. save_flags(flags);
  675. clf();
  676. disable_dma(FLOPPY_DMA);
  677. set_dma_mode(FLOPPY_DMA, rwflag ? DMA_MODE_WRITE : DMA_MODE_READ);
  678. set_dma_addr(FLOPPY_DMA, (long) paddr); /* DAG - changed from Atari specific */
  679. #ifdef TRACKBUFFER
  680. set_dma_count(FLOPPY_DMA,(!read_track ? 1 : unit[SelectedDrive].disktype->spt)*512);
  681. #else
  682. set_dma_count(FLOPPY_DMA, 512); /* Block/sector size - going to have to change */
  683. #endif
  684. SET_IRQ_HANDLER(fd_rwsec_done);
  685. /* Turn on dma int */
  686. enable_dma(FLOPPY_DMA);
  687. /* Now give it something to do */
  688. FDC1772_WRITE(FDC1772REG_CMD, (rwflag ? (FDC1772CMD_WRSEC | FDC1772CMDADD_P) :
  689. #ifdef TRACKBUFFER
  690. (FDC1772CMD_RDSEC | (read_track ? FDC1772CMDADD_M : 0) |
  691. /* Hmm - the idea here is to stop the FDC spinning the disc
  692. up when we know that we already still have it spinning */
  693. (MotorOn?FDC1772CMDADD_H:0))
  694. #else
  695. FDC1772CMD_RDSEC
  696. #endif
  697. ));
  698. restore_flags(flags);
  699. DPRINT(("fd_rwsec() after DMA setup flags=0x%08x\n", flags));
  700. /*sti(); *//* DAG - Hmm */
  701. /* Hmm - should do something DAG */
  702. old_motoron = MotorOn;
  703. MotorOn = 1;
  704. NeedSeek = 1;
  705. /* wait for interrupt */
  706. #ifdef TRACKBUFFER
  707. if (read_track) {
  708. /*
  709. * If reading a whole track, wait about one disk rotation and
  710. * then check if all sectors are read. The FDC will even
  711. * search for the first non-existant sector and need 1 sec to
  712. * recognise that it isn't present :-(
  713. */
  714. /* 1 rot. + 5 rot.s if motor was off */
  715. mod_timer(&readtrack_timer, jiffies + HZ/5 + (old_motoron ? 0 : HZ));
  716. DPRINT(("Setting readtrack_timer to %d @ %d\n",
  717. readtrack_timer.expires,jiffies));
  718. MultReadInProgress = 1;
  719. }
  720. #endif
  721. /*DPRINT(("fd_rwsec() before START_TIMEOUT \n")); */
  722. START_TIMEOUT();
  723. /*DPRINT(("fd_rwsec() after START_TIMEOUT \n")); */
  724. }
  725. #ifdef TRACKBUFFER
  726. static void fd_readtrack_check(unsigned long dummy)
  727. {
  728. unsigned long flags, addr;
  729. extern unsigned char *fdc1772_dataaddr;
  730. DPRINT(("fd_readtrack_check @ %d\n",jiffies));
  731. save_flags(flags);
  732. clf();
  733. del_timer( &readtrack_timer );
  734. if (!MultReadInProgress) {
  735. /* This prevents a race condition that could arise if the
  736. * interrupt is triggered while the calling of this timer
  737. * callback function takes place. The IRQ function then has
  738. * already cleared 'MultReadInProgress' when control flow
  739. * gets here.
  740. */
  741. restore_flags(flags);
  742. return;
  743. }
  744. /* get the current DMA address */
  745. addr=(unsigned long)fdc1772_dataaddr; /* DAG - ? */
  746. DPRINT(("fd_readtrack_check: addr=%x PhysTrackBuffer=%x\n",addr,PhysTrackBuffer));
  747. if (addr >= (unsigned int)PhysTrackBuffer + unit[SelectedDrive].disktype->spt*512) {
  748. /* already read enough data, force an FDC interrupt to stop
  749. * the read operation
  750. */
  751. SET_IRQ_HANDLER( NULL );
  752. restore_flags(flags);
  753. DPRINT(("fd_readtrack_check(): done\n"));
  754. FDC1772_WRITE( FDC1772REG_CMD, FDC1772CMD_FORCI );
  755. udelay(25);
  756. /* No error until now -- the FDC would have interrupted
  757. * otherwise!
  758. */
  759. fd_rwsec_done( 0 );
  760. } else {
  761. /* not yet finished, wait another tenth rotation */
  762. restore_flags(flags);
  763. DPRINT(("fd_readtrack_check(): not yet finished\n"));
  764. readtrack_timer.expires = jiffies + HZ/5/10;
  765. add_timer( &readtrack_timer );
  766. }
  767. }
  768. #endif
  769. static void fd_rwsec_done(int status)
  770. {
  771. unsigned int track;
  772. DPRINT(("fd_rwsec_done() status=%d @ %d\n", status,jiffies));
  773. #ifdef TRACKBUFFER
  774. if (read_track && !MultReadInProgress)
  775. return;
  776. MultReadInProgress = 0;
  777. STOP_TIMEOUT();
  778. if (read_track)
  779. del_timer( &readtrack_timer );
  780. #endif
  781. /* Correct the track if stretch != 0 */
  782. if (unit[SelectedDrive].disktype->stretch) {
  783. track = FDC1772_READ(FDC1772REG_TRACK);
  784. FDC1772_WRITE(FDC1772REG_TRACK, track <<
  785. unit[SelectedDrive].disktype->stretch);
  786. }
  787. if (ReqCmd == WRITE && (status & FDC1772STAT_WPROT)) {
  788. printk("fd%d: is write protected\n", SelectedDrive);
  789. goto err_end;
  790. }
  791. if ((status & FDC1772STAT_RECNF)
  792. #ifdef TRACKBUFFER
  793. /* RECNF is no error after a multiple read when the FDC
  794. * searched for a non-existant sector!
  795. */
  796. && !(read_track &&
  797. FDC1772_READ(FDC1772REG_SECTOR) > unit[SelectedDrive].disktype->spt)
  798. #endif
  799. ) {
  800. if (Probing) {
  801. if (unit[SelectedDrive].disktype > disk_type) {
  802. /* try another disk type */
  803. unit[SelectedDrive].disktype--;
  804. set_capacity(disks[SelectedDrive],
  805. unit[SelectedDrive].disktype->blocks);
  806. } else
  807. Probing = 0;
  808. } else {
  809. /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
  810. if (unit[SelectedDrive].autoprobe) {
  811. unit[SelectedDrive].disktype = disk_type + NUM_DISK_TYPES - 1;
  812. set_capacity(disks[SelectedDrive],
  813. unit[SelectedDrive].disktype->blocks);
  814. Probing = 1;
  815. }
  816. }
  817. if (Probing) {
  818. setup_req_params(SelectedDrive);
  819. #ifdef TRACKBUFFER
  820. BufferDrive = -1;
  821. #endif
  822. do_fd_action(SelectedDrive);
  823. return;
  824. }
  825. printk("fd%d: sector %d not found (side %d, track %d)\n",
  826. SelectedDrive, FDC1772_READ(FDC1772REG_SECTOR), ReqSide, ReqTrack);
  827. goto err_end;
  828. }
  829. if (status & FDC1772STAT_CRC) {
  830. printk("fd%d: CRC error (side %d, track %d, sector %d)\n",
  831. SelectedDrive, ReqSide, ReqTrack, FDC1772_READ(FDC1772REG_SECTOR));
  832. goto err_end;
  833. }
  834. if (status & FDC1772STAT_LOST) {
  835. printk("fd%d: lost data (side %d, track %d, sector %d)\n",
  836. SelectedDrive, ReqSide, ReqTrack, FDC1772_READ(FDC1772REG_SECTOR));
  837. goto err_end;
  838. }
  839. Probing = 0;
  840. if (ReqCmd == READ) {
  841. #ifdef TRACKBUFFER
  842. if (!read_track) {
  843. /*cache_clear (PhysDMABuffer, 512);*/
  844. copy_buffer (DMABuffer, ReqData);
  845. } else {
  846. /*cache_clear (PhysTrackBuffer, FD1772_MAX_SECTORS * 512);*/
  847. BufferDrive = SelectedDrive;
  848. BufferSide = ReqSide;
  849. BufferTrack = ReqTrack;
  850. copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
  851. }
  852. #else
  853. /*cache_clear( PhysDMABuffer, 512 ); */
  854. copy_buffer(DMABuffer, ReqData);
  855. #endif
  856. }
  857. if (++ReqCnt < CURRENT->current_nr_sectors) {
  858. /* read next sector */
  859. setup_req_params(SelectedDrive);
  860. do_fd_action(SelectedDrive);
  861. } else {
  862. /* all sectors finished */
  863. CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
  864. CURRENT->sector += CURRENT->current_nr_sectors;
  865. end_request(CURRENT, 1);
  866. redo_fd_request();
  867. }
  868. return;
  869. err_end:
  870. #ifdef TRACKBUFFER
  871. BufferDrive = -1;
  872. #endif
  873. fd_error();
  874. }
  875. static void fd_times_out(unsigned long dummy)
  876. {
  877. SET_IRQ_HANDLER(NULL);
  878. /* If the timeout occurred while the readtrack_check timer was
  879. * active, we need to cancel it, else bad things will happen */
  880. del_timer( &readtrack_timer );
  881. FDC1772_WRITE(FDC1772REG_CMD, FDC1772CMD_FORCI);
  882. udelay(25);
  883. printk("floppy timeout\n");
  884. STOP_TIMEOUT(); /* hmm - should we do this ? */
  885. fd_error();
  886. }
  887. /* The (noop) seek operation here is needed to make the WP bit in the
  888. * FDC1772 status register accessible for check_change. If the last disk
  889. * operation would have been a RDSEC, this bit would always read as 0
  890. * no matter what :-( To save time, the seek goes to the track we're
  891. * already on.
  892. */
  893. static void finish_fdc(void)
  894. {
  895. /* DAG - just try without this dummy seek! */
  896. finish_fdc_done(0);
  897. return;
  898. if (!NeedSeek) {
  899. finish_fdc_done(0);
  900. } else {
  901. DPRINT(("finish_fdc: dummy seek started\n"));
  902. FDC1772_WRITE(FDC1772REG_DATA, unit[SelectedDrive].track);
  903. SET_IRQ_HANDLER(finish_fdc_done);
  904. FDC1772_WRITE(FDC1772REG_CMD, FDC1772CMD_SEEK);
  905. MotorOn = 1;
  906. START_TIMEOUT();
  907. /* we must wait for the IRQ here, because the ST-DMA is
  908. * released immediately afterwards and the interrupt may be
  909. * delivered to the wrong driver.
  910. */
  911. }
  912. }
  913. static void finish_fdc_done(int dummy)
  914. {
  915. unsigned long flags;
  916. DPRINT(("finish_fdc_done entered\n"));
  917. STOP_TIMEOUT();
  918. NeedSeek = 0;
  919. if (timer_pending(&fd_timer) &&
  920. time_after(jiffies + 5, fd_timer.expires))
  921. /* If the check for a disk change is done too early after this
  922. * last seek command, the WP bit still reads wrong :-((
  923. */
  924. mod_timer(&fd_timer, jiffies + 5);
  925. else {
  926. /* START_CHECK_CHANGE_TIMER( CHECK_CHANGE_DELAY ); */
  927. };
  928. del_timer(&motor_off_timer);
  929. START_MOTOR_OFF_TIMER(FD_MOTOR_OFF_DELAY);
  930. save_flags(flags);
  931. cli();
  932. /* stdma_release(); - not sure if I should do something DAG */
  933. fdc_busy = 0;
  934. wake_up(&fdc_wait);
  935. restore_flags(flags);
  936. DPRINT(("finish_fdc() finished\n"));
  937. }
  938. /* Prevent "aliased" accesses. */
  939. static int fd_ref[4];
  940. static int fd_device[4];
  941. /* dummy for blk.h */
  942. static void floppy_off(unsigned int nr)
  943. {
  944. }
  945. /* On the old arcs write protect depends on the particular model
  946. of machine. On the A310, R140, and A440 there is a disc changed
  947. detect, however on the A4x0/1 range there is not. There
  948. is nothing to tell you which machine your on.
  949. At the moment I'm just marking changed always. I've
  950. left the Atari's 'change on write protect change' code in this
  951. part (but nothing sets it).
  952. RiscOS apparently checks the disc serial number etc. to detect changes
  953. - but if it sees a disc change line go high (?) it flips to using
  954. it. Well maybe I'll add that in the future (!?)
  955. */
  956. static int check_floppy_change(struct gendisk *disk)
  957. {
  958. struct archy_floppy_struct *p = disk->private_data;
  959. unsigned int drive = p - unit;
  960. if (test_bit(drive, &fake_change)) {
  961. /* simulated change (e.g. after formatting) */
  962. return 1;
  963. }
  964. if (test_bit(drive, &changed_floppies)) {
  965. /* surely changed (the WP signal changed at least once) */
  966. return 1;
  967. }
  968. if (p->wpstat) {
  969. /* WP is on -> could be changed: to be sure, buffers should be
  970. * invalidated...
  971. */
  972. return 1;
  973. }
  974. return 1; /* DAG - was 0 */
  975. }
  976. static int floppy_revalidate(struct gendisk *disk)
  977. {
  978. struct archy_floppy_struct *p = disk->private_data;
  979. unsigned int drive = p - unit;
  980. if (test_bit(drive, &changed_floppies) || test_bit(drive, &fake_change)
  981. || unit[drive].disktype == 0) {
  982. #ifdef TRACKBUFFER
  983. BufferDrive = -1;
  984. #endif
  985. clear_bit(drive, &fake_change);
  986. clear_bit(drive, &changed_floppies);
  987. p->disktype = 0;
  988. }
  989. return 0;
  990. }
  991. /* This sets up the global variables describing the current request. */
  992. static void setup_req_params(int drive)
  993. {
  994. int block = ReqBlock + ReqCnt;
  995. ReqTrack = block / unit[drive].disktype->spt;
  996. ReqSector = block - ReqTrack * unit[drive].disktype->spt + 1;
  997. ReqSide = ReqTrack & 1;
  998. ReqTrack >>= 1;
  999. ReqData = ReqBuffer + 512 * ReqCnt;
  1000. #ifdef TRACKBUFFER
  1001. read_track = (ReqCmd == READ && CURRENT->errors == 0);
  1002. #endif
  1003. DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n", ReqSide,
  1004. ReqTrack, ReqSector, (unsigned long) ReqData));
  1005. }
  1006. static void redo_fd_request(void)
  1007. {
  1008. int drive, type;
  1009. struct archy_floppy_struct *floppy;
  1010. DPRINT(("redo_fd_request: CURRENT=%p dev=%s CURRENT->sector=%ld\n",
  1011. CURRENT, CURRENT ? CURRENT->rq_disk->disk_name : "",
  1012. CURRENT ? CURRENT->sector : 0));
  1013. repeat:
  1014. if (!CURRENT)
  1015. goto the_end;
  1016. floppy = CURRENT->rq_disk->private_data;
  1017. drive = floppy - unit;
  1018. type = fd_device[drive];
  1019. if (!floppy->connected) {
  1020. /* drive not connected */
  1021. printk("Unknown Device: fd%d\n", drive);
  1022. end_request(CURRENT, 0);
  1023. goto repeat;
  1024. }
  1025. if (type == 0) {
  1026. if (!floppy->disktype) {
  1027. Probing = 1;
  1028. floppy->disktype = disk_type + NUM_DISK_TYPES - 1;
  1029. set_capacity(disks[drive], floppy->disktype->blocks);
  1030. floppy->autoprobe = 1;
  1031. }
  1032. } else {
  1033. /* user supplied disk type */
  1034. --type;
  1035. if (type >= NUM_DISK_TYPES) {
  1036. printk("fd%d: invalid disk format", drive);
  1037. end_request(CURRENT, 0);
  1038. goto repeat;
  1039. }
  1040. floppy->disktype = &disk_type[type];
  1041. set_capacity(disks[drive], floppy->disktype->blocks);
  1042. floppy->autoprobe = 0;
  1043. }
  1044. if (CURRENT->sector + 1 > floppy->disktype->blocks) {
  1045. end_request(CURRENT, 0);
  1046. goto repeat;
  1047. }
  1048. /* stop deselect timer */
  1049. del_timer(&motor_off_timer);
  1050. ReqCnt = 0;
  1051. ReqCmd = CURRENT->cmd;
  1052. ReqBlock = CURRENT->sector;
  1053. ReqBuffer = CURRENT->buffer;
  1054. setup_req_params(drive);
  1055. do_fd_action(drive);
  1056. return;
  1057. the_end:
  1058. finish_fdc();
  1059. }
  1060. static void fd1772_checkint(void)
  1061. {
  1062. extern int fdc1772_bytestogo;
  1063. /*printk("fd1772_checkint %d\n",fdc1772_fdc_int_done);*/
  1064. if (fdc1772_fdc_int_done)
  1065. floppy_irqconsequencehandler();
  1066. if ((MultReadInProgress) && (fdc1772_bytestogo==0)) fd_readtrack_check(0);
  1067. if (fdc_busy) {
  1068. schedule_work(&fd1772_tq);
  1069. }
  1070. }
  1071. static void do_fd_request(request_queue_t* q)
  1072. {
  1073. unsigned long flags;
  1074. DPRINT(("do_fd_request for pid %d\n", current->pid));
  1075. if (fdc_busy) return;
  1076. save_flags(flags);
  1077. cli();
  1078. wait_event(fdc_wait, !fdc_busy);
  1079. fdc_busy = 1;
  1080. ENABLE_IRQ();
  1081. restore_flags(flags);
  1082. fdc1772_fdc_int_done = 0;
  1083. redo_fd_request();
  1084. schedule_work(&fd1772_tq);
  1085. }
  1086. static int invalidate_drive(struct block_device *bdev)
  1087. {
  1088. struct archy_floppy_struct *p = bdev->bd_disk->private_data;
  1089. /* invalidate the buffer track to force a reread */
  1090. #ifdef TRACKBUFFER
  1091. BufferDrive = -1;
  1092. #endif
  1093. set_bit(p - unit, &fake_change);
  1094. return 0;
  1095. }
  1096. static int fd_ioctl(struct inode *inode, struct file *filp,
  1097. unsigned int cmd, unsigned long param)
  1098. {
  1099. struct block_device *bdev = inode->i_bdev;
  1100. switch (cmd) {
  1101. case FDFMTEND:
  1102. case FDFLUSH:
  1103. invalidate_drive(bdev);
  1104. check_disk_change(bdev);
  1105. case FDFMTBEG:
  1106. return 0;
  1107. default:
  1108. return -EINVAL;
  1109. }
  1110. }
  1111. /* Initialize the 'unit' variable for drive 'drive' */
  1112. static void fd_probe(int drive)
  1113. {
  1114. unit[drive].connected = 0;
  1115. unit[drive].disktype = NULL;
  1116. if (!fd_test_drive_present(drive))
  1117. return;
  1118. unit[drive].connected = 1;
  1119. unit[drive].track = -1; /* If we put the auto detect back in this can go to 0 */
  1120. unit[drive].steprate = FDC1772STEP_6;
  1121. MotorOn = 1; /* from probe restore operation! */
  1122. }
  1123. /* This function tests the physical presence of a floppy drive (not
  1124. * whether a disk is inserted). This is done by issuing a restore
  1125. * command, waiting max. 2 seconds (that should be enough to move the
  1126. * head across the whole disk) and looking at the state of the "TR00"
  1127. * signal. This should now be raised if there is a drive connected
  1128. * (and there is no hardware failure :-) Otherwise, the drive is
  1129. * declared absent.
  1130. */
  1131. static int fd_test_drive_present(int drive)
  1132. {
  1133. unsigned long timeout;
  1134. unsigned char status;
  1135. int ok;
  1136. printk("fd_test_drive_present %d\n", drive);
  1137. if (drive > 1)
  1138. return (0);
  1139. return (1); /* Simple hack for the moment - the autodetect doesn't seem to work on arc */
  1140. fd_select_drive(drive);
  1141. /* disable interrupt temporarily */
  1142. DISABLE_IRQ();
  1143. FDC1772_WRITE(FDC1772REG_TRACK, 0x00); /* was ff00 why? */
  1144. FDC1772_WRITE(FDC1772REG_CMD, FDC1772CMD_RESTORE | FDC1772CMDADD_H | FDC1772STEP_6);
  1145. /*printk("fd_test_drive_present: Going into timeout loop\n"); */
  1146. for (ok = 0, timeout = jiffies + 2 * HZ + HZ / 2; time_before(jiffies, timeout);) {
  1147. /* What does this piece of atariism do? - query for an interrupt? */
  1148. /* if (!(mfp.par_dt_reg & 0x20))
  1149. break; */
  1150. /* Well this is my nearest guess - quit when we get an FDC interrupt */
  1151. if (ioc_readb(IOC_FIQSTAT) & 2)
  1152. break;
  1153. }
  1154. /*printk("fd_test_drive_present: Coming out of timeout loop\n"); */
  1155. status = FDC1772_READ(FDC1772REG_STATUS);
  1156. ok = (status & FDC1772STAT_TR00) != 0;
  1157. /*printk("fd_test_drive_present: ok=%d\n",ok); */
  1158. /* force interrupt to abort restore operation (FDC1772 would try
  1159. * about 50 seconds!) */
  1160. FDC1772_WRITE(FDC1772REG_CMD, FDC1772CMD_FORCI);
  1161. udelay(500);
  1162. status = FDC1772_READ(FDC1772REG_STATUS);
  1163. udelay(20);
  1164. /*printk("fd_test_drive_present: just before OK code %d\n",ok); */
  1165. if (ok) {
  1166. /* dummy seek command to make WP bit accessible */
  1167. FDC1772_WRITE(FDC1772REG_DATA, 0);
  1168. FDC1772_WRITE(FDC1772REG_CMD, FDC1772CMD_SEEK);
  1169. printk("fd_test_drive_present: just before wait for int\n");
  1170. /* DAG: Guess means wait for interrupt */
  1171. while (!(ioc_readb(IOC_FIQSTAT) & 2));
  1172. printk("fd_test_drive_present: just after wait for int\n");
  1173. status = FDC1772_READ(FDC1772REG_STATUS);
  1174. }
  1175. printk("fd_test_drive_present: just before ENABLE_IRQ\n");
  1176. ENABLE_IRQ();
  1177. printk("fd_test_drive_present: about to return\n");
  1178. return (ok);
  1179. }
  1180. /* Look how many and which kind of drives are connected. If there are
  1181. * floppies, additionally start the disk-change and motor-off timers.
  1182. */
  1183. static void config_types(void)
  1184. {
  1185. int drive, cnt = 0;
  1186. printk("Probing floppy drive(s):\n");
  1187. for (drive = 0; drive < FD_MAX_UNITS; drive++) {
  1188. fd_probe(drive);
  1189. if (unit[drive].connected) {
  1190. printk("fd%d\n", drive);
  1191. ++cnt;
  1192. }
  1193. }
  1194. if (FDC1772_READ(FDC1772REG_STATUS) & FDC1772STAT_BUSY) {
  1195. /* If FDC1772 is still busy from probing, give it another FORCI
  1196. * command to abort the operation. If this isn't done, the FDC1772
  1197. * will interrupt later and its IRQ line stays low, because
  1198. * the status register isn't read. And this will block any
  1199. * interrupts on this IRQ line :-(
  1200. */
  1201. FDC1772_WRITE(FDC1772REG_CMD, FDC1772CMD_FORCI);
  1202. udelay(500);
  1203. FDC1772_READ(FDC1772REG_STATUS);
  1204. udelay(20);
  1205. }
  1206. if (cnt > 0) {
  1207. START_MOTOR_OFF_TIMER(FD_MOTOR_OFF_DELAY);
  1208. if (cnt == 1)
  1209. fd_select_drive(0);
  1210. /*START_CHECK_CHANGE_TIMER( CHECK_CHANGE_DELAY ); */
  1211. }
  1212. }
  1213. /*
  1214. * floppy_open check for aliasing (/dev/fd0 can be the same as
  1215. * /dev/PS0 etc), and disallows simultaneous access to the same
  1216. * drive with different device numbers.
  1217. */
  1218. static int floppy_open(struct inode *inode, struct file *filp)
  1219. {
  1220. int drive = iminor(inode) & 3;
  1221. int type = iminor(inode) >> 2;
  1222. int old_dev = fd_device[drive];
  1223. if (fd_ref[drive] && old_dev != type)
  1224. return -EBUSY;
  1225. if (fd_ref[drive] == -1 || (fd_ref[drive] && filp->f_flags & O_EXCL))
  1226. return -EBUSY;
  1227. if (filp->f_flags & O_EXCL)
  1228. fd_ref[drive] = -1;
  1229. else
  1230. fd_ref[drive]++;
  1231. fd_device[drive] = type;
  1232. if (filp->f_flags & O_NDELAY)
  1233. return 0;
  1234. if (filp->f_mode & 3) {
  1235. check_disk_change(inode->i_bdev);
  1236. if (filp->f_mode & 2) {
  1237. if (unit[drive].wpstat) {
  1238. floppy_release(inode, filp);
  1239. return -EROFS;
  1240. }
  1241. }
  1242. }
  1243. return 0;
  1244. }
  1245. static int floppy_release(struct inode *inode, struct file *filp)
  1246. {
  1247. int drive = iminor(inode) & 3;
  1248. if (fd_ref[drive] < 0)
  1249. fd_ref[drive] = 0;
  1250. else if (!fd_ref[drive]--) {
  1251. printk("floppy_release with fd_ref == 0");
  1252. fd_ref[drive] = 0;
  1253. }
  1254. return 0;
  1255. }
  1256. static struct block_device_operations floppy_fops =
  1257. {
  1258. .open = floppy_open,
  1259. .release = floppy_release,
  1260. .ioctl = fd_ioctl,
  1261. .media_changed = check_floppy_change,
  1262. .revalidate_disk= floppy_revalidate,
  1263. };
  1264. static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  1265. {
  1266. int drive = *part & 3;
  1267. if ((*part >> 2) > NUM_DISK_TYPES || drive >= FD_MAX_UNITS)
  1268. return NULL;
  1269. *part = 0;
  1270. return get_disk(disks[drive]);
  1271. }
  1272. int fd1772_init(void)
  1273. {
  1274. static DEFINE_SPINLOCK(lock);
  1275. int i, err = -ENOMEM;
  1276. if (!machine_is_archimedes())
  1277. return 0;
  1278. for (i = 0; i < FD_MAX_UNITS; i++) {
  1279. disks[i] = alloc_disk(1);
  1280. if (!disks[i])
  1281. goto err_disk;
  1282. }
  1283. err = register_blkdev(MAJOR_NR, "fd");
  1284. if (err)
  1285. goto err_disk;
  1286. err = -EBUSY;
  1287. if (request_dma(FLOPPY_DMA, "fd1772")) {
  1288. printk("Unable to grab DMA%d for the floppy (1772) driver\n", FLOPPY_DMA);
  1289. goto err_blkdev;
  1290. };
  1291. if (request_dma(FIQ_FD1772, "fd1772 end")) {
  1292. printk("Unable to grab DMA%d for the floppy (1772) driver\n", FIQ_FD1772);
  1293. goto err_dma1;
  1294. };
  1295. /* initialize variables */
  1296. SelectedDrive = -1;
  1297. #ifdef TRACKBUFFER
  1298. BufferDrive = BufferSide = BufferTrack = -1;
  1299. /* Atari uses 512 - I want to eventually cope with 1K sectors */
  1300. DMABuffer = (char *)kmalloc((FD1772_MAX_SECTORS+1)*512,GFP_KERNEL);
  1301. TrackBuffer = DMABuffer + 512;
  1302. #else
  1303. /* Allocate memory for the DMAbuffer - on the Atari this takes it
  1304. out of some special memory... */
  1305. DMABuffer = (char *) kmalloc(2048); /* Copes with pretty large sectors */
  1306. #endif
  1307. err = -ENOMEM;
  1308. if (!DMAbuffer)
  1309. goto err_dma2;
  1310. enable_dma(FIQ_FD1772); /* This inserts a call to our command end routine */
  1311. floppy_queue = blk_init_queue(do_fd_request, &lock);
  1312. if (!floppy_queue)
  1313. goto err_queue;
  1314. for (i = 0; i < FD_MAX_UNITS; i++) {
  1315. unit[i].track = -1;
  1316. disks[i]->major = MAJOR_NR;
  1317. disks[i]->first_minor = 0;
  1318. disks[i]->fops = &floppy_fops;
  1319. sprintf(disks[i]->disk_name, "fd%d", i);
  1320. disks[i]->private_data = &unit[i];
  1321. disks[i]->queue = floppy_queue;
  1322. set_capacity(disks[i], MAX_DISK_SIZE * 2);
  1323. }
  1324. blk_register_region(MKDEV(MAJOR_NR, 0), 256, THIS_MODULE,
  1325. floppy_find, NULL, NULL);
  1326. for (i = 0; i < FD_MAX_UNITS; i++)
  1327. add_disk(disks[i]);
  1328. config_types();
  1329. return 0;
  1330. err_queue:
  1331. kfree(DMAbuffer);
  1332. err_dma2:
  1333. free_dma(FIQ_FD1772);
  1334. err_dma1:
  1335. free_dma(FLOPPY_DMA);
  1336. err_blkdev:
  1337. unregister_blkdev(MAJOR_NR, "fd");
  1338. err_disk:
  1339. while (i--)
  1340. put_disk(disks[i]);
  1341. return err;
  1342. }