ns87415.c 8.0 KB

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