sata_nv.c 11 KB

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