ns87415.c 9.6 KB

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