xref: /openbmc/linux/drivers/ata/pata_triflex.c (revision 2d2744fc8be620a2dc469cf48349e3e704119f1b)
1669a5db4SJeff Garzik /*
2669a5db4SJeff Garzik  * pata_triflex.c 	- Compaq PATA for new ATA layer
3669a5db4SJeff Garzik  *			  (C) 2005 Red Hat Inc
4669a5db4SJeff Garzik  *			  Alan Cox <alan@redhat.com>
5669a5db4SJeff Garzik  *
6669a5db4SJeff Garzik  * based upon
7669a5db4SJeff Garzik  *
8669a5db4SJeff Garzik  * triflex.c
9669a5db4SJeff Garzik  *
10669a5db4SJeff Garzik  * IDE Chipset driver for the Compaq TriFlex IDE controller.
11669a5db4SJeff Garzik  *
12669a5db4SJeff Garzik  * Known to work with the Compaq Workstation 5x00 series.
13669a5db4SJeff Garzik  *
14669a5db4SJeff Garzik  * Copyright (C) 2002 Hewlett-Packard Development Group, L.P.
15669a5db4SJeff Garzik  * Author: Torben Mathiasen <torben.mathiasen@hp.com>
16669a5db4SJeff Garzik  *
17669a5db4SJeff Garzik  * This program is free software; you can redistribute it and/or modify
18669a5db4SJeff Garzik  * it under the terms of the GNU General Public License version 2 as
19669a5db4SJeff Garzik  * published by the Free Software Foundation.
20669a5db4SJeff Garzik  *
21669a5db4SJeff Garzik  * This program is distributed in the hope that it will be useful,
22669a5db4SJeff Garzik  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23669a5db4SJeff Garzik  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24669a5db4SJeff Garzik  * GNU General Public License for more details.
25669a5db4SJeff Garzik  *
26669a5db4SJeff Garzik  * You should have received a copy of the GNU General Public License
27669a5db4SJeff Garzik  * along with this program; if not, write to the Free Software
28669a5db4SJeff Garzik  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29669a5db4SJeff Garzik  *
30669a5db4SJeff Garzik  * Loosely based on the piix & svwks drivers.
31669a5db4SJeff Garzik  *
32669a5db4SJeff Garzik  * Documentation:
33669a5db4SJeff Garzik  *	Not publically available.
34669a5db4SJeff Garzik  */
35669a5db4SJeff Garzik 
36669a5db4SJeff Garzik #include <linux/kernel.h>
37669a5db4SJeff Garzik #include <linux/module.h>
38669a5db4SJeff Garzik #include <linux/pci.h>
39669a5db4SJeff Garzik #include <linux/init.h>
40669a5db4SJeff Garzik #include <linux/blkdev.h>
41669a5db4SJeff Garzik #include <linux/delay.h>
42669a5db4SJeff Garzik #include <scsi/scsi_host.h>
43669a5db4SJeff Garzik #include <linux/libata.h>
44669a5db4SJeff Garzik 
45669a5db4SJeff Garzik #define DRV_NAME "pata_triflex"
46669a5db4SJeff Garzik #define DRV_VERSION "0.2.5"
47669a5db4SJeff Garzik 
48669a5db4SJeff Garzik /**
49c961922bSAlan Cox  *	triflex_prereset		-	probe begin
50669a5db4SJeff Garzik  *	@ap: ATA port
51669a5db4SJeff Garzik  *
52669a5db4SJeff Garzik  *	Set up cable type and use generic probe init
53669a5db4SJeff Garzik  */
54669a5db4SJeff Garzik 
55c961922bSAlan Cox static int triflex_prereset(struct ata_port *ap)
56669a5db4SJeff Garzik {
57669a5db4SJeff Garzik 	static const struct pci_bits triflex_enable_bits[] = {
58669a5db4SJeff Garzik 		{ 0x80, 1, 0x01, 0x01 },
59669a5db4SJeff Garzik 		{ 0x80, 1, 0x02, 0x02 }
60669a5db4SJeff Garzik 	};
61669a5db4SJeff Garzik 
62669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
63669a5db4SJeff Garzik 
64c961922bSAlan Cox 	if (!pci_test_config_bits(pdev, &triflex_enable_bits[ap->port_no]))
65c961922bSAlan Cox 		return -ENOENT;
66669a5db4SJeff Garzik 	ap->cbl = ATA_CBL_PATA40;
67669a5db4SJeff Garzik 	return ata_std_prereset(ap);
68669a5db4SJeff Garzik }
69669a5db4SJeff Garzik 
70669a5db4SJeff Garzik 
71669a5db4SJeff Garzik 
72669a5db4SJeff Garzik static void triflex_error_handler(struct ata_port *ap)
73669a5db4SJeff Garzik {
74c961922bSAlan Cox 	ata_bmdma_drive_eh(ap, triflex_prereset, ata_std_softreset, NULL, ata_std_postreset);
75669a5db4SJeff Garzik }
76669a5db4SJeff Garzik 
77669a5db4SJeff Garzik /**
78669a5db4SJeff Garzik  *	triflex_load_timing		-	timing configuration
79669a5db4SJeff Garzik  *	@ap: ATA interface
80669a5db4SJeff Garzik  *	@adev: Device on the bus
81669a5db4SJeff Garzik  *	@speed: speed to configure
82669a5db4SJeff Garzik  *
83669a5db4SJeff Garzik  *	The Triflex has one set of timings per device per channel. This
84669a5db4SJeff Garzik  *	means we must do some switching. As the PIO and DMA timings don't
85669a5db4SJeff Garzik  *	match we have to do some reloading unlike PIIX devices where tuning
86669a5db4SJeff Garzik  *	tricks can avoid it.
87669a5db4SJeff Garzik  */
88669a5db4SJeff Garzik 
89669a5db4SJeff Garzik static void triflex_load_timing(struct ata_port *ap, struct ata_device *adev, int speed)
90669a5db4SJeff Garzik {
91669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
92669a5db4SJeff Garzik 	u32 timing = 0;
93669a5db4SJeff Garzik 	u32 triflex_timing, old_triflex_timing;
94669a5db4SJeff Garzik 	int channel_offset = ap->port_no ? 0x74: 0x70;
95669a5db4SJeff Garzik 	unsigned int is_slave	= (adev->devno != 0);
96669a5db4SJeff Garzik 
97669a5db4SJeff Garzik 
98669a5db4SJeff Garzik 	pci_read_config_dword(pdev, channel_offset, &old_triflex_timing);
99669a5db4SJeff Garzik 	triflex_timing = old_triflex_timing;
100669a5db4SJeff Garzik 
101669a5db4SJeff Garzik 	switch(speed)
102669a5db4SJeff Garzik 	{
103669a5db4SJeff Garzik 		case XFER_MW_DMA_2:
104669a5db4SJeff Garzik 			timing = 0x0103;break;
105669a5db4SJeff Garzik 		case XFER_MW_DMA_1:
106669a5db4SJeff Garzik 			timing = 0x0203;break;
107669a5db4SJeff Garzik 		case XFER_MW_DMA_0:
108669a5db4SJeff Garzik 			timing = 0x0808;break;
109669a5db4SJeff Garzik 		case XFER_SW_DMA_2:
110669a5db4SJeff Garzik 		case XFER_SW_DMA_1:
111669a5db4SJeff Garzik 		case XFER_SW_DMA_0:
112669a5db4SJeff Garzik 			timing = 0x0F0F;break;
113669a5db4SJeff Garzik 		case XFER_PIO_4:
114669a5db4SJeff Garzik 			timing = 0x0202;break;
115669a5db4SJeff Garzik 		case XFER_PIO_3:
116669a5db4SJeff Garzik 			timing = 0x0204;break;
117669a5db4SJeff Garzik 		case XFER_PIO_2:
118669a5db4SJeff Garzik 			timing = 0x0404;break;
119669a5db4SJeff Garzik 		case XFER_PIO_1:
120669a5db4SJeff Garzik 			timing = 0x0508;break;
121669a5db4SJeff Garzik 		case XFER_PIO_0:
122669a5db4SJeff Garzik 			timing = 0x0808;break;
123669a5db4SJeff Garzik 		default:
124669a5db4SJeff Garzik 			BUG();
125669a5db4SJeff Garzik 	}
126669a5db4SJeff Garzik 	triflex_timing &= ~ (0xFFFF << (16 * is_slave));
127669a5db4SJeff Garzik 	triflex_timing |= (timing << (16 * is_slave));
128669a5db4SJeff Garzik 
129669a5db4SJeff Garzik 	if (triflex_timing != old_triflex_timing)
130669a5db4SJeff Garzik 		pci_write_config_dword(pdev, channel_offset, triflex_timing);
131669a5db4SJeff Garzik }
132669a5db4SJeff Garzik 
133669a5db4SJeff Garzik /**
134669a5db4SJeff Garzik  *	triflex_set_piomode	-	set initial PIO mode data
135669a5db4SJeff Garzik  *	@ap: ATA interface
136669a5db4SJeff Garzik  *	@adev: ATA device
137669a5db4SJeff Garzik  *
138669a5db4SJeff Garzik  *	Use the timing loader to set up the PIO mode. We have to do this
139669a5db4SJeff Garzik  *	because DMA start/stop will only be called once DMA occurs. If there
140669a5db4SJeff Garzik  *	has been no DMA then the PIO timings are still needed.
141669a5db4SJeff Garzik  */
142669a5db4SJeff Garzik static void triflex_set_piomode(struct ata_port *ap, struct ata_device *adev)
143669a5db4SJeff Garzik {
144669a5db4SJeff Garzik 	triflex_load_timing(ap, adev, adev->pio_mode);
145669a5db4SJeff Garzik }
146669a5db4SJeff Garzik 
147669a5db4SJeff Garzik /**
148669a5db4SJeff Garzik  *	triflex_dma_start	-	DMA start callback
149669a5db4SJeff Garzik  *	@qc: Command in progress
150669a5db4SJeff Garzik  *
151669a5db4SJeff Garzik  *	Usually drivers set the DMA timing at the point the set_dmamode call
152669a5db4SJeff Garzik  *	is made. Triflex however requires we load new timings on the
153669a5db4SJeff Garzik  *	transition or keep matching PIO/DMA pairs (ie MWDMA2/PIO4 etc).
154669a5db4SJeff Garzik  *	We load the DMA timings just before starting DMA and then restore
155669a5db4SJeff Garzik  *	the PIO timing when the DMA is finished.
156669a5db4SJeff Garzik  */
157669a5db4SJeff Garzik 
158669a5db4SJeff Garzik static void triflex_bmdma_start(struct ata_queued_cmd *qc)
159669a5db4SJeff Garzik {
160669a5db4SJeff Garzik 	triflex_load_timing(qc->ap, qc->dev, qc->dev->dma_mode);
161669a5db4SJeff Garzik 	ata_bmdma_start(qc);
162669a5db4SJeff Garzik }
163669a5db4SJeff Garzik 
164669a5db4SJeff Garzik /**
165669a5db4SJeff Garzik  *	triflex_dma_stop	-	DMA stop callback
166669a5db4SJeff Garzik  *	@ap: ATA interface
167669a5db4SJeff Garzik  *	@adev: ATA device
168669a5db4SJeff Garzik  *
169669a5db4SJeff Garzik  *	We loaded new timings in dma_start, as a result we need to restore
170669a5db4SJeff Garzik  *	the PIO timings in dma_stop so that the next command issue gets the
171669a5db4SJeff Garzik  *	right clock values.
172669a5db4SJeff Garzik  */
173669a5db4SJeff Garzik 
174669a5db4SJeff Garzik static void triflex_bmdma_stop(struct ata_queued_cmd *qc)
175669a5db4SJeff Garzik {
176669a5db4SJeff Garzik 	ata_bmdma_stop(qc);
177669a5db4SJeff Garzik 	triflex_load_timing(qc->ap, qc->dev, qc->dev->pio_mode);
178669a5db4SJeff Garzik }
179669a5db4SJeff Garzik 
180669a5db4SJeff Garzik static struct scsi_host_template triflex_sht = {
181669a5db4SJeff Garzik 	.module			= THIS_MODULE,
182669a5db4SJeff Garzik 	.name			= DRV_NAME,
183669a5db4SJeff Garzik 	.ioctl			= ata_scsi_ioctl,
184669a5db4SJeff Garzik 	.queuecommand		= ata_scsi_queuecmd,
185669a5db4SJeff Garzik 	.can_queue		= ATA_DEF_QUEUE,
186669a5db4SJeff Garzik 	.this_id		= ATA_SHT_THIS_ID,
187669a5db4SJeff Garzik 	.sg_tablesize		= LIBATA_MAX_PRD,
188669a5db4SJeff Garzik 	.max_sectors		= ATA_MAX_SECTORS,
189669a5db4SJeff Garzik 	.cmd_per_lun		= ATA_SHT_CMD_PER_LUN,
190669a5db4SJeff Garzik 	.emulated		= ATA_SHT_EMULATED,
191669a5db4SJeff Garzik 	.use_clustering		= ATA_SHT_USE_CLUSTERING,
192669a5db4SJeff Garzik 	.proc_name		= DRV_NAME,
193669a5db4SJeff Garzik 	.dma_boundary		= ATA_DMA_BOUNDARY,
194669a5db4SJeff Garzik 	.slave_configure	= ata_scsi_slave_config,
195669a5db4SJeff Garzik 	.bios_param		= ata_std_bios_param,
196669a5db4SJeff Garzik };
197669a5db4SJeff Garzik 
198669a5db4SJeff Garzik static struct ata_port_operations triflex_port_ops = {
199669a5db4SJeff Garzik 	.port_disable	= ata_port_disable,
200669a5db4SJeff Garzik 	.set_piomode	= triflex_set_piomode,
201669a5db4SJeff Garzik 	.mode_filter	= ata_pci_default_filter,
202669a5db4SJeff Garzik 
203669a5db4SJeff Garzik 	.tf_load	= ata_tf_load,
204669a5db4SJeff Garzik 	.tf_read	= ata_tf_read,
205669a5db4SJeff Garzik 	.check_status 	= ata_check_status,
206669a5db4SJeff Garzik 	.exec_command	= ata_exec_command,
207669a5db4SJeff Garzik 	.dev_select 	= ata_std_dev_select,
208669a5db4SJeff Garzik 
209669a5db4SJeff Garzik 	.freeze		= ata_bmdma_freeze,
210669a5db4SJeff Garzik 	.thaw		= ata_bmdma_thaw,
211669a5db4SJeff Garzik 	.error_handler	= triflex_error_handler,
212669a5db4SJeff Garzik 	.post_internal_cmd = ata_bmdma_post_internal_cmd,
213669a5db4SJeff Garzik 
214669a5db4SJeff Garzik 	.bmdma_setup 	= ata_bmdma_setup,
215669a5db4SJeff Garzik 	.bmdma_start 	= triflex_bmdma_start,
216669a5db4SJeff Garzik 	.bmdma_stop	= triflex_bmdma_stop,
217669a5db4SJeff Garzik 	.bmdma_status 	= ata_bmdma_status,
218669a5db4SJeff Garzik 
219669a5db4SJeff Garzik 	.qc_prep 	= ata_qc_prep,
220669a5db4SJeff Garzik 	.qc_issue	= ata_qc_issue_prot,
221bda30288SJeff Garzik 
222669a5db4SJeff Garzik 	.data_xfer	= ata_pio_data_xfer,
223669a5db4SJeff Garzik 
224669a5db4SJeff Garzik 	.irq_handler	= ata_interrupt,
225669a5db4SJeff Garzik 	.irq_clear	= ata_bmdma_irq_clear,
226669a5db4SJeff Garzik 
227669a5db4SJeff Garzik 	.port_start	= ata_port_start,
228669a5db4SJeff Garzik 	.port_stop	= ata_port_stop,
229669a5db4SJeff Garzik 	.host_stop	= ata_host_stop
230669a5db4SJeff Garzik };
231669a5db4SJeff Garzik 
232669a5db4SJeff Garzik static int triflex_init_one(struct pci_dev *dev, const struct pci_device_id *id)
233669a5db4SJeff Garzik {
234669a5db4SJeff Garzik 	static struct ata_port_info info = {
235669a5db4SJeff Garzik 		.sht = &triflex_sht,
236669a5db4SJeff Garzik 		.flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
237669a5db4SJeff Garzik 		.pio_mask = 0x1f,
238669a5db4SJeff Garzik 		.mwdma_mask = 0x07,
239669a5db4SJeff Garzik 		.port_ops = &triflex_port_ops
240669a5db4SJeff Garzik 	};
241669a5db4SJeff Garzik 	static struct ata_port_info *port_info[2] = { &info, &info };
242669a5db4SJeff Garzik 	static int printed_version;
243669a5db4SJeff Garzik 
244669a5db4SJeff Garzik 	if (!printed_version++)
245669a5db4SJeff Garzik 		dev_printk(KERN_DEBUG, &dev->dev, "version " DRV_VERSION "\n");
246669a5db4SJeff Garzik 
247669a5db4SJeff Garzik 	return ata_pci_init_one(dev, port_info, 2);
248669a5db4SJeff Garzik }
249669a5db4SJeff Garzik 
250669a5db4SJeff Garzik static const struct pci_device_id triflex[] = {
251*2d2744fcSJeff Garzik 	{ PCI_VDEVICE(COMPAQ, PCI_DEVICE_ID_COMPAQ_TRIFLEX_IDE), },
252*2d2744fcSJeff Garzik 
253*2d2744fcSJeff Garzik 	{ },
254669a5db4SJeff Garzik };
255669a5db4SJeff Garzik 
256669a5db4SJeff Garzik static struct pci_driver triflex_pci_driver = {
257669a5db4SJeff Garzik 	.name 		= DRV_NAME,
258669a5db4SJeff Garzik 	.id_table	= triflex,
259669a5db4SJeff Garzik 	.probe 		= triflex_init_one,
260669a5db4SJeff Garzik 	.remove		= ata_pci_remove_one
261669a5db4SJeff Garzik };
262669a5db4SJeff Garzik 
263669a5db4SJeff Garzik static int __init triflex_init(void)
264669a5db4SJeff Garzik {
265669a5db4SJeff Garzik 	return pci_register_driver(&triflex_pci_driver);
266669a5db4SJeff Garzik }
267669a5db4SJeff Garzik 
268669a5db4SJeff Garzik static void __exit triflex_exit(void)
269669a5db4SJeff Garzik {
270669a5db4SJeff Garzik 	pci_unregister_driver(&triflex_pci_driver);
271669a5db4SJeff Garzik }
272669a5db4SJeff Garzik 
273669a5db4SJeff Garzik MODULE_AUTHOR("Alan Cox");
274669a5db4SJeff Garzik MODULE_DESCRIPTION("low-level driver for Compaq Triflex");
275669a5db4SJeff Garzik MODULE_LICENSE("GPL");
276669a5db4SJeff Garzik MODULE_DEVICE_TABLE(pci, triflex);
277669a5db4SJeff Garzik MODULE_VERSION(DRV_VERSION);
278669a5db4SJeff Garzik 
279669a5db4SJeff Garzik module_init(triflex_init);
280669a5db4SJeff Garzik module_exit(triflex_exit);
281