ns87415.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/pci.h>
  14. #include <linux/delay.h>
  15. #include <linux/ide.h>
  16. #include <linux/init.h>
  17. #include <asm/io.h>
  18. #define DRV_NAME "ns87415"
  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. #define SUPERIO_IDE_MAX_RETRIES 25
  28. /* Because of a defect in Super I/O, all reads of the PCI DMA status
  29. * registers, IDE status register and the IDE select register need to be
  30. * retried
  31. */
  32. static u8 superio_ide_inb (unsigned long port)
  33. {
  34. u8 tmp;
  35. int retries = SUPERIO_IDE_MAX_RETRIES;
  36. /* printk(" [ reading port 0x%x with retry ] ", port); */
  37. do {
  38. tmp = inb(port);
  39. if (tmp == 0)
  40. udelay(50);
  41. } while (tmp == 0 && retries-- > 0);
  42. return tmp;
  43. }
  44. static u8 superio_read_status(ide_hwif_t *hwif)
  45. {
  46. return superio_ide_inb(hwif->io_ports.status_addr);
  47. }
  48. static u8 superio_dma_sff_read_status(ide_hwif_t *hwif)
  49. {
  50. return superio_ide_inb(hwif->dma_base + ATA_DMA_STATUS);
  51. }
  52. static void superio_tf_read(ide_drive_t *drive, struct ide_taskfile *tf,
  53. u8 valid)
  54. {
  55. struct ide_io_ports *io_ports = &drive->hwif->io_ports;
  56. if (valid & IDE_VALID_ERROR)
  57. tf->error = inb(io_ports->feature_addr);
  58. if (valid & IDE_VALID_NSECT)
  59. tf->nsect = inb(io_ports->nsect_addr);
  60. if (valid & IDE_VALID_LBAL)
  61. tf->lbal = inb(io_ports->lbal_addr);
  62. if (valid & IDE_VALID_LBAM)
  63. tf->lbam = inb(io_ports->lbam_addr);
  64. if (valid & IDE_VALID_LBAH)
  65. tf->lbah = inb(io_ports->lbah_addr);
  66. if (valid & IDE_VALID_DEVICE)
  67. tf->device = superio_ide_inb(io_ports->device_addr);
  68. }
  69. static void ns87415_dev_select(ide_drive_t *drive);
  70. static const struct ide_tp_ops superio_tp_ops = {
  71. .exec_command = ide_exec_command,
  72. .read_status = superio_read_status,
  73. .read_altstatus = ide_read_altstatus,
  74. .write_devctl = ide_write_devctl,
  75. .dev_select = ns87415_dev_select,
  76. .tf_load = ide_tf_load,
  77. .tf_read = superio_tf_read,
  78. .input_data = ide_input_data,
  79. .output_data = ide_output_data,
  80. };
  81. static void superio_init_iops(struct hwif_s *hwif)
  82. {
  83. struct pci_dev *pdev = to_pci_dev(hwif->dev);
  84. u32 dma_stat;
  85. u8 port = hwif->channel, tmp;
  86. dma_stat = (pci_resource_start(pdev, 4) & ~3) + (!port ? 2 : 0xa);
  87. /* Clear error/interrupt, enable dma */
  88. tmp = superio_ide_inb(dma_stat);
  89. outb(tmp | 0x66, dma_stat);
  90. }
  91. #else
  92. #define superio_dma_sff_read_status ide_dma_sff_read_status
  93. #endif
  94. static unsigned int ns87415_count = 0, ns87415_control[MAX_HWIFS] = { 0 };
  95. /*
  96. * This routine either enables/disables (according to IDE_DFLAG_PRESENT)
  97. * the IRQ associated with the port,
  98. * and selects either PIO or DMA handshaking for the next I/O operation.
  99. */
  100. static void ns87415_prepare_drive (ide_drive_t *drive, unsigned int use_dma)
  101. {
  102. ide_hwif_t *hwif = drive->hwif;
  103. struct pci_dev *dev = to_pci_dev(hwif->dev);
  104. unsigned int bit, other, new, *old = (unsigned int *) hwif->select_data;
  105. unsigned long flags;
  106. local_irq_save(flags);
  107. new = *old;
  108. /* Adjust IRQ enable bit */
  109. bit = 1 << (8 + hwif->channel);
  110. if (drive->dev_flags & IDE_DFLAG_PRESENT)
  111. new &= ~bit;
  112. else
  113. new |= bit;
  114. /* Select PIO or DMA, DMA may only be selected for one drive/channel. */
  115. bit = 1 << (20 + (drive->dn & 1) + (hwif->channel << 1));
  116. other = 1 << (20 + (1 - (drive->dn & 1)) + (hwif->channel << 1));
  117. new = use_dma ? ((new & ~other) | bit) : (new & ~bit);
  118. if (new != *old) {
  119. unsigned char stat;
  120. /*
  121. * Don't change DMA engine settings while Write Buffers
  122. * are busy.
  123. */
  124. (void) pci_read_config_byte(dev, 0x43, &stat);
  125. while (stat & 0x03) {
  126. udelay(1);
  127. (void) pci_read_config_byte(dev, 0x43, &stat);
  128. }
  129. *old = new;
  130. (void) pci_write_config_dword(dev, 0x40, new);
  131. /*
  132. * And let things settle...
  133. */
  134. udelay(10);
  135. }
  136. local_irq_restore(flags);
  137. }
  138. static void ns87415_dev_select(ide_drive_t *drive)
  139. {
  140. ns87415_prepare_drive(drive,
  141. !!(drive->dev_flags & IDE_DFLAG_USING_DMA));
  142. outb(drive->select | ATA_DEVICE_OBS, drive->hwif->io_ports.device_addr);
  143. }
  144. static void ns87415_dma_start(ide_drive_t *drive)
  145. {
  146. ns87415_prepare_drive(drive, 1);
  147. ide_dma_start(drive);
  148. }
  149. static int ns87415_dma_end(ide_drive_t *drive)
  150. {
  151. ide_hwif_t *hwif = drive->hwif;
  152. u8 dma_stat = 0, dma_cmd = 0;
  153. dma_stat = hwif->dma_ops->dma_sff_read_status(hwif);
  154. /* get DMA command mode */
  155. dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
  156. /* stop DMA */
  157. outb(dma_cmd & ~1, hwif->dma_base + ATA_DMA_CMD);
  158. /* from ERRATA: clear the INTR & ERROR bits */
  159. dma_cmd = inb(hwif->dma_base + ATA_DMA_CMD);
  160. outb(dma_cmd | 6, hwif->dma_base + ATA_DMA_CMD);
  161. ns87415_prepare_drive(drive, 0);
  162. /* verify good DMA status */
  163. return (dma_stat & 7) != 4;
  164. }
  165. static void init_hwif_ns87415 (ide_hwif_t *hwif)
  166. {
  167. struct pci_dev *dev = to_pci_dev(hwif->dev);
  168. unsigned int ctrl, using_inta;
  169. u8 progif;
  170. #ifdef __sparc_v9__
  171. int timeout;
  172. u8 stat;
  173. #endif
  174. /*
  175. * We cannot probe for IRQ: both ports share common IRQ on INTA.
  176. * Also, leave IRQ masked during drive probing, to prevent infinite
  177. * interrupts from a potentially floating INTA..
  178. *
  179. * IRQs get unmasked in dev_select() when drive is first used.
  180. */
  181. (void) pci_read_config_dword(dev, 0x40, &ctrl);
  182. (void) pci_read_config_byte(dev, 0x09, &progif);
  183. /* is irq in "native" mode? */
  184. using_inta = progif & (1 << (hwif->channel << 1));
  185. if (!using_inta)
  186. using_inta = ctrl & (1 << (4 + hwif->channel));
  187. if (hwif->mate) {
  188. hwif->select_data = hwif->mate->select_data;
  189. } else {
  190. hwif->select_data = (unsigned long)
  191. &ns87415_control[ns87415_count++];
  192. ctrl |= (1 << 8) | (1 << 9); /* mask both IRQs */
  193. if (using_inta)
  194. ctrl &= ~(1 << 6); /* unmask INTA */
  195. *((unsigned int *)hwif->select_data) = ctrl;
  196. (void) pci_write_config_dword(dev, 0x40, ctrl);
  197. /*
  198. * Set prefetch size to 512 bytes for both ports,
  199. * but don't turn on/off prefetching here.
  200. */
  201. pci_write_config_byte(dev, 0x55, 0xee);
  202. #ifdef __sparc_v9__
  203. /*
  204. * XXX: Reset the device, if we don't it will not respond to
  205. * dev_select() properly during first ide_probe_port().
  206. */
  207. timeout = 10000;
  208. outb(12, hwif->io_ports.ctl_addr);
  209. udelay(10);
  210. outb(8, hwif->io_ports.ctl_addr);
  211. do {
  212. udelay(50);
  213. stat = hwif->tp_ops->read_status(hwif);
  214. if (stat == 0xff)
  215. break;
  216. } while ((stat & ATA_BUSY) && --timeout);
  217. #endif
  218. }
  219. if (!using_inta)
  220. hwif->irq = pci_get_legacy_ide_irq(dev, hwif->channel);
  221. if (!hwif->dma_base)
  222. return;
  223. outb(0x60, hwif->dma_base + ATA_DMA_STATUS);
  224. }
  225. static const struct ide_tp_ops ns87415_tp_ops = {
  226. .exec_command = ide_exec_command,
  227. .read_status = ide_read_status,
  228. .read_altstatus = ide_read_altstatus,
  229. .write_devctl = ide_write_devctl,
  230. .dev_select = ns87415_dev_select,
  231. .tf_load = ide_tf_load,
  232. .tf_read = ide_tf_read,
  233. .input_data = ide_input_data,
  234. .output_data = ide_output_data,
  235. };
  236. static const struct ide_dma_ops ns87415_dma_ops = {
  237. .dma_host_set = ide_dma_host_set,
  238. .dma_setup = ide_dma_setup,
  239. .dma_start = ns87415_dma_start,
  240. .dma_end = ns87415_dma_end,
  241. .dma_test_irq = ide_dma_test_irq,
  242. .dma_lost_irq = ide_dma_lost_irq,
  243. .dma_timer_expiry = ide_dma_sff_timer_expiry,
  244. .dma_sff_read_status = superio_dma_sff_read_status,
  245. };
  246. static const struct ide_port_info ns87415_chipset = {
  247. .name = DRV_NAME,
  248. .init_hwif = init_hwif_ns87415,
  249. .tp_ops = &ns87415_tp_ops,
  250. .dma_ops = &ns87415_dma_ops,
  251. .host_flags = IDE_HFLAG_TRUST_BIOS_FOR_DMA |
  252. IDE_HFLAG_NO_ATAPI_DMA,
  253. };
  254. static int ns87415_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  255. {
  256. struct ide_port_info d = ns87415_chipset;
  257. #ifdef CONFIG_SUPERIO
  258. if (PCI_SLOT(dev->devfn) == 0xE) {
  259. /* Built-in - assume it's under superio. */
  260. d.init_iops = superio_init_iops;
  261. d.tp_ops = &superio_tp_ops;
  262. }
  263. #endif
  264. return ide_pci_init_one(dev, &d, NULL);
  265. }
  266. static const struct pci_device_id ns87415_pci_tbl[] = {
  267. { PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_87415), 0 },
  268. { 0, },
  269. };
  270. MODULE_DEVICE_TABLE(pci, ns87415_pci_tbl);
  271. static struct pci_driver ns87415_pci_driver = {
  272. .name = "NS87415_IDE",
  273. .id_table = ns87415_pci_tbl,
  274. .probe = ns87415_init_one,
  275. .remove = ide_pci_remove,
  276. .suspend = ide_pci_suspend,
  277. .resume = ide_pci_resume,
  278. };
  279. static int __init ns87415_ide_init(void)
  280. {
  281. return ide_pci_register_driver(&ns87415_pci_driver);
  282. }
  283. static void __exit ns87415_ide_exit(void)
  284. {
  285. pci_unregister_driver(&ns87415_pci_driver);
  286. }
  287. module_init(ns87415_ide_init);
  288. module_exit(ns87415_ide_exit);
  289. MODULE_AUTHOR("Mark Lord, Eddie Dost, Andre Hedrick");
  290. MODULE_DESCRIPTION("PCI driver module for NS87415 IDE");
  291. MODULE_LICENSE("GPL");