ataflop.c 51 KB

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