sata_nv.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * sata_nv.c - NVIDIA nForce SATA
  3. *
  4. * Copyright 2004 NVIDIA Corp. All rights reserved.
  5. * Copyright 2004 Andrew Chew
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; see the file COPYING. If not, write to
  20. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. *
  23. * libata documentation is available via 'make {ps|pdf}docs',
  24. * as Documentation/DocBook/libata.*
  25. *
  26. * No hardware documentation available outside of NVIDIA.
  27. * This driver programs the NVIDIA SATA controller in a similar
  28. * fashion as with other PCI IDE BMDMA controllers, with a few
  29. * NV-specific details such as register offsets, SATA phy location,
  30. * hotplug info, etc.
  31. *
  32. */
  33. #include <linux/config.h>
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/pci.h>
  37. #include <linux/init.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/delay.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/device.h>
  42. #include <scsi/scsi_host.h>
  43. #include <linux/libata.h>
  44. #define DRV_NAME "sata_nv"
  45. #define DRV_VERSION "0.9"
  46. enum {
  47. NV_PORTS = 2,
  48. NV_PIO_MASK = 0x1f,
  49. NV_MWDMA_MASK = 0x07,
  50. NV_UDMA_MASK = 0x7f,
  51. NV_PORT0_SCR_REG_OFFSET = 0x00,
  52. NV_PORT1_SCR_REG_OFFSET = 0x40,
  53. /* INT_STATUS/ENABLE */
  54. NV_INT_STATUS = 0x10,
  55. NV_INT_ENABLE = 0x11,
  56. NV_INT_STATUS_CK804 = 0x440,
  57. NV_INT_ENABLE_CK804 = 0x441,
  58. /* INT_STATUS/ENABLE bits */
  59. NV_INT_DEV = 0x01,
  60. NV_INT_PM = 0x02,
  61. NV_INT_ADDED = 0x04,
  62. NV_INT_REMOVED = 0x08,
  63. NV_INT_PORT_SHIFT = 4, /* each port occupies 4 bits */
  64. /* INT_CONFIG */
  65. NV_INT_CONFIG = 0x12,
  66. NV_INT_CONFIG_METHD = 0x01, // 0 = INT, 1 = SMI
  67. // For PCI config register 20
  68. NV_MCP_SATA_CFG_20 = 0x50,
  69. NV_MCP_SATA_CFG_20_SATA_SPACE_EN = 0x04,
  70. };
  71. static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  72. static irqreturn_t nv_interrupt (int irq, void *dev_instance,
  73. struct pt_regs *regs);
  74. static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg);
  75. static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
  76. enum nv_host_type
  77. {
  78. GENERIC,
  79. NFORCE2,
  80. NFORCE3 = NFORCE2, /* NF2 == NF3 as far as sata_nv is concerned */
  81. CK804
  82. };
  83. static const struct pci_device_id nv_pci_tbl[] = {
  84. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA,
  85. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE2 },
  86. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA,
  87. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
  88. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2,
  89. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
  90. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA,
  91. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  92. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA2,
  93. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  94. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA,
  95. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  96. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA2,
  97. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  98. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA,
  99. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  100. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2,
  101. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  102. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA,
  103. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  104. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2,
  105. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  106. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA,
  107. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  108. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA2,
  109. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  110. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA3,
  111. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  112. { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
  113. PCI_ANY_ID, PCI_ANY_ID,
  114. PCI_CLASS_STORAGE_IDE<<8, 0xffff00, GENERIC },
  115. { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
  116. PCI_ANY_ID, PCI_ANY_ID,
  117. PCI_CLASS_STORAGE_RAID<<8, 0xffff00, GENERIC },
  118. { 0, } /* terminate list */
  119. };
  120. static struct pci_driver nv_pci_driver = {
  121. .name = DRV_NAME,
  122. .id_table = nv_pci_tbl,
  123. .probe = nv_init_one,
  124. .remove = ata_pci_remove_one,
  125. };
  126. static struct scsi_host_template nv_sht = {
  127. .module = THIS_MODULE,
  128. .name = DRV_NAME,
  129. .ioctl = ata_scsi_ioctl,
  130. .queuecommand = ata_scsi_queuecmd,
  131. .can_queue = ATA_DEF_QUEUE,
  132. .this_id = ATA_SHT_THIS_ID,
  133. .sg_tablesize = LIBATA_MAX_PRD,
  134. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  135. .emulated = ATA_SHT_EMULATED,
  136. .use_clustering = ATA_SHT_USE_CLUSTERING,
  137. .proc_name = DRV_NAME,
  138. .dma_boundary = ATA_DMA_BOUNDARY,
  139. .slave_configure = ata_scsi_slave_config,
  140. .slave_destroy = ata_scsi_slave_destroy,
  141. .bios_param = ata_std_bios_param,
  142. };
  143. static const struct ata_port_operations nv_ops = {
  144. .port_disable = ata_port_disable,
  145. .tf_load = ata_tf_load,
  146. .tf_read = ata_tf_read,
  147. .exec_command = ata_exec_command,
  148. .check_status = ata_check_status,
  149. .dev_select = ata_std_dev_select,
  150. .phy_reset = sata_phy_reset,
  151. .bmdma_setup = ata_bmdma_setup,
  152. .bmdma_start = ata_bmdma_start,
  153. .bmdma_stop = ata_bmdma_stop,
  154. .bmdma_status = ata_bmdma_status,
  155. .qc_prep = ata_qc_prep,
  156. .qc_issue = ata_qc_issue_prot,
  157. .eng_timeout = ata_eng_timeout,
  158. .data_xfer = ata_pio_data_xfer,
  159. .irq_handler = nv_interrupt,
  160. .irq_clear = ata_bmdma_irq_clear,
  161. .scr_read = nv_scr_read,
  162. .scr_write = nv_scr_write,
  163. .port_start = ata_port_start,
  164. .port_stop = ata_port_stop,
  165. .host_stop = ata_pci_host_stop,
  166. };
  167. /* FIXME: The hardware provides the necessary SATA PHY controls
  168. * to support ATA_FLAG_SATA_RESET. However, it is currently
  169. * necessary to disable that flag, to solve misdetection problems.
  170. * See http://bugme.osdl.org/show_bug.cgi?id=3352 for more info.
  171. *
  172. * This problem really needs to be investigated further. But in the
  173. * meantime, we avoid ATA_FLAG_SATA_RESET to get people working.
  174. */
  175. static struct ata_port_info nv_port_info = {
  176. .sht = &nv_sht,
  177. .host_flags = ATA_FLAG_SATA |
  178. /* ATA_FLAG_SATA_RESET | */
  179. ATA_FLAG_SRST |
  180. ATA_FLAG_NO_LEGACY,
  181. .pio_mask = NV_PIO_MASK,
  182. .mwdma_mask = NV_MWDMA_MASK,
  183. .udma_mask = NV_UDMA_MASK,
  184. .port_ops = &nv_ops,
  185. };
  186. MODULE_AUTHOR("NVIDIA");
  187. MODULE_DESCRIPTION("low-level driver for NVIDIA nForce SATA controller");
  188. MODULE_LICENSE("GPL");
  189. MODULE_DEVICE_TABLE(pci, nv_pci_tbl);
  190. MODULE_VERSION(DRV_VERSION);
  191. static irqreturn_t nv_interrupt (int irq, void *dev_instance,
  192. struct pt_regs *regs)
  193. {
  194. struct ata_host_set *host_set = dev_instance;
  195. unsigned int i;
  196. unsigned int handled = 0;
  197. unsigned long flags;
  198. spin_lock_irqsave(&host_set->lock, flags);
  199. for (i = 0; i < host_set->n_ports; i++) {
  200. struct ata_port *ap;
  201. ap = host_set->ports[i];
  202. if (ap &&
  203. !(ap->flags & ATA_FLAG_DISABLED)) {
  204. struct ata_queued_cmd *qc;
  205. qc = ata_qc_from_tag(ap, ap->active_tag);
  206. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  207. handled += ata_host_intr(ap, qc);
  208. else
  209. // No request pending? Clear interrupt status
  210. // anyway, in case there's one pending.
  211. ap->ops->check_status(ap);
  212. }
  213. }
  214. spin_unlock_irqrestore(&host_set->lock, flags);
  215. return IRQ_RETVAL(handled);
  216. }
  217. static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg)
  218. {
  219. if (sc_reg > SCR_CONTROL)
  220. return 0xffffffffU;
  221. return ioread32((void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
  222. }
  223. static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
  224. {
  225. if (sc_reg > SCR_CONTROL)
  226. return;
  227. iowrite32(val, (void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
  228. }
  229. static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  230. {
  231. static int printed_version = 0;
  232. struct ata_port_info *ppi;
  233. struct ata_probe_ent *probe_ent;
  234. int pci_dev_busy = 0;
  235. int rc;
  236. u32 bar;
  237. unsigned long base;
  238. // Make sure this is a SATA controller by counting the number of bars
  239. // (NVIDIA SATA controllers will always have six bars). Otherwise,
  240. // it's an IDE controller and we ignore it.
  241. for (bar=0; bar<6; bar++)
  242. if (pci_resource_start(pdev, bar) == 0)
  243. return -ENODEV;
  244. if (!printed_version++)
  245. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  246. rc = pci_enable_device(pdev);
  247. if (rc)
  248. goto err_out;
  249. rc = pci_request_regions(pdev, DRV_NAME);
  250. if (rc) {
  251. pci_dev_busy = 1;
  252. goto err_out_disable;
  253. }
  254. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  255. if (rc)
  256. goto err_out_regions;
  257. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  258. if (rc)
  259. goto err_out_regions;
  260. rc = -ENOMEM;
  261. ppi = &nv_port_info;
  262. probe_ent = ata_pci_init_native_mode(pdev, &ppi, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
  263. if (!probe_ent)
  264. goto err_out_regions;
  265. probe_ent->mmio_base = pci_iomap(pdev, 5, 0);
  266. if (!probe_ent->mmio_base) {
  267. rc = -EIO;
  268. goto err_out_free_ent;
  269. }
  270. base = (unsigned long)probe_ent->mmio_base;
  271. probe_ent->port[0].scr_addr = base + NV_PORT0_SCR_REG_OFFSET;
  272. probe_ent->port[1].scr_addr = base + NV_PORT1_SCR_REG_OFFSET;
  273. pci_set_master(pdev);
  274. rc = ata_device_add(probe_ent);
  275. if (rc != NV_PORTS)
  276. goto err_out_iounmap;
  277. kfree(probe_ent);
  278. return 0;
  279. err_out_iounmap:
  280. pci_iounmap(pdev, probe_ent->mmio_base);
  281. err_out_free_ent:
  282. kfree(probe_ent);
  283. err_out_regions:
  284. pci_release_regions(pdev);
  285. err_out_disable:
  286. if (!pci_dev_busy)
  287. pci_disable_device(pdev);
  288. err_out:
  289. return rc;
  290. }
  291. static int __init nv_init(void)
  292. {
  293. return pci_module_init(&nv_pci_driver);
  294. }
  295. static void __exit nv_exit(void)
  296. {
  297. pci_unregister_driver(&nv_pci_driver);
  298. }
  299. module_init(nv_init);
  300. module_exit(nv_exit);