ataflop.c 52 KB

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