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.
7 * (c) 2006 Red Hat <alan@redhat.com>
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>
21 #define DRV_NAME "pata_jmicron"
22 #define DRV_VERSION "0.1.2"
31 * jmicron_pre_reset - check for 40/80 pin
34 * Perform the PATA port setup we need.
36 * On the Jmicron 361/363 there is a single PATA port that can be mapped
37 * either as primary or secondary (or neither). We don't do any policy
38 * and setup here. We assume that has been done by init_one and the
42 static int jmicron_pre_reset(struct ata_port *ap)
44 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
47 int port_mask = 1<< (4 * ap->port_no);
48 int port = ap->port_no;
49 port_type port_map[2];
51 /* Check if our port is enabled */
52 pci_read_config_dword(pdev, 0x40, &control);
53 if ((control & port_mask) == 0)
56 /* There are two basic mappings. One has the two SATA ports merged
57 as master/slave and the secondary as PATA, the other has only the
59 if (control & (1 << 23)) {
60 port_map[0] = PORT_SATA;
61 port_map[1] = PORT_PATA0;
63 port_map[0] = PORT_SATA;
64 port_map[1] = PORT_SATA;
67 /* The 365/366 may have this bit set to map the second PATA port
68 as the internal primary channel */
69 pci_read_config_dword(pdev, 0x80, &control5);
70 if (control5 & (1<<24))
71 port_map[0] = PORT_PATA1;
73 /* The two ports may then be logically swapped by the firmware */
74 if (control & (1 << 22))
78 * Now we know which physical port we are talking about we can
79 * actually do our cable checking etc. Thankfully we don't need
80 * to do the plumbing for other cases.
82 switch (port_map[port])
85 if (control & (1 << 5))
87 if (control & (1 << 3)) /* 40/80 pin primary */
88 ap->cbl = ATA_CBL_PATA40;
90 ap->cbl = ATA_CBL_PATA80;
93 /* Bit 21 is set if the port is enabled */
94 if ((control5 & (1 << 21)) == 0)
96 if (control5 & (1 << 19)) /* 40/80 pin secondary */
97 ap->cbl = ATA_CBL_PATA40;
99 ap->cbl = ATA_CBL_PATA80;
102 ap->cbl = ATA_CBL_SATA;
105 return ata_std_prereset(ap);
109 * jmicron_error_handler - Setup and error handler
110 * @ap: Port to handle
113 * None (inherited from caller).
116 static void jmicron_error_handler(struct ata_port *ap)
118 return ata_bmdma_drive_eh(ap, jmicron_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
121 /* No PIO or DMA methods needed for this device */
123 static struct scsi_host_template jmicron_sht = {
124 .module = THIS_MODULE,
126 .ioctl = ata_scsi_ioctl,
127 .queuecommand = ata_scsi_queuecmd,
128 .can_queue = ATA_DEF_QUEUE,
129 .this_id = ATA_SHT_THIS_ID,
130 .sg_tablesize = LIBATA_MAX_PRD,
131 /* Special handling needed if you have sector or LBA48 limits */
132 .max_sectors = ATA_MAX_SECTORS,
133 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
134 .emulated = ATA_SHT_EMULATED,
135 .use_clustering = ATA_SHT_USE_CLUSTERING,
136 .proc_name = DRV_NAME,
137 .dma_boundary = ATA_DMA_BOUNDARY,
138 .slave_configure = ata_scsi_slave_config,
139 /* Use standard CHS mapping rules */
140 .bios_param = ata_std_bios_param,
143 static const struct ata_port_operations jmicron_ops = {
144 .port_disable = ata_port_disable,
146 /* Task file is PCI ATA format, use helpers */
147 .tf_load = ata_tf_load,
148 .tf_read = ata_tf_read,
149 .check_status = ata_check_status,
150 .exec_command = ata_exec_command,
151 .dev_select = ata_std_dev_select,
153 .freeze = ata_bmdma_freeze,
154 .thaw = ata_bmdma_thaw,
155 .error_handler = jmicron_error_handler,
156 .post_internal_cmd = ata_bmdma_post_internal_cmd,
158 /* BMDMA handling is PCI ATA format, use helpers */
159 .bmdma_setup = ata_bmdma_setup,
160 .bmdma_start = ata_bmdma_start,
161 .bmdma_stop = ata_bmdma_stop,
162 .bmdma_status = ata_bmdma_status,
163 .qc_prep = ata_qc_prep,
164 .qc_issue = ata_qc_issue_prot,
165 .data_xfer = ata_pio_data_xfer,
167 /* Timeout handling. Special recovery hooks here */
168 .eng_timeout = ata_eng_timeout,
169 .irq_handler = ata_interrupt,
170 .irq_clear = ata_bmdma_irq_clear,
172 /* Generic PATA PCI ATA helpers */
173 .port_start = ata_port_start,
174 .port_stop = ata_port_stop,
175 .host_stop = ata_host_stop,
180 * jmicron_init_one - Register Jmicron ATA PCI device with kernel services
181 * @pdev: PCI device to register
182 * @ent: Entry in jmicron_pci_tbl matching with @pdev
184 * Called from kernel PCI layer.
187 * Inherited from PCI layer (may sleep).
190 * Zero on success, or -ERRNO value.
193 static int jmicron_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
195 static struct ata_port_info info = {
197 .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
203 .port_ops = &jmicron_ops,
205 struct ata_port_info *port_info[2] = { &info, &info };
209 if (id->driver_data != 368) {
210 /* Put the controller into AHCI mode in case the AHCI driver
211 has not yet been loaded. This can be done with either
214 /* FIXME: We may want a way to override this in future */
215 pci_write_config_byte(pdev, 0x41, 0xa1);
218 /* PATA controller is fn 1, AHCI is fn 0 */
219 if (PCI_FUNC(pdev->devfn) != 1)
222 if ( id->driver_data == 365 || id->driver_data == 366) {
223 /* The 365/66 have two PATA channels, redirect the second */
224 pci_read_config_dword(pdev, 0x80, ®);
225 reg |= (1 << 24); /* IDE1 to PATA IDE secondary */
226 pci_write_config_dword(pdev, 0x80, reg);
229 return ata_pci_init_one(pdev, port_info, 2);
232 static const struct pci_device_id jmicron_pci_tbl[] = {
233 { PCI_DEVICE(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB361), 361},
234 { PCI_DEVICE(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB363), 363},
235 { PCI_DEVICE(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365), 365},
236 { PCI_DEVICE(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366), 366},
237 { PCI_DEVICE(PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368), 368},
238 { } /* terminate list */
241 static struct pci_driver jmicron_pci_driver = {
243 .id_table = jmicron_pci_tbl,
244 .probe = jmicron_init_one,
245 .remove = ata_pci_remove_one,
248 static int __init jmicron_init(void)
250 return pci_register_driver(&jmicron_pci_driver);
253 static void __exit jmicron_exit(void)
255 pci_unregister_driver(&jmicron_pci_driver);
258 module_init(jmicron_init);
259 module_exit(jmicron_exit);
261 MODULE_AUTHOR("Alan Cox");
262 MODULE_DESCRIPTION("SCSI low-level driver for Jmicron PATA ports");
263 MODULE_LICENSE("GPL");
264 MODULE_DEVICE_TABLE(pci, jmicron_pci_tbl);
265 MODULE_VERSION(DRV_VERSION);