ataflop.c 55 KB

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