ataflop.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  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, blk_mode_t mode,
  386. unsigned int 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 gendisk *disk, blk_mode_t mode);
  391. static void floppy_release(struct gendisk *disk);
  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, blk_mode_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 (disk_check_media_change(disk)) {
  1499. bdev_mark_dead(disk->part0, true);
  1500. floppy_revalidate(disk);
  1501. }
  1502. return 0;
  1503. default:
  1504. return -EINVAL;
  1505. }
  1506. }
  1507. static int fd_ioctl(struct block_device *bdev, blk_mode_t mode,
  1508. unsigned int cmd, unsigned long arg)
  1509. {
  1510. int ret;
  1511. mutex_lock(&ataflop_mutex);
  1512. ret = fd_locked_ioctl(bdev, mode, cmd, arg);
  1513. mutex_unlock(&ataflop_mutex);
  1514. return ret;
  1515. }
  1516. /* Initialize the 'unit' variable for drive 'drive' */
  1517. static void __init fd_probe( int drive )
  1518. {
  1519. UD.connected = 0;
  1520. UDT = NULL;
  1521. if (!fd_test_drive_present( drive ))
  1522. return;
  1523. UD.connected = 1;
  1524. UD.track = 0;
  1525. switch( UserSteprate[drive] ) {
  1526. case 2:
  1527. UD.steprate = FDCSTEP_2;
  1528. break;
  1529. case 3:
  1530. UD.steprate = FDCSTEP_3;
  1531. break;
  1532. case 6:
  1533. UD.steprate = FDCSTEP_6;
  1534. break;
  1535. case 12:
  1536. UD.steprate = FDCSTEP_12;
  1537. break;
  1538. default: /* should be -1 for "not set by user" */
  1539. if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
  1540. UD.steprate = FDCSTEP_3;
  1541. else
  1542. UD.steprate = FDCSTEP_6;
  1543. break;
  1544. }
  1545. MotorOn = 1; /* from probe restore operation! */
  1546. }
  1547. /* This function tests the physical presence of a floppy drive (not
  1548. * whether a disk is inserted). This is done by issuing a restore
  1549. * command, waiting max. 2 seconds (that should be enough to move the
  1550. * head across the whole disk) and looking at the state of the "TR00"
  1551. * signal. This should now be raised if there is a drive connected
  1552. * (and there is no hardware failure :-) Otherwise, the drive is
  1553. * declared absent.
  1554. */
  1555. static int __init fd_test_drive_present( int drive )
  1556. {
  1557. unsigned long timeout;
  1558. unsigned char status;
  1559. int ok;
  1560. if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
  1561. fd_select_drive( drive );
  1562. /* disable interrupt temporarily */
  1563. atari_turnoff_irq( IRQ_MFP_FDC );
  1564. FDC_WRITE (FDCREG_TRACK, 0xff00);
  1565. FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
  1566. timeout = jiffies + 2*HZ+HZ/2;
  1567. while (time_before(jiffies, timeout))
  1568. if (!(st_mfp.par_dt_reg & 0x20))
  1569. break;
  1570. status = FDC_READ( FDCREG_STATUS );
  1571. ok = (status & FDCSTAT_TR00) != 0;
  1572. /* force interrupt to abort restore operation (FDC would try
  1573. * about 50 seconds!) */
  1574. FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
  1575. udelay(500);
  1576. status = FDC_READ( FDCREG_STATUS );
  1577. udelay(20);
  1578. if (ok) {
  1579. /* dummy seek command to make WP bit accessible */
  1580. FDC_WRITE( FDCREG_DATA, 0 );
  1581. FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
  1582. while( st_mfp.par_dt_reg & 0x20 )
  1583. ;
  1584. status = FDC_READ( FDCREG_STATUS );
  1585. }
  1586. atari_turnon_irq( IRQ_MFP_FDC );
  1587. return( ok );
  1588. }
  1589. /* Look how many and which kind of drives are connected. If there are
  1590. * floppies, additionally start the disk-change and motor-off timers.
  1591. */
  1592. static void __init config_types( void )
  1593. {
  1594. int drive, cnt = 0;
  1595. /* for probing drives, set the FDC speed to 8 MHz */
  1596. if (ATARIHW_PRESENT(FDCSPEED))
  1597. dma_wd.fdc_speed = 0;
  1598. printk(KERN_INFO "Probing floppy drive(s):\n");
  1599. for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
  1600. fd_probe( drive );
  1601. if (UD.connected) {
  1602. printk(KERN_INFO "fd%d\n", drive);
  1603. ++cnt;
  1604. }
  1605. }
  1606. if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
  1607. /* If FDC is still busy from probing, give it another FORCI
  1608. * command to abort the operation. If this isn't done, the FDC
  1609. * will interrupt later and its IRQ line stays low, because
  1610. * the status register isn't read. And this will block any
  1611. * interrupts on this IRQ line :-(
  1612. */
  1613. FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
  1614. udelay(500);
  1615. FDC_READ( FDCREG_STATUS );
  1616. udelay(20);
  1617. }
  1618. if (cnt > 0) {
  1619. start_motor_off_timer();
  1620. if (cnt == 1) fd_select_drive( 0 );
  1621. start_check_change_timer();
  1622. }
  1623. }
  1624. /*
  1625. * floppy_open check for aliasing (/dev/fd0 can be the same as
  1626. * /dev/PS0 etc), and disallows simultaneous access to the same
  1627. * drive with different device numbers.
  1628. */
  1629. static int floppy_open(struct gendisk *disk, blk_mode_t mode)
  1630. {
  1631. struct atari_floppy_struct *p = disk->private_data;
  1632. int type = disk->first_minor >> 2;
  1633. DPRINT(("fd_open: type=%d\n",type));
  1634. if (p->ref && p->type != type)
  1635. return -EBUSY;
  1636. if (p->ref == -1 || (p->ref && mode & BLK_OPEN_EXCL))
  1637. return -EBUSY;
  1638. if (mode & BLK_OPEN_EXCL)
  1639. p->ref = -1;
  1640. else
  1641. p->ref++;
  1642. p->type = type;
  1643. if (mode & BLK_OPEN_NDELAY)
  1644. return 0;
  1645. if (mode & (BLK_OPEN_READ | BLK_OPEN_WRITE)) {
  1646. if (disk_check_media_change(disk))
  1647. floppy_revalidate(disk);
  1648. if (mode & BLK_OPEN_WRITE) {
  1649. if (p->wpstat) {
  1650. if (p->ref < 0)
  1651. p->ref = 0;
  1652. else
  1653. p->ref--;
  1654. return -EROFS;
  1655. }
  1656. }
  1657. }
  1658. return 0;
  1659. }
  1660. static int floppy_unlocked_open(struct gendisk *disk, blk_mode_t mode)
  1661. {
  1662. int ret;
  1663. mutex_lock(&ataflop_mutex);
  1664. ret = floppy_open(disk, mode);
  1665. mutex_unlock(&ataflop_mutex);
  1666. return ret;
  1667. }
  1668. static void floppy_release(struct gendisk *disk)
  1669. {
  1670. struct atari_floppy_struct *p = disk->private_data;
  1671. mutex_lock(&ataflop_mutex);
  1672. if (p->ref < 0)
  1673. p->ref = 0;
  1674. else if (!p->ref--) {
  1675. printk(KERN_ERR "floppy_release with fd_ref == 0");
  1676. p->ref = 0;
  1677. }
  1678. mutex_unlock(&ataflop_mutex);
  1679. }
  1680. static const struct block_device_operations floppy_fops = {
  1681. .owner = THIS_MODULE,
  1682. .open = floppy_unlocked_open,
  1683. .release = floppy_release,
  1684. .ioctl = fd_ioctl,
  1685. .check_events = floppy_check_events,
  1686. };
  1687. static const struct blk_mq_ops ataflop_mq_ops = {
  1688. .queue_rq = ataflop_queue_rq,
  1689. };
  1690. static int ataflop_alloc_disk(unsigned int drive, unsigned int type)
  1691. {
  1692. struct queue_limits lim = {
  1693. .features = BLK_FEAT_ROTATIONAL,
  1694. };
  1695. struct gendisk *disk;
  1696. disk = blk_mq_alloc_disk(&unit[drive].tag_set, &lim, NULL);
  1697. if (IS_ERR(disk))
  1698. return PTR_ERR(disk);
  1699. disk->major = FLOPPY_MAJOR;
  1700. disk->first_minor = drive + (type << 2);
  1701. disk->minors = 1;
  1702. sprintf(disk->disk_name, "fd%d", drive);
  1703. disk->fops = &floppy_fops;
  1704. disk->flags |= GENHD_FL_NO_PART;
  1705. disk->events = DISK_EVENT_MEDIA_CHANGE;
  1706. disk->private_data = &unit[drive];
  1707. set_capacity(disk, MAX_DISK_SIZE * 2);
  1708. unit[drive].disk[type] = disk;
  1709. return 0;
  1710. }
  1711. static void ataflop_probe(dev_t dev)
  1712. {
  1713. int drive = MINOR(dev) & 3;
  1714. int type = MINOR(dev) >> 2;
  1715. if (type)
  1716. type--;
  1717. if (drive >= FD_MAX_UNITS || type >= NUM_DISK_MINORS)
  1718. return;
  1719. if (unit[drive].disk[type])
  1720. return;
  1721. if (ataflop_alloc_disk(drive, type))
  1722. return;
  1723. if (add_disk(unit[drive].disk[type]))
  1724. goto cleanup_disk;
  1725. unit[drive].registered[type] = true;
  1726. return;
  1727. cleanup_disk:
  1728. put_disk(unit[drive].disk[type]);
  1729. unit[drive].disk[type] = NULL;
  1730. }
  1731. static void atari_floppy_cleanup(void)
  1732. {
  1733. int i;
  1734. int type;
  1735. for (i = 0; i < FD_MAX_UNITS; i++) {
  1736. for (type = 0; type < NUM_DISK_MINORS; type++) {
  1737. if (!unit[i].disk[type])
  1738. continue;
  1739. del_gendisk(unit[i].disk[type]);
  1740. put_disk(unit[i].disk[type]);
  1741. }
  1742. blk_mq_free_tag_set(&unit[i].tag_set);
  1743. }
  1744. del_timer_sync(&fd_timer);
  1745. atari_stram_free(DMABuffer);
  1746. }
  1747. static void atari_cleanup_floppy_disk(struct atari_floppy_struct *fs)
  1748. {
  1749. int type;
  1750. for (type = 0; type < NUM_DISK_MINORS; type++) {
  1751. if (!fs->disk[type])
  1752. continue;
  1753. if (fs->registered[type])
  1754. del_gendisk(fs->disk[type]);
  1755. put_disk(fs->disk[type]);
  1756. }
  1757. blk_mq_free_tag_set(&fs->tag_set);
  1758. }
  1759. static int __init atari_floppy_init (void)
  1760. {
  1761. int i;
  1762. int ret;
  1763. if (!MACH_IS_ATARI)
  1764. /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
  1765. return -ENODEV;
  1766. for (i = 0; i < FD_MAX_UNITS; i++) {
  1767. memset(&unit[i].tag_set, 0, sizeof(unit[i].tag_set));
  1768. unit[i].tag_set.ops = &ataflop_mq_ops;
  1769. unit[i].tag_set.nr_hw_queues = 1;
  1770. unit[i].tag_set.nr_maps = 1;
  1771. unit[i].tag_set.queue_depth = 2;
  1772. unit[i].tag_set.numa_node = NUMA_NO_NODE;
  1773. unit[i].tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  1774. ret = blk_mq_alloc_tag_set(&unit[i].tag_set);
  1775. if (ret)
  1776. goto err;
  1777. ret = ataflop_alloc_disk(i, 0);
  1778. if (ret) {
  1779. blk_mq_free_tag_set(&unit[i].tag_set);
  1780. goto err;
  1781. }
  1782. }
  1783. if (UseTrackbuffer < 0)
  1784. /* not set by user -> use default: for now, we turn
  1785. track buffering off for all Medusas, though it
  1786. could be used with ones that have a counter
  1787. card. But the test is too hard :-( */
  1788. UseTrackbuffer = !MACH_IS_MEDUSA;
  1789. /* initialize variables */
  1790. SelectedDrive = -1;
  1791. BufferDrive = -1;
  1792. DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
  1793. if (!DMABuffer) {
  1794. printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
  1795. ret = -ENOMEM;
  1796. goto err;
  1797. }
  1798. TrackBuffer = DMABuffer + 512;
  1799. PhysDMABuffer = atari_stram_to_phys(DMABuffer);
  1800. PhysTrackBuffer = virt_to_phys(TrackBuffer);
  1801. BufferDrive = BufferSide = BufferTrack = -1;
  1802. for (i = 0; i < FD_MAX_UNITS; i++) {
  1803. unit[i].track = -1;
  1804. unit[i].flags = 0;
  1805. ret = add_disk(unit[i].disk[0]);
  1806. if (ret)
  1807. goto err_out_dma;
  1808. unit[i].registered[0] = true;
  1809. }
  1810. printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
  1811. DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
  1812. UseTrackbuffer ? "" : "no ");
  1813. config_types();
  1814. ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe);
  1815. if (ret) {
  1816. printk(KERN_ERR "atari_floppy_init: cannot register block device\n");
  1817. atari_floppy_cleanup();
  1818. }
  1819. return ret;
  1820. err_out_dma:
  1821. atari_stram_free(DMABuffer);
  1822. err:
  1823. while (--i >= 0)
  1824. atari_cleanup_floppy_disk(&unit[i]);
  1825. return ret;
  1826. }
  1827. #ifndef MODULE
  1828. static int __init atari_floppy_setup(char *str)
  1829. {
  1830. int ints[3 + FD_MAX_UNITS];
  1831. int i;
  1832. if (!MACH_IS_ATARI)
  1833. return 0;
  1834. str = get_options(str, 3 + FD_MAX_UNITS, ints);
  1835. if (ints[0] < 1) {
  1836. printk(KERN_ERR "ataflop_setup: no arguments!\n" );
  1837. return 0;
  1838. }
  1839. else if (ints[0] > 2+FD_MAX_UNITS) {
  1840. printk(KERN_ERR "ataflop_setup: too many arguments\n" );
  1841. }
  1842. if (ints[1] < 0 || ints[1] > 2)
  1843. printk(KERN_ERR "ataflop_setup: bad drive type\n" );
  1844. else
  1845. DriveType = ints[1];
  1846. if (ints[0] >= 2)
  1847. UseTrackbuffer = (ints[2] > 0);
  1848. for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
  1849. if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
  1850. printk(KERN_ERR "ataflop_setup: bad steprate\n" );
  1851. else
  1852. UserSteprate[i-3] = ints[i];
  1853. }
  1854. return 1;
  1855. }
  1856. __setup("floppy=", atari_floppy_setup);
  1857. #endif
  1858. static void __exit atari_floppy_exit(void)
  1859. {
  1860. unregister_blkdev(FLOPPY_MAJOR, "fd");
  1861. atari_floppy_cleanup();
  1862. }
  1863. module_init(atari_floppy_init)
  1864. module_exit(atari_floppy_exit)
  1865. MODULE_DESCRIPTION("Atari floppy driver");
  1866. MODULE_LICENSE("GPL");