ns87415.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 1997-1998 Mark Lord <mlord@pobox.com>
  3. * Copyright (C) 1998 Eddie C. Dost <ecd@skynet.be>
  4. * Copyright (C) 1999-2000 Andre Hedrick <andre@linux-ide.org>
  5. * Copyright (C) 2004 Grant Grundler <grundler at parisc-linux.org>
  6. *
  7. * Inspired by an earlier effort from David S. Miller <davem@redhat.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/hdreg.h>
  14. #include <linux/pci.h>
  15. #include <linux/delay.h>
  16. #include <linux/ide.h>
  17. #include <linux/init.h>
  18. #include <asm/io.h>
  19. #ifdef CONFIG_SUPERIO
  20. /* SUPERIO 87560 is a PoS chip that NatSem denies exists.
  21. * Unfortunately, it's built-in on all Astro-based PA-RISC workstations
  22. * which use the integrated NS87514 cell for CD-ROM support.
  23. * i.e we have to support for CD-ROM installs.
  24. * See drivers/parisc/superio.c for more gory details.
  25. */
  26. #include <asm/superio.h>
  27. static unsigned long superio_ide_status[2];
  28. static unsigned long superio_ide_select[2];
  29. static unsigned long superio_ide_dma_status[2];
  30. #define SUPERIO_IDE_MAX_RETRIES 25
  31. /* Because of a defect in Super I/O, all reads of the PCI DMA status
  32. * registers, IDE status register and the IDE select register need to be
  33. * retried
  34. */
  35. static u8 superio_ide_inb (unsigned long port)
  36. {
  37. if (port == superio_ide_status[0] ||
  38. port == superio_ide_status[1] ||
  39. port == superio_ide_select[0] ||
  40. port == superio_ide_select[1] ||
  41. port == superio_ide_dma_status[0] ||
  42. port == superio_ide_dma_status[1]) {
  43. u8 tmp;
  44. int retries = SUPERIO_IDE_MAX_RETRIES;
  45. /* printk(" [ reading port 0x%x with retry ] ", port); */
  46. do {
  47. tmp = inb(port);
  48. if (tmp == 0)
  49. udelay(50);
  50. } while (tmp == 0 && retries-- > 0);
  51. return tmp;
  52. }
  53. return inb(port);
  54. }
  55. static void __devinit superio_ide_init_iops (struct hwif_s *hwif)
  56. {
  57. struct pci_dev *pdev = to_pci_dev(hwif->dev);
  58. u32 base, dmabase;
  59. u8 port = hwif->channel, tmp;
  60. base = pci_resource_start(pdev, port * 2) & ~3;
  61. dmabase = pci_resource_start(pdev, 4) & ~3;
  62. superio_ide_status[port] = base + IDE_STATUS_OFFSET;
  63. superio_ide_select[port] = base + IDE_SELECT_OFFSET;
  64. superio_ide_dma_status[port] = dmabase + (!port ? 2 : 0xa);
  65. /* Clear error/interrupt, enable dma */
  66. tmp = superio_ide_inb(superio_ide_dma_status[port]);
  67. outb(tmp | 0x66, superio_ide_dma_status[port]);
  68. /* We need to override inb to workaround a SuperIO errata */
  69. hwif->INB = superio_ide_inb;
  70. }
  71. static void __devinit init_iops_ns87415(ide_hwif_t *hwif)
  72. {
  73. struct pci_dev *dev = to_pci_dev(hwif->dev);
  74. if (PCI_SLOT(dev->devfn) == 0xE)
  75. /* Built-in - assume it's under superio. */
  76. superio_ide_init_iops(hwif);
  77. }
  78. #endif
  79. static unsigned int ns87415_count = 0, ns87415_control[MAX_HWIFS] = { 0 };
  80. /*
  81. * This routine either enables/disables (according to drive->present)
  82. * the IRQ associated with the port (HWIF(drive)),
  83. * and selects either PIO or DMA handshaking for the next I/O operation.
  84. */
  85. static void ns87415_prepare_drive (ide_drive_t *drive, unsigned int use_dma)
  86. {
  87. ide_hwif_t *hwif = HWIF(drive);
  88. struct pci_dev *dev = to_pci_dev(hwif->dev);
  89. unsigned int bit, other, new, *old = (unsigned int *) hwif->select_data;
  90. unsigned long flags;
  91. local_irq_save(flags);
  92. new = *old;
  93. /* Adjust IRQ enable bit */
  94. bit = 1 << (8 + hwif->channel);
  95. new = drive->present ? (new & ~bit) : (new | bit);
  96. /* Select PIO or DMA, DMA may only be selected for one drive/channel. */
  97. bit = 1 << (20 + drive->select.b.unit + (hwif->channel << 1));
  98. other = 1 << (20 + (1 - drive->select.b.unit) + (hwif->channel << 1));
  99. new = use_dma ? ((new & ~other) | bit) : (new & ~bit);
  100. if (new != *old) {
  101. unsigned char stat;
  102. /*
  103. * Don't change DMA engine settings while Write Buffers
  104. * are busy.
  105. */
  106. (void) pci_read_config_byte(dev, 0x43, &stat);
  107. while (stat & 0x03) {
  108. udelay(1);
  109. (void) pci_read_config_byte(dev, 0x43, &stat);
  110. }
  111. *old = new;
  112. (void) pci_write_config_dword(dev, 0x40, new);
  113. /*
  114. * And let things settle...
  115. */
  116. udelay(10);
  117. }
  118. local_irq_restore(flags);
  119. }
  120. static void ns87415_selectproc (ide_drive_t *drive)
  121. {
  122. ns87415_prepare_drive (drive, drive->using_dma);
  123. }
  124. static int ns87415_ide_dma_end (ide_drive_t *drive)
  125. {
  126. ide_hwif_t *hwif = HWIF(drive);
  127. u8 dma_stat = 0, dma_cmd = 0;
  128. drive->waiting_for_dma = 0;
  129. dma_stat = hwif->INB(hwif->dma_status);
  130. /* get dma command mode */
  131. dma_cmd = hwif->INB(hwif->dma_command);
  132. /* stop DMA */
  133. outb(dma_cmd & ~1, hwif->dma_command);
  134. /* from ERRATA: clear the INTR & ERROR bits */
  135. dma_cmd = hwif->INB(hwif->dma_command);
  136. outb(dma_cmd | 6, hwif->dma_command);
  137. /* and free any DMA resources */
  138. ide_destroy_dmatable(drive);
  139. /* verify good DMA status */
  140. return (dma_stat & 7) != 4;
  141. }
  142. static int ns87415_ide_dma_setup(ide_drive_t *drive)
  143. {
  144. /* select DMA xfer */
  145. ns87415_prepare_drive(drive, 1);
  146. if (!ide_dma_setup(drive))
  147. return 0;
  148. /* DMA failed: select PIO xfer */
  149. ns87415_prepare_drive(drive, 0);
  150. return 1;
  151. }
  152. #ifndef ide_default_irq
  153. #define ide_default_irq(irq) 0
  154. #endif
  155. static void __devinit init_hwif_ns87415 (ide_hwif_t *hwif)
  156. {
  157. struct pci_dev *dev = to_pci_dev(hwif->dev);
  158. unsigned int ctrl, using_inta;
  159. u8 progif;
  160. #ifdef __sparc_v9__
  161. int timeout;
  162. u8 stat;
  163. #endif
  164. hwif->selectproc = &ns87415_selectproc;
  165. /*
  166. * We cannot probe for IRQ: both ports share common IRQ on INTA.
  167. * Also, leave IRQ masked during drive probing, to prevent infinite
  168. * interrupts from a potentially floating INTA..
  169. *
  170. * IRQs get unmasked in selectproc when drive is first used.
  171. */
  172. (void) pci_read_config_dword(dev, 0x40, &ctrl);
  173. (void) pci_read_config_byte(dev, 0x09, &progif);
  174. /* is irq in "native" mode? */
  175. using_inta = progif & (1 << (hwif->channel << 1));
  176. if (!using_inta)
  177. using_inta = ctrl & (1 << (4 + hwif->channel));
  178. if (hwif->mate) {
  179. hwif->select_data = hwif->mate->select_data;
  180. } else {
  181. hwif->select_data = (unsigned long)
  182. &ns87415_control[ns87415_count++];
  183. ctrl |= (1 << 8) | (1 << 9); /* mask both IRQs */
  184. if (using_inta)
  185. ctrl &= ~(1 << 6); /* unmask INTA */
  186. *((unsigned int *)hwif->select_data) = ctrl;
  187. (void) pci_write_config_dword(dev, 0x40, ctrl);
  188. /*
  189. * Set prefetch size to 512 bytes for both ports,
  190. * but don't turn on/off prefetching here.
  191. */
  192. pci_write_config_byte(dev, 0x55, 0xee);
  193. #ifdef __sparc_v9__
  194. /*
  195. * XXX: Reset the device, if we don't it will not respond to
  196. * SELECT_DRIVE() properly during first ide_probe_port().
  197. */
  198. timeout = 10000;
  199. outb(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
  200. udelay(10);
  201. outb(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
  202. do {
  203. udelay(50);
  204. stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
  205. if (stat == 0xff)
  206. break;
  207. } while ((stat & BUSY_STAT) && --timeout);
  208. #endif
  209. }
  210. if (!using_inta)
  211. hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
  212. else if (!hwif->irq && hwif->mate && hwif->mate->irq)
  213. hwif->irq = hwif->mate->irq; /* share IRQ with mate */
  214. if (!hwif->dma_base)
  215. return;
  216. outb(0x60, hwif->dma_status);
  217. hwif->dma_setup = &ns87415_ide_dma_setup;
  218. hwif->ide_dma_end = &ns87415_ide_dma_end;
  219. }
  220. static const struct ide_port_info ns87415_chipset __devinitdata = {
  221. .name = "NS87415",
  222. #ifdef CONFIG_SUPERIO
  223. .init_iops = init_iops_ns87415,
  224. #endif
  225. .init_hwif = init_hwif_ns87415,
  226. .host_flags = IDE_HFLAG_TRUST_BIOS_FOR_DMA |
  227. IDE_HFLAG_NO_ATAPI_DMA,
  228. };
  229. static int __devinit ns87415_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  230. {
  231. return ide_setup_pci_device(dev, &ns87415_chipset);
  232. }
  233. static const struct pci_device_id ns87415_pci_tbl[] = {
  234. { PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_87415), 0 },
  235. { 0, },
  236. };
  237. MODULE_DEVICE_TABLE(pci, ns87415_pci_tbl);
  238. static struct pci_driver driver = {
  239. .name = "NS87415_IDE",
  240. .id_table = ns87415_pci_tbl,
  241. .probe = ns87415_init_one,
  242. };
  243. static int __init ns87415_ide_init(void)
  244. {
  245. return ide_pci_register_driver(&driver);
  246. }
  247. module_init(ns87415_ide_init);
  248. MODULE_AUTHOR("Mark Lord, Eddie Dost, Andre Hedrick");
  249. MODULE_DESCRIPTION("PCI driver module for NS87415 IDE");
  250. MODULE_LICENSE("GPL");