fdc-io.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. /*
  2. * Copyright (C) 1993-1996 Bas Laarhoven,
  3. * (C) 1996-1997 Claus-Justus Heine.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  15. *
  16. * $Source: /homes/cvs/ftape-stacked/ftape/lowlevel/fdc-io.c,v $
  17. * $Revision: 1.7.4.2 $
  18. * $Date: 1997/11/16 14:48:17 $
  19. *
  20. * This file contains the low-level floppy disk interface code
  21. * for the QIC-40/80/3010/3020 floppy-tape driver "ftape" for
  22. * Linux.
  23. */
  24. #include <linux/config.h> /* for CONFIG_FT_* */
  25. #include <linux/errno.h>
  26. #include <linux/sched.h>
  27. #include <linux/ioport.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kernel.h>
  30. #include <asm/system.h>
  31. #include <asm/io.h>
  32. #include <asm/dma.h>
  33. #include <asm/irq.h>
  34. #include <linux/ftape.h>
  35. #include <linux/qic117.h>
  36. #include "../lowlevel/ftape-tracing.h"
  37. #include "../lowlevel/fdc-io.h"
  38. #include "../lowlevel/fdc-isr.h"
  39. #include "../lowlevel/ftape-io.h"
  40. #include "../lowlevel/ftape-rw.h"
  41. #include "../lowlevel/ftape-ctl.h"
  42. #include "../lowlevel/ftape-calibr.h"
  43. #include "../lowlevel/fc-10.h"
  44. /* Global vars.
  45. */
  46. static int ftape_motor;
  47. volatile int ftape_current_cylinder = -1;
  48. volatile fdc_mode_enum fdc_mode = fdc_idle;
  49. fdc_config_info fdc;
  50. DECLARE_WAIT_QUEUE_HEAD(ftape_wait_intr);
  51. unsigned int ft_fdc_base = CONFIG_FT_FDC_BASE;
  52. unsigned int ft_fdc_irq = CONFIG_FT_FDC_IRQ;
  53. unsigned int ft_fdc_dma = CONFIG_FT_FDC_DMA;
  54. unsigned int ft_fdc_threshold = CONFIG_FT_FDC_THR; /* bytes */
  55. unsigned int ft_fdc_rate_limit = CONFIG_FT_FDC_MAX_RATE; /* bits/sec */
  56. int ft_probe_fc10 = CONFIG_FT_PROBE_FC10;
  57. int ft_mach2 = CONFIG_FT_MACH2;
  58. /* Local vars.
  59. */
  60. static spinlock_t fdc_io_lock;
  61. static unsigned int fdc_calibr_count;
  62. static unsigned int fdc_calibr_time;
  63. static int fdc_status;
  64. volatile __u8 fdc_head; /* FDC head from sector id */
  65. volatile __u8 fdc_cyl; /* FDC track from sector id */
  66. volatile __u8 fdc_sect; /* FDC sector from sector id */
  67. static int fdc_data_rate = 500; /* data rate (Kbps) */
  68. static int fdc_rate_code; /* data rate code (0 == 500 Kbps) */
  69. static int fdc_seek_rate = 2; /* step rate (msec) */
  70. static void (*do_ftape) (void);
  71. static int fdc_fifo_state; /* original fifo setting - fifo enabled */
  72. static int fdc_fifo_thr; /* original fifo setting - threshold */
  73. static int fdc_lock_state; /* original lock setting - locked */
  74. static int fdc_fifo_locked; /* has fifo && lock set ? */
  75. static __u8 fdc_precomp; /* default precomp. value (nsec) */
  76. static __u8 fdc_prec_code; /* fdc precomp. select code */
  77. static char ftape_id[] = "ftape"; /* used by request irq and free irq */
  78. static int fdc_set_seek_rate(int seek_rate);
  79. void fdc_catch_stray_interrupts(int count)
  80. {
  81. unsigned long flags;
  82. spin_lock_irqsave(&fdc_io_lock, flags);
  83. if (count == 0) {
  84. ft_expected_stray_interrupts = 0;
  85. } else {
  86. ft_expected_stray_interrupts += count;
  87. }
  88. spin_unlock_irqrestore(&fdc_io_lock, flags);
  89. }
  90. /* Wait during a timeout period for a given FDC status.
  91. * If usecs == 0 then just test status, else wait at least for usecs.
  92. * Returns -ETIME on timeout. Function must be calibrated first !
  93. */
  94. static int fdc_wait(unsigned int usecs, __u8 mask, __u8 state)
  95. {
  96. int count_1 = (fdc_calibr_count * usecs +
  97. fdc_calibr_count - 1) / fdc_calibr_time;
  98. do {
  99. fdc_status = inb_p(fdc.msr);
  100. if ((fdc_status & mask) == state) {
  101. return 0;
  102. }
  103. } while (count_1-- >= 0);
  104. return -ETIME;
  105. }
  106. int fdc_ready_wait(unsigned int usecs)
  107. {
  108. return fdc_wait(usecs, FDC_DATA_READY | FDC_BUSY, FDC_DATA_READY);
  109. }
  110. /* Why can't we just use udelay()?
  111. */
  112. static void fdc_usec_wait(unsigned int usecs)
  113. {
  114. fdc_wait(usecs, 0, 1); /* will always timeout ! */
  115. }
  116. static int fdc_ready_out_wait(unsigned int usecs)
  117. {
  118. fdc_usec_wait(FT_RQM_DELAY); /* wait for valid RQM status */
  119. return fdc_wait(usecs, FDC_DATA_OUT_READY, FDC_DATA_OUT_READY);
  120. }
  121. void fdc_wait_calibrate(void)
  122. {
  123. ftape_calibrate("fdc_wait",
  124. fdc_usec_wait, &fdc_calibr_count, &fdc_calibr_time);
  125. }
  126. /* Wait for a (short) while for the FDC to become ready
  127. * and transfer the next command byte.
  128. * Return -ETIME on timeout on getting ready (depends on hardware!).
  129. */
  130. static int fdc_write(const __u8 data)
  131. {
  132. fdc_usec_wait(FT_RQM_DELAY); /* wait for valid RQM status */
  133. if (fdc_wait(150, FDC_DATA_READY_MASK, FDC_DATA_IN_READY) < 0) {
  134. return -ETIME;
  135. } else {
  136. outb(data, fdc.fifo);
  137. return 0;
  138. }
  139. }
  140. /* Wait for a (short) while for the FDC to become ready
  141. * and transfer the next result byte.
  142. * Return -ETIME if timeout on getting ready (depends on hardware!).
  143. */
  144. static int fdc_read(__u8 * data)
  145. {
  146. fdc_usec_wait(FT_RQM_DELAY); /* wait for valid RQM status */
  147. if (fdc_wait(150, FDC_DATA_READY_MASK, FDC_DATA_OUT_READY) < 0) {
  148. return -ETIME;
  149. } else {
  150. *data = inb(fdc.fifo);
  151. return 0;
  152. }
  153. }
  154. /* Output a cmd_len long command string to the FDC.
  155. * The FDC should be ready to receive a new command or
  156. * an error (EBUSY or ETIME) will occur.
  157. */
  158. int fdc_command(const __u8 * cmd_data, int cmd_len)
  159. {
  160. int result = 0;
  161. unsigned long flags;
  162. int count = cmd_len;
  163. int retry = 0;
  164. #ifdef TESTING
  165. static unsigned int last_time;
  166. unsigned int time;
  167. #endif
  168. TRACE_FUN(ft_t_any);
  169. fdc_usec_wait(FT_RQM_DELAY); /* wait for valid RQM status */
  170. spin_lock_irqsave(&fdc_io_lock, flags);
  171. if (!in_interrupt())
  172. /* Yes, I know, too much comments inside this function
  173. * ...
  174. *
  175. * Yet another bug in the original driver. All that
  176. * havoc is caused by the fact that the isr() sends
  177. * itself a command to the floppy tape driver (pause,
  178. * micro step pause). Now, the problem is that
  179. * commands are transmitted via the fdc_seek
  180. * command. But: the fdc performs seeks in the
  181. * background i.e. it doesn't signal busy while
  182. * sending the step pulses to the drive. Therefore the
  183. * non-interrupt level driver has no chance to tell
  184. * whether the isr() just has issued a seek. Therefore
  185. * we HAVE TO have a look at the ft_hide_interrupt
  186. * flag: it signals the non-interrupt level part of
  187. * the driver that it has to wait for the fdc until it
  188. * has completet seeking.
  189. *
  190. * THIS WAS PRESUMABLY THE REASON FOR ALL THAT
  191. * "fdc_read timeout" errors, I HOPE :-)
  192. */
  193. if (ft_hide_interrupt) {
  194. restore_flags(flags);
  195. TRACE(ft_t_info,
  196. "Waiting for the isr() completing fdc_seek()");
  197. if (fdc_interrupt_wait(2 * FT_SECOND) < 0) {
  198. TRACE(ft_t_warn,
  199. "Warning: timeout waiting for isr() seek to complete");
  200. }
  201. if (ft_hide_interrupt || !ft_seek_completed) {
  202. /* There cannot be another
  203. * interrupt. The isr() only stops
  204. * the tape and the next interrupt
  205. * won't come until we have send our
  206. * command to the drive.
  207. */
  208. TRACE_ABORT(-EIO, ft_t_bug,
  209. "BUG? isr() is still seeking?\n"
  210. KERN_INFO "hide: %d\n"
  211. KERN_INFO "seek: %d",
  212. ft_hide_interrupt,
  213. ft_seek_completed);
  214. }
  215. fdc_usec_wait(FT_RQM_DELAY); /* wait for valid RQM status */
  216. spin_lock_irqsave(&fdc_io_lock, flags);
  217. }
  218. fdc_status = inb(fdc.msr);
  219. if ((fdc_status & FDC_DATA_READY_MASK) != FDC_DATA_IN_READY) {
  220. spin_unlock_irqrestore(&fdc_io_lock, flags);
  221. TRACE_ABORT(-EBUSY, ft_t_err, "fdc not ready");
  222. }
  223. fdc_mode = *cmd_data; /* used by isr */
  224. #ifdef TESTING
  225. if (fdc_mode == FDC_SEEK) {
  226. time = ftape_timediff(last_time, ftape_timestamp());
  227. if (time < 6000) {
  228. TRACE(ft_t_bug,"Warning: short timeout between seek commands: %d",
  229. time);
  230. }
  231. }
  232. #endif
  233. if (!in_interrupt()) {
  234. /* shouldn't be cleared if called from isr
  235. */
  236. ft_interrupt_seen = 0;
  237. }
  238. while (count) {
  239. result = fdc_write(*cmd_data);
  240. if (result < 0) {
  241. TRACE(ft_t_fdc_dma,
  242. "fdc_mode = %02x, status = %02x at index %d",
  243. (int) fdc_mode, (int) fdc_status,
  244. cmd_len - count);
  245. if (++retry <= 3) {
  246. TRACE(ft_t_warn, "fdc_write timeout, retry");
  247. } else {
  248. TRACE(ft_t_err, "fdc_write timeout, fatal");
  249. /* recover ??? */
  250. break;
  251. }
  252. } else {
  253. --count;
  254. ++cmd_data;
  255. }
  256. }
  257. #ifdef TESTING
  258. if (fdc_mode == FDC_SEEK) {
  259. last_time = ftape_timestamp();
  260. }
  261. #endif
  262. spin_unlock_irqrestore(&fdc_io_lock, flags);
  263. TRACE_EXIT result;
  264. }
  265. /* Input a res_len long result string from the FDC.
  266. * The FDC should be ready to send the result or an error
  267. * (EBUSY or ETIME) will occur.
  268. */
  269. int fdc_result(__u8 * res_data, int res_len)
  270. {
  271. int result = 0;
  272. unsigned long flags;
  273. int count = res_len;
  274. int retry = 0;
  275. TRACE_FUN(ft_t_any);
  276. spin_lock_irqsave(&fdc_io_lock, flags);
  277. fdc_status = inb(fdc.msr);
  278. if ((fdc_status & FDC_DATA_READY_MASK) != FDC_DATA_OUT_READY) {
  279. TRACE(ft_t_err, "fdc not ready");
  280. result = -EBUSY;
  281. } else while (count) {
  282. if (!(fdc_status & FDC_BUSY)) {
  283. spin_unlock_irqrestore(&fdc_io_lock, flags);
  284. TRACE_ABORT(-EIO, ft_t_err, "premature end of result phase");
  285. }
  286. result = fdc_read(res_data);
  287. if (result < 0) {
  288. TRACE(ft_t_fdc_dma,
  289. "fdc_mode = %02x, status = %02x at index %d",
  290. (int) fdc_mode,
  291. (int) fdc_status,
  292. res_len - count);
  293. if (++retry <= 3) {
  294. TRACE(ft_t_warn, "fdc_read timeout, retry");
  295. } else {
  296. TRACE(ft_t_err, "fdc_read timeout, fatal");
  297. /* recover ??? */
  298. break;
  299. ++retry;
  300. }
  301. } else {
  302. --count;
  303. ++res_data;
  304. }
  305. }
  306. spin_unlock_irqrestore(&fdc_io_lock, flags);
  307. fdc_usec_wait(FT_RQM_DELAY); /* allow FDC to negate BSY */
  308. TRACE_EXIT result;
  309. }
  310. /* Handle command and result phases for
  311. * commands without data phase.
  312. */
  313. static int fdc_issue_command(const __u8 * out_data, int out_count,
  314. __u8 * in_data, int in_count)
  315. {
  316. TRACE_FUN(ft_t_any);
  317. if (out_count > 0) {
  318. TRACE_CATCH(fdc_command(out_data, out_count),);
  319. }
  320. /* will take 24 - 30 usec for fdc_sense_drive_status and
  321. * fdc_sense_interrupt_status commands.
  322. * 35 fails sometimes (5/9/93 SJL)
  323. * On a loaded system it incidentally takes longer than
  324. * this for the fdc to get ready ! ?????? WHY ??????
  325. * So until we know what's going on use a very long timeout.
  326. */
  327. TRACE_CATCH(fdc_ready_out_wait(500 /* usec */),);
  328. if (in_count > 0) {
  329. TRACE_CATCH(fdc_result(in_data, in_count),
  330. TRACE(ft_t_err, "result phase aborted"));
  331. }
  332. TRACE_EXIT 0;
  333. }
  334. /* Wait for FDC interrupt with timeout (in milliseconds).
  335. * Signals are blocked so the wait will not be aborted.
  336. * Note: interrupts must be enabled ! (23/05/93 SJL)
  337. */
  338. int fdc_interrupt_wait(unsigned int time)
  339. {
  340. DECLARE_WAITQUEUE(wait,current);
  341. sigset_t old_sigmask;
  342. static int resetting;
  343. long timeout;
  344. TRACE_FUN(ft_t_fdc_dma);
  345. if (waitqueue_active(&ftape_wait_intr)) {
  346. TRACE_ABORT(-EIO, ft_t_err, "error: nested call");
  347. }
  348. /* timeout time will be up to USPT microseconds too long ! */
  349. timeout = (1000 * time + FT_USPT - 1) / FT_USPT;
  350. spin_lock_irq(&current->sighand->siglock);
  351. old_sigmask = current->blocked;
  352. sigfillset(&current->blocked);
  353. recalc_sigpending();
  354. spin_unlock_irq(&current->sighand->siglock);
  355. set_current_state(TASK_INTERRUPTIBLE);
  356. add_wait_queue(&ftape_wait_intr, &wait);
  357. while (!ft_interrupt_seen && timeout) {
  358. set_current_state(TASK_INTERRUPTIBLE);
  359. timeout = schedule_timeout(timeout);
  360. }
  361. spin_lock_irq(&current->sighand->siglock);
  362. current->blocked = old_sigmask;
  363. recalc_sigpending();
  364. spin_unlock_irq(&current->sighand->siglock);
  365. remove_wait_queue(&ftape_wait_intr, &wait);
  366. /* the following IS necessary. True: as well
  367. * wake_up_interruptible() as the schedule() set TASK_RUNNING
  368. * when they wakeup a task, BUT: it may very well be that
  369. * ft_interrupt_seen is already set to 1 when we enter here
  370. * in which case schedule() gets never called, and
  371. * TASK_RUNNING never set. This has the funny effect that we
  372. * execute all the code until we leave kernel space, but then
  373. * the task is stopped (a task CANNOT be preempted while in
  374. * kernel mode. Sending a pair of SIGSTOP/SIGCONT to the
  375. * tasks wakes it up again. Funny! :-)
  376. */
  377. current->state = TASK_RUNNING;
  378. if (ft_interrupt_seen) { /* woken up by interrupt */
  379. ft_interrupt_seen = 0;
  380. TRACE_EXIT 0;
  381. }
  382. /* Original comment:
  383. * In first instance, next statement seems unnecessary since
  384. * it will be cleared in fdc_command. However, a small part of
  385. * the software seems to rely on this being cleared here
  386. * (ftape_close might fail) so stick to it until things get fixed !
  387. */
  388. /* My deeply sought of knowledge:
  389. * Behold NO! It is obvious. fdc_reset() doesn't call fdc_command()
  390. * but nevertheless uses fdc_interrupt_wait(). OF COURSE this needs to
  391. * be reset here.
  392. */
  393. ft_interrupt_seen = 0; /* clear for next call */
  394. if (!resetting) {
  395. resetting = 1; /* break infinite recursion if reset fails */
  396. TRACE(ft_t_any, "cleanup reset");
  397. fdc_reset();
  398. resetting = 0;
  399. }
  400. TRACE_EXIT (signal_pending(current)) ? -EINTR : -ETIME;
  401. }
  402. /* Start/stop drive motor. Enable DMA mode.
  403. */
  404. void fdc_motor(int motor)
  405. {
  406. int unit = ft_drive_sel;
  407. int data = unit | FDC_RESET_NOT | FDC_DMA_MODE;
  408. TRACE_FUN(ft_t_any);
  409. ftape_motor = motor;
  410. if (ftape_motor) {
  411. data |= FDC_MOTOR_0 << unit;
  412. TRACE(ft_t_noise, "turning motor %d on", unit);
  413. } else {
  414. TRACE(ft_t_noise, "turning motor %d off", unit);
  415. }
  416. if (ft_mach2) {
  417. outb_p(data, fdc.dor2);
  418. } else {
  419. outb_p(data, fdc.dor);
  420. }
  421. ftape_sleep(10 * FT_MILLISECOND);
  422. TRACE_EXIT;
  423. }
  424. static void fdc_update_dsr(void)
  425. {
  426. TRACE_FUN(ft_t_any);
  427. TRACE(ft_t_flow, "rate = %d Kbps, precomp = %d ns",
  428. fdc_data_rate, fdc_precomp);
  429. if (fdc.type >= i82077) {
  430. outb_p((fdc_rate_code & 0x03) | fdc_prec_code, fdc.dsr);
  431. } else {
  432. outb_p(fdc_rate_code & 0x03, fdc.ccr);
  433. }
  434. TRACE_EXIT;
  435. }
  436. void fdc_set_write_precomp(int precomp)
  437. {
  438. TRACE_FUN(ft_t_any);
  439. TRACE(ft_t_noise, "New precomp: %d nsec", precomp);
  440. fdc_precomp = precomp;
  441. /* write precompensation can be set in multiples of 41.67 nsec.
  442. * round the parameter to the nearest multiple and convert it
  443. * into a fdc setting. Note that 0 means default to the fdc,
  444. * 7 is used instead of that.
  445. */
  446. fdc_prec_code = ((fdc_precomp + 21) / 42) << 2;
  447. if (fdc_prec_code == 0 || fdc_prec_code > (6 << 2)) {
  448. fdc_prec_code = 7 << 2;
  449. }
  450. fdc_update_dsr();
  451. TRACE_EXIT;
  452. }
  453. /* Reprogram the 82078 registers to use Data Rate Table 1 on all drives.
  454. */
  455. static void fdc_set_drive_specs(void)
  456. {
  457. __u8 cmd[] = { FDC_DRIVE_SPEC, 0x00, 0x00, 0x00, 0x00, 0xc0};
  458. int result;
  459. TRACE_FUN(ft_t_any);
  460. TRACE(ft_t_flow, "Setting of drive specs called");
  461. if (fdc.type >= i82078_1) {
  462. cmd[1] = (0 << 5) | (2 << 2);
  463. cmd[2] = (1 << 5) | (2 << 2);
  464. cmd[3] = (2 << 5) | (2 << 2);
  465. cmd[4] = (3 << 5) | (2 << 2);
  466. result = fdc_command(cmd, NR_ITEMS(cmd));
  467. if (result < 0) {
  468. TRACE(ft_t_err, "Setting of drive specs failed");
  469. }
  470. }
  471. TRACE_EXIT;
  472. }
  473. /* Select clock for fdc, must correspond with tape drive setting !
  474. * This also influences the fdc timing so we must adjust some values.
  475. */
  476. int fdc_set_data_rate(int rate)
  477. {
  478. int bad_rate = 0;
  479. TRACE_FUN(ft_t_any);
  480. /* Select clock for fdc, must correspond with tape drive setting !
  481. * This also influences the fdc timing so we must adjust some values.
  482. */
  483. TRACE(ft_t_fdc_dma, "new rate = %d", rate);
  484. switch (rate) {
  485. case 250:
  486. fdc_rate_code = fdc_data_rate_250;
  487. break;
  488. case 500:
  489. fdc_rate_code = fdc_data_rate_500;
  490. break;
  491. case 1000:
  492. if (fdc.type < i82077) {
  493. bad_rate = 1;
  494. } else {
  495. fdc_rate_code = fdc_data_rate_1000;
  496. }
  497. break;
  498. case 2000:
  499. if (fdc.type < i82078_1) {
  500. bad_rate = 1;
  501. } else {
  502. fdc_rate_code = fdc_data_rate_2000;
  503. }
  504. break;
  505. default:
  506. bad_rate = 1;
  507. }
  508. if (bad_rate) {
  509. TRACE_ABORT(-EIO,
  510. ft_t_fdc_dma, "%d is not a valid data rate", rate);
  511. }
  512. fdc_data_rate = rate;
  513. fdc_update_dsr();
  514. fdc_set_seek_rate(fdc_seek_rate); /* clock changed! */
  515. ftape_udelay(1000);
  516. TRACE_EXIT 0;
  517. }
  518. /* keep the unit select if keep_select is != 0,
  519. */
  520. static void fdc_dor_reset(int keep_select)
  521. {
  522. __u8 fdc_ctl = ft_drive_sel;
  523. if (keep_select != 0) {
  524. fdc_ctl |= FDC_DMA_MODE;
  525. if (ftape_motor) {
  526. fdc_ctl |= FDC_MOTOR_0 << ft_drive_sel;
  527. }
  528. }
  529. ftape_udelay(10); /* ??? but seems to be necessary */
  530. if (ft_mach2) {
  531. outb_p(fdc_ctl & 0x0f, fdc.dor);
  532. outb_p(fdc_ctl, fdc.dor2);
  533. } else {
  534. outb_p(fdc_ctl, fdc.dor);
  535. }
  536. fdc_usec_wait(10); /* delay >= 14 fdc clocks */
  537. if (keep_select == 0) {
  538. fdc_ctl = 0;
  539. }
  540. fdc_ctl |= FDC_RESET_NOT;
  541. if (ft_mach2) {
  542. outb_p(fdc_ctl & 0x0f, fdc.dor);
  543. outb_p(fdc_ctl, fdc.dor2);
  544. } else {
  545. outb_p(fdc_ctl, fdc.dor);
  546. }
  547. }
  548. /* Reset the floppy disk controller. Leave the ftape_unit selected.
  549. */
  550. void fdc_reset(void)
  551. {
  552. int st0;
  553. int i;
  554. int dummy;
  555. unsigned long flags;
  556. TRACE_FUN(ft_t_any);
  557. spin_lock_irqsave(&fdc_io_lock, flags);
  558. fdc_dor_reset(1); /* keep unit selected */
  559. fdc_mode = fdc_idle;
  560. /* maybe the cli()/sti() pair is not necessary, BUT:
  561. * the following line MUST be here. Otherwise fdc_interrupt_wait()
  562. * won't wait. Note that fdc_reset() is called from
  563. * ftape_dumb_stop() when the fdc is busy transferring data. In this
  564. * case fdc_isr() MOST PROBABLY sets ft_interrupt_seen, and tries
  565. * to get the result bytes from the fdc etc. CLASH.
  566. */
  567. ft_interrupt_seen = 0;
  568. /* Program data rate
  569. */
  570. fdc_update_dsr(); /* restore data rate and precomp */
  571. spin_unlock_irqrestore(&fdc_io_lock, flags);
  572. /*
  573. * Wait for first polling cycle to complete
  574. */
  575. if (fdc_interrupt_wait(1 * FT_SECOND) < 0) {
  576. TRACE(ft_t_err, "no drive polling interrupt!");
  577. } else { /* clear all disk-changed statuses */
  578. for (i = 0; i < 4; ++i) {
  579. if(fdc_sense_interrupt_status(&st0, &dummy) != 0) {
  580. TRACE(ft_t_err, "sense failed for %d", i);
  581. }
  582. if (i == ft_drive_sel) {
  583. ftape_current_cylinder = dummy;
  584. }
  585. }
  586. TRACE(ft_t_noise, "drive polling completed");
  587. }
  588. /*
  589. * SPECIFY COMMAND
  590. */
  591. fdc_set_seek_rate(fdc_seek_rate);
  592. /*
  593. * DRIVE SPECIFICATION COMMAND (if fdc type known)
  594. */
  595. if (fdc.type >= i82078_1) {
  596. fdc_set_drive_specs();
  597. }
  598. TRACE_EXIT;
  599. }
  600. #if !defined(CLK_48MHZ)
  601. # define CLK_48MHZ 1
  602. #endif
  603. /* When we're done, put the fdc into reset mode so that the regular
  604. * floppy disk driver will figure out that something is wrong and
  605. * initialize the controller the way it wants.
  606. */
  607. void fdc_disable(void)
  608. {
  609. __u8 cmd1[] = {FDC_CONFIGURE, 0x00, 0x00, 0x00};
  610. __u8 cmd2[] = {FDC_LOCK};
  611. __u8 cmd3[] = {FDC_UNLOCK};
  612. __u8 stat[1];
  613. TRACE_FUN(ft_t_flow);
  614. if (!fdc_fifo_locked) {
  615. fdc_reset();
  616. TRACE_EXIT;
  617. }
  618. if (fdc_issue_command(cmd3, 1, stat, 1) < 0 || stat[0] != 0x00) {
  619. fdc_dor_reset(0);
  620. TRACE_ABORT(/**/, ft_t_bug,
  621. "couldn't unlock fifo, configuration remains changed");
  622. }
  623. fdc_fifo_locked = 0;
  624. if (CLK_48MHZ && fdc.type >= i82078) {
  625. cmd1[0] |= FDC_CLK48_BIT;
  626. }
  627. cmd1[2] = ((fdc_fifo_state) ? 0 : 0x20) + (fdc_fifo_thr - 1);
  628. if (fdc_command(cmd1, NR_ITEMS(cmd1)) < 0) {
  629. fdc_dor_reset(0);
  630. TRACE_ABORT(/**/, ft_t_bug,
  631. "couldn't reconfigure fifo to old state");
  632. }
  633. if (fdc_lock_state &&
  634. fdc_issue_command(cmd2, 1, stat, 1) < 0) {
  635. fdc_dor_reset(0);
  636. TRACE_ABORT(/**/, ft_t_bug, "couldn't lock old state again");
  637. }
  638. TRACE(ft_t_noise, "fifo restored: %sabled, thr. %d, %slocked",
  639. fdc_fifo_state ? "en" : "dis",
  640. fdc_fifo_thr, (fdc_lock_state) ? "" : "not ");
  641. fdc_dor_reset(0);
  642. TRACE_EXIT;
  643. }
  644. /* Specify FDC seek-rate (milliseconds)
  645. */
  646. static int fdc_set_seek_rate(int seek_rate)
  647. {
  648. /* set step rate, dma mode, and minimal head load and unload times
  649. */
  650. __u8 in[3] = { FDC_SPECIFY, 1, (1 << 1)};
  651. fdc_seek_rate = seek_rate;
  652. in[1] |= (16 - (fdc_data_rate * fdc_seek_rate) / 500) << 4;
  653. return fdc_command(in, 3);
  654. }
  655. /* Sense drive status: get unit's drive status (ST3)
  656. */
  657. int fdc_sense_drive_status(int *st3)
  658. {
  659. __u8 out[2];
  660. __u8 in[1];
  661. TRACE_FUN(ft_t_any);
  662. out[0] = FDC_SENSED;
  663. out[1] = ft_drive_sel;
  664. TRACE_CATCH(fdc_issue_command(out, 2, in, 1),);
  665. *st3 = in[0];
  666. TRACE_EXIT 0;
  667. }
  668. /* Sense Interrupt Status command:
  669. * should be issued at the end of each seek.
  670. * get ST0 and current cylinder.
  671. */
  672. int fdc_sense_interrupt_status(int *st0, int *current_cylinder)
  673. {
  674. __u8 out[1];
  675. __u8 in[2];
  676. TRACE_FUN(ft_t_any);
  677. out[0] = FDC_SENSEI;
  678. TRACE_CATCH(fdc_issue_command(out, 1, in, 2),);
  679. *st0 = in[0];
  680. *current_cylinder = in[1];
  681. TRACE_EXIT 0;
  682. }
  683. /* step to track
  684. */
  685. int fdc_seek(int track)
  686. {
  687. __u8 out[3];
  688. int st0, pcn;
  689. #ifdef TESTING
  690. unsigned int time;
  691. #endif
  692. TRACE_FUN(ft_t_any);
  693. out[0] = FDC_SEEK;
  694. out[1] = ft_drive_sel;
  695. out[2] = track;
  696. #ifdef TESTING
  697. time = ftape_timestamp();
  698. #endif
  699. /* We really need this command to work !
  700. */
  701. ft_seek_completed = 0;
  702. TRACE_CATCH(fdc_command(out, 3),
  703. fdc_reset();
  704. TRACE(ft_t_noise, "destination was: %d, resetting FDC...",
  705. track));
  706. /* Handle interrupts until ft_seek_completed or timeout.
  707. */
  708. for (;;) {
  709. TRACE_CATCH(fdc_interrupt_wait(2 * FT_SECOND),);
  710. if (ft_seek_completed) {
  711. TRACE_CATCH(fdc_sense_interrupt_status(&st0, &pcn),);
  712. if ((st0 & ST0_SEEK_END) == 0) {
  713. TRACE_ABORT(-EIO, ft_t_err,
  714. "no seek-end after seek completion !??");
  715. }
  716. break;
  717. }
  718. }
  719. #ifdef TESTING
  720. time = ftape_timediff(time, ftape_timestamp()) / abs(track - ftape_current_cylinder);
  721. if ((time < 900 || time > 3100) && abs(track - ftape_current_cylinder) > 5) {
  722. TRACE(ft_t_warn, "Wrong FDC STEP interval: %d usecs (%d)",
  723. time, track - ftape_current_cylinder);
  724. }
  725. #endif
  726. /* Verify whether we issued the right tape command.
  727. */
  728. /* Verify that we seek to the proper track. */
  729. if (pcn != track) {
  730. TRACE_ABORT(-EIO, ft_t_err, "bad seek..");
  731. }
  732. ftape_current_cylinder = track;
  733. TRACE_EXIT 0;
  734. }
  735. static int perpend_mode; /* set if fdc is in perpendicular mode */
  736. static int perpend_off(void)
  737. {
  738. __u8 perpend[] = {FDC_PERPEND, 0x00};
  739. TRACE_FUN(ft_t_any);
  740. if (perpend_mode) {
  741. /* Turn off perpendicular mode */
  742. perpend[1] = 0x80;
  743. TRACE_CATCH(fdc_command(perpend, 2),
  744. TRACE(ft_t_err,"Perpendicular mode exit failed!"));
  745. perpend_mode = 0;
  746. }
  747. TRACE_EXIT 0;
  748. }
  749. static int handle_perpend(int segment_id)
  750. {
  751. __u8 perpend[] = {FDC_PERPEND, 0x00};
  752. TRACE_FUN(ft_t_any);
  753. /* When writing QIC-3020 tapes, turn on perpendicular mode
  754. * if tape is moving in forward direction (even tracks).
  755. */
  756. if (ft_qic_std == QIC_TAPE_QIC3020 &&
  757. ((segment_id / ft_segments_per_track) & 1) == 0) {
  758. /* FIXME: some i82077 seem to support perpendicular mode as
  759. * well.
  760. */
  761. #if 0
  762. if (fdc.type < i82077AA) {}
  763. #else
  764. if (fdc.type < i82077 && ft_data_rate < 1000) {
  765. #endif
  766. /* fdc does not support perpendicular mode: complain
  767. */
  768. TRACE_ABORT(-EIO, ft_t_err,
  769. "Your FDC does not support QIC-3020.");
  770. }
  771. perpend[1] = 0x03 /* 0x83 + (0x4 << ft_drive_sel) */ ;
  772. TRACE_CATCH(fdc_command(perpend, 2),
  773. TRACE(ft_t_err,"Perpendicular mode entry failed!"));
  774. TRACE(ft_t_flow, "Perpendicular mode set");
  775. perpend_mode = 1;
  776. TRACE_EXIT 0;
  777. }
  778. TRACE_EXIT perpend_off();
  779. }
  780. static inline void fdc_setup_dma(char mode,
  781. volatile void *addr, unsigned int count)
  782. {
  783. /* Program the DMA controller.
  784. */
  785. disable_dma(fdc.dma);
  786. clear_dma_ff(fdc.dma);
  787. set_dma_mode(fdc.dma, mode);
  788. set_dma_addr(fdc.dma, virt_to_bus((void*)addr));
  789. set_dma_count(fdc.dma, count);
  790. enable_dma(fdc.dma);
  791. }
  792. /* Setup fdc and dma for formatting the next segment
  793. */
  794. int fdc_setup_formatting(buffer_struct * buff)
  795. {
  796. unsigned long flags;
  797. __u8 out[6] = {
  798. FDC_FORMAT, 0x00, 3, 4 * FT_SECTORS_PER_SEGMENT, 0x00, 0x6b
  799. };
  800. TRACE_FUN(ft_t_any);
  801. TRACE_CATCH(handle_perpend(buff->segment_id),);
  802. /* Program the DMA controller.
  803. */
  804. TRACE(ft_t_fdc_dma,
  805. "phys. addr. = %lx", virt_to_bus((void*) buff->ptr));
  806. spin_lock_irqsave(&fdc_io_lock, flags);
  807. fdc_setup_dma(DMA_MODE_WRITE, buff->ptr, FT_SECTORS_PER_SEGMENT * 4);
  808. /* Issue FDC command to start reading/writing.
  809. */
  810. out[1] = ft_drive_sel;
  811. out[4] = buff->gap3;
  812. TRACE_CATCH(fdc_setup_error = fdc_command(out, sizeof(out)),
  813. restore_flags(flags); fdc_mode = fdc_idle);
  814. spin_unlock_irqrestore(&fdc_io_lock, flags);
  815. TRACE_EXIT 0;
  816. }
  817. /* Setup Floppy Disk Controller and DMA to read or write the next cluster
  818. * of good sectors from or to the current segment.
  819. */
  820. int fdc_setup_read_write(buffer_struct * buff, __u8 operation)
  821. {
  822. unsigned long flags;
  823. __u8 out[9];
  824. int dma_mode;
  825. TRACE_FUN(ft_t_any);
  826. switch(operation) {
  827. case FDC_VERIFY:
  828. if (fdc.type < i82077) {
  829. operation = FDC_READ;
  830. }
  831. case FDC_READ:
  832. case FDC_READ_DELETED:
  833. dma_mode = DMA_MODE_READ;
  834. TRACE(ft_t_fdc_dma, "xfer %d sectors to 0x%p",
  835. buff->sector_count, buff->ptr);
  836. TRACE_CATCH(perpend_off(),);
  837. break;
  838. case FDC_WRITE_DELETED:
  839. TRACE(ft_t_noise, "deleting segment %d", buff->segment_id);
  840. case FDC_WRITE:
  841. dma_mode = DMA_MODE_WRITE;
  842. /* When writing QIC-3020 tapes, turn on perpendicular mode
  843. * if tape is moving in forward direction (even tracks).
  844. */
  845. TRACE_CATCH(handle_perpend(buff->segment_id),);
  846. TRACE(ft_t_fdc_dma, "xfer %d sectors from 0x%p",
  847. buff->sector_count, buff->ptr);
  848. break;
  849. default:
  850. TRACE_ABORT(-EIO,
  851. ft_t_bug, "bug: invalid operation parameter");
  852. }
  853. TRACE(ft_t_fdc_dma, "phys. addr. = %lx",virt_to_bus((void*)buff->ptr));
  854. spin_lock_irqsave(&fdc_io_lock, flags);
  855. if (operation != FDC_VERIFY) {
  856. fdc_setup_dma(dma_mode, buff->ptr,
  857. FT_SECTOR_SIZE * buff->sector_count);
  858. }
  859. /* Issue FDC command to start reading/writing.
  860. */
  861. out[0] = operation;
  862. out[1] = ft_drive_sel;
  863. out[2] = buff->cyl;
  864. out[3] = buff->head;
  865. out[4] = buff->sect + buff->sector_offset;
  866. out[5] = 3; /* Sector size of 1K. */
  867. out[6] = out[4] + buff->sector_count - 1; /* last sector */
  868. out[7] = 109; /* Gap length. */
  869. out[8] = 0xff; /* No limit to transfer size. */
  870. TRACE(ft_t_fdc_dma, "C: 0x%02x, H: 0x%02x, R: 0x%02x, cnt: 0x%02x",
  871. out[2], out[3], out[4], out[6] - out[4] + 1);
  872. spin_unlock_irqrestore(&fdc_io_lock, flags);
  873. TRACE_CATCH(fdc_setup_error = fdc_command(out, 9),fdc_mode = fdc_idle);
  874. TRACE_EXIT 0;
  875. }
  876. int fdc_fifo_threshold(__u8 threshold,
  877. int *fifo_state, int *lock_state, int *fifo_thr)
  878. {
  879. const __u8 cmd0[] = {FDC_DUMPREGS};
  880. __u8 cmd1[] = {FDC_CONFIGURE, 0, (0x0f & (threshold - 1)), 0};
  881. const __u8 cmd2[] = {FDC_LOCK};
  882. const __u8 cmd3[] = {FDC_UNLOCK};
  883. __u8 reg[10];
  884. __u8 stat;
  885. int i;
  886. int result;
  887. TRACE_FUN(ft_t_any);
  888. if (CLK_48MHZ && fdc.type >= i82078) {
  889. cmd1[0] |= FDC_CLK48_BIT;
  890. }
  891. /* Dump fdc internal registers for examination
  892. */
  893. TRACE_CATCH(fdc_command(cmd0, NR_ITEMS(cmd0)),
  894. TRACE(ft_t_warn, "dumpreg cmd failed, fifo unchanged"));
  895. /* Now read fdc internal registers from fifo
  896. */
  897. for (i = 0; i < (int)NR_ITEMS(reg); ++i) {
  898. fdc_read(&reg[i]);
  899. TRACE(ft_t_fdc_dma, "Register %d = 0x%02x", i, reg[i]);
  900. }
  901. if (fifo_state && lock_state && fifo_thr) {
  902. *fifo_state = (reg[8] & 0x20) == 0;
  903. *lock_state = reg[7] & 0x80;
  904. *fifo_thr = 1 + (reg[8] & 0x0f);
  905. }
  906. TRACE(ft_t_noise,
  907. "original fifo state: %sabled, threshold %d, %slocked",
  908. ((reg[8] & 0x20) == 0) ? "en" : "dis",
  909. 1 + (reg[8] & 0x0f), (reg[7] & 0x80) ? "" : "not ");
  910. /* If fdc is already locked, unlock it first ! */
  911. if (reg[7] & 0x80) {
  912. fdc_ready_wait(100);
  913. TRACE_CATCH(fdc_issue_command(cmd3, NR_ITEMS(cmd3), &stat, 1),
  914. TRACE(ft_t_bug, "FDC unlock command failed, "
  915. "configuration unchanged"));
  916. }
  917. fdc_fifo_locked = 0;
  918. /* Enable fifo and set threshold at xx bytes to allow a
  919. * reasonably large latency and reduce number of dma bursts.
  920. */
  921. fdc_ready_wait(100);
  922. if ((result = fdc_command(cmd1, NR_ITEMS(cmd1))) < 0) {
  923. TRACE(ft_t_bug, "configure cmd failed, fifo unchanged");
  924. }
  925. /* Now lock configuration so reset will not change it
  926. */
  927. if(fdc_issue_command(cmd2, NR_ITEMS(cmd2), &stat, 1) < 0 ||
  928. stat != 0x10) {
  929. TRACE_ABORT(-EIO, ft_t_bug,
  930. "FDC lock command failed, stat = 0x%02x", stat);
  931. }
  932. fdc_fifo_locked = 1;
  933. TRACE_EXIT result;
  934. }
  935. static int fdc_fifo_enable(void)
  936. {
  937. TRACE_FUN(ft_t_any);
  938. if (fdc_fifo_locked) {
  939. TRACE_ABORT(0, ft_t_warn, "Fifo not enabled because locked");
  940. }
  941. TRACE_CATCH(fdc_fifo_threshold(ft_fdc_threshold /* bytes */,
  942. &fdc_fifo_state,
  943. &fdc_lock_state,
  944. &fdc_fifo_thr),);
  945. TRACE_CATCH(fdc_fifo_threshold(ft_fdc_threshold /* bytes */,
  946. NULL, NULL, NULL),);
  947. TRACE_EXIT 0;
  948. }
  949. /* Determine fd controller type
  950. */
  951. static __u8 fdc_save_state[2];
  952. static int fdc_probe(void)
  953. {
  954. __u8 cmd[1];
  955. __u8 stat[16]; /* must be able to hold dumpregs & save results */
  956. int i;
  957. TRACE_FUN(ft_t_any);
  958. /* Try to find out what kind of fd controller we have to deal with
  959. * Scheme borrowed from floppy driver:
  960. * first try if FDC_DUMPREGS command works
  961. * (this indicates that we have a 82072 or better)
  962. * then try the FDC_VERSION command (82072 doesn't support this)
  963. * then try the FDC_UNLOCK command (some older 82077's don't support this)
  964. * then try the FDC_PARTID command (82078's support this)
  965. */
  966. cmd[0] = FDC_DUMPREGS;
  967. if (fdc_issue_command(cmd, 1, stat, 1) != 0) {
  968. TRACE_ABORT(no_fdc, ft_t_bug, "No FDC found");
  969. }
  970. if (stat[0] == 0x80) {
  971. /* invalid command: must be pre 82072 */
  972. TRACE_ABORT(i8272,
  973. ft_t_warn, "Type 8272A/765A compatible FDC found");
  974. }
  975. fdc_result(&stat[1], 9);
  976. fdc_save_state[0] = stat[7];
  977. fdc_save_state[1] = stat[8];
  978. cmd[0] = FDC_VERSION;
  979. if (fdc_issue_command(cmd, 1, stat, 1) < 0 || stat[0] == 0x80) {
  980. TRACE_ABORT(i8272, ft_t_warn, "Type 82072 FDC found");
  981. }
  982. if (*stat != 0x90) {
  983. TRACE_ABORT(i8272, ft_t_warn, "Unknown FDC found");
  984. }
  985. cmd[0] = FDC_UNLOCK;
  986. if(fdc_issue_command(cmd, 1, stat, 1) < 0 || stat[0] != 0x00) {
  987. TRACE_ABORT(i8272, ft_t_warn,
  988. "Type pre-1991 82077 FDC found, "
  989. "treating it like a 82072");
  990. }
  991. if (fdc_save_state[0] & 0x80) { /* was locked */
  992. cmd[0] = FDC_LOCK; /* restore lock */
  993. (void)fdc_issue_command(cmd, 1, stat, 1);
  994. TRACE(ft_t_warn, "FDC is already locked");
  995. }
  996. /* Test for a i82078 FDC */
  997. cmd[0] = FDC_PARTID;
  998. if (fdc_issue_command(cmd, 1, stat, 1) < 0 || stat[0] == 0x80) {
  999. /* invalid command: not a i82078xx type FDC */
  1000. for (i = 0; i < 4; ++i) {
  1001. outb_p(i, fdc.tdr);
  1002. if ((inb_p(fdc.tdr) & 0x03) != i) {
  1003. TRACE_ABORT(i82077,
  1004. ft_t_warn, "Type 82077 FDC found");
  1005. }
  1006. }
  1007. TRACE_ABORT(i82077AA, ft_t_warn, "Type 82077AA FDC found");
  1008. }
  1009. /* FDC_PARTID cmd succeeded */
  1010. switch (stat[0] >> 5) {
  1011. case 0x0:
  1012. /* i82078SL or i82078-1. The SL part cannot run at
  1013. * 2Mbps (the SL and -1 dies are identical; they are
  1014. * speed graded after production, according to Intel).
  1015. * Some SL's can be detected by doing a SAVE cmd and
  1016. * look at bit 7 of the first byte (the SEL3V# bit).
  1017. * If it is 0, the part runs off 3Volts, and hence it
  1018. * is a SL.
  1019. */
  1020. cmd[0] = FDC_SAVE;
  1021. if(fdc_issue_command(cmd, 1, stat, 16) < 0) {
  1022. TRACE(ft_t_err, "FDC_SAVE failed. Dunno why");
  1023. /* guess we better claim the fdc to be a i82078 */
  1024. TRACE_ABORT(i82078,
  1025. ft_t_warn,
  1026. "Type i82078 FDC (i suppose) found");
  1027. }
  1028. if ((stat[0] & FDC_SEL3V_BIT)) {
  1029. /* fdc running off 5Volts; Pray that it's a i82078-1
  1030. */
  1031. TRACE_ABORT(i82078_1, ft_t_warn,
  1032. "Type i82078-1 or 5Volt i82078SL FDC found");
  1033. }
  1034. TRACE_ABORT(i82078, ft_t_warn,
  1035. "Type 3Volt i82078SL FDC (1Mbps) found");
  1036. case 0x1:
  1037. case 0x2: /* S82078B */
  1038. /* The '78B isn't '78 compatible. Detect it as a '77AA */
  1039. TRACE_ABORT(i82077AA, ft_t_warn, "Type i82077AA FDC found");
  1040. case 0x3: /* NSC PC8744 core; used in several super-IO chips */
  1041. TRACE_ABORT(i82077AA,
  1042. ft_t_warn, "Type 82077AA compatible FDC found");
  1043. default:
  1044. TRACE(ft_t_warn, "A previously undetected FDC found");
  1045. TRACE_ABORT(i82077AA, ft_t_warn,
  1046. "Treating it as a 82077AA. Please report partid= %d",
  1047. stat[0]);
  1048. } /* switch(stat[ 0] >> 5) */
  1049. TRACE_EXIT no_fdc;
  1050. }
  1051. static int fdc_request_regions(void)
  1052. {
  1053. TRACE_FUN(ft_t_flow);
  1054. if (ft_mach2 || ft_probe_fc10) {
  1055. if (!request_region(fdc.sra, 8, "fdc (ft)")) {
  1056. #ifndef BROKEN_FLOPPY_DRIVER
  1057. TRACE_EXIT -EBUSY;
  1058. #else
  1059. TRACE(ft_t_warn,
  1060. "address 0x%03x occupied (by floppy driver?), using it anyway", fdc.sra);
  1061. #endif
  1062. }
  1063. } else {
  1064. if (!request_region(fdc.sra, 6, "fdc (ft)")) {
  1065. #ifndef BROKEN_FLOPPY_DRIVER
  1066. TRACE_EXIT -EBUSY;
  1067. #else
  1068. TRACE(ft_t_warn,
  1069. "address 0x%03x occupied (by floppy driver?), using it anyway", fdc.sra);
  1070. #endif
  1071. }
  1072. if (!request_region(fdc.sra + 7, 1, "fdc (ft)")) {
  1073. #ifndef BROKEN_FLOPPY_DRIVER
  1074. release_region(fdc.sra, 6);
  1075. TRACE_EXIT -EBUSY;
  1076. #else
  1077. TRACE(ft_t_warn,
  1078. "address 0x%03x occupied (by floppy driver?), using it anyway", fdc.sra + 7);
  1079. #endif
  1080. }
  1081. }
  1082. TRACE_EXIT 0;
  1083. }
  1084. void fdc_release_regions(void)
  1085. {
  1086. TRACE_FUN(ft_t_flow);
  1087. if (fdc.sra != 0) {
  1088. if (fdc.dor2 != 0) {
  1089. release_region(fdc.sra, 8);
  1090. } else {
  1091. release_region(fdc.sra, 6);
  1092. release_region(fdc.dir, 1);
  1093. }
  1094. }
  1095. TRACE_EXIT;
  1096. }
  1097. static int fdc_config_regs(unsigned int fdc_base,
  1098. unsigned int fdc_irq,
  1099. unsigned int fdc_dma)
  1100. {
  1101. TRACE_FUN(ft_t_flow);
  1102. fdc.irq = fdc_irq;
  1103. fdc.dma = fdc_dma;
  1104. fdc.sra = fdc_base;
  1105. fdc.srb = fdc_base + 1;
  1106. fdc.dor = fdc_base + 2;
  1107. fdc.tdr = fdc_base + 3;
  1108. fdc.msr = fdc.dsr = fdc_base + 4;
  1109. fdc.fifo = fdc_base + 5;
  1110. fdc.dir = fdc.ccr = fdc_base + 7;
  1111. fdc.dor2 = (ft_mach2 || ft_probe_fc10) ? fdc_base + 6 : 0;
  1112. TRACE_CATCH(fdc_request_regions(), fdc.sra = 0);
  1113. TRACE_EXIT 0;
  1114. }
  1115. static int fdc_config(void)
  1116. {
  1117. static int already_done;
  1118. TRACE_FUN(ft_t_any);
  1119. if (already_done) {
  1120. TRACE_CATCH(fdc_request_regions(),);
  1121. *(fdc.hook) = fdc_isr; /* hook our handler in */
  1122. TRACE_EXIT 0;
  1123. }
  1124. if (ft_probe_fc10) {
  1125. int fc_type;
  1126. TRACE_CATCH(fdc_config_regs(ft_fdc_base,
  1127. ft_fdc_irq, ft_fdc_dma),);
  1128. fc_type = fc10_enable();
  1129. if (fc_type != 0) {
  1130. TRACE(ft_t_warn, "FC-%c0 controller found", '0' + fc_type);
  1131. fdc.type = fc10;
  1132. fdc.hook = &do_ftape;
  1133. *(fdc.hook) = fdc_isr; /* hook our handler in */
  1134. already_done = 1;
  1135. TRACE_EXIT 0;
  1136. } else {
  1137. TRACE(ft_t_warn, "FC-10/20 controller not found");
  1138. fdc_release_regions();
  1139. fdc.type = no_fdc;
  1140. ft_probe_fc10 = 0;
  1141. ft_fdc_base = 0x3f0;
  1142. ft_fdc_irq = 6;
  1143. ft_fdc_dma = 2;
  1144. }
  1145. }
  1146. TRACE(ft_t_warn, "fdc base: 0x%x, irq: %d, dma: %d",
  1147. ft_fdc_base, ft_fdc_irq, ft_fdc_dma);
  1148. TRACE_CATCH(fdc_config_regs(ft_fdc_base, ft_fdc_irq, ft_fdc_dma),);
  1149. fdc.hook = &do_ftape;
  1150. *(fdc.hook) = fdc_isr; /* hook our handler in */
  1151. already_done = 1;
  1152. TRACE_EXIT 0;
  1153. }
  1154. static irqreturn_t ftape_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1155. {
  1156. void (*handler) (void) = *fdc.hook;
  1157. int handled = 0;
  1158. TRACE_FUN(ft_t_any);
  1159. *fdc.hook = NULL;
  1160. if (handler) {
  1161. handled = 1;
  1162. handler();
  1163. } else {
  1164. TRACE(ft_t_bug, "Unexpected ftape interrupt");
  1165. }
  1166. TRACE_EXIT IRQ_RETVAL(handled);
  1167. }
  1168. static int fdc_grab_irq_and_dma(void)
  1169. {
  1170. TRACE_FUN(ft_t_any);
  1171. if (fdc.hook == &do_ftape) {
  1172. /* Get fast interrupt handler.
  1173. */
  1174. if (request_irq(fdc.irq, ftape_interrupt,
  1175. SA_INTERRUPT, "ft", ftape_id)) {
  1176. TRACE_ABORT(-EIO, ft_t_bug,
  1177. "Unable to grab IRQ%d for ftape driver",
  1178. fdc.irq);
  1179. }
  1180. if (request_dma(fdc.dma, ftape_id)) {
  1181. free_irq(fdc.irq, ftape_id);
  1182. TRACE_ABORT(-EIO, ft_t_bug,
  1183. "Unable to grab DMA%d for ftape driver",
  1184. fdc.dma);
  1185. }
  1186. }
  1187. if (ft_fdc_base != 0x3f0 && (ft_fdc_dma == 2 || ft_fdc_irq == 6)) {
  1188. /* Using same dma channel or irq as standard fdc, need
  1189. * to disable the dma-gate on the std fdc. This
  1190. * couldn't be done in the floppy driver as some
  1191. * laptops are using the dma-gate to enter a low power
  1192. * or even suspended state :-(
  1193. */
  1194. outb_p(FDC_RESET_NOT, 0x3f2);
  1195. TRACE(ft_t_noise, "DMA-gate on standard fdc disabled");
  1196. }
  1197. TRACE_EXIT 0;
  1198. }
  1199. int fdc_release_irq_and_dma(void)
  1200. {
  1201. TRACE_FUN(ft_t_any);
  1202. if (fdc.hook == &do_ftape) {
  1203. disable_dma(fdc.dma); /* just in case... */
  1204. free_dma(fdc.dma);
  1205. free_irq(fdc.irq, ftape_id);
  1206. }
  1207. if (ft_fdc_base != 0x3f0 && (ft_fdc_dma == 2 || ft_fdc_irq == 6)) {
  1208. /* Using same dma channel as standard fdc, need to
  1209. * disable the dma-gate on the std fdc. This couldn't
  1210. * be done in the floppy driver as some laptops are
  1211. * using the dma-gate to enter a low power or even
  1212. * suspended state :-(
  1213. */
  1214. outb_p(FDC_RESET_NOT | FDC_DMA_MODE, 0x3f2);
  1215. TRACE(ft_t_noise, "DMA-gate on standard fdc enabled again");
  1216. }
  1217. TRACE_EXIT 0;
  1218. }
  1219. int fdc_init(void)
  1220. {
  1221. TRACE_FUN(ft_t_any);
  1222. /* find a FDC to use */
  1223. TRACE_CATCH(fdc_config(),);
  1224. TRACE_CATCH(fdc_grab_irq_and_dma(), fdc_release_regions());
  1225. ftape_motor = 0;
  1226. fdc_catch_stray_interrupts(0); /* clear number of awainted
  1227. * stray interrupte
  1228. */
  1229. fdc_catch_stray_interrupts(1); /* one always comes (?) */
  1230. TRACE(ft_t_flow, "resetting fdc");
  1231. fdc_set_seek_rate(2); /* use nominal QIC step rate */
  1232. fdc_reset(); /* init fdc & clear track counters */
  1233. if (fdc.type == no_fdc) { /* no FC-10 or FC-20 found */
  1234. fdc.type = fdc_probe();
  1235. fdc_reset(); /* update with new knowledge */
  1236. }
  1237. if (fdc.type == no_fdc) {
  1238. fdc_release_irq_and_dma();
  1239. fdc_release_regions();
  1240. TRACE_EXIT -ENXIO;
  1241. }
  1242. if (fdc.type >= i82077) {
  1243. if (fdc_fifo_enable() < 0) {
  1244. TRACE(ft_t_warn, "couldn't enable fdc fifo !");
  1245. } else {
  1246. TRACE(ft_t_flow, "fdc fifo enabled and locked");
  1247. }
  1248. }
  1249. TRACE_EXIT 0;
  1250. }