blob: 94cb17903fae673c679bc89edaa71d5dfb0bc20e [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
71 /* INT_CONFIG */
Jeff Garzik10ad05d2006-03-22 23:50:50 -050072 NV_INT_CONFIG = 0x12,
73 NV_INT_CONFIG_METHD = 0x01, // 0 = INT, 1 = SMI
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jeff Garzik10ad05d2006-03-22 23:50:50 -050075 // For PCI config register 20
76 NV_MCP_SATA_CFG_20 = 0x50,
77 NV_MCP_SATA_CFG_20_SATA_SPACE_EN = 0x04,
78};
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
Tejun Heoada364e2006-06-17 15:49:56 +090081static void nv_ck804_host_stop(struct ata_host_set *host_set);
82static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance,
83 struct pt_regs *regs);
84static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance,
85 struct pt_regs *regs);
86static irqreturn_t nv_ck804_interrupt(int irq, void *dev_instance,
87 struct pt_regs *regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg);
89static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91enum nv_host_type
92{
93 GENERIC,
94 NFORCE2,
Tejun Heo27e4b272006-06-17 15:49:55 +090095 NFORCE3 = NFORCE2, /* NF2 == NF3 as far as sata_nv is concerned */
Andy Curride7102452005-10-07 08:53:39 -070096 CK804
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
98
Jeff Garzik3b7d6972005-11-10 11:04:11 -050099static const struct pci_device_id nv_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA,
101 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE2 },
102 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA,
103 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
104 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2,
105 PCI_ANY_ID, PCI_ANY_ID, 0, 0, NFORCE3 },
106 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA,
107 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
108 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA2,
109 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
110 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA,
111 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
112 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA2,
113 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CK804 },
Daniel Drake541134c2005-07-03 13:44:39 +0100114 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA,
Andy Curride7102452005-10-07 08:53:39 -0700115 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Daniel Drake541134c2005-07-03 13:44:39 +0100116 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2,
Andy Curride7102452005-10-07 08:53:39 -0700117 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Daniel Drake541134c2005-07-03 13:44:39 +0100118 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA,
Andy Curride7102452005-10-07 08:53:39 -0700119 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Andy Curride86ee662005-09-19 06:17:52 -0700120 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2,
Andy Curride7102452005-10-07 08:53:39 -0700121 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Andrew Chew4c5c8162006-04-20 15:54:26 -0700122 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA,
123 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
124 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA2,
125 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
126 { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA3,
127 PCI_ANY_ID, PCI_ANY_ID, 0, 0, GENERIC },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
129 PCI_ANY_ID, PCI_ANY_ID,
130 PCI_CLASS_STORAGE_IDE<<8, 0xffff00, GENERIC },
Daniel Drake541134c2005-07-03 13:44:39 +0100131 { PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID,
132 PCI_ANY_ID, PCI_ANY_ID,
133 PCI_CLASS_STORAGE_RAID<<8, 0xffff00, GENERIC },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 { 0, } /* terminate list */
135};
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static struct pci_driver nv_pci_driver = {
138 .name = DRV_NAME,
139 .id_table = nv_pci_tbl,
140 .probe = nv_init_one,
141 .remove = ata_pci_remove_one,
142};
143
Jeff Garzik193515d2005-11-07 00:59:37 -0500144static struct scsi_host_template nv_sht = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .module = THIS_MODULE,
146 .name = DRV_NAME,
147 .ioctl = ata_scsi_ioctl,
148 .queuecommand = ata_scsi_queuecmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .can_queue = ATA_DEF_QUEUE,
150 .this_id = ATA_SHT_THIS_ID,
151 .sg_tablesize = LIBATA_MAX_PRD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
153 .emulated = ATA_SHT_EMULATED,
154 .use_clustering = ATA_SHT_USE_CLUSTERING,
155 .proc_name = DRV_NAME,
156 .dma_boundary = ATA_DMA_BOUNDARY,
157 .slave_configure = ata_scsi_slave_config,
Tejun Heoccf68c32006-05-31 18:28:09 +0900158 .slave_destroy = ata_scsi_slave_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .bios_param = ata_std_bios_param,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
Tejun Heoada364e2006-06-17 15:49:56 +0900162static const struct ata_port_operations nv_generic_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 .port_disable = ata_port_disable,
164 .tf_load = ata_tf_load,
165 .tf_read = ata_tf_read,
166 .exec_command = ata_exec_command,
167 .check_status = ata_check_status,
168 .dev_select = ata_std_dev_select,
169 .phy_reset = sata_phy_reset,
170 .bmdma_setup = ata_bmdma_setup,
171 .bmdma_start = ata_bmdma_start,
172 .bmdma_stop = ata_bmdma_stop,
173 .bmdma_status = ata_bmdma_status,
174 .qc_prep = ata_qc_prep,
175 .qc_issue = ata_qc_issue_prot,
176 .eng_timeout = ata_eng_timeout,
Alan Coxa6b2c5d2006-05-22 16:59:59 +0100177 .data_xfer = ata_pio_data_xfer,
Tejun Heoada364e2006-06-17 15:49:56 +0900178 .irq_handler = nv_generic_interrupt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 .irq_clear = ata_bmdma_irq_clear,
180 .scr_read = nv_scr_read,
181 .scr_write = nv_scr_write,
182 .port_start = ata_port_start,
183 .port_stop = ata_port_stop,
Tejun Heoe6faf082006-06-17 15:49:55 +0900184 .host_stop = ata_pci_host_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
Tejun Heoada364e2006-06-17 15:49:56 +0900187static const struct ata_port_operations nv_nf2_ops = {
188 .port_disable = ata_port_disable,
189 .tf_load = ata_tf_load,
190 .tf_read = ata_tf_read,
191 .exec_command = ata_exec_command,
192 .check_status = ata_check_status,
193 .dev_select = ata_std_dev_select,
194 .phy_reset = sata_phy_reset,
195 .bmdma_setup = ata_bmdma_setup,
196 .bmdma_start = ata_bmdma_start,
197 .bmdma_stop = ata_bmdma_stop,
198 .bmdma_status = ata_bmdma_status,
199 .qc_prep = ata_qc_prep,
200 .qc_issue = ata_qc_issue_prot,
201 .eng_timeout = ata_eng_timeout,
202 .data_xfer = ata_pio_data_xfer,
203 .irq_handler = nv_nf2_interrupt,
204 .irq_clear = ata_bmdma_irq_clear,
205 .scr_read = nv_scr_read,
206 .scr_write = nv_scr_write,
207 .port_start = ata_port_start,
208 .port_stop = ata_port_stop,
209 .host_stop = ata_pci_host_stop,
210};
211
212static const struct ata_port_operations nv_ck804_ops = {
213 .port_disable = ata_port_disable,
214 .tf_load = ata_tf_load,
215 .tf_read = ata_tf_read,
216 .exec_command = ata_exec_command,
217 .check_status = ata_check_status,
218 .dev_select = ata_std_dev_select,
219 .phy_reset = sata_phy_reset,
220 .bmdma_setup = ata_bmdma_setup,
221 .bmdma_start = ata_bmdma_start,
222 .bmdma_stop = ata_bmdma_stop,
223 .bmdma_status = ata_bmdma_status,
224 .qc_prep = ata_qc_prep,
225 .qc_issue = ata_qc_issue_prot,
226 .eng_timeout = ata_eng_timeout,
227 .data_xfer = ata_pio_data_xfer,
228 .irq_handler = nv_ck804_interrupt,
229 .irq_clear = ata_bmdma_irq_clear,
230 .scr_read = nv_scr_read,
231 .scr_write = nv_scr_write,
232 .port_start = ata_port_start,
233 .port_stop = ata_port_stop,
234 .host_stop = nv_ck804_host_stop,
235};
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/* FIXME: The hardware provides the necessary SATA PHY controls
238 * to support ATA_FLAG_SATA_RESET. However, it is currently
239 * necessary to disable that flag, to solve misdetection problems.
240 * See http://bugme.osdl.org/show_bug.cgi?id=3352 for more info.
241 *
242 * This problem really needs to be investigated further. But in the
243 * meantime, we avoid ATA_FLAG_SATA_RESET to get people working.
244 */
Tejun Heoada364e2006-06-17 15:49:56 +0900245static struct ata_port_info nv_port_info[] = {
246 /* generic */
247 {
248 .sht = &nv_sht,
249 .host_flags = ATA_FLAG_SATA |
250 /* ATA_FLAG_SATA_RESET | */
251 ATA_FLAG_SRST |
252 ATA_FLAG_NO_LEGACY,
253 .pio_mask = NV_PIO_MASK,
254 .mwdma_mask = NV_MWDMA_MASK,
255 .udma_mask = NV_UDMA_MASK,
256 .port_ops = &nv_generic_ops,
257 },
258 /* nforce2/3 */
259 {
260 .sht = &nv_sht,
261 .host_flags = ATA_FLAG_SATA |
262 /* ATA_FLAG_SATA_RESET | */
263 ATA_FLAG_SRST |
264 ATA_FLAG_NO_LEGACY,
265 .pio_mask = NV_PIO_MASK,
266 .mwdma_mask = NV_MWDMA_MASK,
267 .udma_mask = NV_UDMA_MASK,
268 .port_ops = &nv_nf2_ops,
269 },
270 /* ck804 */
271 {
272 .sht = &nv_sht,
273 .host_flags = ATA_FLAG_SATA |
274 /* ATA_FLAG_SATA_RESET | */
275 ATA_FLAG_SRST |
276 ATA_FLAG_NO_LEGACY,
277 .pio_mask = NV_PIO_MASK,
278 .mwdma_mask = NV_MWDMA_MASK,
279 .udma_mask = NV_UDMA_MASK,
280 .port_ops = &nv_ck804_ops,
281 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
284MODULE_AUTHOR("NVIDIA");
285MODULE_DESCRIPTION("low-level driver for NVIDIA nForce SATA controller");
286MODULE_LICENSE("GPL");
287MODULE_DEVICE_TABLE(pci, nv_pci_tbl);
288MODULE_VERSION(DRV_VERSION);
289
Tejun Heoada364e2006-06-17 15:49:56 +0900290static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance,
291 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct ata_host_set *host_set = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 unsigned int i;
295 unsigned int handled = 0;
296 unsigned long flags;
297
298 spin_lock_irqsave(&host_set->lock, flags);
299
300 for (i = 0; i < host_set->n_ports; i++) {
301 struct ata_port *ap;
302
303 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +0900304 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -0400305 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct ata_queued_cmd *qc;
307
308 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Leee50362e2005-09-27 17:39:50 +0800309 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 handled += ata_host_intr(ap, qc);
Andrew Chewb8870302006-01-04 19:13:04 -0800311 else
312 // No request pending? Clear interrupt status
313 // anyway, in case there's one pending.
314 ap->ops->check_status(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 }
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 spin_unlock_irqrestore(&host_set->lock, flags);
320
321 return IRQ_RETVAL(handled);
322}
323
Tejun Heoada364e2006-06-17 15:49:56 +0900324static int nv_host_intr(struct ata_port *ap, u8 irq_stat)
325{
326 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->active_tag);
327 int handled;
328
329 /* bail out if not our interrupt */
330 if (!(irq_stat & NV_INT_DEV))
331 return 0;
332
333 /* DEV interrupt w/ no active qc? */
334 if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
335 ata_check_status(ap);
336 return 1;
337 }
338
339 /* handle interrupt */
340 handled = ata_host_intr(ap, qc);
341 if (unlikely(!handled)) {
342 /* spurious, clear it */
343 ata_check_status(ap);
344 }
345
346 return 1;
347}
348
349static irqreturn_t nv_do_interrupt(struct ata_host_set *host_set, u8 irq_stat)
350{
351 int i, handled = 0;
352
353 for (i = 0; i < host_set->n_ports; i++) {
354 struct ata_port *ap = host_set->ports[i];
355
356 if (ap && !(ap->flags & ATA_FLAG_DISABLED))
357 handled += nv_host_intr(ap, irq_stat);
358
359 irq_stat >>= NV_INT_PORT_SHIFT;
360 }
361
362 return IRQ_RETVAL(handled);
363}
364
365static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance,
366 struct pt_regs *regs)
367{
368 struct ata_host_set *host_set = dev_instance;
369 unsigned long flags;
370 u8 irq_stat;
371 irqreturn_t ret;
372
373 spin_lock_irqsave(&host_set->lock, flags);
374 irq_stat = inb(host_set->ports[0]->ioaddr.scr_addr + NV_INT_STATUS);
375 ret = nv_do_interrupt(host_set, irq_stat);
376 spin_unlock_irqrestore(&host_set->lock, flags);
377
378 return ret;
379}
380
381static irqreturn_t nv_ck804_interrupt(int irq, void *dev_instance,
382 struct pt_regs *regs)
383{
384 struct ata_host_set *host_set = dev_instance;
385 unsigned long flags;
386 u8 irq_stat;
387 irqreturn_t ret;
388
389 spin_lock_irqsave(&host_set->lock, flags);
390 irq_stat = readb(host_set->mmio_base + NV_INT_STATUS_CK804);
391 ret = nv_do_interrupt(host_set, irq_stat);
392 spin_unlock_irqrestore(&host_set->lock, flags);
393
394 return ret;
395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg)
398{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (sc_reg > SCR_CONTROL)
400 return 0xffffffffU;
401
Jeff Garzik02cbd922006-03-22 23:59:46 -0500402 return ioread32((void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
406{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (sc_reg > SCR_CONTROL)
408 return;
409
Jeff Garzik02cbd922006-03-22 23:59:46 -0500410 iowrite32(val, (void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
414{
415 static int printed_version = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct ata_port_info *ppi;
417 struct ata_probe_ent *probe_ent;
418 int pci_dev_busy = 0;
419 int rc;
420 u32 bar;
Jeff Garzik02cbd922006-03-22 23:59:46 -0500421 unsigned long base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 // Make sure this is a SATA controller by counting the number of bars
424 // (NVIDIA SATA controllers will always have six bars). Otherwise,
425 // it's an IDE controller and we ignore it.
426 for (bar=0; bar<6; bar++)
427 if (pci_resource_start(pdev, bar) == 0)
428 return -ENODEV;
429
430 if (!printed_version++)
Jeff Garzika9524a72005-10-30 14:39:11 -0500431 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 rc = pci_enable_device(pdev);
434 if (rc)
435 goto err_out;
436
437 rc = pci_request_regions(pdev, DRV_NAME);
438 if (rc) {
439 pci_dev_busy = 1;
440 goto err_out_disable;
441 }
442
443 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
444 if (rc)
445 goto err_out_regions;
446 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
447 if (rc)
448 goto err_out_regions;
449
450 rc = -ENOMEM;
451
Tejun Heoada364e2006-06-17 15:49:56 +0900452 ppi = &nv_port_info[ent->driver_data];
Alan Cox47a86592005-10-04 08:09:19 -0400453 probe_ent = ata_pci_init_native_mode(pdev, &ppi, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!probe_ent)
455 goto err_out_regions;
456
Jeff Garzik02cbd922006-03-22 23:59:46 -0500457 probe_ent->mmio_base = pci_iomap(pdev, 5, 0);
458 if (!probe_ent->mmio_base) {
459 rc = -EIO;
Tejun Heoe6faf082006-06-17 15:49:55 +0900460 goto err_out_free_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462
Jeff Garzik02cbd922006-03-22 23:59:46 -0500463 base = (unsigned long)probe_ent->mmio_base;
464
465 probe_ent->port[0].scr_addr = base + NV_PORT0_SCR_REG_OFFSET;
466 probe_ent->port[1].scr_addr = base + NV_PORT1_SCR_REG_OFFSET;
467
Tejun Heoada364e2006-06-17 15:49:56 +0900468 /* enable SATA space for CK804 */
469 if (ent->driver_data == CK804) {
470 u8 regval;
471
472 pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
473 regval |= NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
474 pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
475 }
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 pci_set_master(pdev);
478
479 rc = ata_device_add(probe_ent);
480 if (rc != NV_PORTS)
481 goto err_out_iounmap;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 kfree(probe_ent);
484
485 return 0;
486
487err_out_iounmap:
Jeff Garzik02cbd922006-03-22 23:59:46 -0500488 pci_iounmap(pdev, probe_ent->mmio_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489err_out_free_ent:
490 kfree(probe_ent);
491err_out_regions:
492 pci_release_regions(pdev);
493err_out_disable:
494 if (!pci_dev_busy)
495 pci_disable_device(pdev);
496err_out:
497 return rc;
498}
499
Tejun Heoada364e2006-06-17 15:49:56 +0900500static void nv_ck804_host_stop(struct ata_host_set *host_set)
501{
502 struct pci_dev *pdev = to_pci_dev(host_set->dev);
503 u8 regval;
504
505 /* disable SATA space for CK804 */
506 pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
507 regval &= ~NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
508 pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
509
510 ata_pci_host_stop(host_set);
511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513static int __init nv_init(void)
514{
515 return pci_module_init(&nv_pci_driver);
516}
517
518static void __exit nv_exit(void)
519{
520 pci_unregister_driver(&nv_pci_driver);
521}
522
523module_init(nv_init);
524module_exit(nv_exit);