This commit was generated by cvs2svn to compensate for changes in r517,
[linux-2.6.git] / drivers / scsi / sata_nv.c
1 /*
2  *  sata_nv.c - NVIDIA nForce SATA
3  *
4  *  Copyright 2004 NVIDIA Corp.  All rights reserved.
5  *  Copyright 2004 Andrew Chew
6  *
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
10  *  by reference.
11  *
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.
22  *
23  *  0.03
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.
26  *
27  *  0.02
28  *     - Added support for CK804 SATA controller.
29  *
30  *  0.01
31  *     - Initial revision.
32  */
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>
42 #include "scsi.h"
43 #include <scsi/scsi_host.h>
44 #include <linux/libata.h>
45
46 #define DRV_NAME                        "sata_nv"
47 #define DRV_VERSION                     "0.5"
48
49 #define NV_PORTS                        2
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
55
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)
72
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)
89
90 #define NV_INT_CONFIG                   0x12
91 #define NV_INT_CONFIG_METHD             0x01 // 0 = INT, 1 = SMI
92
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
96
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);
108
109 enum nv_host_type
110 {
111         NFORCE2,
112         NFORCE3,
113         CK804
114 };
115
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 */
132 };
133
134 #define NV_HOST_FLAGS_SCR_MMIO  0x00000001
135
136 struct nv_host_desc
137 {
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);
143
144 };
145 static struct nv_host_desc nv_device_tbl[] = {
146         {
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,
152         },
153         {
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,
159         },
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,
165         },
166 };
167
168 struct nv_host
169 {
170         struct nv_host_desc     *host_desc;
171 };
172
173 static struct pci_driver nv_pci_driver = {
174         .name                   = DRV_NAME,
175         .id_table               = nv_pci_tbl,
176         .probe                  = nv_init_one,
177         .remove                 = ata_pci_remove_one,
178 };
179
180 static Scsi_Host_Template nv_sht = {
181         .module                 = THIS_MODULE,
182         .name                   = DRV_NAME,
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,
197 };
198
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,
219 };
220
221 /* FIXME: The hardware provides the necessary SATA PHY controls
222  * to support ATA_FLAG_SATA_RESET.  However, it is currently
223  * necessary to disable that flag, to solve misdetection problems.
224  * See http://bugme.osdl.org/show_bug.cgi?id=3352 for more info.
225  *
226  * This problem really needs to be investigated further.  But in the
227  * meantime, we avoid ATA_FLAG_SATA_RESET to get people working.
228  */
229 static struct ata_port_info nv_port_info = {
230         .sht            = &nv_sht,
231         .host_flags     = ATA_FLAG_SATA |
232                           /* ATA_FLAG_SATA_RESET | */
233                           ATA_FLAG_SRST |
234                           ATA_FLAG_NO_LEGACY,
235         .pio_mask       = NV_PIO_MASK,
236         .mwdma_mask     = NV_MWDMA_MASK,
237         .udma_mask      = NV_UDMA_MASK,
238         .port_ops       = &nv_ops,
239 };
240
241 MODULE_AUTHOR("NVIDIA");
242 MODULE_DESCRIPTION("low-level driver for NVIDIA nForce SATA controller");
243 MODULE_LICENSE("GPL");
244 MODULE_DEVICE_TABLE(pci, nv_pci_tbl);
245 MODULE_VERSION(DRV_VERSION);
246
247 irqreturn_t nv_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
248 {
249         struct ata_host_set *host_set = dev_instance;
250         struct nv_host *host = host_set->private_data;
251         unsigned int i;
252         unsigned int handled = 0;
253         unsigned long flags;
254
255         spin_lock_irqsave(&host_set->lock, flags);
256
257         for (i = 0; i < host_set->n_ports; i++) {
258                 struct ata_port *ap;
259
260                 ap = host_set->ports[i];
261                 if (ap && (!(ap->flags & ATA_FLAG_PORT_DISABLED))) {
262                         struct ata_queued_cmd *qc;
263
264                         qc = ata_qc_from_tag(ap, ap->active_tag);
265                         if (qc && (!(qc->tf.ctl & ATA_NIEN)))
266                                 handled += ata_host_intr(ap, qc);
267                 }
268
269         }
270
271         if (host->host_desc->check_hotplug)
272                 host->host_desc->check_hotplug(host_set);
273
274         spin_unlock_irqrestore(&host_set->lock, flags);
275
276         return IRQ_RETVAL(handled);
277 }
278
279 static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg)
280 {
281         struct ata_host_set *host_set = ap->host_set;
282         struct nv_host *host = host_set->private_data;
283
284         if (sc_reg > SCR_CONTROL)
285                 return 0xffffffffU;
286
287         if (host->host_desc->host_flags & NV_HOST_FLAGS_SCR_MMIO)
288                 return readl(ap->ioaddr.scr_addr + (sc_reg * 4));
289         else
290                 return inl(ap->ioaddr.scr_addr + (sc_reg * 4));
291 }
292
293 static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
294 {
295         struct ata_host_set *host_set = ap->host_set;
296         struct nv_host *host = host_set->private_data;
297
298         if (sc_reg > SCR_CONTROL)
299                 return;
300
301         if (host->host_desc->host_flags & NV_HOST_FLAGS_SCR_MMIO)
302                 writel(val, ap->ioaddr.scr_addr + (sc_reg * 4));
303         else
304                 outl(val, ap->ioaddr.scr_addr + (sc_reg * 4));
305 }
306
307 static void nv_host_stop (struct ata_host_set *host_set)
308 {
309         struct nv_host *host = host_set->private_data;
310
311         // Disable hotplug event interrupts.
312         if (host->host_desc->disable_hotplug)
313                 host->host_desc->disable_hotplug(host_set);
314
315         kfree(host);
316 }
317
318 static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
319 {
320         static int printed_version = 0;
321         struct nv_host *host;
322         struct ata_port_info *ppi;
323         struct ata_probe_ent *probe_ent;
324         int rc;
325
326         if (!printed_version++)
327                 printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
328
329         rc = pci_enable_device(pdev);
330         if (rc)
331                 goto err_out;
332
333         rc = pci_request_regions(pdev, DRV_NAME);
334         if (rc)
335                 goto err_out_disable;
336
337         rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
338         if (rc)
339                 goto err_out_regions;
340         rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
341         if (rc)
342                 goto err_out_regions;
343
344         rc = -ENOMEM;
345
346         ppi = &nv_port_info;
347         probe_ent = ata_pci_init_native_mode(pdev, &ppi);
348         if (!probe_ent)
349                 goto err_out_regions;
350
351         host = kmalloc(sizeof(struct nv_host), GFP_KERNEL);
352         if (!host)
353                 goto err_out_free_ent;
354
355         host->host_desc = &nv_device_tbl[ent->driver_data];
356
357         probe_ent->private_data = host;
358
359         if (host->host_desc->host_flags & NV_HOST_FLAGS_SCR_MMIO) {
360                 unsigned long base;
361
362                 probe_ent->mmio_base = ioremap(pci_resource_start(pdev, 5),
363                                 pci_resource_len(pdev, 5));
364                 if (probe_ent->mmio_base == NULL) {
365                         rc = -EIO;
366                         goto err_out_free_host;
367                 }
368
369                 base = (unsigned long)probe_ent->mmio_base;
370
371                 probe_ent->port[0].scr_addr =
372                         base + NV_PORT0_SCR_REG_OFFSET;
373                 probe_ent->port[1].scr_addr =
374                         base + NV_PORT1_SCR_REG_OFFSET;
375         } else {
376
377                 probe_ent->port[0].scr_addr =
378                         pci_resource_start(pdev, 5) | NV_PORT0_SCR_REG_OFFSET;
379                 probe_ent->port[1].scr_addr =
380                         pci_resource_start(pdev, 5) | NV_PORT1_SCR_REG_OFFSET;
381         }
382
383         pci_set_master(pdev);
384
385         rc = ata_device_add(probe_ent);
386         if (rc != NV_PORTS)
387                 goto err_out_iounmap;
388
389         // Enable hotplug event interrupts.
390         if (host->host_desc->enable_hotplug)
391                 host->host_desc->enable_hotplug(probe_ent);
392
393         kfree(probe_ent);
394
395         return 0;
396
397 err_out_iounmap:
398         if (host->host_desc->host_flags & NV_HOST_FLAGS_SCR_MMIO)
399                 iounmap(probe_ent->mmio_base);
400 err_out_free_host:
401         kfree(host);
402 err_out_free_ent:
403         kfree(probe_ent);
404 err_out_regions:
405         pci_release_regions(pdev);
406 err_out_disable:
407         pci_disable_device(pdev);
408 err_out:
409         return rc;
410 }
411
412 static void nv_enable_hotplug(struct ata_probe_ent *probe_ent)
413 {
414         u8 intr_mask;
415
416         outb(NV_INT_STATUS_HOTPLUG,
417                 probe_ent->port[0].scr_addr + NV_INT_STATUS);
418
419         intr_mask = inb(probe_ent->port[0].scr_addr + NV_INT_ENABLE);
420         intr_mask |= NV_INT_ENABLE_HOTPLUG;
421
422         outb(intr_mask, probe_ent->port[0].scr_addr + NV_INT_ENABLE);
423 }
424
425 static void nv_disable_hotplug(struct ata_host_set *host_set)
426 {
427         u8 intr_mask;
428
429         intr_mask = inb(host_set->ports[0]->ioaddr.scr_addr + NV_INT_ENABLE);
430
431         intr_mask &= ~(NV_INT_ENABLE_HOTPLUG);
432
433         outb(intr_mask, host_set->ports[0]->ioaddr.scr_addr + NV_INT_ENABLE);
434 }
435
436 static void nv_check_hotplug(struct ata_host_set *host_set)
437 {
438         u8 intr_status;
439
440         intr_status = inb(host_set->ports[0]->ioaddr.scr_addr + NV_INT_STATUS);
441
442         // Clear interrupt status.
443         outb(0xff, host_set->ports[0]->ioaddr.scr_addr + NV_INT_STATUS);
444
445         if (intr_status & NV_INT_STATUS_HOTPLUG) {
446                 if (intr_status & NV_INT_STATUS_PDEV_ADDED)
447                         printk(KERN_WARNING "nv_sata: "
448                                 "Primary device added\n");
449
450                 if (intr_status & NV_INT_STATUS_PDEV_REMOVED)
451                         printk(KERN_WARNING "nv_sata: "
452                                 "Primary device removed\n");
453
454                 if (intr_status & NV_INT_STATUS_SDEV_ADDED)
455                         printk(KERN_WARNING "nv_sata: "
456                                 "Secondary device added\n");
457
458                 if (intr_status & NV_INT_STATUS_SDEV_REMOVED)
459                         printk(KERN_WARNING "nv_sata: "
460                                 "Secondary device removed\n");
461         }
462 }
463
464 static void nv_enable_hotplug_ck804(struct ata_probe_ent *probe_ent)
465 {
466         struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
467         u8 intr_mask;
468         u8 regval;
469
470         pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
471         regval |= NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
472         pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
473
474         writeb(NV_INT_STATUS_HOTPLUG, probe_ent->mmio_base + NV_INT_STATUS_CK804);
475
476         intr_mask = readb(probe_ent->mmio_base + NV_INT_ENABLE_CK804);
477         intr_mask |= NV_INT_ENABLE_HOTPLUG;
478
479         writeb(intr_mask, probe_ent->mmio_base + NV_INT_ENABLE_CK804);
480 }
481
482 static void nv_disable_hotplug_ck804(struct ata_host_set *host_set)
483 {
484         struct pci_dev *pdev = to_pci_dev(host_set->dev);
485         u8 intr_mask;
486         u8 regval;
487
488         intr_mask = readb(host_set->mmio_base + NV_INT_ENABLE_CK804);
489
490         intr_mask &= ~(NV_INT_ENABLE_HOTPLUG);
491
492         writeb(intr_mask, host_set->mmio_base + NV_INT_ENABLE_CK804);
493
494         pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
495         regval &= ~NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
496         pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
497 }
498
499 static void nv_check_hotplug_ck804(struct ata_host_set *host_set)
500 {
501         u8 intr_status;
502
503         intr_status = readb(host_set->mmio_base + NV_INT_STATUS_CK804);
504
505         // Clear interrupt status.
506         writeb(0xff, host_set->mmio_base + NV_INT_STATUS_CK804);
507
508         if (intr_status & NV_INT_STATUS_HOTPLUG) {
509                 if (intr_status & NV_INT_STATUS_PDEV_ADDED)
510                         printk(KERN_WARNING "nv_sata: "
511                                 "Primary device added\n");
512
513                 if (intr_status & NV_INT_STATUS_PDEV_REMOVED)
514                         printk(KERN_WARNING "nv_sata: "
515                                 "Primary device removed\n");
516
517                 if (intr_status & NV_INT_STATUS_SDEV_ADDED)
518                         printk(KERN_WARNING "nv_sata: "
519                                 "Secondary device added\n");
520
521                 if (intr_status & NV_INT_STATUS_SDEV_REMOVED)
522                         printk(KERN_WARNING "nv_sata: "
523                                 "Secondary device removed\n");
524         }
525 }
526
527 static int __init nv_init(void)
528 {
529         return pci_module_init(&nv_pci_driver);
530 }
531
532 static void __exit nv_exit(void)
533 {
534         pci_unregister_driver(&nv_pci_driver);
535 }
536
537 module_init(nv_init);
538 module_exit(nv_exit);