sata_nv.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 void nv_ck804_host_stop(struct ata_host_set *host_set);
  73. static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance,
  74. struct pt_regs *regs);
  75. static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance,
  76. struct pt_regs *regs);
  77. static irqreturn_t nv_ck804_interrupt(int irq, void *dev_instance,
  78. struct pt_regs *regs);
  79. static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg);
  80. static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
  81. enum nv_host_type
  82. {
  83. GENERIC,
  84. NFORCE2,
  85. NFORCE3 = NFORCE2, /* NF2 == NF3 as far as sata_nv is concerned */
  86. CK804
  87. };
  88. static const struct pci_device_id nv_pci_tbl[] = {
  89. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA,
  90. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE2 },
  91. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA,
  92. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
  93. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2,
  94. PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
  95. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA,
  96. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  97. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA2,
  98. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  99. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA,
  100. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  101. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA2,
  102. PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
  103. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA,
  104. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  105. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2,
  106. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  107. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA,
  108. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  109. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2,
  110. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  111. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA,
  112. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  113. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA2,
  114. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  115. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA3,
  116. PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
  117. { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
  118. PCI_ANY_ID, PCI_ANY_ID,
  119. PCI_CLASS_STORAGE_IDE<<8, 0xffff00, GENERIC },
  120. { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
  121. PCI_ANY_ID, PCI_ANY_ID,
  122. PCI_CLASS_STORAGE_RAID<<8, 0xffff00, GENERIC },
  123. { 0, } /* terminate list */
  124. };
  125. static struct pci_driver nv_pci_driver = {
  126. .name = DRV_NAME,
  127. .id_table = nv_pci_tbl,
  128. .probe = nv_init_one,
  129. .remove = ata_pci_remove_one,
  130. };
  131. static struct scsi_host_template nv_sht = {
  132. .module = THIS_MODULE,
  133. .name = DRV_NAME,
  134. .ioctl = ata_scsi_ioctl,
  135. .queuecommand = ata_scsi_queuecmd,
  136. .can_queue = ATA_DEF_QUEUE,
  137. .this_id = ATA_SHT_THIS_ID,
  138. .sg_tablesize = LIBATA_MAX_PRD,
  139. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  140. .emulated = ATA_SHT_EMULATED,
  141. .use_clustering = ATA_SHT_USE_CLUSTERING,
  142. .proc_name = DRV_NAME,
  143. .dma_boundary = ATA_DMA_BOUNDARY,
  144. .slave_configure = ata_scsi_slave_config,
  145. .slave_destroy = ata_scsi_slave_destroy,
  146. .bios_param = ata_std_bios_param,
  147. };
  148. static const struct ata_port_operations nv_generic_ops = {
  149. .port_disable = ata_port_disable,
  150. .tf_load = ata_tf_load,
  151. .tf_read = ata_tf_read,
  152. .exec_command = ata_exec_command,
  153. .check_status = ata_check_status,
  154. .dev_select = ata_std_dev_select,
  155. .phy_reset = sata_phy_reset,
  156. .bmdma_setup = ata_bmdma_setup,
  157. .bmdma_start = ata_bmdma_start,
  158. .bmdma_stop = ata_bmdma_stop,
  159. .bmdma_status = ata_bmdma_status,
  160. .qc_prep = ata_qc_prep,
  161. .qc_issue = ata_qc_issue_prot,
  162. .eng_timeout = ata_eng_timeout,
  163. .data_xfer = ata_pio_data_xfer,
  164. .irq_handler = nv_generic_interrupt,
  165. .irq_clear = ata_bmdma_irq_clear,
  166. .scr_read = nv_scr_read,
  167. .scr_write = nv_scr_write,
  168. .port_start = ata_port_start,
  169. .port_stop = ata_port_stop,
  170. .host_stop = ata_pci_host_stop,
  171. };
  172. static const struct ata_port_operations nv_nf2_ops = {
  173. .port_disable = ata_port_disable,
  174. .tf_load = ata_tf_load,
  175. .tf_read = ata_tf_read,
  176. .exec_command = ata_exec_command,
  177. .check_status = ata_check_status,
  178. .dev_select = ata_std_dev_select,
  179. .phy_reset = sata_phy_reset,
  180. .bmdma_setup = ata_bmdma_setup,
  181. .bmdma_start = ata_bmdma_start,
  182. .bmdma_stop = ata_bmdma_stop,
  183. .bmdma_status = ata_bmdma_status,
  184. .qc_prep = ata_qc_prep,
  185. .qc_issue = ata_qc_issue_prot,
  186. .eng_timeout = ata_eng_timeout,
  187. .data_xfer = ata_pio_data_xfer,
  188. .irq_handler = nv_nf2_interrupt,
  189. .irq_clear = ata_bmdma_irq_clear,
  190. .scr_read = nv_scr_read,
  191. .scr_write = nv_scr_write,
  192. .port_start = ata_port_start,
  193. .port_stop = ata_port_stop,
  194. .host_stop = ata_pci_host_stop,
  195. };
  196. static const struct ata_port_operations nv_ck804_ops = {
  197. .port_disable = ata_port_disable,
  198. .tf_load = ata_tf_load,
  199. .tf_read = ata_tf_read,
  200. .exec_command = ata_exec_command,
  201. .check_status = ata_check_status,
  202. .dev_select = ata_std_dev_select,
  203. .phy_reset = sata_phy_reset,
  204. .bmdma_setup = ata_bmdma_setup,
  205. .bmdma_start = ata_bmdma_start,
  206. .bmdma_stop = ata_bmdma_stop,
  207. .bmdma_status = ata_bmdma_status,
  208. .qc_prep = ata_qc_prep,
  209. .qc_issue = ata_qc_issue_prot,
  210. .eng_timeout = ata_eng_timeout,
  211. .data_xfer = ata_pio_data_xfer,
  212. .irq_handler = nv_ck804_interrupt,
  213. .irq_clear = ata_bmdma_irq_clear,
  214. .scr_read = nv_scr_read,
  215. .scr_write = nv_scr_write,
  216. .port_start = ata_port_start,
  217. .port_stop = ata_port_stop,
  218. .host_stop = nv_ck804_host_stop,
  219. };
  220. /* FIXME: The hardware provides the necessary SATA PHY controls
  221. * to support ATA_FLAG_SATA_RESET. However, it is currently
  222. * necessary to disable that flag, to solve misdetection problems.
  223. * See http://bugme.osdl.org/show_bug.cgi?id=3352 for more info.
  224. *
  225. * This problem really needs to be investigated further. But in the
  226. * meantime, we avoid ATA_FLAG_SATA_RESET to get people working.
  227. */
  228. static struct ata_port_info nv_port_info[] = {
  229. /* generic */
  230. {
  231. .sht = &nv_sht,
  232. .host_flags = ATA_FLAG_SATA |
  233. /* ATA_FLAG_SATA_RESET | */
  234. ATA_FLAG_SRST |
  235. ATA_FLAG_NO_LEGACY,
  236. .pio_mask = NV_PIO_MASK,
  237. .mwdma_mask = NV_MWDMA_MASK,
  238. .udma_mask = NV_UDMA_MASK,
  239. .port_ops = &nv_generic_ops,
  240. },
  241. /* nforce2/3 */
  242. {
  243. .sht = &nv_sht,
  244. .host_flags = ATA_FLAG_SATA |
  245. /* ATA_FLAG_SATA_RESET | */
  246. ATA_FLAG_SRST |
  247. ATA_FLAG_NO_LEGACY,
  248. .pio_mask = NV_PIO_MASK,
  249. .mwdma_mask = NV_MWDMA_MASK,
  250. .udma_mask = NV_UDMA_MASK,
  251. .port_ops = &nv_nf2_ops,
  252. },
  253. /* ck804 */
  254. {
  255. .sht = &nv_sht,
  256. .host_flags = ATA_FLAG_SATA |
  257. /* ATA_FLAG_SATA_RESET | */
  258. ATA_FLAG_SRST |
  259. ATA_FLAG_NO_LEGACY,
  260. .pio_mask = NV_PIO_MASK,
  261. .mwdma_mask = NV_MWDMA_MASK,
  262. .udma_mask = NV_UDMA_MASK,
  263. .port_ops = &nv_ck804_ops,
  264. },
  265. };
  266. MODULE_AUTHOR("NVIDIA");
  267. MODULE_DESCRIPTION("low-level driver for NVIDIA nForce SATA controller");
  268. MODULE_LICENSE("GPL");
  269. MODULE_DEVICE_TABLE(pci, nv_pci_tbl);
  270. MODULE_VERSION(DRV_VERSION);
  271. static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance,
  272. struct pt_regs *regs)
  273. {
  274. struct ata_host_set *host_set = dev_instance;
  275. unsigned int i;
  276. unsigned int handled = 0;
  277. unsigned long flags;
  278. spin_lock_irqsave(&host_set->lock, flags);
  279. for (i = 0; i < host_set->n_ports; i++) {
  280. struct ata_port *ap;
  281. ap = host_set->ports[i];
  282. if (ap &&
  283. !(ap->flags & ATA_FLAG_DISABLED)) {
  284. struct ata_queued_cmd *qc;
  285. qc = ata_qc_from_tag(ap, ap->active_tag);
  286. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  287. handled += ata_host_intr(ap, qc);
  288. else
  289. // No request pending? Clear interrupt status
  290. // anyway, in case there's one pending.
  291. ap->ops->check_status(ap);
  292. }
  293. }
  294. spin_unlock_irqrestore(&host_set->lock, flags);
  295. return IRQ_RETVAL(handled);
  296. }
  297. static int nv_host_intr(struct ata_port *ap, u8 irq_stat)
  298. {
  299. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
  300. int handled;
  301. /* bail out if not our interrupt */
  302. if (!(irq_stat & NV_INT_DEV))
  303. return 0;
  304. /* DEV interrupt w/ no active qc? */
  305. if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
  306. ata_check_status(ap);
  307. return 1;
  308. }
  309. /* handle interrupt */
  310. handled = ata_host_intr(ap, qc);
  311. if (unlikely(!handled)) {
  312. /* spurious, clear it */
  313. ata_check_status(ap);
  314. }
  315. return 1;
  316. }
  317. static irqreturn_t nv_do_interrupt(struct ata_host_set *host_set, u8 irq_stat)
  318. {
  319. int i, handled = 0;
  320. for (i = 0; i < host_set->n_ports; i++) {
  321. struct ata_port *ap = host_set->ports[i];
  322. if (ap && !(ap->flags & ATA_FLAG_DISABLED))
  323. handled += nv_host_intr(ap, irq_stat);
  324. irq_stat >>= NV_INT_PORT_SHIFT;
  325. }
  326. return IRQ_RETVAL(handled);
  327. }
  328. static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance,
  329. struct pt_regs *regs)
  330. {
  331. struct ata_host_set *host_set = dev_instance;
  332. unsigned long flags;
  333. u8 irq_stat;
  334. irqreturn_t ret;
  335. spin_lock_irqsave(&host_set->lock, flags);
  336. irq_stat = inb(host_set->ports[0]->ioaddr.scr_addr + NV_INT_STATUS);
  337. ret = nv_do_interrupt(host_set, irq_stat);
  338. spin_unlock_irqrestore(&host_set->lock, flags);
  339. return ret;
  340. }
  341. static irqreturn_t nv_ck804_interrupt(int irq, void *dev_instance,
  342. struct pt_regs *regs)
  343. {
  344. struct ata_host_set *host_set = dev_instance;
  345. unsigned long flags;
  346. u8 irq_stat;
  347. irqreturn_t ret;
  348. spin_lock_irqsave(&host_set->lock, flags);
  349. irq_stat = readb(host_set->mmio_base + NV_INT_STATUS_CK804);
  350. ret = nv_do_interrupt(host_set, irq_stat);
  351. spin_unlock_irqrestore(&host_set->lock, flags);
  352. return ret;
  353. }
  354. static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg)
  355. {
  356. if (sc_reg > SCR_CONTROL)
  357. return 0xffffffffU;
  358. return ioread32((void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
  359. }
  360. static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
  361. {
  362. if (sc_reg > SCR_CONTROL)
  363. return;
  364. iowrite32(val, (void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
  365. }
  366. static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  367. {
  368. static int printed_version = 0;
  369. struct ata_port_info *ppi;
  370. struct ata_probe_ent *probe_ent;
  371. int pci_dev_busy = 0;
  372. int rc;
  373. u32 bar;
  374. unsigned long base;
  375. // Make sure this is a SATA controller by counting the number of bars
  376. // (NVIDIA SATA controllers will always have six bars). Otherwise,
  377. // it's an IDE controller and we ignore it.
  378. for (bar=0; bar<6; bar++)
  379. if (pci_resource_start(pdev, bar) == 0)
  380. return -ENODEV;
  381. if (!printed_version++)
  382. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  383. rc = pci_enable_device(pdev);
  384. if (rc)
  385. goto err_out;
  386. rc = pci_request_regions(pdev, DRV_NAME);
  387. if (rc) {
  388. pci_dev_busy = 1;
  389. goto err_out_disable;
  390. }
  391. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  392. if (rc)
  393. goto err_out_regions;
  394. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  395. if (rc)
  396. goto err_out_regions;
  397. rc = -ENOMEM;
  398. ppi = &nv_port_info[ent->driver_data];
  399. probe_ent = ata_pci_init_native_mode(pdev, &ppi, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
  400. if (!probe_ent)
  401. goto err_out_regions;
  402. probe_ent->mmio_base = pci_iomap(pdev, 5, 0);
  403. if (!probe_ent->mmio_base) {
  404. rc = -EIO;
  405. goto err_out_free_ent;
  406. }
  407. base = (unsigned long)probe_ent->mmio_base;
  408. probe_ent->port[0].scr_addr = base + NV_PORT0_SCR_REG_OFFSET;
  409. probe_ent->port[1].scr_addr = base + NV_PORT1_SCR_REG_OFFSET;
  410. /* enable SATA space for CK804 */
  411. if (ent->driver_data == CK804) {
  412. u8 regval;
  413. pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
  414. regval |= NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
  415. pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
  416. }
  417. pci_set_master(pdev);
  418. rc = ata_device_add(probe_ent);
  419. if (rc != NV_PORTS)
  420. goto err_out_iounmap;
  421. kfree(probe_ent);
  422. return 0;
  423. err_out_iounmap:
  424. pci_iounmap(pdev, probe_ent->mmio_base);
  425. err_out_free_ent:
  426. kfree(probe_ent);
  427. err_out_regions:
  428. pci_release_regions(pdev);
  429. err_out_disable:
  430. if (!pci_dev_busy)
  431. pci_disable_device(pdev);
  432. err_out:
  433. return rc;
  434. }
  435. static void nv_ck804_host_stop(struct ata_host_set *host_set)
  436. {
  437. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  438. u8 regval;
  439. /* disable SATA space for CK804 */
  440. pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
  441. regval &= ~NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
  442. pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
  443. ata_pci_host_stop(host_set);
  444. }
  445. static int __init nv_init(void)
  446. {
  447. return pci_module_init(&nv_pci_driver);
  448. }
  449. static void __exit nv_exit(void)
  450. {
  451. pci_unregister_driver(&nv_pci_driver);
  452. }
  453. module_init(nv_init);
  454. module_exit(nv_exit);