xref: /openbmc/linux/drivers/ata/pata_sc1200.c (revision 360ff7833098e944e5003618b03894251e937802)
1669a5db4SJeff Garzik /*
2ab771630SAlan Cox  * New ATA layer SC1200 driver		Alan Cox <alan@lxorguk.ukuu.org.uk>
3669a5db4SJeff Garzik  *
4669a5db4SJeff Garzik  * TODO: Mode selection filtering
5669a5db4SJeff Garzik  * TODO: Needs custom DMA cleanup code
6669a5db4SJeff Garzik  *
7669a5db4SJeff Garzik  * Based very heavily on
8669a5db4SJeff Garzik  *
9669a5db4SJeff Garzik  * linux/drivers/ide/pci/sc1200.c		Version 0.91	28-Jan-2003
10669a5db4SJeff Garzik  *
11669a5db4SJeff Garzik  * Copyright (C) 2000-2002		Mark Lord <mlord@pobox.com>
12669a5db4SJeff Garzik  * May be copied or modified under the terms of the GNU General Public License
13669a5db4SJeff Garzik  *
14669a5db4SJeff Garzik  * Development of this chipset driver was funded
15669a5db4SJeff Garzik  * by the nice folks at National Semiconductor.
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  */
31669a5db4SJeff Garzik 
32669a5db4SJeff Garzik #include <linux/kernel.h>
33669a5db4SJeff Garzik #include <linux/module.h>
34669a5db4SJeff Garzik #include <linux/pci.h>
35669a5db4SJeff Garzik #include <linux/init.h>
36669a5db4SJeff Garzik #include <linux/blkdev.h>
37669a5db4SJeff Garzik #include <linux/delay.h>
38669a5db4SJeff Garzik #include <scsi/scsi_host.h>
39669a5db4SJeff Garzik #include <linux/libata.h>
40669a5db4SJeff Garzik 
41669a5db4SJeff Garzik #define DRV_NAME	"sc1200"
422a3103ceSJeff Garzik #define DRV_VERSION	"0.2.6"
43669a5db4SJeff Garzik 
44669a5db4SJeff Garzik #define SC1200_REV_A	0x00
45669a5db4SJeff Garzik #define SC1200_REV_B1	0x01
46669a5db4SJeff Garzik #define SC1200_REV_B3	0x02
47669a5db4SJeff Garzik #define SC1200_REV_C1	0x03
48669a5db4SJeff Garzik #define SC1200_REV_D1	0x04
49669a5db4SJeff Garzik 
50669a5db4SJeff Garzik /**
51669a5db4SJeff Garzik  *	sc1200_clock	-	PCI clock
52669a5db4SJeff Garzik  *
53669a5db4SJeff Garzik  *	Return the PCI bus clocking for the SC1200 chipset configuration
54669a5db4SJeff Garzik  *	in use. We return 0 for 33MHz 1 for 48MHz and 2 for 66Mhz
55669a5db4SJeff Garzik  */
56669a5db4SJeff Garzik 
57669a5db4SJeff Garzik static int sc1200_clock(void)
58669a5db4SJeff Garzik {
59669a5db4SJeff Garzik 	/* Magic registers that give us the chipset data */
60669a5db4SJeff Garzik 	u8 chip_id = inb(0x903C);
61669a5db4SJeff Garzik 	u8 silicon_rev = inb(0x903D);
62669a5db4SJeff Garzik 	u16 pci_clock;
63669a5db4SJeff Garzik 
64669a5db4SJeff Garzik 	if (chip_id == 0x04 && silicon_rev < SC1200_REV_B1)
65669a5db4SJeff Garzik 		return 0;	/* 33 MHz mode */
66669a5db4SJeff Garzik 
67669a5db4SJeff Garzik 	/* Clock generator configuration 0x901E its 8/9 are the PCI clocking
68669a5db4SJeff Garzik 	   0/3 is 33Mhz 1 is 48 2 is 66 */
69669a5db4SJeff Garzik 
70669a5db4SJeff Garzik 	pci_clock = inw(0x901E);
71669a5db4SJeff Garzik 	pci_clock >>= 8;
72669a5db4SJeff Garzik 	pci_clock &= 0x03;
73669a5db4SJeff Garzik 	if (pci_clock == 3)
74669a5db4SJeff Garzik 		pci_clock = 0;
75669a5db4SJeff Garzik 	return pci_clock;
76669a5db4SJeff Garzik }
77669a5db4SJeff Garzik 
78669a5db4SJeff Garzik /**
79669a5db4SJeff Garzik  *	sc1200_set_piomode		-	PIO setup
80669a5db4SJeff Garzik  *	@ap: ATA interface
81669a5db4SJeff Garzik  *	@adev: device on the interface
82669a5db4SJeff Garzik  *
83669a5db4SJeff Garzik  *	Set our PIO requirements. This is fairly simple on the SC1200
84669a5db4SJeff Garzik  */
85669a5db4SJeff Garzik 
86669a5db4SJeff Garzik static void sc1200_set_piomode(struct ata_port *ap, struct ata_device *adev)
87669a5db4SJeff Garzik {
88669a5db4SJeff Garzik 	static const u32 pio_timings[4][5] = {
89669a5db4SJeff Garzik 		{0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010},	// format0  33Mhz
90669a5db4SJeff Garzik 		{0xd1329172, 0x71212171, 0x30200080, 0x20102010, 0x00100010},	// format1, 33Mhz
91669a5db4SJeff Garzik 		{0xfaa3f4f3, 0xc23232b2, 0x513101c1, 0x31213121, 0x10211021},	// format1, 48Mhz
92669a5db4SJeff Garzik 		{0xfff4fff4, 0xf35353d3, 0x814102f1, 0x42314231, 0x11311131}	// format1, 66Mhz
93669a5db4SJeff Garzik 	};
94669a5db4SJeff Garzik 
95669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
96669a5db4SJeff Garzik 	u32 format;
97669a5db4SJeff Garzik 	unsigned int reg = 0x40 + 0x10 * ap->port_no;
98669a5db4SJeff Garzik 	int mode = adev->pio_mode - XFER_PIO_0;
99669a5db4SJeff Garzik 
100669a5db4SJeff Garzik 	pci_read_config_dword(pdev, reg + 4, &format);
101669a5db4SJeff Garzik 	format >>= 31;
102669a5db4SJeff Garzik 	format += sc1200_clock();
103669a5db4SJeff Garzik 	pci_write_config_dword(pdev, reg + 8 * adev->devno,
104669a5db4SJeff Garzik 				pio_timings[format][mode]);
105669a5db4SJeff Garzik }
106669a5db4SJeff Garzik 
107669a5db4SJeff Garzik /**
108669a5db4SJeff Garzik  *	sc1200_set_dmamode		-	DMA timing setup
109669a5db4SJeff Garzik  *	@ap: ATA interface
110669a5db4SJeff Garzik  *	@adev: Device being configured
111669a5db4SJeff Garzik  *
112669a5db4SJeff Garzik  *	We cannot mix MWDMA and UDMA without reloading timings each switch
113669a5db4SJeff Garzik  *	master to slave.
114669a5db4SJeff Garzik  */
115669a5db4SJeff Garzik 
116669a5db4SJeff Garzik static void sc1200_set_dmamode(struct ata_port *ap, struct ata_device *adev)
117669a5db4SJeff Garzik {
118669a5db4SJeff Garzik 	static const u32 udma_timing[3][3] = {
119669a5db4SJeff Garzik 		{ 0x00921250, 0x00911140, 0x00911030 },
120669a5db4SJeff Garzik 		{ 0x00932470, 0x00922260, 0x00922140 },
121669a5db4SJeff Garzik 		{ 0x009436A1, 0x00933481, 0x00923261 }
122669a5db4SJeff Garzik 	};
123669a5db4SJeff Garzik 
124669a5db4SJeff Garzik 	static const u32 mwdma_timing[3][3] = {
125669a5db4SJeff Garzik 		{ 0x00077771, 0x00012121, 0x00002020 },
126669a5db4SJeff Garzik 		{ 0x000BBBB2, 0x00024241, 0x00013131 },
127669a5db4SJeff Garzik 		{ 0x000FFFF3, 0x00035352, 0x00015151 }
128669a5db4SJeff Garzik 	};
129669a5db4SJeff Garzik 
130669a5db4SJeff Garzik 	int clock = sc1200_clock();
131669a5db4SJeff Garzik 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
132669a5db4SJeff Garzik 	unsigned int reg = 0x40 + 0x10 * ap->port_no;
133669a5db4SJeff Garzik 	int mode = adev->dma_mode;
134669a5db4SJeff Garzik 	u32 format;
135669a5db4SJeff Garzik 
136669a5db4SJeff Garzik 	if (mode >= XFER_UDMA_0)
137669a5db4SJeff Garzik 		format = udma_timing[clock][mode - XFER_UDMA_0];
138669a5db4SJeff Garzik 	else
139669a5db4SJeff Garzik 		format = mwdma_timing[clock][mode - XFER_MW_DMA_0];
140669a5db4SJeff Garzik 
141669a5db4SJeff Garzik 	if (adev->devno == 0) {
142669a5db4SJeff Garzik 		u32 timings;
143669a5db4SJeff Garzik 
144669a5db4SJeff Garzik 		pci_read_config_dword(pdev, reg + 4, &timings);
145669a5db4SJeff Garzik 		timings &= 0x80000000UL;
146669a5db4SJeff Garzik 		timings |= format;
147669a5db4SJeff Garzik 		pci_write_config_dword(pdev, reg + 4, timings);
148669a5db4SJeff Garzik 	} else
149669a5db4SJeff Garzik 		pci_write_config_dword(pdev, reg + 12, format);
150669a5db4SJeff Garzik }
151669a5db4SJeff Garzik 
152669a5db4SJeff Garzik /**
1539363c382STejun Heo  *	sc1200_qc_issue		-	command issue
154669a5db4SJeff Garzik  *	@qc: command pending
155669a5db4SJeff Garzik  *
156669a5db4SJeff Garzik  *	Called when the libata layer is about to issue a command. We wrap
157669a5db4SJeff Garzik  *	this interface so that we can load the correct ATA timings if
1583a4fa0a2SRobert P. J. Day  *	necessary.  Specifically we have a problem that there is only
159669a5db4SJeff Garzik  *	one MWDMA/UDMA bit.
160669a5db4SJeff Garzik  */
161669a5db4SJeff Garzik 
1629363c382STejun Heo static unsigned int sc1200_qc_issue(struct ata_queued_cmd *qc)
163669a5db4SJeff Garzik {
164669a5db4SJeff Garzik 	struct ata_port *ap = qc->ap;
165669a5db4SJeff Garzik 	struct ata_device *adev = qc->dev;
166669a5db4SJeff Garzik 	struct ata_device *prev = ap->private_data;
167669a5db4SJeff Garzik 
168669a5db4SJeff Garzik 	/* See if the DMA settings could be wrong */
169b15b3ebaSAlan Cox 	if (ata_dma_enabled(adev) && adev != prev && prev != NULL) {
170669a5db4SJeff Garzik 		/* Maybe, but do the channels match MWDMA/UDMA ? */
171b15b3ebaSAlan Cox 		if ((ata_using_udma(adev) && !ata_using_udma(prev)) ||
172b15b3ebaSAlan Cox 		    (ata_using_udma(prev) && !ata_using_udma(adev)))
173669a5db4SJeff Garzik 		    	/* Switch the mode bits */
174669a5db4SJeff Garzik 		    	sc1200_set_dmamode(ap, adev);
175669a5db4SJeff Garzik 	}
176669a5db4SJeff Garzik 
177*360ff783STejun Heo 	return ata_bmdma_qc_issue(qc);
178669a5db4SJeff Garzik }
179669a5db4SJeff Garzik 
180c0f2ee34SAlan Cox /**
181c0f2ee34SAlan Cox  *	sc1200_qc_defer	-	implement serialization
182c0f2ee34SAlan Cox  *	@qc: command
183c0f2ee34SAlan Cox  *
184c0f2ee34SAlan Cox  *	Serialize command issue on this controller.
185c0f2ee34SAlan Cox  */
186c0f2ee34SAlan Cox 
187c0f2ee34SAlan Cox static int sc1200_qc_defer(struct ata_queued_cmd *qc)
188c0f2ee34SAlan Cox {
189c0f2ee34SAlan Cox 	struct ata_host *host = qc->ap->host;
190c0f2ee34SAlan Cox 	struct ata_port *alt = host->ports[1 ^ qc->ap->port_no];
191c0f2ee34SAlan Cox 	int rc;
192c0f2ee34SAlan Cox 
193c0f2ee34SAlan Cox 	/* First apply the usual rules */
194c0f2ee34SAlan Cox 	rc = ata_std_qc_defer(qc);
195c0f2ee34SAlan Cox 	if (rc != 0)
196c0f2ee34SAlan Cox 		return rc;
197c0f2ee34SAlan Cox 
198c0f2ee34SAlan Cox 	/* Now apply serialization rules. Only allow a command if the
199c0f2ee34SAlan Cox 	   other channel state machine is idle */
200c0f2ee34SAlan Cox 	if (alt && alt->qc_active)
201c0f2ee34SAlan Cox 		return	ATA_DEFER_PORT;
202c0f2ee34SAlan Cox 	return 0;
203c0f2ee34SAlan Cox }
204c0f2ee34SAlan Cox 
205669a5db4SJeff Garzik static struct scsi_host_template sc1200_sht = {
20668d1d07bSTejun Heo 	ATA_BMDMA_SHT(DRV_NAME),
207d26fc955SAlan Cox 	.sg_tablesize	= LIBATA_DUMB_MAX_PRD,
208669a5db4SJeff Garzik };
209669a5db4SJeff Garzik 
210669a5db4SJeff Garzik static struct ata_port_operations sc1200_port_ops = {
211029cfd6bSTejun Heo 	.inherits	= &ata_bmdma_port_ops,
212f47451c4STejun Heo 	.qc_prep 	= ata_bmdma_dumb_qc_prep,
2139363c382STejun Heo 	.qc_issue	= sc1200_qc_issue,
214c0f2ee34SAlan Cox 	.qc_defer	= sc1200_qc_defer,
215029cfd6bSTejun Heo 	.cable_detect	= ata_cable_40wire,
216029cfd6bSTejun Heo 	.set_piomode	= sc1200_set_piomode,
217029cfd6bSTejun Heo 	.set_dmamode	= sc1200_set_dmamode,
218669a5db4SJeff Garzik };
219669a5db4SJeff Garzik 
220669a5db4SJeff Garzik /**
221669a5db4SJeff Garzik  *	sc1200_init_one		-	Initialise an SC1200
222669a5db4SJeff Garzik  *	@dev: PCI device
223669a5db4SJeff Garzik  *	@id: Entry in match table
224669a5db4SJeff Garzik  *
225669a5db4SJeff Garzik  *	Just throw the needed data at the libata helper and it does all
226669a5db4SJeff Garzik  *	our work.
227669a5db4SJeff Garzik  */
228669a5db4SJeff Garzik 
229669a5db4SJeff Garzik static int sc1200_init_one(struct pci_dev *dev, const struct pci_device_id *id)
230669a5db4SJeff Garzik {
2311626aeb8STejun Heo 	static const struct ata_port_info info = {
2321d2808fdSJeff Garzik 		.flags = ATA_FLAG_SLAVE_POSS,
23314bdef98SErik Inge Bolsø 		.pio_mask = ATA_PIO4,
23414bdef98SErik Inge Bolsø 		.mwdma_mask = ATA_MWDMA2,
23514bdef98SErik Inge Bolsø 		.udma_mask = ATA_UDMA2,
236669a5db4SJeff Garzik 		.port_ops = &sc1200_port_ops
237669a5db4SJeff Garzik 	};
2386d4f950eSAlan Cox 	const struct ata_port_info *ppi[] = { &info, NULL };
2391626aeb8STejun Heo 
24016ea0fc9SAlan Cox 	return ata_pci_sff_init_one(dev, ppi, &sc1200_sht, NULL, 0);
241669a5db4SJeff Garzik }
242669a5db4SJeff Garzik 
2432d2744fcSJeff Garzik static const struct pci_device_id sc1200[] = {
2442d2744fcSJeff Garzik 	{ PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_SCx200_IDE), },
2452d2744fcSJeff Garzik 
2462d2744fcSJeff Garzik 	{ },
247669a5db4SJeff Garzik };
248669a5db4SJeff Garzik 
249669a5db4SJeff Garzik static struct pci_driver sc1200_pci_driver = {
250669a5db4SJeff Garzik 	.name 		= DRV_NAME,
251669a5db4SJeff Garzik 	.id_table	= sc1200,
252669a5db4SJeff Garzik 	.probe 		= sc1200_init_one,
25330ced0f0SAlan 	.remove		= ata_pci_remove_one,
254438ac6d5STejun Heo #ifdef CONFIG_PM
25530ced0f0SAlan 	.suspend	= ata_pci_device_suspend,
25630ced0f0SAlan 	.resume		= ata_pci_device_resume,
257438ac6d5STejun Heo #endif
258669a5db4SJeff Garzik };
259669a5db4SJeff Garzik 
260669a5db4SJeff Garzik static int __init sc1200_init(void)
261669a5db4SJeff Garzik {
262669a5db4SJeff Garzik 	return pci_register_driver(&sc1200_pci_driver);
263669a5db4SJeff Garzik }
264669a5db4SJeff Garzik 
265669a5db4SJeff Garzik static void __exit sc1200_exit(void)
266669a5db4SJeff Garzik {
267669a5db4SJeff Garzik 	pci_unregister_driver(&sc1200_pci_driver);
268669a5db4SJeff Garzik }
269669a5db4SJeff Garzik 
270669a5db4SJeff Garzik MODULE_AUTHOR("Alan Cox, Mark Lord");
271669a5db4SJeff Garzik MODULE_DESCRIPTION("low-level driver for the NS/AMD SC1200");
272669a5db4SJeff Garzik MODULE_LICENSE("GPL");
273669a5db4SJeff Garzik MODULE_DEVICE_TABLE(pci, sc1200);
274669a5db4SJeff Garzik MODULE_VERSION(DRV_VERSION);
275669a5db4SJeff Garzik 
276669a5db4SJeff Garzik module_init(sc1200_init);
277669a5db4SJeff Garzik module_exit(sc1200_exit);
278