ns87415.c 9.7 KB

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