blob: 70f81941f76e67d87cc9fa9263dcd8b293c7740b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sata_nv.c - NVIDIA nForce SATA
3 *
4 * Copyright 2004 NVIDIA Corp. All rights reserved.
5 * Copyright 2004 Andrew Chew
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Jeff Garzikaa7e16d2005-08-29 15:12:56 -04008 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -040022 *
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
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>
Jeff Garzika9524a72005-10-30 14:39:11 -050042#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <scsi/scsi_host.h>
44#include <linux/libata.h>
45
46#define DRV_NAME "sata_nv"
Jeff Garzikaf643712006-04-02 20:41:36 -040047#define DRV_VERSION "0.9"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Jeff Garzik10ad05d2006-03-22 23:50:50 -050049enum {
50 NV_PORTS = 2,
51 NV_PIO_MASK = 0x1f,
52 NV_MWDMA_MASK = 0x07,
53 NV_UDMA_MASK = 0x7f,
54 NV_PORT0_SCR_REG_OFFSET = 0x00,
55 NV_PORT1_SCR_REG_OFFSET = 0x40,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jeff Garzik10ad05d2006-03-22 23:50:50 -050057 NV_INT_STATUS = 0x10,
58 NV_INT_STATUS_CK804 = 0x440,
59 NV_INT_STATUS_PDEV_INT = 0x01,
60 NV_INT_STATUS_PDEV_PM = 0x02,
61 NV_INT_STATUS_PDEV_ADDED = 0x04,
62 NV_INT_STATUS_PDEV_REMOVED = 0x08,
63 NV_INT_STATUS_SDEV_INT = 0x10,
64 NV_INT_STATUS_SDEV_PM = 0x20,
65 NV_INT_STATUS_SDEV_ADDED = 0x40,
66 NV_INT_STATUS_SDEV_REMOVED = 0x80,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Jeff Garzik10ad05d2006-03-22 23:50:50 -050068 NV_INT_ENABLE = 0x11,
69 NV_INT_ENABLE_CK804 = 0x441,
70 NV_INT_ENABLE_PDEV_MASK = 0x01,
71 NV_INT_ENABLE_PDEV_PM = 0x02,
72 NV_INT_ENABLE_PDEV_ADDED = 0x04,
73 NV_INT_ENABLE_PDEV_REMOVED = 0x08,
74 NV_INT_ENABLE_SDEV_MASK = 0x10,
75 NV_INT_ENABLE_SDEV_PM = 0x20,
76 NV_INT_ENABLE_SDEV_ADDED = 0x40,
77 NV_INT_ENABLE_SDEV_REMOVED = 0x80,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Jeff Garzik10ad05d2006-03-22 23:50:50 -050079 NV_INT_CONFIG = 0x12,
80 NV_INT_CONFIG_METHD = 0x01, // 0 = INT, 1 = SMI
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Jeff Garzik10ad05d2006-03-22 23:50:50 -050082 // For PCI config register 20
83 NV_MCP_SATA_CFG_20 = 0x50,
84 NV_MCP_SATA_CFG_20_SATA_SPACE_EN = 0x04,
85};
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
88static irqreturn_t nv_interrupt (int irq, void *dev_instance,
89 struct pt_regs *regs);
90static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg);
91static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
92static void nv_host_stop (struct ata_host_set *host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94enum nv_host_type
95{
96 GENERIC,
97 NFORCE2,
98 NFORCE3,
Andy Curride7102452005-10-07 08:53:39 -070099 CK804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Jeff Garzik3b7d6972005-11-10 11:04:11 -0500102static const struct pci_device_id nv_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA,
104 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE2 },
105 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA,
106 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
107 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2,
108 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
109 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA,
110 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
111 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA2,
112 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
113 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA,
114 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
115 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA2,
116 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
Daniel Drake541134c2005-07-03 13:44:39 +0100117 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA,
Andy Curride7102452005-10-07 08:53:39 -0700118 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Daniel Drake541134c2005-07-03 13:44:39 +0100119 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2,
Andy Curride7102452005-10-07 08:53:39 -0700120 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Daniel Drake541134c2005-07-03 13:44:39 +0100121 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA,
Andy Curride7102452005-10-07 08:53:39 -0700122 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Andy Curride86ee662005-09-19 06:17:52 -0700123 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2,
Andy Curride7102452005-10-07 08:53:39 -0700124 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Andrew Chew4c5c8162006-04-20 15:54:26 -0700125 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA,
126 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
127 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA2,
128 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
129 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA3,
130 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
132 PCI_ANY_ID, PCI_ANY_ID,
133 PCI_CLASS_STORAGE_IDE<<8, 0xffff00, GENERIC },
Daniel Drake541134c2005-07-03 13:44:39 +0100134 { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
135 PCI_ANY_ID, PCI_ANY_ID,
136 PCI_CLASS_STORAGE_RAID<<8, 0xffff00, GENERIC },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 { 0, } /* terminate list */
138};
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140struct nv_host_desc
141{
142 enum nv_host_type host_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144static struct nv_host_desc nv_device_tbl[] = {
145 {
146 .host_type = GENERIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 },
148 {
149 .host_type = NFORCE2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 },
151 {
152 .host_type = NFORCE3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 },
154 { .host_type = CK804,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 },
156};
157
158struct nv_host
159{
160 struct nv_host_desc *host_desc;
161 unsigned long host_flags;
162};
163
164static struct pci_driver nv_pci_driver = {
165 .name = DRV_NAME,
166 .id_table = nv_pci_tbl,
167 .probe = nv_init_one,
168 .remove = ata_pci_remove_one,
169};
170
Jeff Garzik193515d2005-11-07 00:59:37 -0500171static struct scsi_host_template nv_sht = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .module = THIS_MODULE,
173 .name = DRV_NAME,
174 .ioctl = ata_scsi_ioctl,
175 .queuecommand = ata_scsi_queuecmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .can_queue = ATA_DEF_QUEUE,
177 .this_id = ATA_SHT_THIS_ID,
178 .sg_tablesize = LIBATA_MAX_PRD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
180 .emulated = ATA_SHT_EMULATED,
181 .use_clustering = ATA_SHT_USE_CLUSTERING,
182 .proc_name = DRV_NAME,
183 .dma_boundary = ATA_DMA_BOUNDARY,
184 .slave_configure = ata_scsi_slave_config,
Tejun Heoccf68c32006-05-31 18:28:09 +0900185 .slave_destroy = ata_scsi_slave_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .bios_param = ata_std_bios_param,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Jeff Garzik057ace52005-10-22 14:27:05 -0400189static const struct ata_port_operations nv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 .port_disable = ata_port_disable,
191 .tf_load = ata_tf_load,
192 .tf_read = ata_tf_read,
193 .exec_command = ata_exec_command,
194 .check_status = ata_check_status,
195 .dev_select = ata_std_dev_select,
196 .phy_reset = sata_phy_reset,
197 .bmdma_setup = ata_bmdma_setup,
198 .bmdma_start = ata_bmdma_start,
199 .bmdma_stop = ata_bmdma_stop,
200 .bmdma_status = ata_bmdma_status,
201 .qc_prep = ata_qc_prep,
202 .qc_issue = ata_qc_issue_prot,
203 .eng_timeout = ata_eng_timeout,
Alan Coxa6b2c5d2006-05-22 16:59:59 +0100204 .data_xfer = ata_pio_data_xfer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .irq_handler = nv_interrupt,
206 .irq_clear = ata_bmdma_irq_clear,
207 .scr_read = nv_scr_read,
208 .scr_write = nv_scr_write,
209 .port_start = ata_port_start,
210 .port_stop = ata_port_stop,
211 .host_stop = nv_host_stop,
212};
213
214/* FIXME: The hardware provides the necessary SATA PHY controls
215 * to support ATA_FLAG_SATA_RESET. However, it is currently
216 * necessary to disable that flag, to solve misdetection problems.
217 * See http://bugme.osdl.org/show_bug.cgi?id=3352 for more info.
218 *
219 * This problem really needs to be investigated further. But in the
220 * meantime, we avoid ATA_FLAG_SATA_RESET to get people working.
221 */
222static struct ata_port_info nv_port_info = {
223 .sht = &nv_sht,
224 .host_flags = ATA_FLAG_SATA |
225 /* ATA_FLAG_SATA_RESET | */
226 ATA_FLAG_SRST |
227 ATA_FLAG_NO_LEGACY,
228 .pio_mask = NV_PIO_MASK,
229 .mwdma_mask = NV_MWDMA_MASK,
230 .udma_mask = NV_UDMA_MASK,
231 .port_ops = &nv_ops,
232};
233
234MODULE_AUTHOR("NVIDIA");
235MODULE_DESCRIPTION("low-level driver for NVIDIA nForce SATA controller");
236MODULE_LICENSE("GPL");
237MODULE_DEVICE_TABLE(pci, nv_pci_tbl);
238MODULE_VERSION(DRV_VERSION);
239
240static irqreturn_t nv_interrupt (int irq, void *dev_instance,
241 struct pt_regs *regs)
242{
243 struct ata_host_set *host_set = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 unsigned int i;
245 unsigned int handled = 0;
246 unsigned long flags;
247
248 spin_lock_irqsave(&host_set->lock, flags);
249
250 for (i = 0; i < host_set->n_ports; i++) {
251 struct ata_port *ap;
252
253 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +0900254 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -0400255 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct ata_queued_cmd *qc;
257
258 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Leee50362e2005-09-27 17:39:50 +0800259 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 handled += ata_host_intr(ap, qc);
Andrew Chewb8870302006-01-04 19:13:04 -0800261 else
262 // No request pending? Clear interrupt status
263 // anyway, in case there's one pending.
264 ap->ops->check_status(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
267 }
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spin_unlock_irqrestore(&host_set->lock, flags);
270
271 return IRQ_RETVAL(handled);
272}
273
274static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg)
275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (sc_reg > SCR_CONTROL)
277 return 0xffffffffU;
278
Jeff Garzik02cbd922006-03-22 23:59:46 -0500279 return ioread32((void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
283{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (sc_reg > SCR_CONTROL)
285 return;
286
Jeff Garzik02cbd922006-03-22 23:59:46 -0500287 iowrite32(val, (void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static void nv_host_stop (struct ata_host_set *host_set)
291{
292 struct nv_host *host = host_set->private_data;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 kfree(host);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -0400295
Jeff Garzik02cbd922006-03-22 23:59:46 -0500296 ata_pci_host_stop(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
300{
301 static int printed_version = 0;
302 struct nv_host *host;
303 struct ata_port_info *ppi;
304 struct ata_probe_ent *probe_ent;
305 int pci_dev_busy = 0;
306 int rc;
307 u32 bar;
Jeff Garzik02cbd922006-03-22 23:59:46 -0500308 unsigned long base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 // Make sure this is a SATA controller by counting the number of bars
311 // (NVIDIA SATA controllers will always have six bars). Otherwise,
312 // it's an IDE controller and we ignore it.
313 for (bar=0; bar<6; bar++)
314 if (pci_resource_start(pdev, bar) == 0)
315 return -ENODEV;
316
317 if (!printed_version++)
Jeff Garzika9524a72005-10-30 14:39:11 -0500318 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 rc = pci_enable_device(pdev);
321 if (rc)
322 goto err_out;
323
324 rc = pci_request_regions(pdev, DRV_NAME);
325 if (rc) {
326 pci_dev_busy = 1;
327 goto err_out_disable;
328 }
329
330 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
331 if (rc)
332 goto err_out_regions;
333 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
334 if (rc)
335 goto err_out_regions;
336
337 rc = -ENOMEM;
338
339 ppi = &nv_port_info;
Alan Cox47a86592005-10-04 08:09:19 -0400340 probe_ent = ata_pci_init_native_mode(pdev, &ppi, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!probe_ent)
342 goto err_out_regions;
343
344 host = kmalloc(sizeof(struct nv_host), GFP_KERNEL);
345 if (!host)
346 goto err_out_free_ent;
347
348 memset(host, 0, sizeof(struct nv_host));
349 host->host_desc = &nv_device_tbl[ent->driver_data];
350
351 probe_ent->private_data = host;
352
Jeff Garzik02cbd922006-03-22 23:59:46 -0500353 probe_ent->mmio_base = pci_iomap(pdev, 5, 0);
354 if (!probe_ent->mmio_base) {
355 rc = -EIO;
356 goto err_out_free_host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
Jeff Garzik02cbd922006-03-22 23:59:46 -0500359 base = (unsigned long)probe_ent->mmio_base;
360
361 probe_ent->port[0].scr_addr = base + NV_PORT0_SCR_REG_OFFSET;
362 probe_ent->port[1].scr_addr = base + NV_PORT1_SCR_REG_OFFSET;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 pci_set_master(pdev);
365
366 rc = ata_device_add(probe_ent);
367 if (rc != NV_PORTS)
368 goto err_out_iounmap;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 kfree(probe_ent);
371
372 return 0;
373
374err_out_iounmap:
Jeff Garzik02cbd922006-03-22 23:59:46 -0500375 pci_iounmap(pdev, probe_ent->mmio_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376err_out_free_host:
377 kfree(host);
378err_out_free_ent:
379 kfree(probe_ent);
380err_out_regions:
381 pci_release_regions(pdev);
382err_out_disable:
383 if (!pci_dev_busy)
384 pci_disable_device(pdev);
385err_out:
386 return rc;
387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static int __init nv_init(void)
390{
391 return pci_module_init(&nv_pci_driver);
392}
393
394static void __exit nv_exit(void)
395{
396 pci_unregister_driver(&nv_pci_driver);
397}
398
399module_init(nv_init);
400module_exit(nv_exit);