ns87415.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/drivers/ide/ns87415.c Version 1.01  Mar. 18, 2000
  3.  *
  4.  * Copyright (C) 1997-1998 Mark Lord <mlord@pobox.com>
  5.  * Copyright (C) 1998 Eddie C. Dost <ecd@skynet.be>
  6.  * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
  7.  *
  8.  * Inspired by an earlier effort from David S. Miller <davem@redhat.com>
  9.  */
  10. #include <linux/config.h>
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/timer.h>
  14. #include <linux/mm.h>
  15. #include <linux/ioport.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/hdreg.h>
  19. #include <linux/pci.h>
  20. #include <linux/delay.h>
  21. #include <linux/ide.h>
  22. #include <linux/init.h>
  23. #include <asm/io.h>
  24. static unsigned int ns87415_count = 0, ns87415_control[MAX_HWIFS] = { 0 };
  25. /*
  26.  * This routine either enables/disables (according to drive->present)
  27.  * the IRQ associated with the port (HWIF(drive)),
  28.  * and selects either PIO or DMA handshaking for the next I/O operation.
  29.  */
  30. static void ns87415_prepare_drive (ide_drive_t *drive, unsigned int use_dma)
  31. {
  32. ide_hwif_t *hwif = HWIF(drive);
  33. unsigned int bit, other, new, *old = (unsigned int *) hwif->select_data;
  34. struct pci_dev *dev = hwif->pci_dev;
  35. unsigned long flags;
  36. __save_flags(flags); /* local CPU only */
  37. __cli(); /* local CPU only */
  38. new = *old;
  39. /* Adjust IRQ enable bit */
  40. bit = 1 << (8 + hwif->channel);
  41. new = drive->present ? (new & ~bit) : (new | bit);
  42. /* Select PIO or DMA, DMA may only be selected for one drive/channel. */
  43. bit   = 1 << (20 + drive->select.b.unit       + (hwif->channel << 1));
  44. other = 1 << (20 + (1 - drive->select.b.unit) + (hwif->channel << 1));
  45. new = use_dma ? ((new & ~other) | bit) : (new & ~bit);
  46. if (new != *old) {
  47. unsigned char stat;
  48. /*
  49.  * Don't change DMA engine settings while Write Buffers
  50.  * are busy.
  51.  */
  52. (void) pci_read_config_byte(dev, 0x43, &stat);
  53. while (stat & 0x03) {
  54. udelay(1);
  55. (void) pci_read_config_byte(dev, 0x43, &stat);
  56. }
  57. *old = new;
  58. (void) pci_write_config_dword(dev, 0x40, new);
  59. /*
  60.  * And let things settle...
  61.  */
  62. udelay(10);
  63. }
  64. __restore_flags(flags); /* local CPU only */
  65. }
  66. static void ns87415_selectproc (ide_drive_t *drive)
  67. {
  68. ns87415_prepare_drive (drive, drive->using_dma);
  69. }
  70. #ifdef CONFIG_BLK_DEV_IDEDMA
  71. static int ns87415_dmaproc(ide_dma_action_t func, ide_drive_t *drive)
  72. {
  73. ide_hwif_t *hwif = HWIF(drive);
  74. byte dma_stat;
  75. switch (func) {
  76. case ide_dma_end: /* returns 1 on error, 0 otherwise */
  77. drive->waiting_for_dma = 0;
  78. dma_stat = inb(hwif->dma_base+2);
  79. outb(inb(hwif->dma_base)&~1, hwif->dma_base); /* stop DMA */
  80. outb(inb(hwif->dma_base)|6, hwif->dma_base); /* from ERRATA: clear the INTR & ERROR bits */
  81. ide_destroy_dmatable(drive); /* and free any DMA resources */
  82. return (dma_stat & 7) != 4; /* verify good DMA status */
  83. case ide_dma_write:
  84. case ide_dma_read:
  85. ns87415_prepare_drive(drive, 1); /* select DMA xfer */
  86. if (!ide_dmaproc(func, drive)) /* use standard DMA stuff */
  87. return 0;
  88. ns87415_prepare_drive(drive, 0); /* DMA failed: select PIO xfer */
  89. return 1;
  90. case ide_dma_check:
  91. if (drive->media != ide_disk)
  92. return ide_dmaproc(ide_dma_off_quietly, drive);
  93. /* Fallthrough... */
  94. default:
  95. return ide_dmaproc(func, drive); /* use standard DMA stuff */
  96. }
  97. }
  98. #endif /* CONFIG_BLK_DEV_IDEDMA */
  99. void __init ide_init_ns87415 (ide_hwif_t *hwif)
  100. {
  101. struct pci_dev *dev = hwif->pci_dev;
  102. unsigned int ctrl, using_inta;
  103. byte progif;
  104. #ifdef __sparc_v9__
  105. int timeout;
  106. byte stat;
  107. #endif
  108. /* Set a good latency timer and cache line size value. */
  109. (void) pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
  110. #ifdef __sparc_v9__
  111. (void) pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 0x10);
  112. #endif
  113. /*
  114.  * We cannot probe for IRQ: both ports share common IRQ on INTA.
  115.  * Also, leave IRQ masked during drive probing, to prevent infinite
  116.  * interrupts from a potentially floating INTA..
  117.  *
  118.  * IRQs get unmasked in selectproc when drive is first used.
  119.  */
  120. (void) pci_read_config_dword(dev, 0x40, &ctrl);
  121. (void) pci_read_config_byte(dev, 0x09, &progif);
  122. /* is irq in "native" mode? */
  123. using_inta = progif & (1 << (hwif->channel << 1));
  124. if (!using_inta)
  125. using_inta = ctrl & (1 << (4 + hwif->channel));
  126. if (hwif->mate) {
  127. hwif->select_data = hwif->mate->select_data;
  128. } else {
  129. hwif->select_data = (unsigned long)
  130. &ns87415_control[ns87415_count++];
  131. ctrl |= (1 << 8) | (1 << 9); /* mask both IRQs */
  132. if (using_inta)
  133. ctrl &= ~(1 << 6); /* unmask INTA */
  134. *((unsigned int *)hwif->select_data) = ctrl;
  135. (void) pci_write_config_dword(dev, 0x40, ctrl);
  136. /*
  137.  * Set prefetch size to 512 bytes for both ports,
  138.  * but don't turn on/off prefetching here.
  139.  */
  140. pci_write_config_byte(dev, 0x55, 0xee);
  141. #ifdef __sparc_v9__
  142. /*
  143.  * XXX: Reset the device, if we don't it will not respond
  144.  *      to SELECT_DRIVE() properly during first probe_hwif().
  145.  */
  146. timeout = 10000;
  147. outb(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
  148. udelay(10);
  149. outb(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
  150. do {
  151. udelay(50);
  152. stat = inb(hwif->io_ports[IDE_STATUS_OFFSET]);
  153.                  if (stat == 0xff)
  154.                          break;
  155.          } while ((stat & BUSY_STAT) && --timeout);
  156. #endif
  157. }
  158. if (hwif->dma_base)
  159. outb(0x60, hwif->dma_base + 2);
  160. if (!using_inta)
  161. hwif->irq = hwif->channel ? 15 : 14; /* legacy mode */
  162. else if (!hwif->irq && hwif->mate && hwif->mate->irq)
  163. hwif->irq = hwif->mate->irq; /* share IRQ with mate */
  164. #ifdef CONFIG_BLK_DEV_IDEDMA
  165. if (hwif->dma_base)
  166. hwif->dmaproc = &ns87415_dmaproc;
  167. #endif /* CONFIG_BLK_DEV_IDEDMA */
  168. hwif->selectproc = &ns87415_selectproc;
  169. }