1/*
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"
22#define DRV_VERSION	"0.1.5"
23
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
32 *	@ap: Port
33 *	@deadline: deadline jiffies for the operation
34 *
35 *	Perform the PATA port setup we need.
36 *
37 *	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 */
42
43static int jmicron_pre_reset(struct ata_port *ap, unsigned long deadline)
44{
45	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)
55		return -ENOENT;
56
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	 */
83	switch (port_map[port])
84	{
85	case PORT_PATA0:
86		if (control & (1 << 5))
87			return 0;
88		if (control & (1 << 3))	/* 40/80 pin primary */
89			ap->cbl = ATA_CBL_PATA40;
90		else
91			ap->cbl = ATA_CBL_PATA80;
92		break;
93	case PORT_PATA1:
94		/* Bit 21 is set if the port is enabled */
95		if ((control5 & (1 << 21)) == 0)
96			return 0;
97		if (control5 & (1 << 19))	/* 40/80 pin secondary */
98			ap->cbl = ATA_CBL_PATA40;
99		else
100			ap->cbl = ATA_CBL_PATA80;
101		break;
102	case PORT_SATA:
103		ap->cbl = ATA_CBL_SATA;
104		break;
105	}
106	return ata_std_prereset(ap, deadline);
107}
108
109/**
110 *	jmicron_error_handler - Setup and error handler
111 *	@ap: Port to handle
112 *
113 *	LOCKING:
114 *	None (inherited from caller).
115 */
116
117static void jmicron_error_handler(struct ata_port *ap)
118{
119	return ata_bmdma_drive_eh(ap, jmicron_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
120}
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,
132	.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,
138	.slave_destroy		= ata_scsi_slave_destroy,
139	/* Use standard CHS mapping rules */
140	.bios_param		= ata_std_bios_param,
141};
142
143static const struct ata_port_operations jmicron_ops = {
144	.port_disable		= ata_port_disable,
145
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,
152
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,
157
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_data_xfer,
166
167	/* IRQ-related hooks */
168	.irq_handler		= ata_interrupt,
169	.irq_clear		= ata_bmdma_irq_clear,
170	.irq_on			= ata_irq_on,
171	.irq_ack		= ata_irq_ack,
172
173	/* Generic PATA PCI ATA helpers */
174	.port_start		= ata_port_start,
175};
176
177
178/**
179 *	jmicron_init_one - Register Jmicron ATA PCI device with kernel services
180 *	@pdev: PCI device to register
181 *	@ent: Entry in jmicron_pci_tbl matching with @pdev
182 *
183 *	Called from kernel PCI layer.
184 *
185 *	LOCKING:
186 *	Inherited from PCI layer (may sleep).
187 *
188 *	RETURNS:
189 *	Zero on success, or -ERRNO value.
190 */
191
192static int jmicron_init_one (struct pci_dev *pdev, const struct pci_device_id *id)
193{
194	static const struct ata_port_info info = {
195		.sht		= &jmicron_sht,
196		.flags	= ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
197
198		.pio_mask	= 0x1f,
199		.mwdma_mask	= 0x07,
200		.udma_mask 	= 0x3f,
201
202		.port_ops	= &jmicron_ops,
203	};
204	const struct ata_port_info *ppi[] = { &info, NULL };
205
206	return ata_pci_init_one(pdev, ppi);
207}
208
209static const struct pci_device_id jmicron_pci_tbl[] = {
210	{ PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB361,
211	  PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xffff00, 361 },
212	{ PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB363,
213	  PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xffff00, 363 },
214	{ PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB365,
215	  PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xffff00, 365 },
216	{ PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB366,
217	  PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xffff00, 366 },
218	{ PCI_VENDOR_ID_JMICRON, PCI_DEVICE_ID_JMICRON_JMB368,
219	  PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE << 8, 0xffff00, 368 },
220
221	{ }	/* terminate list */
222};
223
224static struct pci_driver jmicron_pci_driver = {
225	.name			= DRV_NAME,
226	.id_table		= jmicron_pci_tbl,
227	.probe			= jmicron_init_one,
228	.remove			= ata_pci_remove_one,
229#ifdef CONFIG_PM
230	.suspend		= ata_pci_device_suspend,
231	.resume			= ata_pci_device_resume,
232#endif
233};
234
235static int __init jmicron_init(void)
236{
237	return pci_register_driver(&jmicron_pci_driver);
238}
239
240static void __exit jmicron_exit(void)
241{
242	pci_unregister_driver(&jmicron_pci_driver);
243}
244
245module_init(jmicron_init);
246module_exit(jmicron_exit);
247
248MODULE_AUTHOR("Alan Cox");
249MODULE_DESCRIPTION("SCSI low-level driver for Jmicron PATA ports");
250MODULE_LICENSE("GPL");
251MODULE_DEVICE_TABLE(pci, jmicron_pci_tbl);
252MODULE_VERSION(DRV_VERSION);
253