amiflop.c 47 KB

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