ataflop.c 52 KB

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