ataflop.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. /*
  2. * drivers/block/ataflop.c
  3. *
  4. * Copyright (C) 1993 Greg Harp
  5. * Atari Support by Bjoern Brauel, Roman Hodek
  6. *
  7. * Big cleanup Sep 11..14 1994 Roman Hodek:
  8. * - Driver now works interrupt driven
  9. * - Support for two drives; should work, but I cannot test that :-(
  10. * - Reading is done in whole tracks and buffered to speed up things
  11. * - Disk change detection and drive deselecting after motor-off
  12. * similar to TOS
  13. * - Autodetection of disk format (DD/HD); untested yet, because I
  14. * don't have an HD drive :-(
  15. *
  16. * Fixes Nov 13 1994 Martin Schaller:
  17. * - Autodetection works now
  18. * - Support for 5 1/4'' disks
  19. * - Removed drive type (unknown on atari)
  20. * - Do seeks with 8 Mhz
  21. *
  22. * Changes by Andreas Schwab:
  23. * - After errors in multiple read mode try again reading single sectors
  24. * (Feb 1995):
  25. * - Clean up error handling
  26. * - Set blk_size for proper size checking
  27. * - Initialize track register when testing presence of floppy
  28. * - Implement some ioctl's
  29. *
  30. * Changes by Torsten Lang:
  31. * - When probing the floppies we should add the FDCCMDADD_H flag since
  32. * the FDC will otherwise wait forever when no disk is inserted...
  33. *
  34. * ++ Freddi Aschwanden (fa) 20.9.95 fixes for medusa:
  35. * - MFPDELAY() after each FDC access -> atari
  36. * - more/other disk formats
  37. * - DMA to the block buffer directly if we have a 32bit DMA
  38. * - for medusa, the step rate is always 3ms
  39. * - on medusa, use only cache_push()
  40. * Roman:
  41. * - Make disk format numbering independent from minors
  42. * - Let user set max. supported drive type (speeds up format
  43. * detection, saves buffer space)
  44. *
  45. * Roman 10/15/95:
  46. * - implement some more ioctls
  47. * - disk formatting
  48. *
  49. * Andreas 95/12/12:
  50. * - increase gap size at start of track for HD/ED disks
  51. *
  52. * Michael (MSch) 11/07/96:
  53. * - implemented FDSETPRM and FDDEFPRM ioctl
  54. *
  55. * Andreas (97/03/19):
  56. * - implemented missing BLK* ioctls
  57. *
  58. * Things left to do:
  59. * - Formatting
  60. * - Maybe a better strategy for disk change detection (does anyone
  61. * know one?)
  62. */
  63. #include <linux/module.h>
  64. #include <linux/fd.h>
  65. #include <linux/delay.h>
  66. #include <linux/init.h>
  67. #include <linux/blkdev.h>
  68. #include <linux/mutex.h>
  69. #include <linux/completion.h>
  70. #include <linux/wait.h>
  71. #include <asm/atafd.h>
  72. #include <asm/atafdreg.h>
  73. #include <asm/atariints.h>
  74. #include <asm/atari_stdma.h>
  75. #include <asm/atari_stram.h>
  76. #define FD_MAX_UNITS 2
  77. #undef DEBUG
  78. static DEFINE_MUTEX(ataflop_mutex);
  79. static struct request *fd_request;
  80. static int fdc_queue;
  81. /* Disk types: DD, HD, ED */
  82. static struct atari_disk_type {
  83. const char *name;
  84. unsigned spt; /* sectors per track */
  85. unsigned blocks; /* total number of blocks */
  86. unsigned fdc_speed; /* fdc_speed setting */
  87. unsigned stretch; /* track doubling ? */
  88. } atari_disk_type[] = {
  89. { "d360", 9, 720, 0, 0}, /* 0: 360kB diskette */
  90. { "D360", 9, 720, 0, 1}, /* 1: 360kb in 720k or 1.2MB drive */
  91. { "D720", 9,1440, 0, 0}, /* 2: 720kb in 720k or 1.2MB drive */
  92. { "D820", 10,1640, 0, 0}, /* 3: DD disk with 82 tracks/10 sectors */
  93. /* formats above are probed for type DD */
  94. #define MAX_TYPE_DD 3
  95. { "h1200",15,2400, 3, 0}, /* 4: 1.2MB diskette */
  96. { "H1440",18,2880, 3, 0}, /* 5: 1.4 MB diskette (HD) */
  97. { "H1640",20,3280, 3, 0}, /* 6: 1.64MB diskette (fat HD) 82 tr 20 sec */
  98. /* formats above are probed for types DD and HD */
  99. #define MAX_TYPE_HD 6
  100. { "E2880",36,5760, 3, 0}, /* 7: 2.8 MB diskette (ED) */
  101. { "E3280",40,6560, 3, 0}, /* 8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
  102. /* formats above are probed for types DD, HD and ED */
  103. #define MAX_TYPE_ED 8
  104. /* types below are never autoprobed */
  105. { "H1680",21,3360, 3, 0}, /* 9: 1.68MB diskette (fat HD) 80 tr 21 sec */
  106. { "h410",10,820, 0, 1}, /* 10: 410k diskette 41 tr 10 sec, stretch */
  107. { "h1476",18,2952, 3, 0}, /* 11: 1.48MB diskette 82 tr 18 sec */
  108. { "H1722",21,3444, 3, 0}, /* 12: 1.72MB diskette 82 tr 21 sec */
  109. { "h420",10,840, 0, 1}, /* 13: 420k diskette 42 tr 10 sec, stretch */
  110. { "H830",10,1660, 0, 0}, /* 14: 820k diskette 83 tr 10 sec */
  111. { "h1494",18,2952, 3, 0}, /* 15: 1.49MB diskette 83 tr 18 sec */
  112. { "H1743",21,3486, 3, 0}, /* 16: 1.74MB diskette 83 tr 21 sec */
  113. { "h880",11,1760, 0, 0}, /* 17: 880k diskette 80 tr 11 sec */
  114. { "D1040",13,2080, 0, 0}, /* 18: 1.04MB diskette 80 tr 13 sec */
  115. { "D1120",14,2240, 0, 0}, /* 19: 1.12MB diskette 80 tr 14 sec */
  116. { "h1600",20,3200, 3, 0}, /* 20: 1.60MB diskette 80 tr 20 sec */
  117. { "H1760",22,3520, 3, 0}, /* 21: 1.76MB diskette 80 tr 22 sec */
  118. { "H1920",24,3840, 3, 0}, /* 22: 1.92MB diskette 80 tr 24 sec */
  119. { "E3200",40,6400, 3, 0}, /* 23: 3.2MB diskette 80 tr 40 sec */
  120. { "E3520",44,7040, 3, 0}, /* 24: 3.52MB diskette 80 tr 44 sec */
  121. { "E3840",48,7680, 3, 0}, /* 25: 3.84MB diskette 80 tr 48 sec */
  122. { "H1840",23,3680, 3, 0}, /* 26: 1.84MB diskette 80 tr 23 sec */
  123. { "D800",10,1600, 0, 0}, /* 27: 800k diskette 80 tr 10 sec */
  124. };
  125. static int StartDiskType[] = {
  126. MAX_TYPE_DD,
  127. MAX_TYPE_HD,
  128. MAX_TYPE_ED
  129. };
  130. #define TYPE_DD 0
  131. #define TYPE_HD 1
  132. #define TYPE_ED 2
  133. static int DriveType = TYPE_HD;
  134. static DEFINE_SPINLOCK(ataflop_lock);
  135. /* Array for translating minors into disk formats */
  136. static struct {
  137. int index;
  138. unsigned drive_types;
  139. } minor2disktype[] = {
  140. { 0, TYPE_DD }, /* 1: d360 */
  141. { 4, TYPE_HD }, /* 2: h1200 */
  142. { 1, TYPE_DD }, /* 3: D360 */
  143. { 2, TYPE_DD }, /* 4: D720 */
  144. { 1, TYPE_DD }, /* 5: h360 = D360 */
  145. { 2, TYPE_DD }, /* 6: h720 = D720 */
  146. { 5, TYPE_HD }, /* 7: H1440 */
  147. { 7, TYPE_ED }, /* 8: E2880 */
  148. /* some PC formats :-) */
  149. { 8, TYPE_ED }, /* 9: E3280 <- was "CompaQ" == E2880 for PC */
  150. { 5, TYPE_HD }, /* 10: h1440 = H1440 */
  151. { 9, TYPE_HD }, /* 11: H1680 */
  152. { 10, TYPE_DD }, /* 12: h410 */
  153. { 3, TYPE_DD }, /* 13: H820 <- == D820, 82x10 */
  154. { 11, TYPE_HD }, /* 14: h1476 */
  155. { 12, TYPE_HD }, /* 15: H1722 */
  156. { 13, TYPE_DD }, /* 16: h420 */
  157. { 14, TYPE_DD }, /* 17: H830 */
  158. { 15, TYPE_HD }, /* 18: h1494 */
  159. { 16, TYPE_HD }, /* 19: H1743 */
  160. { 17, TYPE_DD }, /* 20: h880 */
  161. { 18, TYPE_DD }, /* 21: D1040 */
  162. { 19, TYPE_DD }, /* 22: D1120 */
  163. { 20, TYPE_HD }, /* 23: h1600 */
  164. { 21, TYPE_HD }, /* 24: H1760 */
  165. { 22, TYPE_HD }, /* 25: H1920 */
  166. { 23, TYPE_ED }, /* 26: E3200 */
  167. { 24, TYPE_ED }, /* 27: E3520 */
  168. { 25, TYPE_ED }, /* 28: E3840 */
  169. { 26, TYPE_HD }, /* 29: H1840 */
  170. { 27, TYPE_DD }, /* 30: D800 */
  171. { 6, TYPE_HD }, /* 31: H1640 <- was H1600 == h1600 for PC */
  172. };
  173. #define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
  174. /*
  175. * Maximum disk size (in kilobytes). This default is used whenever the
  176. * current disk size is unknown.
  177. */
  178. #define MAX_DISK_SIZE 3280
  179. /*
  180. * MSch: User-provided type information. 'drive' points to
  181. * the respective entry of this array. Set by FDSETPRM ioctls.
  182. */
  183. static struct atari_disk_type user_params[FD_MAX_UNITS];
  184. /*
  185. * User-provided permanent type information. 'drive' points to
  186. * the respective entry of this array. Set by FDDEFPRM ioctls,
  187. * restored upon disk change by floppy_revalidate() if valid (as seen by
  188. * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
  189. */
  190. static struct atari_disk_type default_params[FD_MAX_UNITS];
  191. /* current info on each unit */
  192. static struct atari_floppy_struct {
  193. int connected; /* !=0 : drive is connected */
  194. int autoprobe; /* !=0 : do autoprobe */
  195. struct atari_disk_type *disktype; /* current type of disk */
  196. int track; /* current head position or -1 if
  197. unknown */
  198. unsigned int steprate; /* steprate setting */
  199. unsigned int wpstat; /* current state of WP signal (for
  200. disk change detection) */
  201. int flags; /* flags */
  202. struct gendisk *disk;
  203. int ref;
  204. int type;
  205. } unit[FD_MAX_UNITS];
  206. #define UD unit[drive]
  207. #define UDT unit[drive].disktype
  208. #define SUD unit[SelectedDrive]
  209. #define SUDT unit[SelectedDrive].disktype
  210. #define FDC_READ(reg) ({ \
  211. /* unsigned long __flags; */ \
  212. unsigned short __val; \
  213. /* local_irq_save(__flags); */ \
  214. dma_wd.dma_mode_status = 0x80 | (reg); \
  215. udelay(25); \
  216. __val = dma_wd.fdc_acces_seccount; \
  217. MFPDELAY(); \
  218. /* local_irq_restore(__flags); */ \
  219. __val & 0xff; \
  220. })
  221. #define FDC_WRITE(reg,val) \
  222. do { \
  223. /* unsigned long __flags; */ \
  224. /* local_irq_save(__flags); */ \
  225. dma_wd.dma_mode_status = 0x80 | (reg); \
  226. udelay(25); \
  227. dma_wd.fdc_acces_seccount = (val); \
  228. MFPDELAY(); \
  229. /* local_irq_restore(__flags); */ \
  230. } while(0)
  231. /* Buffering variables:
  232. * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
  233. * operations. Second, a track buffer is used to cache a whole track
  234. * of the disk to save read operations. These are two separate buffers
  235. * because that allows write operations without clearing the track buffer.
  236. */
  237. static int MaxSectors[] = {
  238. 11, 22, 44
  239. };
  240. static int BufferSize[] = {
  241. 15*512, 30*512, 60*512
  242. };
  243. #define BUFFER_SIZE (BufferSize[DriveType])
  244. unsigned char *DMABuffer; /* buffer for writes */
  245. static unsigned long PhysDMABuffer; /* physical address */
  246. static int UseTrackbuffer = -1; /* Do track buffering? */
  247. module_param(UseTrackbuffer, int, 0);
  248. unsigned char *TrackBuffer; /* buffer for reads */
  249. static unsigned long PhysTrackBuffer; /* physical address */
  250. static int BufferDrive, BufferSide, BufferTrack;
  251. static int read_track; /* non-zero if we are reading whole tracks */
  252. #define SECTOR_BUFFER(sec) (TrackBuffer + ((sec)-1)*512)
  253. #define IS_BUFFERED(drive,side,track) \
  254. (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
  255. /*
  256. * These are global variables, as that's the easiest way to give
  257. * information to interrupts. They are the data used for the current
  258. * request.
  259. */
  260. static int SelectedDrive = 0;
  261. static int ReqCmd, ReqBlock;
  262. static int ReqSide, ReqTrack, ReqSector, ReqCnt;
  263. static int HeadSettleFlag = 0;
  264. static unsigned char *ReqData, *ReqBuffer;
  265. static int MotorOn = 0, MotorOffTrys;
  266. static int IsFormatting = 0, FormatError;
  267. static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
  268. module_param_array(UserSteprate, int, NULL, 0);
  269. /* Synchronization of FDC access. */
  270. static volatile int fdc_busy = 0;
  271. static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
  272. static DECLARE_COMPLETION(format_wait);
  273. static unsigned long changed_floppies = 0xff, fake_change = 0;
  274. #define CHECK_CHANGE_DELAY HZ/2
  275. #define FD_MOTOR_OFF_DELAY (3*HZ)
  276. #define FD_MOTOR_OFF_MAXTRY (10*20)
  277. #define FLOPPY_TIMEOUT (6*HZ)
  278. #define RECALIBRATE_ERRORS 4 /* After this many errors the drive
  279. * will be recalibrated. */
  280. #define MAX_ERRORS 8 /* After this many errors the driver
  281. * will give up. */
  282. /*
  283. * The driver is trying to determine the correct media format
  284. * while Probing is set. fd_rwsec_done() clears it after a
  285. * successful access.
  286. */
  287. static int Probing = 0;
  288. /* This flag is set when a dummy seek is necessary to make the WP
  289. * status bit accessible.
  290. */
  291. static int NeedSeek = 0;
  292. #ifdef DEBUG
  293. #define DPRINT(a) printk a
  294. #else
  295. #define DPRINT(a)
  296. #endif
  297. /***************************** Prototypes *****************************/
  298. static void fd_select_side( int side );
  299. static void fd_select_drive( int drive );
  300. static void fd_deselect( void );
  301. static void fd_motor_off_timer( unsigned long dummy );
  302. static void check_change( unsigned long dummy );
  303. static irqreturn_t floppy_irq (int irq, void *dummy);
  304. static void fd_error( void );
  305. static int do_format(int drive, int type, struct atari_format_descr *desc);
  306. static void do_fd_action( int drive );
  307. static void fd_calibrate( void );
  308. static void fd_calibrate_done( int status );
  309. static void fd_seek( void );
  310. static void fd_seek_done( int status );
  311. static void fd_rwsec( void );
  312. static void fd_readtrack_check( unsigned long dummy );
  313. static void fd_rwsec_done( int status );
  314. static void fd_rwsec_done1(int status);
  315. static void fd_writetrack( void );
  316. static void fd_writetrack_done( int status );
  317. static void fd_times_out( unsigned long dummy );
  318. static void finish_fdc( void );
  319. static void finish_fdc_done( int dummy );
  320. static void setup_req_params( int drive );
  321. static void redo_fd_request( void);
  322. static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
  323. cmd, unsigned long param);
  324. static void fd_probe( int drive );
  325. static int fd_test_drive_present( int drive );
  326. static void config_types( void );
  327. static int floppy_open(struct block_device *bdev, fmode_t mode);
  328. static void floppy_release(struct gendisk *disk, fmode_t mode);
  329. /************************* End of Prototypes **************************/
  330. static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer, 0, 0);
  331. static DEFINE_TIMER(readtrack_timer, fd_readtrack_check, 0, 0);
  332. static DEFINE_TIMER(timeout_timer, fd_times_out, 0, 0);
  333. static DEFINE_TIMER(fd_timer, check_change, 0, 0);
  334. static void fd_end_request_cur(int err)
  335. {
  336. if (!__blk_end_request_cur(fd_request, err))
  337. fd_request = NULL;
  338. }
  339. static inline void start_motor_off_timer(void)
  340. {
  341. mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
  342. MotorOffTrys = 0;
  343. }
  344. static inline void start_check_change_timer( void )
  345. {
  346. mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
  347. }
  348. static inline void start_timeout(void)
  349. {
  350. mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
  351. }
  352. static inline void stop_timeout(void)
  353. {
  354. del_timer(&timeout_timer);
  355. }
  356. /* Select the side to use. */
  357. static void fd_select_side( int side )
  358. {
  359. unsigned long flags;
  360. /* protect against various other ints mucking around with the PSG */
  361. local_irq_save(flags);
  362. sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
  363. sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
  364. sound_ym.rd_data_reg_sel & 0xfe;
  365. local_irq_restore(flags);
  366. }
  367. /* Select a drive, update the FDC's track register and set the correct
  368. * clock speed for this disk's type.
  369. */
  370. static void fd_select_drive( int drive )
  371. {
  372. unsigned long flags;
  373. unsigned char tmp;
  374. if (drive == SelectedDrive)
  375. return;
  376. /* protect against various other ints mucking around with the PSG */
  377. local_irq_save(flags);
  378. sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
  379. tmp = sound_ym.rd_data_reg_sel;
  380. sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
  381. atari_dont_touch_floppy_select = 1;
  382. local_irq_restore(flags);
  383. /* restore track register to saved value */
  384. FDC_WRITE( FDCREG_TRACK, UD.track );
  385. udelay(25);
  386. /* select 8/16 MHz */
  387. if (UDT)
  388. if (ATARIHW_PRESENT(FDCSPEED))
  389. dma_wd.fdc_speed = UDT->fdc_speed;
  390. SelectedDrive = drive;
  391. }
  392. /* Deselect both drives. */
  393. static void fd_deselect( void )
  394. {
  395. unsigned long flags;
  396. /* protect against various other ints mucking around with the PSG */
  397. local_irq_save(flags);
  398. atari_dont_touch_floppy_select = 0;
  399. sound_ym.rd_data_reg_sel=14; /* Select PSG Port A */
  400. sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
  401. (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
  402. /* On Falcon, the drive B select line is used on the printer port, so
  403. * leave it alone... */
  404. SelectedDrive = -1;
  405. local_irq_restore(flags);
  406. }
  407. /* This timer function deselects the drives when the FDC switched the
  408. * motor off. The deselection cannot happen earlier because the FDC
  409. * counts the index signals, which arrive only if one drive is selected.
  410. */
  411. static void fd_motor_off_timer( unsigned long dummy )
  412. {
  413. unsigned char status;
  414. if (SelectedDrive < 0)
  415. /* no drive selected, needn't deselect anyone */
  416. return;
  417. if (stdma_islocked())
  418. goto retry;
  419. status = FDC_READ( FDCREG_STATUS );
  420. if (!(status & 0x80)) {
  421. /* motor already turned off by FDC -> deselect drives */
  422. MotorOn = 0;
  423. fd_deselect();
  424. return;
  425. }
  426. /* not yet off, try again */
  427. retry:
  428. /* Test again later; if tested too often, it seems there is no disk
  429. * in the drive and the FDC will leave the motor on forever (or,
  430. * at least until a disk is inserted). So we'll test only twice
  431. * per second from then on...
  432. */
  433. mod_timer(&motor_off_timer,
  434. jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
  435. }
  436. /* This function is repeatedly called to detect disk changes (as good
  437. * as possible) and keep track of the current state of the write protection.
  438. */
  439. static void check_change( unsigned long dummy )
  440. {
  441. static int drive = 0;
  442. unsigned long flags;
  443. unsigned char old_porta;
  444. int stat;
  445. if (++drive > 1 || !UD.connected)
  446. drive = 0;
  447. /* protect against various other ints mucking around with the PSG */
  448. local_irq_save(flags);
  449. if (!stdma_islocked()) {
  450. sound_ym.rd_data_reg_sel = 14;
  451. old_porta = sound_ym.rd_data_reg_sel;
  452. sound_ym.wd_data = (old_porta | DSKDRVNONE) &
  453. ~(drive == 0 ? DSKDRV0 : DSKDRV1);
  454. stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
  455. sound_ym.wd_data = old_porta;
  456. if (stat != UD.wpstat) {
  457. DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
  458. UD.wpstat = stat;
  459. set_bit (drive, &changed_floppies);
  460. }
  461. }
  462. local_irq_restore(flags);
  463. start_check_change_timer();
  464. }
  465. /* Handling of the Head Settling Flag: This flag should be set after each
  466. * seek operation, because we don't use seeks with verify.
  467. */
  468. static inline void set_head_settle_flag(void)
  469. {
  470. HeadSettleFlag = FDCCMDADD_E;
  471. }
  472. static inline int get_head_settle_flag(void)
  473. {
  474. int tmp = HeadSettleFlag;
  475. HeadSettleFlag = 0;
  476. return( tmp );
  477. }
  478. static inline void copy_buffer(void *from, void *to)
  479. {
  480. ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
  481. int cnt;
  482. for (cnt = 512/4; cnt; cnt--)
  483. *p2++ = *p1++;
  484. }
  485. /* General Interrupt Handling */
  486. static void (*FloppyIRQHandler)( int status ) = NULL;
  487. static irqreturn_t floppy_irq (int irq, void *dummy)
  488. {
  489. unsigned char status;
  490. void (*handler)( int );
  491. handler = xchg(&FloppyIRQHandler, NULL);
  492. if (handler) {
  493. nop();
  494. status = FDC_READ( FDCREG_STATUS );
  495. DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
  496. handler( status );
  497. }
  498. else {
  499. DPRINT(("FDC irq, no handler\n"));
  500. }
  501. return IRQ_HANDLED;
  502. }
  503. /* Error handling: If some error happened, retry some times, then
  504. * recalibrate, then try again, and fail after MAX_ERRORS.
  505. */
  506. static void fd_error( void )
  507. {
  508. if (IsFormatting) {
  509. IsFormatting = 0;
  510. FormatError = 1;
  511. complete(&format_wait);
  512. return;
  513. }
  514. if (!fd_request)
  515. return;
  516. fd_request->errors++;
  517. if (fd_request->errors >= MAX_ERRORS) {
  518. printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
  519. fd_end_request_cur(-EIO);
  520. }
  521. else if (fd_request->errors == RECALIBRATE_ERRORS) {
  522. printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
  523. if (SelectedDrive != -1)
  524. SUD.track = -1;
  525. }
  526. redo_fd_request();
  527. }
  528. #define SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
  529. /* ---------- Formatting ---------- */
  530. #define FILL(n,val) \
  531. do { \
  532. memset( p, val, n ); \
  533. p += n; \
  534. } while(0)
  535. static int do_format(int drive, int type, struct atari_format_descr *desc)
  536. {
  537. unsigned char *p;
  538. int sect, nsect;
  539. unsigned long flags;
  540. DPRINT(("do_format( dr=%d tr=%d he=%d offs=%d )\n",
  541. drive, desc->track, desc->head, desc->sect_offset ));
  542. wait_event(fdc_wait, cmpxchg(&fdc_busy, 0, 1) == 0);
  543. local_irq_save(flags);
  544. stdma_lock(floppy_irq, NULL);
  545. atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
  546. local_irq_restore(flags);
  547. if (type) {
  548. if (--type >= NUM_DISK_MINORS ||
  549. minor2disktype[type].drive_types > DriveType) {
  550. redo_fd_request();
  551. return -EINVAL;
  552. }
  553. type = minor2disktype[type].index;
  554. UDT = &atari_disk_type[type];
  555. }
  556. if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
  557. redo_fd_request();
  558. return -EINVAL;
  559. }
  560. nsect = UDT->spt;
  561. p = TrackBuffer;
  562. /* The track buffer is used for the raw track data, so its
  563. contents become invalid! */
  564. BufferDrive = -1;
  565. /* stop deselect timer */
  566. del_timer( &motor_off_timer );
  567. FILL( 60 * (nsect / 9), 0x4e );
  568. for( sect = 0; sect < nsect; ++sect ) {
  569. FILL( 12, 0 );
  570. FILL( 3, 0xf5 );
  571. *p++ = 0xfe;
  572. *p++ = desc->track;
  573. *p++ = desc->head;
  574. *p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
  575. *p++ = 2;
  576. *p++ = 0xf7;
  577. FILL( 22, 0x4e );
  578. FILL( 12, 0 );
  579. FILL( 3, 0xf5 );
  580. *p++ = 0xfb;
  581. FILL( 512, 0xe5 );
  582. *p++ = 0xf7;
  583. FILL( 40, 0x4e );
  584. }
  585. FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
  586. IsFormatting = 1;
  587. FormatError = 0;
  588. ReqTrack = desc->track;
  589. ReqSide = desc->head;
  590. do_fd_action( drive );
  591. wait_for_completion(&format_wait);
  592. redo_fd_request();
  593. return( FormatError ? -EIO : 0 );
  594. }
  595. /* do_fd_action() is the general procedure for a fd request: All
  596. * required parameter settings (drive select, side select, track
  597. * position) are checked and set if needed. For each of these
  598. * parameters and the actual reading or writing exist two functions:
  599. * one that starts the setting (or skips it if possible) and one
  600. * callback for the "done" interrupt. Each done func calls the next
  601. * set function to propagate the request down to fd_rwsec_done().
  602. */
  603. static void do_fd_action( int drive )
  604. {
  605. DPRINT(("do_fd_action\n"));
  606. if (UseTrackbuffer && !IsFormatting) {
  607. repeat:
  608. if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
  609. if (ReqCmd == READ) {
  610. copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
  611. if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
  612. /* read next sector */
  613. setup_req_params( drive );
  614. goto repeat;
  615. }
  616. else {
  617. /* all sectors finished */
  618. fd_end_request_cur(0);
  619. redo_fd_request();
  620. return;
  621. }
  622. }
  623. else {
  624. /* cmd == WRITE, pay attention to track buffer
  625. * consistency! */
  626. copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
  627. }
  628. }
  629. }
  630. if (SelectedDrive != drive)
  631. fd_select_drive( drive );
  632. if (UD.track == -1)
  633. fd_calibrate();
  634. else if (UD.track != ReqTrack << UDT->stretch)
  635. fd_seek();
  636. else if (IsFormatting)
  637. fd_writetrack();
  638. else
  639. fd_rwsec();
  640. }
  641. /* Seek to track 0 if the current track is unknown */
  642. static void fd_calibrate( void )
  643. {
  644. if (SUD.track >= 0) {
  645. fd_calibrate_done( 0 );
  646. return;
  647. }
  648. if (ATARIHW_PRESENT(FDCSPEED))
  649. dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */;
  650. DPRINT(("fd_calibrate\n"));
  651. SET_IRQ_HANDLER( fd_calibrate_done );
  652. /* we can't verify, since the speed may be incorrect */
  653. FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
  654. NeedSeek = 1;
  655. MotorOn = 1;
  656. start_timeout();
  657. /* wait for IRQ */
  658. }
  659. static void fd_calibrate_done( int status )
  660. {
  661. DPRINT(("fd_calibrate_done()\n"));
  662. stop_timeout();
  663. /* set the correct speed now */
  664. if (ATARIHW_PRESENT(FDCSPEED))
  665. dma_wd.fdc_speed = SUDT->fdc_speed;
  666. if (status & FDCSTAT_RECNF) {
  667. printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
  668. fd_error();
  669. }
  670. else {
  671. SUD.track = 0;
  672. fd_seek();
  673. }
  674. }
  675. /* Seek the drive to the requested track. The drive must have been
  676. * calibrated at some point before this.
  677. */
  678. static void fd_seek( void )
  679. {
  680. if (SUD.track == ReqTrack << SUDT->stretch) {
  681. fd_seek_done( 0 );
  682. return;
  683. }
  684. if (ATARIHW_PRESENT(FDCSPEED)) {
  685. dma_wd.fdc_speed = 0; /* always seek witch 8 Mhz */
  686. MFPDELAY();
  687. }
  688. DPRINT(("fd_seek() to track %d\n",ReqTrack));
  689. FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
  690. udelay(25);
  691. SET_IRQ_HANDLER( fd_seek_done );
  692. FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
  693. MotorOn = 1;
  694. set_head_settle_flag();
  695. start_timeout();
  696. /* wait for IRQ */
  697. }
  698. static void fd_seek_done( int status )
  699. {
  700. DPRINT(("fd_seek_done()\n"));
  701. stop_timeout();
  702. /* set the correct speed */
  703. if (ATARIHW_PRESENT(FDCSPEED))
  704. dma_wd.fdc_speed = SUDT->fdc_speed;
  705. if (status & FDCSTAT_RECNF) {
  706. printk(KERN_ERR "fd%d: seek error (to track %d)\n",
  707. SelectedDrive, ReqTrack );
  708. /* we don't know exactly which track we are on now! */
  709. SUD.track = -1;
  710. fd_error();
  711. }
  712. else {
  713. SUD.track = ReqTrack << SUDT->stretch;
  714. NeedSeek = 0;
  715. if (IsFormatting)
  716. fd_writetrack();
  717. else
  718. fd_rwsec();
  719. }
  720. }
  721. /* This does the actual reading/writing after positioning the head
  722. * over the correct track.
  723. */
  724. static int MultReadInProgress = 0;
  725. static void fd_rwsec( void )
  726. {
  727. unsigned long paddr, flags;
  728. unsigned int rwflag, old_motoron;
  729. unsigned int track;
  730. DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
  731. if (ReqCmd == WRITE) {
  732. if (ATARIHW_PRESENT(EXTD_DMA)) {
  733. paddr = virt_to_phys(ReqData);
  734. }
  735. else {
  736. copy_buffer( ReqData, DMABuffer );
  737. paddr = PhysDMABuffer;
  738. }
  739. dma_cache_maintenance( paddr, 512, 1 );
  740. rwflag = 0x100;
  741. }
  742. else {
  743. if (read_track)
  744. paddr = PhysTrackBuffer;
  745. else
  746. paddr = ATARIHW_PRESENT(EXTD_DMA) ?
  747. virt_to_phys(ReqData) : PhysDMABuffer;
  748. rwflag = 0;
  749. }
  750. fd_select_side( ReqSide );
  751. /* Start sector of this operation */
  752. FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
  753. MFPDELAY();
  754. /* Cheat for track if stretch != 0 */
  755. if (SUDT->stretch) {
  756. track = FDC_READ( FDCREG_TRACK);
  757. MFPDELAY();
  758. FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
  759. }
  760. udelay(25);
  761. /* Setup DMA */
  762. local_irq_save(flags);
  763. dma_wd.dma_lo = (unsigned char)paddr;
  764. MFPDELAY();
  765. paddr >>= 8;
  766. dma_wd.dma_md = (unsigned char)paddr;
  767. MFPDELAY();
  768. paddr >>= 8;
  769. if (ATARIHW_PRESENT(EXTD_DMA))
  770. st_dma_ext_dmahi = (unsigned short)paddr;
  771. else
  772. dma_wd.dma_hi = (unsigned char)paddr;
  773. MFPDELAY();
  774. local_irq_restore(flags);
  775. /* Clear FIFO and switch DMA to correct mode */
  776. dma_wd.dma_mode_status = 0x90 | rwflag;
  777. MFPDELAY();
  778. dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
  779. MFPDELAY();
  780. dma_wd.dma_mode_status = 0x90 | rwflag;
  781. MFPDELAY();
  782. /* How many sectors for DMA */
  783. dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
  784. udelay(25);
  785. /* Start operation */
  786. dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
  787. udelay(25);
  788. SET_IRQ_HANDLER( fd_rwsec_done );
  789. dma_wd.fdc_acces_seccount =
  790. (get_head_settle_flag() |
  791. (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
  792. old_motoron = MotorOn;
  793. MotorOn = 1;
  794. NeedSeek = 1;
  795. /* wait for interrupt */
  796. if (read_track) {
  797. /* If reading a whole track, wait about one disk rotation and
  798. * then check if all sectors are read. The FDC will even
  799. * search for the first non-existent sector and need 1 sec to
  800. * recognise that it isn't present :-(
  801. */
  802. MultReadInProgress = 1;
  803. mod_timer(&readtrack_timer,
  804. /* 1 rot. + 5 rot.s if motor was off */
  805. jiffies + HZ/5 + (old_motoron ? 0 : HZ));
  806. }
  807. start_timeout();
  808. }
  809. static void fd_readtrack_check( unsigned long dummy )
  810. {
  811. unsigned long flags, addr, addr2;
  812. local_irq_save(flags);
  813. if (!MultReadInProgress) {
  814. /* This prevents a race condition that could arise if the
  815. * interrupt is triggered while the calling of this timer
  816. * callback function takes place. The IRQ function then has
  817. * already cleared 'MultReadInProgress' when flow of control
  818. * gets here.
  819. */
  820. local_irq_restore(flags);
  821. return;
  822. }
  823. /* get the current DMA address */
  824. /* ++ f.a. read twice to avoid being fooled by switcher */
  825. addr = 0;
  826. do {
  827. addr2 = addr;
  828. addr = dma_wd.dma_lo & 0xff;
  829. MFPDELAY();
  830. addr |= (dma_wd.dma_md & 0xff) << 8;
  831. MFPDELAY();
  832. if (ATARIHW_PRESENT( EXTD_DMA ))
  833. addr |= (st_dma_ext_dmahi & 0xffff) << 16;
  834. else
  835. addr |= (dma_wd.dma_hi & 0xff) << 16;
  836. MFPDELAY();
  837. } while(addr != addr2);
  838. if (addr >= PhysTrackBuffer + SUDT->spt*512) {
  839. /* already read enough data, force an FDC interrupt to stop
  840. * the read operation
  841. */
  842. SET_IRQ_HANDLER( NULL );
  843. MultReadInProgress = 0;
  844. local_irq_restore(flags);
  845. DPRINT(("fd_readtrack_check(): done\n"));
  846. FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
  847. udelay(25);
  848. /* No error until now -- the FDC would have interrupted
  849. * otherwise!
  850. */
  851. fd_rwsec_done1(0);
  852. }
  853. else {
  854. /* not yet finished, wait another tenth rotation */
  855. local_irq_restore(flags);
  856. DPRINT(("fd_readtrack_check(): not yet finished\n"));
  857. mod_timer(&readtrack_timer, jiffies + HZ/5/10);
  858. }
  859. }
  860. static void fd_rwsec_done( int status )
  861. {
  862. DPRINT(("fd_rwsec_done()\n"));
  863. if (read_track) {
  864. del_timer(&readtrack_timer);
  865. if (!MultReadInProgress)
  866. return;
  867. MultReadInProgress = 0;
  868. }
  869. fd_rwsec_done1(status);
  870. }
  871. static void fd_rwsec_done1(int status)
  872. {
  873. unsigned int track;
  874. stop_timeout();
  875. /* Correct the track if stretch != 0 */
  876. if (SUDT->stretch) {
  877. track = FDC_READ( FDCREG_TRACK);
  878. MFPDELAY();
  879. FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
  880. }
  881. if (!UseTrackbuffer) {
  882. dma_wd.dma_mode_status = 0x90;
  883. MFPDELAY();
  884. if (!(dma_wd.dma_mode_status & 0x01)) {
  885. printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
  886. goto err_end;
  887. }
  888. }
  889. MFPDELAY();
  890. if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
  891. printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
  892. goto err_end;
  893. }
  894. if ((status & FDCSTAT_RECNF) &&
  895. /* RECNF is no error after a multiple read when the FDC
  896. searched for a non-existent sector! */
  897. !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
  898. if (Probing) {
  899. if (SUDT > atari_disk_type) {
  900. if (SUDT[-1].blocks > ReqBlock) {
  901. /* try another disk type */
  902. SUDT--;
  903. set_capacity(unit[SelectedDrive].disk,
  904. SUDT->blocks);
  905. } else
  906. Probing = 0;
  907. }
  908. else {
  909. if (SUD.flags & FTD_MSG)
  910. printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
  911. SelectedDrive, SUDT->name );
  912. Probing=0;
  913. }
  914. } else {
  915. /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
  916. if (SUD.autoprobe) {
  917. SUDT = atari_disk_type + StartDiskType[DriveType];
  918. set_capacity(unit[SelectedDrive].disk,
  919. SUDT->blocks);
  920. Probing = 1;
  921. }
  922. }
  923. if (Probing) {
  924. if (ATARIHW_PRESENT(FDCSPEED)) {
  925. dma_wd.fdc_speed = SUDT->fdc_speed;
  926. MFPDELAY();
  927. }
  928. setup_req_params( SelectedDrive );
  929. BufferDrive = -1;
  930. do_fd_action( SelectedDrive );
  931. return;
  932. }
  933. printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
  934. SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
  935. goto err_end;
  936. }
  937. if (status & FDCSTAT_CRC) {
  938. printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
  939. SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
  940. goto err_end;
  941. }
  942. if (status & FDCSTAT_LOST) {
  943. printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
  944. SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
  945. goto err_end;
  946. }
  947. Probing = 0;
  948. if (ReqCmd == READ) {
  949. if (!read_track) {
  950. void *addr;
  951. addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
  952. dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
  953. if (!ATARIHW_PRESENT( EXTD_DMA ))
  954. copy_buffer (addr, ReqData);
  955. } else {
  956. dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
  957. BufferDrive = SelectedDrive;
  958. BufferSide = ReqSide;
  959. BufferTrack = ReqTrack;
  960. copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
  961. }
  962. }
  963. if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
  964. /* read next sector */
  965. setup_req_params( SelectedDrive );
  966. do_fd_action( SelectedDrive );
  967. }
  968. else {
  969. /* all sectors finished */
  970. fd_end_request_cur(0);
  971. redo_fd_request();
  972. }
  973. return;
  974. err_end:
  975. BufferDrive = -1;
  976. fd_error();
  977. }
  978. static void fd_writetrack( void )
  979. {
  980. unsigned long paddr, flags;
  981. unsigned int track;
  982. DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
  983. paddr = PhysTrackBuffer;
  984. dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
  985. fd_select_side( ReqSide );
  986. /* Cheat for track if stretch != 0 */
  987. if (SUDT->stretch) {
  988. track = FDC_READ( FDCREG_TRACK);
  989. MFPDELAY();
  990. FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
  991. }
  992. udelay(40);
  993. /* Setup DMA */
  994. local_irq_save(flags);
  995. dma_wd.dma_lo = (unsigned char)paddr;
  996. MFPDELAY();
  997. paddr >>= 8;
  998. dma_wd.dma_md = (unsigned char)paddr;
  999. MFPDELAY();
  1000. paddr >>= 8;
  1001. if (ATARIHW_PRESENT( EXTD_DMA ))
  1002. st_dma_ext_dmahi = (unsigned short)paddr;
  1003. else
  1004. dma_wd.dma_hi = (unsigned char)paddr;
  1005. MFPDELAY();
  1006. local_irq_restore(flags);
  1007. /* Clear FIFO and switch DMA to correct mode */
  1008. dma_wd.dma_mode_status = 0x190;
  1009. MFPDELAY();
  1010. dma_wd.dma_mode_status = 0x90;
  1011. MFPDELAY();
  1012. dma_wd.dma_mode_status = 0x190;
  1013. MFPDELAY();
  1014. /* How many sectors for DMA */
  1015. dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
  1016. udelay(40);
  1017. /* Start operation */
  1018. dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
  1019. udelay(40);
  1020. SET_IRQ_HANDLER( fd_writetrack_done );
  1021. dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
  1022. MotorOn = 1;
  1023. start_timeout();
  1024. /* wait for interrupt */
  1025. }
  1026. static void fd_writetrack_done( int status )
  1027. {
  1028. DPRINT(("fd_writetrack_done()\n"));
  1029. stop_timeout();
  1030. if (status & FDCSTAT_WPROT) {
  1031. printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
  1032. goto err_end;
  1033. }
  1034. if (status & FDCSTAT_LOST) {
  1035. printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
  1036. SelectedDrive, ReqSide, ReqTrack );
  1037. goto err_end;
  1038. }
  1039. complete(&format_wait);
  1040. return;
  1041. err_end:
  1042. fd_error();
  1043. }
  1044. static void fd_times_out( unsigned long dummy )
  1045. {
  1046. atari_disable_irq( IRQ_MFP_FDC );
  1047. if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
  1048. * before we came here... */
  1049. SET_IRQ_HANDLER( NULL );
  1050. /* If the timeout occurred while the readtrack_check timer was
  1051. * active, we need to cancel it, else bad things will happen */
  1052. if (UseTrackbuffer)
  1053. del_timer( &readtrack_timer );
  1054. FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
  1055. udelay( 25 );
  1056. printk(KERN_ERR "floppy timeout\n" );
  1057. fd_error();
  1058. end:
  1059. atari_enable_irq( IRQ_MFP_FDC );
  1060. }
  1061. /* The (noop) seek operation here is needed to make the WP bit in the
  1062. * FDC status register accessible for check_change. If the last disk
  1063. * operation would have been a RDSEC, this bit would always read as 0
  1064. * no matter what :-( To save time, the seek goes to the track we're
  1065. * already on.
  1066. */
  1067. static void finish_fdc( void )
  1068. {
  1069. if (!NeedSeek) {
  1070. finish_fdc_done( 0 );
  1071. }
  1072. else {
  1073. DPRINT(("finish_fdc: dummy seek started\n"));
  1074. FDC_WRITE (FDCREG_DATA, SUD.track);
  1075. SET_IRQ_HANDLER( finish_fdc_done );
  1076. FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
  1077. MotorOn = 1;
  1078. start_timeout();
  1079. /* we must wait for the IRQ here, because the ST-DMA
  1080. is released immediately afterwards and the interrupt
  1081. may be delivered to the wrong driver. */
  1082. }
  1083. }
  1084. static void finish_fdc_done( int dummy )
  1085. {
  1086. unsigned long flags;
  1087. DPRINT(("finish_fdc_done entered\n"));
  1088. stop_timeout();
  1089. NeedSeek = 0;
  1090. if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
  1091. /* If the check for a disk change is done too early after this
  1092. * last seek command, the WP bit still reads wrong :-((
  1093. */
  1094. mod_timer(&fd_timer, jiffies + 5);
  1095. else
  1096. start_check_change_timer();
  1097. start_motor_off_timer();
  1098. local_irq_save(flags);
  1099. stdma_release();
  1100. fdc_busy = 0;
  1101. wake_up( &fdc_wait );
  1102. local_irq_restore(flags);
  1103. DPRINT(("finish_fdc() finished\n"));
  1104. }
  1105. /* The detection of disk changes is a dark chapter in Atari history :-(
  1106. * Because the "Drive ready" signal isn't present in the Atari
  1107. * hardware, one has to rely on the "Write Protect". This works fine,
  1108. * as long as no write protected disks are used. TOS solves this
  1109. * problem by introducing tri-state logic ("maybe changed") and
  1110. * looking at the serial number in block 0. This isn't possible for
  1111. * Linux, since the floppy driver can't make assumptions about the
  1112. * filesystem used on the disk and thus the contents of block 0. I've
  1113. * chosen the method to always say "The disk was changed" if it is
  1114. * unsure whether it was. This implies that every open or mount
  1115. * invalidates the disk buffers if you work with write protected
  1116. * disks. But at least this is better than working with incorrect data
  1117. * due to unrecognised disk changes.
  1118. */
  1119. static unsigned int floppy_check_events(struct gendisk *disk,
  1120. unsigned int clearing)
  1121. {
  1122. struct atari_floppy_struct *p = disk->private_data;
  1123. unsigned int drive = p - unit;
  1124. if (test_bit (drive, &fake_change)) {
  1125. /* simulated change (e.g. after formatting) */
  1126. return DISK_EVENT_MEDIA_CHANGE;
  1127. }
  1128. if (test_bit (drive, &changed_floppies)) {
  1129. /* surely changed (the WP signal changed at least once) */
  1130. return DISK_EVENT_MEDIA_CHANGE;
  1131. }
  1132. if (UD.wpstat) {
  1133. /* WP is on -> could be changed: to be sure, buffers should be
  1134. * invalidated...
  1135. */
  1136. return DISK_EVENT_MEDIA_CHANGE;
  1137. }
  1138. return 0;
  1139. }
  1140. static int floppy_revalidate(struct gendisk *disk)
  1141. {
  1142. struct atari_floppy_struct *p = disk->private_data;
  1143. unsigned int drive = p - unit;
  1144. if (test_bit(drive, &changed_floppies) ||
  1145. test_bit(drive, &fake_change) ||
  1146. p->disktype == 0) {
  1147. if (UD.flags & FTD_MSG)
  1148. printk(KERN_ERR "floppy: clear format %p!\n", UDT);
  1149. BufferDrive = -1;
  1150. clear_bit(drive, &fake_change);
  1151. clear_bit(drive, &changed_floppies);
  1152. /* MSch: clearing geometry makes sense only for autoprobe
  1153. formats, for 'permanent user-defined' parameter:
  1154. restore default_params[] here if flagged valid! */
  1155. if (default_params[drive].blocks == 0)
  1156. UDT = NULL;
  1157. else
  1158. UDT = &default_params[drive];
  1159. }
  1160. return 0;
  1161. }
  1162. /* This sets up the global variables describing the current request. */
  1163. static void setup_req_params( int drive )
  1164. {
  1165. int block = ReqBlock + ReqCnt;
  1166. ReqTrack = block / UDT->spt;
  1167. ReqSector = block - ReqTrack * UDT->spt + 1;
  1168. ReqSide = ReqTrack & 1;
  1169. ReqTrack >>= 1;
  1170. ReqData = ReqBuffer + 512 * ReqCnt;
  1171. if (UseTrackbuffer)
  1172. read_track = (ReqCmd == READ && fd_request->errors == 0);
  1173. else
  1174. read_track = 0;
  1175. DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
  1176. ReqTrack, ReqSector, (unsigned long)ReqData ));
  1177. }
  1178. /*
  1179. * Round-robin between our available drives, doing one request from each
  1180. */
  1181. static struct request *set_next_request(void)
  1182. {
  1183. struct request_queue *q;
  1184. int old_pos = fdc_queue;
  1185. struct request *rq = NULL;
  1186. do {
  1187. q = unit[fdc_queue].disk->queue;
  1188. if (++fdc_queue == FD_MAX_UNITS)
  1189. fdc_queue = 0;
  1190. if (q) {
  1191. rq = blk_fetch_request(q);
  1192. if (rq)
  1193. break;
  1194. }
  1195. } while (fdc_queue != old_pos);
  1196. return rq;
  1197. }
  1198. static void redo_fd_request(void)
  1199. {
  1200. int drive, type;
  1201. struct atari_floppy_struct *floppy;
  1202. DPRINT(("redo_fd_request: fd_request=%p dev=%s fd_request->sector=%ld\n",
  1203. fd_request, fd_request ? fd_request->rq_disk->disk_name : "",
  1204. fd_request ? blk_rq_pos(fd_request) : 0 ));
  1205. IsFormatting = 0;
  1206. repeat:
  1207. if (!fd_request) {
  1208. fd_request = set_next_request();
  1209. if (!fd_request)
  1210. goto the_end;
  1211. }
  1212. floppy = fd_request->rq_disk->private_data;
  1213. drive = floppy - unit;
  1214. type = floppy->type;
  1215. if (!UD.connected) {
  1216. /* drive not connected */
  1217. printk(KERN_ERR "Unknown Device: fd%d\n", drive );
  1218. fd_end_request_cur(-EIO);
  1219. goto repeat;
  1220. }
  1221. if (type == 0) {
  1222. if (!UDT) {
  1223. Probing = 1;
  1224. UDT = atari_disk_type + StartDiskType[DriveType];
  1225. set_capacity(floppy->disk, UDT->blocks);
  1226. UD.autoprobe = 1;
  1227. }
  1228. }
  1229. else {
  1230. /* user supplied disk type */
  1231. if (--type >= NUM_DISK_MINORS) {
  1232. printk(KERN_WARNING "fd%d: invalid disk format", drive );
  1233. fd_end_request_cur(-EIO);
  1234. goto repeat;
  1235. }
  1236. if (minor2disktype[type].drive_types > DriveType) {
  1237. printk(KERN_WARNING "fd%d: unsupported disk format", drive );
  1238. fd_end_request_cur(-EIO);
  1239. goto repeat;
  1240. }
  1241. type = minor2disktype[type].index;
  1242. UDT = &atari_disk_type[type];
  1243. set_capacity(floppy->disk, UDT->blocks);
  1244. UD.autoprobe = 0;
  1245. }
  1246. if (blk_rq_pos(fd_request) + 1 > UDT->blocks) {
  1247. fd_end_request_cur(-EIO);
  1248. goto repeat;
  1249. }
  1250. /* stop deselect timer */
  1251. del_timer( &motor_off_timer );
  1252. ReqCnt = 0;
  1253. ReqCmd = rq_data_dir(fd_request);
  1254. ReqBlock = blk_rq_pos(fd_request);
  1255. ReqBuffer = bio_data(fd_request->bio);
  1256. setup_req_params( drive );
  1257. do_fd_action( drive );
  1258. return;
  1259. the_end:
  1260. finish_fdc();
  1261. }
  1262. void do_fd_request(struct request_queue * q)
  1263. {
  1264. DPRINT(("do_fd_request for pid %d\n",current->pid));
  1265. wait_event(fdc_wait, cmpxchg(&fdc_busy, 0, 1) == 0);
  1266. stdma_lock(floppy_irq, NULL);
  1267. atari_disable_irq( IRQ_MFP_FDC );
  1268. redo_fd_request();
  1269. atari_enable_irq( IRQ_MFP_FDC );
  1270. }
  1271. static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
  1272. unsigned int cmd, unsigned long param)
  1273. {
  1274. struct gendisk *disk = bdev->bd_disk;
  1275. struct atari_floppy_struct *floppy = disk->private_data;
  1276. int drive = floppy - unit;
  1277. int type = floppy->type;
  1278. struct atari_format_descr fmt_desc;
  1279. struct atari_disk_type *dtp;
  1280. struct floppy_struct getprm;
  1281. int settype;
  1282. struct floppy_struct setprm;
  1283. void __user *argp = (void __user *)param;
  1284. switch (cmd) {
  1285. case FDGETPRM:
  1286. if (type) {
  1287. if (--type >= NUM_DISK_MINORS)
  1288. return -ENODEV;
  1289. if (minor2disktype[type].drive_types > DriveType)
  1290. return -ENODEV;
  1291. type = minor2disktype[type].index;
  1292. dtp = &atari_disk_type[type];
  1293. if (UD.flags & FTD_MSG)
  1294. printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
  1295. drive, dtp, dtp->name);
  1296. }
  1297. else {
  1298. if (!UDT)
  1299. return -ENXIO;
  1300. else
  1301. dtp = UDT;
  1302. }
  1303. memset((void *)&getprm, 0, sizeof(getprm));
  1304. getprm.size = dtp->blocks;
  1305. getprm.sect = dtp->spt;
  1306. getprm.head = 2;
  1307. getprm.track = dtp->blocks/dtp->spt/2;
  1308. getprm.stretch = dtp->stretch;
  1309. if (copy_to_user(argp, &getprm, sizeof(getprm)))
  1310. return -EFAULT;
  1311. return 0;
  1312. }
  1313. switch (cmd) {
  1314. case FDSETPRM:
  1315. case FDDEFPRM:
  1316. /*
  1317. * MSch 7/96: simple 'set geometry' case: just set the
  1318. * 'default' device params (minor == 0).
  1319. * Currently, the drive geometry is cleared after each
  1320. * disk change and subsequent revalidate()! simple
  1321. * implementation of FDDEFPRM: save geometry from a
  1322. * FDDEFPRM call and restore it in floppy_revalidate() !
  1323. */
  1324. /* get the parameters from user space */
  1325. if (floppy->ref != 1 && floppy->ref != -1)
  1326. return -EBUSY;
  1327. if (copy_from_user(&setprm, argp, sizeof(setprm)))
  1328. return -EFAULT;
  1329. /*
  1330. * first of all: check for floppy change and revalidate,
  1331. * or the next access will revalidate - and clear UDT :-(
  1332. */
  1333. if (floppy_check_events(disk, 0))
  1334. floppy_revalidate(disk);
  1335. if (UD.flags & FTD_MSG)
  1336. printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
  1337. drive, setprm.size, setprm.sect, setprm.stretch);
  1338. /* what if type > 0 here? Overwrite specified entry ? */
  1339. if (type) {
  1340. /* refuse to re-set a predefined type for now */
  1341. redo_fd_request();
  1342. return -EINVAL;
  1343. }
  1344. /*
  1345. * type == 0: first look for a matching entry in the type list,
  1346. * and set the UD.disktype field to use the perdefined entry.
  1347. * TODO: add user-defined format to head of autoprobe list ?
  1348. * Useful to include the user-type for future autodetection!
  1349. */
  1350. for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
  1351. int setidx = 0;
  1352. if (minor2disktype[settype].drive_types > DriveType) {
  1353. /* skip this one, invalid for drive ... */
  1354. continue;
  1355. }
  1356. setidx = minor2disktype[settype].index;
  1357. dtp = &atari_disk_type[setidx];
  1358. /* found matching entry ?? */
  1359. if ( dtp->blocks == setprm.size
  1360. && dtp->spt == setprm.sect
  1361. && dtp->stretch == setprm.stretch ) {
  1362. if (UD.flags & FTD_MSG)
  1363. printk (KERN_INFO "floppy%d: setting %s %p!\n",
  1364. drive, dtp->name, dtp);
  1365. UDT = dtp;
  1366. set_capacity(floppy->disk, UDT->blocks);
  1367. if (cmd == FDDEFPRM) {
  1368. /* save settings as permanent default type */
  1369. default_params[drive].name = dtp->name;
  1370. default_params[drive].spt = dtp->spt;
  1371. default_params[drive].blocks = dtp->blocks;
  1372. default_params[drive].fdc_speed = dtp->fdc_speed;
  1373. default_params[drive].stretch = dtp->stretch;
  1374. }
  1375. return 0;
  1376. }
  1377. }
  1378. /* no matching disk type found above - setting user_params */
  1379. if (cmd == FDDEFPRM) {
  1380. /* set permanent type */
  1381. dtp = &default_params[drive];
  1382. } else
  1383. /* set user type (reset by disk change!) */
  1384. dtp = &user_params[drive];
  1385. dtp->name = "user format";
  1386. dtp->blocks = setprm.size;
  1387. dtp->spt = setprm.sect;
  1388. if (setprm.sect > 14)
  1389. dtp->fdc_speed = 3;
  1390. else
  1391. dtp->fdc_speed = 0;
  1392. dtp->stretch = setprm.stretch;
  1393. if (UD.flags & FTD_MSG)
  1394. printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
  1395. drive, dtp->blocks, dtp->spt, dtp->stretch);
  1396. /* sanity check */
  1397. if (setprm.track != dtp->blocks/dtp->spt/2 ||
  1398. setprm.head != 2) {
  1399. redo_fd_request();
  1400. return -EINVAL;
  1401. }
  1402. UDT = dtp;
  1403. set_capacity(floppy->disk, UDT->blocks);
  1404. return 0;
  1405. case FDMSGON:
  1406. UD.flags |= FTD_MSG;
  1407. return 0;
  1408. case FDMSGOFF:
  1409. UD.flags &= ~FTD_MSG;
  1410. return 0;
  1411. case FDSETEMSGTRESH:
  1412. return -EINVAL;
  1413. case FDFMTBEG:
  1414. return 0;
  1415. case FDFMTTRK:
  1416. if (floppy->ref != 1 && floppy->ref != -1)
  1417. return -EBUSY;
  1418. if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
  1419. return -EFAULT;
  1420. return do_format(drive, type, &fmt_desc);
  1421. case FDCLRPRM:
  1422. UDT = NULL;
  1423. /* MSch: invalidate default_params */
  1424. default_params[drive].blocks = 0;
  1425. set_capacity(floppy->disk, MAX_DISK_SIZE * 2);
  1426. case FDFMTEND:
  1427. case FDFLUSH:
  1428. /* invalidate the buffer track to force a reread */
  1429. BufferDrive = -1;
  1430. set_bit(drive, &fake_change);
  1431. check_disk_change(bdev);
  1432. return 0;
  1433. default:
  1434. return -EINVAL;
  1435. }
  1436. }
  1437. static int fd_ioctl(struct block_device *bdev, fmode_t mode,
  1438. unsigned int cmd, unsigned long arg)
  1439. {
  1440. int ret;
  1441. mutex_lock(&ataflop_mutex);
  1442. ret = fd_locked_ioctl(bdev, mode, cmd, arg);
  1443. mutex_unlock(&ataflop_mutex);
  1444. return ret;
  1445. }
  1446. /* Initialize the 'unit' variable for drive 'drive' */
  1447. static void __init fd_probe( int drive )
  1448. {
  1449. UD.connected = 0;
  1450. UDT = NULL;
  1451. if (!fd_test_drive_present( drive ))
  1452. return;
  1453. UD.connected = 1;
  1454. UD.track = 0;
  1455. switch( UserSteprate[drive] ) {
  1456. case 2:
  1457. UD.steprate = FDCSTEP_2;
  1458. break;
  1459. case 3:
  1460. UD.steprate = FDCSTEP_3;
  1461. break;
  1462. case 6:
  1463. UD.steprate = FDCSTEP_6;
  1464. break;
  1465. case 12:
  1466. UD.steprate = FDCSTEP_12;
  1467. break;
  1468. default: /* should be -1 for "not set by user" */
  1469. if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
  1470. UD.steprate = FDCSTEP_3;
  1471. else
  1472. UD.steprate = FDCSTEP_6;
  1473. break;
  1474. }
  1475. MotorOn = 1; /* from probe restore operation! */
  1476. }
  1477. /* This function tests the physical presence of a floppy drive (not
  1478. * whether a disk is inserted). This is done by issuing a restore
  1479. * command, waiting max. 2 seconds (that should be enough to move the
  1480. * head across the whole disk) and looking at the state of the "TR00"
  1481. * signal. This should now be raised if there is a drive connected
  1482. * (and there is no hardware failure :-) Otherwise, the drive is
  1483. * declared absent.
  1484. */
  1485. static int __init fd_test_drive_present( int drive )
  1486. {
  1487. unsigned long timeout;
  1488. unsigned char status;
  1489. int ok;
  1490. if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
  1491. fd_select_drive( drive );
  1492. /* disable interrupt temporarily */
  1493. atari_turnoff_irq( IRQ_MFP_FDC );
  1494. FDC_WRITE (FDCREG_TRACK, 0xff00);
  1495. FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
  1496. timeout = jiffies + 2*HZ+HZ/2;
  1497. while (time_before(jiffies, timeout))
  1498. if (!(st_mfp.par_dt_reg & 0x20))
  1499. break;
  1500. status = FDC_READ( FDCREG_STATUS );
  1501. ok = (status & FDCSTAT_TR00) != 0;
  1502. /* force interrupt to abort restore operation (FDC would try
  1503. * about 50 seconds!) */
  1504. FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
  1505. udelay(500);
  1506. status = FDC_READ( FDCREG_STATUS );
  1507. udelay(20);
  1508. if (ok) {
  1509. /* dummy seek command to make WP bit accessible */
  1510. FDC_WRITE( FDCREG_DATA, 0 );
  1511. FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
  1512. while( st_mfp.par_dt_reg & 0x20 )
  1513. ;
  1514. status = FDC_READ( FDCREG_STATUS );
  1515. }
  1516. atari_turnon_irq( IRQ_MFP_FDC );
  1517. return( ok );
  1518. }
  1519. /* Look how many and which kind of drives are connected. If there are
  1520. * floppies, additionally start the disk-change and motor-off timers.
  1521. */
  1522. static void __init config_types( void )
  1523. {
  1524. int drive, cnt = 0;
  1525. /* for probing drives, set the FDC speed to 8 MHz */
  1526. if (ATARIHW_PRESENT(FDCSPEED))
  1527. dma_wd.fdc_speed = 0;
  1528. printk(KERN_INFO "Probing floppy drive(s):\n");
  1529. for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
  1530. fd_probe( drive );
  1531. if (UD.connected) {
  1532. printk(KERN_INFO "fd%d\n", drive);
  1533. ++cnt;
  1534. }
  1535. }
  1536. if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
  1537. /* If FDC is still busy from probing, give it another FORCI
  1538. * command to abort the operation. If this isn't done, the FDC
  1539. * will interrupt later and its IRQ line stays low, because
  1540. * the status register isn't read. And this will block any
  1541. * interrupts on this IRQ line :-(
  1542. */
  1543. FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
  1544. udelay(500);
  1545. FDC_READ( FDCREG_STATUS );
  1546. udelay(20);
  1547. }
  1548. if (cnt > 0) {
  1549. start_motor_off_timer();
  1550. if (cnt == 1) fd_select_drive( 0 );
  1551. start_check_change_timer();
  1552. }
  1553. }
  1554. /*
  1555. * floppy_open check for aliasing (/dev/fd0 can be the same as
  1556. * /dev/PS0 etc), and disallows simultaneous access to the same
  1557. * drive with different device numbers.
  1558. */
  1559. static int floppy_open(struct block_device *bdev, fmode_t mode)
  1560. {
  1561. struct atari_floppy_struct *p = bdev->bd_disk->private_data;
  1562. int type = MINOR(bdev->bd_dev) >> 2;
  1563. DPRINT(("fd_open: type=%d\n",type));
  1564. if (p->ref && p->type != type)
  1565. return -EBUSY;
  1566. if (p->ref == -1 || (p->ref && mode & FMODE_EXCL))
  1567. return -EBUSY;
  1568. if (mode & FMODE_EXCL)
  1569. p->ref = -1;
  1570. else
  1571. p->ref++;
  1572. p->type = type;
  1573. if (mode & FMODE_NDELAY)
  1574. return 0;
  1575. if (mode & (FMODE_READ|FMODE_WRITE)) {
  1576. check_disk_change(bdev);
  1577. if (mode & FMODE_WRITE) {
  1578. if (p->wpstat) {
  1579. if (p->ref < 0)
  1580. p->ref = 0;
  1581. else
  1582. p->ref--;
  1583. return -EROFS;
  1584. }
  1585. }
  1586. }
  1587. return 0;
  1588. }
  1589. static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
  1590. {
  1591. int ret;
  1592. mutex_lock(&ataflop_mutex);
  1593. ret = floppy_open(bdev, mode);
  1594. mutex_unlock(&ataflop_mutex);
  1595. return ret;
  1596. }
  1597. static void floppy_release(struct gendisk *disk, fmode_t mode)
  1598. {
  1599. struct atari_floppy_struct *p = disk->private_data;
  1600. mutex_lock(&ataflop_mutex);
  1601. if (p->ref < 0)
  1602. p->ref = 0;
  1603. else if (!p->ref--) {
  1604. printk(KERN_ERR "floppy_release with fd_ref == 0");
  1605. p->ref = 0;
  1606. }
  1607. mutex_unlock(&ataflop_mutex);
  1608. }
  1609. static const struct block_device_operations floppy_fops = {
  1610. .owner = THIS_MODULE,
  1611. .open = floppy_unlocked_open,
  1612. .release = floppy_release,
  1613. .ioctl = fd_ioctl,
  1614. .check_events = floppy_check_events,
  1615. .revalidate_disk= floppy_revalidate,
  1616. };
  1617. static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  1618. {
  1619. int drive = *part & 3;
  1620. int type = *part >> 2;
  1621. if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
  1622. return NULL;
  1623. *part = 0;
  1624. return get_disk(unit[drive].disk);
  1625. }
  1626. static int __init atari_floppy_init (void)
  1627. {
  1628. int i;
  1629. if (!MACH_IS_ATARI)
  1630. /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
  1631. return -ENODEV;
  1632. if (register_blkdev(FLOPPY_MAJOR,"fd"))
  1633. return -EBUSY;
  1634. for (i = 0; i < FD_MAX_UNITS; i++) {
  1635. unit[i].disk = alloc_disk(1);
  1636. if (!unit[i].disk)
  1637. goto Enomem;
  1638. }
  1639. if (UseTrackbuffer < 0)
  1640. /* not set by user -> use default: for now, we turn
  1641. track buffering off for all Medusas, though it
  1642. could be used with ones that have a counter
  1643. card. But the test is too hard :-( */
  1644. UseTrackbuffer = !MACH_IS_MEDUSA;
  1645. /* initialize variables */
  1646. SelectedDrive = -1;
  1647. BufferDrive = -1;
  1648. DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
  1649. if (!DMABuffer) {
  1650. printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
  1651. goto Enomem;
  1652. }
  1653. TrackBuffer = DMABuffer + 512;
  1654. PhysDMABuffer = atari_stram_to_phys(DMABuffer);
  1655. PhysTrackBuffer = virt_to_phys(TrackBuffer);
  1656. BufferDrive = BufferSide = BufferTrack = -1;
  1657. for (i = 0; i < FD_MAX_UNITS; i++) {
  1658. unit[i].track = -1;
  1659. unit[i].flags = 0;
  1660. unit[i].disk->major = FLOPPY_MAJOR;
  1661. unit[i].disk->first_minor = i;
  1662. sprintf(unit[i].disk->disk_name, "fd%d", i);
  1663. unit[i].disk->fops = &floppy_fops;
  1664. unit[i].disk->private_data = &unit[i];
  1665. unit[i].disk->queue = blk_init_queue(do_fd_request,
  1666. &ataflop_lock);
  1667. if (!unit[i].disk->queue)
  1668. goto Enomem;
  1669. set_capacity(unit[i].disk, MAX_DISK_SIZE * 2);
  1670. add_disk(unit[i].disk);
  1671. }
  1672. blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  1673. floppy_find, NULL, NULL);
  1674. printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
  1675. DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
  1676. UseTrackbuffer ? "" : "no ");
  1677. config_types();
  1678. return 0;
  1679. Enomem:
  1680. while (i--) {
  1681. struct request_queue *q = unit[i].disk->queue;
  1682. put_disk(unit[i].disk);
  1683. if (q)
  1684. blk_cleanup_queue(q);
  1685. }
  1686. unregister_blkdev(FLOPPY_MAJOR, "fd");
  1687. return -ENOMEM;
  1688. }
  1689. #ifndef MODULE
  1690. static int __init atari_floppy_setup(char *str)
  1691. {
  1692. int ints[3 + FD_MAX_UNITS];
  1693. int i;
  1694. if (!MACH_IS_ATARI)
  1695. return 0;
  1696. str = get_options(str, 3 + FD_MAX_UNITS, ints);
  1697. if (ints[0] < 1) {
  1698. printk(KERN_ERR "ataflop_setup: no arguments!\n" );
  1699. return 0;
  1700. }
  1701. else if (ints[0] > 2+FD_MAX_UNITS) {
  1702. printk(KERN_ERR "ataflop_setup: too many arguments\n" );
  1703. }
  1704. if (ints[1] < 0 || ints[1] > 2)
  1705. printk(KERN_ERR "ataflop_setup: bad drive type\n" );
  1706. else
  1707. DriveType = ints[1];
  1708. if (ints[0] >= 2)
  1709. UseTrackbuffer = (ints[2] > 0);
  1710. for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
  1711. if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
  1712. printk(KERN_ERR "ataflop_setup: bad steprate\n" );
  1713. else
  1714. UserSteprate[i-3] = ints[i];
  1715. }
  1716. return 1;
  1717. }
  1718. __setup("floppy=", atari_floppy_setup);
  1719. #endif
  1720. static void __exit atari_floppy_exit(void)
  1721. {
  1722. int i;
  1723. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  1724. for (i = 0; i < FD_MAX_UNITS; i++) {
  1725. struct request_queue *q = unit[i].disk->queue;
  1726. del_gendisk(unit[i].disk);
  1727. put_disk(unit[i].disk);
  1728. blk_cleanup_queue(q);
  1729. }
  1730. unregister_blkdev(FLOPPY_MAJOR, "fd");
  1731. del_timer_sync(&fd_timer);
  1732. atari_stram_free( DMABuffer );
  1733. }
  1734. module_init(atari_floppy_init)
  1735. module_exit(atari_floppy_exit)
  1736. MODULE_LICENSE("GPL");