ataflop.c 56 KB

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