ns87415.c 10.0 KB

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