blob: 00bbbbd50e97e8abe6aac0dd36fedfe8a91257d9 [file] [log] [blame]
Jeff Garzik669a5db2006-08-29 18:12:40 -04001/*
2 * pata_jmicron.c - JMicron ATA driver for non AHCI mode. This drives the
3 * PATA port of the controller. The SATA ports are
4 * driven by AHCI in the usual configuration although
5 * this driver can handle other setups if we need it.
6 *
7 * (c) 2006 Red Hat <alan@redhat.com>
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/init.h>
14#include <linux/blkdev.h>
15#include <linux/delay.h>
16#include <linux/device.h>
17#include <scsi/scsi_host.h>
18#include <linux/libata.h>
19#include <linux/ata.h>
20
21#define DRV_NAME "pata_jmicron"
Jeff Garzik8bc3fc42007-05-21 20:26:38 -040022#define DRV_VERSION "0.1.5"
Jeff Garzik669a5db2006-08-29 18:12:40 -040023
24typedef enum {
25 PORT_PATA0 = 0,
26 PORT_PATA1 = 1,
27 PORT_SATA = 2,
28} port_type;
29
30/**
31 * jmicron_pre_reset - check for 40/80 pin
Tejun Heocc0680a2007-08-06 18:36:23 +090032 * @link: ATA link
Tejun Heod4b2bab2007-02-02 16:50:52 +090033 * @deadline: deadline jiffies for the operation
Jeff Garzik669a5db2006-08-29 18:12:40 -040034 *
35 * Perform the PATA port setup we need.
Tejun Heod4b2bab2007-02-02 16:50:52 +090036 *
Jeff Garzik669a5db2006-08-29 18:12:40 -040037 * On the Jmicron 361/363 there is a single PATA port that can be mapped
38 * either as primary or secondary (or neither). We don't do any policy
39 * and setup here. We assume that has been done by init_one and the
40 * BIOS.
41 */
Tejun Heocc0680a2007-08-06 18:36:23 +090042static int jmicron_pre_reset(struct ata_link *link, unsigned long deadline)
Jeff Garzik669a5db2006-08-29 18:12:40 -040043{
Tejun Heocc0680a2007-08-06 18:36:23 +090044 struct ata_port *ap = link->ap;
Jeff Garzik669a5db2006-08-29 18:12:40 -040045 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
46 u32 control;
47 u32 control5;
48 int port_mask = 1<< (4 * ap->port_no);
49 int port = ap->port_no;
50 port_type port_map[2];
51
52 /* Check if our port is enabled */
53 pci_read_config_dword(pdev, 0x40, &control);
54 if ((control & port_mask) == 0)
Alan Coxc9619222006-09-26 17:53:38 +010055 return -ENOENT;
Jeff Garzik669a5db2006-08-29 18:12:40 -040056
57 /* There are two basic mappings. One has the two SATA ports merged
58 as master/slave and the secondary as PATA, the other has only the
59 SATA port mapped */
60 if (control & (1 << 23)) {
61 port_map[0] = PORT_SATA;
62 port_map[1] = PORT_PATA0;
63 } else {
64 port_map[0] = PORT_SATA;
65 port_map[1] = PORT_SATA;
66 }
67
68 /* The 365/366 may have this bit set to map the second PATA port
69 as the internal primary channel */
70 pci_read_config_dword(pdev, 0x80, &control5);
71 if (control5 & (1<<24))
72 port_map[0] = PORT_PATA1;
73
74 /* The two ports may then be logically swapped by the firmware */
75 if (control & (1 << 22))
76 port = port ^ 1;
77
78 /*
79 * Now we know which physical port we are talking about we can
80 * actually do our cable checking etc. Thankfully we don't need
81 * to do the plumbing for other cases.
82 */
Tejun Heo0706efd2007-11-19 18:06:11 +090083 switch (port_map[port]) {
Jeff Garzik669a5db2006-08-29 18:12:40 -040084 case PORT_PATA0:
Tejun Heo0706efd2007-11-19 18:06:11 +090085 if ((control & (1 << 5)) == 0)
86 return -ENOENT;
Jeff Garzik669a5db2006-08-29 18:12:40 -040087 if (control & (1 << 3)) /* 40/80 pin primary */
88 ap->cbl = ATA_CBL_PATA40;
89 else
90 ap->cbl = ATA_CBL_PATA80;
91 break;
92 case PORT_PATA1:
93 /* Bit 21 is set if the port is enabled */
94 if ((control5 & (1 << 21)) == 0)
Tejun Heo0706efd2007-11-19 18:06:11 +090095 return -ENOENT;
Jeff Garzik669a5db2006-08-29 18:12:40 -040096 if (control5 & (1 << 19)) /* 40/80 pin secondary */
97 ap->cbl = ATA_CBL_PATA40;
98 else
99 ap->cbl = ATA_CBL_PATA80;
100 break;
101 case PORT_SATA:
102 ap->cbl = ATA_CBL_SATA;
103 break;
104 }
Tejun Heocc0680a2007-08-06 18:36:23 +0900105 return ata_std_prereset(link, deadline);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400106}
107
108/**
109 * jmicron_error_handler - Setup and error handler
110 * @ap: Port to handle
111 *
112 * LOCKING:
113 * None (inherited from caller).
114 */
115
116static void jmicron_error_handler(struct ata_port *ap)
117{
Harvey Harrisonef2f2e42008-02-13 21:14:26 -0800118 ata_bmdma_drive_eh(ap, jmicron_pre_reset, ata_std_softreset, NULL,
119 ata_std_postreset);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400120}
121
122/* No PIO or DMA methods needed for this device */
123
124static struct scsi_host_template jmicron_sht = {
125 .module = THIS_MODULE,
126 .name = DRV_NAME,
127 .ioctl = ata_scsi_ioctl,
128 .queuecommand = ata_scsi_queuecmd,
129 .can_queue = ATA_DEF_QUEUE,
130 .this_id = ATA_SHT_THIS_ID,
131 .sg_tablesize = LIBATA_MAX_PRD,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400132 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
133 .emulated = ATA_SHT_EMULATED,
134 .use_clustering = ATA_SHT_USE_CLUSTERING,
135 .proc_name = DRV_NAME,
136 .dma_boundary = ATA_DMA_BOUNDARY,
137 .slave_configure = ata_scsi_slave_config,
Tejun Heoafdfe892006-11-29 11:26:47 +0900138 .slave_destroy = ata_scsi_slave_destroy,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400139 /* Use standard CHS mapping rules */
140 .bios_param = ata_std_bios_param,
141};
142
143static const struct ata_port_operations jmicron_ops = {
Jeff Garzik669a5db2006-08-29 18:12:40 -0400144 /* Task file is PCI ATA format, use helpers */
145 .tf_load = ata_tf_load,
146 .tf_read = ata_tf_read,
147 .check_status = ata_check_status,
148 .exec_command = ata_exec_command,
149 .dev_select = ata_std_dev_select,
150
151 .freeze = ata_bmdma_freeze,
152 .thaw = ata_bmdma_thaw,
153 .error_handler = jmicron_error_handler,
154 .post_internal_cmd = ata_bmdma_post_internal_cmd,
155
156 /* BMDMA handling is PCI ATA format, use helpers */
157 .bmdma_setup = ata_bmdma_setup,
158 .bmdma_start = ata_bmdma_start,
159 .bmdma_stop = ata_bmdma_stop,
160 .bmdma_status = ata_bmdma_status,
161 .qc_prep = ata_qc_prep,
162 .qc_issue = ata_qc_issue_prot,
Tejun Heo0d5ff562007-02-01 15:06:36 +0900163 .data_xfer = ata_data_xfer,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400164
Jeff Garzikbda30282006-09-27 05:41:13 -0400165 /* IRQ-related hooks */
Jeff Garzik669a5db2006-08-29 18:12:40 -0400166 .irq_handler = ata_interrupt,
167 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900168 .irq_on = ata_irq_on,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400169
170 /* Generic PATA PCI ATA helpers */
171 .port_start = ata_port_start,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400172};
173
174
175/**
176 * jmicron_init_one - Register Jmicron ATA PCI device with kernel services
177 * @pdev: PCI device to register
178 * @ent: Entry in jmicron_pci_tbl matching with @pdev
179 *
180 * Called from kernel PCI layer.
181 *
182 * LOCKING:
183 * Inherited from PCI layer (may sleep).
184 *
185 * RETURNS:
186 * Zero on success, or -ERRNO value.
187 */
188
189static int jmicron_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
190{
Tejun Heo1626aeb2007-05-04 12:43:58 +0200191 static const struct ata_port_info info = {
Jeff Garzik669a5db2006-08-29 18:12:40 -0400192 .sht = &jmicron_sht,
Jeff Garzik1d2808f2007-05-28 06:59:48 -0400193 .flags = ATA_FLAG_SLAVE_POSS,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400194
195 .pio_mask = 0x1f,
196 .mwdma_mask = 0x07,
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400197 .udma_mask = ATA_UDMA5,
Jeff Garzik669a5db2006-08-29 18:12:40 -0400198
199 .port_ops = &jmicron_ops,
200 };
Tejun Heo1626aeb2007-05-04 12:43:58 +0200201 const struct ata_port_info *ppi[] = { &info, NULL };
Jeff Garzik669a5db2006-08-29 18:12:40 -0400202
Tejun Heo1626aeb2007-05-04 12:43:58 +0200203 return ata_pci_init_one(pdev, ppi);
Jeff Garzik669a5db2006-08-29 18:12:40 -0400204}
205
206static const struct pci_device_id jmicron_pci_tbl[] = {
Tejun Heoa01f48b2007-10-07 07:04:50 +0900207 { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
208 PCI_CLASS_STORAGE_IDE << 8, 0xffff00, 0 },
Jeff Garzik669a5db2006-08-29 18:12:40 -0400209 { } /* terminate list */
210};
211
212static struct pci_driver jmicron_pci_driver = {
213 .name = DRV_NAME,
214 .id_table = jmicron_pci_tbl,
215 .probe = jmicron_init_one,
216 .remove = ata_pci_remove_one,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900217#ifdef CONFIG_PM
Aland39ca892006-11-22 17:18:30 +0000218 .suspend = ata_pci_device_suspend,
Tejun Heo960627b2007-02-26 20:09:02 +0900219 .resume = ata_pci_device_resume,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900220#endif
Jeff Garzik669a5db2006-08-29 18:12:40 -0400221};
222
223static int __init jmicron_init(void)
224{
225 return pci_register_driver(&jmicron_pci_driver);
226}
227
228static void __exit jmicron_exit(void)
229{
230 pci_unregister_driver(&jmicron_pci_driver);
231}
Jeff Garzik669a5db2006-08-29 18:12:40 -0400232
233module_init(jmicron_init);
234module_exit(jmicron_exit);
235
236MODULE_AUTHOR("Alan Cox");
237MODULE_DESCRIPTION("SCSI low-level driver for Jmicron PATA ports");
238MODULE_LICENSE("GPL");
239MODULE_DEVICE_TABLE(pci, jmicron_pci_tbl);
240MODULE_VERSION(DRV_VERSION);
241