blob: 19deaa42fbf0dbf41f96cc3f241dd466894cb4f3 [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
Tejun Heo27e4b272006-06-17 15:49:55 +090057 /* INT_STATUS/ENABLE */
Jeff Garzik10ad05d2006-03-22 23:50:50 -050058 NV_INT_STATUS = 0x10,
Jeff Garzik10ad05d2006-03-22 23:50:50 -050059 NV_INT_ENABLE = 0x11,
Tejun Heo27e4b272006-06-17 15:49:55 +090060 NV_INT_STATUS_CK804 = 0x440,
Jeff Garzik10ad05d2006-03-22 23:50:50 -050061 NV_INT_ENABLE_CK804 = 0x441,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Tejun Heo27e4b272006-06-17 15:49:55 +090063 /* INT_STATUS/ENABLE bits */
64 NV_INT_DEV = 0x01,
65 NV_INT_PM = 0x02,
66 NV_INT_ADDED = 0x04,
67 NV_INT_REMOVED = 0x08,
68
69 NV_INT_PORT_SHIFT = 4, /* each port occupies 4 bits */
70
Tejun Heo39f87582006-06-17 15:49:56 +090071 NV_INT_ALL = 0x0f,
Tejun Heo5a44eff2006-06-17 15:49:56 +090072 NV_INT_MASK = NV_INT_DEV |
73 NV_INT_ADDED | NV_INT_REMOVED,
Tejun Heo39f87582006-06-17 15:49:56 +090074
Tejun Heo27e4b272006-06-17 15:49:55 +090075 /* INT_CONFIG */
Jeff Garzik10ad05d2006-03-22 23:50:50 -050076 NV_INT_CONFIG = 0x12,
77 NV_INT_CONFIG_METHD = 0x01, // 0 = INT, 1 = SMI
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Jeff Garzik10ad05d2006-03-22 23:50:50 -050079 // For PCI config register 20
80 NV_MCP_SATA_CFG_20 = 0x50,
81 NV_MCP_SATA_CFG_20_SATA_SPACE_EN = 0x04,
82};
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
Tejun Heoada364e2006-06-17 15:49:56 +090085static void nv_ck804_host_stop(struct ata_host_set *host_set);
86static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance,
87 struct pt_regs *regs);
88static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance,
89 struct pt_regs *regs);
90static irqreturn_t nv_ck804_interrupt(int irq, void *dev_instance,
91 struct pt_regs *regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg);
93static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Tejun Heo39f87582006-06-17 15:49:56 +090095static void nv_nf2_freeze(struct ata_port *ap);
96static void nv_nf2_thaw(struct ata_port *ap);
97static void nv_ck804_freeze(struct ata_port *ap);
98static void nv_ck804_thaw(struct ata_port *ap);
99static void nv_error_handler(struct ata_port *ap);
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101enum nv_host_type
102{
103 GENERIC,
104 NFORCE2,
Tejun Heo27e4b272006-06-17 15:49:55 +0900105 NFORCE3 = NFORCE2, /* NF2 == NF3 as far as sata_nv is concerned */
Andy Curride7102452005-10-07 08:53:39 -0700106 CK804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
Jeff Garzik3b7d6972005-11-10 11:04:11 -0500109static const struct pci_device_id nv_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA,
111 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE2 },
112 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA,
113 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
114 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2,
115 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
116 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA,
117 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
118 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA2,
119 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
120 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA,
121 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
122 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA2,
123 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
Daniel Drake541134c2005-07-03 13:44:39 +0100124 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA,
Andy Curride7102452005-10-07 08:53:39 -0700125 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Daniel Drake541134c2005-07-03 13:44:39 +0100126 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2,
Andy Curride7102452005-10-07 08:53:39 -0700127 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Daniel Drake541134c2005-07-03 13:44:39 +0100128 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA,
Andy Curride7102452005-10-07 08:53:39 -0700129 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Andy Curride86ee662005-09-19 06:17:52 -0700130 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2,
Andy Curride7102452005-10-07 08:53:39 -0700131 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Andrew Chew4c5c8162006-04-20 15:54:26 -0700132 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA,
133 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
134 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA2,
135 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
136 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA3,
137 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
139 PCI_ANY_ID, PCI_ANY_ID,
140 PCI_CLASS_STORAGE_IDE<<8, 0xffff00, GENERIC },
Daniel Drake541134c2005-07-03 13:44:39 +0100141 { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
142 PCI_ANY_ID, PCI_ANY_ID,
143 PCI_CLASS_STORAGE_RAID<<8, 0xffff00, GENERIC },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 { 0, } /* terminate list */
145};
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static struct pci_driver nv_pci_driver = {
148 .name = DRV_NAME,
149 .id_table = nv_pci_tbl,
150 .probe = nv_init_one,
151 .remove = ata_pci_remove_one,
152};
153
Jeff Garzik193515d2005-11-07 00:59:37 -0500154static struct scsi_host_template nv_sht = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .module = THIS_MODULE,
156 .name = DRV_NAME,
157 .ioctl = ata_scsi_ioctl,
158 .queuecommand = ata_scsi_queuecmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .can_queue = ATA_DEF_QUEUE,
160 .this_id = ATA_SHT_THIS_ID,
161 .sg_tablesize = LIBATA_MAX_PRD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
163 .emulated = ATA_SHT_EMULATED,
164 .use_clustering = ATA_SHT_USE_CLUSTERING,
165 .proc_name = DRV_NAME,
166 .dma_boundary = ATA_DMA_BOUNDARY,
167 .slave_configure = ata_scsi_slave_config,
Tejun Heoccf68c32006-05-31 18:28:09 +0900168 .slave_destroy = ata_scsi_slave_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 .bios_param = ata_std_bios_param,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170};
171
Tejun Heoada364e2006-06-17 15:49:56 +0900172static const struct ata_port_operations nv_generic_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .bmdma_setup = ata_bmdma_setup,
180 .bmdma_start = ata_bmdma_start,
181 .bmdma_stop = ata_bmdma_stop,
182 .bmdma_status = ata_bmdma_status,
183 .qc_prep = ata_qc_prep,
184 .qc_issue = ata_qc_issue_prot,
Tejun Heo39f87582006-06-17 15:49:56 +0900185 .freeze = ata_bmdma_freeze,
186 .thaw = ata_bmdma_thaw,
187 .error_handler = nv_error_handler,
188 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Alan Coxa6b2c5d2006-05-22 16:59:59 +0100189 .data_xfer = ata_pio_data_xfer,
Tejun Heoada364e2006-06-17 15:49:56 +0900190 .irq_handler = nv_generic_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .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,
Tejun Heoe6faf082006-06-17 15:49:55 +0900196 .host_stop = ata_pci_host_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
Tejun Heoada364e2006-06-17 15:49:56 +0900199static const struct ata_port_operations nv_nf2_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,
Tejun Heoada364e2006-06-17 15:49:56 +0900206 .bmdma_setup = ata_bmdma_setup,
207 .bmdma_start = ata_bmdma_start,
208 .bmdma_stop = ata_bmdma_stop,
209 .bmdma_status = ata_bmdma_status,
210 .qc_prep = ata_qc_prep,
211 .qc_issue = ata_qc_issue_prot,
Tejun Heo39f87582006-06-17 15:49:56 +0900212 .freeze = nv_nf2_freeze,
213 .thaw = nv_nf2_thaw,
214 .error_handler = nv_error_handler,
215 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Tejun Heoada364e2006-06-17 15:49:56 +0900216 .data_xfer = ata_pio_data_xfer,
217 .irq_handler = nv_nf2_interrupt,
218 .irq_clear = ata_bmdma_irq_clear,
219 .scr_read = nv_scr_read,
220 .scr_write = nv_scr_write,
221 .port_start = ata_port_start,
222 .port_stop = ata_port_stop,
223 .host_stop = ata_pci_host_stop,
224};
225
226static const struct ata_port_operations nv_ck804_ops = {
227 .port_disable = ata_port_disable,
228 .tf_load = ata_tf_load,
229 .tf_read = ata_tf_read,
230 .exec_command = ata_exec_command,
231 .check_status = ata_check_status,
232 .dev_select = ata_std_dev_select,
Tejun Heoada364e2006-06-17 15:49:56 +0900233 .bmdma_setup = ata_bmdma_setup,
234 .bmdma_start = ata_bmdma_start,
235 .bmdma_stop = ata_bmdma_stop,
236 .bmdma_status = ata_bmdma_status,
237 .qc_prep = ata_qc_prep,
238 .qc_issue = ata_qc_issue_prot,
Tejun Heo39f87582006-06-17 15:49:56 +0900239 .freeze = nv_ck804_freeze,
240 .thaw = nv_ck804_thaw,
241 .error_handler = nv_error_handler,
242 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Tejun Heoada364e2006-06-17 15:49:56 +0900243 .data_xfer = ata_pio_data_xfer,
244 .irq_handler = nv_ck804_interrupt,
245 .irq_clear = ata_bmdma_irq_clear,
246 .scr_read = nv_scr_read,
247 .scr_write = nv_scr_write,
248 .port_start = ata_port_start,
249 .port_stop = ata_port_stop,
250 .host_stop = nv_ck804_host_stop,
251};
252
Tejun Heoada364e2006-06-17 15:49:56 +0900253static struct ata_port_info nv_port_info[] = {
254 /* generic */
255 {
256 .sht = &nv_sht,
Tejun Heo39f87582006-06-17 15:49:56 +0900257 .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY,
Tejun Heoada364e2006-06-17 15:49:56 +0900258 .pio_mask = NV_PIO_MASK,
259 .mwdma_mask = NV_MWDMA_MASK,
260 .udma_mask = NV_UDMA_MASK,
261 .port_ops = &nv_generic_ops,
262 },
263 /* nforce2/3 */
264 {
265 .sht = &nv_sht,
Tejun Heo39f87582006-06-17 15:49:56 +0900266 .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY,
Tejun Heoada364e2006-06-17 15:49:56 +0900267 .pio_mask = NV_PIO_MASK,
268 .mwdma_mask = NV_MWDMA_MASK,
269 .udma_mask = NV_UDMA_MASK,
270 .port_ops = &nv_nf2_ops,
271 },
272 /* ck804 */
273 {
274 .sht = &nv_sht,
Tejun Heo39f87582006-06-17 15:49:56 +0900275 .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY,
Tejun Heoada364e2006-06-17 15:49:56 +0900276 .pio_mask = NV_PIO_MASK,
277 .mwdma_mask = NV_MWDMA_MASK,
278 .udma_mask = NV_UDMA_MASK,
279 .port_ops = &nv_ck804_ops,
280 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
282
283MODULE_AUTHOR("NVIDIA");
284MODULE_DESCRIPTION("low-level driver for NVIDIA nForce SATA controller");
285MODULE_LICENSE("GPL");
286MODULE_DEVICE_TABLE(pci, nv_pci_tbl);
287MODULE_VERSION(DRV_VERSION);
288
Tejun Heoada364e2006-06-17 15:49:56 +0900289static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance,
290 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct ata_host_set *host_set = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 unsigned int i;
294 unsigned int handled = 0;
295 unsigned long flags;
296
297 spin_lock_irqsave(&host_set->lock, flags);
298
299 for (i = 0; i < host_set->n_ports; i++) {
300 struct ata_port *ap;
301
302 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +0900303 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -0400304 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct ata_queued_cmd *qc;
306
307 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Leee50362e2005-09-27 17:39:50 +0800308 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 handled += ata_host_intr(ap, qc);
Andrew Chewb8870302006-01-04 19:13:04 -0800310 else
311 // No request pending? Clear interrupt status
312 // anyway, in case there's one pending.
313 ap->ops->check_status(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 }
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 spin_unlock_irqrestore(&host_set->lock, flags);
319
320 return IRQ_RETVAL(handled);
321}
322
Tejun Heoada364e2006-06-17 15:49:56 +0900323static int nv_host_intr(struct ata_port *ap, u8 irq_stat)
324{
325 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
326 int handled;
327
Tejun Heo5a44eff2006-06-17 15:49:56 +0900328 /* freeze if hotplugged */
329 if (unlikely(irq_stat & (NV_INT_ADDED | NV_INT_REMOVED))) {
330 ata_port_freeze(ap);
331 return 1;
332 }
333
Tejun Heoada364e2006-06-17 15:49:56 +0900334 /* bail out if not our interrupt */
335 if (!(irq_stat & NV_INT_DEV))
336 return 0;
337
338 /* DEV interrupt w/ no active qc? */
339 if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
340 ata_check_status(ap);
341 return 1;
342 }
343
344 /* handle interrupt */
345 handled = ata_host_intr(ap, qc);
346 if (unlikely(!handled)) {
347 /* spurious, clear it */
348 ata_check_status(ap);
349 }
350
351 return 1;
352}
353
354static irqreturn_t nv_do_interrupt(struct ata_host_set *host_set, u8 irq_stat)
355{
356 int i, handled = 0;
357
358 for (i = 0; i < host_set->n_ports; i++) {
359 struct ata_port *ap = host_set->ports[i];
360
361 if (ap && !(ap->flags & ATA_FLAG_DISABLED))
362 handled += nv_host_intr(ap, irq_stat);
363
364 irq_stat >>= NV_INT_PORT_SHIFT;
365 }
366
367 return IRQ_RETVAL(handled);
368}
369
370static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance,
371 struct pt_regs *regs)
372{
373 struct ata_host_set *host_set = dev_instance;
374 unsigned long flags;
375 u8 irq_stat;
376 irqreturn_t ret;
377
378 spin_lock_irqsave(&host_set->lock, flags);
379 irq_stat = inb(host_set->ports[0]->ioaddr.scr_addr + NV_INT_STATUS);
380 ret = nv_do_interrupt(host_set, irq_stat);
381 spin_unlock_irqrestore(&host_set->lock, flags);
382
383 return ret;
384}
385
386static irqreturn_t nv_ck804_interrupt(int irq, void *dev_instance,
387 struct pt_regs *regs)
388{
389 struct ata_host_set *host_set = dev_instance;
390 unsigned long flags;
391 u8 irq_stat;
392 irqreturn_t ret;
393
394 spin_lock_irqsave(&host_set->lock, flags);
395 irq_stat = readb(host_set->mmio_base + NV_INT_STATUS_CK804);
396 ret = nv_do_interrupt(host_set, irq_stat);
397 spin_unlock_irqrestore(&host_set->lock, flags);
398
399 return ret;
400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg)
403{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (sc_reg > SCR_CONTROL)
405 return 0xffffffffU;
406
Jeff Garzik02cbd922006-03-22 23:59:46 -0500407 return ioread32((void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
411{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (sc_reg > SCR_CONTROL)
413 return;
414
Jeff Garzik02cbd922006-03-22 23:59:46 -0500415 iowrite32(val, (void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Tejun Heo39f87582006-06-17 15:49:56 +0900418static void nv_nf2_freeze(struct ata_port *ap)
419{
420 unsigned long scr_addr = ap->host_set->ports[0]->ioaddr.scr_addr;
421 int shift = ap->port_no * NV_INT_PORT_SHIFT;
422 u8 mask;
423
424 mask = inb(scr_addr + NV_INT_ENABLE);
425 mask &= ~(NV_INT_ALL << shift);
426 outb(mask, scr_addr + NV_INT_ENABLE);
427}
428
429static void nv_nf2_thaw(struct ata_port *ap)
430{
431 unsigned long scr_addr = ap->host_set->ports[0]->ioaddr.scr_addr;
432 int shift = ap->port_no * NV_INT_PORT_SHIFT;
433 u8 mask;
434
435 outb(NV_INT_ALL << shift, scr_addr + NV_INT_STATUS);
436
437 mask = inb(scr_addr + NV_INT_ENABLE);
438 mask |= (NV_INT_MASK << shift);
439 outb(mask, scr_addr + NV_INT_ENABLE);
440}
441
442static void nv_ck804_freeze(struct ata_port *ap)
443{
444 void __iomem *mmio_base = ap->host_set->mmio_base;
445 int shift = ap->port_no * NV_INT_PORT_SHIFT;
446 u8 mask;
447
448 mask = readb(mmio_base + NV_INT_ENABLE_CK804);
449 mask &= ~(NV_INT_ALL << shift);
450 writeb(mask, mmio_base + NV_INT_ENABLE_CK804);
451}
452
453static void nv_ck804_thaw(struct ata_port *ap)
454{
455 void __iomem *mmio_base = ap->host_set->mmio_base;
456 int shift = ap->port_no * NV_INT_PORT_SHIFT;
457 u8 mask;
458
459 writeb(NV_INT_ALL << shift, mmio_base + NV_INT_STATUS_CK804);
460
461 mask = readb(mmio_base + NV_INT_ENABLE_CK804);
462 mask |= (NV_INT_MASK << shift);
463 writeb(mask, mmio_base + NV_INT_ENABLE_CK804);
464}
465
466static int nv_hardreset(struct ata_port *ap, unsigned int *class)
467{
468 unsigned int dummy;
469
470 /* SATA hardreset fails to retrieve proper device signature on
471 * some controllers. Don't classify on hardreset. For more
472 * info, see http://bugme.osdl.org/show_bug.cgi?id=3352
473 */
474 return sata_std_hardreset(ap, &dummy);
475}
476
477static void nv_error_handler(struct ata_port *ap)
478{
479 ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset,
480 nv_hardreset, ata_std_postreset);
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
484{
485 static int printed_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct ata_port_info *ppi;
487 struct ata_probe_ent *probe_ent;
488 int pci_dev_busy = 0;
489 int rc;
490 u32 bar;
Jeff Garzik02cbd922006-03-22 23:59:46 -0500491 unsigned long base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 // Make sure this is a SATA controller by counting the number of bars
494 // (NVIDIA SATA controllers will always have six bars). Otherwise,
495 // it's an IDE controller and we ignore it.
496 for (bar=0; bar<6; bar++)
497 if (pci_resource_start(pdev, bar) == 0)
498 return -ENODEV;
499
500 if (!printed_version++)
Jeff Garzika9524a72005-10-30 14:39:11 -0500501 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 rc = pci_enable_device(pdev);
504 if (rc)
505 goto err_out;
506
507 rc = pci_request_regions(pdev, DRV_NAME);
508 if (rc) {
509 pci_dev_busy = 1;
510 goto err_out_disable;
511 }
512
513 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
514 if (rc)
515 goto err_out_regions;
516 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
517 if (rc)
518 goto err_out_regions;
519
520 rc = -ENOMEM;
521
Tejun Heoada364e2006-06-17 15:49:56 +0900522 ppi = &nv_port_info[ent->driver_data];
Alan Cox47a86592005-10-04 08:09:19 -0400523 probe_ent = ata_pci_init_native_mode(pdev, &ppi, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (!probe_ent)
525 goto err_out_regions;
526
Jeff Garzik02cbd922006-03-22 23:59:46 -0500527 probe_ent->mmio_base = pci_iomap(pdev, 5, 0);
528 if (!probe_ent->mmio_base) {
529 rc = -EIO;
Tejun Heoe6faf082006-06-17 15:49:55 +0900530 goto err_out_free_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
Jeff Garzik02cbd922006-03-22 23:59:46 -0500533 base = (unsigned long)probe_ent->mmio_base;
534
535 probe_ent->port[0].scr_addr = base + NV_PORT0_SCR_REG_OFFSET;
536 probe_ent->port[1].scr_addr = base + NV_PORT1_SCR_REG_OFFSET;
537
Tejun Heoada364e2006-06-17 15:49:56 +0900538 /* enable SATA space for CK804 */
539 if (ent->driver_data == CK804) {
540 u8 regval;
541
542 pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
543 regval |= NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
544 pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
545 }
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 pci_set_master(pdev);
548
549 rc = ata_device_add(probe_ent);
550 if (rc != NV_PORTS)
551 goto err_out_iounmap;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 kfree(probe_ent);
554
555 return 0;
556
557err_out_iounmap:
Jeff Garzik02cbd922006-03-22 23:59:46 -0500558 pci_iounmap(pdev, probe_ent->mmio_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559err_out_free_ent:
560 kfree(probe_ent);
561err_out_regions:
562 pci_release_regions(pdev);
563err_out_disable:
564 if (!pci_dev_busy)
565 pci_disable_device(pdev);
566err_out:
567 return rc;
568}
569
Tejun Heoada364e2006-06-17 15:49:56 +0900570static void nv_ck804_host_stop(struct ata_host_set *host_set)
571{
572 struct pci_dev *pdev = to_pci_dev(host_set->dev);
573 u8 regval;
574
575 /* disable SATA space for CK804 */
576 pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
577 regval &= ~NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
578 pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
579
580 ata_pci_host_stop(host_set);
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static int __init nv_init(void)
584{
585 return pci_module_init(&nv_pci_driver);
586}
587
588static void __exit nv_exit(void)
589{
590 pci_unregister_driver(&nv_pci_driver);
591}
592
593module_init(nv_init);
594module_exit(nv_exit);