mfmhd.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /*
  2. * linux/arch/arm/drivers/block/mfmhd.c
  3. *
  4. * Copyright (C) 1995, 1996 Russell King, Dave Alan Gilbert (gilbertd@cs.man.ac.uk)
  5. *
  6. * MFM hard drive code [experimental]
  7. */
  8. /*
  9. * Change list:
  10. *
  11. * 3/2/96:DAG: Started a change list :-)
  12. * Set the hardsect_size pointers up since we are running 256 byte
  13. * sectors
  14. * Added DMA code, put it into the rw_intr
  15. * Moved RCAL out of generic interrupt code - don't want to do it
  16. * while DMA'ing - its now in individual handlers.
  17. * Took interrupt handlers off task queue lists and called
  18. * directly - not sure of implications.
  19. *
  20. * 18/2/96:DAG: Well its reading OK I think, well enough for image file code
  21. * to find the image file; but now I've discovered that I actually
  22. * have to put some code in for image files.
  23. *
  24. * Added stuff for image files; seems to work, but I've not
  25. * got a multisegment image file (I don't think!).
  26. * Put in a hack (yep a real hack) for multiple cylinder reads.
  27. * Not convinced its working.
  28. *
  29. * 5/4/96:DAG: Added asm/hardware.h and use IOC_ macros
  30. * Rewrote dma code in mfm.S (again!) - now takes a word at a time
  31. * from main RAM for speed; still doesn't feel speedy!
  32. *
  33. * 20/4/96:DAG: After rewriting mfm.S a heck of a lot of times and speeding
  34. * things up, I've finally figured out why its so damn slow.
  35. * Linux is only reading a block at a time, and so you never
  36. * get more than 1K per disc revoloution ~=60K/second.
  37. *
  38. * 27/4/96:DAG: On Russell's advice I change ll_rw_blk.c to ask it to
  39. * join adjacent blocks together. Everything falls flat on its
  40. * face.
  41. * Four hours of debugging later; I hadn't realised that
  42. * ll_rw_blk would be so generous as to join blocks whose
  43. * results aren't going into consecutive buffers.
  44. *
  45. * OK; severe rehacking of mfm_rw_interrupt; now end_request's
  46. * as soon as its DMA'd each request. Odd thing is that
  47. * we are sometimes getting interrupts where we are not transferring
  48. * any data; why? Is that what happens when you miss? I doubt
  49. * it; are we too fast? No - its just at command ends. Got 240K/s
  50. * better than before, but RiscOS hits 480K/s
  51. *
  52. * 25/6/96:RMK: Fixed init code to allow the MFM podule to work. Increased the
  53. * number of errors for my Miniscribe drive (8425).
  54. *
  55. * 30/6/96:DAG: Russell suggested that a check drive 0 might turn the LEDs off
  56. * - so in request_done just before it clears Busy it sends a
  57. * check drive 0 - and the LEDs go off!!!!
  58. *
  59. * Added test for mainboard controller. - Removes need for separate
  60. * define.
  61. *
  62. * 13/7/96:DAG: Changed hardware sectore size to 512 in attempt to make
  63. * IM drivers work.
  64. * 21/7/96:DAG: Took out old image file stuff (accessing it now produces an IO
  65. * error.)
  66. *
  67. * 17/8/96:DAG: Ran through indent -kr -i8; evil - all my nice 2 character indents
  68. * gone :-( Hand modified afterwards.
  69. * Took out last remains of the older image map system.
  70. *
  71. * 22/9/96:DAG: Changed mfm.S so it will carry on DMA'ing til; BSY is dropped
  72. * Changed mfm_rw_intr so that it doesn't follow the error
  73. * code until BSY is dropped. Nope - still broke. Problem
  74. * may revolve around when it reads the results for the error
  75. * number?
  76. *
  77. *16/11/96:DAG: Modified for 2.0.18; request_irq changed
  78. *
  79. *17/12/96:RMK: Various cleanups, reorganisation, and the changes for new IO system.
  80. * Improved probe for onboard MFM chip - it was hanging on my A5k.
  81. * Added autodetect CHS code such that we don't rely on the presence
  82. * of an ADFS boot block. Added ioport resource manager calls so
  83. * that we don't clash with already-running hardware (eg. RiscPC Ether
  84. * card slots if someone tries this)!
  85. *
  86. * 17/1/97:RMK: Upgraded to 2.1 kernels.
  87. *
  88. * 4/3/98:RMK: Changed major number to 21.
  89. *
  90. * 27/6/98:RMK: Changed asm/delay.h to linux/delay.h for mdelay().
  91. */
  92. /*
  93. * Possible enhancements:
  94. * Multi-thread the code so that it is possible that while one drive
  95. * is seeking, the other one can be reading data/seeking as well.
  96. * This would be a performance boost with dual drive systems.
  97. */
  98. #include <linux/module.h>
  99. #include <linux/config.h>
  100. #include <linux/sched.h>
  101. #include <linux/fs.h>
  102. #include <linux/interrupt.h>
  103. #include <linux/kernel.h>
  104. #include <linux/timer.h>
  105. #include <linux/mm.h>
  106. #include <linux/errno.h>
  107. #include <linux/genhd.h>
  108. #include <linux/major.h>
  109. #include <linux/ioport.h>
  110. #include <linux/delay.h>
  111. #include <linux/blkpg.h>
  112. #include <asm/system.h>
  113. #include <asm/io.h>
  114. #include <asm/irq.h>
  115. #include <asm/uaccess.h>
  116. #include <asm/dma.h>
  117. #include <asm/hardware.h>
  118. #include <asm/ecard.h>
  119. #include <asm/hardware/ioc.h>
  120. static void (*do_mfm)(void) = NULL;
  121. static struct request_queue *mfm_queue;
  122. static DEFINE_SPINLOCK(mfm_lock);
  123. #define MAJOR_NR MFM_ACORN_MAJOR
  124. #define QUEUE (mfm_queue)
  125. #define CURRENT elv_next_request(mfm_queue)
  126. /*
  127. * This sort of stuff should be in a header file shared with ide.c, hd.c, xd.c etc
  128. */
  129. #ifndef HDIO_GETGEO
  130. #define HDIO_GETGEO 0x301
  131. struct hd_geometry {
  132. unsigned char heads;
  133. unsigned char sectors;
  134. unsigned short cylinders;
  135. unsigned long start;
  136. };
  137. #endif
  138. /*
  139. * Configuration section
  140. *
  141. * This is the maximum number of drives that we accept
  142. */
  143. #define MFM_MAXDRIVES 2
  144. /*
  145. * Linux I/O address of onboard MFM controller or 0 to disable this
  146. */
  147. #define ONBOARD_MFM_ADDRESS ((0x002d0000 >> 2) | 0x80000000)
  148. /*
  149. * Uncomment this to enable debugging in the MFM driver...
  150. */
  151. #ifndef DEBUG
  152. /*#define DEBUG */
  153. #endif
  154. /*
  155. * End of configuration
  156. */
  157. /*
  158. * This structure contains all information to do with a particular physical
  159. * device.
  160. */
  161. struct mfm_info {
  162. unsigned char sectors;
  163. unsigned char heads;
  164. unsigned short cylinders;
  165. unsigned short lowcurrent;
  166. unsigned short precomp;
  167. #define NO_TRACK -1
  168. #define NEED_1_RECAL -2
  169. #define NEED_2_RECAL -3
  170. int cylinder;
  171. struct {
  172. char recal;
  173. char report;
  174. char abort;
  175. } errors;
  176. } mfm_info[MFM_MAXDRIVES];
  177. #define MFM_DRV_INFO mfm_info[raw_cmd.dev]
  178. /* Stuff from the assembly routines */
  179. extern unsigned int hdc63463_baseaddress; /* Controller base address */
  180. extern unsigned int hdc63463_irqpolladdress; /* Address to read to test for int */
  181. extern unsigned int hdc63463_irqpollmask; /* Mask for irq register */
  182. extern unsigned int hdc63463_dataptr; /* Pointer to kernel data space to DMA */
  183. extern int hdc63463_dataleft; /* Number of bytes left to transfer */
  184. static int lastspecifieddrive;
  185. static unsigned Busy;
  186. static unsigned int PartFragRead; /* The number of sectors which have been read
  187. during a partial read split over two
  188. cylinders. If 0 it means a partial
  189. read did not occur. */
  190. static unsigned int PartFragRead_RestartBlock; /* Where to restart on a split access */
  191. static unsigned int PartFragRead_SectorsLeft; /* Where to restart on a split access */
  192. static int Sectors256LeftInCurrent; /* i.e. 256 byte sectors left in current */
  193. static int SectorsLeftInRequest; /* i.e. blocks left in the thing mfm_request was called for */
  194. static int Copy_Sector; /* The 256 byte sector we are currently at - fragments need to know
  195. where to take over */
  196. static char *Copy_buffer;
  197. static void mfm_seek(void);
  198. static void mfm_rerequest(void);
  199. static void mfm_request(void);
  200. static void mfm_specify (void);
  201. static void issue_request(unsigned int block, unsigned int nsect,
  202. struct request *req);
  203. static unsigned int mfm_addr; /* Controller address */
  204. static unsigned int mfm_IRQPollLoc; /* Address to read for IRQ information */
  205. static unsigned int mfm_irqenable; /* Podule IRQ enable location */
  206. static unsigned char mfm_irq; /* Interrupt number */
  207. static int mfm_drives = 0; /* drives available */
  208. static int mfm_status = 0; /* interrupt status */
  209. static int *errors;
  210. static struct rawcmd {
  211. unsigned int dev;
  212. unsigned int cylinder;
  213. unsigned int head;
  214. unsigned int sector;
  215. unsigned int cmdtype;
  216. unsigned int cmdcode;
  217. unsigned char cmddata[16];
  218. unsigned int cmdlen;
  219. } raw_cmd;
  220. static unsigned char result[16];
  221. static struct cont {
  222. void (*interrupt) (void); /* interrupt handler */
  223. void (*error) (void); /* error handler */
  224. void (*redo) (void); /* redo handler */
  225. void (*done) (int st); /* done handler */
  226. } *cont = NULL;
  227. #if 0
  228. static struct tq_struct mfm_tq = {0, 0, (void (*)(void *)) NULL, 0};
  229. #endif
  230. int number_mfm_drives = 1;
  231. /* ------------------------------------------------------------------------------------------ */
  232. /*
  233. * From the HD63463 data sheet from Hitachi Ltd.
  234. */
  235. #define MFM_COMMAND (mfm_addr + 0)
  236. #define MFM_DATAOUT (mfm_addr + 1)
  237. #define MFM_STATUS (mfm_addr + 8)
  238. #define MFM_DATAIN (mfm_addr + 9)
  239. #define CMD_ABT 0xF0 /* Abort */
  240. #define CMD_SPC 0xE8 /* Specify */
  241. #define CMD_TST 0xE0 /* Test */
  242. #define CMD_RCLB 0xC8 /* Recalibrate */
  243. #define CMD_SEK 0xC0 /* Seek */
  244. #define CMD_WFS 0xAB /* Write Format Skew */
  245. #define CMD_WFM 0xA3 /* Write Format */
  246. #define CMD_MTB 0x90 /* Memory to buffer */
  247. #define CMD_CMPD 0x88 /* Compare data */
  248. #define CMD_WD 0x87 /* Write data */
  249. #define CMD_RED 0x70 /* Read erroneous data */
  250. #define CMD_RIS 0x68 /* Read ID skew */
  251. #define CMD_FID 0x61 /* Find ID */
  252. #define CMD_RID 0x60 /* Read ID */
  253. #define CMD_BTM 0x50 /* Buffer to memory */
  254. #define CMD_CKD 0x48 /* Check data */
  255. #define CMD_RD 0x40 /* Read data */
  256. #define CMD_OPBW 0x38 /* Open buffer write */
  257. #define CMD_OPBR 0x30 /* Open buffer read */
  258. #define CMD_CKV 0x28 /* Check drive */
  259. #define CMD_CKE 0x20 /* Check ECC */
  260. #define CMD_POD 0x18 /* Polling disable */
  261. #define CMD_POL 0x10 /* Polling enable */
  262. #define CMD_RCAL 0x08 /* Recall */
  263. #define STAT_BSY 0x8000 /* Busy */
  264. #define STAT_CPR 0x4000 /* Command Parameter Rejection */
  265. #define STAT_CED 0x2000 /* Command end */
  266. #define STAT_SED 0x1000 /* Seek end */
  267. #define STAT_DER 0x0800 /* Drive error */
  268. #define STAT_ABN 0x0400 /* Abnormal end */
  269. #define STAT_POL 0x0200 /* Polling */
  270. /* ------------------------------------------------------------------------------------------ */
  271. #ifdef DEBUG
  272. static void console_printf(const char *fmt,...)
  273. {
  274. static char buffer[2048]; /* Arbitary! */
  275. extern void console_print(const char *);
  276. unsigned long flags;
  277. va_list ap;
  278. local_irq_save(flags);
  279. va_start(ap, fmt);
  280. vsprintf(buffer, fmt, ap);
  281. console_print(buffer);
  282. va_end(fmt);
  283. local_irq_restore(flags);
  284. }; /* console_printf */
  285. #define DBG(x...) console_printf(x)
  286. #else
  287. #define DBG(x...)
  288. #endif
  289. static void print_status(void)
  290. {
  291. char *error;
  292. static char *errors[] = {
  293. "no error",
  294. "command aborted",
  295. "invalid command",
  296. "parameter error",
  297. "not initialised",
  298. "rejected TEST",
  299. "no useld",
  300. "write fault",
  301. "not ready",
  302. "no scp",
  303. "in seek",
  304. "invalid NCA",
  305. "invalid step rate",
  306. "seek error",
  307. "over run",
  308. "invalid PHA",
  309. "data field EEC error",
  310. "data field CRC error",
  311. "error corrected",
  312. "data field fatal error",
  313. "no data am",
  314. "not hit",
  315. "ID field CRC error",
  316. "time over",
  317. "no ID am",
  318. "not writable"
  319. };
  320. if (result[1] < 0x65)
  321. error = errors[result[1] >> 2];
  322. else
  323. error = "unknown";
  324. printk("(");
  325. if (mfm_status & STAT_BSY) printk("BSY ");
  326. if (mfm_status & STAT_CPR) printk("CPR ");
  327. if (mfm_status & STAT_CED) printk("CED ");
  328. if (mfm_status & STAT_SED) printk("SED ");
  329. if (mfm_status & STAT_DER) printk("DER ");
  330. if (mfm_status & STAT_ABN) printk("ABN ");
  331. if (mfm_status & STAT_POL) printk("POL ");
  332. printk(") SSB = %X (%s)\n", result[1], error);
  333. }
  334. /* ------------------------------------------------------------------------------------- */
  335. static void issue_command(int command, unsigned char *cmdb, int len)
  336. {
  337. int status;
  338. #ifdef DEBUG
  339. int i;
  340. console_printf("issue_command: %02X: ", command);
  341. for (i = 0; i < len; i++)
  342. console_printf("%02X ", cmdb[i]);
  343. console_printf("\n");
  344. #endif
  345. do {
  346. status = inw(MFM_STATUS);
  347. } while (status & (STAT_BSY | STAT_POL));
  348. DBG("issue_command: status after pol/bsy loop: %02X:\n ", status >> 8);
  349. if (status & (STAT_CPR | STAT_CED | STAT_SED | STAT_DER | STAT_ABN)) {
  350. outw(CMD_RCAL, MFM_COMMAND);
  351. while (inw(MFM_STATUS) & STAT_BSY);
  352. }
  353. status = inw(MFM_STATUS);
  354. DBG("issue_command: status before parameter issue: %02X:\n ", status >> 8);
  355. while (len > 0) {
  356. outw(cmdb[1] | (cmdb[0] << 8), MFM_DATAOUT);
  357. len -= 2;
  358. cmdb += 2;
  359. }
  360. status = inw(MFM_STATUS);
  361. DBG("issue_command: status before command issue: %02X:\n ", status >> 8);
  362. outw(command, MFM_COMMAND);
  363. status = inw(MFM_STATUS);
  364. DBG("issue_command: status immediately after command issue: %02X:\n ", status >> 8);
  365. }
  366. static void wait_for_completion(void)
  367. {
  368. while ((mfm_status = inw(MFM_STATUS)) & STAT_BSY);
  369. }
  370. static void wait_for_command_end(void)
  371. {
  372. int i;
  373. while (!((mfm_status = inw(MFM_STATUS)) & STAT_CED));
  374. for (i = 0; i < 16;) {
  375. int in;
  376. in = inw(MFM_DATAIN);
  377. result[i++] = in >> 8;
  378. result[i++] = in;
  379. }
  380. outw (CMD_RCAL, MFM_COMMAND);
  381. }
  382. /* ------------------------------------------------------------------------------------- */
  383. static void mfm_rw_intr(void)
  384. {
  385. int old_status; /* Holds status on entry, we read to see if the command just finished */
  386. #ifdef DEBUG
  387. console_printf("mfm_rw_intr...dataleft=%d\n", hdc63463_dataleft);
  388. print_status();
  389. #endif
  390. /* Now don't handle the error until BSY drops */
  391. if ((mfm_status & (STAT_DER | STAT_ABN)) && ((mfm_status&STAT_BSY)==0)) {
  392. /* Something has gone wrong - let's try that again */
  393. outw(CMD_RCAL, MFM_COMMAND); /* Clear interrupt condition */
  394. if (cont) {
  395. DBG("mfm_rw_intr: DER/ABN err\n");
  396. cont->error();
  397. cont->redo();
  398. };
  399. return;
  400. };
  401. /* OK so what ever happened it's not an error, now I reckon we are left between
  402. a choice of command end or some data which is ready to be collected */
  403. /* I think we have to transfer data while the interrupt line is on and its
  404. not any other type of interrupt */
  405. if (CURRENT->cmd == WRITE) {
  406. extern void hdc63463_writedma(void);
  407. if ((hdc63463_dataleft <= 0) && (!(mfm_status & STAT_CED))) {
  408. printk("mfm_rw_intr: Apparent DMA write request when no more to DMA\n");
  409. if (cont) {
  410. cont->error();
  411. cont->redo();
  412. };
  413. return;
  414. };
  415. hdc63463_writedma();
  416. } else {
  417. extern void hdc63463_readdma(void);
  418. if ((hdc63463_dataleft <= 0) && (!(mfm_status & STAT_CED))) {
  419. printk("mfm_rw_intr: Apparent DMA read request when no more to DMA\n");
  420. if (cont) {
  421. cont->error();
  422. cont->redo();
  423. };
  424. return;
  425. };
  426. DBG("Going to try read dma..............status=0x%x, buffer=%p\n", mfm_status, hdc63463_dataptr);
  427. hdc63463_readdma();
  428. }; /* Read */
  429. if (hdc63463_dataptr != ((unsigned int) Copy_buffer + 256)) {
  430. /* If we didn't actually manage to get any data on this interrupt - but why? We got the interrupt */
  431. /* Ah - well looking at the status its just when we get command end; so no problem */
  432. /*console_printf("mfm: dataptr mismatch. dataptr=0x%08x Copy_buffer+256=0x%08p\n",
  433. hdc63463_dataptr,Copy_buffer+256);
  434. print_status(); */
  435. } else {
  436. Sectors256LeftInCurrent--;
  437. Copy_buffer += 256;
  438. Copy_Sector++;
  439. /* We have come to the end of this request */
  440. if (!Sectors256LeftInCurrent) {
  441. DBG("mfm: end_request for CURRENT=0x%p CURRENT(sector=%d current_nr_sectors=%d nr_sectors=%d)\n",
  442. CURRENT, CURRENT->sector, CURRENT->current_nr_sectors, CURRENT->nr_sectors);
  443. CURRENT->nr_sectors -= CURRENT->current_nr_sectors;
  444. CURRENT->sector += CURRENT->current_nr_sectors;
  445. SectorsLeftInRequest -= CURRENT->current_nr_sectors;
  446. end_request(CURRENT, 1);
  447. if (SectorsLeftInRequest) {
  448. hdc63463_dataptr = (unsigned int) CURRENT->buffer;
  449. Copy_buffer = CURRENT->buffer;
  450. Sectors256LeftInCurrent = CURRENT->current_nr_sectors * 2;
  451. errors = &(CURRENT->errors);
  452. /* These should match the present calculations of the next logical sector
  453. on the device
  454. Copy_Sector=CURRENT->sector*2; */
  455. if (Copy_Sector != CURRENT->sector * 2)
  456. #ifdef DEBUG
  457. /*console_printf*/printk("mfm: Copy_Sector mismatch. Copy_Sector=%d CURRENT->sector*2=%d\n",
  458. Copy_Sector, CURRENT->sector * 2);
  459. #else
  460. printk("mfm: Copy_Sector mismatch! Eek!\n");
  461. #endif
  462. }; /* CURRENT */
  463. }; /* Sectors256LeftInCurrent */
  464. };
  465. old_status = mfm_status;
  466. mfm_status = inw(MFM_STATUS);
  467. if (mfm_status & (STAT_DER | STAT_ABN)) {
  468. /* Something has gone wrong - let's try that again */
  469. if (cont) {
  470. DBG("mfm_rw_intr: DER/ABN error\n");
  471. cont->error();
  472. cont->redo();
  473. };
  474. return;
  475. };
  476. /* If this code wasn't entered due to command_end but there is
  477. now a command end we must read the command results out. If it was
  478. entered like this then mfm_interrupt_handler would have done the
  479. job. */
  480. if ((!((old_status & (STAT_CPR | STAT_BSY)) == STAT_CPR)) &&
  481. ((mfm_status & (STAT_CPR | STAT_BSY)) == STAT_CPR)) {
  482. int len = 0;
  483. while (len < 16) {
  484. int in;
  485. in = inw(MFM_DATAIN);
  486. result[len++] = in >> 8;
  487. result[len++] = in;
  488. };
  489. }; /* Result read */
  490. /*console_printf ("mfm_rw_intr nearexit [%02X]\n", __raw_readb(mfm_IRQPollLoc)); */
  491. /* If end of command move on */
  492. if (mfm_status & (STAT_CED)) {
  493. outw(CMD_RCAL, MFM_COMMAND); /* Clear interrupt condition */
  494. /* End of command - trigger the next command */
  495. if (cont) {
  496. cont->done(1);
  497. }
  498. DBG("mfm_rw_intr: returned from cont->done\n");
  499. } else {
  500. /* Its going to generate another interrupt */
  501. do_mfm = mfm_rw_intr;
  502. };
  503. }
  504. static void mfm_setup_rw(void)
  505. {
  506. DBG("setting up for rw...\n");
  507. do_mfm = mfm_rw_intr;
  508. issue_command(raw_cmd.cmdcode, raw_cmd.cmddata, raw_cmd.cmdlen);
  509. }
  510. static void mfm_recal_intr(void)
  511. {
  512. #ifdef DEBUG
  513. console_printf("recal intr - status = ");
  514. print_status();
  515. #endif
  516. outw(CMD_RCAL, MFM_COMMAND); /* Clear interrupt condition */
  517. if (mfm_status & (STAT_DER | STAT_ABN)) {
  518. printk("recal failed\n");
  519. MFM_DRV_INFO.cylinder = NEED_2_RECAL;
  520. if (cont) {
  521. cont->error();
  522. cont->redo();
  523. }
  524. return;
  525. }
  526. /* Thats seek end - we are finished */
  527. if (mfm_status & STAT_SED) {
  528. issue_command(CMD_POD, NULL, 0);
  529. MFM_DRV_INFO.cylinder = 0;
  530. mfm_seek();
  531. return;
  532. }
  533. /* Command end without seek end (see data sheet p.20) for parallel seek
  534. - we have to send a POL command to wait for the seek */
  535. if (mfm_status & STAT_CED) {
  536. do_mfm = mfm_recal_intr;
  537. issue_command(CMD_POL, NULL, 0);
  538. return;
  539. }
  540. printk("recal: unknown status\n");
  541. }
  542. static void mfm_seek_intr(void)
  543. {
  544. #ifdef DEBUG
  545. console_printf("seek intr - status = ");
  546. print_status();
  547. #endif
  548. outw(CMD_RCAL, MFM_COMMAND); /* Clear interrupt condition */
  549. if (mfm_status & (STAT_DER | STAT_ABN)) {
  550. printk("seek failed\n");
  551. MFM_DRV_INFO.cylinder = NEED_2_RECAL;
  552. if (cont) {
  553. cont->error();
  554. cont->redo();
  555. }
  556. return;
  557. }
  558. if (mfm_status & STAT_SED) {
  559. issue_command(CMD_POD, NULL, 0);
  560. MFM_DRV_INFO.cylinder = raw_cmd.cylinder;
  561. mfm_seek();
  562. return;
  563. }
  564. if (mfm_status & STAT_CED) {
  565. do_mfm = mfm_seek_intr;
  566. issue_command(CMD_POL, NULL, 0);
  567. return;
  568. }
  569. printk("seek: unknown status\n");
  570. }
  571. /* IDEA2 seems to work better - its what RiscOS sets my
  572. * disc to - on its SECOND call to specify!
  573. */
  574. #define IDEA2
  575. #ifndef IDEA2
  576. #define SPEC_SL 0x16
  577. #define SPEC_SH 0xa9 /* Step pulse high=21, Record Length=001 (256 bytes) */
  578. #else
  579. #define SPEC_SL 0x00 /* OM2 - SL - step pulse low */
  580. #define SPEC_SH 0x21 /* Step pulse high=4, Record Length=001 (256 bytes) */
  581. #endif
  582. static void mfm_setupspecify (int drive, unsigned char *cmdb)
  583. {
  584. cmdb[0] = 0x1f; /* OM0 - !SECT,!MOD,!DIF,PADP,ECD,CRCP,CRCI,ACOR */
  585. cmdb[1] = 0xc3; /* OM1 - DTM,BRST,!CEDM,!SEDM,!DERM,0,AMEX,PSK */
  586. cmdb[2] = SPEC_SL; /* OM2 - SL - step pulse low */
  587. cmdb[3] = (number_mfm_drives == 1) ? 0x02 : 0x06; /* 1 or 2 drives */
  588. cmdb[4] = 0xfc | ((mfm_info[drive].cylinders - 1) >> 8);/* RW time over/high part of number of cylinders */
  589. cmdb[5] = mfm_info[drive].cylinders - 1; /* low part of number of cylinders */
  590. cmdb[6] = mfm_info[drive].heads - 1; /* Number of heads */
  591. cmdb[7] = mfm_info[drive].sectors - 1; /* Number of sectors */
  592. cmdb[8] = SPEC_SH;
  593. cmdb[9] = 0x0a; /* gap length 1 */
  594. cmdb[10] = 0x0d; /* gap length 2 */
  595. cmdb[11] = 0x0c; /* gap length 3 */
  596. cmdb[12] = (mfm_info[drive].precomp - 1) >> 8; /* pre comp cylinder */
  597. cmdb[13] = mfm_info[drive].precomp - 1;
  598. cmdb[14] = (mfm_info[drive].lowcurrent - 1) >> 8; /* Low current cylinder */
  599. cmdb[15] = mfm_info[drive].lowcurrent - 1;
  600. }
  601. static void mfm_specify (void)
  602. {
  603. unsigned char cmdb[16];
  604. DBG("specify...dev=%d lastspecified=%d\n", raw_cmd.dev, lastspecifieddrive);
  605. mfm_setupspecify (raw_cmd.dev, cmdb);
  606. issue_command (CMD_SPC, cmdb, 16);
  607. /* Ensure that we will do another specify if we move to the other drive */
  608. lastspecifieddrive = raw_cmd.dev;
  609. wait_for_completion();
  610. }
  611. static void mfm_seek(void)
  612. {
  613. unsigned char cmdb[4];
  614. DBG("seeking...\n");
  615. if (MFM_DRV_INFO.cylinder < 0) {
  616. do_mfm = mfm_recal_intr;
  617. DBG("mfm_seek: about to call specify\n");
  618. mfm_specify (); /* DAG added this */
  619. cmdb[0] = raw_cmd.dev + 1;
  620. cmdb[1] = 0;
  621. issue_command(CMD_RCLB, cmdb, 2);
  622. return;
  623. }
  624. if (MFM_DRV_INFO.cylinder != raw_cmd.cylinder) {
  625. cmdb[0] = raw_cmd.dev + 1;
  626. cmdb[1] = 0; /* raw_cmd.head; DAG: My data sheet says this should be 0 */
  627. cmdb[2] = raw_cmd.cylinder >> 8;
  628. cmdb[3] = raw_cmd.cylinder;
  629. do_mfm = mfm_seek_intr;
  630. issue_command(CMD_SEK, cmdb, 4);
  631. } else
  632. mfm_setup_rw();
  633. }
  634. static void mfm_initialise(void)
  635. {
  636. DBG("init...\n");
  637. mfm_seek();
  638. }
  639. static void request_done(int uptodate)
  640. {
  641. DBG("mfm:request_done\n");
  642. if (uptodate) {
  643. unsigned char block[2] = {0, 0};
  644. /* Apparently worked - let's check bytes left to DMA */
  645. if (hdc63463_dataleft != (PartFragRead_SectorsLeft * 256)) {
  646. printk("mfm: request_done - dataleft=%d - should be %d - Eek!\n", hdc63463_dataleft, PartFragRead_SectorsLeft * 256);
  647. end_request(CURRENT, 0);
  648. Busy = 0;
  649. };
  650. /* Potentially this means that we've done; but we might be doing
  651. a partial access, (over two cylinders) or we may have a number
  652. of fragments in an image file. First let's deal with partial accesss
  653. */
  654. if (PartFragRead) {
  655. /* Yep - a partial access */
  656. /* and issue the remainder */
  657. issue_request(PartFragRead_RestartBlock, PartFragRead_SectorsLeft, CURRENT);
  658. return;
  659. }
  660. /* ah well - perhaps there is another fragment to go */
  661. /* Increment pointers/counts to start of next fragment */
  662. if (SectorsLeftInRequest > 0) printk("mfm: SectorsLeftInRequest>0 - Eek! Shouldn't happen!\n");
  663. /* No - its the end of the line */
  664. /* end_request's should have happened at the end of sector DMAs */
  665. /* Turns Drive LEDs off - may slow it down? */
  666. if (!elv_next_request(QUEUE))
  667. issue_command(CMD_CKV, block, 2);
  668. Busy = 0;
  669. DBG("request_done: About to mfm_request\n");
  670. /* Next one please */
  671. mfm_request(); /* Moved from mfm_rw_intr */
  672. DBG("request_done: returned from mfm_request\n");
  673. } else {
  674. printk("mfm:request_done: update=0\n");
  675. end_request(CURRENT, 0);
  676. Busy = 0;
  677. }
  678. }
  679. static void error_handler(void)
  680. {
  681. printk("error detected... status = ");
  682. print_status();
  683. (*errors)++;
  684. if (*errors > MFM_DRV_INFO.errors.abort)
  685. cont->done(0);
  686. if (*errors > MFM_DRV_INFO.errors.recal)
  687. MFM_DRV_INFO.cylinder = NEED_2_RECAL;
  688. }
  689. static void rw_interrupt(void)
  690. {
  691. printk("rw_interrupt\n");
  692. }
  693. static struct cont rw_cont =
  694. {
  695. rw_interrupt,
  696. error_handler,
  697. mfm_rerequest,
  698. request_done
  699. };
  700. /*
  701. * Actually gets round to issuing the request - note everything at this
  702. * point is in 256 byte sectors not Linux 512 byte blocks
  703. */
  704. static void issue_request(unsigned int block, unsigned int nsect,
  705. struct request *req)
  706. {
  707. struct gendisk *disk = req->rq_disk;
  708. struct mfm_info *p = disk->private_data;
  709. int track, start_head, start_sector;
  710. int sectors_to_next_cyl;
  711. dev = p - mfm_info;
  712. track = block / p->sectors;
  713. start_sector = block % p->sectors;
  714. start_head = track % p->heads;
  715. /* First get the number of whole tracks which are free before the next
  716. track */
  717. sectors_to_next_cyl = (p->heads - (start_head + 1)) * p->sectors;
  718. /* Then add in the number of sectors left on this track */
  719. sectors_to_next_cyl += (p->sectors - start_sector);
  720. DBG("issue_request: mfm_info[dev].sectors=%d track=%d\n", p->sectors, track);
  721. raw_cmd.dev = dev;
  722. raw_cmd.sector = start_sector;
  723. raw_cmd.head = start_head;
  724. raw_cmd.cylinder = track / p->heads;
  725. raw_cmd.cmdtype = CURRENT->cmd;
  726. raw_cmd.cmdcode = CURRENT->cmd == WRITE ? CMD_WD : CMD_RD;
  727. raw_cmd.cmddata[0] = dev + 1; /* DAG: +1 to get US */
  728. raw_cmd.cmddata[1] = raw_cmd.head;
  729. raw_cmd.cmddata[2] = raw_cmd.cylinder >> 8;
  730. raw_cmd.cmddata[3] = raw_cmd.cylinder;
  731. raw_cmd.cmddata[4] = raw_cmd.head;
  732. raw_cmd.cmddata[5] = raw_cmd.sector;
  733. /* Was == and worked - how the heck??? */
  734. if (lastspecifieddrive != raw_cmd.dev)
  735. mfm_specify ();
  736. if (nsect <= sectors_to_next_cyl) {
  737. raw_cmd.cmddata[6] = nsect >> 8;
  738. raw_cmd.cmddata[7] = nsect;
  739. PartFragRead = 0; /* All in one */
  740. PartFragRead_SectorsLeft = 0; /* Must set this - used in DMA calcs */
  741. } else {
  742. raw_cmd.cmddata[6] = sectors_to_next_cyl >> 8;
  743. raw_cmd.cmddata[7] = sectors_to_next_cyl;
  744. PartFragRead = sectors_to_next_cyl; /* only do this many this time */
  745. PartFragRead_RestartBlock = block + sectors_to_next_cyl; /* Where to restart from */
  746. PartFragRead_SectorsLeft = nsect - sectors_to_next_cyl;
  747. }
  748. raw_cmd.cmdlen = 8;
  749. /* Setup DMA pointers */
  750. hdc63463_dataptr = (unsigned int) Copy_buffer;
  751. hdc63463_dataleft = nsect * 256; /* Better way? */
  752. DBG("mfm%c: %sing: CHS=%d/%d/%d, sectors=%d, buffer=0x%08lx (%p)\n",
  753. raw_cmd.dev + 'a', (CURRENT->cmd == READ) ? "read" : "writ",
  754. raw_cmd.cylinder,
  755. raw_cmd.head,
  756. raw_cmd.sector, nsect, (unsigned long) Copy_buffer, CURRENT);
  757. cont = &rw_cont;
  758. errors = &(CURRENT->errors);
  759. #if 0
  760. mfm_tq.routine = (void (*)(void *)) mfm_initialise;
  761. queue_task(&mfm_tq, &tq_immediate);
  762. mark_bh(IMMEDIATE_BH);
  763. #else
  764. mfm_initialise();
  765. #endif
  766. } /* issue_request */
  767. /*
  768. * Called when an error has just happened - need to trick mfm_request
  769. * into thinking we weren't busy
  770. *
  771. * Turn off ints - mfm_request expects them this way
  772. */
  773. static void mfm_rerequest(void)
  774. {
  775. DBG("mfm_rerequest\n");
  776. cli();
  777. Busy = 0;
  778. mfm_request();
  779. }
  780. static struct gendisk *mfm_gendisk[2];
  781. static void mfm_request(void)
  782. {
  783. DBG("mfm_request CURRENT=%p Busy=%d\n", CURRENT, Busy);
  784. /* If we are still processing then return; we will get called again */
  785. if (Busy) {
  786. /* Again seems to be common in 1.3.45 */
  787. /*DBG*/printk("mfm_request: Exiting due to busy\n");
  788. return;
  789. }
  790. Busy = 1;
  791. while (1) {
  792. unsigned int block, nsect;
  793. struct gendisk *disk;
  794. DBG("mfm_request: loop start\n");
  795. sti();
  796. DBG("mfm_request: before !CURRENT\n");
  797. if (!CURRENT) {
  798. printk("mfm_request: Exiting due to empty queue (pre)\n");
  799. do_mfm = NULL;
  800. Busy = 0;
  801. return;
  802. }
  803. DBG("mfm_request: before arg extraction\n");
  804. disk = CURRENT->rq_disk;
  805. block = CURRENT->sector;
  806. nsect = CURRENT->nr_sectors;
  807. if (block >= get_capacity(disk) ||
  808. block+nsect > get_capacity(disk)) {
  809. printk("%s: bad access: block=%d, count=%d, nr_sects=%ld\n",
  810. disk->disk_name, block, nsect, get_capacity(disk));
  811. printk("mfm: continue 1\n");
  812. end_request(CURRENT, 0);
  813. Busy = 0;
  814. continue;
  815. }
  816. /* DAG: Linux doesn't cope with this - even though it has an array telling
  817. it the hardware block size - silly */
  818. block <<= 1; /* Now in 256 byte sectors */
  819. nsect <<= 1; /* Ditto */
  820. SectorsLeftInRequest = nsect >> 1;
  821. Sectors256LeftInCurrent = CURRENT->current_nr_sectors * 2;
  822. Copy_buffer = CURRENT->buffer;
  823. Copy_Sector = CURRENT->sector << 1;
  824. DBG("mfm_request: block after offset=%d\n", block);
  825. if (CURRENT->cmd != READ && CURRENT->cmd != WRITE) {
  826. printk("unknown mfm-command %d\n", CURRENT->cmd);
  827. end_request(CURRENT, 0);
  828. Busy = 0;
  829. printk("mfm: continue 4\n");
  830. continue;
  831. }
  832. issue_request(block, nsect, CURRENT);
  833. break;
  834. }
  835. DBG("mfm_request: Dropping out bottom\n");
  836. }
  837. static void do_mfm_request(request_queue_t *q)
  838. {
  839. DBG("do_mfm_request: about to mfm_request\n");
  840. mfm_request();
  841. }
  842. static void mfm_interrupt_handler(int unused, void *dev_id, struct pt_regs *regs)
  843. {
  844. void (*handler) (void) = do_mfm;
  845. do_mfm = NULL;
  846. DBG("mfm_interrupt_handler (handler=0x%p)\n", handler);
  847. mfm_status = inw(MFM_STATUS);
  848. /* If CPR (Command Parameter Reject) and not busy it means that the command
  849. has some return message to give us */
  850. if ((mfm_status & (STAT_CPR | STAT_BSY)) == STAT_CPR) {
  851. int len = 0;
  852. while (len < 16) {
  853. int in;
  854. in = inw(MFM_DATAIN);
  855. result[len++] = in >> 8;
  856. result[len++] = in;
  857. }
  858. }
  859. if (handler) {
  860. handler();
  861. return;
  862. }
  863. outw (CMD_RCAL, MFM_COMMAND); /* Clear interrupt condition */
  864. printk ("mfm: unexpected interrupt - status = ");
  865. print_status ();
  866. while (1);
  867. }
  868. /*
  869. * Tell the user about the drive if we decided it exists.
  870. */
  871. static void mfm_geometry(int drive)
  872. {
  873. struct mfm_info *p = mfm_info + drive;
  874. struct gendisk *disk = mfm_gendisk[drive];
  875. disk->private_data = p;
  876. if (p->cylinders)
  877. printk ("%s: %dMB CHS=%d/%d/%d LCC=%d RECOMP=%d\n",
  878. disk->disk_name,
  879. p->cylinders * p->heads * p->sectors / 4096,
  880. p->cylinders, p->heads, p->sectors,
  881. p->lowcurrent, p->precomp);
  882. set_capacity(disk, p->cylinders * p->heads * p->sectors / 2);
  883. }
  884. #ifdef CONFIG_BLK_DEV_MFM_AUTODETECT
  885. /*
  886. * Attempt to detect a drive and find its geometry. The drive has already been
  887. * specified...
  888. *
  889. * We first recalibrate the disk, then try to probe sectors, heads and then
  890. * cylinders. NOTE! the cylinder probe may break drives. The xd disk driver
  891. * does something along these lines, so I assume that most drives are up to
  892. * this mistreatment...
  893. */
  894. static int mfm_detectdrive (int drive)
  895. {
  896. unsigned int mingeo[3], maxgeo[3];
  897. unsigned int attribute, need_recal = 1;
  898. unsigned char cmdb[8];
  899. memset (mingeo, 0, sizeof (mingeo));
  900. maxgeo[0] = mfm_info[drive].sectors;
  901. maxgeo[1] = mfm_info[drive].heads;
  902. maxgeo[2] = mfm_info[drive].cylinders;
  903. cmdb[0] = drive + 1;
  904. cmdb[6] = 0;
  905. cmdb[7] = 1;
  906. for (attribute = 0; attribute < 3; attribute++) {
  907. while (mingeo[attribute] != maxgeo[attribute]) {
  908. unsigned int variable;
  909. variable = (maxgeo[attribute] + mingeo[attribute]) >> 1;
  910. cmdb[1] = cmdb[2] = cmdb[3] = cmdb[4] = cmdb[5] = 0;
  911. if (need_recal) {
  912. int tries = 5;
  913. do {
  914. issue_command (CMD_RCLB, cmdb, 2);
  915. wait_for_completion ();
  916. wait_for_command_end ();
  917. if (result[1] == 0x20)
  918. break;
  919. } while (result[1] && --tries);
  920. if (result[1]) {
  921. outw (CMD_RCAL, MFM_COMMAND);
  922. return 0;
  923. }
  924. need_recal = 0;
  925. }
  926. switch (attribute) {
  927. case 0:
  928. cmdb[5] = variable;
  929. issue_command (CMD_CMPD, cmdb, 8);
  930. break;
  931. case 1:
  932. cmdb[1] = variable;
  933. cmdb[4] = variable;
  934. issue_command (CMD_CMPD, cmdb, 8);
  935. break;
  936. case 2:
  937. cmdb[2] = variable >> 8;
  938. cmdb[3] = variable;
  939. issue_command (CMD_SEK, cmdb, 4);
  940. break;
  941. }
  942. wait_for_completion ();
  943. wait_for_command_end ();
  944. switch (result[1]) {
  945. case 0x00:
  946. case 0x50:
  947. mingeo[attribute] = variable + 1;
  948. break;
  949. case 0x20:
  950. outw (CMD_RCAL, MFM_COMMAND);
  951. return 0;
  952. case 0x24:
  953. need_recal = 1;
  954. default:
  955. maxgeo[attribute] = variable;
  956. break;
  957. }
  958. }
  959. }
  960. mfm_info[drive].cylinders = mingeo[2];
  961. mfm_info[drive].lowcurrent = mingeo[2];
  962. mfm_info[drive].precomp = mingeo[2] / 2;
  963. mfm_info[drive].heads = mingeo[1];
  964. mfm_info[drive].sectors = mingeo[0];
  965. outw (CMD_RCAL, MFM_COMMAND);
  966. return 1;
  967. }
  968. #endif
  969. /*
  970. * Initialise all drive information for this controller.
  971. */
  972. static int mfm_initdrives(void)
  973. {
  974. int drive;
  975. if (number_mfm_drives > MFM_MAXDRIVES) {
  976. number_mfm_drives = MFM_MAXDRIVES;
  977. printk("No. of ADFS MFM drives is greater than MFM_MAXDRIVES - you can't have that many!\n");
  978. }
  979. for (drive = 0; drive < number_mfm_drives; drive++) {
  980. mfm_info[drive].lowcurrent = 1;
  981. mfm_info[drive].precomp = 1;
  982. mfm_info[drive].cylinder = -1;
  983. mfm_info[drive].errors.recal = 0;
  984. mfm_info[drive].errors.report = 0;
  985. mfm_info[drive].errors.abort = 4;
  986. #ifdef CONFIG_BLK_DEV_MFM_AUTODETECT
  987. mfm_info[drive].cylinders = 1024;
  988. mfm_info[drive].heads = 8;
  989. mfm_info[drive].sectors = 64;
  990. {
  991. unsigned char cmdb[16];
  992. mfm_setupspecify (drive, cmdb);
  993. cmdb[1] &= ~0x81;
  994. issue_command (CMD_SPC, cmdb, 16);
  995. wait_for_completion ();
  996. if (!mfm_detectdrive (drive)) {
  997. mfm_info[drive].cylinders = 0;
  998. mfm_info[drive].heads = 0;
  999. mfm_info[drive].sectors = 0;
  1000. }
  1001. cmdb[0] = cmdb[1] = 0;
  1002. issue_command (CMD_CKV, cmdb, 2);
  1003. }
  1004. #else
  1005. mfm_info[drive].cylinders = 1; /* its going to have to figure it out from the partition info */
  1006. mfm_info[drive].heads = 4;
  1007. mfm_info[drive].sectors = 32;
  1008. #endif
  1009. }
  1010. return number_mfm_drives;
  1011. }
  1012. /*
  1013. * The 'front' end of the mfm driver follows...
  1014. */
  1015. static int mfm_ioctl(struct inode *inode, struct file *file, u_int cmd, u_long arg)
  1016. {
  1017. struct mfm_info *p = inode->i_bdev->bd_disk->private_data;
  1018. struct hd_geometry *geo = (struct hd_geometry *) arg;
  1019. if (cmd != HDIO_GETGEO)
  1020. return -EINVAL;
  1021. if (!arg)
  1022. return -EINVAL;
  1023. if (put_user (p->heads, &geo->heads))
  1024. return -EFAULT;
  1025. if (put_user (p->sectors, &geo->sectors))
  1026. return -EFAULT;
  1027. if (put_user (p->cylinders, &geo->cylinders))
  1028. return -EFAULT;
  1029. if (put_user (get_start_sect(inode->i_bdev), &geo->start))
  1030. return -EFAULT;
  1031. return 0;
  1032. }
  1033. /*
  1034. * This is to handle various kernel command line parameters
  1035. * specific to this driver.
  1036. */
  1037. void mfm_setup(char *str, int *ints)
  1038. {
  1039. return;
  1040. }
  1041. /*
  1042. * Set the CHS from the ADFS boot block if it is present. This is not ideal
  1043. * since if there are any non-ADFS partitions on the disk, this won't work!
  1044. * Hence, I want to get rid of this...
  1045. */
  1046. void xd_set_geometry(struct block_device *bdev, unsigned char secsptrack,
  1047. unsigned char heads, unsigned int secsize)
  1048. {
  1049. struct mfm_info *p = bdev->bd_disk->private_data;
  1050. int drive = p - mfm_info;
  1051. unsigned long disksize = bdev->bd_inode->i_size;
  1052. if (p->cylinders == 1) {
  1053. p->sectors = secsptrack;
  1054. p->heads = heads;
  1055. p->cylinders = discsize / (secsptrack * heads * secsize);
  1056. if ((heads < 1) || (p->cylinders > 1024)) {
  1057. printk("%s: Insane disc shape! Setting to 512/4/32\n",
  1058. bdev->bd_disk->disk_name);
  1059. /* These values are fairly arbitary, but are there so that if your
  1060. * lucky you can pick apart your disc to find out what is going on -
  1061. * I reckon these figures won't hurt MOST drives
  1062. */
  1063. p->sectors = 32;
  1064. p->heads = 4;
  1065. p->cylinders = 512;
  1066. }
  1067. if (raw_cmd.dev == drive)
  1068. mfm_specify ();
  1069. mfm_geometry (drive);
  1070. }
  1071. }
  1072. static struct block_device_operations mfm_fops =
  1073. {
  1074. .owner = THIS_MODULE,
  1075. .ioctl = mfm_ioctl,
  1076. };
  1077. /*
  1078. * See if there is a controller at the address presently at mfm_addr
  1079. *
  1080. * We check to see if the controller is busy - if it is, we abort it first,
  1081. * and check that the chip is no longer busy after at least 180 clock cycles.
  1082. * We then issue a command and check that the BSY or CPR bits are set.
  1083. */
  1084. static int mfm_probecontroller (unsigned int mfm_addr)
  1085. {
  1086. if (inw (MFM_STATUS) & STAT_BSY) {
  1087. outw (CMD_ABT, MFM_COMMAND);
  1088. udelay (50);
  1089. if (inw (MFM_STATUS) & STAT_BSY)
  1090. return 0;
  1091. }
  1092. if (inw (MFM_STATUS) & STAT_CED)
  1093. outw (CMD_RCAL, MFM_COMMAND);
  1094. outw (CMD_SEK, MFM_COMMAND);
  1095. if (inw (MFM_STATUS) & (STAT_BSY | STAT_CPR)) {
  1096. unsigned int count = 2000;
  1097. while (inw (MFM_STATUS) & STAT_BSY) {
  1098. udelay (500);
  1099. if (!--count)
  1100. return 0;
  1101. }
  1102. outw (CMD_RCAL, MFM_COMMAND);
  1103. }
  1104. return 1;
  1105. }
  1106. static int mfm_do_init(unsigned char irqmask)
  1107. {
  1108. int i, ret;
  1109. printk("mfm: found at address %08X, interrupt %d\n", mfm_addr, mfm_irq);
  1110. ret = -EBUSY;
  1111. if (!request_region (mfm_addr, 10, "mfm"))
  1112. goto out1;
  1113. ret = register_blkdev(MAJOR_NR, "mfm");
  1114. if (ret)
  1115. goto out2;
  1116. /* Stuff for the assembler routines to get to */
  1117. hdc63463_baseaddress = ioaddr(mfm_addr);
  1118. hdc63463_irqpolladdress = mfm_IRQPollLoc;
  1119. hdc63463_irqpollmask = irqmask;
  1120. mfm_queue = blk_init_queue(do_mfm_request, &mfm_lock);
  1121. if (!mfm_queue)
  1122. goto out2a;
  1123. Busy = 0;
  1124. lastspecifieddrive = -1;
  1125. mfm_drives = mfm_initdrives();
  1126. if (!mfm_drives) {
  1127. ret = -ENODEV;
  1128. goto out3;
  1129. }
  1130. for (i = 0; i < mfm_drives; i++) {
  1131. struct gendisk *disk = alloc_disk(64);
  1132. if (!disk)
  1133. goto Enomem;
  1134. disk->major = MAJOR_NR;
  1135. disk->first_minor = i << 6;
  1136. disk->fops = &mfm_fops;
  1137. sprintf(disk->disk_name, "mfm%c", 'a'+i);
  1138. mfm_gendisk[i] = disk;
  1139. }
  1140. printk("mfm: detected %d hard drive%s\n", mfm_drives,
  1141. mfm_drives == 1 ? "" : "s");
  1142. ret = request_irq(mfm_irq, mfm_interrupt_handler, SA_INTERRUPT, "MFM harddisk", NULL);
  1143. if (ret) {
  1144. printk("mfm: unable to get IRQ%d\n", mfm_irq);
  1145. goto out4;
  1146. }
  1147. if (mfm_irqenable)
  1148. outw(0x80, mfm_irqenable); /* Required to enable IRQs from MFM podule */
  1149. for (i = 0; i < mfm_drives; i++) {
  1150. mfm_geometry(i);
  1151. mfm_gendisk[i]->queue = mfm_queue;
  1152. add_disk(mfm_gendisk[i]);
  1153. }
  1154. return 0;
  1155. out4:
  1156. for (i = 0; i < mfm_drives; i++)
  1157. put_disk(mfm_gendisk[i]);
  1158. out3:
  1159. blk_cleanup_queue(mfm_queue);
  1160. out2a:
  1161. unregister_blkdev(MAJOR_NR, "mfm");
  1162. out2:
  1163. release_region(mfm_addr, 10);
  1164. out1:
  1165. return ret;
  1166. Enomem:
  1167. while (i--)
  1168. put_disk(mfm_gendisk[i]);
  1169. goto out3;
  1170. }
  1171. static void mfm_do_exit(void)
  1172. {
  1173. int i;
  1174. free_irq(mfm_irq, NULL);
  1175. for (i = 0; i < mfm_drives; i++) {
  1176. del_gendisk(mfm_gendisk[i]);
  1177. put_disk(mfm_gendisk[i]);
  1178. }
  1179. blk_cleanup_queue(mfm_queue);
  1180. unregister_blkdev(MAJOR_NR, "mfm");
  1181. if (mfm_addr)
  1182. release_region(mfm_addr, 10);
  1183. }
  1184. static int __devinit mfm_probe(struct expansion_card *ec, struct ecard_id *id)
  1185. {
  1186. if (mfm_addr)
  1187. return -EBUSY;
  1188. mfm_addr = ecard_address(ec, ECARD_IOC, ECARD_MEDIUM) + 0x800;
  1189. mfm_IRQPollLoc = ioaddr(mfm_addr + 0x400);
  1190. mfm_irqenable = mfm_IRQPollLoc;
  1191. mfm_irq = ec->irq;
  1192. return mfm_do_init(0x08);
  1193. }
  1194. static void __devexit mfm_remove(struct expansion_card *ec)
  1195. {
  1196. outw (0, mfm_irqenable); /* Required to enable IRQs from MFM podule */
  1197. mfm_do_exit();
  1198. }
  1199. static const struct ecard_id mfm_cids[] = {
  1200. { MANU_ACORN, PROD_ACORN_MFM },
  1201. { 0xffff, 0xffff },
  1202. };
  1203. static struct ecard_driver mfm_driver = {
  1204. .probe = mfm_probe,
  1205. .remove = __devexit(mfm_remove),
  1206. .id_table = mfm_cids,
  1207. .drv = {
  1208. .name = "mfm",
  1209. },
  1210. };
  1211. /*
  1212. * Look for a MFM controller - first check the motherboard, then the podules
  1213. * The podules have an extra interrupt enable that needs to be played with
  1214. *
  1215. * The HDC is accessed at MEDIUM IOC speeds.
  1216. */
  1217. static int __init mfm_init (void)
  1218. {
  1219. unsigned char irqmask;
  1220. if (mfm_probecontroller(ONBOARD_MFM_ADDRESS)) {
  1221. mfm_addr = ONBOARD_MFM_ADDRESS;
  1222. mfm_IRQPollLoc = IOC_IRQSTATB;
  1223. mfm_irqenable = 0;
  1224. mfm_irq = IRQ_HARDDISK;
  1225. return mfm_do_init(0x08); /* IL3 pin */
  1226. } else {
  1227. return ecard_register_driver(&mfm_driver);
  1228. }
  1229. }
  1230. static void __exit mfm_exit(void)
  1231. {
  1232. if (mfm_addr == ONBOARD_MFM_ADDRESS)
  1233. mfm_do_exit();
  1234. else
  1235. ecard_unregister_driver(&mfm_driver);
  1236. }
  1237. module_init(mfm_init)
  1238. module_exit(mfm_exit)
  1239. MODULE_LICENSE("GPL");