ide-features.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:12k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * linux/drivers/block/ide-features.c Version 0.04 June 9, 2000
  3.  *
  4.  *  Copyright (C) 1999-2000 Linus Torvalds & authors (see below)
  5.  *  
  6.  *  Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
  7.  *
  8.  *  Extracts if ide.c to address the evolving transfer rate code for
  9.  *  the SETFEATURES_XFER callouts.  Various parts of any given function
  10.  *  are credited to previous ATA-IDE maintainers.
  11.  *
  12.  *  Auto-CRC downgrade for Ultra DMA(ing)
  13.  *
  14.  *  May be copied or modified under the terms of the GNU General Public License
  15.  */
  16. #include <linux/config.h>
  17. #define __NO_VERSION__
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/string.h>
  21. #include <linux/kernel.h>
  22. #include <linux/timer.h>
  23. #include <linux/mm.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/major.h>
  26. #include <linux/errno.h>
  27. #include <linux/genhd.h>
  28. #include <linux/blkpg.h>
  29. #include <linux/slab.h>
  30. #include <linux/pci.h>
  31. #include <linux/delay.h>
  32. #include <linux/hdreg.h>
  33. #include <linux/ide.h>
  34. #include <asm/byteorder.h>
  35. #include <asm/irq.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/io.h>
  38. #include <asm/bitops.h>
  39. /*
  40.  * A Verbose noise maker for debugging on the attempted transfer rates.
  41.  */
  42. char *ide_xfer_verbose (byte xfer_rate)
  43. {
  44. switch(xfer_rate) {
  45. case XFER_UDMA_7: return("UDMA 7");
  46. case XFER_UDMA_6: return("UDMA 6");
  47. case XFER_UDMA_5: return("UDMA 5");
  48. case XFER_UDMA_4: return("UDMA 4");
  49. case XFER_UDMA_3: return("UDMA 3");
  50. case XFER_UDMA_2: return("UDMA 2");
  51. case XFER_UDMA_1: return("UDMA 1");
  52. case XFER_UDMA_0: return("UDMA 0");
  53. case XFER_MW_DMA_2: return("MW DMA 2");
  54. case XFER_MW_DMA_1: return("MW DMA 1");
  55. case XFER_MW_DMA_0: return("MW DMA 0");
  56. case XFER_SW_DMA_2: return("SW DMA 2");
  57. case XFER_SW_DMA_1: return("SW DMA 1");
  58. case XFER_SW_DMA_0: return("SW DMA 0");
  59. case XFER_PIO_4: return("PIO 4");
  60. case XFER_PIO_3: return("PIO 3");
  61. case XFER_PIO_2: return("PIO 2");
  62. case XFER_PIO_1: return("PIO 1");
  63. case XFER_PIO_0: return("PIO 0");
  64. case XFER_PIO_SLOW: return("PIO SLOW");
  65. default: return("XFER ERROR");
  66. }
  67. }
  68. /*
  69.  *
  70.  */
  71. char *ide_media_verbose (ide_drive_t *drive)
  72. {
  73. switch (drive->media) {
  74. case ide_scsi: return("scsi   ");
  75. case ide_disk: return("disk   ");
  76. case ide_optical: return("optical");
  77. case ide_cdrom: return("cdrom  ");
  78. case ide_tape: return("tape   ");
  79. case ide_floppy: return("floppy ");
  80. default: return("???????");
  81. }
  82. }
  83. /*
  84.  * A Verbose noise maker for debugging on the attempted dmaing calls.
  85.  */
  86. char *ide_dmafunc_verbose (ide_dma_action_t dmafunc)
  87. {
  88. switch (dmafunc) {
  89. case ide_dma_read: return("ide_dma_read");
  90. case ide_dma_write: return("ide_dma_write");
  91. case ide_dma_begin: return("ide_dma_begin");
  92. case ide_dma_end: return("ide_dma_end:");
  93. case ide_dma_check: return("ide_dma_check");
  94. case ide_dma_on: return("ide_dma_on");
  95. case ide_dma_off: return("ide_dma_off");
  96. case ide_dma_off_quietly: return("ide_dma_off_quietly");
  97. case ide_dma_test_irq: return("ide_dma_test_irq");
  98. case ide_dma_bad_drive: return("ide_dma_bad_drive");
  99. case ide_dma_good_drive: return("ide_dma_good_drive");
  100. case ide_dma_verbose: return("ide_dma_verbose");
  101. case ide_dma_retune: return("ide_dma_retune");
  102. case ide_dma_lostirq: return("ide_dma_lostirq");
  103. case ide_dma_timeout: return("ide_dma_timeout");
  104. default: return("unknown");
  105. }
  106. }
  107. /*
  108.  *
  109.  */
  110. byte ide_auto_reduce_xfer (ide_drive_t *drive)
  111. {
  112. if (!drive->crc_count)
  113. return drive->current_speed;
  114. drive->crc_count = 0;
  115. switch(drive->current_speed) {
  116. case XFER_UDMA_7: return XFER_UDMA_6;
  117. case XFER_UDMA_6: return XFER_UDMA_5;
  118. case XFER_UDMA_5: return XFER_UDMA_4;
  119. case XFER_UDMA_4: return XFER_UDMA_3;
  120. case XFER_UDMA_3: return XFER_UDMA_2;
  121. case XFER_UDMA_2: return XFER_UDMA_1;
  122. case XFER_UDMA_1: return XFER_UDMA_0;
  123. case XFER_UDMA_0:
  124. if (drive->id->dma_mword & 0x0004) return XFER_MW_DMA_2;
  125. else if (drive->id->dma_mword & 0x0002) return XFER_MW_DMA_1;
  126. else if (drive->id->dma_mword & 0x0001) return XFER_MW_DMA_0;
  127. else return XFER_PIO_4;
  128. case XFER_MW_DMA_2: return XFER_MW_DMA_1;
  129. case XFER_MW_DMA_1: return XFER_MW_DMA_0;
  130. case XFER_MW_DMA_0:
  131. if (drive->id->dma_1word & 0x0004) return XFER_SW_DMA_2;
  132. else if (drive->id->dma_1word & 0x0002) return XFER_SW_DMA_1;
  133. else if (drive->id->dma_1word & 0x0001) return XFER_SW_DMA_0;
  134. else return XFER_PIO_4;
  135. case XFER_SW_DMA_2: return XFER_SW_DMA_1;
  136. case XFER_SW_DMA_1: return XFER_SW_DMA_0;
  137. case XFER_SW_DMA_0:
  138. {
  139. return XFER_PIO_4;
  140. }
  141. case XFER_PIO_4: return XFER_PIO_3;
  142. case XFER_PIO_3: return XFER_PIO_2;
  143. case XFER_PIO_2: return XFER_PIO_1;
  144. case XFER_PIO_1: return XFER_PIO_0;
  145. case XFER_PIO_0:
  146. default: return XFER_PIO_SLOW;
  147. }
  148. }
  149. /*
  150.  * Update the 
  151.  */
  152. int ide_driveid_update (ide_drive_t *drive)
  153. {
  154. /*
  155.  * Re-read drive->id for possible DMA mode
  156.  * change (copied from ide-probe.c)
  157.  */
  158. struct hd_driveid *id;
  159. unsigned long timeout, flags;
  160. SELECT_MASK(HWIF(drive), drive, 1);
  161. if (IDE_CONTROL_REG)
  162. OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
  163. ide_delay_50ms();
  164. OUT_BYTE(WIN_IDENTIFY, IDE_COMMAND_REG);
  165. timeout = jiffies + WAIT_WORSTCASE;
  166. do {
  167. if (0 < (signed long)(jiffies - timeout)) {
  168. SELECT_MASK(HWIF(drive), drive, 0);
  169. return 0; /* drive timed-out */
  170. }
  171. ide_delay_50ms(); /* give drive a breather */
  172. } while (IN_BYTE(IDE_ALTSTATUS_REG) & BUSY_STAT);
  173. ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
  174. if (!OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
  175. SELECT_MASK(HWIF(drive), drive, 0);
  176. printk("%s: CHECK for good STATUSn", drive->name);
  177. return 0;
  178. }
  179. __save_flags(flags); /* local CPU only */
  180. __cli(); /* local CPU only; some systems need this */
  181. SELECT_MASK(HWIF(drive), drive, 0);
  182. id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
  183. if (!id) {
  184. __restore_flags(flags); /* local CPU only */
  185. return 0;
  186. }
  187. ide_input_data(drive, id, SECTOR_WORDS);
  188. (void) GET_STAT(); /* clear drive IRQ */
  189. ide__sti(); /* local CPU only */
  190. __restore_flags(flags); /* local CPU only */
  191. ide_fix_driveid(id);
  192. if (id) {
  193. drive->id->dma_ultra = id->dma_ultra;
  194. drive->id->dma_mword = id->dma_mword;
  195. drive->id->dma_1word = id->dma_1word;
  196. /* anything more ? */
  197. kfree(id);
  198. }
  199. return 1;
  200. }
  201. /*
  202.  * Verify that we are doing an approved SETFEATURES_XFER with respect
  203.  * to the hardware being able to support request.  Since some hardware
  204.  * can improperly report capabilties, we check to see if the host adapter
  205.  * in combination with the device (usually a disk) properly detect
  206.  * and acknowledge each end of the ribbon.
  207.  */
  208. int ide_ata66_check (ide_drive_t *drive, byte cmd, byte nsect, byte feature)
  209. {
  210. if ((cmd == WIN_SETFEATURES) &&
  211.     (nsect > XFER_UDMA_2) &&
  212.     (feature == SETFEATURES_XFER)) {
  213. if (!HWIF(drive)->udma_four) {
  214. printk("%s: Speed warnings UDMA 3/4/5 is not functional.n", HWIF(drive)->name);
  215. return 1;
  216. }
  217. #ifndef CONFIG_IDEDMA_IVB
  218. if ((drive->id->hw_config & 0x6000) == 0) {
  219. #else /* !CONFIG_IDEDMA_IVB */
  220. if (((drive->id->hw_config & 0x2000) == 0) ||
  221.     ((drive->id->hw_config & 0x4000) == 0)) {
  222. #endif /* CONFIG_IDEDMA_IVB */
  223. printk("%s: Speed warnings UDMA 3/4/5 is not functional.n", drive->name);
  224. return 1;
  225. }
  226. }
  227. return 0;
  228. }
  229. /*
  230.  * Backside of HDIO_DRIVE_CMD call of SETFEATURES_XFER.
  231.  * 1 : Safe to update drive->id DMA registers.
  232.  * 0 : OOPs not allowed.
  233.  */
  234. int set_transfer (ide_drive_t *drive, byte cmd, byte nsect, byte feature)
  235. {
  236. if ((cmd == WIN_SETFEATURES) &&
  237.     (nsect >= XFER_SW_DMA_0) &&
  238.     (feature == SETFEATURES_XFER) &&
  239.     (drive->id->dma_ultra ||
  240.      drive->id->dma_mword ||
  241.      drive->id->dma_1word))
  242. return 1;
  243. return 0;
  244. }
  245. /*
  246.  *  All hosts that use the 80c ribbon mus use!
  247.  */
  248. byte eighty_ninty_three (ide_drive_t *drive)
  249. {
  250. return ((byte) ((HWIF(drive)->udma_four) &&
  251. #ifndef CONFIG_IDEDMA_IVB
  252. (drive->id->hw_config & 0x4000) &&
  253. #endif /* CONFIG_IDEDMA_IVB */
  254. (drive->id->hw_config & 0x6000)) ? 1 : 0);
  255. }
  256. /*
  257.  * Similar to ide_wait_stat(), except it never calls ide_error internally.
  258.  * This is a kludge to handle the new ide_config_drive_speed() function,
  259.  * and should not otherwise be used anywhere.  Eventually, the tuneproc's
  260.  * should be updated to return ide_startstop_t, in which case we can get
  261.  * rid of this abomination again.  :)   -ml
  262.  *
  263.  * It is gone..........
  264.  *
  265.  * const char *msg == consider adding for verbose errors.
  266.  */
  267. int ide_config_drive_speed (ide_drive_t *drive, byte speed)
  268. {
  269. ide_hwif_t *hwif = HWIF(drive);
  270. int i, error = 1;
  271. byte stat;
  272. #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
  273. byte unit = (drive->select.b.unit & 0x01);
  274. outb(inb(hwif->dma_base+2) & ~(1<<(5+unit)), hwif->dma_base+2);
  275. #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
  276. /*
  277.  * Don't use ide_wait_cmd here - it will
  278.  * attempt to set_geometry and recalibrate,
  279.  * but for some reason these don't work at
  280.  * this point (lost interrupt).
  281.  */
  282.         /*
  283.          * Select the drive, and issue the SETFEATURES command
  284.          */
  285. disable_irq(hwif->irq); /* disable_irq_nosync ?? */
  286. udelay(1);
  287. SELECT_DRIVE(HWIF(drive), drive);
  288. SELECT_MASK(HWIF(drive), drive, 0);
  289. udelay(1);
  290. if (IDE_CONTROL_REG)
  291. OUT_BYTE(drive->ctl | 2, IDE_CONTROL_REG);
  292. OUT_BYTE(speed, IDE_NSECTOR_REG);
  293. OUT_BYTE(SETFEATURES_XFER, IDE_FEATURE_REG);
  294. OUT_BYTE(WIN_SETFEATURES, IDE_COMMAND_REG);
  295. if ((IDE_CONTROL_REG) && (drive->quirk_list == 2))
  296. OUT_BYTE(drive->ctl, IDE_CONTROL_REG);
  297. udelay(1);
  298. /*
  299.  * Wait for drive to become non-BUSY
  300.  */
  301. if ((stat = GET_STAT()) & BUSY_STAT) {
  302. unsigned long flags, timeout;
  303. __save_flags(flags); /* local CPU only */
  304. ide__sti(); /* local CPU only -- for jiffies */
  305. timeout = jiffies + WAIT_CMD;
  306. while ((stat = GET_STAT()) & BUSY_STAT) {
  307. if (0 < (signed long)(jiffies - timeout))
  308. break;
  309. }
  310. __restore_flags(flags); /* local CPU only */
  311. }
  312. /*
  313.  * Allow status to settle, then read it again.
  314.  * A few rare drives vastly violate the 400ns spec here,
  315.  * so we'll wait up to 10usec for a "good" status
  316.  * rather than expensively fail things immediately.
  317.  * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
  318.  */
  319. for (i = 0; i < 10; i++) {
  320. udelay(1);
  321. if (OK_STAT((stat = GET_STAT()), DRIVE_READY, BUSY_STAT|DRQ_STAT|ERR_STAT)) {
  322. error = 0;
  323. break;
  324. }
  325. }
  326. SELECT_MASK(HWIF(drive), drive, 0);
  327. enable_irq(hwif->irq);
  328. if (error) {
  329. (void) ide_dump_status(drive, "set_drive_speed_status", stat);
  330. return error;
  331. }
  332. drive->id->dma_ultra &= ~0xFF00;
  333. drive->id->dma_mword &= ~0x0F00;
  334. drive->id->dma_1word &= ~0x0F00;
  335. #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
  336. if (speed > XFER_PIO_4) {
  337. outb(inb(hwif->dma_base+2)|(1<<(5+unit)), hwif->dma_base+2);
  338. } else {
  339. outb(inb(hwif->dma_base+2) & ~(1<<(5+unit)), hwif->dma_base+2);
  340. }
  341. #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
  342. switch(speed) {
  343. case XFER_UDMA_7:   drive->id->dma_ultra |= 0x8080; break;
  344. case XFER_UDMA_6:   drive->id->dma_ultra |= 0x4040; break;
  345. case XFER_UDMA_5:   drive->id->dma_ultra |= 0x2020; break;
  346. case XFER_UDMA_4:   drive->id->dma_ultra |= 0x1010; break;
  347. case XFER_UDMA_3:   drive->id->dma_ultra |= 0x0808; break;
  348. case XFER_UDMA_2:   drive->id->dma_ultra |= 0x0404; break;
  349. case XFER_UDMA_1:   drive->id->dma_ultra |= 0x0202; break;
  350. case XFER_UDMA_0:   drive->id->dma_ultra |= 0x0101; break;
  351. case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
  352. case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
  353. case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
  354. case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
  355. case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
  356. case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
  357. default: break;
  358. }
  359. return error;
  360. }
  361. EXPORT_SYMBOL(ide_auto_reduce_xfer);
  362. EXPORT_SYMBOL(ide_driveid_update);
  363. EXPORT_SYMBOL(ide_ata66_check);
  364. EXPORT_SYMBOL(set_transfer);
  365. EXPORT_SYMBOL(eighty_ninty_three);
  366. EXPORT_SYMBOL(ide_config_drive_speed);