ataflop.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061
  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. unit[i].disk->queue = blk_init_queue(do_fd_request,
  1639. &ataflop_lock);
  1640. if (!unit[i].disk->queue)
  1641. goto Enomem;
  1642. }
  1643. if (UseTrackbuffer < 0)
  1644. /* not set by user -> use default: for now, we turn
  1645. track buffering off for all Medusas, though it
  1646. could be used with ones that have a counter
  1647. card. But the test is too hard :-( */
  1648. UseTrackbuffer = !MACH_IS_MEDUSA;
  1649. /* initialize variables */
  1650. SelectedDrive = -1;
  1651. BufferDrive = -1;
  1652. DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
  1653. if (!DMABuffer) {
  1654. printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
  1655. goto Enomem;
  1656. }
  1657. TrackBuffer = DMABuffer + 512;
  1658. PhysDMABuffer = atari_stram_to_phys(DMABuffer);
  1659. PhysTrackBuffer = virt_to_phys(TrackBuffer);
  1660. BufferDrive = BufferSide = BufferTrack = -1;
  1661. for (i = 0; i < FD_MAX_UNITS; i++) {
  1662. unit[i].track = -1;
  1663. unit[i].flags = 0;
  1664. unit[i].disk->major = FLOPPY_MAJOR;
  1665. unit[i].disk->first_minor = i;
  1666. sprintf(unit[i].disk->disk_name, "fd%d", i);
  1667. unit[i].disk->fops = &floppy_fops;
  1668. unit[i].disk->private_data = &unit[i];
  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. do {
  1681. struct gendisk *disk = unit[i].disk;
  1682. if (disk) {
  1683. if (disk->queue) {
  1684. blk_cleanup_queue(disk->queue);
  1685. disk->queue = NULL;
  1686. }
  1687. put_disk(unit[i].disk);
  1688. }
  1689. } while (i--);
  1690. unregister_blkdev(FLOPPY_MAJOR, "fd");
  1691. return -ENOMEM;
  1692. }
  1693. #ifndef MODULE
  1694. static int __init atari_floppy_setup(char *str)
  1695. {
  1696. int ints[3 + FD_MAX_UNITS];
  1697. int i;
  1698. if (!MACH_IS_ATARI)
  1699. return 0;
  1700. str = get_options(str, 3 + FD_MAX_UNITS, ints);
  1701. if (ints[0] < 1) {
  1702. printk(KERN_ERR "ataflop_setup: no arguments!\n" );
  1703. return 0;
  1704. }
  1705. else if (ints[0] > 2+FD_MAX_UNITS) {
  1706. printk(KERN_ERR "ataflop_setup: too many arguments\n" );
  1707. }
  1708. if (ints[1] < 0 || ints[1] > 2)
  1709. printk(KERN_ERR "ataflop_setup: bad drive type\n" );
  1710. else
  1711. DriveType = ints[1];
  1712. if (ints[0] >= 2)
  1713. UseTrackbuffer = (ints[2] > 0);
  1714. for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
  1715. if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
  1716. printk(KERN_ERR "ataflop_setup: bad steprate\n" );
  1717. else
  1718. UserSteprate[i-3] = ints[i];
  1719. }
  1720. return 1;
  1721. }
  1722. __setup("floppy=", atari_floppy_setup);
  1723. #endif
  1724. static void __exit atari_floppy_exit(void)
  1725. {
  1726. int i;
  1727. blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
  1728. for (i = 0; i < FD_MAX_UNITS; i++) {
  1729. struct request_queue *q = unit[i].disk->queue;
  1730. del_gendisk(unit[i].disk);
  1731. put_disk(unit[i].disk);
  1732. blk_cleanup_queue(q);
  1733. }
  1734. unregister_blkdev(FLOPPY_MAJOR, "fd");
  1735. del_timer_sync(&fd_timer);
  1736. atari_stram_free( DMABuffer );
  1737. }
  1738. module_init(atari_floppy_init)
  1739. module_exit(atari_floppy_exit)
  1740. MODULE_LICENSE("GPL");