ns87415.c 8.4 KB

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