amiflop.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/amiga/amiflop.c
  4. *
  5. * Copyright (C) 1993 Greg Harp
  6. * Portions of this driver are based on code contributed by Brad Pepers
  7. *
  8. * revised 28.5.95 by Joerg Dorchain
  9. * - now no bugs(?) any more for both HD & DD
  10. * - added support for 40 Track 5.25" drives, 80-track hopefully behaves
  11. * like 3.5" dd (no way to test - are there any 5.25" drives out there
  12. * that work on an A4000?)
  13. * - wrote formatting routine (maybe dirty, but works)
  14. *
  15. * june/july 1995 added ms-dos support by Joerg Dorchain
  16. * (portions based on messydos.device and various contributors)
  17. * - currently only 9 and 18 sector disks
  18. *
  19. * - fixed a bug with the internal trackbuffer when using multiple
  20. * disks the same time
  21. * - made formatting a bit safer
  22. * - added command line and machine based default for "silent" df0
  23. *
  24. * december 1995 adapted for 1.2.13pl4 by Joerg Dorchain
  25. * - works but I think it's inefficient. (look in redo_fd_request)
  26. * But the changes were very efficient. (only three and a half lines)
  27. *
  28. * january 1996 added special ioctl for tracking down read/write problems
  29. * - usage ioctl(d, RAW_TRACK, ptr); the raw track buffer (MFM-encoded data
  30. * is copied to area. (area should be large enough since no checking is
  31. * done - 30K is currently sufficient). return the actual size of the
  32. * trackbuffer
  33. * - replaced udelays() by a timer (CIAA timer B) for the waits
  34. * needed for the disk mechanic.
  35. *
  36. * february 1996 fixed error recovery and multiple disk access
  37. * - both got broken the first time I tampered with the driver :-(
  38. * - still not safe, but better than before
  39. *
  40. * revised Marts 3rd, 1996 by Jes Sorensen for use in the 1.3.28 kernel.
  41. * - Minor changes to accept the kdev_t.
  42. * - Replaced some more udelays with ms_delays. Udelay is just a loop,
  43. * and so the delay will be different depending on the given
  44. * processor :-(
  45. * - The driver could use a major cleanup because of the new
  46. * major/minor handling that came with kdev_t. It seems to work for
  47. * the time being, but I can't guarantee that it will stay like
  48. * that when we start using 16 (24?) bit minors.
  49. *
  50. * restructured jan 1997 by Joerg Dorchain
  51. * - Fixed Bug accessing multiple disks
  52. * - some code cleanup
  53. * - added trackbuffer for each drive to speed things up
  54. * - fixed some race conditions (who finds the next may send it to me ;-)
  55. */
  56. #include <linux/module.h>
  57. #include <linux/slab.h>
  58. #include <linux/fd.h>
  59. #include <linux/hdreg.h>
  60. #include <linux/delay.h>
  61. #include <linux/init.h>
  62. #include <linux/mutex.h>
  63. #include <linux/fs.h>
  64. #include <linux/blk-mq.h>
  65. #include <linux/elevator.h>
  66. #include <linux/interrupt.h>
  67. #include <linux/platform_device.h>
  68. #include <asm/setup.h>
  69. #include <linux/uaccess.h>
  70. #include <asm/amigahw.h>
  71. #include <asm/amigaints.h>
  72. #include <asm/irq.h>
  73. #undef DEBUG /* print _LOTS_ of infos */
  74. #define RAW_IOCTL
  75. #ifdef RAW_IOCTL
  76. #define IOCTL_RAW_TRACK 0x5254524B /* 'RTRK' */
  77. #endif
  78. /*
  79. * Defines
  80. */
  81. /*
  82. * CIAAPRA bits (read only)
  83. */
  84. #define DSKRDY (0x1<<5) /* disk ready when low */
  85. #define DSKTRACK0 (0x1<<4) /* head at track zero when low */
  86. #define DSKPROT (0x1<<3) /* disk protected when low */
  87. #define DSKCHANGE (0x1<<2) /* low when disk removed */
  88. /*
  89. * CIAAPRB bits (read/write)
  90. */
  91. #define DSKMOTOR (0x1<<7) /* motor on when low */
  92. #define DSKSEL3 (0x1<<6) /* select drive 3 when low */
  93. #define DSKSEL2 (0x1<<5) /* select drive 2 when low */
  94. #define DSKSEL1 (0x1<<4) /* select drive 1 when low */
  95. #define DSKSEL0 (0x1<<3) /* select drive 0 when low */
  96. #define DSKSIDE (0x1<<2) /* side selection: 0 = upper, 1 = lower */
  97. #define DSKDIREC (0x1<<1) /* step direction: 0=in, 1=out (to trk 0) */
  98. #define DSKSTEP (0x1) /* pulse low to step head 1 track */
  99. /*
  100. * DSKBYTR bits (read only)
  101. */
  102. #define DSKBYT (1<<15) /* register contains valid byte when set */
  103. #define DMAON (1<<14) /* disk DMA enabled */
  104. #define DISKWRITE (1<<13) /* disk write bit in DSKLEN enabled */
  105. #define WORDEQUAL (1<<12) /* DSKSYNC register match when true */
  106. /* bits 7-0 are data */
  107. /*
  108. * ADKCON/ADKCONR bits
  109. */
  110. #ifndef SETCLR
  111. #define ADK_SETCLR (1<<15) /* control bit */
  112. #endif
  113. #define ADK_PRECOMP1 (1<<14) /* precompensation selection */
  114. #define ADK_PRECOMP0 (1<<13) /* 00=none, 01=140ns, 10=280ns, 11=500ns */
  115. #define ADK_MFMPREC (1<<12) /* 0=GCR precomp., 1=MFM precomp. */
  116. #define ADK_WORDSYNC (1<<10) /* enable DSKSYNC auto DMA */
  117. #define ADK_MSBSYNC (1<<9) /* when 1, enable sync on MSbit (for GCR) */
  118. #define ADK_FAST (1<<8) /* bit cell: 0=2us (GCR), 1=1us (MFM) */
  119. /*
  120. * DSKLEN bits
  121. */
  122. #define DSKLEN_DMAEN (1<<15)
  123. #define DSKLEN_WRITE (1<<14)
  124. /*
  125. * INTENA/INTREQ bits
  126. */
  127. #define DSKINDEX (0x1<<4) /* DSKINDEX bit */
  128. /*
  129. * Misc
  130. */
  131. #define MFM_SYNC 0x4489 /* standard MFM sync value */
  132. /* Values for FD_COMMAND */
  133. #define FD_RECALIBRATE 0x07 /* move to track 0 */
  134. #define FD_SEEK 0x0F /* seek track */
  135. #define FD_READ 0xE6 /* read with MT, MFM, SKip deleted */
  136. #define FD_WRITE 0xC5 /* write with MT, MFM */
  137. #define FD_SENSEI 0x08 /* Sense Interrupt Status */
  138. #define FD_SPECIFY 0x03 /* specify HUT etc */
  139. #define FD_FORMAT 0x4D /* format one track */
  140. #define FD_VERSION 0x10 /* get version code */
  141. #define FD_CONFIGURE 0x13 /* configure FIFO operation */
  142. #define FD_PERPENDICULAR 0x12 /* perpendicular r/w mode */
  143. #define FD_MAX_UNITS 4 /* Max. Number of drives */
  144. #define FLOPPY_MAX_SECTORS 22 /* Max. Number of sectors per track */
  145. struct fd_data_type {
  146. char *name; /* description of data type */
  147. int sects; /* sectors per track */
  148. int (*read_fkt)(int); /* read whole track */
  149. void (*write_fkt)(int); /* write whole track */
  150. };
  151. struct fd_drive_type {
  152. unsigned long code; /* code returned from drive */
  153. char *name; /* description of drive */
  154. unsigned int tracks; /* number of tracks */
  155. unsigned int heads; /* number of heads */
  156. unsigned int read_size; /* raw read size for one track */
  157. unsigned int write_size; /* raw write size for one track */
  158. unsigned int sect_mult; /* sectors and gap multiplier (HD = 2) */
  159. unsigned int precomp1; /* start track for precomp 1 */
  160. unsigned int precomp2; /* start track for precomp 2 */
  161. unsigned int step_delay; /* time (in ms) for delay after step */
  162. unsigned int settle_time; /* time to settle after dir change */
  163. unsigned int side_time; /* time needed to change sides */
  164. };
  165. struct amiga_floppy_struct {
  166. struct fd_drive_type *type; /* type of floppy for this unit */
  167. struct fd_data_type *dtype; /* type of floppy for this unit */
  168. int track; /* current track (-1 == unknown) */
  169. unsigned char *trackbuf; /* current track (kmaloc()'d */
  170. int blocks; /* total # blocks on disk */
  171. int changed; /* true when not known */
  172. int disk; /* disk in drive (-1 == unknown) */
  173. int motor; /* true when motor is at speed */
  174. int busy; /* true when drive is active */
  175. int dirty; /* true when trackbuf is not on disk */
  176. int status; /* current error code for unit */
  177. struct gendisk *gendisk;
  178. struct blk_mq_tag_set tag_set;
  179. };
  180. /*
  181. * Error codes
  182. */
  183. #define FD_OK 0 /* operation succeeded */
  184. #define FD_ERROR -1 /* general error (seek, read, write, etc) */
  185. #define FD_NOUNIT 1 /* unit does not exist */
  186. #define FD_UNITBUSY 2 /* unit already active */
  187. #define FD_NOTACTIVE 3 /* unit is not active */
  188. #define FD_NOTREADY 4 /* unit is not ready (motor not on/no disk) */
  189. #define MFM_NOSYNC 1
  190. #define MFM_HEADER 2
  191. #define MFM_DATA 3
  192. #define MFM_TRACK 4
  193. /*
  194. * Floppy ID values
  195. */
  196. #define FD_NODRIVE 0x00000000 /* response when no unit is present */
  197. #define FD_DD_3 0xffffffff /* double-density 3.5" (880K) drive */
  198. #define FD_HD_3 0x55555555 /* high-density 3.5" (1760K) drive */
  199. #define FD_DD_5 0xaaaaaaaa /* double-density 5.25" (440K) drive */
  200. static DEFINE_MUTEX(amiflop_mutex);
  201. static unsigned long int fd_def_df0 = FD_DD_3; /* default for df0 if it doesn't identify */
  202. module_param(fd_def_df0, ulong, 0);
  203. MODULE_LICENSE("GPL");
  204. /*
  205. * Macros
  206. */
  207. #define MOTOR_ON (ciab.prb &= ~DSKMOTOR)
  208. #define MOTOR_OFF (ciab.prb |= DSKMOTOR)
  209. #define SELECT(mask) (ciab.prb &= ~mask)
  210. #define DESELECT(mask) (ciab.prb |= mask)
  211. #define SELMASK(drive) (1 << (3 + (drive & 3)))
  212. static struct fd_drive_type drive_types[] = {
  213. /* code name tr he rdsz wrsz sm pc1 pc2 sd st st*/
  214. /* warning: times are now in milliseconds (ms) */
  215. { FD_DD_3, "DD 3.5", 80, 2, 14716, 13630, 1, 80,161, 3, 18, 1},
  216. { FD_HD_3, "HD 3.5", 80, 2, 28344, 27258, 2, 80,161, 3, 18, 1},
  217. { FD_DD_5, "DD 5.25", 40, 2, 14716, 13630, 1, 40, 81, 6, 30, 2},
  218. { FD_NODRIVE, "No Drive", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  219. };
  220. static int num_dr_types = ARRAY_SIZE(drive_types);
  221. static int amiga_read(int), dos_read(int);
  222. static void amiga_write(int), dos_write(int);
  223. static struct fd_data_type data_types[] = {
  224. { "Amiga", 11 , amiga_read, amiga_write},
  225. { "MS-Dos", 9, dos_read, dos_write}
  226. };
  227. /* current info on each unit */
  228. static struct amiga_floppy_struct unit[FD_MAX_UNITS];
  229. static struct timer_list flush_track_timer[FD_MAX_UNITS];
  230. static struct timer_list post_write_timer;
  231. static unsigned long post_write_timer_drive;
  232. static struct timer_list motor_on_timer;
  233. static struct timer_list motor_off_timer[FD_MAX_UNITS];
  234. static int on_attempts;
  235. /* Synchronization of FDC access */
  236. /* request loop (trackbuffer) */
  237. static volatile int fdc_busy = -1;
  238. static volatile int fdc_nested;
  239. static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
  240. static DECLARE_COMPLETION(motor_on_completion);
  241. static volatile int selected = -1; /* currently selected drive */
  242. static int writepending;
  243. static int writefromint;
  244. static char *raw_buf;
  245. static DEFINE_SPINLOCK(amiflop_lock);
  246. #define RAW_BUF_SIZE 30000 /* size of raw disk data */
  247. /*
  248. * These are global variables, as that's the easiest way to give
  249. * information to interrupts. They are the data used for the current
  250. * request.
  251. */
  252. static volatile char block_flag;
  253. static DECLARE_WAIT_QUEUE_HEAD(wait_fd_block);
  254. /* MS-Dos MFM Coding tables (should go quick and easy) */
  255. static unsigned char mfmencode[16]={
  256. 0x2a, 0x29, 0x24, 0x25, 0x12, 0x11, 0x14, 0x15,
  257. 0x4a, 0x49, 0x44, 0x45, 0x52, 0x51, 0x54, 0x55
  258. };
  259. static unsigned char mfmdecode[128];
  260. /* floppy internal millisecond timer stuff */
  261. static DECLARE_COMPLETION(ms_wait_completion);
  262. #define MS_TICKS ((amiga_eclock+50)/1000)
  263. /*
  264. * Note that MAX_ERRORS=X doesn't imply that we retry every bad read
  265. * max X times - some types of errors increase the errorcount by 2 or
  266. * even 3, so we might actually retry only X/2 times before giving up.
  267. */
  268. #define MAX_ERRORS 12
  269. #define custom amiga_custom
  270. /* Prevent "aliased" accesses. */
  271. static int fd_ref[4] = { 0,0,0,0 };
  272. static int fd_device[4] = { 0, 0, 0, 0 };
  273. /*
  274. * Here come the actual hardware access and helper functions.
  275. * They are not reentrant and single threaded because all drives
  276. * share the same hardware and the same trackbuffer.
  277. */
  278. /* Milliseconds timer */
  279. static irqreturn_t ms_isr(int irq, void *dummy)
  280. {
  281. complete(&ms_wait_completion);
  282. return IRQ_HANDLED;
  283. }
  284. /* all waits are queued up
  285. A more generic routine would do a schedule a la timer.device */
  286. static void ms_delay(int ms)
  287. {
  288. int ticks;
  289. static DEFINE_MUTEX(mutex);
  290. if (ms > 0) {
  291. mutex_lock(&mutex);
  292. ticks = MS_TICKS*ms-1;
  293. ciaa.tblo=ticks%256;
  294. ciaa.tbhi=ticks/256;
  295. ciaa.crb=0x19; /*count eclock, force load, one-shoot, start */
  296. wait_for_completion(&ms_wait_completion);
  297. mutex_unlock(&mutex);
  298. }
  299. }
  300. /* Hardware semaphore */
  301. /* returns true when we would get the semaphore */
  302. static inline int try_fdc(int drive)
  303. {
  304. drive &= 3;
  305. return ((fdc_busy < 0) || (fdc_busy == drive));
  306. }
  307. static void get_fdc(int drive)
  308. {
  309. unsigned long flags;
  310. drive &= 3;
  311. #ifdef DEBUG
  312. printk("get_fdc: drive %d fdc_busy %d fdc_nested %d\n",drive,fdc_busy,fdc_nested);
  313. #endif
  314. local_irq_save(flags);
  315. wait_event(fdc_wait, try_fdc(drive));
  316. fdc_busy = drive;
  317. fdc_nested++;
  318. local_irq_restore(flags);
  319. }
  320. static inline void rel_fdc(void)
  321. {
  322. #ifdef DEBUG
  323. if (fdc_nested == 0)
  324. printk("fd: unmatched rel_fdc\n");
  325. printk("rel_fdc: fdc_busy %d fdc_nested %d\n",fdc_busy,fdc_nested);
  326. #endif
  327. fdc_nested--;
  328. if (fdc_nested == 0) {
  329. fdc_busy = -1;
  330. wake_up(&fdc_wait);
  331. }
  332. }
  333. static void fd_select (int drive)
  334. {
  335. unsigned char prb = ~0;
  336. drive&=3;
  337. #ifdef DEBUG
  338. printk("selecting %d\n",drive);
  339. #endif
  340. if (drive == selected)
  341. return;
  342. get_fdc(drive);
  343. selected = drive;
  344. if (unit[drive].track % 2 != 0)
  345. prb &= ~DSKSIDE;
  346. if (unit[drive].motor == 1)
  347. prb &= ~DSKMOTOR;
  348. ciab.prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
  349. ciab.prb = prb;
  350. prb &= ~SELMASK(drive);
  351. ciab.prb = prb;
  352. rel_fdc();
  353. }
  354. static void fd_deselect (int drive)
  355. {
  356. unsigned char prb;
  357. unsigned long flags;
  358. drive&=3;
  359. #ifdef DEBUG
  360. printk("deselecting %d\n",drive);
  361. #endif
  362. if (drive != selected) {
  363. printk(KERN_WARNING "Deselecting drive %d while %d was selected!\n",drive,selected);
  364. return;
  365. }
  366. get_fdc(drive);
  367. local_irq_save(flags);
  368. selected = -1;
  369. prb = ciab.prb;
  370. prb |= (SELMASK(0)|SELMASK(1)|SELMASK(2)|SELMASK(3));
  371. ciab.prb = prb;
  372. local_irq_restore (flags);
  373. rel_fdc();
  374. }
  375. static void motor_on_callback(struct timer_list *unused)
  376. {
  377. if (!(ciaa.pra & DSKRDY) || --on_attempts == 0) {
  378. complete_all(&motor_on_completion);
  379. } else {
  380. motor_on_timer.expires = jiffies + HZ/10;
  381. add_timer(&motor_on_timer);
  382. }
  383. }
  384. static int fd_motor_on(int nr)
  385. {
  386. nr &= 3;
  387. del_timer(motor_off_timer + nr);
  388. if (!unit[nr].motor) {
  389. unit[nr].motor = 1;
  390. fd_select(nr);
  391. reinit_completion(&motor_on_completion);
  392. mod_timer(&motor_on_timer, jiffies + HZ/2);
  393. on_attempts = 10;
  394. wait_for_completion(&motor_on_completion);
  395. fd_deselect(nr);
  396. }
  397. if (on_attempts == 0) {
  398. on_attempts = -1;
  399. #if 0
  400. printk (KERN_ERR "motor_on failed, turning motor off\n");
  401. fd_motor_off (motor_off_timer + nr);
  402. return 0;
  403. #else
  404. printk (KERN_WARNING "DSKRDY not set after 1.5 seconds - assuming drive is spinning notwithstanding\n");
  405. #endif
  406. }
  407. return 1;
  408. }
  409. static void fd_motor_off(struct timer_list *timer)
  410. {
  411. unsigned long drive = ((unsigned long)timer -
  412. (unsigned long)&motor_off_timer[0]) /
  413. sizeof(motor_off_timer[0]);
  414. drive&=3;
  415. if (!try_fdc(drive)) {
  416. /* We would be blocked in an interrupt, so try again later */
  417. timer->expires = jiffies + 1;
  418. add_timer(timer);
  419. return;
  420. }
  421. unit[drive].motor = 0;
  422. fd_select(drive);
  423. udelay (1);
  424. fd_deselect(drive);
  425. }
  426. static void floppy_off (unsigned int nr)
  427. {
  428. int drive;
  429. drive = nr & 3;
  430. mod_timer(motor_off_timer + drive, jiffies + 3*HZ);
  431. }
  432. static int fd_calibrate(int drive)
  433. {
  434. unsigned char prb;
  435. int n;
  436. drive &= 3;
  437. get_fdc(drive);
  438. if (!fd_motor_on (drive))
  439. return 0;
  440. fd_select (drive);
  441. prb = ciab.prb;
  442. prb |= DSKSIDE;
  443. prb &= ~DSKDIREC;
  444. ciab.prb = prb;
  445. for (n = unit[drive].type->tracks/2; n != 0; --n) {
  446. if (ciaa.pra & DSKTRACK0)
  447. break;
  448. prb &= ~DSKSTEP;
  449. ciab.prb = prb;
  450. prb |= DSKSTEP;
  451. udelay (2);
  452. ciab.prb = prb;
  453. ms_delay(unit[drive].type->step_delay);
  454. }
  455. ms_delay (unit[drive].type->settle_time);
  456. prb |= DSKDIREC;
  457. n = unit[drive].type->tracks + 20;
  458. for (;;) {
  459. prb &= ~DSKSTEP;
  460. ciab.prb = prb;
  461. prb |= DSKSTEP;
  462. udelay (2);
  463. ciab.prb = prb;
  464. ms_delay(unit[drive].type->step_delay + 1);
  465. if ((ciaa.pra & DSKTRACK0) == 0)
  466. break;
  467. if (--n == 0) {
  468. printk (KERN_ERR "fd%d: calibrate failed, turning motor off\n", drive);
  469. fd_motor_off (motor_off_timer + drive);
  470. unit[drive].track = -1;
  471. rel_fdc();
  472. return 0;
  473. }
  474. }
  475. unit[drive].track = 0;
  476. ms_delay(unit[drive].type->settle_time);
  477. rel_fdc();
  478. fd_deselect(drive);
  479. return 1;
  480. }
  481. static int fd_seek(int drive, int track)
  482. {
  483. unsigned char prb;
  484. int cnt;
  485. #ifdef DEBUG
  486. printk("seeking drive %d to track %d\n",drive,track);
  487. #endif
  488. drive &= 3;
  489. get_fdc(drive);
  490. if (unit[drive].track == track) {
  491. rel_fdc();
  492. return 1;
  493. }
  494. if (!fd_motor_on(drive)) {
  495. rel_fdc();
  496. return 0;
  497. }
  498. if (unit[drive].track < 0 && !fd_calibrate(drive)) {
  499. rel_fdc();
  500. return 0;
  501. }
  502. fd_select (drive);
  503. cnt = unit[drive].track/2 - track/2;
  504. prb = ciab.prb;
  505. prb |= DSKSIDE | DSKDIREC;
  506. if (track % 2 != 0)
  507. prb &= ~DSKSIDE;
  508. if (cnt < 0) {
  509. cnt = - cnt;
  510. prb &= ~DSKDIREC;
  511. }
  512. ciab.prb = prb;
  513. if (track % 2 != unit[drive].track % 2)
  514. ms_delay (unit[drive].type->side_time);
  515. unit[drive].track = track;
  516. if (cnt == 0) {
  517. rel_fdc();
  518. fd_deselect(drive);
  519. return 1;
  520. }
  521. do {
  522. prb &= ~DSKSTEP;
  523. ciab.prb = prb;
  524. prb |= DSKSTEP;
  525. udelay (1);
  526. ciab.prb = prb;
  527. ms_delay (unit[drive].type->step_delay);
  528. } while (--cnt != 0);
  529. ms_delay (unit[drive].type->settle_time);
  530. rel_fdc();
  531. fd_deselect(drive);
  532. return 1;
  533. }
  534. static unsigned long fd_get_drive_id(int drive)
  535. {
  536. int i;
  537. ulong id = 0;
  538. drive&=3;
  539. get_fdc(drive);
  540. /* set up for ID */
  541. MOTOR_ON;
  542. udelay(2);
  543. SELECT(SELMASK(drive));
  544. udelay(2);
  545. DESELECT(SELMASK(drive));
  546. udelay(2);
  547. MOTOR_OFF;
  548. udelay(2);
  549. SELECT(SELMASK(drive));
  550. udelay(2);
  551. DESELECT(SELMASK(drive));
  552. udelay(2);
  553. /* loop and read disk ID */
  554. for (i=0; i<32; i++) {
  555. SELECT(SELMASK(drive));
  556. udelay(2);
  557. /* read and store value of DSKRDY */
  558. id <<= 1;
  559. id |= (ciaa.pra & DSKRDY) ? 0 : 1; /* cia regs are low-active! */
  560. DESELECT(SELMASK(drive));
  561. }
  562. rel_fdc();
  563. /*
  564. * RB: At least A500/A2000's df0: don't identify themselves.
  565. * As every (real) Amiga has at least a 3.5" DD drive as df0:
  566. * we default to that if df0: doesn't identify as a certain
  567. * type.
  568. */
  569. if(drive == 0 && id == FD_NODRIVE)
  570. {
  571. id = fd_def_df0;
  572. printk(KERN_NOTICE "fd: drive 0 didn't identify, setting default %08lx\n", (ulong)fd_def_df0);
  573. }
  574. /* return the ID value */
  575. return (id);
  576. }
  577. static irqreturn_t fd_block_done(int irq, void *dummy)
  578. {
  579. if (block_flag)
  580. custom.dsklen = 0x4000;
  581. if (block_flag == 2) { /* writing */
  582. writepending = 2;
  583. post_write_timer.expires = jiffies + 1; /* at least 2 ms */
  584. post_write_timer_drive = selected;
  585. add_timer(&post_write_timer);
  586. }
  587. else { /* reading */
  588. block_flag = 0;
  589. wake_up (&wait_fd_block);
  590. }
  591. return IRQ_HANDLED;
  592. }
  593. static void raw_read(int drive)
  594. {
  595. drive&=3;
  596. get_fdc(drive);
  597. wait_event(wait_fd_block, !block_flag);
  598. fd_select(drive);
  599. /* setup adkcon bits correctly */
  600. custom.adkcon = ADK_MSBSYNC;
  601. custom.adkcon = ADK_SETCLR|ADK_WORDSYNC|ADK_FAST;
  602. custom.dsksync = MFM_SYNC;
  603. custom.dsklen = 0;
  604. custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
  605. custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
  606. custom.dsklen = unit[drive].type->read_size/sizeof(short) | DSKLEN_DMAEN;
  607. block_flag = 1;
  608. wait_event(wait_fd_block, !block_flag);
  609. custom.dsklen = 0;
  610. fd_deselect(drive);
  611. rel_fdc();
  612. }
  613. static int raw_write(int drive)
  614. {
  615. ushort adk;
  616. drive&=3;
  617. get_fdc(drive); /* corresponds to rel_fdc() in post_write() */
  618. if ((ciaa.pra & DSKPROT) == 0) {
  619. rel_fdc();
  620. return 0;
  621. }
  622. wait_event(wait_fd_block, !block_flag);
  623. fd_select(drive);
  624. /* clear adkcon bits */
  625. custom.adkcon = ADK_PRECOMP1|ADK_PRECOMP0|ADK_WORDSYNC|ADK_MSBSYNC;
  626. /* set appropriate adkcon bits */
  627. adk = ADK_SETCLR|ADK_FAST;
  628. if ((ulong)unit[drive].track >= unit[drive].type->precomp2)
  629. adk |= ADK_PRECOMP1;
  630. else if ((ulong)unit[drive].track >= unit[drive].type->precomp1)
  631. adk |= ADK_PRECOMP0;
  632. custom.adkcon = adk;
  633. custom.dsklen = DSKLEN_WRITE;
  634. custom.dskptr = (u_char *)ZTWO_PADDR((u_char *)raw_buf);
  635. custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
  636. custom.dsklen = unit[drive].type->write_size/sizeof(short) | DSKLEN_DMAEN|DSKLEN_WRITE;
  637. block_flag = 2;
  638. return 1;
  639. }
  640. /*
  641. * to be called at least 2ms after the write has finished but before any
  642. * other access to the hardware.
  643. */
  644. static void post_write (unsigned long drive)
  645. {
  646. #ifdef DEBUG
  647. printk("post_write for drive %ld\n",drive);
  648. #endif
  649. drive &= 3;
  650. custom.dsklen = 0;
  651. block_flag = 0;
  652. writepending = 0;
  653. writefromint = 0;
  654. unit[drive].dirty = 0;
  655. wake_up(&wait_fd_block);
  656. fd_deselect(drive);
  657. rel_fdc(); /* corresponds to get_fdc() in raw_write */
  658. }
  659. static void post_write_callback(struct timer_list *timer)
  660. {
  661. post_write(post_write_timer_drive);
  662. }
  663. /*
  664. * The following functions are to convert the block contents into raw data
  665. * written to disk and vice versa.
  666. * (Add other formats here ;-))
  667. */
  668. static unsigned long scan_sync(unsigned long raw, unsigned long end)
  669. {
  670. ushort *ptr = (ushort *)raw, *endp = (ushort *)end;
  671. while (ptr < endp && *ptr++ != 0x4489)
  672. ;
  673. if (ptr < endp) {
  674. while (*ptr == 0x4489 && ptr < endp)
  675. ptr++;
  676. return (ulong)ptr;
  677. }
  678. return 0;
  679. }
  680. static inline unsigned long checksum(unsigned long *addr, int len)
  681. {
  682. unsigned long csum = 0;
  683. len /= sizeof(*addr);
  684. while (len-- > 0)
  685. csum ^= *addr++;
  686. csum = ((csum>>1) & 0x55555555) ^ (csum & 0x55555555);
  687. return csum;
  688. }
  689. static unsigned long decode (unsigned long *data, unsigned long *raw,
  690. int len)
  691. {
  692. ulong *odd, *even;
  693. /* convert length from bytes to longwords */
  694. len >>= 2;
  695. odd = raw;
  696. even = odd + len;
  697. /* prepare return pointer */
  698. raw += len * 2;
  699. do {
  700. *data++ = ((*odd++ & 0x55555555) << 1) | (*even++ & 0x55555555);
  701. } while (--len != 0);
  702. return (ulong)raw;
  703. }
  704. struct header {
  705. unsigned char magic;
  706. unsigned char track;
  707. unsigned char sect;
  708. unsigned char ord;
  709. unsigned char labels[16];
  710. unsigned long hdrchk;
  711. unsigned long datachk;
  712. };
  713. static int amiga_read(int drive)
  714. {
  715. unsigned long raw;
  716. unsigned long end;
  717. int scnt;
  718. unsigned long csum;
  719. struct header hdr;
  720. drive&=3;
  721. raw = (long) raw_buf;
  722. end = raw + unit[drive].type->read_size;
  723. for (scnt = 0;scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
  724. if (!(raw = scan_sync(raw, end))) {
  725. printk (KERN_INFO "can't find sync for sector %d\n", scnt);
  726. return MFM_NOSYNC;
  727. }
  728. raw = decode ((ulong *)&hdr.magic, (ulong *)raw, 4);
  729. raw = decode ((ulong *)&hdr.labels, (ulong *)raw, 16);
  730. raw = decode ((ulong *)&hdr.hdrchk, (ulong *)raw, 4);
  731. raw = decode ((ulong *)&hdr.datachk, (ulong *)raw, 4);
  732. csum = checksum((ulong *)&hdr,
  733. (char *)&hdr.hdrchk-(char *)&hdr);
  734. #ifdef DEBUG
  735. printk ("(%x,%d,%d,%d) (%lx,%lx,%lx,%lx) %lx %lx\n",
  736. hdr.magic, hdr.track, hdr.sect, hdr.ord,
  737. *(ulong *)&hdr.labels[0], *(ulong *)&hdr.labels[4],
  738. *(ulong *)&hdr.labels[8], *(ulong *)&hdr.labels[12],
  739. hdr.hdrchk, hdr.datachk);
  740. #endif
  741. if (hdr.hdrchk != csum) {
  742. printk(KERN_INFO "MFM_HEADER: %08lx,%08lx\n", hdr.hdrchk, csum);
  743. return MFM_HEADER;
  744. }
  745. /* verify track */
  746. if (hdr.track != unit[drive].track) {
  747. printk(KERN_INFO "MFM_TRACK: %d, %d\n", hdr.track, unit[drive].track);
  748. return MFM_TRACK;
  749. }
  750. raw = decode ((ulong *)(unit[drive].trackbuf + hdr.sect*512),
  751. (ulong *)raw, 512);
  752. csum = checksum((ulong *)(unit[drive].trackbuf + hdr.sect*512), 512);
  753. if (hdr.datachk != csum) {
  754. printk(KERN_INFO "MFM_DATA: (%x:%d:%d:%d) sc=%d %lx, %lx\n",
  755. hdr.magic, hdr.track, hdr.sect, hdr.ord, scnt,
  756. hdr.datachk, csum);
  757. printk (KERN_INFO "data=(%lx,%lx,%lx,%lx)\n",
  758. ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[0],
  759. ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[1],
  760. ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[2],
  761. ((ulong *)(unit[drive].trackbuf+hdr.sect*512))[3]);
  762. return MFM_DATA;
  763. }
  764. }
  765. return 0;
  766. }
  767. static void encode(unsigned long data, unsigned long *dest)
  768. {
  769. unsigned long data2;
  770. data &= 0x55555555;
  771. data2 = data ^ 0x55555555;
  772. data |= ((data2 >> 1) | 0x80000000) & (data2 << 1);
  773. if (*(dest - 1) & 0x00000001)
  774. data &= 0x7FFFFFFF;
  775. *dest = data;
  776. }
  777. static void encode_block(unsigned long *dest, unsigned long *src, int len)
  778. {
  779. int cnt, to_cnt = 0;
  780. unsigned long data;
  781. /* odd bits */
  782. for (cnt = 0; cnt < len / 4; cnt++) {
  783. data = src[cnt] >> 1;
  784. encode(data, dest + to_cnt++);
  785. }
  786. /* even bits */
  787. for (cnt = 0; cnt < len / 4; cnt++) {
  788. data = src[cnt];
  789. encode(data, dest + to_cnt++);
  790. }
  791. }
  792. static unsigned long *putsec(int disk, unsigned long *raw, int cnt)
  793. {
  794. struct header hdr;
  795. int i;
  796. disk&=3;
  797. *raw = (raw[-1]&1) ? 0x2AAAAAAA : 0xAAAAAAAA;
  798. raw++;
  799. *raw++ = 0x44894489;
  800. hdr.magic = 0xFF;
  801. hdr.track = unit[disk].track;
  802. hdr.sect = cnt;
  803. hdr.ord = unit[disk].dtype->sects * unit[disk].type->sect_mult - cnt;
  804. for (i = 0; i < 16; i++)
  805. hdr.labels[i] = 0;
  806. hdr.hdrchk = checksum((ulong *)&hdr,
  807. (char *)&hdr.hdrchk-(char *)&hdr);
  808. hdr.datachk = checksum((ulong *)(unit[disk].trackbuf+cnt*512), 512);
  809. encode_block(raw, (ulong *)&hdr.magic, 4);
  810. raw += 2;
  811. encode_block(raw, (ulong *)&hdr.labels, 16);
  812. raw += 8;
  813. encode_block(raw, (ulong *)&hdr.hdrchk, 4);
  814. raw += 2;
  815. encode_block(raw, (ulong *)&hdr.datachk, 4);
  816. raw += 2;
  817. encode_block(raw, (ulong *)(unit[disk].trackbuf+cnt*512), 512);
  818. raw += 256;
  819. return raw;
  820. }
  821. static void amiga_write(int disk)
  822. {
  823. unsigned int cnt;
  824. unsigned long *ptr = (unsigned long *)raw_buf;
  825. disk&=3;
  826. /* gap space */
  827. for (cnt = 0; cnt < 415 * unit[disk].type->sect_mult; cnt++)
  828. *ptr++ = 0xaaaaaaaa;
  829. /* sectors */
  830. for (cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
  831. ptr = putsec (disk, ptr, cnt);
  832. *(ushort *)ptr = (ptr[-1]&1) ? 0x2AA8 : 0xAAA8;
  833. }
  834. struct dos_header {
  835. unsigned char track, /* 0-80 */
  836. side, /* 0-1 */
  837. sec, /* 0-...*/
  838. len_desc;/* 2 */
  839. unsigned short crc; /* on 68000 we got an alignment problem,
  840. but this compiler solves it by adding silently
  841. adding a pad byte so data won't fit
  842. and this took about 3h to discover.... */
  843. unsigned char gap1[22]; /* for longword-alignedness (0x4e) */
  844. };
  845. /* crc routines are borrowed from the messydos-handler */
  846. /* excerpt from the messydos-device
  847. ; The CRC is computed not only over the actual data, but including
  848. ; the SYNC mark (3 * $a1) and the 'ID/DATA - Address Mark' ($fe/$fb).
  849. ; As we don't read or encode these fields into our buffers, we have to
  850. ; preload the registers containing the CRC with the values they would have
  851. ; after stepping over these fields.
  852. ;
  853. ; How CRCs "really" work:
  854. ;
  855. ; First, you should regard a bitstring as a series of coefficients of
  856. ; polynomials. We calculate with these polynomials in modulo-2
  857. ; arithmetic, in which both add and subtract are done the same as
  858. ; exclusive-or. Now, we modify our data (a very long polynomial) in
  859. ; such a way that it becomes divisible by the CCITT-standard 16-bit
  860. ; 16 12 5
  861. ; polynomial: x + x + x + 1, represented by $11021. The easiest
  862. ; way to do this would be to multiply (using proper arithmetic) our
  863. ; datablock with $11021. So we have:
  864. ; data * $11021 =
  865. ; data * ($10000 + $1021) =
  866. ; data * $10000 + data * $1021
  867. ; The left part of this is simple: Just add two 0 bytes. But then
  868. ; the right part (data $1021) remains difficult and even could have
  869. ; a carry into the left part. The solution is to use a modified
  870. ; multiplication, which has a result that is not correct, but with
  871. ; a difference of any multiple of $11021. We then only need to keep
  872. ; the 16 least significant bits of the result.
  873. ;
  874. ; The following algorithm does this for us:
  875. ;
  876. ; unsigned char *data, c, crclo, crchi;
  877. ; while (not done) {
  878. ; c = *data++ + crchi;
  879. ; crchi = (@ c) >> 8 + crclo;
  880. ; crclo = @ c;
  881. ; }
  882. ;
  883. ; Remember, + is done with EOR, the @ operator is in two tables (high
  884. ; and low byte separately), which is calculated as
  885. ;
  886. ; $1021 * (c & $F0)
  887. ; xor $1021 * (c & $0F)
  888. ; xor $1021 * (c >> 4) (* is regular multiplication)
  889. ;
  890. ;
  891. ; Anyway, the end result is the same as the remainder of the division of
  892. ; the data by $11021. I am afraid I need to study theory a bit more...
  893. my only works was to code this from manx to C....
  894. */
  895. static ushort dos_crc(void * data_a3, int data_d0, int data_d1, int data_d3)
  896. {
  897. static unsigned char CRCTable1[] = {
  898. 0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1,
  899. 0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62,0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3,
  900. 0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54,0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5,
  901. 0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46,0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7,
  902. 0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38,0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9,
  903. 0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a,0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab,
  904. 0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c,0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d,
  905. 0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e,0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,
  906. 0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1,0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60,
  907. 0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3,0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72,
  908. 0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5,0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44,
  909. 0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7,0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56,
  910. 0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9,0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28,
  911. 0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb,0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a,
  912. 0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d,0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c,
  913. 0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f,0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e
  914. };
  915. static unsigned char CRCTable2[] = {
  916. 0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7,0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef,
  917. 0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6,0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde,
  918. 0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85,0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d,
  919. 0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4,0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc,
  920. 0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23,0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b,
  921. 0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12,0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a,
  922. 0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41,0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49,
  923. 0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70,0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78,
  924. 0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f,0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67,
  925. 0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e,0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56,
  926. 0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d,0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05,
  927. 0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c,0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34,
  928. 0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab,0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3,
  929. 0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a,0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92,
  930. 0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9,0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1,
  931. 0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8,0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0
  932. };
  933. /* look at the asm-code - what looks in C a bit strange is almost as good as handmade */
  934. register int i;
  935. register unsigned char *CRCT1, *CRCT2, *data, c, crch, crcl;
  936. CRCT1=CRCTable1;
  937. CRCT2=CRCTable2;
  938. data=data_a3;
  939. crcl=data_d1;
  940. crch=data_d0;
  941. for (i=data_d3; i>=0; i--) {
  942. c = (*data++) ^ crch;
  943. crch = CRCT1[c] ^ crcl;
  944. crcl = CRCT2[c];
  945. }
  946. return (crch<<8)|crcl;
  947. }
  948. static inline ushort dos_hdr_crc (struct dos_header *hdr)
  949. {
  950. return dos_crc(&(hdr->track), 0xb2, 0x30, 3); /* precomputed magic */
  951. }
  952. static inline ushort dos_data_crc(unsigned char *data)
  953. {
  954. return dos_crc(data, 0xe2, 0x95 ,511); /* precomputed magic */
  955. }
  956. static inline unsigned char dos_decode_byte(ushort word)
  957. {
  958. register ushort w2;
  959. register unsigned char byte;
  960. register unsigned char *dec = mfmdecode;
  961. w2=word;
  962. w2>>=8;
  963. w2&=127;
  964. byte = dec[w2];
  965. byte <<= 4;
  966. w2 = word & 127;
  967. byte |= dec[w2];
  968. return byte;
  969. }
  970. static unsigned long dos_decode(unsigned char *data, unsigned short *raw, int len)
  971. {
  972. int i;
  973. for (i = 0; i < len; i++)
  974. *data++=dos_decode_byte(*raw++);
  975. return ((ulong)raw);
  976. }
  977. #ifdef DEBUG
  978. static void dbg(unsigned long ptr)
  979. {
  980. printk("raw data @%08lx: %08lx, %08lx ,%08lx, %08lx\n", ptr,
  981. ((ulong *)ptr)[0], ((ulong *)ptr)[1],
  982. ((ulong *)ptr)[2], ((ulong *)ptr)[3]);
  983. }
  984. #endif
  985. static int dos_read(int drive)
  986. {
  987. unsigned long end;
  988. unsigned long raw;
  989. int scnt;
  990. unsigned short crc,data_crc[2];
  991. struct dos_header hdr;
  992. drive&=3;
  993. raw = (long) raw_buf;
  994. end = raw + unit[drive].type->read_size;
  995. for (scnt=0; scnt < unit[drive].dtype->sects * unit[drive].type->sect_mult; scnt++) {
  996. do { /* search for the right sync of each sec-hdr */
  997. if (!(raw = scan_sync (raw, end))) {
  998. printk(KERN_INFO "dos_read: no hdr sync on "
  999. "track %d, unit %d for sector %d\n",
  1000. unit[drive].track,drive,scnt);
  1001. return MFM_NOSYNC;
  1002. }
  1003. #ifdef DEBUG
  1004. dbg(raw);
  1005. #endif
  1006. } while (*((ushort *)raw)!=0x5554); /* loop usually only once done */
  1007. raw+=2; /* skip over headermark */
  1008. raw = dos_decode((unsigned char *)&hdr,(ushort *) raw,8);
  1009. crc = dos_hdr_crc(&hdr);
  1010. #ifdef DEBUG
  1011. printk("(%3d,%d,%2d,%d) %x\n", hdr.track, hdr.side,
  1012. hdr.sec, hdr.len_desc, hdr.crc);
  1013. #endif
  1014. if (crc != hdr.crc) {
  1015. printk(KERN_INFO "dos_read: MFM_HEADER %04x,%04x\n",
  1016. hdr.crc, crc);
  1017. return MFM_HEADER;
  1018. }
  1019. if (hdr.track != unit[drive].track/unit[drive].type->heads) {
  1020. printk(KERN_INFO "dos_read: MFM_TRACK %d, %d\n",
  1021. hdr.track,
  1022. unit[drive].track/unit[drive].type->heads);
  1023. return MFM_TRACK;
  1024. }
  1025. if (hdr.side != unit[drive].track%unit[drive].type->heads) {
  1026. printk(KERN_INFO "dos_read: MFM_SIDE %d, %d\n",
  1027. hdr.side,
  1028. unit[drive].track%unit[drive].type->heads);
  1029. return MFM_TRACK;
  1030. }
  1031. if (hdr.len_desc != 2) {
  1032. printk(KERN_INFO "dos_read: unknown sector len "
  1033. "descriptor %d\n", hdr.len_desc);
  1034. return MFM_DATA;
  1035. }
  1036. #ifdef DEBUG
  1037. printk("hdr accepted\n");
  1038. #endif
  1039. if (!(raw = scan_sync (raw, end))) {
  1040. printk(KERN_INFO "dos_read: no data sync on track "
  1041. "%d, unit %d for sector%d, disk sector %d\n",
  1042. unit[drive].track, drive, scnt, hdr.sec);
  1043. return MFM_NOSYNC;
  1044. }
  1045. #ifdef DEBUG
  1046. dbg(raw);
  1047. #endif
  1048. if (*((ushort *)raw)!=0x5545) {
  1049. printk(KERN_INFO "dos_read: no data mark after "
  1050. "sync (%d,%d,%d,%d) sc=%d\n",
  1051. hdr.track,hdr.side,hdr.sec,hdr.len_desc,scnt);
  1052. return MFM_NOSYNC;
  1053. }
  1054. raw+=2; /* skip data mark (included in checksum) */
  1055. raw = dos_decode((unsigned char *)(unit[drive].trackbuf + (hdr.sec - 1) * 512), (ushort *) raw, 512);
  1056. raw = dos_decode((unsigned char *)data_crc,(ushort *) raw,4);
  1057. crc = dos_data_crc(unit[drive].trackbuf + (hdr.sec - 1) * 512);
  1058. if (crc != data_crc[0]) {
  1059. printk(KERN_INFO "dos_read: MFM_DATA (%d,%d,%d,%d) "
  1060. "sc=%d, %x %x\n", hdr.track, hdr.side,
  1061. hdr.sec, hdr.len_desc, scnt,data_crc[0], crc);
  1062. printk(KERN_INFO "data=(%lx,%lx,%lx,%lx,...)\n",
  1063. ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[0],
  1064. ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[1],
  1065. ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[2],
  1066. ((ulong *)(unit[drive].trackbuf+(hdr.sec-1)*512))[3]);
  1067. return MFM_DATA;
  1068. }
  1069. }
  1070. return 0;
  1071. }
  1072. static inline ushort dos_encode_byte(unsigned char byte)
  1073. {
  1074. register unsigned char *enc, b2, b1;
  1075. register ushort word;
  1076. enc=mfmencode;
  1077. b1=byte;
  1078. b2=b1>>4;
  1079. b1&=15;
  1080. word=enc[b2] <<8 | enc [b1];
  1081. return (word|((word&(256|64)) ? 0: 128));
  1082. }
  1083. static void dos_encode_block(ushort *dest, unsigned char *src, int len)
  1084. {
  1085. int i;
  1086. for (i = 0; i < len; i++) {
  1087. *dest=dos_encode_byte(*src++);
  1088. *dest|=((dest[-1]&1)||(*dest&0x4000))? 0: 0x8000;
  1089. dest++;
  1090. }
  1091. }
  1092. static unsigned long *ms_putsec(int drive, unsigned long *raw, int cnt)
  1093. {
  1094. static struct dos_header hdr={0,0,0,2,0,
  1095. {78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78}};
  1096. int i;
  1097. static ushort crc[2]={0,0x4e4e};
  1098. drive&=3;
  1099. /* id gap 1 */
  1100. /* the MFM word before is always 9254 */
  1101. for(i=0;i<6;i++)
  1102. *raw++=0xaaaaaaaa;
  1103. /* 3 sync + 1 headermark */
  1104. *raw++=0x44894489;
  1105. *raw++=0x44895554;
  1106. /* fill in the variable parts of the header */
  1107. hdr.track=unit[drive].track/unit[drive].type->heads;
  1108. hdr.side=unit[drive].track%unit[drive].type->heads;
  1109. hdr.sec=cnt+1;
  1110. hdr.crc=dos_hdr_crc(&hdr);
  1111. /* header (without "magic") and id gap 2*/
  1112. dos_encode_block((ushort *)raw,(unsigned char *) &hdr.track,28);
  1113. raw+=14;
  1114. /*id gap 3 */
  1115. for(i=0;i<6;i++)
  1116. *raw++=0xaaaaaaaa;
  1117. /* 3 syncs and 1 datamark */
  1118. *raw++=0x44894489;
  1119. *raw++=0x44895545;
  1120. /* data */
  1121. dos_encode_block((ushort *)raw,
  1122. (unsigned char *)unit[drive].trackbuf+cnt*512,512);
  1123. raw+=256;
  1124. /*data crc + jd's special gap (long words :-/) */
  1125. crc[0]=dos_data_crc(unit[drive].trackbuf+cnt*512);
  1126. dos_encode_block((ushort *) raw,(unsigned char *)crc,4);
  1127. raw+=2;
  1128. /* data gap */
  1129. for(i=0;i<38;i++)
  1130. *raw++=0x92549254;
  1131. return raw; /* wrote 652 MFM words */
  1132. }
  1133. static void dos_write(int disk)
  1134. {
  1135. int cnt;
  1136. unsigned long raw = (unsigned long) raw_buf;
  1137. unsigned long *ptr=(unsigned long *)raw;
  1138. disk&=3;
  1139. /* really gap4 + indexgap , but we write it first and round it up */
  1140. for (cnt=0;cnt<425;cnt++)
  1141. *ptr++=0x92549254;
  1142. /* the following is just guessed */
  1143. if (unit[disk].type->sect_mult==2) /* check for HD-Disks */
  1144. for(cnt=0;cnt<473;cnt++)
  1145. *ptr++=0x92549254;
  1146. /* now the index marks...*/
  1147. for (cnt=0;cnt<20;cnt++)
  1148. *ptr++=0x92549254;
  1149. for (cnt=0;cnt<6;cnt++)
  1150. *ptr++=0xaaaaaaaa;
  1151. *ptr++=0x52245224;
  1152. *ptr++=0x52245552;
  1153. for (cnt=0;cnt<20;cnt++)
  1154. *ptr++=0x92549254;
  1155. /* sectors */
  1156. for(cnt = 0; cnt < unit[disk].dtype->sects * unit[disk].type->sect_mult; cnt++)
  1157. ptr=ms_putsec(disk,ptr,cnt);
  1158. *(ushort *)ptr = 0xaaa8; /* MFM word before is always 0x9254 */
  1159. }
  1160. /*
  1161. * Here comes the high level stuff (i.e. the filesystem interface)
  1162. * and helper functions.
  1163. * Normally this should be the only part that has to be adapted to
  1164. * different kernel versions.
  1165. */
  1166. /* FIXME: this assumes the drive is still spinning -
  1167. * which is only true if we complete writing a track within three seconds
  1168. */
  1169. static void flush_track_callback(struct timer_list *timer)
  1170. {
  1171. unsigned long nr = ((unsigned long)timer -
  1172. (unsigned long)&flush_track_timer[0]) /
  1173. sizeof(flush_track_timer[0]);
  1174. nr&=3;
  1175. writefromint = 1;
  1176. if (!try_fdc(nr)) {
  1177. /* we might block in an interrupt, so try again later */
  1178. flush_track_timer[nr].expires = jiffies + 1;
  1179. add_timer(flush_track_timer + nr);
  1180. return;
  1181. }
  1182. get_fdc(nr);
  1183. (*unit[nr].dtype->write_fkt)(nr);
  1184. if (!raw_write(nr)) {
  1185. printk (KERN_NOTICE "floppy disk write protected\n");
  1186. writefromint = 0;
  1187. writepending = 0;
  1188. }
  1189. rel_fdc();
  1190. }
  1191. static int non_int_flush_track (unsigned long nr)
  1192. {
  1193. unsigned long flags;
  1194. nr&=3;
  1195. writefromint = 0;
  1196. del_timer(&post_write_timer);
  1197. get_fdc(nr);
  1198. if (!fd_motor_on(nr)) {
  1199. writepending = 0;
  1200. rel_fdc();
  1201. return 0;
  1202. }
  1203. local_irq_save(flags);
  1204. if (writepending != 2) {
  1205. local_irq_restore(flags);
  1206. (*unit[nr].dtype->write_fkt)(nr);
  1207. if (!raw_write(nr)) {
  1208. printk (KERN_NOTICE "floppy disk write protected "
  1209. "in write!\n");
  1210. writepending = 0;
  1211. return 0;
  1212. }
  1213. wait_event(wait_fd_block, block_flag != 2);
  1214. }
  1215. else {
  1216. local_irq_restore(flags);
  1217. ms_delay(2); /* 2 ms post_write delay */
  1218. post_write(nr);
  1219. }
  1220. rel_fdc();
  1221. return 1;
  1222. }
  1223. static int get_track(int drive, int track)
  1224. {
  1225. int error, errcnt;
  1226. drive&=3;
  1227. if (unit[drive].track == track)
  1228. return 0;
  1229. get_fdc(drive);
  1230. if (!fd_motor_on(drive)) {
  1231. rel_fdc();
  1232. return -1;
  1233. }
  1234. if (unit[drive].dirty == 1) {
  1235. del_timer (flush_track_timer + drive);
  1236. non_int_flush_track (drive);
  1237. }
  1238. errcnt = 0;
  1239. while (errcnt < MAX_ERRORS) {
  1240. if (!fd_seek(drive, track))
  1241. return -1;
  1242. raw_read(drive);
  1243. error = (*unit[drive].dtype->read_fkt)(drive);
  1244. if (error == 0) {
  1245. rel_fdc();
  1246. return 0;
  1247. }
  1248. /* Read Error Handling: recalibrate and try again */
  1249. unit[drive].track = -1;
  1250. errcnt++;
  1251. }
  1252. rel_fdc();
  1253. return -1;
  1254. }
  1255. static blk_status_t amiflop_rw_cur_segment(struct amiga_floppy_struct *floppy,
  1256. struct request *rq)
  1257. {
  1258. int drive = floppy - unit;
  1259. unsigned int cnt, block, track, sector;
  1260. char *data;
  1261. for (cnt = 0; cnt < blk_rq_cur_sectors(rq); cnt++) {
  1262. #ifdef DEBUG
  1263. printk("fd: sector %ld + %d requested for %s\n",
  1264. blk_rq_pos(rq), cnt,
  1265. (rq_data_dir(rq) == READ) ? "read" : "write");
  1266. #endif
  1267. block = blk_rq_pos(rq) + cnt;
  1268. track = block / (floppy->dtype->sects * floppy->type->sect_mult);
  1269. sector = block % (floppy->dtype->sects * floppy->type->sect_mult);
  1270. data = bio_data(rq->bio) + 512 * cnt;
  1271. #ifdef DEBUG
  1272. printk("access to track %d, sector %d, with buffer at "
  1273. "0x%08lx\n", track, sector, data);
  1274. #endif
  1275. if (get_track(drive, track) == -1)
  1276. return BLK_STS_IOERR;
  1277. if (rq_data_dir(rq) == READ) {
  1278. memcpy(data, floppy->trackbuf + sector * 512, 512);
  1279. } else {
  1280. memcpy(floppy->trackbuf + sector * 512, data, 512);
  1281. /* keep the drive spinning while writes are scheduled */
  1282. if (!fd_motor_on(drive))
  1283. return BLK_STS_IOERR;
  1284. /*
  1285. * setup a callback to write the track buffer
  1286. * after a short (1 tick) delay.
  1287. */
  1288. floppy->dirty = 1;
  1289. /* reset the timer */
  1290. mod_timer (flush_track_timer + drive, jiffies + 1);
  1291. }
  1292. }
  1293. return BLK_STS_OK;
  1294. }
  1295. static blk_status_t amiflop_queue_rq(struct blk_mq_hw_ctx *hctx,
  1296. const struct blk_mq_queue_data *bd)
  1297. {
  1298. struct request *rq = bd->rq;
  1299. struct amiga_floppy_struct *floppy = rq->rq_disk->private_data;
  1300. blk_status_t err;
  1301. if (!spin_trylock_irq(&amiflop_lock))
  1302. return BLK_STS_DEV_RESOURCE;
  1303. blk_mq_start_request(rq);
  1304. do {
  1305. err = amiflop_rw_cur_segment(floppy, rq);
  1306. } while (blk_update_request(rq, err, blk_rq_cur_bytes(rq)));
  1307. blk_mq_end_request(rq, err);
  1308. spin_unlock_irq(&amiflop_lock);
  1309. return BLK_STS_OK;
  1310. }
  1311. static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1312. {
  1313. int drive = MINOR(bdev->bd_dev) & 3;
  1314. geo->heads = unit[drive].type->heads;
  1315. geo->sectors = unit[drive].dtype->sects * unit[drive].type->sect_mult;
  1316. geo->cylinders = unit[drive].type->tracks;
  1317. return 0;
  1318. }
  1319. static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
  1320. unsigned int cmd, unsigned long param)
  1321. {
  1322. struct amiga_floppy_struct *p = bdev->bd_disk->private_data;
  1323. int drive = p - unit;
  1324. static struct floppy_struct getprm;
  1325. void __user *argp = (void __user *)param;
  1326. switch(cmd){
  1327. case FDFMTBEG:
  1328. get_fdc(drive);
  1329. if (fd_ref[drive] > 1) {
  1330. rel_fdc();
  1331. return -EBUSY;
  1332. }
  1333. fsync_bdev(bdev);
  1334. if (fd_motor_on(drive) == 0) {
  1335. rel_fdc();
  1336. return -ENODEV;
  1337. }
  1338. if (fd_calibrate(drive) == 0) {
  1339. rel_fdc();
  1340. return -ENXIO;
  1341. }
  1342. floppy_off(drive);
  1343. rel_fdc();
  1344. break;
  1345. case FDFMTTRK:
  1346. if (param < p->type->tracks * p->type->heads)
  1347. {
  1348. get_fdc(drive);
  1349. if (fd_seek(drive,param) != 0){
  1350. memset(p->trackbuf, FD_FILL_BYTE,
  1351. p->dtype->sects * p->type->sect_mult * 512);
  1352. non_int_flush_track(drive);
  1353. }
  1354. floppy_off(drive);
  1355. rel_fdc();
  1356. }
  1357. else
  1358. return -EINVAL;
  1359. break;
  1360. case FDFMTEND:
  1361. floppy_off(drive);
  1362. invalidate_bdev(bdev);
  1363. break;
  1364. case FDGETPRM:
  1365. memset((void *)&getprm, 0, sizeof (getprm));
  1366. getprm.track=p->type->tracks;
  1367. getprm.head=p->type->heads;
  1368. getprm.sect=p->dtype->sects * p->type->sect_mult;
  1369. getprm.size=p->blocks;
  1370. if (copy_to_user(argp, &getprm, sizeof(struct floppy_struct)))
  1371. return -EFAULT;
  1372. break;
  1373. case FDSETPRM:
  1374. case FDDEFPRM:
  1375. return -EINVAL;
  1376. case FDFLUSH: /* unconditionally, even if not needed */
  1377. del_timer (flush_track_timer + drive);
  1378. non_int_flush_track(drive);
  1379. break;
  1380. #ifdef RAW_IOCTL
  1381. case IOCTL_RAW_TRACK:
  1382. if (copy_to_user(argp, raw_buf, p->type->read_size))
  1383. return -EFAULT;
  1384. else
  1385. return p->type->read_size;
  1386. #endif
  1387. default:
  1388. return -ENOSYS;
  1389. }
  1390. return 0;
  1391. }
  1392. static int fd_ioctl(struct block_device *bdev, fmode_t mode,
  1393. unsigned int cmd, unsigned long param)
  1394. {
  1395. int ret;
  1396. mutex_lock(&amiflop_mutex);
  1397. ret = fd_locked_ioctl(bdev, mode, cmd, param);
  1398. mutex_unlock(&amiflop_mutex);
  1399. return ret;
  1400. }
  1401. static void fd_probe(int dev)
  1402. {
  1403. unsigned long code;
  1404. int type;
  1405. int drive;
  1406. drive = dev & 3;
  1407. code = fd_get_drive_id(drive);
  1408. /* get drive type */
  1409. for (type = 0; type < num_dr_types; type++)
  1410. if (drive_types[type].code == code)
  1411. break;
  1412. if (type >= num_dr_types) {
  1413. printk(KERN_WARNING "fd_probe: unsupported drive type "
  1414. "%08lx found\n", code);
  1415. unit[drive].type = &drive_types[num_dr_types-1]; /* FD_NODRIVE */
  1416. return;
  1417. }
  1418. unit[drive].type = drive_types + type;
  1419. unit[drive].track = -1;
  1420. unit[drive].disk = -1;
  1421. unit[drive].motor = 0;
  1422. unit[drive].busy = 0;
  1423. unit[drive].status = -1;
  1424. }
  1425. /*
  1426. * floppy_open check for aliasing (/dev/fd0 can be the same as
  1427. * /dev/PS0 etc), and disallows simultaneous access to the same
  1428. * drive with different device numbers.
  1429. */
  1430. static int floppy_open(struct block_device *bdev, fmode_t mode)
  1431. {
  1432. int drive = MINOR(bdev->bd_dev) & 3;
  1433. int system = (MINOR(bdev->bd_dev) & 4) >> 2;
  1434. int old_dev;
  1435. unsigned long flags;
  1436. mutex_lock(&amiflop_mutex);
  1437. old_dev = fd_device[drive];
  1438. if (fd_ref[drive] && old_dev != system) {
  1439. mutex_unlock(&amiflop_mutex);
  1440. return -EBUSY;
  1441. }
  1442. if (mode & (FMODE_READ|FMODE_WRITE)) {
  1443. bdev_check_media_change(bdev);
  1444. if (mode & FMODE_WRITE) {
  1445. int wrprot;
  1446. get_fdc(drive);
  1447. fd_select (drive);
  1448. wrprot = !(ciaa.pra & DSKPROT);
  1449. fd_deselect (drive);
  1450. rel_fdc();
  1451. if (wrprot) {
  1452. mutex_unlock(&amiflop_mutex);
  1453. return -EROFS;
  1454. }
  1455. }
  1456. }
  1457. local_irq_save(flags);
  1458. fd_ref[drive]++;
  1459. fd_device[drive] = system;
  1460. local_irq_restore(flags);
  1461. unit[drive].dtype=&data_types[system];
  1462. unit[drive].blocks=unit[drive].type->heads*unit[drive].type->tracks*
  1463. data_types[system].sects*unit[drive].type->sect_mult;
  1464. set_capacity(unit[drive].gendisk, unit[drive].blocks);
  1465. printk(KERN_INFO "fd%d: accessing %s-disk with %s-layout\n",drive,
  1466. unit[drive].type->name, data_types[system].name);
  1467. mutex_unlock(&amiflop_mutex);
  1468. return 0;
  1469. }
  1470. static void floppy_release(struct gendisk *disk, fmode_t mode)
  1471. {
  1472. struct amiga_floppy_struct *p = disk->private_data;
  1473. int drive = p - unit;
  1474. mutex_lock(&amiflop_mutex);
  1475. if (unit[drive].dirty == 1) {
  1476. del_timer (flush_track_timer + drive);
  1477. non_int_flush_track (drive);
  1478. }
  1479. if (!fd_ref[drive]--) {
  1480. printk(KERN_CRIT "floppy_release with fd_ref == 0");
  1481. fd_ref[drive] = 0;
  1482. }
  1483. #ifdef MODULE
  1484. floppy_off (drive);
  1485. #endif
  1486. mutex_unlock(&amiflop_mutex);
  1487. }
  1488. /*
  1489. * check_events is never called from an interrupt, so we can relax a bit
  1490. * here, sleep etc. Note that floppy-on tries to set current_DOR to point
  1491. * to the desired drive, but it will probably not survive the sleep if
  1492. * several floppies are used at the same time: thus the loop.
  1493. */
  1494. static unsigned amiga_check_events(struct gendisk *disk, unsigned int clearing)
  1495. {
  1496. struct amiga_floppy_struct *p = disk->private_data;
  1497. int drive = p - unit;
  1498. int changed;
  1499. static int first_time = 1;
  1500. if (first_time)
  1501. changed = first_time--;
  1502. else {
  1503. get_fdc(drive);
  1504. fd_select (drive);
  1505. changed = !(ciaa.pra & DSKCHANGE);
  1506. fd_deselect (drive);
  1507. rel_fdc();
  1508. }
  1509. if (changed) {
  1510. fd_probe(drive);
  1511. p->track = -1;
  1512. p->dirty = 0;
  1513. writepending = 0; /* if this was true before, too bad! */
  1514. writefromint = 0;
  1515. return DISK_EVENT_MEDIA_CHANGE;
  1516. }
  1517. return 0;
  1518. }
  1519. static const struct block_device_operations floppy_fops = {
  1520. .owner = THIS_MODULE,
  1521. .open = floppy_open,
  1522. .release = floppy_release,
  1523. .ioctl = fd_ioctl,
  1524. .getgeo = fd_getgeo,
  1525. .check_events = amiga_check_events,
  1526. };
  1527. static const struct blk_mq_ops amiflop_mq_ops = {
  1528. .queue_rq = amiflop_queue_rq,
  1529. };
  1530. static struct gendisk *fd_alloc_disk(int drive)
  1531. {
  1532. struct gendisk *disk;
  1533. disk = alloc_disk(1);
  1534. if (!disk)
  1535. goto out;
  1536. disk->queue = blk_mq_init_sq_queue(&unit[drive].tag_set, &amiflop_mq_ops,
  1537. 2, BLK_MQ_F_SHOULD_MERGE);
  1538. if (IS_ERR(disk->queue)) {
  1539. disk->queue = NULL;
  1540. goto out_put_disk;
  1541. }
  1542. unit[drive].trackbuf = kmalloc(FLOPPY_MAX_SECTORS * 512, GFP_KERNEL);
  1543. if (!unit[drive].trackbuf)
  1544. goto out_cleanup_queue;
  1545. return disk;
  1546. out_cleanup_queue:
  1547. blk_cleanup_queue(disk->queue);
  1548. disk->queue = NULL;
  1549. blk_mq_free_tag_set(&unit[drive].tag_set);
  1550. out_put_disk:
  1551. put_disk(disk);
  1552. out:
  1553. unit[drive].type->code = FD_NODRIVE;
  1554. return NULL;
  1555. }
  1556. static int __init fd_probe_drives(void)
  1557. {
  1558. int drive,drives,nomem;
  1559. pr_info("FD: probing units\nfound");
  1560. drives=0;
  1561. nomem=0;
  1562. for(drive=0;drive<FD_MAX_UNITS;drive++) {
  1563. struct gendisk *disk;
  1564. fd_probe(drive);
  1565. if (unit[drive].type->code == FD_NODRIVE)
  1566. continue;
  1567. disk = fd_alloc_disk(drive);
  1568. if (!disk) {
  1569. pr_cont(" no mem for fd%d", drive);
  1570. nomem = 1;
  1571. continue;
  1572. }
  1573. unit[drive].gendisk = disk;
  1574. drives++;
  1575. pr_cont(" fd%d",drive);
  1576. disk->major = FLOPPY_MAJOR;
  1577. disk->first_minor = drive;
  1578. disk->fops = &floppy_fops;
  1579. disk->events = DISK_EVENT_MEDIA_CHANGE;
  1580. sprintf(disk->disk_name, "fd%d", drive);
  1581. disk->private_data = &unit[drive];
  1582. set_capacity(disk, 880*2);
  1583. add_disk(disk);
  1584. }
  1585. if ((drives > 0) || (nomem == 0)) {
  1586. if (drives == 0)
  1587. pr_cont(" no drives");
  1588. pr_cont("\n");
  1589. return drives;
  1590. }
  1591. pr_cont("\n");
  1592. return -ENOMEM;
  1593. }
  1594. static struct kobject *floppy_find(dev_t dev, int *part, void *data)
  1595. {
  1596. int drive = *part & 3;
  1597. if (unit[drive].type->code == FD_NODRIVE)
  1598. return NULL;
  1599. *part = 0;
  1600. return get_disk_and_module(unit[drive].gendisk);
  1601. }
  1602. static int __init amiga_floppy_probe(struct platform_device *pdev)
  1603. {
  1604. int i, ret;
  1605. if (register_blkdev(FLOPPY_MAJOR,"fd"))
  1606. return -EBUSY;
  1607. ret = -ENOMEM;
  1608. raw_buf = amiga_chip_alloc(RAW_BUF_SIZE, "Floppy");
  1609. if (!raw_buf) {
  1610. printk("fd: cannot get chip mem buffer\n");
  1611. goto out_blkdev;
  1612. }
  1613. ret = -EBUSY;
  1614. if (request_irq(IRQ_AMIGA_DSKBLK, fd_block_done, 0, "floppy_dma", NULL)) {
  1615. printk("fd: cannot get irq for dma\n");
  1616. goto out_irq;
  1617. }
  1618. if (request_irq(IRQ_AMIGA_CIAA_TB, ms_isr, 0, "floppy_timer", NULL)) {
  1619. printk("fd: cannot get irq for timer\n");
  1620. goto out_irq2;
  1621. }
  1622. ret = -ENODEV;
  1623. if (fd_probe_drives() < 1) /* No usable drives */
  1624. goto out_probe;
  1625. blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
  1626. floppy_find, NULL, NULL);
  1627. /* initialize variables */
  1628. timer_setup(&motor_on_timer, motor_on_callback, 0);
  1629. motor_on_timer.expires = 0;
  1630. for (i = 0; i < FD_MAX_UNITS; i++) {
  1631. timer_setup(&motor_off_timer[i], fd_motor_off, 0);
  1632. motor_off_timer[i].expires = 0;
  1633. timer_setup(&flush_track_timer[i], flush_track_callback, 0);
  1634. flush_track_timer[i].expires = 0;
  1635. unit[i].track = -1;
  1636. }
  1637. timer_setup(&post_write_timer, post_write_callback, 0);
  1638. post_write_timer.expires = 0;
  1639. for (i = 0; i < 128; i++)
  1640. mfmdecode[i]=255;
  1641. for (i = 0; i < 16; i++)
  1642. mfmdecode[mfmencode[i]]=i;
  1643. /* make sure that disk DMA is enabled */
  1644. custom.dmacon = DMAF_SETCLR | DMAF_DISK;
  1645. /* init ms timer */
  1646. ciaa.crb = 8; /* one-shot, stop */
  1647. return 0;
  1648. out_probe:
  1649. free_irq(IRQ_AMIGA_CIAA_TB, NULL);
  1650. out_irq2:
  1651. free_irq(IRQ_AMIGA_DSKBLK, NULL);
  1652. out_irq:
  1653. amiga_chip_free(raw_buf);
  1654. out_blkdev:
  1655. unregister_blkdev(FLOPPY_MAJOR,"fd");
  1656. return ret;
  1657. }
  1658. static struct platform_driver amiga_floppy_driver = {
  1659. .driver = {
  1660. .name = "amiga-floppy",
  1661. },
  1662. };
  1663. static int __init amiga_floppy_init(void)
  1664. {
  1665. return platform_driver_probe(&amiga_floppy_driver, amiga_floppy_probe);
  1666. }
  1667. module_init(amiga_floppy_init);
  1668. #ifndef MODULE
  1669. static int __init amiga_floppy_setup (char *str)
  1670. {
  1671. int n;
  1672. if (!MACH_IS_AMIGA)
  1673. return 0;
  1674. if (!get_option(&str, &n))
  1675. return 0;
  1676. printk (KERN_INFO "amiflop: Setting default df0 to %x\n", n);
  1677. fd_def_df0 = n;
  1678. return 1;
  1679. }
  1680. __setup("floppy=", amiga_floppy_setup);
  1681. #endif
  1682. MODULE_ALIAS("platform:amiga-floppy");