2 * sata_nv.c - NVIDIA nForce SATA
4 * Copyright 2004 NVIDIA Corp. All rights reserved.
5 * Copyright 2004 Andrew Chew
7 * The contents of this file are subject to the Open
8 * Software License version 1.1 that can be found at
9 * http://www.opensource.org/licenses/osl-1.1.txt and is included herein
12 * Alternatively, the contents of this file may be used under the terms
13 * of the GNU General Public License version 2 (the "GPL") as distributed
14 * in the kernel source COPYING file, in which case the provisions of
15 * the GPL are applicable instead of the above. If you wish to allow
16 * the use of your version of this file only under the terms of the
17 * GPL and not to allow others to use your version of this file under
18 * the OSL, indicate your decision by deleting the provisions above and
19 * replace them with the notice and other provisions required by the GPL.
20 * If you do not delete the provisions above, a recipient may use your
21 * version of this file under either the OSL or the GPL.
24 * - Fixed a bug where the hotplug handlers for non-CK804/MCP04 were using
25 * mmio_base, which is only set for the CK804/MCP04 case.
28 * - Added support for CK804 SATA controller.
34 #include <linux/config.h>
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 #include <linux/init.h>
39 #include <linux/blkdev.h>
40 #include <linux/delay.h>
41 #include <linux/interrupt.h>
43 #include <scsi/scsi_host.h>
44 #include <linux/libata.h>
46 #define DRV_NAME "sata_nv"
47 #define DRV_VERSION "0.03"
50 #define NV_PIO_MASK 0x1f
51 #define NV_MWDMA_MASK 0x07
52 #define NV_UDMA_MASK 0x7f
53 #define NV_PORT0_SCR_REG_OFFSET 0x00
54 #define NV_PORT1_SCR_REG_OFFSET 0x40
56 #define NV_INT_STATUS 0x10
57 #define NV_INT_STATUS_CK804 0x440
58 #define NV_INT_STATUS_PDEV_INT 0x01
59 #define NV_INT_STATUS_PDEV_PM 0x02
60 #define NV_INT_STATUS_PDEV_ADDED 0x04
61 #define NV_INT_STATUS_PDEV_REMOVED 0x08
62 #define NV_INT_STATUS_SDEV_INT 0x10
63 #define NV_INT_STATUS_SDEV_PM 0x20
64 #define NV_INT_STATUS_SDEV_ADDED 0x40
65 #define NV_INT_STATUS_SDEV_REMOVED 0x80
66 #define NV_INT_STATUS_PDEV_HOTPLUG (NV_INT_STATUS_PDEV_ADDED | \
67 NV_INT_STATUS_PDEV_REMOVED)
68 #define NV_INT_STATUS_SDEV_HOTPLUG (NV_INT_STATUS_SDEV_ADDED | \
69 NV_INT_STATUS_SDEV_REMOVED)
70 #define NV_INT_STATUS_HOTPLUG (NV_INT_STATUS_PDEV_HOTPLUG | \
71 NV_INT_STATUS_SDEV_HOTPLUG)
73 #define NV_INT_ENABLE 0x11
74 #define NV_INT_ENABLE_CK804 0x441
75 #define NV_INT_ENABLE_PDEV_MASK 0x01
76 #define NV_INT_ENABLE_PDEV_PM 0x02
77 #define NV_INT_ENABLE_PDEV_ADDED 0x04
78 #define NV_INT_ENABLE_PDEV_REMOVED 0x08
79 #define NV_INT_ENABLE_SDEV_MASK 0x10
80 #define NV_INT_ENABLE_SDEV_PM 0x20
81 #define NV_INT_ENABLE_SDEV_ADDED 0x40
82 #define NV_INT_ENABLE_SDEV_REMOVED 0x80
83 #define NV_INT_ENABLE_PDEV_HOTPLUG (NV_INT_ENABLE_PDEV_ADDED | \
84 NV_INT_ENABLE_PDEV_REMOVED)
85 #define NV_INT_ENABLE_SDEV_HOTPLUG (NV_INT_ENABLE_SDEV_ADDED | \
86 NV_INT_ENABLE_SDEV_REMOVED)
87 #define NV_INT_ENABLE_HOTPLUG (NV_INT_ENABLE_PDEV_HOTPLUG | \
88 NV_INT_ENABLE_SDEV_HOTPLUG)
90 #define NV_INT_CONFIG 0x12
91 #define NV_INT_CONFIG_METHD 0x01 // 0 = INT, 1 = SMI
93 // For PCI config register 20
94 #define NV_MCP_SATA_CFG_20 0x50
95 #define NV_MCP_SATA_CFG_20_SATA_SPACE_EN 0x04
97 static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
98 irqreturn_t nv_interrupt (int irq, void *dev_instance, struct pt_regs *regs);
99 static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg);
100 static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
101 static void nv_host_stop (struct ata_host_set *host_set);
102 static void nv_enable_hotplug(struct ata_probe_ent *probe_ent);
103 static void nv_disable_hotplug(struct ata_host_set *host_set);
104 static void nv_check_hotplug(struct ata_host_set *host_set);
105 static void nv_enable_hotplug_ck804(struct ata_probe_ent *probe_ent);
106 static void nv_disable_hotplug_ck804(struct ata_host_set *host_set);
107 static void nv_check_hotplug_ck804(struct ata_host_set *host_set);
116 static struct pci_device_id nv_pci_tbl[] = {
117 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA,
118 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE2 },
119 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA,
120 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
121 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2,
122 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
123 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA,
124 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
125 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA2,
126 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
127 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA,
128 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
129 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA2,
130 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
131 { 0, } /* terminate list */
134 #define NV_HOST_FLAGS_SCR_MMIO 0x00000001
138 enum nv_host_type host_type;
139 unsigned long host_flags;
140 void (*enable_hotplug)(struct ata_probe_ent *probe_ent);
141 void (*disable_hotplug)(struct ata_host_set *host_set);
142 void (*check_hotplug)(struct ata_host_set *host_set);
145 static struct nv_host_desc nv_device_tbl[] = {
147 .host_type = NFORCE2,
148 .host_flags = 0x00000000,
149 .enable_hotplug = nv_enable_hotplug,
150 .disable_hotplug= nv_disable_hotplug,
151 .check_hotplug = nv_check_hotplug,
154 .host_type = NFORCE3,
155 .host_flags = 0x00000000,
156 .enable_hotplug = nv_enable_hotplug,
157 .disable_hotplug= nv_disable_hotplug,
158 .check_hotplug = nv_check_hotplug,
160 { .host_type = CK804,
161 .host_flags = NV_HOST_FLAGS_SCR_MMIO,
162 .enable_hotplug = nv_enable_hotplug_ck804,
163 .disable_hotplug= nv_disable_hotplug_ck804,
164 .check_hotplug = nv_check_hotplug_ck804,
170 struct nv_host_desc *host_desc;
173 static struct pci_driver nv_pci_driver = {
175 .id_table = nv_pci_tbl,
176 .probe = nv_init_one,
177 .remove = ata_pci_remove_one,
180 static Scsi_Host_Template nv_sht = {
181 .module = THIS_MODULE,
183 .ioctl = ata_scsi_ioctl,
184 .queuecommand = ata_scsi_queuecmd,
185 .eh_strategy_handler = ata_scsi_error,
186 .can_queue = ATA_DEF_QUEUE,
187 .this_id = ATA_SHT_THIS_ID,
188 .sg_tablesize = LIBATA_MAX_PRD,
189 .max_sectors = ATA_MAX_SECTORS,
190 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
191 .emulated = ATA_SHT_EMULATED,
192 .use_clustering = ATA_SHT_USE_CLUSTERING,
193 .proc_name = DRV_NAME,
194 .dma_boundary = ATA_DMA_BOUNDARY,
195 .slave_configure = ata_scsi_slave_config,
196 .bios_param = ata_std_bios_param,
199 static struct ata_port_operations nv_ops = {
200 .port_disable = ata_port_disable,
201 .tf_load = ata_tf_load,
202 .tf_read = ata_tf_read,
203 .exec_command = ata_exec_command,
204 .check_status = ata_check_status,
205 .dev_select = ata_std_dev_select,
206 .phy_reset = sata_phy_reset,
207 .bmdma_setup = ata_bmdma_setup,
208 .bmdma_start = ata_bmdma_start,
209 .qc_prep = ata_qc_prep,
210 .qc_issue = ata_qc_issue_prot,
211 .eng_timeout = ata_eng_timeout,
212 .irq_handler = nv_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_host_stop,
221 static struct ata_port_info nv_port_info = {
223 .host_flags = ATA_FLAG_SATA |
224 ATA_FLAG_SATA_RESET |
227 .pio_mask = NV_PIO_MASK,
228 .mwdma_mask = NV_MWDMA_MASK,
229 .udma_mask = NV_UDMA_MASK,
233 MODULE_AUTHOR("NVIDIA");
234 MODULE_DESCRIPTION("low-level driver for NVIDIA nForce SATA controller");
235 MODULE_LICENSE("GPL");
236 MODULE_DEVICE_TABLE(pci, nv_pci_tbl);
237 MODULE_VERSION(DRV_VERSION);
239 irqreturn_t nv_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
241 struct ata_host_set *host_set = dev_instance;
242 struct nv_host *host = host_set->private_data;
244 unsigned int handled = 0;
247 spin_lock_irqsave(&host_set->lock, flags);
249 for (i = 0; i < host_set->n_ports; i++) {
252 ap = host_set->ports[i];
253 if (ap && (!(ap->flags & ATA_FLAG_PORT_DISABLED))) {
254 struct ata_queued_cmd *qc;
256 qc = ata_qc_from_tag(ap, ap->active_tag);
257 if (qc && (!(qc->tf.ctl & ATA_NIEN)))
258 handled += ata_host_intr(ap, qc);
263 if (host->host_desc->check_hotplug)
264 host->host_desc->check_hotplug(host_set);
266 spin_unlock_irqrestore(&host_set->lock, flags);
268 return IRQ_RETVAL(handled);
271 static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg)
273 struct ata_host_set *host_set = ap->host_set;
274 struct nv_host *host = host_set->private_data;
276 if (sc_reg > SCR_CONTROL)
279 if (host->host_desc->host_flags & NV_HOST_FLAGS_SCR_MMIO)
280 return readl(ap->ioaddr.scr_addr + (sc_reg * 4));
282 return inl(ap->ioaddr.scr_addr + (sc_reg * 4));
285 static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
287 struct ata_host_set *host_set = ap->host_set;
288 struct nv_host *host = host_set->private_data;
290 if (sc_reg > SCR_CONTROL)
293 if (host->host_desc->host_flags & NV_HOST_FLAGS_SCR_MMIO)
294 writel(val, ap->ioaddr.scr_addr + (sc_reg * 4));
296 outl(val, ap->ioaddr.scr_addr + (sc_reg * 4));
299 static void nv_host_stop (struct ata_host_set *host_set)
301 struct nv_host *host = host_set->private_data;
303 // Disable hotplug event interrupts.
304 if (host->host_desc->disable_hotplug)
305 host->host_desc->disable_hotplug(host_set);
310 static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
312 static int printed_version = 0;
313 struct nv_host *host;
314 struct ata_port_info *ppi;
315 struct ata_probe_ent *probe_ent;
318 if (!printed_version++)
319 printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
321 rc = pci_enable_device(pdev);
325 rc = pci_request_regions(pdev, DRV_NAME);
327 goto err_out_disable;
329 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
331 goto err_out_regions;
332 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
334 goto err_out_regions;
339 probe_ent = ata_pci_init_native_mode(pdev, &ppi);
341 goto err_out_regions;
343 host = kmalloc(sizeof(struct nv_host), GFP_KERNEL);
345 goto err_out_free_ent;
347 host->host_desc = &nv_device_tbl[ent->driver_data];
349 probe_ent->private_data = host;
351 if (host->host_desc->host_flags & NV_HOST_FLAGS_SCR_MMIO) {
354 probe_ent->mmio_base = ioremap(pci_resource_start(pdev, 5),
355 pci_resource_len(pdev, 5));
356 if (probe_ent->mmio_base == NULL) {
358 goto err_out_free_host;
361 base = (unsigned long)probe_ent->mmio_base;
363 probe_ent->port[0].scr_addr =
364 base + NV_PORT0_SCR_REG_OFFSET;
365 probe_ent->port[1].scr_addr =
366 base + NV_PORT1_SCR_REG_OFFSET;
369 probe_ent->port[0].scr_addr =
370 pci_resource_start(pdev, 5) | NV_PORT0_SCR_REG_OFFSET;
371 probe_ent->port[1].scr_addr =
372 pci_resource_start(pdev, 5) | NV_PORT1_SCR_REG_OFFSET;
375 pci_set_master(pdev);
377 rc = ata_device_add(probe_ent);
379 goto err_out_iounmap;
381 // Enable hotplug event interrupts.
382 if (host->host_desc->enable_hotplug)
383 host->host_desc->enable_hotplug(probe_ent);
390 if (host->host_desc->host_flags & NV_HOST_FLAGS_SCR_MMIO)
391 iounmap(probe_ent->mmio_base);
397 pci_release_regions(pdev);
399 pci_disable_device(pdev);
404 static void nv_enable_hotplug(struct ata_probe_ent *probe_ent)
408 outb(NV_INT_STATUS_HOTPLUG,
409 probe_ent->port[0].scr_addr + NV_INT_STATUS);
411 intr_mask = inb(probe_ent->port[0].scr_addr + NV_INT_ENABLE);
412 intr_mask |= NV_INT_ENABLE_HOTPLUG;
414 outb(intr_mask, probe_ent->port[0].scr_addr + NV_INT_ENABLE);
417 static void nv_disable_hotplug(struct ata_host_set *host_set)
421 intr_mask = inb(host_set->ports[0]->ioaddr.scr_addr + NV_INT_ENABLE);
423 intr_mask &= ~(NV_INT_ENABLE_HOTPLUG);
425 outb(intr_mask, host_set->ports[0]->ioaddr.scr_addr + NV_INT_ENABLE);
428 static void nv_check_hotplug(struct ata_host_set *host_set)
432 intr_status = inb(host_set->ports[0]->ioaddr.scr_addr + NV_INT_STATUS);
434 // Clear interrupt status.
435 outb(0xff, host_set->ports[0]->ioaddr.scr_addr + NV_INT_STATUS);
437 if (intr_status & NV_INT_STATUS_HOTPLUG) {
438 if (intr_status & NV_INT_STATUS_PDEV_ADDED)
439 printk(KERN_WARNING "nv_sata: "
440 "Primary device added\n");
442 if (intr_status & NV_INT_STATUS_PDEV_REMOVED)
443 printk(KERN_WARNING "nv_sata: "
444 "Primary device removed\n");
446 if (intr_status & NV_INT_STATUS_SDEV_ADDED)
447 printk(KERN_WARNING "nv_sata: "
448 "Secondary device added\n");
450 if (intr_status & NV_INT_STATUS_SDEV_REMOVED)
451 printk(KERN_WARNING "nv_sata: "
452 "Secondary device removed\n");
456 static void nv_enable_hotplug_ck804(struct ata_probe_ent *probe_ent)
461 pci_read_config_byte(probe_ent->pdev, NV_MCP_SATA_CFG_20, ®val);
462 regval |= NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
463 pci_write_config_byte(probe_ent->pdev, NV_MCP_SATA_CFG_20, regval);
465 writeb(NV_INT_STATUS_HOTPLUG, probe_ent->mmio_base + NV_INT_STATUS_CK804);
467 intr_mask = readb(probe_ent->mmio_base + NV_INT_ENABLE_CK804);
468 intr_mask |= NV_INT_ENABLE_HOTPLUG;
470 writeb(intr_mask, probe_ent->mmio_base + NV_INT_ENABLE_CK804);
473 static void nv_disable_hotplug_ck804(struct ata_host_set *host_set)
478 intr_mask = readb(host_set->mmio_base + NV_INT_ENABLE_CK804);
480 intr_mask &= ~(NV_INT_ENABLE_HOTPLUG);
482 writeb(intr_mask, host_set->mmio_base + NV_INT_ENABLE_CK804);
484 pci_read_config_byte(host_set->pdev, NV_MCP_SATA_CFG_20, ®val);
485 regval &= ~NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
486 pci_write_config_byte(host_set->pdev, NV_MCP_SATA_CFG_20, regval);
489 static void nv_check_hotplug_ck804(struct ata_host_set *host_set)
493 intr_status = readb(host_set->mmio_base + NV_INT_STATUS_CK804);
495 // Clear interrupt status.
496 writeb(0xff, host_set->mmio_base + NV_INT_STATUS_CK804);
498 if (intr_status & NV_INT_STATUS_HOTPLUG) {
499 if (intr_status & NV_INT_STATUS_PDEV_ADDED)
500 printk(KERN_WARNING "nv_sata: "
501 "Primary device added\n");
503 if (intr_status & NV_INT_STATUS_PDEV_REMOVED)
504 printk(KERN_WARNING "nv_sata: "
505 "Primary device removed\n");
507 if (intr_status & NV_INT_STATUS_SDEV_ADDED)
508 printk(KERN_WARNING "nv_sata: "
509 "Secondary device added\n");
511 if (intr_status & NV_INT_STATUS_SDEV_REMOVED)
512 printk(KERN_WARNING "nv_sata: "
513 "Secondary device removed\n");
517 static int __init nv_init(void)
519 return pci_module_init(&nv_pci_driver);
522 static void __exit nv_exit(void)
524 pci_unregister_driver(&nv_pci_driver);
527 module_init(nv_init);
528 module_exit(nv_exit);