sata_nv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. NV_INT_STATUS = 0x10,
  54. NV_INT_STATUS_CK804 = 0x440,
  55. NV_INT_STATUS_PDEV_INT = 0x01,
  56. NV_INT_STATUS_PDEV_PM = 0x02,
  57. NV_INT_STATUS_PDEV_ADDED = 0x04,
  58. NV_INT_STATUS_PDEV_REMOVED = 0x08,
  59. NV_INT_STATUS_SDEV_INT = 0x10,
  60. NV_INT_STATUS_SDEV_PM = 0x20,
  61. NV_INT_STATUS_SDEV_ADDED = 0x40,
  62. NV_INT_STATUS_SDEV_REMOVED = 0x80,
  63. NV_INT_ENABLE = 0x11,
  64. NV_INT_ENABLE_CK804 = 0x441,
  65. NV_INT_ENABLE_PDEV_MASK = 0x01,
  66. NV_INT_ENABLE_PDEV_PM = 0x02,
  67. NV_INT_ENABLE_PDEV_ADDED = 0x04,
  68. NV_INT_ENABLE_PDEV_REMOVED = 0x08,
  69. NV_INT_ENABLE_SDEV_MASK = 0x10,
  70. NV_INT_ENABLE_SDEV_PM = 0x20,
  71. NV_INT_ENABLE_SDEV_ADDED = 0x40,
  72. NV_INT_ENABLE_SDEV_REMOVED = 0x80,
  73. NV_INT_CONFIG = 0x12,
  74. NV_INT_CONFIG_METHD = 0x01, // 0 = INT, 1 = SMI
  75. // For PCI config register 20
  76. NV_MCP_SATA_CFG_20 = 0x50,
  77. NV_MCP_SATA_CFG_20_SATA_SPACE_EN = 0x04,
  78. };
  79. static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  80. static irqreturn_t nv_interrupt (int irq, void *dev_instance,
  81. struct pt_regs *regs);
  82. static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg);
  83. static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
  84. enum nv_host_type
  85. {
  86. GENERIC,
  87. NFORCE2,
  88. NFORCE3,
  89. CK804
  90. };
  91. static const struct pci_device_id nv_pci_tbl[] = {
  92. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA,
  93. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE2 },
  94. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA,
  95. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
  96. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2,
  97. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
  98. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA,
  99. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  100. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA2,
  101. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  102. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA,
  103. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  104. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA2,
  105. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  106. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA,
  107. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  108. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2,
  109. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  110. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA,
  111. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  112. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2,
  113. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  114. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA,
  115. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  116. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA2,
  117. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  118. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA3,
  119. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  120. { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
  121. PCI_ANY_ID, PCI_ANY_ID,
  122. PCI_CLASS_STORAGE_IDE<<8, 0xffff00, GENERIC },
  123. { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
  124. PCI_ANY_ID, PCI_ANY_ID,
  125. PCI_CLASS_STORAGE_RAID<<8, 0xffff00, GENERIC },
  126. { 0, } /* terminate list */
  127. };
  128. static struct pci_driver nv_pci_driver = {
  129. .name = DRV_NAME,
  130. .id_table = nv_pci_tbl,
  131. .probe = nv_init_one,
  132. .remove = ata_pci_remove_one,
  133. };
  134. static struct scsi_host_template nv_sht = {
  135. .module = THIS_MODULE,
  136. .name = DRV_NAME,
  137. .ioctl = ata_scsi_ioctl,
  138. .queuecommand = ata_scsi_queuecmd,
  139. .can_queue = ATA_DEF_QUEUE,
  140. .this_id = ATA_SHT_THIS_ID,
  141. .sg_tablesize = LIBATA_MAX_PRD,
  142. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  143. .emulated = ATA_SHT_EMULATED,
  144. .use_clustering = ATA_SHT_USE_CLUSTERING,
  145. .proc_name = DRV_NAME,
  146. .dma_boundary = ATA_DMA_BOUNDARY,
  147. .slave_configure = ata_scsi_slave_config,
  148. .slave_destroy = ata_scsi_slave_destroy,
  149. .bios_param = ata_std_bios_param,
  150. };
  151. static const struct ata_port_operations nv_ops = {
  152. .port_disable = ata_port_disable,
  153. .tf_load = ata_tf_load,
  154. .tf_read = ata_tf_read,
  155. .exec_command = ata_exec_command,
  156. .check_status = ata_check_status,
  157. .dev_select = ata_std_dev_select,
  158. .phy_reset = sata_phy_reset,
  159. .bmdma_setup = ata_bmdma_setup,
  160. .bmdma_start = ata_bmdma_start,
  161. .bmdma_stop = ata_bmdma_stop,
  162. .bmdma_status = ata_bmdma_status,
  163. .qc_prep = ata_qc_prep,
  164. .qc_issue = ata_qc_issue_prot,
  165. .eng_timeout = ata_eng_timeout,
  166. .data_xfer = ata_pio_data_xfer,
  167. .irq_handler = nv_interrupt,
  168. .irq_clear = ata_bmdma_irq_clear,
  169. .scr_read = nv_scr_read,
  170. .scr_write = nv_scr_write,
  171. .port_start = ata_port_start,
  172. .port_stop = ata_port_stop,
  173. .host_stop = ata_pci_host_stop,
  174. };
  175. /* FIXME: The hardware provides the necessary SATA PHY controls
  176. * to support ATA_FLAG_SATA_RESET. However, it is currently
  177. * necessary to disable that flag, to solve misdetection problems.
  178. * See http://bugme.osdl.org/show_bug.cgi?id=3352 for more info.
  179. *
  180. * This problem really needs to be investigated further. But in the
  181. * meantime, we avoid ATA_FLAG_SATA_RESET to get people working.
  182. */
  183. static struct ata_port_info nv_port_info = {
  184. .sht = &nv_sht,
  185. .host_flags = ATA_FLAG_SATA |
  186. /* ATA_FLAG_SATA_RESET | */
  187. ATA_FLAG_SRST |
  188. ATA_FLAG_NO_LEGACY,
  189. .pio_mask = NV_PIO_MASK,
  190. .mwdma_mask = NV_MWDMA_MASK,
  191. .udma_mask = NV_UDMA_MASK,
  192. .port_ops = &nv_ops,
  193. };
  194. MODULE_AUTHOR("NVIDIA");
  195. MODULE_DESCRIPTION("low-level driver for NVIDIA nForce SATA controller");
  196. MODULE_LICENSE("GPL");
  197. MODULE_DEVICE_TABLE(pci, nv_pci_tbl);
  198. MODULE_VERSION(DRV_VERSION);
  199. static irqreturn_t nv_interrupt (int irq, void *dev_instance,
  200. struct pt_regs *regs)
  201. {
  202. struct ata_host_set *host_set = dev_instance;
  203. unsigned int i;
  204. unsigned int handled = 0;
  205. unsigned long flags;
  206. spin_lock_irqsave(&host_set->lock, flags);
  207. for (i = 0; i < host_set->n_ports; i++) {
  208. struct ata_port *ap;
  209. ap = host_set->ports[i];
  210. if (ap &&
  211. !(ap->flags & ATA_FLAG_DISABLED)) {
  212. struct ata_queued_cmd *qc;
  213. qc = ata_qc_from_tag(ap, ap->active_tag);
  214. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  215. handled += ata_host_intr(ap, qc);
  216. else
  217. // No request pending? Clear interrupt status
  218. // anyway, in case there's one pending.
  219. ap->ops->check_status(ap);
  220. }
  221. }
  222. spin_unlock_irqrestore(&host_set->lock, flags);
  223. return IRQ_RETVAL(handled);
  224. }
  225. static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg)
  226. {
  227. if (sc_reg > SCR_CONTROL)
  228. return 0xffffffffU;
  229. return ioread32((void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
  230. }
  231. static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
  232. {
  233. if (sc_reg > SCR_CONTROL)
  234. return;
  235. iowrite32(val, (void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
  236. }
  237. static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  238. {
  239. static int printed_version = 0;
  240. struct ata_port_info *ppi;
  241. struct ata_probe_ent *probe_ent;
  242. int pci_dev_busy = 0;
  243. int rc;
  244. u32 bar;
  245. unsigned long base;
  246. // Make sure this is a SATA controller by counting the number of bars
  247. // (NVIDIA SATA controllers will always have six bars). Otherwise,
  248. // it's an IDE controller and we ignore it.
  249. for (bar=0; bar<6; bar++)
  250. if (pci_resource_start(pdev, bar) == 0)
  251. return -ENODEV;
  252. if (!printed_version++)
  253. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  254. rc = pci_enable_device(pdev);
  255. if (rc)
  256. goto err_out;
  257. rc = pci_request_regions(pdev, DRV_NAME);
  258. if (rc) {
  259. pci_dev_busy = 1;
  260. goto err_out_disable;
  261. }
  262. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  263. if (rc)
  264. goto err_out_regions;
  265. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  266. if (rc)
  267. goto err_out_regions;
  268. rc = -ENOMEM;
  269. ppi = &nv_port_info;
  270. probe_ent = ata_pci_init_native_mode(pdev, &ppi, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
  271. if (!probe_ent)
  272. goto err_out_regions;
  273. probe_ent->mmio_base = pci_iomap(pdev, 5, 0);
  274. if (!probe_ent->mmio_base) {
  275. rc = -EIO;
  276. goto err_out_free_ent;
  277. }
  278. base = (unsigned long)probe_ent->mmio_base;
  279. probe_ent->port[0].scr_addr = base + NV_PORT0_SCR_REG_OFFSET;
  280. probe_ent->port[1].scr_addr = base + NV_PORT1_SCR_REG_OFFSET;
  281. pci_set_master(pdev);
  282. rc = ata_device_add(probe_ent);
  283. if (rc != NV_PORTS)
  284. goto err_out_iounmap;
  285. kfree(probe_ent);
  286. return 0;
  287. err_out_iounmap:
  288. pci_iounmap(pdev, probe_ent->mmio_base);
  289. err_out_free_ent:
  290. kfree(probe_ent);
  291. err_out_regions:
  292. pci_release_regions(pdev);
  293. err_out_disable:
  294. if (!pci_dev_busy)
  295. pci_disable_device(pdev);
  296. err_out:
  297. return rc;
  298. }
  299. static int __init nv_init(void)
  300. {
  301. return pci_module_init(&nv_pci_driver);
  302. }
  303. static void __exit nv_exit(void)
  304. {
  305. pci_unregister_driver(&nv_pci_driver);
  306. }
  307. module_init(nv_init);
  308. module_exit(nv_exit);