amiflop.c 47 KB

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